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