blob: 4004260a7c69606e26b4c6079925ef4d9afd876e [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.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33
Jarod Wilson8e9e6062010-08-03 01:07:04 -030034#include <linux/device.h>
Jarod Wilson19770692010-07-26 20:32:49 -030035#include <linux/module.h>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030036#include <linux/slab.h>
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -020037#include <linux/ktime.h>
Paul Bender635f76b2010-12-16 13:23:07 -030038#include <linux/usb.h>
39#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030040#include <media/rc-core.h>
Jarod Wilson19770692010-07-26 20:32:49 -030041
Jarod Wilson7a569f52010-08-07 13:31:40 -030042#define DRIVER_VERSION "1.61"
Jarod Wilson8e9e6062010-08-03 01:07:04 -030043#define DRIVER_NAME "streamzap"
Jarod Wilson19770692010-07-26 20:32:49 -030044#define DRIVER_DESC "Streamzap Remote Control driver"
45
Jarod Wilson19770692010-07-26 20:32:49 -030046#define USB_STREAMZAP_VENDOR_ID 0x0e9c
47#define USB_STREAMZAP_PRODUCT_ID 0x0000
48
Jarod Wilson19770692010-07-26 20:32:49 -030049/* table of devices that work with this driver */
50static struct usb_device_id streamzap_table[] = {
51 /* Streamzap Remote Control */
52 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
53 /* Terminating entry */
54 { }
55};
56
57MODULE_DEVICE_TABLE(usb, streamzap_table);
58
Jarod Wilsonb768d472010-10-12 10:41:27 -030059#define SZ_PULSE_MASK 0xf0
60#define SZ_SPACE_MASK 0x0f
61#define SZ_TIMEOUT 0xff
62#define SZ_RESOLUTION 256
Jarod Wilson19770692010-07-26 20:32:49 -030063
64/* number of samples buffered */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030065#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030066
67enum StreamzapDecoderState {
68 PulseSpace,
69 FullPulse,
70 FullSpace,
71 IgnorePulse
72};
73
Jarod Wilson8e9e6062010-08-03 01:07:04 -030074/* structure to hold our device specific stuff */
75struct streamzap_ir {
Jarod Wilson8e9e6062010-08-03 01:07:04 -030076 /* ir-core */
David Härdemand8b4b582010-10-29 16:08:23 -030077 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030078
79 /* core device info */
80 struct device *dev;
Jarod Wilson19770692010-07-26 20:32:49 -030081
82 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030083 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030084 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030085 struct usb_endpoint_descriptor *endpoint;
86 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030087
88 /* buffer & dma */
89 unsigned char *buf_in;
90 dma_addr_t dma_in;
91 unsigned int buf_in_len;
92
Jarod Wilson8e9e6062010-08-03 01:07:04 -030093 /* track what state we're in */
94 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -030095 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030096 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -030097 /* sum of signal lengths received since signal start */
98 unsigned long sum;
99 /* start time of signal; necessary for gap tracking */
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200100 ktime_t signal_last;
101 ktime_t signal_start;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300102 bool timeout_enabled;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300103
104 char name[128];
105 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -0300106};
107
108
109/* local function prototypes */
110static int streamzap_probe(struct usb_interface *interface,
111 const struct usb_device_id *id);
112static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300113static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300114static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
115static int streamzap_resume(struct usb_interface *intf);
116
117/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300118static struct usb_driver streamzap_driver = {
119 .name = DRIVER_NAME,
120 .probe = streamzap_probe,
121 .disconnect = streamzap_disconnect,
122 .suspend = streamzap_suspend,
123 .resume = streamzap_resume,
124 .id_table = streamzap_table,
125};
126
Jarod Wilson7a569f52010-08-07 13:31:40 -0300127static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
Jarod Wilson19770692010-07-26 20:32:49 -0300128{
Jarod Wilson1338c922010-11-17 12:25:45 -0300129 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
130 (rawir.pulse ? "pulse" : "space"), rawir.duration);
David Härdemand8b4b582010-10-29 16:08:23 -0300131 ir_raw_event_store_with_filter(sz->rdev, &rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300132}
133
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300134static void sz_push_full_pulse(struct streamzap_ir *sz,
135 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300136{
Maxim Levitsky46519182010-10-16 19:56:28 -0300137 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300138
Jarod Wilson19770692010-07-26 20:32:49 -0300139 if (sz->idle) {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200140 int delta;
Jarod Wilson19770692010-07-26 20:32:49 -0300141
142 sz->signal_last = sz->signal_start;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200143 sz->signal_start = ktime_get_real();
Jarod Wilson19770692010-07-26 20:32:49 -0300144
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200145 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300146 rawir.pulse = false;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200147 if (delta > (15 * USEC_PER_SEC)) {
Jarod Wilson19770692010-07-26 20:32:49 -0300148 /* really long time */
Jarod Wilson7a569f52010-08-07 13:31:40 -0300149 rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300150 } else {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200151 rawir.duration = delta;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300152 rawir.duration -= sz->sum;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300153 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300154 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
155 IR_MAX_DURATION : rawir.duration;
Jarod Wilson19770692010-07-26 20:32:49 -0300156 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300157 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300158
Jarod Wilson7a569f52010-08-07 13:31:40 -0300159 sz->idle = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300160 sz->sum = 0;
161 }
162
Jarod Wilson7a569f52010-08-07 13:31:40 -0300163 rawir.pulse = true;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300164 rawir.duration = ((int) value) * SZ_RESOLUTION;
165 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300166 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300167 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300168 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
169 IR_MAX_DURATION : rawir.duration;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300170 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300171}
172
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300173static void sz_push_half_pulse(struct streamzap_ir *sz,
174 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300175{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300176 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300177}
178
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300179static void sz_push_full_space(struct streamzap_ir *sz,
180 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300181{
Maxim Levitsky46519182010-10-16 19:56:28 -0300182 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300183
184 rawir.pulse = false;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300185 rawir.duration = ((int) value) * SZ_RESOLUTION;
186 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300187 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300188 rawir.duration = US_TO_NS(rawir.duration);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300189 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300190}
191
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300192static void sz_push_half_space(struct streamzap_ir *sz,
193 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300194{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300195 sz_push_full_space(sz, value & SZ_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300196}
197
198/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300199 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300200 *
201 * This procedure is invoked on reception of data from
202 * the usb remote.
203 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300204static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300205{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300206 struct streamzap_ir *sz;
207 unsigned int i;
208 int len;
Jarod Wilson19770692010-07-26 20:32:49 -0300209
210 if (!urb)
211 return;
212
213 sz = urb->context;
214 len = urb->actual_length;
215
216 switch (urb->status) {
217 case -ECONNRESET:
218 case -ENOENT:
219 case -ESHUTDOWN:
220 /*
221 * this urb is terminated, clean up.
222 * sz might already be invalid at this point
223 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300224 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300225 return;
226 default:
227 break;
228 }
229
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300230 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300231 for (i = 0; i < len; i++) {
Jarod Wilson1338c922010-11-17 12:25:45 -0300232 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
Jarod Wilson7a569f52010-08-07 13:31:40 -0300233 i, (unsigned char)sz->buf_in[i]);
234 switch (sz->decoder_state) {
235 case PulseSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300236 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
237 SZ_PULSE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300238 sz->decoder_state = FullPulse;
239 continue;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300240 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
241 == SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300242 sz_push_half_pulse(sz, sz->buf_in[i]);
243 sz->decoder_state = FullSpace;
244 continue;
245 } else {
246 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300247 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300248 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300249 break;
250 case FullPulse:
251 sz_push_full_pulse(sz, sz->buf_in[i]);
252 sz->decoder_state = IgnorePulse;
253 break;
254 case FullSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300255 if (sz->buf_in[i] == SZ_TIMEOUT) {
Maxim Levitsky46519182010-10-16 19:56:28 -0300256 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300257
258 rawir.pulse = false;
David Härdemand8b4b582010-10-29 16:08:23 -0300259 rawir.duration = sz->rdev->timeout;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300260 sz->idle = true;
261 if (sz->timeout_enabled)
262 sz_push(sz, rawir);
David Härdemand8b4b582010-10-29 16:08:23 -0300263 ir_raw_event_handle(sz->rdev);
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300264 ir_raw_event_reset(sz->rdev);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300265 } else {
266 sz_push_full_space(sz, sz->buf_in[i]);
267 }
268 sz->decoder_state = PulseSpace;
269 break;
270 case IgnorePulse:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300271 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
272 SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300273 sz->decoder_state = FullSpace;
274 continue;
275 }
276 sz_push_half_space(sz, sz->buf_in[i]);
277 sz->decoder_state = PulseSpace;
278 break;
Jarod Wilson19770692010-07-26 20:32:49 -0300279 }
280 }
281
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300282 ir_raw_event_handle(sz->rdev);
Jarod Wilson19770692010-07-26 20:32:49 -0300283 usb_submit_urb(urb, GFP_ATOMIC);
284
285 return;
286}
287
David Härdemand8b4b582010-10-29 16:08:23 -0300288static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300289{
David Härdemand8b4b582010-10-29 16:08:23 -0300290 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300291 struct device *dev = sz->dev;
292 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300293
David Härdemand8b4b582010-10-29 16:08:23 -0300294 rdev = rc_allocate_device();
295 if (!rdev) {
296 dev_err(dev, "remote dev allocation failed\n");
297 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300298 }
299
300 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
301 "Receiver (%04x:%04x)",
302 le16_to_cpu(sz->usbdev->descriptor.idVendor),
303 le16_to_cpu(sz->usbdev->descriptor.idProduct));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300304 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
305 strlcat(sz->phys, "/input0", sizeof(sz->phys));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300306
David Härdemand8b4b582010-10-29 16:08:23 -0300307 rdev->input_name = sz->name;
308 rdev->input_phys = sz->phys;
Paul Bender5ad1a552010-11-17 16:56:17 -0300309 usb_to_input_id(sz->usbdev, &rdev->input_id);
310 rdev->dev.parent = dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300311 rdev->priv = sz;
312 rdev->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc5540fb2014-04-03 20:32:21 -0300313 rdev->allowed_protocols = RC_BIT_ALL;
David Härdemand8b4b582010-10-29 16:08:23 -0300314 rdev->driver_name = DRIVER_NAME;
315 rdev->map_name = RC_MAP_STREAMZAP;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300316
David Härdemand8b4b582010-10-29 16:08:23 -0300317 ret = rc_register_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300318 if (ret < 0) {
319 dev_err(dev, "remote input device register failed\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300320 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300321 }
322
David Härdemand8b4b582010-10-29 16:08:23 -0300323 return rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300324
David Härdemand8b4b582010-10-29 16:08:23 -0300325out:
326 rc_free_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300327 return NULL;
328}
329
Jarod Wilson19770692010-07-26 20:32:49 -0300330/**
331 * streamzap_probe
332 *
333 * Called by usb-core to associated with a candidate device
334 * On any failure the return value is the ERROR
335 * On success return 0
336 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800337static int streamzap_probe(struct usb_interface *intf,
338 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300339{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300340 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300341 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300342 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300343 char buf[63], name[128] = "";
344 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300345 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300346
347 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300348 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
349 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300350 return -ENOMEM;
351
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300352 sz->usbdev = usbdev;
353 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300354
355 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300356 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300357
358 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300359 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
360 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300361 retval = -ENODEV;
362 goto free_sz;
363 }
364
365 sz->endpoint = &(iface_host->endpoint[0].desc);
Himangi Saraogi56115882014-08-15 13:22:35 -0300366 if (!usb_endpoint_dir_in(sz->endpoint)) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300367 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
368 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300369 retval = -ENODEV;
370 goto free_sz;
371 }
372
Himangi Saraogi56115882014-08-15 13:22:35 -0300373 if (!usb_endpoint_xfer_int(sz->endpoint)) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300374 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
375 "02%02x\n", __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300376 retval = -ENODEV;
377 goto free_sz;
378 }
379
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300380 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
381 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
382
383 if (maxp == 0) {
384 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
385 __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300386 retval = -ENODEV;
387 goto free_sz;
388 }
389
390 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300391 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
392 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300393 goto free_sz;
394
395 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300396 if (!sz->urb_in)
397 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300398
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300399 sz->dev = &intf->dev;
400 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300401
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300402 if (usbdev->descriptor.iManufacturer
403 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300404 buf, sizeof(buf)) > 0)
405 strlcpy(name, buf, sizeof(name));
406
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300407 if (usbdev->descriptor.iProduct
408 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300409 buf, sizeof(buf)) > 0)
410 snprintf(name + strlen(name), sizeof(name) - strlen(name),
411 " %s", buf);
412
David Härdemand8b4b582010-10-29 16:08:23 -0300413 sz->rdev = streamzap_init_rc_dev(sz);
414 if (!sz->rdev)
415 goto rc_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300416
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300417 sz->idle = true;
418 sz->decoder_state = PulseSpace;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300419 /* FIXME: don't yet have a way to set this */
420 sz->timeout_enabled = true;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300421 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
Jarod Wilson1338c922010-11-17 12:25:45 -0300422 IR_MAX_DURATION) | 0x03000000);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300423 #if 0
424 /* not yet supported, depends on patches from maxim */
425 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300426 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
427 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300428 #endif
Jarod Wilson19770692010-07-26 20:32:49 -0300429
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200430 sz->signal_start = ktime_get_real();
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300431
432 /* Complete final initialisations */
433 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
434 maxp, (usb_complete_t)streamzap_callback,
435 sz, sz->endpoint->bInterval);
436 sz->urb_in->transfer_dma = sz->dma_in;
437 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
438
439 usb_set_intfdata(intf, sz);
440
Jarod Wilson7a569f52010-08-07 13:31:40 -0300441 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
442 dev_err(sz->dev, "urb submit failed\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300443
444 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
445 usbdev->bus->busnum, usbdev->devnum);
Jarod Wilson19770692010-07-26 20:32:49 -0300446
447 return 0;
448
David Härdemand8b4b582010-10-29 16:08:23 -0300449rc_dev_fail:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300450 usb_free_urb(sz->urb_in);
451free_buf_in:
452 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300453free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300454 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300455
456 return retval;
457}
458
Jarod Wilson19770692010-07-26 20:32:49 -0300459/**
460 * streamzap_disconnect
461 *
462 * Called by the usb core when the device is removed from the system.
463 *
464 * This routine guarantees that the driver will not submit any more urbs
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300465 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300466 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
467 * does not provide any way to do this.
468 */
469static void streamzap_disconnect(struct usb_interface *interface)
470{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300471 struct streamzap_ir *sz = usb_get_intfdata(interface);
472 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300473
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300474 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300475
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300476 if (!sz)
477 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300478
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300479 sz->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300480 rc_unregister_device(sz->rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300481 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300482 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300483 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300484
Jarod Wilson19770692010-07-26 20:32:49 -0300485 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300486}
487
488static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
489{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300490 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300491
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300492 usb_kill_urb(sz->urb_in);
493
Jarod Wilson19770692010-07-26 20:32:49 -0300494 return 0;
495}
496
497static int streamzap_resume(struct usb_interface *intf)
498{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300499 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300500
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300501 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
Colin Ian King1c195cb2016-09-01 08:03:14 -0300502 dev_err(sz->dev, "Error submitting urb\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300503 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300504 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300505
Jarod Wilson19770692010-07-26 20:32:49 -0300506 return 0;
507}
508
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800509module_usb_driver(streamzap_driver);
Jarod Wilson19770692010-07-26 20:32:49 -0300510
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300511MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300512MODULE_DESCRIPTION(DRIVER_DESC);
513MODULE_LICENSE("GPL");