ds.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ds.c -- 16-bit PCMCIA core support
  4. *
  5. * The initial developer of the original code is David A. Hinds
  6. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  7. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  8. *
  9. * (C) 1999 David A. Hinds
  10. * (C) 2003 - 2010 Dominik Brodowski
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/delay.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/crc32.h>
  20. #include <linux/firmware.h>
  21. #include <linux/kref.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/slab.h>
  24. #include <pcmcia/cistpl.h>
  25. #include <pcmcia/ds.h>
  26. #include <pcmcia/ss.h>
  27. #include "cs_internal.h"
  28. /*====================================================================*/
  29. /* Module parameters */
  30. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  31. MODULE_DESCRIPTION("PCMCIA Driver Services");
  32. MODULE_LICENSE("GPL");
  33. /*====================================================================*/
  34. static void pcmcia_check_driver(struct pcmcia_driver *p_drv)
  35. {
  36. const struct pcmcia_device_id *did = p_drv->id_table;
  37. unsigned int i;
  38. u32 hash;
  39. if (!p_drv->probe || !p_drv->remove)
  40. printk(KERN_DEBUG "pcmcia: %s lacks a requisite callback "
  41. "function\n", p_drv->name);
  42. while (did && did->match_flags) {
  43. for (i = 0; i < 4; i++) {
  44. if (!did->prod_id[i])
  45. continue;
  46. hash = crc32(0, did->prod_id[i], strlen(did->prod_id[i]));
  47. if (hash == did->prod_id_hash[i])
  48. continue;
  49. printk(KERN_DEBUG "pcmcia: %s: invalid hash for "
  50. "product string \"%s\": is 0x%x, should "
  51. "be 0x%x\n", p_drv->name, did->prod_id[i],
  52. did->prod_id_hash[i], hash);
  53. printk(KERN_DEBUG "pcmcia: see "
  54. "Documentation/pcmcia/devicetable.rst for "
  55. "details\n");
  56. }
  57. did++;
  58. }
  59. return;
  60. }
  61. /*======================================================================*/
  62. struct pcmcia_dynid {
  63. struct list_head node;
  64. struct pcmcia_device_id id;
  65. };
  66. /**
  67. * pcmcia_store_new_id - add a new PCMCIA device ID to this driver and re-probe devices
  68. * @driver: target device driver
  69. * @buf: buffer for scanning device ID data
  70. * @count: input size
  71. *
  72. * Adds a new dynamic PCMCIA device ID to this driver,
  73. * and causes the driver to probe for all devices again.
  74. */
  75. static ssize_t
  76. new_id_store(struct device_driver *driver, const char *buf, size_t count)
  77. {
  78. struct pcmcia_dynid *dynid;
  79. struct pcmcia_driver *pdrv = to_pcmcia_drv(driver);
  80. __u16 match_flags, manf_id, card_id;
  81. __u8 func_id, function, device_no;
  82. __u32 prod_id_hash[4] = {0, 0, 0, 0};
  83. int fields = 0;
  84. int retval = 0;
  85. fields = sscanf(buf, "%hx %hx %hx %hhx %hhx %hhx %x %x %x %x",
  86. &match_flags, &manf_id, &card_id, &func_id, &function, &device_no,
  87. &prod_id_hash[0], &prod_id_hash[1], &prod_id_hash[2], &prod_id_hash[3]);
  88. if (fields < 6)
  89. return -EINVAL;
  90. dynid = kzalloc(sizeof(struct pcmcia_dynid), GFP_KERNEL);
  91. if (!dynid)
  92. return -ENOMEM;
  93. dynid->id.match_flags = match_flags;
  94. dynid->id.manf_id = manf_id;
  95. dynid->id.card_id = card_id;
  96. dynid->id.func_id = func_id;
  97. dynid->id.function = function;
  98. dynid->id.device_no = device_no;
  99. memcpy(dynid->id.prod_id_hash, prod_id_hash, sizeof(__u32) * 4);
  100. mutex_lock(&pdrv->dynids.lock);
  101. list_add_tail(&dynid->node, &pdrv->dynids.list);
  102. mutex_unlock(&pdrv->dynids.lock);
  103. retval = driver_attach(&pdrv->drv);
  104. if (retval)
  105. return retval;
  106. return count;
  107. }
  108. static DRIVER_ATTR_WO(new_id);
  109. static void
  110. pcmcia_free_dynids(struct pcmcia_driver *drv)
  111. {
  112. struct pcmcia_dynid *dynid, *n;
  113. mutex_lock(&drv->dynids.lock);
  114. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  115. list_del(&dynid->node);
  116. kfree(dynid);
  117. }
  118. mutex_unlock(&drv->dynids.lock);
  119. }
  120. static int
  121. pcmcia_create_newid_file(struct pcmcia_driver *drv)
  122. {
  123. int error = 0;
  124. if (drv->probe != NULL)
  125. error = driver_create_file(&drv->drv, &driver_attr_new_id);
  126. return error;
  127. }
  128. static void
  129. pcmcia_remove_newid_file(struct pcmcia_driver *drv)
  130. {
  131. driver_remove_file(&drv->drv, &driver_attr_new_id);
  132. }
  133. /**
  134. * pcmcia_register_driver - register a PCMCIA driver with the bus core
  135. * @driver: the &driver being registered
  136. *
  137. * Registers a PCMCIA driver with the PCMCIA bus core.
  138. */
  139. int pcmcia_register_driver(struct pcmcia_driver *driver)
  140. {
  141. int error;
  142. if (!driver)
  143. return -EINVAL;
  144. pcmcia_check_driver(driver);
  145. /* initialize common fields */
  146. driver->drv.bus = &pcmcia_bus_type;
  147. driver->drv.owner = driver->owner;
  148. driver->drv.name = driver->name;
  149. mutex_init(&driver->dynids.lock);
  150. INIT_LIST_HEAD(&driver->dynids.list);
  151. pr_debug("registering driver %s\n", driver->name);
  152. error = driver_register(&driver->drv);
  153. if (error < 0)
  154. return error;
  155. error = pcmcia_create_newid_file(driver);
  156. if (error)
  157. driver_unregister(&driver->drv);
  158. return error;
  159. }
  160. EXPORT_SYMBOL(pcmcia_register_driver);
  161. /**
  162. * pcmcia_unregister_driver - unregister a PCMCIA driver with the bus core
  163. * @driver: the &driver being unregistered
  164. */
  165. void pcmcia_unregister_driver(struct pcmcia_driver *driver)
  166. {
  167. pr_debug("unregistering driver %s\n", driver->name);
  168. pcmcia_remove_newid_file(driver);
  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_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_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_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 = kstrdup(tmp, GFP_KERNEL);
  387. if (!new)
  388. continue;
  389. tmp = p_dev->prod_id[i];
  390. p_dev->prod_id[i] = new;
  391. kfree(tmp);
  392. }
  393. mutex_unlock(&p_dev->socket->ops_mutex);
  394. }
  395. kfree(vers1);
  396. return 0;
  397. }
  398. static struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s,
  399. unsigned int function)
  400. {
  401. struct pcmcia_device *p_dev, *tmp_dev;
  402. int i;
  403. s = pcmcia_get_socket(s);
  404. if (!s)
  405. return NULL;
  406. pr_debug("adding device to %d, function %d\n", s->sock, function);
  407. p_dev = kzalloc(sizeof(struct pcmcia_device), GFP_KERNEL);
  408. if (!p_dev)
  409. goto err_put;
  410. mutex_lock(&s->ops_mutex);
  411. p_dev->device_no = (s->device_count++);
  412. mutex_unlock(&s->ops_mutex);
  413. /* max of 2 PFC devices */
  414. if ((p_dev->device_no >= 2) && (function == 0))
  415. goto err_free;
  416. /* max of 4 devices overall */
  417. if (p_dev->device_no >= 4)
  418. goto err_free;
  419. p_dev->socket = s;
  420. p_dev->func = function;
  421. p_dev->dev.bus = &pcmcia_bus_type;
  422. p_dev->dev.parent = s->dev.parent;
  423. p_dev->dev.release = pcmcia_release_dev;
  424. /* by default don't allow DMA */
  425. p_dev->dma_mask = 0;
  426. p_dev->dev.dma_mask = &p_dev->dma_mask;
  427. dev_set_name(&p_dev->dev, "%d.%d", p_dev->socket->sock, p_dev->device_no);
  428. if (!dev_name(&p_dev->dev))
  429. goto err_free;
  430. p_dev->devname = kasprintf(GFP_KERNEL, "pcmcia%s", dev_name(&p_dev->dev));
  431. if (!p_dev->devname)
  432. goto err_free;
  433. dev_dbg(&p_dev->dev, "devname is %s\n", p_dev->devname);
  434. mutex_lock(&s->ops_mutex);
  435. /*
  436. * p_dev->function_config must be the same for all card functions.
  437. * Note that this is serialized by ops_mutex, so that only one
  438. * such struct will be created.
  439. */
  440. list_for_each_entry(tmp_dev, &s->devices_list, socket_device_list)
  441. if (p_dev->func == tmp_dev->func) {
  442. p_dev->function_config = tmp_dev->function_config;
  443. p_dev->irq = tmp_dev->irq;
  444. kref_get(&p_dev->function_config->ref);
  445. }
  446. /* Add to the list in pcmcia_bus_socket */
  447. list_add(&p_dev->socket_device_list, &s->devices_list);
  448. if (pcmcia_setup_irq(p_dev))
  449. dev_warn(&p_dev->dev,
  450. "IRQ setup failed -- device might not work\n");
  451. if (!p_dev->function_config) {
  452. config_t *c;
  453. dev_dbg(&p_dev->dev, "creating config_t\n");
  454. c = kzalloc(sizeof(struct config_t), GFP_KERNEL);
  455. if (!c) {
  456. mutex_unlock(&s->ops_mutex);
  457. goto err_unreg;
  458. }
  459. p_dev->function_config = c;
  460. kref_init(&c->ref);
  461. for (i = 0; i < MAX_IO_WIN; i++) {
  462. c->io[i].name = p_dev->devname;
  463. c->io[i].flags = IORESOURCE_IO;
  464. }
  465. for (i = 0; i < MAX_WIN; i++) {
  466. c->mem[i].name = p_dev->devname;
  467. c->mem[i].flags = IORESOURCE_MEM;
  468. }
  469. }
  470. for (i = 0; i < MAX_IO_WIN; i++)
  471. p_dev->resource[i] = &p_dev->function_config->io[i];
  472. for (; i < (MAX_IO_WIN + MAX_WIN); i++)
  473. p_dev->resource[i] = &p_dev->function_config->mem[i-MAX_IO_WIN];
  474. mutex_unlock(&s->ops_mutex);
  475. dev_notice(&p_dev->dev, "pcmcia: registering new device %s (IRQ: %d)\n",
  476. p_dev->devname, p_dev->irq);
  477. pcmcia_device_query(p_dev);
  478. if (device_register(&p_dev->dev))
  479. goto err_unreg;
  480. return p_dev;
  481. err_unreg:
  482. mutex_lock(&s->ops_mutex);
  483. list_del(&p_dev->socket_device_list);
  484. mutex_unlock(&s->ops_mutex);
  485. err_free:
  486. mutex_lock(&s->ops_mutex);
  487. s->device_count--;
  488. mutex_unlock(&s->ops_mutex);
  489. for (i = 0; i < 4; i++)
  490. kfree(p_dev->prod_id[i]);
  491. kfree(p_dev->devname);
  492. kfree(p_dev);
  493. err_put:
  494. pcmcia_put_socket(s);
  495. return NULL;
  496. }
  497. static int pcmcia_card_add(struct pcmcia_socket *s)
  498. {
  499. cistpl_longlink_mfc_t mfc;
  500. unsigned int no_funcs, i, no_chains;
  501. int ret = -EAGAIN;
  502. mutex_lock(&s->ops_mutex);
  503. if (!(s->resource_setup_done)) {
  504. dev_dbg(&s->dev,
  505. "no resources available, delaying card_add\n");
  506. mutex_unlock(&s->ops_mutex);
  507. return -EAGAIN; /* try again, but later... */
  508. }
  509. if (pcmcia_validate_mem(s)) {
  510. dev_dbg(&s->dev, "validating mem resources failed, "
  511. "delaying card_add\n");
  512. mutex_unlock(&s->ops_mutex);
  513. return -EAGAIN; /* try again, but later... */
  514. }
  515. mutex_unlock(&s->ops_mutex);
  516. ret = pccard_validate_cis(s, &no_chains);
  517. if (ret || !no_chains) {
  518. #if defined(CONFIG_MTD_PCMCIA_ANONYMOUS)
  519. /* Set up as an anonymous card. If we don't have anonymous
  520. memory support then just error the card as there is no
  521. point trying to second guess.
  522. Note: some cards have just a device entry, it may be
  523. worth extending support to cover these in future */
  524. if (ret == -EIO) {
  525. dev_info(&s->dev, "no CIS, assuming an anonymous memory card.\n");
  526. pcmcia_replace_cis(s, "\xFF", 1);
  527. no_chains = 1;
  528. ret = 0;
  529. } else
  530. #endif
  531. {
  532. dev_dbg(&s->dev, "invalid CIS or invalid resources\n");
  533. return -ENODEV;
  534. }
  535. }
  536. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
  537. no_funcs = mfc.nfn;
  538. else
  539. no_funcs = 1;
  540. s->functions = no_funcs;
  541. for (i = 0; i < no_funcs; i++)
  542. pcmcia_device_add(s, i);
  543. return ret;
  544. }
  545. static int pcmcia_requery_callback(struct device *dev, void *_data)
  546. {
  547. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  548. if (!p_dev->dev.driver) {
  549. dev_dbg(dev, "update device information\n");
  550. pcmcia_device_query(p_dev);
  551. }
  552. return 0;
  553. }
  554. static void pcmcia_requery(struct pcmcia_socket *s)
  555. {
  556. int has_pfc;
  557. if (!(s->state & SOCKET_PRESENT))
  558. return;
  559. if (s->functions == 0) {
  560. pcmcia_card_add(s);
  561. return;
  562. }
  563. /* some device information might have changed because of a CIS
  564. * update or because we can finally read it correctly... so
  565. * determine it again, overwriting old values if necessary. */
  566. bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery_callback);
  567. /* if the CIS changed, we need to check whether the number of
  568. * functions changed. */
  569. if (s->fake_cis) {
  570. int old_funcs, new_funcs;
  571. cistpl_longlink_mfc_t mfc;
  572. /* does this cis override add or remove functions? */
  573. old_funcs = s->functions;
  574. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
  575. &mfc))
  576. new_funcs = mfc.nfn;
  577. else
  578. new_funcs = 1;
  579. if (old_funcs != new_funcs) {
  580. /* we need to re-start */
  581. pcmcia_card_remove(s, NULL);
  582. s->functions = 0;
  583. pcmcia_card_add(s);
  584. }
  585. }
  586. /* If the PCMCIA device consists of two pseudo devices,
  587. * call pcmcia_device_add() -- which will fail if both
  588. * devices are already registered. */
  589. mutex_lock(&s->ops_mutex);
  590. has_pfc = s->pcmcia_pfc;
  591. mutex_unlock(&s->ops_mutex);
  592. if (has_pfc)
  593. pcmcia_device_add(s, 0);
  594. /* we re-scan all devices, not just the ones connected to this
  595. * socket. This does not matter, though. */
  596. if (bus_rescan_devices(&pcmcia_bus_type))
  597. dev_warn(&s->dev, "rescanning the bus failed\n");
  598. }
  599. #ifdef CONFIG_PCMCIA_LOAD_CIS
  600. /**
  601. * pcmcia_load_firmware - load CIS from userspace if device-provided is broken
  602. * @dev: the pcmcia device which needs a CIS override
  603. * @filename: requested filename in /lib/firmware/
  604. *
  605. * This uses the in-kernel firmware loading mechanism to use a "fake CIS" if
  606. * the one provided by the card is broken. The firmware files reside in
  607. * /lib/firmware/ in userspace.
  608. */
  609. static int pcmcia_load_firmware(struct pcmcia_device *dev, char *filename)
  610. {
  611. struct pcmcia_socket *s = dev->socket;
  612. const struct firmware *fw;
  613. int ret = -ENOMEM;
  614. cistpl_longlink_mfc_t mfc;
  615. int old_funcs, new_funcs = 1;
  616. if (!filename)
  617. return -EINVAL;
  618. dev_dbg(&dev->dev, "trying to load CIS file %s\n", filename);
  619. if (request_firmware(&fw, filename, &dev->dev) == 0) {
  620. if (fw->size >= CISTPL_MAX_CIS_SIZE) {
  621. ret = -EINVAL;
  622. dev_err(&dev->dev, "pcmcia: CIS override is too big\n");
  623. goto release;
  624. }
  625. if (!pcmcia_replace_cis(s, fw->data, fw->size))
  626. ret = 0;
  627. else {
  628. dev_err(&dev->dev, "pcmcia: CIS override failed\n");
  629. goto release;
  630. }
  631. /* we need to re-start if the number of functions changed */
  632. old_funcs = s->functions;
  633. if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
  634. &mfc))
  635. new_funcs = mfc.nfn;
  636. if (old_funcs != new_funcs)
  637. ret = -EBUSY;
  638. /* update information */
  639. pcmcia_device_query(dev);
  640. /* requery (as number of functions might have changed) */
  641. pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
  642. }
  643. release:
  644. release_firmware(fw);
  645. return ret;
  646. }
  647. #else /* !CONFIG_PCMCIA_LOAD_CIS */
  648. static inline int pcmcia_load_firmware(struct pcmcia_device *dev,
  649. char *filename)
  650. {
  651. return -ENODEV;
  652. }
  653. #endif
  654. static inline int pcmcia_devmatch(struct pcmcia_device *dev,
  655. const struct pcmcia_device_id *did)
  656. {
  657. if (did->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID) {
  658. if ((!dev->has_manf_id) || (dev->manf_id != did->manf_id))
  659. return 0;
  660. }
  661. if (did->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID) {
  662. if ((!dev->has_card_id) || (dev->card_id != did->card_id))
  663. return 0;
  664. }
  665. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION) {
  666. if (dev->func != did->function)
  667. return 0;
  668. }
  669. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1) {
  670. if (!dev->prod_id[0])
  671. return 0;
  672. if (strcmp(did->prod_id[0], dev->prod_id[0]))
  673. return 0;
  674. }
  675. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2) {
  676. if (!dev->prod_id[1])
  677. return 0;
  678. if (strcmp(did->prod_id[1], dev->prod_id[1]))
  679. return 0;
  680. }
  681. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3) {
  682. if (!dev->prod_id[2])
  683. return 0;
  684. if (strcmp(did->prod_id[2], dev->prod_id[2]))
  685. return 0;
  686. }
  687. if (did->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4) {
  688. if (!dev->prod_id[3])
  689. return 0;
  690. if (strcmp(did->prod_id[3], dev->prod_id[3]))
  691. return 0;
  692. }
  693. if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
  694. dev_dbg(&dev->dev, "this is a pseudo-multi-function device\n");
  695. mutex_lock(&dev->socket->ops_mutex);
  696. dev->socket->pcmcia_pfc = 1;
  697. mutex_unlock(&dev->socket->ops_mutex);
  698. if (dev->device_no != did->device_no)
  699. return 0;
  700. }
  701. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID) {
  702. int ret;
  703. if ((!dev->has_func_id) || (dev->func_id != did->func_id))
  704. return 0;
  705. /* if this is a pseudo-multi-function device,
  706. * we need explicit matches */
  707. if (dev->socket->pcmcia_pfc)
  708. return 0;
  709. if (dev->device_no)
  710. return 0;
  711. /* also, FUNC_ID matching needs to be activated by userspace
  712. * after it has re-checked that there is no possible module
  713. * with a prod_id/manf_id/card_id match.
  714. */
  715. mutex_lock(&dev->socket->ops_mutex);
  716. ret = dev->allow_func_id_match;
  717. mutex_unlock(&dev->socket->ops_mutex);
  718. if (!ret) {
  719. dev_dbg(&dev->dev,
  720. "skipping FUNC_ID match until userspace ACK\n");
  721. return 0;
  722. }
  723. }
  724. if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
  725. dev_dbg(&dev->dev, "device needs a fake CIS\n");
  726. if (!dev->socket->fake_cis)
  727. if (pcmcia_load_firmware(dev, did->cisfile))
  728. return 0;
  729. }
  730. if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
  731. int i;
  732. for (i = 0; i < 4; i++)
  733. if (dev->prod_id[i])
  734. return 0;
  735. if (dev->has_manf_id || dev->has_card_id || dev->has_func_id)
  736. return 0;
  737. }
  738. return 1;
  739. }
  740. static int pcmcia_bus_match(struct device *dev, struct device_driver *drv)
  741. {
  742. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  743. struct pcmcia_driver *p_drv = to_pcmcia_drv(drv);
  744. const struct pcmcia_device_id *did = p_drv->id_table;
  745. struct pcmcia_dynid *dynid;
  746. /* match dynamic devices first */
  747. mutex_lock(&p_drv->dynids.lock);
  748. list_for_each_entry(dynid, &p_drv->dynids.list, node) {
  749. dev_dbg(dev, "trying to match to %s\n", drv->name);
  750. if (pcmcia_devmatch(p_dev, &dynid->id)) {
  751. dev_dbg(dev, "matched to %s\n", drv->name);
  752. mutex_unlock(&p_drv->dynids.lock);
  753. return 1;
  754. }
  755. }
  756. mutex_unlock(&p_drv->dynids.lock);
  757. while (did && did->match_flags) {
  758. dev_dbg(dev, "trying to match to %s\n", drv->name);
  759. if (pcmcia_devmatch(p_dev, did)) {
  760. dev_dbg(dev, "matched to %s\n", drv->name);
  761. return 1;
  762. }
  763. did++;
  764. }
  765. return 0;
  766. }
  767. static int pcmcia_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  768. {
  769. struct pcmcia_device *p_dev;
  770. int i;
  771. u32 hash[4] = { 0, 0, 0, 0};
  772. if (!dev)
  773. return -ENODEV;
  774. p_dev = to_pcmcia_dev(dev);
  775. /* calculate hashes */
  776. for (i = 0; i < 4; i++) {
  777. if (!p_dev->prod_id[i])
  778. continue;
  779. hash[i] = crc32(0, p_dev->prod_id[i], strlen(p_dev->prod_id[i]));
  780. }
  781. if (add_uevent_var(env, "SOCKET_NO=%u", p_dev->socket->sock))
  782. return -ENOMEM;
  783. if (add_uevent_var(env, "DEVICE_NO=%02X", p_dev->device_no))
  784. return -ENOMEM;
  785. if (add_uevent_var(env, "MODALIAS=pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  786. "pa%08Xpb%08Xpc%08Xpd%08X",
  787. p_dev->has_manf_id ? p_dev->manf_id : 0,
  788. p_dev->has_card_id ? p_dev->card_id : 0,
  789. p_dev->has_func_id ? p_dev->func_id : 0,
  790. p_dev->func,
  791. p_dev->device_no,
  792. hash[0],
  793. hash[1],
  794. hash[2],
  795. hash[3]))
  796. return -ENOMEM;
  797. return 0;
  798. }
  799. /************************ runtime PM support ***************************/
  800. static int pcmcia_dev_suspend(struct device *dev);
  801. static int pcmcia_dev_resume(struct device *dev);
  802. static int runtime_suspend(struct device *dev)
  803. {
  804. int rc;
  805. device_lock(dev);
  806. rc = pcmcia_dev_suspend(dev);
  807. device_unlock(dev);
  808. return rc;
  809. }
  810. static int runtime_resume(struct device *dev)
  811. {
  812. int rc;
  813. device_lock(dev);
  814. rc = pcmcia_dev_resume(dev);
  815. device_unlock(dev);
  816. return rc;
  817. }
  818. /************************ per-device sysfs output ***************************/
  819. #define pcmcia_device_attr(field, test, format) \
  820. static ssize_t field##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  821. { \
  822. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  823. return p_dev->test ? sprintf(buf, format, p_dev->field) : -ENODEV; \
  824. } \
  825. static DEVICE_ATTR_RO(field);
  826. #define pcmcia_device_stringattr(name, field) \
  827. static ssize_t name##_show (struct device *dev, struct device_attribute *attr, char *buf) \
  828. { \
  829. struct pcmcia_device *p_dev = to_pcmcia_dev(dev); \
  830. return p_dev->field ? sprintf(buf, "%s\n", p_dev->field) : -ENODEV; \
  831. } \
  832. static DEVICE_ATTR_RO(name);
  833. pcmcia_device_attr(func_id, has_func_id, "0x%02x\n");
  834. pcmcia_device_attr(manf_id, has_manf_id, "0x%04x\n");
  835. pcmcia_device_attr(card_id, has_card_id, "0x%04x\n");
  836. pcmcia_device_stringattr(prod_id1, prod_id[0]);
  837. pcmcia_device_stringattr(prod_id2, prod_id[1]);
  838. pcmcia_device_stringattr(prod_id3, prod_id[2]);
  839. pcmcia_device_stringattr(prod_id4, prod_id[3]);
  840. static ssize_t function_show(struct device *dev, struct device_attribute *attr,
  841. char *buf)
  842. {
  843. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  844. return p_dev->socket ? sprintf(buf, "0x%02x\n", p_dev->func) : -ENODEV;
  845. }
  846. static DEVICE_ATTR_RO(function);
  847. static ssize_t resources_show(struct device *dev,
  848. struct device_attribute *attr, char *buf)
  849. {
  850. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  851. char *str = buf;
  852. int i;
  853. for (i = 0; i < PCMCIA_NUM_RESOURCES; i++)
  854. str += sprintf(str, "%pr\n", p_dev->resource[i]);
  855. return str - buf;
  856. }
  857. static DEVICE_ATTR_RO(resources);
  858. static ssize_t pm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  859. {
  860. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  861. if (p_dev->suspended)
  862. return sprintf(buf, "off\n");
  863. else
  864. return sprintf(buf, "on\n");
  865. }
  866. static ssize_t pm_state_store(struct device *dev, struct device_attribute *attr,
  867. const char *buf, size_t count)
  868. {
  869. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  870. int ret = 0;
  871. if (!count)
  872. return -EINVAL;
  873. if ((!p_dev->suspended) && !strncmp(buf, "off", 3))
  874. ret = runtime_suspend(dev);
  875. else if (p_dev->suspended && !strncmp(buf, "on", 2))
  876. ret = runtime_resume(dev);
  877. return ret ? ret : count;
  878. }
  879. static DEVICE_ATTR_RW(pm_state);
  880. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  881. {
  882. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  883. int i;
  884. u32 hash[4] = { 0, 0, 0, 0};
  885. /* calculate hashes */
  886. for (i = 0; i < 4; i++) {
  887. if (!p_dev->prod_id[i])
  888. continue;
  889. hash[i] = crc32(0, p_dev->prod_id[i],
  890. strlen(p_dev->prod_id[i]));
  891. }
  892. return sprintf(buf, "pcmcia:m%04Xc%04Xf%02Xfn%02Xpfn%02X"
  893. "pa%08Xpb%08Xpc%08Xpd%08X\n",
  894. p_dev->has_manf_id ? p_dev->manf_id : 0,
  895. p_dev->has_card_id ? p_dev->card_id : 0,
  896. p_dev->has_func_id ? p_dev->func_id : 0,
  897. p_dev->func, p_dev->device_no,
  898. hash[0], hash[1], hash[2], hash[3]);
  899. }
  900. static DEVICE_ATTR_RO(modalias);
  901. static ssize_t allow_func_id_match_store(struct device *dev,
  902. struct device_attribute *attr, const char *buf, size_t count)
  903. {
  904. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  905. if (!count)
  906. return -EINVAL;
  907. mutex_lock(&p_dev->socket->ops_mutex);
  908. p_dev->allow_func_id_match = 1;
  909. mutex_unlock(&p_dev->socket->ops_mutex);
  910. pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
  911. return count;
  912. }
  913. static DEVICE_ATTR_WO(allow_func_id_match);
  914. static struct attribute *pcmcia_dev_attrs[] = {
  915. &dev_attr_resources.attr,
  916. &dev_attr_pm_state.attr,
  917. &dev_attr_function.attr,
  918. &dev_attr_func_id.attr,
  919. &dev_attr_manf_id.attr,
  920. &dev_attr_card_id.attr,
  921. &dev_attr_prod_id1.attr,
  922. &dev_attr_prod_id2.attr,
  923. &dev_attr_prod_id3.attr,
  924. &dev_attr_prod_id4.attr,
  925. &dev_attr_modalias.attr,
  926. &dev_attr_allow_func_id_match.attr,
  927. NULL,
  928. };
  929. ATTRIBUTE_GROUPS(pcmcia_dev);
  930. /* PM support, also needed for reset */
  931. static int pcmcia_dev_suspend(struct device *dev)
  932. {
  933. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  934. struct pcmcia_driver *p_drv = NULL;
  935. int ret = 0;
  936. mutex_lock(&p_dev->socket->ops_mutex);
  937. if (p_dev->suspended) {
  938. mutex_unlock(&p_dev->socket->ops_mutex);
  939. return 0;
  940. }
  941. p_dev->suspended = 1;
  942. mutex_unlock(&p_dev->socket->ops_mutex);
  943. dev_dbg(dev, "suspending\n");
  944. if (dev->driver)
  945. p_drv = to_pcmcia_drv(dev->driver);
  946. if (!p_drv)
  947. goto out;
  948. if (p_drv->suspend) {
  949. ret = p_drv->suspend(p_dev);
  950. if (ret) {
  951. dev_err(dev,
  952. "pcmcia: device %s (driver %s) did not want to go to sleep (%d)\n",
  953. p_dev->devname, p_drv->name, ret);
  954. mutex_lock(&p_dev->socket->ops_mutex);
  955. p_dev->suspended = 0;
  956. mutex_unlock(&p_dev->socket->ops_mutex);
  957. goto out;
  958. }
  959. }
  960. if (p_dev->device_no == p_dev->func) {
  961. dev_dbg(dev, "releasing configuration\n");
  962. pcmcia_release_configuration(p_dev);
  963. }
  964. out:
  965. return ret;
  966. }
  967. static int pcmcia_dev_resume(struct device *dev)
  968. {
  969. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  970. struct pcmcia_driver *p_drv = NULL;
  971. int ret = 0;
  972. mutex_lock(&p_dev->socket->ops_mutex);
  973. if (!p_dev->suspended) {
  974. mutex_unlock(&p_dev->socket->ops_mutex);
  975. return 0;
  976. }
  977. p_dev->suspended = 0;
  978. mutex_unlock(&p_dev->socket->ops_mutex);
  979. dev_dbg(dev, "resuming\n");
  980. if (dev->driver)
  981. p_drv = to_pcmcia_drv(dev->driver);
  982. if (!p_drv)
  983. goto out;
  984. if (p_dev->device_no == p_dev->func) {
  985. dev_dbg(dev, "requesting configuration\n");
  986. ret = pcmcia_enable_device(p_dev);
  987. if (ret)
  988. goto out;
  989. }
  990. if (p_drv->resume)
  991. ret = p_drv->resume(p_dev);
  992. out:
  993. return ret;
  994. }
  995. static int pcmcia_bus_suspend_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. return runtime_suspend(dev);
  1002. }
  1003. static int pcmcia_bus_resume_callback(struct device *dev, void *_data)
  1004. {
  1005. struct pcmcia_socket *skt = _data;
  1006. struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
  1007. if (p_dev->socket != skt || !p_dev->suspended)
  1008. return 0;
  1009. runtime_resume(dev);
  1010. return 0;
  1011. }
  1012. static int pcmcia_bus_resume(struct pcmcia_socket *skt)
  1013. {
  1014. dev_dbg(&skt->dev, "resuming socket %d\n", skt->sock);
  1015. bus_for_each_dev(&pcmcia_bus_type, NULL, skt, pcmcia_bus_resume_callback);
  1016. return 0;
  1017. }
  1018. static int pcmcia_bus_suspend(struct pcmcia_socket *skt)
  1019. {
  1020. dev_dbg(&skt->dev, "suspending socket %d\n", skt->sock);
  1021. if (bus_for_each_dev(&pcmcia_bus_type, NULL, skt,
  1022. pcmcia_bus_suspend_callback)) {
  1023. pcmcia_bus_resume(skt);
  1024. return -EIO;
  1025. }
  1026. return 0;
  1027. }
  1028. static int pcmcia_bus_remove(struct pcmcia_socket *skt)
  1029. {
  1030. atomic_set(&skt->present, 0);
  1031. pcmcia_card_remove(skt, NULL);
  1032. mutex_lock(&skt->ops_mutex);
  1033. destroy_cis_cache(skt);
  1034. pcmcia_cleanup_irq(skt);
  1035. mutex_unlock(&skt->ops_mutex);
  1036. return 0;
  1037. }
  1038. static int pcmcia_bus_add(struct pcmcia_socket *skt)
  1039. {
  1040. atomic_set(&skt->present, 1);
  1041. mutex_lock(&skt->ops_mutex);
  1042. skt->pcmcia_pfc = 0;
  1043. destroy_cis_cache(skt); /* to be on the safe side... */
  1044. mutex_unlock(&skt->ops_mutex);
  1045. pcmcia_card_add(skt);
  1046. return 0;
  1047. }
  1048. static int pcmcia_bus_early_resume(struct pcmcia_socket *skt)
  1049. {
  1050. if (!verify_cis_cache(skt))
  1051. return 0;
  1052. dev_dbg(&skt->dev, "cis mismatch - different card\n");
  1053. /* first, remove the card */
  1054. pcmcia_bus_remove(skt);
  1055. mutex_lock(&skt->ops_mutex);
  1056. destroy_cis_cache(skt);
  1057. kfree(skt->fake_cis);
  1058. skt->fake_cis = NULL;
  1059. skt->functions = 0;
  1060. mutex_unlock(&skt->ops_mutex);
  1061. /* now, add the new card */
  1062. pcmcia_bus_add(skt);
  1063. return 0;
  1064. }
  1065. /*
  1066. * NOTE: This is racy. There's no guarantee the card will still be
  1067. * physically present, even if the call to this function returns
  1068. * non-NULL. Furthermore, the device driver most likely is unbound
  1069. * almost immediately, so the timeframe where pcmcia_dev_present
  1070. * returns NULL is probably really really small.
  1071. */
  1072. struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
  1073. {
  1074. struct pcmcia_device *p_dev;
  1075. struct pcmcia_device *ret = NULL;
  1076. p_dev = pcmcia_get_dev(_p_dev);
  1077. if (!p_dev)
  1078. return NULL;
  1079. if (atomic_read(&p_dev->socket->present) != 0)
  1080. ret = p_dev;
  1081. pcmcia_put_dev(p_dev);
  1082. return ret;
  1083. }
  1084. EXPORT_SYMBOL(pcmcia_dev_present);
  1085. static struct pcmcia_callback pcmcia_bus_callback = {
  1086. .owner = THIS_MODULE,
  1087. .add = pcmcia_bus_add,
  1088. .remove = pcmcia_bus_remove,
  1089. .requery = pcmcia_requery,
  1090. .validate = pccard_validate_cis,
  1091. .suspend = pcmcia_bus_suspend,
  1092. .early_resume = pcmcia_bus_early_resume,
  1093. .resume = pcmcia_bus_resume,
  1094. };
  1095. static int pcmcia_bus_add_socket(struct device *dev,
  1096. struct class_interface *class_intf)
  1097. {
  1098. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1099. int ret;
  1100. socket = pcmcia_get_socket(socket);
  1101. if (!socket) {
  1102. dev_err(dev, "PCMCIA obtaining reference to socket failed\n");
  1103. return -ENODEV;
  1104. }
  1105. ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
  1106. if (ret) {
  1107. dev_err(dev, "PCMCIA registration failed\n");
  1108. pcmcia_put_socket(socket);
  1109. return ret;
  1110. }
  1111. INIT_LIST_HEAD(&socket->devices_list);
  1112. socket->pcmcia_pfc = 0;
  1113. socket->device_count = 0;
  1114. atomic_set(&socket->present, 0);
  1115. ret = pccard_register_pcmcia(socket, &pcmcia_bus_callback);
  1116. if (ret) {
  1117. dev_err(dev, "PCMCIA registration failed\n");
  1118. pcmcia_put_socket(socket);
  1119. return ret;
  1120. }
  1121. return 0;
  1122. }
  1123. static void pcmcia_bus_remove_socket(struct device *dev,
  1124. struct class_interface *class_intf)
  1125. {
  1126. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  1127. if (!socket)
  1128. return;
  1129. pccard_register_pcmcia(socket, NULL);
  1130. /* unregister any unbound devices */
  1131. mutex_lock(&socket->skt_mutex);
  1132. pcmcia_card_remove(socket, NULL);
  1133. release_cis_mem(socket);
  1134. mutex_unlock(&socket->skt_mutex);
  1135. sysfs_remove_bin_file(&dev->kobj, &pccard_cis_attr);
  1136. pcmcia_put_socket(socket);
  1137. return;
  1138. }
  1139. /* the pcmcia_bus_interface is used to handle pcmcia socket devices */
  1140. static struct class_interface pcmcia_bus_interface __refdata = {
  1141. .class = &pcmcia_socket_class,
  1142. .add_dev = &pcmcia_bus_add_socket,
  1143. .remove_dev = &pcmcia_bus_remove_socket,
  1144. };
  1145. static const struct dev_pm_ops pcmcia_bus_pm_ops = {
  1146. SET_SYSTEM_SLEEP_PM_OPS(pcmcia_dev_suspend, pcmcia_dev_resume)
  1147. };
  1148. struct bus_type pcmcia_bus_type = {
  1149. .name = "pcmcia",
  1150. .uevent = pcmcia_bus_uevent,
  1151. .match = pcmcia_bus_match,
  1152. .dev_groups = pcmcia_dev_groups,
  1153. .probe = pcmcia_device_probe,
  1154. .remove = pcmcia_device_remove,
  1155. .pm = &pcmcia_bus_pm_ops,
  1156. };
  1157. static int __init init_pcmcia_bus(void)
  1158. {
  1159. int ret;
  1160. ret = bus_register(&pcmcia_bus_type);
  1161. if (ret < 0) {
  1162. printk(KERN_WARNING "pcmcia: bus_register error: %d\n", ret);
  1163. return ret;
  1164. }
  1165. ret = class_interface_register(&pcmcia_bus_interface);
  1166. if (ret < 0) {
  1167. printk(KERN_WARNING
  1168. "pcmcia: class_interface_register error: %d\n", ret);
  1169. bus_unregister(&pcmcia_bus_type);
  1170. return ret;
  1171. }
  1172. return 0;
  1173. }
  1174. fs_initcall(init_pcmcia_bus); /* one level after subsys_initcall so that
  1175. * pcmcia_socket_class is already registered */
  1176. static void __exit exit_pcmcia_bus(void)
  1177. {
  1178. class_interface_unregister(&pcmcia_bus_interface);
  1179. bus_unregister(&pcmcia_bus_type);
  1180. }
  1181. module_exit(exit_pcmcia_bus);
  1182. MODULE_ALIAS("ds");