ds.c 34 KB

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