Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6] / drivers / usb / host / sl811_cs.c
1 /*
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/ptrace.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/timer.h>
20 #include <linux/ioport.h>
21 #include <linux/platform_device.h>
22
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>
28
29 #include <linux/usb/sl811.h>
30
31 MODULE_AUTHOR("Botond Botyanszki");
32 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
33 MODULE_LICENSE("GPL");
34
35
36 /*====================================================================*/
37 /* MACROS                                                             */
38 /*====================================================================*/
39
40 #if defined(DEBUG) || defined(PCMCIA_DEBUG)
41
42 static int pc_debug = 0;
43 module_param(pc_debug, int, 0644);
44
45 #define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
46
47 #else
48 #define DBG(n, args...) do{}while(0)
49 #endif  /* no debugging */
50
51 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
52
53 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
54
55 #define CS_CHECK(fn, ret) \
56         do { \
57                 last_fn = (fn); \
58                 if ((last_ret = (ret)) != 0) \
59                         goto cs_failed; \
60         } while (0)
61
62 /*====================================================================*/
63 /* VARIABLES                                                          */
64 /*====================================================================*/
65
66 static const char driver_name[DEV_NAME_LEN]  = "sl811_cs";
67
68 typedef struct local_info_t {
69         struct pcmcia_device    *p_dev;
70         dev_node_t              node;
71 } local_info_t;
72
73 static void sl811_cs_release(struct pcmcia_device * link);
74
75 /*====================================================================*/
76
77 static void release_platform_dev(struct device * dev)
78 {
79         DBG(0, "sl811_cs platform_dev release\n");
80         dev->parent = NULL;
81 }
82
83 static struct sl811_platform_data platform_data = {
84         .potpg          = 100,
85         .power          = 50,           /* == 100mA */
86         // .reset       = ... FIXME:  invoke CF reset on the card
87 };
88
89 static struct resource resources[] = {
90         [0] = {
91                 .flags  = IORESOURCE_IRQ,
92         },
93         [1] = {
94                 // .name   = "address",
95                 .flags  = IORESOURCE_IO,
96         },
97         [2] = {
98                 // .name   = "data",
99                 .flags  = IORESOURCE_IO,
100         },
101 };
102
103 extern struct platform_driver sl811h_driver;
104
105 static struct platform_device platform_dev = {
106         .id                     = -1,
107         .dev = {
108                 .platform_data = &platform_data,
109                 .release       = release_platform_dev,
110         },
111         .resource               = resources,
112         .num_resources          = ARRAY_SIZE(resources),
113 };
114
115 static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
116 {
117         if (platform_dev.dev.parent)
118                 return -EBUSY;
119         platform_dev.dev.parent = parent;
120
121         /* finish seting up the platform device */
122         resources[0].start = irq;
123
124         resources[1].start = base_addr;
125         resources[1].end = base_addr;
126
127         resources[2].start = base_addr + 1;
128         resources[2].end   = base_addr + 1;
129
130         /* The driver core will probe for us.  We know sl811-hcd has been
131          * initialized already because of the link order dependency created
132          * by referencing "sl811h_driver".
133          */
134         platform_dev.name = sl811h_driver.driver.name;
135         return platform_device_register(&platform_dev);
136 }
137
138 /*====================================================================*/
139
140 static void sl811_cs_detach(struct pcmcia_device *link)
141 {
142         DBG(0, "sl811_cs_detach(0x%p)\n", link);
143
144         sl811_cs_release(link);
145
146         /* This points to the parent local_info_t struct */
147         kfree(link->priv);
148 }
149
150 static void sl811_cs_release(struct pcmcia_device * link)
151 {
152         DBG(0, "sl811_cs_release(0x%p)\n", link);
153
154         pcmcia_disable_device(link);
155         platform_device_unregister(&platform_dev);
156 }
157
158 static int sl811_cs_config(struct pcmcia_device *link)
159 {
160         struct device           *parent = &handle_to_dev(link);
161         local_info_t            *dev = link->priv;
162         tuple_t                 tuple;
163         cisparse_t              parse;
164         int                     last_fn, last_ret;
165         u_char                  buf[64];
166         config_info_t           conf;
167         cistpl_cftable_entry_t  dflt = { 0 };
168
169         DBG(0, "sl811_cs_config(0x%p)\n", link);
170
171         /* Look up the current Vcc */
172         CS_CHECK(GetConfigurationInfo,
173                         pcmcia_get_configuration_info(link, &conf));
174
175         tuple.Attributes = 0;
176         tuple.TupleData = buf;
177         tuple.TupleDataMax = sizeof(buf);
178         tuple.TupleOffset = 0;
179         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
180         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
181         while (1) {
182                 cistpl_cftable_entry_t  *cfg = &(parse.cftable_entry);
183
184                 if (pcmcia_get_tuple_data(link, &tuple) != 0
185                                 || pcmcia_parse_tuple(link, &tuple, &parse)
186                                                 != 0)
187                         goto next_entry;
188
189                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
190                         dflt = *cfg;
191                 }
192
193                 if (cfg->index == 0)
194                         goto next_entry;
195
196                 link->conf.ConfigIndex = cfg->index;
197
198                 /* Use power settings for Vcc and Vpp if present */
199                 /*  Note that the CIS values need to be rescaled */
200                 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
201                         if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
202                                         != conf.Vcc)
203                                 goto next_entry;
204                 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
205                         if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
206                                         != conf.Vcc)
207                                 goto next_entry;
208                 }
209
210                 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
211                         link->conf.Vpp =
212                                 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
213                 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
214                         link->conf.Vpp =
215                                 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
216
217                 /* we need an interrupt */
218                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
219                         link->conf.Attributes |= CONF_ENABLE_IRQ;
220
221                 /* IO window settings */
222                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
223                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
224                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
225
226                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
227                         link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
228                         link->io.BasePort1 = io->win[0].base;
229                         link->io.NumPorts1 = io->win[0].len;
230
231                         if (pcmcia_request_io(link, &link->io) != 0)
232                                 goto next_entry;
233                 }
234                 break;
235
236 next_entry:
237                 pcmcia_disable_device(link);
238                 last_ret = pcmcia_get_next_tuple(link, &tuple);
239         }
240
241         /* require an IRQ and two registers */
242         if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
243                 goto cs_failed;
244         if (link->conf.Attributes & CONF_ENABLE_IRQ)
245                 CS_CHECK(RequestIRQ,
246                         pcmcia_request_irq(link, &link->irq));
247         else
248                 goto cs_failed;
249
250         CS_CHECK(RequestConfiguration,
251                 pcmcia_request_configuration(link, &link->conf));
252
253         sprintf(dev->node.dev_name, driver_name);
254         dev->node.major = dev->node.minor = 0;
255         link->dev_node = &dev->node;
256
257         printk(KERN_INFO "%s: index 0x%02x: ",
258                dev->node.dev_name, link->conf.ConfigIndex);
259         if (link->conf.Vpp)
260                 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
261         printk(", irq %d", link->irq.AssignedIRQ);
262         printk(", io 0x%04x-0x%04x", link->io.BasePort1,
263                link->io.BasePort1+link->io.NumPorts1-1);
264         printk("\n");
265
266         if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
267                         < 0) {
268 cs_failed:
269                 printk("sl811_cs_config failed\n");
270                 cs_error(link, last_fn, last_ret);
271                 sl811_cs_release(link);
272                 return  -ENODEV;
273         }
274         return 0;
275 }
276
277 static int sl811_cs_probe(struct pcmcia_device *link)
278 {
279         local_info_t *local;
280
281         local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
282         if (!local)
283                 return -ENOMEM;
284         local->p_dev = link;
285         link->priv = local;
286
287         /* Initialize */
288         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
289         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
290         link->irq.Handler = NULL;
291
292         link->conf.Attributes = 0;
293         link->conf.IntType = INT_MEMORY_AND_IO;
294
295         return sl811_cs_config(link);
296 }
297
298 static struct pcmcia_device_id sl811_ids[] = {
299         PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
300         PCMCIA_DEVICE_NULL,
301 };
302 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
303
304 static struct pcmcia_driver sl811_cs_driver = {
305         .owner          = THIS_MODULE,
306         .drv            = {
307                 .name   = (char *)driver_name,
308         },
309         .probe          = sl811_cs_probe,
310         .remove         = sl811_cs_detach,
311         .id_table       = sl811_ids,
312 };
313
314 /*====================================================================*/
315
316 static int __init init_sl811_cs(void)
317 {
318         return pcmcia_register_driver(&sl811_cs_driver);
319 }
320 module_init(init_sl811_cs);
321
322 static void __exit exit_sl811_cs(void)
323 {
324         pcmcia_unregister_driver(&sl811_cs_driver);
325 }
326 module_exit(exit_sl811_cs);