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