18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Streamzap Remote Control driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
68c2ecf20Sopenharmony_ci * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This driver was based on the work of Greg Wickham and Adrian
98c2ecf20Sopenharmony_ci * Dewhurst. It was substantially rewritten to support correct signal
108c2ecf20Sopenharmony_ci * gaps and now maintains a delay buffer, which is used to present
118c2ecf20Sopenharmony_ci * consistent timing behaviour to user space applications. Without the
128c2ecf20Sopenharmony_ci * delay buffer an ugly hack would be required in lircd, which can
138c2ecf20Sopenharmony_ci * cause sluggish signal decoding in certain situations.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Ported to in-kernel ir-core interface by Jarod Wilson
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * This driver is based on the USB skeleton driver packaged with the
188c2ecf20Sopenharmony_ci * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <linux/device.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/ktime.h>
258c2ecf20Sopenharmony_ci#include <linux/usb.h>
268c2ecf20Sopenharmony_ci#include <linux/usb/input.h>
278c2ecf20Sopenharmony_ci#include <media/rc-core.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DRIVER_VERSION	"1.61"
308c2ecf20Sopenharmony_ci#define DRIVER_NAME	"streamzap"
318c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Streamzap Remote Control driver"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define USB_STREAMZAP_VENDOR_ID		0x0e9c
348c2ecf20Sopenharmony_ci#define USB_STREAMZAP_PRODUCT_ID	0x0000
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* table of devices that work with this driver */
378c2ecf20Sopenharmony_cistatic const struct usb_device_id streamzap_table[] = {
388c2ecf20Sopenharmony_ci	/* Streamzap Remote Control */
398c2ecf20Sopenharmony_ci	{ USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
408c2ecf20Sopenharmony_ci	/* Terminating entry */
418c2ecf20Sopenharmony_ci	{ }
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, streamzap_table);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define SZ_PULSE_MASK 0xf0
478c2ecf20Sopenharmony_ci#define SZ_SPACE_MASK 0x0f
488c2ecf20Sopenharmony_ci#define SZ_TIMEOUT    0xff
498c2ecf20Sopenharmony_ci#define SZ_RESOLUTION 256
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* number of samples buffered */
528c2ecf20Sopenharmony_ci#define SZ_BUF_LEN 128
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cienum StreamzapDecoderState {
558c2ecf20Sopenharmony_ci	PulseSpace,
568c2ecf20Sopenharmony_ci	FullPulse,
578c2ecf20Sopenharmony_ci	FullSpace,
588c2ecf20Sopenharmony_ci	IgnorePulse
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* structure to hold our device specific stuff */
628c2ecf20Sopenharmony_cistruct streamzap_ir {
638c2ecf20Sopenharmony_ci	/* ir-core */
648c2ecf20Sopenharmony_ci	struct rc_dev *rdev;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/* core device info */
678c2ecf20Sopenharmony_ci	struct device *dev;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/* usb */
708c2ecf20Sopenharmony_ci	struct usb_device	*usbdev;
718c2ecf20Sopenharmony_ci	struct usb_interface	*interface;
728c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *endpoint;
738c2ecf20Sopenharmony_ci	struct urb		*urb_in;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* buffer & dma */
768c2ecf20Sopenharmony_ci	unsigned char		*buf_in;
778c2ecf20Sopenharmony_ci	dma_addr_t		dma_in;
788c2ecf20Sopenharmony_ci	unsigned int		buf_in_len;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* track what state we're in */
818c2ecf20Sopenharmony_ci	enum StreamzapDecoderState decoder_state;
828c2ecf20Sopenharmony_ci	/* tracks whether we are currently receiving some signal */
838c2ecf20Sopenharmony_ci	bool			idle;
848c2ecf20Sopenharmony_ci	/* sum of signal lengths received since signal start */
858c2ecf20Sopenharmony_ci	unsigned long		sum;
868c2ecf20Sopenharmony_ci	/* start time of signal; necessary for gap tracking */
878c2ecf20Sopenharmony_ci	ktime_t			signal_last;
888c2ecf20Sopenharmony_ci	ktime_t			signal_start;
898c2ecf20Sopenharmony_ci	bool			timeout_enabled;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	char			name[128];
928c2ecf20Sopenharmony_ci	char			phys[64];
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* local function prototypes */
978c2ecf20Sopenharmony_cistatic int streamzap_probe(struct usb_interface *interface,
988c2ecf20Sopenharmony_ci			   const struct usb_device_id *id);
998c2ecf20Sopenharmony_cistatic void streamzap_disconnect(struct usb_interface *interface);
1008c2ecf20Sopenharmony_cistatic void streamzap_callback(struct urb *urb);
1018c2ecf20Sopenharmony_cistatic int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
1028c2ecf20Sopenharmony_cistatic int streamzap_resume(struct usb_interface *intf);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* usb specific object needed to register this driver with the usb subsystem */
1058c2ecf20Sopenharmony_cistatic struct usb_driver streamzap_driver = {
1068c2ecf20Sopenharmony_ci	.name =		DRIVER_NAME,
1078c2ecf20Sopenharmony_ci	.probe =	streamzap_probe,
1088c2ecf20Sopenharmony_ci	.disconnect =	streamzap_disconnect,
1098c2ecf20Sopenharmony_ci	.suspend =	streamzap_suspend,
1108c2ecf20Sopenharmony_ci	.resume =	streamzap_resume,
1118c2ecf20Sopenharmony_ci	.id_table =	streamzap_table,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	dev_dbg(sz->dev, "Storing %s with duration %u us\n",
1178c2ecf20Sopenharmony_ci		(rawir.pulse ? "pulse" : "space"), rawir.duration);
1188c2ecf20Sopenharmony_ci	ir_raw_event_store_with_filter(sz->rdev, &rawir);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void sz_push_full_pulse(struct streamzap_ir *sz,
1228c2ecf20Sopenharmony_ci			       unsigned char value)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct ir_raw_event rawir = {};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (sz->idle) {
1278c2ecf20Sopenharmony_ci		int delta;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		sz->signal_last = sz->signal_start;
1308c2ecf20Sopenharmony_ci		sz->signal_start = ktime_get_real();
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci		delta = ktime_us_delta(sz->signal_start, sz->signal_last);
1338c2ecf20Sopenharmony_ci		rawir.pulse = false;
1348c2ecf20Sopenharmony_ci		if (delta > (15 * USEC_PER_SEC)) {
1358c2ecf20Sopenharmony_ci			/* really long time */
1368c2ecf20Sopenharmony_ci			rawir.duration = IR_MAX_DURATION;
1378c2ecf20Sopenharmony_ci		} else {
1388c2ecf20Sopenharmony_ci			rawir.duration = delta;
1398c2ecf20Sopenharmony_ci			rawir.duration -= sz->sum;
1408c2ecf20Sopenharmony_ci			rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
1418c2ecf20Sopenharmony_ci					 IR_MAX_DURATION : rawir.duration;
1428c2ecf20Sopenharmony_ci		}
1438c2ecf20Sopenharmony_ci		sz_push(sz, rawir);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		sz->idle = false;
1468c2ecf20Sopenharmony_ci		sz->sum = 0;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	rawir.pulse = true;
1508c2ecf20Sopenharmony_ci	rawir.duration = ((int) value) * SZ_RESOLUTION;
1518c2ecf20Sopenharmony_ci	rawir.duration += SZ_RESOLUTION / 2;
1528c2ecf20Sopenharmony_ci	sz->sum += rawir.duration;
1538c2ecf20Sopenharmony_ci	rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
1548c2ecf20Sopenharmony_ci			 IR_MAX_DURATION : rawir.duration;
1558c2ecf20Sopenharmony_ci	sz_push(sz, rawir);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic void sz_push_half_pulse(struct streamzap_ir *sz,
1598c2ecf20Sopenharmony_ci			       unsigned char value)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void sz_push_full_space(struct streamzap_ir *sz,
1658c2ecf20Sopenharmony_ci			       unsigned char value)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct ir_raw_event rawir = {};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	rawir.pulse = false;
1708c2ecf20Sopenharmony_ci	rawir.duration = ((int) value) * SZ_RESOLUTION;
1718c2ecf20Sopenharmony_ci	rawir.duration += SZ_RESOLUTION / 2;
1728c2ecf20Sopenharmony_ci	sz->sum += rawir.duration;
1738c2ecf20Sopenharmony_ci	sz_push(sz, rawir);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void sz_push_half_space(struct streamzap_ir *sz,
1778c2ecf20Sopenharmony_ci			       unsigned long value)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	sz_push_full_space(sz, value & SZ_SPACE_MASK);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci * streamzap_callback - usb IRQ handler callback
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * This procedure is invoked on reception of data from
1868c2ecf20Sopenharmony_ci * the usb remote.
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_cistatic void streamzap_callback(struct urb *urb)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct streamzap_ir *sz;
1918c2ecf20Sopenharmony_ci	unsigned int i;
1928c2ecf20Sopenharmony_ci	int len;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (!urb)
1958c2ecf20Sopenharmony_ci		return;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	sz = urb->context;
1988c2ecf20Sopenharmony_ci	len = urb->actual_length;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	switch (urb->status) {
2018c2ecf20Sopenharmony_ci	case -ECONNRESET:
2028c2ecf20Sopenharmony_ci	case -ENOENT:
2038c2ecf20Sopenharmony_ci	case -ESHUTDOWN:
2048c2ecf20Sopenharmony_ci		/*
2058c2ecf20Sopenharmony_ci		 * this urb is terminated, clean up.
2068c2ecf20Sopenharmony_ci		 * sz might already be invalid at this point
2078c2ecf20Sopenharmony_ci		 */
2088c2ecf20Sopenharmony_ci		dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
2098c2ecf20Sopenharmony_ci		return;
2108c2ecf20Sopenharmony_ci	default:
2118c2ecf20Sopenharmony_ci		break;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
2158c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
2168c2ecf20Sopenharmony_ci		dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
2178c2ecf20Sopenharmony_ci			i, (unsigned char)sz->buf_in[i]);
2188c2ecf20Sopenharmony_ci		switch (sz->decoder_state) {
2198c2ecf20Sopenharmony_ci		case PulseSpace:
2208c2ecf20Sopenharmony_ci			if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
2218c2ecf20Sopenharmony_ci				SZ_PULSE_MASK) {
2228c2ecf20Sopenharmony_ci				sz->decoder_state = FullPulse;
2238c2ecf20Sopenharmony_ci				continue;
2248c2ecf20Sopenharmony_ci			} else if ((sz->buf_in[i] & SZ_SPACE_MASK)
2258c2ecf20Sopenharmony_ci					== SZ_SPACE_MASK) {
2268c2ecf20Sopenharmony_ci				sz_push_half_pulse(sz, sz->buf_in[i]);
2278c2ecf20Sopenharmony_ci				sz->decoder_state = FullSpace;
2288c2ecf20Sopenharmony_ci				continue;
2298c2ecf20Sopenharmony_ci			} else {
2308c2ecf20Sopenharmony_ci				sz_push_half_pulse(sz, sz->buf_in[i]);
2318c2ecf20Sopenharmony_ci				sz_push_half_space(sz, sz->buf_in[i]);
2328c2ecf20Sopenharmony_ci			}
2338c2ecf20Sopenharmony_ci			break;
2348c2ecf20Sopenharmony_ci		case FullPulse:
2358c2ecf20Sopenharmony_ci			sz_push_full_pulse(sz, sz->buf_in[i]);
2368c2ecf20Sopenharmony_ci			sz->decoder_state = IgnorePulse;
2378c2ecf20Sopenharmony_ci			break;
2388c2ecf20Sopenharmony_ci		case FullSpace:
2398c2ecf20Sopenharmony_ci			if (sz->buf_in[i] == SZ_TIMEOUT) {
2408c2ecf20Sopenharmony_ci				struct ir_raw_event rawir = {
2418c2ecf20Sopenharmony_ci					.pulse = false,
2428c2ecf20Sopenharmony_ci					.duration = sz->rdev->timeout
2438c2ecf20Sopenharmony_ci				};
2448c2ecf20Sopenharmony_ci				sz->idle = true;
2458c2ecf20Sopenharmony_ci				if (sz->timeout_enabled)
2468c2ecf20Sopenharmony_ci					sz_push(sz, rawir);
2478c2ecf20Sopenharmony_ci				ir_raw_event_handle(sz->rdev);
2488c2ecf20Sopenharmony_ci				ir_raw_event_reset(sz->rdev);
2498c2ecf20Sopenharmony_ci			} else {
2508c2ecf20Sopenharmony_ci				sz_push_full_space(sz, sz->buf_in[i]);
2518c2ecf20Sopenharmony_ci			}
2528c2ecf20Sopenharmony_ci			sz->decoder_state = PulseSpace;
2538c2ecf20Sopenharmony_ci			break;
2548c2ecf20Sopenharmony_ci		case IgnorePulse:
2558c2ecf20Sopenharmony_ci			if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
2568c2ecf20Sopenharmony_ci				SZ_SPACE_MASK) {
2578c2ecf20Sopenharmony_ci				sz->decoder_state = FullSpace;
2588c2ecf20Sopenharmony_ci				continue;
2598c2ecf20Sopenharmony_ci			}
2608c2ecf20Sopenharmony_ci			sz_push_half_space(sz, sz->buf_in[i]);
2618c2ecf20Sopenharmony_ci			sz->decoder_state = PulseSpace;
2628c2ecf20Sopenharmony_ci			break;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	ir_raw_event_handle(sz->rdev);
2678c2ecf20Sopenharmony_ci	usb_submit_urb(urb, GFP_ATOMIC);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	return;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct rc_dev *rdev;
2758c2ecf20Sopenharmony_ci	struct device *dev = sz->dev;
2768c2ecf20Sopenharmony_ci	int ret;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
2798c2ecf20Sopenharmony_ci	if (!rdev) {
2808c2ecf20Sopenharmony_ci		dev_err(dev, "remote dev allocation failed\n");
2818c2ecf20Sopenharmony_ci		goto out;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
2858c2ecf20Sopenharmony_ci		 le16_to_cpu(sz->usbdev->descriptor.idVendor),
2868c2ecf20Sopenharmony_ci		 le16_to_cpu(sz->usbdev->descriptor.idProduct));
2878c2ecf20Sopenharmony_ci	usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
2888c2ecf20Sopenharmony_ci	strlcat(sz->phys, "/input0", sizeof(sz->phys));
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	rdev->device_name = sz->name;
2918c2ecf20Sopenharmony_ci	rdev->input_phys = sz->phys;
2928c2ecf20Sopenharmony_ci	usb_to_input_id(sz->usbdev, &rdev->input_id);
2938c2ecf20Sopenharmony_ci	rdev->dev.parent = dev;
2948c2ecf20Sopenharmony_ci	rdev->priv = sz;
2958c2ecf20Sopenharmony_ci	rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
2968c2ecf20Sopenharmony_ci	rdev->driver_name = DRIVER_NAME;
2978c2ecf20Sopenharmony_ci	rdev->map_name = RC_MAP_STREAMZAP;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	ret = rc_register_device(rdev);
3008c2ecf20Sopenharmony_ci	if (ret < 0) {
3018c2ecf20Sopenharmony_ci		dev_err(dev, "remote input device register failed\n");
3028c2ecf20Sopenharmony_ci		goto out;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return rdev;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ciout:
3088c2ecf20Sopenharmony_ci	rc_free_device(rdev);
3098c2ecf20Sopenharmony_ci	return NULL;
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/*
3138c2ecf20Sopenharmony_ci *	streamzap_probe
3148c2ecf20Sopenharmony_ci *
3158c2ecf20Sopenharmony_ci *	Called by usb-core to associated with a candidate device
3168c2ecf20Sopenharmony_ci *	On any failure the return value is the ERROR
3178c2ecf20Sopenharmony_ci *	On success return 0
3188c2ecf20Sopenharmony_ci */
3198c2ecf20Sopenharmony_cistatic int streamzap_probe(struct usb_interface *intf,
3208c2ecf20Sopenharmony_ci			   const struct usb_device_id *id)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	struct usb_device *usbdev = interface_to_usbdev(intf);
3238c2ecf20Sopenharmony_ci	struct usb_host_interface *iface_host;
3248c2ecf20Sopenharmony_ci	struct streamzap_ir *sz = NULL;
3258c2ecf20Sopenharmony_ci	char buf[63], name[128] = "";
3268c2ecf20Sopenharmony_ci	int retval = -ENOMEM;
3278c2ecf20Sopenharmony_ci	int pipe, maxp;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/* Allocate space for device driver specific data */
3308c2ecf20Sopenharmony_ci	sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
3318c2ecf20Sopenharmony_ci	if (!sz)
3328c2ecf20Sopenharmony_ci		return -ENOMEM;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	sz->usbdev = usbdev;
3358c2ecf20Sopenharmony_ci	sz->interface = intf;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* Check to ensure endpoint information matches requirements */
3388c2ecf20Sopenharmony_ci	iface_host = intf->cur_altsetting;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if (iface_host->desc.bNumEndpoints != 1) {
3418c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
3428c2ecf20Sopenharmony_ci			__func__, iface_host->desc.bNumEndpoints);
3438c2ecf20Sopenharmony_ci		retval = -ENODEV;
3448c2ecf20Sopenharmony_ci		goto free_sz;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	sz->endpoint = &(iface_host->endpoint[0].desc);
3488c2ecf20Sopenharmony_ci	if (!usb_endpoint_dir_in(sz->endpoint)) {
3498c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
3508c2ecf20Sopenharmony_ci			__func__, sz->endpoint->bEndpointAddress);
3518c2ecf20Sopenharmony_ci		retval = -ENODEV;
3528c2ecf20Sopenharmony_ci		goto free_sz;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (!usb_endpoint_xfer_int(sz->endpoint)) {
3568c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
3578c2ecf20Sopenharmony_ci			__func__, sz->endpoint->bmAttributes);
3588c2ecf20Sopenharmony_ci		retval = -ENODEV;
3598c2ecf20Sopenharmony_ci		goto free_sz;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
3638c2ecf20Sopenharmony_ci	maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	if (maxp == 0) {
3668c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
3678c2ecf20Sopenharmony_ci			__func__);
3688c2ecf20Sopenharmony_ci		retval = -ENODEV;
3698c2ecf20Sopenharmony_ci		goto free_sz;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Allocate the USB buffer and IRQ URB */
3738c2ecf20Sopenharmony_ci	sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
3748c2ecf20Sopenharmony_ci	if (!sz->buf_in)
3758c2ecf20Sopenharmony_ci		goto free_sz;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
3788c2ecf20Sopenharmony_ci	if (!sz->urb_in)
3798c2ecf20Sopenharmony_ci		goto free_buf_in;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	sz->dev = &intf->dev;
3828c2ecf20Sopenharmony_ci	sz->buf_in_len = maxp;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (usbdev->descriptor.iManufacturer
3858c2ecf20Sopenharmony_ci	    && usb_string(usbdev, usbdev->descriptor.iManufacturer,
3868c2ecf20Sopenharmony_ci			  buf, sizeof(buf)) > 0)
3878c2ecf20Sopenharmony_ci		strscpy(name, buf, sizeof(name));
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	if (usbdev->descriptor.iProduct
3908c2ecf20Sopenharmony_ci	    && usb_string(usbdev, usbdev->descriptor.iProduct,
3918c2ecf20Sopenharmony_ci			  buf, sizeof(buf)) > 0)
3928c2ecf20Sopenharmony_ci		snprintf(name + strlen(name), sizeof(name) - strlen(name),
3938c2ecf20Sopenharmony_ci			 " %s", buf);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	sz->rdev = streamzap_init_rc_dev(sz);
3968c2ecf20Sopenharmony_ci	if (!sz->rdev)
3978c2ecf20Sopenharmony_ci		goto rc_dev_fail;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	sz->idle = true;
4008c2ecf20Sopenharmony_ci	sz->decoder_state = PulseSpace;
4018c2ecf20Sopenharmony_ci	/* FIXME: don't yet have a way to set this */
4028c2ecf20Sopenharmony_ci	sz->timeout_enabled = true;
4038c2ecf20Sopenharmony_ci	sz->rdev->timeout = SZ_TIMEOUT * SZ_RESOLUTION;
4048c2ecf20Sopenharmony_ci	#if 0
4058c2ecf20Sopenharmony_ci	/* not yet supported, depends on patches from maxim */
4068c2ecf20Sopenharmony_ci	/* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
4078c2ecf20Sopenharmony_ci	sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
4088c2ecf20Sopenharmony_ci	sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
4098c2ecf20Sopenharmony_ci	#endif
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	sz->signal_start = ktime_get_real();
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/* Complete final initialisations */
4148c2ecf20Sopenharmony_ci	usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
4158c2ecf20Sopenharmony_ci			 maxp, (usb_complete_t)streamzap_callback,
4168c2ecf20Sopenharmony_ci			 sz, sz->endpoint->bInterval);
4178c2ecf20Sopenharmony_ci	sz->urb_in->transfer_dma = sz->dma_in;
4188c2ecf20Sopenharmony_ci	sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, sz);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
4238c2ecf20Sopenharmony_ci		dev_err(sz->dev, "urb submit failed\n");
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
4268c2ecf20Sopenharmony_ci		 usbdev->bus->busnum, usbdev->devnum);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	return 0;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_circ_dev_fail:
4318c2ecf20Sopenharmony_ci	usb_free_urb(sz->urb_in);
4328c2ecf20Sopenharmony_cifree_buf_in:
4338c2ecf20Sopenharmony_ci	usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
4348c2ecf20Sopenharmony_cifree_sz:
4358c2ecf20Sopenharmony_ci	kfree(sz);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	return retval;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/*
4418c2ecf20Sopenharmony_ci * streamzap_disconnect
4428c2ecf20Sopenharmony_ci *
4438c2ecf20Sopenharmony_ci * Called by the usb core when the device is removed from the system.
4448c2ecf20Sopenharmony_ci *
4458c2ecf20Sopenharmony_ci * This routine guarantees that the driver will not submit any more urbs
4468c2ecf20Sopenharmony_ci * by clearing dev->usbdev.  It is also supposed to terminate any currently
4478c2ecf20Sopenharmony_ci * active urbs.  Unfortunately, usb_bulk_msg(), used in streamzap_read(),
4488c2ecf20Sopenharmony_ci * does not provide any way to do this.
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_cistatic void streamzap_disconnect(struct usb_interface *interface)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	struct streamzap_ir *sz = usb_get_intfdata(interface);
4538c2ecf20Sopenharmony_ci	struct usb_device *usbdev = interface_to_usbdev(interface);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	usb_set_intfdata(interface, NULL);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (!sz)
4588c2ecf20Sopenharmony_ci		return;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	sz->usbdev = NULL;
4618c2ecf20Sopenharmony_ci	rc_unregister_device(sz->rdev);
4628c2ecf20Sopenharmony_ci	usb_kill_urb(sz->urb_in);
4638c2ecf20Sopenharmony_ci	usb_free_urb(sz->urb_in);
4648c2ecf20Sopenharmony_ci	usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	kfree(sz);
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
4708c2ecf20Sopenharmony_ci{
4718c2ecf20Sopenharmony_ci	struct streamzap_ir *sz = usb_get_intfdata(intf);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	usb_kill_urb(sz->urb_in);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	return 0;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic int streamzap_resume(struct usb_interface *intf)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct streamzap_ir *sz = usb_get_intfdata(intf);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
4838c2ecf20Sopenharmony_ci		dev_err(sz->dev, "Error submitting urb\n");
4848c2ecf20Sopenharmony_ci		return -EIO;
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	return 0;
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cimodule_usb_driver(streamzap_driver);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
4938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
4948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
495