ds.c 35 KB

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