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 { 0, "Operation succeeded" },
78 { -EIO, "Input/Output error" },
79 { -ENODEV, "No card present" },
80 { -EINVAL, "Bad parameter" },
81 { -EACCES, "Configuration locked" },
82 { -EBUSY, "Resource in use" },
83 { -ENOSPC, "No more items" },
84 { -ENOMEM, "Out of resource" },
88 static const lookup_t service_table[] = {
89 { AccessConfigurationRegister, "AccessConfigurationRegister" },
90 { AddSocketServices, "AddSocketServices" },
91 { AdjustResourceInfo, "AdjustResourceInfo" },
92 { CheckEraseQueue, "CheckEraseQueue" },
93 { CloseMemory, "CloseMemory" },
94 { DeregisterClient, "DeregisterClient" },
95 { DeregisterEraseQueue, "DeregisterEraseQueue" },
96 { GetCardServicesInfo, "GetCardServicesInfo" },
97 { GetClientInfo, "GetClientInfo" },
98 { GetConfigurationInfo, "GetConfigurationInfo" },
99 { GetEventMask, "GetEventMask" },
100 { GetFirstClient, "GetFirstClient" },
101 { GetFirstRegion, "GetFirstRegion" },
102 { GetFirstTuple, "GetFirstTuple" },
103 { GetNextClient, "GetNextClient" },
104 { GetNextRegion, "GetNextRegion" },
105 { GetNextTuple, "GetNextTuple" },
106 { GetStatus, "GetStatus" },
107 { GetTupleData, "GetTupleData" },
108 { MapMemPage, "MapMemPage" },
109 { ModifyConfiguration, "ModifyConfiguration" },
110 { ModifyWindow, "ModifyWindow" },
111 { OpenMemory, "OpenMemory" },
112 { ParseTuple, "ParseTuple" },
113 { ReadMemory, "ReadMemory" },
114 { RegisterClient, "RegisterClient" },
115 { RegisterEraseQueue, "RegisterEraseQueue" },
116 { RegisterMTD, "RegisterMTD" },
117 { ReleaseConfiguration, "ReleaseConfiguration" },
118 { ReleaseIO, "ReleaseIO" },
119 { ReleaseIRQ, "ReleaseIRQ" },
120 { ReleaseWindow, "ReleaseWindow" },
121 { RequestConfiguration, "RequestConfiguration" },
122 { RequestIO, "RequestIO" },
123 { RequestIRQ, "RequestIRQ" },
124 { RequestSocketMask, "RequestSocketMask" },
125 { RequestWindow, "RequestWindow" },
126 { ResetCard, "ResetCard" },
127 { SetEventMask, "SetEventMask" },
128 { ValidateCIS, "ValidateCIS" },
129 { WriteMemory, "WriteMemory" },
130 { BindDevice, "BindDevice" },
131 { BindMTD, "BindMTD" },
132 { ReportError, "ReportError" },
133 { SuspendCard, "SuspendCard" },
134 { ResumeCard, "ResumeCard" },
135 { EjectCard, "EjectCard" },
136 { InsertCard, "InsertCard" },
137 { ReplaceCIS, "ReplaceCIS" }
141 static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
149 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
151 for (i = 0; i < ARRAY_SIZE(service_table); i++)
152 if (service_table[i].key == err->func)
154 if (i < ARRAY_SIZE(service_table))
155 serv = service_table[i].msg;
157 serv = "Unknown service number";
159 for (i = 0; i < ARRAY_SIZE(error_table); i++)
160 if (error_table[i].key == err->retcode)
162 if (i < ARRAY_SIZE(error_table))
163 printk("%s: %s\n", serv, error_table[i].msg);
165 printk("%s: Unknown error code %#x\n", serv, err->retcode);
170 /* end of code which was in cs.c before */
172 /*======================================================================*/
174 void cs_error(struct pcmcia_device *p_dev, int func, int ret)
176 error_info_t err = { func, ret };
177 pcmcia_report_error(p_dev, &err);
179 EXPORT_SYMBOL(cs_error);
182 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
184 struct pcmcia_device_id *did = p_drv->id_table;
188 if (!p_drv->probe || !p_drv->remove)
189 printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
190 "function\n", p_drv->drv.name);
192 while (did && did->match_flags) {
193 for (i=0; i<4; i++) {
194 if (!did->prod_id[i])
197 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
198 if (hash == did->prod_id_hash[i])
201 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
202 "product string \"%s\": is 0x%x, should "
203 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
204 did->prod_id_hash[i], hash);
205 printk(KERN_DEBUG "pcmcia: see "
206 "Documentation/pcmcia/devicetable.txt for "
216 /*======================================================================*/
219 struct pcmcia_dynid {
220 struct list_head node;
221 struct pcmcia_device_id id;
225 * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
226 * @driver: target device driver
227 * @buf: buffer for scanning device ID data
230 * Adds a new dynamic PCMCIA device ID to this driver,
231 * and causes the driver to probe for all devices again.
234 pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
236 struct pcmcia_dynid *dynid;
237 struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
238 __u16 match_flags, manf_id, card_id;
239 __u8 func_id, function, device_no;
240 __u32 prod_id_hash[4] = {0, 0, 0, 0};
244 fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
245 &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
246 &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
250 dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
254 INIT_LIST_HEAD(&dynid->node);
255 dynid->id.match_flags = match_flags;
256 dynid->id.manf_id = manf_id;
257 dynid->id.card_id = card_id;
258 dynid->id.func_id = func_id;
259 dynid->id.function = function;
260 dynid->id.device_no = device_no;
261 memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
263 spin_lock(&pdrv->dynids.lock);
264 list_add_tail(&pdrv->dynids.list, &dynid->node);
265 spin_unlock(&pdrv->dynids.lock);
267 if (get_driver(&pdrv->drv)) {
268 retval = driver_attach(&pdrv->drv);
269 put_driver(&pdrv->drv);
276 static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
279 pcmcia_free_dynids(struct pcmcia_driver *drv)
281 struct pcmcia_dynid *dynid, *n;
283 spin_lock(&drv->dynids.lock);
284 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
285 list_del(&dynid->node);
288 spin_unlock(&drv->dynids.lock);
292 pcmcia_create_newid_file(struct pcmcia_driver *drv)
295 if (drv->probe != NULL)
296 error = driver_create_file(&drv->drv, &driver_attr_new_id);
302 * pcmcia_register_driver - register a PCMCIA driver with the bus core
303 * @driver: the &driver being registered
305 * Registers a PCMCIA driver with the PCMCIA bus core.
307 int pcmcia_register_driver(struct pcmcia_driver *driver)
314 pcmcia_check_driver(driver);
316 /* initialize common fields */
317 driver->drv.bus = &pcmcia_bus_type;
318 driver->drv.owner = driver->owner;
319 spin_lock_init(&driver->dynids.lock);
320 INIT_LIST_HEAD(&driver->dynids.list);
322 ds_dbg(3, "registering driver %s\n", driver->drv.name);
324 error = driver_register(&driver->drv);
328 error = pcmcia_create_newid_file(driver);
330 driver_unregister(&driver->drv);
334 EXPORT_SYMBOL(pcmcia_register_driver);
337 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
338 * @driver: the &driver being unregistered
340 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
342 ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
343 driver_unregister(&driver->drv);
344 pcmcia_free_dynids(driver);
346 EXPORT_SYMBOL(pcmcia_unregister_driver);
349 /* pcmcia_device handling */
351 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
353 struct device *tmp_dev;
354 tmp_dev = get_device(&p_dev->dev);
357 return to_pcmcia_dev(tmp_dev);
360 void pcmcia_put_dev(struct pcmcia_device *p_dev)
363 put_device(&p_dev->dev);
366 static void pcmcia_release_function(struct kref *ref)
368 struct config_t *c = container_of(ref, struct config_t, ref);
369 ds_dbg(1, "releasing config_t\n");
373 static void pcmcia_release_dev(struct device *dev)
375 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
376 ds_dev_dbg(1, dev, "releasing device\n");
377 pcmcia_put_socket(p_dev->socket);
378 kfree(p_dev->devname);
379 kref_put(&p_dev->function_config->ref, pcmcia_release_function);
383 static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
385 if (!s->pcmcia_state.device_add_pending) {
386 ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
387 " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
388 s->pcmcia_state.device_add_pending = 1;
389 s->pcmcia_state.mfc_pfc = mfc;
390 schedule_work(&s->device_add);
395 static int pcmcia_device_probe(struct device * dev)
397 struct pcmcia_device *p_dev;
398 struct pcmcia_driver *p_drv;
399 struct pcmcia_device_id *did;
400 struct pcmcia_socket *s;
401 cistpl_config_t cis_config;
404 dev = get_device(dev);
408 p_dev = to_pcmcia_dev(dev);
409 p_drv = to_pcmcia_drv(dev->driver);
412 ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
414 if ((!p_drv->probe) || (!p_dev->function_config) ||
415 (!try_module_get(p_drv->owner))) {
420 /* set up some more device information */
421 ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
424 p_dev->conf.ConfigBase = cis_config.base;
425 p_dev->conf.Present = cis_config.rmask[0];
427 dev_printk(KERN_INFO, dev,
428 "pcmcia: could not parse base and rmask0 of CIS\n");
429 p_dev->conf.ConfigBase = 0;
430 p_dev->conf.Present = 0;
433 ret = p_drv->probe(p_dev);
435 ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
436 p_drv->drv.name, ret);
440 /* handle pseudo multifunction devices:
441 * there are at most two pseudo multifunction devices.
442 * if we're matching against the first, schedule a
443 * call which will then check whether there are two
444 * pseudo devices, and if not, add the second one.
446 did = p_dev->dev.driver_data;
447 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
448 (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
449 pcmcia_add_device_later(p_dev->socket, 0);
453 module_put(p_drv->owner);
462 * Removes a PCMCIA card from the device tree and socket list.
464 static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
466 struct pcmcia_device *p_dev;
467 struct pcmcia_device *tmp;
470 ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
471 "pcmcia_card_remove(%d) %s\n", s->sock,
472 leftover ? leftover->devname : "");
479 /* unregister all pcmcia_devices registered with this socket, except leftover */
480 list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
481 if (p_dev == leftover)
484 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
485 list_del(&p_dev->socket_device_list);
487 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
489 ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
490 device_unregister(&p_dev->dev);
496 static int pcmcia_device_remove(struct device * dev)
498 struct pcmcia_device *p_dev;
499 struct pcmcia_driver *p_drv;
500 struct pcmcia_device_id *did;
503 p_dev = to_pcmcia_dev(dev);
504 p_drv = to_pcmcia_drv(dev->driver);
506 ds_dev_dbg(1, dev, "removing device\n");
508 /* If we're removing the primary module driving a
509 * pseudo multi-function card, we need to unbind
512 did = p_dev->dev.driver_data;
513 if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
514 (p_dev->socket->device_count != 0) &&
515 (p_dev->device_no == 0))
516 pcmcia_card_remove(p_dev->socket, p_dev);
518 /* detach the "instance" */
523 p_drv->remove(p_dev);
525 p_dev->dev_node = NULL;
527 /* check for proper unloading */
528 if (p_dev->_irq || p_dev->_io || p_dev->_locked)
529 dev_printk(KERN_INFO, dev,
530 "pcmcia: driver %s did not release config properly\n",
533 for (i = 0; i < MAX_WIN; i++)
534 if (p_dev->_win & CLIENT_WIN_REQ(i))
535 dev_printk(KERN_INFO, dev,
536 "pcmcia: driver %s did not release window properly\n",
539 /* references from pcmcia_probe_device */
540 pcmcia_put_dev(p_dev);
541 module_put(p_drv->owner);
548 * pcmcia_device_query -- determine information about a pcmcia device
550 static int pcmcia_device_query(struct pcmcia_device *p_dev)
552 cistpl_manfid_t manf_id;
553 cistpl_funcid_t func_id;
554 cistpl_vers_1_t *vers1;
557 vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
561 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
562 CISTPL_MANFID, &manf_id)) {
563 p_dev->manf_id = manf_id.manf;
564 p_dev->card_id = manf_id.card;
565 p_dev->has_manf_id = 1;
566 p_dev->has_card_id = 1;
569 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
570 CISTPL_FUNCID, &func_id)) {
571 p_dev->func_id = func_id.func;
572 p_dev->has_func_id = 1;
574 /* rule of thumb: cards with no FUNCID, but with
575 * common memory device geometry information, are
576 * probably memory cards (from pcmcia-cs) */
577 cistpl_device_geo_t *devgeo;
579 devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
584 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
585 CISTPL_DEVICE_GEO, devgeo)) {
586 ds_dev_dbg(0, &p_dev->dev,
587 "mem device geometry probably means "
589 p_dev->func_id = CISTPL_FUNCID_MEMORY;
590 p_dev->has_func_id = 1;
595 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
597 for (i=0; i < vers1->ns; i++) {
601 tmp = vers1->str + vers1->ofs[i];
603 length = strlen(tmp) + 1;
604 if ((length < 2) || (length > 255))
607 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
609 if (!p_dev->prod_id[i])
612 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
622 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
623 * Serializes pcmcia_device_add; will most likely be removed in future.
625 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
626 * won't work, this doesn't matter much at the moment: the driver core doesn't
629 static DEFINE_MUTEX(device_add_lock);
631 struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
633 struct pcmcia_device *p_dev, *tmp_dev;
637 s = pcmcia_get_socket(s);
641 mutex_lock(&device_add_lock);
643 ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
645 /* max of 4 devices per card */
646 if (s->device_count == 4)
649 p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
654 p_dev->device_no = (s->device_count++);
655 p_dev->func = function;
657 p_dev->dev.bus = &pcmcia_bus_type;
658 p_dev->dev.parent = s->dev.parent;
659 p_dev->dev.release = pcmcia_release_dev;
660 /* by default don't allow DMA */
661 p_dev->dma_mask = DMA_MASK_NONE;
662 p_dev->dev.dma_mask = &p_dev->dma_mask;
663 bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
665 p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
668 sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
669 ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
671 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
674 * p_dev->function_config must be the same for all card functions.
675 * Note that this is serialized by the device_add_lock, so that
676 * only one such struct will be created.
678 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
679 if (p_dev->func == tmp_dev->func) {
680 p_dev->function_config = tmp_dev->function_config;
681 kref_get(&p_dev->function_config->ref);
684 /* Add to the list in pcmcia_bus_socket */
685 list_add(&p_dev->socket_device_list, &s->devices_list);
687 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
689 if (!p_dev->function_config) {
690 ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
691 p_dev->function_config = kzalloc(sizeof(struct config_t),
693 if (!p_dev->function_config)
695 kref_init(&p_dev->function_config->ref);
698 dev_printk(KERN_NOTICE, &p_dev->dev,
699 "pcmcia: registering new device %s\n",
702 pcmcia_device_query(p_dev);
704 if (device_register(&p_dev->dev))
707 mutex_unlock(&device_add_lock);
712 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
713 list_del(&p_dev->socket_device_list);
714 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
717 kfree(p_dev->devname);
721 mutex_unlock(&device_add_lock);
722 pcmcia_put_socket(s);
728 static int pcmcia_card_add(struct pcmcia_socket *s)
730 cistpl_longlink_mfc_t mfc;
731 unsigned int no_funcs, i, no_chains;
734 if (!(s->resource_setup_done)) {
735 ds_dev_dbg(3, &s->dev,
736 "no resources available, delaying card_add\n");
737 return -EAGAIN; /* try again, but later... */
740 if (pcmcia_validate_mem(s)) {
741 ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
742 "delaying card_add\n");
743 return -EAGAIN; /* try again, but later... */
746 ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
747 if (ret || !no_chains) {
748 ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
752 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
756 s->functions = no_funcs;
758 for (i=0; i < no_funcs; i++)
759 pcmcia_device_add(s, i);
765 static void pcmcia_delayed_add_device(struct work_struct *work)
767 struct pcmcia_socket *s =
768 container_of(work, struct pcmcia_socket, device_add);
769 ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
770 pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
771 s->pcmcia_state.device_add_pending = 0;
772 s->pcmcia_state.mfc_pfc = 0;
775 static int pcmcia_requery(struct device *dev, void * _data)
777 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
778 if (!p_dev->dev.driver) {
779 ds_dev_dbg(1, dev, "update device information\n");
780 pcmcia_device_query(p_dev);
786 static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
792 /* must be called with skt_mutex held */
793 ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
795 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
796 if (list_empty(&skt->devices_list))
798 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
800 /* If this is because of a CIS override, start over */
801 if (new_cis && !no_devices)
802 pcmcia_card_remove(skt, NULL);
804 /* if no devices were added for this socket yet because of
805 * missing resource information or other trouble, we need to
807 if (no_devices || new_cis) {
808 ret = pcmcia_card_add(skt);
813 /* some device information might have changed because of a CIS
814 * update or because we can finally read it correctly... so
815 * determine it again, overwriting old values if necessary. */
816 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
818 /* we re-scan all devices, not just the ones connected to this
819 * socket. This does not matter, though. */
820 ret = bus_rescan_devices(&pcmcia_bus_type);
822 printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
825 #ifdef CONFIG_PCMCIA_LOAD_CIS
828 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
829 * @dev: the pcmcia device which needs a CIS override
830 * @filename: requested filename in /lib/firmware/
832 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
833 * the one provided by the card is broken. The firmware files reside in
834 * /lib/firmware/ in userspace.
836 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
838 struct pcmcia_socket *s = dev->socket;
839 const struct firmware *fw;
840 char path[FIRMWARE_NAME_MAX];
844 cistpl_longlink_mfc_t mfc;
849 ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
851 if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
852 dev_printk(KERN_WARNING, &dev->dev,
853 "pcmcia: CIS filename is too long [%s]\n",
858 snprintf(path, sizeof(path), "%s", filename);
860 if (request_firmware(&fw, path, &dev->dev) == 0) {
861 if (fw->size >= CISTPL_MAX_CIS_SIZE) {
863 dev_printk(KERN_ERR, &dev->dev,
864 "pcmcia: CIS override is too big\n");
868 if (!pcmcia_replace_cis(s, fw->data, fw->size))
871 dev_printk(KERN_ERR, &dev->dev,
872 "pcmcia: CIS override failed\n");
877 /* update information */
878 pcmcia_device_query(dev);
880 /* does this cis override add or remove functions? */
881 old_funcs = s->functions;
883 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
887 s->functions = no_funcs;
889 if (old_funcs > no_funcs)
890 pcmcia_card_remove(s, dev);
891 else if (no_funcs > old_funcs)
892 pcmcia_add_device_later(s, 1);
895 release_firmware(fw);
900 #else /* !CONFIG_PCMCIA_LOAD_CIS */
902 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
910 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
911 struct pcmcia_device_id *did)
913 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
914 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
918 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
919 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
923 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
924 if (dev->func != did->function)
928 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
929 if (!dev->prod_id[0])
931 if (strcmp(did->prod_id[0], dev->prod_id[0]))
935 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
936 if (!dev->prod_id[1])
938 if (strcmp(did->prod_id[1], dev->prod_id[1]))
942 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
943 if (!dev->prod_id[2])
945 if (strcmp(did->prod_id[2], dev->prod_id[2]))
949 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
950 if (!dev->prod_id[3])
952 if (strcmp(did->prod_id[3], dev->prod_id[3]))
956 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
957 if (dev->device_no != did->device_no)
961 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
962 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
965 /* if this is a pseudo-multi-function device,
966 * we need explicit matches */
967 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
972 /* also, FUNC_ID matching needs to be activated by userspace
973 * after it has re-checked that there is no possible module
974 * with a prod_id/manf_id/card_id match.
976 ds_dev_dbg(0, &dev->dev,
977 "skipping FUNC_ID match until userspace interaction\n");
978 if (!dev->allow_func_id_match)
982 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
983 ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
984 if (!dev->socket->fake_cis)
985 pcmcia_load_firmware(dev, did->cisfile);
987 if (!dev->socket->fake_cis)
991 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
996 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
1000 dev->dev.driver_data = (void *) did;
1006 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
1007 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
1008 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
1009 struct pcmcia_device_id *did = p_drv->id_table;
1010 struct pcmcia_dynid *dynid;
1012 /* match dynamic devices first */
1013 spin_lock(&p_drv->dynids.lock);
1014 list_for_each_entry(dynid, &p_drv->dynids.list, node) {
1015 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1016 if (pcmcia_devmatch(p_dev, &dynid->id)) {
1017 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1018 spin_unlock(&p_drv->dynids.lock);
1022 spin_unlock(&p_drv->dynids.lock);
1024 #ifdef CONFIG_PCMCIA_IOCTL
1025 /* matching by cardmgr */
1026 if (p_dev->cardmgr == p_drv) {
1027 ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
1032 while (did && did->match_flags) {
1033 ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
1034 if (pcmcia_devmatch(p_dev, did)) {
1035 ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
1044 #ifdef CONFIG_HOTPLUG
1046 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1048 struct pcmcia_device *p_dev;
1050 u32 hash[4] = { 0, 0, 0, 0};
1055 p_dev = to_pcmcia_dev(dev);
1057 /* calculate hashes */
1058 for (i=0; i<4; i++) {
1059 if (!p_dev->prod_id[i])
1061 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
1064 if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
1067 if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
1070 if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1071 "pa%08Xpb%08Xpc%08Xpd%08X",
1072 p_dev->has_manf_id ? p_dev->manf_id : 0,
1073 p_dev->has_card_id ? p_dev->card_id : 0,
1074 p_dev->has_func_id ? p_dev->func_id : 0,
1088 static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
1095 /************************ runtime PM support ***************************/
1097 static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
1098 static int pcmcia_dev_resume(struct device *dev);
1100 static int runtime_suspend(struct device *dev)
1105 rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
1110 static void runtime_resume(struct device *dev)
1115 rc = pcmcia_dev_resume(dev);
1119 /************************ per-device sysfs output ***************************/
1121 #define pcmcia_device_attr(field, test, format) \
1122 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1124 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1125 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
1128 #define pcmcia_device_stringattr(name, field) \
1129 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
1131 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
1132 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
1135 pcmcia_device_attr(func, socket, "0x%02x\n");
1136 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
1137 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
1138 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
1139 pcmcia_device_stringattr(prod_id1, prod_id[0]);
1140 pcmcia_device_stringattr(prod_id2, prod_id[1]);
1141 pcmcia_device_stringattr(prod_id3, prod_id[2]);
1142 pcmcia_device_stringattr(prod_id4, prod_id[3]);
1145 static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
1147 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1149 if (p_dev->suspended)
1150 return sprintf(buf, "off\n");
1152 return sprintf(buf, "on\n");
1155 static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
1156 const char *buf, size_t count)
1158 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1164 if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
1165 ret = runtime_suspend(dev);
1166 else if (p_dev->suspended && !strncmp(buf, "on", 2))
1167 runtime_resume(dev);
1169 return ret ? ret : count;
1173 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1175 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1177 u32 hash[4] = { 0, 0, 0, 0};
1179 /* calculate hashes */
1180 for (i=0; i<4; i++) {
1181 if (!p_dev->prod_id[i])
1183 hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
1185 return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
1186 "pa%08Xpb%08Xpc%08Xpd%08X\n",
1187 p_dev->has_manf_id ? p_dev->manf_id : 0,
1188 p_dev->has_card_id ? p_dev->card_id : 0,
1189 p_dev->has_func_id ? p_dev->func_id : 0,
1190 p_dev->func, p_dev->device_no,
1191 hash[0], hash[1], hash[2], hash[3]);
1194 static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
1195 struct device_attribute *attr, const char *buf, size_t count)
1197 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1203 mutex_lock(&p_dev->socket->skt_mutex);
1204 p_dev->allow_func_id_match = 1;
1205 mutex_unlock(&p_dev->socket->skt_mutex);
1207 ret = bus_rescan_devices(&pcmcia_bus_type);
1209 printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
1210 "allowing func_id matches\n");
1215 static struct device_attribute pcmcia_dev_attrs[] = {
1216 __ATTR(function, 0444, func_show, NULL),
1217 __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
1221 __ATTR_RO(prod_id1),
1222 __ATTR_RO(prod_id2),
1223 __ATTR_RO(prod_id3),
1224 __ATTR_RO(prod_id4),
1225 __ATTR_RO(modalias),
1226 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
1230 /* PM support, also needed for reset */
1232 static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
1234 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1235 struct pcmcia_driver *p_drv = NULL;
1238 if (p_dev->suspended)
1241 ds_dev_dbg(2, dev, "suspending\n");
1244 p_drv = to_pcmcia_drv(dev->driver);
1249 if (p_drv->suspend) {
1250 ret = p_drv->suspend(p_dev);
1252 dev_printk(KERN_ERR, dev,
1253 "pcmcia: device %s (driver %s) did "
1254 "not want to go to sleep (%d)\n",
1255 p_dev->devname, p_drv->drv.name, ret);
1260 if (p_dev->device_no == p_dev->func) {
1261 ds_dev_dbg(2, dev, "releasing configuration\n");
1262 pcmcia_release_configuration(p_dev);
1267 p_dev->suspended = 1;
1272 static int pcmcia_dev_resume(struct device * dev)
1274 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1275 struct pcmcia_driver *p_drv = NULL;
1278 if (!p_dev->suspended)
1281 ds_dev_dbg(2, dev, "resuming\n");
1284 p_drv = to_pcmcia_drv(dev->driver);
1289 if (p_dev->device_no == p_dev->func) {
1290 ds_dev_dbg(2, dev, "requesting configuration\n");
1291 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
1297 ret = p_drv->resume(p_dev);
1301 p_dev->suspended = 0;
1306 static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
1308 struct pcmcia_socket *skt = _data;
1309 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1311 if (p_dev->socket != skt || p_dev->suspended)
1314 return runtime_suspend(dev);
1317 static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
1319 struct pcmcia_socket *skt = _data;
1320 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1322 if (p_dev->socket != skt || !p_dev->suspended)
1325 runtime_resume(dev);
1330 static int pcmcia_bus_resume(struct pcmcia_socket *skt)
1332 ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
1333 bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
1337 static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
1339 ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
1340 if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
1341 pcmcia_bus_suspend_callback)) {
1342 pcmcia_bus_resume(skt);
1349 /*======================================================================
1351 The card status event handler.
1353 ======================================================================*/
1355 /* Normally, the event is passed to individual drivers after
1356 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1357 * is inversed to maintain historic compatibility.
1360 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1362 struct pcmcia_socket *s = pcmcia_get_socket(skt);
1365 dev_printk(KERN_ERR, &skt->dev,
1366 "PCMCIA obtaining reference to socket " \
1367 "failed, event 0x%x lost!\n", event);
1371 ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
1372 event, priority, skt);
1375 case CS_EVENT_CARD_REMOVAL:
1376 s->pcmcia_state.present = 0;
1377 pcmcia_card_remove(skt, NULL);
1378 handle_event(skt, event);
1381 case CS_EVENT_CARD_INSERTION:
1382 s->pcmcia_state.present = 1;
1383 pcmcia_card_add(skt);
1384 handle_event(skt, event);
1387 case CS_EVENT_EJECTION_REQUEST:
1390 case CS_EVENT_PM_SUSPEND:
1391 case CS_EVENT_PM_RESUME:
1392 case CS_EVENT_RESET_PHYSICAL:
1393 case CS_EVENT_CARD_RESET:
1395 handle_event(skt, event);
1399 pcmcia_put_socket(s);
1405 struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
1407 struct pcmcia_device *p_dev;
1408 struct pcmcia_device *ret = NULL;
1410 p_dev = pcmcia_get_dev(_p_dev);
1414 if (!p_dev->socket->pcmcia_state.present)
1417 if (p_dev->_removed)
1420 if (p_dev->suspended)
1425 pcmcia_put_dev(p_dev);
1428 EXPORT_SYMBOL(pcmcia_dev_present);
1431 static struct pcmcia_callback pcmcia_bus_callback = {
1432 .owner = THIS_MODULE,
1434 .requery = pcmcia_bus_rescan,
1435 .suspend = pcmcia_bus_suspend,
1436 .resume = pcmcia_bus_resume,
1439 static int __devinit pcmcia_bus_add_socket(struct device *dev,
1440 struct class_interface *class_intf)
1442 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1445 socket = pcmcia_get_socket(socket);
1447 dev_printk(KERN_ERR, dev,
1448 "PCMCIA obtaining reference to socket failed\n");
1453 * Ugly. But we want to wait for the socket threads to have started up.
1454 * We really should let the drivers themselves drive some of this..
1458 #ifdef CONFIG_PCMCIA_IOCTL
1459 init_waitqueue_head(&socket->queue);
1461 INIT_LIST_HEAD(&socket->devices_list);
1462 INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
1463 memset(&socket->pcmcia_state, 0, sizeof(u8));
1464 socket->device_count = 0;
1466 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1468 dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
1469 pcmcia_put_socket(socket);
1476 static void pcmcia_bus_remove_socket(struct device *dev,
1477 struct class_interface *class_intf)
1479 struct pcmcia_socket *socket = dev_get_drvdata(dev);
1484 socket->pcmcia_state.dead = 1;
1485 pccard_register_pcmcia(socket, NULL);
1487 /* unregister any unbound devices */
1488 mutex_lock(&socket->skt_mutex);
1489 pcmcia_card_remove(socket, NULL);
1490 mutex_unlock(&socket->skt_mutex);
1492 pcmcia_put_socket(socket);
1498 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1499 static struct class_interface pcmcia_bus_interface __refdata = {
1500 .class = &pcmcia_socket_class,
1501 .add_dev = &pcmcia_bus_add_socket,
1502 .remove_dev = &pcmcia_bus_remove_socket,
1506 struct bus_type pcmcia_bus_type = {
1508 .uevent = pcmcia_bus_uevent,
1509 .match = pcmcia_bus_match,
1510 .dev_attrs = pcmcia_dev_attrs,
1511 .probe = pcmcia_device_probe,
1512 .remove = pcmcia_device_remove,
1513 .suspend = pcmcia_dev_suspend,
1514 .resume = pcmcia_dev_resume,
1518 static int __init init_pcmcia_bus(void)
1522 spin_lock_init(&pcmcia_dev_list_lock);
1524 ret = bus_register(&pcmcia_bus_type);
1526 printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
1529 ret = class_interface_register(&pcmcia_bus_interface);
1532 "pcmcia: class_interface_register error: %d\n", ret);
1533 bus_unregister(&pcmcia_bus_type);
1537 pcmcia_setup_ioctl();
1541 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1542 * pcmcia_socket_class is already registered */
1545 static void __exit exit_pcmcia_bus(void)
1547 pcmcia_cleanup_ioctl();
1549 class_interface_unregister(&pcmcia_bus_interface);
1551 bus_unregister(&pcmcia_bus_type);
1553 module_exit(exit_pcmcia_bus);