ds.c 34 KB

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