adb.c 19 KB

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