ds.c 31 KB

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