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