ds.c 35 KB

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