ds.c 37 KB

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