aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pcmcia
diff options
context:
space:
mode:
authorDominik Brodowski <linux@dominikbrodowski.net>2005-06-27 19:28:47 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-06-27 21:03:18 -0400
commite7a480d229461e54a0b3b0439b2bf0e652545e3d (patch)
treea1a8226fcd713e5db3e26a99ad877e4fd1a58edd /drivers/pcmcia
parent9a5555b81fde402119a6b4f2b38d3373d272ff69 (diff)
[PATCH] pcmcia: move PCMCIA ioctl to a separate file
Move all PCMCIA_IOCTL-related code to a different file. Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net> From: Richard Purdie <rpurdie@rpsys.net> The pcmcia-move-pcmcia-ioctl-to-a-separate-file patch was corrupted in -mm2 causing this problem. Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/pcmcia')
-rw-r--r--drivers/pcmcia/Makefile1
-rw-r--r--drivers/pcmcia/ds.c795
-rw-r--r--drivers/pcmcia/ds_internal.h55
-rw-r--r--drivers/pcmcia/pcmcia_ioctl.c779
4 files changed, 850 insertions, 780 deletions
diff --git a/drivers/pcmcia/Makefile b/drivers/pcmcia/Makefile
index 50c29361bc5f..195e8104ae6b 100644
--- a/drivers/pcmcia/Makefile
+++ b/drivers/pcmcia/Makefile
@@ -11,6 +11,7 @@ pcmcia_core-$(CONFIG_CARDBUS) += cardbus.o
11obj-$(CONFIG_PCCARD) += pcmcia_core.o 11obj-$(CONFIG_PCCARD) += pcmcia_core.o
12 12
13pcmcia-y += ds.o pcmcia_compat.o 13pcmcia-y += ds.o pcmcia_compat.o
14pcmcia-$(CONFIG_PCMCIA_IOCTL) += pcmcia_ioctl.o
14obj-$(CONFIG_PCMCIA) += pcmcia.o 15obj-$(CONFIG_PCMCIA) += pcmcia.o
15 16
16obj-$(CONFIG_PCCARD_NONSTATIC) += rsrc_nonstatic.o 17obj-$(CONFIG_PCCARD_NONSTATIC) += rsrc_nonstatic.o
diff --git a/drivers/pcmcia/ds.c b/drivers/pcmcia/ds.c
index 0c7dc5b38757..80b34b65511c 100644
--- a/drivers/pcmcia/ds.c
+++ b/drivers/pcmcia/ds.c
@@ -50,6 +50,7 @@
50#include <pcmcia/ss.h> 50#include <pcmcia/ss.h>
51 51
52#include "cs_internal.h" 52#include "cs_internal.h"
53#include "ds_internal.h"
53 54
54/*====================================================================*/ 55/*====================================================================*/
55 56
@@ -60,7 +61,7 @@ MODULE_DESCRIPTION("PCMCIA Driver Services");
60MODULE_LICENSE("GPL"); 61MODULE_LICENSE("GPL");
61 62
62#ifdef DEBUG 63#ifdef DEBUG
63static int ds_pc_debug; 64int ds_pc_debug;
64 65
65module_param_named(pc_debug, ds_pc_debug, int, 0644); 66module_param_named(pc_debug, ds_pc_debug, int, 0644);
66 67
@@ -72,57 +73,7 @@ module_param_named(pc_debug, ds_pc_debug, int, 0644);
72#define ds_dbg(lvl, fmt, arg...) do { } while (0) 73#define ds_dbg(lvl, fmt, arg...) do { } while (0)
73#endif 74#endif
74 75
75/*====================================================================*/ 76spinlock_t pcmcia_dev_list_lock;
76
77/* Device user information */
78#define MAX_EVENTS 32
79#define USER_MAGIC 0x7ea4
80#define CHECK_USER(u) \
81 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
82typedef struct user_info_t {
83 u_int user_magic;
84 int event_head, event_tail;
85 event_t event[MAX_EVENTS];
86 struct user_info_t *next;
87 struct pcmcia_bus_socket *socket;
88} user_info_t;
89
90/* Socket state information */
91struct pcmcia_bus_socket {
92 struct kref refcount;
93 struct pcmcia_callback callback;
94 int state;
95 user_info_t *user;
96 wait_queue_head_t queue;
97 struct pcmcia_socket *parent;
98
99 /* the PCMCIA devices connected to this socket (normally one, more
100 * for multifunction devices: */
101 struct list_head devices_list;
102 u8 device_count; /* the number of devices, used
103 * only internally and subject
104 * to incorrectness and change */
105
106 u8 device_add_pending;
107 struct work_struct device_add;
108};
109static spinlock_t pcmcia_dev_list_lock;
110
111static struct bus_type pcmcia_bus_type;
112
113#define DS_SOCKET_PRESENT 0x01
114#define DS_SOCKET_BUSY 0x02
115#define DS_SOCKET_REMOVAL_PENDING 0x10
116#define DS_SOCKET_DEAD 0x80
117
118/*====================================================================*/
119#ifdef CONFIG_PCMCIA_IOCTL
120
121static int major_dev = -1;
122static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr);
123static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info);
124
125#endif
126 77
127static int unbind_request(struct pcmcia_bus_socket *s); 78static int unbind_request(struct pcmcia_bus_socket *s);
128 79
@@ -362,19 +313,19 @@ static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filenam
362/*======================================================================*/ 313/*======================================================================*/
363 314
364 315
365static void pcmcia_release_bus_socket(struct kref *refcount) 316void pcmcia_release_bus_socket(struct kref *refcount)
366{ 317{
367 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount); 318 struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount);
368 pcmcia_put_socket(s->parent); 319 pcmcia_put_socket(s->parent);
369 kfree(s); 320 kfree(s);
370} 321}
371 322
372static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s) 323void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s)
373{ 324{
374 kref_put(&s->refcount, pcmcia_release_bus_socket); 325 kref_put(&s->refcount, pcmcia_release_bus_socket);
375} 326}
376 327
377static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s) 328struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s)
378{ 329{
379 kref_get(&s->refcount); 330 kref_get(&s->refcount);
380 return (s); 331 return (s);
@@ -414,44 +365,10 @@ void pcmcia_unregister_driver(struct pcmcia_driver *driver)
414} 365}
415EXPORT_SYMBOL(pcmcia_unregister_driver); 366EXPORT_SYMBOL(pcmcia_unregister_driver);
416 367
417#ifdef CONFIG_PCMCIA_IOCTL
418#ifdef CONFIG_PROC_FS
419static struct proc_dir_entry *proc_pccard = NULL;
420
421static int proc_read_drivers_callback(struct device_driver *driver, void *d)
422{
423 char **p = d;
424 struct pcmcia_driver *p_drv = container_of(driver,
425 struct pcmcia_driver, drv);
426
427 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
428#ifdef CONFIG_MODULE_UNLOAD
429 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
430#else
431 1
432#endif
433 );
434 d = (void *) p;
435
436 return 0;
437}
438
439static int proc_read_drivers(char *buf, char **start, off_t pos,
440 int count, int *eof, void *data)
441{
442 char *p = buf;
443
444 bus_for_each_drv(&pcmcia_bus_type, NULL,
445 (void *) &p, proc_read_drivers_callback);
446
447 return (p - buf);
448}
449#endif
450#endif
451 368
452/* pcmcia_device handling */ 369/* pcmcia_device handling */
453 370
454static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev) 371struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
455{ 372{
456 struct device *tmp_dev; 373 struct device *tmp_dev;
457 tmp_dev = get_device(&p_dev->dev); 374 tmp_dev = get_device(&p_dev->dev);
@@ -460,7 +377,7 @@ static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
460 return to_pcmcia_dev(tmp_dev); 377 return to_pcmcia_dev(tmp_dev);
461} 378}
462 379
463static void pcmcia_put_dev(struct pcmcia_device *p_dev) 380void pcmcia_put_dev(struct pcmcia_device *p_dev)
464{ 381{
465 if (p_dev) 382 if (p_dev)
466 put_device(&p_dev->dev); 383 put_device(&p_dev->dev);
@@ -605,7 +522,7 @@ static int pcmcia_device_query(struct pcmcia_device *p_dev)
605 */ 522 */
606static DECLARE_MUTEX(device_add_lock); 523static DECLARE_MUTEX(device_add_lock);
607 524
608static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function) 525struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function)
609{ 526{
610 struct pcmcia_device *p_dev; 527 struct pcmcia_device *p_dev;
611 unsigned long flags; 528 unsigned long flags;
@@ -999,47 +916,6 @@ static struct device_attribute pcmcia_dev_attrs[] = {
999 916
1000/*====================================================================== 917/*======================================================================
1001 918
1002 These manage a ring buffer of events pending for one user process
1003
1004======================================================================*/
1005
1006#ifdef CONFIG_PCMCIA_IOCTL
1007
1008static int queue_empty(user_info_t *user)
1009{
1010 return (user->event_head == user->event_tail);
1011}
1012
1013static event_t get_queued_event(user_info_t *user)
1014{
1015 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
1016 return user->event[user->event_tail];
1017}
1018
1019static void queue_event(user_info_t *user, event_t event)
1020{
1021 user->event_head = (user->event_head+1) % MAX_EVENTS;
1022 if (user->event_head == user->event_tail)
1023 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
1024 user->event[user->event_head] = event;
1025}
1026
1027static void handle_event(struct pcmcia_bus_socket *s, event_t event)
1028{
1029 user_info_t *user;
1030 for (user = s->user; user; user = user->next)
1031 queue_event(user, event);
1032 wake_up_interruptible(&s->queue);
1033}
1034#else
1035static inline void handle_event(struct pcmcia_bus_socket *s, event_t event) { return; }
1036static inline int handle_request(struct pcmcia_bus_socket *s, event_t event) { return CS_SUCCESS; }
1037#endif
1038
1039
1040
1041/*======================================================================
1042
1043 The card status event handler. 919 The card status event handler.
1044 920
1045======================================================================*/ 921======================================================================*/
@@ -1131,128 +1007,6 @@ static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
1131} /* ds_event */ 1007} /* ds_event */
1132 1008
1133 1009
1134/*======================================================================
1135
1136 bind_request() and bind_device() are merged by now. Register_client()
1137 is called right at the end of bind_request(), during the driver's
1138 ->attach() call. Individual descriptions:
1139
1140 bind_request() connects a socket to a particular client driver.
1141 It looks up the specified device ID in the list of registered
1142 drivers, binds it to the socket, and tries to create an instance
1143 of the device. unbind_request() deletes a driver instance.
1144
1145 Bind_device() associates a device driver with a particular socket.
1146 It is normally called by Driver Services after it has identified
1147 a newly inserted card. An instance of that driver will then be
1148 eligible to register as a client of this socket.
1149
1150 Register_client() uses the dev_info_t handle to match the
1151 caller with a socket. The driver must have already been bound
1152 to a socket with bind_device() -- in fact, bind_device()
1153 allocates the client structure that will be used.
1154
1155======================================================================*/
1156
1157#ifdef CONFIG_PCMCIA_IOCTL
1158
1159static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
1160{
1161 struct pcmcia_driver *p_drv;
1162 struct pcmcia_device *p_dev;
1163 int ret = 0;
1164 unsigned long flags;
1165
1166 s = pcmcia_get_bus_socket(s);
1167 if (!s)
1168 return -EINVAL;
1169
1170 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
1171 (char *)bind_info->dev_info);
1172
1173 p_drv = get_pcmcia_driver(&bind_info->dev_info);
1174 if (!p_drv) {
1175 ret = -EINVAL;
1176 goto err_put;
1177 }
1178
1179 if (!try_module_get(p_drv->owner)) {
1180 ret = -EINVAL;
1181 goto err_put_driver;
1182 }
1183
1184 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1185 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1186 if (p_dev->func == bind_info->function) {
1187 if ((p_dev->dev.driver == &p_drv->drv)) {
1188 if (p_dev->cardmgr) {
1189 /* if there's already a device
1190 * registered, and it was registered
1191 * by userspace before, we need to
1192 * return the "instance". */
1193 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1194 bind_info->instance = p_dev->instance;
1195 ret = -EBUSY;
1196 goto err_put_module;
1197 } else {
1198 /* the correct driver managed to bind
1199 * itself magically to the correct
1200 * device. */
1201 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1202 p_dev->cardmgr = p_drv;
1203 ret = 0;
1204 goto err_put_module;
1205 }
1206 } else if (!p_dev->dev.driver) {
1207 /* there's already a device available where
1208 * no device has been bound to yet. So we don't
1209 * need to register a device! */
1210 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1211 goto rescan;
1212 }
1213 }
1214 }
1215 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1216
1217 p_dev = pcmcia_device_add(s, bind_info->function);
1218 if (!p_dev) {
1219 ret = -EIO;
1220 goto err_put_module;
1221 }
1222
1223rescan:
1224 p_dev->cardmgr = p_drv;
1225
1226 /* if a driver is already running, we can abort */
1227 if (p_dev->dev.driver)
1228 goto err_put_module;
1229
1230 /*
1231 * Prevent this racing with a card insertion.
1232 */
1233 down(&s->parent->skt_sem);
1234 bus_rescan_devices(&pcmcia_bus_type);
1235 up(&s->parent->skt_sem);
1236
1237 /* check whether the driver indeed matched. I don't care if this
1238 * is racy or not, because it can only happen on cardmgr access
1239 * paths...
1240 */
1241 if (!(p_dev->dev.driver == &p_drv->drv))
1242 p_dev->cardmgr = NULL;
1243
1244 err_put_module:
1245 module_put(p_drv->owner);
1246 err_put_driver:
1247 put_driver(&p_drv->drv);
1248 err_put:
1249 pcmcia_put_bus_socket(s);
1250
1251 return (ret);
1252} /* bind_request */
1253
1254#endif
1255
1256 1010
1257int pcmcia_register_client(client_handle_t *handle, client_reg_t *req) 1011int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1258{ 1012{
@@ -1349,100 +1103,6 @@ int pcmcia_register_client(client_handle_t *handle, client_reg_t *req)
1349EXPORT_SYMBOL(pcmcia_register_client); 1103EXPORT_SYMBOL(pcmcia_register_client);
1350 1104
1351 1105
1352/*====================================================================*/
1353#ifdef CONFIG_PCMCIA_IOCTL
1354
1355extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
1356
1357static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
1358{
1359 dev_node_t *node;
1360 struct pcmcia_device *p_dev;
1361 unsigned long flags;
1362 int ret = 0;
1363
1364#ifdef CONFIG_CARDBUS
1365 /*
1366 * Some unbelievably ugly code to associate the PCI cardbus
1367 * device and its driver with the PCMCIA "bind" information.
1368 */
1369 {
1370 struct pci_bus *bus;
1371
1372 bus = pcmcia_lookup_bus(s->parent);
1373 if (bus) {
1374 struct list_head *list;
1375 struct pci_dev *dev = NULL;
1376
1377 list = bus->devices.next;
1378 while (list != &bus->devices) {
1379 struct pci_dev *pdev = pci_dev_b(list);
1380 list = list->next;
1381
1382 if (first) {
1383 dev = pdev;
1384 break;
1385 }
1386
1387 /* Try to handle "next" here some way? */
1388 }
1389 if (dev && dev->driver) {
1390 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
1391 bind_info->major = 0;
1392 bind_info->minor = 0;
1393 bind_info->next = NULL;
1394 return 0;
1395 }
1396 }
1397 }
1398#endif
1399
1400 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
1401 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
1402 if (p_dev->func == bind_info->function) {
1403 p_dev = pcmcia_get_dev(p_dev);
1404 if (!p_dev)
1405 continue;
1406 goto found;
1407 }
1408 }
1409 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1410 return -ENODEV;
1411
1412 found:
1413 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
1414
1415 if ((!p_dev->instance) ||
1416 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
1417 ret = -EAGAIN;
1418 goto err_put;
1419 }
1420
1421 if (first)
1422 node = p_dev->instance->dev;
1423 else
1424 for (node = p_dev->instance->dev; node; node = node->next)
1425 if (node == bind_info->next)
1426 break;
1427 if (!node) {
1428 ret = -ENODEV;
1429 goto err_put;
1430 }
1431
1432 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
1433 bind_info->major = node->major;
1434 bind_info->minor = node->minor;
1435 bind_info->next = node->next;
1436
1437 err_put:
1438 pcmcia_put_dev(p_dev);
1439 return (ret);
1440} /* get_device_info */
1441
1442#endif
1443
1444/*====================================================================*/
1445
1446/* unbind _all_ devices attached to a given pcmcia_bus_socket. The 1106/* unbind _all_ devices attached to a given pcmcia_bus_socket. The
1447 * drivers have been called with EVENT_CARD_REMOVAL before. 1107 * drivers have been called with EVENT_CARD_REMOVAL before.
1448 */ 1108 */
@@ -1507,377 +1167,6 @@ int pcmcia_deregister_client(client_handle_t handle)
1507} /* deregister_client */ 1167} /* deregister_client */
1508EXPORT_SYMBOL(pcmcia_deregister_client); 1168EXPORT_SYMBOL(pcmcia_deregister_client);
1509 1169
1510
1511/*======================================================================
1512
1513 The user-mode PC Card device interface
1514
1515======================================================================*/
1516
1517#ifdef CONFIG_PCMCIA_IOCTL
1518
1519static int ds_open(struct inode *inode, struct file *file)
1520{
1521 socket_t i = iminor(inode);
1522 struct pcmcia_bus_socket *s;
1523 user_info_t *user;
1524
1525 ds_dbg(0, "ds_open(socket %d)\n", i);
1526
1527 s = get_socket_info_by_nr(i);
1528 if (!s)
1529 return -ENODEV;
1530 s = pcmcia_get_bus_socket(s);
1531 if (!s)
1532 return -ENODEV;
1533
1534 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1535 if (s->state & DS_SOCKET_BUSY) {
1536 pcmcia_put_bus_socket(s);
1537 return -EBUSY;
1538 }
1539 else
1540 s->state |= DS_SOCKET_BUSY;
1541 }
1542
1543 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
1544 if (!user) {
1545 pcmcia_put_bus_socket(s);
1546 return -ENOMEM;
1547 }
1548 user->event_tail = user->event_head = 0;
1549 user->next = s->user;
1550 user->user_magic = USER_MAGIC;
1551 user->socket = s;
1552 s->user = user;
1553 file->private_data = user;
1554
1555 if (s->state & DS_SOCKET_PRESENT)
1556 queue_event(user, CS_EVENT_CARD_INSERTION);
1557 return 0;
1558} /* ds_open */
1559
1560/*====================================================================*/
1561
1562static int ds_release(struct inode *inode, struct file *file)
1563{
1564 struct pcmcia_bus_socket *s;
1565 user_info_t *user, **link;
1566
1567 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
1568
1569 user = file->private_data;
1570 if (CHECK_USER(user))
1571 goto out;
1572
1573 s = user->socket;
1574
1575 /* Unlink user data structure */
1576 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1577 s->state &= ~DS_SOCKET_BUSY;
1578 }
1579 file->private_data = NULL;
1580 for (link = &s->user; *link; link = &(*link)->next)
1581 if (*link == user) break;
1582 if (link == NULL)
1583 goto out;
1584 *link = user->next;
1585 user->user_magic = 0;
1586 kfree(user);
1587 pcmcia_put_bus_socket(s);
1588out:
1589 return 0;
1590} /* ds_release */
1591
1592/*====================================================================*/
1593
1594static ssize_t ds_read(struct file *file, char __user *buf,
1595 size_t count, loff_t *ppos)
1596{
1597 struct pcmcia_bus_socket *s;
1598 user_info_t *user;
1599 int ret;
1600
1601 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
1602
1603 if (count < 4)
1604 return -EINVAL;
1605
1606 user = file->private_data;
1607 if (CHECK_USER(user))
1608 return -EIO;
1609
1610 s = user->socket;
1611 if (s->state & DS_SOCKET_DEAD)
1612 return -EIO;
1613
1614 ret = wait_event_interruptible(s->queue, !queue_empty(user));
1615 if (ret == 0)
1616 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
1617
1618 return ret;
1619} /* ds_read */
1620
1621/*====================================================================*/
1622
1623static ssize_t ds_write(struct file *file, const char __user *buf,
1624 size_t count, loff_t *ppos)
1625{
1626 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
1627
1628 if (count != 4)
1629 return -EINVAL;
1630 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1631 return -EBADF;
1632
1633 return -EIO;
1634} /* ds_write */
1635
1636/*====================================================================*/
1637
1638/* No kernel lock - fine */
1639static u_int ds_poll(struct file *file, poll_table *wait)
1640{
1641 struct pcmcia_bus_socket *s;
1642 user_info_t *user;
1643
1644 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
1645
1646 user = file->private_data;
1647 if (CHECK_USER(user))
1648 return POLLERR;
1649 s = user->socket;
1650 /*
1651 * We don't check for a dead socket here since that
1652 * will send cardmgr into an endless spin.
1653 */
1654 poll_wait(file, &s->queue, wait);
1655 if (!queue_empty(user))
1656 return POLLIN | POLLRDNORM;
1657 return 0;
1658} /* ds_poll */
1659
1660/*====================================================================*/
1661
1662extern int pcmcia_adjust_resource_info(adjust_t *adj);
1663
1664static int ds_ioctl(struct inode * inode, struct file * file,
1665 u_int cmd, u_long arg)
1666{
1667 struct pcmcia_bus_socket *s;
1668 void __user *uarg = (char __user *)arg;
1669 u_int size;
1670 int ret, err;
1671 ds_ioctl_arg_t *buf;
1672 user_info_t *user;
1673
1674 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
1675
1676 user = file->private_data;
1677 if (CHECK_USER(user))
1678 return -EIO;
1679
1680 s = user->socket;
1681 if (s->state & DS_SOCKET_DEAD)
1682 return -EIO;
1683
1684 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
1685 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
1686
1687 /* Permission check */
1688 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
1689 return -EPERM;
1690
1691 if (cmd & IOC_IN) {
1692 if (!access_ok(VERIFY_READ, uarg, size)) {
1693 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
1694 return -EFAULT;
1695 }
1696 }
1697 if (cmd & IOC_OUT) {
1698 if (!access_ok(VERIFY_WRITE, uarg, size)) {
1699 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
1700 return -EFAULT;
1701 }
1702 }
1703 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
1704 if (!buf)
1705 return -ENOMEM;
1706
1707 err = ret = 0;
1708
1709 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
1710
1711 switch (cmd) {
1712 case DS_ADJUST_RESOURCE_INFO:
1713 ret = pcmcia_adjust_resource_info(&buf->adjust);
1714 break;
1715 case DS_GET_CARD_SERVICES_INFO:
1716 ret = pcmcia_get_card_services_info(&buf->servinfo);
1717 break;
1718 case DS_GET_CONFIGURATION_INFO:
1719 if (buf->config.Function &&
1720 (buf->config.Function >= s->parent->functions))
1721 ret = CS_BAD_ARGS;
1722 else
1723 ret = pccard_get_configuration_info(s->parent,
1724 buf->config.Function, &buf->config);
1725 break;
1726 case DS_GET_FIRST_TUPLE:
1727 down(&s->parent->skt_sem);
1728 pcmcia_validate_mem(s->parent);
1729 up(&s->parent->skt_sem);
1730 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1731 break;
1732 case DS_GET_NEXT_TUPLE:
1733 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
1734 break;
1735 case DS_GET_TUPLE_DATA:
1736 buf->tuple.TupleData = buf->tuple_parse.data;
1737 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
1738 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
1739 break;
1740 case DS_PARSE_TUPLE:
1741 buf->tuple.TupleData = buf->tuple_parse.data;
1742 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
1743 break;
1744 case DS_RESET_CARD:
1745 ret = pccard_reset_card(s->parent);
1746 break;
1747 case DS_GET_STATUS:
1748 if (buf->status.Function &&
1749 (buf->status.Function >= s->parent->functions))
1750 ret = CS_BAD_ARGS;
1751 else
1752 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
1753 break;
1754 case DS_VALIDATE_CIS:
1755 down(&s->parent->skt_sem);
1756 pcmcia_validate_mem(s->parent);
1757 up(&s->parent->skt_sem);
1758 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
1759 break;
1760 case DS_SUSPEND_CARD:
1761 ret = pcmcia_suspend_card(s->parent);
1762 break;
1763 case DS_RESUME_CARD:
1764 ret = pcmcia_resume_card(s->parent);
1765 break;
1766 case DS_EJECT_CARD:
1767 err = pcmcia_eject_card(s->parent);
1768 break;
1769 case DS_INSERT_CARD:
1770 err = pcmcia_insert_card(s->parent);
1771 break;
1772 case DS_ACCESS_CONFIGURATION_REGISTER:
1773 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
1774 err = -EPERM;
1775 goto free_out;
1776 }
1777 if (buf->conf_reg.Function &&
1778 (buf->conf_reg.Function >= s->parent->functions))
1779 ret = CS_BAD_ARGS;
1780 else
1781 ret = pccard_access_configuration_register(s->parent,
1782 buf->conf_reg.Function, &buf->conf_reg);
1783 break;
1784 case DS_GET_FIRST_REGION:
1785 case DS_GET_NEXT_REGION:
1786 case DS_BIND_MTD:
1787 if (!capable(CAP_SYS_ADMIN)) {
1788 err = -EPERM;
1789 goto free_out;
1790 } else {
1791 static int printed = 0;
1792 if (!printed) {
1793 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
1794 printk(KERN_WARNING "MTD handling any more.\n");
1795 printed++;
1796 }
1797 }
1798 err = -EINVAL;
1799 goto free_out;
1800 break;
1801 case DS_GET_FIRST_WINDOW:
1802 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
1803 &buf->win_info.window);
1804 break;
1805 case DS_GET_NEXT_WINDOW:
1806 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
1807 buf->win_info.handle->index + 1, &buf->win_info.window);
1808 break;
1809 case DS_GET_MEM_PAGE:
1810 ret = pcmcia_get_mem_page(buf->win_info.handle,
1811 &buf->win_info.map);
1812 break;
1813 case DS_REPLACE_CIS:
1814 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
1815 break;
1816 case DS_BIND_REQUEST:
1817 if (!capable(CAP_SYS_ADMIN)) {
1818 err = -EPERM;
1819 goto free_out;
1820 }
1821 err = bind_request(s, &buf->bind_info);
1822 break;
1823 case DS_GET_DEVICE_INFO:
1824 err = get_device_info(s, &buf->bind_info, 1);
1825 break;
1826 case DS_GET_NEXT_DEVICE:
1827 err = get_device_info(s, &buf->bind_info, 0);
1828 break;
1829 case DS_UNBIND_REQUEST:
1830 err = 0;
1831 break;
1832 default:
1833 err = -EINVAL;
1834 }
1835
1836 if ((err == 0) && (ret != CS_SUCCESS)) {
1837 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
1838 switch (ret) {
1839 case CS_BAD_SOCKET: case CS_NO_CARD:
1840 err = -ENODEV; break;
1841 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
1842 case CS_BAD_TUPLE:
1843 err = -EINVAL; break;
1844 case CS_IN_USE:
1845 err = -EBUSY; break;
1846 case CS_OUT_OF_RESOURCE:
1847 err = -ENOSPC; break;
1848 case CS_NO_MORE_ITEMS:
1849 err = -ENODATA; break;
1850 case CS_UNSUPPORTED_FUNCTION:
1851 err = -ENOSYS; break;
1852 default:
1853 err = -EIO; break;
1854 }
1855 }
1856
1857 if (cmd & IOC_OUT) {
1858 if (__copy_to_user(uarg, (char *)buf, size))
1859 err = -EFAULT;
1860 }
1861
1862free_out:
1863 kfree(buf);
1864 return err;
1865} /* ds_ioctl */
1866
1867/*====================================================================*/
1868
1869static struct file_operations ds_fops = {
1870 .owner = THIS_MODULE,
1871 .open = ds_open,
1872 .release = ds_release,
1873 .ioctl = ds_ioctl,
1874 .read = ds_read,
1875 .write = ds_write,
1876 .poll = ds_poll,
1877};
1878
1879#endif
1880
1881static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev) 1170static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1882{ 1171{
1883 struct pcmcia_socket *socket = class_get_devdata(class_dev); 1172 struct pcmcia_socket *socket = class_get_devdata(class_dev);
@@ -1905,7 +1194,9 @@ static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
1905 */ 1194 */
1906 msleep(250); 1195 msleep(250);
1907 1196
1197#ifdef CONFIG_PCMCIA_IOCTL
1908 init_waitqueue_head(&s->queue); 1198 init_waitqueue_head(&s->queue);
1199#endif
1909 INIT_LIST_HEAD(&s->devices_list); 1200 INIT_LIST_HEAD(&s->devices_list);
1910 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s); 1201 INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s);
1911 1202
@@ -1952,7 +1243,7 @@ static struct class_interface pcmcia_bus_interface = {
1952}; 1243};
1953 1244
1954 1245
1955static struct bus_type pcmcia_bus_type = { 1246struct bus_type pcmcia_bus_type = {
1956 .name = "pcmcia", 1247 .name = "pcmcia",
1957 .hotplug = pcmcia_bus_hotplug, 1248 .hotplug = pcmcia_bus_hotplug,
1958 .match = pcmcia_bus_match, 1249 .match = pcmcia_bus_match,
@@ -1962,30 +1253,12 @@ static struct bus_type pcmcia_bus_type = {
1962 1253
1963static int __init init_pcmcia_bus(void) 1254static int __init init_pcmcia_bus(void)
1964{ 1255{
1965#ifdef CONFIG_PCMCIA_IOCTL
1966 int i;
1967#endif
1968
1969 spin_lock_init(&pcmcia_dev_list_lock); 1256 spin_lock_init(&pcmcia_dev_list_lock);
1970 1257
1971 bus_register(&pcmcia_bus_type); 1258 bus_register(&pcmcia_bus_type);
1972 class_interface_register(&pcmcia_bus_interface); 1259 class_interface_register(&pcmcia_bus_interface);
1973 1260
1974#ifdef CONFIG_PCMCIA_IOCTL 1261 pcmcia_setup_ioctl();
1975 /* Set up character device for user mode clients */
1976 i = register_chrdev(0, "pcmcia", &ds_fops);
1977 if (i < 0)
1978 printk(KERN_NOTICE "unable to find a free device # for "
1979 "Driver Services (error=%d)\n", i);
1980 else
1981 major_dev = i;
1982
1983#ifdef CONFIG_PROC_FS
1984 proc_pccard = proc_mkdir("pccard", proc_bus);
1985 if (proc_pccard)
1986 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
1987#endif
1988#endif
1989 1262
1990 return 0; 1263 return 0;
1991} 1264}
@@ -1995,51 +1268,13 @@ fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
1995 1268
1996static void __exit exit_pcmcia_bus(void) 1269static void __exit exit_pcmcia_bus(void)
1997{ 1270{
1998 class_interface_unregister(&pcmcia_bus_interface); 1271 pcmcia_cleanup_ioctl();
1999 1272
2000#ifdef CONFIG_PCMCIA_IOCTL 1273 class_interface_unregister(&pcmcia_bus_interface);
2001#ifdef CONFIG_PROC_FS
2002 if (proc_pccard) {
2003 remove_proc_entry("drivers", proc_pccard);
2004 remove_proc_entry("pccard", proc_bus);
2005 }
2006#endif
2007 if (major_dev != -1)
2008 unregister_chrdev(major_dev, "pcmcia");
2009#endif
2010 1274
2011 bus_unregister(&pcmcia_bus_type); 1275 bus_unregister(&pcmcia_bus_type);
2012} 1276}
2013module_exit(exit_pcmcia_bus); 1277module_exit(exit_pcmcia_bus);
2014 1278
2015 1279
2016
2017/* helpers for backwards-compatible functions */
2018#ifdef CONFIG_PCMCIA_IOCTL
2019static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
2020{
2021 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
2022 if (s && s->pcmcia)
2023 return s->pcmcia;
2024 else
2025 return NULL;
2026}
2027
2028/* backwards-compatible accessing of driver --- by name! */
2029
2030static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
2031{
2032 struct device_driver *drv;
2033 struct pcmcia_driver *p_drv;
2034
2035 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
2036 if (!drv)
2037 return NULL;
2038
2039 p_drv = container_of(drv, struct pcmcia_driver, drv);
2040
2041 return (p_drv);
2042}
2043#endif
2044
2045MODULE_ALIAS("ds"); 1280MODULE_ALIAS("ds");
diff --git a/drivers/pcmcia/ds_internal.h b/drivers/pcmcia/ds_internal.h
new file mode 100644
index 000000000000..0de90e5e67a7
--- /dev/null
+++ b/drivers/pcmcia/ds_internal.h
@@ -0,0 +1,55 @@
1/* ds_internal.h - internal header for 16-bit PCMCIA devices management */
2
3struct user_info_t;
4
5/* Socket state information */
6struct pcmcia_bus_socket {
7 struct kref refcount;
8 struct pcmcia_callback callback;
9 int state;
10 struct pcmcia_socket *parent;
11
12 /* the PCMCIA devices connected to this socket (normally one, more
13 * for multifunction devices: */
14 struct list_head devices_list;
15 u8 device_count; /* the number of devices, used
16 * only internally and subject
17 * to incorrectness and change */
18
19 u8 device_add_pending;
20 struct work_struct device_add;
21
22
23#ifdef CONFIG_PCMCIA_IOCTL
24 struct user_info_t *user;
25 wait_queue_head_t queue;
26#endif
27};
28extern spinlock_t pcmcia_dev_list_lock;
29
30extern struct bus_type pcmcia_bus_type;
31
32
33#define DS_SOCKET_PRESENT 0x01
34#define DS_SOCKET_BUSY 0x02
35#define DS_SOCKET_DEAD 0x80
36
37extern struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev);
38extern void pcmcia_put_dev(struct pcmcia_device *p_dev);
39
40struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s);
41void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s);
42
43struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function);
44
45#ifdef CONFIG_PCMCIA_IOCTL
46extern void __init pcmcia_setup_ioctl(void);
47extern void __exit pcmcia_cleanup_ioctl(void);
48extern void handle_event(struct pcmcia_bus_socket *s, event_t event);
49extern int handle_request(struct pcmcia_bus_socket *s, event_t event);
50#else
51static inline void __init pcmcia_setup_ioctl(void) { return; }
52static inline void __init pcmcia_cleanup_ioctl(void) { return; }
53static inline void handle_event(struct pcmcia_bus_socket *s, event_t event) { return; }
54static inline int handle_request(struct pcmcia_bus_socket *s, event_t event) { return CS_SUCCESS; }
55#endif
diff --git a/drivers/pcmcia/pcmcia_ioctl.c b/drivers/pcmcia/pcmcia_ioctl.c
new file mode 100644
index 000000000000..e8d2c95db1c9
--- /dev/null
+++ b/drivers/pcmcia/pcmcia_ioctl.c
@@ -0,0 +1,779 @@
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
21#include <linux/config.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/major.h>
27#include <linux/string.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/fcntl.h>
32#include <linux/sched.h>
33#include <linux/smp_lock.h>
34#include <linux/timer.h>
35#include <linux/ioctl.h>
36#include <linux/proc_fs.h>
37#include <linux/poll.h>
38#include <linux/pci.h>
39#include <linux/list.h>
40#include <linux/delay.h>
41#include <linux/kref.h>
42#include <linux/workqueue.h>
43#include <linux/crc32.h>
44
45#include <asm/atomic.h>
46
47#define IN_CARD_SERVICES
48#include <pcmcia/version.h>
49#include <pcmcia/cs_types.h>
50#include <pcmcia/cs.h>
51#include <pcmcia/bulkmem.h>
52#include <pcmcia/cistpl.h>
53#include <pcmcia/ds.h>
54#include <pcmcia/ss.h>
55
56#include "cs_internal.h"
57#include "ds_internal.h"
58
59static int major_dev = -1;
60
61
62/* Device user information */
63#define MAX_EVENTS 32
64#define USER_MAGIC 0x7ea4
65#define CHECK_USER(u) \
66 (((u) == NULL) || ((u)->user_magic != USER_MAGIC))
67
68typedef struct user_info_t {
69 u_int user_magic;
70 int event_head, event_tail;
71 event_t event[MAX_EVENTS];
72 struct user_info_t *next;
73 struct pcmcia_bus_socket *socket;
74} user_info_t;
75
76
77#ifdef DEBUG
78extern int ds_pc_debug;
79#define cs_socket_name(skt) ((skt)->dev.class_id)
80
81#define ds_dbg(lvl, fmt, arg...) do { \
82 if (ds_pc_debug >= lvl) \
83 printk(KERN_DEBUG "ds: " fmt , ## arg); \
84} while (0)
85#else
86#define ds_dbg(lvl, fmt, arg...) do { } while (0)
87#endif
88
89
90static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr)
91{
92 struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr);
93 if (s && s->pcmcia)
94 return s->pcmcia;
95 else
96 return NULL;
97}
98
99/* backwards-compatible accessing of driver --- by name! */
100
101static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info)
102{
103 struct device_driver *drv;
104 struct pcmcia_driver *p_drv;
105
106 drv = driver_find((char *) dev_info, &pcmcia_bus_type);
107 if (!drv)
108 return NULL;
109
110 p_drv = container_of(drv, struct pcmcia_driver, drv);
111
112 return (p_drv);
113}
114
115
116#ifdef CONFIG_PROC_FS
117static struct proc_dir_entry *proc_pccard = NULL;
118
119static int proc_read_drivers_callback(struct device_driver *driver, void *d)
120{
121 char **p = d;
122 struct pcmcia_driver *p_drv = container_of(driver,
123 struct pcmcia_driver, drv);
124
125 *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name,
126#ifdef CONFIG_MODULE_UNLOAD
127 (p_drv->owner) ? module_refcount(p_drv->owner) : 1
128#else
129 1
130#endif
131 );
132 d = (void *) p;
133
134 return 0;
135}
136
137static int proc_read_drivers(char *buf, char **start, off_t pos,
138 int count, int *eof, void *data)
139{
140 char *p = buf;
141
142 bus_for_each_drv(&pcmcia_bus_type, NULL,
143 (void *) &p, proc_read_drivers_callback);
144
145 return (p - buf);
146}
147#endif
148
149/*======================================================================
150
151 These manage a ring buffer of events pending for one user process
152
153======================================================================*/
154
155
156static int queue_empty(user_info_t *user)
157{
158 return (user->event_head == user->event_tail);
159}
160
161static event_t get_queued_event(user_info_t *user)
162{
163 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
164 return user->event[user->event_tail];
165}
166
167static void queue_event(user_info_t *user, event_t event)
168{
169 user->event_head = (user->event_head+1) % MAX_EVENTS;
170 if (user->event_head == user->event_tail)
171 user->event_tail = (user->event_tail+1) % MAX_EVENTS;
172 user->event[user->event_head] = event;
173}
174
175void handle_event(struct pcmcia_bus_socket *s, event_t event)
176{
177 user_info_t *user;
178 for (user = s->user; user; user = user->next)
179 queue_event(user, event);
180 wake_up_interruptible(&s->queue);
181}
182
183
184/*======================================================================
185
186 bind_request() and bind_device() are merged by now. Register_client()
187 is called right at the end of bind_request(), during the driver's
188 ->attach() call. Individual descriptions:
189
190 bind_request() connects a socket to a particular client driver.
191 It looks up the specified device ID in the list of registered
192 drivers, binds it to the socket, and tries to create an instance
193 of the device. unbind_request() deletes a driver instance.
194
195 Bind_device() associates a device driver with a particular socket.
196 It is normally called by Driver Services after it has identified
197 a newly inserted card. An instance of that driver will then be
198 eligible to register as a client of this socket.
199
200 Register_client() uses the dev_info_t handle to match the
201 caller with a socket. The driver must have already been bound
202 to a socket with bind_device() -- in fact, bind_device()
203 allocates the client structure that will be used.
204
205======================================================================*/
206
207static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info)
208{
209 struct pcmcia_driver *p_drv;
210 struct pcmcia_device *p_dev;
211 int ret = 0;
212 unsigned long flags;
213
214 s = pcmcia_get_bus_socket(s);
215 if (!s)
216 return -EINVAL;
217
218 ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock,
219 (char *)bind_info->dev_info);
220
221 p_drv = get_pcmcia_driver(&bind_info->dev_info);
222 if (!p_drv) {
223 ret = -EINVAL;
224 goto err_put;
225 }
226
227 if (!try_module_get(p_drv->owner)) {
228 ret = -EINVAL;
229 goto err_put_driver;
230 }
231
232 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
233 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
234 if (p_dev->func == bind_info->function) {
235 if ((p_dev->dev.driver == &p_drv->drv)) {
236 if (p_dev->cardmgr) {
237 /* if there's already a device
238 * registered, and it was registered
239 * by userspace before, we need to
240 * return the "instance". */
241 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
242 bind_info->instance = p_dev->instance;
243 ret = -EBUSY;
244 goto err_put_module;
245 } else {
246 /* the correct driver managed to bind
247 * itself magically to the correct
248 * device. */
249 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
250 p_dev->cardmgr = p_drv;
251 ret = 0;
252 goto err_put_module;
253 }
254 } else if (!p_dev->dev.driver) {
255 /* there's already a device available where
256 * no device has been bound to yet. So we don't
257 * need to register a device! */
258 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
259 goto rescan;
260 }
261 }
262 }
263 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
264
265 p_dev = pcmcia_device_add(s, bind_info->function);
266 if (!p_dev) {
267 ret = -EIO;
268 goto err_put_module;
269 }
270
271rescan:
272 p_dev->cardmgr = p_drv;
273
274 /* if a driver is already running, we can abort */
275 if (p_dev->dev.driver)
276 goto err_put_module;
277
278 /*
279 * Prevent this racing with a card insertion.
280 */
281 down(&s->parent->skt_sem);
282 bus_rescan_devices(&pcmcia_bus_type);
283 up(&s->parent->skt_sem);
284
285 /* check whether the driver indeed matched. I don't care if this
286 * is racy or not, because it can only happen on cardmgr access
287 * paths...
288 */
289 if (!(p_dev->dev.driver == &p_drv->drv))
290 p_dev->cardmgr = NULL;
291
292 err_put_module:
293 module_put(p_drv->owner);
294 err_put_driver:
295 put_driver(&p_drv->drv);
296 err_put:
297 pcmcia_put_bus_socket(s);
298
299 return (ret);
300} /* bind_request */
301
302
303extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s);
304
305static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first)
306{
307 dev_node_t *node;
308 struct pcmcia_device *p_dev;
309 unsigned long flags;
310 int ret = 0;
311
312#ifdef CONFIG_CARDBUS
313 /*
314 * Some unbelievably ugly code to associate the PCI cardbus
315 * device and its driver with the PCMCIA "bind" information.
316 */
317 {
318 struct pci_bus *bus;
319
320 bus = pcmcia_lookup_bus(s->parent);
321 if (bus) {
322 struct list_head *list;
323 struct pci_dev *dev = NULL;
324
325 list = bus->devices.next;
326 while (list != &bus->devices) {
327 struct pci_dev *pdev = pci_dev_b(list);
328 list = list->next;
329
330 if (first) {
331 dev = pdev;
332 break;
333 }
334
335 /* Try to handle "next" here some way? */
336 }
337 if (dev && dev->driver) {
338 strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN);
339 bind_info->major = 0;
340 bind_info->minor = 0;
341 bind_info->next = NULL;
342 return 0;
343 }
344 }
345 }
346#endif
347
348 spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
349 list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
350 if (p_dev->func == bind_info->function) {
351 p_dev = pcmcia_get_dev(p_dev);
352 if (!p_dev)
353 continue;
354 goto found;
355 }
356 }
357 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
358 return -ENODEV;
359
360 found:
361 spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
362
363 if ((!p_dev->instance) ||
364 (p_dev->instance->state & DEV_CONFIG_PENDING)) {
365 ret = -EAGAIN;
366 goto err_put;
367 }
368
369 if (first)
370 node = p_dev->instance->dev;
371 else
372 for (node = p_dev->instance->dev; node; node = node->next)
373 if (node == bind_info->next)
374 break;
375 if (!node) {
376 ret = -ENODEV;
377 goto err_put;
378 }
379
380 strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN);
381 bind_info->major = node->major;
382 bind_info->minor = node->minor;
383 bind_info->next = node->next;
384
385 err_put:
386 pcmcia_put_dev(p_dev);
387 return (ret);
388} /* get_device_info */
389
390
391static int ds_open(struct inode *inode, struct file *file)
392{
393 socket_t i = iminor(inode);
394 struct pcmcia_bus_socket *s;
395 user_info_t *user;
396
397 ds_dbg(0, "ds_open(socket %d)\n", i);
398
399 s = get_socket_info_by_nr(i);
400 if (!s)
401 return -ENODEV;
402 s = pcmcia_get_bus_socket(s);
403 if (!s)
404 return -ENODEV;
405
406 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
407 if (s->state & DS_SOCKET_BUSY) {
408 pcmcia_put_bus_socket(s);
409 return -EBUSY;
410 }
411 else
412 s->state |= DS_SOCKET_BUSY;
413 }
414
415 user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
416 if (!user) {
417 pcmcia_put_bus_socket(s);
418 return -ENOMEM;
419 }
420 user->event_tail = user->event_head = 0;
421 user->next = s->user;
422 user->user_magic = USER_MAGIC;
423 user->socket = s;
424 s->user = user;
425 file->private_data = user;
426
427 if (s->state & DS_SOCKET_PRESENT)
428 queue_event(user, CS_EVENT_CARD_INSERTION);
429 return 0;
430} /* ds_open */
431
432/*====================================================================*/
433
434static int ds_release(struct inode *inode, struct file *file)
435{
436 struct pcmcia_bus_socket *s;
437 user_info_t *user, **link;
438
439 ds_dbg(0, "ds_release(socket %d)\n", iminor(inode));
440
441 user = file->private_data;
442 if (CHECK_USER(user))
443 goto out;
444
445 s = user->socket;
446
447 /* Unlink user data structure */
448 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
449 s->state &= ~DS_SOCKET_BUSY;
450 }
451 file->private_data = NULL;
452 for (link = &s->user; *link; link = &(*link)->next)
453 if (*link == user) break;
454 if (link == NULL)
455 goto out;
456 *link = user->next;
457 user->user_magic = 0;
458 kfree(user);
459 pcmcia_put_bus_socket(s);
460out:
461 return 0;
462} /* ds_release */
463
464/*====================================================================*/
465
466static ssize_t ds_read(struct file *file, char __user *buf,
467 size_t count, loff_t *ppos)
468{
469 struct pcmcia_bus_socket *s;
470 user_info_t *user;
471 int ret;
472
473 ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
474
475 if (count < 4)
476 return -EINVAL;
477
478 user = file->private_data;
479 if (CHECK_USER(user))
480 return -EIO;
481
482 s = user->socket;
483 if (s->state & DS_SOCKET_DEAD)
484 return -EIO;
485
486 ret = wait_event_interruptible(s->queue, !queue_empty(user));
487 if (ret == 0)
488 ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4;
489
490 return ret;
491} /* ds_read */
492
493/*====================================================================*/
494
495static ssize_t ds_write(struct file *file, const char __user *buf,
496 size_t count, loff_t *ppos)
497{
498 ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
499
500 if (count != 4)
501 return -EINVAL;
502 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
503 return -EBADF;
504
505 return -EIO;
506} /* ds_write */
507
508/*====================================================================*/
509
510/* No kernel lock - fine */
511static u_int ds_poll(struct file *file, poll_table *wait)
512{
513 struct pcmcia_bus_socket *s;
514 user_info_t *user;
515
516 ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
517
518 user = file->private_data;
519 if (CHECK_USER(user))
520 return POLLERR;
521 s = user->socket;
522 /*
523 * We don't check for a dead socket here since that
524 * will send cardmgr into an endless spin.
525 */
526 poll_wait(file, &s->queue, wait);
527 if (!queue_empty(user))
528 return POLLIN | POLLRDNORM;
529 return 0;
530} /* ds_poll */
531
532/*====================================================================*/
533
534extern int pcmcia_adjust_resource_info(adjust_t *adj);
535
536static int ds_ioctl(struct inode * inode, struct file * file,
537 u_int cmd, u_long arg)
538{
539 struct pcmcia_bus_socket *s;
540 void __user *uarg = (char __user *)arg;
541 u_int size;
542 int ret, err;
543 ds_ioctl_arg_t *buf;
544 user_info_t *user;
545
546 ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg);
547
548 user = file->private_data;
549 if (CHECK_USER(user))
550 return -EIO;
551
552 s = user->socket;
553 if (s->state & DS_SOCKET_DEAD)
554 return -EIO;
555
556 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
557 if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL;
558
559 /* Permission check */
560 if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN))
561 return -EPERM;
562
563 if (cmd & IOC_IN) {
564 if (!access_ok(VERIFY_READ, uarg, size)) {
565 ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT);
566 return -EFAULT;
567 }
568 }
569 if (cmd & IOC_OUT) {
570 if (!access_ok(VERIFY_WRITE, uarg, size)) {
571 ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT);
572 return -EFAULT;
573 }
574 }
575 buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL);
576 if (!buf)
577 return -ENOMEM;
578
579 err = ret = 0;
580
581 if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
582
583 switch (cmd) {
584 case DS_ADJUST_RESOURCE_INFO:
585 ret = pcmcia_adjust_resource_info(&buf->adjust);
586 break;
587 case DS_GET_CARD_SERVICES_INFO:
588 ret = pcmcia_get_card_services_info(&buf->servinfo);
589 break;
590 case DS_GET_CONFIGURATION_INFO:
591 if (buf->config.Function &&
592 (buf->config.Function >= s->parent->functions))
593 ret = CS_BAD_ARGS;
594 else
595 ret = pccard_get_configuration_info(s->parent,
596 buf->config.Function, &buf->config);
597 break;
598 case DS_GET_FIRST_TUPLE:
599 down(&s->parent->skt_sem);
600 pcmcia_validate_mem(s->parent);
601 up(&s->parent->skt_sem);
602 ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
603 break;
604 case DS_GET_NEXT_TUPLE:
605 ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple);
606 break;
607 case DS_GET_TUPLE_DATA:
608 buf->tuple.TupleData = buf->tuple_parse.data;
609 buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data);
610 ret = pccard_get_tuple_data(s->parent, &buf->tuple);
611 break;
612 case DS_PARSE_TUPLE:
613 buf->tuple.TupleData = buf->tuple_parse.data;
614 ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse);
615 break;
616 case DS_RESET_CARD:
617 ret = pccard_reset_card(s->parent);
618 break;
619 case DS_GET_STATUS:
620 if (buf->status.Function &&
621 (buf->status.Function >= s->parent->functions))
622 ret = CS_BAD_ARGS;
623 else
624 ret = pccard_get_status(s->parent, buf->status.Function, &buf->status);
625 break;
626 case DS_VALIDATE_CIS:
627 down(&s->parent->skt_sem);
628 pcmcia_validate_mem(s->parent);
629 up(&s->parent->skt_sem);
630 ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo);
631 break;
632 case DS_SUSPEND_CARD:
633 ret = pcmcia_suspend_card(s->parent);
634 break;
635 case DS_RESUME_CARD:
636 ret = pcmcia_resume_card(s->parent);
637 break;
638 case DS_EJECT_CARD:
639 err = pcmcia_eject_card(s->parent);
640 break;
641 case DS_INSERT_CARD:
642 err = pcmcia_insert_card(s->parent);
643 break;
644 case DS_ACCESS_CONFIGURATION_REGISTER:
645 if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) {
646 err = -EPERM;
647 goto free_out;
648 }
649 if (buf->conf_reg.Function &&
650 (buf->conf_reg.Function >= s->parent->functions))
651 ret = CS_BAD_ARGS;
652 else
653 ret = pccard_access_configuration_register(s->parent,
654 buf->conf_reg.Function, &buf->conf_reg);
655 break;
656 case DS_GET_FIRST_REGION:
657 case DS_GET_NEXT_REGION:
658 case DS_BIND_MTD:
659 if (!capable(CAP_SYS_ADMIN)) {
660 err = -EPERM;
661 goto free_out;
662 } else {
663 static int printed = 0;
664 if (!printed) {
665 printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n");
666 printk(KERN_WARNING "MTD handling any more.\n");
667 printed++;
668 }
669 }
670 err = -EINVAL;
671 goto free_out;
672 break;
673 case DS_GET_FIRST_WINDOW:
674 ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0,
675 &buf->win_info.window);
676 break;
677 case DS_GET_NEXT_WINDOW:
678 ret = pcmcia_get_window(s->parent, &buf->win_info.handle,
679 buf->win_info.handle->index + 1, &buf->win_info.window);
680 break;
681 case DS_GET_MEM_PAGE:
682 ret = pcmcia_get_mem_page(buf->win_info.handle,
683 &buf->win_info.map);
684 break;
685 case DS_REPLACE_CIS:
686 ret = pcmcia_replace_cis(s->parent, &buf->cisdump);
687 break;
688 case DS_BIND_REQUEST:
689 if (!capable(CAP_SYS_ADMIN)) {
690 err = -EPERM;
691 goto free_out;
692 }
693 err = bind_request(s, &buf->bind_info);
694 break;
695 case DS_GET_DEVICE_INFO:
696 err = get_device_info(s, &buf->bind_info, 1);
697 break;
698 case DS_GET_NEXT_DEVICE:
699 err = get_device_info(s, &buf->bind_info, 0);
700 break;
701 case DS_UNBIND_REQUEST:
702 err = 0;
703 break;
704 default:
705 err = -EINVAL;
706 }
707
708 if ((err == 0) && (ret != CS_SUCCESS)) {
709 ds_dbg(2, "ds_ioctl: ret = %d\n", ret);
710 switch (ret) {
711 case CS_BAD_SOCKET: case CS_NO_CARD:
712 err = -ENODEV; break;
713 case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ:
714 case CS_BAD_TUPLE:
715 err = -EINVAL; break;
716 case CS_IN_USE:
717 err = -EBUSY; break;
718 case CS_OUT_OF_RESOURCE:
719 err = -ENOSPC; break;
720 case CS_NO_MORE_ITEMS:
721 err = -ENODATA; break;
722 case CS_UNSUPPORTED_FUNCTION:
723 err = -ENOSYS; break;
724 default:
725 err = -EIO; break;
726 }
727 }
728
729 if (cmd & IOC_OUT) {
730 if (__copy_to_user(uarg, (char *)buf, size))
731 err = -EFAULT;
732 }
733
734free_out:
735 kfree(buf);
736 return err;
737} /* ds_ioctl */
738
739/*====================================================================*/
740
741static struct file_operations ds_fops = {
742 .owner = THIS_MODULE,
743 .open = ds_open,
744 .release = ds_release,
745 .ioctl = ds_ioctl,
746 .read = ds_read,
747 .write = ds_write,
748 .poll = ds_poll,
749};
750
751void __init pcmcia_setup_ioctl(void) {
752 int i;
753
754 /* Set up character device for user mode clients */
755 i = register_chrdev(0, "pcmcia", &ds_fops);
756 if (i == -EBUSY)
757 printk(KERN_NOTICE "unable to find a free device # for "
758 "Driver Services\n");
759 else
760 major_dev = i;
761
762#ifdef CONFIG_PROC_FS
763 proc_pccard = proc_mkdir("pccard", proc_bus);
764 if (proc_pccard)
765 create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
766#endif
767}
768
769
770void __exit pcmcia_cleanup_ioctl(void) {
771#ifdef CONFIG_PROC_FS
772 if (proc_pccard) {
773 remove_proc_entry("drivers", proc_pccard);
774 remove_proc_entry("pccard", proc_bus);
775 }
776#endif
777 if (major_dev != -1)
778 unregister_chrdev(major_dev, "pcmcia");
779}