ds.c 34 KB

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