ds.c 35 KB

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