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 int req_pending, req_result;
95 wait_queue_head_t queue, request;
96 struct pcmcia_socket *parent;
98 /* the PCMCIA devices connected to this socket (normally one, more
99 * for multifunction devices: */
100 struct list_head devices_list;
101 u8 device_count; /* the number of devices, used
102 * only internally and subject
103 * to incorrectness and change */
105 static spinlock_t pcmcia_dev_list_lock;
107 #define DS_SOCKET_PRESENT 0x01
108 #define DS_SOCKET_BUSY 0x02
109 #define DS_SOCKET_REMOVAL_PENDING 0x10
110 #define DS_SOCKET_DEAD 0x80
112 /*====================================================================*/
114 static int major_dev = -1;
116 static int unbind_request(struct pcmcia_bus_socket *s);
118 /*====================================================================*/
120 /* code which was in cs.c before */
122 /* String tables for error messages */
124 typedef struct lookup_t {
129 static const lookup_t error_table[] = {
130 { CS_SUCCESS, "Operation succeeded" },
131 { CS_BAD_ADAPTER, "Bad adapter" },
132 { CS_BAD_ATTRIBUTE, "Bad attribute", },
133 { CS_BAD_BASE, "Bad base address" },
134 { CS_BAD_EDC, "Bad EDC" },
135 { CS_BAD_IRQ, "Bad IRQ" },
136 { CS_BAD_OFFSET, "Bad offset" },
137 { CS_BAD_PAGE, "Bad page number" },
138 { CS_READ_FAILURE, "Read failure" },
139 { CS_BAD_SIZE, "Bad size" },
140 { CS_BAD_SOCKET, "Bad socket" },
141 { CS_BAD_TYPE, "Bad type" },
142 { CS_BAD_VCC, "Bad Vcc" },
143 { CS_BAD_VPP, "Bad Vpp" },
144 { CS_BAD_WINDOW, "Bad window" },
145 { CS_WRITE_FAILURE, "Write failure" },
146 { CS_NO_CARD, "No card present" },
147 { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
148 { CS_UNSUPPORTED_MODE, "Unsupported mode" },
149 { CS_BAD_SPEED, "Bad speed" },
150 { CS_BUSY, "Resource busy" },
151 { CS_GENERAL_FAILURE, "General failure" },
152 { CS_WRITE_PROTECTED, "Write protected" },
153 { CS_BAD_ARG_LENGTH, "Bad argument length" },
154 { CS_BAD_ARGS, "Bad arguments" },
155 { CS_CONFIGURATION_LOCKED, "Configuration locked" },
156 { CS_IN_USE, "Resource in use" },
157 { CS_NO_MORE_ITEMS, "No more items" },
158 { CS_OUT_OF_RESOURCE, "Out of resource" },
159 { CS_BAD_HANDLE, "Bad handle" },
160 { CS_BAD_TUPLE, "Bad CIS tuple" }
164 static const lookup_t service_table[] = {
165 { AccessConfigurationRegister, "AccessConfigurationRegister" },
166 { AddSocketServices, "AddSocketServices" },
167 { AdjustResourceInfo, "AdjustResourceInfo" },
168 { CheckEraseQueue, "CheckEraseQueue" },
169 { CloseMemory, "CloseMemory" },
170 { DeregisterClient, "DeregisterClient" },
171 { DeregisterEraseQueue, "DeregisterEraseQueue" },
172 { GetCardServicesInfo, "GetCardServicesInfo" },
173 { GetClientInfo, "GetClientInfo" },
174 { GetConfigurationInfo, "GetConfigurationInfo" },
175 { GetEventMask, "GetEventMask" },
176 { GetFirstClient, "GetFirstClient" },
177 { GetFirstRegion, "GetFirstRegion" },
178 { GetFirstTuple, "GetFirstTuple" },
179 { GetNextClient, "GetNextClient" },
180 { GetNextRegion, "GetNextRegion" },
181 { GetNextTuple, "GetNextTuple" },
182 { GetStatus, "GetStatus" },
183 { GetTupleData, "GetTupleData" },
184 { MapMemPage, "MapMemPage" },
185 { ModifyConfiguration, "ModifyConfiguration" },
186 { ModifyWindow, "ModifyWindow" },
187 { OpenMemory, "OpenMemory" },
188 { ParseTuple, "ParseTuple" },
189 { ReadMemory, "ReadMemory" },
190 { RegisterClient, "RegisterClient" },
191 { RegisterEraseQueue, "RegisterEraseQueue" },
192 { RegisterMTD, "RegisterMTD" },
193 { ReleaseConfiguration, "ReleaseConfiguration" },
194 { ReleaseIO, "ReleaseIO" },
195 { ReleaseIRQ, "ReleaseIRQ" },
196 { ReleaseWindow, "ReleaseWindow" },
197 { RequestConfiguration, "RequestConfiguration" },
198 { RequestIO, "RequestIO" },
199 { RequestIRQ, "RequestIRQ" },
200 { RequestSocketMask, "RequestSocketMask" },
201 { RequestWindow, "RequestWindow" },
202 { ResetCard, "ResetCard" },
203 { SetEventMask, "SetEventMask" },
204 { ValidateCIS, "ValidateCIS" },
205 { WriteMemory, "WriteMemory" },
206 { BindDevice, "BindDevice" },
207 { BindMTD, "BindMTD" },
208 { ReportError, "ReportError" },
209 { SuspendCard, "SuspendCard" },
210 { ResumeCard, "ResumeCard" },
211 { EjectCard, "EjectCard" },
212 { InsertCard, "InsertCard" },
213 { ReplaceCIS, "ReplaceCIS" }
217 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
222 if (CHECK_HANDLE(handle))
225 struct pcmcia_device *p_dev = handle_to_pdev(handle);
226 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
229 for (i = 0; i < ARRAY_SIZE(service_table); i++)
230 if (service_table[i].key == err->func)
232 if (i < ARRAY_SIZE(service_table))
233 serv = service_table[i].msg;
235 serv = "Unknown service number";
237 for (i = 0; i < ARRAY_SIZE(error_table); i++)
238 if (error_table[i].key == err->retcode)
240 if (i < ARRAY_SIZE(error_table))
241 printk("%s: %s\n", serv, error_table[i].msg);
243 printk("%s: Unknown error code %#x\n", serv, err->retcode);
247 EXPORT_SYMBOL(pcmcia_report_error);
249 /* end of code which was in cs.c before */
251 /*======================================================================*/
253 void cs_error(client_handle_t handle, int func, int ret)
255 error_info_t err = { func, ret };
256 pcmcia_report_error(handle, &err);
258 EXPORT_SYMBOL(cs_error);
260 /*======================================================================*/
262 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
263 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
265 static void pcmcia_release_bus_socket(struct kref *refcount)
267 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
268 pcmcia_put_socket(s->parent);
272 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
274 kref_put(&s->refcount, pcmcia_release_bus_socket);
277 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
279 kref_get(&s->refcount);
284 * pcmcia_register_driver - register a PCMCIA driver with the bus core
286 * Registers a PCMCIA driver with the PCMCIA bus core.
288 int pcmcia_register_driver(struct pcmcia_driver *driver)
293 driver->drv.bus = &pcmcia_bus_type;
294 driver->drv.owner = driver->owner;
296 return driver_register(&driver->drv);
298 EXPORT_SYMBOL(pcmcia_register_driver);
301 * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
303 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
305 driver_unregister(&driver->drv);
307 EXPORT_SYMBOL(pcmcia_unregister_driver);
309 #ifdef CONFIG_PROC_FS
310 static struct proc_dir_entry *proc_pccard = NULL;
312 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
315 struct pcmcia_driver *p_drv = container_of(driver,
316 struct pcmcia_driver, drv);
318 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
319 #ifdef CONFIG_MODULE_UNLOAD
320 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
330 static int proc_read_drivers(char *buf, char **start, off_t pos,
331 int count, int *eof, void *data)
335 bus_for_each_drv(&pcmcia_bus_type, NULL,
336 (void *) &p, proc_read_drivers_callback);
342 /* pcmcia_device handling */
344 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
346 struct device *tmp_dev;
347 tmp_dev = get_device(&p_dev->dev);
350 return to_pcmcia_dev(tmp_dev);
353 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
355 put_device(&p_dev->dev);
358 static void pcmcia_release_dev(struct device *dev)
360 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
361 ds_dbg(1, "releasing dev %p\n", p_dev);
362 pcmcia_put_bus_socket(p_dev->socket->pcmcia);
367 /*======================================================================
369 These manage a ring buffer of events pending for one user process
371 ======================================================================*/
373 static int queue_empty(user_info_t *user)
375 return (user->event_head == user->event_tail);
378 static event_t get_queued_event(user_info_t *user)
380 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
381 return user->event[user->event_tail];
384 static void queue_event(user_info_t *user, event_t event)
386 user->event_head = (user->event_head+1) % MAX_EVENTS;
387 if (user->event_head == user->event_tail)
388 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
389 user->event[user->event_head] = event;
392 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
395 for (user = s->user; user; user = user->next)
396 queue_event(user, event);
397 wake_up_interruptible(&s->queue);
400 static int handle_request(struct pcmcia_bus_socket *s, event_t event)
402 if (s->req_pending != 0)
404 if (s->state & DS_SOCKET_BUSY)
406 handle_event(s, event);
407 if (wait_event_interruptible(s->request, s->req_pending <= 0))
409 if (s->state & DS_SOCKET_BUSY)
410 return s->req_result;
414 /*======================================================================
416 The card status event handler.
418 ======================================================================*/
420 struct send_event_data {
421 struct pcmcia_socket *skt;
426 static int send_event_callback(struct device *dev, void * _data)
428 struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
429 struct send_event_data *data = _data;
431 /* we get called for all sockets, but may only pass the event
432 * for drivers _on the affected socket_ */
433 if (p_dev->socket != data->skt)
436 if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
439 if (p_dev->client.EventMask & data->event)
440 return EVENT(&p_dev->client, data->event, data->priority);
445 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
448 struct send_event_data private;
449 struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
455 private.event = event;
456 private.priority = priority;
458 ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
460 pcmcia_put_bus_socket(skt);
465 /* Normally, the event is passed to individual drivers after
466 * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
467 * is inversed to maintain historic compatibility.
470 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
472 struct pcmcia_bus_socket *s = skt->pcmcia;
475 ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
480 case CS_EVENT_CARD_REMOVAL:
481 s->state &= ~DS_SOCKET_PRESENT;
482 send_event(skt, event, priority);
484 handle_event(s, event);
487 case CS_EVENT_CARD_INSERTION:
488 s->state |= DS_SOCKET_PRESENT;
489 handle_event(s, event);
490 send_event(skt, event, priority);
493 case CS_EVENT_EJECTION_REQUEST:
494 ret = handle_request(s, event);
497 ret = send_event(skt, event, priority);
501 handle_event(s, event);
502 send_event(skt, event, priority);
510 /*======================================================================
512 bind_request() and bind_device() are merged by now. Register_client()
513 is called right at the end of bind_request(), during the driver's
514 ->attach() call. Individual descriptions:
516 bind_request() connects a socket to a particular client driver.
517 It looks up the specified device ID in the list of registered
518 drivers, binds it to the socket, and tries to create an instance
519 of the device. unbind_request() deletes a driver instance.
521 Bind_device() associates a device driver with a particular socket.
522 It is normally called by Driver Services after it has identified
523 a newly inserted card. An instance of that driver will then be
524 eligible to register as a client of this socket.
526 Register_client() uses the dev_info_t handle to match the
527 caller with a socket. The driver must have already been bound
528 to a socket with bind_device() -- in fact, bind_device()
529 allocates the client structure that will be used.
531 ======================================================================*/
533 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
535 struct pcmcia_driver *p_drv;
536 struct pcmcia_device *p_dev, *tmp_dev;
540 s = pcmcia_get_bus_socket(s);
544 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
545 (char *)bind_info->dev_info);
547 p_drv = get_pcmcia_driver(&bind_info->dev_info);
553 if (!try_module_get(p_drv->owner)) {
558 /* Currently, the userspace pcmcia cardmgr detects pcmcia devices.
559 * Here this information is translated into a kernel
560 * struct pcmcia_device.
563 p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
568 memset(p_dev, 0, sizeof(struct pcmcia_device));
570 p_dev->socket = s->parent;
571 p_dev->device_no = (s->device_count++);
572 p_dev->func = bind_info->function;
574 p_dev->dev.bus = &pcmcia_bus_type;
575 p_dev->dev.parent = s->parent->dev.dev;
576 p_dev->dev.release = pcmcia_release_dev;
577 sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
578 p_dev->dev.driver = &p_drv->drv;
581 p_dev->client.client_magic = CLIENT_MAGIC;
582 p_dev->client.Socket = s->parent;
583 p_dev->client.Function = bind_info->function;
584 p_dev->client.state = CLIENT_UNBOUND;
586 ret = device_register(&p_dev->dev);
592 /* Add to the list in pcmcia_bus_socket, but only if no device
593 * with the same func _and_ driver exists */
594 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
595 list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list) {
596 if ((tmp_dev->func == bind_info->function) &&
597 (tmp_dev->dev.driver == p_dev->dev.driver)){
598 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
599 bind_info->instance = tmp_dev->instance;
604 list_add_tail(&p_dev->socket_device_list, &s->devices_list);
605 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
608 p_dev->instance = p_drv->attach();
609 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
610 printk(KERN_NOTICE "ds: unable to create instance "
611 "of '%s'!\n", (char *)bind_info->dev_info);
617 put_driver(&p_drv->drv);
622 device_unregister(&p_dev->dev);
623 module_put(p_drv->owner);
624 put_driver(&p_drv->drv);
628 module_put(p_drv->owner);
630 put_driver(&p_drv->drv);
632 pcmcia_put_bus_socket(s);
636 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
638 client_t *client = NULL;
639 struct pcmcia_socket *s;
640 struct pcmcia_bus_socket *skt = NULL;
641 struct pcmcia_device *p_dev = NULL;
643 /* Look for unbound client with matching dev_info */
644 down_read(&pcmcia_socket_list_rwsem);
645 list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
648 if (s->state & SOCKET_CARDBUS)
654 skt = pcmcia_get_bus_socket(skt);
657 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
658 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
659 struct pcmcia_driver *p_drv;
660 p_dev = pcmcia_get_dev(p_dev);
663 if (!(p_dev->client.state & CLIENT_UNBOUND) ||
664 (!p_dev->dev.driver)) {
665 pcmcia_put_dev(p_dev);
668 p_drv = to_pcmcia_drv(p_dev->dev.driver);
669 if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
670 client = &p_dev->client;
671 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
674 pcmcia_put_dev(p_dev);
676 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
677 pcmcia_put_bus_socket(skt);
680 up_read(&pcmcia_socket_list_rwsem);
681 if (!p_dev || !client)
684 pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
687 * Prevent this racing with a card insertion.
691 client->state &= ~CLIENT_UNBOUND;
693 client->EventMask = req->EventMask;
694 client->event_handler = req->event_handler;
695 client->event_callback_args = req->event_callback_args;
696 client->event_callback_args.client_handle = client;
698 if (s->state & SOCKET_CARDBUS)
699 client->state |= CLIENT_CARDBUS;
701 if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
702 (client->Function != BIND_FN_ALL)) {
703 cistpl_longlink_mfc_t mfc;
704 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
706 s->functions = mfc.nfn;
709 s->config = kmalloc(sizeof(config_t) * s->functions,
712 goto out_no_resource;
713 memset(s->config, 0, sizeof(config_t) * s->functions);
716 ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
717 client, p_dev->dev.bus_id);
718 if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
719 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
721 if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
722 if (client->EventMask & CS_EVENT_CARD_INSERTION)
723 EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
731 pcmcia_put_dev(p_dev);
732 return CS_OUT_OF_RESOURCE;
733 } /* register_client */
734 EXPORT_SYMBOL(pcmcia_register_client);
737 /*====================================================================*/
739 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
741 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
744 struct pcmcia_device *p_dev;
748 #ifdef CONFIG_CARDBUS
750 * Some unbelievably ugly code to associate the PCI cardbus
751 * device and its driver with the PCMCIA "bind" information.
756 bus = pcmcia_lookup_bus(s->parent);
758 struct list_head *list;
759 struct pci_dev *dev = NULL;
761 list = bus->devices.next;
762 while (list != &bus->devices) {
763 struct pci_dev *pdev = pci_dev_b(list);
771 /* Try to handle "next" here some way? */
773 if (dev && dev->driver) {
774 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
775 bind_info->major = 0;
776 bind_info->minor = 0;
777 bind_info->next = NULL;
784 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
785 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
786 if (p_dev->func == bind_info->function) {
787 p_dev = pcmcia_get_dev(p_dev);
793 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
797 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
799 if ((!p_dev->instance) ||
800 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
806 node = p_dev->instance->dev;
808 for (node = p_dev->instance->dev; node; node = node->next)
809 if (node == bind_info->next)
816 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
817 bind_info->major = node->major;
818 bind_info->minor = node->minor;
819 bind_info->next = node->next;
822 pcmcia_put_dev(p_dev);
824 } /* get_device_info */
826 /*====================================================================*/
828 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
829 * drivers have been called with EVENT_CARD_REMOVAL before.
831 static int unbind_request(struct pcmcia_bus_socket *s)
833 struct pcmcia_device *p_dev;
834 struct pcmcia_driver *p_drv;
837 ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
842 /* unregister all pcmcia_devices registered with this socket*/
843 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
844 if (list_empty(&s->devices_list)) {
845 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
848 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
849 list_del(&p_dev->socket_device_list);
850 p_dev->client.state |= CLIENT_STALE;
851 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
853 /* detach the "instance" */
854 p_drv = to_pcmcia_drv(p_dev->dev.driver);
856 if ((p_drv->detach) && (p_dev->instance))
857 p_drv->detach(p_dev->instance);
858 module_put(p_drv->owner);
861 device_unregister(&p_dev->dev);
865 } /* unbind_request */
867 int pcmcia_deregister_client(client_handle_t handle)
869 struct pcmcia_socket *s;
871 struct pcmcia_device *p_dev = handle_to_pdev(handle);
873 if (CHECK_HANDLE(handle))
874 return CS_BAD_HANDLE;
877 ds_dbg(1, "deregister_client(%p)\n", handle);
879 if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
881 for (i = 0; i < MAX_WIN; i++)
882 if (handle->state & CLIENT_WIN_REQ(i))
885 if (handle->state & CLIENT_STALE) {
886 handle->client_magic = 0;
887 handle->state &= ~CLIENT_STALE;
888 pcmcia_put_dev(p_dev);
890 handle->state = CLIENT_UNBOUND;
891 handle->event_handler = NULL;
896 printk(KERN_WARNING "ds: deregister_client was called too early.\n");
898 } /* deregister_client */
899 EXPORT_SYMBOL(pcmcia_deregister_client);
902 /*======================================================================
904 The user-mode PC Card device interface
906 ======================================================================*/
908 static int ds_open(struct inode *inode, struct file *file)
910 socket_t i = iminor(inode);
911 struct pcmcia_bus_socket *s;
914 ds_dbg(0, "ds_open(socket %d)\n", i);
916 s = get_socket_info_by_nr(i);
919 s = pcmcia_get_bus_socket(s);
923 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
924 if (s->state & DS_SOCKET_BUSY) {
925 pcmcia_put_bus_socket(s);
929 s->state |= DS_SOCKET_BUSY;
932 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
934 pcmcia_put_bus_socket(s);
937 user->event_tail = user->event_head = 0;
938 user->next = s->user;
939 user->user_magic = USER_MAGIC;
942 file->private_data = user;
944 if (s->state & DS_SOCKET_PRESENT)
945 queue_event(user, CS_EVENT_CARD_INSERTION);
949 /*====================================================================*/
951 static int ds_release(struct inode *inode, struct file *file)
953 struct pcmcia_bus_socket *s;
954 user_info_t *user, **link;
956 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
958 user = file->private_data;
959 if (CHECK_USER(user))
964 /* Unlink user data structure */
965 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
966 s->state &= ~DS_SOCKET_BUSY;
968 wake_up_interruptible(&s->request);
970 file->private_data = NULL;
971 for (link = &s->user; *link; link = &(*link)->next)
972 if (*link == user) break;
976 user->user_magic = 0;
978 pcmcia_put_bus_socket(s);
983 /*====================================================================*/
985 static ssize_t ds_read(struct file *file, char __user *buf,
986 size_t count, loff_t *ppos)
988 struct pcmcia_bus_socket *s;
992 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
997 user = file->private_data;
998 if (CHECK_USER(user))
1002 if (s->state & DS_SOCKET_DEAD)
1005 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1007 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1012 /*====================================================================*/
1014 static ssize_t ds_write(struct file *file, const char __user *buf,
1015 size_t count, loff_t *ppos)
1017 struct pcmcia_bus_socket *s;
1020 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1024 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1027 user = file->private_data;
1028 if (CHECK_USER(user))
1032 if (s->state & DS_SOCKET_DEAD)
1035 if (s->req_pending) {
1037 get_user(s->req_result, (int __user *)buf);
1038 if ((s->req_result != 0) || (s->req_pending == 0))
1039 wake_up_interruptible(&s->request);
1046 /*====================================================================*/
1048 /* No kernel lock - fine */
1049 static u_int ds_poll(struct file *file, poll_table *wait)
1051 struct pcmcia_bus_socket *s;
1054 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1056 user = file->private_data;
1057 if (CHECK_USER(user))
1061 * We don't check for a dead socket here since that
1062 * will send cardmgr into an endless spin.
1064 poll_wait(file, &s->queue, wait);
1065 if (!queue_empty(user))
1066 return POLLIN | POLLRDNORM;
1070 /*====================================================================*/
1072 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1074 static int ds_ioctl(struct inode * inode, struct file * file,
1075 u_int cmd, u_long arg)
1077 struct pcmcia_bus_socket *s;
1078 void __user *uarg = (char __user *)arg;
1081 ds_ioctl_arg_t *buf;
1084 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1086 user = file->private_data;
1087 if (CHECK_USER(user))
1091 if (s->state & DS_SOCKET_DEAD)
1094 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1095 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1097 /* Permission check */
1098 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1102 err = verify_area(VERIFY_READ, uarg, size);
1104 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", err);
1108 if (cmd & IOC_OUT) {
1109 err = verify_area(VERIFY_WRITE, uarg, size);
1111 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", err);
1115 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1121 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1124 case DS_ADJUST_RESOURCE_INFO:
1125 ret = pcmcia_adjust_resource_info(&buf->adjust);
1127 case DS_GET_CARD_SERVICES_INFO:
1128 ret = pcmcia_get_card_services_info(&buf->servinfo);
1130 case DS_GET_CONFIGURATION_INFO:
1131 if (buf->config.Function &&
1132 (buf->config.Function >= s->parent->functions))
1135 ret = pccard_get_configuration_info(s->parent,
1136 buf->config.Function, &buf->config);
1138 case DS_GET_FIRST_TUPLE:
1139 pcmcia_validate_mem(s->parent);
1140 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1142 case DS_GET_NEXT_TUPLE:
1143 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1145 case DS_GET_TUPLE_DATA:
1146 buf->tuple.TupleData = buf->tuple_parse.data;
1147 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1148 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1150 case DS_PARSE_TUPLE:
1151 buf->tuple.TupleData = buf->tuple_parse.data;
1152 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1155 ret = pccard_reset_card(s->parent);
1158 if (buf->status.Function &&
1159 (buf->status.Function >= s->parent->functions))
1162 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1164 case DS_VALIDATE_CIS:
1165 pcmcia_validate_mem(s->parent);
1166 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1168 case DS_SUSPEND_CARD:
1169 ret = pcmcia_suspend_card(s->parent);
1171 case DS_RESUME_CARD:
1172 ret = pcmcia_resume_card(s->parent);
1175 err = pcmcia_eject_card(s->parent);
1177 case DS_INSERT_CARD:
1178 err = pcmcia_insert_card(s->parent);
1180 case DS_ACCESS_CONFIGURATION_REGISTER:
1181 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1185 if (buf->conf_reg.Function &&
1186 (buf->conf_reg.Function >= s->parent->functions))
1189 ret = pccard_access_configuration_register(s->parent,
1190 buf->conf_reg.Function, &buf->conf_reg);
1192 case DS_GET_FIRST_REGION:
1193 case DS_GET_NEXT_REGION:
1195 if (!capable(CAP_SYS_ADMIN)) {
1199 static int printed = 0;
1201 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1202 printk(KERN_WARNING "MTD handling any more.\n");
1209 case DS_GET_FIRST_WINDOW:
1210 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1211 &buf->win_info.window);
1213 case DS_GET_NEXT_WINDOW:
1214 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1215 buf->win_info.handle->index + 1, &buf->win_info.window);
1217 case DS_GET_MEM_PAGE:
1218 ret = pcmcia_get_mem_page(buf->win_info.handle,
1219 &buf->win_info.map);
1221 case DS_REPLACE_CIS:
1222 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1224 case DS_BIND_REQUEST:
1225 if (!capable(CAP_SYS_ADMIN)) {
1229 err = bind_request(s, &buf->bind_info);
1231 case DS_GET_DEVICE_INFO:
1232 err = get_device_info(s, &buf->bind_info, 1);
1234 case DS_GET_NEXT_DEVICE:
1235 err = get_device_info(s, &buf->bind_info, 0);
1237 case DS_UNBIND_REQUEST:
1244 if ((err == 0) && (ret != CS_SUCCESS)) {
1245 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1247 case CS_BAD_SOCKET: case CS_NO_CARD:
1248 err = -ENODEV; break;
1249 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1251 err = -EINVAL; break;
1253 err = -EBUSY; break;
1254 case CS_OUT_OF_RESOURCE:
1255 err = -ENOSPC; break;
1256 case CS_NO_MORE_ITEMS:
1257 err = -ENODATA; break;
1258 case CS_UNSUPPORTED_FUNCTION:
1259 err = -ENOSYS; break;
1265 if (cmd & IOC_OUT) {
1266 if (__copy_to_user(uarg, (char *)buf, size))
1275 /*====================================================================*/
1277 static struct file_operations ds_fops = {
1278 .owner = THIS_MODULE,
1280 .release = ds_release,
1287 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1289 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1290 struct pcmcia_bus_socket *s;
1293 s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1296 memset(s, 0, sizeof(struct pcmcia_bus_socket));
1298 /* get reference to parent socket */
1299 s->parent = pcmcia_get_socket(socket);
1301 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1306 kref_init(&s->refcount);
1309 * Ugly. But we want to wait for the socket threads to have started up.
1310 * We really should let the drivers themselves drive some of this..
1314 init_waitqueue_head(&s->queue);
1315 init_waitqueue_head(&s->request);
1316 INIT_LIST_HEAD(&s->devices_list);
1318 /* Set up hotline to Card Services */
1319 s->callback.owner = THIS_MODULE;
1320 s->callback.event = &ds_event;
1323 ret = pccard_register_pcmcia(socket, &s->callback);
1325 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1326 pcmcia_put_bus_socket(s);
1327 socket->pcmcia = NULL;
1335 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1337 struct pcmcia_socket *socket = class_get_devdata(class_dev);
1339 if (!socket || !socket->pcmcia)
1342 pccard_register_pcmcia(socket, NULL);
1344 socket->pcmcia->state |= DS_SOCKET_DEAD;
1345 pcmcia_put_bus_socket(socket->pcmcia);
1346 socket->pcmcia = NULL;
1352 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1353 static struct class_interface pcmcia_bus_interface = {
1354 .class = &pcmcia_socket_class,
1355 .add = &pcmcia_bus_add_socket,
1356 .remove = &pcmcia_bus_remove_socket,
1360 struct bus_type pcmcia_bus_type = {
1363 EXPORT_SYMBOL(pcmcia_bus_type);
1366 static int __init init_pcmcia_bus(void)
1370 spin_lock_init(&pcmcia_dev_list_lock);
1372 bus_register(&pcmcia_bus_type);
1373 class_interface_register(&pcmcia_bus_interface);
1375 /* Set up character device for user mode clients */
1376 i = register_chrdev(0, "pcmcia", &ds_fops);
1378 printk(KERN_NOTICE "unable to find a free device # for "
1379 "Driver Services\n");
1383 #ifdef CONFIG_PROC_FS
1384 proc_pccard = proc_mkdir("pccard", proc_bus);
1386 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1391 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1392 * pcmcia_socket_class is already registered */
1395 static void __exit exit_pcmcia_bus(void)
1397 class_interface_unregister(&pcmcia_bus_interface);
1399 #ifdef CONFIG_PROC_FS
1401 remove_proc_entry("drivers", proc_pccard);
1402 remove_proc_entry("pccard", proc_bus);
1405 if (major_dev != -1)
1406 unregister_chrdev(major_dev, "pcmcia");
1408 bus_unregister(&pcmcia_bus_type);
1410 module_exit(exit_pcmcia_bus);
1414 /* helpers for backwards-compatible functions */
1416 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1418 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1425 /* backwards-compatible accessing of driver --- by name! */
1427 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1429 struct device_driver *drv;
1430 struct pcmcia_driver *p_drv;
1432 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1436 p_drv = container_of(drv, struct pcmcia_driver, drv);