ir-raw-event.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* ir-raw-event.c - handle IR Pulse/Space event
  2. *
  3. * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <media/ir-core.h>
  15. #include <linux/workqueue.h>
  16. /* Define the max number of bit transitions per IR keycode */
  17. #define MAX_IR_EVENT_SIZE 256
  18. /* Used to handle IR raw handler extensions */
  19. static LIST_HEAD(ir_raw_handler_list);
  20. static DEFINE_MUTEX(ir_raw_handler_lock);
  21. /* Used to load the decoders */
  22. static struct work_struct wq_load;
  23. static void ir_keyup_timer(unsigned long data)
  24. {
  25. struct input_dev *input_dev = (struct input_dev *)data;
  26. ir_keyup(input_dev);
  27. }
  28. int ir_raw_event_register(struct input_dev *input_dev)
  29. {
  30. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  31. int rc, size;
  32. ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
  33. size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
  34. size = roundup_pow_of_two(size);
  35. init_timer(&ir->raw->timer_keyup);
  36. ir->raw->timer_keyup.function = ir_keyup_timer;
  37. ir->raw->timer_keyup.data = (unsigned long)input_dev;
  38. set_bit(EV_REP, input_dev->evbit);
  39. rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
  40. return rc;
  41. }
  42. EXPORT_SYMBOL_GPL(ir_raw_event_register);
  43. void ir_raw_event_unregister(struct input_dev *input_dev)
  44. {
  45. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  46. if (!ir->raw)
  47. return;
  48. del_timer_sync(&ir->raw->timer_keyup);
  49. kfifo_free(&ir->raw->kfifo);
  50. kfree(ir->raw);
  51. ir->raw = NULL;
  52. }
  53. EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
  54. int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
  55. {
  56. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  57. struct timespec ts;
  58. struct ir_raw_event event;
  59. int rc;
  60. if (!ir->raw)
  61. return -EINVAL;
  62. event.type = type;
  63. event.delta.tv_sec = 0;
  64. event.delta.tv_nsec = 0;
  65. ktime_get_ts(&ts);
  66. if (timespec_equal(&ir->raw->last_event, &event.delta))
  67. event.type |= IR_START_EVENT;
  68. else
  69. event.delta = timespec_sub(ts, ir->raw->last_event);
  70. memcpy(&ir->raw->last_event, &ts, sizeof(ts));
  71. if (event.delta.tv_sec) {
  72. event.type |= IR_START_EVENT;
  73. event.delta.tv_sec = 0;
  74. event.delta.tv_nsec = 0;
  75. }
  76. kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
  77. return rc;
  78. }
  79. EXPORT_SYMBOL_GPL(ir_raw_event_store);
  80. int ir_raw_event_handle(struct input_dev *input_dev)
  81. {
  82. struct ir_input_dev *ir = input_get_drvdata(input_dev);
  83. int rc;
  84. struct ir_raw_event *evs;
  85. int len, i;
  86. struct ir_raw_handler *ir_raw_handler;
  87. /*
  88. * Store the events into a temporary buffer. This allows calling more than
  89. * one decoder to deal with the received data
  90. */
  91. len = kfifo_len(&ir->raw->kfifo) / sizeof(*evs);
  92. if (!len)
  93. return 0;
  94. evs = kmalloc(len * sizeof(*evs), GFP_ATOMIC);
  95. for (i = 0; i < len; i++) {
  96. rc = kfifo_out(&ir->raw->kfifo, &evs[i], sizeof(*evs));
  97. if (rc != sizeof(*evs)) {
  98. IR_dprintk(1, "overflow error: received %d instead of %zd\n",
  99. rc, sizeof(*evs));
  100. return -EINVAL;
  101. }
  102. IR_dprintk(2, "event type %d, time before event: %07luus\n",
  103. evs[i].type, (evs[i].delta.tv_nsec + 500) / 1000);
  104. }
  105. /*
  106. * Call all ir decoders. This allows decoding the same event with
  107. * more than one protocol handler.
  108. * FIXME: better handle the returned code: does it make sense to use
  109. * other decoders, if the first one already handled the IR?
  110. */
  111. list_for_each_entry(ir_raw_handler, &ir_raw_handler_list, list) {
  112. rc = ir_raw_handler->decode(input_dev, evs, len);
  113. }
  114. kfree(evs);
  115. return rc;
  116. }
  117. EXPORT_SYMBOL_GPL(ir_raw_event_handle);
  118. /*
  119. * Extension interface - used to register the IR decoders
  120. */
  121. int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
  122. {
  123. mutex_lock(&ir_raw_handler_lock);
  124. list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
  125. mutex_unlock(&ir_raw_handler_lock);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(ir_raw_handler_register);
  129. void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
  130. {
  131. mutex_lock(&ir_raw_handler_lock);
  132. list_del(&ir_raw_handler->list);
  133. mutex_unlock(&ir_raw_handler_lock);
  134. }
  135. EXPORT_SYMBOL(ir_raw_handler_unregister);
  136. static void init_decoders(struct work_struct *work)
  137. {
  138. /* Load the decoder modules */
  139. load_nec_decode();
  140. /* If needed, we may later add some init code. In this case,
  141. it is needed to change the CONFIG_MODULE test at ir-core.h
  142. */
  143. }
  144. void ir_raw_init(void)
  145. {
  146. INIT_WORK(&wq_load, init_decoders);
  147. schedule_work(&wq_load);
  148. }