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