ds.c 35 KB

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