Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / usb / host / sl811_cs.c
blob302aa1ec312f55ef21592dd40c4e164ee12d5322
1 /*
2 * PCMCIA driver for SL811HS (as found in REX-CFU1U)
3 * Filename: sl811_cs.c
4 * Author: Yukio Yamamoto
6 * Port to sl811-hcd and 2.6.x by
7 * Botond Botyanszki <boti@rocketmail.com>
8 * Simon Pickering
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 /*====================================================================*/
38 /* MACROS */
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)
48 #else
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) \
57 do { \
58 last_fn = (fn); \
59 if ((last_ret = (ret)) != 0) \
60 goto cs_failed; \
61 } while (0)
63 /*====================================================================*/
64 /* VARIABLES */
65 /*====================================================================*/
67 static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
69 typedef struct local_info_t {
70 struct pcmcia_device *p_dev;
71 dev_node_t node;
72 } local_info_t;
74 static void sl811_cs_release(struct pcmcia_device * link);
76 /*====================================================================*/
78 static void release_platform_dev(struct device * dev)
80 DBG(0, "sl811_cs platform_dev release\n");
81 dev->parent = NULL;
84 static struct sl811_platform_data platform_data = {
85 .potpg = 100,
86 .power = 50, /* == 100mA */
87 // .reset = ... FIXME: invoke CF reset on the card
90 static struct resource resources[] = {
91 [0] = {
92 .flags = IORESOURCE_IRQ,
94 [1] = {
95 // .name = "address",
96 .flags = IORESOURCE_IO,
98 [2] = {
99 // .name = "data",
100 .flags = IORESOURCE_IO,
104 extern struct platform_driver sl811h_driver;
106 static struct platform_device platform_dev = {
107 .id = -1,
108 .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)
119 return -EBUSY;
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.driver.name;
136 return platform_device_register(&platform_dev);
139 /*====================================================================*/
141 static void sl811_cs_detach(struct pcmcia_device *link)
143 DBG(0, "sl811_cs_detach(0x%p)\n", link);
145 sl811_cs_release(link);
147 /* This points to the parent local_info_t struct */
148 kfree(link->priv);
151 static void sl811_cs_release(struct pcmcia_device * link)
153 DBG(0, "sl811_cs_release(0x%p)\n", link);
155 pcmcia_disable_device(link);
156 platform_device_unregister(&platform_dev);
159 static int sl811_cs_config(struct pcmcia_device *link)
161 struct device *parent = &handle_to_dev(link);
162 local_info_t *dev = link->priv;
163 tuple_t tuple;
164 cisparse_t parse;
165 int last_fn, last_ret;
166 u_char buf[64];
167 config_info_t conf;
168 cistpl_cftable_entry_t dflt = { 0 };
170 DBG(0, "sl811_cs_config(0x%p)\n", link);
172 tuple.DesiredTuple = CISTPL_CONFIG;
173 tuple.Attributes = 0;
174 tuple.TupleData = buf;
175 tuple.TupleDataMax = sizeof(buf);
176 tuple.TupleOffset = 0;
177 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
178 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
179 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
180 link->conf.ConfigBase = parse.config.base;
181 link->conf.Present = parse.config.rmask[0];
183 /* Look up the current Vcc */
184 CS_CHECK(GetConfigurationInfo,
185 pcmcia_get_configuration_info(link, &conf));
187 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
188 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
189 while (1) {
190 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
192 if (pcmcia_get_tuple_data(link, &tuple) != 0
193 || pcmcia_parse_tuple(link, &tuple, &parse)
194 != 0)
195 goto next_entry;
197 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
198 dflt = *cfg;
201 if (cfg->index == 0)
202 goto next_entry;
204 link->conf.ConfigIndex = cfg->index;
206 /* Use power settings for Vcc and Vpp if present */
207 /* Note that the CIS values need to be rescaled */
208 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
209 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
210 != conf.Vcc)
211 goto next_entry;
212 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
213 if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
214 != conf.Vcc)
215 goto next_entry;
218 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
219 link->conf.Vpp =
220 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
221 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
222 link->conf.Vpp =
223 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
225 /* we need an interrupt */
226 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
227 link->conf.Attributes |= CONF_ENABLE_IRQ;
229 /* IO window settings */
230 link->io.NumPorts1 = link->io.NumPorts2 = 0;
231 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
232 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
234 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
235 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
236 link->io.BasePort1 = io->win[0].base;
237 link->io.NumPorts1 = io->win[0].len;
239 if (pcmcia_request_io(link, &link->io) != 0)
240 goto next_entry;
242 break;
244 next_entry:
245 pcmcia_disable_device(link);
246 last_ret = pcmcia_get_next_tuple(link, &tuple);
249 /* require an IRQ and two registers */
250 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
251 goto cs_failed;
252 if (link->conf.Attributes & CONF_ENABLE_IRQ)
253 CS_CHECK(RequestIRQ,
254 pcmcia_request_irq(link, &link->irq));
255 else
256 goto cs_failed;
258 CS_CHECK(RequestConfiguration,
259 pcmcia_request_configuration(link, &link->conf));
261 sprintf(dev->node.dev_name, driver_name);
262 dev->node.major = dev->node.minor = 0;
263 link->dev_node = &dev->node;
265 printk(KERN_INFO "%s: index 0x%02x: ",
266 dev->node.dev_name, link->conf.ConfigIndex);
267 if (link->conf.Vpp)
268 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
269 printk(", irq %d", link->irq.AssignedIRQ);
270 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
271 link->io.BasePort1+link->io.NumPorts1-1);
272 printk("\n");
274 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
275 < 0) {
276 cs_failed:
277 printk("sl811_cs_config failed\n");
278 cs_error(link, last_fn, last_ret);
279 sl811_cs_release(link);
280 return -ENODEV;
282 return 0;
285 static int sl811_cs_probe(struct pcmcia_device *link)
287 local_info_t *local;
289 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
290 if (!local)
291 return -ENOMEM;
292 memset(local, 0, sizeof(local_info_t));
293 local->p_dev = link;
294 link->priv = local;
296 /* Initialize */
297 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
298 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
299 link->irq.Handler = NULL;
301 link->conf.Attributes = 0;
302 link->conf.IntType = INT_MEMORY_AND_IO;
304 return sl811_cs_config(link);
307 static struct pcmcia_device_id sl811_ids[] = {
308 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
309 PCMCIA_DEVICE_NULL,
311 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
313 static struct pcmcia_driver sl811_cs_driver = {
314 .owner = THIS_MODULE,
315 .drv = {
316 .name = (char *)driver_name,
318 .probe = sl811_cs_probe,
319 .remove = sl811_cs_detach,
320 .id_table = sl811_ids,
323 /*====================================================================*/
325 static int __init init_sl811_cs(void)
327 return pcmcia_register_driver(&sl811_cs_driver);
329 module_init(init_sl811_cs);
331 static void __exit exit_sl811_cs(void)
333 pcmcia_unregister_driver(&sl811_cs_driver);
335 module_exit(exit_sl811_cs);