[PATCH] pcmcia: remove include of config.h
[safe/jmp/linux-2.6] / drivers / pcmcia / pcmcia_ioctl.c
1 /*
2  * pcmcia_ioctl.c -- ioctl interface for cardmgr and cardctl
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 /*
17  * This file will go away soon.
18  */
19
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/major.h>
25 #include <linux/errno.h>
26 #include <linux/ioctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/poll.h>
29 #include <linux/pci.h>
30 #include <linux/workqueue.h>
31
32 #define IN_CARD_SERVICES
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/cs.h>
35 #include <pcmcia/cistpl.h>
36 #include <pcmcia/ds.h>
37 #include <pcmcia/ss.h>
38
39 #include "cs_internal.h"
40 #include "ds_internal.h"
41
42 static int major_dev = -1;
43
44
45 /* Device user information */
46 #define MAX_EVENTS      32
47 #define USER_MAGIC      0x7ea4
48 #define CHECK_USER(u) \
49     (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
50
51 typedef struct user_info_t {
52         u_int                   user_magic;
53         int                     event_head, event_tail;
54         event_t                 event[MAX_EVENTS];
55         struct user_info_t      *next;
56         struct pcmcia_socket    *socket;
57 } user_info_t;
58
59
60 #ifdef DEBUG
61 extern int ds_pc_debug;
62 #define cs_socket_name(skt)    ((skt)->dev.class_id)
63
64 #define ds_dbg(lvl, fmt, arg...) do {           \
65         if (ds_pc_debug >= lvl)                         \
66                 printk(KERN_DEBUG "ds: " fmt , ## arg);         \
67 } while (0)
68 #else
69 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
70 #endif
71
72 static struct pcmcia_device *get_pcmcia_device(struct pcmcia_socket *s,
73                                                 unsigned int function)
74 {
75         struct pcmcia_device *p_dev = NULL;
76         unsigned long flags;
77
78         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
79         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
80                 if (p_dev->func == function) {
81                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
82                         return pcmcia_get_dev(p_dev);
83                 }
84         }
85         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
86         return NULL;
87 }
88
89 /* backwards-compatible accessing of driver --- by name! */
90
91 static struct pcmcia_driver *get_pcmcia_driver(dev_info_t *dev_info)
92 {
93         struct device_driver *drv;
94         struct pcmcia_driver *p_drv;
95
96         drv = driver_find((char *) dev_info, &pcmcia_bus_type);
97         if (!drv)
98                 return NULL;
99
100         p_drv = container_of(drv, struct pcmcia_driver, drv);
101
102         return (p_drv);
103 }
104
105
106 #ifdef CONFIG_PROC_FS
107 static struct proc_dir_entry *proc_pccard = NULL;
108
109 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
110 {
111         char **p = d;
112         struct pcmcia_driver *p_drv = container_of(driver,
113                                                    struct pcmcia_driver, drv);
114
115         *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
116 #ifdef CONFIG_MODULE_UNLOAD
117                       (p_drv->owner) ? module_refcount(p_drv->owner) : 1
118 #else
119                       1
120 #endif
121         );
122         d = (void *) p;
123
124         return 0;
125 }
126
127 static int proc_read_drivers(char *buf, char **start, off_t pos,
128                              int count, int *eof, void *data)
129 {
130         char *p = buf;
131
132         bus_for_each_drv(&pcmcia_bus_type, NULL,
133                          (void *) &p, proc_read_drivers_callback);
134
135         return (p - buf);
136 }
137 #endif
138
139 /*======================================================================
140
141     These manage a ring buffer of events pending for one user process
142
143 ======================================================================*/
144
145
146 static int queue_empty(user_info_t *user)
147 {
148     return (user->event_head == user->event_tail);
149 }
150
151 static event_t get_queued_event(user_info_t *user)
152 {
153     user->event_tail = (user->event_tail+1) % MAX_EVENTS;
154     return user->event[user->event_tail];
155 }
156
157 static void queue_event(user_info_t *user, event_t event)
158 {
159     user->event_head = (user->event_head+1) % MAX_EVENTS;
160     if (user->event_head == user->event_tail)
161         user->event_tail = (user->event_tail+1) % MAX_EVENTS;
162     user->event[user->event_head] = event;
163 }
164
165 void handle_event(struct pcmcia_socket *s, event_t event)
166 {
167     user_info_t *user;
168     for (user = s->user; user; user = user->next)
169         queue_event(user, event);
170     wake_up_interruptible(&s->queue);
171 }
172
173
174 /*======================================================================
175
176     bind_request() and bind_device() are merged by now. Register_client()
177     is called right at the end of bind_request(), during the driver's
178     ->attach() call. Individual descriptions:
179
180     bind_request() connects a socket to a particular client driver.
181     It looks up the specified device ID in the list of registered
182     drivers, binds it to the socket, and tries to create an instance
183     of the device.  unbind_request() deletes a driver instance.
184
185     Bind_device() associates a device driver with a particular socket.
186     It is normally called by Driver Services after it has identified
187     a newly inserted card.  An instance of that driver will then be
188     eligible to register as a client of this socket.
189
190     Register_client() uses the dev_info_t handle to match the
191     caller with a socket.  The driver must have already been bound
192     to a socket with bind_device() -- in fact, bind_device()
193     allocates the client structure that will be used.
194
195 ======================================================================*/
196
197 static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
198 {
199         struct pcmcia_driver *p_drv;
200         struct pcmcia_device *p_dev;
201         int ret = 0;
202         unsigned long flags;
203
204         s = pcmcia_get_socket(s);
205         if (!s)
206                 return -EINVAL;
207
208         ds_dbg(2, "bind_request(%d, '%s')\n", s->sock,
209                (char *)bind_info->dev_info);
210
211         p_drv = get_pcmcia_driver(&bind_info->dev_info);
212         if (!p_drv) {
213                 ret = -EINVAL;
214                 goto err_put;
215         }
216
217         if (!try_module_get(p_drv->owner)) {
218                 ret = -EINVAL;
219                 goto err_put_driver;
220         }
221
222         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
223         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
224                 if (p_dev->func == bind_info->function) {
225                         if ((p_dev->dev.driver == &p_drv->drv)) {
226                                 if (p_dev->cardmgr) {
227                                         /* if there's already a device
228                                          * registered, and it was registered
229                                          * by userspace before, we need to
230                                          * return the "instance". */
231                                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
232                                         bind_info->instance = p_dev->instance;
233                                         ret = -EBUSY;
234                                         goto err_put_module;
235                                 } else {
236                                         /* the correct driver managed to bind
237                                          * itself magically to the correct
238                                          * device. */
239                                         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
240                                         p_dev->cardmgr = p_drv;
241                                         ret = 0;
242                                         goto err_put_module;
243                                 }
244                         } else if (!p_dev->dev.driver) {
245                                 /* there's already a device available where
246                                  * no device has been bound to yet. So we don't
247                                  * need to register a device! */
248                                 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
249                                 goto rescan;
250                         }
251                 }
252         }
253         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
254
255         p_dev = pcmcia_device_add(s, bind_info->function);
256         if (!p_dev) {
257                 ret = -EIO;
258                 goto err_put_module;
259         }
260
261 rescan:
262         p_dev->cardmgr = p_drv;
263
264         /* if a driver is already running, we can abort */
265         if (p_dev->dev.driver)
266                 goto err_put_module;
267
268         /*
269          * Prevent this racing with a card insertion.
270          */
271         mutex_lock(&s->skt_mutex);
272         bus_rescan_devices(&pcmcia_bus_type);
273         mutex_unlock(&s->skt_mutex);
274
275         /* check whether the driver indeed matched. I don't care if this
276          * is racy or not, because it can only happen on cardmgr access
277          * paths...
278          */
279         if (!(p_dev->dev.driver == &p_drv->drv))
280                 p_dev->cardmgr = NULL;
281
282  err_put_module:
283         module_put(p_drv->owner);
284  err_put_driver:
285         put_driver(&p_drv->drv);
286  err_put:
287         pcmcia_put_socket(s);
288
289         return (ret);
290 } /* bind_request */
291
292 #ifdef CONFIG_CARDBUS
293
294 static struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
295 {
296         if (!s || !(s->state & SOCKET_CARDBUS))
297                 return NULL;
298
299         return s->cb_dev->subordinate;
300 }
301 #endif
302
303 static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first)
304 {
305         dev_node_t *node;
306         struct pcmcia_device *p_dev;
307         unsigned long flags;
308         int ret = 0;
309
310 #ifdef CONFIG_CARDBUS
311         /*
312          * Some unbelievably ugly code to associate the PCI cardbus
313          * device and its driver with the PCMCIA "bind" information.
314          */
315         {
316                 struct pci_bus *bus;
317
318                 bus = pcmcia_lookup_bus(s);
319                 if (bus) {
320                         struct list_head *list;
321                         struct pci_dev *dev = NULL;
322
323                         list = bus->devices.next;
324                         while (list != &bus->devices) {
325                                 struct pci_dev *pdev = pci_dev_b(list);
326                                 list = list->next;
327
328                                 if (first) {
329                                         dev = pdev;
330                                         break;
331                                 }
332
333                                 /* Try to handle "next" here some way? */
334                         }
335                         if (dev && dev->driver) {
336                                 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
337                                 bind_info->major = 0;
338                                 bind_info->minor = 0;
339                                 bind_info->next = NULL;
340                                 return 0;
341                         }
342                 }
343         }
344 #endif
345
346         spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
347         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
348                 if (p_dev->func == bind_info->function) {
349                         p_dev = pcmcia_get_dev(p_dev);
350                         if (!p_dev)
351                                 continue;
352                         goto found;
353                 }
354         }
355         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
356         return -ENODEV;
357
358  found:
359         spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
360
361         if ((!p_dev->instance) ||
362             (p_dev->instance->state & DEV_CONFIG_PENDING)) {
363                 ret = -EAGAIN;
364                 goto err_put;
365         }
366
367         if (first)
368                 node = p_dev->instance->dev;
369         else
370                 for (node = p_dev->instance->dev; node; node = node->next)
371                         if (node == bind_info->next)
372                                 break;
373         if (!node) {
374                 ret = -ENODEV;
375                 goto err_put;
376         }
377
378         strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
379         bind_info->major = node->major;
380         bind_info->minor = node->minor;
381         bind_info->next = node->next;
382
383  err_put:
384         pcmcia_put_dev(p_dev);
385         return (ret);
386 } /* get_device_info */
387
388
389 static int ds_open(struct inode *inode, struct file *file)
390 {
391     socket_t i = iminor(inode);
392     struct pcmcia_socket *s;
393     user_info_t *user;
394     static int warning_printed = 0;
395
396     ds_dbg(0, "ds_open(socket %d)\n", i);
397
398     s = pcmcia_get_socket_by_nr(i);
399     if (!s)
400             return -ENODEV;
401     s = pcmcia_get_socket(s);
402     if (!s)
403             return -ENODEV;
404
405     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
406             if (s->pcmcia_state.busy) {
407                     pcmcia_put_socket(s);
408                     return -EBUSY;
409             }
410         else
411             s->pcmcia_state.busy = 1;
412     }
413
414     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
415     if (!user) {
416             pcmcia_put_socket(s);
417             return -ENOMEM;
418     }
419     user->event_tail = user->event_head = 0;
420     user->next = s->user;
421     user->user_magic = USER_MAGIC;
422     user->socket = s;
423     s->user = user;
424     file->private_data = user;
425
426     if (!warning_printed) {
427             printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl "
428                         "usage.\n");
429             printk(KERN_INFO "pcmcia: This interface will soon be removed from "
430                         "the kernel; please expect breakage unless you upgrade "
431                         "to new tools.\n");
432             printk(KERN_INFO "pcmcia: see http://www.kernel.org/pub/linux/"
433                         "utils/kernel/pcmcia/pcmcia.html for details.\n");
434             warning_printed = 1;
435     }
436
437     if (s->pcmcia_state.present)
438         queue_event(user, CS_EVENT_CARD_INSERTION);
439     return 0;
440 } /* ds_open */
441
442 /*====================================================================*/
443
444 static int ds_release(struct inode *inode, struct file *file)
445 {
446     struct pcmcia_socket *s;
447     user_info_t *user, **link;
448
449     ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
450
451     user = file->private_data;
452     if (CHECK_USER(user))
453         goto out;
454
455     s = user->socket;
456
457     /* Unlink user data structure */
458     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
459         s->pcmcia_state.busy = 0;
460     }
461     file->private_data = NULL;
462     for (link = &s->user; *link; link = &(*link)->next)
463         if (*link == user) break;
464     if (link == NULL)
465         goto out;
466     *link = user->next;
467     user->user_magic = 0;
468     kfree(user);
469     pcmcia_put_socket(s);
470 out:
471     return 0;
472 } /* ds_release */
473
474 /*====================================================================*/
475
476 static ssize_t ds_read(struct file *file, char __user *buf,
477                        size_t count, loff_t *ppos)
478 {
479     struct pcmcia_socket *s;
480     user_info_t *user;
481     int ret;
482
483     ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
484
485     if (count < 4)
486         return -EINVAL;
487
488     user = file->private_data;
489     if (CHECK_USER(user))
490         return -EIO;
491
492     s = user->socket;
493     if (s->pcmcia_state.dead)
494         return -EIO;
495
496     ret = wait_event_interruptible(s->queue, !queue_empty(user));
497     if (ret == 0)
498         ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
499
500     return ret;
501 } /* ds_read */
502
503 /*====================================================================*/
504
505 static ssize_t ds_write(struct file *file, const char __user *buf,
506                         size_t count, loff_t *ppos)
507 {
508     ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
509
510     if (count != 4)
511         return -EINVAL;
512     if ((file->f_flags & O_ACCMODE) == O_RDONLY)
513         return -EBADF;
514
515     return -EIO;
516 } /* ds_write */
517
518 /*====================================================================*/
519
520 /* No kernel lock - fine */
521 static u_int ds_poll(struct file *file, poll_table *wait)
522 {
523     struct pcmcia_socket *s;
524     user_info_t *user;
525
526     ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
527
528     user = file->private_data;
529     if (CHECK_USER(user))
530         return POLLERR;
531     s = user->socket;
532     /*
533      * We don't check for a dead socket here since that
534      * will send cardmgr into an endless spin.
535      */
536     poll_wait(file, &s->queue, wait);
537     if (!queue_empty(user))
538         return POLLIN | POLLRDNORM;
539     return 0;
540 } /* ds_poll */
541
542 /*====================================================================*/
543
544 extern int pcmcia_adjust_resource_info(adjust_t *adj);
545
546 static int ds_ioctl(struct inode * inode, struct file * file,
547                     u_int cmd, u_long arg)
548 {
549     struct pcmcia_socket *s;
550     void __user *uarg = (char __user *)arg;
551     u_int size;
552     int ret, err;
553     ds_ioctl_arg_t *buf;
554     user_info_t *user;
555
556     ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
557
558     user = file->private_data;
559     if (CHECK_USER(user))
560         return -EIO;
561
562     s = user->socket;
563     if (s->pcmcia_state.dead)
564         return -EIO;
565
566     size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
567     if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
568
569     /* Permission check */
570     if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
571         return -EPERM;
572
573     if (cmd & IOC_IN) {
574         if (!access_ok(VERIFY_READ, uarg, size)) {
575             ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
576             return -EFAULT;
577         }
578     }
579     if (cmd & IOC_OUT) {
580         if (!access_ok(VERIFY_WRITE, uarg, size)) {
581             ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
582             return -EFAULT;
583         }
584     }
585     buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
586     if (!buf)
587         return -ENOMEM;
588
589     err = ret = 0;
590
591     if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
592
593     switch (cmd) {
594     case DS_ADJUST_RESOURCE_INFO:
595         ret = pcmcia_adjust_resource_info(&buf->adjust);
596         break;
597     case DS_GET_CONFIGURATION_INFO:
598         if (buf->config.Function &&
599            (buf->config.Function >= s->functions))
600             ret = CS_BAD_ARGS;
601         else {
602             struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->config.Function);
603             ret = pccard_get_configuration_info(s, p_dev, &buf->config);
604             pcmcia_put_dev(p_dev);
605         }
606         break;
607     case DS_GET_FIRST_TUPLE:
608         mutex_lock(&s->skt_mutex);
609         pcmcia_validate_mem(s);
610         mutex_unlock(&s->skt_mutex);
611         ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
612         break;
613     case DS_GET_NEXT_TUPLE:
614         ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple);
615         break;
616     case DS_GET_TUPLE_DATA:
617         buf->tuple.TupleData = buf->tuple_parse.data;
618         buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
619         ret = pccard_get_tuple_data(s, &buf->tuple);
620         break;
621     case DS_PARSE_TUPLE:
622         buf->tuple.TupleData = buf->tuple_parse.data;
623         ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
624         break;
625     case DS_RESET_CARD:
626         ret = pccard_reset_card(s);
627         break;
628     case DS_GET_STATUS:
629             if (buf->status.Function &&
630                 (buf->status.Function >= s->functions))
631                     ret = CS_BAD_ARGS;
632             else {
633                     struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->status.Function);
634                     ret = pccard_get_status(s, p_dev, &buf->status);
635                     pcmcia_put_dev(p_dev);
636             }
637             break;
638     case DS_VALIDATE_CIS:
639         mutex_lock(&s->skt_mutex);
640         pcmcia_validate_mem(s);
641         mutex_unlock(&s->skt_mutex);
642         ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo);
643         break;
644     case DS_SUSPEND_CARD:
645         ret = pcmcia_suspend_card(s);
646         break;
647     case DS_RESUME_CARD:
648         ret = pcmcia_resume_card(s);
649         break;
650     case DS_EJECT_CARD:
651         err = pcmcia_eject_card(s);
652         break;
653     case DS_INSERT_CARD:
654         err = pcmcia_insert_card(s);
655         break;
656     case DS_ACCESS_CONFIGURATION_REGISTER:
657         if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
658             err = -EPERM;
659             goto free_out;
660         }
661
662         ret = CS_BAD_ARGS;
663
664         if (!(buf->conf_reg.Function &&
665              (buf->conf_reg.Function >= s->functions))) {
666                 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function);
667                 if (p_dev)
668                         ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg);
669                 pcmcia_put_dev(p_dev);
670         }
671         break;
672     case DS_GET_FIRST_REGION:
673     case DS_GET_NEXT_REGION:
674     case DS_BIND_MTD:
675         if (!capable(CAP_SYS_ADMIN)) {
676                 err = -EPERM;
677                 goto free_out;
678         } else {
679                 static int printed = 0;
680                 if (!printed) {
681                         printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
682                         printk(KERN_WARNING "MTD handling any more.\n");
683                         printed++;
684                 }
685         }
686         err = -EINVAL;
687         goto free_out;
688         break;
689     case DS_GET_FIRST_WINDOW:
690         ret = pcmcia_get_window(s, &buf->win_info.handle, 0,
691                         &buf->win_info.window);
692         break;
693     case DS_GET_NEXT_WINDOW:
694         ret = pcmcia_get_window(s, &buf->win_info.handle,
695                         buf->win_info.handle->index + 1, &buf->win_info.window);
696         break;
697     case DS_GET_MEM_PAGE:
698         ret = pcmcia_get_mem_page(buf->win_info.handle,
699                            &buf->win_info.map);
700         break;
701     case DS_REPLACE_CIS:
702         ret = pcmcia_replace_cis(s, &buf->cisdump);
703         break;
704     case DS_BIND_REQUEST:
705         if (!capable(CAP_SYS_ADMIN)) {
706                 err = -EPERM;
707                 goto free_out;
708         }
709         err = bind_request(s, &buf->bind_info);
710         break;
711     case DS_GET_DEVICE_INFO:
712         err = get_device_info(s, &buf->bind_info, 1);
713         break;
714     case DS_GET_NEXT_DEVICE:
715         err = get_device_info(s, &buf->bind_info, 0);
716         break;
717     case DS_UNBIND_REQUEST:
718         err = 0;
719         break;
720     default:
721         err = -EINVAL;
722     }
723
724     if ((err == 0) && (ret != CS_SUCCESS)) {
725         ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
726         switch (ret) {
727         case CS_BAD_SOCKET: case CS_NO_CARD:
728             err = -ENODEV; break;
729         case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
730         case CS_BAD_TUPLE:
731             err = -EINVAL; break;
732         case CS_IN_USE:
733             err = -EBUSY; break;
734         case CS_OUT_OF_RESOURCE:
735             err = -ENOSPC; break;
736         case CS_NO_MORE_ITEMS:
737             err = -ENODATA; break;
738         case CS_UNSUPPORTED_FUNCTION:
739             err = -ENOSYS; break;
740         default:
741             err = -EIO; break;
742         }
743     }
744
745     if (cmd & IOC_OUT) {
746         if (__copy_to_user(uarg, (char *)buf, size))
747             err = -EFAULT;
748     }
749
750 free_out:
751     kfree(buf);
752     return err;
753 } /* ds_ioctl */
754
755 /*====================================================================*/
756
757 static struct file_operations ds_fops = {
758         .owner          = THIS_MODULE,
759         .open           = ds_open,
760         .release        = ds_release,
761         .ioctl          = ds_ioctl,
762         .read           = ds_read,
763         .write          = ds_write,
764         .poll           = ds_poll,
765 };
766
767 void __init pcmcia_setup_ioctl(void) {
768         int i;
769
770         /* Set up character device for user mode clients */
771         i = register_chrdev(0, "pcmcia", &ds_fops);
772         if (i < 0)
773                 printk(KERN_NOTICE "unable to find a free device # for "
774                        "Driver Services (error=%d)\n", i);
775         else
776                 major_dev = i;
777
778 #ifdef CONFIG_PROC_FS
779         proc_pccard = proc_mkdir("pccard", proc_bus);
780         if (proc_pccard)
781                 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
782 #endif
783 }
784
785
786 void __exit pcmcia_cleanup_ioctl(void) {
787 #ifdef CONFIG_PROC_FS
788         if (proc_pccard) {
789                 remove_proc_entry("drivers", proc_pccard);
790                 remove_proc_entry("pccard", proc_bus);
791         }
792 #endif
793         if (major_dev != -1)
794                 unregister_chrdev(major_dev, "pcmcia");
795 }