744c2cd809f196143f13ee185527448f9eedf72d
[linux-2.6.git] / 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/cistpl.h>
24 #include <pcmcia/cisreg.h>
25 #include <pcmcia/ds.h>
26
27 #include <linux/usb/sl811.h>
28
29 MODULE_AUTHOR("Botond Botyanszki");
30 MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
31 MODULE_LICENSE("GPL");
32
33
34 /*====================================================================*/
35 /* MACROS                                                             */
36 /*====================================================================*/
37
38 #define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
39
40 /*====================================================================*/
41 /* VARIABLES                                                          */
42 /*====================================================================*/
43
44 typedef struct local_info_t {
45         struct pcmcia_device    *p_dev;
46 } local_info_t;
47
48 static void sl811_cs_release(struct pcmcia_device * link);
49
50 /*====================================================================*/
51
52 static void release_platform_dev(struct device * dev)
53 {
54         dev_dbg(dev, "sl811_cs platform_dev release\n");
55         dev->parent = NULL;
56 }
57
58 static struct sl811_platform_data platform_data = {
59         .potpg          = 100,
60         .power          = 50,           /* == 100mA */
61         // .reset       = ... FIXME:  invoke CF reset on the card
62 };
63
64 static struct resource resources[] = {
65         [0] = {
66                 .flags  = IORESOURCE_IRQ,
67         },
68         [1] = {
69                 // .name   = "address",
70                 .flags  = IORESOURCE_IO,
71         },
72         [2] = {
73                 // .name   = "data",
74                 .flags  = IORESOURCE_IO,
75         },
76 };
77
78 extern struct platform_driver sl811h_driver;
79
80 static struct platform_device platform_dev = {
81         .id                     = -1,
82         .dev = {
83                 .platform_data = &platform_data,
84                 .release       = release_platform_dev,
85         },
86         .resource               = resources,
87         .num_resources          = ARRAY_SIZE(resources),
88 };
89
90 static int sl811_hc_init(struct device *parent, resource_size_t base_addr,
91                          int irq)
92 {
93         if (platform_dev.dev.parent)
94                 return -EBUSY;
95         platform_dev.dev.parent = parent;
96
97         /* finish seting up the platform device */
98         resources[0].start = irq;
99
100         resources[1].start = base_addr;
101         resources[1].end = base_addr;
102
103         resources[2].start = base_addr + 1;
104         resources[2].end   = base_addr + 1;
105
106         /* The driver core will probe for us.  We know sl811-hcd has been
107          * initialized already because of the link order dependency created
108          * by referencing "sl811h_driver".
109          */
110         platform_dev.name = sl811h_driver.driver.name;
111         return platform_device_register(&platform_dev);
112 }
113
114 /*====================================================================*/
115
116 static void sl811_cs_detach(struct pcmcia_device *link)
117 {
118         dev_dbg(&link->dev, "sl811_cs_detach\n");
119
120         sl811_cs_release(link);
121
122         /* This points to the parent local_info_t struct */
123         kfree(link->priv);
124 }
125
126 static void sl811_cs_release(struct pcmcia_device * link)
127 {
128         dev_dbg(&link->dev, "sl811_cs_release\n");
129
130         pcmcia_disable_device(link);
131         platform_device_unregister(&platform_dev);
132 }
133
134 static int sl811_cs_config_check(struct pcmcia_device *p_dev,
135                                  cistpl_cftable_entry_t *cfg,
136                                  cistpl_cftable_entry_t *dflt,
137                                  unsigned int vcc,
138                                  void *priv_data)
139 {
140         if (cfg->index == 0)
141                 return -ENODEV;
142
143         /* Use power settings for Vcc and Vpp if present */
144         /*  Note that the CIS values need to be rescaled */
145         if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
146                 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 != vcc)
147                         return -ENODEV;
148         } else if (dflt->vcc.present & (1<<CISTPL_POWER_VNOM)) {
149                 if (dflt->vcc.param[CISTPL_POWER_VNOM]/10000 != vcc)
150                         return -ENODEV;
151                 }
152
153         if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
154                 p_dev->vpp =
155                         cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
156         else if (dflt->vpp1.present & (1<<CISTPL_POWER_VNOM))
157                 p_dev->vpp =
158                         dflt->vpp1.param[CISTPL_POWER_VNOM]/10000;
159
160         /* we need an interrupt */
161         p_dev->config_flags |= CONF_ENABLE_IRQ;
162
163         /* IO window settings */
164         p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
165         if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
166                 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
167                 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
168
169                 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
170                 p_dev->resource[0]->start = io->win[0].base;
171                 p_dev->resource[0]->end = io->win[0].len;
172
173                 return pcmcia_request_io(p_dev);
174         }
175         pcmcia_disable_device(p_dev);
176         return -ENODEV;
177 }
178
179
180 static int sl811_cs_config(struct pcmcia_device *link)
181 {
182         struct device           *parent = &link->dev;
183         int                     ret;
184
185         dev_dbg(&link->dev, "sl811_cs_config\n");
186
187         if (pcmcia_loop_config(link, sl811_cs_config_check, NULL))
188                 goto failed;
189
190         /* require an IRQ and two registers */
191         if (resource_size(link->resource[0]) < 2)
192                 goto failed;
193
194         if (!link->irq)
195                 goto failed;
196
197         ret = pcmcia_enable_device(link);
198         if (ret)
199                 goto failed;
200
201         dev_info(&link->dev, "index 0x%02x: ",
202                 link->config_index);
203         if (link->vpp)
204                 printk(", Vpp %d.%d", link->vpp/10, link->vpp%10);
205         printk(", irq %d", link->irq);
206         printk(", io %pR", link->resource[0]);
207         printk("\n");
208
209         if (sl811_hc_init(parent, link->resource[0]->start, link->irq)
210                         < 0) {
211 failed:
212                 printk(KERN_WARNING "sl811_cs_config failed\n");
213                 sl811_cs_release(link);
214                 return  -ENODEV;
215         }
216         return 0;
217 }
218
219 static int sl811_cs_probe(struct pcmcia_device *link)
220 {
221         local_info_t *local;
222
223         local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
224         if (!local)
225                 return -ENOMEM;
226         local->p_dev = link;
227         link->priv = local;
228
229         return sl811_cs_config(link);
230 }
231
232 static struct pcmcia_device_id sl811_ids[] = {
233         PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
234         PCMCIA_DEVICE_NULL,
235 };
236 MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
237
238 static struct pcmcia_driver sl811_cs_driver = {
239         .owner          = THIS_MODULE,
240         .drv            = {
241                 .name   = "sl811_cs",
242         },
243         .probe          = sl811_cs_probe,
244         .remove         = sl811_cs_detach,
245         .id_table       = sl811_ids,
246 };
247
248 /*====================================================================*/
249
250 static int __init init_sl811_cs(void)
251 {
252         return pcmcia_register_driver(&sl811_cs_driver);
253 }
254 module_init(init_sl811_cs);
255
256 static void __exit exit_sl811_cs(void)
257 {
258         pcmcia_unregister_driver(&sl811_cs_driver);
259 }
260 module_exit(exit_sl811_cs);