]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/pcmcia/pcmcia_ioctl.c
pcmcia: simplify locking
[net-next-2.6.git] / 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/seq_file.h>
31 #include <linux/smp_lock.h>
32 #include <linux/workqueue.h>
33
34 #include <pcmcia/cs_types.h>
35 #include <pcmcia/cs.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/cisreg.h>
38 #include <pcmcia/ds.h>
39 #include <pcmcia/ss.h>
40
41 #include "cs_internal.h"
42
43 static int major_dev = -1;
44
45
46 /* Device user information */
47 #define MAX_EVENTS      32
48 #define USER_MAGIC      0x7ea4
49 #define CHECK_USER(u) \
50     (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
51
52 typedef struct user_info_t {
53         u_int                   user_magic;
54         int                     event_head, event_tail;
55         event_t                 event[MAX_EVENTS];
56         struct user_info_t      *next;
57         struct pcmcia_socket    *socket;
58 } user_info_t;
59
60
61 static struct pcmcia_device *get_pcmcia_device(struct pcmcia_socket *s,
62                                                 unsigned int function)
63 {
64         struct pcmcia_device *p_dev = NULL;
65
66         mutex_lock(&s->ops_mutex);
67         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
68                 if (p_dev->func == function) {
69                         mutex_unlock(&s->ops_mutex);
70                         return pcmcia_get_dev(p_dev);
71                 }
72         }
73         mutex_unlock(&s->ops_mutex);
74         return NULL;
75 }
76
77 /* backwards-compatible accessing of driver --- by name! */
78
79 static struct pcmcia_driver *get_pcmcia_driver(dev_info_t *dev_info)
80 {
81         struct device_driver *drv;
82         struct pcmcia_driver *p_drv;
83
84         drv = driver_find((char *) dev_info, &pcmcia_bus_type);
85         if (!drv)
86                 return NULL;
87
88         p_drv = container_of(drv, struct pcmcia_driver, drv);
89
90         return p_drv;
91 }
92
93
94 #ifdef CONFIG_PROC_FS
95 static struct proc_dir_entry *proc_pccard;
96
97 static int proc_read_drivers_callback(struct device_driver *driver, void *_m)
98 {
99         struct seq_file *m = _m;
100         struct pcmcia_driver *p_drv = container_of(driver,
101                                                    struct pcmcia_driver, drv);
102
103         seq_printf(m, "%-24.24s 1 %d\n", p_drv->drv.name,
104 #ifdef CONFIG_MODULE_UNLOAD
105                       (p_drv->owner) ? module_refcount(p_drv->owner) : 1
106 #else
107                       1
108 #endif
109         );
110         return 0;
111 }
112
113 static int pccard_drivers_proc_show(struct seq_file *m, void *v)
114 {
115         return bus_for_each_drv(&pcmcia_bus_type, NULL,
116                                 m, proc_read_drivers_callback);
117 }
118
119 static int pccard_drivers_proc_open(struct inode *inode, struct file *file)
120 {
121         return single_open(file, pccard_drivers_proc_show, NULL);
122 }
123
124 static const struct file_operations pccard_drivers_proc_fops = {
125         .owner          = THIS_MODULE,
126         .open           = pccard_drivers_proc_open,
127         .read           = seq_read,
128         .llseek         = seq_lseek,
129         .release        = single_release,
130 };
131 #endif
132
133
134 #ifdef CONFIG_PCMCIA_PROBE
135
136 static int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
137 {
138         int irq;
139         u32 mask;
140
141         irq = adj->resource.irq.IRQ;
142         if ((irq < 0) || (irq > 15))
143                 return -EINVAL;
144
145         if (adj->Action != REMOVE_MANAGED_RESOURCE)
146                 return 0;
147
148         mask = 1 << irq;
149
150         if (!(s->irq_mask & mask))
151                 return 0;
152
153         s->irq_mask &= ~mask;
154
155         return 0;
156 }
157
158 #else
159
160 static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
161 {
162         return 0;
163 }
164
165 #endif
166
167 static int pcmcia_adjust_resource_info(adjust_t *adj)
168 {
169         struct pcmcia_socket *s;
170         int ret = -ENOSYS;
171
172         down_read(&pcmcia_socket_list_rwsem);
173         list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
174
175                 if (adj->Resource == RES_IRQ)
176                         ret = adjust_irq(s, adj);
177
178                 else if (s->resource_ops->add_io) {
179                         unsigned long begin, end;
180
181                         /* you can't use the old interface if the new
182                          * one was used before */
183                         mutex_lock(&s->ops_mutex);
184                         if ((s->resource_setup_new) &&
185                             !(s->resource_setup_old)) {
186                                 mutex_unlock(&s->ops_mutex);
187                                 continue;
188                         } else if (!(s->resource_setup_old))
189                                 s->resource_setup_old = 1;
190                         mutex_unlock(&s->ops_mutex);
191
192                         switch (adj->Resource) {
193                         case RES_MEMORY_RANGE:
194                                 begin = adj->resource.memory.Base;
195                                 end = adj->resource.memory.Base + adj->resource.memory.Size - 1;
196                                 if (s->resource_ops->add_mem)
197                                         ret = s->resource_ops->add_mem(s, adj->Action, begin, end);
198                         case RES_IO_RANGE:
199                                 begin = adj->resource.io.BasePort;
200                                 end = adj->resource.io.BasePort + adj->resource.io.NumPorts - 1;
201                                 if (s->resource_ops->add_io)
202                                         ret = s->resource_ops->add_io(s, adj->Action, begin, end);
203                         }
204                         if (!ret) {
205                                 /* as there's no way we know this is the
206                                  * last call to adjust_resource_info, we
207                                  * always need to assume this is the latest
208                                  * one... */
209                                 mutex_lock(&s->ops_mutex);
210                                 s->resource_setup_done = 1;
211                                 mutex_unlock(&s->ops_mutex);
212                         }
213                 }
214         }
215         up_read(&pcmcia_socket_list_rwsem);
216
217         return ret;
218 }
219
220
221 /** pcmcia_get_window
222  */
223 static int pcmcia_get_window(struct pcmcia_socket *s, window_handle_t *wh_out,
224                         window_handle_t wh, win_req_t *req)
225 {
226         pccard_mem_map *win;
227         window_handle_t w;
228
229         wh--;
230         if (!s || !(s->state & SOCKET_PRESENT))
231                 return -ENODEV;
232         if (wh >= MAX_WIN)
233                 return -EINVAL;
234         for (w = wh; w < MAX_WIN; w++)
235                 if (s->state & SOCKET_WIN_REQ(w))
236                         break;
237         if (w == MAX_WIN)
238                 return -EINVAL;
239         win = &s->win[w];
240         req->Base = win->res->start;
241         req->Size = win->res->end - win->res->start + 1;
242         req->AccessSpeed = win->speed;
243         req->Attributes = 0;
244         if (win->flags & MAP_ATTRIB)
245                 req->Attributes |= WIN_MEMORY_TYPE_AM;
246         if (win->flags & MAP_ACTIVE)
247                 req->Attributes |= WIN_ENABLE;
248         if (win->flags & MAP_16BIT)
249                 req->Attributes |= WIN_DATA_WIDTH_16;
250         if (win->flags & MAP_USE_WAIT)
251                 req->Attributes |= WIN_USE_WAIT;
252
253         *wh_out = w + 1;
254         return 0;
255 } /* pcmcia_get_window */
256
257
258 /** pcmcia_get_mem_page
259  *
260  * Change the card address of an already open memory window.
261  */
262 static int pcmcia_get_mem_page(struct pcmcia_socket *skt, window_handle_t wh,
263                         memreq_t *req)
264 {
265         wh--;
266         if (wh >= MAX_WIN)
267                 return -EINVAL;
268
269         req->Page = 0;
270         req->CardOffset = skt->win[wh].card_start;
271         return 0;
272 } /* pcmcia_get_mem_page */
273
274
275 /** pccard_get_status
276  *
277  * Get the current socket state bits.  We don't support the latched
278  * SocketState yet: I haven't seen any point for it.
279  */
280
281 static int pccard_get_status(struct pcmcia_socket *s,
282                              struct pcmcia_device *p_dev,
283                              cs_status_t *status)
284 {
285         config_t *c;
286         int val;
287
288         s->ops->get_status(s, &val);
289         status->CardState = status->SocketState = 0;
290         status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
291         status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
292         status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
293         status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
294         if (s->state & SOCKET_SUSPEND)
295                 status->CardState |= CS_EVENT_PM_SUSPEND;
296         if (!(s->state & SOCKET_PRESENT))
297                 return -ENODEV;
298
299         c = (p_dev) ? p_dev->function_config : NULL;
300
301         if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
302             (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
303                 u_char reg;
304                 if (c->CardValues & PRESENT_PIN_REPLACE) {
305                         pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
306                         status->CardState |=
307                                 (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
308                         status->CardState |=
309                                 (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
310                         status->CardState |=
311                                 (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
312                         status->CardState |=
313                                 (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
314                 } else {
315                         /* No PRR?  Then assume we're always ready */
316                         status->CardState |= CS_EVENT_READY_CHANGE;
317                 }
318                 if (c->CardValues & PRESENT_EXT_STATUS) {
319                         pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
320                         status->CardState |=
321                                 (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
322                 }
323                 return 0;
324         }
325         status->CardState |=
326                 (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
327         status->CardState |=
328                 (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
329         status->CardState |=
330                 (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
331         status->CardState |=
332                 (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
333         return 0;
334 } /* pccard_get_status */
335
336 static int pccard_get_configuration_info(struct pcmcia_socket *s,
337                                   struct pcmcia_device *p_dev,
338                                   config_info_t *config)
339 {
340         config_t *c;
341
342         if (!(s->state & SOCKET_PRESENT))
343                 return -ENODEV;
344
345
346 #ifdef CONFIG_CARDBUS
347         if (s->state & SOCKET_CARDBUS) {
348                 memset(config, 0, sizeof(config_info_t));
349                 config->Vcc = s->socket.Vcc;
350                 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
351                 config->Option = s->cb_dev->subordinate->number;
352                 if (s->state & SOCKET_CARDBUS_CONFIG) {
353                         config->Attributes = CONF_VALID_CLIENT;
354                         config->IntType = INT_CARDBUS;
355                         config->AssignedIRQ = s->irq.AssignedIRQ;
356                         if (config->AssignedIRQ)
357                                 config->Attributes |= CONF_ENABLE_IRQ;
358                         if (s->io[0].res) {
359                                 config->BasePort1 = s->io[0].res->start;
360                                 config->NumPorts1 = s->io[0].res->end -
361                                         config->BasePort1 + 1;
362                         }
363                 }
364                 return 0;
365         }
366 #endif
367
368         if (p_dev) {
369                 c = p_dev->function_config;
370                 config->Function = p_dev->func;
371         } else {
372                 c = NULL;
373                 config->Function = 0;
374         }
375
376         if ((c == NULL) || !(c->state & CONFIG_LOCKED)) {
377                 config->Attributes = 0;
378                 config->Vcc = s->socket.Vcc;
379                 config->Vpp1 = config->Vpp2 = s->socket.Vpp;
380                 return 0;
381         }
382
383         config->Attributes = c->Attributes | CONF_VALID_CLIENT;
384         config->Vcc = s->socket.Vcc;
385         config->Vpp1 = config->Vpp2 = s->socket.Vpp;
386         config->IntType = c->IntType;
387         config->ConfigBase = c->ConfigBase;
388         config->Status = c->Status;
389         config->Pin = c->Pin;
390         config->Copy = c->Copy;
391         config->Option = c->Option;
392         config->ExtStatus = c->ExtStatus;
393         config->Present = config->CardValues = c->CardValues;
394         config->IRQAttributes = c->irq.Attributes;
395         config->AssignedIRQ = s->irq.AssignedIRQ;
396         config->BasePort1 = c->io.BasePort1;
397         config->NumPorts1 = c->io.NumPorts1;
398         config->Attributes1 = c->io.Attributes1;
399         config->BasePort2 = c->io.BasePort2;
400         config->NumPorts2 = c->io.NumPorts2;
401         config->Attributes2 = c->io.Attributes2;
402         config->IOAddrLines = c->io.IOAddrLines;
403
404         return 0;
405 } /* pccard_get_configuration_info */
406
407
408 /*======================================================================
409
410     These manage a ring buffer of events pending for one user process
411
412 ======================================================================*/
413
414
415 static int queue_empty(user_info_t *user)
416 {
417     return (user->event_head == user->event_tail);
418 }
419
420 static event_t get_queued_event(user_info_t *user)
421 {
422     user->event_tail = (user->event_tail+1) % MAX_EVENTS;
423     return user->event[user->event_tail];
424 }
425
426 static void queue_event(user_info_t *user, event_t event)
427 {
428     user->event_head = (user->event_head+1) % MAX_EVENTS;
429     if (user->event_head == user->event_tail)
430         user->event_tail = (user->event_tail+1) % MAX_EVENTS;
431     user->event[user->event_head] = event;
432 }
433
434 void handle_event(struct pcmcia_socket *s, event_t event)
435 {
436     user_info_t *user;
437     for (user = s->user; user; user = user->next)
438         queue_event(user, event);
439     wake_up_interruptible(&s->queue);
440 }
441
442
443 /*======================================================================
444
445     bind_request() and bind_device() are merged by now. Register_client()
446     is called right at the end of bind_request(), during the driver's
447     ->attach() call. Individual descriptions:
448
449     bind_request() connects a socket to a particular client driver.
450     It looks up the specified device ID in the list of registered
451     drivers, binds it to the socket, and tries to create an instance
452     of the device.  unbind_request() deletes a driver instance.
453
454     Bind_device() associates a device driver with a particular socket.
455     It is normally called by Driver Services after it has identified
456     a newly inserted card.  An instance of that driver will then be
457     eligible to register as a client of this socket.
458
459     Register_client() uses the dev_info_t handle to match the
460     caller with a socket.  The driver must have already been bound
461     to a socket with bind_device() -- in fact, bind_device()
462     allocates the client structure that will be used.
463
464 ======================================================================*/
465
466 static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
467 {
468         struct pcmcia_driver *p_drv;
469         struct pcmcia_device *p_dev;
470         int ret = 0;
471
472         s = pcmcia_get_socket(s);
473         if (!s)
474                 return -EINVAL;
475
476         pr_debug("bind_request(%d, '%s')\n", s->sock,
477                (char *)bind_info->dev_info);
478
479         p_drv = get_pcmcia_driver(&bind_info->dev_info);
480         if (!p_drv) {
481                 ret = -EINVAL;
482                 goto err_put;
483         }
484
485         if (!try_module_get(p_drv->owner)) {
486                 ret = -EINVAL;
487                 goto err_put_driver;
488         }
489
490         mutex_lock(&s->ops_mutex);
491         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
492                 if (p_dev->func == bind_info->function) {
493                         if ((p_dev->dev.driver == &p_drv->drv)) {
494                                 if (p_dev->cardmgr) {
495                                         /* if there's already a device
496                                          * registered, and it was registered
497                                          * by userspace before, we need to
498                                          * return the "instance". */
499                                         mutex_unlock(&s->ops_mutex);
500                                         bind_info->instance = p_dev;
501                                         ret = -EBUSY;
502                                         goto err_put_module;
503                                 } else {
504                                         /* the correct driver managed to bind
505                                          * itself magically to the correct
506                                          * device. */
507                                         mutex_unlock(&s->ops_mutex);
508                                         p_dev->cardmgr = p_drv;
509                                         ret = 0;
510                                         goto err_put_module;
511                                 }
512                         } else if (!p_dev->dev.driver) {
513                                 /* there's already a device available where
514                                  * no device has been bound to yet. So we don't
515                                  * need to register a device! */
516                                 mutex_unlock(&s->ops_mutex);
517                                 goto rescan;
518                         }
519                 }
520         }
521         mutex_unlock(&s->ops_mutex);
522
523         p_dev = pcmcia_device_add(s, bind_info->function);
524         if (!p_dev) {
525                 ret = -EIO;
526                 goto err_put_module;
527         }
528
529 rescan:
530         p_dev->cardmgr = p_drv;
531
532         /* if a driver is already running, we can abort */
533         if (p_dev->dev.driver)
534                 goto err_put_module;
535
536         /*
537          * Prevent this racing with a card insertion.
538          */
539         mutex_lock(&s->skt_mutex);
540         ret = bus_rescan_devices(&pcmcia_bus_type);
541         mutex_unlock(&s->skt_mutex);
542         if (ret)
543                 goto err_put_module;
544
545         /* check whether the driver indeed matched. I don't care if this
546          * is racy or not, because it can only happen on cardmgr access
547          * paths...
548          */
549         if (!(p_dev->dev.driver == &p_drv->drv))
550                 p_dev->cardmgr = NULL;
551
552  err_put_module:
553         module_put(p_drv->owner);
554  err_put_driver:
555         put_driver(&p_drv->drv);
556  err_put:
557         pcmcia_put_socket(s);
558
559         return ret;
560 } /* bind_request */
561
562 #ifdef CONFIG_CARDBUS
563
564 static struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
565 {
566         if (!s || !(s->state & SOCKET_CARDBUS))
567                 return NULL;
568
569         return s->cb_dev->subordinate;
570 }
571 #endif
572
573 static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first)
574 {
575         dev_node_t *node;
576         struct pcmcia_device *p_dev;
577         struct pcmcia_driver *p_drv;
578         int ret = 0;
579
580 #ifdef CONFIG_CARDBUS
581         /*
582          * Some unbelievably ugly code to associate the PCI cardbus
583          * device and its driver with the PCMCIA "bind" information.
584          */
585         {
586                 struct pci_bus *bus;
587
588                 bus = pcmcia_lookup_bus(s);
589                 if (bus) {
590                         struct list_head *list;
591                         struct pci_dev *dev = NULL;
592
593                         list = bus->devices.next;
594                         while (list != &bus->devices) {
595                                 struct pci_dev *pdev = pci_dev_b(list);
596                                 list = list->next;
597
598                                 if (first) {
599                                         dev = pdev;
600                                         break;
601                                 }
602
603                                 /* Try to handle "next" here some way? */
604                         }
605                         if (dev && dev->driver) {
606                                 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
607                                 bind_info->major = 0;
608                                 bind_info->minor = 0;
609                                 bind_info->next = NULL;
610                                 return 0;
611                         }
612                 }
613         }
614 #endif
615
616         mutex_lock(&s->ops_mutex);
617         list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
618                 if (p_dev->func == bind_info->function) {
619                         p_dev = pcmcia_get_dev(p_dev);
620                         if (!p_dev)
621                                 continue;
622                         goto found;
623                 }
624         }
625         mutex_unlock(&s->ops_mutex);
626         return -ENODEV;
627
628  found:
629         mutex_unlock(&s->ops_mutex);
630
631         p_drv = to_pcmcia_drv(p_dev->dev.driver);
632         if (p_drv && !p_dev->_locked) {
633                 ret = -EAGAIN;
634                 goto err_put;
635         }
636
637         if (first)
638                 node = p_dev->dev_node;
639         else
640                 for (node = p_dev->dev_node; node; node = node->next)
641                         if (node == bind_info->next)
642                                 break;
643         if (!node) {
644                 ret = -ENODEV;
645                 goto err_put;
646         }
647
648         strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
649         bind_info->major = node->major;
650         bind_info->minor = node->minor;
651         bind_info->next = node->next;
652
653  err_put:
654         pcmcia_put_dev(p_dev);
655         return ret;
656 } /* get_device_info */
657
658
659 static int ds_open(struct inode *inode, struct file *file)
660 {
661     socket_t i = iminor(inode);
662     struct pcmcia_socket *s;
663     user_info_t *user;
664     static int warning_printed;
665     int ret = 0;
666
667     pr_debug("ds_open(socket %d)\n", i);
668
669     lock_kernel();
670     s = pcmcia_get_socket_by_nr(i);
671     if (!s) {
672             ret = -ENODEV;
673             goto out;
674     }
675     s = pcmcia_get_socket(s);
676     if (!s) {
677             ret = -ENODEV;
678             goto out;
679     }
680
681     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
682             if (s->pcmcia_state.busy) {
683                     pcmcia_put_socket(s);
684                     ret = -EBUSY;
685                     goto out;
686             }
687         else
688             s->pcmcia_state.busy = 1;
689     }
690
691     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
692     if (!user) {
693             pcmcia_put_socket(s);
694             ret = -ENOMEM;
695             goto out;
696     }
697     user->event_tail = user->event_head = 0;
698     user->next = s->user;
699     user->user_magic = USER_MAGIC;
700     user->socket = s;
701     s->user = user;
702     file->private_data = user;
703
704     if (!warning_printed) {
705             printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl "
706                         "usage from process: %s.\n", current->comm);
707             printk(KERN_INFO "pcmcia: This interface will soon be removed from "
708                         "the kernel; please expect breakage unless you upgrade "
709                         "to new tools.\n");
710             printk(KERN_INFO "pcmcia: see http://www.kernel.org/pub/linux/"
711                         "utils/kernel/pcmcia/pcmcia.html for details.\n");
712             warning_printed = 1;
713     }
714
715     if (s->pcmcia_state.present)
716         queue_event(user, CS_EVENT_CARD_INSERTION);
717 out:
718     unlock_kernel();
719     return ret;
720 } /* ds_open */
721
722 /*====================================================================*/
723
724 static int ds_release(struct inode *inode, struct file *file)
725 {
726     struct pcmcia_socket *s;
727     user_info_t *user, **link;
728
729     pr_debug("ds_release(socket %d)\n", iminor(inode));
730
731     user = file->private_data;
732     if (CHECK_USER(user))
733         goto out;
734
735     s = user->socket;
736
737     /* Unlink user data structure */
738     if ((file->f_flags & O_ACCMODE) != O_RDONLY)
739         s->pcmcia_state.busy = 0;
740
741     file->private_data = NULL;
742     for (link = &s->user; *link; link = &(*link)->next)
743         if (*link == user)
744                 break;
745     if (link == NULL)
746         goto out;
747     *link = user->next;
748     user->user_magic = 0;
749     kfree(user);
750     pcmcia_put_socket(s);
751 out:
752     return 0;
753 } /* ds_release */
754
755 /*====================================================================*/
756
757 static ssize_t ds_read(struct file *file, char __user *buf,
758                        size_t count, loff_t *ppos)
759 {
760     struct pcmcia_socket *s;
761     user_info_t *user;
762     int ret;
763
764     pr_debug("ds_read(socket %d)\n", iminor(file->f_path.dentry->d_inode));
765
766     if (count < 4)
767         return -EINVAL;
768
769     user = file->private_data;
770     if (CHECK_USER(user))
771         return -EIO;
772
773     s = user->socket;
774     if (s->pcmcia_state.dead)
775         return -EIO;
776
777     ret = wait_event_interruptible(s->queue, !queue_empty(user));
778     if (ret == 0)
779         ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
780
781     return ret;
782 } /* ds_read */
783
784 /*====================================================================*/
785
786 static ssize_t ds_write(struct file *file, const char __user *buf,
787                         size_t count, loff_t *ppos)
788 {
789     pr_debug("ds_write(socket %d)\n", iminor(file->f_path.dentry->d_inode));
790
791     if (count != 4)
792         return -EINVAL;
793     if ((file->f_flags & O_ACCMODE) == O_RDONLY)
794         return -EBADF;
795
796     return -EIO;
797 } /* ds_write */
798
799 /*====================================================================*/
800
801 /* No kernel lock - fine */
802 static u_int ds_poll(struct file *file, poll_table *wait)
803 {
804     struct pcmcia_socket *s;
805     user_info_t *user;
806
807     pr_debug("ds_poll(socket %d)\n", iminor(file->f_path.dentry->d_inode));
808
809     user = file->private_data;
810     if (CHECK_USER(user))
811         return POLLERR;
812     s = user->socket;
813     /*
814      * We don't check for a dead socket here since that
815      * will send cardmgr into an endless spin.
816      */
817     poll_wait(file, &s->queue, wait);
818     if (!queue_empty(user))
819         return POLLIN | POLLRDNORM;
820     return 0;
821 } /* ds_poll */
822
823 /*====================================================================*/
824
825 static int ds_ioctl(struct inode *inode, struct file *file,
826                     u_int cmd, u_long arg)
827 {
828     struct pcmcia_socket *s;
829     void __user *uarg = (char __user *)arg;
830     u_int size;
831     int ret, err;
832     ds_ioctl_arg_t *buf;
833     user_info_t *user;
834
835     pr_debug("ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
836
837     user = file->private_data;
838     if (CHECK_USER(user))
839         return -EIO;
840
841     s = user->socket;
842     if (s->pcmcia_state.dead)
843         return -EIO;
844
845     size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
846     if (size > sizeof(ds_ioctl_arg_t))
847         return -EINVAL;
848
849     /* Permission check */
850     if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
851         return -EPERM;
852
853     if (cmd & IOC_IN) {
854         if (!access_ok(VERIFY_READ, uarg, size)) {
855             pr_debug("ds_ioctl(): verify_read = %d\n", -EFAULT);
856             return -EFAULT;
857         }
858     }
859     if (cmd & IOC_OUT) {
860         if (!access_ok(VERIFY_WRITE, uarg, size)) {
861             pr_debug("ds_ioctl(): verify_write = %d\n", -EFAULT);
862             return -EFAULT;
863         }
864     }
865     buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
866     if (!buf)
867         return -ENOMEM;
868
869     err = ret = 0;
870
871     if (cmd & IOC_IN) {
872         if (__copy_from_user((char *)buf, uarg, size)) {
873             err = -EFAULT;
874             goto free_out;
875         }
876     }
877
878     switch (cmd) {
879     case DS_ADJUST_RESOURCE_INFO:
880         ret = pcmcia_adjust_resource_info(&buf->adjust);
881         break;
882     case DS_GET_CONFIGURATION_INFO:
883         if (buf->config.Function &&
884            (buf->config.Function >= s->functions))
885             ret = -EINVAL;
886         else {
887             struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->config.Function);
888             ret = pccard_get_configuration_info(s, p_dev, &buf->config);
889             pcmcia_put_dev(p_dev);
890         }
891         break;
892     case DS_GET_FIRST_TUPLE:
893         mutex_lock(&s->skt_mutex);
894         pcmcia_validate_mem(s);
895         mutex_unlock(&s->skt_mutex);
896         ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
897         break;
898     case DS_GET_NEXT_TUPLE:
899         ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple);
900         break;
901     case DS_GET_TUPLE_DATA:
902         buf->tuple.TupleData = buf->tuple_parse.data;
903         buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
904         ret = pccard_get_tuple_data(s, &buf->tuple);
905         break;
906     case DS_PARSE_TUPLE:
907         buf->tuple.TupleData = buf->tuple_parse.data;
908         ret = pcmcia_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
909         break;
910     case DS_RESET_CARD:
911         ret = pcmcia_reset_card(s);
912         break;
913     case DS_GET_STATUS:
914             if (buf->status.Function &&
915                 (buf->status.Function >= s->functions))
916                     ret = -EINVAL;
917             else {
918                     struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->status.Function);
919                     ret = pccard_get_status(s, p_dev, &buf->status);
920                     pcmcia_put_dev(p_dev);
921             }
922             break;
923     case DS_VALIDATE_CIS:
924         mutex_lock(&s->skt_mutex);
925         pcmcia_validate_mem(s);
926         mutex_unlock(&s->skt_mutex);
927         ret = pccard_validate_cis(s, &buf->cisinfo.Chains);
928         break;
929     case DS_SUSPEND_CARD:
930         ret = pcmcia_suspend_card(s);
931         break;
932     case DS_RESUME_CARD:
933         ret = pcmcia_resume_card(s);
934         break;
935     case DS_EJECT_CARD:
936         err = pcmcia_eject_card(s);
937         break;
938     case DS_INSERT_CARD:
939         err = pcmcia_insert_card(s);
940         break;
941     case DS_ACCESS_CONFIGURATION_REGISTER:
942         if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
943             err = -EPERM;
944             goto free_out;
945         }
946
947         ret = -EINVAL;
948
949         if (!(buf->conf_reg.Function &&
950              (buf->conf_reg.Function >= s->functions))) {
951                 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function);
952                 if (p_dev) {
953                         ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg);
954                         pcmcia_put_dev(p_dev);
955                 }
956         }
957         break;
958     case DS_GET_FIRST_REGION:
959     case DS_GET_NEXT_REGION:
960     case DS_BIND_MTD:
961         if (!capable(CAP_SYS_ADMIN)) {
962                 err = -EPERM;
963                 goto free_out;
964         } else {
965                         printk_once(KERN_WARNING
966                                 "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
967                         printk_once(KERN_WARNING "MTD handling any more.\n");
968         }
969         err = -EINVAL;
970         goto free_out;
971         break;
972     case DS_GET_FIRST_WINDOW:
973         ret = pcmcia_get_window(s, &buf->win_info.handle, 1,
974                         &buf->win_info.window);
975         break;
976     case DS_GET_NEXT_WINDOW:
977         ret = pcmcia_get_window(s, &buf->win_info.handle,
978                         buf->win_info.handle + 1, &buf->win_info.window);
979         break;
980     case DS_GET_MEM_PAGE:
981         ret = pcmcia_get_mem_page(s, buf->win_info.handle,
982                            &buf->win_info.map);
983         break;
984     case DS_REPLACE_CIS:
985         ret = pcmcia_replace_cis(s, buf->cisdump.Data, buf->cisdump.Length);
986         break;
987     case DS_BIND_REQUEST:
988         if (!capable(CAP_SYS_ADMIN)) {
989                 err = -EPERM;
990                 goto free_out;
991         }
992         err = bind_request(s, &buf->bind_info);
993         break;
994     case DS_GET_DEVICE_INFO:
995         err = get_device_info(s, &buf->bind_info, 1);
996         break;
997     case DS_GET_NEXT_DEVICE:
998         err = get_device_info(s, &buf->bind_info, 0);
999         break;
1000     case DS_UNBIND_REQUEST:
1001         err = 0;
1002         break;
1003     default:
1004         err = -EINVAL;
1005     }
1006
1007     if ((err == 0) && (ret != 0)) {
1008         pr_debug("ds_ioctl: ret = %d\n", ret);
1009         switch (ret) {
1010         case -ENODEV:
1011         case -EINVAL:
1012         case -EBUSY:
1013         case -ENOSYS:
1014             err = ret;
1015             break;
1016         case -ENOMEM:
1017             err = -ENOSPC; break;
1018         case -ENOSPC:
1019             err = -ENODATA; break;
1020         default:
1021             err = -EIO; break;
1022         }
1023     }
1024
1025     if (cmd & IOC_OUT) {
1026         if (__copy_to_user(uarg, (char *)buf, size))
1027                 err = -EFAULT;
1028     }
1029
1030 free_out:
1031     kfree(buf);
1032     return err;
1033 } /* ds_ioctl */
1034
1035 /*====================================================================*/
1036
1037 static const struct file_operations ds_fops = {
1038         .owner          = THIS_MODULE,
1039         .open           = ds_open,
1040         .release        = ds_release,
1041         .ioctl          = ds_ioctl,
1042         .read           = ds_read,
1043         .write          = ds_write,
1044         .poll           = ds_poll,
1045 };
1046
1047 void __init pcmcia_setup_ioctl(void)
1048 {
1049         int i;
1050
1051         /* Set up character device for user mode clients */
1052         i = register_chrdev(0, "pcmcia", &ds_fops);
1053         if (i < 0)
1054                 printk(KERN_NOTICE "unable to find a free device # for "
1055                        "Driver Services (error=%d)\n", i);
1056         else
1057                 major_dev = i;
1058
1059 #ifdef CONFIG_PROC_FS
1060         proc_pccard = proc_mkdir("bus/pccard", NULL);
1061         if (proc_pccard)
1062                 proc_create("drivers", 0, proc_pccard, &pccard_drivers_proc_fops);
1063 #endif
1064 }
1065
1066
1067 void __exit pcmcia_cleanup_ioctl(void)
1068 {
1069 #ifdef CONFIG_PROC_FS
1070         if (proc_pccard) {
1071                 remove_proc_entry("drivers", proc_pccard);
1072                 remove_proc_entry("bus/pccard", NULL);
1073         }
1074 #endif
1075         if (major_dev != -1)
1076                 unregister_chrdev(major_dev, "pcmcia");
1077 }