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>
39 #include <asm/atomic.h>
41 #define IN_CARD_SERVICES
42 #include <pcmcia/version.h>
43 #include <pcmcia/cs_types.h>
44 #include <pcmcia/cs.h>
45 #include <pcmcia/bulkmem.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ds.h>
48 #include <pcmcia/ss.h>
50 #include "cs_internal.h"
52 /*====================================================================*/
54 /* Module parameters */
56 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
57 MODULE_DESCRIPTION("PCMCIA Driver Services");
58 MODULE_LICENSE("GPL");
63 module_param_named(pc_debug, ds_pc_debug, int, 0644);
65 #define ds_dbg(lvl, fmt, arg...) do { \
66 if (ds_pc_debug > (lvl)) \
67 printk(KERN_DEBUG "ds: " fmt , ## arg); \
70 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
73 /*====================================================================*/
75 /* Device user information */
77 #define USER_MAGIC 0x7ea4
78 #define CHECK_USER(u) \
79 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
80 typedef struct user_info_t {
82 int event_head, event_tail;
83 event_t event[MAX_EVENTS];
84 struct user_info_t *next;
85 struct pcmcia_bus_socket *socket;
88 /* Socket state information */
89 struct pcmcia_bus_socket {
91 struct pcmcia_callback callback;
94 wait_queue_head_t queue;
95 struct pcmcia_socket *parent;
97 /* the PCMCIA devices connected to this socket (normally one, more
98 * for multifunction devices: */
99 struct list_head devices_list;
100 u8 device_count; /* the number of devices, used
101 * only internally and subject
102 * to incorrectness and change */
104 static spinlock_t pcmcia_dev_list_lock;
106 #define DS_SOCKET_PRESENT 0x01
107 #define DS_SOCKET_BUSY 0x02
108 #define DS_SOCKET_REMOVAL_PENDING 0x10
109 #define DS_SOCKET_DEAD 0x80
111 /*====================================================================*/
113 static int major_dev = -1;
115 static int unbind_request(struct pcmcia_bus_socket *s);
117 /*====================================================================*/
119 /* code which was in cs.c before */
121 /* String tables for error messages */
123 typedef struct lookup_t {
128 static const lookup_t error_table[] = {
129 { CS_SUCCESS, "Operation succeeded" },
130 { CS_BAD_ADAPTER, "Bad adapter" },
131 { CS_BAD_ATTRIBUTE, "Bad attribute", },
132 { CS_BAD_BASE, "Bad base address" },
133 { CS_BAD_EDC, "Bad EDC" },
134 { CS_BAD_IRQ, "Bad IRQ" },
135 { CS_BAD_OFFSET, "Bad offset" },
136 { CS_BAD_PAGE, "Bad page number" },
137 { CS_READ_FAILURE, "Read failure" },
138 { CS_BAD_SIZE, "Bad size" },
139 { CS_BAD_SOCKET, "Bad socket" },
140 { CS_BAD_TYPE, "Bad type" },
141 { CS_BAD_VCC, "Bad Vcc" },
142 { CS_BAD_VPP, "Bad Vpp" },
143 { CS_BAD_WINDOW, "Bad window" },
144 { CS_WRITE_FAILURE, "Write failure" },
145 { CS_NO_CARD, "No card present" },
146 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
147 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
148 { CS_BAD_SPEED, "Bad speed" },
149 { CS_BUSY, "Resource busy" },
150 { CS_GENERAL_FAILURE, "General failure" },
151 { CS_WRITE_PROTECTED, "Write protected" },
152 { CS_BAD_ARG_LENGTH, "Bad argument length" },
153 { CS_BAD_ARGS, "Bad arguments" },
154 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
155 { CS_IN_USE, "Resource in use" },
156 { CS_NO_MORE_ITEMS, "No more items" },
157 { CS_OUT_OF_RESOURCE, "Out of resource" },
158 { CS_BAD_HANDLE, "Bad handle" },
159 { CS_BAD_TUPLE, "Bad CIS tuple" }
163 static const lookup_t service_table[] = {
164 { AccessConfigurationRegister, "AccessConfigurationRegister" },
165 { AddSocketServices, "AddSocketServices" },
166 { AdjustResourceInfo, "AdjustResourceInfo" },
167 { CheckEraseQueue, "CheckEraseQueue" },
168 { CloseMemory, "CloseMemory" },
169 { DeregisterClient, "DeregisterClient" },
170 { DeregisterEraseQueue, "DeregisterEraseQueue" },
171 { GetCardServicesInfo, "GetCardServicesInfo" },
172 { GetClientInfo, "GetClientInfo" },
173 { GetConfigurationInfo, "GetConfigurationInfo" },
174 { GetEventMask, "GetEventMask" },
175 { GetFirstClient, "GetFirstClient" },
176 { GetFirstRegion, "GetFirstRegion" },
177 { GetFirstTuple, "GetFirstTuple" },
178 { GetNextClient, "GetNextClient" },
179 { GetNextRegion, "GetNextRegion" },
180 { GetNextTuple, "GetNextTuple" },
181 { GetStatus, "GetStatus" },
182 { GetTupleData, "GetTupleData" },
183 { MapMemPage, "MapMemPage" },
184 { ModifyConfiguration, "ModifyConfiguration" },
185 { ModifyWindow, "ModifyWindow" },
186 { OpenMemory, "OpenMemory" },
187 { ParseTuple, "ParseTuple" },
188 { ReadMemory, "ReadMemory" },
189 { RegisterClient, "RegisterClient" },
190 { RegisterEraseQueue, "RegisterEraseQueue" },
191 { RegisterMTD, "RegisterMTD" },
192 { ReleaseConfiguration, "ReleaseConfiguration" },
193 { ReleaseIO, "ReleaseIO" },
194 { ReleaseIRQ, "ReleaseIRQ" },
195 { ReleaseWindow, "ReleaseWindow" },
196 { RequestConfiguration, "RequestConfiguration" },
197 { RequestIO, "RequestIO" },
198 { RequestIRQ, "RequestIRQ" },
199 { RequestSocketMask, "RequestSocketMask" },
200 { RequestWindow, "RequestWindow" },
201 { ResetCard, "ResetCard" },
202 { SetEventMask, "SetEventMask" },
203 { ValidateCIS, "ValidateCIS" },
204 { WriteMemory, "WriteMemory" },
205 { BindDevice, "BindDevice" },
206 { BindMTD, "BindMTD" },
207 { ReportError, "ReportError" },
208 { SuspendCard, "SuspendCard" },
209 { ResumeCard, "ResumeCard" },
210 { EjectCard, "EjectCard" },
211 { InsertCard, "InsertCard" },
212 { ReplaceCIS, "ReplaceCIS" }
216 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
221 if (CHECK_HANDLE(handle))
224 struct pcmcia_device *p_dev = handle_to_pdev(handle);
225 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
228 for (i = 0; i < ARRAY_SIZE(service_table); i++)
229 if (service_table[i].key == err->func)
231 if (i < ARRAY_SIZE(service_table))
232 serv = service_table[i].msg;
234 serv = "Unknown service number";
236 for (i = 0; i < ARRAY_SIZE(error_table); i++)
237 if (error_table[i].key == err->retcode)
239 if (i < ARRAY_SIZE(error_table))
240 printk("%s: %s\n", serv, error_table[i].msg);
242 printk("%s: Unknown error code %#x\n", serv, err->retcode);
246 EXPORT_SYMBOL(pcmcia_report_error);
248 /* end of code which was in cs.c before */
250 /*======================================================================*/
252 void cs_error(client_handle_t handle, int func, int ret)
254 error_info_t err = { func, ret };
255 pcmcia_report_error(handle, &err);
257 EXPORT_SYMBOL(cs_error);
259 /*======================================================================*/
261 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
262 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
264 static void pcmcia_release_bus_socket(struct kref *refcount)
266 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
267 pcmcia_put_socket(s->parent);
271 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
273 kref_put(&s->refcount, pcmcia_release_bus_socket);
276 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
278 kref_get(&s->refcount);
283 * pcmcia_register_driver - register a PCMCIA driver with the bus core
285 * Registers a PCMCIA driver with the PCMCIA bus core.
287 static int pcmcia_device_probe(struct device *dev);
288 static int pcmcia_device_remove(struct device * dev);
290 int pcmcia_register_driver(struct pcmcia_driver *driver)
295 /* initialize common fields */
296 driver->drv.bus = &pcmcia_bus_type;
297 driver->drv.owner = driver->owner;
298 driver->drv.probe = pcmcia_device_probe;
299 driver->drv.remove = pcmcia_device_remove;
301 return driver_register(&driver->drv);
303 EXPORT_SYMBOL(pcmcia_register_driver);
306 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
308 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
310 driver_unregister(&driver->drv);
312 EXPORT_SYMBOL(pcmcia_unregister_driver);
314 #ifdef CONFIG_PROC_FS
315 static struct proc_dir_entry *proc_pccard = NULL;
317 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
320 struct pcmcia_driver *p_drv = container_of(driver,
321 struct pcmcia_driver, drv);
323 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
324 #ifdef CONFIG_MODULE_UNLOAD
325 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
335 static int proc_read_drivers(char *buf, char **start, off_t pos,
336 int count, int *eof, void *data)
340 bus_for_each_drv(&pcmcia_bus_type, NULL,
341 (void *) &p, proc_read_drivers_callback);
347 /* pcmcia_device handling */
349 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
351 struct device *tmp_dev;
352 tmp_dev = get_device(&p_dev->dev);
355 return to_pcmcia_dev(tmp_dev);
358 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
361 put_device(&p_dev->dev);
364 static void pcmcia_release_dev(struct device *dev)
366 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
367 ds_dbg(1, "releasing dev %p\n", p_dev);
368 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
373 static int pcmcia_device_probe(struct device * dev)
375 struct pcmcia_device *p_dev;
376 struct pcmcia_driver *p_drv;
379 dev = get_device(dev);
383 p_dev = to_pcmcia_dev(dev);
384 p_drv = to_pcmcia_drv(dev->driver);
386 if (!try_module_get(p_drv->owner)) {
392 p_dev->instance = p_drv->attach();
393 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
394 printk(KERN_NOTICE "ds: unable to create instance "
395 "of '%s'!\n", p_drv->drv.name);
401 module_put(p_drv->owner);
403 if ((ret) || !(p_drv->attach))
409 static int pcmcia_device_remove(struct device * dev)
411 struct pcmcia_device *p_dev;
412 struct pcmcia_driver *p_drv;
414 /* detach the "instance" */
415 p_dev = to_pcmcia_dev(dev);
416 p_drv = to_pcmcia_drv(dev->driver);
419 if ((p_drv->detach) && (p_dev->instance)) {
420 p_drv->detach(p_dev->instance);
421 /* from pcmcia_probe_device */
422 put_device(&p_dev->dev);
424 module_put(p_drv->owner);
433 * pcmcia_device_query -- determine information about a pcmcia device
435 static int pcmcia_device_query(struct pcmcia_device *p_dev)
437 cistpl_manfid_t manf_id;
438 cistpl_funcid_t func_id;
439 cistpl_vers_1_t vers1;
442 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
443 CISTPL_MANFID, &manf_id)) {
444 p_dev->manf_id = manf_id.manf;
445 p_dev->card_id = manf_id.card;
446 p_dev->has_manf_id = 1;
447 p_dev->has_card_id = 1;
450 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
451 CISTPL_FUNCID, &func_id)) {
452 p_dev->func_id = func_id.func;
453 p_dev->has_func_id = 1;
455 /* rule of thumb: cards with no FUNCID, but with
456 * common memory device geometry information, are
457 * probably memory cards (from pcmcia-cs) */
458 cistpl_device_geo_t devgeo;
459 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
460 CISTPL_DEVICE_GEO, &devgeo)) {
461 ds_dbg(0, "mem device geometry probably means "
463 p_dev->func_id = CISTPL_FUNCID_MEMORY;
464 p_dev->has_func_id = 1;
468 if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
470 for (i=0; i < vers1.ns; i++) {
474 tmp = vers1.str + vers1.ofs[i];
476 length = strlen(tmp) + 1;
477 if ((length < 3) || (length > 255))
480 p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
482 if (!p_dev->prod_id[i])
485 p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
494 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
495 * Serializes pcmcia_device_add; will most likely be removed in future.
497 * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
498 * won't work, this doesn't matter much at the moment: the driver core doesn't
501 static DECLARE_MUTEX(device_add_lock);
503 static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
505 struct pcmcia_device *p_dev;
508 s = pcmcia_get_bus_socket(s);
512 down(&device_add_lock);
514 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
517 memset(p_dev, 0, sizeof(struct pcmcia_device));
519 p_dev->socket = s->parent;
520 p_dev->device_no = (s->device_count++);
521 p_dev->func = function;
523 p_dev->dev.bus = &pcmcia_bus_type;
524 p_dev->dev.parent = s->parent->dev.dev;
525 p_dev->dev.release = pcmcia_release_dev;
526 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
529 p_dev->client.client_magic = CLIENT_MAGIC;
530 p_dev->client.Socket = s->parent;
531 p_dev->client.Function = function;
532 p_dev->client.state = CLIENT_UNBOUND;
534 /* Add to the list in pcmcia_bus_socket */
535 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
536 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
537 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
539 if (device_register(&p_dev->dev)) {
540 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
541 list_del(&p_dev->socket_device_list);
542 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
547 up(&device_add_lock);
555 up(&device_add_lock);
556 pcmcia_put_bus_socket(s);
562 static int pcmcia_card_add(struct pcmcia_socket *s)
565 cistpl_longlink_mfc_t mfc;
566 unsigned int no_funcs, i;
569 if (!(s->resource_setup_done))
570 return -EAGAIN; /* try again, but later... */
572 pcmcia_validate_mem(s);
573 ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
574 if (ret || !cisinfo.Chains) {
575 ds_dbg(0, "invalid CIS or invalid resources\n");
579 if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
584 /* this doesn't handle multifunction devices on one pcmcia function
586 for (i=0; i < no_funcs; i++)
587 pcmcia_device_add(s->pcmcia, i);
593 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
594 struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
595 struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
597 /* matching by cardmgr */
598 if (p_dev->cardmgr == p_drv)
604 /************************ per-device sysfs output ***************************/
606 #define pcmcia_device_attr(field, test, format) \
607 static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
609 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
610 return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
613 #define pcmcia_device_stringattr(name, field) \
614 static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
616 struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
617 return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
620 pcmcia_device_attr(func, socket, "0x%02x\n");
621 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
622 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
623 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
624 pcmcia_device_stringattr(prod_id1, prod_id[0]);
625 pcmcia_device_stringattr(prod_id2, prod_id[1]);
626 pcmcia_device_stringattr(prod_id3, prod_id[2]);
627 pcmcia_device_stringattr(prod_id4, prod_id[3]);
629 static struct device_attribute pcmcia_dev_attrs[] = {
630 __ATTR(function, 0444, func_show, NULL),
642 /*======================================================================
644 These manage a ring buffer of events pending for one user process
646 ======================================================================*/
648 static int queue_empty(user_info_t *user)
650 return (user->event_head == user->event_tail);
653 static event_t get_queued_event(user_info_t *user)
655 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
656 return user->event[user->event_tail];
659 static void queue_event(user_info_t *user, event_t event)
661 user->event_head = (user->event_head+1) % MAX_EVENTS;
662 if (user->event_head == user->event_tail)
663 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
664 user->event[user->event_head] = event;
667 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
670 for (user = s->user; user; user = user->next)
671 queue_event(user, event);
672 wake_up_interruptible(&s->queue);
676 /*======================================================================
678 The card status event handler.
680 ======================================================================*/
682 struct send_event_data {
683 struct pcmcia_socket *skt;
688 static int send_event_callback(struct device *dev, void * _data)
690 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
691 struct send_event_data *data = _data;
693 /* we get called for all sockets, but may only pass the event
694 * for drivers _on the affected socket_ */
695 if (p_dev->socket != data->skt)
698 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
701 if (p_dev->client.EventMask & data->event)
702 return EVENT(&p_dev->client, data->event, data->priority);
707 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
710 struct send_event_data private;
711 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
717 private.event = event;
718 private.priority = priority;
720 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
722 pcmcia_put_bus_socket(skt);
727 /* Normally, the event is passed to individual drivers after
728 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
729 * is inversed to maintain historic compatibility.
732 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
734 struct pcmcia_bus_socket *s = skt->pcmcia;
737 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
742 case CS_EVENT_CARD_REMOVAL:
743 s->state &= ~DS_SOCKET_PRESENT;
744 send_event(skt, event, priority);
746 handle_event(s, event);
749 case CS_EVENT_CARD_INSERTION:
750 s->state |= DS_SOCKET_PRESENT;
751 pcmcia_card_add(skt);
752 handle_event(s, event);
755 case CS_EVENT_EJECTION_REQUEST:
756 ret = send_event(skt, event, priority);
760 handle_event(s, event);
761 send_event(skt, event, priority);
769 /*======================================================================
771 bind_request() and bind_device() are merged by now. Register_client()
772 is called right at the end of bind_request(), during the driver's
773 ->attach() call. Individual descriptions:
775 bind_request() connects a socket to a particular client driver.
776 It looks up the specified device ID in the list of registered
777 drivers, binds it to the socket, and tries to create an instance
778 of the device. unbind_request() deletes a driver instance.
780 Bind_device() associates a device driver with a particular socket.
781 It is normally called by Driver Services after it has identified
782 a newly inserted card. An instance of that driver will then be
783 eligible to register as a client of this socket.
785 Register_client() uses the dev_info_t handle to match the
786 caller with a socket. The driver must have already been bound
787 to a socket with bind_device() -- in fact, bind_device()
788 allocates the client structure that will be used.
790 ======================================================================*/
792 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
794 struct pcmcia_driver *p_drv;
795 struct pcmcia_device *p_dev;
799 s = pcmcia_get_bus_socket(s);
803 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
804 (char *)bind_info->dev_info);
806 p_drv = get_pcmcia_driver(&bind_info->dev_info);
812 if (!try_module_get(p_drv->owner)) {
817 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
818 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
819 if (p_dev->func == bind_info->function) {
820 if ((p_dev->dev.driver == &p_drv->drv)) {
821 if (p_dev->cardmgr) {
822 /* if there's already a device
823 * registered, and it was registered
824 * by userspace before, we need to
825 * return the "instance". */
826 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
827 bind_info->instance = p_dev->instance;
831 /* the correct driver managed to bind
832 * itself magically to the correct
834 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
835 p_dev->cardmgr = p_drv;
839 } else if (!p_dev->dev.driver) {
840 /* there's already a device available where
841 * no device has been bound to yet. So we don't
842 * need to register a device! */
843 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
848 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
850 p_dev = pcmcia_device_add(s, bind_info->function);
857 p_dev->cardmgr = p_drv;
859 pcmcia_device_query(p_dev);
862 * Prevent this racing with a card insertion.
864 down(&s->parent->skt_sem);
865 bus_rescan_devices(&pcmcia_bus_type);
866 up(&s->parent->skt_sem);
868 /* check whether the driver indeed matched. I don't care if this
869 * is racy or not, because it can only happen on cardmgr access
872 if (!(p_dev->dev.driver == &p_drv->drv))
873 p_dev->cardmgr = NULL;
876 module_put(p_drv->owner);
878 put_driver(&p_drv->drv);
880 pcmcia_put_bus_socket(s);
886 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
888 client_t *client = NULL;
889 struct pcmcia_socket *s;
890 struct pcmcia_bus_socket *skt = NULL;
891 struct pcmcia_device *p_dev = NULL;
893 /* Look for unbound client with matching dev_info */
894 down_read(&pcmcia_socket_list_rwsem);
895 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
898 if (s->state & SOCKET_CARDBUS)
904 skt = pcmcia_get_bus_socket(skt);
907 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
908 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
909 struct pcmcia_driver *p_drv;
910 p_dev = pcmcia_get_dev(p_dev);
913 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
914 (!p_dev->dev.driver)) {
915 pcmcia_put_dev(p_dev);
918 p_drv = to_pcmcia_drv(p_dev->dev.driver);
919 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
920 client = &p_dev->client;
921 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
924 pcmcia_put_dev(p_dev);
926 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
927 pcmcia_put_bus_socket(skt);
930 up_read(&pcmcia_socket_list_rwsem);
931 if (!p_dev || !client)
934 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
937 client->state &= ~CLIENT_UNBOUND;
939 client->EventMask = req->EventMask;
940 client->event_handler = req->event_handler;
941 client->event_callback_args = req->event_callback_args;
942 client->event_callback_args.client_handle = client;
944 if (s->state & SOCKET_CARDBUS)
945 client->state |= CLIENT_CARDBUS;
947 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
948 (client->Function != BIND_FN_ALL)) {
949 cistpl_longlink_mfc_t mfc;
950 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
952 s->functions = mfc.nfn;
955 s->config = kmalloc(sizeof(config_t) * s->functions,
958 goto out_no_resource;
959 memset(s->config, 0, sizeof(config_t) * s->functions);
962 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
963 client, p_dev->dev.bus_id);
964 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
965 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
967 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
968 if (client->EventMask & CS_EVENT_CARD_INSERTION)
969 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
975 pcmcia_put_dev(p_dev);
976 return CS_OUT_OF_RESOURCE;
977 } /* register_client */
978 EXPORT_SYMBOL(pcmcia_register_client);
981 /*====================================================================*/
983 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
985 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
988 struct pcmcia_device *p_dev;
992 #ifdef CONFIG_CARDBUS
994 * Some unbelievably ugly code to associate the PCI cardbus
995 * device and its driver with the PCMCIA "bind" information.
1000 bus = pcmcia_lookup_bus(s->parent);
1002 struct list_head *list;
1003 struct pci_dev *dev = NULL;
1005 list = bus->devices.next;
1006 while (list != &bus->devices) {
1007 struct pci_dev *pdev = pci_dev_b(list);
1015 /* Try to handle "next" here some way? */
1017 if (dev && dev->driver) {
1018 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1019 bind_info->major = 0;
1020 bind_info->minor = 0;
1021 bind_info->next = NULL;
1028 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1029 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1030 if (p_dev->func == bind_info->function) {
1031 p_dev = pcmcia_get_dev(p_dev);
1037 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1041 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1043 if ((!p_dev->instance) ||
1044 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1050 node = p_dev->instance->dev;
1052 for (node = p_dev->instance->dev; node; node = node->next)
1053 if (node == bind_info->next)
1060 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1061 bind_info->major = node->major;
1062 bind_info->minor = node->minor;
1063 bind_info->next = node->next;
1066 pcmcia_put_dev(p_dev);
1068 } /* get_device_info */
1070 /*====================================================================*/
1072 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1073 * drivers have been called with EVENT_CARD_REMOVAL before.
1075 static int unbind_request(struct pcmcia_bus_socket *s)
1077 struct pcmcia_device *p_dev;
1078 unsigned long flags;
1080 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1082 s->device_count = 0;
1085 /* unregister all pcmcia_devices registered with this socket*/
1086 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1087 if (list_empty(&s->devices_list)) {
1088 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1091 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1092 list_del(&p_dev->socket_device_list);
1093 p_dev->client.state |= CLIENT_STALE;
1094 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1096 device_unregister(&p_dev->dev);
1100 } /* unbind_request */
1102 int pcmcia_deregister_client(client_handle_t handle)
1104 struct pcmcia_socket *s;
1106 struct pcmcia_device *p_dev = handle_to_pdev(handle);
1108 if (CHECK_HANDLE(handle))
1109 return CS_BAD_HANDLE;
1112 ds_dbg(1, "deregister_client(%p)\n", handle);
1114 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1116 for (i = 0; i < MAX_WIN; i++)
1117 if (handle->state & CLIENT_WIN_REQ(i))
1120 if (handle->state & CLIENT_STALE) {
1121 handle->client_magic = 0;
1122 handle->state &= ~CLIENT_STALE;
1123 pcmcia_put_dev(p_dev);
1125 handle->state = CLIENT_UNBOUND;
1126 handle->event_handler = NULL;
1131 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1133 } /* deregister_client */
1134 EXPORT_SYMBOL(pcmcia_deregister_client);
1137 /*======================================================================
1139 The user-mode PC Card device interface
1141 ======================================================================*/
1143 static int ds_open(struct inode *inode, struct file *file)
1145 socket_t i = iminor(inode);
1146 struct pcmcia_bus_socket *s;
1149 ds_dbg(0, "ds_open(socket %d)\n", i);
1151 s = get_socket_info_by_nr(i);
1154 s = pcmcia_get_bus_socket(s);
1158 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1159 if (s->state & DS_SOCKET_BUSY) {
1160 pcmcia_put_bus_socket(s);
1164 s->state |= DS_SOCKET_BUSY;
1167 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1169 pcmcia_put_bus_socket(s);
1172 user->event_tail = user->event_head = 0;
1173 user->next = s->user;
1174 user->user_magic = USER_MAGIC;
1177 file->private_data = user;
1179 if (s->state & DS_SOCKET_PRESENT)
1180 queue_event(user, CS_EVENT_CARD_INSERTION);
1184 /*====================================================================*/
1186 static int ds_release(struct inode *inode, struct file *file)
1188 struct pcmcia_bus_socket *s;
1189 user_info_t *user, **link;
1191 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1193 user = file->private_data;
1194 if (CHECK_USER(user))
1199 /* Unlink user data structure */
1200 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1201 s->state &= ~DS_SOCKET_BUSY;
1203 file->private_data = NULL;
1204 for (link = &s->user; *link; link = &(*link)->next)
1205 if (*link == user) break;
1209 user->user_magic = 0;
1211 pcmcia_put_bus_socket(s);
1216 /*====================================================================*/
1218 static ssize_t ds_read(struct file *file, char __user *buf,
1219 size_t count, loff_t *ppos)
1221 struct pcmcia_bus_socket *s;
1225 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1230 user = file->private_data;
1231 if (CHECK_USER(user))
1235 if (s->state & DS_SOCKET_DEAD)
1238 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1240 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1245 /*====================================================================*/
1247 static ssize_t ds_write(struct file *file, const char __user *buf,
1248 size_t count, loff_t *ppos)
1250 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1254 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1260 /*====================================================================*/
1262 /* No kernel lock - fine */
1263 static u_int ds_poll(struct file *file, poll_table *wait)
1265 struct pcmcia_bus_socket *s;
1268 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1270 user = file->private_data;
1271 if (CHECK_USER(user))
1275 * We don't check for a dead socket here since that
1276 * will send cardmgr into an endless spin.
1278 poll_wait(file, &s->queue, wait);
1279 if (!queue_empty(user))
1280 return POLLIN | POLLRDNORM;
1284 /*====================================================================*/
1286 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1288 static int ds_ioctl(struct inode * inode, struct file * file,
1289 u_int cmd, u_long arg)
1291 struct pcmcia_bus_socket *s;
1292 void __user *uarg = (char __user *)arg;
1295 ds_ioctl_arg_t *buf;
1298 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1300 user = file->private_data;
1301 if (CHECK_USER(user))
1305 if (s->state & DS_SOCKET_DEAD)
1308 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1309 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1311 /* Permission check */
1312 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1316 if (!access_ok(VERIFY_READ, uarg, size)) {
1317 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1321 if (cmd & IOC_OUT) {
1322 if (!access_ok(VERIFY_WRITE, uarg, size)) {
1323 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1327 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1333 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1336 case DS_ADJUST_RESOURCE_INFO:
1337 ret = pcmcia_adjust_resource_info(&buf->adjust);
1339 case DS_GET_CARD_SERVICES_INFO:
1340 ret = pcmcia_get_card_services_info(&buf->servinfo);
1342 case DS_GET_CONFIGURATION_INFO:
1343 if (buf->config.Function &&
1344 (buf->config.Function >= s->parent->functions))
1347 ret = pccard_get_configuration_info(s->parent,
1348 buf->config.Function, &buf->config);
1350 case DS_GET_FIRST_TUPLE:
1351 down(&s->parent->skt_sem);
1352 pcmcia_validate_mem(s->parent);
1353 up(&s->parent->skt_sem);
1354 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1356 case DS_GET_NEXT_TUPLE:
1357 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1359 case DS_GET_TUPLE_DATA:
1360 buf->tuple.TupleData = buf->tuple_parse.data;
1361 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1362 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1364 case DS_PARSE_TUPLE:
1365 buf->tuple.TupleData = buf->tuple_parse.data;
1366 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1369 ret = pccard_reset_card(s->parent);
1372 if (buf->status.Function &&
1373 (buf->status.Function >= s->parent->functions))
1376 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1378 case DS_VALIDATE_CIS:
1379 down(&s->parent->skt_sem);
1380 pcmcia_validate_mem(s->parent);
1381 up(&s->parent->skt_sem);
1382 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1384 case DS_SUSPEND_CARD:
1385 ret = pcmcia_suspend_card(s->parent);
1387 case DS_RESUME_CARD:
1388 ret = pcmcia_resume_card(s->parent);
1391 err = pcmcia_eject_card(s->parent);
1393 case DS_INSERT_CARD:
1394 err = pcmcia_insert_card(s->parent);
1396 case DS_ACCESS_CONFIGURATION_REGISTER:
1397 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1401 if (buf->conf_reg.Function &&
1402 (buf->conf_reg.Function >= s->parent->functions))
1405 ret = pccard_access_configuration_register(s->parent,
1406 buf->conf_reg.Function, &buf->conf_reg);
1408 case DS_GET_FIRST_REGION:
1409 case DS_GET_NEXT_REGION:
1411 if (!capable(CAP_SYS_ADMIN)) {
1415 static int printed = 0;
1417 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1418 printk(KERN_WARNING "MTD handling any more.\n");
1425 case DS_GET_FIRST_WINDOW:
1426 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1427 &buf->win_info.window);
1429 case DS_GET_NEXT_WINDOW:
1430 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1431 buf->win_info.handle->index + 1, &buf->win_info.window);
1433 case DS_GET_MEM_PAGE:
1434 ret = pcmcia_get_mem_page(buf->win_info.handle,
1435 &buf->win_info.map);
1437 case DS_REPLACE_CIS:
1438 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1440 case DS_BIND_REQUEST:
1441 if (!capable(CAP_SYS_ADMIN)) {
1445 err = bind_request(s, &buf->bind_info);
1447 case DS_GET_DEVICE_INFO:
1448 err = get_device_info(s, &buf->bind_info, 1);
1450 case DS_GET_NEXT_DEVICE:
1451 err = get_device_info(s, &buf->bind_info, 0);
1453 case DS_UNBIND_REQUEST:
1460 if ((err == 0) && (ret != CS_SUCCESS)) {
1461 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1463 case CS_BAD_SOCKET: case CS_NO_CARD:
1464 err = -ENODEV; break;
1465 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1467 err = -EINVAL; break;
1469 err = -EBUSY; break;
1470 case CS_OUT_OF_RESOURCE:
1471 err = -ENOSPC; break;
1472 case CS_NO_MORE_ITEMS:
1473 err = -ENODATA; break;
1474 case CS_UNSUPPORTED_FUNCTION:
1475 err = -ENOSYS; break;
1481 if (cmd & IOC_OUT) {
1482 if (__copy_to_user(uarg, (char *)buf, size))
1491 /*====================================================================*/
1493 static struct file_operations ds_fops = {
1494 .owner = THIS_MODULE,
1496 .release = ds_release,
1503 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1505 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1506 struct pcmcia_bus_socket *s;
1509 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1512 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1514 /* get reference to parent socket */
1515 s->parent = pcmcia_get_socket(socket);
1517 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1522 kref_init(&s->refcount);
1525 * Ugly. But we want to wait for the socket threads to have started up.
1526 * We really should let the drivers themselves drive some of this..
1530 init_waitqueue_head(&s->queue);
1531 INIT_LIST_HEAD(&s->devices_list);
1533 /* Set up hotline to Card Services */
1534 s->callback.owner = THIS_MODULE;
1535 s->callback.event = &ds_event;
1536 s->callback.resources_done = &pcmcia_card_add;
1539 ret = pccard_register_pcmcia(socket, &s->callback);
1541 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1542 pcmcia_put_bus_socket(s);
1543 socket->pcmcia = NULL;
1551 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1553 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1555 if (!socket || !socket->pcmcia)
1558 pccard_register_pcmcia(socket, NULL);
1560 socket->pcmcia->state |= DS_SOCKET_DEAD;
1561 pcmcia_put_bus_socket(socket->pcmcia);
1562 socket->pcmcia = NULL;
1568 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1569 static struct class_interface pcmcia_bus_interface = {
1570 .class = &pcmcia_socket_class,
1571 .add = &pcmcia_bus_add_socket,
1572 .remove = &pcmcia_bus_remove_socket,
1576 struct bus_type pcmcia_bus_type = {
1578 .match = pcmcia_bus_match,
1579 .dev_attrs = pcmcia_dev_attrs,
1581 EXPORT_SYMBOL(pcmcia_bus_type);
1584 static int __init init_pcmcia_bus(void)
1588 spin_lock_init(&pcmcia_dev_list_lock);
1590 bus_register(&pcmcia_bus_type);
1591 class_interface_register(&pcmcia_bus_interface);
1593 /* Set up character device for user mode clients */
1594 i = register_chrdev(0, "pcmcia", &ds_fops);
1596 printk(KERN_NOTICE "unable to find a free device # for "
1597 "Driver Services (error=%d)\n", i);
1601 #ifdef CONFIG_PROC_FS
1602 proc_pccard = proc_mkdir("pccard", proc_bus);
1604 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1609 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1610 * pcmcia_socket_class is already registered */
1613 static void __exit exit_pcmcia_bus(void)
1615 class_interface_unregister(&pcmcia_bus_interface);
1617 #ifdef CONFIG_PROC_FS
1619 remove_proc_entry("drivers", proc_pccard);
1620 remove_proc_entry("pccard", proc_bus);
1623 if (major_dev != -1)
1624 unregister_chrdev(major_dev, "pcmcia");
1626 bus_unregister(&pcmcia_bus_type);
1628 module_exit(exit_pcmcia_bus);
1632 /* helpers for backwards-compatible functions */
1634 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1636 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1643 /* backwards-compatible accessing of driver --- by name! */
1645 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1647 struct device_driver *drv;
1648 struct pcmcia_driver *p_drv;
1650 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1654 p_drv = container_of(drv, struct pcmcia_driver, drv);