adb.c 19 KB

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