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>
40 #include <asm/atomic.h>
42 #define IN_CARD_SERVICES
43 #include <pcmcia/version.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/bulkmem.h>
47 #include <pcmcia/cistpl.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/ss.h>
51 #include "cs_internal.h"
53 /*====================================================================*/
55 /* Module parameters */
57 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
58 MODULE_DESCRIPTION("PCMCIA Driver Services");
59 MODULE_LICENSE("GPL");
64 module_param_named(pc_debug, ds_pc_debug, int, 0644);
66 #define ds_dbg(lvl, fmt, arg...) do { \
67 if (ds_pc_debug > (lvl)) \
68 printk(KERN_DEBUG "ds: " fmt , ## arg); \
71 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
74 /*====================================================================*/
76 /* Device user information */
78 #define USER_MAGIC 0x7ea4
79 #define CHECK_USER(u) \
80 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
81 typedef struct user_info_t {
83 int event_head, event_tail;
84 event_t event[MAX_EVENTS];
85 struct user_info_t *next;
86 struct pcmcia_bus_socket *socket;
89 /* Socket state information */
90 struct pcmcia_bus_socket {
92 struct pcmcia_callback callback;
95 wait_queue_head_t queue;
96 struct pcmcia_socket *parent;
98 /* the PCMCIA devices connected to this socket (normally one, more
99 * for multifunction devices: */
100 struct list_head devices_list;
101 u8 device_count; /* the number of devices, used
102 * only internally and subject
103 * to incorrectness and change */
105 u8 device_add_pending;
106 struct work_struct device_add;
108 static spinlock_t pcmcia_dev_list_lock;
110 #define DS_SOCKET_PRESENT 0x01
111 #define DS_SOCKET_BUSY 0x02
112 #define DS_SOCKET_REMOVAL_PENDING 0x10
113 #define DS_SOCKET_DEAD 0x80
115 /*====================================================================*/
117 static int major_dev = -1;
119 static int unbind_request(struct pcmcia_bus_socket *s);
121 /*====================================================================*/
123 /* code which was in cs.c before */
125 /* String tables for error messages */
127 typedef struct lookup_t {
132 static const lookup_t error_table[] = {
133 { CS_SUCCESS, "Operation succeeded" },
134 { CS_BAD_ADAPTER, "Bad adapter" },
135 { CS_BAD_ATTRIBUTE, "Bad attribute", },
136 { CS_BAD_BASE, "Bad base address" },
137 { CS_BAD_EDC, "Bad EDC" },
138 { CS_BAD_IRQ, "Bad IRQ" },
139 { CS_BAD_OFFSET, "Bad offset" },
140 { CS_BAD_PAGE, "Bad page number" },
141 { CS_READ_FAILURE, "Read failure" },
142 { CS_BAD_SIZE, "Bad size" },
143 { CS_BAD_SOCKET, "Bad socket" },
144 { CS_BAD_TYPE, "Bad type" },
145 { CS_BAD_VCC, "Bad Vcc" },
146 { CS_BAD_VPP, "Bad Vpp" },
147 { CS_BAD_WINDOW, "Bad window" },
148 { CS_WRITE_FAILURE, "Write failure" },
149 { CS_NO_CARD, "No card present" },
150 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
151 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
152 { CS_BAD_SPEED, "Bad speed" },
153 { CS_BUSY, "Resource busy" },
154 { CS_GENERAL_FAILURE, "General failure" },
155 { CS_WRITE_PROTECTED, "Write protected" },
156 { CS_BAD_ARG_LENGTH, "Bad argument length" },
157 { CS_BAD_ARGS, "Bad arguments" },
158 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
159 { CS_IN_USE, "Resource in use" },
160 { CS_NO_MORE_ITEMS, "No more items" },
161 { CS_OUT_OF_RESOURCE, "Out of resource" },
162 { CS_BAD_HANDLE, "Bad handle" },
163 { CS_BAD_TUPLE, "Bad CIS tuple" }
167 static const lookup_t service_table[] = {
168 { AccessConfigurationRegister, "AccessConfigurationRegister" },
169 { AddSocketServices, "AddSocketServices" },
170 { AdjustResourceInfo, "AdjustResourceInfo" },
171 { CheckEraseQueue, "CheckEraseQueue" },
172 { CloseMemory, "CloseMemory" },
173 { DeregisterClient, "DeregisterClient" },
174 { DeregisterEraseQueue, "DeregisterEraseQueue" },
175 { GetCardServicesInfo, "GetCardServicesInfo" },
176 { GetClientInfo, "GetClientInfo" },
177 { GetConfigurationInfo, "GetConfigurationInfo" },
178 { GetEventMask, "GetEventMask" },
179 { GetFirstClient, "GetFirstClient" },
180 { GetFirstRegion, "GetFirstRegion" },
181 { GetFirstTuple, "GetFirstTuple" },
182 { GetNextClient, "GetNextClient" },
183 { GetNextRegion, "GetNextRegion" },
184 { GetNextTuple, "GetNextTuple" },
185 { GetStatus, "GetStatus" },
186 { GetTupleData, "GetTupleData" },
187 { MapMemPage, "MapMemPage" },
188 { ModifyConfiguration, "ModifyConfiguration" },
189 { ModifyWindow, "ModifyWindow" },
190 { OpenMemory, "OpenMemory" },
191 { ParseTuple, "ParseTuple" },
192 { ReadMemory, "ReadMemory" },
193 { RegisterClient, "RegisterClient" },
194 { RegisterEraseQueue, "RegisterEraseQueue" },
195 { RegisterMTD, "RegisterMTD" },
196 { ReleaseConfiguration, "ReleaseConfiguration" },
197 { ReleaseIO, "ReleaseIO" },
198 { ReleaseIRQ, "ReleaseIRQ" },
199 { ReleaseWindow, "ReleaseWindow" },
200 { RequestConfiguration, "RequestConfiguration" },
201 { RequestIO, "RequestIO" },
202 { RequestIRQ, "RequestIRQ" },
203 { RequestSocketMask, "RequestSocketMask" },
204 { RequestWindow, "RequestWindow" },
205 { ResetCard, "ResetCard" },
206 { SetEventMask, "SetEventMask" },
207 { ValidateCIS, "ValidateCIS" },
208 { WriteMemory, "WriteMemory" },
209 { BindDevice, "BindDevice" },
210 { BindMTD, "BindMTD" },
211 { ReportError, "ReportError" },
212 { SuspendCard, "SuspendCard" },
213 { ResumeCard, "ResumeCard" },
214 { EjectCard, "EjectCard" },
215 { InsertCard, "InsertCard" },
216 { ReplaceCIS, "ReplaceCIS" }
220 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
225 if (CHECK_HANDLE(handle))
228 struct pcmcia_device *p_dev = handle_to_pdev(handle);
229 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
232 for (i = 0; i < ARRAY_SIZE(service_table); i++)
233 if (service_table[i].key == err->func)
235 if (i < ARRAY_SIZE(service_table))
236 serv = service_table[i].msg;
238 serv = "Unknown service number";
240 for (i = 0; i < ARRAY_SIZE(error_table); i++)
241 if (error_table[i].key == err->retcode)
243 if (i < ARRAY_SIZE(error_table))
244 printk("%s: %s\n", serv, error_table[i].msg);
246 printk("%s: Unknown error code %#x\n", serv, err->retcode);
250 EXPORT_SYMBOL(pcmcia_report_error);
252 /* end of code which was in cs.c before */
254 /*======================================================================*/
256 void cs_error(client_handle_t handle, int func, int ret)
258 error_info_t err = { func, ret };
259 pcmcia_report_error(handle, &err);
261 EXPORT_SYMBOL(cs_error);
263 #ifdef CONFIG_PCMCIA_DEBUG
266 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
268 struct pcmcia_device_id *did = p_drv->id_table;
272 while (did && did->match_flags) {
273 for (i=0; i<4; i++) {
274 if (!did->prod_id[i])
277 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
278 if (hash == did->prod_id_hash[i])
281 printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
282 "product string \"%s\": is 0x%x, should "
283 "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
284 did->prod_id_hash[i], hash);
293 static inline void pcmcia_check_driver(struct pcmcia_driver *p_drv) {
298 /*======================================================================*/
300 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
301 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
303 static void pcmcia_release_bus_socket(struct kref *refcount)
305 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
306 pcmcia_put_socket(s->parent);
310 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
312 kref_put(&s->refcount, pcmcia_release_bus_socket);
315 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
317 kref_get(&s->refcount);
322 * pcmcia_register_driver - register a PCMCIA driver with the bus core
324 * Registers a PCMCIA driver with the PCMCIA bus core.
326 static int pcmcia_device_probe(struct device *dev);
327 static int pcmcia_device_remove(struct device * dev);
329 int pcmcia_register_driver(struct pcmcia_driver *driver)
334 pcmcia_check_driver(driver);
336 /* initialize common fields */
337 driver->drv.bus = &pcmcia_bus_type;
338 driver->drv.owner = driver->owner;
339 driver->drv.probe = pcmcia_device_probe;
340 driver->drv.remove = pcmcia_device_remove;
342 return driver_register(&driver->drv);
344 EXPORT_SYMBOL(pcmcia_register_driver);
347 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
349 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
351 driver_unregister(&driver->drv);
353 EXPORT_SYMBOL(pcmcia_unregister_driver);
355 #ifdef CONFIG_PROC_FS
356 static struct proc_dir_entry *proc_pccard = NULL;
358 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
361 struct pcmcia_driver *p_drv = container_of(driver,
362 struct pcmcia_driver, drv);
364 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
365 #ifdef CONFIG_MODULE_UNLOAD
366 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
376 static int proc_read_drivers(char *buf, char **start, off_t pos,
377 int count, int *eof, void *data)
381 bus_for_each_drv(&pcmcia_bus_type, NULL,
382 (void *) &p, proc_read_drivers_callback);
388 /* pcmcia_device handling */
390 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
392 struct device *tmp_dev;
393 tmp_dev = get_device(&p_dev->dev);
396 return to_pcmcia_dev(tmp_dev);
399 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
402 put_device(&p_dev->dev);
405 static void pcmcia_release_dev(struct device *dev)
407 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
408 ds_dbg(1, "releasing dev %p\n", p_dev);
409 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
414 static int pcmcia_device_probe(struct device * dev)
416 struct pcmcia_device *p_dev;
417 struct pcmcia_driver *p_drv;
420 dev = get_device(dev);
424 p_dev = to_pcmcia_dev(dev);
425 p_drv = to_pcmcia_drv(dev->driver);
427 if (!try_module_get(p_drv->owner)) {
433 p_dev->instance = p_drv->attach();
434 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
435 printk(KERN_NOTICE "ds: unable to create instance "
436 "of '%s'!\n", p_drv->drv.name);
442 module_put(p_drv->owner);
444 if ((ret) || !(p_drv->attach))
450 static int pcmcia_device_remove(struct device * dev)
452 struct pcmcia_device *p_dev;
453 struct pcmcia_driver *p_drv;
455 /* detach the "instance" */
456 p_dev = to_pcmcia_dev(dev);
457 p_drv = to_pcmcia_drv(dev->driver);
460 if ((p_drv->detach) && (p_dev->instance)) {
461 p_drv->detach(p_dev->instance);
462 /* from pcmcia_probe_device */
463 put_device(&p_dev->dev);
465 module_put(p_drv->owner);
474 * pcmcia_device_query -- determine information about a pcmcia device
476 static int pcmcia_device_query(struct pcmcia_device *p_dev)
478 cistpl_manfid_t manf_id;
479 cistpl_funcid_t func_id;
480 cistpl_vers_1_t vers1;
483 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
484 CISTPL_MANFID, &manf_id)) {
485 p_dev->manf_id = manf_id.manf;
486 p_dev->card_id = manf_id.card;
487 p_dev->has_manf_id = 1;
488 p_dev->has_card_id = 1;
491 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
492 CISTPL_FUNCID, &func_id)) {
493 p_dev->func_id = func_id.func;
494 p_dev->has_func_id = 1;
496 /* rule of thumb: cards with no FUNCID, but with
497 * common memory device geometry information, are
498 * probably memory cards (from pcmcia-cs) */
499 cistpl_device_geo_t devgeo;
500 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
501 CISTPL_DEVICE_GEO, &devgeo)) {
502 ds_dbg(0, "mem device geometry probably means "
504 p_dev->func_id = CISTPL_FUNCID_MEMORY;
505 p_dev->has_func_id = 1;
509 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
511 for (i=0; i < vers1.ns; i++) {
515 tmp = vers1.str + vers1.ofs[i];
517 length = strlen(tmp) + 1;
518 if ((length < 3) || (length > 255))
521 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
523 if (!p_dev->prod_id[i])
526 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
535 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
536 * Serializes pcmcia_device_add; will most likely be removed in future.
538 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
539 * won't work, this doesn't matter much at the moment: the driver core doesn't
542 static DECLARE_MUTEX(device_add_lock);
544 static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
546 struct pcmcia_device *p_dev;
549 s = pcmcia_get_bus_socket(s);
553 down(&device_add_lock);
555 /* max of 2 devices per card */
556 if (s->device_count == 2)
559 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
562 memset(p_dev, 0, sizeof(struct pcmcia_device));
564 p_dev->socket = s->parent;
565 p_dev->device_no = (s->device_count++);
566 p_dev->func = function;
568 p_dev->dev.bus = &pcmcia_bus_type;
569 p_dev->dev.parent = s->parent->dev.dev;
570 p_dev->dev.release = pcmcia_release_dev;
571 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
574 p_dev->client.client_magic = CLIENT_MAGIC;
575 p_dev->client.Socket = s->parent;
576 p_dev->client.Function = function;
577 p_dev->client.state = CLIENT_UNBOUND;
579 /* Add to the list in pcmcia_bus_socket */
580 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
581 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
582 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
584 pcmcia_device_query(p_dev);
586 if (device_register(&p_dev->dev)) {
587 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
588 list_del(&p_dev->socket_device_list);
589 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
594 up(&device_add_lock);
602 up(&device_add_lock);
603 pcmcia_put_bus_socket(s);
609 static int pcmcia_card_add(struct pcmcia_socket *s)
612 cistpl_longlink_mfc_t mfc;
613 unsigned int no_funcs, i;
616 if (!(s->resource_setup_done))
617 return -EAGAIN; /* try again, but later... */
619 pcmcia_validate_mem(s);
620 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
621 if (ret || !cisinfo.Chains) {
622 ds_dbg(0, "invalid CIS or invalid resources\n");
626 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
631 /* this doesn't handle multifunction devices on one pcmcia function
633 for (i=0; i < no_funcs; i++)
634 pcmcia_device_add(s->pcmcia, i);
640 static void pcmcia_delayed_add_pseudo_device(void *data)
642 struct pcmcia_bus_socket *s = data;
643 pcmcia_device_add(s, 0);
644 s->device_add_pending = 0;
647 static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s)
649 if (!s->device_add_pending) {
650 schedule_work(&s->device_add);
651 s->device_add_pending = 1;
656 static void pcmcia_bus_rescan(void)
658 /* must be called with skt_sem held */
659 bus_rescan_devices(&pcmcia_bus_type);
662 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
663 struct pcmcia_device_id *did)
665 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
666 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
670 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
671 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
675 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
676 if (dev->func != did->function)
680 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
681 if (!dev->prod_id[0])
683 if (strcmp(did->prod_id[0], dev->prod_id[0]))
687 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
688 if (!dev->prod_id[1])
690 if (strcmp(did->prod_id[1], dev->prod_id[1]))
694 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
695 if (!dev->prod_id[2])
697 if (strcmp(did->prod_id[2], dev->prod_id[2]))
701 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
702 if (!dev->prod_id[3])
704 if (strcmp(did->prod_id[3], dev->prod_id[3]))
708 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
709 /* handle pseudo multifunction devices:
710 * there are at most two pseudo multifunction devices.
711 * if we're matching against the first, schedule a
712 * call which will then check whether there are two
713 * pseudo devices, and if not, add the second one.
715 if (dev->device_no == 0)
716 pcmcia_add_pseudo_device(dev->socket->pcmcia);
718 if (dev->device_no != did->device_no)
722 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
723 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
726 /* if this is a pseudo-multi-function device,
727 * we need explicit matches */
728 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
733 /* also, FUNC_ID matching needs to be activated by userspace
734 * after it has re-checked that there is no possible module
735 * with a prod_id/manf_id/card_id match.
737 if (!dev->allow_func_id_match)
741 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
742 if (!dev->socket->fake_cis) {
743 /* FIXME: evaluate using firmware helpers to
744 * automagically load it from userspace */
749 dev->dev.driver_data = (void *) did;
755 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
756 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
757 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
758 struct pcmcia_device_id *did = p_drv->id_table;
760 /* matching by cardmgr */
761 if (p_dev->cardmgr == p_drv)
764 while (did && did->match_flags) {
765 if (pcmcia_devmatch(p_dev, did))
773 #ifdef CONFIG_HOTPLUG
775 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
776 char *buffer, int buffer_size)
778 struct pcmcia_device *p_dev;
780 u32 hash[4] = { 0, 0, 0, 0};
785 p_dev = to_pcmcia_dev(dev);
787 /* calculate hashes */
788 for (i=0; i<4; i++) {
789 if (!p_dev->prod_id[i])
791 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
796 if (add_hotplug_env_var(envp, num_envp, &i,
797 buffer, buffer_size, &length,
799 p_dev->socket->sock))
802 if (add_hotplug_env_var(envp, num_envp, &i,
803 buffer, buffer_size, &length,
808 if (add_hotplug_env_var(envp, num_envp, &i,
809 buffer, buffer_size, &length,
810 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
811 "pa%08Xpb%08Xpc%08Xpd%08X",
812 p_dev->has_manf_id ? p_dev->manf_id : 0,
813 p_dev->has_card_id ? p_dev->card_id : 0,
814 p_dev->has_func_id ? p_dev->func_id : 0,
830 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
831 char *buffer, int buffer_size)
838 /************************ per-device sysfs output ***************************/
840 #define pcmcia_device_attr(field, test, format) \
841 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
843 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
844 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
847 #define pcmcia_device_stringattr(name, field) \
848 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
850 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
851 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
854 pcmcia_device_attr(func, socket, "0x%02x\n");
855 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
856 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
857 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
858 pcmcia_device_stringattr(prod_id1, prod_id[0]);
859 pcmcia_device_stringattr(prod_id2, prod_id[1]);
860 pcmcia_device_stringattr(prod_id3, prod_id[2]);
861 pcmcia_device_stringattr(prod_id4, prod_id[3]);
863 static struct device_attribute pcmcia_dev_attrs[] = {
864 __ATTR(function, 0444, func_show, NULL),
876 /*======================================================================
878 These manage a ring buffer of events pending for one user process
880 ======================================================================*/
882 static int queue_empty(user_info_t *user)
884 return (user->event_head == user->event_tail);
887 static event_t get_queued_event(user_info_t *user)
889 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
890 return user->event[user->event_tail];
893 static void queue_event(user_info_t *user, event_t event)
895 user->event_head = (user->event_head+1) % MAX_EVENTS;
896 if (user->event_head == user->event_tail)
897 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
898 user->event[user->event_head] = event;
901 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
904 for (user = s->user; user; user = user->next)
905 queue_event(user, event);
906 wake_up_interruptible(&s->queue);
910 /*======================================================================
912 The card status event handler.
914 ======================================================================*/
916 struct send_event_data {
917 struct pcmcia_socket *skt;
922 static int send_event_callback(struct device *dev, void * _data)
924 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
925 struct send_event_data *data = _data;
927 /* we get called for all sockets, but may only pass the event
928 * for drivers _on the affected socket_ */
929 if (p_dev->socket != data->skt)
932 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
935 if (p_dev->client.EventMask & data->event)
936 return EVENT(&p_dev->client, data->event, data->priority);
941 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
944 struct send_event_data private;
945 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
951 private.event = event;
952 private.priority = priority;
954 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
956 pcmcia_put_bus_socket(skt);
961 /* Normally, the event is passed to individual drivers after
962 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
963 * is inversed to maintain historic compatibility.
966 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
968 struct pcmcia_bus_socket *s = skt->pcmcia;
971 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
976 case CS_EVENT_CARD_REMOVAL:
977 s->state &= ~DS_SOCKET_PRESENT;
978 send_event(skt, event, priority);
980 handle_event(s, event);
983 case CS_EVENT_CARD_INSERTION:
984 s->state |= DS_SOCKET_PRESENT;
985 pcmcia_card_add(skt);
986 handle_event(s, event);
989 case CS_EVENT_EJECTION_REQUEST:
990 ret = send_event(skt, event, priority);
994 handle_event(s, event);
995 send_event(skt, event, priority);
1003 /*======================================================================
1005 bind_request() and bind_device() are merged by now. Register_client()
1006 is called right at the end of bind_request(), during the driver's
1007 ->attach() call. Individual descriptions:
1009 bind_request() connects a socket to a particular client driver.
1010 It looks up the specified device ID in the list of registered
1011 drivers, binds it to the socket, and tries to create an instance
1012 of the device. unbind_request() deletes a driver instance.
1014 Bind_device() associates a device driver with a particular socket.
1015 It is normally called by Driver Services after it has identified
1016 a newly inserted card. An instance of that driver will then be
1017 eligible to register as a client of this socket.
1019 Register_client() uses the dev_info_t handle to match the
1020 caller with a socket. The driver must have already been bound
1021 to a socket with bind_device() -- in fact, bind_device()
1022 allocates the client structure that will be used.
1024 ======================================================================*/
1026 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
1028 struct pcmcia_driver *p_drv;
1029 struct pcmcia_device *p_dev;
1031 unsigned long flags;
1033 s = pcmcia_get_bus_socket(s);
1037 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
1038 (char *)bind_info->dev_info);
1040 p_drv = get_pcmcia_driver(&bind_info->dev_info);
1046 if (!try_module_get(p_drv->owner)) {
1048 goto err_put_driver;
1051 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1052 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1053 if (p_dev->func == bind_info->function) {
1054 if ((p_dev->dev.driver == &p_drv->drv)) {
1055 if (p_dev->cardmgr) {
1056 /* if there's already a device
1057 * registered, and it was registered
1058 * by userspace before, we need to
1059 * return the "instance". */
1060 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1061 bind_info->instance = p_dev->instance;
1063 goto err_put_module;
1065 /* the correct driver managed to bind
1066 * itself magically to the correct
1068 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1069 p_dev->cardmgr = p_drv;
1071 goto err_put_module;
1073 } else if (!p_dev->dev.driver) {
1074 /* there's already a device available where
1075 * no device has been bound to yet. So we don't
1076 * need to register a device! */
1077 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1082 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1084 p_dev = pcmcia_device_add(s, bind_info->function);
1087 goto err_put_module;
1091 p_dev->cardmgr = p_drv;
1093 /* if a driver is already running, we can abort */
1094 if (p_dev->dev.driver)
1095 goto err_put_module;
1098 * Prevent this racing with a card insertion.
1100 down(&s->parent->skt_sem);
1101 bus_rescan_devices(&pcmcia_bus_type);
1102 up(&s->parent->skt_sem);
1104 /* check whether the driver indeed matched. I don't care if this
1105 * is racy or not, because it can only happen on cardmgr access
1108 if (!(p_dev->dev.driver == &p_drv->drv))
1109 p_dev->cardmgr = NULL;
1112 module_put(p_drv->owner);
1114 put_driver(&p_drv->drv);
1116 pcmcia_put_bus_socket(s);
1119 } /* bind_request */
1122 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1124 client_t *client = NULL;
1125 struct pcmcia_socket *s;
1126 struct pcmcia_bus_socket *skt = NULL;
1127 struct pcmcia_device *p_dev = NULL;
1129 /* Look for unbound client with matching dev_info */
1130 down_read(&pcmcia_socket_list_rwsem);
1131 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1132 unsigned long flags;
1134 if (s->state & SOCKET_CARDBUS)
1140 skt = pcmcia_get_bus_socket(skt);
1143 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1144 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
1145 struct pcmcia_driver *p_drv;
1146 p_dev = pcmcia_get_dev(p_dev);
1149 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
1150 (!p_dev->dev.driver)) {
1151 pcmcia_put_dev(p_dev);
1154 p_drv = to_pcmcia_drv(p_dev->dev.driver);
1155 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1156 client = &p_dev->client;
1157 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1160 pcmcia_put_dev(p_dev);
1162 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1163 pcmcia_put_bus_socket(skt);
1166 up_read(&pcmcia_socket_list_rwsem);
1167 if (!p_dev || !client)
1170 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
1173 client->state &= ~CLIENT_UNBOUND;
1175 client->EventMask = req->EventMask;
1176 client->event_handler = req->event_handler;
1177 client->event_callback_args = req->event_callback_args;
1178 client->event_callback_args.client_handle = client;
1180 if (s->state & SOCKET_CARDBUS)
1181 client->state |= CLIENT_CARDBUS;
1183 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
1184 (client->Function != BIND_FN_ALL)) {
1185 cistpl_longlink_mfc_t mfc;
1186 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
1188 s->functions = mfc.nfn;
1191 s->config = kmalloc(sizeof(config_t) * s->functions,
1194 goto out_no_resource;
1195 memset(s->config, 0, sizeof(config_t) * s->functions);
1198 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1199 client, p_dev->dev.bus_id);
1200 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
1201 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
1203 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1204 if (client->EventMask & CS_EVENT_CARD_INSERTION)
1205 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1211 pcmcia_put_dev(p_dev);
1212 return CS_OUT_OF_RESOURCE;
1213 } /* register_client */
1214 EXPORT_SYMBOL(pcmcia_register_client);
1217 /*====================================================================*/
1219 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
1221 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
1224 struct pcmcia_device *p_dev;
1225 unsigned long flags;
1228 #ifdef CONFIG_CARDBUS
1230 * Some unbelievably ugly code to associate the PCI cardbus
1231 * device and its driver with the PCMCIA "bind" information.
1234 struct pci_bus *bus;
1236 bus = pcmcia_lookup_bus(s->parent);
1238 struct list_head *list;
1239 struct pci_dev *dev = NULL;
1241 list = bus->devices.next;
1242 while (list != &bus->devices) {
1243 struct pci_dev *pdev = pci_dev_b(list);
1251 /* Try to handle "next" here some way? */
1253 if (dev && dev->driver) {
1254 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1255 bind_info->major = 0;
1256 bind_info->minor = 0;
1257 bind_info->next = NULL;
1264 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1265 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1266 if (p_dev->func == bind_info->function) {
1267 p_dev = pcmcia_get_dev(p_dev);
1273 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1277 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1279 if ((!p_dev->instance) ||
1280 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1286 node = p_dev->instance->dev;
1288 for (node = p_dev->instance->dev; node; node = node->next)
1289 if (node == bind_info->next)
1296 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1297 bind_info->major = node->major;
1298 bind_info->minor = node->minor;
1299 bind_info->next = node->next;
1302 pcmcia_put_dev(p_dev);
1304 } /* get_device_info */
1306 /*====================================================================*/
1308 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1309 * drivers have been called with EVENT_CARD_REMOVAL before.
1311 static int unbind_request(struct pcmcia_bus_socket *s)
1313 struct pcmcia_device *p_dev;
1314 unsigned long flags;
1316 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1318 s->device_count = 0;
1321 /* unregister all pcmcia_devices registered with this socket*/
1322 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1323 if (list_empty(&s->devices_list)) {
1324 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1327 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1328 list_del(&p_dev->socket_device_list);
1329 p_dev->client.state |= CLIENT_STALE;
1330 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1332 device_unregister(&p_dev->dev);
1336 } /* unbind_request */
1338 int pcmcia_deregister_client(client_handle_t handle)
1340 struct pcmcia_socket *s;
1342 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1344 if (CHECK_HANDLE(handle))
1345 return CS_BAD_HANDLE;
1348 ds_dbg(1, "deregister_client(%p)\n", handle);
1350 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1352 for (i = 0; i < MAX_WIN; i++)
1353 if (handle->state & CLIENT_WIN_REQ(i))
1356 if (handle->state & CLIENT_STALE) {
1357 handle->client_magic = 0;
1358 handle->state &= ~CLIENT_STALE;
1359 pcmcia_put_dev(p_dev);
1361 handle->state = CLIENT_UNBOUND;
1362 handle->event_handler = NULL;
1367 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1369 } /* deregister_client */
1370 EXPORT_SYMBOL(pcmcia_deregister_client);
1373 /*======================================================================
1375 The user-mode PC Card device interface
1377 ======================================================================*/
1379 static int ds_open(struct inode *inode, struct file *file)
1381 socket_t i = iminor(inode);
1382 struct pcmcia_bus_socket *s;
1385 ds_dbg(0, "ds_open(socket %d)\n", i);
1387 s = get_socket_info_by_nr(i);
1390 s = pcmcia_get_bus_socket(s);
1394 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1395 if (s->state & DS_SOCKET_BUSY) {
1396 pcmcia_put_bus_socket(s);
1400 s->state |= DS_SOCKET_BUSY;
1403 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1405 pcmcia_put_bus_socket(s);
1408 user->event_tail = user->event_head = 0;
1409 user->next = s->user;
1410 user->user_magic = USER_MAGIC;
1413 file->private_data = user;
1415 if (s->state & DS_SOCKET_PRESENT)
1416 queue_event(user, CS_EVENT_CARD_INSERTION);
1420 /*====================================================================*/
1422 static int ds_release(struct inode *inode, struct file *file)
1424 struct pcmcia_bus_socket *s;
1425 user_info_t *user, **link;
1427 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1429 user = file->private_data;
1430 if (CHECK_USER(user))
1435 /* Unlink user data structure */
1436 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1437 s->state &= ~DS_SOCKET_BUSY;
1439 file->private_data = NULL;
1440 for (link = &s->user; *link; link = &(*link)->next)
1441 if (*link == user) break;
1445 user->user_magic = 0;
1447 pcmcia_put_bus_socket(s);
1452 /*====================================================================*/
1454 static ssize_t ds_read(struct file *file, char __user *buf,
1455 size_t count, loff_t *ppos)
1457 struct pcmcia_bus_socket *s;
1461 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1466 user = file->private_data;
1467 if (CHECK_USER(user))
1471 if (s->state & DS_SOCKET_DEAD)
1474 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1476 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1481 /*====================================================================*/
1483 static ssize_t ds_write(struct file *file, const char __user *buf,
1484 size_t count, loff_t *ppos)
1486 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1490 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1496 /*====================================================================*/
1498 /* No kernel lock - fine */
1499 static u_int ds_poll(struct file *file, poll_table *wait)
1501 struct pcmcia_bus_socket *s;
1504 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1506 user = file->private_data;
1507 if (CHECK_USER(user))
1511 * We don't check for a dead socket here since that
1512 * will send cardmgr into an endless spin.
1514 poll_wait(file, &s->queue, wait);
1515 if (!queue_empty(user))
1516 return POLLIN | POLLRDNORM;
1520 /*====================================================================*/
1522 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1524 static int ds_ioctl(struct inode * inode, struct file * file,
1525 u_int cmd, u_long arg)
1527 struct pcmcia_bus_socket *s;
1528 void __user *uarg = (char __user *)arg;
1531 ds_ioctl_arg_t *buf;
1534 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1536 user = file->private_data;
1537 if (CHECK_USER(user))
1541 if (s->state & DS_SOCKET_DEAD)
1544 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1545 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1547 /* Permission check */
1548 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1552 if (!access_ok(VERIFY_READ, uarg, size)) {
1553 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1557 if (cmd & IOC_OUT) {
1558 if (!access_ok(VERIFY_WRITE, uarg, size)) {
1559 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1563 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1569 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1572 case DS_ADJUST_RESOURCE_INFO:
1573 ret = pcmcia_adjust_resource_info(&buf->adjust);
1575 case DS_GET_CARD_SERVICES_INFO:
1576 ret = pcmcia_get_card_services_info(&buf->servinfo);
1578 case DS_GET_CONFIGURATION_INFO:
1579 if (buf->config.Function &&
1580 (buf->config.Function >= s->parent->functions))
1583 ret = pccard_get_configuration_info(s->parent,
1584 buf->config.Function, &buf->config);
1586 case DS_GET_FIRST_TUPLE:
1587 down(&s->parent->skt_sem);
1588 pcmcia_validate_mem(s->parent);
1589 up(&s->parent->skt_sem);
1590 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1592 case DS_GET_NEXT_TUPLE:
1593 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1595 case DS_GET_TUPLE_DATA:
1596 buf->tuple.TupleData = buf->tuple_parse.data;
1597 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1598 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1600 case DS_PARSE_TUPLE:
1601 buf->tuple.TupleData = buf->tuple_parse.data;
1602 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1605 ret = pccard_reset_card(s->parent);
1608 if (buf->status.Function &&
1609 (buf->status.Function >= s->parent->functions))
1612 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1614 case DS_VALIDATE_CIS:
1615 down(&s->parent->skt_sem);
1616 pcmcia_validate_mem(s->parent);
1617 up(&s->parent->skt_sem);
1618 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1620 case DS_SUSPEND_CARD:
1621 ret = pcmcia_suspend_card(s->parent);
1623 case DS_RESUME_CARD:
1624 ret = pcmcia_resume_card(s->parent);
1627 err = pcmcia_eject_card(s->parent);
1629 case DS_INSERT_CARD:
1630 err = pcmcia_insert_card(s->parent);
1632 case DS_ACCESS_CONFIGURATION_REGISTER:
1633 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1637 if (buf->conf_reg.Function &&
1638 (buf->conf_reg.Function >= s->parent->functions))
1641 ret = pccard_access_configuration_register(s->parent,
1642 buf->conf_reg.Function, &buf->conf_reg);
1644 case DS_GET_FIRST_REGION:
1645 case DS_GET_NEXT_REGION:
1647 if (!capable(CAP_SYS_ADMIN)) {
1651 static int printed = 0;
1653 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1654 printk(KERN_WARNING "MTD handling any more.\n");
1661 case DS_GET_FIRST_WINDOW:
1662 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1663 &buf->win_info.window);
1665 case DS_GET_NEXT_WINDOW:
1666 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1667 buf->win_info.handle->index + 1, &buf->win_info.window);
1669 case DS_GET_MEM_PAGE:
1670 ret = pcmcia_get_mem_page(buf->win_info.handle,
1671 &buf->win_info.map);
1673 case DS_REPLACE_CIS:
1674 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1676 case DS_BIND_REQUEST:
1677 if (!capable(CAP_SYS_ADMIN)) {
1681 err = bind_request(s, &buf->bind_info);
1683 case DS_GET_DEVICE_INFO:
1684 err = get_device_info(s, &buf->bind_info, 1);
1686 case DS_GET_NEXT_DEVICE:
1687 err = get_device_info(s, &buf->bind_info, 0);
1689 case DS_UNBIND_REQUEST:
1696 if ((err == 0) && (ret != CS_SUCCESS)) {
1697 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1699 case CS_BAD_SOCKET: case CS_NO_CARD:
1700 err = -ENODEV; break;
1701 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1703 err = -EINVAL; break;
1705 err = -EBUSY; break;
1706 case CS_OUT_OF_RESOURCE:
1707 err = -ENOSPC; break;
1708 case CS_NO_MORE_ITEMS:
1709 err = -ENODATA; break;
1710 case CS_UNSUPPORTED_FUNCTION:
1711 err = -ENOSYS; break;
1717 if (cmd & IOC_OUT) {
1718 if (__copy_to_user(uarg, (char *)buf, size))
1727 /*====================================================================*/
1729 static struct file_operations ds_fops = {
1730 .owner = THIS_MODULE,
1732 .release = ds_release,
1739 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1741 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1742 struct pcmcia_bus_socket *s;
1745 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1748 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1750 /* get reference to parent socket */
1751 s->parent = pcmcia_get_socket(socket);
1753 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1758 kref_init(&s->refcount);
1761 * Ugly. But we want to wait for the socket threads to have started up.
1762 * We really should let the drivers themselves drive some of this..
1766 init_waitqueue_head(&s->queue);
1767 INIT_LIST_HEAD(&s->devices_list);
1768 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
1770 /* Set up hotline to Card Services */
1771 s->callback.owner = THIS_MODULE;
1772 s->callback.event = &ds_event;
1773 s->callback.resources_done = &pcmcia_card_add;
1774 s->callback.replace_cis = &pcmcia_bus_rescan;
1777 ret = pccard_register_pcmcia(socket, &s->callback);
1779 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1780 pcmcia_put_bus_socket(s);
1781 socket->pcmcia = NULL;
1789 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1791 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1793 if (!socket || !socket->pcmcia)
1796 pccard_register_pcmcia(socket, NULL);
1798 socket->pcmcia->state |= DS_SOCKET_DEAD;
1799 pcmcia_put_bus_socket(socket->pcmcia);
1800 socket->pcmcia = NULL;
1806 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1807 static struct class_interface pcmcia_bus_interface = {
1808 .class = &pcmcia_socket_class,
1809 .add = &pcmcia_bus_add_socket,
1810 .remove = &pcmcia_bus_remove_socket,
1814 struct bus_type pcmcia_bus_type = {
1816 .hotplug = pcmcia_bus_hotplug,
1817 .match = pcmcia_bus_match,
1818 .dev_attrs = pcmcia_dev_attrs,
1820 EXPORT_SYMBOL(pcmcia_bus_type);
1823 static int __init init_pcmcia_bus(void)
1827 spin_lock_init(&pcmcia_dev_list_lock);
1829 bus_register(&pcmcia_bus_type);
1830 class_interface_register(&pcmcia_bus_interface);
1832 /* Set up character device for user mode clients */
1833 i = register_chrdev(0, "pcmcia", &ds_fops);
1835 printk(KERN_NOTICE "unable to find a free device # for "
1836 "Driver Services (error=%d)\n", i);
1840 #ifdef CONFIG_PROC_FS
1841 proc_pccard = proc_mkdir("pccard", proc_bus);
1843 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1848 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1849 * pcmcia_socket_class is already registered */
1852 static void __exit exit_pcmcia_bus(void)
1854 class_interface_unregister(&pcmcia_bus_interface);
1856 #ifdef CONFIG_PROC_FS
1858 remove_proc_entry("drivers", proc_pccard);
1859 remove_proc_entry("pccard", proc_bus);
1862 if (major_dev != -1)
1863 unregister_chrdev(major_dev, "pcmcia");
1865 bus_unregister(&pcmcia_bus_type);
1867 module_exit(exit_pcmcia_bus);
1871 /* helpers for backwards-compatible functions */
1873 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1875 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1882 /* backwards-compatible accessing of driver --- by name! */
1884 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1886 struct device_driver *drv;
1887 struct pcmcia_driver *p_drv;
1889 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1893 p_drv = container_of(drv, struct pcmcia_driver, drv);