cinergyT2-core.c 6.6 KB

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