ide-generic: use ide_find_port() (take 3)
[safe/jmp/linux-2.6] / drivers / ide / ide-generic.c
1 /*
2  * generic/default IDE host driver
3  *
4  * Copyright (C) 2004, 2008 Bartlomiej Zolnierkiewicz
5  * This code was split off from ide.c.  See it for original copyrights.
6  *
7  * May be copied or modified under the terms of the GNU General Public License.
8  */
9
10 /*
11  * For special cases new interfaces may be added using sysfs, i.e.
12  *
13  *      echo -n "0x168:0x36e:10" > /sys/class/ide_generic/add
14  *
15  * will add an interface using I/O ports 0x168-0x16f/0x36e and IRQ 10.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/ide.h>
22
23 #define DRV_NAME        "ide_generic"
24
25 static ssize_t store_add(struct class *cls, const char *buf, size_t n)
26 {
27         ide_hwif_t *hwif;
28         unsigned int base, ctl;
29         int irq;
30         hw_regs_t hw;
31         u8 idx[] = { 0xff, 0xff, 0xff, 0xff };
32
33         if (sscanf(buf, "%x:%x:%d", &base, &ctl, &irq) != 3)
34                 return -EINVAL;
35
36         hwif = ide_find_port();
37         if (hwif == NULL)
38                 return -ENOENT;
39
40         memset(&hw, 0, sizeof(hw));
41         ide_std_init_ports(&hw, base, ctl);
42         hw.irq = irq;
43         hw.chipset = ide_generic;
44
45         ide_init_port_hw(hwif, &hw);
46
47         idx[0] = hwif->index;
48
49         ide_device_add(idx, NULL);
50
51         return n;
52 };
53
54 static struct class_attribute ide_generic_class_attrs[] = {
55         __ATTR(add, S_IWUSR, NULL, store_add),
56         __ATTR_NULL
57 };
58
59 static void ide_generic_class_release(struct class *cls)
60 {
61         kfree(cls);
62 }
63
64 static int __init ide_generic_sysfs_init(void)
65 {
66         struct class *cls;
67         int rc;
68
69         cls = kzalloc(sizeof(*cls), GFP_KERNEL);
70         if (!cls)
71                 return -ENOMEM;
72
73         cls->name = DRV_NAME;
74         cls->owner = THIS_MODULE;
75         cls->class_release = ide_generic_class_release;
76         cls->class_attrs = ide_generic_class_attrs;
77
78         rc = class_register(cls);
79         if (rc) {
80                 kfree(cls);
81                 return rc;
82         }
83
84         return 0;
85 }
86
87 static int __init ide_generic_init(void)
88 {
89         u8 idx[MAX_HWIFS];
90         int i;
91
92         for (i = 0; i < MAX_HWIFS; i++) {
93                 ide_hwif_t *hwif;
94                 unsigned long io_addr = ide_default_io_base(i);
95                 hw_regs_t hw;
96
97                 if (io_addr) {
98                         /*
99                          * Skip probing if the corresponding
100                          * slot is already occupied.
101                          */
102                         hwif = ide_find_port();
103                         if (hwif == NULL || hwif->index != i) {
104                                 idx[i] = 0xff;
105                                 continue;
106                         }
107
108                         memset(&hw, 0, sizeof(hw));
109                         ide_std_init_ports(&hw, io_addr, io_addr + 0x206);
110                         hw.irq = ide_default_irq(io_addr);
111                         ide_init_port_hw(hwif, &hw);
112
113                         idx[i] = i;
114                 } else
115                         idx[i] = 0xff;
116         }
117
118         ide_device_add_all(idx, NULL);
119
120         if (ide_generic_sysfs_init())
121                 printk(KERN_ERR DRV_NAME ": failed to create ide_generic "
122                                          "class\n");
123
124         return 0;
125 }
126
127 module_init(ide_generic_init);
128
129 MODULE_LICENSE("GPL");