ds.c 32 KB

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