[PATCH] pcmcia: add some Documentation
[linux-2.6/mini2440.git] / drivers / pcmcia / ds.c
blob2c3c3da5368efa9b7a4f32072265d7822c94502a
1 /*
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>
25 #include <linux/mm.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"
54 /*====================================================================*/
56 /* Module parameters */
58 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
59 MODULE_DESCRIPTION("PCMCIA Driver Services");
60 MODULE_LICENSE("GPL");
62 #ifdef DEBUG
63 static int ds_pc_debug;
65 module_param_named(pc_debug, ds_pc_debug, int, 0644);
67 #define ds_dbg(lvl, fmt, arg...) do { \
68 if (ds_pc_debug > (lvl)) \
69 printk(KERN_DEBUG "ds: " fmt , ## arg); \
70 } while (0)
71 #else
72 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
73 #endif
75 /*====================================================================*/
77 /* Device user information */
78 #define MAX_EVENTS 32
79 #define USER_MAGIC 0x7ea4
80 #define CHECK_USER(u) \
81 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
82 typedef struct user_info_t {
83 u_int user_magic;
84 int event_head, event_tail;
85 event_t event[MAX_EVENTS];
86 struct user_info_t *next;
87 struct pcmcia_bus_socket *socket;
88 } user_info_t;
90 /* Socket state information */
91 struct pcmcia_bus_socket {
92 struct kref refcount;
93 struct pcmcia_callback callback;
94 int state;
95 user_info_t *user;
96 wait_queue_head_t queue;
97 struct pcmcia_socket *parent;
99 /* the PCMCIA devices connected to this socket (normally one, more
100 * for multifunction devices: */
101 struct list_head devices_list;
102 u8 device_count; /* the number of devices, used
103 * only internally and subject
104 * to incorrectness and change */
106 u8 device_add_pending;
107 struct work_struct device_add;
109 static spinlock_t pcmcia_dev_list_lock;
111 static struct bus_type pcmcia_bus_type;
113 #define DS_SOCKET_PRESENT 0x01
114 #define DS_SOCKET_BUSY 0x02
115 #define DS_SOCKET_REMOVAL_PENDING 0x10
116 #define DS_SOCKET_DEAD 0x80
118 /*====================================================================*/
120 static int major_dev = -1;
122 static int unbind_request(struct pcmcia_bus_socket *s);
124 /*====================================================================*/
126 /* code which was in cs.c before */
128 /* String tables for error messages */
130 typedef struct lookup_t {
131 int key;
132 char *msg;
133 } lookup_t;
135 static const lookup_t error_table[] = {
136 { CS_SUCCESS, "Operation succeeded" },
137 { CS_BAD_ADAPTER, "Bad adapter" },
138 { CS_BAD_ATTRIBUTE, "Bad attribute", },
139 { CS_BAD_BASE, "Bad base address" },
140 { CS_BAD_EDC, "Bad EDC" },
141 { CS_BAD_IRQ, "Bad IRQ" },
142 { CS_BAD_OFFSET, "Bad offset" },
143 { CS_BAD_PAGE, "Bad page number" },
144 { CS_READ_FAILURE, "Read failure" },
145 { CS_BAD_SIZE, "Bad size" },
146 { CS_BAD_SOCKET, "Bad socket" },
147 { CS_BAD_TYPE, "Bad type" },
148 { CS_BAD_VCC, "Bad Vcc" },
149 { CS_BAD_VPP, "Bad Vpp" },
150 { CS_BAD_WINDOW, "Bad window" },
151 { CS_WRITE_FAILURE, "Write failure" },
152 { CS_NO_CARD, "No card present" },
153 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
154 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
155 { CS_BAD_SPEED, "Bad speed" },
156 { CS_BUSY, "Resource busy" },
157 { CS_GENERAL_FAILURE, "General failure" },
158 { CS_WRITE_PROTECTED, "Write protected" },
159 { CS_BAD_ARG_LENGTH, "Bad argument length" },
160 { CS_BAD_ARGS, "Bad arguments" },
161 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
162 { CS_IN_USE, "Resource in use" },
163 { CS_NO_MORE_ITEMS, "No more items" },
164 { CS_OUT_OF_RESOURCE, "Out of resource" },
165 { CS_BAD_HANDLE, "Bad handle" },
166 { CS_BAD_TUPLE, "Bad CIS tuple" }
170 static const lookup_t service_table[] = {
171 { AccessConfigurationRegister, "AccessConfigurationRegister" },
172 { AddSocketServices, "AddSocketServices" },
173 { AdjustResourceInfo, "AdjustResourceInfo" },
174 { CheckEraseQueue, "CheckEraseQueue" },
175 { CloseMemory, "CloseMemory" },
176 { DeregisterClient, "DeregisterClient" },
177 { DeregisterEraseQueue, "DeregisterEraseQueue" },
178 { GetCardServicesInfo, "GetCardServicesInfo" },
179 { GetClientInfo, "GetClientInfo" },
180 { GetConfigurationInfo, "GetConfigurationInfo" },
181 { GetEventMask, "GetEventMask" },
182 { GetFirstClient, "GetFirstClient" },
183 { GetFirstRegion, "GetFirstRegion" },
184 { GetFirstTuple, "GetFirstTuple" },
185 { GetNextClient, "GetNextClient" },
186 { GetNextRegion, "GetNextRegion" },
187 { GetNextTuple, "GetNextTuple" },
188 { GetStatus, "GetStatus" },
189 { GetTupleData, "GetTupleData" },
190 { MapMemPage, "MapMemPage" },
191 { ModifyConfiguration, "ModifyConfiguration" },
192 { ModifyWindow, "ModifyWindow" },
193 { OpenMemory, "OpenMemory" },
194 { ParseTuple, "ParseTuple" },
195 { ReadMemory, "ReadMemory" },
196 { RegisterClient, "RegisterClient" },
197 { RegisterEraseQueue, "RegisterEraseQueue" },
198 { RegisterMTD, "RegisterMTD" },
199 { ReleaseConfiguration, "ReleaseConfiguration" },
200 { ReleaseIO, "ReleaseIO" },
201 { ReleaseIRQ, "ReleaseIRQ" },
202 { ReleaseWindow, "ReleaseWindow" },
203 { RequestConfiguration, "RequestConfiguration" },
204 { RequestIO, "RequestIO" },
205 { RequestIRQ, "RequestIRQ" },
206 { RequestSocketMask, "RequestSocketMask" },
207 { RequestWindow, "RequestWindow" },
208 { ResetCard, "ResetCard" },
209 { SetEventMask, "SetEventMask" },
210 { ValidateCIS, "ValidateCIS" },
211 { WriteMemory, "WriteMemory" },
212 { BindDevice, "BindDevice" },
213 { BindMTD, "BindMTD" },
214 { ReportError, "ReportError" },
215 { SuspendCard, "SuspendCard" },
216 { ResumeCard, "ResumeCard" },
217 { EjectCard, "EjectCard" },
218 { InsertCard, "InsertCard" },
219 { ReplaceCIS, "ReplaceCIS" }
223 static int pcmcia_report_error(client_handle_t handle, error_info_t *err)
225 int i;
226 char *serv;
228 if (CHECK_HANDLE(handle))
229 printk(KERN_NOTICE);
230 else {
231 struct pcmcia_device *p_dev = handle_to_pdev(handle);
232 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
235 for (i = 0; i < ARRAY_SIZE(service_table); i++)
236 if (service_table[i].key == err->func)
237 break;
238 if (i < ARRAY_SIZE(service_table))
239 serv = service_table[i].msg;
240 else
241 serv = "Unknown service number";
243 for (i = 0; i < ARRAY_SIZE(error_table); i++)
244 if (error_table[i].key == err->retcode)
245 break;
246 if (i < ARRAY_SIZE(error_table))
247 printk("%s: %s\n", serv, error_table[i].msg);
248 else
249 printk("%s: Unknown error code %#x\n", serv, err->retcode);
251 return CS_SUCCESS;
252 } /* report_error */
254 /* end of code which was in cs.c before */
256 /*======================================================================*/
258 void cs_error(client_handle_t handle, int func, int ret)
260 error_info_t err = { func, ret };
261 pcmcia_report_error(handle, &err);
263 EXPORT_SYMBOL(cs_error);
266 static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
268 struct pcmcia_device_id *did = p_drv->id_table;
269 unsigned int i;
270 u32 hash;
272 while (did && did->match_flags) {
273 for (i=0; i<4; i++) {
274 if (!did->prod_id[i])
275 continue;
277 hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
278 if (hash == did->prod_id_hash[i])
279 continue;
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);
285 printk(KERN_DEBUG "pcmcia: see "
286 "Documentation/pcmcia/devicetable.txt for "
287 "details\n");
289 did++;
292 return;
296 #ifdef CONFIG_PCMCIA_LOAD_CIS
299 * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
300 * @dev - the pcmcia device which needs a CIS override
301 * @filename - requested filename in /lib/firmware/cis/
303 * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
304 * the one provided by the card is broken. The firmware files reside in
305 * /lib/firmware/cis/ in userspace.
307 static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
309 struct pcmcia_socket *s = dev->socket;
310 const struct firmware *fw;
311 char path[20];
312 int ret=-ENOMEM;
313 cisdump_t *cis;
315 if (!filename)
316 return -EINVAL;
318 ds_dbg(1, "trying to load firmware %s\n", filename);
320 if (strlen(filename) > 14)
321 return -EINVAL;
323 snprintf(path, 20, "%s", filename);
325 if (request_firmware(&fw, path, &dev->dev) == 0) {
326 if (fw->size >= CISTPL_MAX_CIS_SIZE)
327 goto release;
329 cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL);
330 if (!cis)
331 goto release;
333 memset(cis, 0, sizeof(cisdump_t));
335 cis->Length = fw->size + 1;
336 memcpy(cis->Data, fw->data, fw->size);
338 if (!pcmcia_replace_cis(s, cis))
339 ret = 0;
341 release:
342 release_firmware(fw);
344 return (ret);
347 #else /* !CONFIG_PCMCIA_LOAD_CIS */
349 static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
351 return -ENODEV;
354 #endif
357 /*======================================================================*/
359 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
360 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
362 static void pcmcia_release_bus_socket(struct kref *refcount)
364 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
365 pcmcia_put_socket(s->parent);
366 kfree(s);
369 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
371 kref_put(&s->refcount, pcmcia_release_bus_socket);
374 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
376 kref_get(&s->refcount);
377 return (s);
381 * pcmcia_register_driver - register a PCMCIA driver with the bus core
383 * Registers a PCMCIA driver with the PCMCIA bus core.
385 static int pcmcia_device_probe(struct device *dev);
386 static int pcmcia_device_remove(struct device * dev);
388 int pcmcia_register_driver(struct pcmcia_driver *driver)
390 if (!driver)
391 return -EINVAL;
393 pcmcia_check_driver(driver);
395 /* initialize common fields */
396 driver->drv.bus = &pcmcia_bus_type;
397 driver->drv.owner = driver->owner;
398 driver->drv.probe = pcmcia_device_probe;
399 driver->drv.remove = pcmcia_device_remove;
401 return driver_register(&driver->drv);
403 EXPORT_SYMBOL(pcmcia_register_driver);
406 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
408 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
410 driver_unregister(&driver->drv);
412 EXPORT_SYMBOL(pcmcia_unregister_driver);
414 #ifdef CONFIG_PROC_FS
415 static struct proc_dir_entry *proc_pccard = NULL;
417 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
419 char **p = d;
420 struct pcmcia_driver *p_drv = container_of(driver,
421 struct pcmcia_driver, drv);
423 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
424 #ifdef CONFIG_MODULE_UNLOAD
425 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
426 #else
428 #endif
430 d = (void *) p;
432 return 0;
435 static int proc_read_drivers(char *buf, char **start, off_t pos,
436 int count, int *eof, void *data)
438 char *p = buf;
440 bus_for_each_drv(&pcmcia_bus_type, NULL,
441 (void *) &p, proc_read_drivers_callback);
443 return (p - buf);
445 #endif
447 /* pcmcia_device handling */
449 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
451 struct device *tmp_dev;
452 tmp_dev = get_device(&p_dev->dev);
453 if (!tmp_dev)
454 return NULL;
455 return to_pcmcia_dev(tmp_dev);
458 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
460 if (p_dev)
461 put_device(&p_dev->dev);
464 static void pcmcia_release_dev(struct device *dev)
466 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
467 ds_dbg(1, "releasing dev %p\n", p_dev);
468 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
469 kfree(p_dev);
473 static int pcmcia_device_probe(struct device * dev)
475 struct pcmcia_device *p_dev;
476 struct pcmcia_driver *p_drv;
477 int ret = 0;
479 dev = get_device(dev);
480 if (!dev)
481 return -ENODEV;
483 p_dev = to_pcmcia_dev(dev);
484 p_drv = to_pcmcia_drv(dev->driver);
486 if (!try_module_get(p_drv->owner)) {
487 ret = -EINVAL;
488 goto put_dev;
491 if (p_drv->attach) {
492 p_dev->instance = p_drv->attach();
493 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
494 printk(KERN_NOTICE "ds: unable to create instance "
495 "of '%s'!\n", p_drv->drv.name);
496 ret = -EINVAL;
500 if (ret)
501 module_put(p_drv->owner);
502 put_dev:
503 if ((ret) || !(p_drv->attach))
504 put_device(dev);
505 return (ret);
509 static int pcmcia_device_remove(struct device * dev)
511 struct pcmcia_device *p_dev;
512 struct pcmcia_driver *p_drv;
514 /* detach the "instance" */
515 p_dev = to_pcmcia_dev(dev);
516 p_drv = to_pcmcia_drv(dev->driver);
518 if (p_drv) {
519 if ((p_drv->detach) && (p_dev->instance)) {
520 p_drv->detach(p_dev->instance);
521 /* from pcmcia_probe_device */
522 put_device(&p_dev->dev);
524 module_put(p_drv->owner);
527 return 0;
533 * pcmcia_device_query -- determine information about a pcmcia device
535 static int pcmcia_device_query(struct pcmcia_device *p_dev)
537 cistpl_manfid_t manf_id;
538 cistpl_funcid_t func_id;
539 cistpl_vers_1_t vers1;
540 unsigned int i;
542 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
543 CISTPL_MANFID, &manf_id)) {
544 p_dev->manf_id = manf_id.manf;
545 p_dev->card_id = manf_id.card;
546 p_dev->has_manf_id = 1;
547 p_dev->has_card_id = 1;
550 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
551 CISTPL_FUNCID, &func_id)) {
552 p_dev->func_id = func_id.func;
553 p_dev->has_func_id = 1;
554 } else {
555 /* rule of thumb: cards with no FUNCID, but with
556 * common memory device geometry information, are
557 * probably memory cards (from pcmcia-cs) */
558 cistpl_device_geo_t devgeo;
559 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
560 CISTPL_DEVICE_GEO, &devgeo)) {
561 ds_dbg(0, "mem device geometry probably means "
562 "FUNCID_MEMORY\n");
563 p_dev->func_id = CISTPL_FUNCID_MEMORY;
564 p_dev->has_func_id = 1;
568 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
569 &vers1)) {
570 for (i=0; i < vers1.ns; i++) {
571 char *tmp;
572 unsigned int length;
574 tmp = vers1.str + vers1.ofs[i];
576 length = strlen(tmp) + 1;
577 if ((length < 3) || (length > 255))
578 continue;
580 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
581 GFP_KERNEL);
582 if (!p_dev->prod_id[i])
583 continue;
585 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
586 tmp, length);
590 return 0;
594 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
595 * Serializes pcmcia_device_add; will most likely be removed in future.
597 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
598 * won't work, this doesn't matter much at the moment: the driver core doesn't
599 * support it either.
601 static DECLARE_MUTEX(device_add_lock);
603 static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
605 struct pcmcia_device *p_dev;
606 unsigned long flags;
608 s = pcmcia_get_bus_socket(s);
609 if (!s)
610 return NULL;
612 down(&device_add_lock);
614 /* max of 2 devices per card */
615 if (s->device_count == 2)
616 goto err_put;
618 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
619 if (!p_dev)
620 goto err_put;
621 memset(p_dev, 0, sizeof(struct pcmcia_device));
623 p_dev->socket = s->parent;
624 p_dev->device_no = (s->device_count++);
625 p_dev->func = function;
627 p_dev->dev.bus = &pcmcia_bus_type;
628 p_dev->dev.parent = s->parent->dev.dev;
629 p_dev->dev.release = pcmcia_release_dev;
630 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
632 /* compat */
633 p_dev->client.client_magic = CLIENT_MAGIC;
634 p_dev->client.Socket = s->parent;
635 p_dev->client.Function = function;
636 p_dev->client.state = CLIENT_UNBOUND;
638 /* Add to the list in pcmcia_bus_socket */
639 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
640 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
641 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
643 pcmcia_device_query(p_dev);
645 if (device_register(&p_dev->dev)) {
646 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
647 list_del(&p_dev->socket_device_list);
648 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
650 goto err_free;
653 up(&device_add_lock);
655 return p_dev;
657 err_free:
658 kfree(p_dev);
659 s->device_count--;
660 err_put:
661 up(&device_add_lock);
662 pcmcia_put_bus_socket(s);
664 return NULL;
668 static int pcmcia_card_add(struct pcmcia_socket *s)
670 cisinfo_t cisinfo;
671 cistpl_longlink_mfc_t mfc;
672 unsigned int no_funcs, i;
673 int ret = 0;
675 if (!(s->resource_setup_done))
676 return -EAGAIN; /* try again, but later... */
678 pcmcia_validate_mem(s);
679 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
680 if (ret || !cisinfo.Chains) {
681 ds_dbg(0, "invalid CIS or invalid resources\n");
682 return -ENODEV;
685 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
686 no_funcs = mfc.nfn;
687 else
688 no_funcs = 1;
690 /* this doesn't handle multifunction devices on one pcmcia function
691 * yet. */
692 for (i=0; i < no_funcs; i++)
693 pcmcia_device_add(s->pcmcia, i);
695 return (ret);
699 static void pcmcia_delayed_add_pseudo_device(void *data)
701 struct pcmcia_bus_socket *s = data;
702 pcmcia_device_add(s, 0);
703 s->device_add_pending = 0;
706 static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s)
708 if (!s->device_add_pending) {
709 schedule_work(&s->device_add);
710 s->device_add_pending = 1;
712 return;
715 static int pcmcia_requery(struct device *dev, void * _data)
717 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
718 if (!p_dev->dev.driver)
719 pcmcia_device_query(p_dev);
721 return 0;
724 static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
726 int no_devices=0;
727 unsigned long flags;
729 /* must be called with skt_sem held */
730 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
731 if (list_empty(&skt->pcmcia->devices_list))
732 no_devices=1;
733 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
735 /* if no devices were added for this socket yet because of
736 * missing resource information or other trouble, we need to
737 * do this now. */
738 if (no_devices) {
739 int ret = pcmcia_card_add(skt);
740 if (ret)
741 return;
744 /* some device information might have changed because of a CIS
745 * update or because we can finally read it correctly... so
746 * determine it again, overwriting old values if necessary. */
747 bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
749 /* we re-scan all devices, not just the ones connected to this
750 * socket. This does not matter, though. */
751 bus_rescan_devices(&pcmcia_bus_type);
754 static inline int pcmcia_devmatch(struct pcmcia_device *dev,
755 struct pcmcia_device_id *did)
757 if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
758 if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
759 return 0;
762 if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
763 if ((!dev->has_card_id) || (dev->card_id != did->card_id))
764 return 0;
767 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
768 if (dev->func != did->function)
769 return 0;
772 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
773 if (!dev->prod_id[0])
774 return 0;
775 if (strcmp(did->prod_id[0], dev->prod_id[0]))
776 return 0;
779 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
780 if (!dev->prod_id[1])
781 return 0;
782 if (strcmp(did->prod_id[1], dev->prod_id[1]))
783 return 0;
786 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
787 if (!dev->prod_id[2])
788 return 0;
789 if (strcmp(did->prod_id[2], dev->prod_id[2]))
790 return 0;
793 if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
794 if (!dev->prod_id[3])
795 return 0;
796 if (strcmp(did->prod_id[3], dev->prod_id[3]))
797 return 0;
800 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
801 /* handle pseudo multifunction devices:
802 * there are at most two pseudo multifunction devices.
803 * if we're matching against the first, schedule a
804 * call which will then check whether there are two
805 * pseudo devices, and if not, add the second one.
807 if (dev->device_no == 0)
808 pcmcia_add_pseudo_device(dev->socket->pcmcia);
810 if (dev->device_no != did->device_no)
811 return 0;
814 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
815 if ((!dev->has_func_id) || (dev->func_id != did->func_id))
816 return 0;
818 /* if this is a pseudo-multi-function device,
819 * we need explicit matches */
820 if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
821 return 0;
822 if (dev->device_no)
823 return 0;
825 /* also, FUNC_ID matching needs to be activated by userspace
826 * after it has re-checked that there is no possible module
827 * with a prod_id/manf_id/card_id match.
829 if (!dev->allow_func_id_match)
830 return 0;
833 if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
834 if (!dev->socket->fake_cis)
835 pcmcia_load_firmware(dev, did->cisfile);
837 if (!dev->socket->fake_cis)
838 return 0;
841 if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
842 int i;
843 for (i=0; i<4; i++)
844 if (dev->prod_id[i])
845 return 0;
846 if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
847 return 0;
850 dev->dev.driver_data = (void *) did;
852 return 1;
856 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
857 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
858 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
859 struct pcmcia_device_id *did = p_drv->id_table;
861 /* matching by cardmgr */
862 if (p_dev->cardmgr == p_drv)
863 return 1;
865 while (did && did->match_flags) {
866 if (pcmcia_devmatch(p_dev, did))
867 return 1;
868 did++;
871 return 0;
874 #ifdef CONFIG_HOTPLUG
876 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
877 char *buffer, int buffer_size)
879 struct pcmcia_device *p_dev;
880 int i, length = 0;
881 u32 hash[4] = { 0, 0, 0, 0};
883 if (!dev)
884 return -ENODEV;
886 p_dev = to_pcmcia_dev(dev);
888 /* calculate hashes */
889 for (i=0; i<4; i++) {
890 if (!p_dev->prod_id[i])
891 continue;
892 hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
895 i = 0;
897 if (add_hotplug_env_var(envp, num_envp, &i,
898 buffer, buffer_size, &length,
899 "SOCKET_NO=%u",
900 p_dev->socket->sock))
901 return -ENOMEM;
903 if (add_hotplug_env_var(envp, num_envp, &i,
904 buffer, buffer_size, &length,
905 "DEVICE_NO=%02X",
906 p_dev->device_no))
907 return -ENOMEM;
909 if (add_hotplug_env_var(envp, num_envp, &i,
910 buffer, buffer_size, &length,
911 "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
912 "pa%08Xpb%08Xpc%08Xpd%08X",
913 p_dev->has_manf_id ? p_dev->manf_id : 0,
914 p_dev->has_card_id ? p_dev->card_id : 0,
915 p_dev->has_func_id ? p_dev->func_id : 0,
916 p_dev->func,
917 p_dev->device_no,
918 hash[0],
919 hash[1],
920 hash[2],
921 hash[3]))
922 return -ENOMEM;
924 envp[i] = NULL;
926 return 0;
929 #else
931 static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
932 char *buffer, int buffer_size)
934 return -ENODEV;
937 #endif
939 /************************ per-device sysfs output ***************************/
941 #define pcmcia_device_attr(field, test, format) \
942 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
944 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
945 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
948 #define pcmcia_device_stringattr(name, field) \
949 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
951 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
952 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
955 pcmcia_device_attr(func, socket, "0x%02x\n");
956 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
957 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
958 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
959 pcmcia_device_stringattr(prod_id1, prod_id[0]);
960 pcmcia_device_stringattr(prod_id2, prod_id[1]);
961 pcmcia_device_stringattr(prod_id3, prod_id[2]);
962 pcmcia_device_stringattr(prod_id4, prod_id[3]);
965 static ssize_t pcmcia_store_allow_func_id_match (struct device * dev, struct device_attribute *attr,
966 const char * buf, size_t count)
968 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
969 if (!count)
970 return -EINVAL;
972 down(&p_dev->socket->skt_sem);
973 p_dev->allow_func_id_match = 1;
974 up(&p_dev->socket->skt_sem);
976 bus_rescan_devices(&pcmcia_bus_type);
978 return count;
981 static struct device_attribute pcmcia_dev_attrs[] = {
982 __ATTR(function, 0444, func_show, NULL),
983 __ATTR_RO(func_id),
984 __ATTR_RO(manf_id),
985 __ATTR_RO(card_id),
986 __ATTR_RO(prod_id1),
987 __ATTR_RO(prod_id2),
988 __ATTR_RO(prod_id3),
989 __ATTR_RO(prod_id4),
990 __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
991 __ATTR_NULL,
995 /*======================================================================
997 These manage a ring buffer of events pending for one user process
999 ======================================================================*/
1001 static int queue_empty(user_info_t *user)
1003 return (user->event_head == user->event_tail);
1006 static event_t get_queued_event(user_info_t *user)
1008 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
1009 return user->event[user->event_tail];
1012 static void queue_event(user_info_t *user, event_t event)
1014 user->event_head = (user->event_head+1) % MAX_EVENTS;
1015 if (user->event_head == user->event_tail)
1016 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
1017 user->event[user->event_head] = event;
1020 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
1022 user_info_t *user;
1023 for (user = s->user; user; user = user->next)
1024 queue_event(user, event);
1025 wake_up_interruptible(&s->queue);
1029 /*======================================================================
1031 The card status event handler.
1033 ======================================================================*/
1035 struct send_event_data {
1036 struct pcmcia_socket *skt;
1037 event_t event;
1038 int priority;
1041 static int send_event_callback(struct device *dev, void * _data)
1043 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
1044 struct send_event_data *data = _data;
1046 /* we get called for all sockets, but may only pass the event
1047 * for drivers _on the affected socket_ */
1048 if (p_dev->socket != data->skt)
1049 return 0;
1051 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
1052 return 0;
1054 if (p_dev->client.EventMask & data->event)
1055 return EVENT(&p_dev->client, data->event, data->priority);
1057 return 0;
1060 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
1062 int ret = 0;
1063 struct send_event_data private;
1064 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
1066 if (!skt)
1067 return 0;
1069 private.skt = s;
1070 private.event = event;
1071 private.priority = priority;
1073 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
1075 pcmcia_put_bus_socket(skt);
1076 return ret;
1077 } /* send_event */
1080 /* Normally, the event is passed to individual drivers after
1081 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
1082 * is inversed to maintain historic compatibility.
1085 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1087 struct pcmcia_bus_socket *s = skt->pcmcia;
1088 int ret = 0;
1090 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
1091 event, priority, s);
1093 switch (event) {
1095 case CS_EVENT_CARD_REMOVAL:
1096 s->state &= ~DS_SOCKET_PRESENT;
1097 send_event(skt, event, priority);
1098 unbind_request(s);
1099 handle_event(s, event);
1100 break;
1102 case CS_EVENT_CARD_INSERTION:
1103 s->state |= DS_SOCKET_PRESENT;
1104 pcmcia_card_add(skt);
1105 handle_event(s, event);
1106 break;
1108 case CS_EVENT_EJECTION_REQUEST:
1109 ret = send_event(skt, event, priority);
1110 break;
1112 default:
1113 handle_event(s, event);
1114 send_event(skt, event, priority);
1115 break;
1118 return 0;
1119 } /* ds_event */
1122 /*======================================================================
1124 bind_request() and bind_device() are merged by now. Register_client()
1125 is called right at the end of bind_request(), during the driver's
1126 ->attach() call. Individual descriptions:
1128 bind_request() connects a socket to a particular client driver.
1129 It looks up the specified device ID in the list of registered
1130 drivers, binds it to the socket, and tries to create an instance
1131 of the device. unbind_request() deletes a driver instance.
1133 Bind_device() associates a device driver with a particular socket.
1134 It is normally called by Driver Services after it has identified
1135 a newly inserted card. An instance of that driver will then be
1136 eligible to register as a client of this socket.
1138 Register_client() uses the dev_info_t handle to match the
1139 caller with a socket. The driver must have already been bound
1140 to a socket with bind_device() -- in fact, bind_device()
1141 allocates the client structure that will be used.
1143 ======================================================================*/
1145 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
1147 struct pcmcia_driver *p_drv;
1148 struct pcmcia_device *p_dev;
1149 int ret = 0;
1150 unsigned long flags;
1152 s = pcmcia_get_bus_socket(s);
1153 if (!s)
1154 return -EINVAL;
1156 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
1157 (char *)bind_info->dev_info);
1159 p_drv = get_pcmcia_driver(&bind_info->dev_info);
1160 if (!p_drv) {
1161 ret = -EINVAL;
1162 goto err_put;
1165 if (!try_module_get(p_drv->owner)) {
1166 ret = -EINVAL;
1167 goto err_put_driver;
1170 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1171 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1172 if (p_dev->func == bind_info->function) {
1173 if ((p_dev->dev.driver == &p_drv->drv)) {
1174 if (p_dev->cardmgr) {
1175 /* if there's already a device
1176 * registered, and it was registered
1177 * by userspace before, we need to
1178 * return the "instance". */
1179 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1180 bind_info->instance = p_dev->instance;
1181 ret = -EBUSY;
1182 goto err_put_module;
1183 } else {
1184 /* the correct driver managed to bind
1185 * itself magically to the correct
1186 * device. */
1187 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1188 p_dev->cardmgr = p_drv;
1189 ret = 0;
1190 goto err_put_module;
1192 } else if (!p_dev->dev.driver) {
1193 /* there's already a device available where
1194 * no device has been bound to yet. So we don't
1195 * need to register a device! */
1196 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1197 goto rescan;
1201 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1203 p_dev = pcmcia_device_add(s, bind_info->function);
1204 if (!p_dev) {
1205 ret = -EIO;
1206 goto err_put_module;
1209 rescan:
1210 p_dev->cardmgr = p_drv;
1212 /* if a driver is already running, we can abort */
1213 if (p_dev->dev.driver)
1214 goto err_put_module;
1217 * Prevent this racing with a card insertion.
1219 down(&s->parent->skt_sem);
1220 bus_rescan_devices(&pcmcia_bus_type);
1221 up(&s->parent->skt_sem);
1223 /* check whether the driver indeed matched. I don't care if this
1224 * is racy or not, because it can only happen on cardmgr access
1225 * paths...
1227 if (!(p_dev->dev.driver == &p_drv->drv))
1228 p_dev->cardmgr = NULL;
1230 err_put_module:
1231 module_put(p_drv->owner);
1232 err_put_driver:
1233 put_driver(&p_drv->drv);
1234 err_put:
1235 pcmcia_put_bus_socket(s);
1237 return (ret);
1238 } /* bind_request */
1241 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1243 client_t *client = NULL;
1244 struct pcmcia_socket *s;
1245 struct pcmcia_bus_socket *skt = NULL;
1246 struct pcmcia_device *p_dev = NULL;
1248 /* Look for unbound client with matching dev_info */
1249 down_read(&pcmcia_socket_list_rwsem);
1250 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
1251 unsigned long flags;
1253 if (s->state & SOCKET_CARDBUS)
1254 continue;
1256 skt = s->pcmcia;
1257 if (!skt)
1258 continue;
1259 skt = pcmcia_get_bus_socket(skt);
1260 if (!skt)
1261 continue;
1262 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1263 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
1264 struct pcmcia_driver *p_drv;
1265 p_dev = pcmcia_get_dev(p_dev);
1266 if (!p_dev)
1267 continue;
1268 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
1269 (!p_dev->dev.driver)) {
1270 pcmcia_put_dev(p_dev);
1271 continue;
1273 p_drv = to_pcmcia_drv(p_dev->dev.driver);
1274 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
1275 client = &p_dev->client;
1276 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1277 goto found;
1279 pcmcia_put_dev(p_dev);
1281 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1282 pcmcia_put_bus_socket(skt);
1284 found:
1285 up_read(&pcmcia_socket_list_rwsem);
1286 if (!p_dev || !client)
1287 return -ENODEV;
1289 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
1291 *handle = client;
1292 client->state &= ~CLIENT_UNBOUND;
1293 client->Socket = s;
1294 client->EventMask = req->EventMask;
1295 client->event_handler = req->event_handler;
1296 client->event_callback_args = req->event_callback_args;
1297 client->event_callback_args.client_handle = client;
1299 if (s->state & SOCKET_CARDBUS)
1300 client->state |= CLIENT_CARDBUS;
1302 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
1303 (client->Function != BIND_FN_ALL)) {
1304 cistpl_longlink_mfc_t mfc;
1305 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
1306 == CS_SUCCESS)
1307 s->functions = mfc.nfn;
1308 else
1309 s->functions = 1;
1310 s->config = kmalloc(sizeof(config_t) * s->functions,
1311 GFP_KERNEL);
1312 if (!s->config)
1313 goto out_no_resource;
1314 memset(s->config, 0, sizeof(config_t) * s->functions);
1317 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
1318 client, p_dev->dev.bus_id);
1319 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
1320 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
1322 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
1323 if (client->EventMask & CS_EVENT_CARD_INSERTION)
1324 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
1327 return CS_SUCCESS;
1329 out_no_resource:
1330 pcmcia_put_dev(p_dev);
1331 return CS_OUT_OF_RESOURCE;
1332 } /* register_client */
1333 EXPORT_SYMBOL(pcmcia_register_client);
1336 /*====================================================================*/
1338 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
1340 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
1342 dev_node_t *node;
1343 struct pcmcia_device *p_dev;
1344 unsigned long flags;
1345 int ret = 0;
1347 #ifdef CONFIG_CARDBUS
1349 * Some unbelievably ugly code to associate the PCI cardbus
1350 * device and its driver with the PCMCIA "bind" information.
1353 struct pci_bus *bus;
1355 bus = pcmcia_lookup_bus(s->parent);
1356 if (bus) {
1357 struct list_head *list;
1358 struct pci_dev *dev = NULL;
1360 list = bus->devices.next;
1361 while (list != &bus->devices) {
1362 struct pci_dev *pdev = pci_dev_b(list);
1363 list = list->next;
1365 if (first) {
1366 dev = pdev;
1367 break;
1370 /* Try to handle "next" here some way? */
1372 if (dev && dev->driver) {
1373 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1374 bind_info->major = 0;
1375 bind_info->minor = 0;
1376 bind_info->next = NULL;
1377 return 0;
1381 #endif
1383 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1384 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1385 if (p_dev->func == bind_info->function) {
1386 p_dev = pcmcia_get_dev(p_dev);
1387 if (!p_dev)
1388 continue;
1389 goto found;
1392 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1393 return -ENODEV;
1395 found:
1396 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1398 if ((!p_dev->instance) ||
1399 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1400 ret = -EAGAIN;
1401 goto err_put;
1404 if (first)
1405 node = p_dev->instance->dev;
1406 else
1407 for (node = p_dev->instance->dev; node; node = node->next)
1408 if (node == bind_info->next)
1409 break;
1410 if (!node) {
1411 ret = -ENODEV;
1412 goto err_put;
1415 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1416 bind_info->major = node->major;
1417 bind_info->minor = node->minor;
1418 bind_info->next = node->next;
1420 err_put:
1421 pcmcia_put_dev(p_dev);
1422 return (ret);
1423 } /* get_device_info */
1425 /*====================================================================*/
1427 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1428 * drivers have been called with EVENT_CARD_REMOVAL before.
1430 static int unbind_request(struct pcmcia_bus_socket *s)
1432 struct pcmcia_device *p_dev;
1433 unsigned long flags;
1435 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1437 s->device_count = 0;
1439 for (;;) {
1440 /* unregister all pcmcia_devices registered with this socket*/
1441 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1442 if (list_empty(&s->devices_list)) {
1443 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1444 return 0;
1446 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1447 list_del(&p_dev->socket_device_list);
1448 p_dev->client.state |= CLIENT_STALE;
1449 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1451 device_unregister(&p_dev->dev);
1454 return 0;
1455 } /* unbind_request */
1457 int pcmcia_deregister_client(client_handle_t handle)
1459 struct pcmcia_socket *s;
1460 int i;
1461 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1463 if (CHECK_HANDLE(handle))
1464 return CS_BAD_HANDLE;
1466 s = SOCKET(handle);
1467 ds_dbg(1, "deregister_client(%p)\n", handle);
1469 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1470 goto warn_out;
1471 for (i = 0; i < MAX_WIN; i++)
1472 if (handle->state & CLIENT_WIN_REQ(i))
1473 goto warn_out;
1475 if (handle->state & CLIENT_STALE) {
1476 handle->client_magic = 0;
1477 handle->state &= ~CLIENT_STALE;
1478 pcmcia_put_dev(p_dev);
1479 } else {
1480 handle->state = CLIENT_UNBOUND;
1481 handle->event_handler = NULL;
1484 return CS_SUCCESS;
1485 warn_out:
1486 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1487 return CS_IN_USE;
1488 } /* deregister_client */
1489 EXPORT_SYMBOL(pcmcia_deregister_client);
1492 /*======================================================================
1494 The user-mode PC Card device interface
1496 ======================================================================*/
1498 static int ds_open(struct inode *inode, struct file *file)
1500 socket_t i = iminor(inode);
1501 struct pcmcia_bus_socket *s;
1502 user_info_t *user;
1504 ds_dbg(0, "ds_open(socket %d)\n", i);
1506 s = get_socket_info_by_nr(i);
1507 if (!s)
1508 return -ENODEV;
1509 s = pcmcia_get_bus_socket(s);
1510 if (!s)
1511 return -ENODEV;
1513 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1514 if (s->state & DS_SOCKET_BUSY) {
1515 pcmcia_put_bus_socket(s);
1516 return -EBUSY;
1518 else
1519 s->state |= DS_SOCKET_BUSY;
1522 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1523 if (!user) {
1524 pcmcia_put_bus_socket(s);
1525 return -ENOMEM;
1527 user->event_tail = user->event_head = 0;
1528 user->next = s->user;
1529 user->user_magic = USER_MAGIC;
1530 user->socket = s;
1531 s->user = user;
1532 file->private_data = user;
1534 if (s->state & DS_SOCKET_PRESENT)
1535 queue_event(user, CS_EVENT_CARD_INSERTION);
1536 return 0;
1537 } /* ds_open */
1539 /*====================================================================*/
1541 static int ds_release(struct inode *inode, struct file *file)
1543 struct pcmcia_bus_socket *s;
1544 user_info_t *user, **link;
1546 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1548 user = file->private_data;
1549 if (CHECK_USER(user))
1550 goto out;
1552 s = user->socket;
1554 /* Unlink user data structure */
1555 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1556 s->state &= ~DS_SOCKET_BUSY;
1558 file->private_data = NULL;
1559 for (link = &s->user; *link; link = &(*link)->next)
1560 if (*link == user) break;
1561 if (link == NULL)
1562 goto out;
1563 *link = user->next;
1564 user->user_magic = 0;
1565 kfree(user);
1566 pcmcia_put_bus_socket(s);
1567 out:
1568 return 0;
1569 } /* ds_release */
1571 /*====================================================================*/
1573 static ssize_t ds_read(struct file *file, char __user *buf,
1574 size_t count, loff_t *ppos)
1576 struct pcmcia_bus_socket *s;
1577 user_info_t *user;
1578 int ret;
1580 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1582 if (count < 4)
1583 return -EINVAL;
1585 user = file->private_data;
1586 if (CHECK_USER(user))
1587 return -EIO;
1589 s = user->socket;
1590 if (s->state & DS_SOCKET_DEAD)
1591 return -EIO;
1593 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1594 if (ret == 0)
1595 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1597 return ret;
1598 } /* ds_read */
1600 /*====================================================================*/
1602 static ssize_t ds_write(struct file *file, const char __user *buf,
1603 size_t count, loff_t *ppos)
1605 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1607 if (count != 4)
1608 return -EINVAL;
1609 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1610 return -EBADF;
1612 return -EIO;
1613 } /* ds_write */
1615 /*====================================================================*/
1617 /* No kernel lock - fine */
1618 static u_int ds_poll(struct file *file, poll_table *wait)
1620 struct pcmcia_bus_socket *s;
1621 user_info_t *user;
1623 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1625 user = file->private_data;
1626 if (CHECK_USER(user))
1627 return POLLERR;
1628 s = user->socket;
1630 * We don't check for a dead socket here since that
1631 * will send cardmgr into an endless spin.
1633 poll_wait(file, &s->queue, wait);
1634 if (!queue_empty(user))
1635 return POLLIN | POLLRDNORM;
1636 return 0;
1637 } /* ds_poll */
1639 /*====================================================================*/
1641 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1643 static int ds_ioctl(struct inode * inode, struct file * file,
1644 u_int cmd, u_long arg)
1646 struct pcmcia_bus_socket *s;
1647 void __user *uarg = (char __user *)arg;
1648 u_int size;
1649 int ret, err;
1650 ds_ioctl_arg_t *buf;
1651 user_info_t *user;
1653 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1655 user = file->private_data;
1656 if (CHECK_USER(user))
1657 return -EIO;
1659 s = user->socket;
1660 if (s->state & DS_SOCKET_DEAD)
1661 return -EIO;
1663 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1664 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1666 /* Permission check */
1667 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1668 return -EPERM;
1670 if (cmd & IOC_IN) {
1671 if (!access_ok(VERIFY_READ, uarg, size)) {
1672 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1673 return -EFAULT;
1676 if (cmd & IOC_OUT) {
1677 if (!access_ok(VERIFY_WRITE, uarg, size)) {
1678 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1679 return -EFAULT;
1682 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1683 if (!buf)
1684 return -ENOMEM;
1686 err = ret = 0;
1688 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1690 switch (cmd) {
1691 case DS_ADJUST_RESOURCE_INFO:
1692 ret = pcmcia_adjust_resource_info(&buf->adjust);
1693 break;
1694 case DS_GET_CARD_SERVICES_INFO:
1695 ret = pcmcia_get_card_services_info(&buf->servinfo);
1696 break;
1697 case DS_GET_CONFIGURATION_INFO:
1698 if (buf->config.Function &&
1699 (buf->config.Function >= s->parent->functions))
1700 ret = CS_BAD_ARGS;
1701 else
1702 ret = pccard_get_configuration_info(s->parent,
1703 buf->config.Function, &buf->config);
1704 break;
1705 case DS_GET_FIRST_TUPLE:
1706 down(&s->parent->skt_sem);
1707 pcmcia_validate_mem(s->parent);
1708 up(&s->parent->skt_sem);
1709 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1710 break;
1711 case DS_GET_NEXT_TUPLE:
1712 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1713 break;
1714 case DS_GET_TUPLE_DATA:
1715 buf->tuple.TupleData = buf->tuple_parse.data;
1716 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1717 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1718 break;
1719 case DS_PARSE_TUPLE:
1720 buf->tuple.TupleData = buf->tuple_parse.data;
1721 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1722 break;
1723 case DS_RESET_CARD:
1724 ret = pccard_reset_card(s->parent);
1725 break;
1726 case DS_GET_STATUS:
1727 if (buf->status.Function &&
1728 (buf->status.Function >= s->parent->functions))
1729 ret = CS_BAD_ARGS;
1730 else
1731 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1732 break;
1733 case DS_VALIDATE_CIS:
1734 down(&s->parent->skt_sem);
1735 pcmcia_validate_mem(s->parent);
1736 up(&s->parent->skt_sem);
1737 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1738 break;
1739 case DS_SUSPEND_CARD:
1740 ret = pcmcia_suspend_card(s->parent);
1741 break;
1742 case DS_RESUME_CARD:
1743 ret = pcmcia_resume_card(s->parent);
1744 break;
1745 case DS_EJECT_CARD:
1746 err = pcmcia_eject_card(s->parent);
1747 break;
1748 case DS_INSERT_CARD:
1749 err = pcmcia_insert_card(s->parent);
1750 break;
1751 case DS_ACCESS_CONFIGURATION_REGISTER:
1752 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1753 err = -EPERM;
1754 goto free_out;
1756 if (buf->conf_reg.Function &&
1757 (buf->conf_reg.Function >= s->parent->functions))
1758 ret = CS_BAD_ARGS;
1759 else
1760 ret = pccard_access_configuration_register(s->parent,
1761 buf->conf_reg.Function, &buf->conf_reg);
1762 break;
1763 case DS_GET_FIRST_REGION:
1764 case DS_GET_NEXT_REGION:
1765 case DS_BIND_MTD:
1766 if (!capable(CAP_SYS_ADMIN)) {
1767 err = -EPERM;
1768 goto free_out;
1769 } else {
1770 static int printed = 0;
1771 if (!printed) {
1772 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1773 printk(KERN_WARNING "MTD handling any more.\n");
1774 printed++;
1777 err = -EINVAL;
1778 goto free_out;
1779 break;
1780 case DS_GET_FIRST_WINDOW:
1781 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1782 &buf->win_info.window);
1783 break;
1784 case DS_GET_NEXT_WINDOW:
1785 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1786 buf->win_info.handle->index + 1, &buf->win_info.window);
1787 break;
1788 case DS_GET_MEM_PAGE:
1789 ret = pcmcia_get_mem_page(buf->win_info.handle,
1790 &buf->win_info.map);
1791 break;
1792 case DS_REPLACE_CIS:
1793 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1794 break;
1795 case DS_BIND_REQUEST:
1796 if (!capable(CAP_SYS_ADMIN)) {
1797 err = -EPERM;
1798 goto free_out;
1800 err = bind_request(s, &buf->bind_info);
1801 break;
1802 case DS_GET_DEVICE_INFO:
1803 err = get_device_info(s, &buf->bind_info, 1);
1804 break;
1805 case DS_GET_NEXT_DEVICE:
1806 err = get_device_info(s, &buf->bind_info, 0);
1807 break;
1808 case DS_UNBIND_REQUEST:
1809 err = 0;
1810 break;
1811 default:
1812 err = -EINVAL;
1815 if ((err == 0) && (ret != CS_SUCCESS)) {
1816 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1817 switch (ret) {
1818 case CS_BAD_SOCKET: case CS_NO_CARD:
1819 err = -ENODEV; break;
1820 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1821 case CS_BAD_TUPLE:
1822 err = -EINVAL; break;
1823 case CS_IN_USE:
1824 err = -EBUSY; break;
1825 case CS_OUT_OF_RESOURCE:
1826 err = -ENOSPC; break;
1827 case CS_NO_MORE_ITEMS:
1828 err = -ENODATA; break;
1829 case CS_UNSUPPORTED_FUNCTION:
1830 err = -ENOSYS; break;
1831 default:
1832 err = -EIO; break;
1836 if (cmd & IOC_OUT) {
1837 if (__copy_to_user(uarg, (char *)buf, size))
1838 err = -EFAULT;
1841 free_out:
1842 kfree(buf);
1843 return err;
1844 } /* ds_ioctl */
1846 /*====================================================================*/
1848 static struct file_operations ds_fops = {
1849 .owner = THIS_MODULE,
1850 .open = ds_open,
1851 .release = ds_release,
1852 .ioctl = ds_ioctl,
1853 .read = ds_read,
1854 .write = ds_write,
1855 .poll = ds_poll,
1858 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1860 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1861 struct pcmcia_bus_socket *s;
1862 int ret;
1864 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1865 if(!s)
1866 return -ENOMEM;
1867 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1869 /* get reference to parent socket */
1870 s->parent = pcmcia_get_socket(socket);
1871 if (!s->parent) {
1872 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1873 kfree (s);
1874 return -ENODEV;
1877 kref_init(&s->refcount);
1880 * Ugly. But we want to wait for the socket threads to have started up.
1881 * We really should let the drivers themselves drive some of this..
1883 msleep(250);
1885 init_waitqueue_head(&s->queue);
1886 INIT_LIST_HEAD(&s->devices_list);
1887 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
1889 /* Set up hotline to Card Services */
1890 s->callback.owner = THIS_MODULE;
1891 s->callback.event = &ds_event;
1892 s->callback.requery = &pcmcia_bus_rescan;
1893 socket->pcmcia = s;
1895 ret = pccard_register_pcmcia(socket, &s->callback);
1896 if (ret) {
1897 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1898 pcmcia_put_bus_socket(s);
1899 socket->pcmcia = NULL;
1900 return (ret);
1903 return 0;
1907 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1909 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1911 if (!socket || !socket->pcmcia)
1912 return;
1914 pccard_register_pcmcia(socket, NULL);
1916 socket->pcmcia->state |= DS_SOCKET_DEAD;
1917 pcmcia_put_bus_socket(socket->pcmcia);
1918 socket->pcmcia = NULL;
1920 return;
1924 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1925 static struct class_interface pcmcia_bus_interface = {
1926 .class = &pcmcia_socket_class,
1927 .add = &pcmcia_bus_add_socket,
1928 .remove = &pcmcia_bus_remove_socket,
1932 static struct bus_type pcmcia_bus_type = {
1933 .name = "pcmcia",
1934 .hotplug = pcmcia_bus_hotplug,
1935 .match = pcmcia_bus_match,
1936 .dev_attrs = pcmcia_dev_attrs,
1940 static int __init init_pcmcia_bus(void)
1942 int i;
1944 spin_lock_init(&pcmcia_dev_list_lock);
1946 bus_register(&pcmcia_bus_type);
1947 class_interface_register(&pcmcia_bus_interface);
1949 /* Set up character device for user mode clients */
1950 i = register_chrdev(0, "pcmcia", &ds_fops);
1951 if (i < 0)
1952 printk(KERN_NOTICE "unable to find a free device # for "
1953 "Driver Services (error=%d)\n", i);
1954 else
1955 major_dev = i;
1957 #ifdef CONFIG_PROC_FS
1958 proc_pccard = proc_mkdir("pccard", proc_bus);
1959 if (proc_pccard)
1960 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1961 #endif
1963 return 0;
1965 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1966 * pcmcia_socket_class is already registered */
1969 static void __exit exit_pcmcia_bus(void)
1971 class_interface_unregister(&pcmcia_bus_interface);
1973 #ifdef CONFIG_PROC_FS
1974 if (proc_pccard) {
1975 remove_proc_entry("drivers", proc_pccard);
1976 remove_proc_entry("pccard", proc_bus);
1978 #endif
1979 if (major_dev != -1)
1980 unregister_chrdev(major_dev, "pcmcia");
1982 bus_unregister(&pcmcia_bus_type);
1984 module_exit(exit_pcmcia_bus);
1988 /* helpers for backwards-compatible functions */
1990 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1992 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1993 if (s && s->pcmcia)
1994 return s->pcmcia;
1995 else
1996 return NULL;
1999 /* backwards-compatible accessing of driver --- by name! */
2001 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
2003 struct device_driver *drv;
2004 struct pcmcia_driver *p_drv;
2006 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
2007 if (!drv)
2008 return NULL;
2010 p_drv = container_of(drv, struct pcmcia_driver, drv);
2012 return (p_drv);
2015 MODULE_ALIAS("ds");