Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Streamzap Remote Control driver |
| 3 | * |
| 4 | * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de> |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 5 | * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com> |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 6 | * |
| 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 Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 14 | * Ported to in-kernel ir-core interface by Jarod Wilson |
| 15 | * |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 16 | * 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 Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 34 | #include <linux/device.h> |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 35 | #include <linux/module.h> |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 36 | #include <linux/slab.h> |
Paul Bender | 635f76b | 2010-12-16 13:23:07 -0300 | [diff] [blame] | 37 | #include <linux/usb.h> |
| 38 | #include <linux/usb/input.h> |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 39 | #include <media/rc-core.h> |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 40 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 41 | #define DRIVER_VERSION "1.61" |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 42 | #define DRIVER_NAME "streamzap" |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 43 | #define DRIVER_DESC "Streamzap Remote Control driver" |
| 44 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 45 | #define USB_STREAMZAP_VENDOR_ID 0x0e9c |
| 46 | #define USB_STREAMZAP_PRODUCT_ID 0x0000 |
| 47 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 48 | /* table of devices that work with this driver */ |
| 49 | static 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 | |
| 56 | MODULE_DEVICE_TABLE(usb, streamzap_table); |
| 57 | |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 58 | #define SZ_PULSE_MASK 0xf0 |
| 59 | #define SZ_SPACE_MASK 0x0f |
| 60 | #define SZ_TIMEOUT 0xff |
| 61 | #define SZ_RESOLUTION 256 |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 62 | |
| 63 | /* number of samples buffered */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 64 | #define SZ_BUF_LEN 128 |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 65 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 66 | /* from ir-rc5-sz-decoder.c */ |
| 67 | #ifdef CONFIG_IR_RC5_SZ_DECODER_MODULE |
| 68 | #define load_rc5_sz_decode() request_module("ir-rc5-sz-decoder") |
| 69 | #else |
Mauro Carvalho Chehab | 0170f6b | 2010-12-27 13:00:04 -0300 | [diff] [blame] | 70 | #define load_rc5_sz_decode() {} |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 71 | #endif |
| 72 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 73 | enum StreamzapDecoderState { |
| 74 | PulseSpace, |
| 75 | FullPulse, |
| 76 | FullSpace, |
| 77 | IgnorePulse |
| 78 | }; |
| 79 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 80 | /* structure to hold our device specific stuff */ |
| 81 | struct streamzap_ir { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 82 | /* ir-core */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 83 | struct rc_dev *rdev; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 84 | |
| 85 | /* core device info */ |
| 86 | struct device *dev; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 87 | |
| 88 | /* usb */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 89 | struct usb_device *usbdev; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 90 | struct usb_interface *interface; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 91 | struct usb_endpoint_descriptor *endpoint; |
| 92 | struct urb *urb_in; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 93 | |
| 94 | /* buffer & dma */ |
| 95 | unsigned char *buf_in; |
| 96 | dma_addr_t dma_in; |
| 97 | unsigned int buf_in_len; |
| 98 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 99 | /* track what state we're in */ |
| 100 | enum StreamzapDecoderState decoder_state; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 101 | /* tracks whether we are currently receiving some signal */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 102 | bool idle; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 103 | /* sum of signal lengths received since signal start */ |
| 104 | unsigned long sum; |
| 105 | /* start time of signal; necessary for gap tracking */ |
| 106 | struct timeval signal_last; |
| 107 | struct timeval signal_start; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 108 | bool timeout_enabled; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 109 | |
| 110 | char name[128]; |
| 111 | char phys[64]; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | |
| 115 | /* local function prototypes */ |
| 116 | static int streamzap_probe(struct usb_interface *interface, |
| 117 | const struct usb_device_id *id); |
| 118 | static void streamzap_disconnect(struct usb_interface *interface); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 119 | static void streamzap_callback(struct urb *urb); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 120 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message); |
| 121 | static int streamzap_resume(struct usb_interface *intf); |
| 122 | |
| 123 | /* usb specific object needed to register this driver with the usb subsystem */ |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 124 | static struct usb_driver streamzap_driver = { |
| 125 | .name = DRIVER_NAME, |
| 126 | .probe = streamzap_probe, |
| 127 | .disconnect = streamzap_disconnect, |
| 128 | .suspend = streamzap_suspend, |
| 129 | .resume = streamzap_resume, |
| 130 | .id_table = streamzap_table, |
| 131 | }; |
| 132 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 133 | static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 134 | { |
Jarod Wilson | 1338c92 | 2010-11-17 12:25:45 -0300 | [diff] [blame] | 135 | dev_dbg(sz->dev, "Storing %s with duration %u us\n", |
| 136 | (rawir.pulse ? "pulse" : "space"), rawir.duration); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 137 | ir_raw_event_store_with_filter(sz->rdev, &rawir); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 138 | } |
| 139 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 140 | static void sz_push_full_pulse(struct streamzap_ir *sz, |
| 141 | unsigned char value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 142 | { |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 143 | DEFINE_IR_RAW_EVENT(rawir); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 144 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 145 | if (sz->idle) { |
| 146 | long deltv; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 147 | |
| 148 | sz->signal_last = sz->signal_start; |
| 149 | do_gettimeofday(&sz->signal_start); |
| 150 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 151 | deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 152 | rawir.pulse = false; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 153 | if (deltv > 15) { |
| 154 | /* really long time */ |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 155 | rawir.duration = IR_MAX_DURATION; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 156 | } else { |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 157 | rawir.duration = (int)(deltv * 1000000 + |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 158 | sz->signal_start.tv_usec - |
| 159 | sz->signal_last.tv_usec); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 160 | rawir.duration -= sz->sum; |
Jarod Wilson | b4608fa | 2011-01-18 17:31:24 -0300 | [diff] [blame] | 161 | rawir.duration = US_TO_NS(rawir.duration); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 162 | rawir.duration &= IR_MAX_DURATION; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 163 | } |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 164 | sz_push(sz, rawir); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 165 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 166 | sz->idle = false; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 167 | sz->sum = 0; |
| 168 | } |
| 169 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 170 | rawir.pulse = true; |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 171 | rawir.duration = ((int) value) * SZ_RESOLUTION; |
| 172 | rawir.duration += SZ_RESOLUTION / 2; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 173 | sz->sum += rawir.duration; |
Jarod Wilson | b4608fa | 2011-01-18 17:31:24 -0300 | [diff] [blame] | 174 | rawir.duration = US_TO_NS(rawir.duration); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 175 | rawir.duration &= IR_MAX_DURATION; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 176 | sz_push(sz, rawir); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 177 | } |
| 178 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 179 | static void sz_push_half_pulse(struct streamzap_ir *sz, |
| 180 | unsigned char value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 181 | { |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 182 | sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 183 | } |
| 184 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 185 | static void sz_push_full_space(struct streamzap_ir *sz, |
| 186 | unsigned char value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 187 | { |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 188 | DEFINE_IR_RAW_EVENT(rawir); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 189 | |
| 190 | rawir.pulse = false; |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 191 | rawir.duration = ((int) value) * SZ_RESOLUTION; |
| 192 | rawir.duration += SZ_RESOLUTION / 2; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 193 | sz->sum += rawir.duration; |
Jarod Wilson | b4608fa | 2011-01-18 17:31:24 -0300 | [diff] [blame] | 194 | rawir.duration = US_TO_NS(rawir.duration); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 195 | sz_push(sz, rawir); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 196 | } |
| 197 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 198 | static void sz_push_half_space(struct streamzap_ir *sz, |
| 199 | unsigned long value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 200 | { |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 201 | sz_push_full_space(sz, value & SZ_SPACE_MASK); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 205 | * streamzap_callback - usb IRQ handler callback |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 206 | * |
| 207 | * This procedure is invoked on reception of data from |
| 208 | * the usb remote. |
| 209 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 210 | static void streamzap_callback(struct urb *urb) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 211 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 212 | struct streamzap_ir *sz; |
| 213 | unsigned int i; |
| 214 | int len; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 215 | |
| 216 | if (!urb) |
| 217 | return; |
| 218 | |
| 219 | sz = urb->context; |
| 220 | len = urb->actual_length; |
| 221 | |
| 222 | switch (urb->status) { |
| 223 | case -ECONNRESET: |
| 224 | case -ENOENT: |
| 225 | case -ESHUTDOWN: |
| 226 | /* |
| 227 | * this urb is terminated, clean up. |
| 228 | * sz might already be invalid at this point |
| 229 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 230 | dev_err(sz->dev, "urb terminated, status: %d\n", urb->status); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 231 | return; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 236 | dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 237 | for (i = 0; i < len; i++) { |
Jarod Wilson | 1338c92 | 2010-11-17 12:25:45 -0300 | [diff] [blame] | 238 | dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n", |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 239 | i, (unsigned char)sz->buf_in[i]); |
| 240 | switch (sz->decoder_state) { |
| 241 | case PulseSpace: |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 242 | if ((sz->buf_in[i] & SZ_PULSE_MASK) == |
| 243 | SZ_PULSE_MASK) { |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 244 | sz->decoder_state = FullPulse; |
| 245 | continue; |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 246 | } else if ((sz->buf_in[i] & SZ_SPACE_MASK) |
| 247 | == SZ_SPACE_MASK) { |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 248 | sz_push_half_pulse(sz, sz->buf_in[i]); |
| 249 | sz->decoder_state = FullSpace; |
| 250 | continue; |
| 251 | } else { |
| 252 | sz_push_half_pulse(sz, sz->buf_in[i]); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 253 | sz_push_half_space(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 254 | } |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 255 | break; |
| 256 | case FullPulse: |
| 257 | sz_push_full_pulse(sz, sz->buf_in[i]); |
| 258 | sz->decoder_state = IgnorePulse; |
| 259 | break; |
| 260 | case FullSpace: |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 261 | if (sz->buf_in[i] == SZ_TIMEOUT) { |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 262 | DEFINE_IR_RAW_EVENT(rawir); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 263 | |
| 264 | rawir.pulse = false; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 265 | rawir.duration = sz->rdev->timeout; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 266 | sz->idle = true; |
| 267 | if (sz->timeout_enabled) |
| 268 | sz_push(sz, rawir); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 269 | ir_raw_event_handle(sz->rdev); |
Jarod Wilson | 56b0ec3 | 2011-01-27 15:08:35 -0300 | [diff] [blame] | 270 | ir_raw_event_reset(sz->rdev); |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 271 | } else { |
| 272 | sz_push_full_space(sz, sz->buf_in[i]); |
| 273 | } |
| 274 | sz->decoder_state = PulseSpace; |
| 275 | break; |
| 276 | case IgnorePulse: |
Jarod Wilson | b768d47 | 2010-10-12 10:41:27 -0300 | [diff] [blame] | 277 | if ((sz->buf_in[i] & SZ_SPACE_MASK) == |
| 278 | SZ_SPACE_MASK) { |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 279 | sz->decoder_state = FullSpace; |
| 280 | continue; |
| 281 | } |
| 282 | sz_push_half_space(sz, sz->buf_in[i]); |
| 283 | sz->decoder_state = PulseSpace; |
| 284 | break; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Jarod Wilson | 56b0ec3 | 2011-01-27 15:08:35 -0300 | [diff] [blame] | 288 | ir_raw_event_handle(sz->rdev); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 289 | usb_submit_urb(urb, GFP_ATOMIC); |
| 290 | |
| 291 | return; |
| 292 | } |
| 293 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 294 | static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz) |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 295 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 296 | struct rc_dev *rdev; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 297 | struct device *dev = sz->dev; |
| 298 | int ret; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 299 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 300 | rdev = rc_allocate_device(); |
| 301 | if (!rdev) { |
| 302 | dev_err(dev, "remote dev allocation failed\n"); |
| 303 | goto out; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared " |
| 307 | "Receiver (%04x:%04x)", |
| 308 | le16_to_cpu(sz->usbdev->descriptor.idVendor), |
| 309 | le16_to_cpu(sz->usbdev->descriptor.idProduct)); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 310 | usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys)); |
| 311 | strlcat(sz->phys, "/input0", sizeof(sz->phys)); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 312 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 313 | rdev->input_name = sz->name; |
| 314 | rdev->input_phys = sz->phys; |
Paul Bender | 5ad1a55 | 2010-11-17 16:56:17 -0300 | [diff] [blame] | 315 | usb_to_input_id(sz->usbdev, &rdev->input_id); |
| 316 | rdev->dev.parent = dev; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 317 | rdev->priv = sz; |
| 318 | rdev->driver_type = RC_DRIVER_IR_RAW; |
James Hogan | 1a1934f | 2014-02-28 20:17:03 -0300 | [diff] [blame] | 319 | rc_set_allowed_protocols(rdev, RC_BIT_ALL); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 320 | rdev->driver_name = DRIVER_NAME; |
| 321 | rdev->map_name = RC_MAP_STREAMZAP; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 322 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 323 | ret = rc_register_device(rdev); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 324 | if (ret < 0) { |
| 325 | dev_err(dev, "remote input device register failed\n"); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 326 | goto out; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 327 | } |
| 328 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 329 | return rdev; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 330 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 331 | out: |
| 332 | rc_free_device(rdev); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 336 | /** |
| 337 | * streamzap_probe |
| 338 | * |
| 339 | * Called by usb-core to associated with a candidate device |
| 340 | * On any failure the return value is the ERROR |
| 341 | * On success return 0 |
| 342 | */ |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 343 | static int streamzap_probe(struct usb_interface *intf, |
| 344 | const struct usb_device_id *id) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 345 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 346 | struct usb_device *usbdev = interface_to_usbdev(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 347 | struct usb_host_interface *iface_host; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 348 | struct streamzap_ir *sz = NULL; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 349 | char buf[63], name[128] = ""; |
| 350 | int retval = -ENOMEM; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 351 | int pipe, maxp; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 352 | |
| 353 | /* Allocate space for device driver specific data */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 354 | sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL); |
| 355 | if (!sz) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 356 | return -ENOMEM; |
| 357 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 358 | sz->usbdev = usbdev; |
| 359 | sz->interface = intf; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 360 | |
| 361 | /* Check to ensure endpoint information matches requirements */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 362 | iface_host = intf->cur_altsetting; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 363 | |
| 364 | if (iface_host->desc.bNumEndpoints != 1) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 365 | dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n", |
| 366 | __func__, iface_host->desc.bNumEndpoints); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 367 | retval = -ENODEV; |
| 368 | goto free_sz; |
| 369 | } |
| 370 | |
| 371 | sz->endpoint = &(iface_host->endpoint[0].desc); |
| 372 | if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
| 373 | != USB_DIR_IN) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 374 | dev_err(&intf->dev, "%s: endpoint doesn't match input device " |
| 375 | "02%02x\n", __func__, sz->endpoint->bEndpointAddress); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 376 | retval = -ENODEV; |
| 377 | goto free_sz; |
| 378 | } |
| 379 | |
| 380 | if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 381 | != USB_ENDPOINT_XFER_INT) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 382 | dev_err(&intf->dev, "%s: endpoint attributes don't match xfer " |
| 383 | "02%02x\n", __func__, sz->endpoint->bmAttributes); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 384 | retval = -ENODEV; |
| 385 | goto free_sz; |
| 386 | } |
| 387 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 388 | pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress); |
| 389 | maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe)); |
| 390 | |
| 391 | if (maxp == 0) { |
| 392 | dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n", |
| 393 | __func__); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 394 | retval = -ENODEV; |
| 395 | goto free_sz; |
| 396 | } |
| 397 | |
| 398 | /* Allocate the USB buffer and IRQ URB */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 399 | sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in); |
| 400 | if (!sz->buf_in) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 401 | goto free_sz; |
| 402 | |
| 403 | sz->urb_in = usb_alloc_urb(0, GFP_KERNEL); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 404 | if (!sz->urb_in) |
| 405 | goto free_buf_in; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 406 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 407 | sz->dev = &intf->dev; |
| 408 | sz->buf_in_len = maxp; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 409 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 410 | if (usbdev->descriptor.iManufacturer |
| 411 | && usb_string(usbdev, usbdev->descriptor.iManufacturer, |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 412 | buf, sizeof(buf)) > 0) |
| 413 | strlcpy(name, buf, sizeof(name)); |
| 414 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 415 | if (usbdev->descriptor.iProduct |
| 416 | && usb_string(usbdev, usbdev->descriptor.iProduct, |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 417 | buf, sizeof(buf)) > 0) |
| 418 | snprintf(name + strlen(name), sizeof(name) - strlen(name), |
| 419 | " %s", buf); |
| 420 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 421 | sz->rdev = streamzap_init_rc_dev(sz); |
| 422 | if (!sz->rdev) |
| 423 | goto rc_dev_fail; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 424 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 425 | sz->idle = true; |
| 426 | sz->decoder_state = PulseSpace; |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 427 | /* FIXME: don't yet have a way to set this */ |
| 428 | sz->timeout_enabled = true; |
Jarod Wilson | b4608fa | 2011-01-18 17:31:24 -0300 | [diff] [blame] | 429 | sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) & |
Jarod Wilson | 1338c92 | 2010-11-17 12:25:45 -0300 | [diff] [blame] | 430 | IR_MAX_DURATION) | 0x03000000); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 431 | #if 0 |
| 432 | /* not yet supported, depends on patches from maxim */ |
| 433 | /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */ |
Jarod Wilson | b4608fa | 2011-01-18 17:31:24 -0300 | [diff] [blame] | 434 | sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION); |
| 435 | sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 436 | #endif |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 437 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 438 | do_gettimeofday(&sz->signal_start); |
| 439 | |
| 440 | /* Complete final initialisations */ |
| 441 | usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in, |
| 442 | maxp, (usb_complete_t)streamzap_callback, |
| 443 | sz, sz->endpoint->bInterval); |
| 444 | sz->urb_in->transfer_dma = sz->dma_in; |
| 445 | sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 446 | |
| 447 | usb_set_intfdata(intf, sz); |
| 448 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 449 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) |
| 450 | dev_err(sz->dev, "urb submit failed\n"); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 451 | |
| 452 | dev_info(sz->dev, "Registered %s on usb%d:%d\n", name, |
| 453 | usbdev->bus->busnum, usbdev->devnum); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 454 | |
Jarod Wilson | 7a569f5 | 2010-08-07 13:31:40 -0300 | [diff] [blame] | 455 | /* Load the streamzap not-quite-rc5 decoder too */ |
| 456 | load_rc5_sz_decode(); |
| 457 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 458 | return 0; |
| 459 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 460 | rc_dev_fail: |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 461 | usb_free_urb(sz->urb_in); |
| 462 | free_buf_in: |
| 463 | usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 464 | free_sz: |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 465 | kfree(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 466 | |
| 467 | return retval; |
| 468 | } |
| 469 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 470 | /** |
| 471 | * streamzap_disconnect |
| 472 | * |
| 473 | * Called by the usb core when the device is removed from the system. |
| 474 | * |
| 475 | * This routine guarantees that the driver will not submit any more urbs |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 476 | * by clearing dev->usbdev. It is also supposed to terminate any currently |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 477 | * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(), |
| 478 | * does not provide any way to do this. |
| 479 | */ |
| 480 | static void streamzap_disconnect(struct usb_interface *interface) |
| 481 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 482 | struct streamzap_ir *sz = usb_get_intfdata(interface); |
| 483 | struct usb_device *usbdev = interface_to_usbdev(interface); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 484 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 485 | usb_set_intfdata(interface, NULL); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 486 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 487 | if (!sz) |
| 488 | return; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 489 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 490 | sz->usbdev = NULL; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 491 | rc_unregister_device(sz->rdev); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 492 | usb_kill_urb(sz->urb_in); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 493 | usb_free_urb(sz->urb_in); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 494 | usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 495 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 496 | kfree(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message) |
| 500 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 501 | struct streamzap_ir *sz = usb_get_intfdata(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 502 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 503 | usb_kill_urb(sz->urb_in); |
| 504 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int streamzap_resume(struct usb_interface *intf) |
| 509 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 510 | struct streamzap_ir *sz = usb_get_intfdata(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 511 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 512 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) { |
| 513 | dev_err(sz->dev, "Error sumbiting urb\n"); |
| 514 | return -EIO; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 515 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 516 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Greg Kroah-Hartman | ecb3b2b | 2011-11-18 09:46:12 -0800 | [diff] [blame] | 520 | module_usb_driver(streamzap_driver); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 521 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 522 | MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>"); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 523 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 524 | MODULE_LICENSE("GPL"); |