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