2 * ds.c -- 16-bit PCMCIA core support
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.
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.
12 * (C) 1999 David A. Hinds
13 * (C) 2003 - 2006 Dominik Brodowski
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/delay.h>
22 #include <linux/workqueue.h>
23 #include <linux/crc32.h>
24 #include <linux/firmware.h>
25 #include <linux/kref.h>
26 #include <linux/dma-mapping.h>
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/ds.h>
32 #include <pcmcia/ss.h>
34 #include "cs_internal.h"
35 #include "ds_internal.h"
37 /*====================================================================*/
39 /* Module parameters */
41 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
42 MODULE_DESCRIPTION("PCMCIA Driver Services");
43 MODULE_LICENSE("GPL");
45 #ifdef CONFIG_PCMCIA_DEBUG
48 module_param_named(pc_debug, ds_pc_debug, int, 0644);
50 #define ds_dbg(lvl, fmt, arg...) do { \
51 if (ds_pc_debug > (lvl)) \
52 printk(KERN_DEBUG "ds: " fmt , ## arg); \
54 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
55 if (ds_pc_debug > (lvl)) \
56 dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
59 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
60 #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
63 spinlock_t pcmcia_dev_list_lock;
65 /*====================================================================*/
67 /* code which was in cs.c before */
69 /* String tables for error messages */
71 typedef struct lookup_t {
76 static const lookup_t error_table[] = {
77 { CS_SUCCESS, "Operation succeeded" },
78 { CS_BAD_ADAPTER, "Bad adapter" },
79 { CS_BAD_ATTRIBUTE, "Bad attribute", },
80 { CS_BAD_BASE, "Bad base address" },
81 { CS_BAD_EDC, "Bad EDC" },
82 { CS_BAD_IRQ, "Bad IRQ" },
83 { CS_BAD_OFFSET, "Bad offset" },
84 { CS_BAD_PAGE, "Bad page number" },
85 { CS_READ_FAILURE, "Read failure" },
86 { CS_BAD_SIZE, "Bad size" },
87 { CS_BAD_SOCKET, "Bad socket" },
88 { CS_BAD_TYPE, "Bad type" },
89 { CS_BAD_VCC, "Bad Vcc" },
90 { CS_BAD_VPP, "Bad Vpp" },
91 { CS_BAD_WINDOW, "Bad window" },
92 { CS_WRITE_FAILURE, "Write failure" },
93 { CS_NO_CARD, "No card present" },
94 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
95 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
96 { CS_BAD_SPEED, "Bad speed" },
97 { CS_BUSY, "Resource busy" },
98 { CS_GENERAL_FAILURE, "General failure" },
99 { CS_WRITE_PROTECTED, "Write protected" },
100 { CS_BAD_ARG_LENGTH, "Bad argument length" },
101 { CS_BAD_ARGS, "Bad arguments" },
102 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
103 { CS_IN_USE, "Resource in use" },
104 { CS_NO_MORE_ITEMS, "No more items" },
105 { CS_OUT_OF_RESOURCE, "Out of resource" },
106 { CS_BAD_HANDLE, "Bad handle" },
107 { CS_BAD_TUPLE, "Bad CIS tuple" }
111 static const lookup_t service_table[] = {
112 { AccessConfigurationRegister, "AccessConfigurationRegister" },
113 { AddSocketServices, "AddSocketServices" },
114 { AdjustResourceInfo, "AdjustResourceInfo" },
115 { CheckEraseQueue, "CheckEraseQueue" },
116 { CloseMemory, "CloseMemory" },
117 { DeregisterClient, "DeregisterClient" },
118 { DeregisterEraseQueue, "DeregisterEraseQueue" },
119 { GetCardServicesInfo, "GetCardServicesInfo" },
120 { GetClientInfo, "GetClientInfo" },
121 { GetConfigurationInfo, "GetConfigurationInfo" },
122 { GetEventMask, "GetEventMask" },
123 { GetFirstClient, "GetFirstClient" },
124 { GetFirstRegion, "GetFirstRegion" },
125 { GetFirstTuple, "GetFirstTuple" },
126 { GetNextClient, "GetNextClient" },
127 { GetNextRegion, "GetNextRegion" },
128 { GetNextTuple, "GetNextTuple" },
129 { GetStatus, "GetStatus" },
130 { GetTupleData, "GetTupleData" },
131 { MapMemPage, "MapMemPage" },
132 { ModifyConfiguration, "ModifyConfiguration" },
133 { ModifyWindow, "ModifyWindow" },
134 { OpenMemory, "OpenMemory" },
135 { ParseTuple, "ParseTuple" },
136 { ReadMemory, "ReadMemory" },
137 { RegisterClient, "RegisterClient" },
138 { RegisterEraseQueue, "RegisterEraseQueue" },
139 { RegisterMTD, "RegisterMTD" },
140 { ReleaseConfiguration, "ReleaseConfiguration" },
141 { ReleaseIO, "ReleaseIO" },
142 { ReleaseIRQ, "ReleaseIRQ" },
143 { ReleaseWindow, "ReleaseWindow" },
144 { RequestConfiguration, "RequestConfiguration" },
145 { RequestIO, "RequestIO" },
146 { RequestIRQ, "RequestIRQ" },
147 { RequestSocketMask, "RequestSocketMask" },
148 { RequestWindow, "RequestWindow" },
149 { ResetCard, "ResetCard" },
150 { SetEventMask, "SetEventMask" },
151 { ValidateCIS, "ValidateCIS" },
152 { WriteMemory, "WriteMemory" },
153 { BindDevice, "BindDevice" },
154 { BindMTD, "BindMTD" },
155 { ReportError, "ReportError" },
156 { SuspendCard, "SuspendCard" },
157 { ResumeCard, "ResumeCard" },
158 { EjectCard, "EjectCard" },
159 { InsertCard, "InsertCard" },
160 { ReplaceCIS, "ReplaceCIS" }
164 static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
172 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
174 for (i = 0; i < ARRAY_SIZE(service_table); i++)
175 if (service_table[i].key == err->func)
177 if (i < ARRAY_SIZE(service_table))
178 serv = service_table[i].msg;
180 serv = "Unknown service number";
182 for (i = 0; i < ARRAY_SIZE(error_table); i++)
183 if (error_table[i].key == err->retcode)
185 if (i < ARRAY_SIZE(error_table))
186 printk("%s: %s\n", serv, error_table[i].msg);
188 printk("%s: Unknown error code %#x\n", serv, err->retcode);
193 /* end of code which was in cs.c before */
195 /*======================================================================*/
197 void cs_error(struct pcmcia_device *p_dev, int func, int ret)
199 error_info_t err = { func, ret };
200 pcmcia_report_error(p_dev, &err);
202 EXPORT_SYMBOL(cs_error);
205 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
207 struct pcmcia_device_id *did = p_drv->id_table;
211 if (!p_drv->probe || !p_drv->remove)
212 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
213 "function\n", p_drv->drv.name);
215 while (did && did->match_flags) {
216 for (i=0; i<4; i++) {
217 if (!did->prod_id[i])
220 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
221 if (hash == did->prod_id_hash[i])
224 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
225 "product string \"%s\": is 0x%x, should "
226 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
227 did->prod_id_hash[i], hash);
228 printk(KERN_DEBUG "pcmcia: see "
229 "Documentation/pcmcia/devicetable.txt for "
239 /*======================================================================*/
242 struct pcmcia_dynid {
243 struct list_head node;
244 struct pcmcia_device_id id;
248 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
249 * @driver: target device driver
250 * @buf: buffer for scanning device ID data
253 * Adds a new dynamic PCMCIA device ID to this driver,
254 * and causes the driver to probe for all devices again.
257 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
259 struct pcmcia_dynid *dynid;
260 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
261 __u16 match_flags, manf_id, card_id;
262 __u8 func_id, function, device_no;
263 __u32 prod_id_hash[4] = {0, 0, 0, 0};
267 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
268 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
269 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
273 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
277 INIT_LIST_HEAD(&dynid->node);
278 dynid->id.match_flags = match_flags;
279 dynid->id.manf_id = manf_id;
280 dynid->id.card_id = card_id;
281 dynid->id.func_id = func_id;
282 dynid->id.function = function;
283 dynid->id.device_no = device_no;
284 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
286 spin_lock(&pdrv->dynids.lock);
287 list_add_tail(&pdrv->dynids.list, &dynid->node);
288 spin_unlock(&pdrv->dynids.lock);
290 if (get_driver(&pdrv->drv)) {
291 retval = driver_attach(&pdrv->drv);
292 put_driver(&pdrv->drv);
299 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
302 pcmcia_free_dynids(struct pcmcia_driver *drv)
304 struct pcmcia_dynid *dynid, *n;
306 spin_lock(&drv->dynids.lock);
307 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
308 list_del(&dynid->node);
311 spin_unlock(&drv->dynids.lock);
315 pcmcia_create_newid_file(struct pcmcia_driver *drv)
318 if (drv->probe != NULL)
319 error = driver_create_file(&drv->drv, &driver_attr_new_id);
325 * pcmcia_register_driver - register a PCMCIA driver with the bus core
326 * @driver: the &driver being registered
328 * Registers a PCMCIA driver with the PCMCIA bus core.
330 int pcmcia_register_driver(struct pcmcia_driver *driver)
337 pcmcia_check_driver(driver);
339 /* initialize common fields */
340 driver->drv.bus = &pcmcia_bus_type;
341 driver->drv.owner = driver->owner;
342 spin_lock_init(&driver->dynids.lock);
343 INIT_LIST_HEAD(&driver->dynids.list);
345 ds_dbg(3, "registering driver %s\n", driver->drv.name);
347 error = driver_register(&driver->drv);
351 error = pcmcia_create_newid_file(driver);
353 driver_unregister(&driver->drv);
357 EXPORT_SYMBOL(pcmcia_register_driver);
360 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
361 * @driver: the &driver being unregistered
363 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
365 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
366 driver_unregister(&driver->drv);
367 pcmcia_free_dynids(driver);
369 EXPORT_SYMBOL(pcmcia_unregister_driver);
372 /* pcmcia_device handling */
374 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
376 struct device *tmp_dev;
377 tmp_dev = get_device(&p_dev->dev);
380 return to_pcmcia_dev(tmp_dev);
383 void pcmcia_put_dev(struct pcmcia_device *p_dev)
386 put_device(&p_dev->dev);
389 static void pcmcia_release_function(struct kref *ref)
391 struct config_t *c = container_of(ref, struct config_t, ref);
392 ds_dbg(1, "releasing config_t\n");
396 static void pcmcia_release_dev(struct device *dev)
398 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
399 ds_dev_dbg(1, dev, "releasing device\n");
400 pcmcia_put_socket(p_dev->socket);
401 kfree(p_dev->devname);
402 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
406 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
408 if (!s->pcmcia_state.device_add_pending) {
409 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
410 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
411 s->pcmcia_state.device_add_pending = 1;
412 s->pcmcia_state.mfc_pfc = mfc;
413 schedule_work(&s->device_add);
418 static int pcmcia_device_probe(struct device * dev)
420 struct pcmcia_device *p_dev;
421 struct pcmcia_driver *p_drv;
422 struct pcmcia_device_id *did;
423 struct pcmcia_socket *s;
424 cistpl_config_t cis_config;
427 dev = get_device(dev);
431 p_dev = to_pcmcia_dev(dev);
432 p_drv = to_pcmcia_drv(dev->driver);
435 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
437 if ((!p_drv->probe) || (!p_dev->function_config) ||
438 (!try_module_get(p_drv->owner))) {
443 /* set up some more device information */
444 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
447 p_dev->conf.ConfigBase = cis_config.base;
448 p_dev->conf.Present = cis_config.rmask[0];
450 dev_printk(KERN_INFO, dev,
451 "pcmcia: could not parse base and rmask0 of CIS\n");
452 p_dev->conf.ConfigBase = 0;
453 p_dev->conf.Present = 0;
456 ret = p_drv->probe(p_dev);
458 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
459 p_drv->drv.name, ret);
463 /* handle pseudo multifunction devices:
464 * there are at most two pseudo multifunction devices.
465 * if we're matching against the first, schedule a
466 * call which will then check whether there are two
467 * pseudo devices, and if not, add the second one.
469 did = p_dev->dev.driver_data;
470 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
471 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
472 pcmcia_add_device_later(p_dev->socket, 0);
476 module_put(p_drv->owner);
485 * Removes a PCMCIA card from the device tree and socket list.
487 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
489 struct pcmcia_device *p_dev;
490 struct pcmcia_device *tmp;
493 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
494 "pcmcia_card_remove(%d) %s\n", s->sock,
495 leftover ? leftover->devname : "");
502 /* unregister all pcmcia_devices registered with this socket, except leftover */
503 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
504 if (p_dev == leftover)
507 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
508 list_del(&p_dev->socket_device_list);
510 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
512 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
513 device_unregister(&p_dev->dev);
519 static int pcmcia_device_remove(struct device * dev)
521 struct pcmcia_device *p_dev;
522 struct pcmcia_driver *p_drv;
523 struct pcmcia_device_id *did;
526 p_dev = to_pcmcia_dev(dev);
527 p_drv = to_pcmcia_drv(dev->driver);
529 ds_dev_dbg(1, dev, "removing device\n");
531 /* If we're removing the primary module driving a
532 * pseudo multi-function card, we need to unbind
535 did = p_dev->dev.driver_data;
536 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
537 (p_dev->socket->device_count != 0) &&
538 (p_dev->device_no == 0))
539 pcmcia_card_remove(p_dev->socket, p_dev);
541 /* detach the "instance" */
546 p_drv->remove(p_dev);
548 p_dev->dev_node = NULL;
550 /* check for proper unloading */
551 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
552 dev_printk(KERN_INFO, dev,
553 "pcmcia: driver %s did not release config properly\n",
556 for (i = 0; i < MAX_WIN; i++)
557 if (p_dev->_win & CLIENT_WIN_REQ(i))
558 dev_printk(KERN_INFO, dev,
559 "pcmcia: driver %s did not release window properly\n",
562 /* references from pcmcia_probe_device */
563 pcmcia_put_dev(p_dev);
564 module_put(p_drv->owner);
571 * pcmcia_device_query -- determine information about a pcmcia device
573 static int pcmcia_device_query(struct pcmcia_device *p_dev)
575 cistpl_manfid_t manf_id;
576 cistpl_funcid_t func_id;
577 cistpl_vers_1_t *vers1;
580 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
584 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
585 CISTPL_MANFID, &manf_id)) {
586 p_dev->manf_id = manf_id.manf;
587 p_dev->card_id = manf_id.card;
588 p_dev->has_manf_id = 1;
589 p_dev->has_card_id = 1;
592 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
593 CISTPL_FUNCID, &func_id)) {
594 p_dev->func_id = func_id.func;
595 p_dev->has_func_id = 1;
597 /* rule of thumb: cards with no FUNCID, but with
598 * common memory device geometry information, are
599 * probably memory cards (from pcmcia-cs) */
600 cistpl_device_geo_t *devgeo;
602 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
607 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
608 CISTPL_DEVICE_GEO, devgeo)) {
609 ds_dev_dbg(0, &p_dev->dev,
610 "mem device geometry probably means "
612 p_dev->func_id = CISTPL_FUNCID_MEMORY;
613 p_dev->has_func_id = 1;
618 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
620 for (i=0; i < vers1->ns; i++) {
624 tmp = vers1->str + vers1->ofs[i];
626 length = strlen(tmp) + 1;
627 if ((length < 2) || (length > 255))
630 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
632 if (!p_dev->prod_id[i])
635 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
645 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
646 * Serializes pcmcia_device_add; will most likely be removed in future.
648 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
649 * won't work, this doesn't matter much at the moment: the driver core doesn't
652 static DEFINE_MUTEX(device_add_lock);
654 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
656 struct pcmcia_device *p_dev, *tmp_dev;
660 s = pcmcia_get_socket(s);
664 mutex_lock(&device_add_lock);
666 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
668 /* max of 4 devices per card */
669 if (s->device_count == 4)
672 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
677 p_dev->device_no = (s->device_count++);
678 p_dev->func = function;
680 p_dev->dev.bus = &pcmcia_bus_type;
681 p_dev->dev.parent = s->dev.parent;
682 p_dev->dev.release = pcmcia_release_dev;
683 /* by default don't allow DMA */
684 p_dev->dma_mask = DMA_MASK_NONE;
685 p_dev->dev.dma_mask = &p_dev->dma_mask;
686 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
688 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
691 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
692 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
694 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
697 * p_dev->function_config must be the same for all card functions.
698 * Note that this is serialized by the device_add_lock, so that
699 * only one such struct will be created.
701 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
702 if (p_dev->func == tmp_dev->func) {
703 p_dev->function_config = tmp_dev->function_config;
704 kref_get(&p_dev->function_config->ref);
707 /* Add to the list in pcmcia_bus_socket */
708 list_add(&p_dev->socket_device_list, &s->devices_list);
710 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
712 if (!p_dev->function_config) {
713 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
714 p_dev->function_config = kzalloc(sizeof(struct config_t),
716 if (!p_dev->function_config)
718 kref_init(&p_dev->function_config->ref);
721 dev_printk(KERN_NOTICE, &p_dev->dev,
722 "pcmcia: registering new device %s\n",
725 pcmcia_device_query(p_dev);
727 if (device_register(&p_dev->dev))
730 mutex_unlock(&device_add_lock);
735 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
736 list_del(&p_dev->socket_device_list);
737 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
740 kfree(p_dev->devname);
744 mutex_unlock(&device_add_lock);
745 pcmcia_put_socket(s);
751 static int pcmcia_card_add(struct pcmcia_socket *s)
753 cistpl_longlink_mfc_t mfc;
754 unsigned int no_funcs, i, no_chains;
757 if (!(s->resource_setup_done)) {
758 ds_dev_dbg(3, &s->dev,
759 "no resources available, delaying card_add\n");
760 return -EAGAIN; /* try again, but later... */
763 if (pcmcia_validate_mem(s)) {
764 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
765 "delaying card_add\n");
766 return -EAGAIN; /* try again, but later... */
769 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
770 if (ret || !no_chains) {
771 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
775 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
779 s->functions = no_funcs;
781 for (i=0; i < no_funcs; i++)
782 pcmcia_device_add(s, i);
788 static void pcmcia_delayed_add_device(struct work_struct *work)
790 struct pcmcia_socket *s =
791 container_of(work, struct pcmcia_socket, device_add);
792 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
793 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
794 s->pcmcia_state.device_add_pending = 0;
795 s->pcmcia_state.mfc_pfc = 0;
798 static int pcmcia_requery(struct device *dev, void * _data)
800 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
801 if (!p_dev->dev.driver) {
802 ds_dev_dbg(1, dev, "update device information\n");
803 pcmcia_device_query(p_dev);
809 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
815 /* must be called with skt_mutex held */
816 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
818 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
819 if (list_empty(&skt->devices_list))
821 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
823 /* If this is because of a CIS override, start over */
824 if (new_cis && !no_devices)
825 pcmcia_card_remove(skt, NULL);
827 /* if no devices were added for this socket yet because of
828 * missing resource information or other trouble, we need to
830 if (no_devices || new_cis) {
831 ret = pcmcia_card_add(skt);
836 /* some device information might have changed because of a CIS
837 * update or because we can finally read it correctly... so
838 * determine it again, overwriting old values if necessary. */
839 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
841 /* we re-scan all devices, not just the ones connected to this
842 * socket. This does not matter, though. */
843 ret = bus_rescan_devices(&pcmcia_bus_type);
845 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
848 #ifdef CONFIG_PCMCIA_LOAD_CIS
851 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
852 * @dev: the pcmcia device which needs a CIS override
853 * @filename: requested filename in /lib/firmware/
855 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
856 * the one provided by the card is broken. The firmware files reside in
857 * /lib/firmware/ in userspace.
859 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
861 struct pcmcia_socket *s = dev->socket;
862 const struct firmware *fw;
863 char path[FIRMWARE_NAME_MAX];
867 cistpl_longlink_mfc_t mfc;
872 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
874 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
875 dev_printk(KERN_WARNING, &dev->dev,
876 "pcmcia: CIS filename is too long [%s]\n",
881 snprintf(path, sizeof(path), "%s", filename);
883 if (request_firmware(&fw, path, &dev->dev) == 0) {
884 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
886 dev_printk(KERN_ERR, &dev->dev,
887 "pcmcia: CIS override is too big\n");
891 if (!pcmcia_replace_cis(s, fw->data, fw->size))
894 dev_printk(KERN_ERR, &dev->dev,
895 "pcmcia: CIS override failed\n");
900 /* update information */
901 pcmcia_device_query(dev);
903 /* does this cis override add or remove functions? */
904 old_funcs = s->functions;
906 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
910 s->functions = no_funcs;
912 if (old_funcs > no_funcs)
913 pcmcia_card_remove(s, dev);
914 else if (no_funcs > old_funcs)
915 pcmcia_add_device_later(s, 1);
918 release_firmware(fw);
923 #else /* !CONFIG_PCMCIA_LOAD_CIS */
925 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
933 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
934 struct pcmcia_device_id *did)
936 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
937 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
941 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
942 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
946 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
947 if (dev->func != did->function)
951 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
952 if (!dev->prod_id[0])
954 if (strcmp(did->prod_id[0], dev->prod_id[0]))
958 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
959 if (!dev->prod_id[1])
961 if (strcmp(did->prod_id[1], dev->prod_id[1]))
965 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
966 if (!dev->prod_id[2])
968 if (strcmp(did->prod_id[2], dev->prod_id[2]))
972 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
973 if (!dev->prod_id[3])
975 if (strcmp(did->prod_id[3], dev->prod_id[3]))
979 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
980 if (dev->device_no != did->device_no)
984 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
985 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
988 /* if this is a pseudo-multi-function device,
989 * we need explicit matches */
990 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
995 /* also, FUNC_ID matching needs to be activated by userspace
996 * after it has re-checked that there is no possible module
997 * with a prod_id/manf_id/card_id match.
999 ds_dev_dbg(0, &dev->dev,
1000 "skipping FUNC_ID match until userspace interaction\n");
1001 if (!dev->allow_func_id_match)
1005 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
1006 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
1007 if (!dev->socket->fake_cis)
1008 pcmcia_load_firmware(dev, did->cisfile);
1010 if (!dev->socket->fake_cis)
1014 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
1017 if (dev->prod_id[i])
1019 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
1023 dev->dev.driver_data = (void *) did;
1029 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1030 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1031 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1032 struct pcmcia_device_id *did = p_drv->id_table;
1033 struct pcmcia_dynid *dynid;
1035 /* match dynamic devices first */
1036 spin_lock(&p_drv->dynids.lock);
1037 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
1038 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1039 if (pcmcia_devmatch(p_dev, &dynid->id)) {
1040 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1041 spin_unlock(&p_drv->dynids.lock);
1045 spin_unlock(&p_drv->dynids.lock);
1047 #ifdef CONFIG_PCMCIA_IOCTL
1048 /* matching by cardmgr */
1049 if (p_dev->cardmgr == p_drv) {
1050 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1055 while (did && did->match_flags) {
1056 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1057 if (pcmcia_devmatch(p_dev, did)) {
1058 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1067 #ifdef CONFIG_HOTPLUG
1069 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1071 struct pcmcia_device *p_dev;
1073 u32 hash[4] = { 0, 0, 0, 0};
1078 p_dev = to_pcmcia_dev(dev);
1080 /* calculate hashes */
1081 for (i=0; i<4; i++) {
1082 if (!p_dev->prod_id[i])
1084 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1087 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1090 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1093 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1094 "pa%08Xpb%08Xpc%08Xpd%08X",
1095 p_dev->has_manf_id ? p_dev->manf_id : 0,
1096 p_dev->has_card_id ? p_dev->card_id : 0,
1097 p_dev->has_func_id ? p_dev->func_id : 0,
1111 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1118 /************************ runtime PM support ***************************/
1120 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1121 static int pcmcia_dev_resume(struct device *dev);
1123 static int runtime_suspend(struct device *dev)
1128 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1133 static void runtime_resume(struct device *dev)
1138 rc = pcmcia_dev_resume(dev);
1142 /************************ per-device sysfs output ***************************/
1144 #define pcmcia_device_attr(field, test, format) \
1145 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1147 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1148 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1151 #define pcmcia_device_stringattr(name, field) \
1152 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1154 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1155 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1158 pcmcia_device_attr(func, socket, "0x%02x\n");
1159 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1160 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1161 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1162 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1163 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1164 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1165 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1168 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1170 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1172 if (p_dev->suspended)
1173 return sprintf(buf, "off\n");
1175 return sprintf(buf, "on\n");
1178 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1179 const char *buf, size_t count)
1181 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1187 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1188 ret = runtime_suspend(dev);
1189 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1190 runtime_resume(dev);
1192 return ret ? ret : count;
1196 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1198 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1200 u32 hash[4] = { 0, 0, 0, 0};
1202 /* calculate hashes */
1203 for (i=0; i<4; i++) {
1204 if (!p_dev->prod_id[i])
1206 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1208 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1209 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1210 p_dev->has_manf_id ? p_dev->manf_id : 0,
1211 p_dev->has_card_id ? p_dev->card_id : 0,
1212 p_dev->has_func_id ? p_dev->func_id : 0,
1213 p_dev->func, p_dev->device_no,
1214 hash[0], hash[1], hash[2], hash[3]);
1217 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1218 struct device_attribute *attr, const char *buf, size_t count)
1220 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1226 mutex_lock(&p_dev->socket->skt_mutex);
1227 p_dev->allow_func_id_match = 1;
1228 mutex_unlock(&p_dev->socket->skt_mutex);
1230 ret = bus_rescan_devices(&pcmcia_bus_type);
1232 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1233 "allowing func_id matches\n");
1238 static struct device_attribute pcmcia_dev_attrs[] = {
1239 __ATTR(function, 0444, func_show, NULL),
1240 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1244 __ATTR_RO(prod_id1),
1245 __ATTR_RO(prod_id2),
1246 __ATTR_RO(prod_id3),
1247 __ATTR_RO(prod_id4),
1248 __ATTR_RO(modalias),
1249 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1253 /* PM support, also needed for reset */
1255 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1257 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1258 struct pcmcia_driver *p_drv = NULL;
1261 if (p_dev->suspended)
1264 ds_dev_dbg(2, dev, "suspending\n");
1267 p_drv = to_pcmcia_drv(dev->driver);
1272 if (p_drv->suspend) {
1273 ret = p_drv->suspend(p_dev);
1275 dev_printk(KERN_ERR, dev,
1276 "pcmcia: device %s (driver %s) did "
1277 "not want to go to sleep (%d)\n",
1278 p_dev->devname, p_drv->drv.name, ret);
1283 if (p_dev->device_no == p_dev->func) {
1284 ds_dev_dbg(2, dev, "releasing configuration\n");
1285 pcmcia_release_configuration(p_dev);
1290 p_dev->suspended = 1;
1295 static int pcmcia_dev_resume(struct device * dev)
1297 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1298 struct pcmcia_driver *p_drv = NULL;
1301 if (!p_dev->suspended)
1304 ds_dev_dbg(2, dev, "resuming\n");
1307 p_drv = to_pcmcia_drv(dev->driver);
1312 if (p_dev->device_no == p_dev->func) {
1313 ds_dev_dbg(2, dev, "requesting configuration\n");
1314 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1320 ret = p_drv->resume(p_dev);
1324 p_dev->suspended = 0;
1329 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1331 struct pcmcia_socket *skt = _data;
1332 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1334 if (p_dev->socket != skt || p_dev->suspended)
1337 return runtime_suspend(dev);
1340 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1342 struct pcmcia_socket *skt = _data;
1343 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1345 if (p_dev->socket != skt || !p_dev->suspended)
1348 runtime_resume(dev);
1353 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1355 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1356 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1360 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1362 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1363 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1364 pcmcia_bus_suspend_callback)) {
1365 pcmcia_bus_resume(skt);
1372 /*======================================================================
1374 The card status event handler.
1376 ======================================================================*/
1378 /* Normally, the event is passed to individual drivers after
1379 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1380 * is inversed to maintain historic compatibility.
1383 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1385 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1388 dev_printk(KERN_ERR, &skt->dev,
1389 "PCMCIA obtaining reference to socket " \
1390 "failed, event 0x%x lost!\n", event);
1394 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1395 event, priority, skt);
1398 case CS_EVENT_CARD_REMOVAL:
1399 s->pcmcia_state.present = 0;
1400 pcmcia_card_remove(skt, NULL);
1401 handle_event(skt, event);
1404 case CS_EVENT_CARD_INSERTION:
1405 s->pcmcia_state.present = 1;
1406 pcmcia_card_add(skt);
1407 handle_event(skt, event);
1410 case CS_EVENT_EJECTION_REQUEST:
1413 case CS_EVENT_PM_SUSPEND:
1414 case CS_EVENT_PM_RESUME:
1415 case CS_EVENT_RESET_PHYSICAL:
1416 case CS_EVENT_CARD_RESET:
1418 handle_event(skt, event);
1422 pcmcia_put_socket(s);
1428 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1430 struct pcmcia_device *p_dev;
1431 struct pcmcia_device *ret = NULL;
1433 p_dev = pcmcia_get_dev(_p_dev);
1437 if (!p_dev->socket->pcmcia_state.present)
1440 if (p_dev->_removed)
1443 if (p_dev->suspended)
1448 pcmcia_put_dev(p_dev);
1451 EXPORT_SYMBOL(pcmcia_dev_present);
1454 static struct pcmcia_callback pcmcia_bus_callback = {
1455 .owner = THIS_MODULE,
1457 .requery = pcmcia_bus_rescan,
1458 .suspend = pcmcia_bus_suspend,
1459 .resume = pcmcia_bus_resume,
1462 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1463 struct class_interface *class_intf)
1465 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1468 socket = pcmcia_get_socket(socket);
1470 dev_printk(KERN_ERR, dev,
1471 "PCMCIA obtaining reference to socket failed\n");
1476 * Ugly. But we want to wait for the socket threads to have started up.
1477 * We really should let the drivers themselves drive some of this..
1481 #ifdef CONFIG_PCMCIA_IOCTL
1482 init_waitqueue_head(&socket->queue);
1484 INIT_LIST_HEAD(&socket->devices_list);
1485 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1486 memset(&socket->pcmcia_state, 0, sizeof(u8));
1487 socket->device_count = 0;
1489 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1491 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1492 pcmcia_put_socket(socket);
1499 static void pcmcia_bus_remove_socket(struct device *dev,
1500 struct class_interface *class_intf)
1502 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1507 socket->pcmcia_state.dead = 1;
1508 pccard_register_pcmcia(socket, NULL);
1510 /* unregister any unbound devices */
1511 mutex_lock(&socket->skt_mutex);
1512 pcmcia_card_remove(socket, NULL);
1513 mutex_unlock(&socket->skt_mutex);
1515 pcmcia_put_socket(socket);
1521 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1522 static struct class_interface pcmcia_bus_interface __refdata = {
1523 .class = &pcmcia_socket_class,
1524 .add_dev = &pcmcia_bus_add_socket,
1525 .remove_dev = &pcmcia_bus_remove_socket,
1529 struct bus_type pcmcia_bus_type = {
1531 .uevent = pcmcia_bus_uevent,
1532 .match = pcmcia_bus_match,
1533 .dev_attrs = pcmcia_dev_attrs,
1534 .probe = pcmcia_device_probe,
1535 .remove = pcmcia_device_remove,
1536 .suspend = pcmcia_dev_suspend,
1537 .resume = pcmcia_dev_resume,
1541 static int __init init_pcmcia_bus(void)
1545 spin_lock_init(&pcmcia_dev_list_lock);
1547 ret = bus_register(&pcmcia_bus_type);
1549 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1552 ret = class_interface_register(&pcmcia_bus_interface);
1555 "pcmcia: class_interface_register error: %d\n", ret);
1556 bus_unregister(&pcmcia_bus_type);
1560 pcmcia_setup_ioctl();
1564 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1565 * pcmcia_socket_class is already registered */
1568 static void __exit exit_pcmcia_bus(void)
1570 pcmcia_cleanup_ioctl();
1572 class_interface_unregister(&pcmcia_bus_interface);
1574 bus_unregister(&pcmcia_bus_type);
1576 module_exit(exit_pcmcia_bus);