ds.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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_set_drvdata(dev)
  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 = dev_get_drvdata(&p_dev->dev);
  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 = dev_get_drvdata(&p_dev->dev);
  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. s = pcmcia_get_socket(s);
  519. if (!s)
  520. return NULL;
  521. mutex_lock(&device_add_lock);
  522. ds_dbg(3, "adding device to %d, function %d\n", s->sock, function);
  523. /* max of 4 devices per card */
  524. if (s->device_count == 4)
  525. goto err_put;
  526. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  527. if (!p_dev)
  528. goto err_put;
  529. p_dev->socket = s;
  530. p_dev->device_no = (s->device_count++);
  531. p_dev->func = function;
  532. p_dev->dev.bus = &pcmcia_bus_type;
  533. p_dev->dev.parent = s->dev.parent;
  534. p_dev->dev.release = pcmcia_release_dev;
  535. /* by default don't allow DMA */
  536. p_dev->dma_mask = DMA_MASK_NONE;
  537. p_dev->dev.dma_mask = &p_dev->dma_mask;
  538. dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  539. if (!dev_name(&p_dev->dev))
  540. goto err_free;
  541. p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
  542. if (!p_dev->devname)
  543. goto err_free;
  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. p_dev->io = tmp_dev->io;
  555. p_dev->irq = tmp_dev->irq;
  556. kref_get(&p_dev->function_config->ref);
  557. }
  558. /* Add to the list in pcmcia_bus_socket */
  559. list_add(&p_dev->socket_device_list, &s->devices_list);
  560. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  561. if (!p_dev->function_config) {
  562. ds_dev_dbg(3, &p_dev->dev, "creating config_t\n");
  563. p_dev->function_config = kzalloc(sizeof(struct config_t),
  564. GFP_KERNEL);
  565. if (!p_dev->function_config)
  566. goto err_unreg;
  567. kref_init(&p_dev->function_config->ref);
  568. }
  569. dev_printk(KERN_NOTICE, &p_dev->dev,
  570. "pcmcia: registering new device %s\n",
  571. p_dev->devname);
  572. pcmcia_device_query(p_dev);
  573. if (device_register(&p_dev->dev))
  574. goto err_unreg;
  575. mutex_unlock(&device_add_lock);
  576. return p_dev;
  577. err_unreg:
  578. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  579. list_del(&p_dev->socket_device_list);
  580. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  581. err_free:
  582. kfree(p_dev->devname);
  583. kfree(p_dev);
  584. s->device_count--;
  585. err_put:
  586. mutex_unlock(&device_add_lock);
  587. pcmcia_put_socket(s);
  588. return NULL;
  589. }
  590. static int pcmcia_card_add(struct pcmcia_socket *s)
  591. {
  592. cistpl_longlink_mfc_t mfc;
  593. unsigned int no_funcs, i, no_chains;
  594. int ret = 0;
  595. if (!(s->resource_setup_done)) {
  596. ds_dev_dbg(3, &s->dev,
  597. "no resources available, delaying card_add\n");
  598. return -EAGAIN; /* try again, but later... */
  599. }
  600. if (pcmcia_validate_mem(s)) {
  601. ds_dev_dbg(3, &s->dev, "validating mem resources failed, "
  602. "delaying card_add\n");
  603. return -EAGAIN; /* try again, but later... */
  604. }
  605. ret = pccard_validate_cis(s, BIND_FN_ALL, &no_chains);
  606. if (ret || !no_chains) {
  607. ds_dev_dbg(0, &s->dev, "invalid CIS or invalid resources\n");
  608. return -ENODEV;
  609. }
  610. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  611. no_funcs = mfc.nfn;
  612. else
  613. no_funcs = 1;
  614. s->functions = no_funcs;
  615. for (i=0; i < no_funcs; i++)
  616. pcmcia_device_add(s, i);
  617. return (ret);
  618. }
  619. static void pcmcia_delayed_add_device(struct work_struct *work)
  620. {
  621. struct pcmcia_socket *s =
  622. container_of(work, struct pcmcia_socket, device_add);
  623. ds_dev_dbg(1, &s->dev, "adding additional device to %d\n", s->sock);
  624. pcmcia_device_add(s, s->pcmcia_state.mfc_pfc);
  625. s->pcmcia_state.device_add_pending = 0;
  626. s->pcmcia_state.mfc_pfc = 0;
  627. }
  628. static int pcmcia_requery(struct device *dev, void * _data)
  629. {
  630. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  631. if (!p_dev->dev.driver) {
  632. ds_dev_dbg(1, dev, "update device information\n");
  633. pcmcia_device_query(p_dev);
  634. }
  635. return 0;
  636. }
  637. static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
  638. {
  639. int no_devices = 0;
  640. int ret = 0;
  641. unsigned long flags;
  642. /* must be called with skt_mutex held */
  643. ds_dev_dbg(0, &skt->dev, "re-scanning socket %d\n", skt->sock);
  644. spin_lock_irqsave(&pcmcia_dev_list_lock, flags);
  645. if (list_empty(&skt->devices_list))
  646. no_devices = 1;
  647. spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
  648. /* If this is because of a CIS override, start over */
  649. if (new_cis && !no_devices)
  650. pcmcia_card_remove(skt, NULL);
  651. /* if no devices were added for this socket yet because of
  652. * missing resource information or other trouble, we need to
  653. * do this now. */
  654. if (no_devices || new_cis) {
  655. ret = pcmcia_card_add(skt);
  656. if (ret)
  657. return;
  658. }
  659. /* some device information might have changed because of a CIS
  660. * update or because we can finally read it correctly... so
  661. * determine it again, overwriting old values if necessary. */
  662. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
  663. /* we re-scan all devices, not just the ones connected to this
  664. * socket. This does not matter, though. */
  665. ret = bus_rescan_devices(&pcmcia_bus_type);
  666. if (ret)
  667. printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
  668. }
  669. #ifdef CONFIG_PCMCIA_LOAD_CIS
  670. /**
  671. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  672. * @dev: the pcmcia device which needs a CIS override
  673. * @filename: requested filename in /lib/firmware/
  674. *
  675. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  676. * the one provided by the card is broken. The firmware files reside in
  677. * /lib/firmware/ in userspace.
  678. */
  679. static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  680. {
  681. struct pcmcia_socket *s = dev->socket;
  682. const struct firmware *fw;
  683. int ret = -ENOMEM;
  684. int no_funcs;
  685. int old_funcs;
  686. cistpl_longlink_mfc_t mfc;
  687. if (!filename)
  688. return -EINVAL;
  689. ds_dev_dbg(1, &dev->dev, "trying to load CIS file %s\n", filename);
  690. if (request_firmware(&fw, filename, &dev->dev) == 0) {
  691. if (fw->size >= CISTPL_MAX_CIS_SIZE) {
  692. ret = -EINVAL;
  693. dev_printk(KERN_ERR, &dev->dev,
  694. "pcmcia: CIS override is too big\n");
  695. goto release;
  696. }
  697. if (!pcmcia_replace_cis(s, fw->data, fw->size))
  698. ret = 0;
  699. else {
  700. dev_printk(KERN_ERR, &dev->dev,
  701. "pcmcia: CIS override failed\n");
  702. goto release;
  703. }
  704. /* update information */
  705. pcmcia_device_query(dev);
  706. /* does this cis override add or remove functions? */
  707. old_funcs = s->functions;
  708. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  709. no_funcs = mfc.nfn;
  710. else
  711. no_funcs = 1;
  712. s->functions = no_funcs;
  713. if (old_funcs > no_funcs)
  714. pcmcia_card_remove(s, dev);
  715. else if (no_funcs > old_funcs)
  716. pcmcia_add_device_later(s, 1);
  717. }
  718. release:
  719. release_firmware(fw);
  720. return (ret);
  721. }
  722. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  723. static inline int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
  724. {
  725. return -ENODEV;
  726. }
  727. #endif
  728. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  729. struct pcmcia_device_id *did)
  730. {
  731. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  732. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  733. return 0;
  734. }
  735. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  736. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  737. return 0;
  738. }
  739. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  740. if (dev->func != did->function)
  741. return 0;
  742. }
  743. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  744. if (!dev->prod_id[0])
  745. return 0;
  746. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  747. return 0;
  748. }
  749. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  750. if (!dev->prod_id[1])
  751. return 0;
  752. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  753. return 0;
  754. }
  755. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  756. if (!dev->prod_id[2])
  757. return 0;
  758. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  759. return 0;
  760. }
  761. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  762. if (!dev->prod_id[3])
  763. return 0;
  764. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  765. return 0;
  766. }
  767. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  768. if (dev->device_no != did->device_no)
  769. return 0;
  770. }
  771. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  772. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  773. return 0;
  774. /* if this is a pseudo-multi-function device,
  775. * we need explicit matches */
  776. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
  777. return 0;
  778. if (dev->device_no)
  779. return 0;
  780. /* also, FUNC_ID matching needs to be activated by userspace
  781. * after it has re-checked that there is no possible module
  782. * with a prod_id/manf_id/card_id match.
  783. */
  784. ds_dev_dbg(0, &dev->dev,
  785. "skipping FUNC_ID match until userspace interaction\n");
  786. if (!dev->allow_func_id_match)
  787. return 0;
  788. }
  789. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  790. ds_dev_dbg(0, &dev->dev, "device needs a fake CIS\n");
  791. if (!dev->socket->fake_cis)
  792. pcmcia_load_firmware(dev, did->cisfile);
  793. if (!dev->socket->fake_cis)
  794. return 0;
  795. }
  796. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  797. int i;
  798. for (i=0; i<4; i++)
  799. if (dev->prod_id[i])
  800. return 0;
  801. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  802. return 0;
  803. }
  804. dev_set_drvdata(&dev->dev, did);
  805. return 1;
  806. }
  807. static int pcmcia_bus_match(struct device * dev, struct device_driver * drv) {
  808. struct pcmcia_device * p_dev = to_pcmcia_dev(dev);
  809. struct pcmcia_driver * p_drv = to_pcmcia_drv(drv);
  810. struct pcmcia_device_id *did = p_drv->id_table;
  811. struct pcmcia_dynid *dynid;
  812. /* match dynamic devices first */
  813. spin_lock(&p_drv->dynids.lock);
  814. list_for_each_entry(dynid, &p_drv->dynids.list, node) {
  815. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  816. if (pcmcia_devmatch(p_dev, &dynid->id)) {
  817. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  818. spin_unlock(&p_drv->dynids.lock);
  819. return 1;
  820. }
  821. }
  822. spin_unlock(&p_drv->dynids.lock);
  823. #ifdef CONFIG_PCMCIA_IOCTL
  824. /* matching by cardmgr */
  825. if (p_dev->cardmgr == p_drv) {
  826. ds_dev_dbg(0, dev, "cardmgr matched to %s\n", drv->name);
  827. return 1;
  828. }
  829. #endif
  830. while (did && did->match_flags) {
  831. ds_dev_dbg(3, dev, "trying to match to %s\n", drv->name);
  832. if (pcmcia_devmatch(p_dev, did)) {
  833. ds_dev_dbg(0, dev, "matched to %s\n", drv->name);
  834. return 1;
  835. }
  836. did++;
  837. }
  838. return 0;
  839. }
  840. #ifdef CONFIG_HOTPLUG
  841. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  842. {
  843. struct pcmcia_device *p_dev;
  844. int i;
  845. u32 hash[4] = { 0, 0, 0, 0};
  846. if (!dev)
  847. return -ENODEV;
  848. p_dev = to_pcmcia_dev(dev);
  849. /* calculate hashes */
  850. for (i=0; i<4; i++) {
  851. if (!p_dev->prod_id[i])
  852. continue;
  853. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  854. }
  855. if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
  856. return -ENOMEM;
  857. if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
  858. return -ENOMEM;
  859. if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  860. "pa%08Xpb%08Xpc%08Xpd%08X",
  861. p_dev->has_manf_id ? p_dev->manf_id : 0,
  862. p_dev->has_card_id ? p_dev->card_id : 0,
  863. p_dev->has_func_id ? p_dev->func_id : 0,
  864. p_dev->func,
  865. p_dev->device_no,
  866. hash[0],
  867. hash[1],
  868. hash[2],
  869. hash[3]))
  870. return -ENOMEM;
  871. return 0;
  872. }
  873. #else
  874. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  875. {
  876. return -ENODEV;
  877. }
  878. #endif
  879. /************************ runtime PM support ***************************/
  880. static int pcmcia_dev_suspend(struct device *dev, pm_message_t state);
  881. static int pcmcia_dev_resume(struct device *dev);
  882. static int runtime_suspend(struct device *dev)
  883. {
  884. int rc;
  885. down(&dev->sem);
  886. rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
  887. up(&dev->sem);
  888. return rc;
  889. }
  890. static void runtime_resume(struct device *dev)
  891. {
  892. int rc;
  893. down(&dev->sem);
  894. rc = pcmcia_dev_resume(dev);
  895. up(&dev->sem);
  896. }
  897. /************************ per-device sysfs output ***************************/
  898. #define pcmcia_device_attr(field, test, format) \
  899. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  900. { \
  901. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  902. return p_dev->test ? sprintf (buf, format, p_dev->field) : -ENODEV; \
  903. }
  904. #define pcmcia_device_stringattr(name, field) \
  905. static ssize_t name##_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->field ? sprintf (buf, "%s\n", p_dev->field) : -ENODEV; \
  909. }
  910. pcmcia_device_attr(func, socket, "0x%02x\n");
  911. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  912. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  913. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  914. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  915. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  916. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  917. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  918. static ssize_t pcmcia_show_pm_state(struct device *dev, struct device_attribute *attr, char *buf)
  919. {
  920. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  921. if (p_dev->suspended)
  922. return sprintf(buf, "off\n");
  923. else
  924. return sprintf(buf, "on\n");
  925. }
  926. static ssize_t pcmcia_store_pm_state(struct device *dev, struct device_attribute *attr,
  927. const char *buf, size_t count)
  928. {
  929. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  930. int ret = 0;
  931. if (!count)
  932. return -EINVAL;
  933. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  934. ret = runtime_suspend(dev);
  935. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  936. runtime_resume(dev);
  937. return ret ? ret : count;
  938. }
  939. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  940. {
  941. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  942. int i;
  943. u32 hash[4] = { 0, 0, 0, 0};
  944. /* calculate hashes */
  945. for (i=0; i<4; i++) {
  946. if (!p_dev->prod_id[i])
  947. continue;
  948. hash[i] = crc32(0,p_dev->prod_id[i],strlen(p_dev->prod_id[i]));
  949. }
  950. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  951. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  952. p_dev->has_manf_id ? p_dev->manf_id : 0,
  953. p_dev->has_card_id ? p_dev->card_id : 0,
  954. p_dev->has_func_id ? p_dev->func_id : 0,
  955. p_dev->func, p_dev->device_no,
  956. hash[0], hash[1], hash[2], hash[3]);
  957. }
  958. static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
  959. struct device_attribute *attr, const char *buf, size_t count)
  960. {
  961. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  962. int ret;
  963. if (!count)
  964. return -EINVAL;
  965. mutex_lock(&p_dev->socket->skt_mutex);
  966. p_dev->allow_func_id_match = 1;
  967. mutex_unlock(&p_dev->socket->skt_mutex);
  968. ret = bus_rescan_devices(&pcmcia_bus_type);
  969. if (ret)
  970. printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
  971. "allowing func_id matches\n");
  972. return count;
  973. }
  974. static struct device_attribute pcmcia_dev_attrs[] = {
  975. __ATTR(function, 0444, func_show, NULL),
  976. __ATTR(pm_state, 0644, pcmcia_show_pm_state, pcmcia_store_pm_state),
  977. __ATTR_RO(func_id),
  978. __ATTR_RO(manf_id),
  979. __ATTR_RO(card_id),
  980. __ATTR_RO(prod_id1),
  981. __ATTR_RO(prod_id2),
  982. __ATTR_RO(prod_id3),
  983. __ATTR_RO(prod_id4),
  984. __ATTR_RO(modalias),
  985. __ATTR(allow_func_id_match, 0200, NULL, pcmcia_store_allow_func_id_match),
  986. __ATTR_NULL,
  987. };
  988. /* PM support, also needed for reset */
  989. static int pcmcia_dev_suspend(struct device * dev, pm_message_t state)
  990. {
  991. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  992. struct pcmcia_driver *p_drv = NULL;
  993. int ret = 0;
  994. if (p_dev->suspended)
  995. return 0;
  996. ds_dev_dbg(2, dev, "suspending\n");
  997. if (dev->driver)
  998. p_drv = to_pcmcia_drv(dev->driver);
  999. if (!p_drv)
  1000. goto out;
  1001. if (p_drv->suspend) {
  1002. ret = p_drv->suspend(p_dev);
  1003. if (ret) {
  1004. dev_printk(KERN_ERR, dev,
  1005. "pcmcia: device %s (driver %s) did "
  1006. "not want to go to sleep (%d)\n",
  1007. p_dev->devname, p_drv->drv.name, ret);
  1008. goto out;
  1009. }
  1010. }
  1011. if (p_dev->device_no == p_dev->func) {
  1012. ds_dev_dbg(2, dev, "releasing configuration\n");
  1013. pcmcia_release_configuration(p_dev);
  1014. }
  1015. out:
  1016. if (!ret)
  1017. p_dev->suspended = 1;
  1018. return ret;
  1019. }
  1020. static int pcmcia_dev_resume(struct device * dev)
  1021. {
  1022. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1023. struct pcmcia_driver *p_drv = NULL;
  1024. int ret = 0;
  1025. if (!p_dev->suspended)
  1026. return 0;
  1027. ds_dev_dbg(2, dev, "resuming\n");
  1028. if (dev->driver)
  1029. p_drv = to_pcmcia_drv(dev->driver);
  1030. if (!p_drv)
  1031. goto out;
  1032. if (p_dev->device_no == p_dev->func) {
  1033. ds_dev_dbg(2, dev, "requesting configuration\n");
  1034. ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
  1035. if (ret)
  1036. goto out;
  1037. }
  1038. if (p_drv->resume)
  1039. ret = p_drv->resume(p_dev);
  1040. out:
  1041. if (!ret)
  1042. p_dev->suspended = 0;
  1043. return ret;
  1044. }
  1045. static int pcmcia_bus_suspend_callback(struct device *dev, void * _data)
  1046. {
  1047. struct pcmcia_socket *skt = _data;
  1048. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1049. if (p_dev->socket != skt || p_dev->suspended)
  1050. return 0;
  1051. return runtime_suspend(dev);
  1052. }
  1053. static int pcmcia_bus_resume_callback(struct device *dev, void * _data)
  1054. {
  1055. struct pcmcia_socket *skt = _data;
  1056. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1057. if (p_dev->socket != skt || !p_dev->suspended)
  1058. return 0;
  1059. runtime_resume(dev);
  1060. return 0;
  1061. }
  1062. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  1063. {
  1064. ds_dev_dbg(2, &skt->dev, "resuming socket %d\n", skt->sock);
  1065. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  1066. return 0;
  1067. }
  1068. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  1069. {
  1070. ds_dev_dbg(2, &skt->dev, "suspending socket %d\n", skt->sock);
  1071. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  1072. pcmcia_bus_suspend_callback)) {
  1073. pcmcia_bus_resume(skt);
  1074. return -EIO;
  1075. }
  1076. return 0;
  1077. }
  1078. /*======================================================================
  1079. The card status event handler.
  1080. ======================================================================*/
  1081. /* Normally, the event is passed to individual drivers after
  1082. * informing userspace. Only for CS_EVENT_CARD_REMOVAL this
  1083. * is inversed to maintain historic compatibility.
  1084. */
  1085. static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
  1086. {
  1087. struct pcmcia_socket *s = pcmcia_get_socket(skt);
  1088. if (!s) {
  1089. dev_printk(KERN_ERR, &skt->dev,
  1090. "PCMCIA obtaining reference to socket " \
  1091. "failed, event 0x%x lost!\n", event);
  1092. return -ENODEV;
  1093. }
  1094. ds_dev_dbg(1, &skt->dev, "ds_event(0x%06x, %d, 0x%p)\n",
  1095. event, priority, skt);
  1096. switch (event) {
  1097. case CS_EVENT_CARD_REMOVAL:
  1098. s->pcmcia_state.present = 0;
  1099. pcmcia_card_remove(skt, NULL);
  1100. handle_event(skt, event);
  1101. break;
  1102. case CS_EVENT_CARD_INSERTION:
  1103. s->pcmcia_state.present = 1;
  1104. pcmcia_card_add(skt);
  1105. handle_event(skt, event);
  1106. break;
  1107. case CS_EVENT_EJECTION_REQUEST:
  1108. break;
  1109. case CS_EVENT_PM_SUSPEND:
  1110. case CS_EVENT_PM_RESUME:
  1111. case CS_EVENT_RESET_PHYSICAL:
  1112. case CS_EVENT_CARD_RESET:
  1113. default:
  1114. handle_event(skt, event);
  1115. break;
  1116. }
  1117. pcmcia_put_socket(s);
  1118. return 0;
  1119. } /* ds_event */
  1120. struct pcmcia_device * pcmcia_dev_present(struct pcmcia_device *_p_dev)
  1121. {
  1122. struct pcmcia_device *p_dev;
  1123. struct pcmcia_device *ret = NULL;
  1124. p_dev = pcmcia_get_dev(_p_dev);
  1125. if (!p_dev)
  1126. return NULL;
  1127. if (!p_dev->socket->pcmcia_state.present)
  1128. goto out;
  1129. if (p_dev->_removed)
  1130. goto out;
  1131. if (p_dev->suspended)
  1132. goto out;
  1133. ret = p_dev;
  1134. out:
  1135. pcmcia_put_dev(p_dev);
  1136. return ret;
  1137. }
  1138. EXPORT_SYMBOL(pcmcia_dev_present);
  1139. static struct pcmcia_callback pcmcia_bus_callback = {
  1140. .owner = THIS_MODULE,
  1141. .event = ds_event,
  1142. .requery = pcmcia_bus_rescan,
  1143. .suspend = pcmcia_bus_suspend,
  1144. .resume = pcmcia_bus_resume,
  1145. };
  1146. static int __devinit pcmcia_bus_add_socket(struct device *dev,
  1147. struct class_interface *class_intf)
  1148. {
  1149. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1150. int ret;
  1151. socket = pcmcia_get_socket(socket);
  1152. if (!socket) {
  1153. dev_printk(KERN_ERR, dev,
  1154. "PCMCIA obtaining reference to socket failed\n");
  1155. return -ENODEV;
  1156. }
  1157. /*
  1158. * Ugly. But we want to wait for the socket threads to have started up.
  1159. * We really should let the drivers themselves drive some of this..
  1160. */
  1161. msleep(250);
  1162. #ifdef CONFIG_PCMCIA_IOCTL
  1163. init_waitqueue_head(&socket->queue);
  1164. #endif
  1165. INIT_LIST_HEAD(&socket->devices_list);
  1166. INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
  1167. memset(&socket->pcmcia_state, 0, sizeof(u8));
  1168. socket->device_count = 0;
  1169. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1170. if (ret) {
  1171. dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
  1172. pcmcia_put_socket(socket);
  1173. return (ret);
  1174. }
  1175. return 0;
  1176. }
  1177. static void pcmcia_bus_remove_socket(struct device *dev,
  1178. struct class_interface *class_intf)
  1179. {
  1180. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1181. if (!socket)
  1182. return;
  1183. socket->pcmcia_state.dead = 1;
  1184. pccard_register_pcmcia(socket, NULL);
  1185. /* unregister any unbound devices */
  1186. mutex_lock(&socket->skt_mutex);
  1187. pcmcia_card_remove(socket, NULL);
  1188. mutex_unlock(&socket->skt_mutex);
  1189. pcmcia_put_socket(socket);
  1190. return;
  1191. }
  1192. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1193. static struct class_interface pcmcia_bus_interface __refdata = {
  1194. .class = &pcmcia_socket_class,
  1195. .add_dev = &pcmcia_bus_add_socket,
  1196. .remove_dev = &pcmcia_bus_remove_socket,
  1197. };
  1198. struct bus_type pcmcia_bus_type = {
  1199. .name = "pcmcia",
  1200. .uevent = pcmcia_bus_uevent,
  1201. .match = pcmcia_bus_match,
  1202. .dev_attrs = pcmcia_dev_attrs,
  1203. .probe = pcmcia_device_probe,
  1204. .remove = pcmcia_device_remove,
  1205. .suspend = pcmcia_dev_suspend,
  1206. .resume = pcmcia_dev_resume,
  1207. };
  1208. static int __init init_pcmcia_bus(void)
  1209. {
  1210. int ret;
  1211. spin_lock_init(&pcmcia_dev_list_lock);
  1212. ret = bus_register(&pcmcia_bus_type);
  1213. if (ret < 0) {
  1214. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1215. return ret;
  1216. }
  1217. ret = class_interface_register(&pcmcia_bus_interface);
  1218. if (ret < 0) {
  1219. printk(KERN_WARNING
  1220. "pcmcia: class_interface_register error: %d\n", ret);
  1221. bus_unregister(&pcmcia_bus_type);
  1222. return ret;
  1223. }
  1224. pcmcia_setup_ioctl();
  1225. return 0;
  1226. }
  1227. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1228. * pcmcia_socket_class is already registered */
  1229. static void __exit exit_pcmcia_bus(void)
  1230. {
  1231. pcmcia_cleanup_ioctl();
  1232. class_interface_unregister(&pcmcia_bus_interface);
  1233. bus_unregister(&pcmcia_bus_type);
  1234. }
  1235. module_exit(exit_pcmcia_bus);
  1236. MODULE_ALIAS("ds");