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/cs_types.h>
24 #include <pcmcia/cs.h>
25 #include <pcmcia/cistpl.h>
26 #include <pcmcia/cisreg.h>
27 #include <pcmcia/ds.h>
29 #include <linux/usb_sl811.h>
31 MODULE_AUTHOR("Botond Botyanszki");
32 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
33 MODULE_LICENSE("GPL");
36 /*====================================================================*/
38 /*====================================================================*/
40 #if defined(DEBUG) || defined(CONFIG_USB_DEBUG) || defined(PCMCIA_DEBUG)
42 static int pc_debug = 0;
43 module_param(pc_debug, int, 0644);
45 #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
48 #define DBG(n, args...) do{}while(0)
49 #endif /* no debugging */
51 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
53 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
55 #define CS_CHECK(fn, ret) \
58 if ((last_ret = (ret)) != 0) \
62 /*====================================================================*/
64 /*====================================================================*/
66 static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
68 static dev_link_t *dev_list = NULL;
70 typedef struct local_info_t {
75 /*====================================================================*/
77 static void release_platform_dev(struct device * dev)
79 DBG(0, "sl811_cs platform_dev release\n");
83 static struct sl811_platform_data platform_data = {
85 .power = 50, /* == 100mA */
86 // .reset = ... FIXME: invoke CF reset on the card
89 static struct resource resources[] = {
91 .flags = IORESOURCE_IRQ,
95 .flags = IORESOURCE_IO,
99 .flags = IORESOURCE_IO,
103 extern struct device_driver sl811h_driver;
105 static struct platform_device platform_dev = {
108 .platform_data = &platform_data,
109 .release = release_platform_dev,
111 .resource = resources,
112 .num_resources = ARRAY_SIZE(resources),
115 static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
117 if (platform_dev.dev.parent)
119 platform_dev.dev.parent = parent;
121 /* finish seting up the platform device */
122 resources[0].start = irq;
124 resources[1].start = base_addr;
125 resources[1].end = base_addr;
127 resources[2].start = base_addr + 1;
128 resources[2].end = base_addr + 1;
130 /* The driver core will probe for us. We know sl811-hcd has been
131 * initialized already because of the link order dependency.
133 platform_dev.name = sl811h_driver.name;
134 return platform_device_register(&platform_dev);
137 /*====================================================================*/
139 static void sl811_cs_detach(dev_link_t *link)
143 DBG(0, "sl811_cs_detach(0x%p)\n", link);
145 /* Locate device structure */
146 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
153 /* Break the link with Card Services */
155 pcmcia_deregister_client(link->handle);
157 /* Unlink device structure, and free it */
159 /* This points to the parent local_info_t struct */
163 static void sl811_cs_release(dev_link_t * link)
166 DBG(0, "sl811_cs_release(0x%p)\n", link);
169 DBG(1, "sl811_cs: release postponed, '%s' still open\n",
170 link->dev->dev_name);
171 link->state |= DEV_STALE_CONFIG;
175 /* Unlink the device chain */
178 platform_device_unregister(&platform_dev);
179 pcmcia_release_configuration(link->handle);
180 if (link->io.NumPorts1)
181 pcmcia_release_io(link->handle, &link->io);
182 if (link->irq.AssignedIRQ)
183 pcmcia_release_irq(link->handle, &link->irq);
184 link->state &= ~DEV_CONFIG;
186 if (link->state & DEV_STALE_LINK)
187 sl811_cs_detach(link);
190 static void sl811_cs_config(dev_link_t *link)
192 client_handle_t handle = link->handle;
193 struct device *parent = &handle_to_dev(handle);
194 local_info_t *dev = link->priv;
197 int last_fn, last_ret;
200 cistpl_cftable_entry_t dflt = { 0 };
202 DBG(0, "sl811_cs_config(0x%p)\n", link);
204 tuple.DesiredTuple = CISTPL_CONFIG;
205 tuple.Attributes = 0;
206 tuple.TupleData = buf;
207 tuple.TupleDataMax = sizeof(buf);
208 tuple.TupleOffset = 0;
209 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
210 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
211 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
212 link->conf.ConfigBase = parse.config.base;
213 link->conf.Present = parse.config.rmask[0];
216 link->state |= DEV_CONFIG;
218 /* Look up the current Vcc */
219 CS_CHECK(GetConfigurationInfo,
220 pcmcia_get_configuration_info(handle, &conf));
221 link->conf.Vcc = conf.Vcc;
223 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
224 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
226 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
228 if (pcmcia_get_tuple_data(handle, &tuple) != 0
229 || pcmcia_parse_tuple(handle, &tuple, &parse)
233 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
240 link->conf.ConfigIndex = cfg->index;
242 /* Use power settings for Vcc and Vpp if present */
243 /* Note that the CIS values need to be rescaled */
244 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
245 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
248 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
249 if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
254 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
255 link->conf.Vpp1 = link->conf.Vpp2 =
256 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
257 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
258 link->conf.Vpp1 = link->conf.Vpp2 =
259 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
261 /* we need an interrupt */
262 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
263 link->conf.Attributes |= CONF_ENABLE_IRQ;
265 /* IO window settings */
266 link->io.NumPorts1 = link->io.NumPorts2 = 0;
267 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
268 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
270 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
271 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
272 link->io.BasePort1 = io->win[0].base;
273 link->io.NumPorts1 = io->win[0].len;
275 if (pcmcia_request_io(link->handle, &link->io) != 0)
281 if (link->io.NumPorts1)
282 pcmcia_release_io(link->handle, &link->io);
283 last_ret = pcmcia_get_next_tuple(handle, &tuple);
286 /* require an IRQ and two registers */
287 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
289 if (link->conf.Attributes & CONF_ENABLE_IRQ)
291 pcmcia_request_irq(link->handle, &link->irq));
295 CS_CHECK(RequestConfiguration,
296 pcmcia_request_configuration(link->handle, &link->conf));
298 sprintf(dev->node.dev_name, driver_name);
299 dev->node.major = dev->node.minor = 0;
300 link->dev = &dev->node;
302 printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
303 dev->node.dev_name, link->conf.ConfigIndex,
304 link->conf.Vcc/10, link->conf.Vcc%10);
306 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
307 printk(", irq %d", link->irq.AssignedIRQ);
308 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
309 link->io.BasePort1+link->io.NumPorts1-1);
312 link->state &= ~DEV_CONFIG_PENDING;
314 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
317 printk("sl811_cs_config failed\n");
318 cs_error(link->handle, last_fn, last_ret);
319 sl811_cs_release(link);
320 link->state &= ~DEV_CONFIG_PENDING;
325 sl811_cs_event(event_t event, int priority, event_callback_args_t *args)
327 dev_link_t *link = args->client_data;
329 DBG(1, "sl811_cs_event(0x%06x)\n", event);
332 case CS_EVENT_CARD_REMOVAL:
333 link->state &= ~DEV_PRESENT;
334 if (link->state & DEV_CONFIG)
335 sl811_cs_release(link);
338 case CS_EVENT_CARD_INSERTION:
339 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
340 sl811_cs_config(link);
343 case CS_EVENT_PM_SUSPEND:
344 link->state |= DEV_SUSPEND;
345 /* Fall through... */
346 case CS_EVENT_RESET_PHYSICAL:
347 if (link->state & DEV_CONFIG)
348 pcmcia_release_configuration(link->handle);
351 case CS_EVENT_PM_RESUME:
352 link->state &= ~DEV_SUSPEND;
353 /* Fall through... */
354 case CS_EVENT_CARD_RESET:
355 if (link->state & DEV_CONFIG)
356 pcmcia_request_configuration(link->handle, &link->conf);
357 DBG(0, "reset sl811-hcd here?\n");
363 static dev_link_t *sl811_cs_attach(void)
367 client_reg_t client_reg;
370 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
373 memset(local, 0, sizeof(local_info_t));
378 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
379 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
380 link->irq.Handler = NULL;
382 link->conf.Attributes = 0;
384 link->conf.IntType = INT_MEMORY_AND_IO;
386 /* Register with Card Services */
387 link->next = dev_list;
389 client_reg.dev_info = (dev_info_t *) &driver_name;
390 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
391 client_reg.Version = 0x0210;
392 client_reg.event_callback_args.client_data = link;
393 ret = pcmcia_register_client(&link->handle, &client_reg);
394 if (ret != CS_SUCCESS) {
395 cs_error(link->handle, RegisterClient, ret);
396 sl811_cs_detach(link);
403 static struct pcmcia_device_id sl811_ids[] = {
404 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
407 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
409 static struct pcmcia_driver sl811_cs_driver = {
410 .owner = THIS_MODULE,
412 .name = (char *)driver_name,
414 .attach = sl811_cs_attach,
415 .event = sl811_cs_event,
416 .detach = sl811_cs_detach,
417 .id_table = sl811_ids,
420 /*====================================================================*/
422 static int __init init_sl811_cs(void)
424 return pcmcia_register_driver(&sl811_cs_driver);
426 module_init(init_sl811_cs);
428 static void __exit exit_sl811_cs(void)
430 pcmcia_unregister_driver(&sl811_cs_driver);
432 module_exit(exit_sl811_cs);