adb.c 20 KB

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