1 /*======================================================================
2
3 CardBus device enabler
4
5 cb_enabler.c 1.31 2000/06/12 21:29:36
6
7 The contents of this file are subject to the Mozilla Public
8 License Version 1.1 (the "License"); you may not use this file
9 except in compliance with the License. You may obtain a copy of
10 the License at http://www.mozilla.org/MPL/
11
12 Software distributed under the License is distributed on an "AS
13 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 implied. See the License for the specific language governing
15 rights and limitations under the License.
16
17 The initial developer of the original code is David A. Hinds
18 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
19 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
20
21 Alternatively, the contents of this file may be used under the
22 terms of the GNU Public License version 2 (the "GPL"), in which
23 case the provisions of the GPL are applicable instead of the
24 above. If you wish to allow the use of your version of this file
25 only under the terms of the GPL and not to allow others to use
26 your version of this file under the MPL, indicate your decision
27 by deleting the provisions above and replace them with the notice
28 and other provisions required by the GPL. If you do not delete
29 the provisions above, a recipient may use your version of this
30 file under either the MPL or the GPL.
31
32 The general idea:
33
34 A client driver registers using register_driver(). This module
35 then creates a Card Services pseudo-client and registers it, and
36 configures the socket if this is the first client. It then
37 invokes the appropriate PCI client routines in response to Card
38 Services events.
39
40 ======================================================================*/
41
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/malloc.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
49
50 #include <pcmcia/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ds.h>
55
56 #ifdef PCMCIA_DEBUG
57 static int pc_debug = PCMCIA_DEBUG;
58 MODULE_PARM(pc_debug, "i");
59 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
60 static char *version =
61 "cb_enabler.c 1.31 2000/06/12 21:29:36 (David Hinds)";
62 #else
63 #define DEBUG(n, args...) do { } while (0)
64 #endif
65
66 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
67 MODULE_DESCRIPTION("CardBus stub enabler module");
68
69 /*====================================================================*/
70
71 /* Parameters that can be set with 'insmod' */
72
73 /*====================================================================*/
74
75 typedef struct driver_info_t {
76 dev_link_t *(*attach)(void);
77 dev_info_t dev_info;
78 driver_operations *ops;
79 dev_link_t *dev_list;
80 } driver_info_t;
81
82 static dev_link_t *cb_attach(int n);
83 #define MK_ENTRY(fn, n) \
84 static dev_link_t *fn(void) { return cb_attach(n); }
85
86 #define MAX_DRIVER 4
87
88 MK_ENTRY(attach_0, 0);
89 MK_ENTRY(attach_1, 1);
90 MK_ENTRY(attach_2, 2);
91 MK_ENTRY(attach_3, 3);
92
93 static driver_info_t driver[4] = {
94 { attach_0 }, { attach_1 }, { attach_2 }, { attach_3 }
95 };
96
97 typedef struct bus_info_t {
98 u_char bus;
99 int flags, ncfg, nuse;
100 dev_link_t *owner;
101 } bus_info_t;
102
103 #define DID_REQUEST 1
104 #define DID_CONFIG 2
105
106 static void cb_release(u_long arg);
107 static int cb_event(event_t event, int priority,
108 event_callback_args_t *args);
109
110 static void cb_detach(dev_link_t *);
111
112 static bus_info_t bus_table[MAX_DRIVER];
113
114 /*====================================================================*/
115
116 static void cs_error(client_handle_t handle, int func, int ret)
117 {
118 error_info_t err = { func, ret };
119 pcmcia_report_error(handle, &err);
120 }
121
122 /*====================================================================*/
123
124 struct dev_link_t *cb_attach(int n)
125 {
126 client_reg_t client_reg;
127 dev_link_t *link;
128 int ret;
129
130 DEBUG(0, "cb_attach(%d)\n", n);
131
132 link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
133 if (!link) return NULL;
134
135 MOD_INC_USE_COUNT;
136 memset(link, 0, sizeof(struct dev_link_t));
137 link->conf.IntType = INT_CARDBUS;
138 link->conf.Vcc = 33;
139
140 /* Insert into instance chain for this driver */
141 link->priv = &driver[n];
142 link->next = driver[n].dev_list;
143 driver[n].dev_list = link;
144
145 /* Register with Card Services */
146 client_reg.dev_info = &driver[n].dev_info;
147 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
148 client_reg.event_handler = &cb_event;
149 client_reg.EventMask = CS_EVENT_RESET_PHYSICAL |
150 CS_EVENT_RESET_REQUEST | CS_EVENT_CARD_RESET |
151 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
152 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
153 client_reg.Version = 0x0210;
154 client_reg.event_callback_args.client_data = link;
155 ret = pcmcia_register_client(&link->handle, &client_reg);
156 if (ret != 0) {
157 cs_error(link->handle, RegisterClient, ret);
158 cb_detach(link);
159 return NULL;
160 }
161 return link;
162 }
163
164 /*====================================================================*/
165
166 static void cb_detach(dev_link_t *link)
167 {
168 driver_info_t *dev = link->priv;
169 dev_link_t **linkp;
170 bus_info_t *b = (void *)link->win;
171
172 DEBUG(0, "cb_detach(0x%p)\n", link);
173
174 /* Locate device structure */
175 for (linkp = &dev->dev_list; *linkp; linkp = &(*linkp)->next)
176 if (*linkp == link) break;
177 if (*linkp == NULL)
178 return;
179
180 if (link->state & DEV_CONFIG)
181 cb_release((u_long)link);
182
183 /* Don't drop Card Services connection if we are the bus owner */
184 if (b && (b->flags != 0) && (link == b->owner)) {
185 link->state |= DEV_STALE_LINK;
186 return;
187 }
188
189 if (link->handle)
190 pcmcia_deregister_client(link->handle);
191
192 *linkp = link->next;
193 kfree(link);
194 MOD_DEC_USE_COUNT;
195 }
196
197 /*====================================================================*/
198
199 static void cb_config(dev_link_t *link)
200 {
201 client_handle_t handle = link->handle;
202 driver_info_t *drv = link->priv;
203 dev_locator_t loc;
204 bus_info_t *b;
205 config_info_t config;
206 u_char bus, devfn;
207 int i;
208
209 DEBUG(0, "cb_config(0x%p)\n", link);
210 link->state |= DEV_CONFIG;
211
212 /* Get PCI bus info */
213 pcmcia_get_configuration_info(handle, &config);
214 bus = config.Option; devfn = config.Function;
215
216 /* Is this a new bus? */
217 for (i = 0; i < MAX_DRIVER; i++)
218 if (bus == bus_table[i].bus) break;
219 if (i == MAX_DRIVER) {
220 for (i = 0; i < MAX_DRIVER; i++)
221 if (bus_table[i].bus == 0) break;
222 b = &bus_table[i]; link->win = (void *)b;
223 b->bus = bus;
224 b->flags = 0;
225 b->ncfg = b->nuse = 1;
226
227 /* Special hook: CS know what to do... */
228 i = pcmcia_request_io(handle, NULL);
229 if (i != CS_SUCCESS) {
230 cs_error(handle, RequestIO, i);
231 return;
232 }
233 b->flags |= DID_REQUEST;
234 b->owner = link;
235 i = pcmcia_request_configuration(handle, &link->conf);
236 if (i != CS_SUCCESS) {
237 cs_error(handle, RequestConfiguration, i);
238 return;
239 }
240 b->flags |= DID_CONFIG;
241 } else {
242 b = &bus_table[i]; link->win = (void *)b;
243 if (b->flags & DID_CONFIG) {
244 b->ncfg++; b->nuse++;
245 }
246 }
247 loc.bus = LOC_PCI;
248 loc.b.pci.bus = bus;
249 loc.b.pci.devfn = devfn;
250 link->dev = drv->ops->attach(&loc);
251
252 link->state &= ~DEV_CONFIG_PENDING;
253 }
254
255 /*====================================================================*/
256
257 static void cb_release(u_long arg)
258 {
259 dev_link_t *link = (dev_link_t *)arg;
260 driver_info_t *drv = link->priv;
261 bus_info_t *b = (void *)link->win;
262
263 DEBUG(0, "cb_release(0x%p)\n", link);
264
265 if (link->dev != NULL) {
266 drv->ops->detach(link->dev);
267 link->dev = NULL;
268 }
269 if (link->state & DEV_CONFIG) {
270 /* If we're suspended, config was already released */
271 if (link->state & DEV_SUSPEND)
272 b->flags &= ~DID_CONFIG;
273 else if ((b->flags & DID_CONFIG) && (--b->ncfg == 0)) {
274 pcmcia_release_configuration(b->owner->handle);
275 b->flags &= ~DID_CONFIG;
276 }
277 if ((b->flags & DID_REQUEST) && (--b->nuse == 0)) {
278 pcmcia_release_io(b->owner->handle, NULL);
279 b->flags &= ~DID_REQUEST;
280 }
281 if (b->flags == 0) {
282 if (b->owner && (b->owner->state & DEV_STALE_LINK))
283 cb_detach(b->owner);
284 b->bus = 0; b->owner = NULL;
285 }
286 }
287 link->state &= ~DEV_CONFIG;
288 }
289
290 /*====================================================================*/
291
292 static int cb_event(event_t event, int priority,
293 event_callback_args_t *args)
294 {
295 dev_link_t *link = args->client_data;
296 driver_info_t *drv = link->priv;
297 bus_info_t *b = (void *)link->win;
298
299 DEBUG(0, "cb_event(0x%06x)\n", event);
300
301 switch (event) {
302 case CS_EVENT_CARD_REMOVAL:
303 link->state &= ~DEV_PRESENT;
304 if (link->state & DEV_CONFIG)
305 cb_release((u_long)link);
306 break;
307 case CS_EVENT_CARD_INSERTION:
308 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
309 cb_config(link);
310 break;
311 case CS_EVENT_PM_SUSPEND:
312 link->state |= DEV_SUSPEND;
313 /* Fall through... */
314 case CS_EVENT_RESET_PHYSICAL:
315 if (link->state & DEV_CONFIG) {
316 if (drv->ops->suspend != NULL)
317 drv->ops->suspend(link->dev);
318 b->ncfg--;
319 if (b->ncfg == 0)
320 pcmcia_release_configuration(link->handle);
321 }
322 break;
323 case CS_EVENT_PM_RESUME:
324 link->state &= ~DEV_SUSPEND;
325 /* Fall through... */
326 case CS_EVENT_CARD_RESET:
327 if (link->state & DEV_CONFIG) {
328 b->ncfg++;
329 if (b->ncfg == 1)
330 pcmcia_request_configuration(link->handle,
331 &link->conf);
332 if (drv->ops->resume != NULL)
333 drv->ops->resume(link->dev);
334 }
335 break;
336 }
337 return 0;
338 }
339
340 /*====================================================================*/
341
342 int register_driver(struct driver_operations *ops)
343 {
344 int i;
345
346 DEBUG(0, "register_driver('%s')\n", ops->name);
347
348 for (i = 0; i < MAX_DRIVER; i++)
349 if (driver[i].ops == NULL) break;
350 if (i == MAX_DRIVER)
351 return -1;
352
353 MOD_INC_USE_COUNT;
354 driver[i].ops = ops;
355 strcpy(driver[i].dev_info, ops->name);
356 register_pccard_driver(&driver[i].dev_info, driver[i].attach,
357 &cb_detach);
358 return 0;
359 }
360
361 void unregister_driver(struct driver_operations *ops)
362 {
363 int i;
364
365 DEBUG(0, "unregister_driver('%s')\n", ops->name);
366 for (i = 0; i < MAX_DRIVER; i++)
367 if (driver[i].ops == ops) break;
368 if (i < MAX_DRIVER) {
369 unregister_pccard_driver(&driver[i].dev_info);
370 driver[i].ops = NULL;
371 MOD_DEC_USE_COUNT;
372 }
373 }
374
375 /*====================================================================*/
376
377 EXPORT_SYMBOL(register_driver);
378 EXPORT_SYMBOL(unregister_driver);
379
380 static int __init init_cb_enabler(void)
381 {
382 servinfo_t serv;
383 DEBUG(0, "%s\n", version);
384 pcmcia_get_card_services_info(&serv);
385 if (serv.Revision != CS_RELEASE_CODE) {
386 printk(KERN_NOTICE "cb_enabler: Card Services release "
387 "does not match!\n");
388 return -1;
389 }
390 return 0;
391 }
392
393 static void __exit exit_cb_enabler(void)
394 {
395 DEBUG(0, "cb_enabler: unloading\n");
396 }
397
398 module_init(init_cb_enabler);
399 module_exit(exit_cb_enabler);
400
401 /*====================================================================*/
402
403
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.