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_ATTRIBUTE, "Bad attribute", },
79 { CS_BAD_BASE, "Bad base address" },
80 { CS_BAD_IRQ, "Bad IRQ" },
81 { CS_BAD_OFFSET, "Bad offset" },
82 { CS_BAD_PAGE, "Bad page number" },
83 { CS_BAD_SIZE, "Bad size" },
84 { CS_BAD_TYPE, "Bad type" },
85 { CS_BAD_VCC, "Bad Vcc" },
86 { CS_BAD_VPP, "Bad Vpp" },
87 { CS_NO_CARD, "No card present" },
88 { -EINVAL, "Bad parameter" },
89 { CS_GENERAL_FAILURE, "General failure" },
90 { CS_BAD_ARGS, "Bad arguments" },
91 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
92 { CS_IN_USE, "Resource in use" },
93 { CS_NO_MORE_ITEMS, "No more items" },
94 { CS_OUT_OF_RESOURCE, "Out of resource" },
95 { CS_BAD_HANDLE, "Bad handle" },
96 { CS_BAD_TUPLE, "Bad CIS tuple" }
100 static const lookup_t service_table[] = {
101 { AccessConfigurationRegister, "AccessConfigurationRegister" },
102 { AddSocketServices, "AddSocketServices" },
103 { AdjustResourceInfo, "AdjustResourceInfo" },
104 { CheckEraseQueue, "CheckEraseQueue" },
105 { CloseMemory, "CloseMemory" },
106 { DeregisterClient, "DeregisterClient" },
107 { DeregisterEraseQueue, "DeregisterEraseQueue" },
108 { GetCardServicesInfo, "GetCardServicesInfo" },
109 { GetClientInfo, "GetClientInfo" },
110 { GetConfigurationInfo, "GetConfigurationInfo" },
111 { GetEventMask, "GetEventMask" },
112 { GetFirstClient, "GetFirstClient" },
113 { GetFirstRegion, "GetFirstRegion" },
114 { GetFirstTuple, "GetFirstTuple" },
115 { GetNextClient, "GetNextClient" },
116 { GetNextRegion, "GetNextRegion" },
117 { GetNextTuple, "GetNextTuple" },
118 { GetStatus, "GetStatus" },
119 { GetTupleData, "GetTupleData" },
120 { MapMemPage, "MapMemPage" },
121 { ModifyConfiguration, "ModifyConfiguration" },
122 { ModifyWindow, "ModifyWindow" },
123 { OpenMemory, "OpenMemory" },
124 { ParseTuple, "ParseTuple" },
125 { ReadMemory, "ReadMemory" },
126 { RegisterClient, "RegisterClient" },
127 { RegisterEraseQueue, "RegisterEraseQueue" },
128 { RegisterMTD, "RegisterMTD" },
129 { ReleaseConfiguration, "ReleaseConfiguration" },
130 { ReleaseIO, "ReleaseIO" },
131 { ReleaseIRQ, "ReleaseIRQ" },
132 { ReleaseWindow, "ReleaseWindow" },
133 { RequestConfiguration, "RequestConfiguration" },
134 { RequestIO, "RequestIO" },
135 { RequestIRQ, "RequestIRQ" },
136 { RequestSocketMask, "RequestSocketMask" },
137 { RequestWindow, "RequestWindow" },
138 { ResetCard, "ResetCard" },
139 { SetEventMask, "SetEventMask" },
140 { ValidateCIS, "ValidateCIS" },
141 { WriteMemory, "WriteMemory" },
142 { BindDevice, "BindDevice" },
143 { BindMTD, "BindMTD" },
144 { ReportError, "ReportError" },
145 { SuspendCard, "SuspendCard" },
146 { ResumeCard, "ResumeCard" },
147 { EjectCard, "EjectCard" },
148 { InsertCard, "InsertCard" },
149 { ReplaceCIS, "ReplaceCIS" }
153 static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
161 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
163 for (i = 0; i < ARRAY_SIZE(service_table); i++)
164 if (service_table[i].key == err->func)
166 if (i < ARRAY_SIZE(service_table))
167 serv = service_table[i].msg;
169 serv = "Unknown service number";
171 for (i = 0; i < ARRAY_SIZE(error_table); i++)
172 if (error_table[i].key == err->retcode)
174 if (i < ARRAY_SIZE(error_table))
175 printk("%s: %s\n", serv, error_table[i].msg);
177 printk("%s: Unknown error code %#x\n", serv, err->retcode);
182 /* end of code which was in cs.c before */
184 /*======================================================================*/
186 void cs_error(struct pcmcia_device *p_dev, int func, int ret)
188 error_info_t err = { func, ret };
189 pcmcia_report_error(p_dev, &err);
191 EXPORT_SYMBOL(cs_error);
194 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
196 struct pcmcia_device_id *did = p_drv->id_table;
200 if (!p_drv->probe || !p_drv->remove)
201 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
202 "function\n", p_drv->drv.name);
204 while (did && did->match_flags) {
205 for (i=0; i<4; i++) {
206 if (!did->prod_id[i])
209 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
210 if (hash == did->prod_id_hash[i])
213 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
214 "product string \"%s\": is 0x%x, should "
215 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
216 did->prod_id_hash[i], hash);
217 printk(KERN_DEBUG "pcmcia: see "
218 "Documentation/pcmcia/devicetable.txt for "
228 /*======================================================================*/
231 struct pcmcia_dynid {
232 struct list_head node;
233 struct pcmcia_device_id id;
237 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
238 * @driver: target device driver
239 * @buf: buffer for scanning device ID data
242 * Adds a new dynamic PCMCIA device ID to this driver,
243 * and causes the driver to probe for all devices again.
246 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
248 struct pcmcia_dynid *dynid;
249 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
250 __u16 match_flags, manf_id, card_id;
251 __u8 func_id, function, device_no;
252 __u32 prod_id_hash[4] = {0, 0, 0, 0};
256 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
257 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
258 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
262 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
266 INIT_LIST_HEAD(&dynid->node);
267 dynid->id.match_flags = match_flags;
268 dynid->id.manf_id = manf_id;
269 dynid->id.card_id = card_id;
270 dynid->id.func_id = func_id;
271 dynid->id.function = function;
272 dynid->id.device_no = device_no;
273 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
275 spin_lock(&pdrv->dynids.lock);
276 list_add_tail(&pdrv->dynids.list, &dynid->node);
277 spin_unlock(&pdrv->dynids.lock);
279 if (get_driver(&pdrv->drv)) {
280 retval = driver_attach(&pdrv->drv);
281 put_driver(&pdrv->drv);
288 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
291 pcmcia_free_dynids(struct pcmcia_driver *drv)
293 struct pcmcia_dynid *dynid, *n;
295 spin_lock(&drv->dynids.lock);
296 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
297 list_del(&dynid->node);
300 spin_unlock(&drv->dynids.lock);
304 pcmcia_create_newid_file(struct pcmcia_driver *drv)
307 if (drv->probe != NULL)
308 error = driver_create_file(&drv->drv, &driver_attr_new_id);
314 * pcmcia_register_driver - register a PCMCIA driver with the bus core
315 * @driver: the &driver being registered
317 * Registers a PCMCIA driver with the PCMCIA bus core.
319 int pcmcia_register_driver(struct pcmcia_driver *driver)
326 pcmcia_check_driver(driver);
328 /* initialize common fields */
329 driver->drv.bus = &pcmcia_bus_type;
330 driver->drv.owner = driver->owner;
331 spin_lock_init(&driver->dynids.lock);
332 INIT_LIST_HEAD(&driver->dynids.list);
334 ds_dbg(3, "registering driver %s\n", driver->drv.name);
336 error = driver_register(&driver->drv);
340 error = pcmcia_create_newid_file(driver);
342 driver_unregister(&driver->drv);
346 EXPORT_SYMBOL(pcmcia_register_driver);
349 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
350 * @driver: the &driver being unregistered
352 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
354 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
355 driver_unregister(&driver->drv);
356 pcmcia_free_dynids(driver);
358 EXPORT_SYMBOL(pcmcia_unregister_driver);
361 /* pcmcia_device handling */
363 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
365 struct device *tmp_dev;
366 tmp_dev = get_device(&p_dev->dev);
369 return to_pcmcia_dev(tmp_dev);
372 void pcmcia_put_dev(struct pcmcia_device *p_dev)
375 put_device(&p_dev->dev);
378 static void pcmcia_release_function(struct kref *ref)
380 struct config_t *c = container_of(ref, struct config_t, ref);
381 ds_dbg(1, "releasing config_t\n");
385 static void pcmcia_release_dev(struct device *dev)
387 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
388 ds_dev_dbg(1, dev, "releasing device\n");
389 pcmcia_put_socket(p_dev->socket);
390 kfree(p_dev->devname);
391 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
395 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
397 if (!s->pcmcia_state.device_add_pending) {
398 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
399 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
400 s->pcmcia_state.device_add_pending = 1;
401 s->pcmcia_state.mfc_pfc = mfc;
402 schedule_work(&s->device_add);
407 static int pcmcia_device_probe(struct device * dev)
409 struct pcmcia_device *p_dev;
410 struct pcmcia_driver *p_drv;
411 struct pcmcia_device_id *did;
412 struct pcmcia_socket *s;
413 cistpl_config_t cis_config;
416 dev = get_device(dev);
420 p_dev = to_pcmcia_dev(dev);
421 p_drv = to_pcmcia_drv(dev->driver);
424 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
426 if ((!p_drv->probe) || (!p_dev->function_config) ||
427 (!try_module_get(p_drv->owner))) {
432 /* set up some more device information */
433 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
436 p_dev->conf.ConfigBase = cis_config.base;
437 p_dev->conf.Present = cis_config.rmask[0];
439 dev_printk(KERN_INFO, dev,
440 "pcmcia: could not parse base and rmask0 of CIS\n");
441 p_dev->conf.ConfigBase = 0;
442 p_dev->conf.Present = 0;
445 ret = p_drv->probe(p_dev);
447 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
448 p_drv->drv.name, ret);
452 /* handle pseudo multifunction devices:
453 * there are at most two pseudo multifunction devices.
454 * if we're matching against the first, schedule a
455 * call which will then check whether there are two
456 * pseudo devices, and if not, add the second one.
458 did = p_dev->dev.driver_data;
459 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
460 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
461 pcmcia_add_device_later(p_dev->socket, 0);
465 module_put(p_drv->owner);
474 * Removes a PCMCIA card from the device tree and socket list.
476 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
478 struct pcmcia_device *p_dev;
479 struct pcmcia_device *tmp;
482 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
483 "pcmcia_card_remove(%d) %s\n", s->sock,
484 leftover ? leftover->devname : "");
491 /* unregister all pcmcia_devices registered with this socket, except leftover */
492 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
493 if (p_dev == leftover)
496 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
497 list_del(&p_dev->socket_device_list);
499 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
501 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
502 device_unregister(&p_dev->dev);
508 static int pcmcia_device_remove(struct device * dev)
510 struct pcmcia_device *p_dev;
511 struct pcmcia_driver *p_drv;
512 struct pcmcia_device_id *did;
515 p_dev = to_pcmcia_dev(dev);
516 p_drv = to_pcmcia_drv(dev->driver);
518 ds_dev_dbg(1, dev, "removing device\n");
520 /* If we're removing the primary module driving a
521 * pseudo multi-function card, we need to unbind
524 did = p_dev->dev.driver_data;
525 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
526 (p_dev->socket->device_count != 0) &&
527 (p_dev->device_no == 0))
528 pcmcia_card_remove(p_dev->socket, p_dev);
530 /* detach the "instance" */
535 p_drv->remove(p_dev);
537 p_dev->dev_node = NULL;
539 /* check for proper unloading */
540 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
541 dev_printk(KERN_INFO, dev,
542 "pcmcia: driver %s did not release config properly\n",
545 for (i = 0; i < MAX_WIN; i++)
546 if (p_dev->_win & CLIENT_WIN_REQ(i))
547 dev_printk(KERN_INFO, dev,
548 "pcmcia: driver %s did not release window properly\n",
551 /* references from pcmcia_probe_device */
552 pcmcia_put_dev(p_dev);
553 module_put(p_drv->owner);
560 * pcmcia_device_query -- determine information about a pcmcia device
562 static int pcmcia_device_query(struct pcmcia_device *p_dev)
564 cistpl_manfid_t manf_id;
565 cistpl_funcid_t func_id;
566 cistpl_vers_1_t *vers1;
569 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
573 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
574 CISTPL_MANFID, &manf_id)) {
575 p_dev->manf_id = manf_id.manf;
576 p_dev->card_id = manf_id.card;
577 p_dev->has_manf_id = 1;
578 p_dev->has_card_id = 1;
581 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
582 CISTPL_FUNCID, &func_id)) {
583 p_dev->func_id = func_id.func;
584 p_dev->has_func_id = 1;
586 /* rule of thumb: cards with no FUNCID, but with
587 * common memory device geometry information, are
588 * probably memory cards (from pcmcia-cs) */
589 cistpl_device_geo_t *devgeo;
591 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
596 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
597 CISTPL_DEVICE_GEO, devgeo)) {
598 ds_dev_dbg(0, &p_dev->dev,
599 "mem device geometry probably means "
601 p_dev->func_id = CISTPL_FUNCID_MEMORY;
602 p_dev->has_func_id = 1;
607 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
609 for (i=0; i < vers1->ns; i++) {
613 tmp = vers1->str + vers1->ofs[i];
615 length = strlen(tmp) + 1;
616 if ((length < 2) || (length > 255))
619 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
621 if (!p_dev->prod_id[i])
624 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
634 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
635 * Serializes pcmcia_device_add; will most likely be removed in future.
637 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
638 * won't work, this doesn't matter much at the moment: the driver core doesn't
641 static DEFINE_MUTEX(device_add_lock);
643 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
645 struct pcmcia_device *p_dev, *tmp_dev;
649 s = pcmcia_get_socket(s);
653 mutex_lock(&device_add_lock);
655 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
657 /* max of 4 devices per card */
658 if (s->device_count == 4)
661 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
666 p_dev->device_no = (s->device_count++);
667 p_dev->func = function;
669 p_dev->dev.bus = &pcmcia_bus_type;
670 p_dev->dev.parent = s->dev.parent;
671 p_dev->dev.release = pcmcia_release_dev;
672 /* by default don't allow DMA */
673 p_dev->dma_mask = DMA_MASK_NONE;
674 p_dev->dev.dma_mask = &p_dev->dma_mask;
675 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
677 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
680 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
681 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
683 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
686 * p_dev->function_config must be the same for all card functions.
687 * Note that this is serialized by the device_add_lock, so that
688 * only one such struct will be created.
690 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
691 if (p_dev->func == tmp_dev->func) {
692 p_dev->function_config = tmp_dev->function_config;
693 kref_get(&p_dev->function_config->ref);
696 /* Add to the list in pcmcia_bus_socket */
697 list_add(&p_dev->socket_device_list, &s->devices_list);
699 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
701 if (!p_dev->function_config) {
702 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
703 p_dev->function_config = kzalloc(sizeof(struct config_t),
705 if (!p_dev->function_config)
707 kref_init(&p_dev->function_config->ref);
710 dev_printk(KERN_NOTICE, &p_dev->dev,
711 "pcmcia: registering new device %s\n",
714 pcmcia_device_query(p_dev);
716 if (device_register(&p_dev->dev))
719 mutex_unlock(&device_add_lock);
724 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
725 list_del(&p_dev->socket_device_list);
726 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
729 kfree(p_dev->devname);
733 mutex_unlock(&device_add_lock);
734 pcmcia_put_socket(s);
740 static int pcmcia_card_add(struct pcmcia_socket *s)
742 cistpl_longlink_mfc_t mfc;
743 unsigned int no_funcs, i, no_chains;
746 if (!(s->resource_setup_done)) {
747 ds_dev_dbg(3, &s->dev,
748 "no resources available, delaying card_add\n");
749 return -EAGAIN; /* try again, but later... */
752 if (pcmcia_validate_mem(s)) {
753 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
754 "delaying card_add\n");
755 return -EAGAIN; /* try again, but later... */
758 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
759 if (ret || !no_chains) {
760 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
764 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
768 s->functions = no_funcs;
770 for (i=0; i < no_funcs; i++)
771 pcmcia_device_add(s, i);
777 static void pcmcia_delayed_add_device(struct work_struct *work)
779 struct pcmcia_socket *s =
780 container_of(work, struct pcmcia_socket, device_add);
781 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
782 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
783 s->pcmcia_state.device_add_pending = 0;
784 s->pcmcia_state.mfc_pfc = 0;
787 static int pcmcia_requery(struct device *dev, void * _data)
789 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
790 if (!p_dev->dev.driver) {
791 ds_dev_dbg(1, dev, "update device information\n");
792 pcmcia_device_query(p_dev);
798 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
804 /* must be called with skt_mutex held */
805 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
807 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
808 if (list_empty(&skt->devices_list))
810 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
812 /* If this is because of a CIS override, start over */
813 if (new_cis && !no_devices)
814 pcmcia_card_remove(skt, NULL);
816 /* if no devices were added for this socket yet because of
817 * missing resource information or other trouble, we need to
819 if (no_devices || new_cis) {
820 ret = pcmcia_card_add(skt);
825 /* some device information might have changed because of a CIS
826 * update or because we can finally read it correctly... so
827 * determine it again, overwriting old values if necessary. */
828 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
830 /* we re-scan all devices, not just the ones connected to this
831 * socket. This does not matter, though. */
832 ret = bus_rescan_devices(&pcmcia_bus_type);
834 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
837 #ifdef CONFIG_PCMCIA_LOAD_CIS
840 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
841 * @dev: the pcmcia device which needs a CIS override
842 * @filename: requested filename in /lib/firmware/
844 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
845 * the one provided by the card is broken. The firmware files reside in
846 * /lib/firmware/ in userspace.
848 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
850 struct pcmcia_socket *s = dev->socket;
851 const struct firmware *fw;
852 char path[FIRMWARE_NAME_MAX];
856 cistpl_longlink_mfc_t mfc;
861 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
863 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
864 dev_printk(KERN_WARNING, &dev->dev,
865 "pcmcia: CIS filename is too long [%s]\n",
870 snprintf(path, sizeof(path), "%s", filename);
872 if (request_firmware(&fw, path, &dev->dev) == 0) {
873 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
875 dev_printk(KERN_ERR, &dev->dev,
876 "pcmcia: CIS override is too big\n");
880 if (!pcmcia_replace_cis(s, fw->data, fw->size))
883 dev_printk(KERN_ERR, &dev->dev,
884 "pcmcia: CIS override failed\n");
889 /* update information */
890 pcmcia_device_query(dev);
892 /* does this cis override add or remove functions? */
893 old_funcs = s->functions;
895 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
899 s->functions = no_funcs;
901 if (old_funcs > no_funcs)
902 pcmcia_card_remove(s, dev);
903 else if (no_funcs > old_funcs)
904 pcmcia_add_device_later(s, 1);
907 release_firmware(fw);
912 #else /* !CONFIG_PCMCIA_LOAD_CIS */
914 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
922 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
923 struct pcmcia_device_id *did)
925 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
926 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
930 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
931 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
935 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
936 if (dev->func != did->function)
940 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
941 if (!dev->prod_id[0])
943 if (strcmp(did->prod_id[0], dev->prod_id[0]))
947 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
948 if (!dev->prod_id[1])
950 if (strcmp(did->prod_id[1], dev->prod_id[1]))
954 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
955 if (!dev->prod_id[2])
957 if (strcmp(did->prod_id[2], dev->prod_id[2]))
961 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
962 if (!dev->prod_id[3])
964 if (strcmp(did->prod_id[3], dev->prod_id[3]))
968 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
969 if (dev->device_no != did->device_no)
973 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
974 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
977 /* if this is a pseudo-multi-function device,
978 * we need explicit matches */
979 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
984 /* also, FUNC_ID matching needs to be activated by userspace
985 * after it has re-checked that there is no possible module
986 * with a prod_id/manf_id/card_id match.
988 ds_dev_dbg(0, &dev->dev,
989 "skipping FUNC_ID match until userspace interaction\n");
990 if (!dev->allow_func_id_match)
994 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
995 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
996 if (!dev->socket->fake_cis)
997 pcmcia_load_firmware(dev, did->cisfile);
999 if (!dev->socket->fake_cis)
1003 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
1006 if (dev->prod_id[i])
1008 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
1012 dev->dev.driver_data = (void *) did;
1018 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1019 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1020 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1021 struct pcmcia_device_id *did = p_drv->id_table;
1022 struct pcmcia_dynid *dynid;
1024 /* match dynamic devices first */
1025 spin_lock(&p_drv->dynids.lock);
1026 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
1027 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1028 if (pcmcia_devmatch(p_dev, &dynid->id)) {
1029 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1030 spin_unlock(&p_drv->dynids.lock);
1034 spin_unlock(&p_drv->dynids.lock);
1036 #ifdef CONFIG_PCMCIA_IOCTL
1037 /* matching by cardmgr */
1038 if (p_dev->cardmgr == p_drv) {
1039 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1044 while (did && did->match_flags) {
1045 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1046 if (pcmcia_devmatch(p_dev, did)) {
1047 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1056 #ifdef CONFIG_HOTPLUG
1058 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1060 struct pcmcia_device *p_dev;
1062 u32 hash[4] = { 0, 0, 0, 0};
1067 p_dev = to_pcmcia_dev(dev);
1069 /* calculate hashes */
1070 for (i=0; i<4; i++) {
1071 if (!p_dev->prod_id[i])
1073 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1076 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1079 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1082 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1083 "pa%08Xpb%08Xpc%08Xpd%08X",
1084 p_dev->has_manf_id ? p_dev->manf_id : 0,
1085 p_dev->has_card_id ? p_dev->card_id : 0,
1086 p_dev->has_func_id ? p_dev->func_id : 0,
1100 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1107 /************************ runtime PM support ***************************/
1109 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1110 static int pcmcia_dev_resume(struct device *dev);
1112 static int runtime_suspend(struct device *dev)
1117 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1122 static void runtime_resume(struct device *dev)
1127 rc = pcmcia_dev_resume(dev);
1131 /************************ per-device sysfs output ***************************/
1133 #define pcmcia_device_attr(field, test, format) \
1134 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1136 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1137 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1140 #define pcmcia_device_stringattr(name, field) \
1141 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1143 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1144 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1147 pcmcia_device_attr(func, socket, "0x%02x\n");
1148 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1149 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1150 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1151 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1152 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1153 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1154 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1157 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1159 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1161 if (p_dev->suspended)
1162 return sprintf(buf, "off\n");
1164 return sprintf(buf, "on\n");
1167 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1168 const char *buf, size_t count)
1170 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1176 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1177 ret = runtime_suspend(dev);
1178 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1179 runtime_resume(dev);
1181 return ret ? ret : count;
1185 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1187 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1189 u32 hash[4] = { 0, 0, 0, 0};
1191 /* calculate hashes */
1192 for (i=0; i<4; i++) {
1193 if (!p_dev->prod_id[i])
1195 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1197 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1198 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1199 p_dev->has_manf_id ? p_dev->manf_id : 0,
1200 p_dev->has_card_id ? p_dev->card_id : 0,
1201 p_dev->has_func_id ? p_dev->func_id : 0,
1202 p_dev->func, p_dev->device_no,
1203 hash[0], hash[1], hash[2], hash[3]);
1206 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1207 struct device_attribute *attr, const char *buf, size_t count)
1209 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1215 mutex_lock(&p_dev->socket->skt_mutex);
1216 p_dev->allow_func_id_match = 1;
1217 mutex_unlock(&p_dev->socket->skt_mutex);
1219 ret = bus_rescan_devices(&pcmcia_bus_type);
1221 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1222 "allowing func_id matches\n");
1227 static struct device_attribute pcmcia_dev_attrs[] = {
1228 __ATTR(function, 0444, func_show, NULL),
1229 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1233 __ATTR_RO(prod_id1),
1234 __ATTR_RO(prod_id2),
1235 __ATTR_RO(prod_id3),
1236 __ATTR_RO(prod_id4),
1237 __ATTR_RO(modalias),
1238 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1242 /* PM support, also needed for reset */
1244 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1246 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1247 struct pcmcia_driver *p_drv = NULL;
1250 if (p_dev->suspended)
1253 ds_dev_dbg(2, dev, "suspending\n");
1256 p_drv = to_pcmcia_drv(dev->driver);
1261 if (p_drv->suspend) {
1262 ret = p_drv->suspend(p_dev);
1264 dev_printk(KERN_ERR, dev,
1265 "pcmcia: device %s (driver %s) did "
1266 "not want to go to sleep (%d)\n",
1267 p_dev->devname, p_drv->drv.name, ret);
1272 if (p_dev->device_no == p_dev->func) {
1273 ds_dev_dbg(2, dev, "releasing configuration\n");
1274 pcmcia_release_configuration(p_dev);
1279 p_dev->suspended = 1;
1284 static int pcmcia_dev_resume(struct device * dev)
1286 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1287 struct pcmcia_driver *p_drv = NULL;
1290 if (!p_dev->suspended)
1293 ds_dev_dbg(2, dev, "resuming\n");
1296 p_drv = to_pcmcia_drv(dev->driver);
1301 if (p_dev->device_no == p_dev->func) {
1302 ds_dev_dbg(2, dev, "requesting configuration\n");
1303 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1309 ret = p_drv->resume(p_dev);
1313 p_dev->suspended = 0;
1318 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1320 struct pcmcia_socket *skt = _data;
1321 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1323 if (p_dev->socket != skt || p_dev->suspended)
1326 return runtime_suspend(dev);
1329 static int pcmcia_bus_resume_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 runtime_resume(dev);
1342 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1344 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1345 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1349 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1351 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1352 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1353 pcmcia_bus_suspend_callback)) {
1354 pcmcia_bus_resume(skt);
1361 /*======================================================================
1363 The card status event handler.
1365 ======================================================================*/
1367 /* Normally, the event is passed to individual drivers after
1368 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1369 * is inversed to maintain historic compatibility.
1372 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1374 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1377 dev_printk(KERN_ERR, &skt->dev,
1378 "PCMCIA obtaining reference to socket " \
1379 "failed, event 0x%x lost!\n", event);
1383 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1384 event, priority, skt);
1387 case CS_EVENT_CARD_REMOVAL:
1388 s->pcmcia_state.present = 0;
1389 pcmcia_card_remove(skt, NULL);
1390 handle_event(skt, event);
1393 case CS_EVENT_CARD_INSERTION:
1394 s->pcmcia_state.present = 1;
1395 pcmcia_card_add(skt);
1396 handle_event(skt, event);
1399 case CS_EVENT_EJECTION_REQUEST:
1402 case CS_EVENT_PM_SUSPEND:
1403 case CS_EVENT_PM_RESUME:
1404 case CS_EVENT_RESET_PHYSICAL:
1405 case CS_EVENT_CARD_RESET:
1407 handle_event(skt, event);
1411 pcmcia_put_socket(s);
1417 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1419 struct pcmcia_device *p_dev;
1420 struct pcmcia_device *ret = NULL;
1422 p_dev = pcmcia_get_dev(_p_dev);
1426 if (!p_dev->socket->pcmcia_state.present)
1429 if (p_dev->_removed)
1432 if (p_dev->suspended)
1437 pcmcia_put_dev(p_dev);
1440 EXPORT_SYMBOL(pcmcia_dev_present);
1443 static struct pcmcia_callback pcmcia_bus_callback = {
1444 .owner = THIS_MODULE,
1446 .requery = pcmcia_bus_rescan,
1447 .suspend = pcmcia_bus_suspend,
1448 .resume = pcmcia_bus_resume,
1451 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1452 struct class_interface *class_intf)
1454 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1457 socket = pcmcia_get_socket(socket);
1459 dev_printk(KERN_ERR, dev,
1460 "PCMCIA obtaining reference to socket failed\n");
1465 * Ugly. But we want to wait for the socket threads to have started up.
1466 * We really should let the drivers themselves drive some of this..
1470 #ifdef CONFIG_PCMCIA_IOCTL
1471 init_waitqueue_head(&socket->queue);
1473 INIT_LIST_HEAD(&socket->devices_list);
1474 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1475 memset(&socket->pcmcia_state, 0, sizeof(u8));
1476 socket->device_count = 0;
1478 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1480 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1481 pcmcia_put_socket(socket);
1488 static void pcmcia_bus_remove_socket(struct device *dev,
1489 struct class_interface *class_intf)
1491 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1496 socket->pcmcia_state.dead = 1;
1497 pccard_register_pcmcia(socket, NULL);
1499 /* unregister any unbound devices */
1500 mutex_lock(&socket->skt_mutex);
1501 pcmcia_card_remove(socket, NULL);
1502 mutex_unlock(&socket->skt_mutex);
1504 pcmcia_put_socket(socket);
1510 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1511 static struct class_interface pcmcia_bus_interface __refdata = {
1512 .class = &pcmcia_socket_class,
1513 .add_dev = &pcmcia_bus_add_socket,
1514 .remove_dev = &pcmcia_bus_remove_socket,
1518 struct bus_type pcmcia_bus_type = {
1520 .uevent = pcmcia_bus_uevent,
1521 .match = pcmcia_bus_match,
1522 .dev_attrs = pcmcia_dev_attrs,
1523 .probe = pcmcia_device_probe,
1524 .remove = pcmcia_device_remove,
1525 .suspend = pcmcia_dev_suspend,
1526 .resume = pcmcia_dev_resume,
1530 static int __init init_pcmcia_bus(void)
1534 spin_lock_init(&pcmcia_dev_list_lock);
1536 ret = bus_register(&pcmcia_bus_type);
1538 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1541 ret = class_interface_register(&pcmcia_bus_interface);
1544 "pcmcia: class_interface_register error: %d\n", ret);
1545 bus_unregister(&pcmcia_bus_type);
1549 pcmcia_setup_ioctl();
1553 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1554 * pcmcia_socket_class is already registered */
1557 static void __exit exit_pcmcia_bus(void)
1559 pcmcia_cleanup_ioctl();
1561 class_interface_unregister(&pcmcia_bus_interface);
1563 bus_unregister(&pcmcia_bus_type);
1565 module_exit(exit_pcmcia_bus);