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 | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 41 | #define DRIVER_VERSION "1.60" |
| 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 | |
| 64 | #define STREAMZAP_PULSE_MASK 0xf0 |
| 65 | #define STREAMZAP_SPACE_MASK 0x0f |
| 66 | #define STREAMZAP_TIMEOUT 0xff |
| 67 | #define STREAMZAP_RESOLUTION 256 |
| 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 | |
| 72 | enum StreamzapDecoderState { |
| 73 | PulseSpace, |
| 74 | FullPulse, |
| 75 | FullSpace, |
| 76 | IgnorePulse |
| 77 | }; |
| 78 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 79 | /* structure to hold our device specific stuff */ |
| 80 | struct streamzap_ir { |
| 81 | |
| 82 | /* ir-core */ |
| 83 | struct ir_dev_props *props; |
| 84 | struct ir_raw_event rawir; |
| 85 | |
| 86 | /* core device info */ |
| 87 | struct device *dev; |
| 88 | struct input_dev *idev; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 89 | |
| 90 | /* usb */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 91 | struct usb_device *usbdev; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 92 | struct usb_interface *interface; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 93 | struct usb_endpoint_descriptor *endpoint; |
| 94 | struct urb *urb_in; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 95 | |
| 96 | /* buffer & dma */ |
| 97 | unsigned char *buf_in; |
| 98 | dma_addr_t dma_in; |
| 99 | unsigned int buf_in_len; |
| 100 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 101 | /* timer used to support delay buffering */ |
| 102 | struct timer_list delay_timer; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 103 | bool timer_running; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 104 | spinlock_t timer_lock; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 105 | struct timer_list flush_timer; |
| 106 | bool flush; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 107 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 108 | /* delay buffer */ |
| 109 | struct kfifo fifo; |
| 110 | bool fifo_initialized; |
| 111 | |
| 112 | /* track what state we're in */ |
| 113 | enum StreamzapDecoderState decoder_state; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 114 | /* tracks whether we are currently receiving some signal */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 115 | bool idle; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 116 | /* sum of signal lengths received since signal start */ |
| 117 | unsigned long sum; |
| 118 | /* start time of signal; necessary for gap tracking */ |
| 119 | struct timeval signal_last; |
| 120 | struct timeval signal_start; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 121 | /* bool timeout_enabled; */ |
| 122 | |
| 123 | char name[128]; |
| 124 | char phys[64]; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
| 128 | /* local function prototypes */ |
| 129 | static int streamzap_probe(struct usb_interface *interface, |
| 130 | const struct usb_device_id *id); |
| 131 | static void streamzap_disconnect(struct usb_interface *interface); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 132 | static void streamzap_callback(struct urb *urb); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 133 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message); |
| 134 | static int streamzap_resume(struct usb_interface *intf); |
| 135 | |
| 136 | /* usb specific object needed to register this driver with the usb subsystem */ |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 137 | static struct usb_driver streamzap_driver = { |
| 138 | .name = DRIVER_NAME, |
| 139 | .probe = streamzap_probe, |
| 140 | .disconnect = streamzap_disconnect, |
| 141 | .suspend = streamzap_suspend, |
| 142 | .resume = streamzap_resume, |
| 143 | .id_table = streamzap_table, |
| 144 | }; |
| 145 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 146 | static void streamzap_stop_timer(struct streamzap_ir *sz) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 147 | { |
| 148 | unsigned long flags; |
| 149 | |
| 150 | spin_lock_irqsave(&sz->timer_lock, flags); |
| 151 | if (sz->timer_running) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 152 | sz->timer_running = false; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 153 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 154 | del_timer_sync(&sz->delay_timer); |
| 155 | } else { |
| 156 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 157 | } |
| 158 | } |
| 159 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 160 | static void streamzap_flush_timeout(unsigned long arg) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 161 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 162 | struct streamzap_ir *sz = (struct streamzap_ir *)arg; |
| 163 | |
| 164 | dev_info(sz->dev, "%s: callback firing\n", __func__); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 165 | |
| 166 | /* finally start accepting data */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 167 | sz->flush = false; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 168 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 169 | |
| 170 | static void streamzap_delay_timeout(unsigned long arg) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 171 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 172 | struct streamzap_ir *sz = (struct streamzap_ir *)arg; |
| 173 | struct ir_raw_event rawir = { .pulse = false, .duration = 0 }; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 174 | unsigned long flags; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 175 | int len, ret; |
| 176 | static unsigned long delay; |
| 177 | bool wake = false; |
| 178 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 179 | /* deliver data every 10 ms */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 180 | delay = msecs_to_jiffies(10); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 181 | |
| 182 | spin_lock_irqsave(&sz->timer_lock, flags); |
| 183 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 184 | if (kfifo_len(&sz->fifo) > 0) { |
| 185 | ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir)); |
| 186 | if (ret != sizeof(rawir)) |
| 187 | dev_err(sz->dev, "Problem w/kfifo_out...\n"); |
| 188 | ir_raw_event_store(sz->idev, &rawir); |
| 189 | wake = true; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 190 | } |
| 191 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 192 | len = kfifo_len(&sz->fifo); |
| 193 | if (len > 0) { |
| 194 | while ((len < SZ_BUF_LEN / 2) && |
| 195 | (len < SZ_BUF_LEN * sizeof(int))) { |
| 196 | ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir)); |
| 197 | if (ret != sizeof(rawir)) |
| 198 | dev_err(sz->dev, "Problem w/kfifo_out...\n"); |
| 199 | ir_raw_event_store(sz->idev, &rawir); |
| 200 | wake = true; |
| 201 | len = kfifo_len(&sz->fifo); |
| 202 | } |
| 203 | if (sz->timer_running) |
| 204 | mod_timer(&sz->delay_timer, jiffies + delay); |
| 205 | |
| 206 | } else { |
| 207 | sz->timer_running = false; |
| 208 | } |
| 209 | |
| 210 | if (wake) |
| 211 | ir_raw_event_handle(sz->idev); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 212 | |
| 213 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 214 | } |
| 215 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 216 | static void streamzap_flush_delay_buffer(struct streamzap_ir *sz) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 217 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 218 | struct ir_raw_event rawir = { .pulse = false, .duration = 0 }; |
| 219 | bool wake = false; |
| 220 | int ret; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 221 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 222 | while (kfifo_len(&sz->fifo) > 0) { |
| 223 | ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir)); |
| 224 | if (ret != sizeof(rawir)) |
| 225 | dev_err(sz->dev, "Problem w/kfifo_out...\n"); |
| 226 | ir_raw_event_store(sz->idev, &rawir); |
| 227 | wake = true; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 228 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 229 | |
| 230 | if (wake) |
| 231 | ir_raw_event_handle(sz->idev); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 232 | } |
| 233 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 234 | static void sz_push(struct streamzap_ir *sz) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 235 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 236 | struct ir_raw_event rawir = { .pulse = false, .duration = 0 }; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 237 | unsigned long flags; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 238 | int ret; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 239 | |
| 240 | spin_lock_irqsave(&sz->timer_lock, flags); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 241 | if (kfifo_len(&sz->fifo) >= sizeof(int) * SZ_BUF_LEN) { |
| 242 | ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir)); |
| 243 | if (ret != sizeof(rawir)) |
| 244 | dev_err(sz->dev, "Problem w/kfifo_out...\n"); |
| 245 | ir_raw_event_store(sz->idev, &rawir); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 246 | } |
| 247 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 248 | kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir)); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 249 | |
| 250 | if (!sz->timer_running) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 251 | sz->delay_timer.expires = jiffies + (HZ / 10); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 252 | add_timer(&sz->delay_timer); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 253 | sz->timer_running = true; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 257 | } |
| 258 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 259 | static void sz_push_full_pulse(struct streamzap_ir *sz, |
| 260 | unsigned char value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 261 | { |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 262 | if (sz->idle) { |
| 263 | long deltv; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 264 | |
| 265 | sz->signal_last = sz->signal_start; |
| 266 | do_gettimeofday(&sz->signal_start); |
| 267 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 268 | deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec; |
| 269 | sz->rawir.pulse = false; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 270 | if (deltv > 15) { |
| 271 | /* really long time */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 272 | sz->rawir.duration = IR_MAX_DURATION; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 273 | } else { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 274 | sz->rawir.duration = (int)(deltv * 1000000 + |
| 275 | sz->signal_start.tv_usec - |
| 276 | sz->signal_last.tv_usec); |
| 277 | sz->rawir.duration -= sz->sum; |
| 278 | sz->rawir.duration *= 1000; |
| 279 | sz->rawir.duration &= IR_MAX_DURATION; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 280 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 281 | dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration); |
| 282 | sz_push(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 283 | |
| 284 | sz->idle = 0; |
| 285 | sz->sum = 0; |
| 286 | } |
| 287 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 288 | sz->rawir.pulse = true; |
| 289 | sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION; |
| 290 | sz->rawir.duration += STREAMZAP_RESOLUTION / 2; |
| 291 | sz->sum += sz->rawir.duration; |
| 292 | sz->rawir.duration *= 1000; |
| 293 | sz->rawir.duration &= IR_MAX_DURATION; |
| 294 | dev_dbg(sz->dev, "p %u\n", sz->rawir.duration); |
| 295 | sz_push(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 296 | } |
| 297 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 298 | static void sz_push_half_pulse(struct streamzap_ir *sz, |
| 299 | unsigned char value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 300 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 301 | sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 302 | } |
| 303 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 304 | static void sz_push_full_space(struct streamzap_ir *sz, |
| 305 | unsigned char value) |
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 | sz->rawir.pulse = false; |
| 308 | sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION; |
| 309 | sz->rawir.duration += STREAMZAP_RESOLUTION / 2; |
| 310 | sz->sum += sz->rawir.duration; |
| 311 | sz->rawir.duration *= 1000; |
| 312 | dev_dbg(sz->dev, "s %u\n", sz->rawir.duration); |
| 313 | sz_push(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 314 | } |
| 315 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 316 | static void sz_push_half_space(struct streamzap_ir *sz, |
| 317 | unsigned long value) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 318 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 319 | sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 323 | * streamzap_callback - usb IRQ handler callback |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 324 | * |
| 325 | * This procedure is invoked on reception of data from |
| 326 | * the usb remote. |
| 327 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 328 | static void streamzap_callback(struct urb *urb) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 329 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 330 | struct streamzap_ir *sz; |
| 331 | unsigned int i; |
| 332 | int len; |
| 333 | #if 0 |
| 334 | static int timeout = (((STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) & |
| 335 | IR_MAX_DURATION) | 0x03000000); |
| 336 | #endif |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 337 | |
| 338 | if (!urb) |
| 339 | return; |
| 340 | |
| 341 | sz = urb->context; |
| 342 | len = urb->actual_length; |
| 343 | |
| 344 | switch (urb->status) { |
| 345 | case -ECONNRESET: |
| 346 | case -ENOENT: |
| 347 | case -ESHUTDOWN: |
| 348 | /* |
| 349 | * this urb is terminated, clean up. |
| 350 | * sz might already be invalid at this point |
| 351 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 352 | dev_err(sz->dev, "urb terminated, status: %d\n", urb->status); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 353 | return; |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 358 | dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 359 | if (!sz->flush) { |
| 360 | for (i = 0; i < urb->actual_length; i++) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 361 | dev_dbg(sz->dev, "%d: %x\n", i, |
| 362 | (unsigned char)sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 363 | switch (sz->decoder_state) { |
| 364 | case PulseSpace: |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 365 | if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) == |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 366 | STREAMZAP_PULSE_MASK) { |
| 367 | sz->decoder_state = FullPulse; |
| 368 | continue; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 369 | } else if ((sz->buf_in[i] & STREAMZAP_SPACE_MASK) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 370 | == STREAMZAP_SPACE_MASK) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 371 | sz_push_half_pulse(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 372 | sz->decoder_state = FullSpace; |
| 373 | continue; |
| 374 | } else { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 375 | sz_push_half_pulse(sz, sz->buf_in[i]); |
| 376 | sz_push_half_space(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 377 | } |
| 378 | break; |
| 379 | case FullPulse: |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 380 | sz_push_full_pulse(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 381 | sz->decoder_state = IgnorePulse; |
| 382 | break; |
| 383 | case FullSpace: |
| 384 | if (sz->buf_in[i] == STREAMZAP_TIMEOUT) { |
| 385 | sz->idle = 1; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 386 | streamzap_stop_timer(sz); |
| 387 | #if 0 |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 388 | if (sz->timeout_enabled) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 389 | sz->rawir.pulse = false; |
| 390 | sz->rawir.duration = timeout; |
| 391 | sz->rawir.duration *= 1000; |
| 392 | sz_push(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 393 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 394 | #endif |
| 395 | streamzap_flush_delay_buffer(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 396 | } else |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 397 | sz_push_full_space(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 398 | sz->decoder_state = PulseSpace; |
| 399 | break; |
| 400 | case IgnorePulse: |
| 401 | if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) == |
| 402 | STREAMZAP_SPACE_MASK) { |
| 403 | sz->decoder_state = FullSpace; |
| 404 | continue; |
| 405 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 406 | sz_push_half_space(sz, sz->buf_in[i]); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 407 | sz->decoder_state = PulseSpace; |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | usb_submit_urb(urb, GFP_ATOMIC); |
| 414 | |
| 415 | return; |
| 416 | } |
| 417 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 418 | static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz) |
| 419 | { |
| 420 | struct input_dev *idev; |
| 421 | struct ir_dev_props *props; |
| 422 | struct device *dev = sz->dev; |
| 423 | int ret; |
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 | idev = input_allocate_device(); |
| 426 | if (!idev) { |
| 427 | dev_err(dev, "remote input dev allocation failed\n"); |
| 428 | goto idev_alloc_failed; |
| 429 | } |
| 430 | |
| 431 | props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL); |
| 432 | if (!props) { |
| 433 | dev_err(dev, "remote ir dev props allocation failed\n"); |
| 434 | goto props_alloc_failed; |
| 435 | } |
| 436 | |
| 437 | snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared " |
| 438 | "Receiver (%04x:%04x)", |
| 439 | le16_to_cpu(sz->usbdev->descriptor.idVendor), |
| 440 | le16_to_cpu(sz->usbdev->descriptor.idProduct)); |
| 441 | |
| 442 | idev->name = sz->name; |
| 443 | usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys)); |
| 444 | strlcat(sz->phys, "/input0", sizeof(sz->phys)); |
| 445 | idev->phys = sz->phys; |
| 446 | |
| 447 | props->priv = sz; |
| 448 | props->driver_type = RC_DRIVER_IR_RAW; |
| 449 | /* FIXME: not sure about supported protocols, check on this */ |
| 450 | props->allowed_protos = IR_TYPE_RC5 | IR_TYPE_RC6; |
| 451 | |
| 452 | sz->props = props; |
| 453 | |
| 454 | ret = ir_input_register(idev, RC_MAP_RC5_STREAMZAP, props, DRIVER_NAME); |
| 455 | if (ret < 0) { |
| 456 | dev_err(dev, "remote input device register failed\n"); |
| 457 | goto irdev_failed; |
| 458 | } |
| 459 | |
| 460 | return idev; |
| 461 | |
| 462 | irdev_failed: |
| 463 | kfree(props); |
| 464 | props_alloc_failed: |
| 465 | input_free_device(idev); |
| 466 | idev_alloc_failed: |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | static int streamzap_delay_buf_init(struct streamzap_ir *sz) |
| 471 | { |
| 472 | int ret; |
| 473 | |
| 474 | ret = kfifo_alloc(&sz->fifo, sizeof(int) * SZ_BUF_LEN, |
| 475 | GFP_KERNEL); |
| 476 | if (ret == 0) |
| 477 | sz->fifo_initialized = 1; |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static void streamzap_start_flush_timer(struct streamzap_ir *sz) |
| 483 | { |
| 484 | sz->flush_timer.expires = jiffies + HZ; |
| 485 | sz->flush = true; |
| 486 | add_timer(&sz->flush_timer); |
| 487 | |
| 488 | sz->urb_in->dev = sz->usbdev; |
| 489 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) |
| 490 | dev_err(sz->dev, "urb submit failed\n"); |
| 491 | } |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 492 | |
| 493 | /** |
| 494 | * streamzap_probe |
| 495 | * |
| 496 | * Called by usb-core to associated with a candidate device |
| 497 | * On any failure the return value is the ERROR |
| 498 | * On success return 0 |
| 499 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 500 | static int __devinit streamzap_probe(struct usb_interface *intf, |
| 501 | const struct usb_device_id *id) |
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 | struct usb_device *usbdev = interface_to_usbdev(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 504 | struct usb_host_interface *iface_host; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 505 | struct streamzap_ir *sz = NULL; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 506 | char buf[63], name[128] = ""; |
| 507 | int retval = -ENOMEM; |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 508 | int pipe, maxp; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 509 | |
| 510 | /* Allocate space for device driver specific data */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 511 | sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL); |
| 512 | if (!sz) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 513 | return -ENOMEM; |
| 514 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 515 | sz->usbdev = usbdev; |
| 516 | sz->interface = intf; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 517 | |
| 518 | /* Check to ensure endpoint information matches requirements */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 519 | iface_host = intf->cur_altsetting; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 520 | |
| 521 | if (iface_host->desc.bNumEndpoints != 1) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 522 | dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n", |
| 523 | __func__, iface_host->desc.bNumEndpoints); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 524 | retval = -ENODEV; |
| 525 | goto free_sz; |
| 526 | } |
| 527 | |
| 528 | sz->endpoint = &(iface_host->endpoint[0].desc); |
| 529 | if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
| 530 | != USB_DIR_IN) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 531 | dev_err(&intf->dev, "%s: endpoint doesn't match input device " |
| 532 | "02%02x\n", __func__, sz->endpoint->bEndpointAddress); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 533 | retval = -ENODEV; |
| 534 | goto free_sz; |
| 535 | } |
| 536 | |
| 537 | if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 538 | != USB_ENDPOINT_XFER_INT) { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 539 | dev_err(&intf->dev, "%s: endpoint attributes don't match xfer " |
| 540 | "02%02x\n", __func__, sz->endpoint->bmAttributes); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 541 | retval = -ENODEV; |
| 542 | goto free_sz; |
| 543 | } |
| 544 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 545 | pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress); |
| 546 | maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe)); |
| 547 | |
| 548 | if (maxp == 0) { |
| 549 | dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n", |
| 550 | __func__); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 551 | retval = -ENODEV; |
| 552 | goto free_sz; |
| 553 | } |
| 554 | |
| 555 | /* Allocate the USB buffer and IRQ URB */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 556 | sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in); |
| 557 | if (!sz->buf_in) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 558 | goto free_sz; |
| 559 | |
| 560 | sz->urb_in = usb_alloc_urb(0, GFP_KERNEL); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 561 | if (!sz->urb_in) |
| 562 | goto free_buf_in; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 563 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 564 | sz->dev = &intf->dev; |
| 565 | sz->buf_in_len = maxp; |
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 | if (usbdev->descriptor.iManufacturer |
| 568 | && usb_string(usbdev, usbdev->descriptor.iManufacturer, |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 569 | buf, sizeof(buf)) > 0) |
| 570 | strlcpy(name, buf, sizeof(name)); |
| 571 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 572 | if (usbdev->descriptor.iProduct |
| 573 | && usb_string(usbdev, usbdev->descriptor.iProduct, |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 574 | buf, sizeof(buf)) > 0) |
| 575 | snprintf(name + strlen(name), sizeof(name) - strlen(name), |
| 576 | " %s", buf); |
| 577 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 578 | retval = streamzap_delay_buf_init(sz); |
| 579 | if (retval) { |
| 580 | dev_err(&intf->dev, "%s: delay buffer init failed\n", __func__); |
| 581 | goto free_urb_in; |
| 582 | } |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 583 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 584 | sz->idev = streamzap_init_input_dev(sz); |
| 585 | if (!sz->idev) |
| 586 | goto input_dev_fail; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 587 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 588 | sz->idle = true; |
| 589 | sz->decoder_state = PulseSpace; |
| 590 | #if 0 |
| 591 | /* not yet supported, depends on patches from maxim */ |
| 592 | /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */ |
| 593 | sz->timeout_enabled = false; |
| 594 | sz->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000; |
| 595 | sz->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000; |
| 596 | #endif |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 597 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 598 | init_timer(&sz->delay_timer); |
| 599 | sz->delay_timer.function = streamzap_delay_timeout; |
| 600 | sz->delay_timer.data = (unsigned long)sz; |
| 601 | spin_lock_init(&sz->timer_lock); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 602 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 603 | init_timer(&sz->flush_timer); |
| 604 | sz->flush_timer.function = streamzap_flush_timeout; |
| 605 | sz->flush_timer.data = (unsigned long)sz; |
| 606 | |
| 607 | do_gettimeofday(&sz->signal_start); |
| 608 | |
| 609 | /* Complete final initialisations */ |
| 610 | usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in, |
| 611 | maxp, (usb_complete_t)streamzap_callback, |
| 612 | sz, sz->endpoint->bInterval); |
| 613 | sz->urb_in->transfer_dma = sz->dma_in; |
| 614 | sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 615 | |
| 616 | usb_set_intfdata(intf, sz); |
| 617 | |
| 618 | streamzap_start_flush_timer(sz); |
| 619 | |
| 620 | dev_info(sz->dev, "Registered %s on usb%d:%d\n", name, |
| 621 | usbdev->bus->busnum, usbdev->devnum); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 622 | |
| 623 | return 0; |
| 624 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 625 | input_dev_fail: |
| 626 | kfifo_free(&sz->fifo); |
| 627 | free_urb_in: |
| 628 | usb_free_urb(sz->urb_in); |
| 629 | free_buf_in: |
| 630 | usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 631 | free_sz: |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 632 | kfree(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 633 | |
| 634 | return retval; |
| 635 | } |
| 636 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 637 | /** |
| 638 | * streamzap_disconnect |
| 639 | * |
| 640 | * Called by the usb core when the device is removed from the system. |
| 641 | * |
| 642 | * This routine guarantees that the driver will not submit any more urbs |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 643 | * by clearing dev->usbdev. It is also supposed to terminate any currently |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 644 | * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(), |
| 645 | * does not provide any way to do this. |
| 646 | */ |
| 647 | static void streamzap_disconnect(struct usb_interface *interface) |
| 648 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 649 | struct streamzap_ir *sz = usb_get_intfdata(interface); |
| 650 | struct usb_device *usbdev = interface_to_usbdev(interface); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 651 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 652 | usb_set_intfdata(interface, NULL); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 653 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 654 | if (!sz) |
| 655 | return; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 656 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 657 | if (sz->flush) { |
| 658 | sz->flush = false; |
| 659 | del_timer_sync(&sz->flush_timer); |
| 660 | } |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 661 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 662 | streamzap_stop_timer(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 663 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 664 | sz->usbdev = NULL; |
| 665 | ir_input_unregister(sz->idev); |
| 666 | usb_kill_urb(sz->urb_in); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 667 | usb_free_urb(sz->urb_in); |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 668 | 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] | 669 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 670 | kfree(sz); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message) |
| 674 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 675 | struct streamzap_ir *sz = usb_get_intfdata(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 676 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 677 | if (sz->flush) { |
| 678 | sz->flush = false; |
| 679 | del_timer_sync(&sz->flush_timer); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 680 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 681 | |
| 682 | streamzap_stop_timer(sz); |
| 683 | |
| 684 | usb_kill_urb(sz->urb_in); |
| 685 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int streamzap_resume(struct usb_interface *intf) |
| 690 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 691 | struct streamzap_ir *sz = usb_get_intfdata(intf); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 692 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 693 | if (sz->fifo_initialized) |
| 694 | kfifo_reset(&sz->fifo); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 695 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 696 | sz->flush_timer.expires = jiffies + HZ; |
| 697 | sz->flush = true; |
| 698 | add_timer(&sz->flush_timer); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 699 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 700 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) { |
| 701 | dev_err(sz->dev, "Error sumbiting urb\n"); |
| 702 | return -EIO; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 703 | } |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 704 | |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | /** |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 709 | * streamzap_init |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 710 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 711 | static int __init streamzap_init(void) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 712 | { |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 713 | int ret; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 714 | |
| 715 | /* register this driver with the USB subsystem */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 716 | ret = usb_register(&streamzap_driver); |
| 717 | if (ret < 0) |
| 718 | printk(KERN_ERR DRIVER_NAME ": usb register failed, " |
| 719 | "result = %d\n", ret); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 720 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 721 | return ret; |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 725 | * streamzap_exit |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 726 | */ |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 727 | static void __exit streamzap_exit(void) |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 728 | { |
| 729 | usb_deregister(&streamzap_driver); |
| 730 | } |
| 731 | |
| 732 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 733 | module_init(streamzap_init); |
| 734 | module_exit(streamzap_exit); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 735 | |
Jarod Wilson | 8e9e606 | 2010-08-03 01:07:04 -0300 | [diff] [blame] | 736 | MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>"); |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 737 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 738 | MODULE_LICENSE("GPL"); |
| 739 | |
| 740 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 741 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |