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