adb.c 19 KB

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