smipcie-ir.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * SMI PCIe driver for DVBSky cards.
  3. *
  4. * Copyright (C) 2014 Max nibble <nibble.max@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "smipcie.h"
  17. static void smi_ir_enableInterrupt(struct smi_rc *ir)
  18. {
  19. struct smi_dev *dev = ir->dev;
  20. smi_write(MSI_INT_ENA_SET, IR_X_INT);
  21. }
  22. static void smi_ir_disableInterrupt(struct smi_rc *ir)
  23. {
  24. struct smi_dev *dev = ir->dev;
  25. smi_write(MSI_INT_ENA_CLR, IR_X_INT);
  26. }
  27. static void smi_ir_clearInterrupt(struct smi_rc *ir)
  28. {
  29. struct smi_dev *dev = ir->dev;
  30. smi_write(MSI_INT_STATUS_CLR, IR_X_INT);
  31. }
  32. static void smi_ir_stop(struct smi_rc *ir)
  33. {
  34. struct smi_dev *dev = ir->dev;
  35. smi_ir_disableInterrupt(ir);
  36. smi_clear(IR_Init_Reg, 0x80);
  37. }
  38. #define BITS_PER_COMMAND 14
  39. #define GROUPS_PER_BIT 2
  40. #define IR_RC5_MIN_BIT 36
  41. #define IR_RC5_MAX_BIT 52
  42. static u32 smi_decode_rc5(u8 *pData, u8 size)
  43. {
  44. u8 index, current_bit, bit_count;
  45. u8 group_array[BITS_PER_COMMAND * GROUPS_PER_BIT + 4];
  46. u8 group_index = 0;
  47. u32 command = 0xFFFFFFFF;
  48. group_array[group_index++] = 1;
  49. for (index = 0; index < size; index++) {
  50. current_bit = (pData[index] & 0x80) ? 1 : 0;
  51. bit_count = pData[index] & 0x7f;
  52. if ((current_bit == 1) && (bit_count >= 2*IR_RC5_MAX_BIT + 1)) {
  53. goto process_code;
  54. } else if ((bit_count >= IR_RC5_MIN_BIT) &&
  55. (bit_count <= IR_RC5_MAX_BIT)) {
  56. group_array[group_index++] = current_bit;
  57. } else if ((bit_count > IR_RC5_MAX_BIT) &&
  58. (bit_count <= 2*IR_RC5_MAX_BIT)) {
  59. group_array[group_index++] = current_bit;
  60. group_array[group_index++] = current_bit;
  61. } else {
  62. goto invalid_timing;
  63. }
  64. if (group_index >= BITS_PER_COMMAND*GROUPS_PER_BIT)
  65. goto process_code;
  66. if ((group_index == BITS_PER_COMMAND*GROUPS_PER_BIT - 1)
  67. && (group_array[group_index-1] == 0)) {
  68. group_array[group_index++] = 1;
  69. goto process_code;
  70. }
  71. }
  72. process_code:
  73. if (group_index == (BITS_PER_COMMAND*GROUPS_PER_BIT-1))
  74. group_array[group_index++] = 1;
  75. if (group_index == BITS_PER_COMMAND*GROUPS_PER_BIT) {
  76. command = 0;
  77. for (index = 0; index < (BITS_PER_COMMAND*GROUPS_PER_BIT);
  78. index = index + 2) {
  79. if ((group_array[index] == 1) &&
  80. (group_array[index+1] == 0)) {
  81. command |= (1 << (BITS_PER_COMMAND -
  82. (index/2) - 1));
  83. } else if ((group_array[index] == 0) &&
  84. (group_array[index+1] == 1)) {
  85. /* */
  86. } else {
  87. command = 0xFFFFFFFF;
  88. goto invalid_timing;
  89. }
  90. }
  91. }
  92. invalid_timing:
  93. return command;
  94. }
  95. static void smi_ir_decode(struct work_struct *work)
  96. {
  97. struct smi_rc *ir = container_of(work, struct smi_rc, work);
  98. struct smi_dev *dev = ir->dev;
  99. struct rc_dev *rc_dev = ir->rc_dev;
  100. u32 dwIRControl, dwIRData, dwIRCode, scancode;
  101. u8 index, ucIRCount, readLoop, rc5_command, rc5_system, toggle;
  102. dwIRControl = smi_read(IR_Init_Reg);
  103. if (dwIRControl & rbIRVld) {
  104. ucIRCount = (u8) smi_read(IR_Data_Cnt);
  105. if (ucIRCount < 4)
  106. goto end_ir_decode;
  107. readLoop = ucIRCount/4;
  108. if (ucIRCount % 4)
  109. readLoop += 1;
  110. for (index = 0; index < readLoop; index++) {
  111. dwIRData = smi_read(IR_DATA_BUFFER_BASE + (index*4));
  112. ir->irData[index*4 + 0] = (u8)(dwIRData);
  113. ir->irData[index*4 + 1] = (u8)(dwIRData >> 8);
  114. ir->irData[index*4 + 2] = (u8)(dwIRData >> 16);
  115. ir->irData[index*4 + 3] = (u8)(dwIRData >> 24);
  116. }
  117. dwIRCode = smi_decode_rc5(ir->irData, ucIRCount);
  118. if (dwIRCode != 0xFFFFFFFF) {
  119. rc5_command = dwIRCode & 0x3F;
  120. rc5_system = (dwIRCode & 0x7C0) >> 6;
  121. toggle = (dwIRCode & 0x800) ? 1 : 0;
  122. scancode = rc5_system << 8 | rc5_command;
  123. rc_keydown(rc_dev, RC_TYPE_RC5, scancode, toggle);
  124. }
  125. }
  126. end_ir_decode:
  127. smi_set(IR_Init_Reg, 0x04);
  128. smi_ir_enableInterrupt(ir);
  129. }
  130. /* ir functions call by main driver.*/
  131. int smi_ir_irq(struct smi_rc *ir, u32 int_status)
  132. {
  133. int handled = 0;
  134. if (int_status & IR_X_INT) {
  135. smi_ir_disableInterrupt(ir);
  136. smi_ir_clearInterrupt(ir);
  137. schedule_work(&ir->work);
  138. handled = 1;
  139. }
  140. return handled;
  141. }
  142. void smi_ir_start(struct smi_rc *ir)
  143. {
  144. struct smi_dev *dev = ir->dev;
  145. smi_write(IR_Idle_Cnt_Low, 0x00140070);
  146. msleep(20);
  147. smi_set(IR_Init_Reg, 0x90);
  148. smi_ir_enableInterrupt(ir);
  149. }
  150. int smi_ir_init(struct smi_dev *dev)
  151. {
  152. int ret;
  153. struct rc_dev *rc_dev;
  154. struct smi_rc *ir = &dev->ir;
  155. rc_dev = rc_allocate_device();
  156. if (!rc_dev)
  157. return -ENOMEM;
  158. /* init input device */
  159. snprintf(ir->input_name, sizeof(ir->input_name), "IR (%s)",
  160. dev->info->name);
  161. snprintf(ir->input_phys, sizeof(ir->input_phys), "pci-%s/ir0",
  162. pci_name(dev->pci_dev));
  163. rc_dev->driver_name = "SMI_PCIe";
  164. rc_dev->input_phys = ir->input_phys;
  165. rc_dev->input_name = ir->input_name;
  166. rc_dev->input_id.bustype = BUS_PCI;
  167. rc_dev->input_id.version = 1;
  168. rc_dev->input_id.vendor = dev->pci_dev->subsystem_vendor;
  169. rc_dev->input_id.product = dev->pci_dev->subsystem_device;
  170. rc_dev->dev.parent = &dev->pci_dev->dev;
  171. rc_dev->driver_type = RC_DRIVER_SCANCODE;
  172. rc_dev->map_name = RC_MAP_DVBSKY;
  173. ir->rc_dev = rc_dev;
  174. ir->dev = dev;
  175. INIT_WORK(&ir->work, smi_ir_decode);
  176. smi_ir_disableInterrupt(ir);
  177. ret = rc_register_device(rc_dev);
  178. if (ret)
  179. goto ir_err;
  180. return 0;
  181. ir_err:
  182. rc_free_device(rc_dev);
  183. return ret;
  184. }
  185. void smi_ir_exit(struct smi_dev *dev)
  186. {
  187. struct smi_rc *ir = &dev->ir;
  188. struct rc_dev *rc_dev = ir->rc_dev;
  189. smi_ir_stop(ir);
  190. rc_unregister_device(rc_dev);
  191. ir->rc_dev = NULL;
  192. }