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