adb.c 20 KB

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