[PATCH] dvb: ttusb-budget: use time_after_eq()
[deliverable/linux.git] / drivers / pcmcia / ds.c
CommitLineData
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
41MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
42MODULE_DESCRIPTION("PCMCIA Driver Services");
43MODULE_LICENSE("GPL");
44
45#ifdef DEBUG
e7a480d2 46int ds_pc_debug;
1da177e4
LT
47
48module_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 58spinlock_t pcmcia_dev_list_lock;
1da177e4 59
dc109497 60static 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
68typedef struct lookup_t {
69 int key;
70 char *msg;
71} lookup_t;
72
73static 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
108static 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 161static 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 194void 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}
199EXPORT_SYMBOL(cs_error);
200
23a83bfe
DB
201
202static 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 */
247static 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
289static 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 */
305static int pcmcia_device_probe(struct device *dev);
306static int pcmcia_device_remove(struct device * dev);
307
308int 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}
323EXPORT_SYMBOL(pcmcia_register_driver);
324
325/**
326 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
327 */
328void pcmcia_unregister_driver(struct pcmcia_driver *driver)
329{
330 driver_unregister(&driver->drv);
331}
332EXPORT_SYMBOL(pcmcia_unregister_driver);
333
1da177e4
LT
334
335/* pcmcia_device handling */
336
e7a480d2 337struct 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 346void pcmcia_put_dev(struct pcmcia_device *p_dev)
1da177e4
LT
347{
348 if (p_dev)
349 put_device(&p_dev->dev);
350}
351
352static 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);
1da177e4
LT
357 kfree(p_dev);
358}
359
360
361static int pcmcia_device_probe(struct device * dev)
362{
363 struct pcmcia_device *p_dev;
364 struct pcmcia_driver *p_drv;
365 int ret = 0;
366
367 dev = get_device(dev);
368 if (!dev)
369 return -ENODEV;
370
371 p_dev = to_pcmcia_dev(dev);
372 p_drv = to_pcmcia_drv(dev->driver);
373
374 if (!try_module_get(p_drv->owner)) {
375 ret = -EINVAL;
376 goto put_dev;
377 }
378
379 if (p_drv->attach) {
380 p_dev->instance = p_drv->attach();
e12a9a93 381 if ((!p_dev->instance) || (p_dev->state & CLIENT_UNBOUND)) {
1da177e4
LT
382 printk(KERN_NOTICE "ds: unable to create instance "
383 "of '%s'!\n", p_drv->drv.name);
384 ret = -EINVAL;
385 }
386 }
387
388 if (ret)
389 module_put(p_drv->owner);
390 put_dev:
391 if ((ret) || !(p_drv->attach))
392 put_device(dev);
393 return (ret);
394}
395
396
397static int pcmcia_device_remove(struct device * dev)
398{
399 struct pcmcia_device *p_dev;
400 struct pcmcia_driver *p_drv;
401
402 /* detach the "instance" */
403 p_dev = to_pcmcia_dev(dev);
404 p_drv = to_pcmcia_drv(dev->driver);
405
406 if (p_drv) {
407 if ((p_drv->detach) && (p_dev->instance)) {
408 p_drv->detach(p_dev->instance);
409 /* from pcmcia_probe_device */
410 put_device(&p_dev->dev);
411 }
412 module_put(p_drv->owner);
413 }
414
415 return 0;
416}
417
418
419
420/*
421 * pcmcia_device_query -- determine information about a pcmcia device
422 */
423static int pcmcia_device_query(struct pcmcia_device *p_dev)
424{
425 cistpl_manfid_t manf_id;
426 cistpl_funcid_t func_id;
427 cistpl_vers_1_t vers1;
428 unsigned int i;
429
430 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
431 CISTPL_MANFID, &manf_id)) {
432 p_dev->manf_id = manf_id.manf;
433 p_dev->card_id = manf_id.card;
434 p_dev->has_manf_id = 1;
435 p_dev->has_card_id = 1;
436 }
437
438 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
439 CISTPL_FUNCID, &func_id)) {
440 p_dev->func_id = func_id.func;
441 p_dev->has_func_id = 1;
442 } else {
443 /* rule of thumb: cards with no FUNCID, but with
444 * common memory device geometry information, are
445 * probably memory cards (from pcmcia-cs) */
446 cistpl_device_geo_t devgeo;
447 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
448 CISTPL_DEVICE_GEO, &devgeo)) {
449 ds_dbg(0, "mem device geometry probably means "
450 "FUNCID_MEMORY\n");
451 p_dev->func_id = CISTPL_FUNCID_MEMORY;
452 p_dev->has_func_id = 1;
453 }
454 }
455
456 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
457 &vers1)) {
458 for (i=0; i < vers1.ns; i++) {
459 char *tmp;
460 unsigned int length;
461
462 tmp = vers1.str + vers1.ofs[i];
463
464 length = strlen(tmp) + 1;
465 if ((length < 3) || (length > 255))
466 continue;
467
468 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
469 GFP_KERNEL);
470 if (!p_dev->prod_id[i])
471 continue;
472
473 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
474 tmp, length);
475 }
476 }
477
478 return 0;
479}
480
481
482/* device_add_lock is needed to avoid double registration by cardmgr and kernel.
483 * Serializes pcmcia_device_add; will most likely be removed in future.
484 *
485 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
486 * won't work, this doesn't matter much at the moment: the driver core doesn't
487 * support it either.
488 */
489static DECLARE_MUTEX(device_add_lock);
490
dc109497 491struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
1da177e4
LT
492{
493 struct pcmcia_device *p_dev;
494 unsigned long flags;
495
dc109497 496 s = pcmcia_get_socket(s);
1da177e4
LT
497 if (!s)
498 return NULL;
499
500 down(&device_add_lock);
501
1ad275e3
DB
502 /* max of 2 devices per card */
503 if (s->device_count == 2)
504 goto err_put;
505
1da177e4
LT
506 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
507 if (!p_dev)
508 goto err_put;
509 memset(p_dev, 0, sizeof(struct pcmcia_device));
510
dc109497 511 p_dev->socket = s;
1da177e4
LT
512 p_dev->device_no = (s->device_count++);
513 p_dev->func = function;
514
515 p_dev->dev.bus = &pcmcia_bus_type;
dc109497 516 p_dev->dev.parent = s->dev.dev;
1da177e4
LT
517 p_dev->dev.release = pcmcia_release_dev;
518 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
519
520 /* compat */
e12a9a93 521 p_dev->state = CLIENT_UNBOUND;
1da177e4
LT
522
523 /* Add to the list in pcmcia_bus_socket */
524 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
525 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
526 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
527
1ad275e3
DB
528 pcmcia_device_query(p_dev);
529
1da177e4
LT
530 if (device_register(&p_dev->dev)) {
531 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
532 list_del(&p_dev->socket_device_list);
533 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
534
535 goto err_free;
536 }
537
538 up(&device_add_lock);
539
540 return p_dev;
541
542 err_free:
543 kfree(p_dev);
544 s->device_count--;
545 err_put:
546 up(&device_add_lock);
dc109497 547 pcmcia_put_socket(s);
1da177e4
LT
548
549 return NULL;
550}
551
552
553static int pcmcia_card_add(struct pcmcia_socket *s)
554{
555 cisinfo_t cisinfo;
556 cistpl_longlink_mfc_t mfc;
557 unsigned int no_funcs, i;
558 int ret = 0;
559
560 if (!(s->resource_setup_done))
561 return -EAGAIN; /* try again, but later... */
562
563 pcmcia_validate_mem(s);
564 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
565 if (ret || !cisinfo.Chains) {
566 ds_dbg(0, "invalid CIS or invalid resources\n");
567 return -ENODEV;
568 }
569
570 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
571 no_funcs = mfc.nfn;
572 else
573 no_funcs = 1;
574
1da177e4 575 for (i=0; i < no_funcs; i++)
dc109497 576 pcmcia_device_add(s, i);
1da177e4
LT
577
578 return (ret);
579}
580
581
1ad275e3
DB
582static void pcmcia_delayed_add_pseudo_device(void *data)
583{
dc109497 584 struct pcmcia_socket *s = data;
1ad275e3 585 pcmcia_device_add(s, 0);
b5e43913 586 s->pcmcia_state.device_add_pending = 0;
1ad275e3
DB
587}
588
dc109497 589static inline void pcmcia_add_pseudo_device(struct pcmcia_socket *s)
1ad275e3 590{
b5e43913 591 if (!s->pcmcia_state.device_add_pending) {
b5e43913 592 s->pcmcia_state.device_add_pending = 1;
5d546f54 593 schedule_work(&s->device_add);
1ad275e3
DB
594 }
595 return;
596}
597
e2f0b534 598static int pcmcia_requery(struct device *dev, void * _data)
ff1fa9ef 599{
e2f0b534
DB
600 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
601 if (!p_dev->dev.driver)
602 pcmcia_device_query(p_dev);
603
604 return 0;
605}
606
607static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
608{
609 int no_devices=0;
610 unsigned long flags;
611
ff1fa9ef 612 /* must be called with skt_sem held */
e2f0b534 613 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
dc109497 614 if (list_empty(&skt->devices_list))
e2f0b534
DB
615 no_devices=1;
616 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
617
618 /* if no devices were added for this socket yet because of
619 * missing resource information or other trouble, we need to
620 * do this now. */
621 if (no_devices) {
622 int ret = pcmcia_card_add(skt);
623 if (ret)
624 return;
625 }
626
627 /* some device information might have changed because of a CIS
628 * update or because we can finally read it correctly... so
629 * determine it again, overwriting old values if necessary. */
630 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
631
632 /* we re-scan all devices, not just the ones connected to this
633 * socket. This does not matter, though. */
a5b55778 634 bus_rescan_devices(&pcmcia_bus_type);
ff1fa9ef 635}
1ad275e3
DB
636
637static inline int pcmcia_devmatch(struct pcmcia_device *dev,
638 struct pcmcia_device_id *did)
639{
640 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
641 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
642 return 0;
643 }
644
645 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
646 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
647 return 0;
648 }
649
650 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
651 if (dev->func != did->function)
652 return 0;
653 }
654
655 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
656 if (!dev->prod_id[0])
657 return 0;
658 if (strcmp(did->prod_id[0], dev->prod_id[0]))
659 return 0;
660 }
661
662 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
663 if (!dev->prod_id[1])
664 return 0;
665 if (strcmp(did->prod_id[1], dev->prod_id[1]))
666 return 0;
667 }
668
669 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
670 if (!dev->prod_id[2])
671 return 0;
672 if (strcmp(did->prod_id[2], dev->prod_id[2]))
673 return 0;
674 }
675
676 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
677 if (!dev->prod_id[3])
678 return 0;
679 if (strcmp(did->prod_id[3], dev->prod_id[3]))
680 return 0;
681 }
682
683 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
684 /* handle pseudo multifunction devices:
685 * there are at most two pseudo multifunction devices.
686 * if we're matching against the first, schedule a
687 * call which will then check whether there are two
688 * pseudo devices, and if not, add the second one.
689 */
690 if (dev->device_no == 0)
dc109497 691 pcmcia_add_pseudo_device(dev->socket);
1ad275e3
DB
692
693 if (dev->device_no != did->device_no)
694 return 0;
695 }
696
697 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
698 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
699 return 0;
700
701 /* if this is a pseudo-multi-function device,
702 * we need explicit matches */
703 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
704 return 0;
705 if (dev->device_no)
706 return 0;
707
708 /* also, FUNC_ID matching needs to be activated by userspace
709 * after it has re-checked that there is no possible module
710 * with a prod_id/manf_id/card_id match.
711 */
712 if (!dev->allow_func_id_match)
713 return 0;
714 }
715
ea7b3882 716 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
daa9517d
DB
717 if (!dev->socket->fake_cis)
718 pcmcia_load_firmware(dev, did->cisfile);
719
720 if (!dev->socket->fake_cis)
ea7b3882 721 return 0;
ea7b3882
DB
722 }
723
f602ff7e
DB
724 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
725 int i;
726 for (i=0; i<4; i++)
727 if (dev->prod_id[i])
728 return 0;
729 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
730 return 0;
731 }
732
1ad275e3
DB
733 dev->dev.driver_data = (void *) did;
734
735 return 1;
736}
737
738
1da177e4
LT
739static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
740 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
741 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1ad275e3 742 struct pcmcia_device_id *did = p_drv->id_table;
1da177e4
LT
743
744 /* matching by cardmgr */
745 if (p_dev->cardmgr == p_drv)
746 return 1;
747
1ad275e3
DB
748 while (did && did->match_flags) {
749 if (pcmcia_devmatch(p_dev, did))
750 return 1;
751 did++;
752 }
753
1da177e4
LT
754 return 0;
755}
756
840c2ac5
DB
757#ifdef CONFIG_HOTPLUG
758
759static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
760 char *buffer, int buffer_size)
761{
762 struct pcmcia_device *p_dev;
763 int i, length = 0;
764 u32 hash[4] = { 0, 0, 0, 0};
765
766 if (!dev)
767 return -ENODEV;
768
769 p_dev = to_pcmcia_dev(dev);
770
771 /* calculate hashes */
772 for (i=0; i<4; i++) {
773 if (!p_dev->prod_id[i])
774 continue;
775 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
776 }
777
778 i = 0;
779
780 if (add_hotplug_env_var(envp, num_envp, &i,
781 buffer, buffer_size, &length,
782 "SOCKET_NO=%u",
783 p_dev->socket->sock))
784 return -ENOMEM;
785
786 if (add_hotplug_env_var(envp, num_envp, &i,
787 buffer, buffer_size, &length,
788 "DEVICE_NO=%02X",
789 p_dev->device_no))
790 return -ENOMEM;
791
792 if (add_hotplug_env_var(envp, num_envp, &i,
793 buffer, buffer_size, &length,
794 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
795 "pa%08Xpb%08Xpc%08Xpd%08X",
796 p_dev->has_manf_id ? p_dev->manf_id : 0,
797 p_dev->has_card_id ? p_dev->card_id : 0,
798 p_dev->has_func_id ? p_dev->func_id : 0,
799 p_dev->func,
800 p_dev->device_no,
801 hash[0],
802 hash[1],
803 hash[2],
804 hash[3]))
805 return -ENOMEM;
806
807 envp[i] = NULL;
808
809 return 0;
810}
811
812#else
813
814static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
815 char *buffer, int buffer_size)
816{
817 return -ENODEV;
818}
819
820#endif
821
1da177e4
LT
822/************************ per-device sysfs output ***************************/
823
824#define pcmcia_device_attr(field, test, format) \
e404e274 825static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
826{ \
827 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
828 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
829}
830
831#define pcmcia_device_stringattr(name, field) \
e404e274 832static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1da177e4
LT
833{ \
834 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
835 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
836}
837
838pcmcia_device_attr(func, socket, "0x%02x\n");
839pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
840pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
841pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
842pcmcia_device_stringattr(prod_id1, prod_id[0]);
843pcmcia_device_stringattr(prod_id2, prod_id[1]);
844pcmcia_device_stringattr(prod_id3, prod_id[2]);
845pcmcia_device_stringattr(prod_id4, prod_id[3]);
846
3704511b 847static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
3248ff43
DB
848{
849 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
850 int i;
851 u32 hash[4] = { 0, 0, 0, 0};
852
853 /* calculate hashes */
854 for (i=0; i<4; i++) {
855 if (!p_dev->prod_id[i])
856 continue;
857 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
858 }
859 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
860 "pa%08Xpb%08Xpc%08Xpd%08X\n",
861 p_dev->has_manf_id ? p_dev->manf_id : 0,
862 p_dev->has_card_id ? p_dev->card_id : 0,
863 p_dev->has_func_id ? p_dev->func_id : 0,
864 p_dev->func, p_dev->device_no,
865 hash[0], hash[1], hash[2], hash[3]);
866}
a5b55778 867
3248ff43
DB
868static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
869 struct device_attribute *attr, const char *buf, size_t count)
a5b55778
DB
870{
871 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
872 if (!count)
873 return -EINVAL;
874
875 down(&p_dev->socket->skt_sem);
876 p_dev->allow_func_id_match = 1;
877 up(&p_dev->socket->skt_sem);
878
879 bus_rescan_devices(&pcmcia_bus_type);
880
881 return count;
882}
883
1da177e4
LT
884static struct device_attribute pcmcia_dev_attrs[] = {
885 __ATTR(function, 0444, func_show, NULL),
886 __ATTR_RO(func_id),
887 __ATTR_RO(manf_id),
888 __ATTR_RO(card_id),
889 __ATTR_RO(prod_id1),
890 __ATTR_RO(prod_id2),
891 __ATTR_RO(prod_id3),
892 __ATTR_RO(prod_id4),
3248ff43 893 __ATTR_RO(modalias),
a5b55778 894 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1da177e4
LT
895 __ATTR_NULL,
896};
897
898
1da177e4
LT
899/*======================================================================
900
901 The card status event handler.
902
903======================================================================*/
904
905struct send_event_data {
906 struct pcmcia_socket *skt;
907 event_t event;
908 int priority;
909};
910
911static int send_event_callback(struct device *dev, void * _data)
912{
913 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1e212f36 914 struct pcmcia_driver *p_drv;
1da177e4
LT
915 struct send_event_data *data = _data;
916
917 /* we get called for all sockets, but may only pass the event
918 * for drivers _on the affected socket_ */
919 if (p_dev->socket != data->skt)
920 return 0;
921
1e212f36
DB
922 p_drv = to_pcmcia_drv(p_dev->dev.driver);
923 if (!p_drv)
924 return 0;
925
e12a9a93 926 if (p_dev->state & (CLIENT_UNBOUND|CLIENT_STALE))
1da177e4
LT
927 return 0;
928
1e212f36
DB
929 if (p_drv->event)
930 return p_drv->event(data->event, data->priority,
931 &p_dev->event_callback_args);
1da177e4
LT
932
933 return 0;
934}
935
936static int send_event(struct pcmcia_socket *s, event_t event, int priority)
937{
1da177e4 938 struct send_event_data private;
1da177e4
LT
939
940 private.skt = s;
941 private.event = event;
942 private.priority = priority;
943
dc109497 944 return bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
1da177e4
LT
945} /* send_event */
946
947
948/* Normally, the event is passed to individual drivers after
949 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
950 * is inversed to maintain historic compatibility.
951 */
952
953static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
954{
dc109497 955 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1da177e4
LT
956 int ret = 0;
957
958 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
dc109497 959 event, priority, skt);
1da177e4
LT
960
961 switch (event) {
962
963 case CS_EVENT_CARD_REMOVAL:
b5e43913 964 s->pcmcia_state.present = 0;
1da177e4 965 send_event(skt, event, priority);
dc109497
DB
966 unbind_request(skt);
967 handle_event(skt, event);
1da177e4
LT
968 break;
969
970 case CS_EVENT_CARD_INSERTION:
b5e43913 971 s->pcmcia_state.present = 1;
1da177e4 972 pcmcia_card_add(skt);
dc109497 973 handle_event(skt, event);
1da177e4
LT
974 break;
975
976 case CS_EVENT_EJECTION_REQUEST:
977 ret = send_event(skt, event, priority);
978 break;
979
980 default:
dc109497 981 handle_event(skt, event);
1da177e4
LT
982 send_event(skt, event, priority);
983 break;
984 }
985
dc109497
DB
986 pcmcia_put_socket(s);
987
1da177e4
LT
988 return 0;
989} /* ds_event */
990
991
1da177e4 992
2bc5a9bd 993int pcmcia_register_client(struct pcmcia_device **handle, client_reg_t *req)
1da177e4 994{
dc109497 995 struct pcmcia_socket *s = NULL;
1da177e4 996 struct pcmcia_device *p_dev = NULL;
1e212f36 997 struct pcmcia_driver *p_drv = NULL;
1da177e4
LT
998
999 /* Look for unbound client with matching dev_info */
1000 down_read(&pcmcia_socket_list_rwsem);
1001 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1002 unsigned long flags;
1003
1004 if (s->state & SOCKET_CARDBUS)
1005 continue;
1006
dc109497
DB
1007 s = pcmcia_get_socket(s);
1008 if (!s)
1da177e4
LT
1009 continue;
1010 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
dc109497 1011 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1da177e4
LT
1012 p_dev = pcmcia_get_dev(p_dev);
1013 if (!p_dev)
1014 continue;
e12a9a93 1015 if (!(p_dev->state & CLIENT_UNBOUND) ||
1da177e4
LT
1016 (!p_dev->dev.driver)) {
1017 pcmcia_put_dev(p_dev);
1018 continue;
1019 }
1020 p_drv = to_pcmcia_drv(p_dev->dev.driver);
1021 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1da177e4
LT
1022 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1023 goto found;
1024 }
1025 pcmcia_put_dev(p_dev);
1026 }
1027 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
dc109497 1028 pcmcia_put_socket(s);
1da177e4
LT
1029 }
1030 found:
1031 up_read(&pcmcia_socket_list_rwsem);
2bc5a9bd 1032 if (!p_dev)
1da177e4
LT
1033 return -ENODEV;
1034
dc109497 1035 pcmcia_put_socket(s); /* safe, as we already hold a reference from bind_device */
1da177e4 1036
e12a9a93
DB
1037 *handle = p_dev;
1038 p_dev->state &= ~CLIENT_UNBOUND;
1e212f36 1039 p_dev->event_callback_args = req->event_callback_args;
e12a9a93 1040 p_dev->event_callback_args.client_handle = p_dev;
1e212f36 1041
1da177e4 1042
2bc5a9bd 1043 if (!s->functions) {
1da177e4 1044 cistpl_longlink_mfc_t mfc;
2bc5a9bd 1045 if (pccard_read_tuple(s, p_dev->func, CISTPL_LONGLINK_MFC, &mfc)
1da177e4
LT
1046 == CS_SUCCESS)
1047 s->functions = mfc.nfn;
1048 else
1049 s->functions = 1;
1050 s->config = kmalloc(sizeof(config_t) * s->functions,
1051 GFP_KERNEL);
1052 if (!s->config)
1053 goto out_no_resource;
1054 memset(s->config, 0, sizeof(config_t) * s->functions);
1055 }
1056
1057 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
2bc5a9bd 1058 p_dev, p_dev->dev.bus_id);
1da177e4
LT
1059
1060 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1e212f36
DB
1061 if (p_drv->event)
1062 p_drv->event(CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW,
1063 &p_dev->event_callback_args);
1064
1da177e4
LT
1065 }
1066
1067 return CS_SUCCESS;
1068
1069 out_no_resource:
1070 pcmcia_put_dev(p_dev);
1071 return CS_OUT_OF_RESOURCE;
1072} /* register_client */
1073EXPORT_SYMBOL(pcmcia_register_client);
1074
1075
1da177e4
LT
1076/* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1077 * drivers have been called with EVENT_CARD_REMOVAL before.
1078 */
dc109497 1079static int unbind_request(struct pcmcia_socket *s)
1da177e4
LT
1080{
1081 struct pcmcia_device *p_dev;
1082 unsigned long flags;
1083
dc109497 1084 ds_dbg(2, "unbind_request(%d)\n", s->sock);
1da177e4
LT
1085
1086 s->device_count = 0;
1087
1088 for (;;) {
1089 /* unregister all pcmcia_devices registered with this socket*/
1090 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1091 if (list_empty(&s->devices_list)) {
1092 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1093 return 0;
1094 }
1095 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1096 list_del(&p_dev->socket_device_list);
e12a9a93 1097 p_dev->state |= CLIENT_STALE;
1da177e4
LT
1098 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1099
1100 device_unregister(&p_dev->dev);
1101 }
1102
1103 return 0;
1104} /* unbind_request */
1105
2bc5a9bd 1106int pcmcia_deregister_client(struct pcmcia_device *p_dev)
1da177e4
LT
1107{
1108 struct pcmcia_socket *s;
1109 int i;
1da177e4 1110
2bc5a9bd
DB
1111 s = p_dev->socket;
1112 ds_dbg(1, "deregister_client(%p)\n", p_dev);
1da177e4 1113
2bc5a9bd 1114 if (p_dev->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1da177e4
LT
1115 goto warn_out;
1116 for (i = 0; i < MAX_WIN; i++)
2bc5a9bd 1117 if (p_dev->state & CLIENT_WIN_REQ(i))
1da177e4
LT
1118 goto warn_out;
1119
2bc5a9bd
DB
1120 if (p_dev->state & CLIENT_STALE) {
1121 p_dev->state &= ~CLIENT_STALE;
1da177e4
LT
1122 pcmcia_put_dev(p_dev);
1123 } else {
2bc5a9bd 1124 p_dev->state = CLIENT_UNBOUND;
1da177e4
LT
1125 }
1126
1127 return CS_SUCCESS;
1128 warn_out:
1129 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1130 return CS_IN_USE;
1131} /* deregister_client */
1132EXPORT_SYMBOL(pcmcia_deregister_client);
1133
90c6cdd1
DB
1134static struct pcmcia_callback pcmcia_bus_callback = {
1135 .owner = THIS_MODULE,
1136 .event = ds_event,
1137 .requery = pcmcia_bus_rescan,
1138};
1139
1da177e4
LT
1140static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1141{
1142 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1da177e4
LT
1143 int ret;
1144
dc109497
DB
1145 socket = pcmcia_get_socket(socket);
1146 if (!socket) {
1da177e4 1147 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1da177e4
LT
1148 return -ENODEV;
1149 }
1150
1da177e4
LT
1151 /*
1152 * Ugly. But we want to wait for the socket threads to have started up.
1153 * We really should let the drivers themselves drive some of this..
1154 */
1155 msleep(250);
1156
e7a480d2 1157#ifdef CONFIG_PCMCIA_IOCTL
dc109497 1158 init_waitqueue_head(&socket->queue);
e7a480d2 1159#endif
dc109497
DB
1160 INIT_LIST_HEAD(&socket->devices_list);
1161 INIT_WORK(&socket->device_add, pcmcia_delayed_add_pseudo_device, socket);
1162 memset(&socket->pcmcia_state, 0, sizeof(u8));
1163 socket->device_count = 0;
1da177e4 1164
90c6cdd1 1165 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1da177e4
LT
1166 if (ret) {
1167 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
dc109497 1168 pcmcia_put_socket(socket);
1da177e4
LT
1169 return (ret);
1170 }
1171
1172 return 0;
1173}
1174
1da177e4
LT
1175static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1176{
1177 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1178
dc109497 1179 if (!socket)
1da177e4
LT
1180 return;
1181
dc109497 1182 socket->pcmcia_state.dead = 1;
1da177e4
LT
1183 pccard_register_pcmcia(socket, NULL);
1184
dc109497 1185 pcmcia_put_socket(socket);
1da177e4
LT
1186
1187 return;
1188}
1189
1190
1191/* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1192static struct class_interface pcmcia_bus_interface = {
1193 .class = &pcmcia_socket_class,
1194 .add = &pcmcia_bus_add_socket,
1195 .remove = &pcmcia_bus_remove_socket,
1196};
1197
1198
e7a480d2 1199struct bus_type pcmcia_bus_type = {
1da177e4 1200 .name = "pcmcia",
840c2ac5 1201 .hotplug = pcmcia_bus_hotplug,
1da177e4
LT
1202 .match = pcmcia_bus_match,
1203 .dev_attrs = pcmcia_dev_attrs,
1204};
1da177e4
LT
1205
1206
1207static int __init init_pcmcia_bus(void)
1208{
1da177e4
LT
1209 spin_lock_init(&pcmcia_dev_list_lock);
1210
1211 bus_register(&pcmcia_bus_type);
1212 class_interface_register(&pcmcia_bus_interface);
1213
e7a480d2 1214 pcmcia_setup_ioctl();
1da177e4
LT
1215
1216 return 0;
1217}
1218fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1219 * pcmcia_socket_class is already registered */
1220
1221
1222static void __exit exit_pcmcia_bus(void)
1223{
e7a480d2 1224 pcmcia_cleanup_ioctl();
1da177e4 1225
e7a480d2 1226 class_interface_unregister(&pcmcia_bus_interface);
1da177e4
LT
1227
1228 bus_unregister(&pcmcia_bus_type);
1229}
1230module_exit(exit_pcmcia_bus);
1231
1232
1da177e4 1233MODULE_ALIAS("ds");
This page took 0.124818 seconds and 5 git commands to generate.