ds.c 36 KB

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