adb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. struct notifier_block *adb_client_list = NULL;
  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 = notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
  319. if (ret & NOTIFY_STOP_MASK) {
  320. up(&adb_probe_mutex);
  321. return PBOOK_SLEEP_REFUSE;
  322. }
  323. break;
  324. case PBOOK_SLEEP_REJECT:
  325. if (adb_got_sleep) {
  326. adb_got_sleep = 0;
  327. up(&adb_probe_mutex);
  328. adb_reset_bus();
  329. }
  330. break;
  331. case PBOOK_SLEEP_NOW:
  332. break;
  333. case PBOOK_WAKE:
  334. adb_got_sleep = 0;
  335. up(&adb_probe_mutex);
  336. adb_reset_bus();
  337. break;
  338. }
  339. return PBOOK_SLEEP_OK;
  340. }
  341. #endif /* CONFIG_PM */
  342. static int
  343. do_adb_reset_bus(void)
  344. {
  345. int ret, nret;
  346. if (adb_controller == NULL)
  347. return -ENXIO;
  348. if (adb_controller->autopoll)
  349. adb_controller->autopoll(0);
  350. nret = notifier_call_chain(&adb_client_list, 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 = notifier_call_chain(&adb_client_list, ADB_MSG_POST_RESET, NULL);
  380. if (nret & NOTIFY_STOP_MASK)
  381. return -EBUSY;
  382. return ret;
  383. }
  384. void
  385. adb_poll(void)
  386. {
  387. if ((adb_controller == NULL)||(adb_controller->poll == NULL))
  388. return;
  389. adb_controller->poll();
  390. }
  391. static void
  392. adb_probe_wakeup(struct adb_request *req)
  393. {
  394. complete(&adb_probe_task_comp);
  395. }
  396. /* Static request used during probe */
  397. static struct adb_request adb_sreq;
  398. static unsigned long adb_sreq_lock; // Use semaphore ! */
  399. int
  400. adb_request(struct adb_request *req, void (*done)(struct adb_request *),
  401. int flags, int nbytes, ...)
  402. {
  403. va_list list;
  404. int i, use_sreq;
  405. int rc;
  406. if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
  407. return -ENXIO;
  408. if (nbytes < 1)
  409. return -EINVAL;
  410. if (req == NULL && (flags & ADBREQ_NOSEND))
  411. return -EINVAL;
  412. if (req == NULL) {
  413. if (test_and_set_bit(0,&adb_sreq_lock)) {
  414. printk("adb.c: Warning: contention on static request !\n");
  415. return -EPERM;
  416. }
  417. req = &adb_sreq;
  418. flags |= ADBREQ_SYNC;
  419. use_sreq = 1;
  420. } else
  421. use_sreq = 0;
  422. req->nbytes = nbytes+1;
  423. req->done = done;
  424. req->reply_expected = flags & ADBREQ_REPLY;
  425. req->data[0] = ADB_PACKET;
  426. va_start(list, nbytes);
  427. for (i = 0; i < nbytes; ++i)
  428. req->data[i+1] = va_arg(list, int);
  429. va_end(list);
  430. if (flags & ADBREQ_NOSEND)
  431. return 0;
  432. /* Synchronous requests send from the probe thread cause it to
  433. * block. Beware that the "done" callback will be overriden !
  434. */
  435. if ((flags & ADBREQ_SYNC) &&
  436. (current->pid && adb_probe_task_pid &&
  437. adb_probe_task_pid == current->pid)) {
  438. req->done = adb_probe_wakeup;
  439. rc = adb_controller->send_request(req, 0);
  440. if (rc || req->complete)
  441. goto bail;
  442. wait_for_completion(&adb_probe_task_comp);
  443. rc = 0;
  444. goto bail;
  445. }
  446. rc = adb_controller->send_request(req, flags & ADBREQ_SYNC);
  447. bail:
  448. if (use_sreq)
  449. clear_bit(0, &adb_sreq_lock);
  450. return rc;
  451. }
  452. /* Ultimately this should return the number of devices with
  453. the given default id.
  454. And it does it now ! Note: changed behaviour: This function
  455. will now register if default_id _and_ handler_id both match
  456. but handler_id can be left to 0 to match with default_id only.
  457. When handler_id is set, this function will try to adjust
  458. the handler_id id it doesn't match. */
  459. int
  460. adb_register(int default_id, int handler_id, struct adb_ids *ids,
  461. void (*handler)(unsigned char *, int, struct pt_regs *, int))
  462. {
  463. int i;
  464. down(&adb_handler_sem);
  465. ids->nids = 0;
  466. for (i = 1; i < 16; i++) {
  467. if ((adb_handler[i].original_address == default_id) &&
  468. (!handler_id || (handler_id == adb_handler[i].handler_id) ||
  469. try_handler_change(i, handler_id))) {
  470. if (adb_handler[i].handler != 0) {
  471. printk(KERN_ERR
  472. "Two handlers for ADB device %d\n",
  473. default_id);
  474. continue;
  475. }
  476. write_lock_irq(&adb_handler_lock);
  477. adb_handler[i].handler = handler;
  478. write_unlock_irq(&adb_handler_lock);
  479. ids->id[ids->nids++] = i;
  480. }
  481. }
  482. up(&adb_handler_sem);
  483. return ids->nids;
  484. }
  485. int
  486. adb_unregister(int index)
  487. {
  488. int ret = -ENODEV;
  489. down(&adb_handler_sem);
  490. write_lock_irq(&adb_handler_lock);
  491. if (adb_handler[index].handler) {
  492. while(adb_handler[index].busy) {
  493. write_unlock_irq(&adb_handler_lock);
  494. yield();
  495. write_lock_irq(&adb_handler_lock);
  496. }
  497. ret = 0;
  498. adb_handler[index].handler = NULL;
  499. }
  500. write_unlock_irq(&adb_handler_lock);
  501. up(&adb_handler_sem);
  502. return ret;
  503. }
  504. void
  505. adb_input(unsigned char *buf, int nb, struct pt_regs *regs, int autopoll)
  506. {
  507. int i, id;
  508. static int dump_adb_input = 0;
  509. unsigned long flags;
  510. void (*handler)(unsigned char *, int, struct pt_regs *, int);
  511. /* We skip keystrokes and mouse moves when the sleep process
  512. * has been started. We stop autopoll, but this is another security
  513. */
  514. if (adb_got_sleep)
  515. return;
  516. id = buf[0] >> 4;
  517. if (dump_adb_input) {
  518. printk(KERN_INFO "adb packet: ");
  519. for (i = 0; i < nb; ++i)
  520. printk(" %x", buf[i]);
  521. printk(", id = %d\n", id);
  522. }
  523. write_lock_irqsave(&adb_handler_lock, flags);
  524. handler = adb_handler[id].handler;
  525. if (handler != NULL)
  526. adb_handler[id].busy = 1;
  527. write_unlock_irqrestore(&adb_handler_lock, flags);
  528. if (handler != NULL) {
  529. (*handler)(buf, nb, regs, autopoll);
  530. wmb();
  531. adb_handler[id].busy = 0;
  532. }
  533. }
  534. /* Try to change handler to new_id. Will return 1 if successful. */
  535. static int try_handler_change(int address, int new_id)
  536. {
  537. struct adb_request req;
  538. if (adb_handler[address].handler_id == new_id)
  539. return 1;
  540. adb_request(&req, NULL, ADBREQ_SYNC, 3,
  541. ADB_WRITEREG(address, 3), address | 0x20, new_id);
  542. adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
  543. ADB_READREG(address, 3));
  544. if (req.reply_len < 2)
  545. return 0;
  546. if (req.reply[2] != new_id)
  547. return 0;
  548. adb_handler[address].handler_id = req.reply[2];
  549. return 1;
  550. }
  551. int
  552. adb_try_handler_change(int address, int new_id)
  553. {
  554. int ret;
  555. down(&adb_handler_sem);
  556. ret = try_handler_change(address, new_id);
  557. up(&adb_handler_sem);
  558. return ret;
  559. }
  560. int
  561. adb_get_infos(int address, int *original_address, int *handler_id)
  562. {
  563. down(&adb_handler_sem);
  564. *original_address = adb_handler[address].original_address;
  565. *handler_id = adb_handler[address].handler_id;
  566. up(&adb_handler_sem);
  567. return (*original_address != 0);
  568. }
  569. /*
  570. * /dev/adb device driver.
  571. */
  572. #define ADB_MAJOR 56 /* major number for /dev/adb */
  573. struct adbdev_state {
  574. spinlock_t lock;
  575. atomic_t n_pending;
  576. struct adb_request *completed;
  577. wait_queue_head_t wait_queue;
  578. int inuse;
  579. };
  580. static void adb_write_done(struct adb_request *req)
  581. {
  582. struct adbdev_state *state = (struct adbdev_state *) req->arg;
  583. unsigned long flags;
  584. if (!req->complete) {
  585. req->reply_len = 0;
  586. req->complete = 1;
  587. }
  588. spin_lock_irqsave(&state->lock, flags);
  589. atomic_dec(&state->n_pending);
  590. if (!state->inuse) {
  591. kfree(req);
  592. if (atomic_read(&state->n_pending) == 0) {
  593. spin_unlock_irqrestore(&state->lock, flags);
  594. kfree(state);
  595. return;
  596. }
  597. } else {
  598. struct adb_request **ap = &state->completed;
  599. while (*ap != NULL)
  600. ap = &(*ap)->next;
  601. req->next = NULL;
  602. *ap = req;
  603. wake_up_interruptible(&state->wait_queue);
  604. }
  605. spin_unlock_irqrestore(&state->lock, flags);
  606. }
  607. static int
  608. do_adb_query(struct adb_request *req)
  609. {
  610. int ret = -EINVAL;
  611. switch(req->data[1])
  612. {
  613. case ADB_QUERY_GETDEVINFO:
  614. if (req->nbytes < 3)
  615. break;
  616. down(&adb_handler_sem);
  617. req->reply[0] = adb_handler[req->data[2]].original_address;
  618. req->reply[1] = adb_handler[req->data[2]].handler_id;
  619. up(&adb_handler_sem);
  620. req->complete = 1;
  621. req->reply_len = 2;
  622. adb_write_done(req);
  623. ret = 0;
  624. break;
  625. }
  626. return ret;
  627. }
  628. static int adb_open(struct inode *inode, struct file *file)
  629. {
  630. struct adbdev_state *state;
  631. if (iminor(inode) > 0 || adb_controller == NULL)
  632. return -ENXIO;
  633. state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
  634. if (state == 0)
  635. return -ENOMEM;
  636. file->private_data = state;
  637. spin_lock_init(&state->lock);
  638. atomic_set(&state->n_pending, 0);
  639. state->completed = NULL;
  640. init_waitqueue_head(&state->wait_queue);
  641. state->inuse = 1;
  642. return 0;
  643. }
  644. static int adb_release(struct inode *inode, struct file *file)
  645. {
  646. struct adbdev_state *state = file->private_data;
  647. unsigned long flags;
  648. lock_kernel();
  649. if (state) {
  650. file->private_data = NULL;
  651. spin_lock_irqsave(&state->lock, flags);
  652. if (atomic_read(&state->n_pending) == 0
  653. && state->completed == NULL) {
  654. spin_unlock_irqrestore(&state->lock, flags);
  655. kfree(state);
  656. } else {
  657. state->inuse = 0;
  658. spin_unlock_irqrestore(&state->lock, flags);
  659. }
  660. }
  661. unlock_kernel();
  662. return 0;
  663. }
  664. static ssize_t adb_read(struct file *file, char __user *buf,
  665. size_t count, loff_t *ppos)
  666. {
  667. int ret = 0;
  668. struct adbdev_state *state = file->private_data;
  669. struct adb_request *req;
  670. wait_queue_t wait = __WAITQUEUE_INITIALIZER(wait,current);
  671. unsigned long flags;
  672. if (count < 2)
  673. return -EINVAL;
  674. if (count > sizeof(req->reply))
  675. count = sizeof(req->reply);
  676. if (!access_ok(VERIFY_WRITE, buf, count))
  677. return -EFAULT;
  678. req = NULL;
  679. spin_lock_irqsave(&state->lock, flags);
  680. add_wait_queue(&state->wait_queue, &wait);
  681. current->state = TASK_INTERRUPTIBLE;
  682. for (;;) {
  683. req = state->completed;
  684. if (req != NULL)
  685. state->completed = req->next;
  686. else if (atomic_read(&state->n_pending) == 0)
  687. ret = -EIO;
  688. if (req != NULL || ret != 0)
  689. break;
  690. if (file->f_flags & O_NONBLOCK) {
  691. ret = -EAGAIN;
  692. break;
  693. }
  694. if (signal_pending(current)) {
  695. ret = -ERESTARTSYS;
  696. break;
  697. }
  698. spin_unlock_irqrestore(&state->lock, flags);
  699. schedule();
  700. spin_lock_irqsave(&state->lock, flags);
  701. }
  702. current->state = TASK_RUNNING;
  703. remove_wait_queue(&state->wait_queue, &wait);
  704. spin_unlock_irqrestore(&state->lock, flags);
  705. if (ret)
  706. return ret;
  707. ret = req->reply_len;
  708. if (ret > count)
  709. ret = count;
  710. if (ret > 0 && copy_to_user(buf, req->reply, ret))
  711. ret = -EFAULT;
  712. kfree(req);
  713. return ret;
  714. }
  715. static ssize_t adb_write(struct file *file, const char __user *buf,
  716. size_t count, loff_t *ppos)
  717. {
  718. int ret/*, i*/;
  719. struct adbdev_state *state = file->private_data;
  720. struct adb_request *req;
  721. if (count < 2 || count > sizeof(req->data))
  722. return -EINVAL;
  723. if (adb_controller == NULL)
  724. return -ENXIO;
  725. if (!access_ok(VERIFY_READ, buf, count))
  726. return -EFAULT;
  727. req = (struct adb_request *) kmalloc(sizeof(struct adb_request),
  728. GFP_KERNEL);
  729. if (req == NULL)
  730. return -ENOMEM;
  731. req->nbytes = count;
  732. req->done = adb_write_done;
  733. req->arg = (void *) state;
  734. req->complete = 0;
  735. ret = -EFAULT;
  736. if (copy_from_user(req->data, buf, count))
  737. goto out;
  738. atomic_inc(&state->n_pending);
  739. /* If a probe is in progress or we are sleeping, wait for it to complete */
  740. down(&adb_probe_mutex);
  741. /* Queries are special requests sent to the ADB driver itself */
  742. if (req->data[0] == ADB_QUERY) {
  743. if (count > 1)
  744. ret = do_adb_query(req);
  745. else
  746. ret = -EINVAL;
  747. up(&adb_probe_mutex);
  748. }
  749. /* Special case for ADB_BUSRESET request, all others are sent to
  750. the controller */
  751. else if ((req->data[0] == ADB_PACKET)&&(count > 1)
  752. &&(req->data[1] == ADB_BUSRESET)) {
  753. ret = do_adb_reset_bus();
  754. up(&adb_probe_mutex);
  755. atomic_dec(&state->n_pending);
  756. if (ret == 0)
  757. ret = count;
  758. goto out;
  759. } else {
  760. req->reply_expected = ((req->data[1] & 0xc) == 0xc);
  761. if (adb_controller && adb_controller->send_request)
  762. ret = adb_controller->send_request(req, 0);
  763. else
  764. ret = -ENXIO;
  765. up(&adb_probe_mutex);
  766. }
  767. if (ret != 0) {
  768. atomic_dec(&state->n_pending);
  769. goto out;
  770. }
  771. return count;
  772. out:
  773. kfree(req);
  774. return ret;
  775. }
  776. static struct file_operations adb_fops = {
  777. .owner = THIS_MODULE,
  778. .llseek = no_llseek,
  779. .read = adb_read,
  780. .write = adb_write,
  781. .open = adb_open,
  782. .release = adb_release,
  783. };
  784. static void
  785. adbdev_init(void)
  786. {
  787. if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
  788. printk(KERN_ERR "adb: unable to get major %d\n", ADB_MAJOR);
  789. return;
  790. }
  791. devfs_mk_cdev(MKDEV(ADB_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR, "adb");
  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. }