Merge branch 'topic/misc' into topic/pcsp-fix
[linux-2.6] / drivers / media / dvb / 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 int disable_remote;
36
37 module_param_named(debug, dvb_usb_cinergyt2_debug, int, 0644);
38 MODULE_PARM_DESC(debug, "set debugging level (1=info, xfer=2, rc=4 "
39                 "(or-able)).");
40
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
43 struct cinergyt2_state {
44         u8 rc_counter;
45 };
46
47 /* We are missing a release hook with usb_device data */
48 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         char buf[] = { CINERGYT2_EP1_CONTROL_STREAM_TRANSFER, enable ? 1 : 0 };
55         char result[64];
56         return dvb_usb_generic_rw(adap->dev, buf, sizeof(buf), result,
57                                 sizeof(result), 0);
58 }
59
60 static int cinergyt2_power_ctrl(struct dvb_usb_device *d, int enable)
61 {
62         char buf[] = { CINERGYT2_EP1_SLEEP_MODE, enable ? 0 : 1 };
63         char state[3];
64         return dvb_usb_generic_rw(d, buf, sizeof(buf), state, sizeof(state), 0);
65 }
66
67 static int cinergyt2_frontend_attach(struct dvb_usb_adapter *adap)
68 {
69         char query[] = { CINERGYT2_EP1_GET_FIRMWARE_VERSION };
70         char state[3];
71         int ret;
72
73         adap->fe = cinergyt2_fe_attach(adap->dev);
74
75         ret = dvb_usb_generic_rw(adap->dev, query, sizeof(query), state,
76                                 sizeof(state), 0);
77         if (ret < 0) {
78                 deb_rc("cinergyt2_power_ctrl() Failed to retrieve sleep "
79                         "state info\n");
80         }
81
82         /* Copy this pointer as we are gonna need it in the release phase */
83         cinergyt2_usb_device = adap->dev;
84
85         return 0;
86 }
87
88 static struct dvb_usb_rc_key cinergyt2_rc_keys[] = {
89         { 0x04, 0x01,   KEY_POWER },
90         { 0x04, 0x02,   KEY_1 },
91         { 0x04, 0x03,   KEY_2 },
92         { 0x04, 0x04,   KEY_3 },
93         { 0x04, 0x05,   KEY_4 },
94         { 0x04, 0x06,   KEY_5 },
95         { 0x04, 0x07,   KEY_6 },
96         { 0x04, 0x08,   KEY_7 },
97         { 0x04, 0x09,   KEY_8 },
98         { 0x04, 0x0a,   KEY_9 },
99         { 0x04, 0x0c,   KEY_0 },
100         { 0x04, 0x0b,   KEY_VIDEO },
101         { 0x04, 0x0d,   KEY_REFRESH },
102         { 0x04, 0x0e,   KEY_SELECT },
103         { 0x04, 0x0f,   KEY_EPG },
104         { 0x04, 0x10,   KEY_UP },
105         { 0x04, 0x14,   KEY_DOWN },
106         { 0x04, 0x11,   KEY_LEFT },
107         { 0x04, 0x13,   KEY_RIGHT },
108         { 0x04, 0x12,   KEY_OK },
109         { 0x04, 0x15,   KEY_TEXT },
110         { 0x04, 0x16,   KEY_INFO },
111         { 0x04, 0x17,   KEY_RED },
112         { 0x04, 0x18,   KEY_GREEN },
113         { 0x04, 0x19,   KEY_YELLOW },
114         { 0x04, 0x1a,   KEY_BLUE },
115         { 0x04, 0x1c,   KEY_VOLUMEUP },
116         { 0x04, 0x1e,   KEY_VOLUMEDOWN },
117         { 0x04, 0x1d,   KEY_MUTE },
118         { 0x04, 0x1b,   KEY_CHANNELUP },
119         { 0x04, 0x1f,   KEY_CHANNELDOWN },
120         { 0x04, 0x40,   KEY_PAUSE },
121         { 0x04, 0x4c,   KEY_PLAY },
122         { 0x04, 0x58,   KEY_RECORD },
123         { 0x04, 0x54,   KEY_PREVIOUS },
124         { 0x04, 0x48,   KEY_STOP },
125         { 0x04, 0x5c,   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
142 static int cinergyt2_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
143 {
144         struct cinergyt2_state *st = d->priv;
145         u8 key[5] = {0, 0, 0, 0, 0}, cmd = CINERGYT2_EP1_GET_RC_EVENTS;
146         int i;
147
148         *state = REMOTE_NO_KEY_PRESSED;
149
150         dvb_usb_generic_rw(d, &cmd, 1, key, sizeof(key), 0);
151         if (key[4] == 0xff) {
152                 /* key repeat */
153                 st->rc_counter++;
154                 if (st->rc_counter > RC_REPEAT_DELAY) {
155                         for (i = 0; i < ARRAY_SIZE(repeatable_keys); i++) {
156                                 if (d->last_event == repeatable_keys[i]) {
157                                         *state = REMOTE_KEY_REPEAT;
158                                         *event = d->last_event;
159                                         deb_rc("repeat key, event %x\n",
160                                                    *event);
161                                         return 0;
162                                 }
163                         }
164                         deb_rc("repeated key (non repeatable)\n");
165                 }
166                 return 0;
167         }
168
169         /* hack to pass checksum on the custom field */
170         key[2] = ~key[1];
171         dvb_usb_nec_rc_key_to_event(d, key, event, state);
172         if (key[0] != 0) {
173                 if (*event != d->last_event)
174                         st->rc_counter = 0;
175
176                 deb_rc("key: %x %x %x %x %x\n",
177                        key[0], key[1], key[2], key[3], key[4]);
178         }
179         return 0;
180 }
181
182 static int cinergyt2_usb_probe(struct usb_interface *intf,
183                                 const struct usb_device_id *id)
184 {
185         return dvb_usb_device_init(intf, &cinergyt2_properties,
186                                         THIS_MODULE, NULL, adapter_nr);
187 }
188
189
190 static struct usb_device_id cinergyt2_usb_table[] = {
191         { USB_DEVICE(USB_VID_TERRATEC, 0x0038) },
192         { 0 }
193 };
194
195 MODULE_DEVICE_TABLE(usb, cinergyt2_usb_table);
196
197 static struct dvb_usb_device_properties cinergyt2_properties = {
198         .size_of_priv = sizeof(struct cinergyt2_state),
199         .num_adapters = 1,
200         .adapter = {
201                 {
202                         .streaming_ctrl   = cinergyt2_streaming_ctrl,
203                         .frontend_attach  = cinergyt2_frontend_attach,
204
205                         /* parameter for the MPEG2-data transfer */
206                         .stream = {
207                                 .type = USB_BULK,
208                                 .count = 5,
209                                 .endpoint = 0x02,
210                                 .u = {
211                                         .bulk = {
212                                                 .buffersize = 512,
213                                         }
214                                 }
215                         },
216                 }
217         },
218
219         .power_ctrl       = cinergyt2_power_ctrl,
220
221         .rc_interval      = 50,
222         .rc_key_map       = cinergyt2_rc_keys,
223         .rc_key_map_size  = ARRAY_SIZE(cinergyt2_rc_keys),
224         .rc_query         = cinergyt2_rc_query,
225
226         .generic_bulk_ctrl_endpoint = 1,
227
228         .num_device_descs = 1,
229         .devices = {
230                 { .name = "TerraTec/qanu USB2.0 Highspeed DVB-T Receiver",
231                   .cold_ids = {NULL},
232                   .warm_ids = { &cinergyt2_usb_table[0], NULL },
233                 },
234                 { NULL },
235         }
236 };
237
238
239 static struct usb_driver cinergyt2_driver = {
240         .name           = "cinergyT2",
241         .probe          = cinergyt2_usb_probe,
242         .disconnect     = dvb_usb_device_exit,
243         .id_table       = cinergyt2_usb_table
244 };
245
246 static int __init cinergyt2_usb_init(void)
247 {
248         int err;
249
250         err = usb_register(&cinergyt2_driver);
251         if (err) {
252                 err("usb_register() failed! (err %i)\n", err);
253                 return err;
254         }
255         return 0;
256 }
257
258 static void __exit cinergyt2_usb_exit(void)
259 {
260         usb_deregister(&cinergyt2_driver);
261 }
262
263 module_init(cinergyt2_usb_init);
264 module_exit(cinergyt2_usb_exit);
265
266 MODULE_DESCRIPTION("Terratec Cinergy T2 DVB-T driver");
267 MODULE_LICENSE("GPL");
268 MODULE_AUTHOR("Tomi Orava");