adb.c 20 KB

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