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