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