pcmcia: introduce autoconfiguration feature
[linux-2.6.git] / drivers / usb / host / sl811_cs.c
1 /*
2  * PCMCIA driver for SL811HS (as found in REX-CFU1U)
3  * Filename: sl811_cs.c
4  * Author:   Yukio Yamamoto
5  *
6  *  Port to sl811-hcd and 2.6.x by
7  *    Botond Botyanszki <boti@rocketmail.com>
8  *    Simon Pickering
9  *
10  *  Last update: 2005-05-12
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/ptrace.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/timer.h>
20 #include <linux/ioport.h>
21 #include <linux/platform_device.h>
22
23 #include <pcmcia/cistpl.h>
24 #include <pcmcia/cisreg.h>
25 #include <pcmcia/ds.h>
26
27 #include <linux/usb/sl811.h>
28
29 MODULE_AUTHOR("Botond Botyanszki");
30 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
31 MODULE_LICENSE("GPL");
32
33
34 /*====================================================================*/
35 /* MACROS                                                             */
36 /*====================================================================*/
37
38 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
39
40 /*====================================================================*/
41 /* VARIABLES                                                          */
42 /*====================================================================*/
43
44 typedef struct local_info_t {
45         struct pcmcia_device    *p_dev;
46 } local_info_t;
47
48 static void sl811_cs_release(struct pcmcia_device * link);
49
50 /*====================================================================*/
51
52 static void release_platform_dev(struct device * dev)
53 {
54         dev_dbg(dev, "sl811_cs platform_dev release\n");
55         dev->parent = NULL;
56 }
57
58 static struct sl811_platform_data platform_data = {
59         .potpg          = 100,
60         .power          = 50,           /* == 100mA */
61         // .reset       = ... FIXME:  invoke CF reset on the card
62 };
63
64 static struct resource resources[] = {
65         [0] = {
66                 .flags  = IORESOURCE_IRQ,
67         },
68         [1] = {
69                 // .name   = "address",
70                 .flags  = IORESOURCE_IO,
71         },
72         [2] = {
73                 // .name   = "data",
74                 .flags  = IORESOURCE_IO,
75         },
76 };
77
78 extern struct platform_driver sl811h_driver;
79
80 static struct platform_device platform_dev = {
81         .id                     = -1,
82         .dev = {
83                 .platform_data = &platform_data,
84                 .release       = release_platform_dev,
85         },
86         .resource               = resources,
87         .num_resources          = ARRAY_SIZE(resources),
88 };
89
90 static int sl811_hc_init(struct device *parent, resource_size_t base_addr,
91                          int irq)
92 {
93         if (platform_dev.dev.parent)
94                 return -EBUSY;
95         platform_dev.dev.parent = parent;
96
97         /* finish seting up the platform device */
98         resources[0].start = irq;
99
100         resources[1].start = base_addr;
101         resources[1].end = base_addr;
102
103         resources[2].start = base_addr + 1;
104         resources[2].end   = base_addr + 1;
105
106         /* The driver core will probe for us.  We know sl811-hcd has been
107          * initialized already because of the link order dependency created
108          * by referencing "sl811h_driver".
109          */
110         platform_dev.name = sl811h_driver.driver.name;
111         return platform_device_register(&platform_dev);
112 }
113
114 /*====================================================================*/
115
116 static void sl811_cs_detach(struct pcmcia_device *link)
117 {
118         dev_dbg(&link->dev, "sl811_cs_detach\n");
119
120         sl811_cs_release(link);
121
122         /* This points to the parent local_info_t struct */
123         kfree(link->priv);
124 }
125
126 static void sl811_cs_release(struct pcmcia_device * link)
127 {
128         dev_dbg(&link->dev, "sl811_cs_release\n");
129
130         pcmcia_disable_device(link);
131         platform_device_unregister(&platform_dev);
132 }
133
134 static int sl811_cs_config_check(struct pcmcia_device *p_dev,
135                                  cistpl_cftable_entry_t *cfg,
136                                  cistpl_cftable_entry_t *dflt,
137                                  void *priv_data)
138 {
139         if (cfg->index == 0)
140                 return -ENODEV;
141
142         /* IO window settings */
143         p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
144         if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
145                 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
146                 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
147
148                 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
149                 p_dev->resource[0]->start = io->win[0].base;
150                 p_dev->resource[0]->end = io->win[0].len;
151
152                 return pcmcia_request_io(p_dev);
153         }
154         pcmcia_disable_device(p_dev);
155         return -ENODEV;
156 }
157
158
159 static int sl811_cs_config(struct pcmcia_device *link)
160 {
161         struct device           *parent = &link->dev;
162         int                     ret;
163
164         dev_dbg(&link->dev, "sl811_cs_config\n");
165
166         link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
167                 CONF_AUTO_CHECK_VCC;
168
169         if (pcmcia_loop_config(link, sl811_cs_config_check, NULL))
170                 goto failed;
171
172         /* require an IRQ and two registers */
173         if (resource_size(link->resource[0]) < 2)
174                 goto failed;
175
176         if (!link->irq)
177                 goto failed;
178
179         ret = pcmcia_enable_device(link);
180         if (ret)
181                 goto failed;
182
183         dev_info(&link->dev, "index 0x%02x: ",
184                 link->config_index);
185         if (link->vpp)
186                 printk(", Vpp %d.%d", link->vpp/10, link->vpp%10);
187         printk(", irq %d", link->irq);
188         printk(", io %pR", link->resource[0]);
189         printk("\n");
190
191         if (sl811_hc_init(parent, link->resource[0]->start, link->irq)
192                         < 0) {
193 failed:
194                 printk(KERN_WARNING "sl811_cs_config failed\n");
195                 sl811_cs_release(link);
196                 return  -ENODEV;
197         }
198         return 0;
199 }
200
201 static int sl811_cs_probe(struct pcmcia_device *link)
202 {
203         local_info_t *local;
204
205         local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
206         if (!local)
207                 return -ENOMEM;
208         local->p_dev = link;
209         link->priv = local;
210
211         return sl811_cs_config(link);
212 }
213
214 static struct pcmcia_device_id sl811_ids[] = {
215         PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
216         PCMCIA_DEVICE_NULL,
217 };
218 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
219
220 static struct pcmcia_driver sl811_cs_driver = {
221         .owner          = THIS_MODULE,
222         .drv            = {
223                 .name   = "sl811_cs",
224         },
225         .probe          = sl811_cs_probe,
226         .remove         = sl811_cs_detach,
227         .id_table       = sl811_ids,
228 };
229
230 /*====================================================================*/
231
232 static int __init init_sl811_cs(void)
233 {
234         return pcmcia_register_driver(&sl811_cs_driver);
235 }
236 module_init(init_sl811_cs);
237
238 static void __exit exit_sl811_cs(void)
239 {
240         pcmcia_unregister_driver(&sl811_cs_driver);
241 }
242 module_exit(exit_sl811_cs);