Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Device driver for the Apple Desktop Bus
0004  * and the /dev/adb device on macintoshes.
0005  *
0006  * Copyright (C) 1996 Paul Mackerras.
0007  *
0008  * Modified to declare controllers as structures, added
0009  * client notification of bus reset and handles PowerBook
0010  * sleep, by Benjamin Herrenschmidt.
0011  *
0012  * To do:
0013  *
0014  * - /sys/bus/adb to list the devices and infos
0015  * - more /dev/adb to allow userland to receive the
0016  *   flow of auto-polling datas from a given device.
0017  * - move bus probe to a kernel thread
0018  */
0019 
0020 #include <linux/types.h>
0021 #include <linux/errno.h>
0022 #include <linux/kernel.h>
0023 #include <linux/slab.h>
0024 #include <linux/module.h>
0025 #include <linux/fs.h>
0026 #include <linux/mm.h>
0027 #include <linux/sched/signal.h>
0028 #include <linux/adb.h>
0029 #include <linux/cuda.h>
0030 #include <linux/pmu.h>
0031 #include <linux/notifier.h>
0032 #include <linux/wait.h>
0033 #include <linux/init.h>
0034 #include <linux/delay.h>
0035 #include <linux/spinlock.h>
0036 #include <linux/completion.h>
0037 #include <linux/device.h>
0038 #include <linux/kthread.h>
0039 #include <linux/platform_device.h>
0040 #include <linux/mutex.h>
0041 #include <linux/of.h>
0042 
0043 #include <linux/uaccess.h>
0044 #ifdef CONFIG_PPC
0045 #include <asm/machdep.h>
0046 #endif
0047 
0048 
0049 EXPORT_SYMBOL(adb_client_list);
0050 
0051 extern struct adb_driver via_macii_driver;
0052 extern struct adb_driver via_cuda_driver;
0053 extern struct adb_driver adb_iop_driver;
0054 extern struct adb_driver via_pmu_driver;
0055 extern struct adb_driver macio_adb_driver;
0056 
0057 static DEFINE_MUTEX(adb_mutex);
0058 static struct adb_driver *adb_driver_list[] = {
0059 #ifdef CONFIG_ADB_MACII
0060     &via_macii_driver,
0061 #endif
0062 #ifdef CONFIG_ADB_CUDA
0063     &via_cuda_driver,
0064 #endif
0065 #ifdef CONFIG_ADB_IOP
0066     &adb_iop_driver,
0067 #endif
0068 #ifdef CONFIG_ADB_PMU
0069     &via_pmu_driver,
0070 #endif
0071 #ifdef CONFIG_ADB_MACIO
0072     &macio_adb_driver,
0073 #endif
0074     NULL
0075 };
0076 
0077 static struct class *adb_dev_class;
0078 
0079 static struct adb_driver *adb_controller;
0080 BLOCKING_NOTIFIER_HEAD(adb_client_list);
0081 static int adb_got_sleep;
0082 static int adb_inited;
0083 static DEFINE_SEMAPHORE(adb_probe_mutex);
0084 static int sleepy_trackpad;
0085 static int autopoll_devs;
0086 int __adb_probe_sync;
0087 
0088 static int adb_scan_bus(void);
0089 static int do_adb_reset_bus(void);
0090 static void adbdev_init(void);
0091 static int try_handler_change(int, int);
0092 
0093 static struct adb_handler {
0094     void (*handler)(unsigned char *, int, int);
0095     int original_address;
0096     int handler_id;
0097     int busy;
0098 } adb_handler[16];
0099 
0100 /*
0101  * The adb_handler_mutex mutex protects all accesses to the original_address
0102  * and handler_id fields of adb_handler[i] for all i, and changes to the
0103  * handler field.
0104  * Accesses to the handler field are protected by the adb_handler_lock
0105  * rwlock.  It is held across all calls to any handler, so that by the
0106  * time adb_unregister returns, we know that the old handler isn't being
0107  * called.
0108  */
0109 static DEFINE_MUTEX(adb_handler_mutex);
0110 static DEFINE_RWLOCK(adb_handler_lock);
0111 
0112 #if 0
0113 static void printADBreply(struct adb_request *req)
0114 {
0115         int i;
0116 
0117         printk("adb reply (%d)", req->reply_len);
0118         for(i = 0; i < req->reply_len; i++)
0119                 printk(" %x", req->reply[i]);
0120         printk("\n");
0121 
0122 }
0123 #endif
0124 
0125 static int adb_scan_bus(void)
0126 {
0127     int i, highFree=0, noMovement;
0128     int devmask = 0;
0129     struct adb_request req;
0130     
0131     /* assumes adb_handler[] is all zeroes at this point */
0132     for (i = 1; i < 16; i++) {
0133         /* see if there is anything at address i */
0134         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0135                             (i << 4) | 0xf);
0136         if (req.reply_len > 1)
0137             /* one or more devices at this address */
0138             adb_handler[i].original_address = i;
0139         else if (i > highFree)
0140             highFree = i;
0141     }
0142 
0143     /* Note we reset noMovement to 0 each time we move a device */
0144     for (noMovement = 1; noMovement < 2 && highFree > 0; noMovement++) {
0145         for (i = 1; i < 16; i++) {
0146             if (adb_handler[i].original_address == 0)
0147                 continue;
0148             /*
0149              * Send a "talk register 3" command to address i
0150              * to provoke a collision if there is more than
0151              * one device at this address.
0152              */
0153             adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0154                     (i << 4) | 0xf);
0155             /*
0156              * Move the device(s) which didn't detect a
0157              * collision to address `highFree'.  Hopefully
0158              * this only moves one device.
0159              */
0160             adb_request(&req, NULL, ADBREQ_SYNC, 3,
0161                     (i<< 4) | 0xb, (highFree | 0x60), 0xfe);
0162             /*
0163              * See if anybody actually moved. This is suggested
0164              * by HW TechNote 01:
0165              *
0166              * https://developer.apple.com/technotes/hw/hw_01.html
0167              */
0168             adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0169                     (highFree << 4) | 0xf);
0170             if (req.reply_len <= 1) continue;
0171             /*
0172              * Test whether there are any device(s) left
0173              * at address i.
0174              */
0175             adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0176                     (i << 4) | 0xf);
0177             if (req.reply_len > 1) {
0178                 /*
0179                  * There are still one or more devices
0180                  * left at address i.  Register the one(s)
0181                  * we moved to `highFree', and find a new
0182                  * value for highFree.
0183                  */
0184                 adb_handler[highFree].original_address =
0185                     adb_handler[i].original_address;
0186                 while (highFree > 0 &&
0187                        adb_handler[highFree].original_address)
0188                     highFree--;
0189                 if (highFree <= 0)
0190                     break;
0191 
0192                 noMovement = 0;
0193             } else {
0194                 /*
0195                  * No devices left at address i; move the
0196                  * one(s) we moved to `highFree' back to i.
0197                  */
0198                 adb_request(&req, NULL, ADBREQ_SYNC, 3,
0199                         (highFree << 4) | 0xb,
0200                         (i | 0x60), 0xfe);
0201             }
0202         }   
0203     }
0204 
0205     /* Now fill in the handler_id field of the adb_handler entries. */
0206     for (i = 1; i < 16; i++) {
0207         if (adb_handler[i].original_address == 0)
0208             continue;
0209         adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0210                 (i << 4) | 0xf);
0211         adb_handler[i].handler_id = req.reply[2];
0212         printk(KERN_DEBUG "adb device [%d]: %d 0x%X\n", i,
0213                adb_handler[i].original_address,
0214                adb_handler[i].handler_id);
0215         devmask |= 1 << i;
0216     }
0217     return devmask;
0218 }
0219 
0220 /*
0221  * This kernel task handles ADB probing. It dies once probing is
0222  * completed.
0223  */
0224 static int
0225 adb_probe_task(void *x)
0226 {
0227     pr_debug("adb: starting probe task...\n");
0228     do_adb_reset_bus();
0229     pr_debug("adb: finished probe task...\n");
0230 
0231     up(&adb_probe_mutex);
0232 
0233     return 0;
0234 }
0235 
0236 static void
0237 __adb_probe_task(struct work_struct *bullshit)
0238 {
0239     kthread_run(adb_probe_task, NULL, "kadbprobe");
0240 }
0241 
0242 static DECLARE_WORK(adb_reset_work, __adb_probe_task);
0243 
0244 int
0245 adb_reset_bus(void)
0246 {
0247     if (__adb_probe_sync) {
0248         do_adb_reset_bus();
0249         return 0;
0250     }
0251 
0252     down(&adb_probe_mutex);
0253     schedule_work(&adb_reset_work);
0254     return 0;
0255 }
0256 
0257 #ifdef CONFIG_PM
0258 /*
0259  * notify clients before sleep
0260  */
0261 static int __adb_suspend(struct platform_device *dev, pm_message_t state)
0262 {
0263     adb_got_sleep = 1;
0264     /* We need to get a lock on the probe thread */
0265     down(&adb_probe_mutex);
0266     /* Stop autopoll */
0267     if (adb_controller->autopoll)
0268         adb_controller->autopoll(0);
0269     blocking_notifier_call_chain(&adb_client_list, ADB_MSG_POWERDOWN, NULL);
0270 
0271     return 0;
0272 }
0273 
0274 static int adb_suspend(struct device *dev)
0275 {
0276     return __adb_suspend(to_platform_device(dev), PMSG_SUSPEND);
0277 }
0278 
0279 static int adb_freeze(struct device *dev)
0280 {
0281     return __adb_suspend(to_platform_device(dev), PMSG_FREEZE);
0282 }
0283 
0284 static int adb_poweroff(struct device *dev)
0285 {
0286     return __adb_suspend(to_platform_device(dev), PMSG_HIBERNATE);
0287 }
0288 
0289 /*
0290  * reset bus after sleep
0291  */
0292 static int __adb_resume(struct platform_device *dev)
0293 {
0294     adb_got_sleep = 0;
0295     up(&adb_probe_mutex);
0296     adb_reset_bus();
0297 
0298     return 0;
0299 }
0300 
0301 static int adb_resume(struct device *dev)
0302 {
0303     return __adb_resume(to_platform_device(dev));
0304 }
0305 #endif /* CONFIG_PM */
0306 
0307 static int __init adb_init(void)
0308 {
0309     struct adb_driver *driver;
0310     int i;
0311 
0312 #ifdef CONFIG_PPC32
0313     if (!machine_is(chrp) && !machine_is(powermac))
0314         return 0;
0315 #endif
0316 #ifdef CONFIG_MAC
0317     if (!MACH_IS_MAC)
0318         return 0;
0319 #endif
0320 
0321     /* xmon may do early-init */
0322     if (adb_inited)
0323         return 0;
0324     adb_inited = 1;
0325         
0326     adb_controller = NULL;
0327 
0328     i = 0;
0329     while ((driver = adb_driver_list[i++]) != NULL) {
0330         if (!driver->probe()) {
0331             adb_controller = driver;
0332             break;
0333         }
0334     }
0335     if (adb_controller != NULL && adb_controller->init &&
0336         adb_controller->init())
0337         adb_controller = NULL;
0338     if (adb_controller == NULL) {
0339         pr_warn("Warning: no ADB interface detected\n");
0340     } else {
0341 #ifdef CONFIG_PPC
0342         if (of_machine_is_compatible("AAPL,PowerBook1998") ||
0343             of_machine_is_compatible("PowerBook1,1"))
0344             sleepy_trackpad = 1;
0345 #endif /* CONFIG_PPC */
0346 
0347         adbdev_init();
0348         adb_reset_bus();
0349     }
0350     return 0;
0351 }
0352 
0353 device_initcall(adb_init);
0354 
0355 static int
0356 do_adb_reset_bus(void)
0357 {
0358     int ret;
0359     
0360     if (adb_controller == NULL)
0361         return -ENXIO;
0362         
0363     if (adb_controller->autopoll)
0364         adb_controller->autopoll(0);
0365 
0366     blocking_notifier_call_chain(&adb_client_list,
0367         ADB_MSG_PRE_RESET, NULL);
0368 
0369     if (sleepy_trackpad) {
0370         /* Let the trackpad settle down */
0371         msleep(500);
0372     }
0373 
0374     mutex_lock(&adb_handler_mutex);
0375     write_lock_irq(&adb_handler_lock);
0376     memset(adb_handler, 0, sizeof(adb_handler));
0377     write_unlock_irq(&adb_handler_lock);
0378 
0379     /* That one is still a bit synchronous, oh well... */
0380     if (adb_controller->reset_bus)
0381         ret = adb_controller->reset_bus();
0382     else
0383         ret = 0;
0384 
0385     if (sleepy_trackpad) {
0386         /* Let the trackpad settle down */
0387         msleep(1500);
0388     }
0389 
0390     if (!ret) {
0391         autopoll_devs = adb_scan_bus();
0392         if (adb_controller->autopoll)
0393             adb_controller->autopoll(autopoll_devs);
0394     }
0395     mutex_unlock(&adb_handler_mutex);
0396 
0397     blocking_notifier_call_chain(&adb_client_list,
0398         ADB_MSG_POST_RESET, NULL);
0399     
0400     return ret;
0401 }
0402 
0403 void
0404 adb_poll(void)
0405 {
0406     if ((adb_controller == NULL)||(adb_controller->poll == NULL))
0407         return;
0408     adb_controller->poll();
0409 }
0410 EXPORT_SYMBOL(adb_poll);
0411 
0412 static void adb_sync_req_done(struct adb_request *req)
0413 {
0414     struct completion *comp = req->arg;
0415 
0416     complete(comp);
0417 }
0418 
0419 int
0420 adb_request(struct adb_request *req, void (*done)(struct adb_request *),
0421         int flags, int nbytes, ...)
0422 {
0423     va_list list;
0424     int i;
0425     int rc;
0426     struct completion comp;
0427 
0428     if ((adb_controller == NULL) || (adb_controller->send_request == NULL))
0429         return -ENXIO;
0430     if (nbytes < 1)
0431         return -EINVAL;
0432 
0433     req->nbytes = nbytes+1;
0434     req->done = done;
0435     req->reply_expected = flags & ADBREQ_REPLY;
0436     req->data[0] = ADB_PACKET;
0437     va_start(list, nbytes);
0438     for (i = 0; i < nbytes; ++i)
0439         req->data[i+1] = va_arg(list, int);
0440     va_end(list);
0441 
0442     if (flags & ADBREQ_NOSEND)
0443         return 0;
0444 
0445     /* Synchronous requests block using an on-stack completion */
0446     if (flags & ADBREQ_SYNC) {
0447         WARN_ON(done);
0448         req->done = adb_sync_req_done;
0449         req->arg = &comp;
0450         init_completion(&comp);
0451     }
0452 
0453     rc = adb_controller->send_request(req, 0);
0454 
0455     if ((flags & ADBREQ_SYNC) && !rc && !req->complete)
0456         wait_for_completion(&comp);
0457 
0458     return rc;
0459 }
0460 EXPORT_SYMBOL(adb_request);
0461 
0462  /* Ultimately this should return the number of devices with
0463     the given default id.
0464     And it does it now ! Note: changed behaviour: This function
0465     will now register if default_id _and_ handler_id both match
0466     but handler_id can be left to 0 to match with default_id only.
0467     When handler_id is set, this function will try to adjust
0468     the handler_id id it doesn't match. */
0469 int
0470 adb_register(int default_id, int handler_id, struct adb_ids *ids,
0471          void (*handler)(unsigned char *, int, int))
0472 {
0473     int i;
0474 
0475     mutex_lock(&adb_handler_mutex);
0476     ids->nids = 0;
0477     for (i = 1; i < 16; i++) {
0478         if ((adb_handler[i].original_address == default_id) &&
0479             (!handler_id || (handler_id == adb_handler[i].handler_id) || 
0480             try_handler_change(i, handler_id))) {
0481             if (adb_handler[i].handler != 0) {
0482                 pr_err("Two handlers for ADB device %d\n",
0483                        default_id);
0484                 continue;
0485             }
0486             write_lock_irq(&adb_handler_lock);
0487             adb_handler[i].handler = handler;
0488             write_unlock_irq(&adb_handler_lock);
0489             ids->id[ids->nids++] = i;
0490         }
0491     }
0492     mutex_unlock(&adb_handler_mutex);
0493     return ids->nids;
0494 }
0495 EXPORT_SYMBOL(adb_register);
0496 
0497 int
0498 adb_unregister(int index)
0499 {
0500     int ret = -ENODEV;
0501 
0502     mutex_lock(&adb_handler_mutex);
0503     write_lock_irq(&adb_handler_lock);
0504     if (adb_handler[index].handler) {
0505         while(adb_handler[index].busy) {
0506             write_unlock_irq(&adb_handler_lock);
0507             yield();
0508             write_lock_irq(&adb_handler_lock);
0509         }
0510         ret = 0;
0511         adb_handler[index].handler = NULL;
0512     }
0513     write_unlock_irq(&adb_handler_lock);
0514     mutex_unlock(&adb_handler_mutex);
0515     return ret;
0516 }
0517 EXPORT_SYMBOL(adb_unregister);
0518 
0519 void
0520 adb_input(unsigned char *buf, int nb, int autopoll)
0521 {
0522     int i, id;
0523     static int dump_adb_input;
0524     unsigned long flags;
0525     
0526     void (*handler)(unsigned char *, int, int);
0527 
0528     /* We skip keystrokes and mouse moves when the sleep process
0529      * has been started. We stop autopoll, but this is another security
0530      */
0531     if (adb_got_sleep)
0532         return;
0533         
0534     id = buf[0] >> 4;
0535     if (dump_adb_input) {
0536         pr_info("adb packet: ");
0537         for (i = 0; i < nb; ++i)
0538             pr_cont(" %x", buf[i]);
0539         pr_cont(", id = %d\n", id);
0540     }
0541     write_lock_irqsave(&adb_handler_lock, flags);
0542     handler = adb_handler[id].handler;
0543     if (handler != NULL)
0544         adb_handler[id].busy = 1;
0545     write_unlock_irqrestore(&adb_handler_lock, flags);
0546     if (handler != NULL) {
0547         (*handler)(buf, nb, autopoll);
0548         wmb();
0549         adb_handler[id].busy = 0;
0550     }
0551         
0552 }
0553 
0554 /* Try to change handler to new_id. Will return 1 if successful. */
0555 static int try_handler_change(int address, int new_id)
0556 {
0557     struct adb_request req;
0558 
0559     if (adb_handler[address].handler_id == new_id)
0560         return 1;
0561     adb_request(&req, NULL, ADBREQ_SYNC, 3,
0562         ADB_WRITEREG(address, 3), address | 0x20, new_id);
0563     adb_request(&req, NULL, ADBREQ_SYNC | ADBREQ_REPLY, 1,
0564         ADB_READREG(address, 3));
0565     if (req.reply_len < 2)
0566         return 0;
0567     if (req.reply[2] != new_id)
0568         return 0;
0569     adb_handler[address].handler_id = req.reply[2];
0570 
0571     return 1;
0572 }
0573 
0574 int
0575 adb_try_handler_change(int address, int new_id)
0576 {
0577     int ret;
0578 
0579     mutex_lock(&adb_handler_mutex);
0580     ret = try_handler_change(address, new_id);
0581     mutex_unlock(&adb_handler_mutex);
0582     if (ret)
0583         pr_debug("adb handler change: [%d] 0x%X\n", address, new_id);
0584     return ret;
0585 }
0586 EXPORT_SYMBOL(adb_try_handler_change);
0587 
0588 int
0589 adb_get_infos(int address, int *original_address, int *handler_id)
0590 {
0591     mutex_lock(&adb_handler_mutex);
0592     *original_address = adb_handler[address].original_address;
0593     *handler_id = adb_handler[address].handler_id;
0594     mutex_unlock(&adb_handler_mutex);
0595 
0596     return (*original_address != 0);
0597 }
0598 
0599 
0600 /*
0601  * /dev/adb device driver.
0602  */
0603 
0604 #define ADB_MAJOR   56  /* major number for /dev/adb */
0605 
0606 struct adbdev_state {
0607     spinlock_t  lock;
0608     atomic_t    n_pending;
0609     struct adb_request *completed;
0610     wait_queue_head_t wait_queue;
0611     int     inuse;
0612 };
0613 
0614 static void adb_write_done(struct adb_request *req)
0615 {
0616     struct adbdev_state *state = (struct adbdev_state *) req->arg;
0617     unsigned long flags;
0618 
0619     if (!req->complete) {
0620         req->reply_len = 0;
0621         req->complete = 1;
0622     }
0623     spin_lock_irqsave(&state->lock, flags);
0624     atomic_dec(&state->n_pending);
0625     if (!state->inuse) {
0626         kfree(req);
0627         if (atomic_read(&state->n_pending) == 0) {
0628             spin_unlock_irqrestore(&state->lock, flags);
0629             kfree(state);
0630             return;
0631         }
0632     } else {
0633         struct adb_request **ap = &state->completed;
0634         while (*ap != NULL)
0635             ap = &(*ap)->next;
0636         req->next = NULL;
0637         *ap = req;
0638         wake_up_interruptible(&state->wait_queue);
0639     }
0640     spin_unlock_irqrestore(&state->lock, flags);
0641 }
0642 
0643 static int
0644 do_adb_query(struct adb_request *req)
0645 {
0646     int ret = -EINVAL;
0647 
0648     switch(req->data[1]) {
0649     case ADB_QUERY_GETDEVINFO:
0650         if (req->nbytes < 3 || req->data[2] >= 16)
0651             break;
0652         mutex_lock(&adb_handler_mutex);
0653         req->reply[0] = adb_handler[req->data[2]].original_address;
0654         req->reply[1] = adb_handler[req->data[2]].handler_id;
0655         mutex_unlock(&adb_handler_mutex);
0656         req->complete = 1;
0657         req->reply_len = 2;
0658         adb_write_done(req);
0659         ret = 0;
0660         break;
0661     }
0662     return ret;
0663 }
0664 
0665 static int adb_open(struct inode *inode, struct file *file)
0666 {
0667     struct adbdev_state *state;
0668     int ret = 0;
0669 
0670     mutex_lock(&adb_mutex);
0671     if (iminor(inode) > 0 || adb_controller == NULL) {
0672         ret = -ENXIO;
0673         goto out;
0674     }
0675     state = kmalloc(sizeof(struct adbdev_state), GFP_KERNEL);
0676     if (state == 0) {
0677         ret = -ENOMEM;
0678         goto out;
0679     }
0680     file->private_data = state;
0681     spin_lock_init(&state->lock);
0682     atomic_set(&state->n_pending, 0);
0683     state->completed = NULL;
0684     init_waitqueue_head(&state->wait_queue);
0685     state->inuse = 1;
0686 
0687 out:
0688     mutex_unlock(&adb_mutex);
0689     return ret;
0690 }
0691 
0692 static int adb_release(struct inode *inode, struct file *file)
0693 {
0694     struct adbdev_state *state = file->private_data;
0695     unsigned long flags;
0696 
0697     mutex_lock(&adb_mutex);
0698     if (state) {
0699         file->private_data = NULL;
0700         spin_lock_irqsave(&state->lock, flags);
0701         if (atomic_read(&state->n_pending) == 0
0702             && state->completed == NULL) {
0703             spin_unlock_irqrestore(&state->lock, flags);
0704             kfree(state);
0705         } else {
0706             state->inuse = 0;
0707             spin_unlock_irqrestore(&state->lock, flags);
0708         }
0709     }
0710     mutex_unlock(&adb_mutex);
0711     return 0;
0712 }
0713 
0714 static ssize_t adb_read(struct file *file, char __user *buf,
0715             size_t count, loff_t *ppos)
0716 {
0717     int ret = 0;
0718     struct adbdev_state *state = file->private_data;
0719     struct adb_request *req;
0720     DECLARE_WAITQUEUE(wait, current);
0721     unsigned long flags;
0722 
0723     if (count < 2)
0724         return -EINVAL;
0725     if (count > sizeof(req->reply))
0726         count = sizeof(req->reply);
0727 
0728     req = NULL;
0729     spin_lock_irqsave(&state->lock, flags);
0730     add_wait_queue(&state->wait_queue, &wait);
0731     set_current_state(TASK_INTERRUPTIBLE);
0732 
0733     for (;;) {
0734         req = state->completed;
0735         if (req != NULL)
0736             state->completed = req->next;
0737         else if (atomic_read(&state->n_pending) == 0)
0738             ret = -EIO;
0739         if (req != NULL || ret != 0)
0740             break;
0741         
0742         if (file->f_flags & O_NONBLOCK) {
0743             ret = -EAGAIN;
0744             break;
0745         }
0746         if (signal_pending(current)) {
0747             ret = -ERESTARTSYS;
0748             break;
0749         }
0750         spin_unlock_irqrestore(&state->lock, flags);
0751         schedule();
0752         spin_lock_irqsave(&state->lock, flags);
0753     }
0754 
0755     set_current_state(TASK_RUNNING);
0756     remove_wait_queue(&state->wait_queue, &wait);
0757     spin_unlock_irqrestore(&state->lock, flags);
0758     
0759     if (ret)
0760         return ret;
0761 
0762     ret = req->reply_len;
0763     if (ret > count)
0764         ret = count;
0765     if (ret > 0 && copy_to_user(buf, req->reply, ret))
0766         ret = -EFAULT;
0767 
0768     kfree(req);
0769     return ret;
0770 }
0771 
0772 static ssize_t adb_write(struct file *file, const char __user *buf,
0773              size_t count, loff_t *ppos)
0774 {
0775     int ret/*, i*/;
0776     struct adbdev_state *state = file->private_data;
0777     struct adb_request *req;
0778 
0779     if (count < 2 || count > sizeof(req->data))
0780         return -EINVAL;
0781     if (adb_controller == NULL)
0782         return -ENXIO;
0783 
0784     req = kmalloc(sizeof(struct adb_request),
0785                          GFP_KERNEL);
0786     if (req == NULL)
0787         return -ENOMEM;
0788 
0789     req->nbytes = count;
0790     req->done = adb_write_done;
0791     req->arg = (void *) state;
0792     req->complete = 0;
0793     
0794     ret = -EFAULT;
0795     if (copy_from_user(req->data, buf, count))
0796         goto out;
0797 
0798     atomic_inc(&state->n_pending);
0799 
0800     /* If a probe is in progress or we are sleeping, wait for it to complete */
0801     down(&adb_probe_mutex);
0802 
0803     /* Queries are special requests sent to the ADB driver itself */
0804     if (req->data[0] == ADB_QUERY) {
0805         if (count > 1)
0806             ret = do_adb_query(req);
0807         else
0808             ret = -EINVAL;
0809         up(&adb_probe_mutex);
0810     }
0811     /* Special case for ADB_BUSRESET request, all others are sent to
0812        the controller */
0813     else if ((req->data[0] == ADB_PACKET) && (count > 1)
0814         && (req->data[1] == ADB_BUSRESET)) {
0815         ret = do_adb_reset_bus();
0816         up(&adb_probe_mutex);
0817         atomic_dec(&state->n_pending);
0818         if (ret == 0)
0819             ret = count;
0820         goto out;
0821     } else {    
0822         req->reply_expected = ((req->data[1] & 0xc) == 0xc);
0823         if (adb_controller && adb_controller->send_request)
0824             ret = adb_controller->send_request(req, 0);
0825         else
0826             ret = -ENXIO;
0827         up(&adb_probe_mutex);
0828     }
0829 
0830     if (ret != 0) {
0831         atomic_dec(&state->n_pending);
0832         goto out;
0833     }
0834     return count;
0835 
0836 out:
0837     kfree(req);
0838     return ret;
0839 }
0840 
0841 static const struct file_operations adb_fops = {
0842     .owner      = THIS_MODULE,
0843     .llseek     = no_llseek,
0844     .read       = adb_read,
0845     .write      = adb_write,
0846     .open       = adb_open,
0847     .release    = adb_release,
0848 };
0849 
0850 #ifdef CONFIG_PM
0851 static const struct dev_pm_ops adb_dev_pm_ops = {
0852     .suspend = adb_suspend,
0853     .resume = adb_resume,
0854     /* Hibernate hooks */
0855     .freeze = adb_freeze,
0856     .thaw = adb_resume,
0857     .poweroff = adb_poweroff,
0858     .restore = adb_resume,
0859 };
0860 #endif
0861 
0862 static struct platform_driver adb_pfdrv = {
0863     .driver = {
0864         .name = "adb",
0865 #ifdef CONFIG_PM
0866         .pm = &adb_dev_pm_ops,
0867 #endif
0868     },
0869 };
0870 
0871 static struct platform_device adb_pfdev = {
0872     .name = "adb",
0873 };
0874 
0875 static int __init
0876 adb_dummy_probe(struct platform_device *dev)
0877 {
0878     if (dev == &adb_pfdev)
0879         return 0;
0880     return -ENODEV;
0881 }
0882 
0883 static void __init
0884 adbdev_init(void)
0885 {
0886     if (register_chrdev(ADB_MAJOR, "adb", &adb_fops)) {
0887         pr_err("adb: unable to get major %d\n", ADB_MAJOR);
0888         return;
0889     }
0890 
0891     adb_dev_class = class_create(THIS_MODULE, "adb");
0892     if (IS_ERR(adb_dev_class))
0893         return;
0894     device_create(adb_dev_class, NULL, MKDEV(ADB_MAJOR, 0), NULL, "adb");
0895 
0896     platform_device_register(&adb_pfdev);
0897     platform_driver_probe(&adb_pfdrv, adb_dummy_probe);
0898 }