ds.c 38 KB

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