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>
22 #include <linux/platform_device.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(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 typedef struct local_info_t
{
76 /*====================================================================*/
78 static void release_platform_dev(struct device
* dev
)
80 DBG(0, "sl811_cs platform_dev release\n");
84 static struct sl811_platform_data platform_data
= {
86 .power
= 50, /* == 100mA */
87 // .reset = ... FIXME: invoke CF reset on the card
90 static struct resource resources
[] = {
92 .flags
= IORESOURCE_IRQ
,
96 .flags
= IORESOURCE_IO
,
100 .flags
= IORESOURCE_IO
,
104 extern struct device_driver sl811h_driver
;
106 static struct platform_device platform_dev
= {
109 .platform_data
= &platform_data
,
110 .release
= release_platform_dev
,
112 .resource
= resources
,
113 .num_resources
= ARRAY_SIZE(resources
),
116 static int sl811_hc_init(struct device
*parent
, ioaddr_t base_addr
, int irq
)
118 if (platform_dev
.dev
.parent
)
120 platform_dev
.dev
.parent
= parent
;
122 /* finish seting up the platform device */
123 resources
[0].start
= irq
;
125 resources
[1].start
= base_addr
;
126 resources
[1].end
= base_addr
;
128 resources
[2].start
= base_addr
+ 1;
129 resources
[2].end
= base_addr
+ 1;
131 /* The driver core will probe for us. We know sl811-hcd has been
132 * initialized already because of the link order dependency created
133 * by referencing "sl811h_driver".
135 platform_dev
.name
= sl811h_driver
.name
;
136 return platform_device_register(&platform_dev
);
139 /*====================================================================*/
141 static void sl811_cs_detach(dev_link_t
*link
)
145 DBG(0, "sl811_cs_detach(0x%p)\n", link
);
147 /* Locate device structure */
148 for (linkp
= &dev_list
; *linkp
; linkp
= &(*linkp
)->next
) {
155 /* Break the link with Card Services */
157 pcmcia_deregister_client(link
->handle
);
159 /* Unlink device structure, and free it */
161 /* This points to the parent local_info_t struct */
165 static void sl811_cs_release(dev_link_t
* link
)
168 DBG(0, "sl811_cs_release(0x%p)\n", link
);
171 DBG(1, "sl811_cs: release postponed, '%s' still open\n",
172 link
->dev
->dev_name
);
173 link
->state
|= DEV_STALE_CONFIG
;
177 /* Unlink the device chain */
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
;
188 if (link
->state
& DEV_STALE_LINK
)
189 sl811_cs_detach(link
);
192 static void sl811_cs_config(dev_link_t
*link
)
194 client_handle_t handle
= link
->handle
;
195 struct device
*parent
= &handle_to_dev(handle
);
196 local_info_t
*dev
= link
->priv
;
199 int last_fn
, last_ret
;
202 cistpl_cftable_entry_t dflt
= { 0 };
204 DBG(0, "sl811_cs_config(0x%p)\n", link
);
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];
218 link
->state
|= DEV_CONFIG
;
220 /* Look up the current Vcc */
221 CS_CHECK(GetConfigurationInfo
,
222 pcmcia_get_configuration_info(handle
, &conf
));
223 link
->conf
.Vcc
= conf
.Vcc
;
225 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
226 CS_CHECK(GetFirstTuple
, pcmcia_get_first_tuple(handle
, &tuple
));
228 cistpl_cftable_entry_t
*cfg
= &(parse
.cftable_entry
);
230 if (pcmcia_get_tuple_data(handle
, &tuple
) != 0
231 || pcmcia_parse_tuple(handle
, &tuple
, &parse
)
235 if (cfg
->flags
& CISTPL_CFTABLE_DEFAULT
) {
242 link
->conf
.ConfigIndex
= cfg
->index
;
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
250 } else if (dflt
.vcc
.present
& (1<<CISTPL_POWER_VNOM
)) {
251 if (dflt
.vcc
.param
[CISTPL_POWER_VNOM
]/10000
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;
263 /* we need an interrupt */
264 if (cfg
->irq
.IRQInfo1
|| dflt
.irq
.IRQInfo1
)
265 link
->conf
.Attributes
|= CONF_ENABLE_IRQ
;
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
;
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
;
277 if (pcmcia_request_io(link
->handle
, &link
->io
) != 0)
283 if (link
->io
.NumPorts1
)
284 pcmcia_release_io(link
->handle
, &link
->io
);
285 last_ret
= pcmcia_get_next_tuple(handle
, &tuple
);
288 /* require an IRQ and two registers */
289 if (!link
->io
.NumPorts1
|| link
->io
.NumPorts1
< 2)
291 if (link
->conf
.Attributes
& CONF_ENABLE_IRQ
)
293 pcmcia_request_irq(link
->handle
, &link
->irq
));
297 CS_CHECK(RequestConfiguration
,
298 pcmcia_request_configuration(link
->handle
, &link
->conf
));
300 sprintf(dev
->node
.dev_name
, driver_name
);
301 dev
->node
.major
= dev
->node
.minor
= 0;
302 link
->dev
= &dev
->node
;
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);
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);
314 link
->state
&= ~DEV_CONFIG_PENDING
;
316 if (sl811_hc_init(parent
, link
->io
.BasePort1
, link
->irq
.AssignedIRQ
)
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
;
326 static int sl811_suspend(struct pcmcia_device
*dev
)
328 dev_link_t
*link
= dev_to_instance(dev
);
330 link
->state
|= DEV_SUSPEND
;
331 if (link
->state
& DEV_CONFIG
)
332 pcmcia_release_configuration(link
->handle
);
337 static int sl811_resume(struct pcmcia_device
*dev
)
339 dev_link_t
*link
= dev_to_instance(dev
);
341 link
->state
&= ~DEV_SUSPEND
;
342 if (link
->state
& DEV_CONFIG
)
343 pcmcia_request_configuration(link
->handle
, &link
->conf
);
349 sl811_cs_event(event_t event
, int priority
, event_callback_args_t
*args
)
351 dev_link_t
*link
= args
->client_data
;
353 DBG(1, "sl811_cs_event(0x%06x)\n", event
);
356 case CS_EVENT_CARD_REMOVAL
:
357 link
->state
&= ~DEV_PRESENT
;
358 if (link
->state
& DEV_CONFIG
)
359 sl811_cs_release(link
);
362 case CS_EVENT_CARD_INSERTION
:
363 link
->state
|= DEV_PRESENT
| DEV_CONFIG_PENDING
;
364 sl811_cs_config(link
);
370 static dev_link_t
*sl811_cs_attach(void)
374 client_reg_t client_reg
;
377 local
= kmalloc(sizeof(local_info_t
), GFP_KERNEL
);
380 memset(local
, 0, sizeof(local_info_t
));
385 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
;
386 link
->irq
.IRQInfo1
= IRQ_INFO2_VALID
|IRQ_LEVEL_ID
;
387 link
->irq
.Handler
= NULL
;
389 link
->conf
.Attributes
= 0;
391 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
393 /* Register with Card Services */
394 link
->next
= dev_list
;
396 client_reg
.dev_info
= (dev_info_t
*) &driver_name
;
397 client_reg
.Attributes
= INFO_IO_CLIENT
| INFO_CARD_SHARE
;
398 client_reg
.Version
= 0x0210;
399 client_reg
.event_callback_args
.client_data
= link
;
400 ret
= pcmcia_register_client(&link
->handle
, &client_reg
);
401 if (ret
!= CS_SUCCESS
) {
402 cs_error(link
->handle
, RegisterClient
, ret
);
403 sl811_cs_detach(link
);
410 static struct pcmcia_device_id sl811_ids
[] = {
411 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
414 MODULE_DEVICE_TABLE(pcmcia
, sl811_ids
);
416 static struct pcmcia_driver sl811_cs_driver
= {
417 .owner
= THIS_MODULE
,
419 .name
= (char *)driver_name
,
421 .attach
= sl811_cs_attach
,
422 .event
= sl811_cs_event
,
423 .detach
= sl811_cs_detach
,
424 .id_table
= sl811_ids
,
425 .suspend
= sl811_suspend
,
426 .resume
= sl811_resume
,
429 /*====================================================================*/
431 static int __init
init_sl811_cs(void)
433 return pcmcia_register_driver(&sl811_cs_driver
);
435 module_init(init_sl811_cs
);
437 static void __exit
exit_sl811_cs(void)
439 pcmcia_unregister_driver(&sl811_cs_driver
);
441 module_exit(exit_sl811_cs
);