ds.c 38 KB

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