blob: 79a41fc7161cb303b0ca83872eac540eb1ee36f2 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jarod Wilson19770692010-07-26 20:32:49 -03002/*
3 * Streamzap Remote Control driver
4 *
5 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
Jarod Wilson8e9e6062010-08-03 01:07:04 -03006 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson19770692010-07-26 20:32:49 -03007 *
8 * This driver was based on the work of Greg Wickham and Adrian
9 * Dewhurst. It was substantially rewritten to support correct signal
10 * gaps and now maintains a delay buffer, which is used to present
11 * consistent timing behaviour to user space applications. Without the
12 * delay buffer an ugly hack would be required in lircd, which can
13 * cause sluggish signal decoding in certain situations.
14 *
Jarod Wilson8e9e6062010-08-03 01:07:04 -030015 * Ported to in-kernel ir-core interface by Jarod Wilson
16 *
Jarod Wilson19770692010-07-26 20:32:49 -030017 * This driver is based on the USB skeleton driver packaged with the
18 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
Jarod Wilson19770692010-07-26 20:32:49 -030019 */
20
Jarod Wilson8e9e6062010-08-03 01:07:04 -030021#include <linux/device.h>
Jarod Wilson19770692010-07-26 20:32:49 -030022#include <linux/module.h>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030023#include <linux/slab.h>
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -020024#include <linux/ktime.h>
Paul Bender635f76b2010-12-16 13:23:07 -030025#include <linux/usb.h>
26#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030027#include <media/rc-core.h>
Jarod Wilson19770692010-07-26 20:32:49 -030028
Jarod Wilson7a569f52010-08-07 13:31:40 -030029#define DRIVER_VERSION "1.61"
Jarod Wilson8e9e6062010-08-03 01:07:04 -030030#define DRIVER_NAME "streamzap"
Jarod Wilson19770692010-07-26 20:32:49 -030031#define DRIVER_DESC "Streamzap Remote Control driver"
32
Jarod Wilson19770692010-07-26 20:32:49 -030033#define USB_STREAMZAP_VENDOR_ID 0x0e9c
34#define USB_STREAMZAP_PRODUCT_ID 0x0000
35
Jarod Wilson19770692010-07-26 20:32:49 -030036/* table of devices that work with this driver */
Arvind Yadav5fad16b2017-08-13 05:54:44 -030037static const struct usb_device_id streamzap_table[] = {
Jarod Wilson19770692010-07-26 20:32:49 -030038 /* Streamzap Remote Control */
39 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
40 /* Terminating entry */
41 { }
42};
43
44MODULE_DEVICE_TABLE(usb, streamzap_table);
45
Jarod Wilsonb768d472010-10-12 10:41:27 -030046#define SZ_PULSE_MASK 0xf0
47#define SZ_SPACE_MASK 0x0f
48#define SZ_TIMEOUT 0xff
49#define SZ_RESOLUTION 256
Jarod Wilson19770692010-07-26 20:32:49 -030050
51/* number of samples buffered */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030052#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030053
54enum StreamzapDecoderState {
55 PulseSpace,
56 FullPulse,
57 FullSpace,
58 IgnorePulse
59};
60
Jarod Wilson8e9e6062010-08-03 01:07:04 -030061/* structure to hold our device specific stuff */
62struct streamzap_ir {
Jarod Wilson8e9e6062010-08-03 01:07:04 -030063 /* ir-core */
David Härdemand8b4b582010-10-29 16:08:23 -030064 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030065
66 /* core device info */
67 struct device *dev;
Jarod Wilson19770692010-07-26 20:32:49 -030068
69 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030070 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030071 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030072 struct usb_endpoint_descriptor *endpoint;
73 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030074
75 /* buffer & dma */
76 unsigned char *buf_in;
77 dma_addr_t dma_in;
78 unsigned int buf_in_len;
79
Jarod Wilson8e9e6062010-08-03 01:07:04 -030080 /* track what state we're in */
81 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -030082 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030083 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -030084 /* sum of signal lengths received since signal start */
85 unsigned long sum;
86 /* start time of signal; necessary for gap tracking */
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -020087 ktime_t signal_last;
88 ktime_t signal_start;
Jarod Wilson7a569f52010-08-07 13:31:40 -030089 bool timeout_enabled;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030090
91 char name[128];
92 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -030093};
94
95
96/* local function prototypes */
97static int streamzap_probe(struct usb_interface *interface,
98 const struct usb_device_id *id);
99static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300100static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300101static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
102static int streamzap_resume(struct usb_interface *intf);
103
104/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300105static struct usb_driver streamzap_driver = {
106 .name = DRIVER_NAME,
107 .probe = streamzap_probe,
108 .disconnect = streamzap_disconnect,
109 .suspend = streamzap_suspend,
110 .resume = streamzap_resume,
111 .id_table = streamzap_table,
112};
113
Jarod Wilson7a569f52010-08-07 13:31:40 -0300114static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
Jarod Wilson19770692010-07-26 20:32:49 -0300115{
Jarod Wilson1338c922010-11-17 12:25:45 -0300116 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
117 (rawir.pulse ? "pulse" : "space"), rawir.duration);
David Härdemand8b4b582010-10-29 16:08:23 -0300118 ir_raw_event_store_with_filter(sz->rdev, &rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300119}
120
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300121static void sz_push_full_pulse(struct streamzap_ir *sz,
122 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300123{
Sean Young183e19f2018-08-21 15:57:52 -0400124 struct ir_raw_event rawir = {};
Jarod Wilson7a569f52010-08-07 13:31:40 -0300125
Jarod Wilson19770692010-07-26 20:32:49 -0300126 if (sz->idle) {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200127 int delta;
Jarod Wilson19770692010-07-26 20:32:49 -0300128
129 sz->signal_last = sz->signal_start;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200130 sz->signal_start = ktime_get_real();
Jarod Wilson19770692010-07-26 20:32:49 -0300131
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200132 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300133 rawir.pulse = false;
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200134 if (delta > (15 * USEC_PER_SEC)) {
Jarod Wilson19770692010-07-26 20:32:49 -0300135 /* really long time */
Jarod Wilson7a569f52010-08-07 13:31:40 -0300136 rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300137 } else {
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200138 rawir.duration = delta;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300139 rawir.duration -= sz->sum;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300140 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300141 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
142 IR_MAX_DURATION : rawir.duration;
Jarod Wilson19770692010-07-26 20:32:49 -0300143 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300144 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300145
Jarod Wilson7a569f52010-08-07 13:31:40 -0300146 sz->idle = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300147 sz->sum = 0;
148 }
149
Jarod Wilson7a569f52010-08-07 13:31:40 -0300150 rawir.pulse = true;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300151 rawir.duration = ((int) value) * SZ_RESOLUTION;
152 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300153 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300154 rawir.duration = US_TO_NS(rawir.duration);
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300155 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
156 IR_MAX_DURATION : rawir.duration;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300157 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300158}
159
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300160static void sz_push_half_pulse(struct streamzap_ir *sz,
161 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300162{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300163 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300164}
165
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300166static void sz_push_full_space(struct streamzap_ir *sz,
167 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300168{
Sean Young183e19f2018-08-21 15:57:52 -0400169 struct ir_raw_event rawir = {};
Jarod Wilson7a569f52010-08-07 13:31:40 -0300170
171 rawir.pulse = false;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300172 rawir.duration = ((int) value) * SZ_RESOLUTION;
173 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300174 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300175 rawir.duration = US_TO_NS(rawir.duration);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300176 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300177}
178
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300179static void sz_push_half_space(struct streamzap_ir *sz,
180 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300181{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300182 sz_push_full_space(sz, value & SZ_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300183}
184
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -0500185/*
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300186 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300187 *
188 * This procedure is invoked on reception of data from
189 * the usb remote.
190 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300191static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300192{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300193 struct streamzap_ir *sz;
194 unsigned int i;
195 int len;
Jarod Wilson19770692010-07-26 20:32:49 -0300196
197 if (!urb)
198 return;
199
200 sz = urb->context;
201 len = urb->actual_length;
202
203 switch (urb->status) {
204 case -ECONNRESET:
205 case -ENOENT:
206 case -ESHUTDOWN:
207 /*
208 * this urb is terminated, clean up.
209 * sz might already be invalid at this point
210 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300211 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300212 return;
213 default:
214 break;
215 }
216
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300217 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300218 for (i = 0; i < len; i++) {
Jarod Wilson1338c922010-11-17 12:25:45 -0300219 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
Jarod Wilson7a569f52010-08-07 13:31:40 -0300220 i, (unsigned char)sz->buf_in[i]);
221 switch (sz->decoder_state) {
222 case PulseSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300223 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
224 SZ_PULSE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300225 sz->decoder_state = FullPulse;
226 continue;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300227 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
228 == SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300229 sz_push_half_pulse(sz, sz->buf_in[i]);
230 sz->decoder_state = FullSpace;
231 continue;
232 } else {
233 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300234 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300235 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300236 break;
237 case FullPulse:
238 sz_push_full_pulse(sz, sz->buf_in[i]);
239 sz->decoder_state = IgnorePulse;
240 break;
241 case FullSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300242 if (sz->buf_in[i] == SZ_TIMEOUT) {
Sean Young183e19f2018-08-21 15:57:52 -0400243 struct ir_raw_event rawir = {
244 .pulse = false,
245 .duration = sz->rdev->timeout
246 };
Jarod Wilson7a569f52010-08-07 13:31:40 -0300247 sz->idle = true;
248 if (sz->timeout_enabled)
249 sz_push(sz, rawir);
David Härdemand8b4b582010-10-29 16:08:23 -0300250 ir_raw_event_handle(sz->rdev);
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300251 ir_raw_event_reset(sz->rdev);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300252 } else {
253 sz_push_full_space(sz, sz->buf_in[i]);
254 }
255 sz->decoder_state = PulseSpace;
256 break;
257 case IgnorePulse:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300258 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
259 SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300260 sz->decoder_state = FullSpace;
261 continue;
262 }
263 sz_push_half_space(sz, sz->buf_in[i]);
264 sz->decoder_state = PulseSpace;
265 break;
Jarod Wilson19770692010-07-26 20:32:49 -0300266 }
267 }
268
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300269 ir_raw_event_handle(sz->rdev);
Jarod Wilson19770692010-07-26 20:32:49 -0300270 usb_submit_urb(urb, GFP_ATOMIC);
271
272 return;
273}
274
David Härdemand8b4b582010-10-29 16:08:23 -0300275static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300276{
David Härdemand8b4b582010-10-29 16:08:23 -0300277 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300278 struct device *dev = sz->dev;
279 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300280
Andi Shyti0f7499f2016-12-16 06:50:58 -0200281 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
David Härdemand8b4b582010-10-29 16:08:23 -0300282 if (!rdev) {
283 dev_err(dev, "remote dev allocation failed\n");
284 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300285 }
286
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200287 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300288 le16_to_cpu(sz->usbdev->descriptor.idVendor),
289 le16_to_cpu(sz->usbdev->descriptor.idProduct));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300290 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
291 strlcat(sz->phys, "/input0", sizeof(sz->phys));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300292
Sean Young518f4b22017-07-01 12:13:19 -0400293 rdev->device_name = sz->name;
David Härdemand8b4b582010-10-29 16:08:23 -0300294 rdev->input_phys = sz->phys;
Paul Bender5ad1a552010-11-17 16:56:17 -0300295 usb_to_input_id(sz->usbdev, &rdev->input_id);
296 rdev->dev.parent = dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300297 rdev->priv = sz;
Sean Young6d741bf2017-08-07 16:20:58 -0400298 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
David Härdemand8b4b582010-10-29 16:08:23 -0300299 rdev->driver_name = DRIVER_NAME;
300 rdev->map_name = RC_MAP_STREAMZAP;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300301
David Härdemand8b4b582010-10-29 16:08:23 -0300302 ret = rc_register_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300303 if (ret < 0) {
304 dev_err(dev, "remote input device register failed\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300305 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300306 }
307
David Härdemand8b4b582010-10-29 16:08:23 -0300308 return rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300309
David Härdemand8b4b582010-10-29 16:08:23 -0300310out:
311 rc_free_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300312 return NULL;
313}
314
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -0500315/*
Jarod Wilson19770692010-07-26 20:32:49 -0300316 * streamzap_probe
317 *
318 * Called by usb-core to associated with a candidate device
319 * On any failure the return value is the ERROR
320 * On success return 0
321 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800322static int streamzap_probe(struct usb_interface *intf,
323 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300324{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300325 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300326 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300327 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300328 char buf[63], name[128] = "";
329 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300330 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300331
332 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300333 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
334 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300335 return -ENOMEM;
336
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300337 sz->usbdev = usbdev;
338 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300339
340 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300341 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300342
343 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300344 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
345 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300346 retval = -ENODEV;
347 goto free_sz;
348 }
349
350 sz->endpoint = &(iface_host->endpoint[0].desc);
Himangi Saraogi56115882014-08-15 13:22:35 -0300351 if (!usb_endpoint_dir_in(sz->endpoint)) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200352 dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
353 __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300354 retval = -ENODEV;
355 goto free_sz;
356 }
357
Himangi Saraogi56115882014-08-15 13:22:35 -0300358 if (!usb_endpoint_xfer_int(sz->endpoint)) {
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200359 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
360 __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300361 retval = -ENODEV;
362 goto free_sz;
363 }
364
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300365 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
366 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
367
368 if (maxp == 0) {
369 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
370 __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300371 retval = -ENODEV;
372 goto free_sz;
373 }
374
375 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300376 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
377 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300378 goto free_sz;
379
380 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300381 if (!sz->urb_in)
382 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300383
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300384 sz->dev = &intf->dev;
385 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300386
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300387 if (usbdev->descriptor.iManufacturer
388 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300389 buf, sizeof(buf)) > 0)
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400390 strscpy(name, buf, sizeof(name));
Jarod Wilson19770692010-07-26 20:32:49 -0300391
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300392 if (usbdev->descriptor.iProduct
393 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300394 buf, sizeof(buf)) > 0)
395 snprintf(name + strlen(name), sizeof(name) - strlen(name),
396 " %s", buf);
397
David Härdemand8b4b582010-10-29 16:08:23 -0300398 sz->rdev = streamzap_init_rc_dev(sz);
399 if (!sz->rdev)
400 goto rc_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300401
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300402 sz->idle = true;
403 sz->decoder_state = PulseSpace;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300404 /* FIXME: don't yet have a way to set this */
405 sz->timeout_enabled = true;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300406 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
Jarod Wilson1338c922010-11-17 12:25:45 -0300407 IR_MAX_DURATION) | 0x03000000);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300408 #if 0
409 /* not yet supported, depends on patches from maxim */
410 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300411 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
412 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300413 #endif
Jarod Wilson19770692010-07-26 20:32:49 -0300414
Tina Ruchandanidd4c22a2015-10-29 05:16:57 -0200415 sz->signal_start = ktime_get_real();
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300416
417 /* Complete final initialisations */
418 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
419 maxp, (usb_complete_t)streamzap_callback,
420 sz, sz->endpoint->bInterval);
421 sz->urb_in->transfer_dma = sz->dma_in;
422 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
423
424 usb_set_intfdata(intf, sz);
425
Jarod Wilson7a569f52010-08-07 13:31:40 -0300426 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
427 dev_err(sz->dev, "urb submit failed\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300428
429 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
430 usbdev->bus->busnum, usbdev->devnum);
Jarod Wilson19770692010-07-26 20:32:49 -0300431
432 return 0;
433
David Härdemand8b4b582010-10-29 16:08:23 -0300434rc_dev_fail:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300435 usb_free_urb(sz->urb_in);
436free_buf_in:
437 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300438free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300439 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300440
441 return retval;
442}
443
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -0500444/*
Jarod Wilson19770692010-07-26 20:32:49 -0300445 * streamzap_disconnect
446 *
447 * Called by the usb core when the device is removed from the system.
448 *
449 * This routine guarantees that the driver will not submit any more urbs
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300450 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300451 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
452 * does not provide any way to do this.
453 */
454static void streamzap_disconnect(struct usb_interface *interface)
455{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300456 struct streamzap_ir *sz = usb_get_intfdata(interface);
457 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300458
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300459 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300460
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300461 if (!sz)
462 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300463
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300464 sz->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300465 rc_unregister_device(sz->rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300466 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300467 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300468 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300469
Jarod Wilson19770692010-07-26 20:32:49 -0300470 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300471}
472
473static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
474{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300475 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300476
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300477 usb_kill_urb(sz->urb_in);
478
Jarod Wilson19770692010-07-26 20:32:49 -0300479 return 0;
480}
481
482static int streamzap_resume(struct usb_interface *intf)
483{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300484 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300485
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300486 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
Colin Ian King1c195cb2016-09-01 08:03:14 -0300487 dev_err(sz->dev, "Error submitting urb\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300488 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300489 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300490
Jarod Wilson19770692010-07-26 20:32:49 -0300491 return 0;
492}
493
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800494module_usb_driver(streamzap_driver);
Jarod Wilson19770692010-07-26 20:32:49 -0300495
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300496MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300497MODULE_DESCRIPTION(DRIVER_DESC);
498MODULE_LICENSE("GPL");