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" |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 53 | #include "ds_internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /*====================================================================*/ |
| 56 | |
| 57 | /* Module parameters */ |
| 58 | |
| 59 | MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>"); |
| 60 | MODULE_DESCRIPTION("PCMCIA Driver Services"); |
| 61 | MODULE_LICENSE("GPL"); |
| 62 | |
| 63 | #ifdef DEBUG |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 64 | int ds_pc_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | module_param_named(pc_debug, ds_pc_debug, int, 0644); |
| 67 | |
| 68 | #define ds_dbg(lvl, fmt, arg...) do { \ |
| 69 | if (ds_pc_debug > (lvl)) \ |
| 70 | printk(KERN_DEBUG "ds: " fmt , ## arg); \ |
| 71 | } while (0) |
| 72 | #else |
| 73 | #define ds_dbg(lvl, fmt, arg...) do { } while (0) |
| 74 | #endif |
| 75 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 76 | spinlock_t pcmcia_dev_list_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | static int unbind_request(struct pcmcia_bus_socket *s); |
| 79 | |
| 80 | /*====================================================================*/ |
| 81 | |
| 82 | /* code which was in cs.c before */ |
| 83 | |
| 84 | /* String tables for error messages */ |
| 85 | |
| 86 | typedef struct lookup_t { |
| 87 | int key; |
| 88 | char *msg; |
| 89 | } lookup_t; |
| 90 | |
| 91 | static const lookup_t error_table[] = { |
| 92 | { CS_SUCCESS, "Operation succeeded" }, |
| 93 | { CS_BAD_ADAPTER, "Bad adapter" }, |
| 94 | { CS_BAD_ATTRIBUTE, "Bad attribute", }, |
| 95 | { CS_BAD_BASE, "Bad base address" }, |
| 96 | { CS_BAD_EDC, "Bad EDC" }, |
| 97 | { CS_BAD_IRQ, "Bad IRQ" }, |
| 98 | { CS_BAD_OFFSET, "Bad offset" }, |
| 99 | { CS_BAD_PAGE, "Bad page number" }, |
| 100 | { CS_READ_FAILURE, "Read failure" }, |
| 101 | { CS_BAD_SIZE, "Bad size" }, |
| 102 | { CS_BAD_SOCKET, "Bad socket" }, |
| 103 | { CS_BAD_TYPE, "Bad type" }, |
| 104 | { CS_BAD_VCC, "Bad Vcc" }, |
| 105 | { CS_BAD_VPP, "Bad Vpp" }, |
| 106 | { CS_BAD_WINDOW, "Bad window" }, |
| 107 | { CS_WRITE_FAILURE, "Write failure" }, |
| 108 | { CS_NO_CARD, "No card present" }, |
| 109 | { CS_UNSUPPORTED_FUNCTION, "Usupported function" }, |
| 110 | { CS_UNSUPPORTED_MODE, "Unsupported mode" }, |
| 111 | { CS_BAD_SPEED, "Bad speed" }, |
| 112 | { CS_BUSY, "Resource busy" }, |
| 113 | { CS_GENERAL_FAILURE, "General failure" }, |
| 114 | { CS_WRITE_PROTECTED, "Write protected" }, |
| 115 | { CS_BAD_ARG_LENGTH, "Bad argument length" }, |
| 116 | { CS_BAD_ARGS, "Bad arguments" }, |
| 117 | { CS_CONFIGURATION_LOCKED, "Configuration locked" }, |
| 118 | { CS_IN_USE, "Resource in use" }, |
| 119 | { CS_NO_MORE_ITEMS, "No more items" }, |
| 120 | { CS_OUT_OF_RESOURCE, "Out of resource" }, |
| 121 | { CS_BAD_HANDLE, "Bad handle" }, |
| 122 | { CS_BAD_TUPLE, "Bad CIS tuple" } |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | static const lookup_t service_table[] = { |
| 127 | { AccessConfigurationRegister, "AccessConfigurationRegister" }, |
| 128 | { AddSocketServices, "AddSocketServices" }, |
| 129 | { AdjustResourceInfo, "AdjustResourceInfo" }, |
| 130 | { CheckEraseQueue, "CheckEraseQueue" }, |
| 131 | { CloseMemory, "CloseMemory" }, |
| 132 | { DeregisterClient, "DeregisterClient" }, |
| 133 | { DeregisterEraseQueue, "DeregisterEraseQueue" }, |
| 134 | { GetCardServicesInfo, "GetCardServicesInfo" }, |
| 135 | { GetClientInfo, "GetClientInfo" }, |
| 136 | { GetConfigurationInfo, "GetConfigurationInfo" }, |
| 137 | { GetEventMask, "GetEventMask" }, |
| 138 | { GetFirstClient, "GetFirstClient" }, |
| 139 | { GetFirstRegion, "GetFirstRegion" }, |
| 140 | { GetFirstTuple, "GetFirstTuple" }, |
| 141 | { GetNextClient, "GetNextClient" }, |
| 142 | { GetNextRegion, "GetNextRegion" }, |
| 143 | { GetNextTuple, "GetNextTuple" }, |
| 144 | { GetStatus, "GetStatus" }, |
| 145 | { GetTupleData, "GetTupleData" }, |
| 146 | { MapMemPage, "MapMemPage" }, |
| 147 | { ModifyConfiguration, "ModifyConfiguration" }, |
| 148 | { ModifyWindow, "ModifyWindow" }, |
| 149 | { OpenMemory, "OpenMemory" }, |
| 150 | { ParseTuple, "ParseTuple" }, |
| 151 | { ReadMemory, "ReadMemory" }, |
| 152 | { RegisterClient, "RegisterClient" }, |
| 153 | { RegisterEraseQueue, "RegisterEraseQueue" }, |
| 154 | { RegisterMTD, "RegisterMTD" }, |
| 155 | { ReleaseConfiguration, "ReleaseConfiguration" }, |
| 156 | { ReleaseIO, "ReleaseIO" }, |
| 157 | { ReleaseIRQ, "ReleaseIRQ" }, |
| 158 | { ReleaseWindow, "ReleaseWindow" }, |
| 159 | { RequestConfiguration, "RequestConfiguration" }, |
| 160 | { RequestIO, "RequestIO" }, |
| 161 | { RequestIRQ, "RequestIRQ" }, |
| 162 | { RequestSocketMask, "RequestSocketMask" }, |
| 163 | { RequestWindow, "RequestWindow" }, |
| 164 | { ResetCard, "ResetCard" }, |
| 165 | { SetEventMask, "SetEventMask" }, |
| 166 | { ValidateCIS, "ValidateCIS" }, |
| 167 | { WriteMemory, "WriteMemory" }, |
| 168 | { BindDevice, "BindDevice" }, |
| 169 | { BindMTD, "BindMTD" }, |
| 170 | { ReportError, "ReportError" }, |
| 171 | { SuspendCard, "SuspendCard" }, |
| 172 | { ResumeCard, "ResumeCard" }, |
| 173 | { EjectCard, "EjectCard" }, |
| 174 | { InsertCard, "InsertCard" }, |
| 175 | { ReplaceCIS, "ReplaceCIS" } |
| 176 | }; |
| 177 | |
| 178 | |
Dominik Brodowski | e94e15f | 2005-06-27 16:28:15 -0700 | [diff] [blame] | 179 | 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] | 180 | { |
| 181 | int i; |
| 182 | char *serv; |
| 183 | |
| 184 | if (CHECK_HANDLE(handle)) |
| 185 | printk(KERN_NOTICE); |
| 186 | else { |
| 187 | struct pcmcia_device *p_dev = handle_to_pdev(handle); |
| 188 | printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id); |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < ARRAY_SIZE(service_table); i++) |
| 192 | if (service_table[i].key == err->func) |
| 193 | break; |
| 194 | if (i < ARRAY_SIZE(service_table)) |
| 195 | serv = service_table[i].msg; |
| 196 | else |
| 197 | serv = "Unknown service number"; |
| 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(error_table); i++) |
| 200 | if (error_table[i].key == err->retcode) |
| 201 | break; |
| 202 | if (i < ARRAY_SIZE(error_table)) |
| 203 | printk("%s: %s\n", serv, error_table[i].msg); |
| 204 | else |
| 205 | printk("%s: Unknown error code %#x\n", serv, err->retcode); |
| 206 | |
| 207 | return CS_SUCCESS; |
| 208 | } /* report_error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | /* end of code which was in cs.c before */ |
| 211 | |
| 212 | /*======================================================================*/ |
| 213 | |
| 214 | void cs_error(client_handle_t handle, int func, int ret) |
| 215 | { |
| 216 | error_info_t err = { func, ret }; |
| 217 | pcmcia_report_error(handle, &err); |
| 218 | } |
| 219 | EXPORT_SYMBOL(cs_error); |
| 220 | |
Dominik Brodowski | 23a83bf | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 221 | |
| 222 | static void pcmcia_check_driver(struct pcmcia_driver *p_drv) |
| 223 | { |
| 224 | struct pcmcia_device_id *did = p_drv->id_table; |
| 225 | unsigned int i; |
| 226 | u32 hash; |
| 227 | |
| 228 | while (did && did->match_flags) { |
| 229 | for (i=0; i<4; i++) { |
| 230 | if (!did->prod_id[i]) |
| 231 | continue; |
| 232 | |
| 233 | hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i])); |
| 234 | if (hash == did->prod_id_hash[i]) |
| 235 | continue; |
| 236 | |
| 237 | printk(KERN_DEBUG "pcmcia: %s: invalid hash for " |
| 238 | "product string \"%s\": is 0x%x, should " |
| 239 | "be 0x%x\n", p_drv->drv.name, did->prod_id[i], |
| 240 | did->prod_id_hash[i], hash); |
Dominik Brodowski | 5085cb2 | 2005-06-27 16:28:45 -0700 | [diff] [blame] | 241 | printk(KERN_DEBUG "pcmcia: see " |
| 242 | "Documentation/pcmcia/devicetable.txt for " |
| 243 | "details\n"); |
Dominik Brodowski | 23a83bf | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 244 | } |
| 245 | did++; |
| 246 | } |
| 247 | |
| 248 | return; |
| 249 | } |
| 250 | |
Dominik Brodowski | daa9517 | 2005-06-27 16:28:14 -0700 | [diff] [blame] | 251 | |
| 252 | #ifdef CONFIG_PCMCIA_LOAD_CIS |
| 253 | |
| 254 | /** |
| 255 | * pcmcia_load_firmware - load CIS from userspace if device-provided is broken |
| 256 | * @dev - the pcmcia device which needs a CIS override |
| 257 | * @filename - requested filename in /lib/firmware/cis/ |
| 258 | * |
| 259 | * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if |
| 260 | * the one provided by the card is broken. The firmware files reside in |
| 261 | * /lib/firmware/cis/ in userspace. |
| 262 | */ |
| 263 | static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) |
| 264 | { |
| 265 | struct pcmcia_socket *s = dev->socket; |
| 266 | const struct firmware *fw; |
| 267 | char path[20]; |
| 268 | int ret=-ENOMEM; |
| 269 | cisdump_t *cis; |
| 270 | |
| 271 | if (!filename) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | ds_dbg(1, "trying to load firmware %s\n", filename); |
| 275 | |
| 276 | if (strlen(filename) > 14) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | snprintf(path, 20, "%s", filename); |
| 280 | |
| 281 | if (request_firmware(&fw, path, &dev->dev) == 0) { |
| 282 | if (fw->size >= CISTPL_MAX_CIS_SIZE) |
| 283 | goto release; |
| 284 | |
| 285 | cis = kmalloc(sizeof(cisdump_t), GFP_KERNEL); |
| 286 | if (!cis) |
| 287 | goto release; |
| 288 | |
| 289 | memset(cis, 0, sizeof(cisdump_t)); |
| 290 | |
| 291 | cis->Length = fw->size + 1; |
| 292 | memcpy(cis->Data, fw->data, fw->size); |
| 293 | |
| 294 | if (!pcmcia_replace_cis(s, cis)) |
| 295 | ret = 0; |
| 296 | } |
| 297 | release: |
| 298 | release_firmware(fw); |
| 299 | |
| 300 | return (ret); |
| 301 | } |
| 302 | |
| 303 | #else /* !CONFIG_PCMCIA_LOAD_CIS */ |
| 304 | |
| 305 | static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename) |
| 306 | { |
| 307 | return -ENODEV; |
| 308 | } |
| 309 | |
| 310 | #endif |
| 311 | |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /*======================================================================*/ |
| 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 316 | void pcmcia_release_bus_socket(struct kref *refcount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | struct pcmcia_bus_socket *s = container_of(refcount, struct pcmcia_bus_socket, refcount); |
| 319 | pcmcia_put_socket(s->parent); |
| 320 | kfree(s); |
| 321 | } |
| 322 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 323 | void pcmcia_put_bus_socket(struct pcmcia_bus_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | kref_put(&s->refcount, pcmcia_release_bus_socket); |
| 326 | } |
| 327 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 328 | struct pcmcia_bus_socket *pcmcia_get_bus_socket(struct pcmcia_bus_socket *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | kref_get(&s->refcount); |
| 331 | return (s); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * pcmcia_register_driver - register a PCMCIA driver with the bus core |
| 336 | * |
| 337 | * Registers a PCMCIA driver with the PCMCIA bus core. |
| 338 | */ |
| 339 | static int pcmcia_device_probe(struct device *dev); |
| 340 | static int pcmcia_device_remove(struct device * dev); |
| 341 | |
| 342 | int pcmcia_register_driver(struct pcmcia_driver *driver) |
| 343 | { |
| 344 | if (!driver) |
| 345 | return -EINVAL; |
| 346 | |
Dominik Brodowski | 23a83bf | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 347 | pcmcia_check_driver(driver); |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* initialize common fields */ |
| 350 | driver->drv.bus = &pcmcia_bus_type; |
| 351 | driver->drv.owner = driver->owner; |
| 352 | driver->drv.probe = pcmcia_device_probe; |
| 353 | driver->drv.remove = pcmcia_device_remove; |
| 354 | |
| 355 | return driver_register(&driver->drv); |
| 356 | } |
| 357 | EXPORT_SYMBOL(pcmcia_register_driver); |
| 358 | |
| 359 | /** |
| 360 | * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core |
| 361 | */ |
| 362 | void pcmcia_unregister_driver(struct pcmcia_driver *driver) |
| 363 | { |
| 364 | driver_unregister(&driver->drv); |
| 365 | } |
| 366 | EXPORT_SYMBOL(pcmcia_unregister_driver); |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | /* pcmcia_device handling */ |
| 370 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 371 | struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct device *tmp_dev; |
| 374 | tmp_dev = get_device(&p_dev->dev); |
| 375 | if (!tmp_dev) |
| 376 | return NULL; |
| 377 | return to_pcmcia_dev(tmp_dev); |
| 378 | } |
| 379 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 380 | void pcmcia_put_dev(struct pcmcia_device *p_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | if (p_dev) |
| 383 | put_device(&p_dev->dev); |
| 384 | } |
| 385 | |
| 386 | static void pcmcia_release_dev(struct device *dev) |
| 387 | { |
| 388 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 389 | ds_dbg(1, "releasing dev %p\n", p_dev); |
| 390 | pcmcia_put_bus_socket(p_dev->socket->pcmcia); |
| 391 | kfree(p_dev); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | static int pcmcia_device_probe(struct device * dev) |
| 396 | { |
| 397 | struct pcmcia_device *p_dev; |
| 398 | struct pcmcia_driver *p_drv; |
| 399 | int ret = 0; |
| 400 | |
| 401 | dev = get_device(dev); |
| 402 | if (!dev) |
| 403 | return -ENODEV; |
| 404 | |
| 405 | p_dev = to_pcmcia_dev(dev); |
| 406 | p_drv = to_pcmcia_drv(dev->driver); |
| 407 | |
| 408 | if (!try_module_get(p_drv->owner)) { |
| 409 | ret = -EINVAL; |
| 410 | goto put_dev; |
| 411 | } |
| 412 | |
| 413 | if (p_drv->attach) { |
| 414 | p_dev->instance = p_drv->attach(); |
| 415 | if ((!p_dev->instance) || (p_dev->client.state & CLIENT_UNBOUND)) { |
| 416 | printk(KERN_NOTICE "ds: unable to create instance " |
| 417 | "of '%s'!\n", p_drv->drv.name); |
| 418 | ret = -EINVAL; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (ret) |
| 423 | module_put(p_drv->owner); |
| 424 | put_dev: |
| 425 | if ((ret) || !(p_drv->attach)) |
| 426 | put_device(dev); |
| 427 | return (ret); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | static int pcmcia_device_remove(struct device * dev) |
| 432 | { |
| 433 | struct pcmcia_device *p_dev; |
| 434 | struct pcmcia_driver *p_drv; |
| 435 | |
| 436 | /* detach the "instance" */ |
| 437 | p_dev = to_pcmcia_dev(dev); |
| 438 | p_drv = to_pcmcia_drv(dev->driver); |
| 439 | |
| 440 | if (p_drv) { |
| 441 | if ((p_drv->detach) && (p_dev->instance)) { |
| 442 | p_drv->detach(p_dev->instance); |
| 443 | /* from pcmcia_probe_device */ |
| 444 | put_device(&p_dev->dev); |
| 445 | } |
| 446 | module_put(p_drv->owner); |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
| 454 | /* |
| 455 | * pcmcia_device_query -- determine information about a pcmcia device |
| 456 | */ |
| 457 | static int pcmcia_device_query(struct pcmcia_device *p_dev) |
| 458 | { |
| 459 | cistpl_manfid_t manf_id; |
| 460 | cistpl_funcid_t func_id; |
| 461 | cistpl_vers_1_t vers1; |
| 462 | unsigned int i; |
| 463 | |
| 464 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 465 | CISTPL_MANFID, &manf_id)) { |
| 466 | p_dev->manf_id = manf_id.manf; |
| 467 | p_dev->card_id = manf_id.card; |
| 468 | p_dev->has_manf_id = 1; |
| 469 | p_dev->has_card_id = 1; |
| 470 | } |
| 471 | |
| 472 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 473 | CISTPL_FUNCID, &func_id)) { |
| 474 | p_dev->func_id = func_id.func; |
| 475 | p_dev->has_func_id = 1; |
| 476 | } else { |
| 477 | /* rule of thumb: cards with no FUNCID, but with |
| 478 | * common memory device geometry information, are |
| 479 | * probably memory cards (from pcmcia-cs) */ |
| 480 | cistpl_device_geo_t devgeo; |
| 481 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, |
| 482 | CISTPL_DEVICE_GEO, &devgeo)) { |
| 483 | ds_dbg(0, "mem device geometry probably means " |
| 484 | "FUNCID_MEMORY\n"); |
| 485 | p_dev->func_id = CISTPL_FUNCID_MEMORY; |
| 486 | p_dev->has_func_id = 1; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1, |
| 491 | &vers1)) { |
| 492 | for (i=0; i < vers1.ns; i++) { |
| 493 | char *tmp; |
| 494 | unsigned int length; |
| 495 | |
| 496 | tmp = vers1.str + vers1.ofs[i]; |
| 497 | |
| 498 | length = strlen(tmp) + 1; |
| 499 | if ((length < 3) || (length > 255)) |
| 500 | continue; |
| 501 | |
| 502 | p_dev->prod_id[i] = kmalloc(sizeof(char) * length, |
| 503 | GFP_KERNEL); |
| 504 | if (!p_dev->prod_id[i]) |
| 505 | continue; |
| 506 | |
| 507 | p_dev->prod_id[i] = strncpy(p_dev->prod_id[i], |
| 508 | tmp, length); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* device_add_lock is needed to avoid double registration by cardmgr and kernel. |
| 517 | * Serializes pcmcia_device_add; will most likely be removed in future. |
| 518 | * |
| 519 | * While it has the caveat that adding new PCMCIA devices inside(!) device_register() |
| 520 | * won't work, this doesn't matter much at the moment: the driver core doesn't |
| 521 | * support it either. |
| 522 | */ |
| 523 | static DECLARE_MUTEX(device_add_lock); |
| 524 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 525 | struct pcmcia_device * pcmcia_device_add(struct pcmcia_bus_socket *s, unsigned int function) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
| 527 | struct pcmcia_device *p_dev; |
| 528 | unsigned long flags; |
| 529 | |
| 530 | s = pcmcia_get_bus_socket(s); |
| 531 | if (!s) |
| 532 | return NULL; |
| 533 | |
| 534 | down(&device_add_lock); |
| 535 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 536 | /* max of 2 devices per card */ |
| 537 | if (s->device_count == 2) |
| 538 | goto err_put; |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL); |
| 541 | if (!p_dev) |
| 542 | goto err_put; |
| 543 | memset(p_dev, 0, sizeof(struct pcmcia_device)); |
| 544 | |
| 545 | p_dev->socket = s->parent; |
| 546 | p_dev->device_no = (s->device_count++); |
| 547 | p_dev->func = function; |
| 548 | |
| 549 | p_dev->dev.bus = &pcmcia_bus_type; |
| 550 | p_dev->dev.parent = s->parent->dev.dev; |
| 551 | p_dev->dev.release = pcmcia_release_dev; |
| 552 | sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no); |
| 553 | |
| 554 | /* compat */ |
| 555 | p_dev->client.client_magic = CLIENT_MAGIC; |
| 556 | p_dev->client.Socket = s->parent; |
| 557 | p_dev->client.Function = function; |
| 558 | p_dev->client.state = CLIENT_UNBOUND; |
| 559 | |
| 560 | /* Add to the list in pcmcia_bus_socket */ |
| 561 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 562 | list_add_tail(&p_dev->socket_device_list, &s->devices_list); |
| 563 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 564 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 565 | pcmcia_device_query(p_dev); |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (device_register(&p_dev->dev)) { |
| 568 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 569 | list_del(&p_dev->socket_device_list); |
| 570 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 571 | |
| 572 | goto err_free; |
| 573 | } |
| 574 | |
| 575 | up(&device_add_lock); |
| 576 | |
| 577 | return p_dev; |
| 578 | |
| 579 | err_free: |
| 580 | kfree(p_dev); |
| 581 | s->device_count--; |
| 582 | err_put: |
| 583 | up(&device_add_lock); |
| 584 | pcmcia_put_bus_socket(s); |
| 585 | |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | static int pcmcia_card_add(struct pcmcia_socket *s) |
| 591 | { |
| 592 | cisinfo_t cisinfo; |
| 593 | cistpl_longlink_mfc_t mfc; |
| 594 | unsigned int no_funcs, i; |
| 595 | int ret = 0; |
| 596 | |
| 597 | if (!(s->resource_setup_done)) |
| 598 | return -EAGAIN; /* try again, but later... */ |
| 599 | |
| 600 | pcmcia_validate_mem(s); |
| 601 | ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo); |
| 602 | if (ret || !cisinfo.Chains) { |
| 603 | ds_dbg(0, "invalid CIS or invalid resources\n"); |
| 604 | return -ENODEV; |
| 605 | } |
| 606 | |
| 607 | if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc)) |
| 608 | no_funcs = mfc.nfn; |
| 609 | else |
| 610 | no_funcs = 1; |
| 611 | |
| 612 | /* this doesn't handle multifunction devices on one pcmcia function |
| 613 | * yet. */ |
| 614 | for (i=0; i < no_funcs; i++) |
| 615 | pcmcia_device_add(s->pcmcia, i); |
| 616 | |
| 617 | return (ret); |
| 618 | } |
| 619 | |
| 620 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 621 | static void pcmcia_delayed_add_pseudo_device(void *data) |
| 622 | { |
| 623 | struct pcmcia_bus_socket *s = data; |
| 624 | pcmcia_device_add(s, 0); |
| 625 | s->device_add_pending = 0; |
| 626 | } |
| 627 | |
| 628 | static inline void pcmcia_add_pseudo_device(struct pcmcia_bus_socket *s) |
| 629 | { |
| 630 | if (!s->device_add_pending) { |
| 631 | schedule_work(&s->device_add); |
| 632 | s->device_add_pending = 1; |
| 633 | } |
| 634 | return; |
| 635 | } |
| 636 | |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 637 | static int pcmcia_requery(struct device *dev, void * _data) |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 638 | { |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 639 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 640 | if (!p_dev->dev.driver) |
| 641 | pcmcia_device_query(p_dev); |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static void pcmcia_bus_rescan(struct pcmcia_socket *skt) |
| 647 | { |
| 648 | int no_devices=0; |
| 649 | unsigned long flags; |
| 650 | |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 651 | /* must be called with skt_sem held */ |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 652 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 653 | if (list_empty(&skt->pcmcia->devices_list)) |
| 654 | no_devices=1; |
| 655 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 656 | |
| 657 | /* if no devices were added for this socket yet because of |
| 658 | * missing resource information or other trouble, we need to |
| 659 | * do this now. */ |
| 660 | if (no_devices) { |
| 661 | int ret = pcmcia_card_add(skt); |
| 662 | if (ret) |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | /* some device information might have changed because of a CIS |
| 667 | * update or because we can finally read it correctly... so |
| 668 | * determine it again, overwriting old values if necessary. */ |
| 669 | bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery); |
| 670 | |
| 671 | /* we re-scan all devices, not just the ones connected to this |
| 672 | * socket. This does not matter, though. */ |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 673 | bus_rescan_devices(&pcmcia_bus_type); |
Dominik Brodowski | ff1fa9e | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 674 | } |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 675 | |
| 676 | static inline int pcmcia_devmatch(struct pcmcia_device *dev, |
| 677 | struct pcmcia_device_id *did) |
| 678 | { |
| 679 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) { |
| 680 | if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id)) |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) { |
| 685 | if ((!dev->has_card_id) || (dev->card_id != did->card_id)) |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) { |
| 690 | if (dev->func != did->function) |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) { |
| 695 | if (!dev->prod_id[0]) |
| 696 | return 0; |
| 697 | if (strcmp(did->prod_id[0], dev->prod_id[0])) |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) { |
| 702 | if (!dev->prod_id[1]) |
| 703 | return 0; |
| 704 | if (strcmp(did->prod_id[1], dev->prod_id[1])) |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) { |
| 709 | if (!dev->prod_id[2]) |
| 710 | return 0; |
| 711 | if (strcmp(did->prod_id[2], dev->prod_id[2])) |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) { |
| 716 | if (!dev->prod_id[3]) |
| 717 | return 0; |
| 718 | if (strcmp(did->prod_id[3], dev->prod_id[3])) |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) { |
| 723 | /* handle pseudo multifunction devices: |
| 724 | * there are at most two pseudo multifunction devices. |
| 725 | * if we're matching against the first, schedule a |
| 726 | * call which will then check whether there are two |
| 727 | * pseudo devices, and if not, add the second one. |
| 728 | */ |
| 729 | if (dev->device_no == 0) |
| 730 | pcmcia_add_pseudo_device(dev->socket->pcmcia); |
| 731 | |
| 732 | if (dev->device_no != did->device_no) |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) { |
| 737 | if ((!dev->has_func_id) || (dev->func_id != did->func_id)) |
| 738 | return 0; |
| 739 | |
| 740 | /* if this is a pseudo-multi-function device, |
| 741 | * we need explicit matches */ |
| 742 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) |
| 743 | return 0; |
| 744 | if (dev->device_no) |
| 745 | return 0; |
| 746 | |
| 747 | /* also, FUNC_ID matching needs to be activated by userspace |
| 748 | * after it has re-checked that there is no possible module |
| 749 | * with a prod_id/manf_id/card_id match. |
| 750 | */ |
| 751 | if (!dev->allow_func_id_match) |
| 752 | return 0; |
| 753 | } |
| 754 | |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 755 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) { |
Dominik Brodowski | daa9517 | 2005-06-27 16:28:14 -0700 | [diff] [blame] | 756 | if (!dev->socket->fake_cis) |
| 757 | pcmcia_load_firmware(dev, did->cisfile); |
| 758 | |
| 759 | if (!dev->socket->fake_cis) |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 760 | return 0; |
Dominik Brodowski | ea7b388 | 2005-06-27 16:28:07 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Dominik Brodowski | f602ff7 | 2005-06-27 16:28:09 -0700 | [diff] [blame] | 763 | if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) { |
| 764 | int i; |
| 765 | for (i=0; i<4; i++) |
| 766 | if (dev->prod_id[i]) |
| 767 | return 0; |
| 768 | if (dev->has_manf_id || dev->has_card_id || dev->has_func_id) |
| 769 | return 0; |
| 770 | } |
| 771 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 772 | dev->dev.driver_data = (void *) did; |
| 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) { |
| 779 | struct pcmcia_device * p_dev = to_pcmcia_dev(dev); |
| 780 | struct pcmcia_driver * p_drv = to_pcmcia_drv(drv); |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 781 | struct pcmcia_device_id *did = p_drv->id_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | /* matching by cardmgr */ |
| 784 | if (p_dev->cardmgr == p_drv) |
| 785 | return 1; |
| 786 | |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 787 | while (did && did->match_flags) { |
| 788 | if (pcmcia_devmatch(p_dev, did)) |
| 789 | return 1; |
| 790 | did++; |
| 791 | } |
| 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
Dominik Brodowski | 840c2ac | 2005-06-27 16:28:04 -0700 | [diff] [blame] | 796 | #ifdef CONFIG_HOTPLUG |
| 797 | |
| 798 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, |
| 799 | char *buffer, int buffer_size) |
| 800 | { |
| 801 | struct pcmcia_device *p_dev; |
| 802 | int i, length = 0; |
| 803 | u32 hash[4] = { 0, 0, 0, 0}; |
| 804 | |
| 805 | if (!dev) |
| 806 | return -ENODEV; |
| 807 | |
| 808 | p_dev = to_pcmcia_dev(dev); |
| 809 | |
| 810 | /* calculate hashes */ |
| 811 | for (i=0; i<4; i++) { |
| 812 | if (!p_dev->prod_id[i]) |
| 813 | continue; |
| 814 | hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i])); |
| 815 | } |
| 816 | |
| 817 | i = 0; |
| 818 | |
| 819 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 820 | buffer, buffer_size, &length, |
| 821 | "SOCKET_NO=%u", |
| 822 | p_dev->socket->sock)) |
| 823 | return -ENOMEM; |
| 824 | |
| 825 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 826 | buffer, buffer_size, &length, |
| 827 | "DEVICE_NO=%02X", |
| 828 | p_dev->device_no)) |
| 829 | return -ENOMEM; |
| 830 | |
| 831 | if (add_hotplug_env_var(envp, num_envp, &i, |
| 832 | buffer, buffer_size, &length, |
| 833 | "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X" |
| 834 | "pa%08Xpb%08Xpc%08Xpd%08X", |
| 835 | p_dev->has_manf_id ? p_dev->manf_id : 0, |
| 836 | p_dev->has_card_id ? p_dev->card_id : 0, |
| 837 | p_dev->has_func_id ? p_dev->func_id : 0, |
| 838 | p_dev->func, |
| 839 | p_dev->device_no, |
| 840 | hash[0], |
| 841 | hash[1], |
| 842 | hash[2], |
| 843 | hash[3])) |
| 844 | return -ENOMEM; |
| 845 | |
| 846 | envp[i] = NULL; |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | #else |
| 852 | |
| 853 | static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp, |
| 854 | char *buffer, int buffer_size) |
| 855 | { |
| 856 | return -ENODEV; |
| 857 | } |
| 858 | |
| 859 | #endif |
| 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | /************************ per-device sysfs output ***************************/ |
| 862 | |
| 863 | #define pcmcia_device_attr(field, test, format) \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 864 | 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] | 865 | { \ |
| 866 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ |
| 867 | return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \ |
| 868 | } |
| 869 | |
| 870 | #define pcmcia_device_stringattr(name, field) \ |
Yani Ioannou | e404e27 | 2005-05-17 06:42:58 -0400 | [diff] [blame] | 871 | 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] | 872 | { \ |
| 873 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \ |
| 874 | return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \ |
| 875 | } |
| 876 | |
| 877 | pcmcia_device_attr(func, socket, "0x%02x\n"); |
| 878 | pcmcia_device_attr(func_id, has_func_id, "0x%02x\n"); |
| 879 | pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n"); |
| 880 | pcmcia_device_attr(card_id, has_card_id, "0x%04x\n"); |
| 881 | pcmcia_device_stringattr(prod_id1, prod_id[0]); |
| 882 | pcmcia_device_stringattr(prod_id2, prod_id[1]); |
| 883 | pcmcia_device_stringattr(prod_id3, prod_id[2]); |
| 884 | pcmcia_device_stringattr(prod_id4, prod_id[3]); |
| 885 | |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 886 | |
| 887 | static ssize_t pcmcia_store_allow_func_id_match (struct device * dev, struct device_attribute *attr, |
| 888 | const char * buf, size_t count) |
| 889 | { |
| 890 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 891 | if (!count) |
| 892 | return -EINVAL; |
| 893 | |
| 894 | down(&p_dev->socket->skt_sem); |
| 895 | p_dev->allow_func_id_match = 1; |
| 896 | up(&p_dev->socket->skt_sem); |
| 897 | |
| 898 | bus_rescan_devices(&pcmcia_bus_type); |
| 899 | |
| 900 | return count; |
| 901 | } |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | static struct device_attribute pcmcia_dev_attrs[] = { |
| 904 | __ATTR(function, 0444, func_show, NULL), |
| 905 | __ATTR_RO(func_id), |
| 906 | __ATTR_RO(manf_id), |
| 907 | __ATTR_RO(card_id), |
| 908 | __ATTR_RO(prod_id1), |
| 909 | __ATTR_RO(prod_id2), |
| 910 | __ATTR_RO(prod_id3), |
| 911 | __ATTR_RO(prod_id4), |
Dominik Brodowski | a5b5577 | 2005-06-27 16:28:10 -0700 | [diff] [blame] | 912 | __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] | 913 | __ATTR_NULL, |
| 914 | }; |
| 915 | |
| 916 | |
| 917 | /*====================================================================== |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | The card status event handler. |
| 920 | |
| 921 | ======================================================================*/ |
| 922 | |
| 923 | struct send_event_data { |
| 924 | struct pcmcia_socket *skt; |
| 925 | event_t event; |
| 926 | int priority; |
| 927 | }; |
| 928 | |
| 929 | static int send_event_callback(struct device *dev, void * _data) |
| 930 | { |
| 931 | struct pcmcia_device *p_dev = to_pcmcia_dev(dev); |
| 932 | struct send_event_data *data = _data; |
| 933 | |
| 934 | /* we get called for all sockets, but may only pass the event |
| 935 | * for drivers _on the affected socket_ */ |
| 936 | if (p_dev->socket != data->skt) |
| 937 | return 0; |
| 938 | |
| 939 | if (p_dev->client.state & (CLIENT_UNBOUND|CLIENT_STALE)) |
| 940 | return 0; |
| 941 | |
| 942 | if (p_dev->client.EventMask & data->event) |
| 943 | return EVENT(&p_dev->client, data->event, data->priority); |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static int send_event(struct pcmcia_socket *s, event_t event, int priority) |
| 949 | { |
| 950 | int ret = 0; |
| 951 | struct send_event_data private; |
| 952 | struct pcmcia_bus_socket *skt = pcmcia_get_bus_socket(s->pcmcia); |
| 953 | |
| 954 | if (!skt) |
| 955 | return 0; |
| 956 | |
| 957 | private.skt = s; |
| 958 | private.event = event; |
| 959 | private.priority = priority; |
| 960 | |
| 961 | ret = bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback); |
| 962 | |
| 963 | pcmcia_put_bus_socket(skt); |
| 964 | return ret; |
| 965 | } /* send_event */ |
| 966 | |
| 967 | |
| 968 | /* Normally, the event is passed to individual drivers after |
| 969 | * informing userspace. Only for CS_EVENT_CARD_REMOVAL this |
| 970 | * is inversed to maintain historic compatibility. |
| 971 | */ |
| 972 | |
| 973 | static int ds_event(struct pcmcia_socket *skt, event_t event, int priority) |
| 974 | { |
| 975 | struct pcmcia_bus_socket *s = skt->pcmcia; |
| 976 | int ret = 0; |
| 977 | |
| 978 | ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n", |
| 979 | event, priority, s); |
| 980 | |
| 981 | switch (event) { |
| 982 | |
| 983 | case CS_EVENT_CARD_REMOVAL: |
| 984 | s->state &= ~DS_SOCKET_PRESENT; |
| 985 | send_event(skt, event, priority); |
| 986 | unbind_request(s); |
| 987 | handle_event(s, event); |
| 988 | break; |
| 989 | |
| 990 | case CS_EVENT_CARD_INSERTION: |
| 991 | s->state |= DS_SOCKET_PRESENT; |
| 992 | pcmcia_card_add(skt); |
| 993 | handle_event(s, event); |
| 994 | break; |
| 995 | |
| 996 | case CS_EVENT_EJECTION_REQUEST: |
| 997 | ret = send_event(skt, event, priority); |
| 998 | break; |
| 999 | |
| 1000 | default: |
| 1001 | handle_event(s, event); |
| 1002 | send_event(skt, event, priority); |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | return 0; |
| 1007 | } /* ds_event */ |
| 1008 | |
| 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | int pcmcia_register_client(client_handle_t *handle, client_reg_t *req) |
| 1012 | { |
| 1013 | client_t *client = NULL; |
| 1014 | struct pcmcia_socket *s; |
| 1015 | struct pcmcia_bus_socket *skt = NULL; |
| 1016 | struct pcmcia_device *p_dev = NULL; |
| 1017 | |
| 1018 | /* Look for unbound client with matching dev_info */ |
| 1019 | down_read(&pcmcia_socket_list_rwsem); |
| 1020 | list_for_each_entry(s, &pcmcia_socket_list, socket_list) { |
| 1021 | unsigned long flags; |
| 1022 | |
| 1023 | if (s->state & SOCKET_CARDBUS) |
| 1024 | continue; |
| 1025 | |
| 1026 | skt = s->pcmcia; |
| 1027 | if (!skt) |
| 1028 | continue; |
| 1029 | skt = pcmcia_get_bus_socket(skt); |
| 1030 | if (!skt) |
| 1031 | continue; |
| 1032 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1033 | list_for_each_entry(p_dev, &skt->devices_list, socket_device_list) { |
| 1034 | struct pcmcia_driver *p_drv; |
| 1035 | p_dev = pcmcia_get_dev(p_dev); |
| 1036 | if (!p_dev) |
| 1037 | continue; |
| 1038 | if (!(p_dev->client.state & CLIENT_UNBOUND) || |
| 1039 | (!p_dev->dev.driver)) { |
| 1040 | pcmcia_put_dev(p_dev); |
| 1041 | continue; |
| 1042 | } |
| 1043 | p_drv = to_pcmcia_drv(p_dev->dev.driver); |
| 1044 | if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) { |
| 1045 | client = &p_dev->client; |
| 1046 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1047 | goto found; |
| 1048 | } |
| 1049 | pcmcia_put_dev(p_dev); |
| 1050 | } |
| 1051 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1052 | pcmcia_put_bus_socket(skt); |
| 1053 | } |
| 1054 | found: |
| 1055 | up_read(&pcmcia_socket_list_rwsem); |
| 1056 | if (!p_dev || !client) |
| 1057 | return -ENODEV; |
| 1058 | |
| 1059 | pcmcia_put_bus_socket(skt); /* safe, as we already hold a reference from bind_device */ |
| 1060 | |
| 1061 | *handle = client; |
| 1062 | client->state &= ~CLIENT_UNBOUND; |
| 1063 | client->Socket = s; |
| 1064 | client->EventMask = req->EventMask; |
| 1065 | client->event_handler = req->event_handler; |
| 1066 | client->event_callback_args = req->event_callback_args; |
| 1067 | client->event_callback_args.client_handle = client; |
| 1068 | |
| 1069 | if (s->state & SOCKET_CARDBUS) |
| 1070 | client->state |= CLIENT_CARDBUS; |
| 1071 | |
| 1072 | if ((!(s->state & SOCKET_CARDBUS)) && (s->functions == 0) && |
| 1073 | (client->Function != BIND_FN_ALL)) { |
| 1074 | cistpl_longlink_mfc_t mfc; |
| 1075 | if (pccard_read_tuple(s, client->Function, CISTPL_LONGLINK_MFC, &mfc) |
| 1076 | == CS_SUCCESS) |
| 1077 | s->functions = mfc.nfn; |
| 1078 | else |
| 1079 | s->functions = 1; |
| 1080 | s->config = kmalloc(sizeof(config_t) * s->functions, |
| 1081 | GFP_KERNEL); |
| 1082 | if (!s->config) |
| 1083 | goto out_no_resource; |
| 1084 | memset(s->config, 0, sizeof(config_t) * s->functions); |
| 1085 | } |
| 1086 | |
| 1087 | ds_dbg(1, "register_client(): client 0x%p, dev %s\n", |
| 1088 | client, p_dev->dev.bus_id); |
| 1089 | if (client->EventMask & CS_EVENT_REGISTRATION_COMPLETE) |
| 1090 | EVENT(client, CS_EVENT_REGISTRATION_COMPLETE, CS_EVENT_PRI_LOW); |
| 1091 | |
| 1092 | if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) { |
| 1093 | if (client->EventMask & CS_EVENT_CARD_INSERTION) |
| 1094 | EVENT(client, CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW); |
| 1095 | } |
| 1096 | |
| 1097 | return CS_SUCCESS; |
| 1098 | |
| 1099 | out_no_resource: |
| 1100 | pcmcia_put_dev(p_dev); |
| 1101 | return CS_OUT_OF_RESOURCE; |
| 1102 | } /* register_client */ |
| 1103 | EXPORT_SYMBOL(pcmcia_register_client); |
| 1104 | |
| 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* unbind _all_ devices attached to a given pcmcia_bus_socket. The |
| 1107 | * drivers have been called with EVENT_CARD_REMOVAL before. |
| 1108 | */ |
| 1109 | static int unbind_request(struct pcmcia_bus_socket *s) |
| 1110 | { |
| 1111 | struct pcmcia_device *p_dev; |
| 1112 | unsigned long flags; |
| 1113 | |
| 1114 | ds_dbg(2, "unbind_request(%d)\n", s->parent->sock); |
| 1115 | |
| 1116 | s->device_count = 0; |
| 1117 | |
| 1118 | for (;;) { |
| 1119 | /* unregister all pcmcia_devices registered with this socket*/ |
| 1120 | spin_lock_irqsave(&pcmcia_dev_list_lock, flags); |
| 1121 | if (list_empty(&s->devices_list)) { |
| 1122 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list); |
| 1126 | list_del(&p_dev->socket_device_list); |
| 1127 | p_dev->client.state |= CLIENT_STALE; |
| 1128 | spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags); |
| 1129 | |
| 1130 | device_unregister(&p_dev->dev); |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } /* unbind_request */ |
| 1135 | |
| 1136 | int pcmcia_deregister_client(client_handle_t handle) |
| 1137 | { |
| 1138 | struct pcmcia_socket *s; |
| 1139 | int i; |
| 1140 | struct pcmcia_device *p_dev = handle_to_pdev(handle); |
| 1141 | |
| 1142 | if (CHECK_HANDLE(handle)) |
| 1143 | return CS_BAD_HANDLE; |
| 1144 | |
| 1145 | s = SOCKET(handle); |
| 1146 | ds_dbg(1, "deregister_client(%p)\n", handle); |
| 1147 | |
| 1148 | if (handle->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED)) |
| 1149 | goto warn_out; |
| 1150 | for (i = 0; i < MAX_WIN; i++) |
| 1151 | if (handle->state & CLIENT_WIN_REQ(i)) |
| 1152 | goto warn_out; |
| 1153 | |
| 1154 | if (handle->state & CLIENT_STALE) { |
| 1155 | handle->client_magic = 0; |
| 1156 | handle->state &= ~CLIENT_STALE; |
| 1157 | pcmcia_put_dev(p_dev); |
| 1158 | } else { |
| 1159 | handle->state = CLIENT_UNBOUND; |
| 1160 | handle->event_handler = NULL; |
| 1161 | } |
| 1162 | |
| 1163 | return CS_SUCCESS; |
| 1164 | warn_out: |
| 1165 | printk(KERN_WARNING "ds: deregister_client was called too early.\n"); |
| 1166 | return CS_IN_USE; |
| 1167 | } /* deregister_client */ |
| 1168 | EXPORT_SYMBOL(pcmcia_deregister_client); |
| 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev) |
| 1171 | { |
| 1172 | struct pcmcia_socket *socket = class_get_devdata(class_dev); |
| 1173 | struct pcmcia_bus_socket *s; |
| 1174 | int ret; |
| 1175 | |
| 1176 | s = kmalloc(sizeof(struct pcmcia_bus_socket), GFP_KERNEL); |
| 1177 | if(!s) |
| 1178 | return -ENOMEM; |
| 1179 | memset(s, 0, sizeof(struct pcmcia_bus_socket)); |
| 1180 | |
| 1181 | /* get reference to parent socket */ |
| 1182 | s->parent = pcmcia_get_socket(socket); |
| 1183 | if (!s->parent) { |
| 1184 | printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket); |
| 1185 | kfree (s); |
| 1186 | return -ENODEV; |
| 1187 | } |
| 1188 | |
| 1189 | kref_init(&s->refcount); |
| 1190 | |
| 1191 | /* |
| 1192 | * Ugly. But we want to wait for the socket threads to have started up. |
| 1193 | * We really should let the drivers themselves drive some of this.. |
| 1194 | */ |
| 1195 | msleep(250); |
| 1196 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1197 | #ifdef CONFIG_PCMCIA_IOCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | init_waitqueue_head(&s->queue); |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1199 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | INIT_LIST_HEAD(&s->devices_list); |
Dominik Brodowski | 1ad275e | 2005-06-27 16:28:06 -0700 | [diff] [blame] | 1201 | INIT_WORK(&s->device_add, pcmcia_delayed_add_pseudo_device, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* Set up hotline to Card Services */ |
| 1204 | s->callback.owner = THIS_MODULE; |
| 1205 | s->callback.event = &ds_event; |
Dominik Brodowski | e2f0b53 | 2005-06-27 16:28:17 -0700 | [diff] [blame] | 1206 | s->callback.requery = &pcmcia_bus_rescan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | socket->pcmcia = s; |
| 1208 | |
| 1209 | ret = pccard_register_pcmcia(socket, &s->callback); |
| 1210 | if (ret) { |
| 1211 | printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket); |
| 1212 | pcmcia_put_bus_socket(s); |
| 1213 | socket->pcmcia = NULL; |
| 1214 | return (ret); |
| 1215 | } |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | static void pcmcia_bus_remove_socket(struct class_device *class_dev) |
| 1222 | { |
| 1223 | struct pcmcia_socket *socket = class_get_devdata(class_dev); |
| 1224 | |
| 1225 | if (!socket || !socket->pcmcia) |
| 1226 | return; |
| 1227 | |
| 1228 | pccard_register_pcmcia(socket, NULL); |
| 1229 | |
| 1230 | socket->pcmcia->state |= DS_SOCKET_DEAD; |
| 1231 | pcmcia_put_bus_socket(socket->pcmcia); |
| 1232 | socket->pcmcia = NULL; |
| 1233 | |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | /* the pcmcia_bus_interface is used to handle pcmcia socket devices */ |
| 1239 | static struct class_interface pcmcia_bus_interface = { |
| 1240 | .class = &pcmcia_socket_class, |
| 1241 | .add = &pcmcia_bus_add_socket, |
| 1242 | .remove = &pcmcia_bus_remove_socket, |
| 1243 | }; |
| 1244 | |
| 1245 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1246 | struct bus_type pcmcia_bus_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | .name = "pcmcia", |
Dominik Brodowski | 840c2ac | 2005-06-27 16:28:04 -0700 | [diff] [blame] | 1248 | .hotplug = pcmcia_bus_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | .match = pcmcia_bus_match, |
| 1250 | .dev_attrs = pcmcia_dev_attrs, |
| 1251 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
| 1253 | |
| 1254 | static int __init init_pcmcia_bus(void) |
| 1255 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | spin_lock_init(&pcmcia_dev_list_lock); |
| 1257 | |
| 1258 | bus_register(&pcmcia_bus_type); |
| 1259 | class_interface_register(&pcmcia_bus_interface); |
| 1260 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1261 | pcmcia_setup_ioctl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that |
| 1266 | * pcmcia_socket_class is already registered */ |
| 1267 | |
| 1268 | |
| 1269 | static void __exit exit_pcmcia_bus(void) |
| 1270 | { |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1271 | pcmcia_cleanup_ioctl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
Dominik Brodowski | e7a480d | 2005-06-27 16:28:47 -0700 | [diff] [blame^] | 1273 | class_interface_unregister(&pcmcia_bus_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | |
| 1275 | bus_unregister(&pcmcia_bus_type); |
| 1276 | } |
| 1277 | module_exit(exit_pcmcia_bus); |
| 1278 | |
| 1279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | MODULE_ALIAS("ds"); |