adb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. static void adb_sync_req_done(struct adb_request *req)
  362. {
  363. struct completion *comp = req->arg;
  364. complete(comp);
  365. }
  366. int
  367. adb_request(struct adb_request *req, void (*done)(struct adb_request *),
  368. int flags, int nbytes, ...)
  369. {
  370. va_list list;
  371. int i;
  372. int rc;
  373. struct completion comp;
  374. if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
  375. return -ENXIO;
  376. if (nbytes < 1)
  377. return -EINVAL;
  378. req->nbytes = nbytes+1;
  379. req->done = done;
  380. req->reply_expected = flags & ADBREQ_REPLY;
  381. req->data[0] = ADB_PACKET;
  382. va_start(list, nbytes);
  383. for (i = 0; i < nbytes; ++i)
  384. req->data[i+1] = va_arg(list, int);
  385. va_end(list);
  386. if (flags & ADBREQ_NOSEND)
  387. return 0;
  388. /* Synchronous requests block using an on-stack completion */
  389. if (flags & ADBREQ_SYNC) {
  390. WARN_ON(done);
  391. req->done = adb_sync_req_done;
  392. req->arg = &comp;
  393. init_completion(&comp);
  394. }
  395. rc = adb_controller->send_request(req, 0);
  396. if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
  397. wait_for_completion(&comp);
  398. return rc;
  399. }
  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 != 0) {
  419. printk(KERN_ERR
  420. "Two handlers for ADB device %d\n",
  421. default_id);
  422. continue;
  423. }
  424. write_lock_irq(&adb_handler_lock);
  425. adb_handler[i].handler = handler;
  426. write_unlock_irq(&adb_handler_lock);
  427. ids->id[ids->nids++] = i;
  428. }
  429. }
  430. mutex_unlock(&adb_handler_mutex);
  431. return ids->nids;
  432. }
  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. 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. int
  509. adb_get_infos(int address, int *original_address, int *handler_id)
  510. {
  511. mutex_lock(&adb_handler_mutex);
  512. *original_address = adb_handler[address].original_address;
  513. *handler_id = adb_handler[address].handler_id;
  514. mutex_unlock(&adb_handler_mutex);
  515. return (*original_address != 0);
  516. }
  517. /*
  518. * /dev/adb device driver.
  519. */
  520. #define ADB_MAJOR 56 /* major number for /dev/adb */
  521. struct adbdev_state {
  522. spinlock_t lock;
  523. atomic_t n_pending;
  524. struct adb_request *completed;
  525. wait_queue_head_t wait_queue;
  526. int inuse;
  527. };
  528. static void adb_write_done(struct adb_request *req)
  529. {
  530. struct adbdev_state *state = (struct adbdev_state *) req->arg;
  531. unsigned long flags;
  532. if (!req->complete) {
  533. req->reply_len = 0;
  534. req->complete = 1;
  535. }
  536. spin_lock_irqsave(&state->lock, flags);
  537. atomic_dec(&state->n_pending);
  538. if (!state->inuse) {
  539. kfree(req);
  540. if (atomic_read(&state->n_pending) == 0) {
  541. spin_unlock_irqrestore(&state->lock, flags);
  542. kfree(state);
  543. return;
  544. }
  545. } else {
  546. struct adb_request **ap = &state->completed;
  547. while (*ap != NULL)
  548. ap = &(*ap)->next;
  549. req->next = NULL;
  550. *ap = req;
  551. wake_up_interruptible(&state->wait_queue);
  552. }
  553. spin_unlock_irqrestore(&state->lock, flags);
  554. }
  555. static int
  556. do_adb_query(struct adb_request *req)
  557. {
  558. int ret = -EINVAL;
  559. switch(req->data[1]) {
  560. case ADB_QUERY_GETDEVINFO:
  561. if (req->nbytes < 3)
  562. break;
  563. mutex_lock(&adb_handler_mutex);
  564. req->reply[0] = adb_handler[req->data[2]].original_address;
  565. req->reply[1] = adb_handler[req->data[2]].handler_id;
  566. mutex_unlock(&adb_handler_mutex);
  567. req->complete = 1;
  568. req->reply_len = 2;
  569. adb_write_done(req);
  570. ret = 0;
  571. break;
  572. }
  573. return ret;
  574. }
  575. static int adb_open(struct inode *inode, struct file *file)
  576. {
  577. struct adbdev_state *state;
  578. int ret = 0;
  579. mutex_lock(&adb_mutex);
  580. if (iminor(inode) > 0 || adb_controller == NULL) {
  581. ret = -ENXIO;
  582. goto out;
  583. }
  584. state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
  585. if (state == 0) {
  586. ret = -ENOMEM;
  587. goto out;
  588. }
  589. file->private_data = state;
  590. spin_lock_init(&state->lock);
  591. atomic_set(&state->n_pending, 0);
  592. state->completed = NULL;
  593. init_waitqueue_head(&state->wait_queue);
  594. state->inuse = 1;
  595. out:
  596. mutex_unlock(&adb_mutex);
  597. return ret;
  598. }
  599. static int adb_release(struct inode *inode, struct file *file)
  600. {
  601. struct adbdev_state *state = file->private_data;
  602. unsigned long flags;
  603. mutex_lock(&adb_mutex);
  604. if (state) {
  605. file->private_data = NULL;
  606. spin_lock_irqsave(&state->lock, flags);
  607. if (atomic_read(&state->n_pending) == 0
  608. && state->completed == NULL) {
  609. spin_unlock_irqrestore(&state->lock, flags);
  610. kfree(state);
  611. } else {
  612. state->inuse = 0;
  613. spin_unlock_irqrestore(&state->lock, flags);
  614. }
  615. }
  616. mutex_unlock(&adb_mutex);
  617. return 0;
  618. }
  619. static ssize_t adb_read(struct file *file, char __user *buf,
  620. size_t count, loff_t *ppos)
  621. {
  622. int ret = 0;
  623. struct adbdev_state *state = file->private_data;
  624. struct adb_request *req;
  625. DECLARE_WAITQUEUE(wait, current);
  626. unsigned long flags;
  627. if (count < 2)
  628. return -EINVAL;
  629. if (count > sizeof(req->reply))
  630. count = sizeof(req->reply);
  631. if (!access_ok(VERIFY_WRITE, buf, count))
  632. return -EFAULT;
  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. if (!access_ok(VERIFY_READ, buf, count))
  681. return -EFAULT;
  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. .llseek = no_llseek,
  734. .read = adb_read,
  735. .write = adb_write,
  736. .open = adb_open,
  737. .release = adb_release,
  738. };
  739. #ifdef CONFIG_PM
  740. static const struct dev_pm_ops adb_dev_pm_ops = {
  741. .suspend = adb_suspend,
  742. .resume = adb_resume,
  743. /* Hibernate hooks */
  744. .freeze = adb_freeze,
  745. .thaw = adb_resume,
  746. .poweroff = adb_poweroff,
  747. .restore = adb_resume,
  748. };
  749. #endif
  750. static struct platform_driver adb_pfdrv = {
  751. .driver = {
  752. .name = "adb",
  753. #ifdef CONFIG_PM
  754. .pm = &adb_dev_pm_ops,
  755. #endif
  756. },
  757. };
  758. static struct platform_device adb_pfdev = {
  759. .name = "adb",
  760. };
  761. static int __init
  762. adb_dummy_probe(struct platform_device *dev)
  763. {
  764. if (dev == &adb_pfdev)
  765. return 0;
  766. return -ENODEV;
  767. }
  768. static void __init
  769. adbdev_init(void)
  770. {
  771. if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
  772. printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
  773. return;
  774. }
  775. adb_dev_class = class_create(THIS_MODULE, "adb");
  776. if (IS_ERR(adb_dev_class))
  777. return;
  778. device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
  779. platform_device_register(&adb_pfdev);
  780. platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
  781. }