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