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/smp_lock.h>
31 #include <linux/workqueue.h>
33 #define IN_CARD_SERVICES
34 #include <pcmcia/cs_types.h>
35 #include <pcmcia/cs.h>
36 #include <pcmcia/cistpl.h>
37 #include <pcmcia/ds.h>
38 #include <pcmcia/ss.h>
40 #include "cs_internal.h"
41 #include "ds_internal.h"
43 static int major_dev = -1;
46 /* Device user information */
48 #define USER_MAGIC 0x7ea4
49 #define CHECK_USER(u) \
50 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
52 typedef struct user_info_t {
54 int event_head, event_tail;
55 event_t event[MAX_EVENTS];
56 struct user_info_t *next;
57 struct pcmcia_socket *socket;
62 extern int ds_pc_debug;
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)
133 rc = bus_for_each_drv(&pcmcia_bus_type, NULL,
134 (void *) &p, proc_read_drivers_callback);
142 /*======================================================================
144 These manage a ring buffer of events pending for one user process
146 ======================================================================*/
149 static int queue_empty(user_info_t *user)
151 return (user->event_head == user->event_tail);
154 static event_t get_queued_event(user_info_t *user)
156 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
157 return user->event[user->event_tail];
160 static void queue_event(user_info_t *user, event_t event)
162 user->event_head = (user->event_head+1) % MAX_EVENTS;
163 if (user->event_head == user->event_tail)
164 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
165 user->event[user->event_head] = event;
168 void handle_event(struct pcmcia_socket *s, event_t event)
171 for (user = s->user; user; user = user->next)
172 queue_event(user, event);
173 wake_up_interruptible(&s->queue);
177 /*======================================================================
179 bind_request() and bind_device() are merged by now. Register_client()
180 is called right at the end of bind_request(), during the driver's
181 ->attach() call. Individual descriptions:
183 bind_request() connects a socket to a particular client driver.
184 It looks up the specified device ID in the list of registered
185 drivers, binds it to the socket, and tries to create an instance
186 of the device. unbind_request() deletes a driver instance.
188 Bind_device() associates a device driver with a particular socket.
189 It is normally called by Driver Services after it has identified
190 a newly inserted card. An instance of that driver will then be
191 eligible to register as a client of this socket.
193 Register_client() uses the dev_info_t handle to match the
194 caller with a socket. The driver must have already been bound
195 to a socket with bind_device() -- in fact, bind_device()
196 allocates the client structure that will be used.
198 ======================================================================*/
200 static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
202 struct pcmcia_driver *p_drv;
203 struct pcmcia_device *p_dev;
207 s = pcmcia_get_socket(s);
211 ds_dbg(2, "bind_request(%d, '%s')\n", s->sock,
212 (char *)bind_info->dev_info);
214 p_drv = get_pcmcia_driver(&bind_info->dev_info);
220 if (!try_module_get(p_drv->owner)) {
225 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
226 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
227 if (p_dev->func == bind_info->function) {
228 if ((p_dev->dev.driver == &p_drv->drv)) {
229 if (p_dev->cardmgr) {
230 /* if there's already a device
231 * registered, and it was registered
232 * by userspace before, we need to
233 * return the "instance". */
234 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
235 bind_info->instance = p_dev;
239 /* the correct driver managed to bind
240 * itself magically to the correct
242 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
243 p_dev->cardmgr = p_drv;
247 } else if (!p_dev->dev.driver) {
248 /* there's already a device available where
249 * no device has been bound to yet. So we don't
250 * need to register a device! */
251 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
256 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
258 p_dev = pcmcia_device_add(s, bind_info->function);
265 p_dev->cardmgr = p_drv;
267 /* if a driver is already running, we can abort */
268 if (p_dev->dev.driver)
272 * Prevent this racing with a card insertion.
274 mutex_lock(&s->skt_mutex);
275 ret = bus_rescan_devices(&pcmcia_bus_type);
276 mutex_unlock(&s->skt_mutex);
280 /* check whether the driver indeed matched. I don't care if this
281 * is racy or not, because it can only happen on cardmgr access
284 if (!(p_dev->dev.driver == &p_drv->drv))
285 p_dev->cardmgr = NULL;
288 module_put(p_drv->owner);
290 put_driver(&p_drv->drv);
292 pcmcia_put_socket(s);
297 #ifdef CONFIG_CARDBUS
299 static struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s)
301 if (!s || !(s->state & SOCKET_CARDBUS))
304 return s->cb_dev->subordinate;
308 static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first)
311 struct pcmcia_device *p_dev;
312 struct pcmcia_driver *p_drv;
316 #ifdef CONFIG_CARDBUS
318 * Some unbelievably ugly code to associate the PCI cardbus
319 * device and its driver with the PCMCIA "bind" information.
324 bus = pcmcia_lookup_bus(s);
326 struct list_head *list;
327 struct pci_dev *dev = NULL;
329 list = bus->devices.next;
330 while (list != &bus->devices) {
331 struct pci_dev *pdev = pci_dev_b(list);
339 /* Try to handle "next" here some way? */
341 if (dev && dev->driver) {
342 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
343 bind_info->major = 0;
344 bind_info->minor = 0;
345 bind_info->next = NULL;
352 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
353 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
354 if (p_dev->func == bind_info->function) {
355 p_dev = pcmcia_get_dev(p_dev);
361 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
365 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
367 p_drv = to_pcmcia_drv(p_dev->dev.driver);
368 if (p_drv && !p_dev->_locked) {
374 node = p_dev->dev_node;
376 for (node = p_dev->dev_node; node; node = node->next)
377 if (node == bind_info->next)
384 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
385 bind_info->major = node->major;
386 bind_info->minor = node->minor;
387 bind_info->next = node->next;
390 pcmcia_put_dev(p_dev);
392 } /* get_device_info */
395 static int ds_open(struct inode *inode, struct file *file)
397 socket_t i = iminor(inode);
398 struct pcmcia_socket *s;
400 static int warning_printed = 0;
403 ds_dbg(0, "ds_open(socket %d)\n", i);
406 s = pcmcia_get_socket_by_nr(i);
411 s = pcmcia_get_socket(s);
417 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
418 if (s->pcmcia_state.busy) {
419 pcmcia_put_socket(s);
424 s->pcmcia_state.busy = 1;
427 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
429 pcmcia_put_socket(s);
433 user->event_tail = user->event_head = 0;
434 user->next = s->user;
435 user->user_magic = USER_MAGIC;
438 file->private_data = user;
440 if (!warning_printed) {
441 printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl "
442 "usage from process: %s.\n", current->comm);
443 printk(KERN_INFO "pcmcia: This interface will soon be removed from "
444 "the kernel; please expect breakage unless you upgrade "
446 printk(KERN_INFO "pcmcia: see http://www.kernel.org/pub/linux/"
447 "utils/kernel/pcmcia/pcmcia.html for details.\n");
451 if (s->pcmcia_state.present)
452 queue_event(user, CS_EVENT_CARD_INSERTION);
458 /*====================================================================*/
460 static int ds_release(struct inode *inode, struct file *file)
462 struct pcmcia_socket *s;
463 user_info_t *user, **link;
465 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
467 user = file->private_data;
468 if (CHECK_USER(user))
473 /* Unlink user data structure */
474 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
475 s->pcmcia_state.busy = 0;
477 file->private_data = NULL;
478 for (link = &s->user; *link; link = &(*link)->next)
479 if (*link == user) break;
483 user->user_magic = 0;
485 pcmcia_put_socket(s);
490 /*====================================================================*/
492 static ssize_t ds_read(struct file *file, char __user *buf,
493 size_t count, loff_t *ppos)
495 struct pcmcia_socket *s;
499 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_path.dentry->d_inode));
504 user = file->private_data;
505 if (CHECK_USER(user))
509 if (s->pcmcia_state.dead)
512 ret = wait_event_interruptible(s->queue, !queue_empty(user));
514 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
519 /*====================================================================*/
521 static ssize_t ds_write(struct file *file, const char __user *buf,
522 size_t count, loff_t *ppos)
524 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_path.dentry->d_inode));
528 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
534 /*====================================================================*/
536 /* No kernel lock - fine */
537 static u_int ds_poll(struct file *file, poll_table *wait)
539 struct pcmcia_socket *s;
542 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_path.dentry->d_inode));
544 user = file->private_data;
545 if (CHECK_USER(user))
549 * We don't check for a dead socket here since that
550 * will send cardmgr into an endless spin.
552 poll_wait(file, &s->queue, wait);
553 if (!queue_empty(user))
554 return POLLIN | POLLRDNORM;
558 /*====================================================================*/
560 extern int pcmcia_adjust_resource_info(adjust_t *adj);
562 static int ds_ioctl(struct inode * inode, struct file * file,
563 u_int cmd, u_long arg)
565 struct pcmcia_socket *s;
566 void __user *uarg = (char __user *)arg;
572 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
574 user = file->private_data;
575 if (CHECK_USER(user))
579 if (s->pcmcia_state.dead)
582 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
583 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
585 /* Permission check */
586 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
590 if (!access_ok(VERIFY_READ, uarg, size)) {
591 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
596 if (!access_ok(VERIFY_WRITE, uarg, size)) {
597 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
601 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
608 if (__copy_from_user((char *)buf, uarg, size)) {
615 case DS_ADJUST_RESOURCE_INFO:
616 ret = pcmcia_adjust_resource_info(&buf->adjust);
618 case DS_GET_CONFIGURATION_INFO:
619 if (buf->config.Function &&
620 (buf->config.Function >= s->functions))
623 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->config.Function);
624 ret = pccard_get_configuration_info(s, p_dev, &buf->config);
625 pcmcia_put_dev(p_dev);
628 case DS_GET_FIRST_TUPLE:
629 mutex_lock(&s->skt_mutex);
630 pcmcia_validate_mem(s);
631 mutex_unlock(&s->skt_mutex);
632 ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
634 case DS_GET_NEXT_TUPLE:
635 ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple);
637 case DS_GET_TUPLE_DATA:
638 buf->tuple.TupleData = buf->tuple_parse.data;
639 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
640 ret = pccard_get_tuple_data(s, &buf->tuple);
643 buf->tuple.TupleData = buf->tuple_parse.data;
644 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
647 ret = pccard_reset_card(s);
650 if (buf->status.Function &&
651 (buf->status.Function >= s->functions))
654 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->status.Function);
655 ret = pccard_get_status(s, p_dev, &buf->status);
656 pcmcia_put_dev(p_dev);
659 case DS_VALIDATE_CIS:
660 mutex_lock(&s->skt_mutex);
661 pcmcia_validate_mem(s);
662 mutex_unlock(&s->skt_mutex);
663 ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo);
665 case DS_SUSPEND_CARD:
666 ret = pcmcia_suspend_card(s);
669 ret = pcmcia_resume_card(s);
672 err = pcmcia_eject_card(s);
675 err = pcmcia_insert_card(s);
677 case DS_ACCESS_CONFIGURATION_REGISTER:
678 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
685 if (!(buf->conf_reg.Function &&
686 (buf->conf_reg.Function >= s->functions))) {
687 struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function);
689 ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg);
690 pcmcia_put_dev(p_dev);
694 case DS_GET_FIRST_REGION:
695 case DS_GET_NEXT_REGION:
697 if (!capable(CAP_SYS_ADMIN)) {
701 static int printed = 0;
703 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
704 printk(KERN_WARNING "MTD handling any more.\n");
711 case DS_GET_FIRST_WINDOW:
712 ret = pcmcia_get_window(s, &buf->win_info.handle, 0,
713 &buf->win_info.window);
715 case DS_GET_NEXT_WINDOW:
716 ret = pcmcia_get_window(s, &buf->win_info.handle,
717 buf->win_info.handle->index + 1, &buf->win_info.window);
719 case DS_GET_MEM_PAGE:
720 ret = pcmcia_get_mem_page(buf->win_info.handle,
724 ret = pcmcia_replace_cis(s, &buf->cisdump);
726 case DS_BIND_REQUEST:
727 if (!capable(CAP_SYS_ADMIN)) {
731 err = bind_request(s, &buf->bind_info);
733 case DS_GET_DEVICE_INFO:
734 err = get_device_info(s, &buf->bind_info, 1);
736 case DS_GET_NEXT_DEVICE:
737 err = get_device_info(s, &buf->bind_info, 0);
739 case DS_UNBIND_REQUEST:
746 if ((err == 0) && (ret != CS_SUCCESS)) {
747 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
749 case CS_BAD_SOCKET: case CS_NO_CARD:
750 err = -ENODEV; break;
751 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
753 err = -EINVAL; break;
756 case CS_OUT_OF_RESOURCE:
757 err = -ENOSPC; break;
758 case CS_NO_MORE_ITEMS:
759 err = -ENODATA; break;
760 case CS_UNSUPPORTED_FUNCTION:
761 err = -ENOSYS; break;
768 if (__copy_to_user(uarg, (char *)buf, size))
777 /*====================================================================*/
779 static const struct file_operations ds_fops = {
780 .owner = THIS_MODULE,
782 .release = ds_release,
789 void __init pcmcia_setup_ioctl(void) {
792 /* Set up character device for user mode clients */
793 i = register_chrdev(0, "pcmcia", &ds_fops);
795 printk(KERN_NOTICE "unable to find a free device # for "
796 "Driver Services (error=%d)\n", i);
800 #ifdef CONFIG_PROC_FS
801 proc_pccard = proc_mkdir("bus/pccard", NULL);
803 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
808 void __exit pcmcia_cleanup_ioctl(void) {
809 #ifdef CONFIG_PROC_FS
811 remove_proc_entry("drivers", proc_pccard);
812 remove_proc_entry("bus/pccard", NULL);
816 unregister_chrdev(major_dev, "pcmcia");