cinergyT2-core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. if (adap->fe_adap[0].fe)
  72. adap->fe_adap[0].fe->ops.release(adap->fe_adap[0].fe);
  73. deb_rc("cinergyt2_power_ctrl() Failed to retrieve sleep state info\n");
  74. }
  75. mutex_unlock(&d->data_mutex);
  76. /* Copy this pointer as we are gonna need it in the release phase */
  77. cinergyt2_usb_device = adap->dev;
  78. return ret;
  79. }
  80. static struct rc_map_table rc_map_cinergyt2_table[] = {
  81. { 0x0401, KEY_POWER },
  82. { 0x0402, KEY_1 },
  83. { 0x0403, KEY_2 },
  84. { 0x0404, KEY_3 },
  85. { 0x0405, KEY_4 },
  86. { 0x0406, KEY_5 },
  87. { 0x0407, KEY_6 },
  88. { 0x0408, KEY_7 },
  89. { 0x0409, KEY_8 },
  90. { 0x040a, KEY_9 },
  91. { 0x040c, KEY_0 },
  92. { 0x040b, KEY_VIDEO },
  93. { 0x040d, KEY_REFRESH },
  94. { 0x040e, KEY_SELECT },
  95. { 0x040f, KEY_EPG },
  96. { 0x0410, KEY_UP },
  97. { 0x0414, KEY_DOWN },
  98. { 0x0411, KEY_LEFT },
  99. { 0x0413, KEY_RIGHT },
  100. { 0x0412, KEY_OK },
  101. { 0x0415, KEY_TEXT },
  102. { 0x0416, KEY_INFO },
  103. { 0x0417, KEY_RED },
  104. { 0x0418, KEY_GREEN },
  105. { 0x0419, KEY_YELLOW },
  106. { 0x041a, KEY_BLUE },
  107. { 0x041c, KEY_VOLUMEUP },
  108. { 0x041e, KEY_VOLUMEDOWN },
  109. { 0x041d, KEY_MUTE },
  110. { 0x041b, KEY_CHANNELUP },
  111. { 0x041f, KEY_CHANNELDOWN },
  112. { 0x0440, KEY_PAUSE },
  113. { 0x044c, KEY_PLAY },
  114. { 0x0458, KEY_RECORD },
  115. { 0x0454, KEY_PREVIOUS },
  116. { 0x0448, KEY_STOP },
  117. { 0x045c, KEY_NEXT }
  118. };
  119. /* Number of keypresses to ignore before detect repeating */
  120. #define RC_REPEAT_DELAY 3
  121. static int repeatable_keys[] = {
  122. KEY_UP,
  123. KEY_DOWN,
  124. KEY_LEFT,
  125. KEY_RIGHT,
  126. KEY_VOLUMEUP,
  127. KEY_VOLUMEDOWN,
  128. KEY_CHANNELUP,
  129. KEY_CHANNELDOWN
  130. };
  131. static int cinergyt2_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  132. {
  133. struct cinergyt2_state *st = d->priv;
  134. int i, ret;
  135. *state = REMOTE_NO_KEY_PRESSED;
  136. mutex_lock(&d->data_mutex);
  137. st->data[0] = CINERGYT2_EP1_GET_RC_EVENTS;
  138. ret = dvb_usb_generic_rw(d, st->data, 1, st->data, 5, 0);
  139. if (ret < 0)
  140. goto ret;
  141. if (st->data[4] == 0xff) {
  142. /* key repeat */
  143. st->rc_counter++;
  144. if (st->rc_counter > RC_REPEAT_DELAY) {
  145. for (i = 0; i < ARRAY_SIZE(repeatable_keys); i++) {
  146. if (d->last_event == repeatable_keys[i]) {
  147. *state = REMOTE_KEY_REPEAT;
  148. *event = d->last_event;
  149. deb_rc("repeat key, event %x\n",
  150. *event);
  151. goto ret;
  152. }
  153. }
  154. deb_rc("repeated key (non repeatable)\n");
  155. }
  156. goto ret;
  157. }
  158. /* hack to pass checksum on the custom field */
  159. st->data[2] = ~st->data[1];
  160. dvb_usb_nec_rc_key_to_event(d, st->data, event, state);
  161. if (st->data[0] != 0) {
  162. if (*event != d->last_event)
  163. st->rc_counter = 0;
  164. deb_rc("key: %*ph\n", 5, st->data);
  165. }
  166. ret:
  167. mutex_unlock(&d->data_mutex);
  168. return ret;
  169. }
  170. static int cinergyt2_usb_probe(struct usb_interface *intf,
  171. const struct usb_device_id *id)
  172. {
  173. return dvb_usb_device_init(intf, &cinergyt2_properties,
  174. THIS_MODULE, NULL, adapter_nr);
  175. }
  176. static struct usb_device_id cinergyt2_usb_table[] = {
  177. { USB_DEVICE(USB_VID_TERRATEC, 0x0038) },
  178. { 0 }
  179. };
  180. MODULE_DEVICE_TABLE(usb, cinergyt2_usb_table);
  181. static struct dvb_usb_device_properties cinergyt2_properties = {
  182. .size_of_priv = sizeof(struct cinergyt2_state),
  183. .num_adapters = 1,
  184. .adapter = {
  185. {
  186. .num_frontends = 1,
  187. .fe = {{
  188. .streaming_ctrl = cinergyt2_streaming_ctrl,
  189. .frontend_attach = cinergyt2_frontend_attach,
  190. /* parameter for the MPEG2-data transfer */
  191. .stream = {
  192. .type = USB_BULK,
  193. .count = 5,
  194. .endpoint = 0x02,
  195. .u = {
  196. .bulk = {
  197. .buffersize = 512,
  198. }
  199. }
  200. },
  201. }},
  202. }
  203. },
  204. .power_ctrl = cinergyt2_power_ctrl,
  205. .rc.legacy = {
  206. .rc_interval = 50,
  207. .rc_map_table = rc_map_cinergyt2_table,
  208. .rc_map_size = ARRAY_SIZE(rc_map_cinergyt2_table),
  209. .rc_query = cinergyt2_rc_query,
  210. },
  211. .generic_bulk_ctrl_endpoint = 1,
  212. .num_device_descs = 1,
  213. .devices = {
  214. { .name = "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver",
  215. .cold_ids = {NULL},
  216. .warm_ids = { &cinergyt2_usb_table[0], NULL },
  217. },
  218. { NULL },
  219. }
  220. };
  221. static struct usb_driver cinergyt2_driver = {
  222. .name = "cinergyT2",
  223. .probe = cinergyt2_usb_probe,
  224. .disconnect = dvb_usb_device_exit,
  225. .id_table = cinergyt2_usb_table
  226. };
  227. module_usb_driver(cinergyt2_driver);
  228. MODULE_DESCRIPTION("Terratec Cinergy T2 DVB-T driver");
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("Tomi Orava");