ds.c 33 KB

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