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