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