Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ds.c -- 16-bit PCMCIA core support | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * The initial developer of the original code is David A. Hinds | |
9 | * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds | |
10 | * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. | |
11 | * | |
12 | * (C) 1999 David A. Hinds | |
3b659fb8 | 13 | * (C) 2003 - 2005 Dominik Brodowski |
1da177e4 LT |
14 | */ |
15 | ||
16 | #include <linux/config.h> | |
3b659fb8 | 17 | #include <linux/kernel.h> |
1da177e4 | 18 | #include <linux/module.h> |
1da177e4 | 19 | #include <linux/init.h> |
1da177e4 | 20 | #include <linux/errno.h> |
1da177e4 LT |
21 | #include <linux/list.h> |
22 | #include <linux/delay.h> | |
1da177e4 | 23 | #include <linux/workqueue.h> |
840c2ac5 | 24 | #include <linux/crc32.h> |
daa9517d | 25 | #include <linux/firmware.h> |
1da177e4 | 26 | |
1da177e4 | 27 | #define IN_CARD_SERVICES |
1da177e4 LT |
28 | #include <pcmcia/cs_types.h> |
29 | #include <pcmcia/cs.h> | |
1da177e4 LT |
30 | #include <pcmcia/cistpl.h> |
31 | #include <pcmcia/ds.h> | |
32 | #include <pcmcia/ss.h> | |
33 | ||
34 | #include "cs_internal.h" | |
e7a480d2 | 35 | #include "ds_internal.h" |
1da177e4 LT |
36 | |
37 | /*====================================================================*/ | |
38 | ||
39 | /* Module parameters */ | |
40 | ||
41 | MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>"); | |
42 | MODULE_DESCRIPTION("PCMCIA Driver Services"); | |
43 | MODULE_LICENSE("GPL"); | |
44 | ||
45 | #ifdef DEBUG | |
e7a480d2 | 46 | int ds_pc_debug; |
1da177e4 LT |
47 | |
48 | module_param_named(pc_debug, ds_pc_debug, int, 0644); | |
49 | ||
50 | #define ds_dbg(lvl, fmt, arg...) do { \ | |
51 | if (ds_pc_debug > (lvl)) \ | |
52 | printk(KERN_DEBUG "ds: " fmt , ## arg); \ | |
53 | } while (0) | |
54 | #else | |
55 | #define ds_dbg(lvl, fmt, arg...) do { } while (0) | |
56 | #endif | |
57 | ||
e7a480d2 | 58 | spinlock_t pcmcia_dev_list_lock; |
1da177e4 | 59 | |
dc109497 | 60 | static int unbind_request(struct pcmcia_socket *s); |
1da177e4 LT |
61 | |
62 | /*====================================================================*/ | |
63 | ||
64 | /* code which was in cs.c before */ | |
65 | ||
66 | /* String tables for error messages */ | |
67 | ||
68 | typedef struct lookup_t { | |
69 | int key; | |
70 | char *msg; | |
71 | } lookup_t; | |
72 | ||
73 | static const lookup_t error_table[] = { | |
74 | { CS_SUCCESS, "Operation succeeded" }, | |
75 | { CS_BAD_ADAPTER, "Bad adapter" }, | |
76 | { CS_BAD_ATTRIBUTE, "Bad attribute", }, | |
77 | { CS_BAD_BASE, "Bad base address" }, | |
78 | { CS_BAD_EDC, "Bad EDC" }, | |
79 | { CS_BAD_IRQ, "Bad IRQ" }, | |
80 | { CS_BAD_OFFSET, "Bad offset" }, | |
81 | { CS_BAD_PAGE, "Bad page number" }, | |
82 | { CS_READ_FAILURE, "Read failure" }, | |
83 | { CS_BAD_SIZE, "Bad size" }, | |
84 | { CS_BAD_SOCKET, "Bad socket" }, | |
85 | { CS_BAD_TYPE, "Bad type" }, | |
86 | { CS_BAD_VCC, "Bad Vcc" }, | |
87 | { CS_BAD_VPP, "Bad Vpp" }, | |
88 | { CS_BAD_WINDOW, "Bad window" }, | |
89 | { CS_WRITE_FAILURE, "Write failure" }, | |
90 | { CS_NO_CARD, "No card present" }, | |
91 | { CS_UNSUPPORTED_FUNCTION, "Usupported function" }, | |
92 | { CS_UNSUPPORTED_MODE, "Unsupported mode" }, | |
93 | { CS_BAD_SPEED, "Bad speed" }, | |
94 | { CS_BUSY, "Resource busy" }, | |
95 | { CS_GENERAL_FAILURE, "General failure" }, | |
96 | { CS_WRITE_PROTECTED, "Write protected" }, | |
97 | { CS_BAD_ARG_LENGTH, "Bad argument length" }, | |
98 | { CS_BAD_ARGS, "Bad arguments" }, | |
99 | { CS_CONFIGURATION_LOCKED, "Configuration locked" }, | |
100 | { CS_IN_USE, "Resource in use" }, | |
101 | { CS_NO_MORE_ITEMS, "No more items" }, | |
102 | { CS_OUT_OF_RESOURCE, "Out of resource" }, | |
103 | { CS_BAD_HANDLE, "Bad handle" }, | |
104 | { CS_BAD_TUPLE, "Bad CIS tuple" } | |
105 | }; | |
106 | ||
107 | ||
108 | static const lookup_t service_table[] = { | |
109 | { AccessConfigurationRegister, "AccessConfigurationRegister" }, | |
110 | { AddSocketServices, "AddSocketServices" }, | |
111 | { AdjustResourceInfo, "AdjustResourceInfo" }, | |
112 | { CheckEraseQueue, "CheckEraseQueue" }, | |
113 | { CloseMemory, "CloseMemory" }, | |
114 | { DeregisterClient, "DeregisterClient" }, | |
115 | { DeregisterEraseQueue, "DeregisterEraseQueue" }, | |
116 | { GetCardServicesInfo, "GetCardServicesInfo" }, | |
117 | { GetClientInfo, "GetClientInfo" }, | |
118 | { GetConfigurationInfo, "GetConfigurationInfo" }, | |
119 | { GetEventMask, "GetEventMask" }, | |
120 | { GetFirstClient, "GetFirstClient" }, | |
121 | { GetFirstRegion, "GetFirstRegion" }, | |
122 | { GetFirstTuple, "GetFirstTuple" }, | |
123 | { GetNextClient, "GetNextClient" }, | |
124 | { GetNextRegion, "GetNextRegion" }, | |
125 | { GetNextTuple, "GetNextTuple" }, | |
126 | { GetStatus, "GetStatus" }, | |
127 | { GetTupleData, "GetTupleData" }, | |
128 | { MapMemPage, "MapMemPage" }, | |
129 | { ModifyConfiguration, "ModifyConfiguration" }, | |
130 | { ModifyWindow, "ModifyWindow" }, | |
131 | { OpenMemory, "OpenMemory" }, | |
132 | { ParseTuple, "ParseTuple" }, | |
133 | { ReadMemory, "ReadMemory" }, | |
134 | { RegisterClient, "RegisterClient" }, | |
135 | { RegisterEraseQueue, "RegisterEraseQueue" }, | |
136 | { RegisterMTD, "RegisterMTD" }, | |
137 | { ReleaseConfiguration, "ReleaseConfiguration" }, | |
138 | { ReleaseIO, "ReleaseIO" }, | |
139 | { ReleaseIRQ, "ReleaseIRQ" }, | |
140 | { ReleaseWindow, "ReleaseWindow" }, | |
141 | { RequestConfiguration, "RequestConfiguration" }, | |
142 | { RequestIO, "RequestIO" }, | |
143 | { RequestIRQ, "RequestIRQ" }, | |
144 | { RequestSocketMask, "RequestSocketMask" }, | |
145 | { RequestWindow, "RequestWindow" }, | |
146 | { ResetCard, "ResetCard" }, | |
147 | { SetEventMask, "SetEventMask" }, | |
148 | { ValidateCIS, "ValidateCIS" }, | |
149 | { WriteMemory, "WriteMemory" }, | |
150 | { BindDevice, "BindDevice" }, | |
151 | { BindMTD, "BindMTD" }, | |
152 | { ReportError, "ReportError" }, | |
153 | { SuspendCard, "SuspendCard" }, | |
154 | { ResumeCard, "ResumeCard" }, | |
155 | { EjectCard, "EjectCard" }, | |
156 | { InsertCard, "InsertCard" }, | |
157 | { ReplaceCIS, "ReplaceCIS" } | |
158 | }; | |
159 | ||
160 | ||
2bc5a9bd | 161 | static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err) |
1da177e4 LT |
162 | { |
163 | int i; | |
164 | char *serv; | |
165 | ||
2bc5a9bd | 166 | if (!p_dev) |
1da177e4 | 167 | printk(KERN_NOTICE); |
2bc5a9bd | 168 | else |
1da177e4 | 169 | printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id); |
1da177e4 LT |
170 | |
171 | for (i = 0; i < ARRAY_SIZE(service_table); i++) | |
172 | if (service_table[i].key == err->func) | |
173 | break; | |
174 | if (i < ARRAY_SIZE(service_table)) | |
175 | serv = service_table[i].msg; | |
176 | else | |
177 | serv = "Unknown service number"; | |
178 | ||
179 | for (i = 0; i < ARRAY_SIZE(error_table); i++) | |
180 | if (error_table[i].key == err->retcode) | |
181 | break; | |
182 | if (i < ARRAY_SIZE(error_table)) | |
183 | printk("%s: %s\n", serv, error_table[i].msg); | |
184 | else | |
185 | printk("%s: Unknown error code %#x\n", serv, err->retcode); | |
186 | ||
187 | return CS_SUCCESS; | |
188 | } /* report_error */ | |
1da177e4 LT |
189 | |
190 | /* end of code which was in cs.c before */ | |
191 | ||
192 | /*======================================================================*/ | |
193 | ||
2bc5a9bd | 194 | void cs_error(struct pcmcia_device *p_dev, int func, int ret) |
1da177e4 LT |
195 | { |
196 | error_info_t err = { func, ret }; | |
2bc5a9bd | 197 | pcmcia_report_error(p_dev, &err); |
1da177e4 LT |
198 | } |
199 | EXPORT_SYMBOL(cs_error); | |
200 | ||
23a83bfe DB |
201 | |
202 | static void pcmcia_check_driver(struct pcmcia_driver *p_drv) | |
203 | { | |
204 | struct pcmcia_device_id *did = p_drv->id_table; | |
205 | unsigned int i; | |
206 | u32 hash; | |
207 | ||
1e212f36 | 208 | if (!p_drv->attach || !p_drv->event || !p_drv->detach) |
ba5bb6b5 PR |
209 | printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback " |
210 | "function\n", p_drv->drv.name); | |
1e212f36 | 211 | |
23a83bfe DB |
212 | while (did && did->match_flags) { |
213 | for (i=0; i<4; i++) { | |
214 | if (!did->prod_id[i]) | |
215 | continue; | |
216 | ||
217 | hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i])); | |
218 | if (hash == did->prod_id_hash[i]) | |
219 | continue; | |
220 | ||
221 | printk(KERN_DEBUG "pcmcia: %s: invalid hash for " | |
222 | "product string \"%s\": is 0x%x, should " | |
223 | "be 0x%x\n", p_drv->drv.name, did->prod_id[i], | |
224 | did->prod_id_hash[i], hash); | |
5085cb26 DB |
225 | printk(KERN_DEBUG "pcmcia: see " |
226 | "Documentation/pcmcia/devicetable.txt for " | |
227 | "details\n"); | |
23a83bfe DB |
228 | } |
229 | did++; | |
230 | } | |
231 | ||
232 | return; | |
233 | } | |
234 | ||
daa9517d DB |
235 | |
236 | #ifdef CONFIG_PCMCIA_LOAD_CIS | |
237 | ||
238 | /** | |
239 | * pcmcia_load_firmware - load CIS from userspace if device-provided is broken | |
240 | * @dev - the pcmcia device which needs a CIS override | |
241 | * @filename - requested filename in /lib/firmware/cis/ | |
242 | * | |
243 | * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if | |
244 | * the one provided by the card is broken. The firmware files reside in | |
245 | * /lib/firmware/cis/ in userspace. | |
246 | */ | |
247 | static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) | |
248 | { | |
249 | struct pcmcia_socket *s = dev->socket; | |
250 | const struct firmware *fw; | |
251 | char path[20]; | |
252 | int ret=-ENOMEM; | |
253 | cisdump_t *cis; | |
254 | ||
255 | if (!filename) | |
256 | return -EINVAL; | |
257 | ||
258 | ds_dbg(1, "trying to load firmware %s\n", filename); | |
259 | ||
260 | if (strlen(filename) > 14) | |
261 | return -EINVAL; | |
262 | ||
263 | snprintf(path, 20, "%s", filename); | |
264 | ||
265 | if (request_firmware(&fw, path, &dev->dev) == 0) { | |
266 | if (fw->size >= CISTPL_MAX_CIS_SIZE) | |
267 | goto release; | |
268 | ||
269 | cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL); | |
270 | if (!cis) | |
271 | goto release; | |
272 | ||
273 | memset(cis, 0, sizeof(cisdump_t)); | |
274 | ||
275 | cis->Length = fw->size + 1; | |
276 | memcpy(cis->Data, fw->data, fw->size); | |
277 | ||
278 | if (!pcmcia_replace_cis(s, cis)) | |
279 | ret = 0; | |
280 | } | |
281 | release: | |
282 | release_firmware(fw); | |
283 | ||
284 | return (ret); | |
285 | } | |
286 | ||
287 | #else /* !CONFIG_PCMCIA_LOAD_CIS */ | |
288 | ||
289 | static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) | |
290 | { | |
291 | return -ENODEV; | |
292 | } | |
293 | ||
294 | #endif | |
295 | ||
296 | ||
1da177e4 LT |
297 | /*======================================================================*/ |
298 | ||
1da177e4 | 299 | |
1da177e4 LT |
300 | /** |
301 | * pcmcia_register_driver - register a PCMCIA driver with the bus core | |
302 | * | |
303 | * Registers a PCMCIA driver with the PCMCIA bus core. | |
304 | */ | |
305 | static int pcmcia_device_probe(struct device *dev); | |
306 | static int pcmcia_device_remove(struct device * dev); | |
307 | ||
308 | int pcmcia_register_driver(struct pcmcia_driver *driver) | |
309 | { | |
310 | if (!driver) | |
311 | return -EINVAL; | |
312 | ||
23a83bfe DB |
313 | pcmcia_check_driver(driver); |
314 | ||
1da177e4 LT |
315 | /* initialize common fields */ |
316 | driver->drv.bus = &pcmcia_bus_type; | |
317 | driver->drv.owner = driver->owner; | |
318 | driver->drv.probe = pcmcia_device_probe; | |
319 | driver->drv.remove = pcmcia_device_remove; | |
320 | ||
321 | return driver_register(&driver->drv); | |
322 | } | |
323 | EXPORT_SYMBOL(pcmcia_register_driver); | |
324 | ||
325 | /** | |
326 | * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core | |
327 | */ | |
328 | void pcmcia_unregister_driver(struct pcmcia_driver *driver) | |
329 | { | |
330 | driver_unregister(&driver->drv); | |
331 | } | |
332 | EXPORT_SYMBOL(pcmcia_unregister_driver); | |
333 | ||
1da177e4 LT |
334 | |
335 | /* pcmcia_device handling */ | |
336 | ||
e7a480d2 | 337 | struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev) |
1da177e4 LT |
338 | { |
339 | struct device *tmp_dev; | |
340 | tmp_dev = get_device(&p_dev->dev); | |
341 | if (!tmp_dev) | |
342 | return NULL; | |
343 | return to_pcmcia_dev(tmp_dev); | |
344 | } | |
345 | ||
e7a480d2 | 346 | void pcmcia_put_dev(struct pcmcia_device *p_dev) |
1da177e4 LT |
347 | { |
348 | if (p_dev) | |
349 | put_device(&p_dev->dev); | |
350 | } | |
351 | ||
352 | static void pcmcia_release_dev(struct device *dev) | |
353 | { | |
354 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
355 | ds_dbg(1, "releasing dev %p\n", p_dev); | |
dc109497 | 356 | pcmcia_put_socket(p_dev->socket); |
bd65a685 | 357 | kfree(p_dev->devname); |
1da177e4 LT |
358 | kfree(p_dev); |
359 | } | |
360 | ||
361 | ||
362 | static int pcmcia_device_probe(struct device * dev) | |
363 | { | |
364 | struct pcmcia_device *p_dev; | |
365 | struct pcmcia_driver *p_drv; | |
366 | int ret = 0; | |
367 | ||
368 | dev = get_device(dev); | |
369 | if (!dev) | |
370 | return -ENODEV; | |
371 | ||
372 | p_dev = to_pcmcia_dev(dev); | |
373 | p_drv = to_pcmcia_drv(dev->driver); | |
374 | ||
375 | if (!try_module_get(p_drv->owner)) { | |
376 | ret = -EINVAL; | |
377 | goto put_dev; | |
378 | } | |
379 | ||
380 | if (p_drv->attach) { | |
381 | p_dev->instance = p_drv->attach(); | |
e12a9a93 | 382 | if ((!p_dev->instance) || (p_dev->state & CLIENT_UNBOUND)) { |
1da177e4 LT |
383 | printk(KERN_NOTICE "ds: unable to create instance " |
384 | "of '%s'!\n", p_drv->drv.name); | |
385 | ret = -EINVAL; | |
386 | } | |
387 | } | |
388 | ||
389 | if (ret) | |
390 | module_put(p_drv->owner); | |
391 | put_dev: | |
392 | if ((ret) || !(p_drv->attach)) | |
393 | put_device(dev); | |
394 | return (ret); | |
395 | } | |
396 | ||
397 | ||
398 | static int pcmcia_device_remove(struct device * dev) | |
399 | { | |
400 | struct pcmcia_device *p_dev; | |
401 | struct pcmcia_driver *p_drv; | |
402 | ||
403 | /* detach the "instance" */ | |
404 | p_dev = to_pcmcia_dev(dev); | |
405 | p_drv = to_pcmcia_drv(dev->driver); | |
406 | ||
407 | if (p_drv) { | |
408 | if ((p_drv->detach) && (p_dev->instance)) { | |
409 | p_drv->detach(p_dev->instance); | |
410 | /* from pcmcia_probe_device */ | |
411 | put_device(&p_dev->dev); | |
412 | } | |
413 | module_put(p_drv->owner); | |
414 | } | |
415 | ||
416 | return 0; | |
417 | } | |
418 | ||
419 | ||
420 | ||
421 | /* | |
422 | * pcmcia_device_query -- determine information about a pcmcia device | |
423 | */ | |
424 | static int pcmcia_device_query(struct pcmcia_device *p_dev) | |
425 | { | |
426 | cistpl_manfid_t manf_id; | |
427 | cistpl_funcid_t func_id; | |
76fa82fb | 428 | cistpl_vers_1_t *vers1; |
1da177e4 LT |
429 | unsigned int i; |
430 | ||
76fa82fb IM |
431 | vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL); |
432 | if (!vers1) | |
433 | return -ENOMEM; | |
434 | ||
1da177e4 LT |
435 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
436 | CISTPL_MANFID, &manf_id)) { | |
437 | p_dev->manf_id = manf_id.manf; | |
438 | p_dev->card_id = manf_id.card; | |
439 | p_dev->has_manf_id = 1; | |
440 | p_dev->has_card_id = 1; | |
441 | } | |
442 | ||
443 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, | |
444 | CISTPL_FUNCID, &func_id)) { | |
445 | p_dev->func_id = func_id.func; | |
446 | p_dev->has_func_id = 1; | |
447 | } else { | |
448 | /* rule of thumb: cards with no FUNCID, but with | |
449 | * common memory device geometry information, are | |
450 | * probably memory cards (from pcmcia-cs) */ | |
76fa82fb IM |
451 | cistpl_device_geo_t *devgeo; |
452 | ||
453 | devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL); | |
454 | if (!devgeo) { | |
455 | kfree(vers1); | |
456 | return -ENOMEM; | |
457 | } | |
1da177e4 | 458 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
76fa82fb | 459 | CISTPL_DEVICE_GEO, devgeo)) { |
1da177e4 LT |
460 | ds_dbg(0, "mem device geometry probably means " |
461 | "FUNCID_MEMORY\n"); | |
462 | p_dev->func_id = CISTPL_FUNCID_MEMORY; | |
463 | p_dev->has_func_id = 1; | |
464 | } | |
76fa82fb | 465 | kfree(devgeo); |
1da177e4 LT |
466 | } |
467 | ||
468 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1, | |
76fa82fb IM |
469 | vers1)) { |
470 | for (i=0; i < vers1->ns; i++) { | |
1da177e4 LT |
471 | char *tmp; |
472 | unsigned int length; | |
473 | ||
76fa82fb | 474 | tmp = vers1->str + vers1->ofs[i]; |
1da177e4 LT |
475 | |
476 | length = strlen(tmp) + 1; | |
477 | if ((length < 3) || (length > 255)) | |
478 | continue; | |
479 | ||
480 | p_dev->prod_id[i] = kmalloc(sizeof(char) * length, | |
481 | GFP_KERNEL); | |
482 | if (!p_dev->prod_id[i]) | |
483 | continue; | |
484 | ||
485 | p_dev->prod_id[i] = strncpy(p_dev->prod_id[i], | |
486 | tmp, length); | |
487 | } | |
488 | } | |
489 | ||
76fa82fb | 490 | kfree(vers1); |
1da177e4 LT |
491 | return 0; |
492 | } | |
493 | ||
494 | ||
495 | /* device_add_lock is needed to avoid double registration by cardmgr and kernel. | |
496 | * Serializes pcmcia_device_add; will most likely be removed in future. | |
497 | * | |
498 | * While it has the caveat that adding new PCMCIA devices inside(!) device_register() | |
499 | * won't work, this doesn't matter much at the moment: the driver core doesn't | |
500 | * support it either. | |
501 | */ | |
502 | static DECLARE_MUTEX(device_add_lock); | |
503 | ||
dc109497 | 504 | struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function) |
1da177e4 LT |
505 | { |
506 | struct pcmcia_device *p_dev; | |
507 | unsigned long flags; | |
bd65a685 | 508 | int bus_id_len; |
1da177e4 | 509 | |
dc109497 | 510 | s = pcmcia_get_socket(s); |
1da177e4 LT |
511 | if (!s) |
512 | return NULL; | |
513 | ||
514 | down(&device_add_lock); | |
515 | ||
1ad275e3 DB |
516 | /* max of 2 devices per card */ |
517 | if (s->device_count == 2) | |
518 | goto err_put; | |
519 | ||
1da177e4 LT |
520 | p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL); |
521 | if (!p_dev) | |
522 | goto err_put; | |
523 | memset(p_dev, 0, sizeof(struct pcmcia_device)); | |
524 | ||
dc109497 | 525 | p_dev->socket = s; |
1da177e4 LT |
526 | p_dev->device_no = (s->device_count++); |
527 | p_dev->func = function; | |
528 | ||
529 | p_dev->dev.bus = &pcmcia_bus_type; | |
dc109497 | 530 | p_dev->dev.parent = s->dev.dev; |
1da177e4 | 531 | p_dev->dev.release = pcmcia_release_dev; |
bd65a685 BG |
532 | bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no); |
533 | ||
534 | p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL); | |
535 | if (!p_dev->devname) | |
536 | goto err_free; | |
537 | sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id); | |
1da177e4 LT |
538 | |
539 | /* compat */ | |
e12a9a93 | 540 | p_dev->state = CLIENT_UNBOUND; |
1da177e4 LT |
541 | |
542 | /* Add to the list in pcmcia_bus_socket */ | |
543 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
544 | list_add_tail(&p_dev->socket_device_list, &s->devices_list); | |
545 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
546 | ||
807277cb DB |
547 | printk(KERN_NOTICE "pcmcia: registering new device %s\n", |
548 | p_dev->devname); | |
549 | ||
1ad275e3 DB |
550 | pcmcia_device_query(p_dev); |
551 | ||
1da177e4 LT |
552 | if (device_register(&p_dev->dev)) { |
553 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
554 | list_del(&p_dev->socket_device_list); | |
555 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
556 | ||
557 | goto err_free; | |
558 | } | |
559 | ||
560 | up(&device_add_lock); | |
561 | ||
562 | return p_dev; | |
563 | ||
564 | err_free: | |
bd65a685 | 565 | kfree(p_dev->devname); |
1da177e4 LT |
566 | kfree(p_dev); |
567 | s->device_count--; | |
568 | err_put: | |
569 | up(&device_add_lock); | |
dc109497 | 570 | pcmcia_put_socket(s); |
1da177e4 LT |
571 | |
572 | return NULL; | |
573 | } | |
574 | ||
575 | ||
576 | static int pcmcia_card_add(struct pcmcia_socket *s) | |
577 | { | |
578 | cisinfo_t cisinfo; | |
579 | cistpl_longlink_mfc_t mfc; | |
580 | unsigned int no_funcs, i; | |
581 | int ret = 0; | |
582 | ||
583 | if (!(s->resource_setup_done)) | |
584 | return -EAGAIN; /* try again, but later... */ | |
585 | ||
586 | pcmcia_validate_mem(s); | |
587 | ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo); | |
588 | if (ret || !cisinfo.Chains) { | |
589 | ds_dbg(0, "invalid CIS or invalid resources\n"); | |
590 | return -ENODEV; | |
591 | } | |
592 | ||
593 | if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc)) | |
594 | no_funcs = mfc.nfn; | |
595 | else | |
596 | no_funcs = 1; | |
597 | ||
1da177e4 | 598 | for (i=0; i < no_funcs; i++) |
dc109497 | 599 | pcmcia_device_add(s, i); |
1da177e4 LT |
600 | |
601 | return (ret); | |
602 | } | |
603 | ||
604 | ||
1ad275e3 DB |
605 | static void pcmcia_delayed_add_pseudo_device(void *data) |
606 | { | |
dc109497 | 607 | struct pcmcia_socket *s = data; |
1ad275e3 | 608 | pcmcia_device_add(s, 0); |
b5e43913 | 609 | s->pcmcia_state.device_add_pending = 0; |
1ad275e3 DB |
610 | } |
611 | ||
dc109497 | 612 | static inline void pcmcia_add_pseudo_device(struct pcmcia_socket *s) |
1ad275e3 | 613 | { |
b5e43913 | 614 | if (!s->pcmcia_state.device_add_pending) { |
b5e43913 | 615 | s->pcmcia_state.device_add_pending = 1; |
5d546f54 | 616 | schedule_work(&s->device_add); |
1ad275e3 DB |
617 | } |
618 | return; | |
619 | } | |
620 | ||
e2f0b534 | 621 | static int pcmcia_requery(struct device *dev, void * _data) |
ff1fa9ef | 622 | { |
e2f0b534 DB |
623 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
624 | if (!p_dev->dev.driver) | |
625 | pcmcia_device_query(p_dev); | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | static void pcmcia_bus_rescan(struct pcmcia_socket *skt) | |
631 | { | |
632 | int no_devices=0; | |
633 | unsigned long flags; | |
634 | ||
ff1fa9ef | 635 | /* must be called with skt_sem held */ |
e2f0b534 | 636 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
dc109497 | 637 | if (list_empty(&skt->devices_list)) |
e2f0b534 DB |
638 | no_devices=1; |
639 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
640 | ||
641 | /* if no devices were added for this socket yet because of | |
642 | * missing resource information or other trouble, we need to | |
643 | * do this now. */ | |
644 | if (no_devices) { | |
645 | int ret = pcmcia_card_add(skt); | |
646 | if (ret) | |
647 | return; | |
648 | } | |
649 | ||
650 | /* some device information might have changed because of a CIS | |
651 | * update or because we can finally read it correctly... so | |
652 | * determine it again, overwriting old values if necessary. */ | |
653 | bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery); | |
654 | ||
655 | /* we re-scan all devices, not just the ones connected to this | |
656 | * socket. This does not matter, though. */ | |
a5b55778 | 657 | bus_rescan_devices(&pcmcia_bus_type); |
ff1fa9ef | 658 | } |
1ad275e3 DB |
659 | |
660 | static inline int pcmcia_devmatch(struct pcmcia_device *dev, | |
661 | struct pcmcia_device_id *did) | |
662 | { | |
663 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) { | |
664 | if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id)) | |
665 | return 0; | |
666 | } | |
667 | ||
668 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) { | |
669 | if ((!dev->has_card_id) || (dev->card_id != did->card_id)) | |
670 | return 0; | |
671 | } | |
672 | ||
673 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) { | |
674 | if (dev->func != did->function) | |
675 | return 0; | |
676 | } | |
677 | ||
678 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) { | |
679 | if (!dev->prod_id[0]) | |
680 | return 0; | |
681 | if (strcmp(did->prod_id[0], dev->prod_id[0])) | |
682 | return 0; | |
683 | } | |
684 | ||
685 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) { | |
686 | if (!dev->prod_id[1]) | |
687 | return 0; | |
688 | if (strcmp(did->prod_id[1], dev->prod_id[1])) | |
689 | return 0; | |
690 | } | |
691 | ||
692 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) { | |
693 | if (!dev->prod_id[2]) | |
694 | return 0; | |
695 | if (strcmp(did->prod_id[2], dev->prod_id[2])) | |
696 | return 0; | |
697 | } | |
698 | ||
699 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) { | |
700 | if (!dev->prod_id[3]) | |
701 | return 0; | |
702 | if (strcmp(did->prod_id[3], dev->prod_id[3])) | |
703 | return 0; | |
704 | } | |
705 | ||
706 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) { | |
707 | /* handle pseudo multifunction devices: | |
708 | * there are at most two pseudo multifunction devices. | |
709 | * if we're matching against the first, schedule a | |
710 | * call which will then check whether there are two | |
711 | * pseudo devices, and if not, add the second one. | |
712 | */ | |
713 | if (dev->device_no == 0) | |
dc109497 | 714 | pcmcia_add_pseudo_device(dev->socket); |
1ad275e3 DB |
715 | |
716 | if (dev->device_no != did->device_no) | |
717 | return 0; | |
718 | } | |
719 | ||
720 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) { | |
721 | if ((!dev->has_func_id) || (dev->func_id != did->func_id)) | |
722 | return 0; | |
723 | ||
724 | /* if this is a pseudo-multi-function device, | |
725 | * we need explicit matches */ | |
726 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) | |
727 | return 0; | |
728 | if (dev->device_no) | |
729 | return 0; | |
730 | ||
731 | /* also, FUNC_ID matching needs to be activated by userspace | |
732 | * after it has re-checked that there is no possible module | |
733 | * with a prod_id/manf_id/card_id match. | |
734 | */ | |
735 | if (!dev->allow_func_id_match) | |
736 | return 0; | |
737 | } | |
738 | ||
ea7b3882 | 739 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) { |
daa9517d DB |
740 | if (!dev->socket->fake_cis) |
741 | pcmcia_load_firmware(dev, did->cisfile); | |
742 | ||
743 | if (!dev->socket->fake_cis) | |
ea7b3882 | 744 | return 0; |
ea7b3882 DB |
745 | } |
746 | ||
f602ff7e DB |
747 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) { |
748 | int i; | |
749 | for (i=0; i<4; i++) | |
750 | if (dev->prod_id[i]) | |
751 | return 0; | |
752 | if (dev->has_manf_id || dev->has_card_id || dev->has_func_id) | |
753 | return 0; | |
754 | } | |
755 | ||
1ad275e3 DB |
756 | dev->dev.driver_data = (void *) did; |
757 | ||
758 | return 1; | |
759 | } | |
760 | ||
761 | ||
1da177e4 LT |
762 | static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) { |
763 | struct pcmcia_device * p_dev = to_pcmcia_dev(dev); | |
764 | struct pcmcia_driver * p_drv = to_pcmcia_drv(drv); | |
1ad275e3 | 765 | struct pcmcia_device_id *did = p_drv->id_table; |
1da177e4 LT |
766 | |
767 | /* matching by cardmgr */ | |
768 | if (p_dev->cardmgr == p_drv) | |
769 | return 1; | |
770 | ||
1ad275e3 DB |
771 | while (did && did->match_flags) { |
772 | if (pcmcia_devmatch(p_dev, did)) | |
773 | return 1; | |
774 | did++; | |
775 | } | |
776 | ||
1da177e4 LT |
777 | return 0; |
778 | } | |
779 | ||
840c2ac5 DB |
780 | #ifdef CONFIG_HOTPLUG |
781 | ||
782 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, | |
783 | char *buffer, int buffer_size) | |
784 | { | |
785 | struct pcmcia_device *p_dev; | |
786 | int i, length = 0; | |
787 | u32 hash[4] = { 0, 0, 0, 0}; | |
788 | ||
789 | if (!dev) | |
790 | return -ENODEV; | |
791 | ||
792 | p_dev = to_pcmcia_dev(dev); | |
793 | ||
794 | /* calculate hashes */ | |
795 | for (i=0; i<4; i++) { | |
796 | if (!p_dev->prod_id[i]) | |
797 | continue; | |
798 | hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i])); | |
799 | } | |
800 | ||
801 | i = 0; | |
802 | ||
803 | if (add_hotplug_env_var(envp, num_envp, &i, | |
804 | buffer, buffer_size, &length, | |
805 | "SOCKET_NO=%u", | |
806 | p_dev->socket->sock)) | |
807 | return -ENOMEM; | |
808 | ||
809 | if (add_hotplug_env_var(envp, num_envp, &i, | |
810 | buffer, buffer_size, &length, | |
811 | "DEVICE_NO=%02X", | |
812 | p_dev->device_no)) | |
813 | return -ENOMEM; | |
814 | ||
815 | if (add_hotplug_env_var(envp, num_envp, &i, | |
816 | buffer, buffer_size, &length, | |
817 | "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" | |
818 | "pa%08Xpb%08Xpc%08Xpd%08X", | |
819 | p_dev->has_manf_id ? p_dev->manf_id : 0, | |
820 | p_dev->has_card_id ? p_dev->card_id : 0, | |
821 | p_dev->has_func_id ? p_dev->func_id : 0, | |
822 | p_dev->func, | |
823 | p_dev->device_no, | |
824 | hash[0], | |
825 | hash[1], | |
826 | hash[2], | |
827 | hash[3])) | |
828 | return -ENOMEM; | |
829 | ||
830 | envp[i] = NULL; | |
831 | ||
832 | return 0; | |
833 | } | |
834 | ||
835 | #else | |
836 | ||
837 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, | |
838 | char *buffer, int buffer_size) | |
839 | { | |
840 | return -ENODEV; | |
841 | } | |
842 | ||
843 | #endif | |
844 | ||
1da177e4 LT |
845 | /************************ per-device sysfs output ***************************/ |
846 | ||
847 | #define pcmcia_device_attr(field, test, format) \ | |
e404e274 | 848 | static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
849 | { \ |
850 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ | |
851 | return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \ | |
852 | } | |
853 | ||
854 | #define pcmcia_device_stringattr(name, field) \ | |
e404e274 | 855 | static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4 LT |
856 | { \ |
857 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ | |
858 | return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \ | |
859 | } | |
860 | ||
861 | pcmcia_device_attr(func, socket, "0x%02x\n"); | |
862 | pcmcia_device_attr(func_id, has_func_id, "0x%02x\n"); | |
863 | pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n"); | |
864 | pcmcia_device_attr(card_id, has_card_id, "0x%04x\n"); | |
865 | pcmcia_device_stringattr(prod_id1, prod_id[0]); | |
866 | pcmcia_device_stringattr(prod_id2, prod_id[1]); | |
867 | pcmcia_device_stringattr(prod_id3, prod_id[2]); | |
868 | pcmcia_device_stringattr(prod_id4, prod_id[3]); | |
869 | ||
3704511b | 870 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf) |
3248ff43 DB |
871 | { |
872 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
873 | int i; | |
874 | u32 hash[4] = { 0, 0, 0, 0}; | |
875 | ||
876 | /* calculate hashes */ | |
877 | for (i=0; i<4; i++) { | |
878 | if (!p_dev->prod_id[i]) | |
879 | continue; | |
880 | hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i])); | |
881 | } | |
882 | return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" | |
883 | "pa%08Xpb%08Xpc%08Xpd%08X\n", | |
884 | p_dev->has_manf_id ? p_dev->manf_id : 0, | |
885 | p_dev->has_card_id ? p_dev->card_id : 0, | |
886 | p_dev->has_func_id ? p_dev->func_id : 0, | |
887 | p_dev->func, p_dev->device_no, | |
888 | hash[0], hash[1], hash[2], hash[3]); | |
889 | } | |
a5b55778 | 890 | |
3248ff43 DB |
891 | static ssize_t pcmcia_store_allow_func_id_match(struct device *dev, |
892 | struct device_attribute *attr, const char *buf, size_t count) | |
a5b55778 DB |
893 | { |
894 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
895 | if (!count) | |
896 | return -EINVAL; | |
897 | ||
898 | down(&p_dev->socket->skt_sem); | |
899 | p_dev->allow_func_id_match = 1; | |
900 | up(&p_dev->socket->skt_sem); | |
901 | ||
902 | bus_rescan_devices(&pcmcia_bus_type); | |
903 | ||
904 | return count; | |
905 | } | |
906 | ||
1da177e4 LT |
907 | static struct device_attribute pcmcia_dev_attrs[] = { |
908 | __ATTR(function, 0444, func_show, NULL), | |
909 | __ATTR_RO(func_id), | |
910 | __ATTR_RO(manf_id), | |
911 | __ATTR_RO(card_id), | |
912 | __ATTR_RO(prod_id1), | |
913 | __ATTR_RO(prod_id2), | |
914 | __ATTR_RO(prod_id3), | |
915 | __ATTR_RO(prod_id4), | |
3248ff43 | 916 | __ATTR_RO(modalias), |
a5b55778 | 917 | __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match), |
1da177e4 LT |
918 | __ATTR_NULL, |
919 | }; | |
920 | ||
921 | ||
1da177e4 LT |
922 | /*====================================================================== |
923 | ||
924 | The card status event handler. | |
925 | ||
926 | ======================================================================*/ | |
927 | ||
928 | struct send_event_data { | |
929 | struct pcmcia_socket *skt; | |
930 | event_t event; | |
931 | int priority; | |
932 | }; | |
933 | ||
934 | static int send_event_callback(struct device *dev, void * _data) | |
935 | { | |
936 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); | |
1e212f36 | 937 | struct pcmcia_driver *p_drv; |
1da177e4 LT |
938 | struct send_event_data *data = _data; |
939 | ||
940 | /* we get called for all sockets, but may only pass the event | |
941 | * for drivers _on the affected socket_ */ | |
942 | if (p_dev->socket != data->skt) | |
943 | return 0; | |
944 | ||
1e212f36 DB |
945 | p_drv = to_pcmcia_drv(p_dev->dev.driver); |
946 | if (!p_drv) | |
947 | return 0; | |
948 | ||
e12a9a93 | 949 | if (p_dev->state & (CLIENT_UNBOUND|CLIENT_STALE)) |
1da177e4 LT |
950 | return 0; |
951 | ||
1e212f36 DB |
952 | if (p_drv->event) |
953 | return p_drv->event(data->event, data->priority, | |
954 | &p_dev->event_callback_args); | |
1da177e4 LT |
955 | |
956 | return 0; | |
957 | } | |
958 | ||
959 | static int send_event(struct pcmcia_socket *s, event_t event, int priority) | |
960 | { | |
1da177e4 | 961 | struct send_event_data private; |
1da177e4 LT |
962 | |
963 | private.skt = s; | |
964 | private.event = event; | |
965 | private.priority = priority; | |
966 | ||
dc109497 | 967 | return bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback); |
1da177e4 LT |
968 | } /* send_event */ |
969 | ||
970 | ||
971 | /* Normally, the event is passed to individual drivers after | |
972 | * informing userspace. Only for CS_EVENT_CARD_REMOVAL this | |
973 | * is inversed to maintain historic compatibility. | |
974 | */ | |
975 | ||
976 | static int ds_event(struct pcmcia_socket *skt, event_t event, int priority) | |
977 | { | |
dc109497 | 978 | struct pcmcia_socket *s = pcmcia_get_socket(skt); |
1da177e4 LT |
979 | int ret = 0; |
980 | ||
981 | ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n", | |
dc109497 | 982 | event, priority, skt); |
1da177e4 LT |
983 | |
984 | switch (event) { | |
985 | ||
986 | case CS_EVENT_CARD_REMOVAL: | |
b5e43913 | 987 | s->pcmcia_state.present = 0; |
1da177e4 | 988 | send_event(skt, event, priority); |
dc109497 DB |
989 | unbind_request(skt); |
990 | handle_event(skt, event); | |
1da177e4 LT |
991 | break; |
992 | ||
993 | case CS_EVENT_CARD_INSERTION: | |
b5e43913 | 994 | s->pcmcia_state.present = 1; |
1da177e4 | 995 | pcmcia_card_add(skt); |
dc109497 | 996 | handle_event(skt, event); |
1da177e4 LT |
997 | break; |
998 | ||
999 | case CS_EVENT_EJECTION_REQUEST: | |
1000 | ret = send_event(skt, event, priority); | |
1001 | break; | |
1002 | ||
1003 | default: | |
dc109497 | 1004 | handle_event(skt, event); |
1da177e4 LT |
1005 | send_event(skt, event, priority); |
1006 | break; | |
1007 | } | |
1008 | ||
dc109497 DB |
1009 | pcmcia_put_socket(s); |
1010 | ||
1da177e4 LT |
1011 | return 0; |
1012 | } /* ds_event */ | |
1013 | ||
1014 | ||
1da177e4 | 1015 | |
2bc5a9bd | 1016 | int pcmcia_register_client(struct pcmcia_device **handle, client_reg_t *req) |
1da177e4 | 1017 | { |
dc109497 | 1018 | struct pcmcia_socket *s = NULL; |
1da177e4 | 1019 | struct pcmcia_device *p_dev = NULL; |
1e212f36 | 1020 | struct pcmcia_driver *p_drv = NULL; |
1da177e4 LT |
1021 | |
1022 | /* Look for unbound client with matching dev_info */ | |
1023 | down_read(&pcmcia_socket_list_rwsem); | |
1024 | list_for_each_entry(s, &pcmcia_socket_list, socket_list) { | |
1025 | unsigned long flags; | |
1026 | ||
1027 | if (s->state & SOCKET_CARDBUS) | |
1028 | continue; | |
1029 | ||
dc109497 DB |
1030 | s = pcmcia_get_socket(s); |
1031 | if (!s) | |
1da177e4 LT |
1032 | continue; |
1033 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
dc109497 | 1034 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
1da177e4 LT |
1035 | p_dev = pcmcia_get_dev(p_dev); |
1036 | if (!p_dev) | |
1037 | continue; | |
e12a9a93 | 1038 | if (!(p_dev->state & CLIENT_UNBOUND) || |
1da177e4 LT |
1039 | (!p_dev->dev.driver)) { |
1040 | pcmcia_put_dev(p_dev); | |
1041 | continue; | |
1042 | } | |
1043 | p_drv = to_pcmcia_drv(p_dev->dev.driver); | |
1044 | if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) { | |
1da177e4 LT |
1045 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
1046 | goto found; | |
1047 | } | |
1048 | pcmcia_put_dev(p_dev); | |
1049 | } | |
1050 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
dc109497 | 1051 | pcmcia_put_socket(s); |
1da177e4 LT |
1052 | } |
1053 | found: | |
1054 | up_read(&pcmcia_socket_list_rwsem); | |
2bc5a9bd | 1055 | if (!p_dev) |
1da177e4 LT |
1056 | return -ENODEV; |
1057 | ||
dc109497 | 1058 | pcmcia_put_socket(s); /* safe, as we already hold a reference from bind_device */ |
1da177e4 | 1059 | |
e12a9a93 DB |
1060 | *handle = p_dev; |
1061 | p_dev->state &= ~CLIENT_UNBOUND; | |
1e212f36 | 1062 | p_dev->event_callback_args = req->event_callback_args; |
e12a9a93 | 1063 | p_dev->event_callback_args.client_handle = p_dev; |
1e212f36 | 1064 | |
1da177e4 | 1065 | |
2bc5a9bd | 1066 | if (!s->functions) { |
1da177e4 | 1067 | cistpl_longlink_mfc_t mfc; |
2bc5a9bd | 1068 | if (pccard_read_tuple(s, p_dev->func, CISTPL_LONGLINK_MFC, &mfc) |
1da177e4 LT |
1069 | == CS_SUCCESS) |
1070 | s->functions = mfc.nfn; | |
1071 | else | |
1072 | s->functions = 1; | |
1073 | s->config = kmalloc(sizeof(config_t) * s->functions, | |
1074 | GFP_KERNEL); | |
1075 | if (!s->config) | |
1076 | goto out_no_resource; | |
1077 | memset(s->config, 0, sizeof(config_t) * s->functions); | |
1078 | } | |
1079 | ||
1080 | ds_dbg(1, "register_client(): client 0x%p, dev %s\n", | |
2bc5a9bd | 1081 | p_dev, p_dev->dev.bus_id); |
1da177e4 LT |
1082 | |
1083 | if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) { | |
1e212f36 DB |
1084 | if (p_drv->event) |
1085 | p_drv->event(CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW, | |
1086 | &p_dev->event_callback_args); | |
1087 | ||
1da177e4 LT |
1088 | } |
1089 | ||
1090 | return CS_SUCCESS; | |
1091 | ||
1092 | out_no_resource: | |
1093 | pcmcia_put_dev(p_dev); | |
1094 | return CS_OUT_OF_RESOURCE; | |
1095 | } /* register_client */ | |
1096 | EXPORT_SYMBOL(pcmcia_register_client); | |
1097 | ||
1098 | ||
1da177e4 LT |
1099 | /* unbind _all_ devices attached to a given pcmcia_bus_socket. The |
1100 | * drivers have been called with EVENT_CARD_REMOVAL before. | |
1101 | */ | |
dc109497 | 1102 | static int unbind_request(struct pcmcia_socket *s) |
1da177e4 LT |
1103 | { |
1104 | struct pcmcia_device *p_dev; | |
1105 | unsigned long flags; | |
1106 | ||
dc109497 | 1107 | ds_dbg(2, "unbind_request(%d)\n", s->sock); |
1da177e4 LT |
1108 | |
1109 | s->device_count = 0; | |
1110 | ||
1111 | for (;;) { | |
1112 | /* unregister all pcmcia_devices registered with this socket*/ | |
1113 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); | |
1114 | if (list_empty(&s->devices_list)) { | |
1115 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); | |
1116 | return 0; | |
1117 | } | |
1118 | p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list); | |
1119 | list_del(&p_dev->socket_device_list); | |
e12a9a93 | 1120 | p_dev->state |= CLIENT_STALE; |
1da177e4 LT |
1121 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
1122 | ||
1123 | device_unregister(&p_dev->dev); | |
1124 | } | |
1125 | ||
1126 | return 0; | |
1127 | } /* unbind_request */ | |
1128 | ||
2bc5a9bd | 1129 | int pcmcia_deregister_client(struct pcmcia_device *p_dev) |
1da177e4 LT |
1130 | { |
1131 | struct pcmcia_socket *s; | |
1132 | int i; | |
1da177e4 | 1133 | |
2bc5a9bd DB |
1134 | s = p_dev->socket; |
1135 | ds_dbg(1, "deregister_client(%p)\n", p_dev); | |
1da177e4 | 1136 | |
2bc5a9bd | 1137 | if (p_dev->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED)) |
1da177e4 LT |
1138 | goto warn_out; |
1139 | for (i = 0; i < MAX_WIN; i++) | |
2bc5a9bd | 1140 | if (p_dev->state & CLIENT_WIN_REQ(i)) |
1da177e4 LT |
1141 | goto warn_out; |
1142 | ||
2bc5a9bd DB |
1143 | if (p_dev->state & CLIENT_STALE) { |
1144 | p_dev->state &= ~CLIENT_STALE; | |
1da177e4 LT |
1145 | pcmcia_put_dev(p_dev); |
1146 | } else { | |
2bc5a9bd | 1147 | p_dev->state = CLIENT_UNBOUND; |
1da177e4 LT |
1148 | } |
1149 | ||
1150 | return CS_SUCCESS; | |
1151 | warn_out: | |
1152 | printk(KERN_WARNING "ds: deregister_client was called too early.\n"); | |
1153 | return CS_IN_USE; | |
1154 | } /* deregister_client */ | |
1155 | EXPORT_SYMBOL(pcmcia_deregister_client); | |
1156 | ||
90c6cdd1 DB |
1157 | static struct pcmcia_callback pcmcia_bus_callback = { |
1158 | .owner = THIS_MODULE, | |
1159 | .event = ds_event, | |
1160 | .requery = pcmcia_bus_rescan, | |
1161 | }; | |
1162 | ||
d8539d81 DT |
1163 | static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev, |
1164 | struct class_interface *class_intf) | |
1da177e4 LT |
1165 | { |
1166 | struct pcmcia_socket *socket = class_get_devdata(class_dev); | |
1da177e4 LT |
1167 | int ret; |
1168 | ||
dc109497 DB |
1169 | socket = pcmcia_get_socket(socket); |
1170 | if (!socket) { | |
1da177e4 | 1171 | printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket); |
1da177e4 LT |
1172 | return -ENODEV; |
1173 | } | |
1174 | ||
1da177e4 LT |
1175 | /* |
1176 | * Ugly. But we want to wait for the socket threads to have started up. | |
1177 | * We really should let the drivers themselves drive some of this.. | |
1178 | */ | |
1179 | msleep(250); | |
1180 | ||
e7a480d2 | 1181 | #ifdef CONFIG_PCMCIA_IOCTL |
dc109497 | 1182 | init_waitqueue_head(&socket->queue); |
e7a480d2 | 1183 | #endif |
dc109497 DB |
1184 | INIT_LIST_HEAD(&socket->devices_list); |
1185 | INIT_WORK(&socket->device_add, pcmcia_delayed_add_pseudo_device, socket); | |
1186 | memset(&socket->pcmcia_state, 0, sizeof(u8)); | |
1187 | socket->device_count = 0; | |
1da177e4 | 1188 | |
90c6cdd1 | 1189 | ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback); |
1da177e4 LT |
1190 | if (ret) { |
1191 | printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket); | |
dc109497 | 1192 | pcmcia_put_socket(socket); |
1da177e4 LT |
1193 | return (ret); |
1194 | } | |
1195 | ||
1196 | return 0; | |
1197 | } | |
1198 | ||
d8539d81 DT |
1199 | static void pcmcia_bus_remove_socket(struct class_device *class_dev, |
1200 | struct class_interface *class_intf) | |
1da177e4 LT |
1201 | { |
1202 | struct pcmcia_socket *socket = class_get_devdata(class_dev); | |
1203 | ||
dc109497 | 1204 | if (!socket) |
1da177e4 LT |
1205 | return; |
1206 | ||
dc109497 | 1207 | socket->pcmcia_state.dead = 1; |
1da177e4 LT |
1208 | pccard_register_pcmcia(socket, NULL); |
1209 | ||
dc109497 | 1210 | pcmcia_put_socket(socket); |
1da177e4 LT |
1211 | |
1212 | return; | |
1213 | } | |
1214 | ||
1215 | ||
1216 | /* the pcmcia_bus_interface is used to handle pcmcia socket devices */ | |
1217 | static struct class_interface pcmcia_bus_interface = { | |
1218 | .class = &pcmcia_socket_class, | |
1219 | .add = &pcmcia_bus_add_socket, | |
1220 | .remove = &pcmcia_bus_remove_socket, | |
1221 | }; | |
1222 | ||
1223 | ||
e7a480d2 | 1224 | struct bus_type pcmcia_bus_type = { |
1da177e4 | 1225 | .name = "pcmcia", |
840c2ac5 | 1226 | .hotplug = pcmcia_bus_hotplug, |
1da177e4 LT |
1227 | .match = pcmcia_bus_match, |
1228 | .dev_attrs = pcmcia_dev_attrs, | |
1229 | }; | |
1da177e4 LT |
1230 | |
1231 | ||
1232 | static int __init init_pcmcia_bus(void) | |
1233 | { | |
1da177e4 LT |
1234 | spin_lock_init(&pcmcia_dev_list_lock); |
1235 | ||
1236 | bus_register(&pcmcia_bus_type); | |
1237 | class_interface_register(&pcmcia_bus_interface); | |
1238 | ||
e7a480d2 | 1239 | pcmcia_setup_ioctl(); |
1da177e4 LT |
1240 | |
1241 | return 0; | |
1242 | } | |
1243 | fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that | |
1244 | * pcmcia_socket_class is already registered */ | |
1245 | ||
1246 | ||
1247 | static void __exit exit_pcmcia_bus(void) | |
1248 | { | |
e7a480d2 | 1249 | pcmcia_cleanup_ioctl(); |
1da177e4 | 1250 | |
e7a480d2 | 1251 | class_interface_unregister(&pcmcia_bus_interface); |
1da177e4 LT |
1252 | |
1253 | bus_unregister(&pcmcia_bus_type); | |
1254 | } | |
1255 | module_exit(exit_pcmcia_bus); | |
1256 | ||
1257 | ||
1da177e4 | 1258 | MODULE_ALIAS("ds"); |