Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Dominik Brodowski | 3b659fb | 2005-06-27 16:28:51 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/init.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 24 | #include <linux/major.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 25 | #include <linux/errno.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 26 | #include <linux/ioctl.h> |
| 27 | #include <linux/proc_fs.h> |
| 28 | #include <linux/poll.h> |
| 29 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 30 | #include <linux/slab.h> |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 31 | #include <linux/seq_file.h> |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 32 | #include <linux/smp_lock.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 33 | #include <linux/workqueue.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 34 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 35 | #include <pcmcia/cs_types.h> |
| 36 | #include <pcmcia/cs.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 37 | #include <pcmcia/cistpl.h> |
Dominik Brodowski | 4aeba01 | 2008-06-20 13:24:31 +0200 | [diff] [blame] | 38 | #include <pcmcia/cisreg.h> |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 39 | #include <pcmcia/ds.h> |
| 40 | #include <pcmcia/ss.h> |
| 41 | |
| 42 | #include "cs_internal.h" |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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; |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 58 | struct pcmcia_socket *socket; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 59 | } user_info_t; |
| 60 | |
| 61 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 62 | static struct pcmcia_device *get_pcmcia_device(struct pcmcia_socket *s, |
| 63 | unsigned int function) |
| 64 | { |
| 65 | struct pcmcia_device *p_dev = NULL; |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 66 | |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 67 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 68 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
| 69 | if (p_dev->func == function) { |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 70 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 71 | return pcmcia_get_dev(p_dev); |
| 72 | } |
| 73 | } |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 74 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 77 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 78 | /* backwards-compatible accessing of driver --- by name! */ |
| 79 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 80 | static struct pcmcia_driver *get_pcmcia_driver(dev_info_t *dev_info) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 91 | return p_drv; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | #ifdef CONFIG_PROC_FS |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 96 | static struct proc_dir_entry *proc_pccard; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 97 | |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 98 | static int proc_read_drivers_callback(struct device_driver *driver, void *_m) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 99 | { |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 100 | struct seq_file *m = _m; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 101 | struct pcmcia_driver *p_drv = container_of(driver, |
| 102 | struct pcmcia_driver, drv); |
| 103 | |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 104 | seq_printf(m, "%-24.24s 1 %d\n", p_drv->drv.name, |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 105 | #ifdef CONFIG_MODULE_UNLOAD |
| 106 | (p_drv->owner) ? module_refcount(p_drv->owner) : 1 |
| 107 | #else |
| 108 | 1 |
| 109 | #endif |
| 110 | ); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 114 | static int pccard_drivers_proc_show(struct seq_file *m, void *v) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 115 | { |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 116 | return bus_for_each_drv(&pcmcia_bus_type, NULL, |
| 117 | m, proc_read_drivers_callback); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 118 | } |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 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 | }; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 132 | #endif |
| 133 | |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 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)) |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 144 | return -EINVAL; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 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 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 161 | static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj) |
| 162 | { |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 163 | return 0; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | #endif |
| 167 | |
| 168 | static int pcmcia_adjust_resource_info(adjust_t *adj) |
| 169 | { |
| 170 | struct pcmcia_socket *s; |
Dominik Brodowski | de6405e | 2008-08-03 10:47:59 +0200 | [diff] [blame] | 171 | int ret = -ENOSYS; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 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 */ |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 184 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 185 | if ((s->resource_setup_new) && |
| 186 | !(s->resource_setup_old)) { |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 187 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 188 | continue; |
| 189 | } else if (!(s->resource_setup_old)) |
| 190 | s->resource_setup_old = 1; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 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) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 197 | ret = s->resource_ops->add_mem(s, adj->Action, begin, end); |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 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... */ |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 209 | s->resource_setup_done = 1; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 210 | } |
Dominik Brodowski | cfe5d80 | 2010-01-17 19:31:45 +0100 | [diff] [blame] | 211 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | up_read(&pcmcia_socket_list_rwsem); |
| 215 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 216 | return ret; |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 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 | { |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 225 | pccard_mem_map *win; |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 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]; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 239 | req->Base = win->res->start; |
| 240 | req->Size = win->res->end - win->res->start + 1; |
| 241 | req->AccessSpeed = win->speed; |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 242 | req->Attributes = 0; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 243 | if (win->flags & MAP_ATTRIB) |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 244 | req->Attributes |= WIN_MEMORY_TYPE_AM; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 245 | if (win->flags & MAP_ACTIVE) |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 246 | req->Attributes |= WIN_ENABLE; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 247 | if (win->flags & MAP_16BIT) |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 248 | req->Attributes |= WIN_DATA_WIDTH_16; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 249 | if (win->flags & MAP_USE_WAIT) |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 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; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 269 | req->CardOffset = skt->win[wh].card_start; |
Dominik Brodowski | d7b0364 | 2009-11-03 01:05:33 +0100 | [diff] [blame] | 270 | return 0; |
| 271 | } /* pcmcia_get_mem_page */ |
| 272 | |
| 273 | |
Dominik Brodowski | 4aeba01 | 2008-06-20 13:24:31 +0200 | [diff] [blame] | 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)) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 296 | return -ENODEV; |
Dominik Brodowski | 4aeba01 | 2008-06-20 13:24:31 +0200 | [diff] [blame] | 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, ®); |
| 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, ®); |
| 319 | status->CardState |= |
| 320 | (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0; |
| 321 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 322 | return 0; |
Dominik Brodowski | 4aeba01 | 2008-06-20 13:24:31 +0200 | [diff] [blame] | 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; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 332 | return 0; |
Dominik Brodowski | 4aeba01 | 2008-06-20 13:24:31 +0200 | [diff] [blame] | 333 | } /* pccard_get_status */ |
Dominik Brodowski | c502380 | 2008-06-19 19:02:52 +0200 | [diff] [blame] | 334 | |
Roel Kluin | a077932 | 2008-12-09 22:12:50 +0100 | [diff] [blame] | 335 | static int pccard_get_configuration_info(struct pcmcia_socket *s, |
Dominik Brodowski | 64f3464 | 2008-08-02 17:00:46 +0200 | [diff] [blame] | 336 | struct pcmcia_device *p_dev, |
| 337 | config_info_t *config) |
| 338 | { |
| 339 | config_t *c; |
| 340 | |
| 341 | if (!(s->state & SOCKET_PRESENT)) |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 342 | return -ENODEV; |
Dominik Brodowski | 64f3464 | 2008-08-02 17:00:46 +0200 | [diff] [blame] | 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->irq.AssignedIRQ; |
| 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 | } |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 363 | return 0; |
Dominik Brodowski | 64f3464 | 2008-08-02 17:00:46 +0200 | [diff] [blame] | 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; |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 379 | return 0; |
Dominik Brodowski | 64f3464 | 2008-08-02 17:00:46 +0200 | [diff] [blame] | 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->irq.AssignedIRQ; |
| 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 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 403 | return 0; |
Dominik Brodowski | 64f3464 | 2008-08-02 17:00:46 +0200 | [diff] [blame] | 404 | } /* pccard_get_configuration_info */ |
| 405 | |
| 406 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 433 | void handle_event(struct pcmcia_socket *s, event_t event) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 465 | static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 466 | { |
| 467 | struct pcmcia_driver *p_drv; |
| 468 | struct pcmcia_device *p_dev; |
| 469 | int ret = 0; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 470 | |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 471 | s = pcmcia_get_socket(s); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 472 | if (!s) |
| 473 | return -EINVAL; |
| 474 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 475 | pr_debug("bind_request(%d, '%s')\n", s->sock, |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 489 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 490 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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". */ |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 498 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 499 | bind_info->instance = p_dev; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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. */ |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 506 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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! */ |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 515 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 516 | goto rescan; |
| 517 | } |
| 518 | } |
| 519 | } |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 520 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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 | */ |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 538 | mutex_lock(&s->skt_mutex); |
Jeff Garzik | 4deb7c1 | 2006-10-20 14:44:23 -0700 | [diff] [blame] | 539 | ret = bus_rescan_devices(&pcmcia_bus_type); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 540 | mutex_unlock(&s->skt_mutex); |
Jeff Garzik | 4deb7c1 | 2006-10-20 14:44:23 -0700 | [diff] [blame] | 541 | if (ret) |
| 542 | goto err_put_module; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 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: |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 556 | pcmcia_put_socket(s); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 557 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 558 | return ret; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 559 | } /* bind_request */ |
| 560 | |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 561 | #ifdef CONFIG_CARDBUS |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 562 | |
Dominik Brodowski | 33519dd | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 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 |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 571 | |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 572 | static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int first) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 573 | { |
| 574 | dev_node_t *node; |
| 575 | struct pcmcia_device *p_dev; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 576 | struct pcmcia_driver *p_drv; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 577 | int ret = 0; |
| 578 | |
| 579 | #ifdef CONFIG_CARDBUS |
| 580 | /* |
| 581 | * Some unbelievably ugly code to associate the PCI cardbus |
| 582 | * device and its driver with the PCMCIA "bind" information. |
| 583 | */ |
| 584 | { |
| 585 | struct pci_bus *bus; |
| 586 | |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 587 | bus = pcmcia_lookup_bus(s); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 588 | if (bus) { |
| 589 | struct list_head *list; |
| 590 | struct pci_dev *dev = NULL; |
| 591 | |
| 592 | list = bus->devices.next; |
| 593 | while (list != &bus->devices) { |
| 594 | struct pci_dev *pdev = pci_dev_b(list); |
| 595 | list = list->next; |
| 596 | |
| 597 | if (first) { |
| 598 | dev = pdev; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | /* Try to handle "next" here some way? */ |
| 603 | } |
| 604 | if (dev && dev->driver) { |
| 605 | strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN); |
| 606 | bind_info->major = 0; |
| 607 | bind_info->minor = 0; |
| 608 | bind_info->next = NULL; |
| 609 | return 0; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | #endif |
| 614 | |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 615 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 616 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
| 617 | if (p_dev->func == bind_info->function) { |
| 618 | p_dev = pcmcia_get_dev(p_dev); |
| 619 | if (!p_dev) |
| 620 | continue; |
| 621 | goto found; |
| 622 | } |
| 623 | } |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 624 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 625 | return -ENODEV; |
| 626 | |
| 627 | found: |
Dominik Brodowski | 00ce99f | 2010-01-16 09:14:11 +0100 | [diff] [blame] | 628 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 629 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 630 | p_drv = to_pcmcia_drv(p_dev->dev.driver); |
| 631 | if (p_drv && !p_dev->_locked) { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 632 | ret = -EAGAIN; |
| 633 | goto err_put; |
| 634 | } |
| 635 | |
| 636 | if (first) |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 637 | node = p_dev->dev_node; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 638 | else |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 639 | for (node = p_dev->dev_node; node; node = node->next) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 640 | if (node == bind_info->next) |
| 641 | break; |
| 642 | if (!node) { |
| 643 | ret = -ENODEV; |
| 644 | goto err_put; |
| 645 | } |
| 646 | |
| 647 | strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN); |
| 648 | bind_info->major = node->major; |
| 649 | bind_info->minor = node->minor; |
| 650 | bind_info->next = node->next; |
| 651 | |
| 652 | err_put: |
| 653 | pcmcia_put_dev(p_dev); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 654 | return ret; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 655 | } /* get_device_info */ |
| 656 | |
| 657 | |
| 658 | static int ds_open(struct inode *inode, struct file *file) |
| 659 | { |
| 660 | socket_t i = iminor(inode); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 661 | struct pcmcia_socket *s; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 662 | user_info_t *user; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 663 | static int warning_printed; |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 664 | int ret = 0; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 665 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 666 | pr_debug("ds_open(socket %d)\n", i); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 667 | |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 668 | lock_kernel(); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 669 | s = pcmcia_get_socket_by_nr(i); |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 670 | if (!s) { |
| 671 | ret = -ENODEV; |
| 672 | goto out; |
| 673 | } |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 674 | s = pcmcia_get_socket(s); |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 675 | if (!s) { |
| 676 | ret = -ENODEV; |
| 677 | goto out; |
| 678 | } |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 679 | |
| 680 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) { |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 681 | if (s->pcmcia_state.busy) { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 682 | pcmcia_put_socket(s); |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 683 | ret = -EBUSY; |
| 684 | goto out; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 685 | } |
| 686 | else |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 687 | s->pcmcia_state.busy = 1; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | user = kmalloc(sizeof(user_info_t), GFP_KERNEL); |
| 691 | if (!user) { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 692 | pcmcia_put_socket(s); |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 693 | ret = -ENOMEM; |
| 694 | goto out; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 695 | } |
| 696 | user->event_tail = user->event_head = 0; |
| 697 | user->next = s->user; |
| 698 | user->user_magic = USER_MAGIC; |
| 699 | user->socket = s; |
| 700 | s->user = user; |
| 701 | file->private_data = user; |
| 702 | |
Dominik Brodowski | c352ec8 | 2005-09-13 01:25:03 -0700 | [diff] [blame] | 703 | if (!warning_printed) { |
| 704 | printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl " |
Benjamin Herrenschmidt | 73d5858 | 2006-05-15 09:43:53 -0700 | [diff] [blame] | 705 | "usage from process: %s.\n", current->comm); |
Dominik Brodowski | c352ec8 | 2005-09-13 01:25:03 -0700 | [diff] [blame] | 706 | printk(KERN_INFO "pcmcia: This interface will soon be removed from " |
| 707 | "the kernel; please expect breakage unless you upgrade " |
| 708 | "to new tools.\n"); |
| 709 | printk(KERN_INFO "pcmcia: see http://www.kernel.org/pub/linux/" |
| 710 | "utils/kernel/pcmcia/pcmcia.html for details.\n"); |
| 711 | warning_printed = 1; |
| 712 | } |
| 713 | |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 714 | if (s->pcmcia_state.present) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 715 | queue_event(user, CS_EVENT_CARD_INSERTION); |
Jonathan Corbet | 0bec0bb | 2008-05-15 09:25:03 -0600 | [diff] [blame] | 716 | out: |
| 717 | unlock_kernel(); |
| 718 | return ret; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 719 | } /* ds_open */ |
| 720 | |
| 721 | /*====================================================================*/ |
| 722 | |
| 723 | static int ds_release(struct inode *inode, struct file *file) |
| 724 | { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 725 | struct pcmcia_socket *s; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 726 | user_info_t *user, **link; |
| 727 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 728 | pr_debug("ds_release(socket %d)\n", iminor(inode)); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 729 | |
| 730 | user = file->private_data; |
| 731 | if (CHECK_USER(user)) |
| 732 | goto out; |
| 733 | |
| 734 | s = user->socket; |
| 735 | |
| 736 | /* Unlink user data structure */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 737 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 738 | s->pcmcia_state.busy = 0; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 739 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 740 | file->private_data = NULL; |
| 741 | for (link = &s->user; *link; link = &(*link)->next) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 742 | if (*link == user) |
| 743 | break; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 744 | if (link == NULL) |
| 745 | goto out; |
| 746 | *link = user->next; |
| 747 | user->user_magic = 0; |
| 748 | kfree(user); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 749 | pcmcia_put_socket(s); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 750 | out: |
| 751 | return 0; |
| 752 | } /* ds_release */ |
| 753 | |
| 754 | /*====================================================================*/ |
| 755 | |
| 756 | static ssize_t ds_read(struct file *file, char __user *buf, |
| 757 | size_t count, loff_t *ppos) |
| 758 | { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 759 | struct pcmcia_socket *s; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 760 | user_info_t *user; |
| 761 | int ret; |
| 762 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 763 | pr_debug("ds_read(socket %d)\n", iminor(file->f_path.dentry->d_inode)); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 764 | |
| 765 | if (count < 4) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | user = file->private_data; |
| 769 | if (CHECK_USER(user)) |
| 770 | return -EIO; |
| 771 | |
| 772 | s = user->socket; |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 773 | if (s->pcmcia_state.dead) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 774 | return -EIO; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 775 | |
| 776 | ret = wait_event_interruptible(s->queue, !queue_empty(user)); |
| 777 | if (ret == 0) |
| 778 | ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4; |
| 779 | |
| 780 | return ret; |
| 781 | } /* ds_read */ |
| 782 | |
| 783 | /*====================================================================*/ |
| 784 | |
| 785 | static ssize_t ds_write(struct file *file, const char __user *buf, |
| 786 | size_t count, loff_t *ppos) |
| 787 | { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 788 | pr_debug("ds_write(socket %d)\n", iminor(file->f_path.dentry->d_inode)); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 789 | |
| 790 | if (count != 4) |
| 791 | return -EINVAL; |
| 792 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) |
| 793 | return -EBADF; |
| 794 | |
| 795 | return -EIO; |
| 796 | } /* ds_write */ |
| 797 | |
| 798 | /*====================================================================*/ |
| 799 | |
| 800 | /* No kernel lock - fine */ |
| 801 | static u_int ds_poll(struct file *file, poll_table *wait) |
| 802 | { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 803 | struct pcmcia_socket *s; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 804 | user_info_t *user; |
| 805 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 806 | pr_debug("ds_poll(socket %d)\n", iminor(file->f_path.dentry->d_inode)); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 807 | |
| 808 | user = file->private_data; |
| 809 | if (CHECK_USER(user)) |
| 810 | return POLLERR; |
| 811 | s = user->socket; |
| 812 | /* |
| 813 | * We don't check for a dead socket here since that |
| 814 | * will send cardmgr into an endless spin. |
| 815 | */ |
| 816 | poll_wait(file, &s->queue, wait); |
| 817 | if (!queue_empty(user)) |
| 818 | return POLLIN | POLLRDNORM; |
| 819 | return 0; |
| 820 | } /* ds_poll */ |
| 821 | |
| 822 | /*====================================================================*/ |
| 823 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 824 | static int ds_ioctl(struct inode *inode, struct file *file, |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 825 | u_int cmd, u_long arg) |
| 826 | { |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 827 | struct pcmcia_socket *s; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 828 | void __user *uarg = (char __user *)arg; |
| 829 | u_int size; |
| 830 | int ret, err; |
| 831 | ds_ioctl_arg_t *buf; |
| 832 | user_info_t *user; |
| 833 | |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 834 | pr_debug("ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 835 | |
| 836 | user = file->private_data; |
| 837 | if (CHECK_USER(user)) |
| 838 | return -EIO; |
| 839 | |
| 840 | s = user->socket; |
Dominik Brodowski | b5e4391 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 841 | if (s->pcmcia_state.dead) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 842 | return -EIO; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 843 | |
| 844 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 845 | if (size > sizeof(ds_ioctl_arg_t)) |
| 846 | return -EINVAL; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 847 | |
| 848 | /* Permission check */ |
| 849 | if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN)) |
| 850 | return -EPERM; |
| 851 | |
| 852 | if (cmd & IOC_IN) { |
| 853 | if (!access_ok(VERIFY_READ, uarg, size)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 854 | pr_debug("ds_ioctl(): verify_read = %d\n", -EFAULT); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 855 | return -EFAULT; |
| 856 | } |
| 857 | } |
| 858 | if (cmd & IOC_OUT) { |
| 859 | if (!access_ok(VERIFY_WRITE, uarg, size)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 860 | pr_debug("ds_ioctl(): verify_write = %d\n", -EFAULT); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 861 | return -EFAULT; |
| 862 | } |
| 863 | } |
| 864 | buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL); |
| 865 | if (!buf) |
| 866 | return -ENOMEM; |
| 867 | |
| 868 | err = ret = 0; |
| 869 | |
Dominik Brodowski | 9374074 | 2006-11-19 11:21:27 -0500 | [diff] [blame] | 870 | if (cmd & IOC_IN) { |
| 871 | if (__copy_from_user((char *)buf, uarg, size)) { |
| 872 | err = -EFAULT; |
| 873 | goto free_out; |
| 874 | } |
| 875 | } |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 876 | |
| 877 | switch (cmd) { |
| 878 | case DS_ADJUST_RESOURCE_INFO: |
| 879 | ret = pcmcia_adjust_resource_info(&buf->adjust); |
| 880 | break; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 881 | case DS_GET_CONFIGURATION_INFO: |
| 882 | if (buf->config.Function && |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 883 | (buf->config.Function >= s->functions)) |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 884 | ret = -EINVAL; |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 885 | else { |
| 886 | struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->config.Function); |
Daniel Ritz | f47ad21 | 2006-07-30 03:03:47 -0700 | [diff] [blame] | 887 | ret = pccard_get_configuration_info(s, p_dev, &buf->config); |
| 888 | pcmcia_put_dev(p_dev); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 889 | } |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 890 | break; |
| 891 | case DS_GET_FIRST_TUPLE: |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 892 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 893 | pcmcia_validate_mem(s); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 894 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 895 | ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 896 | break; |
| 897 | case DS_GET_NEXT_TUPLE: |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 898 | ret = pccard_get_next_tuple(s, BIND_FN_ALL, &buf->tuple); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 899 | break; |
| 900 | case DS_GET_TUPLE_DATA: |
| 901 | buf->tuple.TupleData = buf->tuple_parse.data; |
| 902 | buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 903 | ret = pccard_get_tuple_data(s, &buf->tuple); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 904 | break; |
| 905 | case DS_PARSE_TUPLE: |
| 906 | buf->tuple.TupleData = buf->tuple_parse.data; |
Dominik Brodowski | 2f3061e | 2008-08-31 15:50:33 +0200 | [diff] [blame] | 907 | ret = pcmcia_parse_tuple(&buf->tuple, &buf->tuple_parse.parse); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 908 | break; |
| 909 | case DS_RESET_CARD: |
Dominik Brodowski | 994917f | 2008-08-31 15:20:26 +0200 | [diff] [blame] | 910 | ret = pcmcia_reset_card(s); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 911 | break; |
| 912 | case DS_GET_STATUS: |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 913 | if (buf->status.Function && |
| 914 | (buf->status.Function >= s->functions)) |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 915 | ret = -EINVAL; |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 916 | else { |
| 917 | struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->status.Function); |
Daniel Ritz | f47ad21 | 2006-07-30 03:03:47 -0700 | [diff] [blame] | 918 | ret = pccard_get_status(s, p_dev, &buf->status); |
| 919 | pcmcia_put_dev(p_dev); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 920 | } |
| 921 | break; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 922 | case DS_VALIDATE_CIS: |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 923 | mutex_lock(&s->skt_mutex); |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 924 | pcmcia_validate_mem(s); |
Dominik Brodowski | 7fe908d | 2006-01-10 21:20:36 +0100 | [diff] [blame] | 925 | mutex_unlock(&s->skt_mutex); |
Dominik Brodowski | 84897fc | 2009-10-18 23:51:09 +0200 | [diff] [blame] | 926 | ret = pccard_validate_cis(s, &buf->cisinfo.Chains); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 927 | break; |
| 928 | case DS_SUSPEND_CARD: |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 929 | pcmcia_parse_uevents(s, PCMCIA_UEVENT_SUSPEND); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 930 | break; |
| 931 | case DS_RESUME_CARD: |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 932 | pcmcia_parse_uevents(s, PCMCIA_UEVENT_RESUME); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 933 | break; |
| 934 | case DS_EJECT_CARD: |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 935 | pcmcia_parse_uevents(s, PCMCIA_UEVENT_EJECT); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 936 | break; |
| 937 | case DS_INSERT_CARD: |
Dominik Brodowski | f971dbd | 2010-01-17 18:13:31 +0100 | [diff] [blame] | 938 | pcmcia_parse_uevents(s, PCMCIA_UEVENT_INSERT); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 939 | break; |
| 940 | case DS_ACCESS_CONFIGURATION_REGISTER: |
| 941 | if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) { |
| 942 | err = -EPERM; |
| 943 | goto free_out; |
| 944 | } |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 945 | |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 946 | ret = -EINVAL; |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 947 | |
| 948 | if (!(buf->conf_reg.Function && |
| 949 | (buf->conf_reg.Function >= s->functions))) { |
| 950 | struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function); |
Benjamin Herrenschmidt | 73d5858 | 2006-05-15 09:43:53 -0700 | [diff] [blame] | 951 | if (p_dev) { |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 952 | ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg); |
Benjamin Herrenschmidt | 73d5858 | 2006-05-15 09:43:53 -0700 | [diff] [blame] | 953 | pcmcia_put_dev(p_dev); |
| 954 | } |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 955 | } |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 956 | break; |
| 957 | case DS_GET_FIRST_REGION: |
| 958 | case DS_GET_NEXT_REGION: |
| 959 | case DS_BIND_MTD: |
| 960 | if (!capable(CAP_SYS_ADMIN)) { |
| 961 | err = -EPERM; |
| 962 | goto free_out; |
| 963 | } else { |
Minchan Kim | a9c5695 | 2009-06-16 15:33:44 -0700 | [diff] [blame] | 964 | printk_once(KERN_WARNING |
| 965 | "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n"); |
| 966 | printk_once(KERN_WARNING "MTD handling any more.\n"); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 967 | } |
| 968 | err = -EINVAL; |
| 969 | goto free_out; |
| 970 | break; |
| 971 | case DS_GET_FIRST_WINDOW: |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 972 | ret = pcmcia_get_window(s, &buf->win_info.handle, 1, |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 973 | &buf->win_info.window); |
| 974 | break; |
| 975 | case DS_GET_NEXT_WINDOW: |
Dominik Brodowski | dc10949 | 2005-06-27 16:28:50 -0700 | [diff] [blame] | 976 | ret = pcmcia_get_window(s, &buf->win_info.handle, |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 977 | buf->win_info.handle + 1, &buf->win_info.window); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 978 | break; |
| 979 | case DS_GET_MEM_PAGE: |
Magnus Damm | 16456eb | 2006-12-13 19:46:48 +0900 | [diff] [blame] | 980 | ret = pcmcia_get_mem_page(s, buf->win_info.handle, |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 981 | &buf->win_info.map); |
| 982 | break; |
| 983 | case DS_REPLACE_CIS: |
Dominik Brodowski | 53efec9 | 2008-07-28 19:44:05 +0200 | [diff] [blame] | 984 | ret = pcmcia_replace_cis(s, buf->cisdump.Data, buf->cisdump.Length); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 985 | break; |
| 986 | case DS_BIND_REQUEST: |
| 987 | if (!capable(CAP_SYS_ADMIN)) { |
| 988 | err = -EPERM; |
| 989 | goto free_out; |
| 990 | } |
| 991 | err = bind_request(s, &buf->bind_info); |
| 992 | break; |
| 993 | case DS_GET_DEVICE_INFO: |
| 994 | err = get_device_info(s, &buf->bind_info, 1); |
| 995 | break; |
| 996 | case DS_GET_NEXT_DEVICE: |
| 997 | err = get_device_info(s, &buf->bind_info, 0); |
| 998 | break; |
| 999 | case DS_UNBIND_REQUEST: |
| 1000 | err = 0; |
| 1001 | break; |
| 1002 | default: |
| 1003 | err = -EINVAL; |
| 1004 | } |
| 1005 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 1006 | if ((err == 0) && (ret != 0)) { |
Dominik Brodowski | d50dbec | 2009-10-23 12:51:28 +0200 | [diff] [blame] | 1007 | pr_debug("ds_ioctl: ret = %d\n", ret); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1008 | switch (ret) { |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 1009 | case -ENODEV: |
| 1010 | case -EINVAL: |
| 1011 | case -EBUSY: |
| 1012 | case -ENOSYS: |
| 1013 | err = ret; |
| 1014 | break; |
Dominik Brodowski | 610e237 | 2008-08-03 11:58:53 +0200 | [diff] [blame] | 1015 | case -ENOMEM: |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1016 | err = -ENOSPC; break; |
Dominik Brodowski | 635d19b | 2008-08-03 11:47:29 +0200 | [diff] [blame] | 1017 | case -ENOSPC: |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1018 | err = -ENODATA; break; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1019 | default: |
| 1020 | err = -EIO; break; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | if (cmd & IOC_OUT) { |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1025 | if (__copy_to_user(uarg, (char *)buf, size)) |
| 1026 | err = -EFAULT; |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | free_out: |
| 1030 | kfree(buf); |
| 1031 | return err; |
| 1032 | } /* ds_ioctl */ |
| 1033 | |
| 1034 | /*====================================================================*/ |
| 1035 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1036 | static const struct file_operations ds_fops = { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1037 | .owner = THIS_MODULE, |
| 1038 | .open = ds_open, |
| 1039 | .release = ds_release, |
| 1040 | .ioctl = ds_ioctl, |
| 1041 | .read = ds_read, |
| 1042 | .write = ds_write, |
| 1043 | .poll = ds_poll, |
| 1044 | }; |
| 1045 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1046 | void __init pcmcia_setup_ioctl(void) |
| 1047 | { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1048 | int i; |
| 1049 | |
| 1050 | /* Set up character device for user mode clients */ |
| 1051 | i = register_chrdev(0, "pcmcia", &ds_fops); |
Dominik Brodowski | 1a8ceaf | 2005-06-27 16:29:00 -0700 | [diff] [blame] | 1052 | if (i < 0) |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1053 | printk(KERN_NOTICE "unable to find a free device # for " |
Dominik Brodowski | 1a8ceaf | 2005-06-27 16:29:00 -0700 | [diff] [blame] | 1054 | "Driver Services (error=%d)\n", i); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1055 | else |
| 1056 | major_dev = i; |
| 1057 | |
| 1058 | #ifdef CONFIG_PROC_FS |
akpm@linux-foundation.org | 97094dc | 2008-04-29 10:47:54 -0700 | [diff] [blame] | 1059 | proc_pccard = proc_mkdir("bus/pccard", NULL); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1060 | if (proc_pccard) |
Alexey Dobriyan | e6be4a8 | 2009-09-21 17:03:55 -0700 | [diff] [blame] | 1061 | proc_create("drivers", 0, proc_pccard, &pccard_drivers_proc_fops); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1062 | #endif |
| 1063 | } |
| 1064 | |
| 1065 | |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 1066 | void __exit pcmcia_cleanup_ioctl(void) |
| 1067 | { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1068 | #ifdef CONFIG_PROC_FS |
| 1069 | if (proc_pccard) { |
| 1070 | remove_proc_entry("drivers", proc_pccard); |
akpm@linux-foundation.org | 97094dc | 2008-04-29 10:47:54 -0700 | [diff] [blame] | 1071 | remove_proc_entry("bus/pccard", NULL); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame] | 1072 | } |
| 1073 | #endif |
| 1074 | if (major_dev != -1) |
| 1075 | unregister_chrdev(major_dev, "pcmcia"); |
| 1076 | } |