cinergyT2-core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * TerraTec Cinergy T2/qanu USB2 DVB-T adapter.
  3. *
  4. * Copyright (C) 2007 Tomi Orava (tomimo@ncircle.nullnet.fi)
  5. *
  6. * Based on the dvb-usb-framework code and the
  7. * original Terratec Cinergy T2 driver by:
  8. *
  9. * Copyright (C) 2004 Daniel Mack <daniel@qanu.de> and
  10. * Holger Waechtler <holger@qanu.de>
  11. *
  12. * Protocol Spec published on http://qanu.de/specs/terratec_cinergyT2.pdf
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. */
  25. #include "cinergyT2.h"
  26. /* debug */
  27. int dvb_usb_cinergyt2_debug;
  28. module_param_named(debug, dvb_usb_cinergyt2_debug, int, 0644);
  29. MODULE_PARM_DESC(debug, "set debugging level (1=info, xfer=2, rc=4 (or-able)).");
  30. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  31. struct cinergyt2_state {
  32. u8 rc_counter;
  33. unsigned char data[64];
  34. };
  35. /* We are missing a release hook with usb_device data */
  36. static struct dvb_usb_device *cinergyt2_usb_device;
  37. static struct dvb_usb_device_properties cinergyt2_properties;
  38. static int cinergyt2_streaming_ctrl(struct dvb_usb_adapter *adap, int enable)
  39. {
  40. struct dvb_usb_device *d = adap->dev;
  41. struct cinergyt2_state *st = d->priv;
  42. int ret;
  43. mutex_lock(&d->data_mutex);
  44. st->data[0] = CINERGYT2_EP1_CONTROL_STREAM_TRANSFER;
  45. st->data[1] = enable ? 1 : 0;
  46. ret = dvb_usb_generic_rw(d, st->data, 2, st->data, 64, 0);
  47. mutex_unlock(&d->data_mutex);
  48. return ret;
  49. }
  50. static int cinergyt2_power_ctrl(struct dvb_usb_device *d, int enable)
  51. {
  52. struct cinergyt2_state *st = d->priv;
  53. int ret;
  54. mutex_lock(&d->data_mutex);
  55. st->data[0] = CINERGYT2_EP1_SLEEP_MODE;
  56. st->data[1] = enable ? 0 : 1;
  57. ret = dvb_usb_generic_rw(d, st->data, 2, st->data, 3, 0);
  58. mutex_unlock(&d->data_mutex);
  59. return ret;
  60. }
  61. static int cinergyt2_frontend_attach(struct dvb_usb_adapter *adap)
  62. {
  63. struct dvb_usb_device *d = adap->dev;
  64. struct cinergyt2_state *st = d->priv;
  65. int ret;
  66. adap->fe_adap[0].fe = cinergyt2_fe_attach(adap->dev);
  67. mutex_lock(&d->data_mutex);
  68. st->data[0] = CINERGYT2_EP1_GET_FIRMWARE_VERSION;
  69. ret = dvb_usb_generic_rw(d, st->data, 1, st->data, 3, 0);
  70. if (ret < 0) {
  71. deb_rc("cinergyt2_power_ctrl() Failed to retrieve sleep state info\n");
  72. }
  73. mutex_unlock(&d->data_mutex);
  74. /* Copy this pointer as we are gonna need it in the release phase */
  75. cinergyt2_usb_device = adap->dev;
  76. return ret;
  77. }
  78. static struct rc_map_table rc_map_cinergyt2_table[] = {
  79. { 0x0401, KEY_POWER },
  80. { 0x0402, KEY_1 },
  81. { 0x0403, KEY_2 },
  82. { 0x0404, KEY_3 },
  83. { 0x0405, KEY_4 },
  84. { 0x0406, KEY_5 },
  85. { 0x0407, KEY_6 },
  86. { 0x0408, KEY_7 },
  87. { 0x0409, KEY_8 },
  88. { 0x040a, KEY_9 },
  89. { 0x040c, KEY_0 },
  90. { 0x040b, KEY_VIDEO },
  91. { 0x040d, KEY_REFRESH },
  92. { 0x040e, KEY_SELECT },
  93. { 0x040f, KEY_EPG },
  94. { 0x0410, KEY_UP },
  95. { 0x0414, KEY_DOWN },
  96. { 0x0411, KEY_LEFT },
  97. { 0x0413, KEY_RIGHT },
  98. { 0x0412, KEY_OK },
  99. { 0x0415, KEY_TEXT },
  100. { 0x0416, KEY_INFO },
  101. { 0x0417, KEY_RED },
  102. { 0x0418, KEY_GREEN },
  103. { 0x0419, KEY_YELLOW },
  104. { 0x041a, KEY_BLUE },
  105. { 0x041c, KEY_VOLUMEUP },
  106. { 0x041e, KEY_VOLUMEDOWN },
  107. { 0x041d, KEY_MUTE },
  108. { 0x041b, KEY_CHANNELUP },
  109. { 0x041f, KEY_CHANNELDOWN },
  110. { 0x0440, KEY_PAUSE },
  111. { 0x044c, KEY_PLAY },
  112. { 0x0458, KEY_RECORD },
  113. { 0x0454, KEY_PREVIOUS },
  114. { 0x0448, KEY_STOP },
  115. { 0x045c, KEY_NEXT }
  116. };
  117. /* Number of keypresses to ignore before detect repeating */
  118. #define RC_REPEAT_DELAY 3
  119. static int repeatable_keys[] = {
  120. KEY_UP,
  121. KEY_DOWN,
  122. KEY_LEFT,
  123. KEY_RIGHT,
  124. KEY_VOLUMEUP,
  125. KEY_VOLUMEDOWN,
  126. KEY_CHANNELUP,
  127. KEY_CHANNELDOWN
  128. };
  129. static int cinergyt2_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  130. {
  131. struct cinergyt2_state *st = d->priv;
  132. int i, ret;
  133. *state = REMOTE_NO_KEY_PRESSED;
  134. mutex_lock(&d->data_mutex);
  135. st->data[0] = CINERGYT2_EP1_GET_RC_EVENTS;
  136. ret = dvb_usb_generic_rw(d, st->data, 1, st->data, 5, 0);
  137. if (ret < 0)
  138. goto ret;
  139. if (st->data[4] == 0xff) {
  140. /* key repeat */
  141. st->rc_counter++;
  142. if (st->rc_counter > RC_REPEAT_DELAY) {
  143. for (i = 0; i < ARRAY_SIZE(repeatable_keys); i++) {
  144. if (d->last_event == repeatable_keys[i]) {
  145. *state = REMOTE_KEY_REPEAT;
  146. *event = d->last_event;
  147. deb_rc("repeat key, event %x\n",
  148. *event);
  149. goto ret;
  150. }
  151. }
  152. deb_rc("repeated key (non repeatable)\n");
  153. }
  154. goto ret;
  155. }
  156. /* hack to pass checksum on the custom field */
  157. st->data[2] = ~st->data[1];
  158. dvb_usb_nec_rc_key_to_event(d, st->data, event, state);
  159. if (st->data[0] != 0) {
  160. if (*event != d->last_event)
  161. st->rc_counter = 0;
  162. deb_rc("key: %*ph\n", 5, st->data);
  163. }
  164. ret:
  165. mutex_unlock(&d->data_mutex);
  166. return ret;
  167. }
  168. static int cinergyt2_usb_probe(struct usb_interface *intf,
  169. const struct usb_device_id *id)
  170. {
  171. return dvb_usb_device_init(intf, &cinergyt2_properties,
  172. THIS_MODULE, NULL, adapter_nr);
  173. }
  174. static struct usb_device_id cinergyt2_usb_table[] = {
  175. { USB_DEVICE(USB_VID_TERRATEC, 0x0038) },
  176. { 0 }
  177. };
  178. MODULE_DEVICE_TABLE(usb, cinergyt2_usb_table);
  179. static struct dvb_usb_device_properties cinergyt2_properties = {
  180. .size_of_priv = sizeof(struct cinergyt2_state),
  181. .num_adapters = 1,
  182. .adapter = {
  183. {
  184. .num_frontends = 1,
  185. .fe = {{
  186. .streaming_ctrl = cinergyt2_streaming_ctrl,
  187. .frontend_attach = cinergyt2_frontend_attach,
  188. /* parameter for the MPEG2-data transfer */
  189. .stream = {
  190. .type = USB_BULK,
  191. .count = 5,
  192. .endpoint = 0x02,
  193. .u = {
  194. .bulk = {
  195. .buffersize = 512,
  196. }
  197. }
  198. },
  199. }},
  200. }
  201. },
  202. .power_ctrl = cinergyt2_power_ctrl,
  203. .rc.legacy = {
  204. .rc_interval = 50,
  205. .rc_map_table = rc_map_cinergyt2_table,
  206. .rc_map_size = ARRAY_SIZE(rc_map_cinergyt2_table),
  207. .rc_query = cinergyt2_rc_query,
  208. },
  209. .generic_bulk_ctrl_endpoint = 1,
  210. .num_device_descs = 1,
  211. .devices = {
  212. { .name = "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver",
  213. .cold_ids = {NULL},
  214. .warm_ids = { &cinergyt2_usb_table[0], NULL },
  215. },
  216. { NULL },
  217. }
  218. };
  219. static struct usb_driver cinergyt2_driver = {
  220. .name = "cinergyT2",
  221. .probe = cinergyt2_usb_probe,
  222. .disconnect = dvb_usb_device_exit,
  223. .id_table = cinergyt2_usb_table
  224. };
  225. module_usb_driver(cinergyt2_driver);
  226. MODULE_DESCRIPTION("Terratec Cinergy T2 DVB-T driver");
  227. MODULE_LICENSE("GPL");
  228. MODULE_AUTHOR("Tomi Orava");