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