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