ds.c 34 KB

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