55dfeec6fdb53c7800aaf085824ebbb176dcbfb1
[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/sched.h>
17 #include <linux/ptrace.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/timer.h>
21 #include <linux/ioport.h>
22
23 #include <pcmcia/version.h>
24 #include <pcmcia/cs_types.h>
25 #include <pcmcia/cs.h>
26 #include <pcmcia/cistpl.h>
27 #include <pcmcia/cisreg.h>
28 #include <pcmcia/ds.h>
29
30 #include <linux/usb_sl811.h>
31
32 MODULE_AUTHOR("Botond Botyanszki");
33 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
34 MODULE_LICENSE("GPL");
35
36
37 /*====================================================================*/
38 /* MACROS                                                             */
39 /*====================================================================*/
40
41 #if defined(DEBUG) || defined(CONFIG_USB_DEBUG) || defined(PCMCIA_DEBUG)
42
43 static int pc_debug = 0;
44 module_param(pc_debug, int, 0644);
45
46 #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
47
48 #else
49 #define DBG(n, args...) do{}while(0)
50 #endif  /* no debugging */
51
52 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
53
54 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
55
56 #define CS_CHECK(fn, ret) \
57         do { \
58                 last_fn = (fn); \
59                 if ((last_ret = (ret)) != 0) \
60                         goto cs_failed; \
61         } while (0)
62
63 /*====================================================================*/
64 /* VARIABLES                                                          */
65 /*====================================================================*/
66
67 static const char driver_name[DEV_NAME_LEN]  = "sl811_cs";
68
69 static dev_link_t *dev_list = NULL;
70
71 typedef struct local_info_t {
72         dev_link_t              link;
73         dev_node_t              node;
74 } local_info_t;
75
76 /*====================================================================*/
77
78 static void release_platform_dev(struct device * dev)
79 {
80         DBG(0, "sl811_cs platform_dev release\n");
81         dev->parent = NULL;
82 }
83
84 static struct sl811_platform_data platform_data = {
85         .potpg          = 100,
86         .power          = 50,           /* == 100mA */
87         // .reset       = ... FIXME:  invoke CF reset on the card
88 };
89
90 static struct resource resources[] = {
91         [0] = {
92                 .flags  = IORESOURCE_IRQ,
93         },
94         [1] = {
95                 // .name   = "address",
96                 .flags  = IORESOURCE_IO,
97         },
98         [2] = {
99                 // .name   = "data",
100                 .flags  = IORESOURCE_IO,
101         },
102 };
103
104 extern struct device_driver sl811h_driver;
105
106 static struct platform_device platform_dev = {
107         .id                     = -1,
108         .dev = {
109                 .platform_data = &platform_data,
110                 .release       = release_platform_dev,
111         },
112         .resource               = resources,
113         .num_resources          = ARRAY_SIZE(resources),
114 };
115
116 static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
117 {
118         if (platform_dev.dev.parent)
119                 return -EBUSY;
120         platform_dev.dev.parent = parent;
121
122         /* finish seting up the platform device */
123         resources[0].start = irq;
124
125         resources[1].start = base_addr;
126         resources[1].end = base_addr;
127
128         resources[2].start = base_addr + 1;
129         resources[2].end   = base_addr + 1;
130
131         /* The driver core will probe for us.  We know sl811-hcd has been
132          * initialized already because of the link order dependency.
133          */
134         platform_dev.name = sl811h_driver.name;
135         return platform_device_register(&platform_dev);
136 }
137
138 /*====================================================================*/
139
140 static void sl811_cs_detach(dev_link_t *link)
141 {
142         dev_link_t **linkp;
143
144         DBG(0, "sl811_cs_detach(0x%p)\n", link);
145
146         /* Locate device structure */
147         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
148                 if (*linkp == link)
149                         break;
150         }
151         if (*linkp == NULL)
152                 return;
153
154         /* Break the link with Card Services */
155         if (link->handle)
156                 pcmcia_deregister_client(link->handle);
157
158         /* Unlink device structure, and free it */
159         *linkp = link->next;
160         /* This points to the parent local_info_t struct */
161         kfree(link->priv);
162 }
163
164 static void sl811_cs_release(dev_link_t * link)
165 {
166
167         DBG(0, "sl811_cs_release(0x%p)\n", link);
168
169         if (link->open) {
170                 DBG(1, "sl811_cs: release postponed, '%s' still open\n",
171                     link->dev->dev_name);
172                 link->state |= DEV_STALE_CONFIG;
173                 return;
174         }
175
176         /* Unlink the device chain */
177         link->dev = NULL;
178
179         platform_device_unregister(&platform_dev);
180         pcmcia_release_configuration(link->handle);
181         if (link->io.NumPorts1)
182                 pcmcia_release_io(link->handle, &link->io);
183         if (link->irq.AssignedIRQ)
184                 pcmcia_release_irq(link->handle, &link->irq);
185         link->state &= ~DEV_CONFIG;
186
187         if (link->state & DEV_STALE_LINK)
188                 sl811_cs_detach(link);
189 }
190
191 static void sl811_cs_config(dev_link_t *link)
192 {
193         client_handle_t         handle = link->handle;
194         struct device           *parent = &handle_to_dev(handle);
195         local_info_t            *dev = link->priv;
196         tuple_t                 tuple;
197         cisparse_t              parse;
198         int                     last_fn, last_ret;
199         u_char                  buf[64];
200         config_info_t           conf;
201         cistpl_cftable_entry_t  dflt = { 0 };
202
203         DBG(0, "sl811_cs_config(0x%p)\n", link);
204
205         tuple.DesiredTuple = CISTPL_CONFIG;
206         tuple.Attributes = 0;
207         tuple.TupleData = buf;
208         tuple.TupleDataMax = sizeof(buf);
209         tuple.TupleOffset = 0;
210         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
211         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
212         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
213         link->conf.ConfigBase = parse.config.base;
214         link->conf.Present = parse.config.rmask[0];
215
216         /* Configure card */
217         link->state |= DEV_CONFIG;
218
219         /* Look up the current Vcc */
220         CS_CHECK(GetConfigurationInfo,
221                         pcmcia_get_configuration_info(handle, &conf));
222         link->conf.Vcc = conf.Vcc;
223
224         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
225         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
226         while (1) {
227                 cistpl_cftable_entry_t  *cfg = &(parse.cftable_entry);
228
229                 if (pcmcia_get_tuple_data(handle, &tuple) != 0
230                                 || pcmcia_parse_tuple(handle, &tuple, &parse)
231                                                 != 0)
232                         goto next_entry;
233
234                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
235                         dflt = *cfg;
236                 }
237
238                 if (cfg->index == 0)
239                         goto next_entry;
240
241                 link->conf.ConfigIndex = cfg->index;
242
243                 /* Use power settings for Vcc and Vpp if present */
244                 /*  Note that the CIS values need to be rescaled */
245                 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
246                         if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
247                                         != conf.Vcc)
248                                 goto next_entry;
249                 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
250                         if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
251                                         != conf.Vcc)
252                                 goto next_entry;
253                 }
254
255                 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
256                         link->conf.Vpp1 = link->conf.Vpp2 =
257                                 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
258                 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
259                         link->conf.Vpp1 = link->conf.Vpp2 =
260                                 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
261
262                 /* we need an interrupt */
263                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
264                         link->conf.Attributes |= CONF_ENABLE_IRQ;
265
266                 /* IO window settings */
267                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
268                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
269                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
270
271                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
272                         link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
273                         link->io.BasePort1 = io->win[0].base;
274                         link->io.NumPorts1 = io->win[0].len;
275
276                         if (pcmcia_request_io(link->handle, &link->io) != 0)
277                                 goto next_entry;
278                 }
279                 break;
280
281 next_entry:
282                 if (link->io.NumPorts1)
283                         pcmcia_release_io(link->handle, &link->io);
284                 last_ret = pcmcia_get_next_tuple(handle, &tuple);
285         }
286
287         /* require an IRQ and two registers */
288         if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
289                 goto cs_failed;
290         if (link->conf.Attributes & CONF_ENABLE_IRQ)
291                 CS_CHECK(RequestIRQ,
292                         pcmcia_request_irq(link->handle, &link->irq));
293         else
294                 goto cs_failed;
295
296         CS_CHECK(RequestConfiguration,
297                 pcmcia_request_configuration(link->handle, &link->conf));
298
299         sprintf(dev->node.dev_name, driver_name);
300         dev->node.major = dev->node.minor = 0;
301         link->dev = &dev->node;
302
303         printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
304                dev->node.dev_name, link->conf.ConfigIndex,
305                link->conf.Vcc/10, link->conf.Vcc%10);
306         if (link->conf.Vpp1)
307                 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
308         printk(", irq %d", link->irq.AssignedIRQ);
309         printk(", io 0x%04x-0x%04x", link->io.BasePort1,
310                link->io.BasePort1+link->io.NumPorts1-1);
311         printk("\n");
312
313         link->state &= ~DEV_CONFIG_PENDING;
314
315         if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
316                         < 0) {
317 cs_failed:
318                 printk("sl811_cs_config failed\n");
319                 cs_error(link->handle, last_fn, last_ret);
320                 sl811_cs_release(link);
321                 link->state &= ~DEV_CONFIG_PENDING;
322         }
323 }
324
325 static int
326 sl811_cs_event(event_t event, int priority, event_callback_args_t *args)
327 {
328         dev_link_t *link = args->client_data;
329
330         DBG(1, "sl811_cs_event(0x%06x)\n", event);
331
332         switch (event) {
333         case CS_EVENT_CARD_REMOVAL:
334                 link->state &= ~DEV_PRESENT;
335                 if (link->state & DEV_CONFIG)
336                         sl811_cs_release(link);
337                 break;
338
339         case CS_EVENT_CARD_INSERTION:
340                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
341                 sl811_cs_config(link);
342                 break;
343
344         case CS_EVENT_PM_SUSPEND:
345                 link->state |= DEV_SUSPEND;
346                 /* Fall through... */
347         case CS_EVENT_RESET_PHYSICAL:
348                 if (link->state & DEV_CONFIG)
349                         pcmcia_release_configuration(link->handle);
350                 break;
351
352         case CS_EVENT_PM_RESUME:
353                 link->state &= ~DEV_SUSPEND;
354                 /* Fall through... */
355         case CS_EVENT_CARD_RESET:
356                 if (link->state & DEV_CONFIG)
357                         pcmcia_request_configuration(link->handle, &link->conf);
358                 DBG(0, "reset sl811-hcd here?\n");
359                 break;
360         }
361         return 0;
362 }
363
364 static dev_link_t *sl811_cs_attach(void)
365 {
366         local_info_t *local;
367         dev_link_t *link;
368         client_reg_t client_reg;
369         int ret;
370
371         local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
372         if (!local)
373                 return NULL;
374         memset(local, 0, sizeof(local_info_t));
375         link = &local->link;
376         link->priv = local;
377
378         /* Initialize */
379         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
380         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
381         link->irq.Handler = NULL;
382
383         link->conf.Attributes = 0;
384         link->conf.Vcc = 33;
385         link->conf.IntType = INT_MEMORY_AND_IO;
386
387         /* Register with Card Services */
388         link->next = dev_list;
389         dev_list = link;
390         client_reg.dev_info = (dev_info_t *) &driver_name;
391         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
392         client_reg.Version = 0x0210;
393         client_reg.event_callback_args.client_data = link;
394         ret = pcmcia_register_client(&link->handle, &client_reg);
395         if (ret != CS_SUCCESS) {
396                 cs_error(link->handle, RegisterClient, ret);
397                 sl811_cs_detach(link);
398                 return NULL;
399         }
400
401         return link;
402 }
403
404 static struct pcmcia_device_id sl811_ids[] = {
405         PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
406         PCMCIA_DEVICE_NULL,
407 };
408 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
409
410 static struct pcmcia_driver sl811_cs_driver = {
411         .owner          = THIS_MODULE,
412         .drv            = {
413                 .name   = (char *)driver_name,
414         },
415         .attach         = sl811_cs_attach,
416         .event          = sl811_cs_event,
417         .detach         = sl811_cs_detach,
418         .id_table       = sl811_ids,
419 };
420
421 /*====================================================================*/
422
423 static int __init init_sl811_cs(void)
424 {
425         return pcmcia_register_driver(&sl811_cs_driver);
426 }
427 module_init(init_sl811_cs);
428
429 static void __exit exit_sl811_cs(void)
430 {
431         pcmcia_unregister_driver(&sl811_cs_driver);
432 }
433 module_exit(exit_sl811_cs);