ds.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * ds.c -- 16-bit PCMCIA core support
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * The initial developer of the original code is David A. Hinds
  9. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  10. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  11. *
  12. * (C) 1999 David A. Hinds
  13. * (C) 2003 - 2006 Dominik Brodowski
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/errno.h>
  19. #include <linux/list.h>
  20. #include <linux/delay.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/crc32.h>
  23. #include <linux/firmware.h>
  24. #include <linux/kref.h>
  25. #include <linux/dma-mapping.h>
  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 CONFIG_PCMCIA_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. #define ds_dev_dbg(lvl, dev, fmt, arg...) do { \
  46. if (ds_pc_debug > (lvl)) \
  47. dev_printk(KERN_DEBUG, dev, "ds: " fmt , ## arg); \
  48. } while (0)
  49. #else
  50. #define ds_dbg(lvl, fmt, arg...) do { } while (0)
  51. #define ds_dev_dbg(lvl, dev, fmt, arg...) do { } while (0)
  52. #endif
  53. spinlock_t pcmcia_dev_list_lock;
  54. /*====================================================================*/
  55. /* code which was in cs.c before */
  56. /* String tables for error messages */
  57. typedef struct lookup_t {
  58. int key;
  59. char *msg;
  60. } lookup_t;
  61. static const lookup_t error_table[] = {
  62. { CS_SUCCESS, "Operation succeeded" },
  63. { CS_BAD_ADAPTER, "Bad adapter" },
  64. { CS_BAD_ATTRIBUTE, "Bad attribute", },
  65. { CS_BAD_BASE, "Bad base address" },
  66. { CS_BAD_EDC, "Bad EDC" },
  67. { CS_BAD_IRQ, "Bad IRQ" },
  68. { CS_BAD_OFFSET, "Bad offset" },
  69. { CS_BAD_PAGE, "Bad page number" },
  70. { CS_READ_FAILURE, "Read failure" },
  71. { CS_BAD_SIZE, "Bad size" },
  72. { CS_BAD_SOCKET, "Bad socket" },
  73. { CS_BAD_TYPE, "Bad type" },
  74. { CS_BAD_VCC, "Bad Vcc" },
  75. { CS_BAD_VPP, "Bad Vpp" },
  76. { CS_BAD_WINDOW, "Bad window" },
  77. { CS_WRITE_FAILURE, "Write failure" },
  78. { CS_NO_CARD, "No card present" },
  79. { CS_UNSUPPORTED_FUNCTION, "Usupported function" },
  80. { CS_UNSUPPORTED_MODE, "Unsupported mode" },
  81. { CS_BAD_SPEED, "Bad speed" },
  82. { CS_BUSY, "Resource busy" },
  83. { CS_GENERAL_FAILURE, "General failure" },
  84. { CS_WRITE_PROTECTED, "Write protected" },
  85. { CS_BAD_ARG_LENGTH, "Bad argument length" },
  86. { CS_BAD_ARGS, "Bad arguments" },
  87. { CS_CONFIGURATION_LOCKED, "Configuration locked" },
  88. { CS_IN_USE, "Resource in use" },
  89. { CS_NO_MORE_ITEMS, "No more items" },
  90. { CS_OUT_OF_RESOURCE, "Out of resource" },
  91. { CS_BAD_HANDLE, "Bad handle" },
  92. { CS_BAD_TUPLE, "Bad CIS tuple" }
  93. };
  94. static const lookup_t service_table[] = {
  95. { AccessConfigurationRegister, "AccessConfigurationRegister" },
  96. { AddSocketServices, "AddSocketServices" },
  97. { AdjustResourceInfo, "AdjustResourceInfo" },
  98. { CheckEraseQueue, "CheckEraseQueue" },
  99. { CloseMemory, "CloseMemory" },
  100. { DeregisterClient, "DeregisterClient" },
  101. { DeregisterEraseQueue, "DeregisterEraseQueue" },
  102. { GetCardServicesInfo, "GetCardServicesInfo" },
  103. { GetClientInfo, "GetClientInfo" },
  104. { GetConfigurationInfo, "GetConfigurationInfo" },
  105. { GetEventMask, "GetEventMask" },
  106. { GetFirstClient, "GetFirstClient" },
  107. { GetFirstRegion, "GetFirstRegion" },
  108. { GetFirstTuple, "GetFirstTuple" },
  109. { GetNextClient, "GetNextClient" },
  110. { GetNextRegion, "GetNextRegion" },
  111. { GetNextTuple, "GetNextTuple" },
  112. { GetStatus, "GetStatus" },
  113. { GetTupleData, "GetTupleData" },
  114. { MapMemPage, "MapMemPage" },
  115. { ModifyConfiguration, "ModifyConfiguration" },
  116. { ModifyWindow, "ModifyWindow" },
  117. { OpenMemory, "OpenMemory" },
  118. { ParseTuple, "ParseTuple" },
  119. { ReadMemory, "ReadMemory" },
  120. { RegisterClient, "RegisterClient" },
  121. { RegisterEraseQueue, "RegisterEraseQueue" },
  122. { RegisterMTD, "RegisterMTD" },
  123. { ReleaseConfiguration, "ReleaseConfiguration" },
  124. { ReleaseIO, "ReleaseIO" },
  125. { ReleaseIRQ, "ReleaseIRQ" },
  126. { ReleaseWindow, "ReleaseWindow" },
  127. { RequestConfiguration, "RequestConfiguration" },
  128. { RequestIO, "RequestIO" },
  129. { RequestIRQ, "RequestIRQ" },
  130. { RequestSocketMask, "RequestSocketMask" },
  131. { RequestWindow, "RequestWindow" },
  132. { ResetCard, "ResetCard" },
  133. { SetEventMask, "SetEventMask" },
  134. { ValidateCIS, "ValidateCIS" },
  135. { WriteMemory, "WriteMemory" },
  136. { BindDevice, "BindDevice" },
  137. { BindMTD, "BindMTD" },
  138. { ReportError, "ReportError" },
  139. { SuspendCard, "SuspendCard" },
  140. { ResumeCard, "ResumeCard" },
  141. { EjectCard, "EjectCard" },
  142. { InsertCard, "InsertCard" },
  143. { ReplaceCIS, "ReplaceCIS" }
  144. };
  145. static int pcmcia_report_error(struct pcmcia_device *p_dev, error_info_t *err)
  146. {
  147. int i;
  148. char *serv;
  149. if (!p_dev)
  150. printk(KERN_NOTICE);
  151. else
  152. printk(KERN_NOTICE "%s: ", p_dev->dev.bus_id);
  153. for (i = 0; i < ARRAY_SIZE(service_table); i++)
  154. if (service_table[i].key == err->func)
  155. break;
  156. if (i < ARRAY_SIZE(service_table))
  157. serv = service_table[i].msg;
  158. else
  159. serv = "Unknown service number";
  160. for (i = 0; i < ARRAY_SIZE(error_table); i++)
  161. if (error_table[i].key == err->retcode)
  162. break;
  163. if (i < ARRAY_SIZE(error_table))
  164. printk("%s: %s\n", serv, error_table[i].msg);
  165. else
  166. printk("%s: Unknown error code %#x\n", serv, err->retcode);
  167. return CS_SUCCESS;
  168. } /* report_error */
  169. /* end of code which was in cs.c before */
  170. /*======================================================================*/
  171. void cs_error(struct pcmcia_device *p_dev, int func, int ret)
  172. {
  173. error_info_t err = { func, ret };
  174. pcmcia_report_error(p_dev, &err);
  175. }
  176. EXPORT_SYMBOL(cs_error);
  177. static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
  178. {
  179. struct pcmcia_device_id *did = p_drv->id_table;
  180. unsigned int i;
  181. u32 hash;
  182. if (!p_drv->probe || !p_drv->remove)
  183. printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
  184. "function\n", p_drv->drv.name);
  185. while (did && did->match_flags) {
  186. for (i=0; i<4; i++) {
  187. if (!did->prod_id[i])
  188. continue;
  189. hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
  190. if (hash == did->prod_id_hash[i])
  191. continue;
  192. printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
  193. "product string \"%s\": is 0x%x, should "
  194. "be 0x%x\n", p_drv->drv.name, did->prod_id[i],
  195. did->prod_id_hash[i], hash);
  196. printk(KERN_DEBUG "pcmcia: see "
  197. "Documentation/pcmcia/devicetable.txt for "
  198. "details\n");
  199. }
  200. did++;
  201. }
  202. return;
  203. }
  204. /*======================================================================*/
  205. struct pcmcia_dynid {
  206. struct list_head node;
  207. struct pcmcia_device_id id;
  208. };
  209. /**
  210. * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
  211. * @driver: target device driver
  212. * @buf: buffer for scanning device ID data
  213. * @count: input size
  214. *
  215. * Adds a new dynamic PCMCIA device ID to this driver,
  216. * and causes the driver to probe for all devices again.
  217. */
  218. static ssize_t
  219. pcmcia_store_new_id(struct device_driver *driver, const char *buf, size_t count)
  220. {
  221. struct pcmcia_dynid *dynid;
  222. struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
  223. __u16 match_flags, manf_id, card_id;
  224. __u8 func_id, function, device_no;
  225. __u32 prod_id_hash[4] = {0, 0, 0, 0};
  226. int fields=0;
  227. int retval = 0;
  228. fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
  229. &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
  230. &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
  231. if (fields < 6)
  232. return -EINVAL;
  233. dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
  234. if (!dynid)
  235. return -ENOMEM;
  236. INIT_LIST_HEAD(&dynid->node);
  237. dynid->id.match_flags = match_flags;
  238. dynid->id.manf_id = manf_id;
  239. dynid->id.card_id = card_id;
  240. dynid->id.func_id = func_id;
  241. dynid->id.function = function;
  242. dynid->id.device_no = device_no;
  243. memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
  244. spin_lock(&pdrv->dynids.lock);
  245. list_add_tail(&pdrv->dynids.list, &dynid->node);
  246. spin_unlock(&pdrv->dynids.lock);
  247. if (get_driver(&pdrv->drv)) {
  248. retval = driver_attach(&pdrv->drv);
  249. put_driver(&pdrv->drv);
  250. }
  251. if (retval)
  252. return retval;
  253. return count;
  254. }
  255. static DRIVER_ATTR(new_id, S_IWUSR, NULL, pcmcia_store_new_id);
  256. static void
  257. pcmcia_free_dynids(struct pcmcia_driver *drv)
  258. {
  259. struct pcmcia_dynid *dynid, *n;
  260. spin_lock(&drv->dynids.lock);
  261. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  262. list_del(&dynid->node);
  263. kfree(dynid);
  264. }
  265. spin_unlock(&drv->dynids.lock);
  266. }
  267. static int
  268. pcmcia_create_newid_file(struct pcmcia_driver *drv)
  269. {
  270. int error = 0;
  271. if (drv->probe != NULL)
  272. error = driver_create_file(&drv->drv, &driver_attr_new_id);
  273. return error;
  274. }
  275. /**
  276. * pcmcia_register_driver - register a PCMCIA driver with the bus core
  277. * @driver: the &driver being registered
  278. *
  279. * Registers a PCMCIA driver with the PCMCIA bus core.
  280. */
  281. int pcmcia_register_driver(struct pcmcia_driver *driver)
  282. {
  283. int error;
  284. if (!driver)
  285. return -EINVAL;
  286. pcmcia_check_driver(driver);
  287. /* initialize common fields */
  288. driver->drv.bus = &pcmcia_bus_type;
  289. driver->drv.owner = driver->owner;
  290. spin_lock_init(&driver->dynids.lock);
  291. INIT_LIST_HEAD(&driver->dynids.list);
  292. ds_dbg(3, "registering driver %s\n", driver->drv.name);
  293. error = driver_register(&driver->drv);
  294. if (error < 0)
  295. return error;
  296. error = pcmcia_create_newid_file(driver);
  297. if (error)
  298. driver_unregister(&driver->drv);
  299. return error;
  300. }
  301. EXPORT_SYMBOL(pcmcia_register_driver);
  302. /**
  303. * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
  304. * @driver: the &driver being unregistered
  305. */
  306. void pcmcia_unregister_driver(struct pcmcia_driver *driver)
  307. {
  308. ds_dbg(3, "unregistering driver %s\n", driver->drv.name);
  309. driver_unregister(&driver->drv);
  310. pcmcia_free_dynids(driver);
  311. }
  312. EXPORT_SYMBOL(pcmcia_unregister_driver);
  313. /* pcmcia_device handling */
  314. struct pcmcia_device * pcmcia_get_dev(struct pcmcia_device *p_dev)
  315. {
  316. struct device *tmp_dev;
  317. tmp_dev = get_device(&p_dev->dev);
  318. if (!tmp_dev)
  319. return NULL;
  320. return to_pcmcia_dev(tmp_dev);
  321. }
  322. void pcmcia_put_dev(struct pcmcia_device *p_dev)
  323. {
  324. if (p_dev)
  325. put_device(&p_dev->dev);
  326. }
  327. static void pcmcia_release_function(struct kref *ref)
  328. {
  329. struct config_t *c = container_of(ref, struct config_t, ref);
  330. ds_dbg(1, "releasing config_t\n");
  331. kfree(c);
  332. }
  333. static void pcmcia_release_dev(struct device *dev)
  334. {
  335. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  336. ds_dev_dbg(1, dev, "releasing device\n");
  337. pcmcia_put_socket(p_dev->socket);
  338. kfree(p_dev->devname);
  339. kref_put(&p_dev->function_config->ref, pcmcia_release_function);
  340. kfree(p_dev);
  341. }
  342. static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
  343. {
  344. if (!s->pcmcia_state.device_add_pending) {
  345. ds_dev_dbg(1, &s->dev, "scheduling to add %s secondary"
  346. " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
  347. s->pcmcia_state.device_add_pending = 1;
  348. s->pcmcia_state.mfc_pfc = mfc;
  349. schedule_work(&s->device_add);
  350. }
  351. return;
  352. }
  353. static int pcmcia_device_probe(struct device * dev)
  354. {
  355. struct pcmcia_device *p_dev;
  356. struct pcmcia_driver *p_drv;
  357. struct pcmcia_device_id *did;
  358. struct pcmcia_socket *s;
  359. cistpl_config_t cis_config;
  360. int ret = 0;
  361. dev = get_device(dev);
  362. if (!dev)
  363. return -ENODEV;
  364. p_dev = to_pcmcia_dev(dev);
  365. p_drv = to_pcmcia_drv(dev->driver);
  366. s = p_dev->socket;
  367. ds_dev_dbg(1, dev, "trying to bind to %s\n", p_drv->drv.name);
  368. if ((!p_drv->probe) || (!p_dev->function_config) ||
  369. (!try_module_get(p_drv->owner))) {
  370. ret = -EINVAL;
  371. goto put_dev;
  372. }
  373. /* set up some more device information */
  374. ret = pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_CONFIG,
  375. &cis_config);
  376. if (!ret) {
  377. p_dev->conf.ConfigBase = cis_config.base;
  378. p_dev->conf.Present = cis_config.rmask[0];
  379. } else {
  380. dev_printk(KERN_INFO, dev,
  381. "pcmcia: could not parse base and rmask0 of CIS\n");
  382. p_dev->conf.ConfigBase = 0;
  383. p_dev->conf.Present = 0;
  384. }
  385. ret = p_drv->probe(p_dev);
  386. if (ret) {
  387. ds_dev_dbg(1, dev, "binding to %s failed with %d\n",
  388. p_drv->drv.name, ret);
  389. goto put_module;
  390. }
  391. /* handle pseudo multifunction devices:
  392. * there are at most two pseudo multifunction devices.
  393. * if we're matching against the first, schedule a
  394. * call which will then check whether there are two
  395. * pseudo devices, and if not, add the second one.
  396. */
  397. did = p_dev->dev.driver_data;
  398. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  399. (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
  400. pcmcia_add_device_later(p_dev->socket, 0);
  401. put_module:
  402. if (ret)
  403. module_put(p_drv->owner);
  404. put_dev:
  405. if (ret)
  406. put_device(dev);
  407. return (ret);
  408. }
  409. /*
  410. * Removes a PCMCIA card from the device tree and socket list.
  411. */
  412. static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *leftover)
  413. {
  414. struct pcmcia_device *p_dev;
  415. struct pcmcia_device *tmp;
  416. unsigned long flags;
  417. ds_dev_dbg(2, leftover ? &leftover->dev : &s->dev,
  418. "pcmcia_card_remove(%d) %s\n", s->sock,
  419. leftover ? leftover->devname : "");
  420. if (!leftover)
  421. s->device_count = 0;
  422. else
  423. s->device_count = 1;
  424. /* unregister all pcmcia_devices registered with this socket, except leftover */
  425. list_for_each_entry_safe(p_dev, tmp, &s->devices_list, socket_device_list) {
  426. if (p_dev == leftover)
  427. continue;
  428. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  429. list_del(&p_dev->socket_device_list);
  430. p_dev->_removed=1;
  431. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  432. ds_dev_dbg(2, &p_dev->dev, "unregistering device\n");
  433. device_unregister(&p_dev->dev);
  434. }
  435. return;
  436. }
  437. static int pcmcia_device_remove(struct device * dev)
  438. {
  439. struct pcmcia_device *p_dev;
  440. struct pcmcia_driver *p_drv;
  441. struct pcmcia_device_id *did;
  442. int i;
  443. p_dev = to_pcmcia_dev(dev);
  444. p_drv = to_pcmcia_drv(dev->driver);
  445. ds_dev_dbg(1, dev, "removing device\n");
  446. /* If we're removing the primary module driving a
  447. * pseudo multi-function card, we need to unbind
  448. * all devices
  449. */
  450. did = p_dev->dev.driver_data;
  451. if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
  452. (p_dev->socket->device_count != 0) &&
  453. (p_dev->device_no == 0))
  454. pcmcia_card_remove(p_dev->socket, p_dev);
  455. /* detach the "instance" */
  456. if (!p_drv)
  457. return 0;
  458. if (p_drv->remove)
  459. p_drv->remove(p_dev);
  460. p_dev->dev_node = NULL;
  461. /* check for proper unloading */
  462. if (p_dev->_irq || p_dev->_io || p_dev->_locked)
  463. dev_printk(KERN_INFO, dev,
  464. "pcmcia: driver %s did not release config properly\n",
  465. p_drv->drv.name);
  466. for (i = 0; i < MAX_WIN; i++)
  467. if (p_dev->_win & CLIENT_WIN_REQ(i))
  468. dev_printk(KERN_INFO, dev,
  469. "pcmcia: driver %s did not release window properly\n",
  470. p_drv->drv.name);
  471. /* references from pcmcia_probe_device */
  472. pcmcia_put_dev(p_dev);
  473. module_put(p_drv->owner);
  474. return 0;
  475. }
  476. /*
  477. * pcmcia_device_query -- determine information about a pcmcia device
  478. */
  479. static int pcmcia_device_query(struct pcmcia_device *p_dev)
  480. {
  481. cistpl_manfid_t manf_id;
  482. cistpl_funcid_t func_id;
  483. cistpl_vers_1_t *vers1;
  484. unsigned int i;
  485. vers1 = kmalloc(sizeof(*vers1), GFP_KERNEL);
  486. if (!vers1)
  487. return -ENOMEM;
  488. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  489. CISTPL_MANFID, &manf_id)) {
  490. p_dev->manf_id = manf_id.manf;
  491. p_dev->card_id = manf_id.card;
  492. p_dev->has_manf_id = 1;
  493. p_dev->has_card_id = 1;
  494. }
  495. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  496. CISTPL_FUNCID, &func_id)) {
  497. p_dev->func_id = func_id.func;
  498. p_dev->has_func_id = 1;
  499. } else {
  500. /* rule of thumb: cards with no FUNCID, but with
  501. * common memory device geometry information, are
  502. * probably memory cards (from pcmcia-cs) */
  503. cistpl_device_geo_t *devgeo;
  504. devgeo = kmalloc(sizeof(*devgeo), GFP_KERNEL);
  505. if (!devgeo) {
  506. kfree(vers1);
  507. return -ENOMEM;
  508. }
  509. if (!pccard_read_tuple(p_dev->socket, p_dev->func,
  510. CISTPL_DEVICE_GEO, devgeo)) {
  511. ds_dev_dbg(0, &p_dev->dev,
  512. "mem device geometry probably means "
  513. "FUNCID_MEMORY\n");
  514. p_dev->func_id = CISTPL_FUNCID_MEMORY;
  515. p_dev->has_func_id = 1;
  516. }
  517. kfree(devgeo);
  518. }
  519. if (!pccard_read_tuple(p_dev->socket, p_dev->func, CISTPL_VERS_1,
  520. vers1)) {
  521. for (i=0; i < vers1->ns; i++) {
  522. char *tmp;
  523. unsigned int length;
  524. tmp = vers1->str + vers1->ofs[i];
  525. length = strlen(tmp) + 1;
  526. if ((length < 2) || (length > 255))
  527. continue;
  528. p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
  529. GFP_KERNEL);
  530. if (!p_dev->prod_id[i])
  531. continue;
  532. p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
  533. tmp, length);
  534. }
  535. }
  536. kfree(vers1);
  537. return 0;
  538. }
  539. /* device_add_lock is needed to avoid double registration by cardmgr and kernel.
  540. * Serializes pcmcia_device_add; will most likely be removed in future.
  541. *
  542. * While it has the caveat that adding new PCMCIA devices inside(!) device_register()
  543. * won't work, this doesn't matter much at the moment: the driver core doesn't
  544. * support it either.
  545. */
  546. static DEFINE_MUTEX(device_add_lock);
  547. struct pcmcia_device * pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
  548. {
  549. struct pcmcia_device *p_dev, *tmp_dev;
  550. unsigned long flags;
  551. int bus_id_len;
  552. s = pcmcia_get_socket(s);
  553. if (!s)
  554. return NULL;
  555. mutex_lock(&device_add_lock);
  556. ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
  557. /* max of 4 devices per card */
  558. if (s->device_count == 4)
  559. goto err_put;
  560. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  561. if (!p_dev)
  562. goto err_put;
  563. p_dev->socket = s;
  564. p_dev->device_no = (s->device_count++);
  565. p_dev->func = function;
  566. p_dev->dev.bus = &pcmcia_bus_type;
  567. p_dev->dev.parent = s->dev.parent;
  568. p_dev->dev.release = pcmcia_release_dev;
  569. /* by default don't allow DMA */
  570. p_dev->dma_mask = DMA_MASK_NONE;
  571. p_dev->dev.dma_mask = &p_dev->dma_mask;
  572. bus_id_len = sprintf (p_dev->dev.bus_id, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  573. p_dev->devname = kmalloc(6 + bus_id_len + 1, GFP_KERNEL);
  574. if (!p_dev->devname)
  575. goto err_free;
  576. sprintf (p_dev->devname, "pcmcia%s", p_dev->dev.bus_id);
  577. ds_dev_dbg(3, &p_dev->dev, "devname is %s\n", p_dev->devname);
  578. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  579. /*
  580. * p_dev->function_config must be the same for all card functions.
  581. * Note that this is serialized by the device_add_lock, so that
  582. * only one such struct will be created.
  583. */
  584. list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
  585. if (p_dev->func == tmp_dev->func) {
  586. p_dev->function_config = tmp_dev->function_config;
  587. kref_get(&p_dev->function_config->ref);
  588. }
  589. /* Add to the list in pcmcia_bus_socket */
  590. list_add(&p_dev->socket_device_list, &s->devices_list);
  591. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  592. if (!p_dev->function_config) {
  593. ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
  594. p_dev->function_config = kzalloc(sizeof(struct config_t),
  595. GFP_KERNEL);
  596. if (!p_dev->function_config)
  597. goto err_unreg;
  598. kref_init(&p_dev->function_config->ref);
  599. }
  600. dev_printk(KERN_NOTICE, &p_dev->dev,
  601. "pcmcia: registering new device %s\n",
  602. p_dev->devname);
  603. pcmcia_device_query(p_dev);
  604. if (device_register(&p_dev->dev))
  605. goto err_unreg;
  606. mutex_unlock(&device_add_lock);
  607. return p_dev;
  608. err_unreg:
  609. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  610. list_del(&p_dev->socket_device_list);
  611. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  612. err_free:
  613. kfree(p_dev->devname);
  614. kfree(p_dev);
  615. s->device_count--;
  616. err_put:
  617. mutex_unlock(&device_add_lock);
  618. pcmcia_put_socket(s);
  619. return NULL;
  620. }
  621. static int pcmcia_card_add(struct pcmcia_socket *s)
  622. {
  623. cistpl_longlink_mfc_t mfc;
  624. unsigned int no_funcs, i, no_chains;
  625. int ret = 0;
  626. if (!(s->resource_setup_done)) {
  627. ds_dev_dbg(3, &s->dev,
  628. "no resources available, delaying card_add\n");
  629. return -EAGAIN; /* try again, but later... */
  630. }
  631. if (pcmcia_validate_mem(s)) {
  632. ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
  633. "delaying card_add\n");
  634. return -EAGAIN; /* try again, but later... */
  635. }
  636. ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
  637. if (ret || !no_chains) {
  638. ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
  639. return -ENODEV;
  640. }
  641. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  642. no_funcs = mfc.nfn;
  643. else
  644. no_funcs = 1;
  645. s->functions = no_funcs;
  646. for (i=0; i < no_funcs; i++)
  647. pcmcia_device_add(s, i);
  648. return (ret);
  649. }
  650. static void pcmcia_delayed_add_device(struct work_struct *work)
  651. {
  652. struct pcmcia_socket *s =
  653. container_of(work, struct pcmcia_socket, device_add);
  654. ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
  655. pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
  656. s->pcmcia_state.device_add_pending = 0;
  657. s->pcmcia_state.mfc_pfc = 0;
  658. }
  659. static int pcmcia_requery(struct device *dev, void * _data)
  660. {
  661. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  662. if (!p_dev->dev.driver) {
  663. ds_dev_dbg(1, dev, "update device information\n");
  664. pcmcia_device_query(p_dev);
  665. }
  666. return 0;
  667. }
  668. static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
  669. {
  670. int no_devices = 0;
  671. int ret = 0;
  672. unsigned long flags;
  673. /* must be called with skt_mutex held */
  674. ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
  675. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  676. if (list_empty(&skt->devices_list))
  677. no_devices = 1;
  678. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  679. /* If this is because of a CIS override, start over */
  680. if (new_cis && !no_devices)
  681. pcmcia_card_remove(skt, NULL);
  682. /* if no devices were added for this socket yet because of
  683. * missing resource information or other trouble, we need to
  684. * do this now. */
  685. if (no_devices || new_cis) {
  686. ret = pcmcia_card_add(skt);
  687. if (ret)
  688. return;
  689. }
  690. /* some device information might have changed because of a CIS
  691. * update or because we can finally read it correctly... so
  692. * determine it again, overwriting old values if necessary. */
  693. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  694. /* we re-scan all devices, not just the ones connected to this
  695. * socket. This does not matter, though. */
  696. ret = bus_rescan_devices(&pcmcia_bus_type);
  697. if (ret)
  698. printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
  699. }
  700. #ifdef CONFIG_PCMCIA_LOAD_CIS
  701. /**
  702. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  703. * @dev: the pcmcia device which needs a CIS override
  704. * @filename: requested filename in /lib/firmware/
  705. *
  706. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  707. * the one provided by the card is broken. The firmware files reside in
  708. * /lib/firmware/ in userspace.
  709. */
  710. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  711. {
  712. struct pcmcia_socket *s = dev->socket;
  713. const struct firmware *fw;
  714. char path[FIRMWARE_NAME_MAX];
  715. int ret = -ENOMEM;
  716. int no_funcs;
  717. int old_funcs;
  718. cistpl_longlink_mfc_t mfc;
  719. if (!filename)
  720. return -EINVAL;
  721. ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
  722. if (strlen(filename) > (FIRMWARE_NAME_MAX - 1)) {
  723. dev_printk(KERN_WARNING, &dev->dev,
  724. "pcmcia: CIS filename is too long [%s]\n",
  725. filename);
  726. return -EINVAL;
  727. }
  728. snprintf(path, sizeof(path), "%s", filename);
  729. if (request_firmware(&fw, path, &dev->dev) == 0) {
  730. if (fw->size >= CISTPL_MAX_CIS_SIZE) {
  731. ret = -EINVAL;
  732. dev_printk(KERN_ERR, &dev->dev,
  733. "pcmcia: CIS override is too big\n");
  734. goto release;
  735. }
  736. if (!pcmcia_replace_cis(s, fw->data, fw->size))
  737. ret = 0;
  738. else {
  739. dev_printk(KERN_ERR, &dev->dev,
  740. "pcmcia: CIS override failed\n");
  741. goto release;
  742. }
  743. /* update information */
  744. pcmcia_device_query(dev);
  745. /* does this cis override add or remove functions? */
  746. old_funcs = s->functions;
  747. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  748. no_funcs = mfc.nfn;
  749. else
  750. no_funcs = 1;
  751. s->functions = no_funcs;
  752. if (old_funcs > no_funcs)
  753. pcmcia_card_remove(s, dev);
  754. else if (no_funcs > old_funcs)
  755. pcmcia_add_device_later(s, 1);
  756. }
  757. release:
  758. release_firmware(fw);
  759. return (ret);
  760. }
  761. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  762. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  763. {
  764. return -ENODEV;
  765. }
  766. #endif
  767. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  768. struct pcmcia_device_id *did)
  769. {
  770. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  771. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  772. return 0;
  773. }
  774. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  775. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  776. return 0;
  777. }
  778. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  779. if (dev->func != did->function)
  780. return 0;
  781. }
  782. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  783. if (!dev->prod_id[0])
  784. return 0;
  785. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  786. return 0;
  787. }
  788. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  789. if (!dev->prod_id[1])
  790. return 0;
  791. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  792. return 0;
  793. }
  794. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  795. if (!dev->prod_id[2])
  796. return 0;
  797. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  798. return 0;
  799. }
  800. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  801. if (!dev->prod_id[3])
  802. return 0;
  803. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  804. return 0;
  805. }
  806. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  807. if (dev->device_no != did->device_no)
  808. return 0;
  809. }
  810. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  811. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  812. return 0;
  813. /* if this is a pseudo-multi-function device,
  814. * we need explicit matches */
  815. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  816. return 0;
  817. if (dev->device_no)
  818. return 0;
  819. /* also, FUNC_ID matching needs to be activated by userspace
  820. * after it has re-checked that there is no possible module
  821. * with a prod_id/manf_id/card_id match.
  822. */
  823. ds_dev_dbg(0, &dev->dev,
  824. "skipping FUNC_ID match until userspace interaction\n");
  825. if (!dev->allow_func_id_match)
  826. return 0;
  827. }
  828. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  829. ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
  830. if (!dev->socket->fake_cis)
  831. pcmcia_load_firmware(dev, did->cisfile);
  832. if (!dev->socket->fake_cis)
  833. return 0;
  834. }
  835. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  836. int i;
  837. for (i=0; i<4; i++)
  838. if (dev->prod_id[i])
  839. return 0;
  840. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  841. return 0;
  842. }
  843. dev->dev.driver_data = (void *) did;
  844. return 1;
  845. }
  846. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  847. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  848. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  849. struct pcmcia_device_id *did = p_drv->id_table;
  850. struct pcmcia_dynid *dynid;
  851. /* match dynamic devices first */
  852. spin_lock(&p_drv->dynids.lock);
  853. list_for_each_entry(dynid, &p_drv->dynids.list, node) {
  854. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  855. if (pcmcia_devmatch(p_dev, &dynid->id)) {
  856. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  857. spin_unlock(&p_drv->dynids.lock);
  858. return 1;
  859. }
  860. }
  861. spin_unlock(&p_drv->dynids.lock);
  862. #ifdef CONFIG_PCMCIA_IOCTL
  863. /* matching by cardmgr */
  864. if (p_dev->cardmgr == p_drv) {
  865. ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
  866. return 1;
  867. }
  868. #endif
  869. while (did && did->match_flags) {
  870. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  871. if (pcmcia_devmatch(p_dev, did)) {
  872. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  873. return 1;
  874. }
  875. did++;
  876. }
  877. return 0;
  878. }
  879. #ifdef CONFIG_HOTPLUG
  880. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  881. {
  882. struct pcmcia_device *p_dev;
  883. int i;
  884. u32 hash[4] = { 0, 0, 0, 0};
  885. if (!dev)
  886. return -ENODEV;
  887. p_dev = to_pcmcia_dev(dev);
  888. /* calculate hashes */
  889. for (i=0; i<4; i++) {
  890. if (!p_dev->prod_id[i])
  891. continue;
  892. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  893. }
  894. if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
  895. return -ENOMEM;
  896. if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
  897. return -ENOMEM;
  898. if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  899. "pa%08Xpb%08Xpc%08Xpd%08X",
  900. p_dev->has_manf_id ? p_dev->manf_id : 0,
  901. p_dev->has_card_id ? p_dev->card_id : 0,
  902. p_dev->has_func_id ? p_dev->func_id : 0,
  903. p_dev->func,
  904. p_dev->device_no,
  905. hash[0],
  906. hash[1],
  907. hash[2],
  908. hash[3]))
  909. return -ENOMEM;
  910. return 0;
  911. }
  912. #else
  913. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  914. {
  915. return -ENODEV;
  916. }
  917. #endif
  918. /************************ runtime PM support ***************************/
  919. static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
  920. static int pcmcia_dev_resume(struct device *dev);
  921. static int runtime_suspend(struct device *dev)
  922. {
  923. int rc;
  924. down(&dev->sem);
  925. rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
  926. up(&dev->sem);
  927. return rc;
  928. }
  929. static void runtime_resume(struct device *dev)
  930. {
  931. int rc;
  932. down(&dev->sem);
  933. rc = pcmcia_dev_resume(dev);
  934. up(&dev->sem);
  935. }
  936. /************************ per-device sysfs output ***************************/
  937. #define pcmcia_device_attr(field, test, format) \
  938. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  939. { \
  940. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  941. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  942. }
  943. #define pcmcia_device_stringattr(name, field) \
  944. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  945. { \
  946. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  947. return p_dev->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  948. }
  949. pcmcia_device_attr(func, socket, "0x%02x\n");
  950. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  951. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  952. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  953. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  954. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  955. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  956. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  957. static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
  958. {
  959. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  960. if (p_dev->suspended)
  961. return sprintf(buf, "off\n");
  962. else
  963. return sprintf(buf, "on\n");
  964. }
  965. static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
  966. const char *buf, size_t count)
  967. {
  968. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  969. int ret = 0;
  970. if (!count)
  971. return -EINVAL;
  972. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  973. ret = runtime_suspend(dev);
  974. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  975. runtime_resume(dev);
  976. return ret ? ret : count;
  977. }
  978. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  979. {
  980. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  981. int i;
  982. u32 hash[4] = { 0, 0, 0, 0};
  983. /* calculate hashes */
  984. for (i=0; i<4; i++) {
  985. if (!p_dev->prod_id[i])
  986. continue;
  987. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  988. }
  989. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  990. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  991. p_dev->has_manf_id ? p_dev->manf_id : 0,
  992. p_dev->has_card_id ? p_dev->card_id : 0,
  993. p_dev->has_func_id ? p_dev->func_id : 0,
  994. p_dev->func, p_dev->device_no,
  995. hash[0], hash[1], hash[2], hash[3]);
  996. }
  997. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  998. struct device_attribute *attr, const char *buf, size_t count)
  999. {
  1000. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1001. int ret;
  1002. if (!count)
  1003. return -EINVAL;
  1004. mutex_lock(&p_dev->socket->skt_mutex);
  1005. p_dev->allow_func_id_match = 1;
  1006. mutex_unlock(&p_dev->socket->skt_mutex);
  1007. ret = bus_rescan_devices(&pcmcia_bus_type);
  1008. if (ret)
  1009. printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
  1010. "allowing func_id matches\n");
  1011. return count;
  1012. }
  1013. static struct device_attribute pcmcia_dev_attrs[] = {
  1014. __ATTR(function, 0444, func_show, NULL),
  1015. __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
  1016. __ATTR_RO(func_id),
  1017. __ATTR_RO(manf_id),
  1018. __ATTR_RO(card_id),
  1019. __ATTR_RO(prod_id1),
  1020. __ATTR_RO(prod_id2),
  1021. __ATTR_RO(prod_id3),
  1022. __ATTR_RO(prod_id4),
  1023. __ATTR_RO(modalias),
  1024. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  1025. __ATTR_NULL,
  1026. };
  1027. /* PM support, also needed for reset */
  1028. static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
  1029. {
  1030. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1031. struct pcmcia_driver *p_drv = NULL;
  1032. int ret = 0;
  1033. if (p_dev->suspended)
  1034. return 0;
  1035. ds_dev_dbg(2, dev, "suspending\n");
  1036. if (dev->driver)
  1037. p_drv = to_pcmcia_drv(dev->driver);
  1038. if (!p_drv)
  1039. goto out;
  1040. if (p_drv->suspend) {
  1041. ret = p_drv->suspend(p_dev);
  1042. if (ret) {
  1043. dev_printk(KERN_ERR, dev,
  1044. "pcmcia: device %s (driver %s) did "
  1045. "not want to go to sleep (%d)\n",
  1046. p_dev->devname, p_drv->drv.name, ret);
  1047. goto out;
  1048. }
  1049. }
  1050. if (p_dev->device_no == p_dev->func) {
  1051. ds_dev_dbg(2, dev, "releasing configuration\n");
  1052. pcmcia_release_configuration(p_dev);
  1053. }
  1054. out:
  1055. if (!ret)
  1056. p_dev->suspended = 1;
  1057. return ret;
  1058. }
  1059. static int pcmcia_dev_resume(struct device * dev)
  1060. {
  1061. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1062. struct pcmcia_driver *p_drv = NULL;
  1063. int ret = 0;
  1064. if (!p_dev->suspended)
  1065. return 0;
  1066. ds_dev_dbg(2, dev, "resuming\n");
  1067. if (dev->driver)
  1068. p_drv = to_pcmcia_drv(dev->driver);
  1069. if (!p_drv)
  1070. goto out;
  1071. if (p_dev->device_no == p_dev->func) {
  1072. ds_dev_dbg(2, dev, "requesting configuration\n");
  1073. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  1074. if (ret)
  1075. goto out;
  1076. }
  1077. if (p_drv->resume)
  1078. ret = p_drv->resume(p_dev);
  1079. out:
  1080. if (!ret)
  1081. p_dev->suspended = 0;
  1082. return ret;
  1083. }
  1084. static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
  1085. {
  1086. struct pcmcia_socket *skt = _data;
  1087. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1088. if (p_dev->socket != skt || p_dev->suspended)
  1089. return 0;
  1090. return runtime_suspend(dev);
  1091. }
  1092. static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
  1093. {
  1094. struct pcmcia_socket *skt = _data;
  1095. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1096. if (p_dev->socket != skt || !p_dev->suspended)
  1097. return 0;
  1098. runtime_resume(dev);
  1099. return 0;
  1100. }
  1101. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  1102. {
  1103. ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
  1104. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  1105. return 0;
  1106. }
  1107. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  1108. {
  1109. ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
  1110. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  1111. pcmcia_bus_suspend_callback)) {
  1112. pcmcia_bus_resume(skt);
  1113. return -EIO;
  1114. }
  1115. return 0;
  1116. }
  1117. /*======================================================================
  1118. The card status event handler.
  1119. ======================================================================*/
  1120. /* Normally, the event is passed to individual drivers after
  1121. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  1122. * is inversed to maintain historic compatibility.
  1123. */
  1124. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  1125. {
  1126. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  1127. if (!s) {
  1128. dev_printk(KERN_ERR, &skt->dev,
  1129. "PCMCIA obtaining reference to socket " \
  1130. "failed, event 0x%x lost!\n", event);
  1131. return -ENODEV;
  1132. }
  1133. ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
  1134. event, priority, skt);
  1135. switch (event) {
  1136. case CS_EVENT_CARD_REMOVAL:
  1137. s->pcmcia_state.present = 0;
  1138. pcmcia_card_remove(skt, NULL);
  1139. handle_event(skt, event);
  1140. break;
  1141. case CS_EVENT_CARD_INSERTION:
  1142. s->pcmcia_state.present = 1;
  1143. pcmcia_card_add(skt);
  1144. handle_event(skt, event);
  1145. break;
  1146. case CS_EVENT_EJECTION_REQUEST:
  1147. break;
  1148. case CS_EVENT_PM_SUSPEND:
  1149. case CS_EVENT_PM_RESUME:
  1150. case CS_EVENT_RESET_PHYSICAL:
  1151. case CS_EVENT_CARD_RESET:
  1152. default:
  1153. handle_event(skt, event);
  1154. break;
  1155. }
  1156. pcmcia_put_socket(s);
  1157. return 0;
  1158. } /* ds_event */
  1159. struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
  1160. {
  1161. struct pcmcia_device *p_dev;
  1162. struct pcmcia_device *ret = NULL;
  1163. p_dev = pcmcia_get_dev(_p_dev);
  1164. if (!p_dev)
  1165. return NULL;
  1166. if (!p_dev->socket->pcmcia_state.present)
  1167. goto out;
  1168. if (p_dev->_removed)
  1169. goto out;
  1170. if (p_dev->suspended)
  1171. goto out;
  1172. ret = p_dev;
  1173. out:
  1174. pcmcia_put_dev(p_dev);
  1175. return ret;
  1176. }
  1177. EXPORT_SYMBOL(pcmcia_dev_present);
  1178. static struct pcmcia_callback pcmcia_bus_callback = {
  1179. .owner = THIS_MODULE,
  1180. .event = ds_event,
  1181. .requery = pcmcia_bus_rescan,
  1182. .suspend = pcmcia_bus_suspend,
  1183. .resume = pcmcia_bus_resume,
  1184. };
  1185. static int __devinit pcmcia_bus_add_socket(struct device *dev,
  1186. struct class_interface *class_intf)
  1187. {
  1188. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1189. int ret;
  1190. socket = pcmcia_get_socket(socket);
  1191. if (!socket) {
  1192. dev_printk(KERN_ERR, dev,
  1193. "PCMCIA obtaining reference to socket failed\n");
  1194. return -ENODEV;
  1195. }
  1196. /*
  1197. * Ugly. But we want to wait for the socket threads to have started up.
  1198. * We really should let the drivers themselves drive some of this..
  1199. */
  1200. msleep(250);
  1201. #ifdef CONFIG_PCMCIA_IOCTL
  1202. init_waitqueue_head(&socket->queue);
  1203. #endif
  1204. INIT_LIST_HEAD(&socket->devices_list);
  1205. INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
  1206. memset(&socket->pcmcia_state, 0, sizeof(u8));
  1207. socket->device_count = 0;
  1208. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1209. if (ret) {
  1210. dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
  1211. pcmcia_put_socket(socket);
  1212. return (ret);
  1213. }
  1214. return 0;
  1215. }
  1216. static void pcmcia_bus_remove_socket(struct device *dev,
  1217. struct class_interface *class_intf)
  1218. {
  1219. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1220. if (!socket)
  1221. return;
  1222. socket->pcmcia_state.dead = 1;
  1223. pccard_register_pcmcia(socket, NULL);
  1224. /* unregister any unbound devices */
  1225. mutex_lock(&socket->skt_mutex);
  1226. pcmcia_card_remove(socket, NULL);
  1227. mutex_unlock(&socket->skt_mutex);
  1228. pcmcia_put_socket(socket);
  1229. return;
  1230. }
  1231. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1232. static struct class_interface pcmcia_bus_interface __refdata = {
  1233. .class = &pcmcia_socket_class,
  1234. .add_dev = &pcmcia_bus_add_socket,
  1235. .remove_dev = &pcmcia_bus_remove_socket,
  1236. };
  1237. struct bus_type pcmcia_bus_type = {
  1238. .name = "pcmcia",
  1239. .uevent = pcmcia_bus_uevent,
  1240. .match = pcmcia_bus_match,
  1241. .dev_attrs = pcmcia_dev_attrs,
  1242. .probe = pcmcia_device_probe,
  1243. .remove = pcmcia_device_remove,
  1244. .suspend = pcmcia_dev_suspend,
  1245. .resume = pcmcia_dev_resume,
  1246. };
  1247. static int __init init_pcmcia_bus(void)
  1248. {
  1249. int ret;
  1250. spin_lock_init(&pcmcia_dev_list_lock);
  1251. ret = bus_register(&pcmcia_bus_type);
  1252. if (ret < 0) {
  1253. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1254. return ret;
  1255. }
  1256. ret = class_interface_register(&pcmcia_bus_interface);
  1257. if (ret < 0) {
  1258. printk(KERN_WARNING
  1259. "pcmcia: class_interface_register error: %d\n", ret);
  1260. bus_unregister(&pcmcia_bus_type);
  1261. return ret;
  1262. }
  1263. pcmcia_setup_ioctl();
  1264. return 0;
  1265. }
  1266. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1267. * pcmcia_socket_class is already registered */
  1268. static void __exit exit_pcmcia_bus(void)
  1269. {
  1270. pcmcia_cleanup_ioctl();
  1271. class_interface_unregister(&pcmcia_bus_interface);
  1272. bus_unregister(&pcmcia_bus_type);
  1273. }
  1274. module_exit(exit_pcmcia_bus);
  1275. MODULE_ALIAS("ds");