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