blob: f05f5c173fdfbea9f1ed9f2c499a37ac1600c6dd [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>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030039#include <media/ir-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 Wilson8e9e6062010-08-03 01:07:04 -030045#ifdef CONFIG_USB_DEBUG
46static int debug = 1;
47#else
Jarod Wilson19770692010-07-26 20:32:49 -030048static int debug;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030049#endif
Jarod Wilson19770692010-07-26 20:32:49 -030050
51#define USB_STREAMZAP_VENDOR_ID 0x0e9c
52#define USB_STREAMZAP_PRODUCT_ID 0x0000
53
Jarod Wilson19770692010-07-26 20:32:49 -030054/* table of devices that work with this driver */
55static struct usb_device_id streamzap_table[] = {
56 /* Streamzap Remote Control */
57 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
58 /* Terminating entry */
59 { }
60};
61
62MODULE_DEVICE_TABLE(usb, streamzap_table);
63
Jarod Wilsonb768d472010-10-12 10:41:27 -030064#define SZ_PULSE_MASK 0xf0
65#define SZ_SPACE_MASK 0x0f
66#define SZ_TIMEOUT 0xff
67#define SZ_RESOLUTION 256
Jarod Wilson19770692010-07-26 20:32:49 -030068
69/* number of samples buffered */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030070#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030071
Jarod Wilson7a569f52010-08-07 13:31:40 -030072/* from ir-rc5-sz-decoder.c */
73#ifdef CONFIG_IR_RC5_SZ_DECODER_MODULE
74#define load_rc5_sz_decode() request_module("ir-rc5-sz-decoder")
75#else
76#define load_rc5_sz_decode() 0
77#endif
78
Jarod Wilson19770692010-07-26 20:32:49 -030079enum StreamzapDecoderState {
80 PulseSpace,
81 FullPulse,
82 FullSpace,
83 IgnorePulse
84};
85
Jarod Wilson8e9e6062010-08-03 01:07:04 -030086/* structure to hold our device specific stuff */
87struct streamzap_ir {
Jarod Wilson8e9e6062010-08-03 01:07:04 -030088 /* ir-core */
David Härdemand8b4b582010-10-29 16:08:23 -030089 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030090
91 /* core device info */
92 struct device *dev;
Jarod Wilson19770692010-07-26 20:32:49 -030093
94 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030095 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030096 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030097 struct usb_endpoint_descriptor *endpoint;
98 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030099
100 /* buffer & dma */
101 unsigned char *buf_in;
102 dma_addr_t dma_in;
103 unsigned int buf_in_len;
104
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300105 /* track what state we're in */
106 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -0300107 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300108 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -0300109 /* sum of signal lengths received since signal start */
110 unsigned long sum;
111 /* start time of signal; necessary for gap tracking */
112 struct timeval signal_last;
113 struct timeval signal_start;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300114 bool timeout_enabled;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300115
116 char name[128];
117 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -0300118};
119
120
121/* local function prototypes */
122static int streamzap_probe(struct usb_interface *interface,
123 const struct usb_device_id *id);
124static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300125static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300126static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
127static int streamzap_resume(struct usb_interface *intf);
128
129/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300130static struct usb_driver streamzap_driver = {
131 .name = DRIVER_NAME,
132 .probe = streamzap_probe,
133 .disconnect = streamzap_disconnect,
134 .suspend = streamzap_suspend,
135 .resume = streamzap_resume,
136 .id_table = streamzap_table,
137};
138
Jarod Wilson7a569f52010-08-07 13:31:40 -0300139static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
Jarod Wilson19770692010-07-26 20:32:49 -0300140{
Jarod Wilson1338c922010-11-17 12:25:45 -0300141 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
142 (rawir.pulse ? "pulse" : "space"), rawir.duration);
David Härdemand8b4b582010-10-29 16:08:23 -0300143 ir_raw_event_store_with_filter(sz->rdev, &rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300144}
145
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300146static void sz_push_full_pulse(struct streamzap_ir *sz,
147 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300148{
Maxim Levitsky46519182010-10-16 19:56:28 -0300149 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300150
Jarod Wilson19770692010-07-26 20:32:49 -0300151 if (sz->idle) {
152 long deltv;
Jarod Wilson19770692010-07-26 20:32:49 -0300153
154 sz->signal_last = sz->signal_start;
155 do_gettimeofday(&sz->signal_start);
156
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300157 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300158 rawir.pulse = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300159 if (deltv > 15) {
160 /* really long time */
Jarod Wilson7a569f52010-08-07 13:31:40 -0300161 rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300162 } else {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300163 rawir.duration = (int)(deltv * 1000000 +
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300164 sz->signal_start.tv_usec -
165 sz->signal_last.tv_usec);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300166 rawir.duration -= sz->sum;
167 rawir.duration *= 1000;
168 rawir.duration &= IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300169 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300170 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300171
Jarod Wilson7a569f52010-08-07 13:31:40 -0300172 sz->idle = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300173 sz->sum = 0;
174 }
175
Jarod Wilson7a569f52010-08-07 13:31:40 -0300176 rawir.pulse = true;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300177 rawir.duration = ((int) value) * SZ_RESOLUTION;
178 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300179 sz->sum += rawir.duration;
180 rawir.duration *= 1000;
181 rawir.duration &= IR_MAX_DURATION;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300182 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300183}
184
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300185static void sz_push_half_pulse(struct streamzap_ir *sz,
186 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300187{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300188 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300189}
190
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300191static void sz_push_full_space(struct streamzap_ir *sz,
192 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300193{
Maxim Levitsky46519182010-10-16 19:56:28 -0300194 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300195
196 rawir.pulse = false;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300197 rawir.duration = ((int) value) * SZ_RESOLUTION;
198 rawir.duration += SZ_RESOLUTION / 2;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300199 sz->sum += rawir.duration;
200 rawir.duration *= 1000;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300201 sz_push(sz, rawir);
Jarod Wilson19770692010-07-26 20:32:49 -0300202}
203
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300204static void sz_push_half_space(struct streamzap_ir *sz,
205 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300206{
Jarod Wilsonb768d472010-10-12 10:41:27 -0300207 sz_push_full_space(sz, value & SZ_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300208}
209
210/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300211 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300212 *
213 * This procedure is invoked on reception of data from
214 * the usb remote.
215 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300216static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300217{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300218 struct streamzap_ir *sz;
219 unsigned int i;
220 int len;
Jarod Wilson19770692010-07-26 20:32:49 -0300221
222 if (!urb)
223 return;
224
225 sz = urb->context;
226 len = urb->actual_length;
227
228 switch (urb->status) {
229 case -ECONNRESET:
230 case -ENOENT:
231 case -ESHUTDOWN:
232 /*
233 * this urb is terminated, clean up.
234 * sz might already be invalid at this point
235 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300236 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300237 return;
238 default:
239 break;
240 }
241
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300242 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300243 for (i = 0; i < len; i++) {
Jarod Wilson1338c922010-11-17 12:25:45 -0300244 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
Jarod Wilson7a569f52010-08-07 13:31:40 -0300245 i, (unsigned char)sz->buf_in[i]);
246 switch (sz->decoder_state) {
247 case PulseSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300248 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
249 SZ_PULSE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300250 sz->decoder_state = FullPulse;
251 continue;
Jarod Wilsonb768d472010-10-12 10:41:27 -0300252 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
253 == SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300254 sz_push_half_pulse(sz, sz->buf_in[i]);
255 sz->decoder_state = FullSpace;
256 continue;
257 } else {
258 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300259 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300260 }
Jarod Wilson7a569f52010-08-07 13:31:40 -0300261 break;
262 case FullPulse:
263 sz_push_full_pulse(sz, sz->buf_in[i]);
264 sz->decoder_state = IgnorePulse;
265 break;
266 case FullSpace:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300267 if (sz->buf_in[i] == SZ_TIMEOUT) {
Maxim Levitsky46519182010-10-16 19:56:28 -0300268 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300269
270 rawir.pulse = false;
David Härdemand8b4b582010-10-29 16:08:23 -0300271 rawir.duration = sz->rdev->timeout;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300272 sz->idle = true;
273 if (sz->timeout_enabled)
274 sz_push(sz, rawir);
David Härdemand8b4b582010-10-29 16:08:23 -0300275 ir_raw_event_handle(sz->rdev);
Jarod Wilson7a569f52010-08-07 13:31:40 -0300276 } else {
277 sz_push_full_space(sz, sz->buf_in[i]);
278 }
279 sz->decoder_state = PulseSpace;
280 break;
281 case IgnorePulse:
Jarod Wilsonb768d472010-10-12 10:41:27 -0300282 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
283 SZ_SPACE_MASK) {
Jarod Wilson7a569f52010-08-07 13:31:40 -0300284 sz->decoder_state = FullSpace;
285 continue;
286 }
287 sz_push_half_space(sz, sz->buf_in[i]);
288 sz->decoder_state = PulseSpace;
289 break;
Jarod Wilson19770692010-07-26 20:32:49 -0300290 }
291 }
292
293 usb_submit_urb(urb, GFP_ATOMIC);
294
295 return;
296}
297
David Härdemand8b4b582010-10-29 16:08:23 -0300298static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300299{
David Härdemand8b4b582010-10-29 16:08:23 -0300300 struct rc_dev *rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300301 struct device *dev = sz->dev;
302 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300303
David Härdemand8b4b582010-10-29 16:08:23 -0300304 rdev = rc_allocate_device();
305 if (!rdev) {
306 dev_err(dev, "remote dev allocation failed\n");
307 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300308 }
309
310 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
311 "Receiver (%04x:%04x)",
312 le16_to_cpu(sz->usbdev->descriptor.idVendor),
313 le16_to_cpu(sz->usbdev->descriptor.idProduct));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300314 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
315 strlcat(sz->phys, "/input0", sizeof(sz->phys));
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300316
David Härdemand8b4b582010-10-29 16:08:23 -0300317 rdev->input_name = sz->name;
318 rdev->input_phys = sz->phys;
319 rdev->priv = sz;
320 rdev->driver_type = RC_DRIVER_IR_RAW;
321 rdev->allowed_protos = IR_TYPE_ALL;
322 rdev->driver_name = DRIVER_NAME;
323 rdev->map_name = RC_MAP_STREAMZAP;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300324
David Härdemand8b4b582010-10-29 16:08:23 -0300325 ret = rc_register_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300326 if (ret < 0) {
327 dev_err(dev, "remote input device register failed\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300328 goto out;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300329 }
330
David Härdemand8b4b582010-10-29 16:08:23 -0300331 return rdev;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300332
David Härdemand8b4b582010-10-29 16:08:23 -0300333out:
334 rc_free_device(rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300335 return NULL;
336}
337
Jarod Wilson19770692010-07-26 20:32:49 -0300338/**
339 * streamzap_probe
340 *
341 * Called by usb-core to associated with a candidate device
342 * On any failure the return value is the ERROR
343 * On success return 0
344 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300345static int __devinit streamzap_probe(struct usb_interface *intf,
346 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300347{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300348 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300349 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300350 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300351 char buf[63], name[128] = "";
352 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300353 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300354
355 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300356 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
357 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300358 return -ENOMEM;
359
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300360 sz->usbdev = usbdev;
361 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300362
363 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300364 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300365
366 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300367 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
368 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300369 retval = -ENODEV;
370 goto free_sz;
371 }
372
373 sz->endpoint = &(iface_host->endpoint[0].desc);
374 if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
375 != USB_DIR_IN) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300376 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
377 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300378 retval = -ENODEV;
379 goto free_sz;
380 }
381
382 if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
383 != USB_ENDPOINT_XFER_INT) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300384 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
385 "02%02x\n", __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300386 retval = -ENODEV;
387 goto free_sz;
388 }
389
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300390 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
391 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
392
393 if (maxp == 0) {
394 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
395 __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300396 retval = -ENODEV;
397 goto free_sz;
398 }
399
400 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300401 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
402 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300403 goto free_sz;
404
405 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300406 if (!sz->urb_in)
407 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300408
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300409 sz->dev = &intf->dev;
410 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300411
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300412 if (usbdev->descriptor.iManufacturer
413 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300414 buf, sizeof(buf)) > 0)
415 strlcpy(name, buf, sizeof(name));
416
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300417 if (usbdev->descriptor.iProduct
418 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300419 buf, sizeof(buf)) > 0)
420 snprintf(name + strlen(name), sizeof(name) - strlen(name),
421 " %s", buf);
422
David Härdemand8b4b582010-10-29 16:08:23 -0300423 sz->rdev = streamzap_init_rc_dev(sz);
424 if (!sz->rdev)
425 goto rc_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300426
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300427 sz->idle = true;
428 sz->decoder_state = PulseSpace;
Jarod Wilson7a569f52010-08-07 13:31:40 -0300429 /* FIXME: don't yet have a way to set this */
430 sz->timeout_enabled = true;
David Härdemand8b4b582010-10-29 16:08:23 -0300431 sz->rdev->timeout = (((SZ_TIMEOUT * SZ_RESOLUTION * 1000) &
Jarod Wilson1338c922010-11-17 12:25:45 -0300432 IR_MAX_DURATION) | 0x03000000);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300433 #if 0
434 /* not yet supported, depends on patches from maxim */
435 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
Jarod Wilsonb768d472010-10-12 10:41:27 -0300436 sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
437 sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300438 #endif
Jarod Wilson19770692010-07-26 20:32:49 -0300439
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300440 do_gettimeofday(&sz->signal_start);
441
442 /* Complete final initialisations */
443 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
444 maxp, (usb_complete_t)streamzap_callback,
445 sz, sz->endpoint->bInterval);
446 sz->urb_in->transfer_dma = sz->dma_in;
447 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
448
449 usb_set_intfdata(intf, sz);
450
Jarod Wilson7a569f52010-08-07 13:31:40 -0300451 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
452 dev_err(sz->dev, "urb submit failed\n");
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300453
454 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
455 usbdev->bus->busnum, usbdev->devnum);
Jarod Wilson19770692010-07-26 20:32:49 -0300456
Jarod Wilson7a569f52010-08-07 13:31:40 -0300457 /* Load the streamzap not-quite-rc5 decoder too */
458 load_rc5_sz_decode();
459
Jarod Wilson19770692010-07-26 20:32:49 -0300460 return 0;
461
David Härdemand8b4b582010-10-29 16:08:23 -0300462rc_dev_fail:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300463 usb_free_urb(sz->urb_in);
464free_buf_in:
465 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300466free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300467 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300468
469 return retval;
470}
471
Jarod Wilson19770692010-07-26 20:32:49 -0300472/**
473 * streamzap_disconnect
474 *
475 * Called by the usb core when the device is removed from the system.
476 *
477 * This routine guarantees that the driver will not submit any more urbs
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300478 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300479 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
480 * does not provide any way to do this.
481 */
482static void streamzap_disconnect(struct usb_interface *interface)
483{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300484 struct streamzap_ir *sz = usb_get_intfdata(interface);
485 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300486
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300487 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300488
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300489 if (!sz)
490 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300491
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300492 sz->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -0300493 rc_unregister_device(sz->rdev);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300494 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300495 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300496 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300497
Jarod Wilson19770692010-07-26 20:32:49 -0300498 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300499}
500
501static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
502{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300503 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300504
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300505 usb_kill_urb(sz->urb_in);
506
Jarod Wilson19770692010-07-26 20:32:49 -0300507 return 0;
508}
509
510static int streamzap_resume(struct usb_interface *intf)
511{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300512 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300513
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300514 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
515 dev_err(sz->dev, "Error sumbiting urb\n");
516 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300517 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300518
Jarod Wilson19770692010-07-26 20:32:49 -0300519 return 0;
520}
521
522/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300523 * streamzap_init
Jarod Wilson19770692010-07-26 20:32:49 -0300524 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300525static int __init streamzap_init(void)
Jarod Wilson19770692010-07-26 20:32:49 -0300526{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300527 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300528
529 /* register this driver with the USB subsystem */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300530 ret = usb_register(&streamzap_driver);
531 if (ret < 0)
532 printk(KERN_ERR DRIVER_NAME ": usb register failed, "
533 "result = %d\n", ret);
Jarod Wilson19770692010-07-26 20:32:49 -0300534
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300535 return ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300536}
537
538/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300539 * streamzap_exit
Jarod Wilson19770692010-07-26 20:32:49 -0300540 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300541static void __exit streamzap_exit(void)
Jarod Wilson19770692010-07-26 20:32:49 -0300542{
543 usb_deregister(&streamzap_driver);
544}
545
546
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300547module_init(streamzap_init);
548module_exit(streamzap_exit);
Jarod Wilson19770692010-07-26 20:32:49 -0300549
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300550MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300551MODULE_DESCRIPTION(DRIVER_DESC);
552MODULE_LICENSE("GPL");
553
554module_param(debug, bool, S_IRUGO | S_IWUSR);
555MODULE_PARM_DESC(debug, "Enable debugging messages");