ds.c 39 KB

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