adb.c 20 KB

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