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