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