2 * PCMCIA driver for SL811HS (as found in REX-CFU1U)
4 * Author: Yukio Yamamoto
6 * Port to sl811-hcd and 2.6.x by
7 * Botond Botyanszki <boti@rocketmail.com>
10 * Last update: 2005-05-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>
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>
30 #include <linux/usb_sl811.h>
32 MODULE_AUTHOR("Botond Botyanszki");
33 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
34 MODULE_LICENSE("GPL");
37 /*====================================================================*/
39 /*====================================================================*/
41 #if defined(DEBUG) || defined(CONFIG_USB_DEBUG) || defined(PCMCIA_DEBUG)
43 static int pc_debug = 0;
44 module_param(pc_debug, int, 0644);
46 #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
49 #define DBG(n, args...) do{}while(0)
50 #endif /* no debugging */
52 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
54 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
56 #define CS_CHECK(fn, ret) \
59 if ((last_ret = (ret)) != 0) \
63 /*====================================================================*/
65 /*====================================================================*/
67 static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
69 static dev_link_t *dev_list = NULL;
71 static int irq_list[4] = { -1 };
72 static int irq_list_count;
74 module_param_array(irq_list, int, &irq_list_count, 0444);
76 INT_MODULE_PARM(irq_mask, 0xdeb8);
78 typedef struct local_info_t {
83 /*====================================================================*/
85 static void release_platform_dev(struct device * dev)
87 DBG(0, "sl811_cs platform_dev release\n");
91 static struct sl811_platform_data platform_data = {
93 .power = 50, /* == 100mA */
94 // .reset = ... FIXME: invoke CF reset on the card
97 static struct resource resources[] = {
99 .flags = IORESOURCE_IRQ,
102 // .name = "address",
103 .flags = IORESOURCE_IO,
107 .flags = IORESOURCE_IO,
111 extern struct device_driver sl811h_driver;
113 static struct platform_device platform_dev = {
116 .platform_data = &platform_data,
117 .release = release_platform_dev,
119 .resource = resources,
120 .num_resources = ARRAY_SIZE(resources),
123 static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
125 if (platform_dev.dev.parent)
127 platform_dev.dev.parent = parent;
129 /* finish seting up the platform device */
130 resources[0].start = irq;
132 resources[1].start = base_addr;
133 resources[1].end = base_addr;
135 resources[2].start = base_addr + 1;
136 resources[2].end = base_addr + 1;
138 /* The driver core will probe for us. We know sl811-hcd has been
139 * initialized already because of the link order dependency.
141 platform_dev.name = sl811h_driver.name;
142 return platform_device_register(&platform_dev);
145 /*====================================================================*/
147 static void sl811_cs_detach(dev_link_t *link)
151 DBG(0, "sl811_cs_detach(0x%p)\n", link);
153 /* Locate device structure */
154 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
161 /* Break the link with Card Services */
163 pcmcia_deregister_client(link->handle);
165 /* Unlink device structure, and free it */
167 /* This points to the parent local_info_t struct */
171 static void sl811_cs_release(dev_link_t * link)
174 DBG(0, "sl811_cs_release(0x%p)\n", link);
177 DBG(1, "sl811_cs: release postponed, '%s' still open\n",
178 link->dev->dev_name);
179 link->state |= DEV_STALE_CONFIG;
183 /* Unlink the device chain */
186 platform_device_unregister(&platform_dev);
187 pcmcia_release_configuration(link->handle);
188 if (link->io.NumPorts1)
189 pcmcia_release_io(link->handle, &link->io);
190 if (link->irq.AssignedIRQ)
191 pcmcia_release_irq(link->handle, &link->irq);
192 link->state &= ~DEV_CONFIG;
194 if (link->state & DEV_STALE_LINK)
195 sl811_cs_detach(link);
198 static void sl811_cs_config(dev_link_t *link)
200 client_handle_t handle = link->handle;
201 struct device *parent = &handle_to_dev(handle);
202 local_info_t *dev = link->priv;
205 int last_fn, last_ret;
208 cistpl_cftable_entry_t dflt = { 0 };
210 DBG(0, "sl811_cs_config(0x%p)\n", link);
212 tuple.DesiredTuple = CISTPL_CONFIG;
213 tuple.Attributes = 0;
214 tuple.TupleData = buf;
215 tuple.TupleDataMax = sizeof(buf);
216 tuple.TupleOffset = 0;
217 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
218 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
219 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
220 link->conf.ConfigBase = parse.config.base;
221 link->conf.Present = parse.config.rmask[0];
224 link->state |= DEV_CONFIG;
226 /* Look up the current Vcc */
227 CS_CHECK(GetConfigurationInfo,
228 pcmcia_get_configuration_info(handle, &conf));
229 link->conf.Vcc = conf.Vcc;
231 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
232 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
234 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
236 if (pcmcia_get_tuple_data(handle, &tuple) != 0
237 || pcmcia_parse_tuple(handle, &tuple, &parse)
241 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
248 link->conf.ConfigIndex = cfg->index;
250 /* Use power settings for Vcc and Vpp if present */
251 /* Note that the CIS values need to be rescaled */
252 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
253 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
256 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
257 if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
262 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
263 link->conf.Vpp1 = link->conf.Vpp2 =
264 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
265 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
266 link->conf.Vpp1 = link->conf.Vpp2 =
267 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
269 /* we need an interrupt */
270 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
271 link->conf.Attributes |= CONF_ENABLE_IRQ;
273 /* IO window settings */
274 link->io.NumPorts1 = link->io.NumPorts2 = 0;
275 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
276 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
278 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
279 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
280 link->io.BasePort1 = io->win[0].base;
281 link->io.NumPorts1 = io->win[0].len;
283 if (pcmcia_request_io(link->handle, &link->io) != 0)
289 if (link->io.NumPorts1)
290 pcmcia_release_io(link->handle, &link->io);
291 last_ret = pcmcia_get_next_tuple(handle, &tuple);
294 /* require an IRQ and two registers */
295 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
297 if (link->conf.Attributes & CONF_ENABLE_IRQ)
299 pcmcia_request_irq(link->handle, &link->irq));
303 CS_CHECK(RequestConfiguration,
304 pcmcia_request_configuration(link->handle, &link->conf));
306 sprintf(dev->node.dev_name, driver_name);
307 dev->node.major = dev->node.minor = 0;
308 link->dev = &dev->node;
310 printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
311 dev->node.dev_name, link->conf.ConfigIndex,
312 link->conf.Vcc/10, link->conf.Vcc%10);
314 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
315 printk(", irq %d", link->irq.AssignedIRQ);
316 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
317 link->io.BasePort1+link->io.NumPorts1-1);
320 link->state &= ~DEV_CONFIG_PENDING;
322 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
325 printk("sl811_cs_config failed\n");
326 cs_error(link->handle, last_fn, last_ret);
327 sl811_cs_release(link);
328 link->state &= ~DEV_CONFIG_PENDING;
333 sl811_cs_event(event_t event, int priority, event_callback_args_t *args)
335 dev_link_t *link = args->client_data;
337 DBG(1, "sl811_cs_event(0x%06x)\n", event);
340 case CS_EVENT_CARD_REMOVAL:
341 link->state &= ~DEV_PRESENT;
342 if (link->state & DEV_CONFIG)
343 sl811_cs_release(link);
346 case CS_EVENT_CARD_INSERTION:
347 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
348 sl811_cs_config(link);
351 case CS_EVENT_PM_SUSPEND:
352 link->state |= DEV_SUSPEND;
353 /* Fall through... */
354 case CS_EVENT_RESET_PHYSICAL:
355 if (link->state & DEV_CONFIG)
356 pcmcia_release_configuration(link->handle);
359 case CS_EVENT_PM_RESUME:
360 link->state &= ~DEV_SUSPEND;
361 /* Fall through... */
362 case CS_EVENT_CARD_RESET:
363 if (link->state & DEV_CONFIG)
364 pcmcia_request_configuration(link->handle, &link->conf);
365 DBG(0, "reset sl811-hcd here?\n");
371 static dev_link_t *sl811_cs_attach(void)
375 client_reg_t client_reg;
378 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
381 memset(local, 0, sizeof(local_info_t));
386 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
387 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
388 if (irq_list[0] == -1)
389 link->irq.IRQInfo2 = irq_mask;
391 for (i = 0; i < irq_list_count; i++)
392 link->irq.IRQInfo2 |= 1 << irq_list[i];
393 link->irq.Handler = NULL;
395 link->conf.Attributes = 0;
397 link->conf.IntType = INT_MEMORY_AND_IO;
399 /* Register with Card Services */
400 link->next = dev_list;
402 client_reg.dev_info = (dev_info_t *) &driver_name;
403 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
404 client_reg.EventMask =
405 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
406 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
407 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
408 client_reg.event_handler = &sl811_cs_event;
409 client_reg.Version = 0x0210;
410 client_reg.event_callback_args.client_data = link;
411 ret = pcmcia_register_client(&link->handle, &client_reg);
412 if (ret != CS_SUCCESS) {
413 cs_error(link->handle, RegisterClient, ret);
414 sl811_cs_detach(link);
421 static struct pcmcia_driver sl811_cs_driver = {
422 .owner = THIS_MODULE,
424 .name = (char *)driver_name,
426 .attach = sl811_cs_attach,
427 .detach = sl811_cs_detach,
430 /*====================================================================*/
432 static int __init init_sl811_cs(void)
434 return pcmcia_register_driver(&sl811_cs_driver);
436 module_init(init_sl811_cs);
438 static void __exit exit_sl811_cs(void)
440 pcmcia_unregister_driver(&sl811_cs_driver);
442 module_exit(exit_sl811_cs);