]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/pcmcia/ds.c
66150d08b5c7c6d470c83a723559504b9b40dc70
[linux-3.10.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         wait_queue_head_t       queue;
95         struct pcmcia_socket    *parent;
96
97         /* the PCMCIA devices connected to this socket (normally one, more
98          * for multifunction devices: */
99         struct list_head        devices_list;
100         u8                      device_count; /* the number of devices, used
101                                                * only internally and subject
102                                                * to incorrectness and change */
103 };
104 static spinlock_t pcmcia_dev_list_lock;
105
106 #define DS_SOCKET_PRESENT               0x01
107 #define DS_SOCKET_BUSY                  0x02
108 #define DS_SOCKET_REMOVAL_PENDING       0x10
109 #define DS_SOCKET_DEAD                  0x80
110
111 /*====================================================================*/
112
113 static int major_dev = -1;
114
115 static int unbind_request(struct pcmcia_bus_socket *s);
116
117 /*====================================================================*/
118
119 /* code which was in cs.c before */
120
121 /* String tables for error messages */
122
123 typedef struct lookup_t {
124     int key;
125     char *msg;
126 } lookup_t;
127
128 static const lookup_t error_table[] = {
129     { CS_SUCCESS,               "Operation succeeded" },
130     { CS_BAD_ADAPTER,           "Bad adapter" },
131     { CS_BAD_ATTRIBUTE,         "Bad attribute", },
132     { CS_BAD_BASE,              "Bad base address" },
133     { CS_BAD_EDC,               "Bad EDC" },
134     { CS_BAD_IRQ,               "Bad IRQ" },
135     { CS_BAD_OFFSET,            "Bad offset" },
136     { CS_BAD_PAGE,              "Bad page number" },
137     { CS_READ_FAILURE,          "Read failure" },
138     { CS_BAD_SIZE,              "Bad size" },
139     { CS_BAD_SOCKET,            "Bad socket" },
140     { CS_BAD_TYPE,              "Bad type" },
141     { CS_BAD_VCC,               "Bad Vcc" },
142     { CS_BAD_VPP,               "Bad Vpp" },
143     { CS_BAD_WINDOW,            "Bad window" },
144     { CS_WRITE_FAILURE,         "Write failure" },
145     { CS_NO_CARD,               "No card present" },
146     { CS_UNSUPPORTED_FUNCTION,  "Usupported function" },
147     { CS_UNSUPPORTED_MODE,      "Unsupported mode" },
148     { CS_BAD_SPEED,             "Bad speed" },
149     { CS_BUSY,                  "Resource busy" },
150     { CS_GENERAL_FAILURE,       "General failure" },
151     { CS_WRITE_PROTECTED,       "Write protected" },
152     { CS_BAD_ARG_LENGTH,        "Bad argument length" },
153     { CS_BAD_ARGS,              "Bad arguments" },
154     { CS_CONFIGURATION_LOCKED,  "Configuration locked" },
155     { CS_IN_USE,                "Resource in use" },
156     { CS_NO_MORE_ITEMS,         "No more items" },
157     { CS_OUT_OF_RESOURCE,       "Out of resource" },
158     { CS_BAD_HANDLE,            "Bad handle" },
159     { CS_BAD_TUPLE,             "Bad CIS tuple" }
160 };
161
162
163 static const lookup_t service_table[] = {
164     { AccessConfigurationRegister,      "AccessConfigurationRegister" },
165     { AddSocketServices,                "AddSocketServices" },
166     { AdjustResourceInfo,               "AdjustResourceInfo" },
167     { CheckEraseQueue,                  "CheckEraseQueue" },
168     { CloseMemory,                      "CloseMemory" },
169     { DeregisterClient,                 "DeregisterClient" },
170     { DeregisterEraseQueue,             "DeregisterEraseQueue" },
171     { GetCardServicesInfo,              "GetCardServicesInfo" },
172     { GetClientInfo,                    "GetClientInfo" },
173     { GetConfigurationInfo,             "GetConfigurationInfo" },
174     { GetEventMask,                     "GetEventMask" },
175     { GetFirstClient,                   "GetFirstClient" },
176     { GetFirstRegion,                   "GetFirstRegion" },
177     { GetFirstTuple,                    "GetFirstTuple" },
178     { GetNextClient,                    "GetNextClient" },
179     { GetNextRegion,                    "GetNextRegion" },
180     { GetNextTuple,                     "GetNextTuple" },
181     { GetStatus,                        "GetStatus" },
182     { GetTupleData,                     "GetTupleData" },
183     { MapMemPage,                       "MapMemPage" },
184     { ModifyConfiguration,              "ModifyConfiguration" },
185     { ModifyWindow,                     "ModifyWindow" },
186     { OpenMemory,                       "OpenMemory" },
187     { ParseTuple,                       "ParseTuple" },
188     { ReadMemory,                       "ReadMemory" },
189     { RegisterClient,                   "RegisterClient" },
190     { RegisterEraseQueue,               "RegisterEraseQueue" },
191     { RegisterMTD,                      "RegisterMTD" },
192     { ReleaseConfiguration,             "ReleaseConfiguration" },
193     { ReleaseIO,                        "ReleaseIO" },
194     { ReleaseIRQ,                       "ReleaseIRQ" },
195     { ReleaseWindow,                    "ReleaseWindow" },
196     { RequestConfiguration,             "RequestConfiguration" },
197     { RequestIO,                        "RequestIO" },
198     { RequestIRQ,                       "RequestIRQ" },
199     { RequestSocketMask,                "RequestSocketMask" },
200     { RequestWindow,                    "RequestWindow" },
201     { ResetCard,                        "ResetCard" },
202     { SetEventMask,                     "SetEventMask" },
203     { ValidateCIS,                      "ValidateCIS" },
204     { WriteMemory,                      "WriteMemory" },
205     { BindDevice,                       "BindDevice" },
206     { BindMTD,                          "BindMTD" },
207     { ReportError,                      "ReportError" },
208     { SuspendCard,                      "SuspendCard" },
209     { ResumeCard,                       "ResumeCard" },
210     { EjectCard,                        "EjectCard" },
211     { InsertCard,                       "InsertCard" },
212     { ReplaceCIS,                       "ReplaceCIS" }
213 };
214
215
216 int pcmcia_report_error(client_handle_t handle, error_info_t *err)
217 {
218         int i;
219         char *serv;
220
221         if (CHECK_HANDLE(handle))
222                 printk(KERN_NOTICE);
223         else {
224                 struct pcmcia_device *p_dev = handle_to_pdev(handle);
225                 printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
226         }
227
228         for (i = 0; i < ARRAY_SIZE(service_table); i++)
229                 if (service_table[i].key == err->func)
230                         break;
231         if (i < ARRAY_SIZE(service_table))
232                 serv = service_table[i].msg;
233         else
234                 serv = "Unknown service number";
235
236         for (i = 0; i < ARRAY_SIZE(error_table); i++)
237                 if (error_table[i].key == err->retcode)
238                         break;
239         if (i < ARRAY_SIZE(error_table))
240                 printk("%s: %s\n", serv, error_table[i].msg);
241         else
242                 printk("%s: Unknown error code %#x\n", serv, err->retcode);
243
244         return CS_SUCCESS;
245 } /* report_error */
246 EXPORT_SYMBOL(pcmcia_report_error);
247
248 /* end of code which was in cs.c before */
249
250 /*======================================================================*/
251
252 void cs_error(client_handle_t handle, int func, int ret)
253 {
254         error_info_t err = { func, ret };
255         pcmcia_report_error(handle, &err);
256 }
257 EXPORT_SYMBOL(cs_error);
258
259 /*======================================================================*/
260
261 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
262 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
263
264 static void pcmcia_release_bus_socket(struct kref *refcount)
265 {
266         struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
267         pcmcia_put_socket(s->parent);
268         kfree(s);
269 }
270
271 static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
272 {
273         kref_put(&s->refcount, pcmcia_release_bus_socket);
274 }
275
276 static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
277 {
278         kref_get(&s->refcount);
279         return (s);
280 }
281
282 /**
283  * pcmcia_register_driver - register a PCMCIA driver with the bus core
284  *
285  * Registers a PCMCIA driver with the PCMCIA bus core.
286  */
287 static int pcmcia_device_probe(struct device *dev);
288 static int pcmcia_device_remove(struct device * dev);
289
290 int pcmcia_register_driver(struct pcmcia_driver *driver)
291 {
292         if (!driver)
293                 return -EINVAL;
294
295         /* initialize common fields */
296         driver->drv.bus = &pcmcia_bus_type;
297         driver->drv.owner = driver->owner;
298         driver->drv.probe = pcmcia_device_probe;
299         driver->drv.remove = pcmcia_device_remove;
300
301         return driver_register(&driver->drv);
302 }
303 EXPORT_SYMBOL(pcmcia_register_driver);
304
305 /**
306  * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
307  */
308 void pcmcia_unregister_driver(struct pcmcia_driver *driver)
309 {
310         driver_unregister(&driver->drv);
311 }
312 EXPORT_SYMBOL(pcmcia_unregister_driver);
313
314 #ifdef CONFIG_PROC_FS
315 static struct proc_dir_entry *proc_pccard = NULL;
316
317 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
318 {
319         char **p = d;
320         struct pcmcia_driver *p_drv = container_of(driver,
321                                                    struct pcmcia_driver, drv);
322
323         *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
324 #ifdef CONFIG_MODULE_UNLOAD
325                       (p_drv->owner) ? module_refcount(p_drv->owner) : 1
326 #else
327                       1
328 #endif
329         );
330         d = (void *) p;
331
332         return 0;
333 }
334
335 static int proc_read_drivers(char *buf, char **start, off_t pos,
336                              int count, int *eof, void *data)
337 {
338         char *p = buf;
339
340         bus_for_each_drv(&pcmcia_bus_type, NULL, 
341                          (void *) &p, proc_read_drivers_callback);
342
343         return (p - buf);
344 }
345 #endif
346
347 /* pcmcia_device handling */
348
349 static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
350 {
351         struct device *tmp_dev;
352         tmp_dev = get_device(&p_dev->dev);
353         if (!tmp_dev)
354                 return NULL;
355         return to_pcmcia_dev(tmp_dev);
356 }
357
358 static void pcmcia_put_dev(struct pcmcia_device *p_dev)
359 {
360         if (p_dev)
361                 put_device(&p_dev->dev);
362 }
363
364 static void pcmcia_release_dev(struct device *dev)
365 {
366         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
367         ds_dbg(1, "releasing dev %p\n", p_dev);
368         pcmcia_put_bus_socket(p_dev->socket->pcmcia);
369         kfree(p_dev);
370 }
371
372
373 static int pcmcia_device_probe(struct device * dev)
374 {
375         struct pcmcia_device *p_dev;
376         struct pcmcia_driver *p_drv;
377         int ret = 0;
378
379         dev = get_device(dev);
380         if (!dev)
381                 return -ENODEV;
382
383         p_dev = to_pcmcia_dev(dev);
384         p_drv = to_pcmcia_drv(dev->driver);
385
386         if (!try_module_get(p_drv->owner)) {
387                 ret = -EINVAL;
388                 goto put_dev;
389         }
390
391         if (p_drv->attach) {
392                 p_dev->instance = p_drv->attach();
393                 if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) {
394                         printk(KERN_NOTICE "ds: unable to create instance "
395                                "of '%s'!\n", p_drv->drv.name);
396                         ret = -EINVAL;
397                 }
398         }
399
400         if (ret)
401                 module_put(p_drv->owner);
402  put_dev:
403         if ((ret) || !(p_drv->attach))
404                 put_device(dev);
405         return (ret);
406 }
407
408
409 static int pcmcia_device_remove(struct device * dev)
410 {
411         struct pcmcia_device *p_dev;
412         struct pcmcia_driver *p_drv;
413
414         /* detach the "instance" */
415         p_dev = to_pcmcia_dev(dev);
416         p_drv = to_pcmcia_drv(dev->driver);
417
418         if (p_drv) {
419                 if ((p_drv->detach) && (p_dev->instance)) {
420                         p_drv->detach(p_dev->instance);
421                         /* from pcmcia_probe_device */
422                         put_device(&p_dev->dev);
423                 }
424                 module_put(p_drv->owner);
425         }
426
427         return 0;
428 }
429
430
431
432 /*
433  * pcmcia_device_query -- determine information about a pcmcia device
434  */
435 static int pcmcia_device_query(struct pcmcia_device *p_dev)
436 {
437         cistpl_manfid_t manf_id;
438         cistpl_funcid_t func_id;
439         cistpl_vers_1_t vers1;
440         unsigned int i;
441
442         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
443                                CISTPL_MANFID, &manf_id)) {
444                 p_dev->manf_id = manf_id.manf;
445                 p_dev->card_id = manf_id.card;
446                 p_dev->has_manf_id = 1;
447                 p_dev->has_card_id = 1;
448         }
449
450         if (!pccard_read_tuple(p_dev->socket, p_dev->func,
451                                CISTPL_FUNCID, &func_id)) {
452                 p_dev->func_id = func_id.func;
453                 p_dev->has_func_id = 1;
454         } else {
455                 /* rule of thumb: cards with no FUNCID, but with
456                  * common memory device geometry information, are
457                  * probably memory cards (from pcmcia-cs) */
458                 cistpl_device_geo_t devgeo;
459                 if (!pccard_read_tuple(p_dev->socket, p_dev->func,
460                                       CISTPL_DEVICE_GEO, &devgeo)) {
461                         ds_dbg(0, "mem device geometry probably means "
462                                "FUNCID_MEMORY\n");
463                         p_dev->func_id = CISTPL_FUNCID_MEMORY;
464                         p_dev->has_func_id = 1;
465                 }
466         }
467
468         if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
469                                &vers1)) {
470                 for (i=0; i < vers1.ns; i++) {
471                         char *tmp;
472                         unsigned int length;
473
474                         tmp = vers1.str + vers1.ofs[i];
475
476                         length = strlen(tmp) + 1;
477                         if ((length < 3) || (length > 255))
478                                 continue;
479
480                         p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
481                                                     GFP_KERNEL);
482                         if (!p_dev->prod_id[i])
483                                 continue;
484
485                         p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
486                                                     tmp, length);
487                 }
488         }
489
490         return 0;
491 }
492
493
494 /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
495  * Serializes pcmcia_device_add; will most likely be removed in future.
496  *
497  * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
498  * won't work, this doesn't matter much at the moment: the driver core doesn't
499  * support it either.
500  */
501 static DECLARE_MUTEX(device_add_lock);
502
503 static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
504 {
505         struct pcmcia_device *p_dev;
506         unsigned long flags;
507
508         s = pcmcia_get_bus_socket(s);
509         if (!s)
510                 return NULL;
511
512         down(&device_add_lock);
513
514         p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
515         if (!p_dev)
516                 goto err_put;
517         memset(p_dev, 0, sizeof(struct pcmcia_device));
518
519         p_dev->socket = s->parent;
520         p_dev->device_no = (s->device_count++);
521         p_dev->func   = function;
522
523         p_dev->dev.bus = &pcmcia_bus_type;
524         p_dev->dev.parent = s->parent->dev.dev;
525         p_dev->dev.release = pcmcia_release_dev;
526         sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
527
528         /* compat */
529         p_dev->client.client_magic = CLIENT_MAGIC;
530         p_dev->client.Socket = s->parent;
531         p_dev->client.Function = function;
532         p_dev->client.state = CLIENT_UNBOUND;
533
534         /* Add to the list in pcmcia_bus_socket */
535         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
536         list_add_tail(&p_dev->socket_device_list, &s->devices_list);
537         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
538
539         if (device_register(&p_dev->dev)) {
540                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
541                 list_del(&p_dev->socket_device_list);
542                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
543
544                 goto err_free;
545        }
546
547         up(&device_add_lock);
548
549         return p_dev;
550
551  err_free:
552         kfree(p_dev);
553         s->device_count--;
554  err_put:
555         up(&device_add_lock);
556         pcmcia_put_bus_socket(s);
557
558         return NULL;
559 }
560
561
562 static int pcmcia_card_add(struct pcmcia_socket *s)
563 {
564         cisinfo_t cisinfo;
565         cistpl_longlink_mfc_t mfc;
566         unsigned int no_funcs, i;
567         int ret = 0;
568
569         if (!(s->resource_setup_done))
570                 return -EAGAIN; /* try again, but later... */
571
572         pcmcia_validate_mem(s);
573         ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
574         if (ret || !cisinfo.Chains) {
575                 ds_dbg(0, "invalid CIS or invalid resources\n");
576                 return -ENODEV;
577         }
578
579         if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
580                 no_funcs = mfc.nfn;
581         else
582                 no_funcs = 1;
583
584         /* this doesn't handle multifunction devices on one pcmcia function
585          * yet. */
586         for (i=0; i < no_funcs; i++)
587                 pcmcia_device_add(s->pcmcia, i);
588
589         return (ret);
590 }
591
592
593 static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
594         struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
595         struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
596
597         /* matching by cardmgr */
598         if (p_dev->cardmgr == p_drv)
599                 return 1;
600
601         return 0;
602 }
603
604 /************************ per-device sysfs output ***************************/
605
606 #define pcmcia_device_attr(field, test, format)                         \
607 static ssize_t field##_show (struct device *dev, char *buf)             \
608 {                                                                       \
609         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
610         return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
611 }
612
613 #define pcmcia_device_stringattr(name, field)                                   \
614 static ssize_t name##_show (struct device *dev, char *buf)              \
615 {                                                                       \
616         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);               \
617         return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
618 }
619
620 pcmcia_device_attr(func, socket, "0x%02x\n");
621 pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
622 pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
623 pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
624 pcmcia_device_stringattr(prod_id1, prod_id[0]);
625 pcmcia_device_stringattr(prod_id2, prod_id[1]);
626 pcmcia_device_stringattr(prod_id3, prod_id[2]);
627 pcmcia_device_stringattr(prod_id4, prod_id[3]);
628
629 static struct device_attribute pcmcia_dev_attrs[] = {
630         __ATTR(function, 0444, func_show, NULL),
631         __ATTR_RO(func_id),
632         __ATTR_RO(manf_id),
633         __ATTR_RO(card_id),
634         __ATTR_RO(prod_id1),
635         __ATTR_RO(prod_id2),
636         __ATTR_RO(prod_id3),
637         __ATTR_RO(prod_id4),
638         __ATTR_NULL,
639 };
640
641
642 /*======================================================================
643
644     These manage a ring buffer of events pending for one user process
645     
646 ======================================================================*/
647
648 static int queue_empty(user_info_t *user)
649 {
650     return (user->event_head == user->event_tail);
651 }
652
653 static event_t get_queued_event(user_info_t *user)
654 {
655     user->event_tail = (user->event_tail+1) % MAX_EVENTS;
656     return user->event[user->event_tail];
657 }
658
659 static void queue_event(user_info_t *user, event_t event)
660 {
661     user->event_head = (user->event_head+1) % MAX_EVENTS;
662     if (user->event_head == user->event_tail)
663         user->event_tail = (user->event_tail+1) % MAX_EVENTS;
664     user->event[user->event_head] = event;
665 }
666
667 static void handle_event(struct pcmcia_bus_socket *s, event_t event)
668 {
669     user_info_t *user;
670     for (user = s->user; user; user = user->next)
671         queue_event(user, event);
672     wake_up_interruptible(&s->queue);
673 }
674
675
676 /*======================================================================
677
678     The card status event handler.
679     
680 ======================================================================*/
681
682 struct send_event_data {
683         struct pcmcia_socket *skt;
684         event_t event;
685         int priority;
686 };
687
688 static int send_event_callback(struct device *dev, void * _data)
689 {
690         struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
691         struct send_event_data *data = _data;
692
693         /* we get called for all sockets, but may only pass the event
694          * for drivers _on the affected socket_ */
695         if (p_dev->socket != data->skt)
696                 return 0;
697
698         if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE))
699                 return 0;
700
701         if (p_dev->client.EventMask & data->event)
702                 return EVENT(&p_dev->client, data->event, data->priority);
703
704         return 0;
705 }
706
707 static int send_event(struct pcmcia_socket *s, event_t event, int priority)
708 {
709         int ret = 0;
710         struct send_event_data private;
711         struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia);
712
713         if (!skt)
714                 return 0;
715
716         private.skt = s;
717         private.event = event;
718         private.priority = priority;
719
720         ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
721
722         pcmcia_put_bus_socket(skt);
723         return ret;
724 } /* send_event */
725
726
727 /* Normally, the event is passed to individual drivers after
728  * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
729  * is inversed to maintain historic compatibility.
730  */
731
732 static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
733 {
734         struct pcmcia_bus_socket *s = skt->pcmcia;
735         int ret = 0;
736
737         ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
738                event, priority, s);
739     
740         switch (event) {
741
742         case CS_EVENT_CARD_REMOVAL:
743                 s->state &= ~DS_SOCKET_PRESENT;
744                 send_event(skt, event, priority);
745                 unbind_request(s);
746                 handle_event(s, event);
747                 break;
748         
749         case CS_EVENT_CARD_INSERTION:
750                 s->state |= DS_SOCKET_PRESENT;
751                 pcmcia_card_add(skt);
752                 handle_event(s, event);
753                 break;
754
755         case CS_EVENT_EJECTION_REQUEST:
756                 ret = send_event(skt, event, priority);
757                 break;
758
759         default:
760                 handle_event(s, event);
761                 send_event(skt, event, priority);
762                 break;
763     }
764
765     return 0;
766 } /* ds_event */
767
768
769 /*======================================================================
770
771     bind_request() and bind_device() are merged by now. Register_client()
772     is called right at the end of bind_request(), during the driver's
773     ->attach() call. Individual descriptions:
774
775     bind_request() connects a socket to a particular client driver.
776     It looks up the specified device ID in the list of registered
777     drivers, binds it to the socket, and tries to create an instance
778     of the device.  unbind_request() deletes a driver instance.
779     
780     Bind_device() associates a device driver with a particular socket.
781     It is normally called by Driver Services after it has identified
782     a newly inserted card.  An instance of that driver will then be
783     eligible to register as a client of this socket.
784
785     Register_client() uses the dev_info_t handle to match the
786     caller with a socket.  The driver must have already been bound
787     to a socket with bind_device() -- in fact, bind_device()
788     allocates the client structure that will be used.
789
790 ======================================================================*/
791
792 static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
793 {
794         struct pcmcia_driver *p_drv;
795         struct pcmcia_device *p_dev;
796         int ret = 0;
797         unsigned long flags;
798
799         s = pcmcia_get_bus_socket(s);
800         if (!s)
801                 return -EINVAL;
802
803         ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
804                (char *)bind_info->dev_info);
805
806         p_drv = get_pcmcia_driver(&bind_info->dev_info);
807         if (!p_drv) {
808                 ret = -EINVAL;
809                 goto err_put;
810         }
811
812         if (!try_module_get(p_drv->owner)) {
813                 ret = -EINVAL;
814                 goto err_put_driver;
815         }
816
817         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
818         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
819                 if (p_dev->func == bind_info->function) {
820                         if ((p_dev->dev.driver == &p_drv->drv)) {
821                                 if (p_dev->cardmgr) {
822                                         /* if there's already a device
823                                          * registered, and it was registered
824                                          * by userspace before, we need to
825                                          * return the "instance". */
826                                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
827                                         bind_info->instance = p_dev->instance;
828                                         ret = -EBUSY;
829                                         goto err_put_module;
830                                 } else {
831                                         /* the correct driver managed to bind
832                                          * itself magically to the correct
833                                          * device. */
834                                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
835                                         p_dev->cardmgr = p_drv;
836                                         ret = 0;
837                                         goto err_put_module;
838                                 }
839                         } else if (!p_dev->dev.driver) {
840                                 /* there's already a device available where
841                                  * no device has been bound to yet. So we don't
842                                  * need to register a device! */
843                                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
844                                 goto rescan;
845                         }
846                 }
847         }
848         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
849
850         p_dev = pcmcia_device_add(s, bind_info->function);
851         if (!p_dev) {
852                 ret = -EIO;
853                 goto err_put_module;
854         }
855
856 rescan:
857         p_dev->cardmgr = p_drv;
858
859         pcmcia_device_query(p_dev);
860
861         /*
862          * Prevent this racing with a card insertion.
863          */
864         down(&s->parent->skt_sem);
865         bus_rescan_devices(&pcmcia_bus_type);
866         up(&s->parent->skt_sem);
867
868         /* check whether the driver indeed matched. I don't care if this
869          * is racy or not, because it can only happen on cardmgr access
870          * paths...
871          */
872         if (!(p_dev->dev.driver == &p_drv->drv))
873                 p_dev->cardmgr = NULL;
874
875  err_put_module:
876         module_put(p_drv->owner);
877  err_put_driver:
878         put_driver(&p_drv->drv);
879  err_put:
880         pcmcia_put_bus_socket(s);
881
882         return (ret);
883 } /* bind_request */
884
885
886 int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
887 {
888         client_t *client = NULL;
889         struct pcmcia_socket *s;
890         struct pcmcia_bus_socket *skt = NULL;
891         struct pcmcia_device *p_dev = NULL;
892
893         /* Look for unbound client with matching dev_info */
894         down_read(&pcmcia_socket_list_rwsem);
895         list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
896                 unsigned long flags;
897
898                 if (s->state & SOCKET_CARDBUS)
899                         continue;
900
901                 skt = s->pcmcia;
902                 if (!skt)
903                         continue;
904                 skt = pcmcia_get_bus_socket(skt);
905                 if (!skt)
906                         continue;
907                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
908                 list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) {
909                         struct pcmcia_driver *p_drv;
910                         p_dev = pcmcia_get_dev(p_dev);
911                         if (!p_dev)
912                                 continue;
913                         if (!(p_dev->client.state & CLIENT_UNBOUND) ||
914                             (!p_dev->dev.driver)) {
915                                 pcmcia_put_dev(p_dev);
916                                 continue;
917                         }
918                         p_drv = to_pcmcia_drv(p_dev->dev.driver);
919                         if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
920                                 client = &p_dev->client;
921                                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
922                                 goto found;
923                         }
924                         pcmcia_put_dev(p_dev);
925                 }
926                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
927                 pcmcia_put_bus_socket(skt);
928         }
929  found:
930         up_read(&pcmcia_socket_list_rwsem);
931         if (!p_dev || !client)
932                 return -ENODEV;
933
934         pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */
935
936         *handle = client;
937         client->state &= ~CLIENT_UNBOUND;
938         client->Socket = s;
939         client->EventMask = req->EventMask;
940         client->event_handler = req->event_handler;
941         client->event_callback_args = req->event_callback_args;
942         client->event_callback_args.client_handle = client;
943
944         if (s->state & SOCKET_CARDBUS)
945                 client->state |= CLIENT_CARDBUS;
946
947         if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) &&
948             (client->Function != BIND_FN_ALL)) {
949                 cistpl_longlink_mfc_t mfc;
950                 if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc)
951                     == CS_SUCCESS)
952                         s->functions = mfc.nfn;
953                 else
954                         s->functions = 1;
955                 s->config = kmalloc(sizeof(config_t) * s->functions,
956                                     GFP_KERNEL);
957                 if (!s->config)
958                         goto out_no_resource;
959                 memset(s->config, 0, sizeof(config_t) * s->functions);
960         }
961
962         ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
963                client, p_dev->dev.bus_id);
964         if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE)
965                 EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW);
966
967         if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
968                 if (client->EventMask & CS_EVENT_CARD_INSERTION)
969                         EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW);
970         }
971
972         return CS_SUCCESS;
973
974  out_no_resource:
975         pcmcia_put_dev(p_dev);
976         return CS_OUT_OF_RESOURCE;
977 } /* register_client */
978 EXPORT_SYMBOL(pcmcia_register_client);
979
980
981 /*====================================================================*/
982
983 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
984
985 static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
986 {
987         dev_node_t *node;
988         struct pcmcia_device *p_dev;
989         unsigned long flags;
990         int ret = 0;
991
992 #ifdef CONFIG_CARDBUS
993         /*
994          * Some unbelievably ugly code to associate the PCI cardbus
995          * device and its driver with the PCMCIA "bind" information.
996          */
997         {
998                 struct pci_bus *bus;
999
1000                 bus = pcmcia_lookup_bus(s->parent);
1001                 if (bus) {
1002                         struct list_head *list;
1003                         struct pci_dev *dev = NULL;
1004
1005                         list = bus->devices.next;
1006                         while (list != &bus->devices) {
1007                                 struct pci_dev *pdev = pci_dev_b(list);
1008                                 list = list->next;
1009
1010                                 if (first) {
1011                                         dev = pdev;
1012                                         break;
1013                                 }
1014
1015                                 /* Try to handle "next" here some way? */
1016                         }
1017                         if (dev && dev->driver) {
1018                                 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1019                                 bind_info->major = 0;
1020                                 bind_info->minor = 0;
1021                                 bind_info->next = NULL;
1022                                 return 0;
1023                         }
1024                 }
1025         }
1026 #endif
1027
1028         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1029         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1030                 if (p_dev->func == bind_info->function) {
1031                         p_dev = pcmcia_get_dev(p_dev);
1032                         if (!p_dev)
1033                                 continue;
1034                         goto found;
1035                 }
1036         }
1037         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1038         return -ENODEV;
1039
1040  found:
1041         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1042
1043         if ((!p_dev->instance) ||
1044             (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1045                 ret = -EAGAIN;
1046                 goto err_put;
1047         }
1048
1049         if (first)
1050                 node = p_dev->instance->dev;
1051         else
1052                 for (node = p_dev->instance->dev; node; node = node->next)
1053                         if (node == bind_info->next)
1054                                 break;
1055         if (!node) {
1056                 ret = -ENODEV;
1057                 goto err_put;
1058         }
1059
1060         strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1061         bind_info->major = node->major;
1062         bind_info->minor = node->minor;
1063         bind_info->next = node->next;
1064
1065  err_put:
1066         pcmcia_put_dev(p_dev);
1067         return (ret);
1068 } /* get_device_info */
1069
1070 /*====================================================================*/
1071
1072 /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1073  * drivers have been called with EVENT_CARD_REMOVAL before.
1074  */
1075 static int unbind_request(struct pcmcia_bus_socket *s)
1076 {
1077         struct pcmcia_device    *p_dev;
1078         unsigned long           flags;
1079
1080         ds_dbg(2, "unbind_request(%d)\n", s->parent->sock);
1081
1082         s->device_count = 0;
1083
1084         for (;;) {
1085                 /* unregister all pcmcia_devices registered with this socket*/
1086                 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1087                 if (list_empty(&s->devices_list)) {
1088                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1089                         return 0;
1090                 }
1091                 p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
1092                 list_del(&p_dev->socket_device_list);
1093                 p_dev->client.state |= CLIENT_STALE;
1094                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1095
1096                 device_unregister(&p_dev->dev);
1097         }
1098
1099         return 0;
1100 } /* unbind_request */
1101
1102 int pcmcia_deregister_client(client_handle_t handle)
1103 {
1104         struct pcmcia_socket *s;
1105         int i;
1106         struct pcmcia_device *p_dev = handle_to_pdev(handle);
1107
1108         if (CHECK_HANDLE(handle))
1109                 return CS_BAD_HANDLE;
1110
1111         s = SOCKET(handle);
1112         ds_dbg(1, "deregister_client(%p)\n", handle);
1113
1114         if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
1115                 goto warn_out;
1116         for (i = 0; i < MAX_WIN; i++)
1117                 if (handle->state & CLIENT_WIN_REQ(i))
1118                         goto warn_out;
1119
1120         if (handle->state & CLIENT_STALE) {
1121                 handle->client_magic = 0;
1122                 handle->state &= ~CLIENT_STALE;
1123                 pcmcia_put_dev(p_dev);
1124         } else {
1125                 handle->state = CLIENT_UNBOUND;
1126                 handle->event_handler = NULL;
1127         }
1128
1129         return CS_SUCCESS;
1130  warn_out:
1131         printk(KERN_WARNING "ds: deregister_client was called too early.\n");
1132         return CS_IN_USE;
1133 } /* deregister_client */
1134 EXPORT_SYMBOL(pcmcia_deregister_client);
1135
1136
1137 /*======================================================================
1138
1139     The user-mode PC Card device interface
1140
1141 ======================================================================*/
1142
1143 static int ds_open(struct inode *inode, struct file *file)
1144 {
1145     socket_t i = iminor(inode);
1146     struct pcmcia_bus_socket *s;
1147     user_info_t *user;
1148
1149     ds_dbg(0, "ds_open(socket %d)\n", i);
1150
1151     s = get_socket_info_by_nr(i);
1152     if (!s)
1153             return -ENODEV;
1154     s = pcmcia_get_bus_socket(s);
1155     if (!s)
1156             return -ENODEV;
1157
1158     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1159             if (s->state & DS_SOCKET_BUSY) {
1160                     pcmcia_put_bus_socket(s);
1161                     return -EBUSY;
1162             }
1163         else
1164             s->state |= DS_SOCKET_BUSY;
1165     }
1166     
1167     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1168     if (!user) {
1169             pcmcia_put_bus_socket(s);
1170             return -ENOMEM;
1171     }
1172     user->event_tail = user->event_head = 0;
1173     user->next = s->user;
1174     user->user_magic = USER_MAGIC;
1175     user->socket = s;
1176     s->user = user;
1177     file->private_data = user;
1178     
1179     if (s->state & DS_SOCKET_PRESENT)
1180         queue_event(user, CS_EVENT_CARD_INSERTION);
1181     return 0;
1182 } /* ds_open */
1183
1184 /*====================================================================*/
1185
1186 static int ds_release(struct inode *inode, struct file *file)
1187 {
1188     struct pcmcia_bus_socket *s;
1189     user_info_t *user, **link;
1190
1191     ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1192
1193     user = file->private_data;
1194     if (CHECK_USER(user))
1195         goto out;
1196
1197     s = user->socket;
1198
1199     /* Unlink user data structure */
1200     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1201         s->state &= ~DS_SOCKET_BUSY;
1202     }
1203     file->private_data = NULL;
1204     for (link = &s->user; *link; link = &(*link)->next)
1205         if (*link == user) break;
1206     if (link == NULL)
1207         goto out;
1208     *link = user->next;
1209     user->user_magic = 0;
1210     kfree(user);
1211     pcmcia_put_bus_socket(s);
1212 out:
1213     return 0;
1214 } /* ds_release */
1215
1216 /*====================================================================*/
1217
1218 static ssize_t ds_read(struct file *file, char __user *buf,
1219                        size_t count, loff_t *ppos)
1220 {
1221     struct pcmcia_bus_socket *s;
1222     user_info_t *user;
1223     int ret;
1224
1225     ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1226     
1227     if (count < 4)
1228         return -EINVAL;
1229
1230     user = file->private_data;
1231     if (CHECK_USER(user))
1232         return -EIO;
1233     
1234     s = user->socket;
1235     if (s->state & DS_SOCKET_DEAD)
1236         return -EIO;
1237
1238     ret = wait_event_interruptible(s->queue, !queue_empty(user));
1239     if (ret == 0)
1240         ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1241
1242     return ret;
1243 } /* ds_read */
1244
1245 /*====================================================================*/
1246
1247 static ssize_t ds_write(struct file *file, const char __user *buf,
1248                         size_t count, loff_t *ppos)
1249 {
1250     ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1251
1252     if (count != 4)
1253         return -EINVAL;
1254     if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1255         return -EBADF;
1256
1257     return -EIO;
1258 } /* ds_write */
1259
1260 /*====================================================================*/
1261
1262 /* No kernel lock - fine */
1263 static u_int ds_poll(struct file *file, poll_table *wait)
1264 {
1265     struct pcmcia_bus_socket *s;
1266     user_info_t *user;
1267
1268     ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1269     
1270     user = file->private_data;
1271     if (CHECK_USER(user))
1272         return POLLERR;
1273     s = user->socket;
1274     /*
1275      * We don't check for a dead socket here since that
1276      * will send cardmgr into an endless spin.
1277      */
1278     poll_wait(file, &s->queue, wait);
1279     if (!queue_empty(user))
1280         return POLLIN | POLLRDNORM;
1281     return 0;
1282 } /* ds_poll */
1283
1284 /*====================================================================*/
1285
1286 extern int pcmcia_adjust_resource_info(adjust_t *adj);
1287
1288 static int ds_ioctl(struct inode * inode, struct file * file,
1289                     u_int cmd, u_long arg)
1290 {
1291     struct pcmcia_bus_socket *s;
1292     void __user *uarg = (char __user *)arg;
1293     u_int size;
1294     int ret, err;
1295     ds_ioctl_arg_t *buf;
1296     user_info_t *user;
1297
1298     ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1299     
1300     user = file->private_data;
1301     if (CHECK_USER(user))
1302         return -EIO;
1303
1304     s = user->socket;
1305     if (s->state & DS_SOCKET_DEAD)
1306         return -EIO;
1307     
1308     size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1309     if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1310
1311     /* Permission check */
1312     if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1313         return -EPERM;
1314         
1315     if (cmd & IOC_IN) {
1316         if (!access_ok(VERIFY_READ, uarg, size)) {
1317             ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1318             return -EFAULT;
1319         }
1320     }
1321     if (cmd & IOC_OUT) {
1322         if (!access_ok(VERIFY_WRITE, uarg, size)) {
1323             ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1324             return -EFAULT;
1325         }
1326     }
1327     buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1328     if (!buf)
1329         return -ENOMEM;
1330     
1331     err = ret = 0;
1332     
1333     if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1334     
1335     switch (cmd) {
1336     case DS_ADJUST_RESOURCE_INFO:
1337         ret = pcmcia_adjust_resource_info(&buf->adjust);
1338         break;
1339     case DS_GET_CARD_SERVICES_INFO:
1340         ret = pcmcia_get_card_services_info(&buf->servinfo);
1341         break;
1342     case DS_GET_CONFIGURATION_INFO:
1343         if (buf->config.Function &&
1344            (buf->config.Function >= s->parent->functions))
1345             ret = CS_BAD_ARGS;
1346         else
1347             ret = pccard_get_configuration_info(s->parent,
1348                         buf->config.Function, &buf->config);
1349         break;
1350     case DS_GET_FIRST_TUPLE:
1351         down(&s->parent->skt_sem);
1352         pcmcia_validate_mem(s->parent);
1353         up(&s->parent->skt_sem);
1354         ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1355         break;
1356     case DS_GET_NEXT_TUPLE:
1357         ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1358         break;
1359     case DS_GET_TUPLE_DATA:
1360         buf->tuple.TupleData = buf->tuple_parse.data;
1361         buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1362         ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1363         break;
1364     case DS_PARSE_TUPLE:
1365         buf->tuple.TupleData = buf->tuple_parse.data;
1366         ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1367         break;
1368     case DS_RESET_CARD:
1369         ret = pccard_reset_card(s->parent);
1370         break;
1371     case DS_GET_STATUS:
1372         if (buf->status.Function &&
1373            (buf->status.Function >= s->parent->functions))
1374             ret = CS_BAD_ARGS;
1375         else
1376         ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1377         break;
1378     case DS_VALIDATE_CIS:
1379         down(&s->parent->skt_sem);
1380         pcmcia_validate_mem(s->parent);
1381         up(&s->parent->skt_sem);
1382         ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1383         break;
1384     case DS_SUSPEND_CARD:
1385         ret = pcmcia_suspend_card(s->parent);
1386         break;
1387     case DS_RESUME_CARD:
1388         ret = pcmcia_resume_card(s->parent);
1389         break;
1390     case DS_EJECT_CARD:
1391         err = pcmcia_eject_card(s->parent);
1392         break;
1393     case DS_INSERT_CARD:
1394         err = pcmcia_insert_card(s->parent);
1395         break;
1396     case DS_ACCESS_CONFIGURATION_REGISTER:
1397         if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1398             err = -EPERM;
1399             goto free_out;
1400         }
1401         if (buf->conf_reg.Function &&
1402            (buf->conf_reg.Function >= s->parent->functions))
1403             ret = CS_BAD_ARGS;
1404         else
1405             ret = pccard_access_configuration_register(s->parent,
1406                         buf->conf_reg.Function, &buf->conf_reg);
1407         break;
1408     case DS_GET_FIRST_REGION:
1409     case DS_GET_NEXT_REGION:
1410     case DS_BIND_MTD:
1411         if (!capable(CAP_SYS_ADMIN)) {
1412                 err = -EPERM;
1413                 goto free_out;
1414         } else {
1415                 static int printed = 0;
1416                 if (!printed) {
1417                         printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1418                         printk(KERN_WARNING "MTD handling any more.\n");
1419                         printed++;
1420                 }
1421         }
1422         err = -EINVAL;
1423         goto free_out;
1424         break;
1425     case DS_GET_FIRST_WINDOW:
1426         ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1427                         &buf->win_info.window);
1428         break;
1429     case DS_GET_NEXT_WINDOW:
1430         ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1431                         buf->win_info.handle->index + 1, &buf->win_info.window);
1432         break;
1433     case DS_GET_MEM_PAGE:
1434         ret = pcmcia_get_mem_page(buf->win_info.handle,
1435                            &buf->win_info.map);
1436         break;
1437     case DS_REPLACE_CIS:
1438         ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1439         break;
1440     case DS_BIND_REQUEST:
1441         if (!capable(CAP_SYS_ADMIN)) {
1442                 err = -EPERM;
1443                 goto free_out;
1444         }
1445         err = bind_request(s, &buf->bind_info);
1446         break;
1447     case DS_GET_DEVICE_INFO:
1448         err = get_device_info(s, &buf->bind_info, 1);
1449         break;
1450     case DS_GET_NEXT_DEVICE:
1451         err = get_device_info(s, &buf->bind_info, 0);
1452         break;
1453     case DS_UNBIND_REQUEST:
1454         err = 0;
1455         break;
1456     default:
1457         err = -EINVAL;
1458     }
1459     
1460     if ((err == 0) && (ret != CS_SUCCESS)) {
1461         ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1462         switch (ret) {
1463         case CS_BAD_SOCKET: case CS_NO_CARD:
1464             err = -ENODEV; break;
1465         case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1466         case CS_BAD_TUPLE:
1467             err = -EINVAL; break;
1468         case CS_IN_USE:
1469             err = -EBUSY; break;
1470         case CS_OUT_OF_RESOURCE:
1471             err = -ENOSPC; break;
1472         case CS_NO_MORE_ITEMS:
1473             err = -ENODATA; break;
1474         case CS_UNSUPPORTED_FUNCTION:
1475             err = -ENOSYS; break;
1476         default:
1477             err = -EIO; break;
1478         }
1479     }
1480
1481     if (cmd & IOC_OUT) {
1482         if (__copy_to_user(uarg, (char *)buf, size))
1483             err = -EFAULT;
1484     }
1485
1486 free_out:
1487     kfree(buf);
1488     return err;
1489 } /* ds_ioctl */
1490
1491 /*====================================================================*/
1492
1493 static struct file_operations ds_fops = {
1494         .owner          = THIS_MODULE,
1495         .open           = ds_open,
1496         .release        = ds_release,
1497         .ioctl          = ds_ioctl,
1498         .read           = ds_read,
1499         .write          = ds_write,
1500         .poll           = ds_poll,
1501 };
1502
1503 static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1504 {
1505         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1506         struct pcmcia_bus_socket *s;
1507         int ret;
1508
1509         s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL);
1510         if(!s)
1511                 return -ENOMEM;
1512         memset(s, 0, sizeof(struct pcmcia_bus_socket));
1513
1514         /* get reference to parent socket */
1515         s->parent = pcmcia_get_socket(socket);
1516         if (!s->parent) {
1517                 printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
1518                 kfree (s);
1519                 return -ENODEV;
1520         }
1521
1522         kref_init(&s->refcount);
1523     
1524         /*
1525          * Ugly. But we want to wait for the socket threads to have started up.
1526          * We really should let the drivers themselves drive some of this..
1527          */
1528         msleep(250);
1529
1530         init_waitqueue_head(&s->queue);
1531         INIT_LIST_HEAD(&s->devices_list);
1532
1533         /* Set up hotline to Card Services */
1534         s->callback.owner = THIS_MODULE;
1535         s->callback.event = &ds_event;
1536         s->callback.resources_done = &pcmcia_card_add;
1537         socket->pcmcia = s;
1538
1539         ret = pccard_register_pcmcia(socket, &s->callback);
1540         if (ret) {
1541                 printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
1542                 pcmcia_put_bus_socket(s);
1543                 socket->pcmcia = NULL;
1544                 return (ret);
1545         }
1546
1547         return 0;
1548 }
1549
1550
1551 static void pcmcia_bus_remove_socket(struct class_device *class_dev)
1552 {
1553         struct pcmcia_socket *socket = class_get_devdata(class_dev);
1554
1555         if (!socket || !socket->pcmcia)
1556                 return;
1557
1558         pccard_register_pcmcia(socket, NULL);
1559
1560         socket->pcmcia->state |= DS_SOCKET_DEAD;
1561         pcmcia_put_bus_socket(socket->pcmcia);
1562         socket->pcmcia = NULL;
1563
1564         return;
1565 }
1566
1567
1568 /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
1569 static struct class_interface pcmcia_bus_interface = {
1570         .class = &pcmcia_socket_class,
1571         .add = &pcmcia_bus_add_socket,
1572         .remove = &pcmcia_bus_remove_socket,
1573 };
1574
1575
1576 struct bus_type pcmcia_bus_type = {
1577         .name = "pcmcia",
1578         .match = pcmcia_bus_match,
1579         .dev_attrs = pcmcia_dev_attrs,
1580 };
1581 EXPORT_SYMBOL(pcmcia_bus_type);
1582
1583
1584 static int __init init_pcmcia_bus(void)
1585 {
1586         int i;
1587
1588         spin_lock_init(&pcmcia_dev_list_lock);
1589
1590         bus_register(&pcmcia_bus_type);
1591         class_interface_register(&pcmcia_bus_interface);
1592
1593         /* Set up character device for user mode clients */
1594         i = register_chrdev(0, "pcmcia", &ds_fops);
1595         if (i == -EBUSY)
1596                 printk(KERN_NOTICE "unable to find a free device # for "
1597                        "Driver Services\n");
1598         else
1599                 major_dev = i;
1600
1601 #ifdef CONFIG_PROC_FS
1602         proc_pccard = proc_mkdir("pccard", proc_bus);
1603         if (proc_pccard)
1604                 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1605 #endif
1606
1607         return 0;
1608 }
1609 fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that 
1610                                * pcmcia_socket_class is already registered */
1611
1612
1613 static void __exit exit_pcmcia_bus(void)
1614 {
1615         class_interface_unregister(&pcmcia_bus_interface);
1616
1617 #ifdef CONFIG_PROC_FS
1618         if (proc_pccard) {
1619                 remove_proc_entry("drivers", proc_pccard);
1620                 remove_proc_entry("pccard", proc_bus);
1621         }
1622 #endif
1623         if (major_dev != -1)
1624                 unregister_chrdev(major_dev, "pcmcia");
1625
1626         bus_unregister(&pcmcia_bus_type);
1627 }
1628 module_exit(exit_pcmcia_bus);
1629
1630
1631
1632 /* helpers for backwards-compatible functions */
1633
1634 static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
1635 {
1636         struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
1637         if (s && s->pcmcia)
1638                 return s->pcmcia;
1639         else
1640                 return NULL;
1641 }
1642
1643 /* backwards-compatible accessing of driver --- by name! */
1644
1645 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
1646 {
1647         struct device_driver *drv;
1648         struct pcmcia_driver *p_drv;
1649
1650         drv = driver_find((char *) dev_info, &pcmcia_bus_type);
1651         if (!drv)
1652                 return NULL;
1653
1654         p_drv = container_of(drv, struct pcmcia_driver, drv);
1655
1656         return (p_drv);
1657 }
1658
1659 MODULE_ALIAS("ds");