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)  * IguanaWorks USB IR Transceiver support
^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/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/usb/input.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/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <media/rc-core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define BUF_SIZE 152
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) struct iguanair {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct rc_dev *rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	uint16_t version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	uint8_t bufsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	uint8_t cycle_overhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	/* receiver support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	bool receiver_on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	dma_addr_t dma_in, dma_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	uint8_t *buf_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct urb *urb_in, *urb_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct completion completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	/* transmit support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	bool tx_overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	uint32_t carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct send_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	char name[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	char phys[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define CMD_NOP			0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define CMD_GET_VERSION		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define CMD_GET_BUFSIZE		0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define CMD_GET_FEATURES	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define CMD_SEND		0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define CMD_EXECUTE		0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define CMD_RX_OVERFLOW		0x31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define CMD_TX_OVERFLOW		0x32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define CMD_RECEIVER_ON		0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define CMD_RECEIVER_OFF	0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define DIR_IN			0xdc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define DIR_OUT			0xcd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define MAX_IN_PACKET		8u
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define MAX_OUT_PACKET		(sizeof(struct send_packet) + BUF_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define TIMEOUT			1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define RX_RESOLUTION		21
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) struct packet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	uint16_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	uint8_t direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	uint8_t cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) struct send_packet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct packet header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	uint8_t length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	uint8_t channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	uint8_t busy7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	uint8_t busy4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	uint8_t payload[];
^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) static void process_ir_data(struct iguanair *ir, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		switch (ir->buf_in[3]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		case CMD_GET_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			if (len == 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				ir->version = (ir->buf_in[5] << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 							ir->buf_in[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				complete(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		case CMD_GET_BUFSIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			if (len >= 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				ir->bufsize = ir->buf_in[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				complete(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		case CMD_GET_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			if (len > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				ir->cycle_overhead = ir->buf_in[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 				complete(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		case CMD_TX_OVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			ir->tx_overflow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		case CMD_RECEIVER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		case CMD_RECEIVER_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		case CMD_SEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			complete(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		case CMD_RX_OVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			dev_warn(ir->dev, "receive overflow\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			ir_raw_event_reset(ir->rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			dev_warn(ir->dev, "control code %02x received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 							ir->buf_in[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	} else if (len >= 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		struct ir_raw_event rawir = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		bool event = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		for (i = 0; i < 7; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			if (ir->buf_in[i] == 0x80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				rawir.pulse = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				rawir.duration = 21845;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 								 RX_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			if (ir_raw_event_store_with_filter(ir->rc, &rawir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 				event = true;
^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) 		if (event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			ir_raw_event_handle(ir->rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^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) static void iguanair_rx(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct iguanair *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ir = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!ir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		usb_unlink_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return;
^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) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		process_ir_data(ir, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		usb_unlink_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case -EPIPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	rc = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (rc && rc != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
^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) static void iguanair_irq_out(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct iguanair *ir = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (urb->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/* if we sent an nop packet, do not expect a response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		complete(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int iguanair_send(struct iguanair *ir, unsigned size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	reinit_completion(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	ir->urb_out->transfer_buffer_length = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int iguanair_get_features(struct iguanair *ir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * On cold boot, the iguanair initializes on the first packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * received but does not process that packet. Send an empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	ir->packet->header.start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	ir->packet->header.direction = DIR_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ir->packet->header.cmd = CMD_NOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	iguanair_send(ir, sizeof(ir->packet->header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	ir->packet->header.cmd = CMD_GET_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	rc = iguanair_send(ir, sizeof(ir->packet->header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		dev_info(ir->dev, "failed to get version\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (ir->version < 0x205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ir->bufsize = 150;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	ir->cycle_overhead = 65;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	ir->packet->header.cmd = CMD_GET_BUFSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	rc = iguanair_send(ir, sizeof(ir->packet->header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		dev_info(ir->dev, "failed to get buffer size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (ir->bufsize > BUF_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		dev_info(ir->dev, "buffer size %u larger than expected\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 								ir->bufsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		ir->bufsize = BUF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	ir->packet->header.cmd = CMD_GET_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	rc = iguanair_send(ir, sizeof(ir->packet->header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dev_info(ir->dev, "failed to get features\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int iguanair_receiver(struct iguanair *ir, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	ir->packet->header.start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ir->packet->header.direction = DIR_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ir_raw_event_reset(ir->rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return iguanair_send(ir, sizeof(ir->packet->header));
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * The iguanair creates the carrier by busy spinning after each half period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * This is counted in CPU cycles, with the CPU running at 24MHz. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * broken down into 7-cycles and 4-cyles delays, with a preference for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct iguanair *ir = dev->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (carrier < 25000 || carrier > 150000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (carrier != ir->carrier) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		uint32_t cycles, fours, sevens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		ir->carrier = carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 							ir->cycle_overhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		 * Calculate minimum number of 7 cycles needed so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		 * we are left with a multiple of 4; so we want to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		 * (sevens * 7) & 3 == cycles & 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		sevens = (4 - cycles) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		fours = (cycles - sevens * 7) / 4;
^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) 		 * The firmware interprets these values as a relative offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		 * for a branch. Immediately following the branches, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		 * 4 instructions of 7 cycles (2 bytes each) and 110
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		 * instructions of 4 cycles (1 byte each). A relative branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		 * of 0 will execute all of them, branch further for less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		 * cycle burning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		ir->packet->busy7 = (4 - sevens) * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		ir->packet->busy4 = 110 - fours;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	struct iguanair *ir = dev->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (mask > 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	ir->packet->channels = mask << 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct iguanair *ir = dev->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	unsigned int i, size, p, periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/* convert from us to carrier periods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	for (i = size = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		while (periods) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			p = min(periods, 127u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			if (size >= ir->bufsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			ir->packet->payload[size++] = p | ((i & 1) ? 0x80 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			periods -= p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^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) 	ir->packet->header.start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	ir->packet->header.direction = DIR_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	ir->packet->header.cmd = CMD_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	ir->packet->length = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	ir->tx_overflow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	rc = iguanair_send(ir, sizeof(*ir->packet) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (rc == 0 && ir->tx_overflow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		rc = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int iguanair_open(struct rc_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct iguanair *ir = rdev->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	rc = iguanair_receiver(ir, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		ir->receiver_on = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static void iguanair_close(struct rc_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct iguanair *ir = rdev->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	rc = iguanair_receiver(ir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	ir->receiver_on = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (rc && rc != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static int iguanair_probe(struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			  const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	struct usb_device *udev = interface_to_usbdev(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct iguanair *ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct rc_dev *rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int ret, pipein, pipeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	struct usb_host_interface *idesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	idesc = intf->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (idesc->desc.bNumEndpoints < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!ir || !rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 								&ir->dma_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 								&ir->dma_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	    !usb_endpoint_is_int_in(&idesc->endpoint[0].desc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	    !usb_endpoint_is_int_out(&idesc->endpoint[1].desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		goto out;
^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) 	ir->rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	ir->dev = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	ir->udev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	init_completion(&ir->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	pipeout = usb_sndintpipe(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 				idesc->endpoint[1].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 						iguanair_irq_out, ir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	ir->urb_out->transfer_dma = ir->dma_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 							 iguanair_rx, ir, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	ir->urb_in->transfer_dma = ir->dma_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	ret = iguanair_get_features(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	snprintf(ir->name, sizeof(ir->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		"IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	rc->device_name = ir->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	rc->input_phys = ir->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	usb_to_input_id(ir->udev, &rc->input_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	rc->dev.parent = &intf->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	rc->priv = ir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	rc->open = iguanair_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	rc->close = iguanair_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	rc->s_tx_mask = iguanair_set_tx_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	rc->s_tx_carrier = iguanair_set_tx_carrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	rc->tx_ir = iguanair_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	rc->driver_name = KBUILD_MODNAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	rc->map_name = RC_MAP_RC6_MCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	rc->min_timeout = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	rc->timeout = IR_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	rc->rx_resolution = RX_RESOLUTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	iguanair_set_tx_carrier(rc, 38000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	iguanair_set_tx_mask(rc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	ret = rc_register_device(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		dev_err(&intf->dev, "failed to register rc device %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	usb_set_intfdata(intf, ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	usb_kill_urb(ir->urb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	usb_kill_urb(ir->urb_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (ir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		usb_free_urb(ir->urb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		usb_free_urb(ir->urb_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 								ir->dma_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	rc_free_device(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	kfree(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) static void iguanair_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	struct iguanair *ir = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	rc_unregister_device(ir->rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	usb_kill_urb(ir->urb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	usb_kill_urb(ir->urb_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	usb_free_urb(ir->urb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	usb_free_urb(ir->urb_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	kfree(ir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct iguanair *ir = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (ir->receiver_on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		rc = iguanair_receiver(ir, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			dev_warn(ir->dev, "failed to disable receiver for suspend\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	usb_kill_urb(ir->urb_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	usb_kill_urb(ir->urb_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static int iguanair_resume(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	struct iguanair *ir = usb_get_intfdata(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	if (ir->receiver_on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		rc = iguanair_receiver(ir, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			dev_warn(ir->dev, "failed to enable receiver after resume\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static const struct usb_device_id iguanair_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	{ USB_DEVICE(0x1781, 0x0938) },
^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) static struct usb_driver iguanair_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	.name =	KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	.probe = iguanair_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	.disconnect = iguanair_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	.suspend = iguanair_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	.resume = iguanair_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	.reset_resume = iguanair_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	.id_table = iguanair_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	.soft_unbind = 1	/* we want to disable receiver on unbind */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) module_usb_driver(iguanair_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) MODULE_AUTHOR("Sean Young <sean@mess.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) MODULE_DEVICE_TABLE(usb, iguanair_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)