2 * pcmcia_ioctl.c -- ioctl interface for cardmgr and cardctl
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.
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.
12 * (C) 1999 David A. Hinds
13 * (C) 2003 - 2004 Dominik Brodowski
17 * This file will go away soon.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/major.h>
25 #include <linux/errno.h>
26 #include <linux/ioctl.h>
27 #include <linux/proc_fs.h>
28 #include <linux/poll.h>
29 #include <linux/pci.h>
30 #include <linux/workqueue.h>
32 #define IN_CARD_SERVICES
33 #include <pcmcia/cs_types.h>
34 #include <pcmcia/cs.h>
35 #include <pcmcia/cistpl.h>
36 #include <pcmcia/ds.h>
37 #include <pcmcia/ss.h>
39 #include "cs_internal.h"
40 #include "ds_internal.h"
42 static int major_dev = -1;
45 /* Device user information */
47 #define USER_MAGIC 0x7ea4
48 #define CHECK_USER(u) \
49 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
51 typedef struct user_info_t {
53 int event_head, event_tail;
54 event_t event[MAX_EVENTS];
55 struct user_info_t *next;
56 struct pcmcia_socket *socket;
61 extern int ds_pc_debug;
62 #define cs_socket_name(skt) ((skt)->dev.class_id)
64 #define ds_dbg(lvl, fmt, arg...) do { \
65 if (ds_pc_debug >= lvl) \
66 printk(KERN_DEBUG "ds: " fmt , ## arg); \
69 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
72 static struct pcmcia_device *get_pcmcia_device(struct pcmcia_socket *s,
73 unsigned int function)
75 struct pcmcia_device *p_dev = NULL;
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);
85 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
89 /* backwards-compatible accessing of driver --- by name! */
91 static struct pcmcia_driver *get_pcmcia_driver(dev_info_t *dev_info)
93 struct device_driver *drv;
94 struct pcmcia_driver *p_drv;
96 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
100 p_drv = container_of(drv, struct pcmcia_driver, drv);
106 #ifdef CONFIG_PROC_FS
107 static struct proc_dir_entry *proc_pccard = NULL;
109 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
112 struct pcmcia_driver *p_drv = container_of(driver,
113 struct pcmcia_driver, drv);
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
127 static int proc_read_drivers(char *buf, char **start, off_t pos,
128 int count, int *eof, void *data)
132 bus_for_each_drv(&pcmcia_bus_type, NULL,
133 (void *) &p, proc_read_drivers_callback);
139 /*======================================================================
141 These manage a ring buffer of events pending for one user process
143 ======================================================================*/
146 static int queue_empty(user_info_t *user)
148 return (user->event_head == user->event_tail);
151 static event_t get_queued_event(user_info_t *user)
153 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
154 return user->event[user->event_tail];
157 static void queue_event(user_info_t *user, event_t event)
159 user->event_head = (user->event_head+1) % MAX_EVENTS;
160 if (user->event_head == user->event_tail)
161 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
162 user->event[user->event_head] = event;
165 void handle_event(struct pcmcia_socket *s, event_t event)
168 for (user = s->user; user; user = user->next)
169 queue_event(user, event);
170 wake_up_interruptible(&s->queue);
174 /*======================================================================
176 bind_request() and bind_device() are merged by now. Register_client()
177 is called right at the end of bind_request(), during the driver's
178 ->attach() call. Individual descriptions:
180 bind_request() connects a socket to a particular client driver.
181 It looks up the specified device ID in the list of registered
182 drivers, binds it to the socket, and tries to create an instance
183 of the device. unbind_request() deletes a driver instance.
185 Bind_device() associates a device driver with a particular socket.
186 It is normally called by Driver Services after it has identified
187 a newly inserted card. An instance of that driver will then be
188 eligible to register as a client of this socket.
190 Register_client() uses the dev_info_t handle to match the
191 caller with a socket. The driver must have already been bound
192 to a socket with bind_device() -- in fact, bind_device()
193 allocates the client structure that will be used.
195 ======================================================================*/
197 static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
199 struct pcmcia_driver *p_drv;
200 struct pcmcia_device *p_dev;
204 s = pcmcia_get_socket(s);
208 ds_dbg(2, "bind_request(%d, '%s')\n", s->sock,
209 (char *)bind_info->dev_info);
211 p_drv = get_pcmcia_driver(&bind_info->dev_info);
217 if (!try_module_get(p_drv->owner)) {
222 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
223 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
224 if (p_dev->func == bind_info->function) {
225 if ((p_dev->dev.driver == &p_drv->drv)) {
226 if (p_dev->cardmgr) {
227 /* if there's already a device
228 * registered, and it was registered
229 * by userspace before, we need to
230 * return the "instance". */
231 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
232 bind_info->instance = p_dev;
236 /* the correct driver managed to bind
237 * itself magically to the correct
239 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
240 p_dev->cardmgr = p_drv;
244 } else if (!p_dev->dev.driver) {
245 /* there's already a device available where
246 * no device has been bound to yet. So we don't
247 * need to register a device! */
248 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
253 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
255 p_dev = pcmcia_device_add(s, bind_info->function);
262 p_dev->cardmgr = p_drv;
264 /* if a driver is already running, we can abort */
265 if (p_dev->dev.driver)
269 * Prevent this racing with a card insertion.
271 mutex_lock(&s->skt_mutex);
272 bus_rescan_devices(&pcmcia_bus_type);
273 mutex_unlock(&s->skt_mutex);
275 /* check whether the driver indeed matched. I don't care if this
276 * is racy or not, because it can only happen on cardmgr access
279 if (!(p_dev->dev.driver == &p_drv->drv))
280 p_dev->cardmgr = NULL;
283 module_put(p_drv->owner);
285 put_driver(&p_drv->drv);
287 pcmcia_put_socket(s);
292 #ifdef CONFIG_CARDBUS
294 static struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
296 if (!s || !(s->state & SOCKET_CARDBUS))
299 return s->cb_dev->subordinate;
303 static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first)
306 struct pcmcia_device *p_dev;
310 #ifdef CONFIG_CARDBUS
312 * Some unbelievably ugly code to associate the PCI cardbus
313 * device and its driver with the PCMCIA "bind" information.
318 bus = pcmcia_lookup_bus(s);
320 struct list_head *list;
321 struct pci_dev *dev = NULL;
323 list = bus->devices.next;
324 while (list != &bus->devices) {
325 struct pci_dev *pdev = pci_dev_b(list);
333 /* Try to handle "next" here some way? */
335 if (dev && dev->driver) {
336 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
337 bind_info->major = 0;
338 bind_info->minor = 0;
339 bind_info->next = NULL;
346 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
347 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
348 if (p_dev->func == bind_info->function) {
349 p_dev = pcmcia_get_dev(p_dev);
355 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
359 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
361 if (p_dev->state & DEV_CONFIG_PENDING) {
367 node = p_dev->dev_node;
369 for (node = p_dev->dev_node; node; node = node->next)
370 if (node == bind_info->next)
377 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
378 bind_info->major = node->major;
379 bind_info->minor = node->minor;
380 bind_info->next = node->next;
383 pcmcia_put_dev(p_dev);
385 } /* get_device_info */
388 static int ds_open(struct inode *inode, struct file *file)
390 socket_t i = iminor(inode);
391 struct pcmcia_socket *s;
393 static int warning_printed = 0;
395 ds_dbg(0, "ds_open(socket %d)\n", i);
397 s = pcmcia_get_socket_by_nr(i);
400 s = pcmcia_get_socket(s);
404 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
405 if (s->pcmcia_state.busy) {
406 pcmcia_put_socket(s);
410 s->pcmcia_state.busy = 1;
413 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
415 pcmcia_put_socket(s);
418 user->event_tail = user->event_head = 0;
419 user->next = s->user;
420 user->user_magic = USER_MAGIC;
423 file->private_data = user;
425 if (!warning_printed) {
426 printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl "
428 printk(KERN_INFO "pcmcia: This interface will soon be removed from "
429 "the kernel; please expect breakage unless you upgrade "
431 printk(KERN_INFO "pcmcia: see http://www.kernel.org/pub/linux/"
432 "utils/kernel/pcmcia/pcmcia.html for details.\n");
436 if (s->pcmcia_state.present)
437 queue_event(user, CS_EVENT_CARD_INSERTION);
441 /*====================================================================*/
443 static int ds_release(struct inode *inode, struct file *file)
445 struct pcmcia_socket *s;
446 user_info_t *user, **link;
448 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
450 user = file->private_data;
451 if (CHECK_USER(user))
456 /* Unlink user data structure */
457 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
458 s->pcmcia_state.busy = 0;
460 file->private_data = NULL;
461 for (link = &s->user; *link; link = &(*link)->next)
462 if (*link == user) break;
466 user->user_magic = 0;
468 pcmcia_put_socket(s);
473 /*====================================================================*/
475 static ssize_t ds_read(struct file *file, char __user *buf,
476 size_t count, loff_t *ppos)
478 struct pcmcia_socket *s;
482 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
487 user = file->private_data;
488 if (CHECK_USER(user))
492 if (s->pcmcia_state.dead)
495 ret = wait_event_interruptible(s->queue, !queue_empty(user));
497 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
502 /*====================================================================*/
504 static ssize_t ds_write(struct file *file, const char __user *buf,
505 size_t count, loff_t *ppos)
507 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
511 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
517 /*====================================================================*/
519 /* No kernel lock - fine */
520 static u_int ds_poll(struct file *file, poll_table *wait)
522 struct pcmcia_socket *s;
525 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
527 user = file->private_data;
528 if (CHECK_USER(user))
532 * We don't check for a dead socket here since that
533 * will send cardmgr into an endless spin.
535 poll_wait(file, &s->queue, wait);
536 if (!queue_empty(user))
537 return POLLIN | POLLRDNORM;
541 /*====================================================================*/
543 extern int pcmcia_adjust_resource_info(adjust_t *adj);
545 static int ds_ioctl(struct inode * inode, struct file * file,
546 u_int cmd, u_long arg)
548 struct pcmcia_socket *s;
549 void __user *uarg = (char __user *)arg;
555 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
557 user = file->private_data;
558 if (CHECK_USER(user))
562 if (s->pcmcia_state.dead)
565 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
566 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
568 /* Permission check */
569 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
573 if (!access_ok(VERIFY_READ, uarg, size)) {
574 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
579 if (!access_ok(VERIFY_WRITE, uarg, size)) {
580 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
584 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
590 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
593 case DS_ADJUST_RESOURCE_INFO:
594 ret = pcmcia_adjust_resource_info(&buf->adjust);
596 case DS_GET_CONFIGURATION_INFO:
597 if (buf->config.Function &&
598 (buf->config.Function >= s->functions))
601 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->config.Function);
602 ret = pccard_get_configuration_info(s, p_dev, &buf->config);
603 pcmcia_put_dev(p_dev);
606 case DS_GET_FIRST_TUPLE:
607 mutex_lock(&s->skt_mutex);
608 pcmcia_validate_mem(s);
609 mutex_unlock(&s->skt_mutex);
610 ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
612 case DS_GET_NEXT_TUPLE:
613 ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple);
615 case DS_GET_TUPLE_DATA:
616 buf->tuple.TupleData = buf->tuple_parse.data;
617 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
618 ret = pccard_get_tuple_data(s, &buf->tuple);
621 buf->tuple.TupleData = buf->tuple_parse.data;
622 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
625 ret = pccard_reset_card(s);
628 if (buf->status.Function &&
629 (buf->status.Function >= s->functions))
632 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->status.Function);
633 ret = pccard_get_status(s, p_dev, &buf->status);
634 pcmcia_put_dev(p_dev);
637 case DS_VALIDATE_CIS:
638 mutex_lock(&s->skt_mutex);
639 pcmcia_validate_mem(s);
640 mutex_unlock(&s->skt_mutex);
641 ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo);
643 case DS_SUSPEND_CARD:
644 ret = pcmcia_suspend_card(s);
647 ret = pcmcia_resume_card(s);
650 err = pcmcia_eject_card(s);
653 err = pcmcia_insert_card(s);
655 case DS_ACCESS_CONFIGURATION_REGISTER:
656 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
663 if (!(buf->conf_reg.Function &&
664 (buf->conf_reg.Function >= s->functions))) {
665 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function);
667 ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg);
668 pcmcia_put_dev(p_dev);
671 case DS_GET_FIRST_REGION:
672 case DS_GET_NEXT_REGION:
674 if (!capable(CAP_SYS_ADMIN)) {
678 static int printed = 0;
680 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
681 printk(KERN_WARNING "MTD handling any more.\n");
688 case DS_GET_FIRST_WINDOW:
689 ret = pcmcia_get_window(s, &buf->win_info.handle, 0,
690 &buf->win_info.window);
692 case DS_GET_NEXT_WINDOW:
693 ret = pcmcia_get_window(s, &buf->win_info.handle,
694 buf->win_info.handle->index + 1, &buf->win_info.window);
696 case DS_GET_MEM_PAGE:
697 ret = pcmcia_get_mem_page(buf->win_info.handle,
701 ret = pcmcia_replace_cis(s, &buf->cisdump);
703 case DS_BIND_REQUEST:
704 if (!capable(CAP_SYS_ADMIN)) {
708 err = bind_request(s, &buf->bind_info);
710 case DS_GET_DEVICE_INFO:
711 err = get_device_info(s, &buf->bind_info, 1);
713 case DS_GET_NEXT_DEVICE:
714 err = get_device_info(s, &buf->bind_info, 0);
716 case DS_UNBIND_REQUEST:
723 if ((err == 0) && (ret != CS_SUCCESS)) {
724 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
726 case CS_BAD_SOCKET: case CS_NO_CARD:
727 err = -ENODEV; break;
728 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
730 err = -EINVAL; break;
733 case CS_OUT_OF_RESOURCE:
734 err = -ENOSPC; break;
735 case CS_NO_MORE_ITEMS:
736 err = -ENODATA; break;
737 case CS_UNSUPPORTED_FUNCTION:
738 err = -ENOSYS; break;
745 if (__copy_to_user(uarg, (char *)buf, size))
754 /*====================================================================*/
756 static struct file_operations ds_fops = {
757 .owner = THIS_MODULE,
759 .release = ds_release,
766 void __init pcmcia_setup_ioctl(void) {
769 /* Set up character device for user mode clients */
770 i = register_chrdev(0, "pcmcia", &ds_fops);
772 printk(KERN_NOTICE "unable to find a free device # for "
773 "Driver Services (error=%d)\n", i);
777 #ifdef CONFIG_PROC_FS
778 proc_pccard = proc_mkdir("pccard", proc_bus);
780 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
785 void __exit pcmcia_cleanup_ioctl(void) {
786 #ifdef CONFIG_PROC_FS
788 remove_proc_entry("drivers", proc_pccard);
789 remove_proc_entry("pccard", proc_bus);
793 unregister_chrdev(major_dev, "pcmcia");