18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Linux driver for Technisat DVB-S/S2 USB 2.0 device
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2010 Patrick Boettcher,
58c2ecf20Sopenharmony_ci *                    Kernel Labs Inc. PO Box 745, St James, NY 11780
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Development was sponsored by Technisat Digital UK Limited, whose
88c2ecf20Sopenharmony_ci * registered office is Witan Gate House 500 - 600 Witan Gate West,
98c2ecf20Sopenharmony_ci * Milton Keynes, MK9 1SH
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
128c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License as
138c2ecf20Sopenharmony_ci * published by the Free Software Foundation; either version 2 of the
148c2ecf20Sopenharmony_ci * License, or (at your option) any later version.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * THIS PROGRAM IS PROVIDED "AS IS" AND BOTH THE COPYRIGHT HOLDER AND
188c2ecf20Sopenharmony_ci * TECHNISAT DIGITAL UK LTD DISCLAIM ALL WARRANTIES WITH REGARD TO
198c2ecf20Sopenharmony_ci * THIS PROGRAM INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY OR
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE.  NEITHER THE COPYRIGHT HOLDER
218c2ecf20Sopenharmony_ci * NOR TECHNISAT DIGITAL UK LIMITED SHALL BE LIABLE FOR ANY SPECIAL,
228c2ecf20Sopenharmony_ci * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
238c2ecf20Sopenharmony_ci * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
248c2ecf20Sopenharmony_ci * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
258c2ecf20Sopenharmony_ci * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS PROGRAM. See the
268c2ecf20Sopenharmony_ci * GNU General Public License for more details.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DVB_USB_LOG_PREFIX "technisat-usb2"
308c2ecf20Sopenharmony_ci#include "dvb-usb.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "stv6110x.h"
338c2ecf20Sopenharmony_ci#include "stv090x.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* module parameters */
368c2ecf20Sopenharmony_ciDVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int debug;
398c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug,
418c2ecf20Sopenharmony_ci		"set debugging level (bit-mask: 1=info,2=eeprom,4=i2c,8=rc)." \
428c2ecf20Sopenharmony_ci		DVB_USB_DEBUG_STATUS);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* disables all LED control command and
458c2ecf20Sopenharmony_ci * also does not start the signal polling thread */
468c2ecf20Sopenharmony_cistatic int disable_led_control;
478c2ecf20Sopenharmony_cimodule_param(disable_led_control, int, 0444);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(disable_led_control,
498c2ecf20Sopenharmony_ci		"disable LED control of the device (default: 0 - LED control is active).");
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* device private data */
528c2ecf20Sopenharmony_cistruct technisat_usb2_state {
538c2ecf20Sopenharmony_ci	struct dvb_usb_device *dev;
548c2ecf20Sopenharmony_ci	struct delayed_work green_led_work;
558c2ecf20Sopenharmony_ci	u8 power_state;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	u16 last_scan_code;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	u8 buf[64];
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* debug print helpers */
638c2ecf20Sopenharmony_ci#define deb_info(args...)    dprintk(debug, 0x01, args)
648c2ecf20Sopenharmony_ci#define deb_eeprom(args...)  dprintk(debug, 0x02, args)
658c2ecf20Sopenharmony_ci#define deb_i2c(args...)     dprintk(debug, 0x04, args)
668c2ecf20Sopenharmony_ci#define deb_rc(args...)      dprintk(debug, 0x08, args)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* vendor requests */
698c2ecf20Sopenharmony_ci#define SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST 0xB3
708c2ecf20Sopenharmony_ci#define SET_FRONT_END_RESET_VENDOR_REQUEST         0xB4
718c2ecf20Sopenharmony_ci#define GET_VERSION_INFO_VENDOR_REQUEST            0xB5
728c2ecf20Sopenharmony_ci#define SET_GREEN_LED_VENDOR_REQUEST               0xB6
738c2ecf20Sopenharmony_ci#define SET_RED_LED_VENDOR_REQUEST                 0xB7
748c2ecf20Sopenharmony_ci#define GET_IR_DATA_VENDOR_REQUEST                 0xB8
758c2ecf20Sopenharmony_ci#define SET_LED_TIMER_DIVIDER_VENDOR_REQUEST       0xB9
768c2ecf20Sopenharmony_ci#define SET_USB_REENUMERATION                      0xBA
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* i2c-access methods */
798c2ecf20Sopenharmony_ci#define I2C_SPEED_100KHZ_BIT 0x40
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define I2C_STATUS_NAK 7
828c2ecf20Sopenharmony_ci#define I2C_STATUS_OK 8
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int technisat_usb2_i2c_access(struct usb_device *udev,
858c2ecf20Sopenharmony_ci		u8 device_addr, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	u8 *b;
888c2ecf20Sopenharmony_ci	int ret, actual_length;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	b = kmalloc(64, GFP_KERNEL);
918c2ecf20Sopenharmony_ci	if (!b)
928c2ecf20Sopenharmony_ci		return -ENOMEM;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	deb_i2c("i2c-access: %02x, tx: ", device_addr);
958c2ecf20Sopenharmony_ci	debug_dump(tx, txlen, deb_i2c);
968c2ecf20Sopenharmony_ci	deb_i2c(" ");
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (txlen > 62) {
998c2ecf20Sopenharmony_ci		err("i2c TX buffer can't exceed 62 bytes (dev 0x%02x)",
1008c2ecf20Sopenharmony_ci				device_addr);
1018c2ecf20Sopenharmony_ci		txlen = 62;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	if (rxlen > 62) {
1048c2ecf20Sopenharmony_ci		err("i2c RX buffer can't exceed 62 bytes (dev 0x%02x)",
1058c2ecf20Sopenharmony_ci				device_addr);
1068c2ecf20Sopenharmony_ci		rxlen = 62;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	b[0] = I2C_SPEED_100KHZ_BIT;
1108c2ecf20Sopenharmony_ci	b[1] = device_addr << 1;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (rx != NULL) {
1138c2ecf20Sopenharmony_ci		b[0] |= rxlen;
1148c2ecf20Sopenharmony_ci		b[1] |= 1;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	memcpy(&b[2], tx, txlen);
1188c2ecf20Sopenharmony_ci	ret = usb_bulk_msg(udev,
1198c2ecf20Sopenharmony_ci			usb_sndbulkpipe(udev, 0x01),
1208c2ecf20Sopenharmony_ci			b, 2 + txlen,
1218c2ecf20Sopenharmony_ci			NULL, 1000);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (ret < 0) {
1248c2ecf20Sopenharmony_ci		err("i2c-error: out failed %02x = %d", device_addr, ret);
1258c2ecf20Sopenharmony_ci		goto err;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ret = usb_bulk_msg(udev,
1298c2ecf20Sopenharmony_ci			usb_rcvbulkpipe(udev, 0x01),
1308c2ecf20Sopenharmony_ci			b, 64, &actual_length, 1000);
1318c2ecf20Sopenharmony_ci	if (ret < 0) {
1328c2ecf20Sopenharmony_ci		err("i2c-error: in failed %02x = %d", device_addr, ret);
1338c2ecf20Sopenharmony_ci		goto err;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (b[0] != I2C_STATUS_OK) {
1378c2ecf20Sopenharmony_ci		err("i2c-error: %02x = %d", device_addr, b[0]);
1388c2ecf20Sopenharmony_ci		/* handle tuner-i2c-nak */
1398c2ecf20Sopenharmony_ci		if (!(b[0] == I2C_STATUS_NAK &&
1408c2ecf20Sopenharmony_ci				device_addr == 0x60
1418c2ecf20Sopenharmony_ci				/* && device_is_technisat_usb2 */))
1428c2ecf20Sopenharmony_ci			goto err;
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	deb_i2c("status: %d, ", b[0]);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (rx != NULL) {
1488c2ecf20Sopenharmony_ci		memcpy(rx, &b[2], rxlen);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		deb_i2c("rx (%d): ", rxlen);
1518c2ecf20Sopenharmony_ci		debug_dump(rx, rxlen, deb_i2c);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	deb_i2c("\n");
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cierr:
1578c2ecf20Sopenharmony_ci	kfree(b);
1588c2ecf20Sopenharmony_ci	return ret;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic int technisat_usb2_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
1628c2ecf20Sopenharmony_ci				int num)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	int ret = 0, i;
1658c2ecf20Sopenharmony_ci	struct dvb_usb_device *d = i2c_get_adapdata(adap);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* Ensure nobody else hits the i2c bus while we're sending our
1688c2ecf20Sopenharmony_ci	   sequence of messages, (such as the remote control thread) */
1698c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
1708c2ecf20Sopenharmony_ci		return -EAGAIN;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++) {
1738c2ecf20Sopenharmony_ci		if (i+1 < num && msg[i+1].flags & I2C_M_RD) {
1748c2ecf20Sopenharmony_ci			ret = technisat_usb2_i2c_access(d->udev, msg[i+1].addr,
1758c2ecf20Sopenharmony_ci						msg[i].buf, msg[i].len,
1768c2ecf20Sopenharmony_ci						msg[i+1].buf, msg[i+1].len);
1778c2ecf20Sopenharmony_ci			if (ret != 0)
1788c2ecf20Sopenharmony_ci				break;
1798c2ecf20Sopenharmony_ci			i++;
1808c2ecf20Sopenharmony_ci		} else {
1818c2ecf20Sopenharmony_ci			ret = technisat_usb2_i2c_access(d->udev, msg[i].addr,
1828c2ecf20Sopenharmony_ci						msg[i].buf, msg[i].len,
1838c2ecf20Sopenharmony_ci						NULL, 0);
1848c2ecf20Sopenharmony_ci			if (ret != 0)
1858c2ecf20Sopenharmony_ci				break;
1868c2ecf20Sopenharmony_ci		}
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (ret == 0)
1908c2ecf20Sopenharmony_ci		ret = i;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	mutex_unlock(&d->i2c_mutex);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return ret;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic u32 technisat_usb2_i2c_func(struct i2c_adapter *adapter)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic struct i2c_algorithm technisat_usb2_i2c_algo = {
2038c2ecf20Sopenharmony_ci	.master_xfer   = technisat_usb2_i2c_xfer,
2048c2ecf20Sopenharmony_ci	.functionality = technisat_usb2_i2c_func,
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#if 0
2088c2ecf20Sopenharmony_cistatic void technisat_usb2_frontend_reset(struct usb_device *udev)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2118c2ecf20Sopenharmony_ci			SET_FRONT_END_RESET_VENDOR_REQUEST,
2128c2ecf20Sopenharmony_ci			USB_TYPE_VENDOR | USB_DIR_OUT,
2138c2ecf20Sopenharmony_ci			10, 0,
2148c2ecf20Sopenharmony_ci			NULL, 0, 500);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci#endif
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/* LED control */
2198c2ecf20Sopenharmony_cienum technisat_usb2_led_state {
2208c2ecf20Sopenharmony_ci	TECH_LED_OFF,
2218c2ecf20Sopenharmony_ci	TECH_LED_BLINK,
2228c2ecf20Sopenharmony_ci	TECH_LED_ON,
2238c2ecf20Sopenharmony_ci	TECH_LED_UNDEFINED
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int technisat_usb2_set_led(struct dvb_usb_device *d, int red,
2278c2ecf20Sopenharmony_ci				  enum technisat_usb2_led_state st)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	struct technisat_usb2_state *state = d->priv;
2308c2ecf20Sopenharmony_ci	u8 *led = state->buf;
2318c2ecf20Sopenharmony_ci	int ret;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	led[0] = red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (disable_led_control && st != TECH_LED_OFF)
2368c2ecf20Sopenharmony_ci		return 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	switch (st) {
2398c2ecf20Sopenharmony_ci	case TECH_LED_ON:
2408c2ecf20Sopenharmony_ci		led[1] = 0x82;
2418c2ecf20Sopenharmony_ci		break;
2428c2ecf20Sopenharmony_ci	case TECH_LED_BLINK:
2438c2ecf20Sopenharmony_ci		led[1] = 0x82;
2448c2ecf20Sopenharmony_ci		if (red) {
2458c2ecf20Sopenharmony_ci			led[2] = 0x02;
2468c2ecf20Sopenharmony_ci			led[3] = 10;
2478c2ecf20Sopenharmony_ci			led[4] = 10;
2488c2ecf20Sopenharmony_ci		} else {
2498c2ecf20Sopenharmony_ci			led[2] = 0xff;
2508c2ecf20Sopenharmony_ci			led[3] = 50;
2518c2ecf20Sopenharmony_ci			led[4] = 50;
2528c2ecf20Sopenharmony_ci		}
2538c2ecf20Sopenharmony_ci		led[5] = 1;
2548c2ecf20Sopenharmony_ci		break;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	default:
2578c2ecf20Sopenharmony_ci	case TECH_LED_OFF:
2588c2ecf20Sopenharmony_ci		led[1] = 0x80;
2598c2ecf20Sopenharmony_ci		break;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
2638c2ecf20Sopenharmony_ci		return -EAGAIN;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
2668c2ecf20Sopenharmony_ci		red ? SET_RED_LED_VENDOR_REQUEST : SET_GREEN_LED_VENDOR_REQUEST,
2678c2ecf20Sopenharmony_ci		USB_TYPE_VENDOR | USB_DIR_OUT,
2688c2ecf20Sopenharmony_ci		0, 0,
2698c2ecf20Sopenharmony_ci		led, 8, 500);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	mutex_unlock(&d->i2c_mutex);
2728c2ecf20Sopenharmony_ci	return ret;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic int technisat_usb2_set_led_timer(struct dvb_usb_device *d, u8 red, u8 green)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct technisat_usb2_state *state = d->priv;
2788c2ecf20Sopenharmony_ci	u8 *b = state->buf;
2798c2ecf20Sopenharmony_ci	int ret;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	b[0] = 0;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
2848c2ecf20Sopenharmony_ci		return -EAGAIN;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
2878c2ecf20Sopenharmony_ci		SET_LED_TIMER_DIVIDER_VENDOR_REQUEST,
2888c2ecf20Sopenharmony_ci		USB_TYPE_VENDOR | USB_DIR_OUT,
2898c2ecf20Sopenharmony_ci		(red << 8) | green, 0,
2908c2ecf20Sopenharmony_ci		b, 1, 500);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	mutex_unlock(&d->i2c_mutex);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic void technisat_usb2_green_led_control(struct work_struct *work)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct technisat_usb2_state *state =
3008c2ecf20Sopenharmony_ci		container_of(work, struct technisat_usb2_state, green_led_work.work);
3018c2ecf20Sopenharmony_ci	struct dvb_frontend *fe = state->dev->adapter[0].fe_adap[0].fe;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (state->power_state == 0)
3048c2ecf20Sopenharmony_ci		goto schedule;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	if (fe != NULL) {
3078c2ecf20Sopenharmony_ci		enum fe_status status;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci		if (fe->ops.read_status(fe, &status) != 0)
3108c2ecf20Sopenharmony_ci			goto schedule;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		if (status & FE_HAS_LOCK) {
3138c2ecf20Sopenharmony_ci			u32 ber;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci			if (fe->ops.read_ber(fe, &ber) != 0)
3168c2ecf20Sopenharmony_ci				goto schedule;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci			if (ber > 1000)
3198c2ecf20Sopenharmony_ci				technisat_usb2_set_led(state->dev, 0, TECH_LED_BLINK);
3208c2ecf20Sopenharmony_ci			else
3218c2ecf20Sopenharmony_ci				technisat_usb2_set_led(state->dev, 0, TECH_LED_ON);
3228c2ecf20Sopenharmony_ci		} else
3238c2ecf20Sopenharmony_ci			technisat_usb2_set_led(state->dev, 0, TECH_LED_OFF);
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cischedule:
3278c2ecf20Sopenharmony_ci	schedule_delayed_work(&state->green_led_work,
3288c2ecf20Sopenharmony_ci			msecs_to_jiffies(500));
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/* method to find out whether the firmware has to be downloaded or not */
3328c2ecf20Sopenharmony_cistatic int technisat_usb2_identify_state(struct usb_device *udev,
3338c2ecf20Sopenharmony_ci		const struct dvb_usb_device_properties *props,
3348c2ecf20Sopenharmony_ci		const struct dvb_usb_device_description **desc, int *cold)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	int ret;
3378c2ecf20Sopenharmony_ci	u8 *version;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	version = kmalloc(3, GFP_KERNEL);
3408c2ecf20Sopenharmony_ci	if (!version)
3418c2ecf20Sopenharmony_ci		return -ENOMEM;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/* first select the interface */
3448c2ecf20Sopenharmony_ci	if (usb_set_interface(udev, 0, 1) != 0)
3458c2ecf20Sopenharmony_ci		err("could not set alternate setting to 0");
3468c2ecf20Sopenharmony_ci	else
3478c2ecf20Sopenharmony_ci		info("set alternate setting");
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	*cold = 0; /* by default do not download a firmware - just in case something is wrong */
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
3528c2ecf20Sopenharmony_ci		GET_VERSION_INFO_VENDOR_REQUEST,
3538c2ecf20Sopenharmony_ci		USB_TYPE_VENDOR | USB_DIR_IN,
3548c2ecf20Sopenharmony_ci		0, 0,
3558c2ecf20Sopenharmony_ci		version, 3, 500);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (ret < 0)
3588c2ecf20Sopenharmony_ci		*cold = 1;
3598c2ecf20Sopenharmony_ci	else {
3608c2ecf20Sopenharmony_ci		info("firmware version: %d.%d", version[1], version[2]);
3618c2ecf20Sopenharmony_ci		*cold = 0;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	kfree(version);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci/* power control */
3708c2ecf20Sopenharmony_cistatic int technisat_usb2_power_ctrl(struct dvb_usb_device *d, int level)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct technisat_usb2_state *state = d->priv;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	state->power_state = level;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	if (disable_led_control)
3778c2ecf20Sopenharmony_ci		return 0;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* green led is turned off in any case - will be turned on when tuning */
3808c2ecf20Sopenharmony_ci	technisat_usb2_set_led(d, 0, TECH_LED_OFF);
3818c2ecf20Sopenharmony_ci	/* red led is turned on all the time */
3828c2ecf20Sopenharmony_ci	technisat_usb2_set_led(d, 1, TECH_LED_ON);
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/* mac address reading - from the eeprom */
3878c2ecf20Sopenharmony_ci#if 0
3888c2ecf20Sopenharmony_cistatic void technisat_usb2_eeprom_dump(struct dvb_usb_device *d)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	u8 reg;
3918c2ecf20Sopenharmony_ci	u8 b[16];
3928c2ecf20Sopenharmony_ci	int i, j;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	/* full EEPROM dump */
3958c2ecf20Sopenharmony_ci	for (j = 0; j < 256 * 4; j += 16) {
3968c2ecf20Sopenharmony_ci		reg = j;
3978c2ecf20Sopenharmony_ci		if (technisat_usb2_i2c_access(d->udev, 0x50 + j / 256, &reg, 1, b, 16) != 0)
3988c2ecf20Sopenharmony_ci			break;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		deb_eeprom("EEPROM: %01x%02x: ", j / 256, reg);
4018c2ecf20Sopenharmony_ci		for (i = 0; i < 16; i++)
4028c2ecf20Sopenharmony_ci			deb_eeprom("%02x ", b[i]);
4038c2ecf20Sopenharmony_ci		deb_eeprom("\n");
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci#endif
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic u8 technisat_usb2_calc_lrc(const u8 *b, u16 length)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u8 lrc = 0;
4118c2ecf20Sopenharmony_ci	while (--length)
4128c2ecf20Sopenharmony_ci		lrc ^= *b++;
4138c2ecf20Sopenharmony_ci	return lrc;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic int technisat_usb2_eeprom_lrc_read(struct dvb_usb_device *d,
4178c2ecf20Sopenharmony_ci	u16 offset, u8 *b, u16 length, u8 tries)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	u8 bo = offset & 0xff;
4208c2ecf20Sopenharmony_ci	struct i2c_msg msg[] = {
4218c2ecf20Sopenharmony_ci		{
4228c2ecf20Sopenharmony_ci			.addr = 0x50 | ((offset >> 8) & 0x3),
4238c2ecf20Sopenharmony_ci			.buf = &bo,
4248c2ecf20Sopenharmony_ci			.len = 1
4258c2ecf20Sopenharmony_ci		}, {
4268c2ecf20Sopenharmony_ci			.addr = 0x50 | ((offset >> 8) & 0x3),
4278c2ecf20Sopenharmony_ci			.flags	= I2C_M_RD,
4288c2ecf20Sopenharmony_ci			.buf = b,
4298c2ecf20Sopenharmony_ci			.len = length
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci	};
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	while (tries--) {
4348c2ecf20Sopenharmony_ci		int status;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		if (i2c_transfer(&d->i2c_adap, msg, 2) != 2)
4378c2ecf20Sopenharmony_ci			break;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		status =
4408c2ecf20Sopenharmony_ci			technisat_usb2_calc_lrc(b, length - 1) == b[length - 1];
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		if (status)
4438c2ecf20Sopenharmony_ci			return 0;
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	return -EREMOTEIO;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci#define EEPROM_MAC_START 0x3f8
4508c2ecf20Sopenharmony_ci#define EEPROM_MAC_TOTAL 8
4518c2ecf20Sopenharmony_cistatic int technisat_usb2_read_mac_address(struct dvb_usb_device *d,
4528c2ecf20Sopenharmony_ci		u8 mac[])
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	u8 buf[EEPROM_MAC_TOTAL];
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (technisat_usb2_eeprom_lrc_read(d, EEPROM_MAC_START,
4578c2ecf20Sopenharmony_ci				buf, EEPROM_MAC_TOTAL, 4) != 0)
4588c2ecf20Sopenharmony_ci		return -ENODEV;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	memcpy(mac, buf, 6);
4618c2ecf20Sopenharmony_ci	return 0;
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic struct stv090x_config technisat_usb2_stv090x_config;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/* frontend attach */
4678c2ecf20Sopenharmony_cistatic int technisat_usb2_set_voltage(struct dvb_frontend *fe,
4688c2ecf20Sopenharmony_ci				      enum fe_sec_voltage voltage)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	int i;
4718c2ecf20Sopenharmony_ci	u8 gpio[3] = { 0 }; /* 0 = 2, 1 = 3, 2 = 4 */
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	gpio[2] = 1; /* high - voltage ? */
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	switch (voltage) {
4768c2ecf20Sopenharmony_ci	case SEC_VOLTAGE_13:
4778c2ecf20Sopenharmony_ci		gpio[0] = 1;
4788c2ecf20Sopenharmony_ci		break;
4798c2ecf20Sopenharmony_ci	case SEC_VOLTAGE_18:
4808c2ecf20Sopenharmony_ci		gpio[0] = 1;
4818c2ecf20Sopenharmony_ci		gpio[1] = 1;
4828c2ecf20Sopenharmony_ci		break;
4838c2ecf20Sopenharmony_ci	default:
4848c2ecf20Sopenharmony_ci	case SEC_VOLTAGE_OFF:
4858c2ecf20Sopenharmony_ci		break;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++)
4898c2ecf20Sopenharmony_ci		if (technisat_usb2_stv090x_config.set_gpio(fe, i+2, 0,
4908c2ecf20Sopenharmony_ci							   gpio[i], 0) != 0)
4918c2ecf20Sopenharmony_ci			return -EREMOTEIO;
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic struct stv090x_config technisat_usb2_stv090x_config = {
4968c2ecf20Sopenharmony_ci	.device         = STV0903,
4978c2ecf20Sopenharmony_ci	.demod_mode     = STV090x_SINGLE,
4988c2ecf20Sopenharmony_ci	.clk_mode       = STV090x_CLK_EXT,
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	.xtal           = 8000000,
5018c2ecf20Sopenharmony_ci	.address        = 0x68,
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	.ts1_mode       = STV090x_TSMODE_DVBCI,
5048c2ecf20Sopenharmony_ci	.ts1_clk        = 13400000,
5058c2ecf20Sopenharmony_ci	.ts1_tei        = 1,
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	.repeater_level = STV090x_RPTLEVEL_64,
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	.tuner_bbgain   = 6,
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic struct stv6110x_config technisat_usb2_stv6110x_config = {
5138c2ecf20Sopenharmony_ci	.addr           = 0x60,
5148c2ecf20Sopenharmony_ci	.refclk         = 16000000,
5158c2ecf20Sopenharmony_ci	.clk_div        = 2,
5168c2ecf20Sopenharmony_ci};
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int technisat_usb2_frontend_attach(struct dvb_usb_adapter *a)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	struct usb_device *udev = a->dev->udev;
5218c2ecf20Sopenharmony_ci	int ret;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	a->fe_adap[0].fe = dvb_attach(stv090x_attach, &technisat_usb2_stv090x_config,
5248c2ecf20Sopenharmony_ci			&a->dev->i2c_adap, STV090x_DEMODULATOR_0);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	if (a->fe_adap[0].fe) {
5278c2ecf20Sopenharmony_ci		const struct stv6110x_devctl *ctl;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		ctl = dvb_attach(stv6110x_attach,
5308c2ecf20Sopenharmony_ci				a->fe_adap[0].fe,
5318c2ecf20Sopenharmony_ci				&technisat_usb2_stv6110x_config,
5328c2ecf20Sopenharmony_ci				&a->dev->i2c_adap);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		if (ctl) {
5358c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_init          = ctl->tuner_init;
5368c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_sleep         = ctl->tuner_sleep;
5378c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_set_mode      = ctl->tuner_set_mode;
5388c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_set_frequency = ctl->tuner_set_frequency;
5398c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_get_frequency = ctl->tuner_get_frequency;
5408c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_set_bandwidth = ctl->tuner_set_bandwidth;
5418c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_get_bandwidth = ctl->tuner_get_bandwidth;
5428c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_set_bbgain    = ctl->tuner_set_bbgain;
5438c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_get_bbgain    = ctl->tuner_get_bbgain;
5448c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_set_refclk    = ctl->tuner_set_refclk;
5458c2ecf20Sopenharmony_ci			technisat_usb2_stv090x_config.tuner_get_status    = ctl->tuner_get_status;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci			/* call the init function once to initialize
5488c2ecf20Sopenharmony_ci			   tuner's clock output divider and demod's
5498c2ecf20Sopenharmony_ci			   master clock */
5508c2ecf20Sopenharmony_ci			if (a->fe_adap[0].fe->ops.init)
5518c2ecf20Sopenharmony_ci				a->fe_adap[0].fe->ops.init(a->fe_adap[0].fe);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci			if (mutex_lock_interruptible(&a->dev->i2c_mutex) < 0)
5548c2ecf20Sopenharmony_ci				return -EAGAIN;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci			ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5578c2ecf20Sopenharmony_ci					SET_IFCLK_TO_EXTERNAL_TSCLK_VENDOR_REQUEST,
5588c2ecf20Sopenharmony_ci					USB_TYPE_VENDOR | USB_DIR_OUT,
5598c2ecf20Sopenharmony_ci					0, 0,
5608c2ecf20Sopenharmony_ci					NULL, 0, 500);
5618c2ecf20Sopenharmony_ci			mutex_unlock(&a->dev->i2c_mutex);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci			if (ret != 0)
5648c2ecf20Sopenharmony_ci				err("could not set IF_CLK to external");
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci			a->fe_adap[0].fe->ops.set_voltage = technisat_usb2_set_voltage;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci			/* if everything was successful assign a nice name to the frontend */
5698c2ecf20Sopenharmony_ci			strscpy(a->fe_adap[0].fe->ops.info.name,
5708c2ecf20Sopenharmony_ci				a->dev->desc->name,
5718c2ecf20Sopenharmony_ci				sizeof(a->fe_adap[0].fe->ops.info.name));
5728c2ecf20Sopenharmony_ci		} else {
5738c2ecf20Sopenharmony_ci			dvb_frontend_detach(a->fe_adap[0].fe);
5748c2ecf20Sopenharmony_ci			a->fe_adap[0].fe = NULL;
5758c2ecf20Sopenharmony_ci		}
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	technisat_usb2_set_led_timer(a->dev, 1, 1);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	return a->fe_adap[0].fe == NULL ? -ENODEV : 0;
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/* Remote control */
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci/* the device is giving providing raw IR-signals to the host mapping
5868c2ecf20Sopenharmony_ci * it only to one remote control is just the default implementation
5878c2ecf20Sopenharmony_ci */
5888c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TRANSITION_TIME_US 889
5898c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TIME_US (2 * NOMINAL_IR_BIT_TRANSITION_TIME_US)
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci#define FIRMWARE_CLOCK_TICK 83333
5928c2ecf20Sopenharmony_ci#define FIRMWARE_CLOCK_DIVISOR 256
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci#define IR_PERCENT_TOLERANCE 15
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TRANSITION_TICKS ((NOMINAL_IR_BIT_TRANSITION_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
5978c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICKS / FIRMWARE_CLOCK_DIVISOR)
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TIME_TICKS ((NOMINAL_IR_BIT_TIME_US * 1000 * 1000) / FIRMWARE_CLOCK_TICK)
6008c2ecf20Sopenharmony_ci#define NOMINAL_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICKS / FIRMWARE_CLOCK_DIVISOR)
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci#define MINIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT - ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
6038c2ecf20Sopenharmony_ci#define MAXIMUM_IR_BIT_TRANSITION_TICK_COUNT (NOMINAL_IR_BIT_TRANSITION_TICK_COUNT + ((NOMINAL_IR_BIT_TRANSITION_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci#define MINIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT - ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
6068c2ecf20Sopenharmony_ci#define MAXIMUM_IR_BIT_TIME_TICK_COUNT (NOMINAL_IR_BIT_TIME_TICK_COUNT + ((NOMINAL_IR_BIT_TIME_TICK_COUNT * IR_PERCENT_TOLERANCE) / 100))
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic int technisat_usb2_get_ir(struct dvb_usb_device *d)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	struct technisat_usb2_state *state = d->priv;
6118c2ecf20Sopenharmony_ci	struct ir_raw_event ev;
6128c2ecf20Sopenharmony_ci	u8 *buf = state->buf;
6138c2ecf20Sopenharmony_ci	int i, ret;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	buf[0] = GET_IR_DATA_VENDOR_REQUEST;
6168c2ecf20Sopenharmony_ci	buf[1] = 0x08;
6178c2ecf20Sopenharmony_ci	buf[2] = 0x8f;
6188c2ecf20Sopenharmony_ci	buf[3] = MINIMUM_IR_BIT_TRANSITION_TICK_COUNT;
6198c2ecf20Sopenharmony_ci	buf[4] = MAXIMUM_IR_BIT_TIME_TICK_COUNT;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
6228c2ecf20Sopenharmony_ci		return -EAGAIN;
6238c2ecf20Sopenharmony_ci	ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
6248c2ecf20Sopenharmony_ci			GET_IR_DATA_VENDOR_REQUEST,
6258c2ecf20Sopenharmony_ci			USB_TYPE_VENDOR | USB_DIR_OUT,
6268c2ecf20Sopenharmony_ci			0, 0,
6278c2ecf20Sopenharmony_ci			buf, 5, 500);
6288c2ecf20Sopenharmony_ci	if (ret < 0)
6298c2ecf20Sopenharmony_ci		goto unlock;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	buf[1] = 0;
6328c2ecf20Sopenharmony_ci	buf[2] = 0;
6338c2ecf20Sopenharmony_ci	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
6348c2ecf20Sopenharmony_ci			GET_IR_DATA_VENDOR_REQUEST,
6358c2ecf20Sopenharmony_ci			USB_TYPE_VENDOR | USB_DIR_IN,
6368c2ecf20Sopenharmony_ci			0x8080, 0,
6378c2ecf20Sopenharmony_ci			buf, 62, 500);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ciunlock:
6408c2ecf20Sopenharmony_ci	mutex_unlock(&d->i2c_mutex);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	if (ret < 0)
6438c2ecf20Sopenharmony_ci		return ret;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	if (ret == 1)
6468c2ecf20Sopenharmony_ci		return 0; /* no key pressed */
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/* decoding */
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci#if 0
6518c2ecf20Sopenharmony_ci	deb_rc("RC: %d ", ret);
6528c2ecf20Sopenharmony_ci	debug_dump(buf + 1, ret, deb_rc);
6538c2ecf20Sopenharmony_ci#endif
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	ev.pulse = 0;
6568c2ecf20Sopenharmony_ci	for (i = 1; i < ARRAY_SIZE(state->buf); i++) {
6578c2ecf20Sopenharmony_ci		if (buf[i] == 0xff) {
6588c2ecf20Sopenharmony_ci			ev.pulse = 0;
6598c2ecf20Sopenharmony_ci			ev.duration = 889 * 2;
6608c2ecf20Sopenharmony_ci			ir_raw_event_store(d->rc_dev, &ev);
6618c2ecf20Sopenharmony_ci			break;
6628c2ecf20Sopenharmony_ci		}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci		ev.pulse = !ev.pulse;
6658c2ecf20Sopenharmony_ci		ev.duration = (buf[i] * FIRMWARE_CLOCK_DIVISOR *
6668c2ecf20Sopenharmony_ci			       FIRMWARE_CLOCK_TICK) / (1000 * 1000);
6678c2ecf20Sopenharmony_ci		ir_raw_event_store(d->rc_dev, &ev);
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	ir_raw_event_handle(d->rc_dev);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	return 1;
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic int technisat_usb2_rc_query(struct dvb_usb_device *d)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	int ret = technisat_usb2_get_ir(d);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	if (ret < 0)
6808c2ecf20Sopenharmony_ci		return ret;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	if (ret == 0)
6838c2ecf20Sopenharmony_ci		return 0;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (!disable_led_control)
6868c2ecf20Sopenharmony_ci		technisat_usb2_set_led(d, 1, TECH_LED_BLINK);
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	return 0;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci/* DVB-USB and USB stuff follows */
6928c2ecf20Sopenharmony_cistatic struct usb_device_id technisat_usb2_id_table[] = {
6938c2ecf20Sopenharmony_ci	{ USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_DVB_S2) },
6948c2ecf20Sopenharmony_ci	{ 0 }		/* Terminating entry */
6958c2ecf20Sopenharmony_ci};
6968c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, technisat_usb2_id_table);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci/* device description */
6998c2ecf20Sopenharmony_cistatic struct dvb_usb_device_properties technisat_usb2_devices = {
7008c2ecf20Sopenharmony_ci	.caps              = DVB_USB_IS_AN_I2C_ADAPTER,
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	.usb_ctrl          = CYPRESS_FX2,
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	.identify_state    = technisat_usb2_identify_state,
7058c2ecf20Sopenharmony_ci	.firmware          = "dvb-usb-SkyStar_USB_HD_FW_v17_63.HEX.fw",
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	.size_of_priv      = sizeof(struct technisat_usb2_state),
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	.i2c_algo          = &technisat_usb2_i2c_algo,
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	.power_ctrl        = technisat_usb2_power_ctrl,
7128c2ecf20Sopenharmony_ci	.read_mac_address  = technisat_usb2_read_mac_address,
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	.num_adapters = 1,
7158c2ecf20Sopenharmony_ci	.adapter = {
7168c2ecf20Sopenharmony_ci		{
7178c2ecf20Sopenharmony_ci		.num_frontends = 1,
7188c2ecf20Sopenharmony_ci		.fe = {{
7198c2ecf20Sopenharmony_ci			.frontend_attach  = technisat_usb2_frontend_attach,
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci			.stream = {
7228c2ecf20Sopenharmony_ci				.type = USB_ISOC,
7238c2ecf20Sopenharmony_ci				.count = 4,
7248c2ecf20Sopenharmony_ci				.endpoint = 0x2,
7258c2ecf20Sopenharmony_ci				.u = {
7268c2ecf20Sopenharmony_ci					.isoc = {
7278c2ecf20Sopenharmony_ci						.framesperurb = 32,
7288c2ecf20Sopenharmony_ci						.framesize = 2048,
7298c2ecf20Sopenharmony_ci						.interval = 1,
7308c2ecf20Sopenharmony_ci					}
7318c2ecf20Sopenharmony_ci				}
7328c2ecf20Sopenharmony_ci			},
7338c2ecf20Sopenharmony_ci		}},
7348c2ecf20Sopenharmony_ci			.size_of_priv = 0,
7358c2ecf20Sopenharmony_ci		},
7368c2ecf20Sopenharmony_ci	},
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	.num_device_descs = 1,
7398c2ecf20Sopenharmony_ci	.devices = {
7408c2ecf20Sopenharmony_ci		{   "Technisat SkyStar USB HD (DVB-S/S2)",
7418c2ecf20Sopenharmony_ci			{ &technisat_usb2_id_table[0], NULL },
7428c2ecf20Sopenharmony_ci			{ NULL },
7438c2ecf20Sopenharmony_ci		},
7448c2ecf20Sopenharmony_ci	},
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	.rc.core = {
7478c2ecf20Sopenharmony_ci		.rc_interval = 100,
7488c2ecf20Sopenharmony_ci		.rc_codes    = RC_MAP_TECHNISAT_USB2,
7498c2ecf20Sopenharmony_ci		.module_name = "technisat-usb2",
7508c2ecf20Sopenharmony_ci		.rc_query    = technisat_usb2_rc_query,
7518c2ecf20Sopenharmony_ci		.allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER,
7528c2ecf20Sopenharmony_ci		.driver_type    = RC_DRIVER_IR_RAW,
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci};
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_cistatic int technisat_usb2_probe(struct usb_interface *intf,
7578c2ecf20Sopenharmony_ci		const struct usb_device_id *id)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	struct dvb_usb_device *dev;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	if (dvb_usb_device_init(intf, &technisat_usb2_devices, THIS_MODULE,
7628c2ecf20Sopenharmony_ci				&dev, adapter_nr) != 0)
7638c2ecf20Sopenharmony_ci		return -ENODEV;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	if (dev) {
7668c2ecf20Sopenharmony_ci		struct technisat_usb2_state *state = dev->priv;
7678c2ecf20Sopenharmony_ci		state->dev = dev;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci		if (!disable_led_control) {
7708c2ecf20Sopenharmony_ci			INIT_DELAYED_WORK(&state->green_led_work,
7718c2ecf20Sopenharmony_ci					technisat_usb2_green_led_control);
7728c2ecf20Sopenharmony_ci			schedule_delayed_work(&state->green_led_work,
7738c2ecf20Sopenharmony_ci					msecs_to_jiffies(500));
7748c2ecf20Sopenharmony_ci		}
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	return 0;
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_cistatic void technisat_usb2_disconnect(struct usb_interface *intf)
7818c2ecf20Sopenharmony_ci{
7828c2ecf20Sopenharmony_ci	struct dvb_usb_device *dev = usb_get_intfdata(intf);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	/* work and stuff was only created when the device is is hot-state */
7858c2ecf20Sopenharmony_ci	if (dev != NULL) {
7868c2ecf20Sopenharmony_ci		struct technisat_usb2_state *state = dev->priv;
7878c2ecf20Sopenharmony_ci		if (state != NULL)
7888c2ecf20Sopenharmony_ci			cancel_delayed_work_sync(&state->green_led_work);
7898c2ecf20Sopenharmony_ci	}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	dvb_usb_device_exit(intf);
7928c2ecf20Sopenharmony_ci}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_cistatic struct usb_driver technisat_usb2_driver = {
7958c2ecf20Sopenharmony_ci	.name       = "dvb_usb_technisat_usb2",
7968c2ecf20Sopenharmony_ci	.probe      = technisat_usb2_probe,
7978c2ecf20Sopenharmony_ci	.disconnect = technisat_usb2_disconnect,
7988c2ecf20Sopenharmony_ci	.id_table   = technisat_usb2_id_table,
7998c2ecf20Sopenharmony_ci};
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cimodule_usb_driver(technisat_usb2_driver);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Patrick Boettcher <pboettcher@kernellabs.com>");
8048c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for Technisat DVB-S/S2 USB 2.0 device");
8058c2ecf20Sopenharmony_ciMODULE_VERSION("1.0");
8068c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
807