ds.c 36 KB

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