patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / pcmcia / ds.c
1 /*======================================================================
2
3     PC Card Driver Services
4     
5     ds.c 1.112 2001/10/13 00:08:28
6     
7     The contents of this file are subject to the Mozilla Public
8     License Version 1.1 (the "License"); you may not use this file
9     except in compliance with the License. You may obtain a copy of
10     the License at http://www.mozilla.org/MPL/
11
12     Software distributed under the License is distributed on an "AS
13     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14     implied. See the License for the specific language governing
15     rights and limitations under the License.
16
17     The initial developer of the original code is David A. Hinds
18     <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
19     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
20
21     Alternatively, the contents of this file may be used under the
22     terms of the GNU General Public License version 2 (the "GPL"), in
23     which case the provisions of the GPL are applicable instead of the
24     above.  If you wish to allow the use of your version of this file
25     only under the terms of the GPL and not to allow others to use
26     your version of this file under the MPL, indicate your decision
27     by deleting the provisions above and replace them with the notice
28     and other provisions required by the GPL.  If you do not delete
29     the provisions above, a recipient may use your version of this
30     file under either the MPL or the GPL.
31     
32 ======================================================================*/
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/init.h>
38 #include <linux/kernel.h>
39 #include <linux/major.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/slab.h>
43 #include <linux/mm.h>
44 #include <linux/fcntl.h>
45 #include <linux/sched.h>
46 #include <linux/smp_lock.h>
47 #include <linux/timer.h>
48 #include <linux/ioctl.h>
49 #include <linux/proc_fs.h>
50 #include <linux/poll.h>
51 #include <linux/pci.h>
52 #include <linux/list.h>
53 #include <linux/workqueue.h>
54
55 #include <asm/atomic.h>
56
57 #define IN_CARD_SERVICES
58 #include <pcmcia/version.h>
59 #include <pcmcia/cs_types.h>
60 #include <pcmcia/cs.h>
61 #include <pcmcia/bulkmem.h>
62 #include <pcmcia/cistpl.h>
63 #include <pcmcia/ds.h>
64 #include <pcmcia/ss.h>
65
66 #include "cs_internal.h"
67
68 /*====================================================================*/
69
70 /* Module parameters */
71
72 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
73 MODULE_DESCRIPTION("PCMCIA Driver Services");
74 MODULE_LICENSE("Dual MPL/GPL");
75
76 #ifdef DEBUG
77 static int pc_debug;
78
79 module_param(pc_debug, int, 0644);
80
81 #define ds_dbg(lvl, fmt, arg...) do {                           \
82         if (pc_debug > (lvl))                                   \
83                 printk(KERN_DEBUG "ds: " fmt , ## arg);         \
84 } while (0)
85 #else
86 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
87 #endif
88
89 /*====================================================================*/
90
91 typedef struct socket_bind_t {
92     struct pcmcia_driver        *driver;
93     u_char              function;
94     dev_link_t          *instance;
95     struct socket_bind_t *next;
96 } socket_bind_t;
97
98 /* Device user information */
99 #define MAX_EVENTS      32
100 #define USER_MAGIC      0x7ea4
101 #define CHECK_USER(u) \
102     (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
103 typedef struct user_info_t {
104     u_int               user_magic;
105     int                 event_head, event_tail;
106     event_t             event[MAX_EVENTS];
107     struct user_info_t  *next;
108     struct pcmcia_bus_socket *socket;
109 } user_info_t;
110
111 /* Socket state information */
112 struct pcmcia_bus_socket {
113         atomic_t                refcount;
114         client_handle_t         handle;
115         int                     state;
116         user_info_t             *user;
117         int                     req_pending, req_result;
118         wait_queue_head_t       queue, request;
119         struct work_struct      removal;
120         socket_bind_t           *bind;
121         struct pcmcia_socket    *parent;
122 };
123
124 #define DS_SOCKET_PRESENT               0x01
125 #define DS_SOCKET_BUSY                  0x02
126 #define DS_SOCKET_REMOVAL_PENDING       0x10
127 #define DS_SOCKET_DEAD                  0x80
128
129 /*====================================================================*/
130
131 /* Device driver ID passed to Card Services */
132 static dev_info_t dev_info = "Driver Services";
133
134 static int major_dev = -1;
135
136 extern struct proc_dir_entry *proc_pccard;
137
138 /*====================================================================*/
139
140 /* code which was in cs.c before */
141
142 /*======================================================================
143
144     Bind_device() associates a device driver with a particular socket.
145     It is normally called by Driver Services after it has identified
146     a newly inserted card.  An instance of that driver will then be
147     eligible to register as a client of this socket.
148     
149 ======================================================================*/
150
151 static int pcmcia_bind_device(bind_req_t *req)
152 {
153         client_t *client;
154         struct pcmcia_socket *s;
155
156         s = req->Socket;
157         if (!s)
158                 return CS_BAD_SOCKET;
159
160         client = (client_t *) kmalloc(sizeof(client_t), GFP_KERNEL);
161         if (!client) 
162                 return CS_OUT_OF_RESOURCE;
163         memset(client, '\0', sizeof(client_t));
164         client->client_magic = CLIENT_MAGIC;
165         strlcpy(client->dev_info, (char *)req->dev_info, DEV_NAME_LEN);
166         client->Socket = s;
167         client->Function = req->Function;
168         client->state = CLIENT_UNBOUND;
169         client->erase_busy.next = &client->erase_busy;
170         client->erase_busy.prev = &client->erase_busy;
171         init_waitqueue_head(&client->mtd_req);
172         client->next = s->clients;
173         s->clients = client;
174         ds_dbg(1, "%s: bind_device(): client 0x%p, dev %s\n",
175                 cs_socket_name(client->Socket), client, client->dev_info);
176         return CS_SUCCESS;
177 } /* bind_device */
178
179
180 /*======================================================================
181
182     Bind_mtd() associates a device driver with a particular memory
183     region.  It is normally called by Driver Services after it has
184     identified a memory device type.  An instance of the corresponding
185     driver will then be able to register to control this region.
186     
187 ======================================================================*/
188
189 static int pcmcia_bind_mtd(mtd_bind_t *req)
190 {
191         struct pcmcia_socket *s;
192         memory_handle_t region;
193
194         s = req->Socket;
195         if (!s)
196                 return CS_BAD_SOCKET;
197     
198         if (req->Attributes & REGION_TYPE_AM)
199                 region = s->a_region;
200         else
201                 region = s->c_region;
202
203         while (region) {
204                 if (region->info.CardOffset == req->CardOffset) 
205                         break;
206                 region = region->info.next;
207         }
208         if (!region || (region->mtd != NULL))
209                 return CS_BAD_OFFSET;
210         strlcpy(region->dev_info, (char *)req->dev_info, DEV_NAME_LEN);
211
212         ds_dbg(1, "%s: bind_mtd: attr 0x%x, offset 0x%x, dev %s\n",
213               cs_socket_name(s), req->Attributes, req->CardOffset,
214               (char *)req->dev_info);
215         return CS_SUCCESS;
216 } /* bind_mtd */
217
218
219 /* String tables for error messages */
220
221 typedef struct lookup_t {
222     int key;
223     char *msg;
224 } lookup_t;
225
226 static const lookup_t error_table[] = {
227     { CS_SUCCESS,               "Operation succeeded" },
228     { CS_BAD_ADAPTER,           "Bad adapter" },
229     { CS_BAD_ATTRIBUTE,         "Bad attribute", },
230     { CS_BAD_BASE,              "Bad base address" },
231     { CS_BAD_EDC,               "Bad EDC" },
232     { CS_BAD_IRQ,               "Bad IRQ" },
233     { CS_BAD_OFFSET,            "Bad offset" },
234     { CS_BAD_PAGE,              "Bad page number" },
235     { CS_READ_FAILURE,          "Read failure" },
236     { CS_BAD_SIZE,              "Bad size" },
237     { CS_BAD_SOCKET,            "Bad socket" },
238     { CS_BAD_TYPE,              "Bad type" },
239     { CS_BAD_VCC,               "Bad Vcc" },
240     { CS_BAD_VPP,               "Bad Vpp" },
241     { CS_BAD_WINDOW,            "Bad window" },
242     { CS_WRITE_FAILURE,         "Write failure" },
243     { CS_NO_CARD,               "No card present" },
244     { CS_UNSUPPORTED_FUNCTION,  "Usupported function" },
245     { CS_UNSUPPORTED_MODE,      "Unsupported mode" },
246     { CS_BAD_SPEED,             "Bad speed" },
247     { CS_BUSY,                  "Resource busy" },
248     { CS_GENERAL_FAILURE,       "General failure" },
249     { CS_WRITE_PROTECTED,       "Write protected" },
250     { CS_BAD_ARG_LENGTH,        "Bad argument length" },
251     { CS_BAD_ARGS,              "Bad arguments" },
252     { CS_CONFIGURATION_LOCKED,  "Configuration locked" },
253     { CS_IN_USE,                "Resource in use" },
254     { CS_NO_MORE_ITEMS,         "No more items" },
255     { CS_OUT_OF_RESOURCE,       "Out of resource" },
256     { CS_BAD_HANDLE,            "Bad handle" },
257     { CS_BAD_TUPLE,             "Bad CIS tuple" }
258 };
259
260
261 static const lookup_t service_table[] = {
262     { AccessConfigurationRegister,      "AccessConfigurationRegister" },
263     { AddSocketServices,                "AddSocketServices" },
264     { AdjustResourceInfo,               "AdjustResourceInfo" },
265     { CheckEraseQueue,                  "CheckEraseQueue" },
266     { CloseMemory,                      "CloseMemory" },
267     { DeregisterClient,                 "DeregisterClient" },
268     { DeregisterEraseQueue,             "DeregisterEraseQueue" },
269     { GetCardServicesInfo,              "GetCardServicesInfo" },
270     { GetClientInfo,                    "GetClientInfo" },
271     { GetConfigurationInfo,             "GetConfigurationInfo" },
272     { GetEventMask,                     "GetEventMask" },
273     { GetFirstClient,                   "GetFirstClient" },
274     { GetFirstRegion,                   "GetFirstRegion" },
275     { GetFirstTuple,                    "GetFirstTuple" },
276     { GetNextClient,                    "GetNextClient" },
277     { GetNextRegion,                    "GetNextRegion" },
278     { GetNextTuple,                     "GetNextTuple" },
279     { GetStatus,                        "GetStatus" },
280     { GetTupleData,                     "GetTupleData" },
281     { MapMemPage,                       "MapMemPage" },
282     { ModifyConfiguration,              "ModifyConfiguration" },
283     { ModifyWindow,                     "ModifyWindow" },
284     { OpenMemory,                       "OpenMemory" },
285     { ParseTuple,                       "ParseTuple" },
286     { ReadMemory,                       "ReadMemory" },
287     { RegisterClient,                   "RegisterClient" },
288     { RegisterEraseQueue,               "RegisterEraseQueue" },
289     { RegisterMTD,                      "RegisterMTD" },
290     { ReleaseConfiguration,             "ReleaseConfiguration" },
291     { ReleaseIO,                        "ReleaseIO" },
292     { ReleaseIRQ,                       "ReleaseIRQ" },
293     { ReleaseWindow,                    "ReleaseWindow" },
294     { RequestConfiguration,             "RequestConfiguration" },
295     { RequestIO,                        "RequestIO" },
296     { RequestIRQ,                       "RequestIRQ" },
297     { RequestSocketMask,                "RequestSocketMask" },
298     { RequestWindow,                    "RequestWindow" },
299     { ResetCard,                        "ResetCard" },
300     { SetEventMask,                     "SetEventMask" },
301     { ValidateCIS,                      "ValidateCIS" },
302     { WriteMemory,                      "WriteMemory" },
303     { BindDevice,                       "BindDevice" },
304     { BindMTD,                          "BindMTD" },
305     { ReportError,                      "ReportError" },
306     { SuspendCard,                      "SuspendCard" },
307     { ResumeCard,                       "ResumeCard" },
308     { EjectCard,                        "EjectCard" },
309     { InsertCard,                       "InsertCard" },
310     { ReplaceCIS,                       "ReplaceCIS" }
311 };
312
313
314 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
315 {
316         int i;
317         char *serv;
318
319         if (CHECK_HANDLE(handle))
320                 printk(KERN_NOTICE);
321         else
322                 printk(KERN_NOTICE "%s: ", handle->dev_info);
323
324         for (i = 0; i < ARRAY_SIZE(service_table); i++)
325                 if (service_table[i].key == err->func)
326                         break;
327         if (i < ARRAY_SIZE(service_table))
328                 serv = service_table[i].msg;
329         else
330                 serv = "Unknown service number";
331
332         for (i = 0; i < ARRAY_SIZE(error_table); i++)
333                 if (error_table[i].key == err->retcode)
334                         break;
335         if (i < ARRAY_SIZE(error_table))
336                 printk("%s: %s\n", serv, error_table[i].msg);
337         else
338                 printk("%s: Unknown error code %#x\n", serv, err->retcode);
339
340         return CS_SUCCESS;
341 } /* report_error */
342 EXPORT_SYMBOL(pcmcia_report_error);
343
344 /* end of code which was in cs.c before */
345
346 /*======================================================================*/
347
348 void cs_error(client_handle_t handle, int func, int ret)
349 {
350         error_info_t err = { func, ret };
351         pcmcia_report_error(handle, &err);
352 }
353 EXPORT_SYMBOL(cs_error);
354
355 /*======================================================================*/
356
357 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
358 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
359
360 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
361 {
362         if (atomic_dec_and_test(&s->refcount))
363                 kfree(s);
364 }
365
366 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(int nr)
367 {
368         struct pcmcia_bus_socket *s;
369
370         s = get_socket_info_by_nr(nr);
371         if (s) {
372                 WARN_ON(atomic_read(&s->refcount) == 0);
373                 atomic_inc(&s->refcount);
374         }
375         return s;
376 }
377
378 /**
379  * pcmcia_register_driver - register a PCMCIA driver with the bus core
380  *
381  * Registers a PCMCIA driver with the PCMCIA bus core.
382  */
383 int pcmcia_register_driver(struct pcmcia_driver *driver)
384 {
385         if (!driver)
386                 return -EINVAL;
387
388         driver->use_count = 0;
389         driver->drv.bus = &pcmcia_bus_type;
390
391         return driver_register(&driver->drv);
392 }
393 EXPORT_SYMBOL(pcmcia_register_driver);
394
395 /**
396  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
397  */
398 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
399 {
400         driver_unregister(&driver->drv);
401 }
402 EXPORT_SYMBOL(pcmcia_unregister_driver);
403
404 #ifdef CONFIG_PROC_FS
405 static struct proc_dir_entry *proc_pccard = NULL;
406
407 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
408 {
409         char **p = d;
410         struct pcmcia_driver *p_dev = container_of(driver, 
411                                                    struct pcmcia_driver, drv);
412
413         *p += sprintf(*p, "%-24.24s 1 %d\n", driver->name, p_dev->use_count);
414         d = (void *) p;
415
416         return 0;
417 }
418
419 static int proc_read_drivers(char *buf, char **start, off_t pos,
420                              int count, int *eof, void *data)
421 {
422         char *p = buf;
423
424         bus_for_each_drv(&pcmcia_bus_type, NULL, 
425                          (void *) &p, proc_read_drivers_callback);
426
427         return (p - buf);
428 }
429 #endif
430
431 /*======================================================================
432
433     These manage a ring buffer of events pending for one user process
434     
435 ======================================================================*/
436
437 static int queue_empty(user_info_t *user)
438 {
439     return (user->event_head == user->event_tail);
440 }
441
442 static event_t get_queued_event(user_info_t *user)
443 {
444     user->event_tail = (user->event_tail+1) % MAX_EVENTS;
445     return user->event[user->event_tail];
446 }
447
448 static void queue_event(user_info_t *user, event_t event)
449 {
450     user->event_head = (user->event_head+1) % MAX_EVENTS;
451     if (user->event_head == user->event_tail)
452         user->event_tail = (user->event_tail+1) % MAX_EVENTS;
453     user->event[user->event_head] = event;
454 }
455
456 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
457 {
458     user_info_t *user;
459     for (user = s->user; user; user = user->next)
460         queue_event(user, event);
461     wake_up_interruptible(&s->queue);
462 }
463
464 static int handle_request(struct pcmcia_bus_socket *s, event_t event)
465 {
466     if (s->req_pending != 0)
467         return CS_IN_USE;
468     if (s->state & DS_SOCKET_BUSY)
469         s->req_pending = 1;
470     handle_event(s, event);
471     if (wait_event_interruptible(s->request, s->req_pending <= 0))
472         return CS_IN_USE;
473     if (s->state & DS_SOCKET_BUSY)
474         return s->req_result;
475     return CS_SUCCESS;
476 }
477
478 static void handle_removal(void *data)
479 {
480     struct pcmcia_bus_socket *s = data;
481     handle_event(s, CS_EVENT_CARD_REMOVAL);
482     s->state &= ~DS_SOCKET_REMOVAL_PENDING;
483 }
484
485 /*======================================================================
486
487     The card status event handler.
488     
489 ======================================================================*/
490
491 static int ds_event(event_t event, int priority,
492                     event_callback_args_t *args)
493 {
494     struct pcmcia_bus_socket *s;
495
496     ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
497           event, priority, args->client_handle);
498     s = args->client_data;
499     
500     switch (event) {
501         
502     case CS_EVENT_CARD_REMOVAL:
503         s->state &= ~DS_SOCKET_PRESENT;
504         if (!(s->state & DS_SOCKET_REMOVAL_PENDING)) {
505                 s->state |= DS_SOCKET_REMOVAL_PENDING;
506                 schedule_delayed_work(&s->removal,  HZ/10);
507         }
508         break;
509         
510     case CS_EVENT_CARD_INSERTION:
511         s->state |= DS_SOCKET_PRESENT;
512         handle_event(s, event);
513         break;
514
515     case CS_EVENT_EJECTION_REQUEST:
516         return handle_request(s, event);
517         break;
518         
519     default:
520         handle_event(s, event);
521         break;
522     }
523
524     return 0;
525 } /* ds_event */
526
527 /*======================================================================
528
529     bind_mtd() connects a memory region with an MTD client.
530     
531 ======================================================================*/
532
533 static int bind_mtd(struct pcmcia_bus_socket *bus_sock, mtd_info_t *mtd_info)
534 {
535     mtd_bind_t bind_req;
536     int ret;
537
538     bind_req.dev_info = &mtd_info->dev_info;
539     bind_req.Attributes = mtd_info->Attributes;
540     bind_req.Socket = bus_sock->parent;
541     bind_req.CardOffset = mtd_info->CardOffset;
542     ret = pcmcia_bind_mtd(&bind_req);
543     if (ret != CS_SUCCESS) {
544         cs_error(NULL, BindMTD, ret);
545         printk(KERN_NOTICE "ds: unable to bind MTD '%s' to socket %d"
546                " offset 0x%x\n",
547                (char *)bind_req.dev_info, bus_sock->parent->sock, bind_req.CardOffset);
548         return -ENODEV;
549     }
550     return 0;
551 } /* bind_mtd */
552
553 /*======================================================================
554
555     bind_request() connects a socket to a particular client driver.
556     It looks up the specified device ID in the list of registered
557     drivers, binds it to the socket, and tries to create an instance
558     of the device.  unbind_request() deletes a driver instance.
559     
560 ======================================================================*/
561
562 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
563 {
564     struct pcmcia_driver *driver;
565     socket_bind_t *b;
566     bind_req_t bind_req;
567     int ret;
568
569     if (!s)
570             return -EINVAL;
571
572     ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
573           (char *)bind_info->dev_info);
574     driver = get_pcmcia_driver(&bind_info->dev_info);
575     if (!driver)
576             return -EINVAL;
577
578     for (b = s->bind; b; b = b->next)
579         if ((driver == b->driver) &&
580             (bind_info->function == b->function))
581             break;
582     if (b != NULL) {
583         bind_info->instance = b->instance;
584         return -EBUSY;
585     }
586
587     if (!try_module_get(driver->owner))
588             return -EINVAL;
589
590     bind_req.Socket = s->parent;
591     bind_req.Function = bind_info->function;
592     bind_req.dev_info = (dev_info_t *) driver->drv.name;
593     ret = pcmcia_bind_device(&bind_req);
594     if (ret != CS_SUCCESS) {
595         cs_error(NULL, BindDevice, ret);
596         printk(KERN_NOTICE "ds: unable to bind '%s' to socket %d\n",
597                (char *)dev_info, s->parent->sock);
598         module_put(driver->owner);
599         return -ENODEV;
600     }
601
602     /* Add binding to list for this socket */
603     driver->use_count++;
604     b = kmalloc(sizeof(socket_bind_t), GFP_KERNEL);
605     if (!b) 
606     {
607         driver->use_count--;
608         module_put(driver->owner);
609         return -ENOMEM;    
610     }
611     b->driver = driver;
612     b->function = bind_info->function;
613     b->instance = NULL;
614     b->next = s->bind;
615     s->bind = b;
616     
617     if (driver->attach) {
618         b->instance = driver->attach();
619         if (b->instance == NULL) {
620             printk(KERN_NOTICE "ds: unable to create instance "
621                    "of '%s'!\n", (char *)bind_info->dev_info);
622             module_put(driver->owner);
623             return -ENODEV;
624         }
625     }
626     
627     return 0;
628 } /* bind_request */
629
630 /*====================================================================*/
631
632 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
633 {
634     socket_bind_t *b;
635     dev_node_t *node;
636
637 #ifdef CONFIG_CARDBUS
638     /*
639      * Some unbelievably ugly code to associate the PCI cardbus
640      * device and its driver with the PCMCIA "bind" information.
641      */
642     {
643         struct pci_bus *bus;
644
645         bus = pcmcia_lookup_bus(s->handle);
646         if (bus) {
647                 struct list_head *list;
648                 struct pci_dev *dev = NULL;
649                 
650                 list = bus->devices.next;
651                 while (list != &bus->devices) {
652                         struct pci_dev *pdev = pci_dev_b(list);
653                         list = list->next;
654
655                         if (first) {
656                                 dev = pdev;
657                                 break;
658                         }
659
660                         /* Try to handle "next" here some way? */
661                 }
662                 if (dev && dev->driver) {
663                         strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
664                         bind_info->major = 0;
665                         bind_info->minor = 0;
666                         bind_info->next = NULL;
667                         return 0;
668                 }
669         }
670     }
671 #endif
672
673     for (b = s->bind; b; b = b->next)
674         if ((strcmp((char *)b->driver->drv.name,
675                     (char *)bind_info->dev_info) == 0) &&
676             (b->function == bind_info->function))
677             break;
678     if (b == NULL) return -ENODEV;
679     if ((b->instance == NULL) ||
680         (b->instance->state & DEV_CONFIG_PENDING))
681         return -EAGAIN;
682     if (first)
683         node = b->instance->dev;
684     else
685         for (node = b->instance->dev; node; node = node->next)
686             if (node == bind_info->next) break;
687     if (node == NULL) return -ENODEV;
688
689     strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
690     bind_info->major = node->major;
691     bind_info->minor = node->minor;
692     bind_info->next = node->next;
693     
694     return 0;
695 } /* get_device_info */
696
697 /*====================================================================*/
698
699 static int unbind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
700 {
701     socket_bind_t **b, *c;
702
703     ds_dbg(2, "unbind_request(%d, '%s')\n", s->parent->sock,
704           (char *)bind_info->dev_info);
705     for (b = &s->bind; *b; b = &(*b)->next)
706         if ((strcmp((char *)(*b)->driver->drv.name,
707                     (char *)bind_info->dev_info) == 0) &&
708             ((*b)->function == bind_info->function))
709             break;
710     if (*b == NULL)
711         return -ENODEV;
712     
713     c = *b;
714     c->driver->use_count--;
715     if (c->driver->detach) {
716         if (c->instance)
717             c->driver->detach(c->instance);
718     }
719     module_put(c->driver->owner);
720     *b = c->next;
721     kfree(c);
722     return 0;
723 } /* unbind_request */
724
725 /*======================================================================
726
727     The user-mode PC Card device interface
728
729 ======================================================================*/
730
731 static int ds_open(struct inode *inode, struct file *file)
732 {
733     socket_t i = iminor(inode);
734     struct pcmcia_bus_socket *s;
735     user_info_t *user;
736
737     ds_dbg(0, "ds_open(socket %d)\n", i);
738
739     s = pcmcia_get_bus_socket(i);
740     if (!s)
741             return -ENODEV;
742
743     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
744         if (s->state & DS_SOCKET_BUSY)
745             return -EBUSY;
746         else
747             s->state |= DS_SOCKET_BUSY;
748     }
749     
750     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
751     if (!user) return -ENOMEM;
752     user->event_tail = user->event_head = 0;
753     user->next = s->user;
754     user->user_magic = USER_MAGIC;
755     user->socket = s;
756     s->user = user;
757     file->private_data = user;
758     
759     if (s->state & DS_SOCKET_PRESENT)
760         queue_event(user, CS_EVENT_CARD_INSERTION);
761     return 0;
762 } /* ds_open */
763
764 /*====================================================================*/
765
766 static int ds_release(struct inode *inode, struct file *file)
767 {
768     struct pcmcia_bus_socket *s;
769     user_info_t *user, **link;
770
771     ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
772
773     user = file->private_data;
774     if (CHECK_USER(user))
775         goto out;
776
777     s = user->socket;
778
779     /* Unlink user data structure */
780     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
781         s->state &= ~DS_SOCKET_BUSY;
782         s->req_pending = 0;
783         wake_up_interruptible(&s->request);
784     }
785     file->private_data = NULL;
786     for (link = &s->user; *link; link = &(*link)->next)
787         if (*link == user) break;
788     if (link == NULL)
789         goto out;
790     *link = user->next;
791     user->user_magic = 0;
792     kfree(user);
793     pcmcia_put_bus_socket(s);
794 out:
795     return 0;
796 } /* ds_release */
797
798 /*====================================================================*/
799
800 static ssize_t ds_read(struct file *file, char __user *buf,
801                        size_t count, loff_t *ppos)
802 {
803     struct pcmcia_bus_socket *s;
804     user_info_t *user;
805     int ret;
806
807     ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
808     
809     if (count < 4)
810         return -EINVAL;
811
812     user = file->private_data;
813     if (CHECK_USER(user))
814         return -EIO;
815     
816     s = user->socket;
817     if (s->state & DS_SOCKET_DEAD)
818         return -EIO;
819
820     ret = wait_event_interruptible(s->queue, !queue_empty(user));
821     if (ret == 0)
822         ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
823
824     return ret;
825 } /* ds_read */
826
827 /*====================================================================*/
828
829 static ssize_t ds_write(struct file *file, const char __user *buf,
830                         size_t count, loff_t *ppos)
831 {
832     struct pcmcia_bus_socket *s;
833     user_info_t *user;
834
835     ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
836     
837     if (count != 4)
838         return -EINVAL;
839     if ((file->f_flags & O_ACCMODE) == O_RDONLY)
840         return -EBADF;
841
842     user = file->private_data;
843     if (CHECK_USER(user))
844         return -EIO;
845
846     s = user->socket;
847     if (s->state & DS_SOCKET_DEAD)
848         return -EIO;
849
850     if (s->req_pending) {
851         s->req_pending--;
852         get_user(s->req_result, (int __user *)buf);
853         if ((s->req_result != 0) || (s->req_pending == 0))
854             wake_up_interruptible(&s->request);
855     } else
856         return -EIO;
857
858     return 4;
859 } /* ds_write */
860
861 /*====================================================================*/
862
863 /* No kernel lock - fine */
864 static u_int ds_poll(struct file *file, poll_table *wait)
865 {
866     struct pcmcia_bus_socket *s;
867     user_info_t *user;
868
869     ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
870     
871     user = file->private_data;
872     if (CHECK_USER(user))
873         return POLLERR;
874     s = user->socket;
875     /*
876      * We don't check for a dead socket here since that
877      * will send cardmgr into an endless spin.
878      */
879     poll_wait(file, &s->queue, wait);
880     if (!queue_empty(user))
881         return POLLIN | POLLRDNORM;
882     return 0;
883 } /* ds_poll */
884
885 /*====================================================================*/
886
887 static int ds_ioctl(struct inode * inode, struct file * file,
888                     u_int cmd, u_long arg)
889 {
890     struct pcmcia_bus_socket *s;
891     void __user *uarg = (char __user *)arg;
892     u_int size;
893     int ret, err;
894     ds_ioctl_arg_t buf;
895     user_info_t *user;
896
897     ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
898     
899     user = file->private_data;
900     if (CHECK_USER(user))
901         return -EIO;
902
903     s = user->socket;
904     if (s->state & DS_SOCKET_DEAD)
905         return -EIO;
906     
907     size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
908     if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
909
910     /* Permission check */
911     if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
912         return -EPERM;
913         
914     if (cmd & IOC_IN) {
915         err = verify_area(VERIFY_READ, uarg, size);
916         if (err) {
917             ds_dbg(3, "ds_ioctl(): verify_read = %d\n", err);
918             return err;
919         }
920     }
921     if (cmd & IOC_OUT) {
922         err = verify_area(VERIFY_WRITE, uarg, size);
923         if (err) {
924             ds_dbg(3, "ds_ioctl(): verify_write = %d\n", err);
925             return err;
926         }
927     }
928     
929     err = ret = 0;
930     
931     if (cmd & IOC_IN) __copy_from_user((char *)&buf, uarg, size);
932     
933     switch (cmd) {
934     case DS_ADJUST_RESOURCE_INFO:
935         ret = pcmcia_adjust_resource_info(s->handle, &buf.adjust);
936         break;
937     case DS_GET_CARD_SERVICES_INFO:
938         ret = pcmcia_get_card_services_info(&buf.servinfo);
939         break;
940     case DS_GET_CONFIGURATION_INFO:
941         ret = pcmcia_get_configuration_info(s->handle, &buf.config);
942         break;
943     case DS_GET_FIRST_TUPLE:
944         ret = pcmcia_get_first_tuple(s->handle, &buf.tuple);
945         break;
946     case DS_GET_NEXT_TUPLE:
947         ret = pcmcia_get_next_tuple(s->handle, &buf.tuple);
948         break;
949     case DS_GET_TUPLE_DATA:
950         buf.tuple.TupleData = buf.tuple_parse.data;
951         buf.tuple.TupleDataMax = sizeof(buf.tuple_parse.data);
952         ret = pcmcia_get_tuple_data(s->handle, &buf.tuple);
953         break;
954     case DS_PARSE_TUPLE:
955         buf.tuple.TupleData = buf.tuple_parse.data;
956         ret = pcmcia_parse_tuple(s->handle, &buf.tuple, &buf.tuple_parse.parse);
957         break;
958     case DS_RESET_CARD:
959         ret = pcmcia_reset_card(s->handle, NULL);
960         break;
961     case DS_GET_STATUS:
962         ret = pcmcia_get_status(s->handle, &buf.status);
963         break;
964     case DS_VALIDATE_CIS:
965         ret = pcmcia_validate_cis(s->handle, &buf.cisinfo);
966         break;
967     case DS_SUSPEND_CARD:
968         ret = pcmcia_suspend_card(s->parent);
969         break;
970     case DS_RESUME_CARD:
971         ret = pcmcia_resume_card(s->parent);
972         break;
973     case DS_EJECT_CARD:
974         ret = pcmcia_eject_card(s->parent);
975         break;
976     case DS_INSERT_CARD:
977         ret = pcmcia_insert_card(s->parent);
978         break;
979     case DS_ACCESS_CONFIGURATION_REGISTER:
980         if ((buf.conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN))
981             return -EPERM;
982         ret = pcmcia_access_configuration_register(s->handle, &buf.conf_reg);
983         break;
984     case DS_GET_FIRST_REGION:
985         ret = pcmcia_get_first_region(s->handle, &buf.region);
986         break;
987     case DS_GET_NEXT_REGION:
988         ret = pcmcia_get_next_region(s->handle, &buf.region);
989         break;
990     case DS_GET_FIRST_WINDOW:
991         buf.win_info.handle = (window_handle_t)s->handle;
992         ret = pcmcia_get_first_window(&buf.win_info.handle, &buf.win_info.window);
993         break;
994     case DS_GET_NEXT_WINDOW:
995         ret = pcmcia_get_next_window(&buf.win_info.handle, &buf.win_info.window);
996         break;
997     case DS_GET_MEM_PAGE:
998         ret = pcmcia_get_mem_page(buf.win_info.handle,
999                            &buf.win_info.map);
1000         break;
1001     case DS_REPLACE_CIS:
1002         ret = pcmcia_replace_cis(s->handle, &buf.cisdump);
1003         break;
1004     case DS_BIND_REQUEST:
1005         if (!capable(CAP_SYS_ADMIN)) return -EPERM;
1006         err = bind_request(s, &buf.bind_info);
1007         break;
1008     case DS_GET_DEVICE_INFO:
1009         err = get_device_info(s, &buf.bind_info, 1);
1010         break;
1011     case DS_GET_NEXT_DEVICE:
1012         err = get_device_info(s, &buf.bind_info, 0);
1013         break;
1014     case DS_UNBIND_REQUEST:
1015         err = unbind_request(s, &buf.bind_info);
1016         break;
1017     case DS_BIND_MTD:
1018         if (!capable(CAP_SYS_ADMIN)) return -EPERM;
1019         err = bind_mtd(s, &buf.mtd_info);
1020         break;
1021     default:
1022         err = -EINVAL;
1023     }
1024     
1025     if ((err == 0) && (ret != CS_SUCCESS)) {
1026         ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1027         switch (ret) {
1028         case CS_BAD_SOCKET: case CS_NO_CARD:
1029             err = -ENODEV; break;
1030         case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1031         case CS_BAD_TUPLE:
1032             err = -EINVAL; break;
1033         case CS_IN_USE:
1034             err = -EBUSY; break;
1035         case CS_OUT_OF_RESOURCE:
1036             err = -ENOSPC; break;
1037         case CS_NO_MORE_ITEMS:
1038             err = -ENODATA; break;
1039         case CS_UNSUPPORTED_FUNCTION:
1040             err = -ENOSYS; break;
1041         default:
1042             err = -EIO; break;
1043         }
1044     }
1045
1046     if (cmd & IOC_OUT) __copy_to_user(uarg, (char *)&buf, size);
1047
1048     return err;
1049 } /* ds_ioctl */
1050
1051 /*====================================================================*/
1052
1053 static struct file_operations ds_fops = {
1054         .owner          = THIS_MODULE,
1055         .open           = ds_open,
1056         .release        = ds_release,
1057         .ioctl          = ds_ioctl,
1058         .read           = ds_read,
1059         .write          = ds_write,
1060         .poll           = ds_poll,
1061 };
1062
1063 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1064 {
1065         struct pcmcia_socket *socket = class_dev->class_data;
1066         client_reg_t client_reg;
1067         bind_req_t bind;
1068         struct pcmcia_bus_socket *s;
1069         int ret;
1070
1071         s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1072         if(!s)
1073                 return -ENOMEM;
1074         memset(s, 0, sizeof(struct pcmcia_bus_socket));
1075         atomic_set(&s->refcount, 1);
1076     
1077         /*
1078          * Ugly. But we want to wait for the socket threads to have started up.
1079          * We really should let the drivers themselves drive some of this..
1080          */
1081         current->state = TASK_INTERRUPTIBLE;
1082         schedule_timeout(HZ/4);
1083
1084         init_waitqueue_head(&s->queue);
1085         init_waitqueue_head(&s->request);
1086
1087         /* initialize data */
1088         INIT_WORK(&s->removal, handle_removal, s);
1089         s->parent = socket;
1090
1091         /* Set up hotline to Card Services */
1092         client_reg.dev_info = bind.dev_info = &dev_info;
1093
1094         bind.Socket = socket;
1095         bind.Function = BIND_FN_ALL;
1096         ret = pcmcia_bind_device(&bind);
1097         if (ret != CS_SUCCESS) {
1098                 cs_error(NULL, BindDevice, ret);
1099                 kfree(s);
1100                 return -EINVAL;
1101         }
1102
1103         client_reg.Attributes = INFO_MASTER_CLIENT;
1104         client_reg.EventMask =
1105                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
1106                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
1107                 CS_EVENT_EJECTION_REQUEST | CS_EVENT_INSERTION_REQUEST |
1108                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
1109         client_reg.event_handler = &ds_event;
1110         client_reg.Version = 0x0210;
1111         client_reg.event_callback_args.client_data = s;
1112         ret = pcmcia_register_client(&s->handle, &client_reg);
1113         if (ret != CS_SUCCESS) {
1114                 cs_error(NULL, RegisterClient, ret);
1115                 kfree(s);
1116                 return -EINVAL;
1117         }
1118
1119         socket->pcmcia = s;
1120
1121         return 0;
1122 }
1123
1124
1125 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1126 {
1127         struct pcmcia_socket *socket = class_dev->class_data;
1128
1129         if (!socket || !socket->pcmcia)
1130                 return;
1131
1132         flush_scheduled_work();
1133
1134         pcmcia_deregister_client(socket->pcmcia->handle);
1135
1136         socket->pcmcia->state |= DS_SOCKET_DEAD;
1137         pcmcia_put_bus_socket(socket->pcmcia);
1138         socket->pcmcia = NULL;
1139
1140         return;
1141 }
1142
1143
1144 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1145 static struct class_interface pcmcia_bus_interface = {
1146         .class = &pcmcia_socket_class,
1147         .add = &pcmcia_bus_add_socket,
1148         .remove = &pcmcia_bus_remove_socket,
1149 };
1150
1151
1152 struct bus_type pcmcia_bus_type = {
1153         .name = "pcmcia",
1154 };
1155 EXPORT_SYMBOL(pcmcia_bus_type);
1156
1157
1158 static int __init init_pcmcia_bus(void)
1159 {
1160         int i;
1161
1162         bus_register(&pcmcia_bus_type);
1163         class_interface_register(&pcmcia_bus_interface);
1164
1165         /* Set up character device for user mode clients */
1166         i = register_chrdev(0, "pcmcia", &ds_fops);
1167         if (i == -EBUSY)
1168                 printk(KERN_NOTICE "unable to find a free device # for "
1169                        "Driver Services\n");
1170         else
1171                 major_dev = i;
1172
1173 #ifdef CONFIG_PROC_FS
1174         proc_pccard = proc_mkdir("pccard", proc_bus);
1175         if (proc_pccard)
1176                 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1177 #endif
1178
1179         return 0;
1180 }
1181 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
1182                                * pcmcia_socket_class is already registered */
1183
1184
1185 static void __exit exit_pcmcia_bus(void)
1186 {
1187         class_interface_unregister(&pcmcia_bus_interface);
1188
1189 #ifdef CONFIG_PROC_FS
1190         if (proc_pccard) {
1191                 remove_proc_entry("drivers", proc_pccard);
1192                 remove_proc_entry("pccard", proc_bus);
1193         }
1194 #endif
1195         if (major_dev != -1)
1196                 unregister_chrdev(major_dev, "pcmcia");
1197
1198         bus_unregister(&pcmcia_bus_type);
1199 }
1200 module_exit(exit_pcmcia_bus);
1201
1202
1203
1204 /* helpers for backwards-compatible functions */
1205
1206 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1207 {
1208         struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1209         if (s && s->pcmcia)
1210                 return s->pcmcia;
1211         else
1212                 return NULL;
1213 }
1214
1215 /* backwards-compatible accessing of driver --- by name! */
1216
1217 struct cmp_data {
1218         void *dev_info;
1219         struct pcmcia_driver *drv;
1220 };
1221
1222 static int cmp_drv_callback(struct device_driver *drv, void *data)
1223 {
1224         struct cmp_data *cmp = data;
1225         if (strncmp((char *)cmp->dev_info, (char *)drv->name,
1226                     DEV_NAME_LEN) == 0) {
1227                 cmp->drv = container_of(drv, struct pcmcia_driver, drv);
1228                 return -EINVAL;
1229         }
1230         return 0;
1231 }
1232
1233 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1234 {
1235         int ret;
1236         struct cmp_data cmp = {
1237                 .dev_info = dev_info,
1238         };
1239         
1240         ret = bus_for_each_drv(&pcmcia_bus_type, NULL, &cmp, cmp_drv_callback);
1241         if (ret)
1242                 return cmp.drv;
1243         return NULL;
1244 }