ds.c 39 KB

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