[PATCH] pcmcia: remove dev_list from drivers
[pandora-kernel.git] / drivers / telephony / ixj_pcmcia.c
1 #include "ixj-ver.h"
2
3 #include <linux/module.h>
4
5 #include <linux/init.h>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>       /* printk() */
8 #include <linux/fs.h>           /* everything... */
9 #include <linux/errno.h>        /* error codes */
10 #include <linux/slab.h>
11
12 #include <pcmcia/cs_types.h>
13 #include <pcmcia/cs.h>
14 #include <pcmcia/cistpl.h>
15 #include <pcmcia/ds.h>
16
17 #include "ixj.h"
18
19 /*
20  *      PCMCIA service support for Quicknet cards
21  */
22  
23 #ifdef PCMCIA_DEBUG
24 static int pc_debug = PCMCIA_DEBUG;
25 module_param(pc_debug, int, 0644);
26 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
27 #else
28 #define DEBUG(n, args...)
29 #endif
30
31 typedef struct ixj_info_t {
32         int ndev;
33         dev_node_t node;
34         struct ixj *port;
35 } ixj_info_t;
36
37 static dev_link_t *ixj_attach(void);
38 static void ixj_detach(struct pcmcia_device *p_dev);
39 static void ixj_config(dev_link_t * link);
40 static void ixj_cs_release(dev_link_t * link);
41 static int ixj_event(event_t event, int priority, event_callback_args_t * args);
42 static dev_info_t dev_info = "ixj_cs";
43
44 static dev_link_t *ixj_attach(void)
45 {
46         client_reg_t client_reg;
47         dev_link_t *link;
48         int ret;
49         DEBUG(0, "ixj_attach()\n");
50         /* Create new ixj device */
51         link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
52         if (!link)
53                 return NULL;
54         memset(link, 0, sizeof(struct dev_link_t));
55         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
56         link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
57         link->io.IOAddrLines = 3;
58         link->conf.Vcc = 50;
59         link->conf.IntType = INT_MEMORY_AND_IO;
60         link->priv = kmalloc(sizeof(struct ixj_info_t), GFP_KERNEL);
61         if (!link->priv) {
62                 kfree(link);
63                 return NULL;
64         }
65         memset(link->priv, 0, sizeof(struct ixj_info_t));
66         /* Register with Card Services */
67         link->next = NULL;
68         client_reg.dev_info = &dev_info;
69         client_reg.Version = 0x0210;
70         client_reg.event_callback_args.client_data = link;
71         ret = pcmcia_register_client(&link->handle, &client_reg);
72         if (ret != CS_SUCCESS) {
73                 cs_error(link->handle, RegisterClient, ret);
74                 ixj_detach(link->handle);
75                 return NULL;
76         }
77         return link;
78 }
79
80 static void ixj_detach(struct pcmcia_device *p_dev)
81 {
82         dev_link_t *link = dev_to_instance(p_dev);
83
84         DEBUG(0, "ixj_detach(0x%p)\n", link);
85
86         link->state &= ~DEV_RELEASE_PENDING;
87         if (link->state & DEV_CONFIG)
88                 ixj_cs_release(link);
89
90         kfree(link->priv);
91         kfree(link);
92 }
93
94 #define CS_CHECK(fn, ret) \
95 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
96
97 static void ixj_get_serial(dev_link_t * link, IXJ * j)
98 {
99         client_handle_t handle;
100         tuple_t tuple;
101         u_short buf[128];
102         char *str;
103         int last_ret, last_fn, i, place;
104         handle = link->handle;
105         DEBUG(0, "ixj_get_serial(0x%p)\n", link);
106         tuple.TupleData = (cisdata_t *) buf;
107         tuple.TupleOffset = 0;
108         tuple.TupleDataMax = 80;
109         tuple.Attributes = 0;
110         tuple.DesiredTuple = CISTPL_VERS_1;
111         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
112         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
113         str = (char *) buf;
114         printk("PCMCIA Version %d.%d\n", str[0], str[1]);
115         str += 2;
116         printk("%s", str);
117         str = str + strlen(str) + 1;
118         printk(" %s", str);
119         str = str + strlen(str) + 1;
120         place = 1;
121         for (i = strlen(str) - 1; i >= 0; i--) {
122                 switch (str[i]) {
123                 case '0':
124                 case '1':
125                 case '2':
126                 case '3':
127                 case '4':
128                 case '5':
129                 case '6':
130                 case '7':
131                 case '8':
132                 case '9':
133                         j->serial += (str[i] - 48) * place;
134                         break;
135                 case 'A':
136                 case 'B':
137                 case 'C':
138                 case 'D':
139                 case 'E':
140                 case 'F':
141                         j->serial += (str[i] - 55) * place;
142                         break;
143                 case 'a':
144                 case 'b':
145                 case 'c':
146                 case 'd':
147                 case 'e':
148                 case 'f':
149                         j->serial += (str[i] - 87) * place;
150                         break;
151                 }
152                 place = place * 0x10;
153         }
154         str = str + strlen(str) + 1;
155         printk(" version %s\n", str);
156       cs_failed:
157         return;
158 }
159
160 static void ixj_config(dev_link_t * link)
161 {
162         IXJ *j;
163         client_handle_t handle;
164         ixj_info_t *info;
165         tuple_t tuple;
166         u_short buf[128];
167         cisparse_t parse;
168         config_info_t conf;
169         cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
170         cistpl_cftable_entry_t dflt =
171         {
172                 0
173         };
174         int last_ret, last_fn;
175         handle = link->handle;
176         info = link->priv;
177         DEBUG(0, "ixj_config(0x%p)\n", link);
178         tuple.TupleData = (cisdata_t *) buf;
179         tuple.TupleOffset = 0;
180         tuple.TupleDataMax = 255;
181         tuple.Attributes = 0;
182         tuple.DesiredTuple = CISTPL_CONFIG;
183         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
184         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
185         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
186         link->conf.ConfigBase = parse.config.base;
187         link->conf.Present = parse.config.rmask[0];
188         link->state |= DEV_CONFIG;
189         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
190         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
191         tuple.Attributes = 0;
192         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
193         while (1) {
194                 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
195                                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
196                         goto next_entry;
197                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
198                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
199                         link->conf.ConfigIndex = cfg->index;
200                         link->io.BasePort1 = io->win[0].base;
201                         link->io.NumPorts1 = io->win[0].len;
202                         if (io->nwin == 2) {
203                                 link->io.BasePort2 = io->win[1].base;
204                                 link->io.NumPorts2 = io->win[1].len;
205                         }
206                         if (pcmcia_request_io(link->handle, &link->io) != 0)
207                                 goto next_entry;
208                         /* If we've got this far, we're done */
209                         break;
210                 }
211               next_entry:
212                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
213                         dflt = *cfg;
214                 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
215         }
216
217         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
218
219         /*
220          *      Register the card with the core.
221          */     
222         j=ixj_pcmcia_probe(link->io.BasePort1,link->io.BasePort1 + 0x10);
223
224         info->ndev = 1;
225         info->node.major = PHONE_MAJOR;
226         link->dev = &info->node;
227         ixj_get_serial(link, j);
228         link->state &= ~DEV_CONFIG_PENDING;
229         return;
230       cs_failed:
231         cs_error(link->handle, last_fn, last_ret);
232         ixj_cs_release(link);
233 }
234
235 static void ixj_cs_release(dev_link_t *link)
236 {
237         ixj_info_t *info = link->priv;
238         DEBUG(0, "ixj_cs_release(0x%p)\n", link);
239         info->ndev = 0;
240         link->dev = NULL;
241         pcmcia_release_configuration(link->handle);
242         pcmcia_release_io(link->handle, &link->io);
243         link->state &= ~DEV_CONFIG;
244 }
245
246 static int ixj_suspend(struct pcmcia_device *dev)
247 {
248         dev_link_t *link = dev_to_instance(dev);
249
250         link->state |= DEV_SUSPEND;
251         if (link->state & DEV_CONFIG)
252                 pcmcia_release_configuration(link->handle);
253
254         return 0;
255 }
256
257 static int ixj_resume(struct pcmcia_device *dev)
258 {
259         dev_link_t *link = dev_to_instance(dev);
260
261         link->state &= ~DEV_SUSPEND;
262         if (DEV_OK(link))
263                 pcmcia_request_configuration(link->handle, &link->conf);
264
265         return 0;
266 }
267
268 static int ixj_event(event_t event, int priority, event_callback_args_t * args)
269 {
270         dev_link_t *link = args->client_data;
271         DEBUG(1, "ixj_event(0x%06x)\n", event);
272         switch (event) {
273         case CS_EVENT_CARD_INSERTION:
274                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
275                 ixj_config(link);
276                 break;
277         }
278         return 0;
279 }
280
281 static struct pcmcia_device_id ixj_ids[] = {
282         PCMCIA_DEVICE_MANF_CARD(0x0257, 0x0600),
283         PCMCIA_DEVICE_NULL
284 };
285 MODULE_DEVICE_TABLE(pcmcia, ixj_ids);
286
287 static struct pcmcia_driver ixj_driver = {
288         .owner          = THIS_MODULE,
289         .drv            = {
290                 .name   = "ixj_cs",
291         },
292         .attach         = ixj_attach,
293         .event          = ixj_event,
294         .remove         = ixj_detach,
295         .id_table       = ixj_ids,
296         .suspend        = ixj_suspend,
297         .resume         = ixj_resume,
298 };
299
300 static int __init ixj_pcmcia_init(void)
301 {
302         return pcmcia_register_driver(&ixj_driver);
303 }
304
305 static void ixj_pcmcia_exit(void)
306 {
307         pcmcia_unregister_driver(&ixj_driver);
308 }
309
310 module_init(ixj_pcmcia_init);
311 module_exit(ixj_pcmcia_exit);
312
313 MODULE_LICENSE("GPL");