rc-ir-raw.c 19 KB

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