ds.c 34 KB

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