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