ds.c 31 KB

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