ds.c 30 KB

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