blob: f6391dd6e29f73d7e57c7bfae7178e23b8d54933 [file] [log] [blame]
Jarod Wilson19770692010-07-26 20:32:49 -03001/*
2 * Streamzap Remote Control driver
3 *
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
Jarod Wilson8e9e6062010-08-03 01:07:04 -03005 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson19770692010-07-26 20:32:49 -03006 *
7 * This driver was based on the work of Greg Wickham and Adrian
8 * Dewhurst. It was substantially rewritten to support correct signal
9 * gaps and now maintains a delay buffer, which is used to present
10 * consistent timing behaviour to user space applications. Without the
11 * delay buffer an ugly hack would be required in lircd, which can
12 * cause sluggish signal decoding in certain situations.
13 *
Jarod Wilson8e9e6062010-08-03 01:07:04 -030014 * Ported to in-kernel ir-core interface by Jarod Wilson
15 *
Jarod Wilson19770692010-07-26 20:32:49 -030016 * This driver is based on the USB skeleton driver packaged with the
17 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
Jarod Wilson19770692010-07-26 20:32:49 -030028 */
29
Jarod Wilson8e9e6062010-08-03 01:07:04 -030030#include <linux/device.h>
Jarod Wilson19770692010-07-26 20:32:49 -030031#include <linux/module.h>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030032#include <linux/slab.h>
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -020033#include <linux/ktime.h>
Paul Bender635f76b2010-12-16 13:23:07 -030034#include <linux/usb.h>
35#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030036#include <media/rc-core.h>
Jarod Wilson19770692010-07-26 20:32:49 -030037
Jarod Wilson7a569f52010-08-07 13:31:40 -030038#define DRIVER_VERSION "1.61"
Jarod Wilson8e9e6062010-08-03 01:07:04 -030039#define DRIVER_NAME "streamzap"
Jarod Wilson19770692010-07-26 20:32:49 -030040#define DRIVER_DESC "Streamzap Remote Control driver"
41
Jarod Wilson19770692010-07-26 20:32:49 -030042#define USB_STREAMZAP_VENDOR_ID 0x0e9c
43#define USB_STREAMZAP_PRODUCT_ID 0x0000
44
Jarod Wilson19770692010-07-26 20:32:49 -030045/* table of devices that work with this driver */
46static struct usb_device_id streamzap_table[] = {
47 /* Streamzap Remote Control */
48 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
49 /* Terminating entry */
50 { }
51};
52
53MODULE_DEVICE_TABLE(usb, streamzap_table);
54
Jarod Wilsonb768d472010-10-12 10:41:27 -030055#define SZ_PULSE_MASK 0xf0
56#define SZ_SPACE_MASK 0x0f
57#define SZ_TIMEOUT 0xff
58#define SZ_RESOLUTION 256
Jarod Wilson19770692010-07-26 20:32:49 -030059
60/* number of samples buffered */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030061#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030062
63enum StreamzapDecoderState {
64 PulseSpace,
65 FullPulse,
66 FullSpace,
67 IgnorePulse
68};
69
Jarod Wilson8e9e6062010-08-03 01:07:04 -030070/* structure to hold our device specific stuff */
71struct streamzap_ir {
Jarod Wilson8e9e6062010-08-03 01:07:04 -030072 /* ir-core */
David Härdemand8b4b582010-10-29 16:08:23 -030073 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030074
75 /* core device info */
76 struct device *dev;
Jarod Wilson19770692010-07-26 20:32:49 -030077
78 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030079 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030080 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030081 struct usb_endpoint_descriptor *endpoint;
82 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030083
84 /* buffer & dma */
85 unsigned char *buf_in;
86 dma_addr_t dma_in;
87 unsigned int buf_in_len;
88
Jarod Wilson8e9e6062010-08-03 01:07:04 -030089 /* track what state we're in */
90 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -030091 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030092 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -030093 /* sum of signal lengths received since signal start */
94 unsigned long sum;
95 /* start time of signal; necessary for gap tracking */
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -020096 ktime_t signal_last;
97 ktime_t signal_start;
Jarod Wilson7a569f52010-08-07 13:31:40 -030098 bool timeout_enabled;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030099
100 char name[128];
101 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -0300102};
103
104
105/* local function prototypes */
106static int streamzap_probe(struct usb_interface *interface,
107 const struct usb_device_id *id);
108static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300109static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300110static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
111static int streamzap_resume(struct usb_interface *intf);
112
113/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300114static struct usb_driver streamzap_driver = {
115 .name = DRIVER_NAME,
116 .probe = streamzap_probe,
117 .disconnect = streamzap_disconnect,
118 .suspend = streamzap_suspend,
119 .resume = streamzap_resume,
120 .id_table = streamzap_table,
121};
122
Jarod Wilson7a569f52010-08-07 13:31:40 -0300123static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
Jarod Wilson19770692010-07-26 20:32:49 -0300124{
Jarod Wilson1338c922010-11-17 12:25:45 -0300125 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
126 (rawir.pulse ? "pulse" : "space"), rawir.duration);
David Härdemand8b4b582010-10-29 16:08:23 -0300127 ir_raw_event_store_with_filter(sz->rdev, &rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300128}
129
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300130static void sz_push_full_pulse(struct streamzap_ir *sz,
131 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300132{
Maxim Levitsky46519182010-10-16 19:56:28 -0300133 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300134
Jarod Wilson19770692010-07-26 20:32:49 -0300135 if (sz->idle) {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200136 int delta;
Jarod Wilson19770692010-07-26 20:32:49 -0300137
138 sz->signal_last = sz->signal_start;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200139 sz->signal_start = ktime_get_real();
Jarod Wilson19770692010-07-26 20:32:49 -0300140
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200141 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300142 rawir.pulse = false;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200143 if (delta > (15 * USEC_PER_SEC)) {
Jarod Wilson19770692010-07-26 20:32:49 -0300144 /* really long time */
Jarod Wilson7a569f52010-08-07 13:31:40 -0300145 rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300146 } else {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200147 rawir.duration = delta;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300148 rawir.duration -= sz->sum;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300149 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300150 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
151 IR_MAX_DURATION : rawir.duration;
Jarod Wilson19770692010-07-26 20:32:49 -0300152 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300153 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300154
Jarod Wilson7a569f52010-08-07 13:31:40 -0300155 sz->idle = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300156 sz->sum = 0;
157 }
158
Jarod Wilson7a569f52010-08-07 13:31:40 -0300159 rawir.pulse = true;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300160 rawir.duration = ((int) value) * SZ_RESOLUTION;
161 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300162 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300163 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300164 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
165 IR_MAX_DURATION : rawir.duration;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300166 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300167}
168
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300169static void sz_push_half_pulse(struct streamzap_ir *sz,
170 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300171{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300172 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300173}
174
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300175static void sz_push_full_space(struct streamzap_ir *sz,
176 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300177{
Maxim Levitsky46519182010-10-16 19:56:28 -0300178 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300179
180 rawir.pulse = false;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300181 rawir.duration = ((int) value) * SZ_RESOLUTION;
182 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300183 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300184 rawir.duration = US_TO_NS(rawir.duration);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300185 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300186}
187
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300188static void sz_push_half_space(struct streamzap_ir *sz,
189 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300190{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300191 sz_push_full_space(sz, value & SZ_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300192}
193
194/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300195 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300196 *
197 * This procedure is invoked on reception of data from
198 * the usb remote.
199 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300200static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300201{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300202 struct streamzap_ir *sz;
203 unsigned int i;
204 int len;
Jarod Wilson19770692010-07-26 20:32:49 -0300205
206 if (!urb)
207 return;
208
209 sz = urb->context;
210 len = urb->actual_length;
211
212 switch (urb->status) {
213 case -ECONNRESET:
214 case -ENOENT:
215 case -ESHUTDOWN:
216 /*
217 * this urb is terminated, clean up.
218 * sz might already be invalid at this point
219 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300220 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300221 return;
222 default:
223 break;
224 }
225
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300226 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300227 for (i = 0; i < len; i++) {
Jarod Wilson1338c922010-11-17 12:25:45 -0300228 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
Jarod Wilson7a569f52010-08-07 13:31:40 -0300229 i, (unsigned char)sz->buf_in[i]);
230 switch (sz->decoder_state) {
231 case PulseSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300232 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
233 SZ_PULSE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300234 sz->decoder_state = FullPulse;
235 continue;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300236 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
237 == SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300238 sz_push_half_pulse(sz, sz->buf_in[i]);
239 sz->decoder_state = FullSpace;
240 continue;
241 } else {
242 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300243 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300244 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300245 break;
246 case FullPulse:
247 sz_push_full_pulse(sz, sz->buf_in[i]);
248 sz->decoder_state = IgnorePulse;
249 break;
250 case FullSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300251 if (sz->buf_in[i] == SZ_TIMEOUT) {
Maxim Levitsky46519182010-10-16 19:56:28 -0300252 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300253
254 rawir.pulse = false;
David Härdemand8b4b582010-10-29 16:08:23 -0300255 rawir.duration = sz->rdev->timeout;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300256 sz->idle = true;
257 if (sz->timeout_enabled)
258 sz_push(sz, rawir);
David Härdemand8b4b582010-10-29 16:08:23 -0300259 ir_raw_event_handle(sz->rdev);
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300260 ir_raw_event_reset(sz->rdev);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300261 } else {
262 sz_push_full_space(sz, sz->buf_in[i]);
263 }
264 sz->decoder_state = PulseSpace;
265 break;
266 case IgnorePulse:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300267 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
268 SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300269 sz->decoder_state = FullSpace;
270 continue;
271 }
272 sz_push_half_space(sz, sz->buf_in[i]);
273 sz->decoder_state = PulseSpace;
274 break;
Jarod Wilson19770692010-07-26 20:32:49 -0300275 }
276 }
277
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300278 ir_raw_event_handle(sz->rdev);
Jarod Wilson19770692010-07-26 20:32:49 -0300279 usb_submit_urb(urb, GFP_ATOMIC);
280
281 return;
282}
283
David Härdemand8b4b582010-10-29 16:08:23 -0300284static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300285{
David Härdemand8b4b582010-10-29 16:08:23 -0300286 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300287 struct device *dev = sz->dev;
288 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300289
David Härdemand8b4b582010-10-29 16:08:23 -0300290 rdev = rc_allocate_device();
291 if (!rdev) {
292 dev_err(dev, "remote dev allocation failed\n");
293 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300294 }
295
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200296 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300297 le16_to_cpu(sz->usbdev->descriptor.idVendor),
298 le16_to_cpu(sz->usbdev->descriptor.idProduct));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300299 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
300 strlcat(sz->phys, "/input0", sizeof(sz->phys));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300301
David Härdemand8b4b582010-10-29 16:08:23 -0300302 rdev->input_name = sz->name;
303 rdev->input_phys = sz->phys;
Paul Bender5ad1a552010-11-17 16:56:17 -0300304 usb_to_input_id(sz->usbdev, &rdev->input_id);
305 rdev->dev.parent = dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300306 rdev->priv = sz;
307 rdev->driver_type = RC_DRIVER_IR_RAW;
Sean Young8c34b5c2016-12-03 08:55:56 -0200308 rdev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
David Härdemand8b4b582010-10-29 16:08:23 -0300309 rdev->driver_name = DRIVER_NAME;
310 rdev->map_name = RC_MAP_STREAMZAP;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300311
David Härdemand8b4b582010-10-29 16:08:23 -0300312 ret = rc_register_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300313 if (ret < 0) {
314 dev_err(dev, "remote input device register failed\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300315 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300316 }
317
David Härdemand8b4b582010-10-29 16:08:23 -0300318 return rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300319
David Härdemand8b4b582010-10-29 16:08:23 -0300320out:
321 rc_free_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300322 return NULL;
323}
324
Jarod Wilson19770692010-07-26 20:32:49 -0300325/**
326 * streamzap_probe
327 *
328 * Called by usb-core to associated with a candidate device
329 * On any failure the return value is the ERROR
330 * On success return 0
331 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800332static int streamzap_probe(struct usb_interface *intf,
333 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300334{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300335 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300336 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300337 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300338 char buf[63], name[128] = "";
339 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300340 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300341
342 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300343 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
344 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300345 return -ENOMEM;
346
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300347 sz->usbdev = usbdev;
348 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300349
350 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300351 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300352
353 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300354 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
355 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300356 retval = -ENODEV;
357 goto free_sz;
358 }
359
360 sz->endpoint = &(iface_host->endpoint[0].desc);
Himangi Saraogi56115882014-08-15 13:22:35 -0300361 if (!usb_endpoint_dir_in(sz->endpoint)) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200362 dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
363 __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300364 retval = -ENODEV;
365 goto free_sz;
366 }
367
Himangi Saraogi56115882014-08-15 13:22:35 -0300368 if (!usb_endpoint_xfer_int(sz->endpoint)) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200369 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
370 __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300371 retval = -ENODEV;
372 goto free_sz;
373 }
374
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300375 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
376 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
377
378 if (maxp == 0) {
379 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
380 __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300381 retval = -ENODEV;
382 goto free_sz;
383 }
384
385 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300386 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
387 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300388 goto free_sz;
389
390 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300391 if (!sz->urb_in)
392 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300393
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300394 sz->dev = &intf->dev;
395 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300396
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300397 if (usbdev->descriptor.iManufacturer
398 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300399 buf, sizeof(buf)) > 0)
400 strlcpy(name, buf, sizeof(name));
401
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300402 if (usbdev->descriptor.iProduct
403 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300404 buf, sizeof(buf)) > 0)
405 snprintf(name + strlen(name), sizeof(name) - strlen(name),
406 " %s", buf);
407
David Härdemand8b4b582010-10-29 16:08:23 -0300408 sz->rdev = streamzap_init_rc_dev(sz);
409 if (!sz->rdev)
410 goto rc_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300411
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300412 sz->idle = true;
413 sz->decoder_state = PulseSpace;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300414 /* FIXME: don't yet have a way to set this */
415 sz->timeout_enabled = true;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300416 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
Jarod Wilson1338c922010-11-17 12:25:45 -0300417 IR_MAX_DURATION) | 0x03000000);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300418 #if 0
419 /* not yet supported, depends on patches from maxim */
420 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300421 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
422 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300423 #endif
Jarod Wilson19770692010-07-26 20:32:49 -0300424
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200425 sz->signal_start = ktime_get_real();
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300426
427 /* Complete final initialisations */
428 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
429 maxp, (usb_complete_t)streamzap_callback,
430 sz, sz->endpoint->bInterval);
431 sz->urb_in->transfer_dma = sz->dma_in;
432 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
433
434 usb_set_intfdata(intf, sz);
435
Jarod Wilson7a569f52010-08-07 13:31:40 -0300436 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
437 dev_err(sz->dev, "urb submit failed\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300438
439 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
440 usbdev->bus->busnum, usbdev->devnum);
Jarod Wilson19770692010-07-26 20:32:49 -0300441
442 return 0;
443
David Härdemand8b4b582010-10-29 16:08:23 -0300444rc_dev_fail:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300445 usb_free_urb(sz->urb_in);
446free_buf_in:
447 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300448free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300449 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300450
451 return retval;
452}
453
Jarod Wilson19770692010-07-26 20:32:49 -0300454/**
455 * streamzap_disconnect
456 *
457 * Called by the usb core when the device is removed from the system.
458 *
459 * This routine guarantees that the driver will not submit any more urbs
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300460 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300461 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
462 * does not provide any way to do this.
463 */
464static void streamzap_disconnect(struct usb_interface *interface)
465{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300466 struct streamzap_ir *sz = usb_get_intfdata(interface);
467 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300468
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300469 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300470
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300471 if (!sz)
472 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300473
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300474 sz->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300475 rc_unregister_device(sz->rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300476 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300477 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300478 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300479
Jarod Wilson19770692010-07-26 20:32:49 -0300480 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300481}
482
483static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
484{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300485 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300486
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300487 usb_kill_urb(sz->urb_in);
488
Jarod Wilson19770692010-07-26 20:32:49 -0300489 return 0;
490}
491
492static int streamzap_resume(struct usb_interface *intf)
493{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300494 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300495
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300496 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
Colin Ian King1c195cb2016-09-01 08:03:14 -0300497 dev_err(sz->dev, "Error submitting urb\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300498 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300499 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300500
Jarod Wilson19770692010-07-26 20:32:49 -0300501 return 0;
502}
503
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800504module_usb_driver(streamzap_driver);
Jarod Wilson19770692010-07-26 20:32:49 -0300505
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300506MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300507MODULE_DESCRIPTION(DRIVER_DESC);
508MODULE_LICENSE("GPL");