ds.c 34 KB

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