ds.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  351. CISTPL_MANFID, &manf_id)) {
  352. p_dev->manf_id = manf_id.manf;
  353. p_dev->card_id = manf_id.card;
  354. p_dev->has_manf_id = 1;
  355. p_dev->has_card_id = 1;
  356. }
  357. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  358. CISTPL_FUNCID, &func_id)) {
  359. p_dev->func_id = func_id.func;
  360. p_dev->has_func_id = 1;
  361. } else {
  362. /* rule of thumb: cards with no FUNCID, but with
  363. * common memory device geometry information, are
  364. * probably memory cards (from pcmcia-cs) */
  365. cistpl_device_geo_t devgeo;
  366. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  367. CISTPL_DEVICE_GEO, &devgeo)) {
  368. ds_dbg(0, "mem device geometry probably means "
  369. "FUNCID_MEMORY\n");
  370. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  371. p_dev->has_func_id = 1;
  372. }
  373. }
  374. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  375. &vers1)) {
  376. for (i=0; i < vers1.ns; i++) {
  377. char *tmp;
  378. unsigned int length;
  379. tmp = vers1.str + vers1.ofs[i];
  380. length = strlen(tmp) + 1;
  381. if ((length < 3) || (length > 255))
  382. continue;
  383. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  384. GFP_KERNEL);
  385. if (!p_dev->prod_id[i])
  386. continue;
  387. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  388. tmp, length);
  389. }
  390. }
  391. return 0;
  392. }
  393. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  394. * Serializes pcmcia_device_add; will most likely be removed in future.
  395. *
  396. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  397. * won't work, this doesn't matter much at the moment: the driver core doesn't
  398. * support it either.
  399. */
  400. static DECLARE_MUTEX(device_add_lock);
  401. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  402. {
  403. struct pcmcia_device *p_dev;
  404. unsigned long flags;
  405. s = pcmcia_get_socket(s);
  406. if (!s)
  407. return NULL;
  408. down(&device_add_lock);
  409. /* max of 2 devices per card */
  410. if (s->device_count == 2)
  411. goto err_put;
  412. p_dev = kmalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  413. if (!p_dev)
  414. goto err_put;
  415. memset(p_dev, 0, sizeof(struct pcmcia_device));
  416. p_dev->socket = s;
  417. p_dev->device_no = (s->device_count++);
  418. p_dev->func = function;
  419. p_dev->dev.bus = &pcmcia_bus_type;
  420. p_dev->dev.parent = s->dev.dev;
  421. p_dev->dev.release = pcmcia_release_dev;
  422. sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  423. /* compat */
  424. p_dev->state = CLIENT_UNBOUND;
  425. /* Add to the list in pcmcia_bus_socket */
  426. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  427. list_add_tail(&p_dev->socket_device_list, &s->devices_list);
  428. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  429. pcmcia_device_query(p_dev);
  430. if (device_register(&p_dev->dev)) {
  431. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  432. list_del(&p_dev->socket_device_list);
  433. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  434. goto err_free;
  435. }
  436. up(&device_add_lock);
  437. return p_dev;
  438. err_free:
  439. kfree(p_dev);
  440. s->device_count--;
  441. err_put:
  442. up(&device_add_lock);
  443. pcmcia_put_socket(s);
  444. return NULL;
  445. }
  446. static int pcmcia_card_add(struct pcmcia_socket *s)
  447. {
  448. cisinfo_t cisinfo;
  449. cistpl_longlink_mfc_t mfc;
  450. unsigned int no_funcs, i;
  451. int ret = 0;
  452. if (!(s->resource_setup_done))
  453. return -EAGAIN; /* try again, but later... */
  454. pcmcia_validate_mem(s);
  455. ret = pccard_validate_cis(s, BIND_FN_ALL, &cisinfo);
  456. if (ret || !cisinfo.Chains) {
  457. ds_dbg(0, "invalid CIS or invalid resources\n");
  458. return -ENODEV;
  459. }
  460. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  461. no_funcs = mfc.nfn;
  462. else
  463. no_funcs = 1;
  464. for (i=0; i < no_funcs; i++)
  465. pcmcia_device_add(s, i);
  466. return (ret);
  467. }
  468. static void pcmcia_delayed_add_pseudo_device(void *data)
  469. {
  470. struct pcmcia_socket *s = data;
  471. pcmcia_device_add(s, 0);
  472. s->pcmcia_state.device_add_pending = 0;
  473. }
  474. static inline void pcmcia_add_pseudo_device(struct pcmcia_socket *s)
  475. {
  476. if (!s->pcmcia_state.device_add_pending) {
  477. s->pcmcia_state.device_add_pending = 1;
  478. schedule_work(&s->device_add);
  479. }
  480. return;
  481. }
  482. static int pcmcia_requery(struct device *dev, void * _data)
  483. {
  484. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  485. if (!p_dev->dev.driver)
  486. pcmcia_device_query(p_dev);
  487. return 0;
  488. }
  489. static void pcmcia_bus_rescan(struct pcmcia_socket *skt)
  490. {
  491. int no_devices=0;
  492. unsigned long flags;
  493. /* must be called with skt_sem held */
  494. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  495. if (list_empty(&skt->devices_list))
  496. no_devices=1;
  497. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  498. /* if no devices were added for this socket yet because of
  499. * missing resource information or other trouble, we need to
  500. * do this now. */
  501. if (no_devices) {
  502. int ret = pcmcia_card_add(skt);
  503. if (ret)
  504. return;
  505. }
  506. /* some device information might have changed because of a CIS
  507. * update or because we can finally read it correctly... so
  508. * determine it again, overwriting old values if necessary. */
  509. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  510. /* we re-scan all devices, not just the ones connected to this
  511. * socket. This does not matter, though. */
  512. bus_rescan_devices(&pcmcia_bus_type);
  513. }
  514. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  515. struct pcmcia_device_id *did)
  516. {
  517. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  518. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  519. return 0;
  520. }
  521. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  522. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  523. return 0;
  524. }
  525. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  526. if (dev->func != did->function)
  527. return 0;
  528. }
  529. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  530. if (!dev->prod_id[0])
  531. return 0;
  532. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  533. return 0;
  534. }
  535. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  536. if (!dev->prod_id[1])
  537. return 0;
  538. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  539. return 0;
  540. }
  541. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  542. if (!dev->prod_id[2])
  543. return 0;
  544. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  545. return 0;
  546. }
  547. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  548. if (!dev->prod_id[3])
  549. return 0;
  550. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  551. return 0;
  552. }
  553. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  554. /* handle pseudo multifunction devices:
  555. * there are at most two pseudo multifunction devices.
  556. * if we're matching against the first, schedule a
  557. * call which will then check whether there are two
  558. * pseudo devices, and if not, add the second one.
  559. */
  560. if (dev->device_no == 0)
  561. pcmcia_add_pseudo_device(dev->socket);
  562. if (dev->device_no != did->device_no)
  563. return 0;
  564. }
  565. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  566. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  567. return 0;
  568. /* if this is a pseudo-multi-function device,
  569. * we need explicit matches */
  570. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  571. return 0;
  572. if (dev->device_no)
  573. return 0;
  574. /* also, FUNC_ID matching needs to be activated by userspace
  575. * after it has re-checked that there is no possible module
  576. * with a prod_id/manf_id/card_id match.
  577. */
  578. if (!dev->allow_func_id_match)
  579. return 0;
  580. }
  581. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  582. if (!dev->socket->fake_cis)
  583. pcmcia_load_firmware(dev, did->cisfile);
  584. if (!dev->socket->fake_cis)
  585. return 0;
  586. }
  587. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  588. int i;
  589. for (i=0; i<4; i++)
  590. if (dev->prod_id[i])
  591. return 0;
  592. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  593. return 0;
  594. }
  595. dev->dev.driver_data = (void *) did;
  596. return 1;
  597. }
  598. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  599. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  600. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  601. struct pcmcia_device_id *did = p_drv->id_table;
  602. /* matching by cardmgr */
  603. if (p_dev->cardmgr == p_drv)
  604. return 1;
  605. while (did && did->match_flags) {
  606. if (pcmcia_devmatch(p_dev, did))
  607. return 1;
  608. did++;
  609. }
  610. return 0;
  611. }
  612. #ifdef CONFIG_HOTPLUG
  613. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  614. char *buffer, int buffer_size)
  615. {
  616. struct pcmcia_device *p_dev;
  617. int i, length = 0;
  618. u32 hash[4] = { 0, 0, 0, 0};
  619. if (!dev)
  620. return -ENODEV;
  621. p_dev = to_pcmcia_dev(dev);
  622. /* calculate hashes */
  623. for (i=0; i<4; i++) {
  624. if (!p_dev->prod_id[i])
  625. continue;
  626. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  627. }
  628. i = 0;
  629. if (add_hotplug_env_var(envp, num_envp, &i,
  630. buffer, buffer_size, &length,
  631. "SOCKET_NO=%u",
  632. p_dev->socket->sock))
  633. return -ENOMEM;
  634. if (add_hotplug_env_var(envp, num_envp, &i,
  635. buffer, buffer_size, &length,
  636. "DEVICE_NO=%02X",
  637. p_dev->device_no))
  638. return -ENOMEM;
  639. if (add_hotplug_env_var(envp, num_envp, &i,
  640. buffer, buffer_size, &length,
  641. "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  642. "pa%08Xpb%08Xpc%08Xpd%08X",
  643. p_dev->has_manf_id ? p_dev->manf_id : 0,
  644. p_dev->has_card_id ? p_dev->card_id : 0,
  645. p_dev->has_func_id ? p_dev->func_id : 0,
  646. p_dev->func,
  647. p_dev->device_no,
  648. hash[0],
  649. hash[1],
  650. hash[2],
  651. hash[3]))
  652. return -ENOMEM;
  653. envp[i] = NULL;
  654. return 0;
  655. }
  656. #else
  657. static int pcmcia_bus_hotplug(struct device *dev, char **envp, int num_envp,
  658. char *buffer, int buffer_size)
  659. {
  660. return -ENODEV;
  661. }
  662. #endif
  663. /************************ per-device sysfs output ***************************/
  664. #define pcmcia_device_attr(field, test, format) \
  665. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  666. { \
  667. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  668. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  669. }
  670. #define pcmcia_device_stringattr(name, field) \
  671. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  672. { \
  673. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  674. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  675. }
  676. pcmcia_device_attr(func, socket, "0x%02x\n");
  677. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  678. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  679. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  680. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  681. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  682. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  683. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  684. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  685. {
  686. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  687. int i;
  688. u32 hash[4] = { 0, 0, 0, 0};
  689. /* calculate hashes */
  690. for (i=0; i<4; i++) {
  691. if (!p_dev->prod_id[i])
  692. continue;
  693. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  694. }
  695. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  696. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  697. p_dev->has_manf_id ? p_dev->manf_id : 0,
  698. p_dev->has_card_id ? p_dev->card_id : 0,
  699. p_dev->has_func_id ? p_dev->func_id : 0,
  700. p_dev->func, p_dev->device_no,
  701. hash[0], hash[1], hash[2], hash[3]);
  702. }
  703. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  704. struct device_attribute *attr, const char *buf, size_t count)
  705. {
  706. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  707. if (!count)
  708. return -EINVAL;
  709. down(&p_dev->socket->skt_sem);
  710. p_dev->allow_func_id_match = 1;
  711. up(&p_dev->socket->skt_sem);
  712. bus_rescan_devices(&pcmcia_bus_type);
  713. return count;
  714. }
  715. static struct device_attribute pcmcia_dev_attrs[] = {
  716. __ATTR(function, 0444, func_show, NULL),
  717. __ATTR_RO(func_id),
  718. __ATTR_RO(manf_id),
  719. __ATTR_RO(card_id),
  720. __ATTR_RO(prod_id1),
  721. __ATTR_RO(prod_id2),
  722. __ATTR_RO(prod_id3),
  723. __ATTR_RO(prod_id4),
  724. __ATTR_RO(modalias),
  725. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  726. __ATTR_NULL,
  727. };
  728. /*======================================================================
  729. The card status event handler.
  730. ======================================================================*/
  731. struct send_event_data {
  732. struct pcmcia_socket *skt;
  733. event_t event;
  734. int priority;
  735. };
  736. static int send_event_callback(struct device *dev, void * _data)
  737. {
  738. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  739. struct pcmcia_driver *p_drv;
  740. struct send_event_data *data = _data;
  741. /* we get called for all sockets, but may only pass the event
  742. * for drivers _on the affected socket_ */
  743. if (p_dev->socket != data->skt)
  744. return 0;
  745. p_drv = to_pcmcia_drv(p_dev->dev.driver);
  746. if (!p_drv)
  747. return 0;
  748. if (p_dev->state & (CLIENT_UNBOUND|CLIENT_STALE))
  749. return 0;
  750. if (p_drv->event)
  751. return p_drv->event(data->event, data->priority,
  752. &p_dev->event_callback_args);
  753. return 0;
  754. }
  755. static int send_event(struct pcmcia_socket *s, event_t event, int priority)
  756. {
  757. struct send_event_data private;
  758. private.skt = s;
  759. private.event = event;
  760. private.priority = priority;
  761. return bus_for_each_dev(&pcmcia_bus_type, NULL, &private, send_event_callback);
  762. } /* send_event */
  763. /* Normally, the event is passed to individual drivers after
  764. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  765. * is inversed to maintain historic compatibility.
  766. */
  767. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  768. {
  769. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  770. int ret = 0;
  771. ds_dbg(1, "ds_event(0x%06x, %d, 0x%p)\n",
  772. event, priority, skt);
  773. switch (event) {
  774. case CS_EVENT_CARD_REMOVAL:
  775. s->pcmcia_state.present = 0;
  776. send_event(skt, event, priority);
  777. unbind_request(skt);
  778. handle_event(skt, event);
  779. break;
  780. case CS_EVENT_CARD_INSERTION:
  781. s->pcmcia_state.present = 1;
  782. pcmcia_card_add(skt);
  783. handle_event(skt, event);
  784. break;
  785. case CS_EVENT_EJECTION_REQUEST:
  786. ret = send_event(skt, event, priority);
  787. break;
  788. default:
  789. handle_event(skt, event);
  790. send_event(skt, event, priority);
  791. break;
  792. }
  793. pcmcia_put_socket(s);
  794. return 0;
  795. } /* ds_event */
  796. int pcmcia_register_client(struct pcmcia_device **handle, client_reg_t *req)
  797. {
  798. struct pcmcia_socket *s = NULL;
  799. struct pcmcia_device *p_dev = NULL;
  800. struct pcmcia_driver *p_drv = NULL;
  801. /* Look for unbound client with matching dev_info */
  802. down_read(&pcmcia_socket_list_rwsem);
  803. list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
  804. unsigned long flags;
  805. if (s->state & SOCKET_CARDBUS)
  806. continue;
  807. s = pcmcia_get_socket(s);
  808. if (!s)
  809. continue;
  810. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  811. list_for_each_entry(p_dev, &s->devices_list, socket_device_list) {
  812. p_dev = pcmcia_get_dev(p_dev);
  813. if (!p_dev)
  814. continue;
  815. if (!(p_dev->state & CLIENT_UNBOUND) ||
  816. (!p_dev->dev.driver)) {
  817. pcmcia_put_dev(p_dev);
  818. continue;
  819. }
  820. p_drv = to_pcmcia_drv(p_dev->dev.driver);
  821. if (!strncmp(p_drv->drv.name, (char *)req->dev_info, DEV_NAME_LEN)) {
  822. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  823. goto found;
  824. }
  825. pcmcia_put_dev(p_dev);
  826. }
  827. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  828. pcmcia_put_socket(s);
  829. }
  830. found:
  831. up_read(&pcmcia_socket_list_rwsem);
  832. if (!p_dev)
  833. return -ENODEV;
  834. pcmcia_put_socket(s); /* safe, as we already hold a reference from bind_device */
  835. *handle = p_dev;
  836. p_dev->state &= ~CLIENT_UNBOUND;
  837. p_dev->event_callback_args = req->event_callback_args;
  838. p_dev->event_callback_args.client_handle = p_dev;
  839. if (!s->functions) {
  840. cistpl_longlink_mfc_t mfc;
  841. if (pccard_read_tuple(s, p_dev->func, CISTPL_LONGLINK_MFC, &mfc)
  842. == CS_SUCCESS)
  843. s->functions = mfc.nfn;
  844. else
  845. s->functions = 1;
  846. s->config = kmalloc(sizeof(config_t) * s->functions,
  847. GFP_KERNEL);
  848. if (!s->config)
  849. goto out_no_resource;
  850. memset(s->config, 0, sizeof(config_t) * s->functions);
  851. }
  852. ds_dbg(1, "register_client(): client 0x%p, dev %s\n",
  853. p_dev, p_dev->dev.bus_id);
  854. if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT) {
  855. if (p_drv->event)
  856. p_drv->event(CS_EVENT_CARD_INSERTION, CS_EVENT_PRI_LOW,
  857. &p_dev->event_callback_args);
  858. }
  859. return CS_SUCCESS;
  860. out_no_resource:
  861. pcmcia_put_dev(p_dev);
  862. return CS_OUT_OF_RESOURCE;
  863. } /* register_client */
  864. EXPORT_SYMBOL(pcmcia_register_client);
  865. /* unbind _all_ devices attached to a given pcmcia_bus_socket. The
  866. * drivers have been called with EVENT_CARD_REMOVAL before.
  867. */
  868. static int unbind_request(struct pcmcia_socket *s)
  869. {
  870. struct pcmcia_device *p_dev;
  871. unsigned long flags;
  872. ds_dbg(2, "unbind_request(%d)\n", s->sock);
  873. s->device_count = 0;
  874. for (;;) {
  875. /* unregister all pcmcia_devices registered with this socket*/
  876. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  877. if (list_empty(&s->devices_list)) {
  878. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  879. return 0;
  880. }
  881. p_dev = list_entry((&s->devices_list)->next, struct pcmcia_device, socket_device_list);
  882. list_del(&p_dev->socket_device_list);
  883. p_dev->state |= CLIENT_STALE;
  884. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  885. device_unregister(&p_dev->dev);
  886. }
  887. return 0;
  888. } /* unbind_request */
  889. int pcmcia_deregister_client(struct pcmcia_device *p_dev)
  890. {
  891. struct pcmcia_socket *s;
  892. int i;
  893. s = p_dev->socket;
  894. ds_dbg(1, "deregister_client(%p)\n", p_dev);
  895. if (p_dev->state & (CLIENT_IRQ_REQ|CLIENT_IO_REQ|CLIENT_CONFIG_LOCKED))
  896. goto warn_out;
  897. for (i = 0; i < MAX_WIN; i++)
  898. if (p_dev->state & CLIENT_WIN_REQ(i))
  899. goto warn_out;
  900. if (p_dev->state & CLIENT_STALE) {
  901. p_dev->state &= ~CLIENT_STALE;
  902. pcmcia_put_dev(p_dev);
  903. } else {
  904. p_dev->state = CLIENT_UNBOUND;
  905. }
  906. return CS_SUCCESS;
  907. warn_out:
  908. printk(KERN_WARNING "ds: deregister_client was called too early.\n");
  909. return CS_IN_USE;
  910. } /* deregister_client */
  911. EXPORT_SYMBOL(pcmcia_deregister_client);
  912. static struct pcmcia_callback pcmcia_bus_callback = {
  913. .owner = THIS_MODULE,
  914. .event = ds_event,
  915. .requery = pcmcia_bus_rescan,
  916. };
  917. static int __devinit pcmcia_bus_add_socket(struct class_device *class_dev)
  918. {
  919. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  920. int ret;
  921. socket = pcmcia_get_socket(socket);
  922. if (!socket) {
  923. printk(KERN_ERR "PCMCIA obtaining reference to socket %p failed\n", socket);
  924. return -ENODEV;
  925. }
  926. /*
  927. * Ugly. But we want to wait for the socket threads to have started up.
  928. * We really should let the drivers themselves drive some of this..
  929. */
  930. msleep(250);
  931. #ifdef CONFIG_PCMCIA_IOCTL
  932. init_waitqueue_head(&socket->queue);
  933. #endif
  934. INIT_LIST_HEAD(&socket->devices_list);
  935. INIT_WORK(&socket->device_add, pcmcia_delayed_add_pseudo_device, socket);
  936. memset(&socket->pcmcia_state, 0, sizeof(u8));
  937. socket->device_count = 0;
  938. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  939. if (ret) {
  940. printk(KERN_ERR "PCMCIA registration PCCard core failed for socket %p\n", socket);
  941. pcmcia_put_socket(socket);
  942. return (ret);
  943. }
  944. return 0;
  945. }
  946. static void pcmcia_bus_remove_socket(struct class_device *class_dev)
  947. {
  948. struct pcmcia_socket *socket = class_get_devdata(class_dev);
  949. if (!socket)
  950. return;
  951. socket->pcmcia_state.dead = 1;
  952. pccard_register_pcmcia(socket, NULL);
  953. pcmcia_put_socket(socket);
  954. return;
  955. }
  956. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  957. static struct class_interface pcmcia_bus_interface = {
  958. .class = &pcmcia_socket_class,
  959. .add = &pcmcia_bus_add_socket,
  960. .remove = &pcmcia_bus_remove_socket,
  961. };
  962. struct bus_type pcmcia_bus_type = {
  963. .name = "pcmcia",
  964. .hotplug = pcmcia_bus_hotplug,
  965. .match = pcmcia_bus_match,
  966. .dev_attrs = pcmcia_dev_attrs,
  967. };
  968. static int __init init_pcmcia_bus(void)
  969. {
  970. spin_lock_init(&pcmcia_dev_list_lock);
  971. bus_register(&pcmcia_bus_type);
  972. class_interface_register(&pcmcia_bus_interface);
  973. pcmcia_setup_ioctl();
  974. return 0;
  975. }
  976. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  977. * pcmcia_socket_class is already registered */
  978. static void __exit exit_pcmcia_bus(void)
  979. {
  980. pcmcia_cleanup_ioctl();
  981. class_interface_unregister(&pcmcia_bus_interface);
  982. bus_unregister(&pcmcia_bus_type);
  983. }
  984. module_exit(exit_pcmcia_bus);
  985. MODULE_ALIAS("ds");