vserver 1.9.5.x5
[linux-2.6.git] / drivers / pcmcia / ds.c
1 /*
2  * ds.c -- 16-bit PCMCIA core support
3  *
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.
7  *
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.
11  *
12  * (C) 1999             David A. Hinds
13  * (C) 2003 - 2004      Dominik Brodowski
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/timer.h>
30 #include <linux/ioctl.h>
31 #include <linux/proc_fs.h>
32 #include <linux/poll.h>
33 #include <linux/pci.h>
34 #include <linux/list.h>
35 #include <linux/delay.h>
36 #include <linux/kref.h>
37 #include <linux/workqueue.h>
38
39 #include <asm/atomic.h>
40
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>
49
50 #include "cs_internal.h"
51
52 /*====================================================================*/
53
54 /* Module parameters */
55
56 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
57 MODULE_DESCRIPTION("PCMCIA Driver Services");
58 MODULE_LICENSE("GPL");
59
60 #ifdef DEBUG
61 int ds_pc_debug;
62
63 module_param_named(pc_debug, ds_pc_debug, int, 0644);
64
65 #define ds_dbg(lvl, fmt, arg...) do {                           \
66         if (ds_pc_debug > (lvl))                                        \
67                 printk(KERN_DEBUG "ds: " fmt , ## arg);         \
68 } while (0)
69 #else
70 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
71 #endif
72
73 /*====================================================================*/
74
75 /* Device user information */
76 #define MAX_EVENTS      32
77 #define USER_MAGIC      0x7ea4
78 #define CHECK_USER(u) \
79     (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
80 typedef struct user_info_t {
81     u_int               user_magic;
82     int                 event_head, event_tail;
83     event_t             event[MAX_EVENTS];
84     struct user_info_t  *next;
85     struct pcmcia_bus_socket *socket;
86 } user_info_t;
87
88 /* Socket state information */
89 struct pcmcia_bus_socket {
90         struct kref             refcount;
91         struct pcmcia_callback  callback;
92         int                     state;
93         user_info_t             *user;
94         int                     req_pending, req_result;
95         wait_queue_head_t       queue, request;
96         struct pcmcia_socket    *parent;
97
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 */
104 };
105 static spinlock_t pcmcia_dev_list_lock;
106
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
111
112 /*====================================================================*/
113
114 static int major_dev = -1;
115
116 static int unbind_request(struct pcmcia_bus_socket *s);
117
118 /*====================================================================*/
119
120 /* code which was in cs.c before */
121
122 /* String tables for error messages */
123
124 typedef struct lookup_t {
125     int key;
126     char *msg;
127 } lookup_t;
128
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" }
161 };
162
163
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" }
214 };
215
216
217 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
218 {
219         int i;
220         char *serv;
221
222         if (CHECK_HANDLE(handle))
223                 printk(KERN_NOTICE);
224         else {
225                 struct pcmcia_device *p_dev = handle_to_pdev(handle);
226                 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
227         }
228
229         for (i = 0; i < ARRAY_SIZE(service_table); i++)
230                 if (service_table[i].key == err->func)
231                         break;
232         if (i < ARRAY_SIZE(service_table))
233                 serv = service_table[i].msg;
234         else
235                 serv = "Unknown service number";
236
237         for (i = 0; i < ARRAY_SIZE(error_table); i++)
238                 if (error_table[i].key == err->retcode)
239                         break;
240         if (i < ARRAY_SIZE(error_table))
241                 printk("%s: %s\n", serv, error_table[i].msg);
242         else
243                 printk("%s: Unknown error code %#x\n", serv, err->retcode);
244
245         return CS_SUCCESS;
246 } /* report_error */
247 EXPORT_SYMBOL(pcmcia_report_error);
248
249 /* end of code which was in cs.c before */
250
251 /*======================================================================*/
252
253 void cs_error(client_handle_t handle, int func, int ret)
254 {
255         error_info_t err = { func, ret };
256         pcmcia_report_error(handle, &err);
257 }
258 EXPORT_SYMBOL(cs_error);
259
260 /*======================================================================*/
261
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);
264
265 static void pcmcia_release_bus_socket(struct kref *refcount)
266 {
267         struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
268         pcmcia_put_socket(s->parent);
269         kfree(s);
270 }
271
272 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
273 {
274         kref_put(&s->refcount, pcmcia_release_bus_socket);
275 }
276
277 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
278 {
279         kref_get(&s->refcount);
280         return (s);
281 }
282
283 /**
284  * pcmcia_register_driver - register a PCMCIA driver with the bus core
285  *
286  * Registers a PCMCIA driver with the PCMCIA bus core.
287  */
288 int pcmcia_register_driver(struct pcmcia_driver *driver)
289 {
290         if (!driver)
291                 return -EINVAL;
292
293         driver->drv.bus = &pcmcia_bus_type;
294         driver->drv.owner = driver->owner;
295
296         return driver_register(&driver->drv);
297 }
298 EXPORT_SYMBOL(pcmcia_register_driver);
299
300 /**
301  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
302  */
303 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
304 {
305         driver_unregister(&driver->drv);
306 }
307 EXPORT_SYMBOL(pcmcia_unregister_driver);
308
309 #ifdef CONFIG_PROC_FS
310 static struct proc_dir_entry *proc_pccard = NULL;
311
312 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
313 {
314         char **p = d;
315         struct pcmcia_driver *p_drv = container_of(driver,
316                                                    struct pcmcia_driver, drv);
317
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
321 #else
322                       1
323 #endif
324         );
325         d = (void *) p;
326
327         return 0;
328 }
329
330 static int proc_read_drivers(char *buf, char **start, off_t pos,
331                              int count, int *eof, void *data)
332 {
333         char *p = buf;
334
335         bus_for_each_drv(&pcmcia_bus_type, NULL, 
336                          (void *) &p, proc_read_drivers_callback);
337
338         return (p - buf);
339 }
340 #endif
341
342 /* pcmcia_device handling */
343
344 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
345 {
346         struct device *tmp_dev;
347         tmp_dev = get_device(&p_dev->dev);
348         if (!tmp_dev)
349                 return NULL;
350         return to_pcmcia_dev(tmp_dev);
351 }
352
353 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
354 {
355         put_device(&p_dev->dev);
356 }
357
358 static void pcmcia_release_dev(struct device *dev)
359 {
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);
363         kfree(p_dev);
364 }
365
366
367 /*======================================================================
368
369     These manage a ring buffer of events pending for one user process
370     
371 ======================================================================*/
372
373 static int queue_empty(user_info_t *user)
374 {
375     return (user->event_head == user->event_tail);
376 }
377
378 static event_t get_queued_event(user_info_t *user)
379 {
380     user->event_tail = (user->event_tail+1) % MAX_EVENTS;
381     return user->event[user->event_tail];
382 }
383
384 static void queue_event(user_info_t *user, event_t event)
385 {
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;
390 }
391
392 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
393 {
394     user_info_t *user;
395     for (user = s->user; user; user = user->next)
396         queue_event(user, event);
397     wake_up_interruptible(&s->queue);
398 }
399
400 static int handle_request(struct pcmcia_bus_socket *s, event_t event)
401 {
402     if (s->req_pending != 0)
403         return CS_IN_USE;
404     if (s->state & DS_SOCKET_BUSY)
405         s->req_pending = 1;
406     handle_event(s, event);
407     if (wait_event_interruptible(s->request, s->req_pending <= 0))
408         return CS_IN_USE;
409     if (s->state & DS_SOCKET_BUSY)
410         return s->req_result;
411     return CS_SUCCESS;
412 }
413
414 /*======================================================================
415
416     The card status event handler.
417     
418 ======================================================================*/
419
420 struct send_event_data {
421         struct pcmcia_socket *skt;
422         event_t event;
423         int priority;
424 };
425
426 static int send_event_callback(struct device *dev, void * _data)
427 {
428         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
429         struct send_event_data *data = _data;
430
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)
434                 return 0;
435
436         if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
437                 return 0;
438
439         if (p_dev->client.EventMask & data->event)
440                 return EVENT(&p_dev->client, data->event, data->priority);
441
442         return 0;
443 }
444
445 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
446 {
447         int ret = 0;
448         struct send_event_data private;
449         struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
450
451         if (!skt)
452                 return 0;
453
454         private.skt = s;
455         private.event = event;
456         private.priority = priority;
457
458         ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
459
460         pcmcia_put_bus_socket(skt);
461         return ret;
462 } /* send_event */
463
464
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.
468  */
469
470 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
471 {
472         struct pcmcia_bus_socket *s = skt->pcmcia;
473         int ret = 0;
474
475         ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
476                event, priority, s);
477     
478         switch (event) {
479
480         case CS_EVENT_CARD_REMOVAL:
481                 s->state &= ~DS_SOCKET_PRESENT;
482                 send_event(skt, event, priority);
483                 unbind_request(s);
484                 handle_event(s, event);
485                 break;
486         
487         case CS_EVENT_CARD_INSERTION:
488                 s->state |= DS_SOCKET_PRESENT;
489                 handle_event(s, event);
490                 send_event(skt, event, priority);
491                 break;
492
493         case CS_EVENT_EJECTION_REQUEST:
494                 ret = handle_request(s, event);
495                 if (ret)
496                         break;
497                 ret = send_event(skt, event, priority);
498                 break;
499
500         default:
501                 handle_event(s, event);
502                 send_event(skt, event, priority);
503                 break;
504     }
505
506     return 0;
507 } /* ds_event */
508
509
510 /*======================================================================
511
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:
515
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.
520     
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.
525
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.
530
531 ======================================================================*/
532
533 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
534 {
535         struct pcmcia_driver *p_drv;
536         struct pcmcia_device *p_dev, *tmp_dev;
537         unsigned long flags;
538         int ret = 0;
539
540         s = pcmcia_get_bus_socket(s);
541         if (!s)
542                 return -EINVAL;
543
544         ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
545                (char *)bind_info->dev_info);
546
547         p_drv = get_pcmcia_driver(&bind_info->dev_info);
548         if (!p_drv) {
549                 ret = -EINVAL;
550                 goto err_put;
551         }
552
553         if (!try_module_get(p_drv->owner)) {
554                 ret = -EINVAL;
555                 goto err_put_driver;
556         }
557
558         /* Currently, the userspace pcmcia cardmgr detects pcmcia devices.
559          * Here this information is translated into a kernel
560          * struct pcmcia_device.
561          */
562
563         p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
564         if (!p_dev) {
565                 ret = -ENOMEM;
566                 goto err_put_module;
567         }
568         memset(p_dev, 0, sizeof(struct pcmcia_device));
569
570         p_dev->socket = s->parent;
571         p_dev->device_no = (s->device_count++);
572         p_dev->func   = bind_info->function;
573
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;
579
580         /* compat */
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;
585
586         ret = device_register(&p_dev->dev);
587         if (ret) {
588                 kfree(p_dev);
589                 goto err_put_module;
590         }
591
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;
600                         ret = -EBUSY;
601                         goto err_unregister;
602                 }
603         }
604         list_add_tail(&p_dev->socket_device_list, &s->devices_list);
605         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
606
607         if (p_drv->attach) {
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);
612                         ret = -ENODEV;
613                         goto err_unregister;
614                 }
615         }
616
617         put_driver(&p_drv->drv);
618
619         return 0;
620
621  err_unregister:
622         device_unregister(&p_dev->dev);
623         module_put(p_drv->owner);
624         put_driver(&p_drv->drv);
625         return (ret);
626
627  err_put_module:
628         module_put(p_drv->owner);
629  err_put_driver:
630         put_driver(&p_drv->drv);
631  err_put:
632         pcmcia_put_bus_socket(s);
633         return (ret);
634 } /* bind_request */
635
636 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
637 {
638         client_t *client = NULL;
639         struct pcmcia_socket *s;
640         struct pcmcia_bus_socket *skt = NULL;
641         struct pcmcia_device *p_dev = NULL;
642
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) {
646                 unsigned long flags;
647
648                 if (s->state & SOCKET_CARDBUS)
649                         continue;
650
651                 skt = s->pcmcia;
652                 if (!skt)
653                         continue;
654                 skt = pcmcia_get_bus_socket(skt);
655                 if (!skt)
656                         continue;
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);
661                         if (!p_dev)
662                                 continue;
663                         if (!(p_dev->client.state & CLIENT_UNBOUND) ||
664                             (!p_dev->dev.driver)) {
665                                 pcmcia_put_dev(p_dev);
666                                 continue;
667                         }
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);
672                                 goto found;
673                         }
674                         pcmcia_put_dev(p_dev);
675                 }
676                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
677                 pcmcia_put_bus_socket(skt);
678         }
679  found:
680         up_read(&pcmcia_socket_list_rwsem);
681         if (!p_dev || !client)
682                 return -ENODEV;
683
684         pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
685
686         /*
687          * Prevent this racing with a card insertion.
688          */
689         down(&s->skt_sem);
690         *handle = client;
691         client->state &= ~CLIENT_UNBOUND;
692         client->Socket = s;
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;
697
698         if (s->state & SOCKET_CARDBUS)
699                 client->state |= CLIENT_CARDBUS;
700
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)
705                     == CS_SUCCESS)
706                         s->functions = mfc.nfn;
707                 else
708                         s->functions = 1;
709                 s->config = kmalloc(sizeof(config_t) * s->functions,
710                                     GFP_KERNEL);
711                 if (!s->config)
712                         goto out_no_resource;
713                 memset(s->config, 0, sizeof(config_t) * s->functions);
714         }
715
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);
720
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);
724         }
725
726         up(&s->skt_sem);
727         return CS_SUCCESS;
728
729  out_no_resource:
730         up(&s->skt_sem);
731         pcmcia_put_dev(p_dev);
732         return CS_OUT_OF_RESOURCE;
733 } /* register_client */
734 EXPORT_SYMBOL(pcmcia_register_client);
735
736
737 /*====================================================================*/
738
739 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
740
741 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
742 {
743         dev_node_t *node;
744         struct pcmcia_device *p_dev;
745         unsigned long flags;
746         int ret = 0;
747
748 #ifdef CONFIG_CARDBUS
749         /*
750          * Some unbelievably ugly code to associate the PCI cardbus
751          * device and its driver with the PCMCIA "bind" information.
752          */
753         {
754                 struct pci_bus *bus;
755
756                 bus = pcmcia_lookup_bus(s->parent);
757                 if (bus) {
758                         struct list_head *list;
759                         struct pci_dev *dev = NULL;
760
761                         list = bus->devices.next;
762                         while (list != &bus->devices) {
763                                 struct pci_dev *pdev = pci_dev_b(list);
764                                 list = list->next;
765
766                                 if (first) {
767                                         dev = pdev;
768                                         break;
769                                 }
770
771                                 /* Try to handle "next" here some way? */
772                         }
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;
778                                 return 0;
779                         }
780                 }
781         }
782 #endif
783
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);
788                         if (!p_dev)
789                                 continue;
790                         goto found;
791                 }
792         }
793         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
794         return -ENODEV;
795
796  found:
797         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
798
799         if ((!p_dev->instance) ||
800             (p_dev->instance->state & DEV_CONFIG_PENDING)) {
801                 ret = -EAGAIN;
802                 goto err_put;
803         }
804
805         if (first)
806                 node = p_dev->instance->dev;
807         else
808                 for (node = p_dev->instance->dev; node; node = node->next)
809                         if (node == bind_info->next)
810                                 break;
811         if (!node) {
812                 ret = -ENODEV;
813                 goto err_put;
814         }
815
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;
820
821  err_put:
822         pcmcia_put_dev(p_dev);
823         return (ret);
824 } /* get_device_info */
825
826 /*====================================================================*/
827
828 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
829  * drivers have been called with EVENT_CARD_REMOVAL before.
830  */
831 static int unbind_request(struct pcmcia_bus_socket *s)
832 {
833         struct pcmcia_device    *p_dev;
834         struct pcmcia_driver    *p_drv;
835         unsigned long           flags;
836
837         ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
838
839         s->device_count = 0;
840
841         for (;;) {
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);
846                         return 0;
847                 }
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);
852
853                 /* detach the "instance" */
854                 p_drv = to_pcmcia_drv(p_dev->dev.driver);
855                 if (p_drv) {
856                         if ((p_drv->detach) && (p_dev->instance))
857                                 p_drv->detach(p_dev->instance);
858                         module_put(p_drv->owner);
859                 }
860
861                 device_unregister(&p_dev->dev);
862         }
863
864         return 0;
865 } /* unbind_request */
866
867 int pcmcia_deregister_client(client_handle_t handle)
868 {
869         struct pcmcia_socket *s;
870         int i;
871         struct pcmcia_device *p_dev = handle_to_pdev(handle);
872
873         if (CHECK_HANDLE(handle))
874                 return CS_BAD_HANDLE;
875
876         s = SOCKET(handle);
877         ds_dbg(1, "deregister_client(%p)\n", handle);
878
879         if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
880                 goto warn_out;
881         for (i = 0; i < MAX_WIN; i++)
882                 if (handle->state & CLIENT_WIN_REQ(i))
883                         goto warn_out;
884
885         if (handle->state & CLIENT_STALE) {
886                 handle->client_magic = 0;
887                 handle->state &= ~CLIENT_STALE;
888                 pcmcia_put_dev(p_dev);
889         } else {
890                 handle->state = CLIENT_UNBOUND;
891                 handle->event_handler = NULL;
892         }
893
894         return CS_SUCCESS;
895  warn_out:
896         printk(KERN_WARNING "ds: deregister_client was called too early.\n");
897         return CS_IN_USE;
898 } /* deregister_client */
899 EXPORT_SYMBOL(pcmcia_deregister_client);
900
901
902 /*======================================================================
903
904     The user-mode PC Card device interface
905
906 ======================================================================*/
907
908 static int ds_open(struct inode *inode, struct file *file)
909 {
910     socket_t i = iminor(inode);
911     struct pcmcia_bus_socket *s;
912     user_info_t *user;
913
914     ds_dbg(0, "ds_open(socket %d)\n", i);
915
916     s = get_socket_info_by_nr(i);
917     if (!s)
918             return -ENODEV;
919     s = pcmcia_get_bus_socket(s);
920     if (!s)
921             return -ENODEV;
922
923     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
924             if (s->state & DS_SOCKET_BUSY) {
925                     pcmcia_put_bus_socket(s);
926                     return -EBUSY;
927             }
928         else
929             s->state |= DS_SOCKET_BUSY;
930     }
931     
932     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
933     if (!user) {
934             pcmcia_put_bus_socket(s);
935             return -ENOMEM;
936     }
937     user->event_tail = user->event_head = 0;
938     user->next = s->user;
939     user->user_magic = USER_MAGIC;
940     user->socket = s;
941     s->user = user;
942     file->private_data = user;
943     
944     if (s->state & DS_SOCKET_PRESENT)
945         queue_event(user, CS_EVENT_CARD_INSERTION);
946     return 0;
947 } /* ds_open */
948
949 /*====================================================================*/
950
951 static int ds_release(struct inode *inode, struct file *file)
952 {
953     struct pcmcia_bus_socket *s;
954     user_info_t *user, **link;
955
956     ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
957
958     user = file->private_data;
959     if (CHECK_USER(user))
960         goto out;
961
962     s = user->socket;
963
964     /* Unlink user data structure */
965     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
966         s->state &= ~DS_SOCKET_BUSY;
967         s->req_pending = 0;
968         wake_up_interruptible(&s->request);
969     }
970     file->private_data = NULL;
971     for (link = &s->user; *link; link = &(*link)->next)
972         if (*link == user) break;
973     if (link == NULL)
974         goto out;
975     *link = user->next;
976     user->user_magic = 0;
977     kfree(user);
978     pcmcia_put_bus_socket(s);
979 out:
980     return 0;
981 } /* ds_release */
982
983 /*====================================================================*/
984
985 static ssize_t ds_read(struct file *file, char __user *buf,
986                        size_t count, loff_t *ppos)
987 {
988     struct pcmcia_bus_socket *s;
989     user_info_t *user;
990     int ret;
991
992     ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
993     
994     if (count < 4)
995         return -EINVAL;
996
997     user = file->private_data;
998     if (CHECK_USER(user))
999         return -EIO;
1000     
1001     s = user->socket;
1002     if (s->state & DS_SOCKET_DEAD)
1003         return -EIO;
1004
1005     ret = wait_event_interruptible(s->queue, !queue_empty(user));
1006     if (ret == 0)
1007         ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1008
1009     return ret;
1010 } /* ds_read */
1011
1012 /*====================================================================*/
1013
1014 static ssize_t ds_write(struct file *file, const char __user *buf,
1015                         size_t count, loff_t *ppos)
1016 {
1017     struct pcmcia_bus_socket *s;
1018     user_info_t *user;
1019
1020     ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1021     
1022     if (count != 4)
1023         return -EINVAL;
1024     if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1025         return -EBADF;
1026
1027     user = file->private_data;
1028     if (CHECK_USER(user))
1029         return -EIO;
1030
1031     s = user->socket;
1032     if (s->state & DS_SOCKET_DEAD)
1033         return -EIO;
1034
1035     if (s->req_pending) {
1036         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);
1040     } else
1041         return -EIO;
1042
1043     return 4;
1044 } /* ds_write */
1045
1046 /*====================================================================*/
1047
1048 /* No kernel lock - fine */
1049 static u_int ds_poll(struct file *file, poll_table *wait)
1050 {
1051     struct pcmcia_bus_socket *s;
1052     user_info_t *user;
1053
1054     ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1055     
1056     user = file->private_data;
1057     if (CHECK_USER(user))
1058         return POLLERR;
1059     s = user->socket;
1060     /*
1061      * We don't check for a dead socket here since that
1062      * will send cardmgr into an endless spin.
1063      */
1064     poll_wait(file, &s->queue, wait);
1065     if (!queue_empty(user))
1066         return POLLIN | POLLRDNORM;
1067     return 0;
1068 } /* ds_poll */
1069
1070 /*====================================================================*/
1071
1072 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1073
1074 static int ds_ioctl(struct inode * inode, struct file * file,
1075                     u_int cmd, u_long arg)
1076 {
1077     struct pcmcia_bus_socket *s;
1078     void __user *uarg = (char __user *)arg;
1079     u_int size;
1080     int ret, err;
1081     ds_ioctl_arg_t *buf;
1082     user_info_t *user;
1083
1084     ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1085     
1086     user = file->private_data;
1087     if (CHECK_USER(user))
1088         return -EIO;
1089
1090     s = user->socket;
1091     if (s->state & DS_SOCKET_DEAD)
1092         return -EIO;
1093     
1094     size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1095     if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1096
1097     /* Permission check */
1098     if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1099         return -EPERM;
1100         
1101     if (cmd & IOC_IN) {
1102         err = verify_area(VERIFY_READ, uarg, size);
1103         if (err) {
1104             ds_dbg(3, "ds_ioctl(): verify_read = %d\n", err);
1105             return err;
1106         }
1107     }
1108     if (cmd & IOC_OUT) {
1109         err = verify_area(VERIFY_WRITE, uarg, size);
1110         if (err) {
1111             ds_dbg(3, "ds_ioctl(): verify_write = %d\n", err);
1112             return err;
1113         }
1114     }
1115     buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1116     if (!buf)
1117         return -ENOMEM;
1118     
1119     err = ret = 0;
1120     
1121     if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1122     
1123     switch (cmd) {
1124     case DS_ADJUST_RESOURCE_INFO:
1125         ret = pcmcia_adjust_resource_info(&buf->adjust);
1126         break;
1127     case DS_GET_CARD_SERVICES_INFO:
1128         ret = pcmcia_get_card_services_info(&buf->servinfo);
1129         break;
1130     case DS_GET_CONFIGURATION_INFO:
1131         if (buf->config.Function &&
1132            (buf->config.Function >= s->parent->functions))
1133             ret = CS_BAD_ARGS;
1134         else
1135             ret = pccard_get_configuration_info(s->parent,
1136                         buf->config.Function, &buf->config);
1137         break;
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);
1141         break;
1142     case DS_GET_NEXT_TUPLE:
1143         ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1144         break;
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);
1149         break;
1150     case DS_PARSE_TUPLE:
1151         buf->tuple.TupleData = buf->tuple_parse.data;
1152         ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1153         break;
1154     case DS_RESET_CARD:
1155         ret = pccard_reset_card(s->parent);
1156         break;
1157     case DS_GET_STATUS:
1158         if (buf->status.Function &&
1159            (buf->status.Function >= s->parent->functions))
1160             ret = CS_BAD_ARGS;
1161         else
1162         ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1163         break;
1164     case DS_VALIDATE_CIS:
1165         pcmcia_validate_mem(s->parent);
1166         ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1167         break;
1168     case DS_SUSPEND_CARD:
1169         ret = pcmcia_suspend_card(s->parent);
1170         break;
1171     case DS_RESUME_CARD:
1172         ret = pcmcia_resume_card(s->parent);
1173         break;
1174     case DS_EJECT_CARD:
1175         err = pcmcia_eject_card(s->parent);
1176         break;
1177     case DS_INSERT_CARD:
1178         err = pcmcia_insert_card(s->parent);
1179         break;
1180     case DS_ACCESS_CONFIGURATION_REGISTER:
1181         if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1182             err = -EPERM;
1183             goto free_out;
1184         }
1185         if (buf->conf_reg.Function &&
1186            (buf->conf_reg.Function >= s->parent->functions))
1187             ret = CS_BAD_ARGS;
1188         else
1189             ret = pccard_access_configuration_register(s->parent,
1190                         buf->conf_reg.Function, &buf->conf_reg);
1191         break;
1192     case DS_GET_FIRST_REGION:
1193     case DS_GET_NEXT_REGION:
1194     case DS_BIND_MTD:
1195         if (!capable(CAP_SYS_ADMIN)) {
1196                 err = -EPERM;
1197                 goto free_out;
1198         } else {
1199                 static int printed = 0;
1200                 if (!printed) {
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");
1203                         printed++;
1204                 }
1205         }
1206         ret = -EINVAL;
1207         goto free_out;
1208         break;
1209     case DS_GET_FIRST_WINDOW:
1210         ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1211                         &buf->win_info.window);
1212         break;
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);
1216         break;
1217     case DS_GET_MEM_PAGE:
1218         ret = pcmcia_get_mem_page(buf->win_info.handle,
1219                            &buf->win_info.map);
1220         break;
1221     case DS_REPLACE_CIS:
1222         ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1223         break;
1224     case DS_BIND_REQUEST:
1225         if (!capable(CAP_SYS_ADMIN)) {
1226                 err = -EPERM;
1227                 goto free_out;
1228         }
1229         err = bind_request(s, &buf->bind_info);
1230         break;
1231     case DS_GET_DEVICE_INFO:
1232         err = get_device_info(s, &buf->bind_info, 1);
1233         break;
1234     case DS_GET_NEXT_DEVICE:
1235         err = get_device_info(s, &buf->bind_info, 0);
1236         break;
1237     case DS_UNBIND_REQUEST:
1238         err = 0;
1239         break;
1240     default:
1241         err = -EINVAL;
1242     }
1243     
1244     if ((err == 0) && (ret != CS_SUCCESS)) {
1245         ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1246         switch (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:
1250         case CS_BAD_TUPLE:
1251             err = -EINVAL; break;
1252         case CS_IN_USE:
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;
1260         default:
1261             err = -EIO; break;
1262         }
1263     }
1264
1265     if (cmd & IOC_OUT) {
1266         if (__copy_to_user(uarg, (char *)buf, size))
1267             err = -EFAULT;
1268     }
1269
1270 free_out:
1271     kfree(buf);
1272     return err;
1273 } /* ds_ioctl */
1274
1275 /*====================================================================*/
1276
1277 static struct file_operations ds_fops = {
1278         .owner          = THIS_MODULE,
1279         .open           = ds_open,
1280         .release        = ds_release,
1281         .ioctl          = ds_ioctl,
1282         .read           = ds_read,
1283         .write          = ds_write,
1284         .poll           = ds_poll,
1285 };
1286
1287 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1288 {
1289         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1290         struct pcmcia_bus_socket *s;
1291         int ret;
1292
1293         s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1294         if(!s)
1295                 return -ENOMEM;
1296         memset(s, 0, sizeof(struct pcmcia_bus_socket));
1297
1298         /* get reference to parent socket */
1299         s->parent = pcmcia_get_socket(socket);
1300         if (!s->parent) {
1301                 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1302                 kfree (s);
1303                 return -ENODEV;
1304         }
1305
1306         kref_init(&s->refcount);
1307     
1308         /*
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..
1311          */
1312         msleep(250);
1313
1314         init_waitqueue_head(&s->queue);
1315         init_waitqueue_head(&s->request);
1316         INIT_LIST_HEAD(&s->devices_list);
1317
1318         /* Set up hotline to Card Services */
1319         s->callback.owner = THIS_MODULE;
1320         s->callback.event = &ds_event;
1321         socket->pcmcia = s;
1322
1323         ret = pccard_register_pcmcia(socket, &s->callback);
1324         if (ret) {
1325                 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1326                 pcmcia_put_bus_socket(s);
1327                 socket->pcmcia = NULL;
1328                 return (ret);
1329         }
1330
1331         return 0;
1332 }
1333
1334
1335 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1336 {
1337         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1338
1339         if (!socket || !socket->pcmcia)
1340                 return;
1341
1342         pccard_register_pcmcia(socket, NULL);
1343
1344         socket->pcmcia->state |= DS_SOCKET_DEAD;
1345         pcmcia_put_bus_socket(socket->pcmcia);
1346         socket->pcmcia = NULL;
1347
1348         return;
1349 }
1350
1351
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,
1357 };
1358
1359
1360 struct bus_type pcmcia_bus_type = {
1361         .name = "pcmcia",
1362 };
1363 EXPORT_SYMBOL(pcmcia_bus_type);
1364
1365
1366 static int __init init_pcmcia_bus(void)
1367 {
1368         int i;
1369
1370         spin_lock_init(&pcmcia_dev_list_lock);
1371
1372         bus_register(&pcmcia_bus_type);
1373         class_interface_register(&pcmcia_bus_interface);
1374
1375         /* Set up character device for user mode clients */
1376         i = register_chrdev(0, "pcmcia", &ds_fops);
1377         if (i == -EBUSY)
1378                 printk(KERN_NOTICE "unable to find a free device # for "
1379                        "Driver Services\n");
1380         else
1381                 major_dev = i;
1382
1383 #ifdef CONFIG_PROC_FS
1384         proc_pccard = proc_mkdir("pccard", proc_bus);
1385         if (proc_pccard)
1386                 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1387 #endif
1388
1389         return 0;
1390 }
1391 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
1392                                * pcmcia_socket_class is already registered */
1393
1394
1395 static void __exit exit_pcmcia_bus(void)
1396 {
1397         class_interface_unregister(&pcmcia_bus_interface);
1398
1399 #ifdef CONFIG_PROC_FS
1400         if (proc_pccard) {
1401                 remove_proc_entry("drivers", proc_pccard);
1402                 remove_proc_entry("pccard", proc_bus);
1403         }
1404 #endif
1405         if (major_dev != -1)
1406                 unregister_chrdev(major_dev, "pcmcia");
1407
1408         bus_unregister(&pcmcia_bus_type);
1409 }
1410 module_exit(exit_pcmcia_bus);
1411
1412
1413
1414 /* helpers for backwards-compatible functions */
1415
1416 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1417 {
1418         struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1419         if (s && s->pcmcia)
1420                 return s->pcmcia;
1421         else
1422                 return NULL;
1423 }
1424
1425 /* backwards-compatible accessing of driver --- by name! */
1426
1427 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1428 {
1429         struct device_driver *drv;
1430         struct pcmcia_driver *p_drv;
1431
1432         drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1433         if (!drv)
1434                 return NULL;
1435
1436         p_drv = container_of(drv, struct pcmcia_driver, drv);
1437
1438         return (p_drv);
1439 }
1440
1441 MODULE_ALIAS("ds");