ds.c 33 KB

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