hid-picolcd_cir.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***************************************************************************
  2. * Copyright (C) 2010-2012 by Bruno Prémont <bonbons@linux-vserver.org> *
  3. * *
  4. * Based on Logitech G13 driver (v0.4) *
  5. * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, version 2 of the License. *
  10. * *
  11. * This driver is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this software. If not see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include <linux/hid.h>
  20. #include <linux/hid-debug.h>
  21. #include <linux/input.h>
  22. #include "hid-ids.h"
  23. #include "usbhid/usbhid.h"
  24. #include <linux/usb.h>
  25. #include <linux/fb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/backlight.h>
  28. #include <linux/lcd.h>
  29. #include <linux/leds.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/completion.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/module.h>
  35. #include <media/rc-core.h>
  36. #include "hid-picolcd.h"
  37. int picolcd_raw_cir(struct picolcd_data *data,
  38. struct hid_report *report, u8 *raw_data, int size)
  39. {
  40. unsigned long flags;
  41. int i, w, sz;
  42. DEFINE_IR_RAW_EVENT(rawir);
  43. /* ignore if rc_dev is NULL or status is shunned */
  44. spin_lock_irqsave(&data->lock, flags);
  45. if (!data->rc_dev || (data->status & PICOLCD_CIR_SHUN)) {
  46. spin_unlock_irqrestore(&data->lock, flags);
  47. return 1;
  48. }
  49. spin_unlock_irqrestore(&data->lock, flags);
  50. /* PicoLCD USB packets contain 16-bit intervals in network order,
  51. * with value negated for pulse. Intervals are in microseconds.
  52. *
  53. * Note: some userspace LIRC code for PicoLCD says negated values
  54. * for space - is it a matter of IR chip? (pulse for my TSOP2236)
  55. *
  56. * In addition, the first interval seems to be around 15000 + base
  57. * interval for non-first report of IR data - thus the quirk below
  58. * to get RC_CODE to understand Sony and JVC remotes I have at hand
  59. */
  60. sz = size > 0 ? min((int)raw_data[0], size-1) : 0;
  61. for (i = 0; i+1 < sz; i += 2) {
  62. init_ir_raw_event(&rawir);
  63. w = (raw_data[i] << 8) | (raw_data[i+1]);
  64. rawir.pulse = !!(w & 0x8000);
  65. rawir.duration = US_TO_NS(rawir.pulse ? (65536 - w) : w);
  66. /* Quirk!! - see above */
  67. if (i == 0 && rawir.duration > 15000000)
  68. rawir.duration -= 15000000;
  69. ir_raw_event_store(data->rc_dev, &rawir);
  70. }
  71. ir_raw_event_handle(data->rc_dev);
  72. return 1;
  73. }
  74. static int picolcd_cir_open(struct rc_dev *dev)
  75. {
  76. struct picolcd_data *data = dev->priv;
  77. unsigned long flags;
  78. spin_lock_irqsave(&data->lock, flags);
  79. data->status &= ~PICOLCD_CIR_SHUN;
  80. spin_unlock_irqrestore(&data->lock, flags);
  81. return 0;
  82. }
  83. static void picolcd_cir_close(struct rc_dev *dev)
  84. {
  85. struct picolcd_data *data = dev->priv;
  86. unsigned long flags;
  87. spin_lock_irqsave(&data->lock, flags);
  88. data->status |= PICOLCD_CIR_SHUN;
  89. spin_unlock_irqrestore(&data->lock, flags);
  90. }
  91. /* initialize CIR input device */
  92. int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
  93. {
  94. struct rc_dev *rdev;
  95. int ret = 0;
  96. rdev = rc_allocate_device();
  97. if (!rdev)
  98. return -ENOMEM;
  99. rdev->priv = data;
  100. rdev->driver_type = RC_DRIVER_IR_RAW;
  101. rdev->allowed_protos = RC_BIT_ALL;
  102. rdev->open = picolcd_cir_open;
  103. rdev->close = picolcd_cir_close;
  104. rdev->input_name = data->hdev->name;
  105. rdev->input_phys = data->hdev->phys;
  106. rdev->input_id.bustype = data->hdev->bus;
  107. rdev->input_id.vendor = data->hdev->vendor;
  108. rdev->input_id.product = data->hdev->product;
  109. rdev->input_id.version = data->hdev->version;
  110. rdev->dev.parent = &data->hdev->dev;
  111. rdev->driver_name = PICOLCD_NAME;
  112. rdev->map_name = RC_MAP_RC6_MCE;
  113. rdev->timeout = MS_TO_NS(100);
  114. rdev->rx_resolution = US_TO_NS(1);
  115. ret = rc_register_device(rdev);
  116. if (ret)
  117. goto err;
  118. data->rc_dev = rdev;
  119. return 0;
  120. err:
  121. rc_free_device(rdev);
  122. return ret;
  123. }
  124. void picolcd_exit_cir(struct picolcd_data *data)
  125. {
  126. struct rc_dev *rdev = data->rc_dev;
  127. data->rc_dev = NULL;
  128. rc_unregister_device(rdev);
  129. }