xref: /openbmc/linux/drivers/media/rc/redrat3.c (revision c53f9f00e5ddf72046698d6a378384e29fc9795f)
12154be65SJarod Wilson /*
22154be65SJarod Wilson  * USB RedRat3 IR Transceiver rc-core driver
32154be65SJarod Wilson  *
42154be65SJarod Wilson  * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
52154be65SJarod Wilson  *  based heavily on the work of Stephen Cox, with additional
62154be65SJarod Wilson  *  help from RedRat Ltd.
72154be65SJarod Wilson  *
82154be65SJarod Wilson  * This driver began life based an an old version of the first-generation
92154be65SJarod Wilson  * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
102154be65SJarod Wilson  * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
112154be65SJarod Wilson  * Chris Dodge.
122154be65SJarod Wilson  *
132154be65SJarod Wilson  * The driver was then ported to rc-core and significantly rewritten again,
142154be65SJarod Wilson  * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
152154be65SJarod Wilson  * port effort was started by Stephen.
162154be65SJarod Wilson  *
172154be65SJarod Wilson  * TODO LIST:
182154be65SJarod Wilson  * - fix lirc not showing repeats properly
192154be65SJarod Wilson  * --
202154be65SJarod Wilson  *
212154be65SJarod Wilson  * The RedRat3 is a USB transceiver with both send & receive,
222154be65SJarod Wilson  * with 2 separate sensors available for receive to enable
232154be65SJarod Wilson  * both good long range reception for general use, and good
242154be65SJarod Wilson  * short range reception when required for learning a signal.
252154be65SJarod Wilson  *
262154be65SJarod Wilson  * http://www.redrat.co.uk/
272154be65SJarod Wilson  *
282154be65SJarod Wilson  * It uses its own little protocol to communicate, the required
292154be65SJarod Wilson  * parts of which are embedded within this driver.
302154be65SJarod Wilson  * --
312154be65SJarod Wilson  *
322154be65SJarod Wilson  * This program is free software; you can redistribute it and/or modify
332154be65SJarod Wilson  * it under the terms of the GNU General Public License as published by
342154be65SJarod Wilson  * the Free Software Foundation; either version 2 of the License, or
352154be65SJarod Wilson  * (at your option) any later version.
362154be65SJarod Wilson  *
372154be65SJarod Wilson  * This program is distributed in the hope that it will be useful,
382154be65SJarod Wilson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
392154be65SJarod Wilson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
402154be65SJarod Wilson  * GNU General Public License for more details.
412154be65SJarod Wilson  *
422154be65SJarod Wilson  * You should have received a copy of the GNU General Public License
432154be65SJarod Wilson  * along with this program; if not, write to the Free Software
442154be65SJarod Wilson  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
452154be65SJarod Wilson  *
462154be65SJarod Wilson  */
472154be65SJarod Wilson 
482154be65SJarod Wilson #include <linux/device.h>
492154be65SJarod Wilson #include <linux/module.h>
502154be65SJarod Wilson #include <linux/slab.h>
512154be65SJarod Wilson #include <linux/usb.h>
522154be65SJarod Wilson #include <linux/usb/input.h>
532154be65SJarod Wilson #include <media/rc-core.h>
542154be65SJarod Wilson 
552154be65SJarod Wilson /* Driver Information */
562154be65SJarod Wilson #define DRIVER_VERSION "0.70"
572154be65SJarod Wilson #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
582154be65SJarod Wilson #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
592154be65SJarod Wilson #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
602154be65SJarod Wilson #define DRIVER_NAME "redrat3"
612154be65SJarod Wilson 
622154be65SJarod Wilson /* module parameters */
632154be65SJarod Wilson #ifdef CONFIG_USB_DEBUG
642154be65SJarod Wilson static int debug = 1;
652154be65SJarod Wilson #else
662154be65SJarod Wilson static int debug;
672154be65SJarod Wilson #endif
682154be65SJarod Wilson 
692154be65SJarod Wilson #define RR3_DEBUG_STANDARD		0x1
702154be65SJarod Wilson #define RR3_DEBUG_FUNCTION_TRACE	0x2
712154be65SJarod Wilson 
722154be65SJarod Wilson #define rr3_dbg(dev, fmt, ...)					\
732154be65SJarod Wilson 	do {							\
742154be65SJarod Wilson 		if (debug & RR3_DEBUG_STANDARD)			\
752154be65SJarod Wilson 			dev_info(dev, fmt, ## __VA_ARGS__);	\
762154be65SJarod Wilson 	} while (0)
772154be65SJarod Wilson 
782154be65SJarod Wilson #define rr3_ftr(dev, fmt, ...)					\
792154be65SJarod Wilson 	do {							\
802154be65SJarod Wilson 		if (debug & RR3_DEBUG_FUNCTION_TRACE)		\
812154be65SJarod Wilson 			dev_info(dev, fmt, ## __VA_ARGS__);	\
822154be65SJarod Wilson 	} while (0)
832154be65SJarod Wilson 
842154be65SJarod Wilson /* bulk data transfer types */
852154be65SJarod Wilson #define RR3_ERROR		0x01
862154be65SJarod Wilson #define RR3_MOD_SIGNAL_IN	0x20
872154be65SJarod Wilson #define RR3_MOD_SIGNAL_OUT	0x21
882154be65SJarod Wilson 
892154be65SJarod Wilson /* Get the RR firmware version */
902154be65SJarod Wilson #define RR3_FW_VERSION		0xb1
912154be65SJarod Wilson #define RR3_FW_VERSION_LEN	64
922154be65SJarod Wilson /* Send encoded signal bulk-sent earlier*/
932154be65SJarod Wilson #define RR3_TX_SEND_SIGNAL	0xb3
942154be65SJarod Wilson #define RR3_SET_IR_PARAM	0xb7
952154be65SJarod Wilson #define RR3_GET_IR_PARAM	0xb8
962154be65SJarod Wilson /* Blink the red LED on the device */
972154be65SJarod Wilson #define RR3_BLINK_LED		0xb9
982154be65SJarod Wilson /* Read serial number of device */
992154be65SJarod Wilson #define RR3_READ_SER_NO		0xba
1002154be65SJarod Wilson #define RR3_SER_NO_LEN		4
1012154be65SJarod Wilson /* Start capture with the RC receiver */
1022154be65SJarod Wilson #define RR3_RC_DET_ENABLE	0xbb
1032154be65SJarod Wilson /* Stop capture with the RC receiver */
1042154be65SJarod Wilson #define RR3_RC_DET_DISABLE	0xbc
1052154be65SJarod Wilson /* Return the status of RC detector capture */
1062154be65SJarod Wilson #define RR3_RC_DET_STATUS	0xbd
1072154be65SJarod Wilson /* Reset redrat */
1082154be65SJarod Wilson #define RR3_RESET		0xa0
1092154be65SJarod Wilson 
1102154be65SJarod Wilson /* Max number of lengths in the signal. */
1112154be65SJarod Wilson #define RR3_IR_IO_MAX_LENGTHS	0x01
1122154be65SJarod Wilson /* Periods to measure mod. freq. */
1132154be65SJarod Wilson #define RR3_IR_IO_PERIODS_MF	0x02
1142154be65SJarod Wilson /* Size of memory for main signal data */
1152154be65SJarod Wilson #define RR3_IR_IO_SIG_MEM_SIZE	0x03
1162154be65SJarod Wilson /* Delta value when measuring lengths */
1172154be65SJarod Wilson #define RR3_IR_IO_LENGTH_FUZZ	0x04
1182154be65SJarod Wilson /* Timeout for end of signal detection */
1192154be65SJarod Wilson #define RR3_IR_IO_SIG_TIMEOUT	0x05
1202154be65SJarod Wilson /* Minumum value for pause recognition. */
1212154be65SJarod Wilson #define RR3_IR_IO_MIN_PAUSE	0x06
1222154be65SJarod Wilson 
1232154be65SJarod Wilson /* Clock freq. of EZ-USB chip */
1242154be65SJarod Wilson #define RR3_CLK			24000000
1252154be65SJarod Wilson /* Clock periods per timer count */
1262154be65SJarod Wilson #define RR3_CLK_PER_COUNT	12
1272154be65SJarod Wilson /* (RR3_CLK / RR3_CLK_PER_COUNT) */
1282154be65SJarod Wilson #define RR3_CLK_CONV_FACTOR	2000000
1292154be65SJarod Wilson /* USB bulk-in IR data endpoint address */
1302154be65SJarod Wilson #define RR3_BULK_IN_EP_ADDR	0x82
1312154be65SJarod Wilson 
1322154be65SJarod Wilson /* Raw Modulated signal data value offsets */
1332154be65SJarod Wilson #define RR3_PAUSE_OFFSET	0
1342154be65SJarod Wilson #define RR3_FREQ_COUNT_OFFSET	4
1352154be65SJarod Wilson #define RR3_NUM_PERIOD_OFFSET	6
1362154be65SJarod Wilson #define RR3_MAX_LENGTHS_OFFSET	8
1372154be65SJarod Wilson #define RR3_NUM_LENGTHS_OFFSET	9
1382154be65SJarod Wilson #define RR3_MAX_SIGS_OFFSET	10
1392154be65SJarod Wilson #define RR3_NUM_SIGS_OFFSET	12
1402154be65SJarod Wilson #define RR3_REPEATS_OFFSET	14
1412154be65SJarod Wilson 
1422154be65SJarod Wilson /* Size of the fixed-length portion of the signal */
1432154be65SJarod Wilson #define RR3_HEADER_LENGTH	15
1442154be65SJarod Wilson #define RR3_DRIVER_MAXLENS	128
1452154be65SJarod Wilson #define RR3_MAX_SIG_SIZE	512
1462154be65SJarod Wilson #define RR3_MAX_BUF_SIZE	\
1472154be65SJarod Wilson 	((2 * RR3_HEADER_LENGTH) + RR3_DRIVER_MAXLENS + RR3_MAX_SIG_SIZE)
1482154be65SJarod Wilson #define RR3_TIME_UNIT		50
1492154be65SJarod Wilson #define RR3_END_OF_SIGNAL	0x7f
1502154be65SJarod Wilson #define RR3_TX_HEADER_OFFSET	4
1512154be65SJarod Wilson #define RR3_TX_TRAILER_LEN	2
1522154be65SJarod Wilson #define RR3_RX_MIN_TIMEOUT	5
1532154be65SJarod Wilson #define RR3_RX_MAX_TIMEOUT	2000
1542154be65SJarod Wilson 
1552154be65SJarod Wilson /* The 8051's CPUCS Register address */
1562154be65SJarod Wilson #define RR3_CPUCS_REG_ADDR	0x7f92
1572154be65SJarod Wilson 
1582154be65SJarod Wilson #define USB_RR3USB_VENDOR_ID	0x112a
1592154be65SJarod Wilson #define USB_RR3USB_PRODUCT_ID	0x0001
1602154be65SJarod Wilson #define USB_RR3IIUSB_PRODUCT_ID	0x0005
1612154be65SJarod Wilson 
1622154be65SJarod Wilson /* table of devices that work with this driver */
1632154be65SJarod Wilson static struct usb_device_id redrat3_dev_table[] = {
1642154be65SJarod Wilson 	/* Original version of the RedRat3 */
1652154be65SJarod Wilson 	{USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
1662154be65SJarod Wilson 	/* Second Version/release of the RedRat3 - RetRat3-II */
1672154be65SJarod Wilson 	{USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
1682154be65SJarod Wilson 	{}			/* Terminating entry */
1692154be65SJarod Wilson };
1702154be65SJarod Wilson 
1712154be65SJarod Wilson /* Structure to hold all of our device specific stuff */
1722154be65SJarod Wilson struct redrat3_dev {
1732154be65SJarod Wilson 	/* core device bits */
1742154be65SJarod Wilson 	struct rc_dev *rc;
1752154be65SJarod Wilson 	struct device *dev;
1762154be65SJarod Wilson 
1772154be65SJarod Wilson 	/* save off the usb device pointer */
1782154be65SJarod Wilson 	struct usb_device *udev;
1792154be65SJarod Wilson 
1802154be65SJarod Wilson 	/* the receive endpoint */
1812154be65SJarod Wilson 	struct usb_endpoint_descriptor *ep_in;
1822154be65SJarod Wilson 	/* the buffer to receive data */
1832154be65SJarod Wilson 	unsigned char *bulk_in_buf;
1842154be65SJarod Wilson 	/* urb used to read ir data */
1852154be65SJarod Wilson 	struct urb *read_urb;
1862154be65SJarod Wilson 
1872154be65SJarod Wilson 	/* the send endpoint */
1882154be65SJarod Wilson 	struct usb_endpoint_descriptor *ep_out;
1892154be65SJarod Wilson 	/* the buffer to send data */
1902154be65SJarod Wilson 	unsigned char *bulk_out_buf;
1912154be65SJarod Wilson 	/* the urb used to send data */
1922154be65SJarod Wilson 	struct urb *write_urb;
1932154be65SJarod Wilson 
1942154be65SJarod Wilson 	/* usb dma */
1952154be65SJarod Wilson 	dma_addr_t dma_in;
1962154be65SJarod Wilson 	dma_addr_t dma_out;
1972154be65SJarod Wilson 
1982154be65SJarod Wilson 	/* true if write urb is busy */
1992154be65SJarod Wilson 	bool write_busy;
2002154be65SJarod Wilson 	/* wait for the write to finish */
2012154be65SJarod Wilson 	struct completion write_finished;
2022154be65SJarod Wilson 
2032154be65SJarod Wilson 	/* locks this structure */
2042154be65SJarod Wilson 	struct mutex lock;
2052154be65SJarod Wilson 
2062154be65SJarod Wilson 	/* rx signal timeout timer */
2072154be65SJarod Wilson 	struct timer_list rx_timeout;
208*c53f9f00SJarod Wilson 	u32 hw_timeout;
2092154be65SJarod Wilson 
2102154be65SJarod Wilson 	/* Is the device currently receiving? */
2112154be65SJarod Wilson 	bool recv_in_progress;
2122154be65SJarod Wilson 	/* is the detector enabled*/
2132154be65SJarod Wilson 	bool det_enabled;
2142154be65SJarod Wilson 	/* Is the device currently transmitting?*/
2152154be65SJarod Wilson 	bool transmitting;
2162154be65SJarod Wilson 
2172154be65SJarod Wilson 	/* store for current packet */
2182154be65SJarod Wilson 	char pbuf[RR3_MAX_BUF_SIZE];
2192154be65SJarod Wilson 	u16 pktlen;
2202154be65SJarod Wilson 	u16 pkttype;
2212154be65SJarod Wilson 	u16 bytes_read;
2222154be65SJarod Wilson 	/* indicate whether we are going to reprocess
2232154be65SJarod Wilson 	 * the USB callback with a bigger buffer */
2242154be65SJarod Wilson 	int buftoosmall;
2252154be65SJarod Wilson 	char *datap;
2262154be65SJarod Wilson 
2272154be65SJarod Wilson 	u32 carrier;
2282154be65SJarod Wilson 
2292154be65SJarod Wilson 	char name[128];
2302154be65SJarod Wilson 	char phys[64];
2312154be65SJarod Wilson };
2322154be65SJarod Wilson 
2332154be65SJarod Wilson /* All incoming data buffers adhere to a very specific data format */
2342154be65SJarod Wilson struct redrat3_signal_header {
2352154be65SJarod Wilson 	u16 length;	/* Length of data being transferred */
2362154be65SJarod Wilson 	u16 transfer_type; /* Type of data transferred */
2372154be65SJarod Wilson 	u32 pause;	/* Pause between main and repeat signals */
2382154be65SJarod Wilson 	u16 mod_freq_count; /* Value of timer on mod. freq. measurement */
2392154be65SJarod Wilson 	u16 no_periods;	/* No. of periods over which mod. freq. is measured */
2402154be65SJarod Wilson 	u8 max_lengths;	/* Max no. of lengths (i.e. size of array) */
2412154be65SJarod Wilson 	u8 no_lengths;	/* Actual no. of elements in lengths array */
2422154be65SJarod Wilson 	u16 max_sig_size; /* Max no. of values in signal data array */
2432154be65SJarod Wilson 	u16 sig_size;	/* Acuto no. of values in signal data array */
2442154be65SJarod Wilson 	u8 no_repeats;	/* No. of repeats of repeat signal section */
2452154be65SJarod Wilson 	/* Here forward is the lengths and signal data */
2462154be65SJarod Wilson };
2472154be65SJarod Wilson 
2482154be65SJarod Wilson static void redrat3_dump_signal_header(struct redrat3_signal_header *header)
2492154be65SJarod Wilson {
2502154be65SJarod Wilson 	pr_info("%s:\n", __func__);
2512154be65SJarod Wilson 	pr_info(" * length: %u, transfer_type: 0x%02x\n",
2522154be65SJarod Wilson 		header->length, header->transfer_type);
2532154be65SJarod Wilson 	pr_info(" * pause: %u, freq_count: %u, no_periods: %u\n",
2542154be65SJarod Wilson 		header->pause, header->mod_freq_count, header->no_periods);
2552154be65SJarod Wilson 	pr_info(" * lengths: %u (max: %u)\n",
2562154be65SJarod Wilson 		header->no_lengths, header->max_lengths);
2572154be65SJarod Wilson 	pr_info(" * sig_size: %u (max: %u)\n",
2582154be65SJarod Wilson 		header->sig_size, header->max_sig_size);
2592154be65SJarod Wilson 	pr_info(" * repeats: %u\n", header->no_repeats);
2602154be65SJarod Wilson }
2612154be65SJarod Wilson 
2622154be65SJarod Wilson static void redrat3_dump_signal_data(char *buffer, u16 len)
2632154be65SJarod Wilson {
2642154be65SJarod Wilson 	int offset, i;
2652154be65SJarod Wilson 	char *data_vals;
2662154be65SJarod Wilson 
2672154be65SJarod Wilson 	pr_info("%s:", __func__);
2682154be65SJarod Wilson 
2692154be65SJarod Wilson 	offset = RR3_TX_HEADER_OFFSET + RR3_HEADER_LENGTH
2702154be65SJarod Wilson 		 + (RR3_DRIVER_MAXLENS * sizeof(u16));
2712154be65SJarod Wilson 
2722154be65SJarod Wilson 	/* read RR3_DRIVER_MAXLENS from ctrl msg */
2732154be65SJarod Wilson 	data_vals = buffer + offset;
2742154be65SJarod Wilson 
2752154be65SJarod Wilson 	for (i = 0; i < len; i++) {
2762154be65SJarod Wilson 		if (i % 10 == 0)
2772154be65SJarod Wilson 			pr_cont("\n * ");
2782154be65SJarod Wilson 		pr_cont("%02x ", *data_vals++);
2792154be65SJarod Wilson 	}
2802154be65SJarod Wilson 
2812154be65SJarod Wilson 	pr_cont("\n");
2822154be65SJarod Wilson }
2832154be65SJarod Wilson 
2842154be65SJarod Wilson /*
2852154be65SJarod Wilson  * redrat3_issue_async
2862154be65SJarod Wilson  *
2872154be65SJarod Wilson  *  Issues an async read to the ir data in port..
2882154be65SJarod Wilson  *  sets the callback to be redrat3_handle_async
2892154be65SJarod Wilson  */
2902154be65SJarod Wilson static void redrat3_issue_async(struct redrat3_dev *rr3)
2912154be65SJarod Wilson {
2922154be65SJarod Wilson 	int res;
2932154be65SJarod Wilson 
2942154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entering %s\n", __func__);
2952154be65SJarod Wilson 
2962154be65SJarod Wilson 	if (!rr3->det_enabled) {
2972154be65SJarod Wilson 		dev_warn(rr3->dev, "not issuing async read, "
2982154be65SJarod Wilson 			 "detector not enabled\n");
2992154be65SJarod Wilson 		return;
3002154be65SJarod Wilson 	}
3012154be65SJarod Wilson 
3022154be65SJarod Wilson 	memset(rr3->bulk_in_buf, 0, rr3->ep_in->wMaxPacketSize);
3032154be65SJarod Wilson 	res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
3042154be65SJarod Wilson 	if (res)
3052154be65SJarod Wilson 		rr3_dbg(rr3->dev, "%s: receive request FAILED! "
3062154be65SJarod Wilson 			"(res %d, len %d)\n", __func__, res,
3072154be65SJarod Wilson 			rr3->read_urb->transfer_buffer_length);
3082154be65SJarod Wilson }
3092154be65SJarod Wilson 
3102154be65SJarod Wilson static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
3112154be65SJarod Wilson {
3122154be65SJarod Wilson 	if (!rr3->transmitting && (code != 0x40))
3132154be65SJarod Wilson 		dev_info(rr3->dev, "fw error code 0x%02x: ", code);
3142154be65SJarod Wilson 
3152154be65SJarod Wilson 	switch (code) {
3162154be65SJarod Wilson 	case 0x00:
3172154be65SJarod Wilson 		pr_cont("No Error\n");
3182154be65SJarod Wilson 		break;
3192154be65SJarod Wilson 
3202154be65SJarod Wilson 	/* Codes 0x20 through 0x2f are IR Firmware Errors */
3212154be65SJarod Wilson 	case 0x20:
3222154be65SJarod Wilson 		pr_cont("Initial signal pulse not long enough "
3232154be65SJarod Wilson 			"to measure carrier frequency\n");
3242154be65SJarod Wilson 		break;
3252154be65SJarod Wilson 	case 0x21:
3262154be65SJarod Wilson 		pr_cont("Not enough length values allocated for signal\n");
3272154be65SJarod Wilson 		break;
3282154be65SJarod Wilson 	case 0x22:
3292154be65SJarod Wilson 		pr_cont("Not enough memory allocated for signal data\n");
3302154be65SJarod Wilson 		break;
3312154be65SJarod Wilson 	case 0x23:
3322154be65SJarod Wilson 		pr_cont("Too many signal repeats\n");
3332154be65SJarod Wilson 		break;
3342154be65SJarod Wilson 	case 0x28:
3352154be65SJarod Wilson 		pr_cont("Insufficient memory available for IR signal "
3362154be65SJarod Wilson 			"data memory allocation\n");
3372154be65SJarod Wilson 		break;
3382154be65SJarod Wilson 	case 0x29:
3392154be65SJarod Wilson 		pr_cont("Insufficient memory available "
3402154be65SJarod Wilson 			"for IrDa signal data memory allocation\n");
3412154be65SJarod Wilson 		break;
3422154be65SJarod Wilson 
3432154be65SJarod Wilson 	/* Codes 0x30 through 0x3f are USB Firmware Errors */
3442154be65SJarod Wilson 	case 0x30:
3452154be65SJarod Wilson 		pr_cont("Insufficient memory available for bulk "
3462154be65SJarod Wilson 			"transfer structure\n");
3472154be65SJarod Wilson 		break;
3482154be65SJarod Wilson 
3492154be65SJarod Wilson 	/*
3502154be65SJarod Wilson 	 * Other error codes... These are primarily errors that can occur in
3512154be65SJarod Wilson 	 * the control messages sent to the redrat
3522154be65SJarod Wilson 	 */
3532154be65SJarod Wilson 	case 0x40:
3542154be65SJarod Wilson 		if (!rr3->transmitting)
3552154be65SJarod Wilson 			pr_cont("Signal capture has been terminated\n");
3562154be65SJarod Wilson 		break;
3572154be65SJarod Wilson 	case 0x41:
3582154be65SJarod Wilson 		pr_cont("Attempt to set/get and unknown signal I/O "
3592154be65SJarod Wilson 			"algorithm parameter\n");
3602154be65SJarod Wilson 		break;
3612154be65SJarod Wilson 	case 0x42:
3622154be65SJarod Wilson 		pr_cont("Signal capture already started\n");
3632154be65SJarod Wilson 		break;
3642154be65SJarod Wilson 
3652154be65SJarod Wilson 	default:
3662154be65SJarod Wilson 		pr_cont("Unknown Error\n");
3672154be65SJarod Wilson 		break;
3682154be65SJarod Wilson 	}
3692154be65SJarod Wilson }
3702154be65SJarod Wilson 
3712154be65SJarod Wilson static u32 redrat3_val_to_mod_freq(struct redrat3_signal_header *ph)
3722154be65SJarod Wilson {
3732154be65SJarod Wilson 	u32 mod_freq = 0;
3742154be65SJarod Wilson 
3752154be65SJarod Wilson 	if (ph->mod_freq_count != 0)
3762154be65SJarod Wilson 		mod_freq = (RR3_CLK * ph->no_periods) /
3772154be65SJarod Wilson 				(ph->mod_freq_count * RR3_CLK_PER_COUNT);
3782154be65SJarod Wilson 
3792154be65SJarod Wilson 	return mod_freq;
3802154be65SJarod Wilson }
3812154be65SJarod Wilson 
3822154be65SJarod Wilson /* this function scales down the figures for the same result... */
3832154be65SJarod Wilson static u32 redrat3_len_to_us(u32 length)
3842154be65SJarod Wilson {
3852154be65SJarod Wilson 	u32 biglen = length * 1000;
3862154be65SJarod Wilson 	u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
3872154be65SJarod Wilson 	u32 result = (u32) (biglen / divisor);
3882154be65SJarod Wilson 
3892154be65SJarod Wilson 	/* don't allow zero lengths to go back, breaks lirc */
3902154be65SJarod Wilson 	return result ? result : 1;
3912154be65SJarod Wilson }
3922154be65SJarod Wilson 
3932154be65SJarod Wilson /*
3942154be65SJarod Wilson  * convert us back into redrat3 lengths
3952154be65SJarod Wilson  *
3962154be65SJarod Wilson  * length * 1000   length * 1000000
3972154be65SJarod Wilson  * ------------- = ---------------- = micro
3982154be65SJarod Wilson  * rr3clk / 1000       rr3clk
3992154be65SJarod Wilson 
4002154be65SJarod Wilson  * 6 * 2       4 * 3        micro * rr3clk          micro * rr3clk / 1000
4012154be65SJarod Wilson  * ----- = 4   ----- = 6    -------------- = len    ---------------------
4022154be65SJarod Wilson  *   3           2             1000000                    1000
4032154be65SJarod Wilson  */
4042154be65SJarod Wilson static u32 redrat3_us_to_len(u32 microsec)
4052154be65SJarod Wilson {
4062154be65SJarod Wilson 	u32 result;
4072154be65SJarod Wilson 	u32 divisor;
4082154be65SJarod Wilson 
4092154be65SJarod Wilson 	microsec &= IR_MAX_DURATION;
4102154be65SJarod Wilson 	divisor = (RR3_CLK_CONV_FACTOR / 1000);
4112154be65SJarod Wilson 	result = (u32)(microsec * divisor) / 1000;
4122154be65SJarod Wilson 
4132154be65SJarod Wilson 	/* don't allow zero lengths to go back, breaks lirc */
4142154be65SJarod Wilson 	return result ? result : 1;
4152154be65SJarod Wilson 
4162154be65SJarod Wilson }
4172154be65SJarod Wilson 
41868b2a69dSJarod Wilson /* timer callback to send reset event */
4192154be65SJarod Wilson static void redrat3_rx_timeout(unsigned long data)
4202154be65SJarod Wilson {
4212154be65SJarod Wilson 	struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
4222154be65SJarod Wilson 
4232154be65SJarod Wilson 	rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n");
4242154be65SJarod Wilson 	ir_raw_event_reset(rr3->rc);
4252154be65SJarod Wilson }
4262154be65SJarod Wilson 
4272154be65SJarod Wilson static void redrat3_process_ir_data(struct redrat3_dev *rr3)
4282154be65SJarod Wilson {
4292154be65SJarod Wilson 	DEFINE_IR_RAW_EVENT(rawir);
4302154be65SJarod Wilson 	struct redrat3_signal_header header;
4312154be65SJarod Wilson 	struct device *dev;
432*c53f9f00SJarod Wilson 	int i, trailer = 0;
4332154be65SJarod Wilson 	unsigned long delay;
4342154be65SJarod Wilson 	u32 mod_freq, single_len;
4352154be65SJarod Wilson 	u16 *len_vals;
4362154be65SJarod Wilson 	u8 *data_vals;
4372154be65SJarod Wilson 	u32 tmp32;
4382154be65SJarod Wilson 	u16 tmp16;
4392154be65SJarod Wilson 	char *sig_data;
4402154be65SJarod Wilson 
4412154be65SJarod Wilson 	if (!rr3) {
4422154be65SJarod Wilson 		pr_err("%s called with no context!\n", __func__);
4432154be65SJarod Wilson 		return;
4442154be65SJarod Wilson 	}
4452154be65SJarod Wilson 
4462154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entered %s\n", __func__);
4472154be65SJarod Wilson 
4482154be65SJarod Wilson 	dev = rr3->dev;
4492154be65SJarod Wilson 	sig_data = rr3->pbuf;
4502154be65SJarod Wilson 
4512154be65SJarod Wilson 	header.length = rr3->pktlen;
4522154be65SJarod Wilson 	header.transfer_type = rr3->pkttype;
4532154be65SJarod Wilson 
4542154be65SJarod Wilson 	/* Sanity check */
4552154be65SJarod Wilson 	if (!(header.length >= RR3_HEADER_LENGTH))
4562154be65SJarod Wilson 		dev_warn(dev, "read returned less than rr3 header len\n");
4572154be65SJarod Wilson 
458*c53f9f00SJarod Wilson 	/* Make sure we reset the IR kfifo after a bit of inactivity */
459*c53f9f00SJarod Wilson 	delay = usecs_to_jiffies(rr3->hw_timeout);
4602154be65SJarod Wilson 	mod_timer(&rr3->rx_timeout, jiffies + delay);
4612154be65SJarod Wilson 
4622154be65SJarod Wilson 	memcpy(&tmp32, sig_data + RR3_PAUSE_OFFSET, sizeof(tmp32));
4632154be65SJarod Wilson 	header.pause = be32_to_cpu(tmp32);
4642154be65SJarod Wilson 
4652154be65SJarod Wilson 	memcpy(&tmp16, sig_data + RR3_FREQ_COUNT_OFFSET, sizeof(tmp16));
4662154be65SJarod Wilson 	header.mod_freq_count = be16_to_cpu(tmp16);
4672154be65SJarod Wilson 
4682154be65SJarod Wilson 	memcpy(&tmp16, sig_data + RR3_NUM_PERIOD_OFFSET, sizeof(tmp16));
4692154be65SJarod Wilson 	header.no_periods = be16_to_cpu(tmp16);
4702154be65SJarod Wilson 
4712154be65SJarod Wilson 	header.max_lengths = sig_data[RR3_MAX_LENGTHS_OFFSET];
4722154be65SJarod Wilson 	header.no_lengths = sig_data[RR3_NUM_LENGTHS_OFFSET];
4732154be65SJarod Wilson 
4742154be65SJarod Wilson 	memcpy(&tmp16, sig_data + RR3_MAX_SIGS_OFFSET, sizeof(tmp16));
4752154be65SJarod Wilson 	header.max_sig_size = be16_to_cpu(tmp16);
4762154be65SJarod Wilson 
4772154be65SJarod Wilson 	memcpy(&tmp16, sig_data + RR3_NUM_SIGS_OFFSET, sizeof(tmp16));
4782154be65SJarod Wilson 	header.sig_size = be16_to_cpu(tmp16);
4792154be65SJarod Wilson 
4802154be65SJarod Wilson 	header.no_repeats= sig_data[RR3_REPEATS_OFFSET];
4812154be65SJarod Wilson 
4822154be65SJarod Wilson 	if (debug) {
4832154be65SJarod Wilson 		redrat3_dump_signal_header(&header);
4842154be65SJarod Wilson 		redrat3_dump_signal_data(sig_data, header.sig_size);
4852154be65SJarod Wilson 	}
4862154be65SJarod Wilson 
4872154be65SJarod Wilson 	mod_freq = redrat3_val_to_mod_freq(&header);
4882154be65SJarod Wilson 	rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq);
4892154be65SJarod Wilson 
4902154be65SJarod Wilson 	/* Here we pull out the 'length' values from the signal */
4912154be65SJarod Wilson 	len_vals = (u16 *)(sig_data + RR3_HEADER_LENGTH);
4922154be65SJarod Wilson 
4932154be65SJarod Wilson 	data_vals = sig_data + RR3_HEADER_LENGTH +
4942154be65SJarod Wilson 		    (header.max_lengths * sizeof(u16));
4952154be65SJarod Wilson 
4962154be65SJarod Wilson 	/* process each rr3 encoded byte into an int */
4972154be65SJarod Wilson 	for (i = 0; i < header.sig_size; i++) {
4982154be65SJarod Wilson 		u16 val = len_vals[data_vals[i]];
4992154be65SJarod Wilson 		single_len = redrat3_len_to_us((u32)be16_to_cpu(val));
5002154be65SJarod Wilson 
5012154be65SJarod Wilson 		/* we should always get pulse/space/pulse/space samples */
5022154be65SJarod Wilson 		if (i % 2)
5032154be65SJarod Wilson 			rawir.pulse = false;
5042154be65SJarod Wilson 		else
5052154be65SJarod Wilson 			rawir.pulse = true;
5062154be65SJarod Wilson 
5072154be65SJarod Wilson 		rawir.duration = US_TO_NS(single_len);
508*c53f9f00SJarod Wilson 		/* Save initial pulse length to fudge trailer */
509*c53f9f00SJarod Wilson 		if (i == 0)
510*c53f9f00SJarod Wilson 			trailer = rawir.duration;
5112c594ffaSJarod Wilson 		/* cap the value to IR_MAX_DURATION */
5122c594ffaSJarod Wilson 		rawir.duration &= IR_MAX_DURATION;
5132c594ffaSJarod Wilson 
5142154be65SJarod Wilson 		rr3_dbg(dev, "storing %s with duration %d (i: %d)\n",
5152154be65SJarod Wilson 			rawir.pulse ? "pulse" : "space", rawir.duration, i);
5162154be65SJarod Wilson 		ir_raw_event_store_with_filter(rr3->rc, &rawir);
5172154be65SJarod Wilson 	}
5182154be65SJarod Wilson 
5192154be65SJarod Wilson 	/* add a trailing space, if need be */
5202154be65SJarod Wilson 	if (i % 2) {
5212154be65SJarod Wilson 		rawir.pulse = false;
5222154be65SJarod Wilson 		/* this duration is made up, and may not be ideal... */
523*c53f9f00SJarod Wilson 		if (trailer < US_TO_NS(1000))
524*c53f9f00SJarod Wilson 			rawir.duration = US_TO_NS(2800);
525*c53f9f00SJarod Wilson 		else
526*c53f9f00SJarod Wilson 			rawir.duration = trailer;
5272154be65SJarod Wilson 		rr3_dbg(dev, "storing trailing space with duration %d\n",
5282154be65SJarod Wilson 			rawir.duration);
5292154be65SJarod Wilson 		ir_raw_event_store_with_filter(rr3->rc, &rawir);
5302154be65SJarod Wilson 	}
5312154be65SJarod Wilson 
5322154be65SJarod Wilson 	rr3_dbg(dev, "calling ir_raw_event_handle\n");
5332154be65SJarod Wilson 	ir_raw_event_handle(rr3->rc);
5342154be65SJarod Wilson 
5352154be65SJarod Wilson 	return;
5362154be65SJarod Wilson }
5372154be65SJarod Wilson 
5382154be65SJarod Wilson /* Util fn to send rr3 cmds */
5392154be65SJarod Wilson static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
5402154be65SJarod Wilson {
5412154be65SJarod Wilson 	struct usb_device *udev;
5422154be65SJarod Wilson 	u8 *data;
5432154be65SJarod Wilson 	int res;
5442154be65SJarod Wilson 
5452154be65SJarod Wilson 	data = kzalloc(sizeof(u8), GFP_KERNEL);
5462154be65SJarod Wilson 	if (!data)
5472154be65SJarod Wilson 		return -ENOMEM;
5482154be65SJarod Wilson 
5492154be65SJarod Wilson 	udev = rr3->udev;
5502154be65SJarod Wilson 	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
5512154be65SJarod Wilson 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
5522154be65SJarod Wilson 			      0x0000, 0x0000, data, sizeof(u8), HZ * 10);
5532154be65SJarod Wilson 
5542154be65SJarod Wilson 	if (res < 0) {
5552154be65SJarod Wilson 		dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
5562154be65SJarod Wilson 			__func__, res, *data);
5572154be65SJarod Wilson 		res = -EIO;
5582154be65SJarod Wilson 	} else
5592154be65SJarod Wilson 		res = (u8)data[0];
5602154be65SJarod Wilson 
5612154be65SJarod Wilson 	kfree(data);
5622154be65SJarod Wilson 
5632154be65SJarod Wilson 	return res;
5642154be65SJarod Wilson }
5652154be65SJarod Wilson 
5662154be65SJarod Wilson /* Enables the long range detector and starts async receive */
5672154be65SJarod Wilson static int redrat3_enable_detector(struct redrat3_dev *rr3)
5682154be65SJarod Wilson {
5692154be65SJarod Wilson 	struct device *dev = rr3->dev;
5702154be65SJarod Wilson 	u8 ret;
5712154be65SJarod Wilson 
5722154be65SJarod Wilson 	rr3_ftr(dev, "Entering %s\n", __func__);
5732154be65SJarod Wilson 
5742154be65SJarod Wilson 	ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
5752154be65SJarod Wilson 	if (ret != 0)
5762154be65SJarod Wilson 		dev_dbg(dev, "%s: unexpected ret of %d\n",
5772154be65SJarod Wilson 			__func__, ret);
5782154be65SJarod Wilson 
5792154be65SJarod Wilson 	ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
5802154be65SJarod Wilson 	if (ret != 1) {
5812154be65SJarod Wilson 		dev_err(dev, "%s: detector status: %d, should be 1\n",
5822154be65SJarod Wilson 			__func__, ret);
5832154be65SJarod Wilson 		return -EIO;
5842154be65SJarod Wilson 	}
5852154be65SJarod Wilson 
5862154be65SJarod Wilson 	rr3->det_enabled = true;
5872154be65SJarod Wilson 	redrat3_issue_async(rr3);
5882154be65SJarod Wilson 
5892154be65SJarod Wilson 	return 0;
5902154be65SJarod Wilson }
5912154be65SJarod Wilson 
5922154be65SJarod Wilson /* Disables the rr3 long range detector */
5932154be65SJarod Wilson static void redrat3_disable_detector(struct redrat3_dev *rr3)
5942154be65SJarod Wilson {
5952154be65SJarod Wilson 	struct device *dev = rr3->dev;
5962154be65SJarod Wilson 	u8 ret;
5972154be65SJarod Wilson 
5982154be65SJarod Wilson 	rr3_ftr(dev, "Entering %s\n", __func__);
5992154be65SJarod Wilson 
6002154be65SJarod Wilson 	ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3);
6012154be65SJarod Wilson 	if (ret != 0)
6022154be65SJarod Wilson 		dev_err(dev, "%s: failure!\n", __func__);
6032154be65SJarod Wilson 
6042154be65SJarod Wilson 	ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
6052154be65SJarod Wilson 	if (ret != 0)
6062154be65SJarod Wilson 		dev_warn(dev, "%s: detector status: %d, should be 0\n",
6072154be65SJarod Wilson 			 __func__, ret);
6082154be65SJarod Wilson 
6092154be65SJarod Wilson 	rr3->det_enabled = false;
6102154be65SJarod Wilson }
6112154be65SJarod Wilson 
6122154be65SJarod Wilson static inline void redrat3_delete(struct redrat3_dev *rr3,
6132154be65SJarod Wilson 				  struct usb_device *udev)
6142154be65SJarod Wilson {
6152154be65SJarod Wilson 	rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
6162154be65SJarod Wilson 	usb_kill_urb(rr3->read_urb);
6172154be65SJarod Wilson 	usb_kill_urb(rr3->write_urb);
6182154be65SJarod Wilson 
6192154be65SJarod Wilson 	usb_free_urb(rr3->read_urb);
6202154be65SJarod Wilson 	usb_free_urb(rr3->write_urb);
6212154be65SJarod Wilson 
6222154be65SJarod Wilson 	usb_free_coherent(udev, rr3->ep_in->wMaxPacketSize,
6232154be65SJarod Wilson 			  rr3->bulk_in_buf, rr3->dma_in);
6242154be65SJarod Wilson 	usb_free_coherent(udev, rr3->ep_out->wMaxPacketSize,
6252154be65SJarod Wilson 			  rr3->bulk_out_buf, rr3->dma_out);
6262154be65SJarod Wilson 
6272154be65SJarod Wilson 	kfree(rr3);
6282154be65SJarod Wilson }
6292154be65SJarod Wilson 
630*c53f9f00SJarod Wilson static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
6312154be65SJarod Wilson {
6322154be65SJarod Wilson 	u32 *tmp;
633*c53f9f00SJarod Wilson 	u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
6342154be65SJarod Wilson 	int len, ret, pipe;
6352154be65SJarod Wilson 
6362154be65SJarod Wilson 	len = sizeof(*tmp);
6372154be65SJarod Wilson 	tmp = kzalloc(len, GFP_KERNEL);
6382154be65SJarod Wilson 	if (!tmp) {
639*c53f9f00SJarod Wilson 		dev_warn(rr3->dev, "Memory allocation faillure\n");
6402154be65SJarod Wilson 		return timeout;
6412154be65SJarod Wilson 	}
6422154be65SJarod Wilson 
643*c53f9f00SJarod Wilson 	pipe = usb_rcvctrlpipe(rr3->udev, 0);
644*c53f9f00SJarod Wilson 	ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
6452154be65SJarod Wilson 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
6462154be65SJarod Wilson 			      RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
6472154be65SJarod Wilson 	if (ret != len) {
648*c53f9f00SJarod Wilson 		dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
6492154be65SJarod Wilson 		return timeout;
6502154be65SJarod Wilson 	}
6512154be65SJarod Wilson 
652*c53f9f00SJarod Wilson 	timeout = redrat3_len_to_us(be32_to_cpu(*tmp));
6532154be65SJarod Wilson 
654*c53f9f00SJarod Wilson 	rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
6552154be65SJarod Wilson 	return timeout;
6562154be65SJarod Wilson }
6572154be65SJarod Wilson 
6582154be65SJarod Wilson static void redrat3_reset(struct redrat3_dev *rr3)
6592154be65SJarod Wilson {
6602154be65SJarod Wilson 	struct usb_device *udev = rr3->udev;
6612154be65SJarod Wilson 	struct device *dev = rr3->dev;
6622154be65SJarod Wilson 	int rc, rxpipe, txpipe;
6632154be65SJarod Wilson 	u8 *val;
6642154be65SJarod Wilson 	int len = sizeof(u8);
6652154be65SJarod Wilson 
6662154be65SJarod Wilson 	rr3_ftr(dev, "Entering %s\n", __func__);
6672154be65SJarod Wilson 
6682154be65SJarod Wilson 	rxpipe = usb_rcvctrlpipe(udev, 0);
6692154be65SJarod Wilson 	txpipe = usb_sndctrlpipe(udev, 0);
6702154be65SJarod Wilson 
6712154be65SJarod Wilson 	val = kzalloc(len, GFP_KERNEL);
6722154be65SJarod Wilson 	if (!val) {
6732154be65SJarod Wilson 		dev_err(dev, "Memory allocation failure\n");
6742154be65SJarod Wilson 		return;
6752154be65SJarod Wilson 	}
6762154be65SJarod Wilson 
6772154be65SJarod Wilson 	*val = 0x01;
6782154be65SJarod Wilson 	rc = usb_control_msg(udev, rxpipe, RR3_RESET,
6792154be65SJarod Wilson 			     USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
6802154be65SJarod Wilson 			     RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
6812154be65SJarod Wilson 	rr3_dbg(dev, "reset returned 0x%02x\n", rc);
6822154be65SJarod Wilson 
6832154be65SJarod Wilson 	*val = 5;
6842154be65SJarod Wilson 	rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
6852154be65SJarod Wilson 			     USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
6862154be65SJarod Wilson 			     RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
6872154be65SJarod Wilson 	rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
6882154be65SJarod Wilson 
6892154be65SJarod Wilson 	*val = RR3_DRIVER_MAXLENS;
6902154be65SJarod Wilson 	rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
6912154be65SJarod Wilson 			     USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
6922154be65SJarod Wilson 			     RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
6932154be65SJarod Wilson 	rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
6942154be65SJarod Wilson 
6952154be65SJarod Wilson 	kfree(val);
6962154be65SJarod Wilson }
6972154be65SJarod Wilson 
6982154be65SJarod Wilson static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
6992154be65SJarod Wilson {
7002154be65SJarod Wilson 	int rc = 0;
7012154be65SJarod Wilson 	char *buffer;
7022154be65SJarod Wilson 
7032154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entering %s\n", __func__);
7042154be65SJarod Wilson 
7052154be65SJarod Wilson 	buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
7062154be65SJarod Wilson 	if (!buffer) {
7072154be65SJarod Wilson 		dev_err(rr3->dev, "Memory allocation failure\n");
7082154be65SJarod Wilson 		return;
7092154be65SJarod Wilson 	}
7102154be65SJarod Wilson 
7112154be65SJarod Wilson 	rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
7122154be65SJarod Wilson 			     RR3_FW_VERSION,
7132154be65SJarod Wilson 			     USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
7142154be65SJarod Wilson 			     0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
7152154be65SJarod Wilson 
7162154be65SJarod Wilson 	if (rc >= 0)
7172154be65SJarod Wilson 		dev_info(rr3->dev, "Firmware rev: %s", buffer);
7182154be65SJarod Wilson 	else
7192154be65SJarod Wilson 		dev_err(rr3->dev, "Problem fetching firmware ID\n");
7202154be65SJarod Wilson 
7212154be65SJarod Wilson 	kfree(buffer);
7222154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
7232154be65SJarod Wilson }
7242154be65SJarod Wilson 
7252154be65SJarod Wilson static void redrat3_read_packet_start(struct redrat3_dev *rr3, int len)
7262154be65SJarod Wilson {
7272154be65SJarod Wilson 	u16 tx_error;
7282154be65SJarod Wilson 	u16 hdrlen;
7292154be65SJarod Wilson 
7302154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entering %s\n", __func__);
7312154be65SJarod Wilson 
7322154be65SJarod Wilson 	/* grab the Length and type of transfer */
7332154be65SJarod Wilson 	memcpy(&(rr3->pktlen), (unsigned char *) rr3->bulk_in_buf,
7342154be65SJarod Wilson 	       sizeof(rr3->pktlen));
7352154be65SJarod Wilson 	memcpy(&(rr3->pkttype), ((unsigned char *) rr3->bulk_in_buf +
7362154be65SJarod Wilson 		sizeof(rr3->pktlen)),
7372154be65SJarod Wilson 	       sizeof(rr3->pkttype));
7382154be65SJarod Wilson 
7392154be65SJarod Wilson 	/*data needs conversion to know what its real values are*/
7402154be65SJarod Wilson 	rr3->pktlen = be16_to_cpu(rr3->pktlen);
7412154be65SJarod Wilson 	rr3->pkttype = be16_to_cpu(rr3->pkttype);
7422154be65SJarod Wilson 
7432154be65SJarod Wilson 	switch (rr3->pkttype) {
7442154be65SJarod Wilson 	case RR3_ERROR:
7452154be65SJarod Wilson 		memcpy(&tx_error, ((unsigned char *)rr3->bulk_in_buf
7462154be65SJarod Wilson 			+ (sizeof(rr3->pktlen) + sizeof(rr3->pkttype))),
7472154be65SJarod Wilson 		       sizeof(tx_error));
7482154be65SJarod Wilson 		tx_error = be16_to_cpu(tx_error);
7492154be65SJarod Wilson 		redrat3_dump_fw_error(rr3, tx_error);
7502154be65SJarod Wilson 		break;
7512154be65SJarod Wilson 
7522154be65SJarod Wilson 	case RR3_MOD_SIGNAL_IN:
7532154be65SJarod Wilson 		hdrlen = sizeof(rr3->pktlen) + sizeof(rr3->pkttype);
7542154be65SJarod Wilson 		rr3->bytes_read = len;
7552154be65SJarod Wilson 		rr3->bytes_read -= hdrlen;
7562154be65SJarod Wilson 		rr3->datap = &(rr3->pbuf[0]);
7572154be65SJarod Wilson 
7582154be65SJarod Wilson 		memcpy(rr3->datap, ((unsigned char *)rr3->bulk_in_buf + hdrlen),
7592154be65SJarod Wilson 		       rr3->bytes_read);
7602154be65SJarod Wilson 		rr3->datap += rr3->bytes_read;
7612154be65SJarod Wilson 		rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
7622154be65SJarod Wilson 			rr3->bytes_read, rr3->pktlen);
7632154be65SJarod Wilson 		break;
7642154be65SJarod Wilson 
7652154be65SJarod Wilson 	default:
7662154be65SJarod Wilson 		rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, "
7672154be65SJarod Wilson 			"len of %d, 0x%02x\n", rr3->pkttype, len, rr3->pktlen);
7682154be65SJarod Wilson 		break;
7692154be65SJarod Wilson 	}
7702154be65SJarod Wilson }
7712154be65SJarod Wilson 
7722154be65SJarod Wilson static void redrat3_read_packet_continue(struct redrat3_dev *rr3, int len)
7732154be65SJarod Wilson {
7742154be65SJarod Wilson 
7752154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entering %s\n", __func__);
7762154be65SJarod Wilson 
7772154be65SJarod Wilson 	memcpy(rr3->datap, (unsigned char *)rr3->bulk_in_buf, len);
7782154be65SJarod Wilson 	rr3->datap += len;
7792154be65SJarod Wilson 
7802154be65SJarod Wilson 	rr3->bytes_read += len;
7812154be65SJarod Wilson 	rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
7822154be65SJarod Wilson 		rr3->bytes_read, rr3->pktlen);
7832154be65SJarod Wilson }
7842154be65SJarod Wilson 
7852154be65SJarod Wilson /* gather IR data from incoming urb, process it when we have enough */
7862154be65SJarod Wilson static int redrat3_get_ir_data(struct redrat3_dev *rr3, int len)
7872154be65SJarod Wilson {
7882154be65SJarod Wilson 	struct device *dev = rr3->dev;
7892154be65SJarod Wilson 	int ret = 0;
7902154be65SJarod Wilson 
7912154be65SJarod Wilson 	rr3_ftr(dev, "Entering %s\n", __func__);
7922154be65SJarod Wilson 
7932154be65SJarod Wilson 	if (rr3->pktlen > RR3_MAX_BUF_SIZE) {
7942154be65SJarod Wilson 		dev_err(rr3->dev, "error: packet larger than buffer\n");
7952154be65SJarod Wilson 		ret = -EINVAL;
7962154be65SJarod Wilson 		goto out;
7972154be65SJarod Wilson 	}
7982154be65SJarod Wilson 
7992154be65SJarod Wilson 	if ((rr3->bytes_read == 0) &&
8002154be65SJarod Wilson 	    (len >= (sizeof(rr3->pkttype) + sizeof(rr3->pktlen)))) {
8012154be65SJarod Wilson 		redrat3_read_packet_start(rr3, len);
8022154be65SJarod Wilson 	} else if (rr3->bytes_read != 0) {
8032154be65SJarod Wilson 		redrat3_read_packet_continue(rr3, len);
8042154be65SJarod Wilson 	} else if (rr3->bytes_read == 0) {
8052154be65SJarod Wilson 		dev_err(dev, "error: no packet data read\n");
8062154be65SJarod Wilson 		ret = -ENODATA;
8072154be65SJarod Wilson 		goto out;
8082154be65SJarod Wilson 	}
8092154be65SJarod Wilson 
8102154be65SJarod Wilson 	if (rr3->bytes_read > rr3->pktlen) {
8112154be65SJarod Wilson 		dev_err(dev, "bytes_read (%d) greater than pktlen (%d)\n",
8122154be65SJarod Wilson 			rr3->bytes_read, rr3->pktlen);
8132154be65SJarod Wilson 		ret = -EINVAL;
8142154be65SJarod Wilson 		goto out;
8152154be65SJarod Wilson 	} else if (rr3->bytes_read < rr3->pktlen)
8162154be65SJarod Wilson 		/* we're still accumulating data */
8172154be65SJarod Wilson 		return 0;
8182154be65SJarod Wilson 
8192154be65SJarod Wilson 	/* if we get here, we've got IR data to decode */
8202154be65SJarod Wilson 	if (rr3->pkttype == RR3_MOD_SIGNAL_IN)
8212154be65SJarod Wilson 		redrat3_process_ir_data(rr3);
8222154be65SJarod Wilson 	else
8232154be65SJarod Wilson 		rr3_dbg(dev, "discarding non-signal data packet "
8242154be65SJarod Wilson 			"(type 0x%02x)\n", rr3->pkttype);
8252154be65SJarod Wilson 
8262154be65SJarod Wilson out:
8272154be65SJarod Wilson 	rr3->bytes_read = 0;
8282154be65SJarod Wilson 	rr3->pktlen = 0;
8292154be65SJarod Wilson 	rr3->pkttype = 0;
8302154be65SJarod Wilson 	return ret;
8312154be65SJarod Wilson }
8322154be65SJarod Wilson 
8332154be65SJarod Wilson /* callback function from USB when async USB request has completed */
8342154be65SJarod Wilson static void redrat3_handle_async(struct urb *urb, struct pt_regs *regs)
8352154be65SJarod Wilson {
8362154be65SJarod Wilson 	struct redrat3_dev *rr3;
8372154be65SJarod Wilson 
8382154be65SJarod Wilson 	if (!urb)
8392154be65SJarod Wilson 		return;
8402154be65SJarod Wilson 
8412154be65SJarod Wilson 	rr3 = urb->context;
8422154be65SJarod Wilson 	if (!rr3) {
8432154be65SJarod Wilson 		pr_err("%s called with invalid context!\n", __func__);
8442154be65SJarod Wilson 		usb_unlink_urb(urb);
8452154be65SJarod Wilson 		return;
8462154be65SJarod Wilson 	}
8472154be65SJarod Wilson 
8482154be65SJarod Wilson 	rr3_ftr(rr3->dev, "Entering %s\n", __func__);
8492154be65SJarod Wilson 
8502154be65SJarod Wilson 	if (!rr3->det_enabled) {
8512154be65SJarod Wilson 		rr3_dbg(rr3->dev, "received a read callback but detector "
8522154be65SJarod Wilson 			"disabled - ignoring\n");
8532154be65SJarod Wilson 		return;
8542154be65SJarod Wilson 	}
8552154be65SJarod Wilson 
8562154be65SJarod Wilson 	switch (urb->status) {
8572154be65SJarod Wilson 	case 0:
8582154be65SJarod Wilson 		redrat3_get_ir_data(rr3, urb->actual_length);
8592154be65SJarod Wilson 		break;
8602154be65SJarod Wilson 
8612154be65SJarod Wilson 	case -ECONNRESET:
8622154be65SJarod Wilson 	case -ENOENT:
8632154be65SJarod Wilson 	case -ESHUTDOWN:
8642154be65SJarod Wilson 		usb_unlink_urb(urb);
8652154be65SJarod Wilson 		return;
8662154be65SJarod Wilson 
8672154be65SJarod Wilson 	case -EPIPE:
8682154be65SJarod Wilson 	default:
8692154be65SJarod Wilson 		dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
8702154be65SJarod Wilson 		rr3->bytes_read = 0;
8712154be65SJarod Wilson 		rr3->pktlen = 0;
8722154be65SJarod Wilson 		rr3->pkttype = 0;
8732154be65SJarod Wilson 		break;
8742154be65SJarod Wilson 	}
8752154be65SJarod Wilson 
8762154be65SJarod Wilson 	if (!rr3->transmitting)
8772154be65SJarod Wilson 		redrat3_issue_async(rr3);
8782154be65SJarod Wilson 	else
8792154be65SJarod Wilson 		rr3_dbg(rr3->dev, "IR transmit in progress\n");
8802154be65SJarod Wilson }
8812154be65SJarod Wilson 
8822154be65SJarod Wilson static void redrat3_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
8832154be65SJarod Wilson {
8842154be65SJarod Wilson 	struct redrat3_dev *rr3;
8852154be65SJarod Wilson 	int len;
8862154be65SJarod Wilson 
8872154be65SJarod Wilson 	if (!urb)
8882154be65SJarod Wilson 		return;
8892154be65SJarod Wilson 
8902154be65SJarod Wilson 	rr3 = urb->context;
8912154be65SJarod Wilson 	if (rr3) {
8922154be65SJarod Wilson 		len = urb->actual_length;
8932154be65SJarod Wilson 		rr3_ftr(rr3->dev, "%s: called (status=%d len=%d)\n",
8942154be65SJarod Wilson 			__func__, urb->status, len);
8952154be65SJarod Wilson 	}
8962154be65SJarod Wilson }
8972154be65SJarod Wilson 
8982154be65SJarod Wilson static u16 mod_freq_to_val(unsigned int mod_freq)
8992154be65SJarod Wilson {
9002154be65SJarod Wilson 	int mult = 6000000;
9012154be65SJarod Wilson 
9022154be65SJarod Wilson 	/* Clk used in mod. freq. generation is CLK24/4. */
9032154be65SJarod Wilson 	return (u16)(65536 - (mult / mod_freq));
9042154be65SJarod Wilson }
9052154be65SJarod Wilson 
9062154be65SJarod Wilson static int redrat3_set_tx_carrier(struct rc_dev *dev, u32 carrier)
9072154be65SJarod Wilson {
9082154be65SJarod Wilson 	struct redrat3_dev *rr3 = dev->priv;
9092154be65SJarod Wilson 
9102154be65SJarod Wilson 	rr3->carrier = carrier;
9112154be65SJarod Wilson 
9122154be65SJarod Wilson 	return carrier;
9132154be65SJarod Wilson }
9142154be65SJarod Wilson 
9152154be65SJarod Wilson static int redrat3_transmit_ir(struct rc_dev *rcdev, int *txbuf, u32 n)
9162154be65SJarod Wilson {
9172154be65SJarod Wilson 	struct redrat3_dev *rr3 = rcdev->priv;
9182154be65SJarod Wilson 	struct device *dev = rr3->dev;
9192154be65SJarod Wilson 	struct redrat3_signal_header header;
9202154be65SJarod Wilson 	int i, j, count, ret, ret_len, offset;
9212154be65SJarod Wilson 	int lencheck, cur_sample_len, pipe;
9222154be65SJarod Wilson 	char *buffer = NULL, *sigdata = NULL;
9232154be65SJarod Wilson 	int *sample_lens = NULL;
9242154be65SJarod Wilson 	u32 tmpi;
9252154be65SJarod Wilson 	u16 tmps;
9262154be65SJarod Wilson 	u8 *datap;
9272154be65SJarod Wilson 	u8 curlencheck = 0;
9282154be65SJarod Wilson 	u16 *lengths_ptr;
9292154be65SJarod Wilson 	int sendbuf_len;
9302154be65SJarod Wilson 
9312154be65SJarod Wilson 	rr3_ftr(dev, "Entering %s\n", __func__);
9322154be65SJarod Wilson 
9332154be65SJarod Wilson 	if (rr3->transmitting) {
9342154be65SJarod Wilson 		dev_warn(dev, "%s: transmitter already in use\n", __func__);
9352154be65SJarod Wilson 		return -EAGAIN;
9362154be65SJarod Wilson 	}
9372154be65SJarod Wilson 
9382154be65SJarod Wilson 	count = n / sizeof(int);
9392154be65SJarod Wilson 	if (count > (RR3_DRIVER_MAXLENS * 2))
9402154be65SJarod Wilson 		return -EINVAL;
9412154be65SJarod Wilson 
9422154be65SJarod Wilson 	rr3->transmitting = true;
9432154be65SJarod Wilson 
9442154be65SJarod Wilson 	redrat3_disable_detector(rr3);
9452154be65SJarod Wilson 
9462154be65SJarod Wilson 	if (rr3->det_enabled) {
9472154be65SJarod Wilson 		dev_err(dev, "%s: cannot tx while rx is enabled\n", __func__);
9482154be65SJarod Wilson 		ret = -EIO;
9492154be65SJarod Wilson 		goto out;
9502154be65SJarod Wilson 	}
9512154be65SJarod Wilson 
9522154be65SJarod Wilson 	sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
9532154be65SJarod Wilson 	if (!sample_lens) {
9542154be65SJarod Wilson 		ret = -ENOMEM;
9552154be65SJarod Wilson 		goto out;
9562154be65SJarod Wilson 	}
9572154be65SJarod Wilson 
9582154be65SJarod Wilson 	for (i = 0; i < count; i++) {
9592154be65SJarod Wilson 		for (lencheck = 0; lencheck < curlencheck; lencheck++) {
9602154be65SJarod Wilson 			cur_sample_len = redrat3_us_to_len(txbuf[i]);
9612154be65SJarod Wilson 			if (sample_lens[lencheck] == cur_sample_len)
9622154be65SJarod Wilson 				break;
9632154be65SJarod Wilson 		}
9642154be65SJarod Wilson 		if (lencheck == curlencheck) {
9652154be65SJarod Wilson 			cur_sample_len = redrat3_us_to_len(txbuf[i]);
9662154be65SJarod Wilson 			rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
9672154be65SJarod Wilson 				i, txbuf[i], curlencheck, cur_sample_len);
9682154be65SJarod Wilson 			if (curlencheck < 255) {
9692154be65SJarod Wilson 				/* now convert the value to a proper
9702154be65SJarod Wilson 				 * rr3 value.. */
9712154be65SJarod Wilson 				sample_lens[curlencheck] = cur_sample_len;
9722154be65SJarod Wilson 				curlencheck++;
9732154be65SJarod Wilson 			} else {
9742154be65SJarod Wilson 				dev_err(dev, "signal too long\n");
9752154be65SJarod Wilson 				ret = -EINVAL;
9762154be65SJarod Wilson 				goto out;
9772154be65SJarod Wilson 			}
9782154be65SJarod Wilson 		}
9792154be65SJarod Wilson 	}
9802154be65SJarod Wilson 
9812154be65SJarod Wilson 	sigdata = kzalloc((count + RR3_TX_TRAILER_LEN), GFP_KERNEL);
9822154be65SJarod Wilson 	if (!sigdata) {
9832154be65SJarod Wilson 		ret = -ENOMEM;
9842154be65SJarod Wilson 		goto out;
9852154be65SJarod Wilson 	}
9862154be65SJarod Wilson 
9872154be65SJarod Wilson 	sigdata[count] = RR3_END_OF_SIGNAL;
9882154be65SJarod Wilson 	sigdata[count + 1] = RR3_END_OF_SIGNAL;
9892154be65SJarod Wilson 	for (i = 0; i < count; i++) {
9902154be65SJarod Wilson 		for (j = 0; j < curlencheck; j++) {
9912154be65SJarod Wilson 			if (sample_lens[j] == redrat3_us_to_len(txbuf[i]))
9922154be65SJarod Wilson 				sigdata[i] = j;
9932154be65SJarod Wilson 		}
9942154be65SJarod Wilson 	}
9952154be65SJarod Wilson 
9962154be65SJarod Wilson 	offset = RR3_TX_HEADER_OFFSET;
9972154be65SJarod Wilson 	sendbuf_len = RR3_HEADER_LENGTH + (sizeof(u16) * RR3_DRIVER_MAXLENS)
9982154be65SJarod Wilson 			+ count + RR3_TX_TRAILER_LEN + offset;
9992154be65SJarod Wilson 
10002154be65SJarod Wilson 	buffer = kzalloc(sendbuf_len, GFP_KERNEL);
10012154be65SJarod Wilson 	if (!buffer) {
10022154be65SJarod Wilson 		ret = -ENOMEM;
10032154be65SJarod Wilson 		goto out;
10042154be65SJarod Wilson 	}
10052154be65SJarod Wilson 
10062154be65SJarod Wilson 	/* fill in our packet header */
10072154be65SJarod Wilson 	header.length = sendbuf_len - offset;
10082154be65SJarod Wilson 	header.transfer_type = RR3_MOD_SIGNAL_OUT;
10092154be65SJarod Wilson 	header.pause = redrat3_len_to_us(100);
10102154be65SJarod Wilson 	header.mod_freq_count = mod_freq_to_val(rr3->carrier);
10112154be65SJarod Wilson 	header.no_periods = 0; /* n/a to transmit */
10122154be65SJarod Wilson 	header.max_lengths = RR3_DRIVER_MAXLENS;
10132154be65SJarod Wilson 	header.no_lengths = curlencheck;
10142154be65SJarod Wilson 	header.max_sig_size = RR3_MAX_SIG_SIZE;
10152154be65SJarod Wilson 	header.sig_size = count + RR3_TX_TRAILER_LEN;
10162154be65SJarod Wilson 	/* we currently rely on repeat handling in the IR encoding source */
10172154be65SJarod Wilson 	header.no_repeats = 0;
10182154be65SJarod Wilson 
10192154be65SJarod Wilson 	tmps = cpu_to_be16(header.length);
10202154be65SJarod Wilson 	memcpy(buffer, &tmps, 2);
10212154be65SJarod Wilson 
10222154be65SJarod Wilson 	tmps = cpu_to_be16(header.transfer_type);
10232154be65SJarod Wilson 	memcpy(buffer + 2, &tmps, 2);
10242154be65SJarod Wilson 
10252154be65SJarod Wilson 	tmpi = cpu_to_be32(header.pause);
10262154be65SJarod Wilson 	memcpy(buffer + offset, &tmpi, sizeof(tmpi));
10272154be65SJarod Wilson 
10282154be65SJarod Wilson 	tmps = cpu_to_be16(header.mod_freq_count);
10292154be65SJarod Wilson 	memcpy(buffer + offset + RR3_FREQ_COUNT_OFFSET, &tmps, 2);
10302154be65SJarod Wilson 
10312154be65SJarod Wilson 	buffer[offset + RR3_NUM_LENGTHS_OFFSET] = header.no_lengths;
10322154be65SJarod Wilson 
10332154be65SJarod Wilson 	tmps = cpu_to_be16(header.sig_size);
10342154be65SJarod Wilson 	memcpy(buffer + offset + RR3_NUM_SIGS_OFFSET, &tmps, 2);
10352154be65SJarod Wilson 
10362154be65SJarod Wilson 	buffer[offset + RR3_REPEATS_OFFSET] = header.no_repeats;
10372154be65SJarod Wilson 
10382154be65SJarod Wilson 	lengths_ptr = (u16 *)(buffer + offset + RR3_HEADER_LENGTH);
10392154be65SJarod Wilson 	for (i = 0; i < curlencheck; ++i)
10402154be65SJarod Wilson 		lengths_ptr[i] = cpu_to_be16(sample_lens[i]);
10412154be65SJarod Wilson 
10422154be65SJarod Wilson 	datap = (u8 *)(buffer + offset + RR3_HEADER_LENGTH +
10432154be65SJarod Wilson 			    (sizeof(u16) * RR3_DRIVER_MAXLENS));
10442154be65SJarod Wilson 	memcpy(datap, sigdata, (count + RR3_TX_TRAILER_LEN));
10452154be65SJarod Wilson 
10462154be65SJarod Wilson 	if (debug) {
10472154be65SJarod Wilson 		redrat3_dump_signal_header(&header);
10482154be65SJarod Wilson 		redrat3_dump_signal_data(buffer, header.sig_size);
10492154be65SJarod Wilson 	}
10502154be65SJarod Wilson 
10512154be65SJarod Wilson 	pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
10522154be65SJarod Wilson 	tmps = usb_bulk_msg(rr3->udev, pipe, buffer,
10532154be65SJarod Wilson 			    sendbuf_len, &ret_len, 10 * HZ);
10542154be65SJarod Wilson 	rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, tmps);
10552154be65SJarod Wilson 
10562154be65SJarod Wilson 	/* now tell the hardware to transmit what we sent it */
10572154be65SJarod Wilson 	pipe = usb_rcvctrlpipe(rr3->udev, 0);
10582154be65SJarod Wilson 	ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
10592154be65SJarod Wilson 			      USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
10602154be65SJarod Wilson 			      0, 0, buffer, 2, HZ * 10);
10612154be65SJarod Wilson 
10622154be65SJarod Wilson 	if (ret < 0)
10632154be65SJarod Wilson 		dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
10642154be65SJarod Wilson 	else
10652154be65SJarod Wilson 		ret = n;
10662154be65SJarod Wilson 
10672154be65SJarod Wilson out:
10682154be65SJarod Wilson 	kfree(sample_lens);
10692154be65SJarod Wilson 	kfree(buffer);
10702154be65SJarod Wilson 	kfree(sigdata);
10712154be65SJarod Wilson 
10722154be65SJarod Wilson 	rr3->transmitting = false;
10732154be65SJarod Wilson 
10742154be65SJarod Wilson 	redrat3_enable_detector(rr3);
10752154be65SJarod Wilson 
10762154be65SJarod Wilson 	return ret;
10772154be65SJarod Wilson }
10782154be65SJarod Wilson 
10792154be65SJarod Wilson static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
10802154be65SJarod Wilson {
10812154be65SJarod Wilson 	struct device *dev = rr3->dev;
10822154be65SJarod Wilson 	struct rc_dev *rc;
10832154be65SJarod Wilson 	int ret = -ENODEV;
10842154be65SJarod Wilson 	u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
10852154be65SJarod Wilson 
10862154be65SJarod Wilson 	rc = rc_allocate_device();
10872154be65SJarod Wilson 	if (!rc) {
10882154be65SJarod Wilson 		dev_err(dev, "remote input dev allocation failed\n");
10892154be65SJarod Wilson 		goto out;
10902154be65SJarod Wilson 	}
10912154be65SJarod Wilson 
10922154be65SJarod Wilson 	snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
10932154be65SJarod Wilson 		 "Infrared Remote Transceiver (%04x:%04x)",
10942154be65SJarod Wilson 		 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
10952154be65SJarod Wilson 		 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
10962154be65SJarod Wilson 
10972154be65SJarod Wilson 	usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
10982154be65SJarod Wilson 
10992154be65SJarod Wilson 	rc->input_name = rr3->name;
11002154be65SJarod Wilson 	rc->input_phys = rr3->phys;
11012154be65SJarod Wilson 	usb_to_input_id(rr3->udev, &rc->input_id);
11022154be65SJarod Wilson 	rc->dev.parent = dev;
11032154be65SJarod Wilson 	rc->priv = rr3;
11042154be65SJarod Wilson 	rc->driver_type = RC_DRIVER_IR_RAW;
11052154be65SJarod Wilson 	rc->allowed_protos = RC_TYPE_ALL;
1106*c53f9f00SJarod Wilson 	rc->timeout = US_TO_NS(2750);
11072154be65SJarod Wilson 	rc->tx_ir = redrat3_transmit_ir;
11082154be65SJarod Wilson 	rc->s_tx_carrier = redrat3_set_tx_carrier;
11092154be65SJarod Wilson 	rc->driver_name = DRIVER_NAME;
11102154be65SJarod Wilson 	rc->map_name = RC_MAP_HAUPPAUGE;
11112154be65SJarod Wilson 
11122154be65SJarod Wilson 	ret = rc_register_device(rc);
11132154be65SJarod Wilson 	if (ret < 0) {
11142154be65SJarod Wilson 		dev_err(dev, "remote dev registration failed\n");
11152154be65SJarod Wilson 		goto out;
11162154be65SJarod Wilson 	}
11172154be65SJarod Wilson 
11182154be65SJarod Wilson 	return rc;
11192154be65SJarod Wilson 
11202154be65SJarod Wilson out:
11212154be65SJarod Wilson 	rc_free_device(rc);
11222154be65SJarod Wilson 	return NULL;
11232154be65SJarod Wilson }
11242154be65SJarod Wilson 
11252154be65SJarod Wilson static int __devinit redrat3_dev_probe(struct usb_interface *intf,
11262154be65SJarod Wilson 				       const struct usb_device_id *id)
11272154be65SJarod Wilson {
11282154be65SJarod Wilson 	struct usb_device *udev = interface_to_usbdev(intf);
11292154be65SJarod Wilson 	struct device *dev = &intf->dev;
11302154be65SJarod Wilson 	struct usb_host_interface *uhi;
11312154be65SJarod Wilson 	struct redrat3_dev *rr3;
11322154be65SJarod Wilson 	struct usb_endpoint_descriptor *ep;
11332154be65SJarod Wilson 	struct usb_endpoint_descriptor *ep_in = NULL;
11342154be65SJarod Wilson 	struct usb_endpoint_descriptor *ep_out = NULL;
11352154be65SJarod Wilson 	u8 addr, attrs;
11362154be65SJarod Wilson 	int pipe, i;
11372154be65SJarod Wilson 	int retval = -ENOMEM;
11382154be65SJarod Wilson 
11392154be65SJarod Wilson 	rr3_ftr(dev, "%s called\n", __func__);
11402154be65SJarod Wilson 
11412154be65SJarod Wilson 	uhi = intf->cur_altsetting;
11422154be65SJarod Wilson 
11432154be65SJarod Wilson 	/* find our bulk-in and bulk-out endpoints */
11442154be65SJarod Wilson 	for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
11452154be65SJarod Wilson 		ep = &uhi->endpoint[i].desc;
11462154be65SJarod Wilson 		addr = ep->bEndpointAddress;
11472154be65SJarod Wilson 		attrs = ep->bmAttributes;
11482154be65SJarod Wilson 
11492154be65SJarod Wilson 		if ((ep_in == NULL) &&
11502154be65SJarod Wilson 		    ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
11512154be65SJarod Wilson 		    ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
11522154be65SJarod Wilson 		     USB_ENDPOINT_XFER_BULK)) {
11532154be65SJarod Wilson 			rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
11542154be65SJarod Wilson 				ep->bEndpointAddress);
11552154be65SJarod Wilson 			/* data comes in on 0x82, 0x81 is for other data... */
11562154be65SJarod Wilson 			if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
11572154be65SJarod Wilson 				ep_in = ep;
11582154be65SJarod Wilson 		}
11592154be65SJarod Wilson 
11602154be65SJarod Wilson 		if ((ep_out == NULL) &&
11612154be65SJarod Wilson 		    ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
11622154be65SJarod Wilson 		    ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
11632154be65SJarod Wilson 		     USB_ENDPOINT_XFER_BULK)) {
11642154be65SJarod Wilson 			rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
11652154be65SJarod Wilson 				ep->bEndpointAddress);
11662154be65SJarod Wilson 			ep_out = ep;
11672154be65SJarod Wilson 		}
11682154be65SJarod Wilson 	}
11692154be65SJarod Wilson 
11702154be65SJarod Wilson 	if (!ep_in || !ep_out) {
11712154be65SJarod Wilson 		dev_err(dev, "Couldn't find both in and out endpoints\n");
11722154be65SJarod Wilson 		retval = -ENODEV;
11732154be65SJarod Wilson 		goto no_endpoints;
11742154be65SJarod Wilson 	}
11752154be65SJarod Wilson 
11762154be65SJarod Wilson 	/* allocate memory for our device state and initialize it */
11772154be65SJarod Wilson 	rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
11782154be65SJarod Wilson 	if (rr3 == NULL) {
11792154be65SJarod Wilson 		dev_err(dev, "Memory allocation failure\n");
11807eb75715SDan Carpenter 		goto no_endpoints;
11812154be65SJarod Wilson 	}
11822154be65SJarod Wilson 
11832154be65SJarod Wilson 	rr3->dev = &intf->dev;
11842154be65SJarod Wilson 
11852154be65SJarod Wilson 	/* set up bulk-in endpoint */
11862154be65SJarod Wilson 	rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
11872154be65SJarod Wilson 	if (!rr3->read_urb) {
11882154be65SJarod Wilson 		dev_err(dev, "Read urb allocation failure\n");
11892154be65SJarod Wilson 		goto error;
11902154be65SJarod Wilson 	}
11912154be65SJarod Wilson 
11922154be65SJarod Wilson 	rr3->ep_in = ep_in;
11932154be65SJarod Wilson 	rr3->bulk_in_buf = usb_alloc_coherent(udev, ep_in->wMaxPacketSize,
11942154be65SJarod Wilson 					      GFP_ATOMIC, &rr3->dma_in);
11952154be65SJarod Wilson 	if (!rr3->bulk_in_buf) {
11962154be65SJarod Wilson 		dev_err(dev, "Read buffer allocation failure\n");
11972154be65SJarod Wilson 		goto error;
11982154be65SJarod Wilson 	}
11992154be65SJarod Wilson 
12002154be65SJarod Wilson 	pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
12012154be65SJarod Wilson 	usb_fill_bulk_urb(rr3->read_urb, udev, pipe,
12022154be65SJarod Wilson 			  rr3->bulk_in_buf, ep_in->wMaxPacketSize,
12032154be65SJarod Wilson 			  (usb_complete_t)redrat3_handle_async, rr3);
12042154be65SJarod Wilson 
12052154be65SJarod Wilson 	/* set up bulk-out endpoint*/
12062154be65SJarod Wilson 	rr3->write_urb = usb_alloc_urb(0, GFP_KERNEL);
12072154be65SJarod Wilson 	if (!rr3->write_urb) {
12082154be65SJarod Wilson 		dev_err(dev, "Write urb allocation failure\n");
12092154be65SJarod Wilson 		goto error;
12102154be65SJarod Wilson 	}
12112154be65SJarod Wilson 
12122154be65SJarod Wilson 	rr3->ep_out = ep_out;
12132154be65SJarod Wilson 	rr3->bulk_out_buf = usb_alloc_coherent(udev, ep_out->wMaxPacketSize,
12142154be65SJarod Wilson 					       GFP_ATOMIC, &rr3->dma_out);
12152154be65SJarod Wilson 	if (!rr3->bulk_out_buf) {
12162154be65SJarod Wilson 		dev_err(dev, "Write buffer allocation failure\n");
12172154be65SJarod Wilson 		goto error;
12182154be65SJarod Wilson 	}
12192154be65SJarod Wilson 
12202154be65SJarod Wilson 	pipe = usb_sndbulkpipe(udev, ep_out->bEndpointAddress);
12212154be65SJarod Wilson 	usb_fill_bulk_urb(rr3->write_urb, udev, pipe,
12222154be65SJarod Wilson 			  rr3->bulk_out_buf, ep_out->wMaxPacketSize,
12232154be65SJarod Wilson 			  (usb_complete_t)redrat3_write_bulk_callback, rr3);
12242154be65SJarod Wilson 
12252154be65SJarod Wilson 	mutex_init(&rr3->lock);
12262154be65SJarod Wilson 	rr3->udev = udev;
12272154be65SJarod Wilson 
12282154be65SJarod Wilson 	redrat3_reset(rr3);
12292154be65SJarod Wilson 	redrat3_get_firmware_rev(rr3);
12302154be65SJarod Wilson 
12312154be65SJarod Wilson 	/* might be all we need to do? */
12322154be65SJarod Wilson 	retval = redrat3_enable_detector(rr3);
12332154be65SJarod Wilson 	if (retval < 0)
12342154be65SJarod Wilson 		goto error;
12352154be65SJarod Wilson 
1236*c53f9f00SJarod Wilson 	/* store current hardware timeout, in us, will use for kfifo resets */
1237*c53f9f00SJarod Wilson 	rr3->hw_timeout = redrat3_get_timeout(rr3);
1238*c53f9f00SJarod Wilson 
12392154be65SJarod Wilson 	/* default.. will get overridden by any sends with a freq defined */
12402154be65SJarod Wilson 	rr3->carrier = 38000;
12412154be65SJarod Wilson 
12422154be65SJarod Wilson 	rr3->rc = redrat3_init_rc_dev(rr3);
12432154be65SJarod Wilson 	if (!rr3->rc)
12442154be65SJarod Wilson 		goto error;
12452154be65SJarod Wilson 
12462154be65SJarod Wilson 	setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
12472154be65SJarod Wilson 
12482154be65SJarod Wilson 	/* we can register the device now, as it is ready */
12492154be65SJarod Wilson 	usb_set_intfdata(intf, rr3);
12502154be65SJarod Wilson 
12512154be65SJarod Wilson 	rr3_ftr(dev, "Exiting %s\n", __func__);
12522154be65SJarod Wilson 	return 0;
12532154be65SJarod Wilson 
12542154be65SJarod Wilson error:
12552154be65SJarod Wilson 	redrat3_delete(rr3, rr3->udev);
12562154be65SJarod Wilson 
12572154be65SJarod Wilson no_endpoints:
12582154be65SJarod Wilson 	dev_err(dev, "%s: retval = %x", __func__, retval);
12592154be65SJarod Wilson 
12602154be65SJarod Wilson 	return retval;
12612154be65SJarod Wilson }
12622154be65SJarod Wilson 
12632154be65SJarod Wilson static void __devexit redrat3_dev_disconnect(struct usb_interface *intf)
12642154be65SJarod Wilson {
12652154be65SJarod Wilson 	struct usb_device *udev = interface_to_usbdev(intf);
12662154be65SJarod Wilson 	struct redrat3_dev *rr3 = usb_get_intfdata(intf);
12672154be65SJarod Wilson 
12682154be65SJarod Wilson 	rr3_ftr(&intf->dev, "Entering %s\n", __func__);
12692154be65SJarod Wilson 
12702154be65SJarod Wilson 	if (!rr3)
12712154be65SJarod Wilson 		return;
12722154be65SJarod Wilson 
12732154be65SJarod Wilson 	redrat3_disable_detector(rr3);
12742154be65SJarod Wilson 
12752154be65SJarod Wilson 	usb_set_intfdata(intf, NULL);
12762154be65SJarod Wilson 	rc_unregister_device(rr3->rc);
1277*c53f9f00SJarod Wilson 	del_timer_sync(&rr3->rx_timeout);
12782154be65SJarod Wilson 	redrat3_delete(rr3, udev);
12792154be65SJarod Wilson 
12802154be65SJarod Wilson 	rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
12812154be65SJarod Wilson }
12822154be65SJarod Wilson 
12832154be65SJarod Wilson static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
12842154be65SJarod Wilson {
12852154be65SJarod Wilson 	struct redrat3_dev *rr3 = usb_get_intfdata(intf);
12862154be65SJarod Wilson 	rr3_ftr(rr3->dev, "suspend\n");
12872154be65SJarod Wilson 	usb_kill_urb(rr3->read_urb);
12882154be65SJarod Wilson 	return 0;
12892154be65SJarod Wilson }
12902154be65SJarod Wilson 
12912154be65SJarod Wilson static int redrat3_dev_resume(struct usb_interface *intf)
12922154be65SJarod Wilson {
12932154be65SJarod Wilson 	struct redrat3_dev *rr3 = usb_get_intfdata(intf);
12942154be65SJarod Wilson 	rr3_ftr(rr3->dev, "resume\n");
12952154be65SJarod Wilson 	if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
12962154be65SJarod Wilson 		return -EIO;
12972154be65SJarod Wilson 	return 0;
12982154be65SJarod Wilson }
12992154be65SJarod Wilson 
13002154be65SJarod Wilson static struct usb_driver redrat3_dev_driver = {
13012154be65SJarod Wilson 	.name		= DRIVER_NAME,
13022154be65SJarod Wilson 	.probe		= redrat3_dev_probe,
13032154be65SJarod Wilson 	.disconnect	= redrat3_dev_disconnect,
13042154be65SJarod Wilson 	.suspend	= redrat3_dev_suspend,
13052154be65SJarod Wilson 	.resume		= redrat3_dev_resume,
13062154be65SJarod Wilson 	.reset_resume	= redrat3_dev_resume,
13072154be65SJarod Wilson 	.id_table	= redrat3_dev_table
13082154be65SJarod Wilson };
13092154be65SJarod Wilson 
13102154be65SJarod Wilson static int __init redrat3_dev_init(void)
13112154be65SJarod Wilson {
13122154be65SJarod Wilson 	int ret;
13132154be65SJarod Wilson 
13142154be65SJarod Wilson 	ret = usb_register(&redrat3_dev_driver);
13152154be65SJarod Wilson 	if (ret < 0)
13162154be65SJarod Wilson 		pr_err(DRIVER_NAME
13172154be65SJarod Wilson 		       ": usb register failed, result = %d\n", ret);
13182154be65SJarod Wilson 
13192154be65SJarod Wilson 	return ret;
13202154be65SJarod Wilson }
13212154be65SJarod Wilson 
13222154be65SJarod Wilson static void __exit redrat3_dev_exit(void)
13232154be65SJarod Wilson {
13242154be65SJarod Wilson 	usb_deregister(&redrat3_dev_driver);
13252154be65SJarod Wilson }
13262154be65SJarod Wilson 
13272154be65SJarod Wilson module_init(redrat3_dev_init);
13282154be65SJarod Wilson module_exit(redrat3_dev_exit);
13292154be65SJarod Wilson 
13302154be65SJarod Wilson MODULE_DESCRIPTION(DRIVER_DESC);
13312154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR);
13322154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR2);
13332154be65SJarod Wilson MODULE_LICENSE("GPL");
13342154be65SJarod Wilson MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
13352154be65SJarod Wilson 
13362154be65SJarod Wilson module_param(debug, int, S_IRUGO | S_IWUSR);
13372154be65SJarod Wilson MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
13382154be65SJarod Wilson 		 "0x1 = standard debug messages, 0x2 = function tracing debug. "
13392154be65SJarod Wilson 		 "Flag bits are addative (i.e., 0x3 for both debug types).");
1340