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/config.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/major.h>
26 #include <linux/errno.h>
27 #include <linux/ioctl.h>
28 #include <linux/proc_fs.h>
29 #include <linux/poll.h>
30 #include <linux/pci.h>
31 #include <linux/workqueue.h>
33 #define IN_CARD_SERVICES
34 #include <pcmcia/version.h>
35 #include <pcmcia/cs_types.h>
36 #include <pcmcia/cs.h>
37 #include <pcmcia/cistpl.h>
38 #include <pcmcia/ds.h>
39 #include <pcmcia/ss.h>
41 #include "cs_internal.h"
42 #include "ds_internal.h"
44 static int major_dev = -1;
47 /* Device user information */
49 #define USER_MAGIC 0x7ea4
50 #define CHECK_USER(u) \
51 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
53 typedef struct user_info_t {
55 int event_head, event_tail;
56 event_t event[MAX_EVENTS];
57 struct user_info_t *next;
58 struct pcmcia_socket *socket;
63 extern int ds_pc_debug;
64 #define cs_socket_name(skt) ((skt)->dev.class_id)
66 #define ds_dbg(lvl, fmt, arg...) do { \
67 if (ds_pc_debug >= lvl) \
68 printk(KERN_DEBUG "ds: " fmt , ## arg); \
71 #define ds_dbg(lvl, fmt, arg...) do { } while (0)
75 /* backwards-compatible accessing of driver --- by name! */
77 static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
79 struct device_driver *drv;
80 struct pcmcia_driver *p_drv;
82 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
86 p_drv = container_of(drv, struct pcmcia_driver, drv);
93 static struct proc_dir_entry *proc_pccard = NULL;
95 static int proc_read_drivers_callback(struct device_driver *driver, void *d)
98 struct pcmcia_driver *p_drv = container_of(driver,
99 struct pcmcia_driver, drv);
101 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
102 #ifdef CONFIG_MODULE_UNLOAD
103 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
113 static int proc_read_drivers(char *buf, char **start, off_t pos,
114 int count, int *eof, void *data)
118 bus_for_each_drv(&pcmcia_bus_type, NULL,
119 (void *) &p, proc_read_drivers_callback);
125 /*======================================================================
127 These manage a ring buffer of events pending for one user process
129 ======================================================================*/
132 static int queue_empty(user_info_t *user)
134 return (user->event_head == user->event_tail);
137 static event_t get_queued_event(user_info_t *user)
139 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
140 return user->event[user->event_tail];
143 static void queue_event(user_info_t *user, event_t event)
145 user->event_head = (user->event_head+1) % MAX_EVENTS;
146 if (user->event_head == user->event_tail)
147 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
148 user->event[user->event_head] = event;
151 void handle_event(struct pcmcia_socket *s, event_t event)
154 for (user = s->user; user; user = user->next)
155 queue_event(user, event);
156 wake_up_interruptible(&s->queue);
160 /*======================================================================
162 bind_request() and bind_device() are merged by now. Register_client()
163 is called right at the end of bind_request(), during the driver's
164 ->attach() call. Individual descriptions:
166 bind_request() connects a socket to a particular client driver.
167 It looks up the specified device ID in the list of registered
168 drivers, binds it to the socket, and tries to create an instance
169 of the device. unbind_request() deletes a driver instance.
171 Bind_device() associates a device driver with a particular socket.
172 It is normally called by Driver Services after it has identified
173 a newly inserted card. An instance of that driver will then be
174 eligible to register as a client of this socket.
176 Register_client() uses the dev_info_t handle to match the
177 caller with a socket. The driver must have already been bound
178 to a socket with bind_device() -- in fact, bind_device()
179 allocates the client structure that will be used.
181 ======================================================================*/
183 static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
185 struct pcmcia_driver *p_drv;
186 struct pcmcia_device *p_dev;
190 s = pcmcia_get_socket(s);
194 ds_dbg(2, "bind_request(%d, '%s')\n", s->sock,
195 (char *)bind_info->dev_info);
197 p_drv = get_pcmcia_driver(&bind_info->dev_info);
203 if (!try_module_get(p_drv->owner)) {
208 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
209 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
210 if (p_dev->func == bind_info->function) {
211 if ((p_dev->dev.driver == &p_drv->drv)) {
212 if (p_dev->cardmgr) {
213 /* if there's already a device
214 * registered, and it was registered
215 * by userspace before, we need to
216 * return the "instance". */
217 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
218 bind_info->instance = p_dev->instance;
222 /* the correct driver managed to bind
223 * itself magically to the correct
225 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
226 p_dev->cardmgr = p_drv;
230 } else if (!p_dev->dev.driver) {
231 /* there's already a device available where
232 * no device has been bound to yet. So we don't
233 * need to register a device! */
234 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
239 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
241 p_dev = pcmcia_device_add(s, bind_info->function);
248 p_dev->cardmgr = p_drv;
250 /* if a driver is already running, we can abort */
251 if (p_dev->dev.driver)
255 * Prevent this racing with a card insertion.
258 bus_rescan_devices(&pcmcia_bus_type);
261 /* check whether the driver indeed matched. I don't care if this
262 * is racy or not, because it can only happen on cardmgr access
265 if (!(p_dev->dev.driver == &p_drv->drv))
266 p_dev->cardmgr = NULL;
269 module_put(p_drv->owner);
271 put_driver(&p_drv->drv);
273 pcmcia_put_socket(s);
279 extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
281 static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first)
284 struct pcmcia_device *p_dev;
288 #ifdef CONFIG_CARDBUS
290 * Some unbelievably ugly code to associate the PCI cardbus
291 * device and its driver with the PCMCIA "bind" information.
296 bus = pcmcia_lookup_bus(s);
298 struct list_head *list;
299 struct pci_dev *dev = NULL;
301 list = bus->devices.next;
302 while (list != &bus->devices) {
303 struct pci_dev *pdev = pci_dev_b(list);
311 /* Try to handle "next" here some way? */
313 if (dev && dev->driver) {
314 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
315 bind_info->major = 0;
316 bind_info->minor = 0;
317 bind_info->next = NULL;
324 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
325 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
326 if (p_dev->func == bind_info->function) {
327 p_dev = pcmcia_get_dev(p_dev);
333 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
337 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
339 if ((!p_dev->instance) ||
340 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
346 node = p_dev->instance->dev;
348 for (node = p_dev->instance->dev; node; node = node->next)
349 if (node == bind_info->next)
356 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
357 bind_info->major = node->major;
358 bind_info->minor = node->minor;
359 bind_info->next = node->next;
362 pcmcia_put_dev(p_dev);
364 } /* get_device_info */
367 static int ds_open(struct inode *inode, struct file *file)
369 socket_t i = iminor(inode);
370 struct pcmcia_socket *s;
373 ds_dbg(0, "ds_open(socket %d)\n", i);
375 s = pcmcia_get_socket_by_nr(i);
378 s = pcmcia_get_socket(s);
382 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
383 if (s->pcmcia_state.busy) {
384 pcmcia_put_socket(s);
388 s->pcmcia_state.busy = 1;
391 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
393 pcmcia_put_socket(s);
396 user->event_tail = user->event_head = 0;
397 user->next = s->user;
398 user->user_magic = USER_MAGIC;
401 file->private_data = user;
403 if (s->pcmcia_state.present)
404 queue_event(user, CS_EVENT_CARD_INSERTION);
408 /*====================================================================*/
410 static int ds_release(struct inode *inode, struct file *file)
412 struct pcmcia_socket *s;
413 user_info_t *user, **link;
415 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
417 user = file->private_data;
418 if (CHECK_USER(user))
423 /* Unlink user data structure */
424 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
425 s->pcmcia_state.busy = 0;
427 file->private_data = NULL;
428 for (link = &s->user; *link; link = &(*link)->next)
429 if (*link == user) break;
433 user->user_magic = 0;
435 pcmcia_put_socket(s);
440 /*====================================================================*/
442 static ssize_t ds_read(struct file *file, char __user *buf,
443 size_t count, loff_t *ppos)
445 struct pcmcia_socket *s;
449 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
454 user = file->private_data;
455 if (CHECK_USER(user))
459 if (s->pcmcia_state.dead)
462 ret = wait_event_interruptible(s->queue, !queue_empty(user));
464 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
469 /*====================================================================*/
471 static ssize_t ds_write(struct file *file, const char __user *buf,
472 size_t count, loff_t *ppos)
474 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
478 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
484 /*====================================================================*/
486 /* No kernel lock - fine */
487 static u_int ds_poll(struct file *file, poll_table *wait)
489 struct pcmcia_socket *s;
492 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
494 user = file->private_data;
495 if (CHECK_USER(user))
499 * We don't check for a dead socket here since that
500 * will send cardmgr into an endless spin.
502 poll_wait(file, &s->queue, wait);
503 if (!queue_empty(user))
504 return POLLIN | POLLRDNORM;
508 /*====================================================================*/
510 extern int pcmcia_adjust_resource_info(adjust_t *adj);
512 static int ds_ioctl(struct inode * inode, struct file * file,
513 u_int cmd, u_long arg)
515 struct pcmcia_socket *s;
516 void __user *uarg = (char __user *)arg;
522 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
524 user = file->private_data;
525 if (CHECK_USER(user))
529 if (s->pcmcia_state.dead)
532 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
533 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
535 /* Permission check */
536 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
540 if (!access_ok(VERIFY_READ, uarg, size)) {
541 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
546 if (!access_ok(VERIFY_WRITE, uarg, size)) {
547 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
551 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
557 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
560 case DS_ADJUST_RESOURCE_INFO:
561 ret = pcmcia_adjust_resource_info(&buf->adjust);
563 case DS_GET_CARD_SERVICES_INFO:
564 ret = pcmcia_get_card_services_info(&buf->servinfo);
566 case DS_GET_CONFIGURATION_INFO:
567 if (buf->config.Function &&
568 (buf->config.Function >= s->functions))
571 ret = pccard_get_configuration_info(s,
572 buf->config.Function, &buf->config);
574 case DS_GET_FIRST_TUPLE:
576 pcmcia_validate_mem(s);
578 ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
580 case DS_GET_NEXT_TUPLE:
581 ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple);
583 case DS_GET_TUPLE_DATA:
584 buf->tuple.TupleData = buf->tuple_parse.data;
585 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
586 ret = pccard_get_tuple_data(s, &buf->tuple);
589 buf->tuple.TupleData = buf->tuple_parse.data;
590 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
593 ret = pccard_reset_card(s);
596 if (buf->status.Function &&
597 (buf->status.Function >= s->functions))
600 ret = pccard_get_status(s, buf->status.Function, &buf->status);
602 case DS_VALIDATE_CIS:
604 pcmcia_validate_mem(s);
606 ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo);
608 case DS_SUSPEND_CARD:
609 ret = pcmcia_suspend_card(s);
612 ret = pcmcia_resume_card(s);
615 err = pcmcia_eject_card(s);
618 err = pcmcia_insert_card(s);
620 case DS_ACCESS_CONFIGURATION_REGISTER:
621 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
625 if (buf->conf_reg.Function &&
626 (buf->conf_reg.Function >= s->functions))
629 ret = pccard_access_configuration_register(s,
630 buf->conf_reg.Function, &buf->conf_reg);
632 case DS_GET_FIRST_REGION:
633 case DS_GET_NEXT_REGION:
635 if (!capable(CAP_SYS_ADMIN)) {
639 static int printed = 0;
641 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
642 printk(KERN_WARNING "MTD handling any more.\n");
649 case DS_GET_FIRST_WINDOW:
650 ret = pcmcia_get_window(s, &buf->win_info.handle, 0,
651 &buf->win_info.window);
653 case DS_GET_NEXT_WINDOW:
654 ret = pcmcia_get_window(s, &buf->win_info.handle,
655 buf->win_info.handle->index + 1, &buf->win_info.window);
657 case DS_GET_MEM_PAGE:
658 ret = pcmcia_get_mem_page(buf->win_info.handle,
662 ret = pcmcia_replace_cis(s, &buf->cisdump);
664 case DS_BIND_REQUEST:
665 if (!capable(CAP_SYS_ADMIN)) {
669 err = bind_request(s, &buf->bind_info);
671 case DS_GET_DEVICE_INFO:
672 err = get_device_info(s, &buf->bind_info, 1);
674 case DS_GET_NEXT_DEVICE:
675 err = get_device_info(s, &buf->bind_info, 0);
677 case DS_UNBIND_REQUEST:
684 if ((err == 0) && (ret != CS_SUCCESS)) {
685 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
687 case CS_BAD_SOCKET: case CS_NO_CARD:
688 err = -ENODEV; break;
689 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
691 err = -EINVAL; break;
694 case CS_OUT_OF_RESOURCE:
695 err = -ENOSPC; break;
696 case CS_NO_MORE_ITEMS:
697 err = -ENODATA; break;
698 case CS_UNSUPPORTED_FUNCTION:
699 err = -ENOSYS; break;
706 if (__copy_to_user(uarg, (char *)buf, size))
715 /*====================================================================*/
717 static struct file_operations ds_fops = {
718 .owner = THIS_MODULE,
720 .release = ds_release,
727 void __init pcmcia_setup_ioctl(void) {
730 /* Set up character device for user mode clients */
731 i = register_chrdev(0, "pcmcia", &ds_fops);
733 printk(KERN_NOTICE "unable to find a free device # for "
734 "Driver Services\n");
738 #ifdef CONFIG_PROC_FS
739 proc_pccard = proc_mkdir("pccard", proc_bus);
741 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
746 void __exit pcmcia_cleanup_ioctl(void) {
747 #ifdef CONFIG_PROC_FS
749 remove_proc_entry("drivers", proc_pccard);
750 remove_proc_entry("pccard", proc_bus);
754 unregister_chrdev(major_dev, "pcmcia");