ds.c 34 KB

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