ds.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. int ret = 0;
  268. dev = get_device(dev);
  269. if (!dev)
  270. return -ENODEV;
  271. p_dev = to_pcmcia_dev(dev);
  272. p_drv = to_pcmcia_drv(dev->driver);
  273. s = p_dev->socket;
  274. if ((!p_drv->probe) || (!p_dev->function_config) ||
  275. (!try_module_get(p_drv->owner))) {
  276. ret = -EINVAL;
  277. goto put_dev;
  278. }
  279. ret = p_drv->probe(p_dev);
  280. if (ret)
  281. goto put_module;
  282. /* handle pseudo multifunction devices:
  283. * there are at most two pseudo multifunction devices.
  284. * if we're matching against the first, schedule a
  285. * call which will then check whether there are two
  286. * pseudo devices, and if not, add the second one.
  287. */
  288. did = p_dev->dev.driver_data;
  289. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  290. (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
  291. pcmcia_add_device_later(p_dev->socket, 0);
  292. put_module:
  293. if (ret)
  294. module_put(p_drv->owner);
  295. put_dev:
  296. if (ret)
  297. put_device(dev);
  298. return (ret);
  299. }
  300. /*
  301. * Removes a PCMCIA card from the device tree and socket list.
  302. */
  303. static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
  304. {
  305. struct pcmcia_device *p_dev;
  306. struct pcmcia_device *tmp;
  307. unsigned long flags;
  308. ds_dbg(2, "unbind_request(%d)\n", s->sock);
  309. if (!leftover)
  310. s->device_count = 0;
  311. else
  312. s->device_count = 1;
  313. /* unregister all pcmcia_devices registered with this socket, except leftover */
  314. list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
  315. if (p_dev == leftover)
  316. continue;
  317. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  318. list_del(&p_dev->socket_device_list);
  319. p_dev->_removed=1;
  320. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  321. device_unregister(&p_dev->dev);
  322. }
  323. return;
  324. }
  325. static int pcmcia_device_remove(struct device * dev)
  326. {
  327. struct pcmcia_device *p_dev;
  328. struct pcmcia_driver *p_drv;
  329. struct pcmcia_device_id *did;
  330. int i;
  331. p_dev = to_pcmcia_dev(dev);
  332. p_drv = to_pcmcia_drv(dev->driver);
  333. /* If we're removing the primary module driving a
  334. * pseudo multi-function card, we need to unbind
  335. * all devices
  336. */
  337. did = p_dev->dev.driver_data;
  338. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  339. (p_dev->socket->device_count != 0) &&
  340. (p_dev->device_no == 0))
  341. pcmcia_card_remove(p_dev->socket, p_dev);
  342. /* detach the "instance" */
  343. if (!p_drv)
  344. return 0;
  345. if (p_drv->remove)
  346. p_drv->remove(p_dev);
  347. p_dev->dev_node = NULL;
  348. /* check for proper unloading */
  349. if (p_dev->_irq || p_dev->_io || p_dev->_locked)
  350. printk(KERN_INFO "pcmcia: driver %s did not release config properly\n",
  351. p_drv->drv.name);
  352. for (i = 0; i < MAX_WIN; i++)
  353. if (p_dev->_win & CLIENT_WIN_REQ(i))
  354. printk(KERN_INFO "pcmcia: driver %s did not release windows properly\n",
  355. p_drv->drv.name);
  356. /* references from pcmcia_probe_device */
  357. pcmcia_put_dev(p_dev);
  358. module_put(p_drv->owner);
  359. return 0;
  360. }
  361. /*
  362. * pcmcia_device_query -- determine information about a pcmcia device
  363. */
  364. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  365. {
  366. cistpl_manfid_t manf_id;
  367. cistpl_funcid_t func_id;
  368. cistpl_vers_1_t *vers1;
  369. unsigned int i;
  370. vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
  371. if (!vers1)
  372. return -ENOMEM;
  373. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  374. CISTPL_MANFID, &manf_id)) {
  375. p_dev->manf_id = manf_id.manf;
  376. p_dev->card_id = manf_id.card;
  377. p_dev->has_manf_id = 1;
  378. p_dev->has_card_id = 1;
  379. }
  380. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  381. CISTPL_FUNCID, &func_id)) {
  382. p_dev->func_id = func_id.func;
  383. p_dev->has_func_id = 1;
  384. } else {
  385. /* rule of thumb: cards with no FUNCID, but with
  386. * common memory device geometry information, are
  387. * probably memory cards (from pcmcia-cs) */
  388. cistpl_device_geo_t *devgeo;
  389. devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
  390. if (!devgeo) {
  391. kfree(vers1);
  392. return -ENOMEM;
  393. }
  394. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  395. CISTPL_DEVICE_GEO, devgeo)) {
  396. ds_dbg(0, "mem device geometry probably means "
  397. "FUNCID_MEMORY\n");
  398. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  399. p_dev->has_func_id = 1;
  400. }
  401. kfree(devgeo);
  402. }
  403. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  404. vers1)) {
  405. for (i=0; i < vers1->ns; i++) {
  406. char *tmp;
  407. unsigned int length;
  408. tmp = vers1->str + vers1->ofs[i];
  409. length = strlen(tmp) + 1;
  410. if ((length < 2) || (length > 255))
  411. continue;
  412. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  413. GFP_KERNEL);
  414. if (!p_dev->prod_id[i])
  415. continue;
  416. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  417. tmp, length);
  418. }
  419. }
  420. kfree(vers1);
  421. return 0;
  422. }
  423. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  424. * Serializes pcmcia_device_add; will most likely be removed in future.
  425. *
  426. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  427. * won't work, this doesn't matter much at the moment: the driver core doesn't
  428. * support it either.
  429. */
  430. static DEFINE_MUTEX(device_add_lock);
  431. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  432. {
  433. struct pcmcia_device *p_dev, *tmp_dev;
  434. unsigned long flags;
  435. int bus_id_len;
  436. s = pcmcia_get_socket(s);
  437. if (!s)
  438. return NULL;
  439. mutex_lock(&device_add_lock);
  440. /* max of 4 devices per card */
  441. if (s->device_count == 4)
  442. goto err_put;
  443. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  444. if (!p_dev)
  445. goto err_put;
  446. p_dev->socket = s;
  447. p_dev->device_no = (s->device_count++);
  448. p_dev->func = function;
  449. p_dev->dev.bus = &pcmcia_bus_type;
  450. p_dev->dev.parent = s->dev.dev;
  451. p_dev->dev.release = pcmcia_release_dev;
  452. bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  453. p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
  454. if (!p_dev->devname)
  455. goto err_free;
  456. sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
  457. /* compat */
  458. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  459. /*
  460. * p_dev->function_config must be the same for all card functions.
  461. * Note that this is serialized by the device_add_lock, so that
  462. * only one such struct will be created.
  463. */
  464. list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
  465. if (p_dev->func == tmp_dev->func) {
  466. p_dev->function_config = tmp_dev->function_config;
  467. kref_get(&p_dev->function_config->ref);
  468. }
  469. /* Add to the list in pcmcia_bus_socket */
  470. list_add(&p_dev->socket_device_list, &s->devices_list);
  471. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  472. if (!p_dev->function_config) {
  473. p_dev->function_config = kzalloc(sizeof(struct config_t),
  474. GFP_KERNEL);
  475. if (!p_dev->function_config)
  476. goto err_unreg;
  477. kref_init(&p_dev->function_config->ref);
  478. }
  479. printk(KERN_NOTICE "pcmcia: registering new device %s\n",
  480. p_dev->devname);
  481. pcmcia_device_query(p_dev);
  482. if (device_register(&p_dev->dev))
  483. goto err_unreg;
  484. mutex_unlock(&device_add_lock);
  485. return p_dev;
  486. err_unreg:
  487. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  488. list_del(&p_dev->socket_device_list);
  489. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  490. err_free:
  491. kfree(p_dev->devname);
  492. kfree(p_dev);
  493. s->device_count--;
  494. err_put:
  495. mutex_unlock(&device_add_lock);
  496. pcmcia_put_socket(s);
  497. return NULL;
  498. }
  499. static int pcmcia_card_add(struct pcmcia_socket *s)
  500. {
  501. cisinfo_t cisinfo;
  502. cistpl_longlink_mfc_t mfc;
  503. unsigned int no_funcs, i;
  504. int ret = 0;
  505. if (!(s->resource_setup_done))
  506. return -EAGAIN; /* try again, but later... */
  507. if (pcmcia_validate_mem(s))
  508. return -EAGAIN; /* try again, but later... */
  509. ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
  510. if (ret || !cisinfo.Chains) {
  511. ds_dbg(0, "invalid CIS or invalid resources\n");
  512. return -ENODEV;
  513. }
  514. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  515. no_funcs = mfc.nfn;
  516. else
  517. no_funcs = 1;
  518. s->functions = no_funcs;
  519. for (i=0; i < no_funcs; i++)
  520. pcmcia_device_add(s, i);
  521. return (ret);
  522. }
  523. static void pcmcia_delayed_add_device(void *data)
  524. {
  525. struct pcmcia_socket *s = data;
  526. pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
  527. s->pcmcia_state.device_add_pending = 0;
  528. s->pcmcia_state.mfc_pfc = 0;
  529. }
  530. static int pcmcia_requery(struct device *dev, void * _data)
  531. {
  532. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  533. if (!p_dev->dev.driver)
  534. pcmcia_device_query(p_dev);
  535. return 0;
  536. }
  537. static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
  538. {
  539. int no_devices = 0;
  540. int ret = 0;
  541. unsigned long flags;
  542. /* must be called with skt_mutex held */
  543. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  544. if (list_empty(&skt->devices_list))
  545. no_devices = 1;
  546. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  547. /* If this is because of a CIS override, start over */
  548. if (new_cis && !no_devices)
  549. pcmcia_card_remove(skt, NULL);
  550. /* if no devices were added for this socket yet because of
  551. * missing resource information or other trouble, we need to
  552. * do this now. */
  553. if (no_devices || new_cis) {
  554. ret = pcmcia_card_add(skt);
  555. if (ret)
  556. return;
  557. }
  558. /* some device information might have changed because of a CIS
  559. * update or because we can finally read it correctly... so
  560. * determine it again, overwriting old values if necessary. */
  561. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  562. /* we re-scan all devices, not just the ones connected to this
  563. * socket. This does not matter, though. */
  564. ret = bus_rescan_devices(&pcmcia_bus_type);
  565. if (ret)
  566. printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
  567. }
  568. #ifdef CONFIG_PCMCIA_LOAD_CIS
  569. /**
  570. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  571. * @dev - the pcmcia device which needs a CIS override
  572. * @filename - requested filename in /lib/firmware/
  573. *
  574. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  575. * the one provided by the card is broken. The firmware files reside in
  576. * /lib/firmware/ in userspace.
  577. */
  578. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  579. {
  580. struct pcmcia_socket *s = dev->socket;
  581. const struct firmware *fw;
  582. char path[20];
  583. int ret = -ENOMEM;
  584. int no_funcs;
  585. int old_funcs;
  586. cisdump_t *cis;
  587. cistpl_longlink_mfc_t mfc;
  588. if (!filename)
  589. return -EINVAL;
  590. ds_dbg(1, "trying to load firmware %s\n", filename);
  591. if (strlen(filename) > 14)
  592. return -EINVAL;
  593. snprintf(path, 20, "%s", filename);
  594. if (request_firmware(&fw, path, &dev->dev) == 0) {
  595. if (fw->size >= CISTPL_MAX_CIS_SIZE)
  596. goto release;
  597. cis = kzalloc(sizeof(cisdump_t), GFP_KERNEL);
  598. if (!cis)
  599. goto release;
  600. cis->Length = fw->size + 1;
  601. memcpy(cis->Data, fw->data, fw->size);
  602. if (!pcmcia_replace_cis(s, cis))
  603. ret = 0;
  604. /* update information */
  605. pcmcia_device_query(dev);
  606. /* does this cis override add or remove functions? */
  607. old_funcs = s->functions;
  608. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  609. no_funcs = mfc.nfn;
  610. else
  611. no_funcs = 1;
  612. s->functions = no_funcs;
  613. if (old_funcs > no_funcs)
  614. pcmcia_card_remove(s, dev);
  615. else if (no_funcs > old_funcs)
  616. pcmcia_add_device_later(s, 1);
  617. }
  618. release:
  619. release_firmware(fw);
  620. return (ret);
  621. }
  622. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  623. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  624. {
  625. return -ENODEV;
  626. }
  627. #endif
  628. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  629. struct pcmcia_device_id *did)
  630. {
  631. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  632. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  633. return 0;
  634. }
  635. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  636. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  637. return 0;
  638. }
  639. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  640. if (dev->func != did->function)
  641. return 0;
  642. }
  643. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  644. if (!dev->prod_id[0])
  645. return 0;
  646. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  647. return 0;
  648. }
  649. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  650. if (!dev->prod_id[1])
  651. return 0;
  652. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  653. return 0;
  654. }
  655. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  656. if (!dev->prod_id[2])
  657. return 0;
  658. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  659. return 0;
  660. }
  661. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  662. if (!dev->prod_id[3])
  663. return 0;
  664. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  665. return 0;
  666. }
  667. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  668. if (dev->device_no != did->device_no)
  669. return 0;
  670. }
  671. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  672. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  673. return 0;
  674. /* if this is a pseudo-multi-function device,
  675. * we need explicit matches */
  676. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  677. return 0;
  678. if (dev->device_no)
  679. return 0;
  680. /* also, FUNC_ID matching needs to be activated by userspace
  681. * after it has re-checked that there is no possible module
  682. * with a prod_id/manf_id/card_id match.
  683. */
  684. if (!dev->allow_func_id_match)
  685. return 0;
  686. }
  687. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  688. if (!dev->socket->fake_cis)
  689. pcmcia_load_firmware(dev, did->cisfile);
  690. if (!dev->socket->fake_cis)
  691. return 0;
  692. }
  693. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  694. int i;
  695. for (i=0; i<4; i++)
  696. if (dev->prod_id[i])
  697. return 0;
  698. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  699. return 0;
  700. }
  701. dev->dev.driver_data = (void *) did;
  702. return 1;
  703. }
  704. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  705. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  706. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  707. struct pcmcia_device_id *did = p_drv->id_table;
  708. #ifdef CONFIG_PCMCIA_IOCTL
  709. /* matching by cardmgr */
  710. if (p_dev->cardmgr == p_drv)
  711. return 1;
  712. #endif
  713. while (did && did->match_flags) {
  714. if (pcmcia_devmatch(p_dev, did))
  715. return 1;
  716. did++;
  717. }
  718. return 0;
  719. }
  720. #ifdef CONFIG_HOTPLUG
  721. static int pcmcia_bus_uevent(struct device *dev, char **envp, int num_envp,
  722. char *buffer, int buffer_size)
  723. {
  724. struct pcmcia_device *p_dev;
  725. int i, length = 0;
  726. u32 hash[4] = { 0, 0, 0, 0};
  727. if (!dev)
  728. return -ENODEV;
  729. p_dev = to_pcmcia_dev(dev);
  730. /* calculate hashes */
  731. for (i=0; i<4; i++) {
  732. if (!p_dev->prod_id[i])
  733. continue;
  734. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  735. }
  736. i = 0;
  737. if (add_uevent_var(envp, num_envp, &i,
  738. buffer, buffer_size, &length,
  739. "SOCKET_NO=%u",
  740. p_dev->socket->sock))
  741. return -ENOMEM;
  742. if (add_uevent_var(envp, num_envp, &i,
  743. buffer, buffer_size, &length,
  744. "DEVICE_NO=%02X",
  745. p_dev->device_no))
  746. return -ENOMEM;
  747. if (add_uevent_var(envp, num_envp, &i,
  748. buffer, buffer_size, &length,
  749. "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  750. "pa%08Xpb%08Xpc%08Xpd%08X",
  751. p_dev->has_manf_id ? p_dev->manf_id : 0,
  752. p_dev->has_card_id ? p_dev->card_id : 0,
  753. p_dev->has_func_id ? p_dev->func_id : 0,
  754. p_dev->func,
  755. p_dev->device_no,
  756. hash[0],
  757. hash[1],
  758. hash[2],
  759. hash[3]))
  760. return -ENOMEM;
  761. envp[i] = NULL;
  762. return 0;
  763. }
  764. #else
  765. static int pcmcia_bus_uevent(struct device *dev, char **envp, int num_envp,
  766. char *buffer, int buffer_size)
  767. {
  768. return -ENODEV;
  769. }
  770. #endif
  771. /************************ per-device sysfs output ***************************/
  772. #define pcmcia_device_attr(field, test, format) \
  773. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  774. { \
  775. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  776. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  777. }
  778. #define pcmcia_device_stringattr(name, field) \
  779. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  780. { \
  781. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  782. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  783. }
  784. pcmcia_device_attr(func, socket, "0x%02x\n");
  785. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  786. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  787. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  788. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  789. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  790. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  791. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  792. static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
  793. {
  794. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  795. if (p_dev->suspended)
  796. return sprintf(buf, "off\n");
  797. else
  798. return sprintf(buf, "on\n");
  799. }
  800. static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
  801. const char *buf, size_t count)
  802. {
  803. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  804. int ret = 0;
  805. if (!count)
  806. return -EINVAL;
  807. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  808. ret = dpm_runtime_suspend(dev, PMSG_SUSPEND);
  809. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  810. dpm_runtime_resume(dev);
  811. return ret ? ret : count;
  812. }
  813. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  814. {
  815. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  816. int i;
  817. u32 hash[4] = { 0, 0, 0, 0};
  818. /* calculate hashes */
  819. for (i=0; i<4; i++) {
  820. if (!p_dev->prod_id[i])
  821. continue;
  822. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  823. }
  824. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  825. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  826. p_dev->has_manf_id ? p_dev->manf_id : 0,
  827. p_dev->has_card_id ? p_dev->card_id : 0,
  828. p_dev->has_func_id ? p_dev->func_id : 0,
  829. p_dev->func, p_dev->device_no,
  830. hash[0], hash[1], hash[2], hash[3]);
  831. }
  832. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  833. struct device_attribute *attr, const char *buf, size_t count)
  834. {
  835. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  836. int ret;
  837. if (!count)
  838. return -EINVAL;
  839. mutex_lock(&p_dev->socket->skt_mutex);
  840. p_dev->allow_func_id_match = 1;
  841. mutex_unlock(&p_dev->socket->skt_mutex);
  842. ret = bus_rescan_devices(&pcmcia_bus_type);
  843. if (ret)
  844. printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
  845. "allowing func_id matches\n");
  846. return count;
  847. }
  848. static struct device_attribute pcmcia_dev_attrs[] = {
  849. __ATTR(function, 0444, func_show, NULL),
  850. __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
  851. __ATTR_RO(func_id),
  852. __ATTR_RO(manf_id),
  853. __ATTR_RO(card_id),
  854. __ATTR_RO(prod_id1),
  855. __ATTR_RO(prod_id2),
  856. __ATTR_RO(prod_id3),
  857. __ATTR_RO(prod_id4),
  858. __ATTR_RO(modalias),
  859. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  860. __ATTR_NULL,
  861. };
  862. /* PM support, also needed for reset */
  863. static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
  864. {
  865. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  866. struct pcmcia_driver *p_drv = NULL;
  867. int ret = 0;
  868. if (dev->driver)
  869. p_drv = to_pcmcia_drv(dev->driver);
  870. if (!p_drv)
  871. goto out;
  872. if (p_drv->suspend) {
  873. ret = p_drv->suspend(p_dev);
  874. if (ret)
  875. goto out;
  876. }
  877. if (p_dev->device_no == p_dev->func)
  878. pcmcia_release_configuration(p_dev);
  879. out:
  880. if (!ret)
  881. p_dev->suspended = 1;
  882. return ret;
  883. }
  884. static int pcmcia_dev_resume(struct device * dev)
  885. {
  886. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  887. struct pcmcia_driver *p_drv = NULL;
  888. int ret = 0;
  889. if (dev->driver)
  890. p_drv = to_pcmcia_drv(dev->driver);
  891. if (!p_drv)
  892. goto out;
  893. if (p_dev->device_no == p_dev->func) {
  894. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  895. if (ret)
  896. goto out;
  897. }
  898. if (p_drv->resume)
  899. ret = p_drv->resume(p_dev);
  900. out:
  901. if (!ret)
  902. p_dev->suspended = 0;
  903. return ret;
  904. }
  905. static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
  906. {
  907. struct pcmcia_socket *skt = _data;
  908. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  909. if (p_dev->socket != skt)
  910. return 0;
  911. return dpm_runtime_suspend(dev, PMSG_SUSPEND);
  912. }
  913. static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
  914. {
  915. struct pcmcia_socket *skt = _data;
  916. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  917. if (p_dev->socket != skt)
  918. return 0;
  919. dpm_runtime_resume(dev);
  920. return 0;
  921. }
  922. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  923. {
  924. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  925. return 0;
  926. }
  927. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  928. {
  929. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  930. pcmcia_bus_suspend_callback)) {
  931. pcmcia_bus_resume(skt);
  932. return -EIO;
  933. }
  934. return 0;
  935. }
  936. /*======================================================================
  937. The card status event handler.
  938. ======================================================================*/
  939. /* Normally, the event is passed to individual drivers after
  940. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  941. * is inversed to maintain historic compatibility.
  942. */
  943. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  944. {
  945. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  946. if (!s) {
  947. printk(KERN_ERR "PCMCIA obtaining reference to socket %p " \
  948. "failed, event 0x%x lost!\n", skt, event);
  949. return -ENODEV;
  950. }
  951. ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
  952. event, priority, skt);
  953. switch (event) {
  954. case CS_EVENT_CARD_REMOVAL:
  955. s->pcmcia_state.present = 0;
  956. pcmcia_card_remove(skt, NULL);
  957. handle_event(skt, event);
  958. break;
  959. case CS_EVENT_CARD_INSERTION:
  960. s->pcmcia_state.present = 1;
  961. pcmcia_card_add(skt);
  962. handle_event(skt, event);
  963. break;
  964. case CS_EVENT_EJECTION_REQUEST:
  965. break;
  966. case CS_EVENT_PM_SUSPEND:
  967. case CS_EVENT_PM_RESUME:
  968. case CS_EVENT_RESET_PHYSICAL:
  969. case CS_EVENT_CARD_RESET:
  970. default:
  971. handle_event(skt, event);
  972. break;
  973. }
  974. pcmcia_put_socket(s);
  975. return 0;
  976. } /* ds_event */
  977. struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
  978. {
  979. struct pcmcia_device *p_dev;
  980. struct pcmcia_device *ret = NULL;
  981. p_dev = pcmcia_get_dev(_p_dev);
  982. if (!p_dev)
  983. return NULL;
  984. if (!p_dev->socket->pcmcia_state.present)
  985. goto out;
  986. if (p_dev->_removed)
  987. goto out;
  988. if (p_dev->suspended)
  989. goto out;
  990. ret = p_dev;
  991. out:
  992. pcmcia_put_dev(p_dev);
  993. return ret;
  994. }
  995. EXPORT_SYMBOL(pcmcia_dev_present);
  996. static struct pcmcia_callback pcmcia_bus_callback = {
  997. .owner = THIS_MODULE,
  998. .event = ds_event,
  999. .requery = pcmcia_bus_rescan,
  1000. .suspend = pcmcia_bus_suspend,
  1001. .resume = pcmcia_bus_resume,
  1002. };
  1003. static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev,
  1004. struct class_interface *class_intf)
  1005. {
  1006. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  1007. int ret;
  1008. socket = pcmcia_get_socket(socket);
  1009. if (!socket) {
  1010. printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
  1011. return -ENODEV;
  1012. }
  1013. /*
  1014. * Ugly. But we want to wait for the socket threads to have started up.
  1015. * We really should let the drivers themselves drive some of this..
  1016. */
  1017. msleep(250);
  1018. #ifdef CONFIG_PCMCIA_IOCTL
  1019. init_waitqueue_head(&socket->queue);
  1020. #endif
  1021. INIT_LIST_HEAD(&socket->devices_list);
  1022. INIT_WORK(&socket->device_add, pcmcia_delayed_add_device, socket);
  1023. memset(&socket->pcmcia_state, 0, sizeof(u8));
  1024. socket->device_count = 0;
  1025. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1026. if (ret) {
  1027. printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
  1028. pcmcia_put_socket(socket);
  1029. return (ret);
  1030. }
  1031. return 0;
  1032. }
  1033. static void pcmcia_bus_remove_socket(struct class_device *class_dev,
  1034. struct class_interface *class_intf)
  1035. {
  1036. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  1037. if (!socket)
  1038. return;
  1039. socket->pcmcia_state.dead = 1;
  1040. pccard_register_pcmcia(socket, NULL);
  1041. /* unregister any unbound devices */
  1042. mutex_lock(&socket->skt_mutex);
  1043. pcmcia_card_remove(socket, NULL);
  1044. mutex_unlock(&socket->skt_mutex);
  1045. pcmcia_put_socket(socket);
  1046. return;
  1047. }
  1048. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1049. static struct class_interface pcmcia_bus_interface = {
  1050. .class = &pcmcia_socket_class,
  1051. .add = &pcmcia_bus_add_socket,
  1052. .remove = &pcmcia_bus_remove_socket,
  1053. };
  1054. struct bus_type pcmcia_bus_type = {
  1055. .name = "pcmcia",
  1056. .uevent = pcmcia_bus_uevent,
  1057. .match = pcmcia_bus_match,
  1058. .dev_attrs = pcmcia_dev_attrs,
  1059. .probe = pcmcia_device_probe,
  1060. .remove = pcmcia_device_remove,
  1061. .suspend = pcmcia_dev_suspend,
  1062. .resume = pcmcia_dev_resume,
  1063. };
  1064. static int __init init_pcmcia_bus(void)
  1065. {
  1066. int ret;
  1067. spin_lock_init(&pcmcia_dev_list_lock);
  1068. ret = bus_register(&pcmcia_bus_type);
  1069. if (ret < 0) {
  1070. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1071. return ret;
  1072. }
  1073. ret = class_interface_register(&pcmcia_bus_interface);
  1074. if (ret < 0) {
  1075. printk(KERN_WARNING
  1076. "pcmcia: class_interface_register error: %d\n", ret);
  1077. bus_unregister(&pcmcia_bus_type);
  1078. return ret;
  1079. }
  1080. pcmcia_setup_ioctl();
  1081. return 0;
  1082. }
  1083. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1084. * pcmcia_socket_class is already registered */
  1085. static void __exit exit_pcmcia_bus(void)
  1086. {
  1087. pcmcia_cleanup_ioctl();
  1088. class_interface_unregister(&pcmcia_bus_interface);
  1089. bus_unregister(&pcmcia_bus_type);
  1090. }
  1091. module_exit(exit_pcmcia_bus);
  1092. MODULE_ALIAS("ds");