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