ds.c 36 KB

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