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