adb.c 21 KB

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