blob: bf4a44272f0e3e00a01c0bc3fb44720e1732ec27 [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>
Paul Bender635f76b2010-12-16 13:23:07 -030037#include <linux/usb.h>
38#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030039#include <media/rc-core.h>
Jarod Wilson19770692010-07-26 20:32:49 -030040
Jarod Wilson7a569f52010-08-07 13:31:40 -030041#define DRIVER_VERSION "1.61"
Jarod Wilson8e9e6062010-08-03 01:07:04 -030042#define DRIVER_NAME "streamzap"
Jarod Wilson19770692010-07-26 20:32:49 -030043#define DRIVER_DESC "Streamzap Remote Control driver"
44
Jarod Wilson19770692010-07-26 20:32:49 -030045#define USB_STREAMZAP_VENDOR_ID 0x0e9c
46#define USB_STREAMZAP_PRODUCT_ID 0x0000
47
Jarod Wilson19770692010-07-26 20:32:49 -030048/* table of devices that work with this driver */
49static struct usb_device_id streamzap_table[] = {
50 /* Streamzap Remote Control */
51 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
52 /* Terminating entry */
53 { }
54};
55
56MODULE_DEVICE_TABLE(usb, streamzap_table);
57
Jarod Wilsonb768d472010-10-12 10:41:27 -030058#define SZ_PULSE_MASK 0xf0
59#define SZ_SPACE_MASK 0x0f
60#define SZ_TIMEOUT 0xff
61#define SZ_RESOLUTION 256
Jarod Wilson19770692010-07-26 20:32:49 -030062
63/* number of samples buffered */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030064#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030065
66enum StreamzapDecoderState {
67 PulseSpace,
68 FullPulse,
69 FullSpace,
70 IgnorePulse
71};
72
Jarod Wilson8e9e6062010-08-03 01:07:04 -030073/* structure to hold our device specific stuff */
74struct streamzap_ir {
Jarod Wilson8e9e6062010-08-03 01:07:04 -030075 /* ir-core */
David Härdemand8b4b582010-10-29 16:08:23 -030076 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030077
78 /* core device info */
79 struct device *dev;
Jarod Wilson19770692010-07-26 20:32:49 -030080
81 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030082 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030083 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030084 struct usb_endpoint_descriptor *endpoint;
85 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030086
87 /* buffer & dma */
88 unsigned char *buf_in;
89 dma_addr_t dma_in;
90 unsigned int buf_in_len;
91
Jarod Wilson8e9e6062010-08-03 01:07:04 -030092 /* track what state we're in */
93 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -030094 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030095 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -030096 /* sum of signal lengths received since signal start */
97 unsigned long sum;
98 /* start time of signal; necessary for gap tracking */
99 struct timeval signal_last;
100 struct timeval signal_start;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300101 bool timeout_enabled;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300102
103 char name[128];
104 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -0300105};
106
107
108/* local function prototypes */
109static int streamzap_probe(struct usb_interface *interface,
110 const struct usb_device_id *id);
111static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300112static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300113static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
114static int streamzap_resume(struct usb_interface *intf);
115
116/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300117static struct usb_driver streamzap_driver = {
118 .name = DRIVER_NAME,
119 .probe = streamzap_probe,
120 .disconnect = streamzap_disconnect,
121 .suspend = streamzap_suspend,
122 .resume = streamzap_resume,
123 .id_table = streamzap_table,
124};
125
Jarod Wilson7a569f52010-08-07 13:31:40 -0300126static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
Jarod Wilson19770692010-07-26 20:32:49 -0300127{
Jarod Wilson1338c922010-11-17 12:25:45 -0300128 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
129 (rawir.pulse ? "pulse" : "space"), rawir.duration);
David Härdemand8b4b582010-10-29 16:08:23 -0300130 ir_raw_event_store_with_filter(sz->rdev, &rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300131}
132
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300133static void sz_push_full_pulse(struct streamzap_ir *sz,
134 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300135{
Maxim Levitsky46519182010-10-16 19:56:28 -0300136 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300137
Jarod Wilson19770692010-07-26 20:32:49 -0300138 if (sz->idle) {
139 long deltv;
Jarod Wilson19770692010-07-26 20:32:49 -0300140
141 sz->signal_last = sz->signal_start;
142 do_gettimeofday(&sz->signal_start);
143
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300144 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300145 rawir.pulse = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300146 if (deltv > 15) {
147 /* really long time */
Jarod Wilson7a569f52010-08-07 13:31:40 -0300148 rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300149 } else {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300150 rawir.duration = (int)(deltv * 1000000 +
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300151 sz->signal_start.tv_usec -
152 sz->signal_last.tv_usec);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300153 rawir.duration -= sz->sum;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300154 rawir.duration = US_TO_NS(rawir.duration);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300155 rawir.duration &= IR_MAX_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);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300168 rawir.duration &= IR_MAX_DURATION;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300169 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300170}
171
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300172static void sz_push_half_pulse(struct streamzap_ir *sz,
173 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300174{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300175 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300176}
177
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300178static void sz_push_full_space(struct streamzap_ir *sz,
179 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300180{
Maxim Levitsky46519182010-10-16 19:56:28 -0300181 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300182
183 rawir.pulse = false;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300184 rawir.duration = ((int) value) * SZ_RESOLUTION;
185 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300186 sz->sum += rawir.duration;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300187 rawir.duration = US_TO_NS(rawir.duration);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300188 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300189}
190
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300191static void sz_push_half_space(struct streamzap_ir *sz,
192 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300193{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300194 sz_push_full_space(sz, value & SZ_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300195}
196
197/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300198 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300199 *
200 * This procedure is invoked on reception of data from
201 * the usb remote.
202 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300203static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300204{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300205 struct streamzap_ir *sz;
206 unsigned int i;
207 int len;
Jarod Wilson19770692010-07-26 20:32:49 -0300208
209 if (!urb)
210 return;
211
212 sz = urb->context;
213 len = urb->actual_length;
214
215 switch (urb->status) {
216 case -ECONNRESET:
217 case -ENOENT:
218 case -ESHUTDOWN:
219 /*
220 * this urb is terminated, clean up.
221 * sz might already be invalid at this point
222 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300223 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300224 return;
225 default:
226 break;
227 }
228
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300229 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300230 for (i = 0; i < len; i++) {
Jarod Wilson1338c922010-11-17 12:25:45 -0300231 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
Jarod Wilson7a569f52010-08-07 13:31:40 -0300232 i, (unsigned char)sz->buf_in[i]);
233 switch (sz->decoder_state) {
234 case PulseSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300235 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
236 SZ_PULSE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300237 sz->decoder_state = FullPulse;
238 continue;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300239 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
240 == SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300241 sz_push_half_pulse(sz, sz->buf_in[i]);
242 sz->decoder_state = FullSpace;
243 continue;
244 } else {
245 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300246 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300247 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300248 break;
249 case FullPulse:
250 sz_push_full_pulse(sz, sz->buf_in[i]);
251 sz->decoder_state = IgnorePulse;
252 break;
253 case FullSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300254 if (sz->buf_in[i] == SZ_TIMEOUT) {
Maxim Levitsky46519182010-10-16 19:56:28 -0300255 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300256
257 rawir.pulse = false;
David Härdemand8b4b582010-10-29 16:08:23 -0300258 rawir.duration = sz->rdev->timeout;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300259 sz->idle = true;
260 if (sz->timeout_enabled)
261 sz_push(sz, rawir);
David Härdemand8b4b582010-10-29 16:08:23 -0300262 ir_raw_event_handle(sz->rdev);
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300263 ir_raw_event_reset(sz->rdev);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300264 } else {
265 sz_push_full_space(sz, sz->buf_in[i]);
266 }
267 sz->decoder_state = PulseSpace;
268 break;
269 case IgnorePulse:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300270 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
271 SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300272 sz->decoder_state = FullSpace;
273 continue;
274 }
275 sz_push_half_space(sz, sz->buf_in[i]);
276 sz->decoder_state = PulseSpace;
277 break;
Jarod Wilson19770692010-07-26 20:32:49 -0300278 }
279 }
280
Jarod Wilson56b0ec32011-01-27 15:08:35 -0300281 ir_raw_event_handle(sz->rdev);
Jarod Wilson19770692010-07-26 20:32:49 -0300282 usb_submit_urb(urb, GFP_ATOMIC);
283
284 return;
285}
286
David Härdemand8b4b582010-10-29 16:08:23 -0300287static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300288{
David Härdemand8b4b582010-10-29 16:08:23 -0300289 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300290 struct device *dev = sz->dev;
291 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300292
David Härdemand8b4b582010-10-29 16:08:23 -0300293 rdev = rc_allocate_device();
294 if (!rdev) {
295 dev_err(dev, "remote dev allocation failed\n");
296 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300297 }
298
299 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
300 "Receiver (%04x:%04x)",
301 le16_to_cpu(sz->usbdev->descriptor.idVendor),
302 le16_to_cpu(sz->usbdev->descriptor.idProduct));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300303 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
304 strlcat(sz->phys, "/input0", sizeof(sz->phys));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300305
David Härdemand8b4b582010-10-29 16:08:23 -0300306 rdev->input_name = sz->name;
307 rdev->input_phys = sz->phys;
Paul Bender5ad1a552010-11-17 16:56:17 -0300308 usb_to_input_id(sz->usbdev, &rdev->input_id);
309 rdev->dev.parent = dev;
David Härdemand8b4b582010-10-29 16:08:23 -0300310 rdev->priv = sz;
311 rdev->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc5540fb2014-04-03 20:32:21 -0300312 rdev->allowed_protocols = RC_BIT_ALL;
David Härdemand8b4b582010-10-29 16:08:23 -0300313 rdev->driver_name = DRIVER_NAME;
314 rdev->map_name = RC_MAP_STREAMZAP;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300315
David Härdemand8b4b582010-10-29 16:08:23 -0300316 ret = rc_register_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300317 if (ret < 0) {
318 dev_err(dev, "remote input device register failed\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300319 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300320 }
321
David Härdemand8b4b582010-10-29 16:08:23 -0300322 return rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300323
David Härdemand8b4b582010-10-29 16:08:23 -0300324out:
325 rc_free_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300326 return NULL;
327}
328
Jarod Wilson19770692010-07-26 20:32:49 -0300329/**
330 * streamzap_probe
331 *
332 * Called by usb-core to associated with a candidate device
333 * On any failure the return value is the ERROR
334 * On success return 0
335 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800336static int streamzap_probe(struct usb_interface *intf,
337 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300338{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300339 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300340 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300341 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300342 char buf[63], name[128] = "";
343 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300344 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300345
346 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300347 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
348 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300349 return -ENOMEM;
350
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300351 sz->usbdev = usbdev;
352 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300353
354 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300355 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300356
357 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300358 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
359 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300360 retval = -ENODEV;
361 goto free_sz;
362 }
363
364 sz->endpoint = &(iface_host->endpoint[0].desc);
Himangi Saraogi56115882014-08-15 13:22:35 -0300365 if (!usb_endpoint_dir_in(sz->endpoint)) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300366 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
367 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300368 retval = -ENODEV;
369 goto free_sz;
370 }
371
Himangi Saraogi56115882014-08-15 13:22:35 -0300372 if (!usb_endpoint_xfer_int(sz->endpoint)) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300373 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
374 "02%02x\n", __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300375 retval = -ENODEV;
376 goto free_sz;
377 }
378
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300379 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
380 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
381
382 if (maxp == 0) {
383 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
384 __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300385 retval = -ENODEV;
386 goto free_sz;
387 }
388
389 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300390 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
391 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300392 goto free_sz;
393
394 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300395 if (!sz->urb_in)
396 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300397
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300398 sz->dev = &intf->dev;
399 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300400
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300401 if (usbdev->descriptor.iManufacturer
402 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300403 buf, sizeof(buf)) > 0)
404 strlcpy(name, buf, sizeof(name));
405
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300406 if (usbdev->descriptor.iProduct
407 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300408 buf, sizeof(buf)) > 0)
409 snprintf(name + strlen(name), sizeof(name) - strlen(name),
410 " %s", buf);
411
David Härdemand8b4b582010-10-29 16:08:23 -0300412 sz->rdev = streamzap_init_rc_dev(sz);
413 if (!sz->rdev)
414 goto rc_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300415
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300416 sz->idle = true;
417 sz->decoder_state = PulseSpace;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300418 /* FIXME: don't yet have a way to set this */
419 sz->timeout_enabled = true;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300420 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
Jarod Wilson1338c922010-11-17 12:25:45 -0300421 IR_MAX_DURATION) | 0x03000000);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300422 #if 0
423 /* not yet supported, depends on patches from maxim */
424 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300425 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
426 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300427 #endif
Jarod Wilson19770692010-07-26 20:32:49 -0300428
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300429 do_gettimeofday(&sz->signal_start);
430
431 /* Complete final initialisations */
432 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
433 maxp, (usb_complete_t)streamzap_callback,
434 sz, sz->endpoint->bInterval);
435 sz->urb_in->transfer_dma = sz->dma_in;
436 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
437
438 usb_set_intfdata(intf, sz);
439
Jarod Wilson7a569f52010-08-07 13:31:40 -0300440 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
441 dev_err(sz->dev, "urb submit failed\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300442
443 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
444 usbdev->bus->busnum, usbdev->devnum);
Jarod Wilson19770692010-07-26 20:32:49 -0300445
446 return 0;
447
David Härdemand8b4b582010-10-29 16:08:23 -0300448rc_dev_fail:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300449 usb_free_urb(sz->urb_in);
450free_buf_in:
451 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300452free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300453 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300454
455 return retval;
456}
457
Jarod Wilson19770692010-07-26 20:32:49 -0300458/**
459 * streamzap_disconnect
460 *
461 * Called by the usb core when the device is removed from the system.
462 *
463 * This routine guarantees that the driver will not submit any more urbs
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300464 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300465 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
466 * does not provide any way to do this.
467 */
468static void streamzap_disconnect(struct usb_interface *interface)
469{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300470 struct streamzap_ir *sz = usb_get_intfdata(interface);
471 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300472
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300473 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300474
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300475 if (!sz)
476 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300477
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300478 sz->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300479 rc_unregister_device(sz->rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300480 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300481 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300482 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300483
Jarod Wilson19770692010-07-26 20:32:49 -0300484 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300485}
486
487static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
488{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300489 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300490
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300491 usb_kill_urb(sz->urb_in);
492
Jarod Wilson19770692010-07-26 20:32:49 -0300493 return 0;
494}
495
496static int streamzap_resume(struct usb_interface *intf)
497{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300498 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300499
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300500 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
501 dev_err(sz->dev, "Error sumbiting urb\n");
502 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300503 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300504
Jarod Wilson19770692010-07-26 20:32:49 -0300505 return 0;
506}
507
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800508module_usb_driver(streamzap_driver);
Jarod Wilson19770692010-07-26 20:32:49 -0300509
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300510MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300511MODULE_DESCRIPTION(DRIVER_DESC);
512MODULE_LICENSE("GPL");