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