1 /*
2  * Linux driver for Technisat DVB-S/S2 USB 2.0 device
3  *
4  * Copyright (C) 2010 Patrick Boettcher,
5  *                    Kernel Labs Inc. PO Box 745, St James, NY 11780
6  *
7  * Development was sponsored by Technisat Digital UK Limited, whose
8  * registered office is Witan Gate House 500 - 600 Witan Gate West,
9  * Milton Keynes, MK9 1SH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of the
14  * License, or (at your option) any later version.
15  *
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * THIS PROGRAM IS PROVIDED "AS IS" AND BOTH THE COPYRIGHT HOLDER AND
22  * TECHNISAT DIGITAL UK LTD DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS PROGRAM INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY OR
24  * FITNESS FOR A PARTICULAR PURPOSE.  NEITHER THE COPYRIGHT HOLDER
25  * NOR TECHNISAT DIGITAL UK LIMITED SHALL BE LIABLE FOR ANY SPECIAL,
26  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
27  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
28  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS PROGRAM. See the
30  * GNU General Public License for more details.
31  */
32 
33 #define DVB_USB_LOG_PREFIX "technisat-usb2"
34 #include "dvb-usb.h"
35 
36 #include "stv6110x.h"
37 #include "stv090x.h"
38 
39 /* module parameters */
40 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
41 
42 static int debug;
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug,
45 		"set debugging level (bit-mask: 1=info,2=eeprom,4=i2c,8=rc)." \
46 		DVB_USB_DEBUG_STATUS);
47 
48 /* disables all LED control command and
49  * also does not start the signal polling thread */
50 static int disable_led_control;
51 module_param(disable_led_control, int, 0444);
52 MODULE_PARM_DESC(disable_led_control,
53 		"disable LED control of the device (default: 0 - LED control is active).");
54 
55 /* device private data */
56 struct technisat_usb2_state {
57 	struct dvb_usb_device *dev;
58 	struct delayed_work green_led_work;
59 	u8 power_state;
60 
61 	u16 last_scan_code;
62 
63 	u8 buf[64];
64 };
65 
66 /* debug print helpers */
67 #define deb_info(args...)    dprintk(debug, 0x01, args)
68 #define deb_eeprom(args...)  dprintk(debug, 0x02, args)
69 #define deb_i2c(args...)     dprintk(debug, 0x04, args)
70 #define deb_rc(args...)      dprintk(debug, 0x08, args)
71 
72 /* vendor requests */
73 #define SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST 0xB3
74 #define SET_FRONT_END_RESET_VENDOR_REQUEST         0xB4
75 #define GET_VERSION_INFO_VENDOR_REQUEST            0xB5
76 #define SET_GREEN_LED_VENDOR_REQUEST               0xB6
77 #define SET_RED_LED_VENDOR_REQUEST                 0xB7
78 #define GET_IR_DATA_VENDOR_REQUEST                 0xB8
79 #define SET_LED_TIMER_DIVIDER_VENDOR_REQUEST       0xB9
80 #define SET_USB_REENUMERATION                      0xBA
81 
82 /* i2c-access methods */
83 #define I2C_SPEED_100KHZ_BIT 0x40
84 
85 #define I2C_STATUS_NAK 7
86 #define I2C_STATUS_OK 8
87 
88 static int technisat_usb2_i2c_access(struct usb_device *udev,
89 		u8 device_addr, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
90 {
91 	u8 *b;
92 	int ret, actual_length;
93 
94 	b = kmalloc(64, GFP_KERNEL);
95 	if (!b)
96 		return -ENOMEM;
97 
98 	deb_i2c("i2c-access: %02x, tx: ", device_addr);
99 	debug_dump(tx, txlen, deb_i2c);
100 	deb_i2c(" ");
101 
102 	if (txlen > 62) {
103 		err("i2c TX buffer can't exceed 62 bytes (dev 0x%02x)",
104 				device_addr);
105 		txlen = 62;
106 	}
107 	if (rxlen > 62) {
108 		err("i2c RX buffer can't exceed 62 bytes (dev 0x%02x)",
109 				device_addr);
110 		rxlen = 62;
111 	}
112 
113 	b[0] = I2C_SPEED_100KHZ_BIT;
114 	b[1] = device_addr << 1;
115 
116 	if (rx != NULL) {
117 		b[0] |= rxlen;
118 		b[1] |= 1;
119 	}
120 
121 	memcpy(&b[2], tx, txlen);
122 	ret = usb_bulk_msg(udev,
123 			usb_sndbulkpipe(udev, 0x01),
124 			b, 2 + txlen,
125 			NULL, 1000);
126 
127 	if (ret < 0) {
128 		err("i2c-error: out failed %02x = %d", device_addr, ret);
129 		goto err;
130 	}
131 
132 	ret = usb_bulk_msg(udev,
133 			usb_rcvbulkpipe(udev, 0x01),
134 			b, 64, &actual_length, 1000);
135 	if (ret < 0) {
136 		err("i2c-error: in failed %02x = %d", device_addr, ret);
137 		goto err;
138 	}
139 
140 	if (b[0] != I2C_STATUS_OK) {
141 		err("i2c-error: %02x = %d", device_addr, b[0]);
142 		/* handle tuner-i2c-nak */
143 		if (!(b[0] == I2C_STATUS_NAK &&
144 				device_addr == 0x60
145 				/* && device_is_technisat_usb2 */))
146 			goto err;
147 	}
148 
149 	deb_i2c("status: %d, ", b[0]);
150 
151 	if (rx != NULL) {
152 		memcpy(rx, &b[2], rxlen);
153 
154 		deb_i2c("rx (%d): ", rxlen);
155 		debug_dump(rx, rxlen, deb_i2c);
156 	}
157 
158 	deb_i2c("\n");
159 
160 err:
161 	kfree(b);
162 	return ret;
163 }
164 
165 static int technisat_usb2_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
166 				int num)
167 {
168 	int ret = 0, i;
169 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
170 
171 	/* Ensure nobody else hits the i2c bus while we're sending our
172 	   sequence of messages, (such as the remote control thread) */
173 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
174 		return -EAGAIN;
175 
176 	for (i = 0; i < num; i++) {
177 		if (i+1 < num && msg[i+1].flags & I2C_M_RD) {
178 			ret = technisat_usb2_i2c_access(d->udev, msg[i+1].addr,
179 						msg[i].buf, msg[i].len,
180 						msg[i+1].buf, msg[i+1].len);
181 			if (ret != 0)
182 				break;
183 			i++;
184 		} else {
185 			ret = technisat_usb2_i2c_access(d->udev, msg[i].addr,
186 						msg[i].buf, msg[i].len,
187 						NULL, 0);
188 			if (ret != 0)
189 				break;
190 		}
191 	}
192 
193 	if (ret == 0)
194 		ret = i;
195 
196 	mutex_unlock(&d->i2c_mutex);
197 
198 	return ret;
199 }
200 
201 static u32 technisat_usb2_i2c_func(struct i2c_adapter *adapter)
202 {
203 	return I2C_FUNC_I2C;
204 }
205 
206 static struct i2c_algorithm technisat_usb2_i2c_algo = {
207 	.master_xfer   = technisat_usb2_i2c_xfer,
208 	.functionality = technisat_usb2_i2c_func,
209 };
210 
211 #if 0
212 static void technisat_usb2_frontend_reset(struct usb_device *udev)
213 {
214 	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
215 			SET_FRONT_END_RESET_VENDOR_REQUEST,
216 			USB_TYPE_VENDOR | USB_DIR_OUT,
217 			10, 0,
218 			NULL, 0, 500);
219 }
220 #endif
221 
222 /* LED control */
223 enum technisat_usb2_led_state {
224 	TECH_LED_OFF,
225 	TECH_LED_BLINK,
226 	TECH_LED_ON,
227 	TECH_LED_UNDEFINED
228 };
229 
230 static int technisat_usb2_set_led(struct dvb_usb_device *d, int red,
231 				  enum technisat_usb2_led_state st)
232 {
233 	struct technisat_usb2_state *state = d->priv;
234 	u8 *led = state->buf;
235 	int ret;
236 
237 	led[0] = red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST;
238 
239 	if (disable_led_control && st != TECH_LED_OFF)
240 		return 0;
241 
242 	switch (st) {
243 	case TECH_LED_ON:
244 		led[1] = 0x82;
245 		break;
246 	case TECH_LED_BLINK:
247 		led[1] = 0x82;
248 		if (red) {
249 			led[2] = 0x02;
250 			led[3] = 10;
251 			led[4] = 10;
252 		} else {
253 			led[2] = 0xff;
254 			led[3] = 50;
255 			led[4] = 50;
256 		}
257 		led[5] = 1;
258 		break;
259 
260 	default:
261 	case TECH_LED_OFF:
262 		led[1] = 0x80;
263 		break;
264 	}
265 
266 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
267 		return -EAGAIN;
268 
269 	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
270 		red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST,
271 		USB_TYPE_VENDOR | USB_DIR_OUT,
272 		0, 0,
273 		led, 8, 500);
274 
275 	mutex_unlock(&d->i2c_mutex);
276 	return ret;
277 }
278 
279 static int technisat_usb2_set_led_timer(struct dvb_usb_device *d, u8 red, u8 green)
280 {
281 	struct technisat_usb2_state *state = d->priv;
282 	u8 *b = state->buf;
283 	int ret;
284 
285 	b[0] = 0;
286 
287 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
288 		return -EAGAIN;
289 
290 	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
291 		SET_LED_TIMER_DIVIDER_VENDOR_REQUEST,
292 		USB_TYPE_VENDOR | USB_DIR_OUT,
293 		(red << 8) | green, 0,
294 		b, 1, 500);
295 
296 	mutex_unlock(&d->i2c_mutex);
297 
298 	return ret;
299 }
300 
301 static void technisat_usb2_green_led_control(struct work_struct *work)
302 {
303 	struct technisat_usb2_state *state =
304 		container_of(work, struct technisat_usb2_state, green_led_work.work);
305 	struct dvb_frontend *fe = state->dev->adapter[0].fe_adap[0].fe;
306 
307 	if (state->power_state == 0)
308 		goto schedule;
309 
310 	if (fe != NULL) {
311 		enum fe_status status;
312 
313 		if (fe->ops.read_status(fe, &status) != 0)
314 			goto schedule;
315 
316 		if (status & FE_HAS_LOCK) {
317 			u32 ber;
318 
319 			if (fe->ops.read_ber(fe, &ber) != 0)
320 				goto schedule;
321 
322 			if (ber > 1000)
323 				technisat_usb2_set_led(state->dev, 0, TECH_LED_BLINK);
324 			else
325 				technisat_usb2_set_led(state->dev, 0, TECH_LED_ON);
326 		} else
327 			technisat_usb2_set_led(state->dev, 0, TECH_LED_OFF);
328 	}
329 
330 schedule:
331 	schedule_delayed_work(&state->green_led_work,
332 			msecs_to_jiffies(500));
333 }
334 
335 /* method to find out whether the firmware has to be downloaded or not */
336 static int technisat_usb2_identify_state(struct usb_device *udev,
337 		struct dvb_usb_device_properties *props,
338 		struct dvb_usb_device_description **desc, int *cold)
339 {
340 	int ret;
341 	u8 *version;
342 
343 	version = kmalloc(3, GFP_KERNEL);
344 	if (!version)
345 		return -ENOMEM;
346 
347 	/* first select the interface */
348 	if (usb_set_interface(udev, 0, 1) != 0)
349 		err("could not set alternate setting to 0");
350 	else
351 		info("set alternate setting");
352 
353 	*cold = 0; /* by default do not download a firmware - just in case something is wrong */
354 
355 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
356 		GET_VERSION_INFO_VENDOR_REQUEST,
357 		USB_TYPE_VENDOR | USB_DIR_IN,
358 		0, 0,
359 		version, 3, 500);
360 
361 	if (ret < 0)
362 		*cold = 1;
363 	else {
364 		info("firmware version: %d.%d", version[1], version[2]);
365 		*cold = 0;
366 	}
367 
368 	kfree(version);
369 
370 	return 0;
371 }
372 
373 /* power control */
374 static int technisat_usb2_power_ctrl(struct dvb_usb_device *d, int level)
375 {
376 	struct technisat_usb2_state *state = d->priv;
377 
378 	state->power_state = level;
379 
380 	if (disable_led_control)
381 		return 0;
382 
383 	/* green led is turned off in any case - will be turned on when tuning */
384 	technisat_usb2_set_led(d, 0, TECH_LED_OFF);
385 	/* red led is turned on all the time */
386 	technisat_usb2_set_led(d, 1, TECH_LED_ON);
387 	return 0;
388 }
389 
390 /* mac address reading - from the eeprom */
391 #if 0
392 static void technisat_usb2_eeprom_dump(struct dvb_usb_device *d)
393 {
394 	u8 reg;
395 	u8 b[16];
396 	int i, j;
397 
398 	/* full EEPROM dump */
399 	for (j = 0; j < 256 * 4; j += 16) {
400 		reg = j;
401 		if (technisat_usb2_i2c_access(d->udev, 0x50 + j / 256, &reg, 1, b, 16) != 0)
402 			break;
403 
404 		deb_eeprom("EEPROM: %01x%02x: ", j / 256, reg);
405 		for (i = 0; i < 16; i++)
406 			deb_eeprom("%02x ", b[i]);
407 		deb_eeprom("\n");
408 	}
409 }
410 #endif
411 
412 static u8 technisat_usb2_calc_lrc(const u8 *b, u16 length)
413 {
414 	u8 lrc = 0;
415 	while (--length)
416 		lrc ^= *b++;
417 	return lrc;
418 }
419 
420 static int technisat_usb2_eeprom_lrc_read(struct dvb_usb_device *d,
421 	u16 offset, u8 *b, u16 length, u8 tries)
422 {
423 	u8 bo = offset & 0xff;
424 	struct i2c_msg msg[] = {
425 		{
426 			.addr = 0x50 | ((offset >> 8) & 0x3),
427 			.buf = &bo,
428 			.len = 1
429 		}, {
430 			.addr = 0x50 | ((offset >> 8) & 0x3),
431 			.flags	= I2C_M_RD,
432 			.buf = b,
433 			.len = length
434 		}
435 	};
436 
437 	while (tries--) {
438 		int status;
439 
440 		if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
441 			break;
442 
443 		status =
444 			technisat_usb2_calc_lrc(b, length - 1) == b[length - 1];
445 
446 		if (status)
447 			return 0;
448 	}
449 
450 	return -EREMOTEIO;
451 }
452 
453 #define EEPROM_MAC_START 0x3f8
454 #define EEPROM_MAC_TOTAL 8
455 static int technisat_usb2_read_mac_address(struct dvb_usb_device *d,
456 		u8 mac[])
457 {
458 	u8 buf[EEPROM_MAC_TOTAL];
459 
460 	if (technisat_usb2_eeprom_lrc_read(d, EEPROM_MAC_START,
461 				buf, EEPROM_MAC_TOTAL, 4) != 0)
462 		return -ENODEV;
463 
464 	memcpy(mac, buf, 6);
465 	return 0;
466 }
467 
468 static struct stv090x_config technisat_usb2_stv090x_config;
469 
470 /* frontend attach */
471 static int technisat_usb2_set_voltage(struct dvb_frontend *fe,
472 				      enum fe_sec_voltage voltage)
473 {
474 	int i;
475 	u8 gpio[3] = { 0 }; /* 0 = 2, 1 = 3, 2 = 4 */
476 
477 	gpio[2] = 1; /* high - voltage ? */
478 
479 	switch (voltage) {
480 	case SEC_VOLTAGE_13:
481 		gpio[0] = 1;
482 		break;
483 	case SEC_VOLTAGE_18:
484 		gpio[0] = 1;
485 		gpio[1] = 1;
486 		break;
487 	default:
488 	case SEC_VOLTAGE_OFF:
489 		break;
490 	}
491 
492 	for (i = 0; i < 3; i++)
493 		if (technisat_usb2_stv090x_config.set_gpio(fe, i+2, 0,
494 							   gpio[i], 0) != 0)
495 			return -EREMOTEIO;
496 	return 0;
497 }
498 
499 static struct stv090x_config technisat_usb2_stv090x_config = {
500 	.device         = STV0903,
501 	.demod_mode     = STV090x_SINGLE,
502 	.clk_mode       = STV090x_CLK_EXT,
503 
504 	.xtal           = 8000000,
505 	.address        = 0x68,
506 
507 	.ts1_mode       = STV090x_TSMODE_DVBCI,
508 	.ts1_clk        = 13400000,
509 	.ts1_tei        = 1,
510 
511 	.repeater_level = STV090x_RPTLEVEL_64,
512 
513 	.tuner_bbgain   = 6,
514 };
515 
516 static struct stv6110x_config technisat_usb2_stv6110x_config = {
517 	.addr           = 0x60,
518 	.refclk         = 16000000,
519 	.clk_div        = 2,
520 };
521 
522 static int technisat_usb2_frontend_attach(struct dvb_usb_adapter *a)
523 {
524 	struct usb_device *udev = a->dev->udev;
525 	int ret;
526 
527 	a->fe_adap[0].fe = dvb_attach(stv090x_attach, &technisat_usb2_stv090x_config,
528 			&a->dev->i2c_adap, STV090x_DEMODULATOR_0);
529 
530 	if (a->fe_adap[0].fe) {
531 		const struct stv6110x_devctl *ctl;
532 
533 		ctl = dvb_attach(stv6110x_attach,
534 				a->fe_adap[0].fe,
535 				&technisat_usb2_stv6110x_config,
536 				&a->dev->i2c_adap);
537 
538 		if (ctl) {
539 			technisat_usb2_stv090x_config.tuner_init          = ctl->tuner_init;
540 			technisat_usb2_stv090x_config.tuner_sleep         = ctl->tuner_sleep;
541 			technisat_usb2_stv090x_config.tuner_set_mode      = ctl->tuner_set_mode;
542 			technisat_usb2_stv090x_config.tuner_set_frequency = ctl->tuner_set_frequency;
543 			technisat_usb2_stv090x_config.tuner_get_frequency = ctl->tuner_get_frequency;
544 			technisat_usb2_stv090x_config.tuner_set_bandwidth = ctl->tuner_set_bandwidth;
545 			technisat_usb2_stv090x_config.tuner_get_bandwidth = ctl->tuner_get_bandwidth;
546 			technisat_usb2_stv090x_config.tuner_set_bbgain    = ctl->tuner_set_bbgain;
547 			technisat_usb2_stv090x_config.tuner_get_bbgain    = ctl->tuner_get_bbgain;
548 			technisat_usb2_stv090x_config.tuner_set_refclk    = ctl->tuner_set_refclk;
549 			technisat_usb2_stv090x_config.tuner_get_status    = ctl->tuner_get_status;
550 
551 			/* call the init function once to initialize
552 			   tuner's clock output divider and demod's
553 			   master clock */
554 			if (a->fe_adap[0].fe->ops.init)
555 				a->fe_adap[0].fe->ops.init(a->fe_adap[0].fe);
556 
557 			if (mutex_lock_interruptible(&a->dev->i2c_mutex) < 0)
558 				return -EAGAIN;
559 
560 			ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
561 					SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST,
562 					USB_TYPE_VENDOR | USB_DIR_OUT,
563 					0, 0,
564 					NULL, 0, 500);
565 			mutex_unlock(&a->dev->i2c_mutex);
566 
567 			if (ret != 0)
568 				err("could not set IF_CLK to external");
569 
570 			a->fe_adap[0].fe->ops.set_voltage = technisat_usb2_set_voltage;
571 
572 			/* if everything was successful assign a nice name to the frontend */
573 			strlcpy(a->fe_adap[0].fe->ops.info.name, a->dev->desc->name,
574 					sizeof(a->fe_adap[0].fe->ops.info.name));
575 		} else {
576 			dvb_frontend_detach(a->fe_adap[0].fe);
577 			a->fe_adap[0].fe = NULL;
578 		}
579 	}
580 
581 	technisat_usb2_set_led_timer(a->dev, 1, 1);
582 
583 	return a->fe_adap[0].fe == NULL ? -ENODEV : 0;
584 }
585 
586 /* Remote control */
587 
588 /* the device is giving providing raw IR-signals to the host mapping
589  * it only to one remote control is just the default implementation
590  */
591 #define NOMINAL_IR_BIT_TRANSITION_TIME_US 889
592 #define NOMINAL_IR_BIT_TIME_US (2 * NOMINAL_IR_BIT_TRANSITION_TIME_US)
593 
594 #define FIRMWARE_CLOCK_TICK 83333
595 #define FIRMWARE_CLOCK_DIVISOR 256
596 
597 #define IR_PERCENT_TOLERANCE 15
598 
599 #define NOMINAL_IR_BIT_TRANSITION_TICKS ((NOMINAL_IR_BIT_TRANSITION_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
600 #define NOMINAL_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICKS / FIRMWARE_CLOCK_DIVISOR)
601 
602 #define NOMINAL_IR_BIT_TIME_TICKS ((NOMINAL_IR_BIT_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
603 #define NOMINAL_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICKS / FIRMWARE_CLOCK_DIVISOR)
604 
605 #define MINIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT - ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
606 #define MAXIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT + ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
607 
608 #define MINIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT - ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
609 #define MAXIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT + ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
610 
611 static int technisat_usb2_get_ir(struct dvb_usb_device *d)
612 {
613 	struct technisat_usb2_state *state = d->priv;
614 	u8 *buf = state->buf;
615 	u8 *b;
616 	int ret;
617 	struct ir_raw_event ev;
618 
619 	buf[0] = GET_IR_DATA_VENDOR_REQUEST;
620 	buf[1] = 0x08;
621 	buf[2] = 0x8f;
622 	buf[3] = MINIMUM_IR_BIT_TRANSITION_TICK_COUNT;
623 	buf[4] = MAXIMUM_IR_BIT_TIME_TICK_COUNT;
624 
625 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
626 		return -EAGAIN;
627 	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
628 			GET_IR_DATA_VENDOR_REQUEST,
629 			USB_TYPE_VENDOR | USB_DIR_OUT,
630 			0, 0,
631 			buf, 5, 500);
632 	if (ret < 0)
633 		goto unlock;
634 
635 	buf[1] = 0;
636 	buf[2] = 0;
637 	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
638 			GET_IR_DATA_VENDOR_REQUEST,
639 			USB_TYPE_VENDOR | USB_DIR_IN,
640 			0x8080, 0,
641 			buf, 62, 500);
642 
643 unlock:
644 	mutex_unlock(&d->i2c_mutex);
645 
646 	if (ret < 0)
647 		return ret;
648 
649 	if (ret == 1)
650 		return 0; /* no key pressed */
651 
652 	/* decoding */
653 	b = buf+1;
654 
655 #if 0
656 	deb_rc("RC: %d ", ret);
657 	debug_dump(b, ret, deb_rc);
658 #endif
659 
660 	ev.pulse = 0;
661 	while (1) {
662 		ev.pulse = !ev.pulse;
663 		ev.duration = (*b * FIRMWARE_CLOCK_DIVISOR * FIRMWARE_CLOCK_TICK) / 1000;
664 		ir_raw_event_store(d->rc_dev, &ev);
665 
666 		b++;
667 		if (*b == 0xff) {
668 			ev.pulse = 0;
669 			ev.duration = 888888*2;
670 			ir_raw_event_store(d->rc_dev, &ev);
671 			break;
672 		}
673 	}
674 
675 	ir_raw_event_handle(d->rc_dev);
676 
677 	return 1;
678 }
679 
680 static int technisat_usb2_rc_query(struct dvb_usb_device *d)
681 {
682 	int ret = technisat_usb2_get_ir(d);
683 
684 	if (ret < 0)
685 		return ret;
686 
687 	if (ret == 0)
688 		return 0;
689 
690 	if (!disable_led_control)
691 		technisat_usb2_set_led(d, 1, TECH_LED_BLINK);
692 
693 	return 0;
694 }
695 
696 /* DVB-USB and USB stuff follows */
697 static struct usb_device_id technisat_usb2_id_table[] = {
698 	{ USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_DVB_S2) },
699 	{ 0 }		/* Terminating entry */
700 };
701 MODULE_DEVICE_TABLE(usb, technisat_usb2_id_table);
702 
703 /* device description */
704 static struct dvb_usb_device_properties technisat_usb2_devices = {
705 	.caps              = DVB_USB_IS_AN_I2C_ADAPTER,
706 
707 	.usb_ctrl          = CYPRESS_FX2,
708 
709 	.identify_state    = technisat_usb2_identify_state,
710 	.firmware          = "dvb-usb-SkyStar_USB_HD_FW_v17_63.HEX.fw",
711 
712 	.size_of_priv      = sizeof(struct technisat_usb2_state),
713 
714 	.i2c_algo          = &technisat_usb2_i2c_algo,
715 
716 	.power_ctrl        = technisat_usb2_power_ctrl,
717 	.read_mac_address  = technisat_usb2_read_mac_address,
718 
719 	.num_adapters = 1,
720 	.adapter = {
721 		{
722 		.num_frontends = 1,
723 		.fe = {{
724 			.frontend_attach  = technisat_usb2_frontend_attach,
725 
726 			.stream = {
727 				.type = USB_ISOC,
728 				.count = 4,
729 				.endpoint = 0x2,
730 				.u = {
731 					.isoc = {
732 						.framesperurb = 32,
733 						.framesize = 2048,
734 						.interval = 1,
735 					}
736 				}
737 			},
738 		}},
739 			.size_of_priv = 0,
740 		},
741 	},
742 
743 	.num_device_descs = 1,
744 	.devices = {
745 		{   "Technisat SkyStar USB HD (DVB-S/S2)",
746 			{ &technisat_usb2_id_table[0], NULL },
747 			{ NULL },
748 		},
749 	},
750 
751 	.rc.core = {
752 		.rc_interval = 100,
753 		.rc_codes    = RC_MAP_TECHNISAT_USB2,
754 		.module_name = "technisat-usb2",
755 		.rc_query    = technisat_usb2_rc_query,
756 		.allowed_protos = RC_BIT_ALL,
757 		.driver_type    = RC_DRIVER_IR_RAW,
758 	}
759 };
760 
761 static int technisat_usb2_probe(struct usb_interface *intf,
762 		const struct usb_device_id *id)
763 {
764 	struct dvb_usb_device *dev;
765 
766 	if (dvb_usb_device_init(intf, &technisat_usb2_devices, THIS_MODULE,
767 				&dev, adapter_nr) != 0)
768 		return -ENODEV;
769 
770 	if (dev) {
771 		struct technisat_usb2_state *state = dev->priv;
772 		state->dev = dev;
773 
774 		if (!disable_led_control) {
775 			INIT_DELAYED_WORK(&state->green_led_work,
776 					technisat_usb2_green_led_control);
777 			schedule_delayed_work(&state->green_led_work,
778 					msecs_to_jiffies(500));
779 		}
780 	}
781 
782 	return 0;
783 }
784 
785 static void technisat_usb2_disconnect(struct usb_interface *intf)
786 {
787 	struct dvb_usb_device *dev = usb_get_intfdata(intf);
788 
789 	/* work and stuff was only created when the device is is hot-state */
790 	if (dev != NULL) {
791 		struct technisat_usb2_state *state = dev->priv;
792 		if (state != NULL)
793 			cancel_delayed_work_sync(&state->green_led_work);
794 	}
795 
796 	dvb_usb_device_exit(intf);
797 }
798 
799 static struct usb_driver technisat_usb2_driver = {
800 	.name       = "dvb_usb_technisat_usb2",
801 	.probe      = technisat_usb2_probe,
802 	.disconnect = technisat_usb2_disconnect,
803 	.id_table   = technisat_usb2_id_table,
804 };
805 
806 module_usb_driver(technisat_usb2_driver);
807 
808 MODULE_AUTHOR("Patrick Boettcher <pboettcher@kernellabs.com>");
809 MODULE_DESCRIPTION("Driver for Technisat DVB-S/S2 USB 2.0 device");
810 MODULE_VERSION("1.0");
811 MODULE_LICENSE("GPL");
812