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 - 2004 Dominik Brodowski
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/timer.h>
30 #include <linux/ioctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/poll.h>
33 #include <linux/pci.h>
34 #include <linux/list.h>
35 #include <linux/delay.h>
36 #include <linux/kref.h>
37 #include <linux/workqueue.h>
38 #include <linux/crc32.h>
39 #include <linux/firmware.h>
41 #include <asm/atomic.h>
43 #define IN_CARD_SERVICES
44 #include <pcmcia/version.h>
45 #include <pcmcia/cs_types.h>
46 #include <pcmcia/cs.h>
47 #include <pcmcia/bulkmem.h>
48 #include <pcmcia/cistpl.h>
49 #include <pcmcia/ds.h>
50 #include <pcmcia/ss.h>
52 #include "cs_internal.h"
53 #include "ds_internal.h"
55 /*====================================================================*/
57 /* Module parameters */
59 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
60 MODULE_DESCRIPTION("PCMCIA Driver Services");
61 MODULE_LICENSE("GPL");
66 module_param_named(pc_debug, ds_pc_debug, int, 0644);
68 #define ds_dbg(lvl, fmt, arg...) do { \
69 if (ds_pc_debug > (lvl)) \
70 printk(KERN_DEBUG "ds: " fmt , ## arg); \
73 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
76 spinlock_t pcmcia_dev_list_lock;
78 static int unbind_request(struct pcmcia_bus_socket *s);
80 /*====================================================================*/
82 /* code which was in cs.c before */
84 /* String tables for error messages */
86 typedef struct lookup_t {
91 static const lookup_t error_table[] = {
92 { CS_SUCCESS, "Operation succeeded" },
93 { CS_BAD_ADAPTER, "Bad adapter" },
94 { CS_BAD_ATTRIBUTE, "Bad attribute", },
95 { CS_BAD_BASE, "Bad base address" },
96 { CS_BAD_EDC, "Bad EDC" },
97 { CS_BAD_IRQ, "Bad IRQ" },
98 { CS_BAD_OFFSET, "Bad offset" },
99 { CS_BAD_PAGE, "Bad page number" },
100 { CS_READ_FAILURE, "Read failure" },
101 { CS_BAD_SIZE, "Bad size" },
102 { CS_BAD_SOCKET, "Bad socket" },
103 { CS_BAD_TYPE, "Bad type" },
104 { CS_BAD_VCC, "Bad Vcc" },
105 { CS_BAD_VPP, "Bad Vpp" },
106 { CS_BAD_WINDOW, "Bad window" },
107 { CS_WRITE_FAILURE, "Write failure" },
108 { CS_NO_CARD, "No card present" },
109 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
110 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
111 { CS_BAD_SPEED, "Bad speed" },
112 { CS_BUSY, "Resource busy" },
113 { CS_GENERAL_FAILURE, "General failure" },
114 { CS_WRITE_PROTECTED, "Write protected" },
115 { CS_BAD_ARG_LENGTH, "Bad argument length" },
116 { CS_BAD_ARGS, "Bad arguments" },
117 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
118 { CS_IN_USE, "Resource in use" },
119 { CS_NO_MORE_ITEMS, "No more items" },
120 { CS_OUT_OF_RESOURCE, "Out of resource" },
121 { CS_BAD_HANDLE, "Bad handle" },
122 { CS_BAD_TUPLE, "Bad CIS tuple" }
126 static const lookup_t service_table[] = {
127 { AccessConfigurationRegister, "AccessConfigurationRegister" },
128 { AddSocketServices, "AddSocketServices" },
129 { AdjustResourceInfo, "AdjustResourceInfo" },
130 { CheckEraseQueue, "CheckEraseQueue" },
131 { CloseMemory, "CloseMemory" },
132 { DeregisterClient, "DeregisterClient" },
133 { DeregisterEraseQueue, "DeregisterEraseQueue" },
134 { GetCardServicesInfo, "GetCardServicesInfo" },
135 { GetClientInfo, "GetClientInfo" },
136 { GetConfigurationInfo, "GetConfigurationInfo" },
137 { GetEventMask, "GetEventMask" },
138 { GetFirstClient, "GetFirstClient" },
139 { GetFirstRegion, "GetFirstRegion" },
140 { GetFirstTuple, "GetFirstTuple" },
141 { GetNextClient, "GetNextClient" },
142 { GetNextRegion, "GetNextRegion" },
143 { GetNextTuple, "GetNextTuple" },
144 { GetStatus, "GetStatus" },
145 { GetTupleData, "GetTupleData" },
146 { MapMemPage, "MapMemPage" },
147 { ModifyConfiguration, "ModifyConfiguration" },
148 { ModifyWindow, "ModifyWindow" },
149 { OpenMemory, "OpenMemory" },
150 { ParseTuple, "ParseTuple" },
151 { ReadMemory, "ReadMemory" },
152 { RegisterClient, "RegisterClient" },
153 { RegisterEraseQueue, "RegisterEraseQueue" },
154 { RegisterMTD, "RegisterMTD" },
155 { ReleaseConfiguration, "ReleaseConfiguration" },
156 { ReleaseIO, "ReleaseIO" },
157 { ReleaseIRQ, "ReleaseIRQ" },
158 { ReleaseWindow, "ReleaseWindow" },
159 { RequestConfiguration, "RequestConfiguration" },
160 { RequestIO, "RequestIO" },
161 { RequestIRQ, "RequestIRQ" },
162 { RequestSocketMask, "RequestSocketMask" },
163 { RequestWindow, "RequestWindow" },
164 { ResetCard, "ResetCard" },
165 { SetEventMask, "SetEventMask" },
166 { ValidateCIS, "ValidateCIS" },
167 { WriteMemory, "WriteMemory" },
168 { BindDevice, "BindDevice" },
169 { BindMTD, "BindMTD" },
170 { ReportError, "ReportError" },
171 { SuspendCard, "SuspendCard" },
172 { ResumeCard, "ResumeCard" },
173 { EjectCard, "EjectCard" },
174 { InsertCard, "InsertCard" },
175 { ReplaceCIS, "ReplaceCIS" }
179 static int pcmcia_report_error(client_handle_t handle, error_info_t *err)
184 if (CHECK_HANDLE(handle))
187 struct pcmcia_device *p_dev = handle_to_pdev(handle);
188 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
191 for (i = 0; i < ARRAY_SIZE(service_table); i++)
192 if (service_table[i].key == err->func)
194 if (i < ARRAY_SIZE(service_table))
195 serv = service_table[i].msg;
197 serv = "Unknown service number";
199 for (i = 0; i < ARRAY_SIZE(error_table); i++)
200 if (error_table[i].key == err->retcode)
202 if (i < ARRAY_SIZE(error_table))
203 printk("%s: %s\n", serv, error_table[i].msg);
205 printk("%s: Unknown error code %#x\n", serv, err->retcode);
210 /* end of code which was in cs.c before */
212 /*======================================================================*/
214 void cs_error(client_handle_t handle, int func, int ret)
216 error_info_t err = { func, ret };
217 pcmcia_report_error(handle, &err);
219 EXPORT_SYMBOL(cs_error);
222 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
224 struct pcmcia_device_id *did = p_drv->id_table;
228 while (did && did->match_flags) {
229 for (i=0; i<4; i++) {
230 if (!did->prod_id[i])
233 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
234 if (hash == did->prod_id_hash[i])
237 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
238 "product string \"%s\": is 0x%x, should "
239 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
240 did->prod_id_hash[i], hash);
241 printk(KERN_DEBUG "pcmcia: see "
242 "Documentation/pcmcia/devicetable.txt for "
252 #ifdef CONFIG_PCMCIA_LOAD_CIS
255 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
256 * @dev - the pcmcia device which needs a CIS override
257 * @filename - requested filename in /lib/firmware/cis/
259 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
260 * the one provided by the card is broken. The firmware files reside in
261 * /lib/firmware/cis/ in userspace.
263 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
265 struct pcmcia_socket *s = dev->socket;
266 const struct firmware *fw;
274 ds_dbg(1, "trying to load firmware %s\n", filename);
276 if (strlen(filename) > 14)
279 snprintf(path, 20, "%s", filename);
281 if (request_firmware(&fw, path, &dev->dev) == 0) {
282 if (fw->size >= CISTPL_MAX_CIS_SIZE)
285 cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL);
289 memset(cis, 0, sizeof(cisdump_t));
291 cis->Length = fw->size + 1;
292 memcpy(cis->Data, fw->data, fw->size);
294 if (!pcmcia_replace_cis(s, cis))
298 release_firmware(fw);
303 #else /* !CONFIG_PCMCIA_LOAD_CIS */
305 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
313 /*======================================================================*/
316 void pcmcia_release_bus_socket(struct kref *refcount)
318 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
319 pcmcia_put_socket(s->parent);
323 void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
325 kref_put(&s->refcount, pcmcia_release_bus_socket);
328 struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
330 kref_get(&s->refcount);
335 * pcmcia_register_driver - register a PCMCIA driver with the bus core
337 * Registers a PCMCIA driver with the PCMCIA bus core.
339 static int pcmcia_device_probe(struct device *dev);
340 static int pcmcia_device_remove(struct device * dev);
342 int pcmcia_register_driver(struct pcmcia_driver *driver)
347 pcmcia_check_driver(driver);
349 /* initialize common fields */
350 driver->drv.bus = &pcmcia_bus_type;
351 driver->drv.owner = driver->owner;
352 driver->drv.probe = pcmcia_device_probe;
353 driver->drv.remove = pcmcia_device_remove;
355 return driver_register(&driver->drv);
357 EXPORT_SYMBOL(pcmcia_register_driver);
360 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
362 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
364 driver_unregister(&driver->drv);
366 EXPORT_SYMBOL(pcmcia_unregister_driver);
369 /* pcmcia_device handling */
371 struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
373 struct device *tmp_dev;
374 tmp_dev = get_device(&p_dev->dev);
377 return to_pcmcia_dev(tmp_dev);
380 void pcmcia_put_dev(struct pcmcia_device *p_dev)
383 put_device(&p_dev->dev);
386 static void pcmcia_release_dev(struct device *dev)
388 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
389 ds_dbg(1, "releasing dev %p\n", p_dev);
390 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
395 static int pcmcia_device_probe(struct device * dev)
397 struct pcmcia_device *p_dev;
398 struct pcmcia_driver *p_drv;
401 dev = get_device(dev);
405 p_dev = to_pcmcia_dev(dev);
406 p_drv = to_pcmcia_drv(dev->driver);
408 if (!try_module_get(p_drv->owner)) {
414 p_dev->instance = p_drv->attach();
415 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
416 printk(KERN_NOTICE "ds: unable to create instance "
417 "of '%s'!\n", p_drv->drv.name);
423 module_put(p_drv->owner);
425 if ((ret) || !(p_drv->attach))
431 static int pcmcia_device_remove(struct device * dev)
433 struct pcmcia_device *p_dev;
434 struct pcmcia_driver *p_drv;
436 /* detach the "instance" */
437 p_dev = to_pcmcia_dev(dev);
438 p_drv = to_pcmcia_drv(dev->driver);
441 if ((p_drv->detach) && (p_dev->instance)) {
442 p_drv->detach(p_dev->instance);
443 /* from pcmcia_probe_device */
444 put_device(&p_dev->dev);
446 module_put(p_drv->owner);
455 * pcmcia_device_query -- determine information about a pcmcia device
457 static int pcmcia_device_query(struct pcmcia_device *p_dev)
459 cistpl_manfid_t manf_id;
460 cistpl_funcid_t func_id;
461 cistpl_vers_1_t vers1;
464 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
465 CISTPL_MANFID, &manf_id)) {
466 p_dev->manf_id = manf_id.manf;
467 p_dev->card_id = manf_id.card;
468 p_dev->has_manf_id = 1;
469 p_dev->has_card_id = 1;
472 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
473 CISTPL_FUNCID, &func_id)) {
474 p_dev->func_id = func_id.func;
475 p_dev->has_func_id = 1;
477 /* rule of thumb: cards with no FUNCID, but with
478 * common memory device geometry information, are
479 * probably memory cards (from pcmcia-cs) */
480 cistpl_device_geo_t devgeo;
481 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
482 CISTPL_DEVICE_GEO, &devgeo)) {
483 ds_dbg(0, "mem device geometry probably means "
485 p_dev->func_id = CISTPL_FUNCID_MEMORY;
486 p_dev->has_func_id = 1;
490 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
492 for (i=0; i < vers1.ns; i++) {
496 tmp = vers1.str + vers1.ofs[i];
498 length = strlen(tmp) + 1;
499 if ((length < 3) || (length > 255))
502 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
504 if (!p_dev->prod_id[i])
507 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
516 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
517 * Serializes pcmcia_device_add; will most likely be removed in future.
519 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
520 * won't work, this doesn't matter much at the moment: the driver core doesn't
523 static DECLARE_MUTEX(device_add_lock);
525 struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
527 struct pcmcia_device *p_dev;
530 s = pcmcia_get_bus_socket(s);
534 down(&device_add_lock);
536 /* max of 2 devices per card */
537 if (s->device_count == 2)
540 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
543 memset(p_dev, 0, sizeof(struct pcmcia_device));
545 p_dev->socket = s->parent;
546 p_dev->device_no = (s->device_count++);
547 p_dev->func = function;
549 p_dev->dev.bus = &pcmcia_bus_type;
550 p_dev->dev.parent = s->parent->dev.dev;
551 p_dev->dev.release = pcmcia_release_dev;
552 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
555 p_dev->client.client_magic = CLIENT_MAGIC;
556 p_dev->client.Socket = s->parent;
557 p_dev->client.Function = function;
558 p_dev->client.state = CLIENT_UNBOUND;
560 /* Add to the list in pcmcia_bus_socket */
561 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
562 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
563 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
565 pcmcia_device_query(p_dev);
567 if (device_register(&p_dev->dev)) {
568 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
569 list_del(&p_dev->socket_device_list);
570 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
575 up(&device_add_lock);
583 up(&device_add_lock);
584 pcmcia_put_bus_socket(s);
590 static int pcmcia_card_add(struct pcmcia_socket *s)
593 cistpl_longlink_mfc_t mfc;
594 unsigned int no_funcs, i;
597 if (!(s->resource_setup_done))
598 return -EAGAIN; /* try again, but later... */
600 pcmcia_validate_mem(s);
601 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
602 if (ret || !cisinfo.Chains) {
603 ds_dbg(0, "invalid CIS or invalid resources\n");
607 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
612 /* this doesn't handle multifunction devices on one pcmcia function
614 for (i=0; i < no_funcs; i++)
615 pcmcia_device_add(s->pcmcia, i);
621 static void pcmcia_delayed_add_pseudo_device(void *data)
623 struct pcmcia_bus_socket *s = data;
624 pcmcia_device_add(s, 0);
625 s->pcmcia_state.device_add_pending = 0;
628 static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s)
630 if (!s->pcmcia_state.device_add_pending) {
631 schedule_work(&s->device_add);
632 s->pcmcia_state.device_add_pending = 1;
637 static int pcmcia_requery(struct device *dev, void * _data)
639 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
640 if (!p_dev->dev.driver)
641 pcmcia_device_query(p_dev);
646 static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
651 /* must be called with skt_sem held */
652 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
653 if (list_empty(&skt->pcmcia->devices_list))
655 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
657 /* if no devices were added for this socket yet because of
658 * missing resource information or other trouble, we need to
661 int ret = pcmcia_card_add(skt);
666 /* some device information might have changed because of a CIS
667 * update or because we can finally read it correctly... so
668 * determine it again, overwriting old values if necessary. */
669 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
671 /* we re-scan all devices, not just the ones connected to this
672 * socket. This does not matter, though. */
673 bus_rescan_devices(&pcmcia_bus_type);
676 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
677 struct pcmcia_device_id *did)
679 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
680 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
684 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
685 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
689 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
690 if (dev->func != did->function)
694 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
695 if (!dev->prod_id[0])
697 if (strcmp(did->prod_id[0], dev->prod_id[0]))
701 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
702 if (!dev->prod_id[1])
704 if (strcmp(did->prod_id[1], dev->prod_id[1]))
708 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
709 if (!dev->prod_id[2])
711 if (strcmp(did->prod_id[2], dev->prod_id[2]))
715 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
716 if (!dev->prod_id[3])
718 if (strcmp(did->prod_id[3], dev->prod_id[3]))
722 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
723 /* handle pseudo multifunction devices:
724 * there are at most two pseudo multifunction devices.
725 * if we're matching against the first, schedule a
726 * call which will then check whether there are two
727 * pseudo devices, and if not, add the second one.
729 if (dev->device_no == 0)
730 pcmcia_add_pseudo_device(dev->socket->pcmcia);
732 if (dev->device_no != did->device_no)
736 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
737 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
740 /* if this is a pseudo-multi-function device,
741 * we need explicit matches */
742 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
747 /* also, FUNC_ID matching needs to be activated by userspace
748 * after it has re-checked that there is no possible module
749 * with a prod_id/manf_id/card_id match.
751 if (!dev->allow_func_id_match)
755 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
756 if (!dev->socket->fake_cis)
757 pcmcia_load_firmware(dev, did->cisfile);
759 if (!dev->socket->fake_cis)
763 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
768 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
772 dev->dev.driver_data = (void *) did;
778 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
779 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
780 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
781 struct pcmcia_device_id *did = p_drv->id_table;
783 /* matching by cardmgr */
784 if (p_dev->cardmgr == p_drv)
787 while (did && did->match_flags) {
788 if (pcmcia_devmatch(p_dev, did))
796 #ifdef CONFIG_HOTPLUG
798 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
799 char *buffer, int buffer_size)
801 struct pcmcia_device *p_dev;
803 u32 hash[4] = { 0, 0, 0, 0};
808 p_dev = to_pcmcia_dev(dev);
810 /* calculate hashes */
811 for (i=0; i<4; i++) {
812 if (!p_dev->prod_id[i])
814 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
819 if (add_hotplug_env_var(envp, num_envp, &i,
820 buffer, buffer_size, &length,
822 p_dev->socket->sock))
825 if (add_hotplug_env_var(envp, num_envp, &i,
826 buffer, buffer_size, &length,
831 if (add_hotplug_env_var(envp, num_envp, &i,
832 buffer, buffer_size, &length,
833 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
834 "pa%08Xpb%08Xpc%08Xpd%08X",
835 p_dev->has_manf_id ? p_dev->manf_id : 0,
836 p_dev->has_card_id ? p_dev->card_id : 0,
837 p_dev->has_func_id ? p_dev->func_id : 0,
853 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
854 char *buffer, int buffer_size)
861 /************************ per-device sysfs output ***************************/
863 #define pcmcia_device_attr(field, test, format) \
864 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
866 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
867 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
870 #define pcmcia_device_stringattr(name, field) \
871 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
873 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
874 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
877 pcmcia_device_attr(func, socket, "0x%02x\n");
878 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
879 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
880 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
881 pcmcia_device_stringattr(prod_id1, prod_id[0]);
882 pcmcia_device_stringattr(prod_id2, prod_id[1]);
883 pcmcia_device_stringattr(prod_id3, prod_id[2]);
884 pcmcia_device_stringattr(prod_id4, prod_id[3]);
887 static ssize_t pcmcia_store_allow_func_id_match (struct device * dev, struct device_attribute *attr,
888 const char * buf, size_t count)
890 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
894 down(&p_dev->socket->skt_sem);
895 p_dev->allow_func_id_match = 1;
896 up(&p_dev->socket->skt_sem);
898 bus_rescan_devices(&pcmcia_bus_type);
903 static struct device_attribute pcmcia_dev_attrs[] = {
904 __ATTR(function, 0444, func_show, NULL),
912 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
917 /*======================================================================
919 The card status event handler.
921 ======================================================================*/
923 struct send_event_data {
924 struct pcmcia_socket *skt;
929 static int send_event_callback(struct device *dev, void * _data)
931 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
932 struct send_event_data *data = _data;
934 /* we get called for all sockets, but may only pass the event
935 * for drivers _on the affected socket_ */
936 if (p_dev->socket != data->skt)
939 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
942 if (p_dev->client.EventMask & data->event)
943 return EVENT(&p_dev->client, data->event, data->priority);
948 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
951 struct send_event_data private;
952 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
958 private.event = event;
959 private.priority = priority;
961 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
963 pcmcia_put_bus_socket(skt);
968 /* Normally, the event is passed to individual drivers after
969 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
970 * is inversed to maintain historic compatibility.
973 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
975 struct pcmcia_bus_socket *s = skt->pcmcia;
978 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
983 case CS_EVENT_CARD_REMOVAL:
984 s->pcmcia_state.present = 0;
985 send_event(skt, event, priority);
987 handle_event(s, event);
990 case CS_EVENT_CARD_INSERTION:
991 s->pcmcia_state.present = 1;
992 pcmcia_card_add(skt);
993 handle_event(s, event);
996 case CS_EVENT_EJECTION_REQUEST:
997 ret = send_event(skt, event, priority);
1001 handle_event(s, event);
1002 send_event(skt, event, priority);
1011 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1013 client_t *client = NULL;
1014 struct pcmcia_socket *s;
1015 struct pcmcia_bus_socket *skt = NULL;
1016 struct pcmcia_device *p_dev = NULL;
1018 /* Look for unbound client with matching dev_info */
1019 down_read(&pcmcia_socket_list_rwsem);
1020 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1021 unsigned long flags;
1023 if (s->state & SOCKET_CARDBUS)
1029 skt = pcmcia_get_bus_socket(skt);
1032 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1033 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
1034 struct pcmcia_driver *p_drv;
1035 p_dev = pcmcia_get_dev(p_dev);
1038 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
1039 (!p_dev->dev.driver)) {
1040 pcmcia_put_dev(p_dev);
1043 p_drv = to_pcmcia_drv(p_dev->dev.driver);
1044 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1045 client = &p_dev->client;
1046 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1049 pcmcia_put_dev(p_dev);
1051 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1052 pcmcia_put_bus_socket(skt);
1055 up_read(&pcmcia_socket_list_rwsem);
1056 if (!p_dev || !client)
1059 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
1062 client->state &= ~CLIENT_UNBOUND;
1064 client->EventMask = req->EventMask;
1065 client->event_handler = req->event_handler;
1066 client->event_callback_args = req->event_callback_args;
1067 client->event_callback_args.client_handle = client;
1069 if (s->state & SOCKET_CARDBUS)
1070 client->state |= CLIENT_CARDBUS;
1072 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
1073 (client->Function != BIND_FN_ALL)) {
1074 cistpl_longlink_mfc_t mfc;
1075 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
1077 s->functions = mfc.nfn;
1080 s->config = kmalloc(sizeof(config_t) * s->functions,
1083 goto out_no_resource;
1084 memset(s->config, 0, sizeof(config_t) * s->functions);
1087 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1088 client, p_dev->dev.bus_id);
1089 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
1090 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
1092 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1093 if (client->EventMask & CS_EVENT_CARD_INSERTION)
1094 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1100 pcmcia_put_dev(p_dev);
1101 return CS_OUT_OF_RESOURCE;
1102 } /* register_client */
1103 EXPORT_SYMBOL(pcmcia_register_client);
1106 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1107 * drivers have been called with EVENT_CARD_REMOVAL before.
1109 static int unbind_request(struct pcmcia_bus_socket *s)
1111 struct pcmcia_device *p_dev;
1112 unsigned long flags;
1114 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1116 s->device_count = 0;
1119 /* unregister all pcmcia_devices registered with this socket*/
1120 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1121 if (list_empty(&s->devices_list)) {
1122 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1125 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1126 list_del(&p_dev->socket_device_list);
1127 p_dev->client.state |= CLIENT_STALE;
1128 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1130 device_unregister(&p_dev->dev);
1134 } /* unbind_request */
1136 int pcmcia_deregister_client(client_handle_t handle)
1138 struct pcmcia_socket *s;
1140 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1142 if (CHECK_HANDLE(handle))
1143 return CS_BAD_HANDLE;
1146 ds_dbg(1, "deregister_client(%p)\n", handle);
1148 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1150 for (i = 0; i < MAX_WIN; i++)
1151 if (handle->state & CLIENT_WIN_REQ(i))
1154 if (handle->state & CLIENT_STALE) {
1155 handle->client_magic = 0;
1156 handle->state &= ~CLIENT_STALE;
1157 pcmcia_put_dev(p_dev);
1159 handle->state = CLIENT_UNBOUND;
1160 handle->event_handler = NULL;
1165 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1167 } /* deregister_client */
1168 EXPORT_SYMBOL(pcmcia_deregister_client);
1170 static struct pcmcia_callback pcmcia_bus_callback = {
1171 .owner = THIS_MODULE,
1173 .requery = pcmcia_bus_rescan,
1176 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1178 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1179 struct pcmcia_bus_socket *s;
1182 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1185 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1187 /* get reference to parent socket */
1188 s->parent = pcmcia_get_socket(socket);
1190 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1195 kref_init(&s->refcount);
1198 * Ugly. But we want to wait for the socket threads to have started up.
1199 * We really should let the drivers themselves drive some of this..
1203 #ifdef CONFIG_PCMCIA_IOCTL
1204 init_waitqueue_head(&s->queue);
1206 INIT_LIST_HEAD(&s->devices_list);
1207 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
1209 /* Set up hotline to Card Services */
1212 ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
1214 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1215 pcmcia_put_bus_socket(s);
1216 socket->pcmcia = NULL;
1223 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1225 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1227 if (!socket || !socket->pcmcia)
1230 pccard_register_pcmcia(socket, NULL);
1232 socket->pcmcia->pcmcia_state.dead = 1;
1233 pcmcia_put_bus_socket(socket->pcmcia);
1234 socket->pcmcia = NULL;
1240 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1241 static struct class_interface pcmcia_bus_interface = {
1242 .class = &pcmcia_socket_class,
1243 .add = &pcmcia_bus_add_socket,
1244 .remove = &pcmcia_bus_remove_socket,
1248 struct bus_type pcmcia_bus_type = {
1250 .hotplug = pcmcia_bus_hotplug,
1251 .match = pcmcia_bus_match,
1252 .dev_attrs = pcmcia_dev_attrs,
1256 static int __init init_pcmcia_bus(void)
1258 spin_lock_init(&pcmcia_dev_list_lock);
1260 bus_register(&pcmcia_bus_type);
1261 class_interface_register(&pcmcia_bus_interface);
1263 pcmcia_setup_ioctl();
1267 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1268 * pcmcia_socket_class is already registered */
1271 static void __exit exit_pcmcia_bus(void)
1273 pcmcia_cleanup_ioctl();
1275 class_interface_unregister(&pcmcia_bus_interface);
1277 bus_unregister(&pcmcia_bus_type);
1279 module_exit(exit_pcmcia_bus);