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