ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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_PARM(pc_debug, "i");
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.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
73         client_reg.EventMask =
74             CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
75             CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
76             CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
77         client_reg.event_handler = &ixj_event;
78         client_reg.Version = 0x0210;
79         client_reg.event_callback_args.client_data = link;
80         ret = pcmcia_register_client(&link->handle, &client_reg);
81         if (ret != CS_SUCCESS) {
82                 cs_error(link->handle, RegisterClient, ret);
83                 ixj_detach(link);
84                 return NULL;
85         }
86         return link;
87 }
88
89 static void ixj_detach(dev_link_t * link)
90 {
91         dev_link_t **linkp;
92         int ret;
93         DEBUG(0, "ixj_detach(0x%p)\n", link);
94         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
95                 if (*linkp == link)
96                         break;
97         if (*linkp == NULL)
98                 return;
99         link->state &= ~DEV_RELEASE_PENDING;
100         if (link->state & DEV_CONFIG)
101                 ixj_cs_release(link);
102         if (link->handle) {
103                 ret = pcmcia_deregister_client(link->handle);
104                 if (ret != CS_SUCCESS)
105                         cs_error(link->handle, DeregisterClient, ret);
106         }
107         /* Unlink device structure, free bits */
108         *linkp = link->next;
109         kfree(link->priv);
110         kfree(link);
111 }
112
113 #define CS_CHECK(fn, ret) \
114 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
115
116 static void ixj_get_serial(dev_link_t * link, IXJ * j)
117 {
118         client_handle_t handle;
119         tuple_t tuple;
120         u_short buf[128];
121         char *str;
122         int last_ret, last_fn, i, place;
123         handle = link->handle;
124         DEBUG(0, "ixj_get_serial(0x%p)\n", link);
125         tuple.TupleData = (cisdata_t *) buf;
126         tuple.TupleOffset = 0;
127         tuple.TupleDataMax = 80;
128         tuple.Attributes = 0;
129         tuple.DesiredTuple = CISTPL_VERS_1;
130         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
131         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
132         str = (char *) buf;
133         printk("PCMCIA Version %d.%d\n", str[0], str[1]);
134         str += 2;
135         printk("%s", str);
136         str = str + strlen(str) + 1;
137         printk(" %s", str);
138         str = str + strlen(str) + 1;
139         place = 1;
140         for (i = strlen(str) - 1; i >= 0; i--) {
141                 switch (str[i]) {
142                 case '0':
143                 case '1':
144                 case '2':
145                 case '3':
146                 case '4':
147                 case '5':
148                 case '6':
149                 case '7':
150                 case '8':
151                 case '9':
152                         j->serial += (str[i] - 48) * place;
153                         break;
154                 case 'A':
155                 case 'B':
156                 case 'C':
157                 case 'D':
158                 case 'E':
159                 case 'F':
160                         j->serial += (str[i] - 55) * place;
161                         break;
162                 case 'a':
163                 case 'b':
164                 case 'c':
165                 case 'd':
166                 case 'e':
167                 case 'f':
168                         j->serial += (str[i] - 87) * place;
169                         break;
170                 }
171                 place = place * 0x10;
172         }
173         str = str + strlen(str) + 1;
174         printk(" version %s\n", str);
175       cs_failed:
176         return;
177 }
178
179 static void ixj_config(dev_link_t * link)
180 {
181         IXJ *j;
182         client_handle_t handle;
183         ixj_info_t *info;
184         tuple_t tuple;
185         u_short buf[128];
186         cisparse_t parse;
187         config_info_t conf;
188         cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
189         cistpl_cftable_entry_t dflt =
190         {
191                 0
192         };
193         int last_ret, last_fn;
194         handle = link->handle;
195         info = link->priv;
196         DEBUG(0, "ixj_config(0x%p)\n", link);
197         tuple.TupleData = (cisdata_t *) buf;
198         tuple.TupleOffset = 0;
199         tuple.TupleDataMax = 255;
200         tuple.Attributes = 0;
201         tuple.DesiredTuple = CISTPL_CONFIG;
202         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
203         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
204         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
205         link->conf.ConfigBase = parse.config.base;
206         link->conf.Present = parse.config.rmask[0];
207         link->state |= DEV_CONFIG;
208         CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
209         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
210         tuple.Attributes = 0;
211         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
212         while (1) {
213                 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
214                                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
215                         goto next_entry;
216                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
217                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
218                         link->conf.ConfigIndex = cfg->index;
219                         link->io.BasePort1 = io->win[0].base;
220                         link->io.NumPorts1 = io->win[0].len;
221                         if (io->nwin == 2) {
222                                 link->io.BasePort2 = io->win[1].base;
223                                 link->io.NumPorts2 = io->win[1].len;
224                         }
225                         if (pcmcia_request_io(link->handle, &link->io) != 0)
226                                 goto next_entry;
227                         /* If we've got this far, we're done */
228                         break;
229                 }
230               next_entry:
231                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
232                         dflt = *cfg;
233                 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
234         }
235
236         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
237
238         /*
239          *      Register the card with the core.
240          */     
241         j=ixj_pcmcia_probe(link->io.BasePort1,link->io.BasePort1 + 0x10);
242
243         info->ndev = 1;
244         info->node.major = PHONE_MAJOR;
245         link->dev = &info->node;
246         ixj_get_serial(link, j);
247         link->state &= ~DEV_CONFIG_PENDING;
248         return;
249       cs_failed:
250         cs_error(link->handle, last_fn, last_ret);
251         ixj_cs_release(link);
252 }
253
254 static void ixj_cs_release(dev_link_t *link)
255 {
256         ixj_info_t *info = link->priv;
257         DEBUG(0, "ixj_cs_release(0x%p)\n", link);
258         info->ndev = 0;
259         link->dev = NULL;
260         pcmcia_release_configuration(link->handle);
261         pcmcia_release_io(link->handle, &link->io);
262         link->state &= ~DEV_CONFIG;
263 }
264
265 static int ixj_event(event_t event, int priority, event_callback_args_t * args)
266 {
267         dev_link_t *link = args->client_data;
268         DEBUG(1, "ixj_event(0x%06x)\n", event);
269         switch (event) {
270         case CS_EVENT_CARD_REMOVAL:
271                 link->state &= ~DEV_PRESENT;
272                 if (link->state & DEV_CONFIG) {
273                         link->state |= DEV_RELEASE_PENDING;
274                         ixj_cs_release(link);
275                 }
276                 break;
277         case CS_EVENT_CARD_INSERTION:
278                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
279                 ixj_config(link);
280                 break;
281         case CS_EVENT_PM_SUSPEND:
282                 link->state |= DEV_SUSPEND;
283                 /* Fall through... */
284         case CS_EVENT_RESET_PHYSICAL:
285                 if (link->state & DEV_CONFIG)
286                         pcmcia_release_configuration(link->handle);
287                 break;
288         case CS_EVENT_PM_RESUME:
289                 link->state &= ~DEV_SUSPEND;
290                 /* Fall through... */
291         case CS_EVENT_CARD_RESET:
292                 if (DEV_OK(link))
293                         pcmcia_request_configuration(link->handle, &link->conf);
294                 break;
295         }
296         return 0;
297 }
298
299 static struct pcmcia_driver ixj_driver = {
300         .owner          = THIS_MODULE,
301         .drv            = {
302                 .name   = "ixj_cs",
303         },
304         .attach         = ixj_attach,
305         .detach         = ixj_detach,
306 };
307
308 static int __init ixj_pcmcia_init(void)
309 {
310         return pcmcia_register_driver(&ixj_driver);
311 }
312
313 static void ixj_pcmcia_exit(void)
314 {
315         pcmcia_unregister_driver(&ixj_driver);
316
317         /* XXX: this really needs to move into generic code.. */
318         while (dev_list != NULL)
319                 ixj_detach(dev_list);
320 }
321
322 module_init(ixj_pcmcia_init);
323 module_exit(ixj_pcmcia_exit);
324
325 MODULE_LICENSE("GPL");