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