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