cs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*
  2. * cs.c -- Kernel Card Services - core services
  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. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/major.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/timer.h>
  25. #include <linux/ioport.h>
  26. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/device.h>
  29. #include <linux/kthread.h>
  30. #include <linux/freezer.h>
  31. #include <asm/irq.h>
  32. #include <pcmcia/ss.h>
  33. #include <pcmcia/cistpl.h>
  34. #include <pcmcia/cisreg.h>
  35. #include <pcmcia/ds.h>
  36. #include "cs_internal.h"
  37. /* Module parameters */
  38. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  39. MODULE_DESCRIPTION("Linux Kernel Card Services");
  40. MODULE_LICENSE("GPL");
  41. #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
  42. INT_MODULE_PARM(setup_delay, 10); /* centiseconds */
  43. INT_MODULE_PARM(resume_delay, 20); /* centiseconds */
  44. INT_MODULE_PARM(shutdown_delay, 3); /* centiseconds */
  45. INT_MODULE_PARM(vcc_settle, 40); /* centiseconds */
  46. INT_MODULE_PARM(reset_time, 10); /* usecs */
  47. INT_MODULE_PARM(unreset_delay, 10); /* centiseconds */
  48. INT_MODULE_PARM(unreset_check, 10); /* centiseconds */
  49. INT_MODULE_PARM(unreset_limit, 30); /* unreset_check's */
  50. /* Access speed for attribute memory windows */
  51. INT_MODULE_PARM(cis_speed, 300); /* ns */
  52. socket_state_t dead_socket = {
  53. .csc_mask = SS_DETECT,
  54. };
  55. EXPORT_SYMBOL(dead_socket);
  56. /* List of all sockets, protected by a rwsem */
  57. LIST_HEAD(pcmcia_socket_list);
  58. EXPORT_SYMBOL(pcmcia_socket_list);
  59. DECLARE_RWSEM(pcmcia_socket_list_rwsem);
  60. EXPORT_SYMBOL(pcmcia_socket_list_rwsem);
  61. struct pcmcia_socket *pcmcia_get_socket(struct pcmcia_socket *skt)
  62. {
  63. struct device *dev = get_device(&skt->dev);
  64. if (!dev)
  65. return NULL;
  66. return dev_get_drvdata(dev);
  67. }
  68. EXPORT_SYMBOL(pcmcia_get_socket);
  69. void pcmcia_put_socket(struct pcmcia_socket *skt)
  70. {
  71. put_device(&skt->dev);
  72. }
  73. EXPORT_SYMBOL(pcmcia_put_socket);
  74. static void pcmcia_release_socket(struct device *dev)
  75. {
  76. struct pcmcia_socket *socket = dev_get_drvdata(dev);
  77. complete(&socket->socket_released);
  78. }
  79. static int pccardd(void *__skt);
  80. /**
  81. * pcmcia_register_socket - add a new pcmcia socket device
  82. * @socket: the &socket to register
  83. */
  84. int pcmcia_register_socket(struct pcmcia_socket *socket)
  85. {
  86. struct task_struct *tsk;
  87. int ret;
  88. if (!socket || !socket->ops || !socket->dev.parent || !socket->resource_ops)
  89. return -EINVAL;
  90. dev_dbg(&socket->dev, "pcmcia_register_socket(0x%p)\n", socket->ops);
  91. /* try to obtain a socket number [yes, it gets ugly if we
  92. * register more than 2^sizeof(unsigned int) pcmcia
  93. * sockets... but the socket number is deprecated
  94. * anyways, so I don't care] */
  95. down_write(&pcmcia_socket_list_rwsem);
  96. if (list_empty(&pcmcia_socket_list))
  97. socket->sock = 0;
  98. else {
  99. unsigned int found, i = 1;
  100. struct pcmcia_socket *tmp;
  101. do {
  102. found = 1;
  103. list_for_each_entry(tmp, &pcmcia_socket_list, socket_list) {
  104. if (tmp->sock == i)
  105. found = 0;
  106. }
  107. i++;
  108. } while (!found);
  109. socket->sock = i - 1;
  110. }
  111. list_add_tail(&socket->socket_list, &pcmcia_socket_list);
  112. up_write(&pcmcia_socket_list_rwsem);
  113. #ifndef CONFIG_CARDBUS
  114. /*
  115. * If we do not support Cardbus, ensure that
  116. * the Cardbus socket capability is disabled.
  117. */
  118. socket->features &= ~SS_CAP_CARDBUS;
  119. #endif
  120. /* set proper values in socket->dev */
  121. dev_set_drvdata(&socket->dev, socket);
  122. socket->dev.class = &pcmcia_socket_class;
  123. dev_set_name(&socket->dev, "pcmcia_socket%u", socket->sock);
  124. /* base address = 0, map = 0 */
  125. socket->cis_mem.flags = 0;
  126. socket->cis_mem.speed = cis_speed;
  127. INIT_LIST_HEAD(&socket->cis_cache);
  128. init_completion(&socket->socket_released);
  129. init_completion(&socket->thread_done);
  130. mutex_init(&socket->skt_mutex);
  131. mutex_init(&socket->ops_mutex);
  132. spin_lock_init(&socket->thread_lock);
  133. if (socket->resource_ops->init) {
  134. mutex_lock(&socket->ops_mutex);
  135. ret = socket->resource_ops->init(socket);
  136. mutex_unlock(&socket->ops_mutex);
  137. if (ret)
  138. goto err;
  139. }
  140. tsk = kthread_run(pccardd, socket, "pccardd");
  141. if (IS_ERR(tsk)) {
  142. ret = PTR_ERR(tsk);
  143. goto err;
  144. }
  145. wait_for_completion(&socket->thread_done);
  146. if (!socket->thread) {
  147. dev_warn(&socket->dev,
  148. "PCMCIA: warning: socket thread did not start\n");
  149. return -EIO;
  150. }
  151. pcmcia_parse_events(socket, SS_DETECT);
  152. /*
  153. * Let's try to get the PCMCIA module for 16-bit PCMCIA support.
  154. * If it fails, it doesn't matter -- we still have 32-bit CardBus
  155. * support to offer, so this is not a failure mode.
  156. */
  157. request_module_nowait("pcmcia");
  158. return 0;
  159. err:
  160. down_write(&pcmcia_socket_list_rwsem);
  161. list_del(&socket->socket_list);
  162. up_write(&pcmcia_socket_list_rwsem);
  163. return ret;
  164. } /* pcmcia_register_socket */
  165. EXPORT_SYMBOL(pcmcia_register_socket);
  166. /**
  167. * pcmcia_unregister_socket - remove a pcmcia socket device
  168. * @socket: the &socket to unregister
  169. */
  170. void pcmcia_unregister_socket(struct pcmcia_socket *socket)
  171. {
  172. if (!socket)
  173. return;
  174. dev_dbg(&socket->dev, "pcmcia_unregister_socket(0x%p)\n", socket->ops);
  175. if (socket->thread)
  176. kthread_stop(socket->thread);
  177. /* remove from our own list */
  178. down_write(&pcmcia_socket_list_rwsem);
  179. list_del(&socket->socket_list);
  180. up_write(&pcmcia_socket_list_rwsem);
  181. /* wait for sysfs to drop all references */
  182. if (socket->resource_ops->exit) {
  183. mutex_lock(&socket->ops_mutex);
  184. socket->resource_ops->exit(socket);
  185. mutex_unlock(&socket->ops_mutex);
  186. }
  187. wait_for_completion(&socket->socket_released);
  188. } /* pcmcia_unregister_socket */
  189. EXPORT_SYMBOL(pcmcia_unregister_socket);
  190. struct pcmcia_socket *pcmcia_get_socket_by_nr(unsigned int nr)
  191. {
  192. struct pcmcia_socket *s;
  193. down_read(&pcmcia_socket_list_rwsem);
  194. list_for_each_entry(s, &pcmcia_socket_list, socket_list)
  195. if (s->sock == nr) {
  196. up_read(&pcmcia_socket_list_rwsem);
  197. return s;
  198. }
  199. up_read(&pcmcia_socket_list_rwsem);
  200. return NULL;
  201. }
  202. EXPORT_SYMBOL(pcmcia_get_socket_by_nr);
  203. static int socket_reset(struct pcmcia_socket *skt)
  204. {
  205. int status, i;
  206. dev_dbg(&skt->dev, "reset\n");
  207. skt->socket.flags |= SS_OUTPUT_ENA | SS_RESET;
  208. skt->ops->set_socket(skt, &skt->socket);
  209. udelay((long)reset_time);
  210. skt->socket.flags &= ~SS_RESET;
  211. skt->ops->set_socket(skt, &skt->socket);
  212. msleep(unreset_delay * 10);
  213. for (i = 0; i < unreset_limit; i++) {
  214. skt->ops->get_status(skt, &status);
  215. if (!(status & SS_DETECT))
  216. return -ENODEV;
  217. if (status & SS_READY)
  218. return 0;
  219. msleep(unreset_check * 10);
  220. }
  221. dev_err(&skt->dev, "time out after reset\n");
  222. return -ETIMEDOUT;
  223. }
  224. /*
  225. * socket_setup() and socket_shutdown() are called by the main event handler
  226. * when card insertion and removal events are received.
  227. * socket_setup() turns on socket power and resets the socket, in two stages.
  228. * socket_shutdown() unconfigures a socket and turns off socket power.
  229. */
  230. static void socket_shutdown(struct pcmcia_socket *s)
  231. {
  232. int status;
  233. dev_dbg(&s->dev, "shutdown\n");
  234. if (s->callback)
  235. s->callback->remove(s);
  236. mutex_lock(&s->ops_mutex);
  237. s->state &= SOCKET_INUSE | SOCKET_PRESENT;
  238. msleep(shutdown_delay * 10);
  239. s->state &= SOCKET_INUSE;
  240. /* Blank out the socket state */
  241. s->socket = dead_socket;
  242. s->ops->init(s);
  243. s->ops->set_socket(s, &s->socket);
  244. s->lock_count = 0;
  245. kfree(s->fake_cis);
  246. s->fake_cis = NULL;
  247. s->functions = 0;
  248. /* From here on we can be sure that only we (that is, the
  249. * pccardd thread) accesses this socket, and all (16-bit)
  250. * PCMCIA interactions are gone. Therefore, release
  251. * ops_mutex so that we don't get a sysfs-related lockdep
  252. * warning.
  253. */
  254. mutex_unlock(&s->ops_mutex);
  255. #ifdef CONFIG_CARDBUS
  256. cb_free(s);
  257. #endif
  258. /* give socket some time to power down */
  259. msleep(100);
  260. s->ops->get_status(s, &status);
  261. if (status & SS_POWERON) {
  262. dev_err(&s->dev,
  263. "*** DANGER *** unable to remove socket power\n");
  264. }
  265. s->state &= ~SOCKET_INUSE;
  266. }
  267. static int socket_setup(struct pcmcia_socket *skt, int initial_delay)
  268. {
  269. int status, i;
  270. dev_dbg(&skt->dev, "setup\n");
  271. skt->ops->get_status(skt, &status);
  272. if (!(status & SS_DETECT))
  273. return -ENODEV;
  274. msleep(initial_delay * 10);
  275. for (i = 0; i < 100; i++) {
  276. skt->ops->get_status(skt, &status);
  277. if (!(status & SS_DETECT))
  278. return -ENODEV;
  279. if (!(status & SS_PENDING))
  280. break;
  281. msleep(100);
  282. }
  283. if (status & SS_PENDING) {
  284. dev_err(&skt->dev, "voltage interrogation timed out\n");
  285. return -ETIMEDOUT;
  286. }
  287. if (status & SS_CARDBUS) {
  288. if (!(skt->features & SS_CAP_CARDBUS)) {
  289. dev_err(&skt->dev, "cardbus cards are not supported\n");
  290. return -EINVAL;
  291. }
  292. skt->state |= SOCKET_CARDBUS;
  293. } else
  294. skt->state &= ~SOCKET_CARDBUS;
  295. /*
  296. * Decode the card voltage requirements, and apply power to the card.
  297. */
  298. if (status & SS_3VCARD)
  299. skt->socket.Vcc = skt->socket.Vpp = 33;
  300. else if (!(status & SS_XVCARD))
  301. skt->socket.Vcc = skt->socket.Vpp = 50;
  302. else {
  303. dev_err(&skt->dev, "unsupported voltage key\n");
  304. return -EIO;
  305. }
  306. if (skt->power_hook)
  307. skt->power_hook(skt, HOOK_POWER_PRE);
  308. skt->socket.flags = 0;
  309. skt->ops->set_socket(skt, &skt->socket);
  310. /*
  311. * Wait "vcc_settle" for the supply to stabilise.
  312. */
  313. msleep(vcc_settle * 10);
  314. skt->ops->get_status(skt, &status);
  315. if (!(status & SS_POWERON)) {
  316. dev_err(&skt->dev, "unable to apply power\n");
  317. return -EIO;
  318. }
  319. status = socket_reset(skt);
  320. if (skt->power_hook)
  321. skt->power_hook(skt, HOOK_POWER_POST);
  322. return status;
  323. }
  324. /*
  325. * Handle card insertion. Setup the socket, reset the card,
  326. * and then tell the rest of PCMCIA that a card is present.
  327. */
  328. static int socket_insert(struct pcmcia_socket *skt)
  329. {
  330. int ret;
  331. dev_dbg(&skt->dev, "insert\n");
  332. mutex_lock(&skt->ops_mutex);
  333. if (skt->state & SOCKET_INUSE) {
  334. mutex_unlock(&skt->ops_mutex);
  335. return -EINVAL;
  336. }
  337. skt->state |= SOCKET_INUSE;
  338. ret = socket_setup(skt, setup_delay);
  339. if (ret == 0) {
  340. skt->state |= SOCKET_PRESENT;
  341. dev_notice(&skt->dev, "pccard: %s card inserted into slot %d\n",
  342. (skt->state & SOCKET_CARDBUS) ? "CardBus" : "PCMCIA",
  343. skt->sock);
  344. #ifdef CONFIG_CARDBUS
  345. if (skt->state & SOCKET_CARDBUS) {
  346. cb_alloc(skt);
  347. skt->state |= SOCKET_CARDBUS_CONFIG;
  348. }
  349. #endif
  350. dev_dbg(&skt->dev, "insert done\n");
  351. mutex_unlock(&skt->ops_mutex);
  352. if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
  353. skt->callback->add(skt);
  354. } else {
  355. mutex_unlock(&skt->ops_mutex);
  356. socket_shutdown(skt);
  357. }
  358. return ret;
  359. }
  360. static int socket_suspend(struct pcmcia_socket *skt)
  361. {
  362. if (skt->state & SOCKET_SUSPEND)
  363. return -EBUSY;
  364. mutex_lock(&skt->ops_mutex);
  365. skt->suspended_state = skt->state;
  366. skt->socket = dead_socket;
  367. skt->ops->set_socket(skt, &skt->socket);
  368. if (skt->ops->suspend)
  369. skt->ops->suspend(skt);
  370. skt->state |= SOCKET_SUSPEND;
  371. mutex_unlock(&skt->ops_mutex);
  372. return 0;
  373. }
  374. static int socket_early_resume(struct pcmcia_socket *skt)
  375. {
  376. mutex_lock(&skt->ops_mutex);
  377. skt->socket = dead_socket;
  378. skt->ops->init(skt);
  379. skt->ops->set_socket(skt, &skt->socket);
  380. if (skt->state & SOCKET_PRESENT)
  381. skt->resume_status = socket_setup(skt, resume_delay);
  382. mutex_unlock(&skt->ops_mutex);
  383. return 0;
  384. }
  385. static int socket_late_resume(struct pcmcia_socket *skt)
  386. {
  387. int ret = 0;
  388. mutex_lock(&skt->ops_mutex);
  389. skt->state &= ~SOCKET_SUSPEND;
  390. mutex_unlock(&skt->ops_mutex);
  391. if (!(skt->state & SOCKET_PRESENT)) {
  392. ret = socket_insert(skt);
  393. if (ret == -ENODEV)
  394. ret = 0;
  395. return ret;
  396. }
  397. if (skt->resume_status) {
  398. socket_shutdown(skt);
  399. return 0;
  400. }
  401. if (skt->suspended_state != skt->state) {
  402. dev_dbg(&skt->dev,
  403. "suspend state 0x%x != resume state 0x%x\n",
  404. skt->suspended_state, skt->state);
  405. socket_shutdown(skt);
  406. return socket_insert(skt);
  407. }
  408. if (!(skt->state & SOCKET_CARDBUS) && (skt->callback))
  409. ret = skt->callback->early_resume(skt);
  410. return ret;
  411. }
  412. /*
  413. * Finalize the resume. In case of a cardbus socket, we have
  414. * to rebind the devices as we can't be certain that it has been
  415. * replaced, or not.
  416. */
  417. static int socket_complete_resume(struct pcmcia_socket *skt)
  418. {
  419. int ret = 0;
  420. #ifdef CONFIG_CARDBUS
  421. if (skt->state & SOCKET_CARDBUS) {
  422. /* We can't be sure the CardBus card is the same
  423. * as the one previously inserted. Therefore, remove
  424. * and re-add... */
  425. cb_free(skt);
  426. ret = cb_alloc(skt);
  427. if (ret)
  428. cb_free(skt);
  429. }
  430. #endif
  431. return ret;
  432. }
  433. /*
  434. * Resume a socket. If a card is present, verify its CIS against
  435. * our cached copy. If they are different, the card has been
  436. * replaced, and we need to tell the drivers.
  437. */
  438. static int socket_resume(struct pcmcia_socket *skt)
  439. {
  440. int err;
  441. if (!(skt->state & SOCKET_SUSPEND))
  442. return -EBUSY;
  443. socket_early_resume(skt);
  444. err = socket_late_resume(skt);
  445. if (!err)
  446. err = socket_complete_resume(skt);
  447. return err;
  448. }
  449. static void socket_remove(struct pcmcia_socket *skt)
  450. {
  451. dev_notice(&skt->dev, "pccard: card ejected from slot %d\n", skt->sock);
  452. socket_shutdown(skt);
  453. }
  454. /*
  455. * Process a socket card detect status change.
  456. *
  457. * If we don't have a card already present, delay the detect event for
  458. * about 20ms (to be on the safe side) before reading the socket status.
  459. *
  460. * Some i82365-based systems send multiple SS_DETECT events during card
  461. * insertion, and the "card present" status bit seems to bounce. This
  462. * will probably be true with GPIO-based card detection systems after
  463. * the product has aged.
  464. */
  465. static void socket_detect_change(struct pcmcia_socket *skt)
  466. {
  467. if (!(skt->state & SOCKET_SUSPEND)) {
  468. int status;
  469. if (!(skt->state & SOCKET_PRESENT))
  470. msleep(20);
  471. skt->ops->get_status(skt, &status);
  472. if ((skt->state & SOCKET_PRESENT) &&
  473. !(status & SS_DETECT))
  474. socket_remove(skt);
  475. if (!(skt->state & SOCKET_PRESENT) &&
  476. (status & SS_DETECT))
  477. socket_insert(skt);
  478. }
  479. }
  480. static int pccardd(void *__skt)
  481. {
  482. struct pcmcia_socket *skt = __skt;
  483. int ret;
  484. skt->thread = current;
  485. skt->socket = dead_socket;
  486. skt->ops->init(skt);
  487. skt->ops->set_socket(skt, &skt->socket);
  488. /* register with the device core */
  489. ret = device_register(&skt->dev);
  490. if (ret) {
  491. dev_warn(&skt->dev, "PCMCIA: unable to register socket\n");
  492. skt->thread = NULL;
  493. complete(&skt->thread_done);
  494. return 0;
  495. }
  496. ret = pccard_sysfs_add_socket(&skt->dev);
  497. if (ret)
  498. dev_warn(&skt->dev, "err %d adding socket attributes\n", ret);
  499. complete(&skt->thread_done);
  500. /* wait for userspace to catch up */
  501. msleep(250);
  502. set_freezable();
  503. for (;;) {
  504. unsigned long flags;
  505. unsigned int events;
  506. unsigned int sysfs_events;
  507. spin_lock_irqsave(&skt->thread_lock, flags);
  508. events = skt->thread_events;
  509. skt->thread_events = 0;
  510. sysfs_events = skt->sysfs_events;
  511. skt->sysfs_events = 0;
  512. spin_unlock_irqrestore(&skt->thread_lock, flags);
  513. mutex_lock(&skt->skt_mutex);
  514. if (events & SS_DETECT)
  515. socket_detect_change(skt);
  516. if (sysfs_events) {
  517. if (sysfs_events & PCMCIA_UEVENT_EJECT)
  518. socket_remove(skt);
  519. if (sysfs_events & PCMCIA_UEVENT_INSERT)
  520. socket_insert(skt);
  521. if ((sysfs_events & PCMCIA_UEVENT_SUSPEND) &&
  522. !(skt->state & SOCKET_CARDBUS)) {
  523. if (skt->callback)
  524. ret = skt->callback->suspend(skt);
  525. else
  526. ret = 0;
  527. if (!ret) {
  528. socket_suspend(skt);
  529. msleep(100);
  530. }
  531. }
  532. if ((sysfs_events & PCMCIA_UEVENT_RESUME) &&
  533. !(skt->state & SOCKET_CARDBUS)) {
  534. ret = socket_resume(skt);
  535. if (!ret && skt->callback)
  536. skt->callback->resume(skt);
  537. }
  538. if ((sysfs_events & PCMCIA_UEVENT_REQUERY) &&
  539. !(skt->state & SOCKET_CARDBUS)) {
  540. if (!ret && skt->callback)
  541. skt->callback->requery(skt);
  542. }
  543. }
  544. mutex_unlock(&skt->skt_mutex);
  545. if (events || sysfs_events)
  546. continue;
  547. if (kthread_should_stop())
  548. break;
  549. set_current_state(TASK_INTERRUPTIBLE);
  550. schedule();
  551. /* make sure we are running */
  552. __set_current_state(TASK_RUNNING);
  553. try_to_freeze();
  554. }
  555. /* shut down socket, if a device is still present */
  556. if (skt->state & SOCKET_PRESENT) {
  557. mutex_lock(&skt->skt_mutex);
  558. socket_remove(skt);
  559. mutex_unlock(&skt->skt_mutex);
  560. }
  561. /* remove from the device core */
  562. pccard_sysfs_remove_socket(&skt->dev);
  563. device_unregister(&skt->dev);
  564. return 0;
  565. }
  566. /*
  567. * Yenta (at least) probes interrupts before registering the socket and
  568. * starting the handler thread.
  569. */
  570. void pcmcia_parse_events(struct pcmcia_socket *s, u_int events)
  571. {
  572. unsigned long flags;
  573. dev_dbg(&s->dev, "parse_events: events %08x\n", events);
  574. if (s->thread) {
  575. spin_lock_irqsave(&s->thread_lock, flags);
  576. s->thread_events |= events;
  577. spin_unlock_irqrestore(&s->thread_lock, flags);
  578. wake_up_process(s->thread);
  579. }
  580. } /* pcmcia_parse_events */
  581. EXPORT_SYMBOL(pcmcia_parse_events);
  582. /**
  583. * pcmcia_parse_uevents() - tell pccardd to issue manual commands
  584. * @s: the PCMCIA socket we wan't to command
  585. * @events: events to pass to pccardd
  586. *
  587. * userspace-issued insert, eject, suspend and resume commands must be
  588. * handled by pccardd to avoid any sysfs-related deadlocks. Valid events
  589. * are PCMCIA_UEVENT_EJECT (for eject), PCMCIA_UEVENT__INSERT (for insert),
  590. * PCMCIA_UEVENT_RESUME (for resume), PCMCIA_UEVENT_SUSPEND (for suspend)
  591. * and PCMCIA_UEVENT_REQUERY (for re-querying the PCMCIA card).
  592. */
  593. void pcmcia_parse_uevents(struct pcmcia_socket *s, u_int events)
  594. {
  595. unsigned long flags;
  596. dev_dbg(&s->dev, "parse_uevents: events %08x\n", events);
  597. if (s->thread) {
  598. spin_lock_irqsave(&s->thread_lock, flags);
  599. s->sysfs_events |= events;
  600. spin_unlock_irqrestore(&s->thread_lock, flags);
  601. wake_up_process(s->thread);
  602. }
  603. }
  604. EXPORT_SYMBOL(pcmcia_parse_uevents);
  605. /* register pcmcia_callback */
  606. int pccard_register_pcmcia(struct pcmcia_socket *s, struct pcmcia_callback *c)
  607. {
  608. int ret = 0;
  609. /* s->skt_mutex also protects s->callback */
  610. mutex_lock(&s->skt_mutex);
  611. if (c) {
  612. /* registration */
  613. if (s->callback) {
  614. ret = -EBUSY;
  615. goto err;
  616. }
  617. s->callback = c;
  618. if ((s->state & (SOCKET_PRESENT|SOCKET_CARDBUS)) == SOCKET_PRESENT)
  619. s->callback->add(s);
  620. } else
  621. s->callback = NULL;
  622. err:
  623. mutex_unlock(&s->skt_mutex);
  624. return ret;
  625. }
  626. EXPORT_SYMBOL(pccard_register_pcmcia);
  627. /* I'm not sure which "reset" function this is supposed to use,
  628. * but for now, it uses the low-level interface's reset, not the
  629. * CIS register.
  630. */
  631. int pcmcia_reset_card(struct pcmcia_socket *skt)
  632. {
  633. int ret;
  634. dev_dbg(&skt->dev, "resetting socket\n");
  635. mutex_lock(&skt->skt_mutex);
  636. do {
  637. if (!(skt->state & SOCKET_PRESENT)) {
  638. dev_dbg(&skt->dev, "can't reset, not present\n");
  639. ret = -ENODEV;
  640. break;
  641. }
  642. if (skt->state & SOCKET_SUSPEND) {
  643. dev_dbg(&skt->dev, "can't reset, suspended\n");
  644. ret = -EBUSY;
  645. break;
  646. }
  647. if (skt->state & SOCKET_CARDBUS) {
  648. dev_dbg(&skt->dev, "can't reset, is cardbus\n");
  649. ret = -EPERM;
  650. break;
  651. }
  652. if (skt->callback)
  653. skt->callback->suspend(skt);
  654. mutex_lock(&skt->ops_mutex);
  655. ret = socket_reset(skt);
  656. mutex_unlock(&skt->ops_mutex);
  657. if ((ret == 0) && (skt->callback))
  658. skt->callback->resume(skt);
  659. ret = 0;
  660. } while (0);
  661. mutex_unlock(&skt->skt_mutex);
  662. return ret;
  663. } /* reset_card */
  664. EXPORT_SYMBOL(pcmcia_reset_card);
  665. static int pcmcia_socket_uevent(struct device *dev,
  666. struct kobj_uevent_env *env)
  667. {
  668. struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
  669. if (add_uevent_var(env, "SOCKET_NO=%u", s->sock))
  670. return -ENOMEM;
  671. return 0;
  672. }
  673. static struct completion pcmcia_unload;
  674. static void pcmcia_release_socket_class(struct class *data)
  675. {
  676. complete(&pcmcia_unload);
  677. }
  678. #ifdef CONFIG_PM
  679. static int __pcmcia_pm_op(struct device *dev,
  680. int (*callback) (struct pcmcia_socket *skt))
  681. {
  682. struct pcmcia_socket *s = container_of(dev, struct pcmcia_socket, dev);
  683. int ret;
  684. mutex_lock(&s->skt_mutex);
  685. ret = callback(s);
  686. mutex_unlock(&s->skt_mutex);
  687. return ret;
  688. }
  689. static int pcmcia_socket_dev_suspend_noirq(struct device *dev)
  690. {
  691. return __pcmcia_pm_op(dev, socket_suspend);
  692. }
  693. static int pcmcia_socket_dev_resume_noirq(struct device *dev)
  694. {
  695. return __pcmcia_pm_op(dev, socket_early_resume);
  696. }
  697. static int __used pcmcia_socket_dev_resume(struct device *dev)
  698. {
  699. return __pcmcia_pm_op(dev, socket_late_resume);
  700. }
  701. static void __used pcmcia_socket_dev_complete(struct device *dev)
  702. {
  703. WARN(__pcmcia_pm_op(dev, socket_complete_resume),
  704. "failed to complete resume");
  705. }
  706. static const struct dev_pm_ops pcmcia_socket_pm_ops = {
  707. /* dev_resume may be called with IRQs enabled */
  708. SET_SYSTEM_SLEEP_PM_OPS(NULL,
  709. pcmcia_socket_dev_resume)
  710. /* late suspend must be called with IRQs disabled */
  711. .suspend_noirq = pcmcia_socket_dev_suspend_noirq,
  712. .freeze_noirq = pcmcia_socket_dev_suspend_noirq,
  713. .poweroff_noirq = pcmcia_socket_dev_suspend_noirq,
  714. /* early resume must be called with IRQs disabled */
  715. .resume_noirq = pcmcia_socket_dev_resume_noirq,
  716. .thaw_noirq = pcmcia_socket_dev_resume_noirq,
  717. .restore_noirq = pcmcia_socket_dev_resume_noirq,
  718. .complete = pcmcia_socket_dev_complete,
  719. };
  720. #define PCMCIA_SOCKET_CLASS_PM_OPS (&pcmcia_socket_pm_ops)
  721. #else /* CONFIG_PM */
  722. #define PCMCIA_SOCKET_CLASS_PM_OPS NULL
  723. #endif /* CONFIG_PM */
  724. struct class pcmcia_socket_class = {
  725. .name = "pcmcia_socket",
  726. .dev_uevent = pcmcia_socket_uevent,
  727. .dev_release = pcmcia_release_socket,
  728. .class_release = pcmcia_release_socket_class,
  729. .pm = PCMCIA_SOCKET_CLASS_PM_OPS,
  730. };
  731. EXPORT_SYMBOL(pcmcia_socket_class);
  732. static int __init init_pcmcia_cs(void)
  733. {
  734. init_completion(&pcmcia_unload);
  735. return class_register(&pcmcia_socket_class);
  736. }
  737. static void __exit exit_pcmcia_cs(void)
  738. {
  739. class_unregister(&pcmcia_socket_class);
  740. wait_for_completion(&pcmcia_unload);
  741. }
  742. subsys_initcall(init_pcmcia_cs);
  743. module_exit(exit_pcmcia_cs);