Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ds.c -- 16-bit PCMCIA core support |
| 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 | #include <linux/config.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/major.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/smp_lock.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/ioctl.h> |
| 31 | #include <linux/proc_fs.h> |
| 32 | #include <linux/poll.h> |
| 33 | #include <linux/pci.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/kref.h> |
| 37 | #include <linux/workqueue.h> |
Dominik Brodowski | 840c2ac | 2005-06-27 16:28:04 -0700 | [diff] [blame] | 38 | #include <linux/crc32.h> |
Dominik Brodowski | daa9517 | 2005-06-27 16:28:14 -0700 | [diff] [blame] | 39 | #include <linux/firmware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <asm/atomic.h> |
| 42 | |
| 43 | #define IN_CARD_SERVICES |
| 44 | #include <pcmcia/version.h> |
| 45 | #include <pcmcia/cs_types.h> |
| 46 | #include <pcmcia/cs.h> |
| 47 | #include <pcmcia/bulkmem.h> |
| 48 | #include <pcmcia/cistpl.h> |
| 49 | #include <pcmcia/ds.h> |
| 50 | #include <pcmcia/ss.h> |
| 51 | |
| 52 | #include "cs_internal.h" |
| 53 | |
| 54 | /*====================================================================*/ |
| 55 | |
| 56 | /* Module parameters */ |
| 57 | |
| 58 | MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>"); |
| 59 | MODULE_DESCRIPTION("PCMCIA Driver Services"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
| 62 | #ifdef DEBUG |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame^] | 63 | static int ds_pc_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | module_param_named(pc_debug, ds_pc_debug, int, 0644); |
| 66 | |
| 67 | #define ds_dbg(lvl, fmt, arg...) do { \ |
| 68 | if (ds_pc_debug > (lvl)) \ |
| 69 | printk(KERN_DEBUG "ds: " fmt , ## arg); \ |
| 70 | } while (0) |
| 71 | #else |
| 72 | #define ds_dbg(lvl, fmt, arg...) do { } while (0) |
| 73 | #endif |
| 74 | |
| 75 | /*====================================================================*/ |
| 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)) |
| 82 | typedef 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 */ |
| 91 | struct 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 */ |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 105 | |
| 106 | u8 device_add_pending; |
| 107 | struct work_struct device_add; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | static spinlock_t pcmcia_dev_list_lock; |
| 110 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame^] | 111 | static struct bus_type pcmcia_bus_type; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 120 | static int major_dev = -1; |
| 121 | |
| 122 | static int unbind_request(struct pcmcia_bus_socket *s); |
| 123 | |
| 124 | /*====================================================================*/ |
| 125 | |
| 126 | /* code which was in cs.c before */ |
| 127 | |
| 128 | /* String tables for error messages */ |
| 129 | |
| 130 | typedef struct lookup_t { |
| 131 | int key; |
| 132 | char *msg; |
| 133 | } lookup_t; |
| 134 | |
| 135 | static const lookup_t error_table[] = { |
| 136 | { CS_SUCCESS, "Operation succeeded" }, |
| 137 | { CS_BAD_ADAPTER, "Bad adapter" }, |
| 138 | { CS_BAD_ATTRIBUTE, "Bad attribute", }, |
| 139 | { CS_BAD_BASE, "Bad base address" }, |
| 140 | { CS_BAD_EDC, "Bad EDC" }, |
| 141 | { CS_BAD_IRQ, "Bad IRQ" }, |
| 142 | { CS_BAD_OFFSET, "Bad offset" }, |
| 143 | { CS_BAD_PAGE, "Bad page number" }, |
| 144 | { CS_READ_FAILURE, "Read failure" }, |
| 145 | { CS_BAD_SIZE, "Bad size" }, |
| 146 | { CS_BAD_SOCKET, "Bad socket" }, |
| 147 | { CS_BAD_TYPE, "Bad type" }, |
| 148 | { CS_BAD_VCC, "Bad Vcc" }, |
| 149 | { CS_BAD_VPP, "Bad Vpp" }, |
| 150 | { CS_BAD_WINDOW, "Bad window" }, |
| 151 | { CS_WRITE_FAILURE, "Write failure" }, |
| 152 | { CS_NO_CARD, "No card present" }, |
| 153 | { CS_UNSUPPORTED_FUNCTION, "Usupported function" }, |
| 154 | { CS_UNSUPPORTED_MODE, "Unsupported mode" }, |
| 155 | { CS_BAD_SPEED, "Bad speed" }, |
| 156 | { CS_BUSY, "Resource busy" }, |
| 157 | { CS_GENERAL_FAILURE, "General failure" }, |
| 158 | { CS_WRITE_PROTECTED, "Write protected" }, |
| 159 | { CS_BAD_ARG_LENGTH, "Bad argument length" }, |
| 160 | { CS_BAD_ARGS, "Bad arguments" }, |
| 161 | { CS_CONFIGURATION_LOCKED, "Configuration locked" }, |
| 162 | { CS_IN_USE, "Resource in use" }, |
| 163 | { CS_NO_MORE_ITEMS, "No more items" }, |
| 164 | { CS_OUT_OF_RESOURCE, "Out of resource" }, |
| 165 | { CS_BAD_HANDLE, "Bad handle" }, |
| 166 | { CS_BAD_TUPLE, "Bad CIS tuple" } |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | static const lookup_t service_table[] = { |
| 171 | { AccessConfigurationRegister, "AccessConfigurationRegister" }, |
| 172 | { AddSocketServices, "AddSocketServices" }, |
| 173 | { AdjustResourceInfo, "AdjustResourceInfo" }, |
| 174 | { CheckEraseQueue, "CheckEraseQueue" }, |
| 175 | { CloseMemory, "CloseMemory" }, |
| 176 | { DeregisterClient, "DeregisterClient" }, |
| 177 | { DeregisterEraseQueue, "DeregisterEraseQueue" }, |
| 178 | { GetCardServicesInfo, "GetCardServicesInfo" }, |
| 179 | { GetClientInfo, "GetClientInfo" }, |
| 180 | { GetConfigurationInfo, "GetConfigurationInfo" }, |
| 181 | { GetEventMask, "GetEventMask" }, |
| 182 | { GetFirstClient, "GetFirstClient" }, |
| 183 | { GetFirstRegion, "GetFirstRegion" }, |
| 184 | { GetFirstTuple, "GetFirstTuple" }, |
| 185 | { GetNextClient, "GetNextClient" }, |
| 186 | { GetNextRegion, "GetNextRegion" }, |
| 187 | { GetNextTuple, "GetNextTuple" }, |
| 188 | { GetStatus, "GetStatus" }, |
| 189 | { GetTupleData, "GetTupleData" }, |
| 190 | { MapMemPage, "MapMemPage" }, |
| 191 | { ModifyConfiguration, "ModifyConfiguration" }, |
| 192 | { ModifyWindow, "ModifyWindow" }, |
| 193 | { OpenMemory, "OpenMemory" }, |
| 194 | { ParseTuple, "ParseTuple" }, |
| 195 | { ReadMemory, "ReadMemory" }, |
| 196 | { RegisterClient, "RegisterClient" }, |
| 197 | { RegisterEraseQueue, "RegisterEraseQueue" }, |
| 198 | { RegisterMTD, "RegisterMTD" }, |
| 199 | { ReleaseConfiguration, "ReleaseConfiguration" }, |
| 200 | { ReleaseIO, "ReleaseIO" }, |
| 201 | { ReleaseIRQ, "ReleaseIRQ" }, |
| 202 | { ReleaseWindow, "ReleaseWindow" }, |
| 203 | { RequestConfiguration, "RequestConfiguration" }, |
| 204 | { RequestIO, "RequestIO" }, |
| 205 | { RequestIRQ, "RequestIRQ" }, |
| 206 | { RequestSocketMask, "RequestSocketMask" }, |
| 207 | { RequestWindow, "RequestWindow" }, |
| 208 | { ResetCard, "ResetCard" }, |
| 209 | { SetEventMask, "SetEventMask" }, |
| 210 | { ValidateCIS, "ValidateCIS" }, |
| 211 | { WriteMemory, "WriteMemory" }, |
| 212 | { BindDevice, "BindDevice" }, |
| 213 | { BindMTD, "BindMTD" }, |
| 214 | { ReportError, "ReportError" }, |
| 215 | { SuspendCard, "SuspendCard" }, |
| 216 | { ResumeCard, "ResumeCard" }, |
| 217 | { EjectCard, "EjectCard" }, |
| 218 | { InsertCard, "InsertCard" }, |
| 219 | { ReplaceCIS, "ReplaceCIS" } |
| 220 | }; |
| 221 | |
| 222 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame^] | 223 | static int pcmcia_report_error(client_handle_t handle, error_info_t *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | int i; |
| 226 | char *serv; |
| 227 | |
| 228 | if (CHECK_HANDLE(handle)) |
| 229 | printk(KERN_NOTICE); |
| 230 | else { |
| 231 | struct pcmcia_device *p_dev = handle_to_pdev(handle); |
| 232 | printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id); |
| 233 | } |
| 234 | |
| 235 | for (i = 0; i < ARRAY_SIZE(service_table); i++) |
| 236 | if (service_table[i].key == err->func) |
| 237 | break; |
| 238 | if (i < ARRAY_SIZE(service_table)) |
| 239 | serv = service_table[i].msg; |
| 240 | else |
| 241 | serv = "Unknown service number"; |
| 242 | |
| 243 | for (i = 0; i < ARRAY_SIZE(error_table); i++) |
| 244 | if (error_table[i].key == err->retcode) |
| 245 | break; |
| 246 | if (i < ARRAY_SIZE(error_table)) |
| 247 | printk("%s: %s\n", serv, error_table[i].msg); |
| 248 | else |
| 249 | printk("%s: Unknown error code %#x\n", serv, err->retcode); |
| 250 | |
| 251 | return CS_SUCCESS; |
| 252 | } /* report_error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | /* end of code which was in cs.c before */ |
| 255 | |
| 256 | /*======================================================================*/ |
| 257 | |
| 258 | void cs_error(client_handle_t handle, int func, int ret) |
| 259 | { |
| 260 | error_info_t err = { func, ret }; |
| 261 | pcmcia_report_error(handle, &err); |
| 262 | } |
| 263 | EXPORT_SYMBOL(cs_error); |
| 264 | |
Dominik Brodowski | 23a83bf | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_PCMCIA_DEBUG |
| 266 | |
| 267 | |
| 268 | static void pcmcia_check_driver(struct pcmcia_driver *p_drv) |
| 269 | { |
| 270 | struct pcmcia_device_id *did = p_drv->id_table; |
| 271 | unsigned int i; |
| 272 | u32 hash; |
| 273 | |
| 274 | while (did && did->match_flags) { |
| 275 | for (i=0; i<4; i++) { |
| 276 | if (!did->prod_id[i]) |
| 277 | continue; |
| 278 | |
| 279 | hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i])); |
| 280 | if (hash == did->prod_id_hash[i]) |
| 281 | continue; |
| 282 | |
| 283 | printk(KERN_DEBUG "pcmcia: %s: invalid hash for " |
| 284 | "product string \"%s\": is 0x%x, should " |
| 285 | "be 0x%x\n", p_drv->drv.name, did->prod_id[i], |
| 286 | did->prod_id_hash[i], hash); |
| 287 | } |
| 288 | did++; |
| 289 | } |
| 290 | |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | #else |
| 295 | static inline void pcmcia_check_driver(struct pcmcia_driver *p_drv) { |
| 296 | return; |
| 297 | } |
| 298 | #endif |
| 299 | |
Dominik Brodowski | daa9517 | 2005-06-27 16:28:14 -0700 | [diff] [blame] | 300 | |
| 301 | #ifdef CONFIG_PCMCIA_LOAD_CIS |
| 302 | |
| 303 | /** |
| 304 | * pcmcia_load_firmware - load CIS from userspace if device-provided is broken |
| 305 | * @dev - the pcmcia device which needs a CIS override |
| 306 | * @filename - requested filename in /lib/firmware/cis/ |
| 307 | * |
| 308 | * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if |
| 309 | * the one provided by the card is broken. The firmware files reside in |
| 310 | * /lib/firmware/cis/ in userspace. |
| 311 | */ |
| 312 | static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) |
| 313 | { |
| 314 | struct pcmcia_socket *s = dev->socket; |
| 315 | const struct firmware *fw; |
| 316 | char path[20]; |
| 317 | int ret=-ENOMEM; |
| 318 | cisdump_t *cis; |
| 319 | |
| 320 | if (!filename) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | ds_dbg(1, "trying to load firmware %s\n", filename); |
| 324 | |
| 325 | if (strlen(filename) > 14) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | snprintf(path, 20, "%s", filename); |
| 329 | |
| 330 | if (request_firmware(&fw, path, &dev->dev) == 0) { |
| 331 | if (fw->size >= CISTPL_MAX_CIS_SIZE) |
| 332 | goto release; |
| 333 | |
| 334 | cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL); |
| 335 | if (!cis) |
| 336 | goto release; |
| 337 | |
| 338 | memset(cis, 0, sizeof(cisdump_t)); |
| 339 | |
| 340 | cis->Length = fw->size + 1; |
| 341 | memcpy(cis->Data, fw->data, fw->size); |
| 342 | |
| 343 | if (!pcmcia_replace_cis(s, cis)) |
| 344 | ret = 0; |
| 345 | } |
| 346 | release: |
| 347 | release_firmware(fw); |
| 348 | |
| 349 | return (ret); |
| 350 | } |
| 351 | |
| 352 | #else /* !CONFIG_PCMCIA_LOAD_CIS */ |
| 353 | |
| 354 | static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) |
| 355 | { |
| 356 | return -ENODEV; |
| 357 | } |
| 358 | |
| 359 | #endif |
| 360 | |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | /*======================================================================*/ |
| 363 | |
| 364 | static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info); |
| 365 | static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr); |
| 366 | |
| 367 | static void pcmcia_release_bus_socket(struct kref *refcount) |
| 368 | { |
| 369 | struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount); |
| 370 | pcmcia_put_socket(s->parent); |
| 371 | kfree(s); |
| 372 | } |
| 373 | |
| 374 | static void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s) |
| 375 | { |
| 376 | kref_put(&s->refcount, pcmcia_release_bus_socket); |
| 377 | } |
| 378 | |
| 379 | static struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s) |
| 380 | { |
| 381 | kref_get(&s->refcount); |
| 382 | return (s); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * pcmcia_register_driver - register a PCMCIA driver with the bus core |
| 387 | * |
| 388 | * Registers a PCMCIA driver with the PCMCIA bus core. |
| 389 | */ |
| 390 | static int pcmcia_device_probe(struct device *dev); |
| 391 | static int pcmcia_device_remove(struct device * dev); |
| 392 | |
| 393 | int pcmcia_register_driver(struct pcmcia_driver *driver) |
| 394 | { |
| 395 | if (!driver) |
| 396 | return -EINVAL; |
| 397 | |
Dominik Brodowski | 23a83bf | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 398 | pcmcia_check_driver(driver); |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* initialize common fields */ |
| 401 | driver->drv.bus = &pcmcia_bus_type; |
| 402 | driver->drv.owner = driver->owner; |
| 403 | driver->drv.probe = pcmcia_device_probe; |
| 404 | driver->drv.remove = pcmcia_device_remove; |
| 405 | |
| 406 | return driver_register(&driver->drv); |
| 407 | } |
| 408 | EXPORT_SYMBOL(pcmcia_register_driver); |
| 409 | |
| 410 | /** |
| 411 | * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core |
| 412 | */ |
| 413 | void pcmcia_unregister_driver(struct pcmcia_driver *driver) |
| 414 | { |
| 415 | driver_unregister(&driver->drv); |
| 416 | } |
| 417 | EXPORT_SYMBOL(pcmcia_unregister_driver); |
| 418 | |
| 419 | #ifdef CONFIG_PROC_FS |
| 420 | static struct proc_dir_entry *proc_pccard = NULL; |
| 421 | |
| 422 | static int proc_read_drivers_callback(struct device_driver *driver, void *d) |
| 423 | { |
| 424 | char **p = d; |
| 425 | struct pcmcia_driver *p_drv = container_of(driver, |
| 426 | struct pcmcia_driver, drv); |
| 427 | |
| 428 | *p += sprintf(*p, "%-24.24s 1 %d\n", p_drv->drv.name, |
| 429 | #ifdef CONFIG_MODULE_UNLOAD |
| 430 | (p_drv->owner) ? module_refcount(p_drv->owner) : 1 |
| 431 | #else |
| 432 | 1 |
| 433 | #endif |
| 434 | ); |
| 435 | d = (void *) p; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int proc_read_drivers(char *buf, char **start, off_t pos, |
| 441 | int count, int *eof, void *data) |
| 442 | { |
| 443 | char *p = buf; |
| 444 | |
| 445 | bus_for_each_drv(&pcmcia_bus_type, NULL, |
| 446 | (void *) &p, proc_read_drivers_callback); |
| 447 | |
| 448 | return (p - buf); |
| 449 | } |
| 450 | #endif |
| 451 | |
| 452 | /* pcmcia_device handling */ |
| 453 | |
| 454 | static struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev) |
| 455 | { |
| 456 | struct device *tmp_dev; |
| 457 | tmp_dev = get_device(&p_dev->dev); |
| 458 | if (!tmp_dev) |
| 459 | return NULL; |
| 460 | return to_pcmcia_dev(tmp_dev); |
| 461 | } |
| 462 | |
| 463 | static void pcmcia_put_dev(struct pcmcia_device *p_dev) |
| 464 | { |
| 465 | if (p_dev) |
| 466 | put_device(&p_dev->dev); |
| 467 | } |
| 468 | |
| 469 | static void pcmcia_release_dev(struct device *dev) |
| 470 | { |
| 471 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 472 | ds_dbg(1, "releasing dev %p\n", p_dev); |
| 473 | pcmcia_put_bus_socket(p_dev->socket->pcmcia); |
| 474 | kfree(p_dev); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | static int pcmcia_device_probe(struct device * dev) |
| 479 | { |
| 480 | struct pcmcia_device *p_dev; |
| 481 | struct pcmcia_driver *p_drv; |
| 482 | int ret = 0; |
| 483 | |
| 484 | dev = get_device(dev); |
| 485 | if (!dev) |
| 486 | return -ENODEV; |
| 487 | |
| 488 | p_dev = to_pcmcia_dev(dev); |
| 489 | p_drv = to_pcmcia_drv(dev->driver); |
| 490 | |
| 491 | if (!try_module_get(p_drv->owner)) { |
| 492 | ret = -EINVAL; |
| 493 | goto put_dev; |
| 494 | } |
| 495 | |
| 496 | if (p_drv->attach) { |
| 497 | p_dev->instance = p_drv->attach(); |
| 498 | if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) { |
| 499 | printk(KERN_NOTICE "ds: unable to create instance " |
| 500 | "of '%s'!\n", p_drv->drv.name); |
| 501 | ret = -EINVAL; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (ret) |
| 506 | module_put(p_drv->owner); |
| 507 | put_dev: |
| 508 | if ((ret) || !(p_drv->attach)) |
| 509 | put_device(dev); |
| 510 | return (ret); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | static int pcmcia_device_remove(struct device * dev) |
| 515 | { |
| 516 | struct pcmcia_device *p_dev; |
| 517 | struct pcmcia_driver *p_drv; |
| 518 | |
| 519 | /* detach the "instance" */ |
| 520 | p_dev = to_pcmcia_dev(dev); |
| 521 | p_drv = to_pcmcia_drv(dev->driver); |
| 522 | |
| 523 | if (p_drv) { |
| 524 | if ((p_drv->detach) && (p_dev->instance)) { |
| 525 | p_drv->detach(p_dev->instance); |
| 526 | /* from pcmcia_probe_device */ |
| 527 | put_device(&p_dev->dev); |
| 528 | } |
| 529 | module_put(p_drv->owner); |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | /* |
| 538 | * pcmcia_device_query -- determine information about a pcmcia device |
| 539 | */ |
| 540 | static int pcmcia_device_query(struct pcmcia_device *p_dev) |
| 541 | { |
| 542 | cistpl_manfid_t manf_id; |
| 543 | cistpl_funcid_t func_id; |
| 544 | cistpl_vers_1_t vers1; |
| 545 | unsigned int i; |
| 546 | |
| 547 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 548 | CISTPL_MANFID, &manf_id)) { |
| 549 | p_dev->manf_id = manf_id.manf; |
| 550 | p_dev->card_id = manf_id.card; |
| 551 | p_dev->has_manf_id = 1; |
| 552 | p_dev->has_card_id = 1; |
| 553 | } |
| 554 | |
| 555 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 556 | CISTPL_FUNCID, &func_id)) { |
| 557 | p_dev->func_id = func_id.func; |
| 558 | p_dev->has_func_id = 1; |
| 559 | } else { |
| 560 | /* rule of thumb: cards with no FUNCID, but with |
| 561 | * common memory device geometry information, are |
| 562 | * probably memory cards (from pcmcia-cs) */ |
| 563 | cistpl_device_geo_t devgeo; |
| 564 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 565 | CISTPL_DEVICE_GEO, &devgeo)) { |
| 566 | ds_dbg(0, "mem device geometry probably means " |
| 567 | "FUNCID_MEMORY\n"); |
| 568 | p_dev->func_id = CISTPL_FUNCID_MEMORY; |
| 569 | p_dev->has_func_id = 1; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1, |
| 574 | &vers1)) { |
| 575 | for (i=0; i < vers1.ns; i++) { |
| 576 | char *tmp; |
| 577 | unsigned int length; |
| 578 | |
| 579 | tmp = vers1.str + vers1.ofs[i]; |
| 580 | |
| 581 | length = strlen(tmp) + 1; |
| 582 | if ((length < 3) || (length > 255)) |
| 583 | continue; |
| 584 | |
| 585 | p_dev->prod_id[i] = kmalloc(sizeof(char) * length, |
| 586 | GFP_KERNEL); |
| 587 | if (!p_dev->prod_id[i]) |
| 588 | continue; |
| 589 | |
| 590 | p_dev->prod_id[i] = strncpy(p_dev->prod_id[i], |
| 591 | tmp, length); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | /* device_add_lock is needed to avoid double registration by cardmgr and kernel. |
| 600 | * Serializes pcmcia_device_add; will most likely be removed in future. |
| 601 | * |
| 602 | * While it has the caveat that adding new PCMCIA devices inside(!) device_register() |
| 603 | * won't work, this doesn't matter much at the moment: the driver core doesn't |
| 604 | * support it either. |
| 605 | */ |
| 606 | static DECLARE_MUTEX(device_add_lock); |
| 607 | |
| 608 | static struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function) |
| 609 | { |
| 610 | struct pcmcia_device *p_dev; |
| 611 | unsigned long flags; |
| 612 | |
| 613 | s = pcmcia_get_bus_socket(s); |
| 614 | if (!s) |
| 615 | return NULL; |
| 616 | |
| 617 | down(&device_add_lock); |
| 618 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 619 | /* max of 2 devices per card */ |
| 620 | if (s->device_count == 2) |
| 621 | goto err_put; |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL); |
| 624 | if (!p_dev) |
| 625 | goto err_put; |
| 626 | memset(p_dev, 0, sizeof(struct pcmcia_device)); |
| 627 | |
| 628 | p_dev->socket = s->parent; |
| 629 | p_dev->device_no = (s->device_count++); |
| 630 | p_dev->func = function; |
| 631 | |
| 632 | p_dev->dev.bus = &pcmcia_bus_type; |
| 633 | p_dev->dev.parent = s->parent->dev.dev; |
| 634 | p_dev->dev.release = pcmcia_release_dev; |
| 635 | sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no); |
| 636 | |
| 637 | /* compat */ |
| 638 | p_dev->client.client_magic = CLIENT_MAGIC; |
| 639 | p_dev->client.Socket = s->parent; |
| 640 | p_dev->client.Function = function; |
| 641 | p_dev->client.state = CLIENT_UNBOUND; |
| 642 | |
| 643 | /* Add to the list in pcmcia_bus_socket */ |
| 644 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 645 | list_add_tail(&p_dev->socket_device_list, &s->devices_list); |
| 646 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 647 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 648 | pcmcia_device_query(p_dev); |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (device_register(&p_dev->dev)) { |
| 651 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 652 | list_del(&p_dev->socket_device_list); |
| 653 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 654 | |
| 655 | goto err_free; |
| 656 | } |
| 657 | |
| 658 | up(&device_add_lock); |
| 659 | |
| 660 | return p_dev; |
| 661 | |
| 662 | err_free: |
| 663 | kfree(p_dev); |
| 664 | s->device_count--; |
| 665 | err_put: |
| 666 | up(&device_add_lock); |
| 667 | pcmcia_put_bus_socket(s); |
| 668 | |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | static int pcmcia_card_add(struct pcmcia_socket *s) |
| 674 | { |
| 675 | cisinfo_t cisinfo; |
| 676 | cistpl_longlink_mfc_t mfc; |
| 677 | unsigned int no_funcs, i; |
| 678 | int ret = 0; |
| 679 | |
| 680 | if (!(s->resource_setup_done)) |
| 681 | return -EAGAIN; /* try again, but later... */ |
| 682 | |
| 683 | pcmcia_validate_mem(s); |
| 684 | ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo); |
| 685 | if (ret || !cisinfo.Chains) { |
| 686 | ds_dbg(0, "invalid CIS or invalid resources\n"); |
| 687 | return -ENODEV; |
| 688 | } |
| 689 | |
| 690 | if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc)) |
| 691 | no_funcs = mfc.nfn; |
| 692 | else |
| 693 | no_funcs = 1; |
| 694 | |
| 695 | /* this doesn't handle multifunction devices on one pcmcia function |
| 696 | * yet. */ |
| 697 | for (i=0; i < no_funcs; i++) |
| 698 | pcmcia_device_add(s->pcmcia, i); |
| 699 | |
| 700 | return (ret); |
| 701 | } |
| 702 | |
| 703 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 704 | static void pcmcia_delayed_add_pseudo_device(void *data) |
| 705 | { |
| 706 | struct pcmcia_bus_socket *s = data; |
| 707 | pcmcia_device_add(s, 0); |
| 708 | s->device_add_pending = 0; |
| 709 | } |
| 710 | |
| 711 | static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s) |
| 712 | { |
| 713 | if (!s->device_add_pending) { |
| 714 | schedule_work(&s->device_add); |
| 715 | s->device_add_pending = 1; |
| 716 | } |
| 717 | return; |
| 718 | } |
| 719 | |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 720 | static void pcmcia_bus_rescan(void) |
| 721 | { |
| 722 | /* must be called with skt_sem held */ |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 723 | bus_rescan_devices(&pcmcia_bus_type); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 724 | } |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 725 | |
| 726 | static inline int pcmcia_devmatch(struct pcmcia_device *dev, |
| 727 | struct pcmcia_device_id *did) |
| 728 | { |
| 729 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) { |
| 730 | if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id)) |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) { |
| 735 | if ((!dev->has_card_id) || (dev->card_id != did->card_id)) |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) { |
| 740 | if (dev->func != did->function) |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) { |
| 745 | if (!dev->prod_id[0]) |
| 746 | return 0; |
| 747 | if (strcmp(did->prod_id[0], dev->prod_id[0])) |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) { |
| 752 | if (!dev->prod_id[1]) |
| 753 | return 0; |
| 754 | if (strcmp(did->prod_id[1], dev->prod_id[1])) |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) { |
| 759 | if (!dev->prod_id[2]) |
| 760 | return 0; |
| 761 | if (strcmp(did->prod_id[2], dev->prod_id[2])) |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) { |
| 766 | if (!dev->prod_id[3]) |
| 767 | return 0; |
| 768 | if (strcmp(did->prod_id[3], dev->prod_id[3])) |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) { |
| 773 | /* handle pseudo multifunction devices: |
| 774 | * there are at most two pseudo multifunction devices. |
| 775 | * if we're matching against the first, schedule a |
| 776 | * call which will then check whether there are two |
| 777 | * pseudo devices, and if not, add the second one. |
| 778 | */ |
| 779 | if (dev->device_no == 0) |
| 780 | pcmcia_add_pseudo_device(dev->socket->pcmcia); |
| 781 | |
| 782 | if (dev->device_no != did->device_no) |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) { |
| 787 | if ((!dev->has_func_id) || (dev->func_id != did->func_id)) |
| 788 | return 0; |
| 789 | |
| 790 | /* if this is a pseudo-multi-function device, |
| 791 | * we need explicit matches */ |
| 792 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) |
| 793 | return 0; |
| 794 | if (dev->device_no) |
| 795 | return 0; |
| 796 | |
| 797 | /* also, FUNC_ID matching needs to be activated by userspace |
| 798 | * after it has re-checked that there is no possible module |
| 799 | * with a prod_id/manf_id/card_id match. |
| 800 | */ |
| 801 | if (!dev->allow_func_id_match) |
| 802 | return 0; |
| 803 | } |
| 804 | |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 805 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) { |
Dominik Brodowski | daa9517 | 2005-06-27 16:28:14 -0700 | [diff] [blame] | 806 | if (!dev->socket->fake_cis) |
| 807 | pcmcia_load_firmware(dev, did->cisfile); |
| 808 | |
| 809 | if (!dev->socket->fake_cis) |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 810 | return 0; |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Dominik Brodowski | f602ff7 | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 813 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) { |
| 814 | int i; |
| 815 | for (i=0; i<4; i++) |
| 816 | if (dev->prod_id[i]) |
| 817 | return 0; |
| 818 | if (dev->has_manf_id || dev->has_card_id || dev->has_func_id) |
| 819 | return 0; |
| 820 | } |
| 821 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 822 | dev->dev.driver_data = (void *) did; |
| 823 | |
| 824 | return 1; |
| 825 | } |
| 826 | |
| 827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) { |
| 829 | struct pcmcia_device * p_dev = to_pcmcia_dev(dev); |
| 830 | struct pcmcia_driver * p_drv = to_pcmcia_drv(drv); |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 831 | struct pcmcia_device_id *did = p_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | /* matching by cardmgr */ |
| 834 | if (p_dev->cardmgr == p_drv) |
| 835 | return 1; |
| 836 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 837 | while (did && did->match_flags) { |
| 838 | if (pcmcia_devmatch(p_dev, did)) |
| 839 | return 1; |
| 840 | did++; |
| 841 | } |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Dominik Brodowski | 840c2ac | 2005-06-27 16:28:04 -0700 | [diff] [blame] | 846 | #ifdef CONFIG_HOTPLUG |
| 847 | |
| 848 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, |
| 849 | char *buffer, int buffer_size) |
| 850 | { |
| 851 | struct pcmcia_device *p_dev; |
| 852 | int i, length = 0; |
| 853 | u32 hash[4] = { 0, 0, 0, 0}; |
| 854 | |
| 855 | if (!dev) |
| 856 | return -ENODEV; |
| 857 | |
| 858 | p_dev = to_pcmcia_dev(dev); |
| 859 | |
| 860 | /* calculate hashes */ |
| 861 | for (i=0; i<4; i++) { |
| 862 | if (!p_dev->prod_id[i]) |
| 863 | continue; |
| 864 | hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i])); |
| 865 | } |
| 866 | |
| 867 | i = 0; |
| 868 | |
| 869 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 870 | buffer, buffer_size, &length, |
| 871 | "SOCKET_NO=%u", |
| 872 | p_dev->socket->sock)) |
| 873 | return -ENOMEM; |
| 874 | |
| 875 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 876 | buffer, buffer_size, &length, |
| 877 | "DEVICE_NO=%02X", |
| 878 | p_dev->device_no)) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 882 | buffer, buffer_size, &length, |
| 883 | "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" |
| 884 | "pa%08Xpb%08Xpc%08Xpd%08X", |
| 885 | p_dev->has_manf_id ? p_dev->manf_id : 0, |
| 886 | p_dev->has_card_id ? p_dev->card_id : 0, |
| 887 | p_dev->has_func_id ? p_dev->func_id : 0, |
| 888 | p_dev->func, |
| 889 | p_dev->device_no, |
| 890 | hash[0], |
| 891 | hash[1], |
| 892 | hash[2], |
| 893 | hash[3])) |
| 894 | return -ENOMEM; |
| 895 | |
| 896 | envp[i] = NULL; |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | |
| 901 | #else |
| 902 | |
| 903 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, |
| 904 | char *buffer, int buffer_size) |
| 905 | { |
| 906 | return -ENODEV; |
| 907 | } |
| 908 | |
| 909 | #endif |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | /************************ per-device sysfs output ***************************/ |
| 912 | |
| 913 | #define pcmcia_device_attr(field, test, format) \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 914 | static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | { \ |
| 916 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ |
| 917 | return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \ |
| 918 | } |
| 919 | |
| 920 | #define pcmcia_device_stringattr(name, field) \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 921 | static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | { \ |
| 923 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ |
| 924 | return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \ |
| 925 | } |
| 926 | |
| 927 | pcmcia_device_attr(func, socket, "0x%02x\n"); |
| 928 | pcmcia_device_attr(func_id, has_func_id, "0x%02x\n"); |
| 929 | pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n"); |
| 930 | pcmcia_device_attr(card_id, has_card_id, "0x%04x\n"); |
| 931 | pcmcia_device_stringattr(prod_id1, prod_id[0]); |
| 932 | pcmcia_device_stringattr(prod_id2, prod_id[1]); |
| 933 | pcmcia_device_stringattr(prod_id3, prod_id[2]); |
| 934 | pcmcia_device_stringattr(prod_id4, prod_id[3]); |
| 935 | |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 936 | |
| 937 | static ssize_t pcmcia_store_allow_func_id_match (struct device * dev, struct device_attribute *attr, |
| 938 | const char * buf, size_t count) |
| 939 | { |
| 940 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 941 | if (!count) |
| 942 | return -EINVAL; |
| 943 | |
| 944 | down(&p_dev->socket->skt_sem); |
| 945 | p_dev->allow_func_id_match = 1; |
| 946 | up(&p_dev->socket->skt_sem); |
| 947 | |
| 948 | bus_rescan_devices(&pcmcia_bus_type); |
| 949 | |
| 950 | return count; |
| 951 | } |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | static struct device_attribute pcmcia_dev_attrs[] = { |
| 954 | __ATTR(function, 0444, func_show, NULL), |
| 955 | __ATTR_RO(func_id), |
| 956 | __ATTR_RO(manf_id), |
| 957 | __ATTR_RO(card_id), |
| 958 | __ATTR_RO(prod_id1), |
| 959 | __ATTR_RO(prod_id2), |
| 960 | __ATTR_RO(prod_id3), |
| 961 | __ATTR_RO(prod_id4), |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 962 | __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | __ATTR_NULL, |
| 964 | }; |
| 965 | |
| 966 | |
| 967 | /*====================================================================== |
| 968 | |
| 969 | These manage a ring buffer of events pending for one user process |
| 970 | |
| 971 | ======================================================================*/ |
| 972 | |
| 973 | static int queue_empty(user_info_t *user) |
| 974 | { |
| 975 | return (user->event_head == user->event_tail); |
| 976 | } |
| 977 | |
| 978 | static event_t get_queued_event(user_info_t *user) |
| 979 | { |
| 980 | user->event_tail = (user->event_tail+1) % MAX_EVENTS; |
| 981 | return user->event[user->event_tail]; |
| 982 | } |
| 983 | |
| 984 | static void queue_event(user_info_t *user, event_t event) |
| 985 | { |
| 986 | user->event_head = (user->event_head+1) % MAX_EVENTS; |
| 987 | if (user->event_head == user->event_tail) |
| 988 | user->event_tail = (user->event_tail+1) % MAX_EVENTS; |
| 989 | user->event[user->event_head] = event; |
| 990 | } |
| 991 | |
| 992 | static void handle_event(struct pcmcia_bus_socket *s, event_t event) |
| 993 | { |
| 994 | user_info_t *user; |
| 995 | for (user = s->user; user; user = user->next) |
| 996 | queue_event(user, event); |
| 997 | wake_up_interruptible(&s->queue); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | /*====================================================================== |
| 1002 | |
| 1003 | The card status event handler. |
| 1004 | |
| 1005 | ======================================================================*/ |
| 1006 | |
| 1007 | struct send_event_data { |
| 1008 | struct pcmcia_socket *skt; |
| 1009 | event_t event; |
| 1010 | int priority; |
| 1011 | }; |
| 1012 | |
| 1013 | static int send_event_callback(struct device *dev, void * _data) |
| 1014 | { |
| 1015 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 1016 | struct send_event_data *data = _data; |
| 1017 | |
| 1018 | /* we get called for all sockets, but may only pass the event |
| 1019 | * for drivers _on the affected socket_ */ |
| 1020 | if (p_dev->socket != data->skt) |
| 1021 | return 0; |
| 1022 | |
| 1023 | if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE)) |
| 1024 | return 0; |
| 1025 | |
| 1026 | if (p_dev->client.EventMask & data->event) |
| 1027 | return EVENT(&p_dev->client, data->event, data->priority); |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int send_event(struct pcmcia_socket *s, event_t event, int priority) |
| 1033 | { |
| 1034 | int ret = 0; |
| 1035 | struct send_event_data private; |
| 1036 | struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia); |
| 1037 | |
| 1038 | if (!skt) |
| 1039 | return 0; |
| 1040 | |
| 1041 | private.skt = s; |
| 1042 | private.event = event; |
| 1043 | private.priority = priority; |
| 1044 | |
| 1045 | ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback); |
| 1046 | |
| 1047 | pcmcia_put_bus_socket(skt); |
| 1048 | return ret; |
| 1049 | } /* send_event */ |
| 1050 | |
| 1051 | |
| 1052 | /* Normally, the event is passed to individual drivers after |
| 1053 | * informing userspace. Only for CS_EVENT_CARD_REMOVAL this |
| 1054 | * is inversed to maintain historic compatibility. |
| 1055 | */ |
| 1056 | |
| 1057 | static int ds_event(struct pcmcia_socket *skt, event_t event, int priority) |
| 1058 | { |
| 1059 | struct pcmcia_bus_socket *s = skt->pcmcia; |
| 1060 | int ret = 0; |
| 1061 | |
| 1062 | ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n", |
| 1063 | event, priority, s); |
| 1064 | |
| 1065 | switch (event) { |
| 1066 | |
| 1067 | case CS_EVENT_CARD_REMOVAL: |
| 1068 | s->state &= ~DS_SOCKET_PRESENT; |
| 1069 | send_event(skt, event, priority); |
| 1070 | unbind_request(s); |
| 1071 | handle_event(s, event); |
| 1072 | break; |
| 1073 | |
| 1074 | case CS_EVENT_CARD_INSERTION: |
| 1075 | s->state |= DS_SOCKET_PRESENT; |
| 1076 | pcmcia_card_add(skt); |
| 1077 | handle_event(s, event); |
| 1078 | break; |
| 1079 | |
| 1080 | case CS_EVENT_EJECTION_REQUEST: |
| 1081 | ret = send_event(skt, event, priority); |
| 1082 | break; |
| 1083 | |
| 1084 | default: |
| 1085 | handle_event(s, event); |
| 1086 | send_event(skt, event, priority); |
| 1087 | break; |
| 1088 | } |
| 1089 | |
| 1090 | return 0; |
| 1091 | } /* ds_event */ |
| 1092 | |
| 1093 | |
| 1094 | /*====================================================================== |
| 1095 | |
| 1096 | bind_request() and bind_device() are merged by now. Register_client() |
| 1097 | is called right at the end of bind_request(), during the driver's |
| 1098 | ->attach() call. Individual descriptions: |
| 1099 | |
| 1100 | bind_request() connects a socket to a particular client driver. |
| 1101 | It looks up the specified device ID in the list of registered |
| 1102 | drivers, binds it to the socket, and tries to create an instance |
| 1103 | of the device. unbind_request() deletes a driver instance. |
| 1104 | |
| 1105 | Bind_device() associates a device driver with a particular socket. |
| 1106 | It is normally called by Driver Services after it has identified |
| 1107 | a newly inserted card. An instance of that driver will then be |
| 1108 | eligible to register as a client of this socket. |
| 1109 | |
| 1110 | Register_client() uses the dev_info_t handle to match the |
| 1111 | caller with a socket. The driver must have already been bound |
| 1112 | to a socket with bind_device() -- in fact, bind_device() |
| 1113 | allocates the client structure that will be used. |
| 1114 | |
| 1115 | ======================================================================*/ |
| 1116 | |
| 1117 | static int bind_request(struct pcmcia_bus_socket *s, bind_info_t *bind_info) |
| 1118 | { |
| 1119 | struct pcmcia_driver *p_drv; |
| 1120 | struct pcmcia_device *p_dev; |
| 1121 | int ret = 0; |
| 1122 | unsigned long flags; |
| 1123 | |
| 1124 | s = pcmcia_get_bus_socket(s); |
| 1125 | if (!s) |
| 1126 | return -EINVAL; |
| 1127 | |
| 1128 | ds_dbg(2, "bind_request(%d, '%s')\n", s->parent->sock, |
| 1129 | (char *)bind_info->dev_info); |
| 1130 | |
| 1131 | p_drv = get_pcmcia_driver(&bind_info->dev_info); |
| 1132 | if (!p_drv) { |
| 1133 | ret = -EINVAL; |
| 1134 | goto err_put; |
| 1135 | } |
| 1136 | |
| 1137 | if (!try_module_get(p_drv->owner)) { |
| 1138 | ret = -EINVAL; |
| 1139 | goto err_put_driver; |
| 1140 | } |
| 1141 | |
| 1142 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1143 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
| 1144 | if (p_dev->func == bind_info->function) { |
| 1145 | if ((p_dev->dev.driver == &p_drv->drv)) { |
| 1146 | if (p_dev->cardmgr) { |
| 1147 | /* if there's already a device |
| 1148 | * registered, and it was registered |
| 1149 | * by userspace before, we need to |
| 1150 | * return the "instance". */ |
| 1151 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1152 | bind_info->instance = p_dev->instance; |
| 1153 | ret = -EBUSY; |
| 1154 | goto err_put_module; |
| 1155 | } else { |
| 1156 | /* the correct driver managed to bind |
| 1157 | * itself magically to the correct |
| 1158 | * device. */ |
| 1159 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1160 | p_dev->cardmgr = p_drv; |
| 1161 | ret = 0; |
| 1162 | goto err_put_module; |
| 1163 | } |
| 1164 | } else if (!p_dev->dev.driver) { |
| 1165 | /* there's already a device available where |
| 1166 | * no device has been bound to yet. So we don't |
| 1167 | * need to register a device! */ |
| 1168 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1169 | goto rescan; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1174 | |
| 1175 | p_dev = pcmcia_device_add(s, bind_info->function); |
| 1176 | if (!p_dev) { |
| 1177 | ret = -EIO; |
| 1178 | goto err_put_module; |
| 1179 | } |
| 1180 | |
| 1181 | rescan: |
| 1182 | p_dev->cardmgr = p_drv; |
| 1183 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 1184 | /* if a driver is already running, we can abort */ |
| 1185 | if (p_dev->dev.driver) |
| 1186 | goto err_put_module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* |
| 1189 | * Prevent this racing with a card insertion. |
| 1190 | */ |
| 1191 | down(&s->parent->skt_sem); |
| 1192 | bus_rescan_devices(&pcmcia_bus_type); |
| 1193 | up(&s->parent->skt_sem); |
| 1194 | |
| 1195 | /* check whether the driver indeed matched. I don't care if this |
| 1196 | * is racy or not, because it can only happen on cardmgr access |
| 1197 | * paths... |
| 1198 | */ |
| 1199 | if (!(p_dev->dev.driver == &p_drv->drv)) |
| 1200 | p_dev->cardmgr = NULL; |
| 1201 | |
| 1202 | err_put_module: |
| 1203 | module_put(p_drv->owner); |
| 1204 | err_put_driver: |
| 1205 | put_driver(&p_drv->drv); |
| 1206 | err_put: |
| 1207 | pcmcia_put_bus_socket(s); |
| 1208 | |
| 1209 | return (ret); |
| 1210 | } /* bind_request */ |
| 1211 | |
| 1212 | |
| 1213 | int pcmcia_register_client(client_handle_t *handle, client_reg_t *req) |
| 1214 | { |
| 1215 | client_t *client = NULL; |
| 1216 | struct pcmcia_socket *s; |
| 1217 | struct pcmcia_bus_socket *skt = NULL; |
| 1218 | struct pcmcia_device *p_dev = NULL; |
| 1219 | |
| 1220 | /* Look for unbound client with matching dev_info */ |
| 1221 | down_read(&pcmcia_socket_list_rwsem); |
| 1222 | list_for_each_entry(s, &pcmcia_socket_list, socket_list) { |
| 1223 | unsigned long flags; |
| 1224 | |
| 1225 | if (s->state & SOCKET_CARDBUS) |
| 1226 | continue; |
| 1227 | |
| 1228 | skt = s->pcmcia; |
| 1229 | if (!skt) |
| 1230 | continue; |
| 1231 | skt = pcmcia_get_bus_socket(skt); |
| 1232 | if (!skt) |
| 1233 | continue; |
| 1234 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1235 | list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) { |
| 1236 | struct pcmcia_driver *p_drv; |
| 1237 | p_dev = pcmcia_get_dev(p_dev); |
| 1238 | if (!p_dev) |
| 1239 | continue; |
| 1240 | if (!(p_dev->client.state & CLIENT_UNBOUND) || |
| 1241 | (!p_dev->dev.driver)) { |
| 1242 | pcmcia_put_dev(p_dev); |
| 1243 | continue; |
| 1244 | } |
| 1245 | p_drv = to_pcmcia_drv(p_dev->dev.driver); |
| 1246 | if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) { |
| 1247 | client = &p_dev->client; |
| 1248 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1249 | goto found; |
| 1250 | } |
| 1251 | pcmcia_put_dev(p_dev); |
| 1252 | } |
| 1253 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1254 | pcmcia_put_bus_socket(skt); |
| 1255 | } |
| 1256 | found: |
| 1257 | up_read(&pcmcia_socket_list_rwsem); |
| 1258 | if (!p_dev || !client) |
| 1259 | return -ENODEV; |
| 1260 | |
| 1261 | pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */ |
| 1262 | |
| 1263 | *handle = client; |
| 1264 | client->state &= ~CLIENT_UNBOUND; |
| 1265 | client->Socket = s; |
| 1266 | client->EventMask = req->EventMask; |
| 1267 | client->event_handler = req->event_handler; |
| 1268 | client->event_callback_args = req->event_callback_args; |
| 1269 | client->event_callback_args.client_handle = client; |
| 1270 | |
| 1271 | if (s->state & SOCKET_CARDBUS) |
| 1272 | client->state |= CLIENT_CARDBUS; |
| 1273 | |
| 1274 | if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) && |
| 1275 | (client->Function != BIND_FN_ALL)) { |
| 1276 | cistpl_longlink_mfc_t mfc; |
| 1277 | if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc) |
| 1278 | == CS_SUCCESS) |
| 1279 | s->functions = mfc.nfn; |
| 1280 | else |
| 1281 | s->functions = 1; |
| 1282 | s->config = kmalloc(sizeof(config_t) * s->functions, |
| 1283 | GFP_KERNEL); |
| 1284 | if (!s->config) |
| 1285 | goto out_no_resource; |
| 1286 | memset(s->config, 0, sizeof(config_t) * s->functions); |
| 1287 | } |
| 1288 | |
| 1289 | ds_dbg(1, "register_client(): client 0x%p, dev %s\n", |
| 1290 | client, p_dev->dev.bus_id); |
| 1291 | if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE) |
| 1292 | EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW); |
| 1293 | |
| 1294 | if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) { |
| 1295 | if (client->EventMask & CS_EVENT_CARD_INSERTION) |
| 1296 | EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW); |
| 1297 | } |
| 1298 | |
| 1299 | return CS_SUCCESS; |
| 1300 | |
| 1301 | out_no_resource: |
| 1302 | pcmcia_put_dev(p_dev); |
| 1303 | return CS_OUT_OF_RESOURCE; |
| 1304 | } /* register_client */ |
| 1305 | EXPORT_SYMBOL(pcmcia_register_client); |
| 1306 | |
| 1307 | |
| 1308 | /*====================================================================*/ |
| 1309 | |
| 1310 | extern struct pci_bus *pcmcia_lookup_bus(struct pcmcia_socket *s); |
| 1311 | |
| 1312 | static int get_device_info(struct pcmcia_bus_socket *s, bind_info_t *bind_info, int first) |
| 1313 | { |
| 1314 | dev_node_t *node; |
| 1315 | struct pcmcia_device *p_dev; |
| 1316 | unsigned long flags; |
| 1317 | int ret = 0; |
| 1318 | |
| 1319 | #ifdef CONFIG_CARDBUS |
| 1320 | /* |
| 1321 | * Some unbelievably ugly code to associate the PCI cardbus |
| 1322 | * device and its driver with the PCMCIA "bind" information. |
| 1323 | */ |
| 1324 | { |
| 1325 | struct pci_bus *bus; |
| 1326 | |
| 1327 | bus = pcmcia_lookup_bus(s->parent); |
| 1328 | if (bus) { |
| 1329 | struct list_head *list; |
| 1330 | struct pci_dev *dev = NULL; |
| 1331 | |
| 1332 | list = bus->devices.next; |
| 1333 | while (list != &bus->devices) { |
| 1334 | struct pci_dev *pdev = pci_dev_b(list); |
| 1335 | list = list->next; |
| 1336 | |
| 1337 | if (first) { |
| 1338 | dev = pdev; |
| 1339 | break; |
| 1340 | } |
| 1341 | |
| 1342 | /* Try to handle "next" here some way? */ |
| 1343 | } |
| 1344 | if (dev && dev->driver) { |
| 1345 | strlcpy(bind_info->name, dev->driver->name, DEV_NAME_LEN); |
| 1346 | bind_info->major = 0; |
| 1347 | bind_info->minor = 0; |
| 1348 | bind_info->next = NULL; |
| 1349 | return 0; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | #endif |
| 1354 | |
| 1355 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1356 | list_for_each_entry(p_dev, &s->devices_list, socket_device_list) { |
| 1357 | if (p_dev->func == bind_info->function) { |
| 1358 | p_dev = pcmcia_get_dev(p_dev); |
| 1359 | if (!p_dev) |
| 1360 | continue; |
| 1361 | goto found; |
| 1362 | } |
| 1363 | } |
| 1364 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1365 | return -ENODEV; |
| 1366 | |
| 1367 | found: |
| 1368 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1369 | |
| 1370 | if ((!p_dev->instance) || |
| 1371 | (p_dev->instance->state & DEV_CONFIG_PENDING)) { |
| 1372 | ret = -EAGAIN; |
| 1373 | goto err_put; |
| 1374 | } |
| 1375 | |
| 1376 | if (first) |
| 1377 | node = p_dev->instance->dev; |
| 1378 | else |
| 1379 | for (node = p_dev->instance->dev; node; node = node->next) |
| 1380 | if (node == bind_info->next) |
| 1381 | break; |
| 1382 | if (!node) { |
| 1383 | ret = -ENODEV; |
| 1384 | goto err_put; |
| 1385 | } |
| 1386 | |
| 1387 | strlcpy(bind_info->name, node->dev_name, DEV_NAME_LEN); |
| 1388 | bind_info->major = node->major; |
| 1389 | bind_info->minor = node->minor; |
| 1390 | bind_info->next = node->next; |
| 1391 | |
| 1392 | err_put: |
| 1393 | pcmcia_put_dev(p_dev); |
| 1394 | return (ret); |
| 1395 | } /* get_device_info */ |
| 1396 | |
| 1397 | /*====================================================================*/ |
| 1398 | |
| 1399 | /* unbind _all_ devices attached to a given pcmcia_bus_socket. The |
| 1400 | * drivers have been called with EVENT_CARD_REMOVAL before. |
| 1401 | */ |
| 1402 | static int unbind_request(struct pcmcia_bus_socket *s) |
| 1403 | { |
| 1404 | struct pcmcia_device *p_dev; |
| 1405 | unsigned long flags; |
| 1406 | |
| 1407 | ds_dbg(2, "unbind_request(%d)\n", s->parent->sock); |
| 1408 | |
| 1409 | s->device_count = 0; |
| 1410 | |
| 1411 | for (;;) { |
| 1412 | /* unregister all pcmcia_devices registered with this socket*/ |
| 1413 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1414 | if (list_empty(&s->devices_list)) { |
| 1415 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1416 | return 0; |
| 1417 | } |
| 1418 | p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list); |
| 1419 | list_del(&p_dev->socket_device_list); |
| 1420 | p_dev->client.state |= CLIENT_STALE; |
| 1421 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1422 | |
| 1423 | device_unregister(&p_dev->dev); |
| 1424 | } |
| 1425 | |
| 1426 | return 0; |
| 1427 | } /* unbind_request */ |
| 1428 | |
| 1429 | int pcmcia_deregister_client(client_handle_t handle) |
| 1430 | { |
| 1431 | struct pcmcia_socket *s; |
| 1432 | int i; |
| 1433 | struct pcmcia_device *p_dev = handle_to_pdev(handle); |
| 1434 | |
| 1435 | if (CHECK_HANDLE(handle)) |
| 1436 | return CS_BAD_HANDLE; |
| 1437 | |
| 1438 | s = SOCKET(handle); |
| 1439 | ds_dbg(1, "deregister_client(%p)\n", handle); |
| 1440 | |
| 1441 | if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED)) |
| 1442 | goto warn_out; |
| 1443 | for (i = 0; i < MAX_WIN; i++) |
| 1444 | if (handle->state & CLIENT_WIN_REQ(i)) |
| 1445 | goto warn_out; |
| 1446 | |
| 1447 | if (handle->state & CLIENT_STALE) { |
| 1448 | handle->client_magic = 0; |
| 1449 | handle->state &= ~CLIENT_STALE; |
| 1450 | pcmcia_put_dev(p_dev); |
| 1451 | } else { |
| 1452 | handle->state = CLIENT_UNBOUND; |
| 1453 | handle->event_handler = NULL; |
| 1454 | } |
| 1455 | |
| 1456 | return CS_SUCCESS; |
| 1457 | warn_out: |
| 1458 | printk(KERN_WARNING "ds: deregister_client was called too early.\n"); |
| 1459 | return CS_IN_USE; |
| 1460 | } /* deregister_client */ |
| 1461 | EXPORT_SYMBOL(pcmcia_deregister_client); |
| 1462 | |
| 1463 | |
| 1464 | /*====================================================================== |
| 1465 | |
| 1466 | The user-mode PC Card device interface |
| 1467 | |
| 1468 | ======================================================================*/ |
| 1469 | |
| 1470 | static int ds_open(struct inode *inode, struct file *file) |
| 1471 | { |
| 1472 | socket_t i = iminor(inode); |
| 1473 | struct pcmcia_bus_socket *s; |
| 1474 | user_info_t *user; |
| 1475 | |
| 1476 | ds_dbg(0, "ds_open(socket %d)\n", i); |
| 1477 | |
| 1478 | s = get_socket_info_by_nr(i); |
| 1479 | if (!s) |
| 1480 | return -ENODEV; |
| 1481 | s = pcmcia_get_bus_socket(s); |
| 1482 | if (!s) |
| 1483 | return -ENODEV; |
| 1484 | |
| 1485 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) { |
| 1486 | if (s->state & DS_SOCKET_BUSY) { |
| 1487 | pcmcia_put_bus_socket(s); |
| 1488 | return -EBUSY; |
| 1489 | } |
| 1490 | else |
| 1491 | s->state |= DS_SOCKET_BUSY; |
| 1492 | } |
| 1493 | |
| 1494 | user = kmalloc(sizeof(user_info_t), GFP_KERNEL); |
| 1495 | if (!user) { |
| 1496 | pcmcia_put_bus_socket(s); |
| 1497 | return -ENOMEM; |
| 1498 | } |
| 1499 | user->event_tail = user->event_head = 0; |
| 1500 | user->next = s->user; |
| 1501 | user->user_magic = USER_MAGIC; |
| 1502 | user->socket = s; |
| 1503 | s->user = user; |
| 1504 | file->private_data = user; |
| 1505 | |
| 1506 | if (s->state & DS_SOCKET_PRESENT) |
| 1507 | queue_event(user, CS_EVENT_CARD_INSERTION); |
| 1508 | return 0; |
| 1509 | } /* ds_open */ |
| 1510 | |
| 1511 | /*====================================================================*/ |
| 1512 | |
| 1513 | static int ds_release(struct inode *inode, struct file *file) |
| 1514 | { |
| 1515 | struct pcmcia_bus_socket *s; |
| 1516 | user_info_t *user, **link; |
| 1517 | |
| 1518 | ds_dbg(0, "ds_release(socket %d)\n", iminor(inode)); |
| 1519 | |
| 1520 | user = file->private_data; |
| 1521 | if (CHECK_USER(user)) |
| 1522 | goto out; |
| 1523 | |
| 1524 | s = user->socket; |
| 1525 | |
| 1526 | /* Unlink user data structure */ |
| 1527 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) { |
| 1528 | s->state &= ~DS_SOCKET_BUSY; |
| 1529 | } |
| 1530 | file->private_data = NULL; |
| 1531 | for (link = &s->user; *link; link = &(*link)->next) |
| 1532 | if (*link == user) break; |
| 1533 | if (link == NULL) |
| 1534 | goto out; |
| 1535 | *link = user->next; |
| 1536 | user->user_magic = 0; |
| 1537 | kfree(user); |
| 1538 | pcmcia_put_bus_socket(s); |
| 1539 | out: |
| 1540 | return 0; |
| 1541 | } /* ds_release */ |
| 1542 | |
| 1543 | /*====================================================================*/ |
| 1544 | |
| 1545 | static ssize_t ds_read(struct file *file, char __user *buf, |
| 1546 | size_t count, loff_t *ppos) |
| 1547 | { |
| 1548 | struct pcmcia_bus_socket *s; |
| 1549 | user_info_t *user; |
| 1550 | int ret; |
| 1551 | |
| 1552 | ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode)); |
| 1553 | |
| 1554 | if (count < 4) |
| 1555 | return -EINVAL; |
| 1556 | |
| 1557 | user = file->private_data; |
| 1558 | if (CHECK_USER(user)) |
| 1559 | return -EIO; |
| 1560 | |
| 1561 | s = user->socket; |
| 1562 | if (s->state & DS_SOCKET_DEAD) |
| 1563 | return -EIO; |
| 1564 | |
| 1565 | ret = wait_event_interruptible(s->queue, !queue_empty(user)); |
| 1566 | if (ret == 0) |
| 1567 | ret = put_user(get_queued_event(user), (int __user *)buf) ? -EFAULT : 4; |
| 1568 | |
| 1569 | return ret; |
| 1570 | } /* ds_read */ |
| 1571 | |
| 1572 | /*====================================================================*/ |
| 1573 | |
| 1574 | static ssize_t ds_write(struct file *file, const char __user *buf, |
| 1575 | size_t count, loff_t *ppos) |
| 1576 | { |
| 1577 | ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode)); |
| 1578 | |
| 1579 | if (count != 4) |
| 1580 | return -EINVAL; |
| 1581 | if ((file->f_flags & O_ACCMODE) == O_RDONLY) |
| 1582 | return -EBADF; |
| 1583 | |
| 1584 | return -EIO; |
| 1585 | } /* ds_write */ |
| 1586 | |
| 1587 | /*====================================================================*/ |
| 1588 | |
| 1589 | /* No kernel lock - fine */ |
| 1590 | static u_int ds_poll(struct file *file, poll_table *wait) |
| 1591 | { |
| 1592 | struct pcmcia_bus_socket *s; |
| 1593 | user_info_t *user; |
| 1594 | |
| 1595 | ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode)); |
| 1596 | |
| 1597 | user = file->private_data; |
| 1598 | if (CHECK_USER(user)) |
| 1599 | return POLLERR; |
| 1600 | s = user->socket; |
| 1601 | /* |
| 1602 | * We don't check for a dead socket here since that |
| 1603 | * will send cardmgr into an endless spin. |
| 1604 | */ |
| 1605 | poll_wait(file, &s->queue, wait); |
| 1606 | if (!queue_empty(user)) |
| 1607 | return POLLIN | POLLRDNORM; |
| 1608 | return 0; |
| 1609 | } /* ds_poll */ |
| 1610 | |
| 1611 | /*====================================================================*/ |
| 1612 | |
| 1613 | extern int pcmcia_adjust_resource_info(adjust_t *adj); |
| 1614 | |
| 1615 | static int ds_ioctl(struct inode * inode, struct file * file, |
| 1616 | u_int cmd, u_long arg) |
| 1617 | { |
| 1618 | struct pcmcia_bus_socket *s; |
| 1619 | void __user *uarg = (char __user *)arg; |
| 1620 | u_int size; |
| 1621 | int ret, err; |
| 1622 | ds_ioctl_arg_t *buf; |
| 1623 | user_info_t *user; |
| 1624 | |
| 1625 | ds_dbg(2, "ds_ioctl(socket %d, %#x, %#lx)\n", iminor(inode), cmd, arg); |
| 1626 | |
| 1627 | user = file->private_data; |
| 1628 | if (CHECK_USER(user)) |
| 1629 | return -EIO; |
| 1630 | |
| 1631 | s = user->socket; |
| 1632 | if (s->state & DS_SOCKET_DEAD) |
| 1633 | return -EIO; |
| 1634 | |
| 1635 | size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT; |
| 1636 | if (size > sizeof(ds_ioctl_arg_t)) return -EINVAL; |
| 1637 | |
| 1638 | /* Permission check */ |
| 1639 | if (!(cmd & IOC_OUT) && !capable(CAP_SYS_ADMIN)) |
| 1640 | return -EPERM; |
| 1641 | |
| 1642 | if (cmd & IOC_IN) { |
| 1643 | if (!access_ok(VERIFY_READ, uarg, size)) { |
| 1644 | ds_dbg(3, "ds_ioctl(): verify_read = %d\n", -EFAULT); |
| 1645 | return -EFAULT; |
| 1646 | } |
| 1647 | } |
| 1648 | if (cmd & IOC_OUT) { |
| 1649 | if (!access_ok(VERIFY_WRITE, uarg, size)) { |
| 1650 | ds_dbg(3, "ds_ioctl(): verify_write = %d\n", -EFAULT); |
| 1651 | return -EFAULT; |
| 1652 | } |
| 1653 | } |
| 1654 | buf = kmalloc(sizeof(ds_ioctl_arg_t), GFP_KERNEL); |
| 1655 | if (!buf) |
| 1656 | return -ENOMEM; |
| 1657 | |
| 1658 | err = ret = 0; |
| 1659 | |
| 1660 | if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size); |
| 1661 | |
| 1662 | switch (cmd) { |
| 1663 | case DS_ADJUST_RESOURCE_INFO: |
| 1664 | ret = pcmcia_adjust_resource_info(&buf->adjust); |
| 1665 | break; |
| 1666 | case DS_GET_CARD_SERVICES_INFO: |
| 1667 | ret = pcmcia_get_card_services_info(&buf->servinfo); |
| 1668 | break; |
| 1669 | case DS_GET_CONFIGURATION_INFO: |
| 1670 | if (buf->config.Function && |
| 1671 | (buf->config.Function >= s->parent->functions)) |
| 1672 | ret = CS_BAD_ARGS; |
| 1673 | else |
| 1674 | ret = pccard_get_configuration_info(s->parent, |
| 1675 | buf->config.Function, &buf->config); |
| 1676 | break; |
| 1677 | case DS_GET_FIRST_TUPLE: |
| 1678 | down(&s->parent->skt_sem); |
| 1679 | pcmcia_validate_mem(s->parent); |
| 1680 | up(&s->parent->skt_sem); |
| 1681 | ret = pccard_get_first_tuple(s->parent, BIND_FN_ALL, &buf->tuple); |
| 1682 | break; |
| 1683 | case DS_GET_NEXT_TUPLE: |
| 1684 | ret = pccard_get_next_tuple(s->parent, BIND_FN_ALL, &buf->tuple); |
| 1685 | break; |
| 1686 | case DS_GET_TUPLE_DATA: |
| 1687 | buf->tuple.TupleData = buf->tuple_parse.data; |
| 1688 | buf->tuple.TupleDataMax = sizeof(buf->tuple_parse.data); |
| 1689 | ret = pccard_get_tuple_data(s->parent, &buf->tuple); |
| 1690 | break; |
| 1691 | case DS_PARSE_TUPLE: |
| 1692 | buf->tuple.TupleData = buf->tuple_parse.data; |
| 1693 | ret = pccard_parse_tuple(&buf->tuple, &buf->tuple_parse.parse); |
| 1694 | break; |
| 1695 | case DS_RESET_CARD: |
| 1696 | ret = pccard_reset_card(s->parent); |
| 1697 | break; |
| 1698 | case DS_GET_STATUS: |
| 1699 | if (buf->status.Function && |
| 1700 | (buf->status.Function >= s->parent->functions)) |
| 1701 | ret = CS_BAD_ARGS; |
| 1702 | else |
| 1703 | ret = pccard_get_status(s->parent, buf->status.Function, &buf->status); |
| 1704 | break; |
| 1705 | case DS_VALIDATE_CIS: |
| 1706 | down(&s->parent->skt_sem); |
| 1707 | pcmcia_validate_mem(s->parent); |
| 1708 | up(&s->parent->skt_sem); |
| 1709 | ret = pccard_validate_cis(s->parent, BIND_FN_ALL, &buf->cisinfo); |
| 1710 | break; |
| 1711 | case DS_SUSPEND_CARD: |
| 1712 | ret = pcmcia_suspend_card(s->parent); |
| 1713 | break; |
| 1714 | case DS_RESUME_CARD: |
| 1715 | ret = pcmcia_resume_card(s->parent); |
| 1716 | break; |
| 1717 | case DS_EJECT_CARD: |
| 1718 | err = pcmcia_eject_card(s->parent); |
| 1719 | break; |
| 1720 | case DS_INSERT_CARD: |
| 1721 | err = pcmcia_insert_card(s->parent); |
| 1722 | break; |
| 1723 | case DS_ACCESS_CONFIGURATION_REGISTER: |
| 1724 | if ((buf->conf_reg.Action == CS_WRITE) && !capable(CAP_SYS_ADMIN)) { |
| 1725 | err = -EPERM; |
| 1726 | goto free_out; |
| 1727 | } |
| 1728 | if (buf->conf_reg.Function && |
| 1729 | (buf->conf_reg.Function >= s->parent->functions)) |
| 1730 | ret = CS_BAD_ARGS; |
| 1731 | else |
| 1732 | ret = pccard_access_configuration_register(s->parent, |
| 1733 | buf->conf_reg.Function, &buf->conf_reg); |
| 1734 | break; |
| 1735 | case DS_GET_FIRST_REGION: |
| 1736 | case DS_GET_NEXT_REGION: |
| 1737 | case DS_BIND_MTD: |
| 1738 | if (!capable(CAP_SYS_ADMIN)) { |
| 1739 | err = -EPERM; |
| 1740 | goto free_out; |
| 1741 | } else { |
| 1742 | static int printed = 0; |
| 1743 | if (!printed) { |
| 1744 | printk(KERN_WARNING "2.6. kernels use pcmciamtd instead of memory_cs.c and do not require special\n"); |
| 1745 | printk(KERN_WARNING "MTD handling any more.\n"); |
| 1746 | printed++; |
| 1747 | } |
| 1748 | } |
| 1749 | err = -EINVAL; |
| 1750 | goto free_out; |
| 1751 | break; |
| 1752 | case DS_GET_FIRST_WINDOW: |
| 1753 | ret = pcmcia_get_window(s->parent, &buf->win_info.handle, 0, |
| 1754 | &buf->win_info.window); |
| 1755 | break; |
| 1756 | case DS_GET_NEXT_WINDOW: |
| 1757 | ret = pcmcia_get_window(s->parent, &buf->win_info.handle, |
| 1758 | buf->win_info.handle->index + 1, &buf->win_info.window); |
| 1759 | break; |
| 1760 | case DS_GET_MEM_PAGE: |
| 1761 | ret = pcmcia_get_mem_page(buf->win_info.handle, |
| 1762 | &buf->win_info.map); |
| 1763 | break; |
| 1764 | case DS_REPLACE_CIS: |
| 1765 | ret = pcmcia_replace_cis(s->parent, &buf->cisdump); |
| 1766 | break; |
| 1767 | case DS_BIND_REQUEST: |
| 1768 | if (!capable(CAP_SYS_ADMIN)) { |
| 1769 | err = -EPERM; |
| 1770 | goto free_out; |
| 1771 | } |
| 1772 | err = bind_request(s, &buf->bind_info); |
| 1773 | break; |
| 1774 | case DS_GET_DEVICE_INFO: |
| 1775 | err = get_device_info(s, &buf->bind_info, 1); |
| 1776 | break; |
| 1777 | case DS_GET_NEXT_DEVICE: |
| 1778 | err = get_device_info(s, &buf->bind_info, 0); |
| 1779 | break; |
| 1780 | case DS_UNBIND_REQUEST: |
| 1781 | err = 0; |
| 1782 | break; |
| 1783 | default: |
| 1784 | err = -EINVAL; |
| 1785 | } |
| 1786 | |
| 1787 | if ((err == 0) && (ret != CS_SUCCESS)) { |
| 1788 | ds_dbg(2, "ds_ioctl: ret = %d\n", ret); |
| 1789 | switch (ret) { |
| 1790 | case CS_BAD_SOCKET: case CS_NO_CARD: |
| 1791 | err = -ENODEV; break; |
| 1792 | case CS_BAD_ARGS: case CS_BAD_ATTRIBUTE: case CS_BAD_IRQ: |
| 1793 | case CS_BAD_TUPLE: |
| 1794 | err = -EINVAL; break; |
| 1795 | case CS_IN_USE: |
| 1796 | err = -EBUSY; break; |
| 1797 | case CS_OUT_OF_RESOURCE: |
| 1798 | err = -ENOSPC; break; |
| 1799 | case CS_NO_MORE_ITEMS: |
| 1800 | err = -ENODATA; break; |
| 1801 | case CS_UNSUPPORTED_FUNCTION: |
| 1802 | err = -ENOSYS; break; |
| 1803 | default: |
| 1804 | err = -EIO; break; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | if (cmd & IOC_OUT) { |
| 1809 | if (__copy_to_user(uarg, (char *)buf, size)) |
| 1810 | err = -EFAULT; |
| 1811 | } |
| 1812 | |
| 1813 | free_out: |
| 1814 | kfree(buf); |
| 1815 | return err; |
| 1816 | } /* ds_ioctl */ |
| 1817 | |
| 1818 | /*====================================================================*/ |
| 1819 | |
| 1820 | static struct file_operations ds_fops = { |
| 1821 | .owner = THIS_MODULE, |
| 1822 | .open = ds_open, |
| 1823 | .release = ds_release, |
| 1824 | .ioctl = ds_ioctl, |
| 1825 | .read = ds_read, |
| 1826 | .write = ds_write, |
| 1827 | .poll = ds_poll, |
| 1828 | }; |
| 1829 | |
| 1830 | static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev) |
| 1831 | { |
| 1832 | struct pcmcia_socket *socket = class_get_devdata(class_dev); |
| 1833 | struct pcmcia_bus_socket *s; |
| 1834 | int ret; |
| 1835 | |
| 1836 | s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL); |
| 1837 | if(!s) |
| 1838 | return -ENOMEM; |
| 1839 | memset(s, 0, sizeof(struct pcmcia_bus_socket)); |
| 1840 | |
| 1841 | /* get reference to parent socket */ |
| 1842 | s->parent = pcmcia_get_socket(socket); |
| 1843 | if (!s->parent) { |
| 1844 | printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket); |
| 1845 | kfree (s); |
| 1846 | return -ENODEV; |
| 1847 | } |
| 1848 | |
| 1849 | kref_init(&s->refcount); |
| 1850 | |
| 1851 | /* |
| 1852 | * Ugly. But we want to wait for the socket threads to have started up. |
| 1853 | * We really should let the drivers themselves drive some of this.. |
| 1854 | */ |
| 1855 | msleep(250); |
| 1856 | |
| 1857 | init_waitqueue_head(&s->queue); |
| 1858 | INIT_LIST_HEAD(&s->devices_list); |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 1859 | INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
| 1861 | /* Set up hotline to Card Services */ |
| 1862 | s->callback.owner = THIS_MODULE; |
| 1863 | s->callback.event = &ds_event; |
| 1864 | s->callback.resources_done = &pcmcia_card_add; |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 1865 | s->callback.replace_cis = &pcmcia_bus_rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | socket->pcmcia = s; |
| 1867 | |
| 1868 | ret = pccard_register_pcmcia(socket, &s->callback); |
| 1869 | if (ret) { |
| 1870 | printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket); |
| 1871 | pcmcia_put_bus_socket(s); |
| 1872 | socket->pcmcia = NULL; |
| 1873 | return (ret); |
| 1874 | } |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
| 1879 | |
| 1880 | static void pcmcia_bus_remove_socket(struct class_device *class_dev) |
| 1881 | { |
| 1882 | struct pcmcia_socket *socket = class_get_devdata(class_dev); |
| 1883 | |
| 1884 | if (!socket || !socket->pcmcia) |
| 1885 | return; |
| 1886 | |
| 1887 | pccard_register_pcmcia(socket, NULL); |
| 1888 | |
| 1889 | socket->pcmcia->state |= DS_SOCKET_DEAD; |
| 1890 | pcmcia_put_bus_socket(socket->pcmcia); |
| 1891 | socket->pcmcia = NULL; |
| 1892 | |
| 1893 | return; |
| 1894 | } |
| 1895 | |
| 1896 | |
| 1897 | /* the pcmcia_bus_interface is used to handle pcmcia socket devices */ |
| 1898 | static struct class_interface pcmcia_bus_interface = { |
| 1899 | .class = &pcmcia_socket_class, |
| 1900 | .add = &pcmcia_bus_add_socket, |
| 1901 | .remove = &pcmcia_bus_remove_socket, |
| 1902 | }; |
| 1903 | |
| 1904 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame^] | 1905 | static struct bus_type pcmcia_bus_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | .name = "pcmcia", |
Dominik Brodowski | 840c2ac | 2005-06-27 16:28:04 -0700 | [diff] [blame] | 1907 | .hotplug = pcmcia_bus_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | .match = pcmcia_bus_match, |
| 1909 | .dev_attrs = pcmcia_dev_attrs, |
| 1910 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
| 1912 | |
| 1913 | static int __init init_pcmcia_bus(void) |
| 1914 | { |
| 1915 | int i; |
| 1916 | |
| 1917 | spin_lock_init(&pcmcia_dev_list_lock); |
| 1918 | |
| 1919 | bus_register(&pcmcia_bus_type); |
| 1920 | class_interface_register(&pcmcia_bus_interface); |
| 1921 | |
| 1922 | /* Set up character device for user mode clients */ |
| 1923 | i = register_chrdev(0, "pcmcia", &ds_fops); |
Randy Dunlap | afbf510 | 2005-05-16 21:53:56 -0700 | [diff] [blame] | 1924 | if (i < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | printk(KERN_NOTICE "unable to find a free device # for " |
Randy Dunlap | afbf510 | 2005-05-16 21:53:56 -0700 | [diff] [blame] | 1926 | "Driver Services (error=%d)\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | else |
| 1928 | major_dev = i; |
| 1929 | |
| 1930 | #ifdef CONFIG_PROC_FS |
| 1931 | proc_pccard = proc_mkdir("pccard", proc_bus); |
| 1932 | if (proc_pccard) |
| 1933 | create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL); |
| 1934 | #endif |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |
| 1938 | fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that |
| 1939 | * pcmcia_socket_class is already registered */ |
| 1940 | |
| 1941 | |
| 1942 | static void __exit exit_pcmcia_bus(void) |
| 1943 | { |
| 1944 | class_interface_unregister(&pcmcia_bus_interface); |
| 1945 | |
| 1946 | #ifdef CONFIG_PROC_FS |
| 1947 | if (proc_pccard) { |
| 1948 | remove_proc_entry("drivers", proc_pccard); |
| 1949 | remove_proc_entry("pccard", proc_bus); |
| 1950 | } |
| 1951 | #endif |
| 1952 | if (major_dev != -1) |
| 1953 | unregister_chrdev(major_dev, "pcmcia"); |
| 1954 | |
| 1955 | bus_unregister(&pcmcia_bus_type); |
| 1956 | } |
| 1957 | module_exit(exit_pcmcia_bus); |
| 1958 | |
| 1959 | |
| 1960 | |
| 1961 | /* helpers for backwards-compatible functions */ |
| 1962 | |
| 1963 | static struct pcmcia_bus_socket * get_socket_info_by_nr(unsigned int nr) |
| 1964 | { |
| 1965 | struct pcmcia_socket * s = pcmcia_get_socket_by_nr(nr); |
| 1966 | if (s && s->pcmcia) |
| 1967 | return s->pcmcia; |
| 1968 | else |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | |
| 1972 | /* backwards-compatible accessing of driver --- by name! */ |
| 1973 | |
| 1974 | static struct pcmcia_driver * get_pcmcia_driver (dev_info_t *dev_info) |
| 1975 | { |
| 1976 | struct device_driver *drv; |
| 1977 | struct pcmcia_driver *p_drv; |
| 1978 | |
| 1979 | drv = driver_find((char *) dev_info, &pcmcia_bus_type); |
| 1980 | if (!drv) |
| 1981 | return NULL; |
| 1982 | |
| 1983 | p_drv = container_of(drv, struct pcmcia_driver, drv); |
| 1984 | |
| 1985 | return (p_drv); |
| 1986 | } |
| 1987 | |
| 1988 | MODULE_ALIAS("ds"); |