adb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * Device driver for the Apple Desktop Bus
  3. * and the /dev/adb device on macintoshes.
  4. *
  5. * Copyright (C) 1996 Paul Mackerras.
  6. *
  7. * Modified to declare controllers as structures, added
  8. * client notification of bus reset and handles PowerBook
  9. * sleep, by Benjamin Herrenschmidt.
  10. *
  11. * To do:
  12. *
  13. * - /sys/bus/adb to list the devices and infos
  14. * - more /dev/adb to allow userland to receive the
  15. * flow of auto-polling datas from a given device.
  16. * - move bus probe to a kernel thread
  17. */
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/fs.h>
  24. #include <linux/mm.h>
  25. #include <linux/sched.h>
  26. #include <linux/adb.h>
  27. #include <linux/cuda.h>
  28. #include <linux/pmu.h>
  29. #include <linux/notifier.h>
  30. #include <linux/wait.h>
  31. #include <linux/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/completion.h>
  35. #include <linux/device.h>
  36. #include <linux/kthread.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/mutex.h>
  39. #include <linux/uaccess.h>
  40. #ifdef CONFIG_PPC
  41. #include <asm/prom.h>
  42. #include <asm/machdep.h>
  43. #endif
  44. EXPORT_SYMBOL(adb_client_list);
  45. extern struct adb_driver via_macii_driver;
  46. extern struct adb_driver via_maciisi_driver;
  47. extern struct adb_driver via_cuda_driver;
  48. extern struct adb_driver adb_iop_driver;
  49. extern struct adb_driver via_pmu_driver;
  50. extern struct adb_driver macio_adb_driver;
  51. static DEFINE_MUTEX(adb_mutex);
  52. static struct adb_driver *adb_driver_list[] = {
  53. #ifdef CONFIG_ADB_MACII
  54. &via_macii_driver,
  55. #endif
  56. #ifdef CONFIG_ADB_MACIISI
  57. &via_maciisi_driver,
  58. #endif
  59. #ifdef CONFIG_ADB_CUDA
  60. &via_cuda_driver,
  61. #endif
  62. #ifdef CONFIG_ADB_IOP
  63. &adb_iop_driver,
  64. #endif
  65. #if defined(CONFIG_ADB_PMU) || defined(CONFIG_ADB_PMU68K)
  66. &via_pmu_driver,
  67. #endif
  68. #ifdef CONFIG_ADB_MACIO
  69. &macio_adb_driver,
  70. #endif
  71. NULL
  72. };
  73. static struct class *adb_dev_class;
  74. static struct adb_driver *adb_controller;
  75. BLOCKING_NOTIFIER_HEAD(adb_client_list);
  76. static int adb_got_sleep;
  77. static int adb_inited;
  78. static DEFINE_SEMAPHORE(adb_probe_mutex);
  79. static int sleepy_trackpad;
  80. static int autopoll_devs;
  81. int __adb_probe_sync;
  82. static int adb_scan_bus(void);
  83. static int do_adb_reset_bus(void);
  84. static void adbdev_init(void);
  85. static int try_handler_change(int, int);
  86. static struct adb_handler {
  87. void (*handler)(unsigned char *, int, int);
  88. int original_address;
  89. int handler_id;
  90. int busy;
  91. } adb_handler[16];
  92. /*
  93. * The adb_handler_mutex mutex protects all accesses to the original_address
  94. * and handler_id fields of adb_handler[i] for all i, and changes to the
  95. * handler field.
  96. * Accesses to the handler field are protected by the adb_handler_lock
  97. * rwlock. It is held across all calls to any handler, so that by the
  98. * time adb_unregister returns, we know that the old handler isn't being
  99. * called.
  100. */
  101. static DEFINE_MUTEX(adb_handler_mutex);
  102. static DEFINE_RWLOCK(adb_handler_lock);
  103. #if 0
  104. static void printADBreply(struct adb_request *req)
  105. {
  106. int i;
  107. printk("adb reply (%d)", req->reply_len);
  108. for(i = 0; i < req->reply_len; i++)
  109. printk(" %x", req->reply[i]);
  110. printk("\n");
  111. }
  112. #endif
  113. static int adb_scan_bus(void)
  114. {
  115. int i, highFree=0, noMovement;
  116. int devmask = 0;
  117. struct adb_request req;
  118. /* assumes adb_handler[] is all zeroes at this point */
  119. for (i = 1; i < 16; i++) {
  120. /* see if there is anything at address i */
  121. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  122. (i << 4) | 0xf);
  123. if (req.reply_len > 1)
  124. /* one or more devices at this address */
  125. adb_handler[i].original_address = i;
  126. else if (i > highFree)
  127. highFree = i;
  128. }
  129. /* Note we reset noMovement to 0 each time we move a device */
  130. for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
  131. for (i = 1; i < 16; i++) {
  132. if (adb_handler[i].original_address == 0)
  133. continue;
  134. /*
  135. * Send a "talk register 3" command to address i
  136. * to provoke a collision if there is more than
  137. * one device at this address.
  138. */
  139. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  140. (i << 4) | 0xf);
  141. /*
  142. * Move the device(s) which didn't detect a
  143. * collision to address `highFree'. Hopefully
  144. * this only moves one device.
  145. */
  146. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  147. (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
  148. /*
  149. * See if anybody actually moved. This is suggested
  150. * by HW TechNote 01:
  151. *
  152. * http://developer.apple.com/technotes/hw/hw_01.html
  153. */
  154. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  155. (highFree << 4) | 0xf);
  156. if (req.reply_len <= 1) continue;
  157. /*
  158. * Test whether there are any device(s) left
  159. * at address i.
  160. */
  161. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  162. (i << 4) | 0xf);
  163. if (req.reply_len > 1) {
  164. /*
  165. * There are still one or more devices
  166. * left at address i. Register the one(s)
  167. * we moved to `highFree', and find a new
  168. * value for highFree.
  169. */
  170. adb_handler[highFree].original_address =
  171. adb_handler[i].original_address;
  172. while (highFree > 0 &&
  173. adb_handler[highFree].original_address)
  174. highFree--;
  175. if (highFree <= 0)
  176. break;
  177. noMovement = 0;
  178. } else {
  179. /*
  180. * No devices left at address i; move the
  181. * one(s) we moved to `highFree' back to i.
  182. */
  183. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  184. (highFree << 4) | 0xb,
  185. (i | 0x60), 0xfe);
  186. }
  187. }
  188. }
  189. /* Now fill in the handler_id field of the adb_handler entries. */
  190. printk(KERN_DEBUG "adb devices:");
  191. for (i = 1; i < 16; i++) {
  192. if (adb_handler[i].original_address == 0)
  193. continue;
  194. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  195. (i << 4) | 0xf);
  196. adb_handler[i].handler_id = req.reply[2];
  197. printk(" [%d]: %d %x", i, adb_handler[i].original_address,
  198. adb_handler[i].handler_id);
  199. devmask |= 1 << i;
  200. }
  201. printk("\n");
  202. return devmask;
  203. }
  204. /*
  205. * This kernel task handles ADB probing. It dies once probing is
  206. * completed.
  207. */
  208. static int
  209. adb_probe_task(void *x)
  210. {
  211. printk(KERN_INFO "adb: starting probe task...\n");
  212. do_adb_reset_bus();
  213. printk(KERN_INFO "adb: finished probe task...\n");
  214. up(&adb_probe_mutex);
  215. return 0;
  216. }
  217. static void
  218. __adb_probe_task(struct work_struct *bullshit)
  219. {
  220. kthread_run(adb_probe_task, NULL, "kadbprobe");
  221. }
  222. static DECLARE_WORK(adb_reset_work, __adb_probe_task);
  223. int
  224. adb_reset_bus(void)
  225. {
  226. if (__adb_probe_sync) {
  227. do_adb_reset_bus();
  228. return 0;
  229. }
  230. down(&adb_probe_mutex);
  231. schedule_work(&adb_reset_work);
  232. return 0;
  233. }
  234. #ifdef CONFIG_PM
  235. /*
  236. * notify clients before sleep
  237. */
  238. static int __adb_suspend(struct platform_device *dev, pm_message_t state)
  239. {
  240. adb_got_sleep = 1;
  241. /* We need to get a lock on the probe thread */
  242. down(&adb_probe_mutex);
  243. /* Stop autopoll */
  244. if (adb_controller->autopoll)
  245. adb_controller->autopoll(0);
  246. blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
  247. return 0;
  248. }
  249. static int adb_suspend(struct device *dev)
  250. {
  251. return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
  252. }
  253. static int adb_freeze(struct device *dev)
  254. {
  255. return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
  256. }
  257. static int adb_poweroff(struct device *dev)
  258. {
  259. return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
  260. }
  261. /*
  262. * reset bus after sleep
  263. */
  264. static int __adb_resume(struct platform_device *dev)
  265. {
  266. adb_got_sleep = 0;
  267. up(&adb_probe_mutex);
  268. adb_reset_bus();
  269. return 0;
  270. }
  271. static int adb_resume(struct device *dev)
  272. {
  273. return __adb_resume(to_platform_device(dev));
  274. }
  275. #endif /* CONFIG_PM */
  276. static int __init adb_init(void)
  277. {
  278. struct adb_driver *driver;
  279. int i;
  280. #ifdef CONFIG_PPC32
  281. if (!machine_is(chrp) && !machine_is(powermac))
  282. return 0;
  283. #endif
  284. #ifdef CONFIG_MAC
  285. if (!MACH_IS_MAC)
  286. return 0;
  287. #endif
  288. /* xmon may do early-init */
  289. if (adb_inited)
  290. return 0;
  291. adb_inited = 1;
  292. adb_controller = NULL;
  293. i = 0;
  294. while ((driver = adb_driver_list[i++]) != NULL) {
  295. if (!driver->probe()) {
  296. adb_controller = driver;
  297. break;
  298. }
  299. }
  300. if (adb_controller != NULL && adb_controller->init &&
  301. adb_controller->init())
  302. adb_controller = NULL;
  303. if (adb_controller == NULL) {
  304. printk(KERN_WARNING "Warning: no ADB interface detected\n");
  305. } else {
  306. #ifdef CONFIG_PPC
  307. if (of_machine_is_compatible("AAPL,PowerBook1998") ||
  308. of_machine_is_compatible("PowerBook1,1"))
  309. sleepy_trackpad = 1;
  310. #endif /* CONFIG_PPC */
  311. adbdev_init();
  312. adb_reset_bus();
  313. }
  314. return 0;
  315. }
  316. device_initcall(adb_init);
  317. static int
  318. do_adb_reset_bus(void)
  319. {
  320. int ret;
  321. if (adb_controller == NULL)
  322. return -ENXIO;
  323. if (adb_controller->autopoll)
  324. adb_controller->autopoll(0);
  325. blocking_notifier_call_chain(&adb_client_list,
  326. ADB_MSG_PRE_RESET, NULL);
  327. if (sleepy_trackpad) {
  328. /* Let the trackpad settle down */
  329. msleep(500);
  330. }
  331. mutex_lock(&adb_handler_mutex);
  332. write_lock_irq(&adb_handler_lock);
  333. memset(adb_handler, 0, sizeof(adb_handler));
  334. write_unlock_irq(&adb_handler_lock);
  335. /* That one is still a bit synchronous, oh well... */
  336. if (adb_controller->reset_bus)
  337. ret = adb_controller->reset_bus();
  338. else
  339. ret = 0;
  340. if (sleepy_trackpad) {
  341. /* Let the trackpad settle down */
  342. msleep(1500);
  343. }
  344. if (!ret) {
  345. autopoll_devs = adb_scan_bus();
  346. if (adb_controller->autopoll)
  347. adb_controller->autopoll(autopoll_devs);
  348. }
  349. mutex_unlock(&adb_handler_mutex);
  350. blocking_notifier_call_chain(&adb_client_list,
  351. ADB_MSG_POST_RESET, NULL);
  352. return ret;
  353. }
  354. void
  355. adb_poll(void)
  356. {
  357. if ((adb_controller == NULL)||(adb_controller->poll == NULL))
  358. return;
  359. adb_controller->poll();
  360. }
  361. EXPORT_SYMBOL(adb_poll);
  362. static void adb_sync_req_done(struct adb_request *req)
  363. {
  364. struct completion *comp = req->arg;
  365. complete(comp);
  366. }
  367. int
  368. adb_request(struct adb_request *req, void (*done)(struct adb_request *),
  369. int flags, int nbytes, ...)
  370. {
  371. va_list list;
  372. int i;
  373. int rc;
  374. struct completion comp;
  375. if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
  376. return -ENXIO;
  377. if (nbytes < 1)
  378. return -EINVAL;
  379. req->nbytes = nbytes+1;
  380. req->done = done;
  381. req->reply_expected = flags & ADBREQ_REPLY;
  382. req->data[0] = ADB_PACKET;
  383. va_start(list, nbytes);
  384. for (i = 0; i < nbytes; ++i)
  385. req->data[i+1] = va_arg(list, int);
  386. va_end(list);
  387. if (flags & ADBREQ_NOSEND)
  388. return 0;
  389. /* Synchronous requests block using an on-stack completion */
  390. if (flags & ADBREQ_SYNC) {
  391. WARN_ON(done);
  392. req->done = adb_sync_req_done;
  393. req->arg = &comp;
  394. init_completion(&comp);
  395. }
  396. rc = adb_controller->send_request(req, 0);
  397. if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
  398. wait_for_completion(&comp);
  399. return rc;
  400. }
  401. EXPORT_SYMBOL(adb_request);
  402. /* Ultimately this should return the number of devices with
  403. the given default id.
  404. And it does it now ! Note: changed behaviour: This function
  405. will now register if default_id _and_ handler_id both match
  406. but handler_id can be left to 0 to match with default_id only.
  407. When handler_id is set, this function will try to adjust
  408. the handler_id id it doesn't match. */
  409. int
  410. adb_register(int default_id, int handler_id, struct adb_ids *ids,
  411. void (*handler)(unsigned char *, int, int))
  412. {
  413. int i;
  414. mutex_lock(&adb_handler_mutex);
  415. ids->nids = 0;
  416. for (i = 1; i < 16; i++) {
  417. if ((adb_handler[i].original_address == default_id) &&
  418. (!handler_id || (handler_id == adb_handler[i].handler_id) ||
  419. try_handler_change(i, handler_id))) {
  420. if (adb_handler[i].handler != 0) {
  421. printk(KERN_ERR
  422. "Two handlers for ADB device %d\n",
  423. default_id);
  424. continue;
  425. }
  426. write_lock_irq(&adb_handler_lock);
  427. adb_handler[i].handler = handler;
  428. write_unlock_irq(&adb_handler_lock);
  429. ids->id[ids->nids++] = i;
  430. }
  431. }
  432. mutex_unlock(&adb_handler_mutex);
  433. return ids->nids;
  434. }
  435. EXPORT_SYMBOL(adb_register);
  436. int
  437. adb_unregister(int index)
  438. {
  439. int ret = -ENODEV;
  440. mutex_lock(&adb_handler_mutex);
  441. write_lock_irq(&adb_handler_lock);
  442. if (adb_handler[index].handler) {
  443. while(adb_handler[index].busy) {
  444. write_unlock_irq(&adb_handler_lock);
  445. yield();
  446. write_lock_irq(&adb_handler_lock);
  447. }
  448. ret = 0;
  449. adb_handler[index].handler = NULL;
  450. }
  451. write_unlock_irq(&adb_handler_lock);
  452. mutex_unlock(&adb_handler_mutex);
  453. return ret;
  454. }
  455. EXPORT_SYMBOL(adb_unregister);
  456. void
  457. adb_input(unsigned char *buf, int nb, int autopoll)
  458. {
  459. int i, id;
  460. static int dump_adb_input;
  461. unsigned long flags;
  462. void (*handler)(unsigned char *, int, int);
  463. /* We skip keystrokes and mouse moves when the sleep process
  464. * has been started. We stop autopoll, but this is another security
  465. */
  466. if (adb_got_sleep)
  467. return;
  468. id = buf[0] >> 4;
  469. if (dump_adb_input) {
  470. printk(KERN_INFO "adb packet: ");
  471. for (i = 0; i < nb; ++i)
  472. printk(" %x", buf[i]);
  473. printk(", id = %d\n", id);
  474. }
  475. write_lock_irqsave(&adb_handler_lock, flags);
  476. handler = adb_handler[id].handler;
  477. if (handler != NULL)
  478. adb_handler[id].busy = 1;
  479. write_unlock_irqrestore(&adb_handler_lock, flags);
  480. if (handler != NULL) {
  481. (*handler)(buf, nb, autopoll);
  482. wmb();
  483. adb_handler[id].busy = 0;
  484. }
  485. }
  486. /* Try to change handler to new_id. Will return 1 if successful. */
  487. static int try_handler_change(int address, int new_id)
  488. {
  489. struct adb_request req;
  490. if (adb_handler[address].handler_id == new_id)
  491. return 1;
  492. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  493. ADB_WRITEREG(address, 3), address | 0x20, new_id);
  494. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  495. ADB_READREG(address, 3));
  496. if (req.reply_len < 2)
  497. return 0;
  498. if (req.reply[2] != new_id)
  499. return 0;
  500. adb_handler[address].handler_id = req.reply[2];
  501. return 1;
  502. }
  503. int
  504. adb_try_handler_change(int address, int new_id)
  505. {
  506. int ret;
  507. mutex_lock(&adb_handler_mutex);
  508. ret = try_handler_change(address, new_id);
  509. mutex_unlock(&adb_handler_mutex);
  510. return ret;
  511. }
  512. EXPORT_SYMBOL(adb_try_handler_change);
  513. int
  514. adb_get_infos(int address, int *original_address, int *handler_id)
  515. {
  516. mutex_lock(&adb_handler_mutex);
  517. *original_address = adb_handler[address].original_address;
  518. *handler_id = adb_handler[address].handler_id;
  519. mutex_unlock(&adb_handler_mutex);
  520. return (*original_address != 0);
  521. }
  522. /*
  523. * /dev/adb device driver.
  524. */
  525. #define ADB_MAJOR 56 /* major number for /dev/adb */
  526. struct adbdev_state {
  527. spinlock_t lock;
  528. atomic_t n_pending;
  529. struct adb_request *completed;
  530. wait_queue_head_t wait_queue;
  531. int inuse;
  532. };
  533. static void adb_write_done(struct adb_request *req)
  534. {
  535. struct adbdev_state *state = (struct adbdev_state *) req->arg;
  536. unsigned long flags;
  537. if (!req->complete) {
  538. req->reply_len = 0;
  539. req->complete = 1;
  540. }
  541. spin_lock_irqsave(&state->lock, flags);
  542. atomic_dec(&state->n_pending);
  543. if (!state->inuse) {
  544. kfree(req);
  545. if (atomic_read(&state->n_pending) == 0) {
  546. spin_unlock_irqrestore(&state->lock, flags);
  547. kfree(state);
  548. return;
  549. }
  550. } else {
  551. struct adb_request **ap = &state->completed;
  552. while (*ap != NULL)
  553. ap = &(*ap)->next;
  554. req->next = NULL;
  555. *ap = req;
  556. wake_up_interruptible(&state->wait_queue);
  557. }
  558. spin_unlock_irqrestore(&state->lock, flags);
  559. }
  560. static int
  561. do_adb_query(struct adb_request *req)
  562. {
  563. int ret = -EINVAL;
  564. switch(req->data[1]) {
  565. case ADB_QUERY_GETDEVINFO:
  566. if (req->nbytes < 3)
  567. break;
  568. mutex_lock(&adb_handler_mutex);
  569. req->reply[0] = adb_handler[req->data[2]].original_address;
  570. req->reply[1] = adb_handler[req->data[2]].handler_id;
  571. mutex_unlock(&adb_handler_mutex);
  572. req->complete = 1;
  573. req->reply_len = 2;
  574. adb_write_done(req);
  575. ret = 0;
  576. break;
  577. }
  578. return ret;
  579. }
  580. static int adb_open(struct inode *inode, struct file *file)
  581. {
  582. struct adbdev_state *state;
  583. int ret = 0;
  584. mutex_lock(&adb_mutex);
  585. if (iminor(inode) > 0 || adb_controller == NULL) {
  586. ret = -ENXIO;
  587. goto out;
  588. }
  589. state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
  590. if (state == 0) {
  591. ret = -ENOMEM;
  592. goto out;
  593. }
  594. file->private_data = state;
  595. spin_lock_init(&state->lock);
  596. atomic_set(&state->n_pending, 0);
  597. state->completed = NULL;
  598. init_waitqueue_head(&state->wait_queue);
  599. state->inuse = 1;
  600. out:
  601. mutex_unlock(&adb_mutex);
  602. return ret;
  603. }
  604. static int adb_release(struct inode *inode, struct file *file)
  605. {
  606. struct adbdev_state *state = file->private_data;
  607. unsigned long flags;
  608. mutex_lock(&adb_mutex);
  609. if (state) {
  610. file->private_data = NULL;
  611. spin_lock_irqsave(&state->lock, flags);
  612. if (atomic_read(&state->n_pending) == 0
  613. && state->completed == NULL) {
  614. spin_unlock_irqrestore(&state->lock, flags);
  615. kfree(state);
  616. } else {
  617. state->inuse = 0;
  618. spin_unlock_irqrestore(&state->lock, flags);
  619. }
  620. }
  621. mutex_unlock(&adb_mutex);
  622. return 0;
  623. }
  624. static ssize_t adb_read(struct file *file, char __user *buf,
  625. size_t count, loff_t *ppos)
  626. {
  627. int ret = 0;
  628. struct adbdev_state *state = file->private_data;
  629. struct adb_request *req;
  630. DECLARE_WAITQUEUE(wait, current);
  631. unsigned long flags;
  632. if (count < 2)
  633. return -EINVAL;
  634. if (count > sizeof(req->reply))
  635. count = sizeof(req->reply);
  636. if (!access_ok(VERIFY_WRITE, buf, count))
  637. return -EFAULT;
  638. req = NULL;
  639. spin_lock_irqsave(&state->lock, flags);
  640. add_wait_queue(&state->wait_queue, &wait);
  641. set_current_state(TASK_INTERRUPTIBLE);
  642. for (;;) {
  643. req = state->completed;
  644. if (req != NULL)
  645. state->completed = req->next;
  646. else if (atomic_read(&state->n_pending) == 0)
  647. ret = -EIO;
  648. if (req != NULL || ret != 0)
  649. break;
  650. if (file->f_flags & O_NONBLOCK) {
  651. ret = -EAGAIN;
  652. break;
  653. }
  654. if (signal_pending(current)) {
  655. ret = -ERESTARTSYS;
  656. break;
  657. }
  658. spin_unlock_irqrestore(&state->lock, flags);
  659. schedule();
  660. spin_lock_irqsave(&state->lock, flags);
  661. }
  662. set_current_state(TASK_RUNNING);
  663. remove_wait_queue(&state->wait_queue, &wait);
  664. spin_unlock_irqrestore(&state->lock, flags);
  665. if (ret)
  666. return ret;
  667. ret = req->reply_len;
  668. if (ret > count)
  669. ret = count;
  670. if (ret > 0 && copy_to_user(buf, req->reply, ret))
  671. ret = -EFAULT;
  672. kfree(req);
  673. return ret;
  674. }
  675. static ssize_t adb_write(struct file *file, const char __user *buf,
  676. size_t count, loff_t *ppos)
  677. {
  678. int ret/*, i*/;
  679. struct adbdev_state *state = file->private_data;
  680. struct adb_request *req;
  681. if (count < 2 || count > sizeof(req->data))
  682. return -EINVAL;
  683. if (adb_controller == NULL)
  684. return -ENXIO;
  685. if (!access_ok(VERIFY_READ, buf, count))
  686. return -EFAULT;
  687. req = kmalloc(sizeof(struct adb_request),
  688. GFP_KERNEL);
  689. if (req == NULL)
  690. return -ENOMEM;
  691. req->nbytes = count;
  692. req->done = adb_write_done;
  693. req->arg = (void *) state;
  694. req->complete = 0;
  695. ret = -EFAULT;
  696. if (copy_from_user(req->data, buf, count))
  697. goto out;
  698. atomic_inc(&state->n_pending);
  699. /* If a probe is in progress or we are sleeping, wait for it to complete */
  700. down(&adb_probe_mutex);
  701. /* Queries are special requests sent to the ADB driver itself */
  702. if (req->data[0] == ADB_QUERY) {
  703. if (count > 1)
  704. ret = do_adb_query(req);
  705. else
  706. ret = -EINVAL;
  707. up(&adb_probe_mutex);
  708. }
  709. /* Special case for ADB_BUSRESET request, all others are sent to
  710. the controller */
  711. else if ((req->data[0] == ADB_PACKET) && (count > 1)
  712. && (req->data[1] == ADB_BUSRESET)) {
  713. ret = do_adb_reset_bus();
  714. up(&adb_probe_mutex);
  715. atomic_dec(&state->n_pending);
  716. if (ret == 0)
  717. ret = count;
  718. goto out;
  719. } else {
  720. req->reply_expected = ((req->data[1] & 0xc) == 0xc);
  721. if (adb_controller && adb_controller->send_request)
  722. ret = adb_controller->send_request(req, 0);
  723. else
  724. ret = -ENXIO;
  725. up(&adb_probe_mutex);
  726. }
  727. if (ret != 0) {
  728. atomic_dec(&state->n_pending);
  729. goto out;
  730. }
  731. return count;
  732. out:
  733. kfree(req);
  734. return ret;
  735. }
  736. static const struct file_operations adb_fops = {
  737. .owner = THIS_MODULE,
  738. .llseek = no_llseek,
  739. .read = adb_read,
  740. .write = adb_write,
  741. .open = adb_open,
  742. .release = adb_release,
  743. };
  744. #ifdef CONFIG_PM
  745. static const struct dev_pm_ops adb_dev_pm_ops = {
  746. .suspend = adb_suspend,
  747. .resume = adb_resume,
  748. /* Hibernate hooks */
  749. .freeze = adb_freeze,
  750. .thaw = adb_resume,
  751. .poweroff = adb_poweroff,
  752. .restore = adb_resume,
  753. };
  754. #endif
  755. static struct platform_driver adb_pfdrv = {
  756. .driver = {
  757. .name = "adb",
  758. #ifdef CONFIG_PM
  759. .pm = &adb_dev_pm_ops,
  760. #endif
  761. },
  762. };
  763. static struct platform_device adb_pfdev = {
  764. .name = "adb",
  765. };
  766. static int __init
  767. adb_dummy_probe(struct platform_device *dev)
  768. {
  769. if (dev == &adb_pfdev)
  770. return 0;
  771. return -ENODEV;
  772. }
  773. static void __init
  774. adbdev_init(void)
  775. {
  776. if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
  777. printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
  778. return;
  779. }
  780. adb_dev_class = class_create(THIS_MODULE, "adb");
  781. if (IS_ERR(adb_dev_class))
  782. return;
  783. device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
  784. platform_device_register(&adb_pfdev);
  785. platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
  786. }