ds.c 36 KB

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