cinergyT2-core.c 6.6 KB

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