ds.c 35 KB

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