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