blob: 5056b7459994d6c891297f8d45f946802aa4fc84 [file] [log] [blame]
David Brownellc6de2b62005-05-26 05:55:55 -07001/*
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>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
David Brownellc6de2b62005-05-26 05:55:55 -070023
David Brownellc6de2b62005-05-26 05:55:55 -070024#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
32MODULE_AUTHOR("Botond Botyanszki");
33MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
34MODULE_LICENSE("GPL");
35
36
37/*====================================================================*/
38/* MACROS */
39/*====================================================================*/
40
David Brownellc9a50cc2005-11-07 20:45:20 -080041#if defined(DEBUG) || defined(PCMCIA_DEBUG)
David Brownellc6de2b62005-05-26 05:55:55 -070042
43static int pc_debug = 0;
44module_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
67static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
68
69static dev_link_t *dev_list = NULL;
70
David Brownellc6de2b62005-05-26 05:55:55 -070071typedef struct local_info_t {
72 dev_link_t link;
73 dev_node_t node;
74} local_info_t;
75
76/*====================================================================*/
77
78static void release_platform_dev(struct device * dev)
79{
80 DBG(0, "sl811_cs platform_dev release\n");
81 dev->parent = NULL;
82}
83
84static struct sl811_platform_data platform_data = {
85 .potpg = 100,
86 .power = 50, /* == 100mA */
87 // .reset = ... FIXME: invoke CF reset on the card
88};
89
90static 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
104extern struct device_driver sl811h_driver;
105
106static 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
116static 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
David Brownellc9a50cc2005-11-07 20:45:20 -0800132 * initialized already because of the link order dependency created
133 * by referencing "sl811h_driver".
David Brownellc6de2b62005-05-26 05:55:55 -0700134 */
135 platform_dev.name = sl811h_driver.name;
136 return platform_device_register(&platform_dev);
137}
138
139/*====================================================================*/
140
141static void sl811_cs_detach(dev_link_t *link)
142{
143 dev_link_t **linkp;
144
145 DBG(0, "sl811_cs_detach(0x%p)\n", link);
146
147 /* Locate device structure */
148 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
149 if (*linkp == link)
150 break;
151 }
152 if (*linkp == NULL)
153 return;
154
155 /* Break the link with Card Services */
156 if (link->handle)
157 pcmcia_deregister_client(link->handle);
158
159 /* Unlink device structure, and free it */
160 *linkp = link->next;
161 /* This points to the parent local_info_t struct */
162 kfree(link->priv);
163}
164
165static void sl811_cs_release(dev_link_t * link)
166{
167
168 DBG(0, "sl811_cs_release(0x%p)\n", link);
169
170 if (link->open) {
171 DBG(1, "sl811_cs: release postponed, '%s' still open\n",
172 link->dev->dev_name);
173 link->state |= DEV_STALE_CONFIG;
174 return;
175 }
176
177 /* Unlink the device chain */
178 link->dev = NULL;
179
180 platform_device_unregister(&platform_dev);
181 pcmcia_release_configuration(link->handle);
182 if (link->io.NumPorts1)
183 pcmcia_release_io(link->handle, &link->io);
184 if (link->irq.AssignedIRQ)
185 pcmcia_release_irq(link->handle, &link->irq);
186 link->state &= ~DEV_CONFIG;
187
188 if (link->state & DEV_STALE_LINK)
189 sl811_cs_detach(link);
190}
191
192static void sl811_cs_config(dev_link_t *link)
193{
194 client_handle_t handle = link->handle;
195 struct device *parent = &handle_to_dev(handle);
196 local_info_t *dev = link->priv;
197 tuple_t tuple;
198 cisparse_t parse;
199 int last_fn, last_ret;
200 u_char buf[64];
201 config_info_t conf;
202 cistpl_cftable_entry_t dflt = { 0 };
203
204 DBG(0, "sl811_cs_config(0x%p)\n", link);
205
206 tuple.DesiredTuple = CISTPL_CONFIG;
207 tuple.Attributes = 0;
208 tuple.TupleData = buf;
209 tuple.TupleDataMax = sizeof(buf);
210 tuple.TupleOffset = 0;
211 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
212 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
213 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
214 link->conf.ConfigBase = parse.config.base;
215 link->conf.Present = parse.config.rmask[0];
216
217 /* Configure card */
218 link->state |= DEV_CONFIG;
219
220 /* Look up the current Vcc */
221 CS_CHECK(GetConfigurationInfo,
222 pcmcia_get_configuration_info(handle, &conf));
223 link->conf.Vcc = conf.Vcc;
224
225 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
226 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
227 while (1) {
228 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
229
230 if (pcmcia_get_tuple_data(handle, &tuple) != 0
231 || pcmcia_parse_tuple(handle, &tuple, &parse)
232 != 0)
233 goto next_entry;
234
235 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
236 dflt = *cfg;
237 }
238
239 if (cfg->index == 0)
240 goto next_entry;
241
242 link->conf.ConfigIndex = cfg->index;
243
244 /* Use power settings for Vcc and Vpp if present */
245 /* Note that the CIS values need to be rescaled */
246 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
247 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
248 != conf.Vcc)
249 goto next_entry;
250 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
251 if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
252 != conf.Vcc)
253 goto next_entry;
254 }
255
256 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
257 link->conf.Vpp1 = link->conf.Vpp2 =
258 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
259 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
260 link->conf.Vpp1 = link->conf.Vpp2 =
261 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
262
263 /* we need an interrupt */
264 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
265 link->conf.Attributes |= CONF_ENABLE_IRQ;
266
267 /* IO window settings */
268 link->io.NumPorts1 = link->io.NumPorts2 = 0;
269 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
270 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
271
272 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
273 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
274 link->io.BasePort1 = io->win[0].base;
275 link->io.NumPorts1 = io->win[0].len;
276
277 if (pcmcia_request_io(link->handle, &link->io) != 0)
278 goto next_entry;
279 }
280 break;
281
282next_entry:
283 if (link->io.NumPorts1)
284 pcmcia_release_io(link->handle, &link->io);
285 last_ret = pcmcia_get_next_tuple(handle, &tuple);
286 }
287
288 /* require an IRQ and two registers */
289 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
290 goto cs_failed;
291 if (link->conf.Attributes & CONF_ENABLE_IRQ)
292 CS_CHECK(RequestIRQ,
293 pcmcia_request_irq(link->handle, &link->irq));
294 else
295 goto cs_failed;
296
297 CS_CHECK(RequestConfiguration,
298 pcmcia_request_configuration(link->handle, &link->conf));
299
300 sprintf(dev->node.dev_name, driver_name);
301 dev->node.major = dev->node.minor = 0;
302 link->dev = &dev->node;
303
304 printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
305 dev->node.dev_name, link->conf.ConfigIndex,
306 link->conf.Vcc/10, link->conf.Vcc%10);
307 if (link->conf.Vpp1)
308 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
309 printk(", irq %d", link->irq.AssignedIRQ);
310 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
311 link->io.BasePort1+link->io.NumPorts1-1);
312 printk("\n");
313
314 link->state &= ~DEV_CONFIG_PENDING;
315
316 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
317 < 0) {
318cs_failed:
319 printk("sl811_cs_config failed\n");
320 cs_error(link->handle, last_fn, last_ret);
321 sl811_cs_release(link);
322 link->state &= ~DEV_CONFIG_PENDING;
323 }
324}
325
326static int
327sl811_cs_event(event_t event, int priority, event_callback_args_t *args)
328{
329 dev_link_t *link = args->client_data;
330
331 DBG(1, "sl811_cs_event(0x%06x)\n", event);
332
333 switch (event) {
334 case CS_EVENT_CARD_REMOVAL:
335 link->state &= ~DEV_PRESENT;
336 if (link->state & DEV_CONFIG)
337 sl811_cs_release(link);
338 break;
339
340 case CS_EVENT_CARD_INSERTION:
341 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
342 sl811_cs_config(link);
343 break;
344
345 case CS_EVENT_PM_SUSPEND:
346 link->state |= DEV_SUSPEND;
347 /* Fall through... */
348 case CS_EVENT_RESET_PHYSICAL:
349 if (link->state & DEV_CONFIG)
350 pcmcia_release_configuration(link->handle);
351 break;
352
353 case CS_EVENT_PM_RESUME:
354 link->state &= ~DEV_SUSPEND;
355 /* Fall through... */
356 case CS_EVENT_CARD_RESET:
357 if (link->state & DEV_CONFIG)
358 pcmcia_request_configuration(link->handle, &link->conf);
359 DBG(0, "reset sl811-hcd here?\n");
360 break;
361 }
362 return 0;
363}
364
365static dev_link_t *sl811_cs_attach(void)
366{
367 local_info_t *local;
368 dev_link_t *link;
369 client_reg_t client_reg;
David Brownell22f3a8f2005-06-27 16:28:43 -0700370 int ret;
David Brownellc6de2b62005-05-26 05:55:55 -0700371
372 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
373 if (!local)
374 return NULL;
375 memset(local, 0, sizeof(local_info_t));
376 link = &local->link;
377 link->priv = local;
378
379 /* Initialize */
380 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
381 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
David Brownellc6de2b62005-05-26 05:55:55 -0700382 link->irq.Handler = NULL;
383
384 link->conf.Attributes = 0;
385 link->conf.Vcc = 33;
386 link->conf.IntType = INT_MEMORY_AND_IO;
387
388 /* Register with Card Services */
389 link->next = dev_list;
390 dev_list = link;
391 client_reg.dev_info = (dev_info_t *) &driver_name;
392 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
David Brownellc6de2b62005-05-26 05:55:55 -0700393 client_reg.Version = 0x0210;
394 client_reg.event_callback_args.client_data = link;
395 ret = pcmcia_register_client(&link->handle, &client_reg);
396 if (ret != CS_SUCCESS) {
397 cs_error(link->handle, RegisterClient, ret);
398 sl811_cs_detach(link);
399 return NULL;
400 }
401
402 return link;
403}
404
David Brownell22f3a8f2005-06-27 16:28:43 -0700405static struct pcmcia_device_id sl811_ids[] = {
406 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
407 PCMCIA_DEVICE_NULL,
408};
409MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
410
David Brownellc6de2b62005-05-26 05:55:55 -0700411static struct pcmcia_driver sl811_cs_driver = {
412 .owner = THIS_MODULE,
413 .drv = {
414 .name = (char *)driver_name,
415 },
416 .attach = sl811_cs_attach,
Dominik Brodowski1e212f32005-07-07 17:59:00 -0700417 .event = sl811_cs_event,
David Brownellc6de2b62005-05-26 05:55:55 -0700418 .detach = sl811_cs_detach,
David Brownell22f3a8f2005-06-27 16:28:43 -0700419 .id_table = sl811_ids,
David Brownellc6de2b62005-05-26 05:55:55 -0700420};
421
422/*====================================================================*/
423
424static int __init init_sl811_cs(void)
425{
426 return pcmcia_register_driver(&sl811_cs_driver);
427}
428module_init(init_sl811_cs);
429
430static void __exit exit_sl811_cs(void)
431{
432 pcmcia_unregister_driver(&sl811_cs_driver);
433}
434module_exit(exit_sl811_cs);