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