Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * keyspan_remote: USB driver for the Keyspan DMR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This driver has been put together with the support of Innosys, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * and Keyspan, Inc the manufacturers of the Keyspan USB DMR product.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/usb/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* Parameters that can be passed to the driver. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static int debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) module_param(debug, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Vendor and product ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define USB_KEYSPAN_VENDOR_ID		0x06CD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define USB_KEYSPAN_PRODUCT_UIA11	0x0202
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) /* Defines for converting the data from the remote. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define ZERO		0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define ZERO_MASK	0x1F	/* 5 bits for a 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define ONE		0x3C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define ONE_MASK	0x3F	/* 6 bits for a 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define SYNC		0x3F80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define SYNC_MASK	0x3FFF	/* 14 bits for a SYNC sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define STOP		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define STOP_MASK	0x1F	/* 5 bits for the STOP sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define GAP		0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define RECV_SIZE	8	/* The UIA-11 type have a 8 byte limit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * Table that maps the 31 possible keycodes to input keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Currently there are 15 and 17 button models so RESERVED codes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * are blank areas in the mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static const unsigned short keyspan_key_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	KEY_RESERVED,		/* 0 is just a place holder. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	KEY_STOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	KEY_PLAYCD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	KEY_PREVIOUSSONG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	KEY_REWIND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	KEY_FORWARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	KEY_NEXTSONG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	KEY_PAUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	KEY_VOLUMEUP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	KEY_VOLUMEDOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	KEY_UP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	KEY_MUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	KEY_LEFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	KEY_ENTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	KEY_RIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	KEY_DOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	KEY_KPASTERISK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	KEY_RESERVED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	KEY_MENU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /* table of devices that work with this driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static const struct usb_device_id keyspan_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	{ USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	{ }					/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) /* Structure to store all the real stuff that a remote sends to us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) struct keyspan_message {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u16	system;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	u8	button;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u8	toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) /* Structure used for all the bit testing magic needed to be done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct bit_tester {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u32	tester;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int	len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int	pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int	bits_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u8	buffer[32];
^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) /* Structure to hold all of our driver specific stuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct usb_keyspan {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	char				name[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	char				phys[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	unsigned short			keymap[ARRAY_SIZE(keyspan_key_table)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct usb_device		*udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct input_dev		*input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct usb_interface		*interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct usb_endpoint_descriptor	*in_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct urb*			irq_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int				open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	dma_addr_t			in_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	unsigned char			*in_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* variables used to parse messages from remote. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct bit_tester		data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int				stage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int				toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct usb_driver keyspan_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * Debug routine that prints out what we've received from the remote.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	char codes[4 * RECV_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	for (i = 0; i < RECV_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	dev_info(&dev->udev->dev, "%s\n", codes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * Routine that manages the bit_tester structure.  It makes sure that there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * at least bits_needed bits loaded into the tester.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (dev->data.bits_left >= bits_needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 * Somehow we've missed the last message. The message will be repeated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	 * though so it's not too big a deal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (dev->data.pos >= dev->data.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		dev_dbg(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			"%s - Error ran out of data. pos: %d, len: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			__func__, dev->data.pos, dev->data.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	/* Load as much as we can into the tester. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	       (dev->data.pos < dev->data.len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		dev->data.bits_left += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct input_dev *input = remote->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	input_event(input, EV_MSC, MSC_SCAN, button);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	input_report_key(input, remote->keymap[button], press);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * Routine that handles all the logic needed to parse out the message from the remote.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static void keyspan_check_data(struct usb_keyspan *remote)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	struct keyspan_message message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	switch(remote->stage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		 * In stage 0 we want to find the start of a message.  The remote sends a 0xFF as filler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		 * So the first byte that isn't a FF should be the start of a new message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		if (i < RECV_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			remote->data.len = RECV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			remote->data.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			remote->data.tester = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			remote->data.bits_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			remote->stage = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		 * Stage 1 we should have 16 bytes and should be able to detect a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		 * SYNC.  The SYNC is 14 bits, 7 0's and then 7 1's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		remote->data.len += RECV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			for (i = 0; i < 8; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				if (keyspan_load_tester(remote, 14) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 					remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 					return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				if ((remote->data.tester & SYNC_MASK) == SYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 					remote->data.tester = remote->data.tester >> 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 					remote->data.bits_left -= 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 					found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 					remote->data.tester = remote->data.tester >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 					--remote->data.bits_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			remote->data.len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			remote->stage = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		 * Stage 2 we should have 24 bytes which will be enough for a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		 * message.  We need to parse out the system code, button code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		 * toggle code, and stop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		remote->data.len += RECV_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		message.system = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		for (i = 0; i < 9; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			keyspan_load_tester(remote, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			if ((remote->data.tester & ZERO_MASK) == ZERO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				message.system = message.system << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				remote->data.tester = remote->data.tester >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				remote->data.bits_left -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			} else if ((remote->data.tester & ONE_MASK) == ONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				message.system = (message.system << 1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				remote->data.tester = remote->data.tester >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				remote->data.bits_left -= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				dev_err(&remote->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 					"%s - Unknown sequence found in system data.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 					__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		message.button = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			keyspan_load_tester(remote, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			if ((remote->data.tester & ZERO_MASK) == ZERO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				message.button = message.button << 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 				remote->data.tester = remote->data.tester >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				remote->data.bits_left -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			} else if ((remote->data.tester & ONE_MASK) == ONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				message.button = (message.button << 1) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				remote->data.tester = remote->data.tester >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				remote->data.bits_left -= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				dev_err(&remote->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					"%s - Unknown sequence found in button data.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 					__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				return;
^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) 		keyspan_load_tester(remote, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		if ((remote->data.tester & ZERO_MASK) == ZERO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			message.toggle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			remote->data.tester = remote->data.tester >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			remote->data.bits_left -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		} else if ((remote->data.tester & ONE_MASK) == ONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			message.toggle = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			remote->data.tester = remote->data.tester >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			remote->data.bits_left -= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			dev_err(&remote->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 				"%s - Error in message, invalid toggle.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		keyspan_load_tester(remote, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if ((remote->data.tester & STOP_MASK) == STOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			remote->data.tester = remote->data.tester >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			remote->data.bits_left -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			dev_err(&remote->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				"Bad message received, no stop bit found.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		dev_dbg(&remote->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			"%s found valid message: system: %d, button: %d, toggle: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			__func__, message.system, message.button, message.toggle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (message.toggle != remote->toggle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			keyspan_report_button(remote, message.button, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			keyspan_report_button(remote, message.button, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			remote->toggle = message.toggle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		remote->stage = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * Routine for sending all the initialization messages to the remote.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int keyspan_setup(struct usb_device* dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				 0x11, 0x40, 0x5601, 0x0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				 USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				 0x44, 0x40, 0x0, 0x0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				 USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		return(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 				 0x22, 0x40, 0x0, 0x0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				 USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	return(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * Routine used to handle a new message that has come in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static void keyspan_irq_recv(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	struct usb_keyspan *dev = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	/* Check our status in case we need to bail out early. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	/* Device went away so don't keep trying to read from it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		keyspan_print(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	keyspan_check_data(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		dev_err(&dev->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			"%s - usb_submit_urb failed with result: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static int keyspan_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct usb_keyspan *remote = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	remote->irq_urb->dev = remote->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static void keyspan_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	struct usb_keyspan *remote = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	usb_kill_urb(remote->irq_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	struct usb_endpoint_descriptor *endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		endpoint = &iface->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		if (usb_endpoint_is_int_in(endpoint)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			/* we found our interrupt in endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			return endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * Routine that sets up the driver to handle a specific USB device detected on the bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	struct usb_device *udev = interface_to_usbdev(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	struct usb_endpoint_descriptor *endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	struct usb_keyspan *remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	int i, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	if (!endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	remote = kzalloc(sizeof(*remote), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (!remote || !input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	remote->udev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	remote->input = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	remote->interface = interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	remote->in_endpoint = endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	remote->toggle = -1;	/* Set to -1 so we will always not match the toggle from the first remote message. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_KERNEL, &remote->in_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (!remote->in_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (!remote->irq_urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	error = keyspan_setup(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		error = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (udev->manufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (udev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		if (udev->manufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			strlcat(remote->name, " ", sizeof(remote->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		strlcat(remote->name, udev->product, sizeof(remote->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	if (!strlen(remote->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		snprintf(remote->name, sizeof(remote->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			 "USB Keyspan Remote %04x:%04x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			 le16_to_cpu(udev->descriptor.idVendor),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			 le16_to_cpu(udev->descriptor.idProduct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	usb_make_path(udev, remote->phys, sizeof(remote->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	strlcat(remote->phys, "/input0", sizeof(remote->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	input_dev->name = remote->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	input_dev->phys = remote->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	usb_to_input_id(udev, &input_dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	input_dev->dev.parent = &interface->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	input_dev->keycode = remote->keymap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	input_dev->keycodesize = sizeof(unsigned short);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	__set_bit(EV_KEY, input_dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		__set_bit(keyspan_key_table[i], input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	__clear_bit(KEY_RESERVED, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	input_set_drvdata(input_dev, remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	input_dev->open = keyspan_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	input_dev->close = keyspan_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	 * Initialize the URB to access the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 * The urb gets sent to the device in keyspan_open()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	usb_fill_int_urb(remote->irq_urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			 remote->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			 usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			 endpoint->bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	remote->irq_urb->transfer_dma = remote->in_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	/* we can register the device now, as it is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	error = input_register_device(remote->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		goto fail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	/* save our data pointer in this interface device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	usb_set_intfdata(interface, remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  fail3:	usb_free_urb(remote->irq_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  fail2:	usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)  fail1:	kfree(remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)  * Routine called when a device is disconnected from the USB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) static void keyspan_disconnect(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	struct usb_keyspan *remote;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	remote = usb_get_intfdata(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	usb_set_intfdata(interface, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	if (remote) {	/* We have a valid driver structure so clean up everything we allocated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		input_unregister_device(remote->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		usb_kill_urb(remote->irq_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		usb_free_urb(remote->irq_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		kfree(remote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	}
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  * Standard driver set up sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static struct usb_driver keyspan_driver =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	.name =		"keyspan_remote",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	.probe =	keyspan_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	.disconnect =	keyspan_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	.id_table =	keyspan_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) module_usb_driver(keyspan_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) MODULE_DEVICE_TABLE(usb, keyspan_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) MODULE_AUTHOR("Michael Downey <downey@zymeta.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) MODULE_DESCRIPTION("Driver for the USB Keyspan remote control.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) MODULE_LICENSE("GPL");