[PATCH] pcmcia: embed dev_link_t into struct pcmcia_device
[linux-3.10.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 void ixj_detach(struct pcmcia_device *p_dev);
38 static void ixj_config(dev_link_t * link);
39 static void ixj_cs_release(dev_link_t * link);
40
41 static int ixj_attach(struct pcmcia_device *p_dev)
42 {
43         DEBUG(0, "ixj_attach()\n");
44         /* Create new ixj device */
45         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
46         p_dev->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
47         p_dev->io.IOAddrLines = 3;
48         p_dev->conf.IntType = INT_MEMORY_AND_IO;
49         p_dev->priv = kmalloc(sizeof(struct ixj_info_t), GFP_KERNEL);
50         if (!p_dev->priv) {
51                 return -ENOMEM;
52         }
53         memset(p_dev->priv, 0, sizeof(struct ixj_info_t));
54
55         p_dev->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
56         ixj_config(p_dev);
57
58         return 0;
59 }
60
61 static void ixj_detach(struct pcmcia_device *p_dev)
62 {
63         dev_link_t *link = dev_to_instance(p_dev);
64
65         DEBUG(0, "ixj_detach(0x%p)\n", link);
66
67         link->state &= ~DEV_RELEASE_PENDING;
68         if (link->state & DEV_CONFIG)
69                 ixj_cs_release(link);
70
71         kfree(link->priv);
72 }
73
74 #define CS_CHECK(fn, ret) \
75 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
76
77 static void ixj_get_serial(dev_link_t * link, IXJ * j)
78 {
79         client_handle_t handle;
80         tuple_t tuple;
81         u_short buf[128];
82         char *str;
83         int last_ret, last_fn, i, place;
84         handle = link->handle;
85         DEBUG(0, "ixj_get_serial(0x%p)\n", link);
86         tuple.TupleData = (cisdata_t *) buf;
87         tuple.TupleOffset = 0;
88         tuple.TupleDataMax = 80;
89         tuple.Attributes = 0;
90         tuple.DesiredTuple = CISTPL_VERS_1;
91         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
92         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
93         str = (char *) buf;
94         printk("PCMCIA Version %d.%d\n", str[0], str[1]);
95         str += 2;
96         printk("%s", str);
97         str = str + strlen(str) + 1;
98         printk(" %s", str);
99         str = str + strlen(str) + 1;
100         place = 1;
101         for (i = strlen(str) - 1; i >= 0; i--) {
102                 switch (str[i]) {
103                 case '0':
104                 case '1':
105                 case '2':
106                 case '3':
107                 case '4':
108                 case '5':
109                 case '6':
110                 case '7':
111                 case '8':
112                 case '9':
113                         j->serial += (str[i] - 48) * place;
114                         break;
115                 case 'A':
116                 case 'B':
117                 case 'C':
118                 case 'D':
119                 case 'E':
120                 case 'F':
121                         j->serial += (str[i] - 55) * place;
122                         break;
123                 case 'a':
124                 case 'b':
125                 case 'c':
126                 case 'd':
127                 case 'e':
128                 case 'f':
129                         j->serial += (str[i] - 87) * place;
130                         break;
131                 }
132                 place = place * 0x10;
133         }
134         str = str + strlen(str) + 1;
135         printk(" version %s\n", str);
136       cs_failed:
137         return;
138 }
139
140 static void ixj_config(dev_link_t * link)
141 {
142         IXJ *j;
143         client_handle_t handle;
144         ixj_info_t *info;
145         tuple_t tuple;
146         u_short buf[128];
147         cisparse_t parse;
148         cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
149         cistpl_cftable_entry_t dflt =
150         {
151                 0
152         };
153         int last_ret, last_fn;
154         handle = link->handle;
155         info = link->priv;
156         DEBUG(0, "ixj_config(0x%p)\n", link);
157         tuple.TupleData = (cisdata_t *) buf;
158         tuple.TupleOffset = 0;
159         tuple.TupleDataMax = 255;
160         tuple.Attributes = 0;
161         tuple.DesiredTuple = CISTPL_CONFIG;
162         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
163         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
164         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
165         link->conf.ConfigBase = parse.config.base;
166         link->conf.Present = parse.config.rmask[0];
167         link->state |= DEV_CONFIG;
168         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
169         tuple.Attributes = 0;
170         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
171         while (1) {
172                 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
173                                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
174                         goto next_entry;
175                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
176                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
177                         link->conf.ConfigIndex = cfg->index;
178                         link->io.BasePort1 = io->win[0].base;
179                         link->io.NumPorts1 = io->win[0].len;
180                         if (io->nwin == 2) {
181                                 link->io.BasePort2 = io->win[1].base;
182                                 link->io.NumPorts2 = io->win[1].len;
183                         }
184                         if (pcmcia_request_io(link->handle, &link->io) != 0)
185                                 goto next_entry;
186                         /* If we've got this far, we're done */
187                         break;
188                 }
189               next_entry:
190                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
191                         dflt = *cfg;
192                 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
193         }
194
195         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
196
197         /*
198          *      Register the card with the core.
199          */     
200         j=ixj_pcmcia_probe(link->io.BasePort1,link->io.BasePort1 + 0x10);
201
202         info->ndev = 1;
203         info->node.major = PHONE_MAJOR;
204         link->dev_node = &info->node;
205         ixj_get_serial(link, j);
206         link->state &= ~DEV_CONFIG_PENDING;
207         return;
208       cs_failed:
209         cs_error(link->handle, last_fn, last_ret);
210         ixj_cs_release(link);
211 }
212
213 static void ixj_cs_release(dev_link_t *link)
214 {
215         ixj_info_t *info = link->priv;
216         DEBUG(0, "ixj_cs_release(0x%p)\n", link);
217         info->ndev = 0;
218         pcmcia_disable_device(link->handle);
219 }
220
221 static struct pcmcia_device_id ixj_ids[] = {
222         PCMCIA_DEVICE_MANF_CARD(0x0257, 0x0600),
223         PCMCIA_DEVICE_NULL
224 };
225 MODULE_DEVICE_TABLE(pcmcia, ixj_ids);
226
227 static struct pcmcia_driver ixj_driver = {
228         .owner          = THIS_MODULE,
229         .drv            = {
230                 .name   = "ixj_cs",
231         },
232         .probe          = ixj_attach,
233         .remove         = ixj_detach,
234         .id_table       = ixj_ids,
235 };
236
237 static int __init ixj_pcmcia_init(void)
238 {
239         return pcmcia_register_driver(&ixj_driver);
240 }
241
242 static void ixj_pcmcia_exit(void)
243 {
244         pcmcia_unregister_driver(&ixj_driver);
245 }
246
247 module_init(ixj_pcmcia_init);
248 module_exit(ixj_pcmcia_exit);
249
250 MODULE_LICENSE("GPL");