adb.c 20 KB

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