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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * TechnoTrend USB IR Receiver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012 Sean Young <sean@mess.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/usb/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <media/rc-core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define DRIVER_NAME	"ttusbir"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define DRIVER_DESC	"TechnoTrend USB IR Receiver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * The Windows driver uses 8 URBS, the original lirc drivers has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * configurable amount (2 default, 4 max). This device generates about 125
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * messages per second (!), whether IR is idle or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define NUM_URBS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define US_PER_BYTE	62
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define US_PER_BIT	(US_PER_BYTE / 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct ttusbir {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct rc_dev *rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	struct urb *urb[NUM_URBS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct led_classdev led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct urb *bulk_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	uint8_t bulk_buffer[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int bulk_out_endp, iso_in_endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool led_on, is_led_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	atomic_t led_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	char phys[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static enum led_brightness ttusbir_brightness_get(struct led_classdev *led_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return tt->led_on ? LED_FULL : LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void ttusbir_set_led(struct ttusbir *tt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (tt->led_on != tt->is_led_on && tt->udev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 				atomic_add_unless(&tt->led_complete, 1, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		tt->bulk_buffer[4] = tt->is_led_on = tt->led_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		ret = usb_submit_urb(tt->bulk_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			dev_warn(tt->dev, "failed to submit bulk urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 									ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			atomic_dec(&tt->led_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void ttusbir_brightness_set(struct led_classdev *led_dev, enum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 						led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	tt->led_on = brightness != LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	ttusbir_set_led(tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^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)  * The urb cannot be reused until the urb completes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static void ttusbir_bulk_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct ttusbir *tt = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	atomic_dec(&tt->led_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		usb_unlink_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		break;
^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) 	ttusbir_set_led(tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * The data is one bit per sample, a set bit signifying silence and samples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * being MSB first. Bit 0 can contain garbage so take it to be whatever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * bit 1 is, so we don't have unexpected edges.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void ttusbir_process_ir_data(struct ttusbir *tt, uint8_t *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct ir_raw_event rawir = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	unsigned i, v, b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	bool event = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	for (i = 0; i < 128; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		v = buf[i] & 0xfe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		switch (v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		case 0xfe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			rawir.pulse = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			rawir.duration = US_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 				event = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			rawir.pulse = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			rawir.duration = US_PER_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				event = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			/* one edge per byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			if (v & 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				b = ffz(v | 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				rawir.pulse = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 				b = ffs(v) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				rawir.pulse = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			rawir.duration = US_PER_BIT * (8 - b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				event = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			rawir.pulse = !rawir.pulse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			rawir.duration = US_PER_BIT * b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				event = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* don't wakeup when there's nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		ir_raw_event_handle(tt->rc);
^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) static void ttusbir_urb_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct ttusbir *tt = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		ttusbir_process_ir_data(tt, urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		usb_unlink_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		break;
^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) 	rc = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (rc && rc != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		dev_warn(tt->dev, "failed to resubmit urb: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int ttusbir_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			 const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct ttusbir *tt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct usb_interface_descriptor *idesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct usb_endpoint_descriptor *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct rc_dev *rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	int i, j, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int altsetting = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!tt || !rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	/* find the correct alt setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	for (i = 0; i < intf->num_altsetting && altsetting == -1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		int max_packet, bulk_out_endp = -1, iso_in_endp = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		idesc = &intf->altsetting[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		for (j = 0; j < idesc->bNumEndpoints; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			desc = &intf->altsetting[i].endpoint[j].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			max_packet = le16_to_cpu(desc->wMaxPacketSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			if (usb_endpoint_dir_in(desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 					usb_endpoint_xfer_isoc(desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 					max_packet == 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				iso_in_endp = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			else if (usb_endpoint_dir_out(desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 					usb_endpoint_xfer_bulk(desc) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					max_packet == 0x20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 				bulk_out_endp = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			if (bulk_out_endp != -1 && iso_in_endp != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				tt->bulk_out_endp = bulk_out_endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				tt->iso_in_endp = iso_in_endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				altsetting = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (altsetting == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		dev_err(&intf->dev, "cannot find expected altsetting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	tt->dev = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	tt->udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	tt->rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	ret = usb_set_interface(tt->udev, 0, altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	for (i = 0; i < NUM_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		struct urb *urb = usb_alloc_urb(8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		void *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		urb->dev = tt->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		urb->context = tt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		urb->pipe = usb_rcvisocpipe(tt->udev, tt->iso_in_endp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		urb->interval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		buffer = usb_alloc_coherent(tt->udev, 128, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 						&urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP | URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		urb->transfer_buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		urb->complete = ttusbir_urb_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		urb->number_of_packets = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		urb->transfer_buffer_length = 128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		for (j = 0; j < 8; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			urb->iso_frame_desc[j].offset = j * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			urb->iso_frame_desc[j].length = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		tt->urb[i] = urb;
^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) 	tt->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!tt->bulk_urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	tt->bulk_buffer[0] = 0xaa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	tt->bulk_buffer[1] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	tt->bulk_buffer[2] = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	tt->bulk_buffer[3] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	usb_fill_bulk_urb(tt->bulk_urb, tt->udev, usb_sndbulkpipe(tt->udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		tt->bulk_out_endp), tt->bulk_buffer, sizeof(tt->bulk_buffer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 						ttusbir_bulk_complete, tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	tt->led.name = "ttusbir:green:power";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	tt->led.default_trigger = "rc-feedback";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	tt->led.brightness_set = ttusbir_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	tt->led.brightness_get = ttusbir_brightness_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	tt->is_led_on = tt->led_on = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	atomic_set(&tt->led_complete, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ret = led_classdev_register(&intf->dev, &tt->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	usb_make_path(tt->udev, tt->phys, sizeof(tt->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	rc->device_name = DRIVER_DESC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	rc->input_phys = tt->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	usb_to_input_id(tt->udev, &rc->input_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	rc->dev.parent = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	rc->priv = tt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	rc->driver_name = DRIVER_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	rc->map_name = RC_MAP_TT_1500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	rc->min_timeout = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	rc->timeout = IR_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 * The precision is US_PER_BIT, but since every 8th bit can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 * overwritten with garbage the accuracy is at best 2 * US_PER_BIT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	rc->rx_resolution = 2 * US_PER_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	ret = rc_register_device(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		dev_err(&intf->dev, "failed to register rc device %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	usb_set_intfdata(intf, tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	for (i = 0; i < NUM_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		ret = usb_submit_urb(tt->urb[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			dev_err(tt->dev, "failed to submit urb %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			goto out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		}
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	rc_unregister_device(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	rc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	led_classdev_unregister(&tt->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (tt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		for (i = 0; i < NUM_URBS && tt->urb[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			struct urb *urb = tt->urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			usb_free_coherent(tt->udev, 128, urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 							urb->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		usb_kill_urb(tt->bulk_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		usb_free_urb(tt->bulk_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		kfree(tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	rc_free_device(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static void ttusbir_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct ttusbir *tt = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	struct usb_device *udev = tt->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	tt->udev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	rc_unregister_device(tt->rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	led_classdev_unregister(&tt->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	for (i = 0; i < NUM_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		usb_kill_urb(tt->urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		usb_free_coherent(udev, 128, tt->urb[i]->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 						tt->urb[i]->transfer_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		usb_free_urb(tt->urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	usb_kill_urb(tt->bulk_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	usb_free_urb(tt->bulk_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	kfree(tt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static int ttusbir_suspend(struct usb_interface *intf, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct ttusbir *tt = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	for (i = 0; i < NUM_URBS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		usb_kill_urb(tt->urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	led_classdev_suspend(&tt->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	usb_kill_urb(tt->bulk_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static int ttusbir_resume(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	struct ttusbir *tt = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	tt->is_led_on = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	led_classdev_resume(&tt->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	for (i = 0; i < NUM_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		rc = usb_submit_urb(tt->urb[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			dev_warn(tt->dev, "failed to submit urb: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static const struct usb_device_id ttusbir_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	{ USB_DEVICE(0x0b48, 0x2003) },
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static struct usb_driver ttusbir_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	.name = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	.id_table = ttusbir_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	.probe = ttusbir_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	.suspend = ttusbir_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	.resume = ttusbir_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	.reset_resume = ttusbir_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	.disconnect = ttusbir_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) module_usb_driver(ttusbir_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) MODULE_AUTHOR("Sean Young <sean@mess.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) MODULE_DEVICE_TABLE(usb, ttusbir_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)