1 // SPDX-License-Identifier: GPL-2.0 2 // rc-ir-raw.c - handle IR pulse/space events 3 // 4 // Copyright (C) 2010 by Mauro Carvalho Chehab 5 6 #include <linux/export.h> 7 #include <linux/kthread.h> 8 #include <linux/mutex.h> 9 #include <linux/kmod.h> 10 #include <linux/sched.h> 11 #include "rc-core-priv.h" 12 13 /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */ 14 static LIST_HEAD(ir_raw_client_list); 15 16 /* Used to handle IR raw handler extensions */ 17 static DEFINE_MUTEX(ir_raw_handler_lock); 18 static LIST_HEAD(ir_raw_handler_list); 19 static atomic64_t available_protocols = ATOMIC64_INIT(0); 20 21 static int ir_raw_event_thread(void *data) 22 { 23 struct ir_raw_event ev; 24 struct ir_raw_handler *handler; 25 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data; 26 27 while (1) { 28 mutex_lock(&ir_raw_handler_lock); 29 while (kfifo_out(&raw->kfifo, &ev, 1)) { 30 list_for_each_entry(handler, &ir_raw_handler_list, list) 31 if (raw->dev->enabled_protocols & 32 handler->protocols || !handler->protocols) 33 handler->decode(raw->dev, ev); 34 ir_lirc_raw_event(raw->dev, ev); 35 raw->prev_ev = ev; 36 } 37 mutex_unlock(&ir_raw_handler_lock); 38 39 set_current_state(TASK_INTERRUPTIBLE); 40 41 if (kthread_should_stop()) { 42 __set_current_state(TASK_RUNNING); 43 break; 44 } else if (!kfifo_is_empty(&raw->kfifo)) 45 set_current_state(TASK_RUNNING); 46 47 schedule(); 48 } 49 50 return 0; 51 } 52 53 /** 54 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders 55 * @dev: the struct rc_dev device descriptor 56 * @ev: the struct ir_raw_event descriptor of the pulse/space 57 * 58 * This routine (which may be called from an interrupt context) stores a 59 * pulse/space duration for the raw ir decoding state machines. Pulses are 60 * signalled as positive values and spaces as negative values. A zero value 61 * will reset the decoding state machines. 62 */ 63 int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev) 64 { 65 if (!dev->raw) 66 return -EINVAL; 67 68 IR_dprintk(2, "sample: (%05dus %s)\n", 69 TO_US(ev->duration), TO_STR(ev->pulse)); 70 71 if (!kfifo_put(&dev->raw->kfifo, *ev)) { 72 dev_err(&dev->dev, "IR event FIFO is full!\n"); 73 return -ENOSPC; 74 } 75 76 return 0; 77 } 78 EXPORT_SYMBOL_GPL(ir_raw_event_store); 79 80 /** 81 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space 82 * @dev: the struct rc_dev device descriptor 83 * @pulse: true for pulse, false for space 84 * 85 * This routine (which may be called from an interrupt context) is used to 86 * store the beginning of an ir pulse or space (or the start/end of ir 87 * reception) for the raw ir decoding state machines. This is used by 88 * hardware which does not provide durations directly but only interrupts 89 * (or similar events) on state change. 90 */ 91 int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse) 92 { 93 ktime_t now; 94 DEFINE_IR_RAW_EVENT(ev); 95 int rc = 0; 96 97 if (!dev->raw) 98 return -EINVAL; 99 100 now = ktime_get(); 101 ev.duration = ktime_to_ns(ktime_sub(now, dev->raw->last_event)); 102 ev.pulse = !pulse; 103 104 rc = ir_raw_event_store(dev, &ev); 105 106 dev->raw->last_event = now; 107 108 /* timer could be set to timeout (125ms by default) */ 109 if (!timer_pending(&dev->raw->edge_handle) || 110 time_after(dev->raw->edge_handle.expires, 111 jiffies + msecs_to_jiffies(15))) { 112 mod_timer(&dev->raw->edge_handle, 113 jiffies + msecs_to_jiffies(15)); 114 } 115 116 return rc; 117 } 118 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge); 119 120 /** 121 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing 122 * @dev: the struct rc_dev device descriptor 123 * @ev: the event that has occurred 124 * 125 * This routine (which may be called from an interrupt context) works 126 * in similar manner to ir_raw_event_store_edge. 127 * This routine is intended for devices with limited internal buffer 128 * It automerges samples of same type, and handles timeouts. Returns non-zero 129 * if the event was added, and zero if the event was ignored due to idle 130 * processing. 131 */ 132 int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev) 133 { 134 if (!dev->raw) 135 return -EINVAL; 136 137 /* Ignore spaces in idle mode */ 138 if (dev->idle && !ev->pulse) 139 return 0; 140 else if (dev->idle) 141 ir_raw_event_set_idle(dev, false); 142 143 if (!dev->raw->this_ev.duration) 144 dev->raw->this_ev = *ev; 145 else if (ev->pulse == dev->raw->this_ev.pulse) 146 dev->raw->this_ev.duration += ev->duration; 147 else { 148 ir_raw_event_store(dev, &dev->raw->this_ev); 149 dev->raw->this_ev = *ev; 150 } 151 152 /* Enter idle mode if nessesary */ 153 if (!ev->pulse && dev->timeout && 154 dev->raw->this_ev.duration >= dev->timeout) 155 ir_raw_event_set_idle(dev, true); 156 157 return 1; 158 } 159 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter); 160 161 /** 162 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not 163 * @dev: the struct rc_dev device descriptor 164 * @idle: whether the device is idle or not 165 */ 166 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle) 167 { 168 if (!dev->raw) 169 return; 170 171 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave"); 172 173 if (idle) { 174 dev->raw->this_ev.timeout = true; 175 ir_raw_event_store(dev, &dev->raw->this_ev); 176 init_ir_raw_event(&dev->raw->this_ev); 177 } 178 179 if (dev->s_idle) 180 dev->s_idle(dev, idle); 181 182 dev->idle = idle; 183 } 184 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle); 185 186 /** 187 * ir_raw_event_handle() - schedules the decoding of stored ir data 188 * @dev: the struct rc_dev device descriptor 189 * 190 * This routine will tell rc-core to start decoding stored ir data. 191 */ 192 void ir_raw_event_handle(struct rc_dev *dev) 193 { 194 if (!dev->raw || !dev->raw->thread) 195 return; 196 197 wake_up_process(dev->raw->thread); 198 } 199 EXPORT_SYMBOL_GPL(ir_raw_event_handle); 200 201 /* used internally by the sysfs interface */ 202 u64 203 ir_raw_get_allowed_protocols(void) 204 { 205 return atomic64_read(&available_protocols); 206 } 207 208 static int change_protocol(struct rc_dev *dev, u64 *rc_proto) 209 { 210 /* the caller will update dev->enabled_protocols */ 211 return 0; 212 } 213 214 static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols) 215 { 216 mutex_lock(&dev->lock); 217 dev->enabled_protocols &= ~protocols; 218 mutex_unlock(&dev->lock); 219 } 220 221 /** 222 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation. 223 * @ev: Pointer to pointer to next free event. *@ev is incremented for 224 * each raw event filled. 225 * @max: Maximum number of raw events to fill. 226 * @timings: Manchester modulation timings. 227 * @n: Number of bits of data. 228 * @data: Data bits to encode. 229 * 230 * Encodes the @n least significant bits of @data using Manchester (bi-phase) 231 * modulation with the timing characteristics described by @timings, writing up 232 * to @max raw IR events using the *@ev pointer. 233 * 234 * Returns: 0 on success. 235 * -ENOBUFS if there isn't enough space in the array to fit the 236 * full encoded data. In this case all @max events will have been 237 * written. 238 */ 239 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max, 240 const struct ir_raw_timings_manchester *timings, 241 unsigned int n, u64 data) 242 { 243 bool need_pulse; 244 u64 i; 245 int ret = -ENOBUFS; 246 247 i = BIT_ULL(n - 1); 248 249 if (timings->leader_pulse) { 250 if (!max--) 251 return ret; 252 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse); 253 if (timings->leader_space) { 254 if (!max--) 255 return ret; 256 init_ir_raw_event_duration(++(*ev), 0, 257 timings->leader_space); 258 } 259 } else { 260 /* continue existing signal */ 261 --(*ev); 262 } 263 /* from here on *ev will point to the last event rather than the next */ 264 265 while (n && i > 0) { 266 need_pulse = !(data & i); 267 if (timings->invert) 268 need_pulse = !need_pulse; 269 if (need_pulse == !!(*ev)->pulse) { 270 (*ev)->duration += timings->clock; 271 } else { 272 if (!max--) 273 goto nobufs; 274 init_ir_raw_event_duration(++(*ev), need_pulse, 275 timings->clock); 276 } 277 278 if (!max--) 279 goto nobufs; 280 init_ir_raw_event_duration(++(*ev), !need_pulse, 281 timings->clock); 282 i >>= 1; 283 } 284 285 if (timings->trailer_space) { 286 if (!(*ev)->pulse) 287 (*ev)->duration += timings->trailer_space; 288 else if (!max--) 289 goto nobufs; 290 else 291 init_ir_raw_event_duration(++(*ev), 0, 292 timings->trailer_space); 293 } 294 295 ret = 0; 296 nobufs: 297 /* point to the next event rather than last event before returning */ 298 ++(*ev); 299 return ret; 300 } 301 EXPORT_SYMBOL(ir_raw_gen_manchester); 302 303 /** 304 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation. 305 * @ev: Pointer to pointer to next free event. *@ev is incremented for 306 * each raw event filled. 307 * @max: Maximum number of raw events to fill. 308 * @timings: Pulse distance modulation timings. 309 * @n: Number of bits of data. 310 * @data: Data bits to encode. 311 * 312 * Encodes the @n least significant bits of @data using pulse-distance 313 * modulation with the timing characteristics described by @timings, writing up 314 * to @max raw IR events using the *@ev pointer. 315 * 316 * Returns: 0 on success. 317 * -ENOBUFS if there isn't enough space in the array to fit the 318 * full encoded data. In this case all @max events will have been 319 * written. 320 */ 321 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max, 322 const struct ir_raw_timings_pd *timings, 323 unsigned int n, u64 data) 324 { 325 int i; 326 int ret; 327 unsigned int space; 328 329 if (timings->header_pulse) { 330 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse, 331 timings->header_space); 332 if (ret) 333 return ret; 334 } 335 336 if (timings->msb_first) { 337 for (i = n - 1; i >= 0; --i) { 338 space = timings->bit_space[(data >> i) & 1]; 339 ret = ir_raw_gen_pulse_space(ev, &max, 340 timings->bit_pulse, 341 space); 342 if (ret) 343 return ret; 344 } 345 } else { 346 for (i = 0; i < n; ++i, data >>= 1) { 347 space = timings->bit_space[data & 1]; 348 ret = ir_raw_gen_pulse_space(ev, &max, 349 timings->bit_pulse, 350 space); 351 if (ret) 352 return ret; 353 } 354 } 355 356 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse, 357 timings->trailer_space); 358 return ret; 359 } 360 EXPORT_SYMBOL(ir_raw_gen_pd); 361 362 /** 363 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation. 364 * @ev: Pointer to pointer to next free event. *@ev is incremented for 365 * each raw event filled. 366 * @max: Maximum number of raw events to fill. 367 * @timings: Pulse distance modulation timings. 368 * @n: Number of bits of data. 369 * @data: Data bits to encode. 370 * 371 * Encodes the @n least significant bits of @data using space-distance 372 * modulation with the timing characteristics described by @timings, writing up 373 * to @max raw IR events using the *@ev pointer. 374 * 375 * Returns: 0 on success. 376 * -ENOBUFS if there isn't enough space in the array to fit the 377 * full encoded data. In this case all @max events will have been 378 * written. 379 */ 380 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max, 381 const struct ir_raw_timings_pl *timings, 382 unsigned int n, u64 data) 383 { 384 int i; 385 int ret = -ENOBUFS; 386 unsigned int pulse; 387 388 if (!max--) 389 return ret; 390 391 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse); 392 393 if (timings->msb_first) { 394 for (i = n - 1; i >= 0; --i) { 395 if (!max--) 396 return ret; 397 init_ir_raw_event_duration((*ev)++, 0, 398 timings->bit_space); 399 if (!max--) 400 return ret; 401 pulse = timings->bit_pulse[(data >> i) & 1]; 402 init_ir_raw_event_duration((*ev)++, 1, pulse); 403 } 404 } else { 405 for (i = 0; i < n; ++i, data >>= 1) { 406 if (!max--) 407 return ret; 408 init_ir_raw_event_duration((*ev)++, 0, 409 timings->bit_space); 410 if (!max--) 411 return ret; 412 pulse = timings->bit_pulse[data & 1]; 413 init_ir_raw_event_duration((*ev)++, 1, pulse); 414 } 415 } 416 417 if (!max--) 418 return ret; 419 420 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space); 421 422 return 0; 423 } 424 EXPORT_SYMBOL(ir_raw_gen_pl); 425 426 /** 427 * ir_raw_encode_scancode() - Encode a scancode as raw events 428 * 429 * @protocol: protocol 430 * @scancode: scancode filter describing a single scancode 431 * @events: array of raw events to write into 432 * @max: max number of raw events 433 * 434 * Attempts to encode the scancode as raw events. 435 * 436 * Returns: The number of events written. 437 * -ENOBUFS if there isn't enough space in the array to fit the 438 * encoding. In this case all @max events will have been written. 439 * -EINVAL if the scancode is ambiguous or invalid, or if no 440 * compatible encoder was found. 441 */ 442 int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode, 443 struct ir_raw_event *events, unsigned int max) 444 { 445 struct ir_raw_handler *handler; 446 int ret = -EINVAL; 447 u64 mask = 1ULL << protocol; 448 449 ir_raw_load_modules(&mask); 450 451 mutex_lock(&ir_raw_handler_lock); 452 list_for_each_entry(handler, &ir_raw_handler_list, list) { 453 if (handler->protocols & mask && handler->encode) { 454 ret = handler->encode(protocol, scancode, events, max); 455 if (ret >= 0 || ret == -ENOBUFS) 456 break; 457 } 458 } 459 mutex_unlock(&ir_raw_handler_lock); 460 461 return ret; 462 } 463 EXPORT_SYMBOL(ir_raw_encode_scancode); 464 465 static void edge_handle(struct timer_list *t) 466 { 467 struct ir_raw_event_ctrl *raw = from_timer(raw, t, edge_handle); 468 struct rc_dev *dev = raw->dev; 469 ktime_t interval = ktime_sub(ktime_get(), dev->raw->last_event); 470 471 if (ktime_to_ns(interval) >= dev->timeout) { 472 DEFINE_IR_RAW_EVENT(ev); 473 474 ev.timeout = true; 475 ev.duration = ktime_to_ns(interval); 476 477 ir_raw_event_store(dev, &ev); 478 } else { 479 mod_timer(&dev->raw->edge_handle, 480 jiffies + nsecs_to_jiffies(dev->timeout - 481 ktime_to_ns(interval))); 482 } 483 484 ir_raw_event_handle(dev); 485 } 486 487 /** 488 * ir_raw_encode_carrier() - Get carrier used for protocol 489 * 490 * @protocol: protocol 491 * 492 * Attempts to find the carrier for the specified protocol 493 * 494 * Returns: The carrier in Hz 495 * -EINVAL if the protocol is invalid, or if no 496 * compatible encoder was found. 497 */ 498 int ir_raw_encode_carrier(enum rc_proto protocol) 499 { 500 struct ir_raw_handler *handler; 501 int ret = -EINVAL; 502 u64 mask = BIT_ULL(protocol); 503 504 mutex_lock(&ir_raw_handler_lock); 505 list_for_each_entry(handler, &ir_raw_handler_list, list) { 506 if (handler->protocols & mask && handler->encode) { 507 ret = handler->carrier; 508 break; 509 } 510 } 511 mutex_unlock(&ir_raw_handler_lock); 512 513 return ret; 514 } 515 EXPORT_SYMBOL(ir_raw_encode_carrier); 516 517 /* 518 * Used to (un)register raw event clients 519 */ 520 int ir_raw_event_prepare(struct rc_dev *dev) 521 { 522 if (!dev) 523 return -EINVAL; 524 525 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL); 526 if (!dev->raw) 527 return -ENOMEM; 528 529 dev->raw->dev = dev; 530 dev->change_protocol = change_protocol; 531 timer_setup(&dev->raw->edge_handle, edge_handle, 0); 532 INIT_KFIFO(dev->raw->kfifo); 533 534 return 0; 535 } 536 537 int ir_raw_event_register(struct rc_dev *dev) 538 { 539 struct ir_raw_handler *handler; 540 struct task_struct *thread; 541 542 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor); 543 if (IS_ERR(thread)) 544 return PTR_ERR(thread); 545 546 dev->raw->thread = thread; 547 548 mutex_lock(&ir_raw_handler_lock); 549 list_add_tail(&dev->raw->list, &ir_raw_client_list); 550 list_for_each_entry(handler, &ir_raw_handler_list, list) 551 if (handler->raw_register) 552 handler->raw_register(dev); 553 mutex_unlock(&ir_raw_handler_lock); 554 555 return 0; 556 } 557 558 void ir_raw_event_free(struct rc_dev *dev) 559 { 560 if (!dev) 561 return; 562 563 kfree(dev->raw); 564 dev->raw = NULL; 565 } 566 567 void ir_raw_event_unregister(struct rc_dev *dev) 568 { 569 struct ir_raw_handler *handler; 570 571 if (!dev || !dev->raw) 572 return; 573 574 kthread_stop(dev->raw->thread); 575 del_timer_sync(&dev->raw->edge_handle); 576 577 mutex_lock(&ir_raw_handler_lock); 578 list_del(&dev->raw->list); 579 list_for_each_entry(handler, &ir_raw_handler_list, list) 580 if (handler->raw_unregister) 581 handler->raw_unregister(dev); 582 mutex_unlock(&ir_raw_handler_lock); 583 584 ir_raw_event_free(dev); 585 } 586 587 /* 588 * Extension interface - used to register the IR decoders 589 */ 590 591 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler) 592 { 593 struct ir_raw_event_ctrl *raw; 594 595 mutex_lock(&ir_raw_handler_lock); 596 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list); 597 if (ir_raw_handler->raw_register) 598 list_for_each_entry(raw, &ir_raw_client_list, list) 599 ir_raw_handler->raw_register(raw->dev); 600 atomic64_or(ir_raw_handler->protocols, &available_protocols); 601 mutex_unlock(&ir_raw_handler_lock); 602 603 return 0; 604 } 605 EXPORT_SYMBOL(ir_raw_handler_register); 606 607 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler) 608 { 609 struct ir_raw_event_ctrl *raw; 610 u64 protocols = ir_raw_handler->protocols; 611 612 mutex_lock(&ir_raw_handler_lock); 613 list_del(&ir_raw_handler->list); 614 list_for_each_entry(raw, &ir_raw_client_list, list) { 615 ir_raw_disable_protocols(raw->dev, protocols); 616 if (ir_raw_handler->raw_unregister) 617 ir_raw_handler->raw_unregister(raw->dev); 618 } 619 atomic64_andnot(protocols, &available_protocols); 620 mutex_unlock(&ir_raw_handler_lock); 621 } 622 EXPORT_SYMBOL(ir_raw_handler_unregister); 623