18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* DVB USB compliant Linux driver for the Afatech 9005
38c2ecf20Sopenharmony_ci * USB1.1 DVB-T receiver.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Thanks to Afatech who kindly provided information.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include "af9005.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/* debug */
148c2ecf20Sopenharmony_ciint dvb_usb_af9005_debug;
158c2ecf20Sopenharmony_cimodule_param_named(debug, dvb_usb_af9005_debug, int, 0644);
168c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug,
178c2ecf20Sopenharmony_ci		 "set debugging level (1=info,xfer=2,rc=4,reg=8,i2c=16,fw=32 (or-able))."
188c2ecf20Sopenharmony_ci		 DVB_USB_DEBUG_STATUS);
198c2ecf20Sopenharmony_ci/* enable obnoxious led */
208c2ecf20Sopenharmony_cibool dvb_usb_af9005_led = true;
218c2ecf20Sopenharmony_cimodule_param_named(led, dvb_usb_af9005_led, bool, 0644);
228c2ecf20Sopenharmony_ciMODULE_PARM_DESC(led, "enable led (default: 1).");
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* eeprom dump */
258c2ecf20Sopenharmony_cistatic int dvb_usb_af9005_dump_eeprom;
268c2ecf20Sopenharmony_cimodule_param_named(dump_eeprom, dvb_usb_af9005_dump_eeprom, int, 0);
278c2ecf20Sopenharmony_ciMODULE_PARM_DESC(dump_eeprom, "dump contents of the eeprom.");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciDVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* remote control decoder */
328c2ecf20Sopenharmony_cistatic int (*rc_decode) (struct dvb_usb_device *d, u8 *data, int len,
338c2ecf20Sopenharmony_ci		u32 *event, int *state);
348c2ecf20Sopenharmony_cistatic void *rc_keys;
358c2ecf20Sopenharmony_cistatic int *rc_keys_size;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciu8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct af9005_device_state {
408c2ecf20Sopenharmony_ci	u8 sequence;
418c2ecf20Sopenharmony_ci	int led_state;
428c2ecf20Sopenharmony_ci	unsigned char data[256];
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
468c2ecf20Sopenharmony_ci			      int readwrite, int type, u8 * values, int len)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct af9005_device_state *st = d->priv;
498c2ecf20Sopenharmony_ci	u8 command, seq;
508c2ecf20Sopenharmony_ci	int i, ret;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (len < 1) {
538c2ecf20Sopenharmony_ci		err("generic read/write, less than 1 byte. Makes no sense.");
548c2ecf20Sopenharmony_ci		return -EINVAL;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci	if (len > 8) {
578c2ecf20Sopenharmony_ci		err("generic read/write, more than 8 bytes. Not supported.");
588c2ecf20Sopenharmony_ci		return -EINVAL;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	mutex_lock(&d->data_mutex);
628c2ecf20Sopenharmony_ci	st->data[0] = 14;		/* rest of buffer length low */
638c2ecf20Sopenharmony_ci	st->data[1] = 0;		/* rest of buffer length high */
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	st->data[2] = AF9005_REGISTER_RW;	/* register operation */
668c2ecf20Sopenharmony_ci	st->data[3] = 12;		/* rest of buffer length */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	st->data[4] = seq = st->sequence++;	/* sequence number */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	st->data[5] = (u8) (reg >> 8);	/* register address */
718c2ecf20Sopenharmony_ci	st->data[6] = (u8) (reg & 0xff);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (type == AF9005_OFDM_REG) {
748c2ecf20Sopenharmony_ci		command = AF9005_CMD_OFDM_REG;
758c2ecf20Sopenharmony_ci	} else {
768c2ecf20Sopenharmony_ci		command = AF9005_CMD_TUNER;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (len > 1)
808c2ecf20Sopenharmony_ci		command |=
818c2ecf20Sopenharmony_ci		    AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
828c2ecf20Sopenharmony_ci	command |= readwrite;
838c2ecf20Sopenharmony_ci	if (readwrite == AF9005_CMD_WRITE)
848c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++)
858c2ecf20Sopenharmony_ci			st->data[8 + i] = values[i];
868c2ecf20Sopenharmony_ci	else if (type == AF9005_TUNER_REG)
878c2ecf20Sopenharmony_ci		/* read command for tuner, the first byte contains the i2c address */
888c2ecf20Sopenharmony_ci		st->data[8] = values[0];
898c2ecf20Sopenharmony_ci	st->data[7] = command;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 17, 0);
928c2ecf20Sopenharmony_ci	if (ret)
938c2ecf20Sopenharmony_ci		goto ret;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* sanity check */
968c2ecf20Sopenharmony_ci	if (st->data[2] != AF9005_REGISTER_RW_ACK) {
978c2ecf20Sopenharmony_ci		err("generic read/write, wrong reply code.");
988c2ecf20Sopenharmony_ci		ret = -EIO;
998c2ecf20Sopenharmony_ci		goto ret;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci	if (st->data[3] != 0x0d) {
1028c2ecf20Sopenharmony_ci		err("generic read/write, wrong length in reply.");
1038c2ecf20Sopenharmony_ci		ret = -EIO;
1048c2ecf20Sopenharmony_ci		goto ret;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	if (st->data[4] != seq) {
1078c2ecf20Sopenharmony_ci		err("generic read/write, wrong sequence in reply.");
1088c2ecf20Sopenharmony_ci		ret = -EIO;
1098c2ecf20Sopenharmony_ci		goto ret;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci	/*
1128c2ecf20Sopenharmony_ci	 * In thesis, both input and output buffers should have
1138c2ecf20Sopenharmony_ci	 * identical values for st->data[5] to st->data[8].
1148c2ecf20Sopenharmony_ci	 * However, windows driver doesn't check these fields, in fact
1158c2ecf20Sopenharmony_ci	 * sometimes the register in the reply is different that what
1168c2ecf20Sopenharmony_ci	 * has been sent
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	if (st->data[16] != 0x01) {
1198c2ecf20Sopenharmony_ci		err("generic read/write wrong status code in reply.");
1208c2ecf20Sopenharmony_ci		ret = -EIO;
1218c2ecf20Sopenharmony_ci		goto ret;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (readwrite == AF9005_CMD_READ)
1258c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++)
1268c2ecf20Sopenharmony_ci			values[i] = st->data[8 + i];
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ciret:
1298c2ecf20Sopenharmony_ci	mutex_unlock(&d->data_mutex);
1308c2ecf20Sopenharmony_ci	return ret;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciint af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int ret;
1378c2ecf20Sopenharmony_ci	deb_reg("read register %x ", reg);
1388c2ecf20Sopenharmony_ci	ret = af9005_generic_read_write(d, reg,
1398c2ecf20Sopenharmony_ci					AF9005_CMD_READ, AF9005_OFDM_REG,
1408c2ecf20Sopenharmony_ci					value, 1);
1418c2ecf20Sopenharmony_ci	if (ret)
1428c2ecf20Sopenharmony_ci		deb_reg("failed\n");
1438c2ecf20Sopenharmony_ci	else
1448c2ecf20Sopenharmony_ci		deb_reg("value %x\n", *value);
1458c2ecf20Sopenharmony_ci	return ret;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ciint af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
1498c2ecf20Sopenharmony_ci			       u8 * values, int len)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	int ret;
1528c2ecf20Sopenharmony_ci	deb_reg("read %d registers %x ", len, reg);
1538c2ecf20Sopenharmony_ci	ret = af9005_generic_read_write(d, reg,
1548c2ecf20Sopenharmony_ci					AF9005_CMD_READ, AF9005_OFDM_REG,
1558c2ecf20Sopenharmony_ci					values, len);
1568c2ecf20Sopenharmony_ci	if (ret)
1578c2ecf20Sopenharmony_ci		deb_reg("failed\n");
1588c2ecf20Sopenharmony_ci	else
1598c2ecf20Sopenharmony_ci		debug_dump(values, len, deb_reg);
1608c2ecf20Sopenharmony_ci	return ret;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciint af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	int ret;
1668c2ecf20Sopenharmony_ci	u8 temp = value;
1678c2ecf20Sopenharmony_ci	deb_reg("write register %x value %x ", reg, value);
1688c2ecf20Sopenharmony_ci	ret = af9005_generic_read_write(d, reg,
1698c2ecf20Sopenharmony_ci					AF9005_CMD_WRITE, AF9005_OFDM_REG,
1708c2ecf20Sopenharmony_ci					&temp, 1);
1718c2ecf20Sopenharmony_ci	if (ret)
1728c2ecf20Sopenharmony_ci		deb_reg("failed\n");
1738c2ecf20Sopenharmony_ci	else
1748c2ecf20Sopenharmony_ci		deb_reg("ok\n");
1758c2ecf20Sopenharmony_ci	return ret;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ciint af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
1798c2ecf20Sopenharmony_ci				u8 * values, int len)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	int ret;
1828c2ecf20Sopenharmony_ci	deb_reg("write %d registers %x values ", len, reg);
1838c2ecf20Sopenharmony_ci	debug_dump(values, len, deb_reg);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	ret = af9005_generic_read_write(d, reg,
1868c2ecf20Sopenharmony_ci					AF9005_CMD_WRITE, AF9005_OFDM_REG,
1878c2ecf20Sopenharmony_ci					values, len);
1888c2ecf20Sopenharmony_ci	if (ret)
1898c2ecf20Sopenharmony_ci		deb_reg("failed\n");
1908c2ecf20Sopenharmony_ci	else
1918c2ecf20Sopenharmony_ci		deb_reg("ok\n");
1928c2ecf20Sopenharmony_ci	return ret;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciint af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
1968c2ecf20Sopenharmony_ci			      u8 len, u8 * value)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	u8 temp;
1998c2ecf20Sopenharmony_ci	int ret;
2008c2ecf20Sopenharmony_ci	deb_reg("read bits %x %x %x", reg, pos, len);
2018c2ecf20Sopenharmony_ci	ret = af9005_read_ofdm_register(d, reg, &temp);
2028c2ecf20Sopenharmony_ci	if (ret) {
2038c2ecf20Sopenharmony_ci		deb_reg(" failed\n");
2048c2ecf20Sopenharmony_ci		return ret;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	*value = (temp >> pos) & regmask[len - 1];
2078c2ecf20Sopenharmony_ci	deb_reg(" value %x\n", *value);
2088c2ecf20Sopenharmony_ci	return 0;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ciint af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
2138c2ecf20Sopenharmony_ci			       u8 len, u8 value)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	u8 temp, mask;
2168c2ecf20Sopenharmony_ci	int ret;
2178c2ecf20Sopenharmony_ci	deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
2188c2ecf20Sopenharmony_ci	if (pos == 0 && len == 8)
2198c2ecf20Sopenharmony_ci		return af9005_write_ofdm_register(d, reg, value);
2208c2ecf20Sopenharmony_ci	ret = af9005_read_ofdm_register(d, reg, &temp);
2218c2ecf20Sopenharmony_ci	if (ret)
2228c2ecf20Sopenharmony_ci		return ret;
2238c2ecf20Sopenharmony_ci	mask = regmask[len - 1] << pos;
2248c2ecf20Sopenharmony_ci	temp = (temp & ~mask) | ((value << pos) & mask);
2258c2ecf20Sopenharmony_ci	return af9005_write_ofdm_register(d, reg, temp);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
2308c2ecf20Sopenharmony_ci					   u16 reg, u8 * values, int len)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	return af9005_generic_read_write(d, reg,
2338c2ecf20Sopenharmony_ci					 AF9005_CMD_READ, AF9005_TUNER_REG,
2348c2ecf20Sopenharmony_ci					 values, len);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
2388c2ecf20Sopenharmony_ci					    u16 reg, u8 * values, int len)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	return af9005_generic_read_write(d, reg,
2418c2ecf20Sopenharmony_ci					 AF9005_CMD_WRITE,
2428c2ecf20Sopenharmony_ci					 AF9005_TUNER_REG, values, len);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ciint af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
2468c2ecf20Sopenharmony_ci				 u8 * values, int len)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	/* don't let the name of this function mislead you: it's just used
2498c2ecf20Sopenharmony_ci	   as an interface from the firmware to the i2c bus. The actual
2508c2ecf20Sopenharmony_ci	   i2c addresses are contained in the data */
2518c2ecf20Sopenharmony_ci	int ret, i, done = 0, fail = 0;
2528c2ecf20Sopenharmony_ci	u8 temp;
2538c2ecf20Sopenharmony_ci	ret = af9005_usb_write_tuner_registers(d, reg, values, len);
2548c2ecf20Sopenharmony_ci	if (ret)
2558c2ecf20Sopenharmony_ci		return ret;
2568c2ecf20Sopenharmony_ci	if (reg != 0xffff) {
2578c2ecf20Sopenharmony_ci		/* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
2588c2ecf20Sopenharmony_ci		for (i = 0; i < 200; i++) {
2598c2ecf20Sopenharmony_ci			ret =
2608c2ecf20Sopenharmony_ci			    af9005_read_ofdm_register(d,
2618c2ecf20Sopenharmony_ci						      xd_I2C_i2c_m_status_wdat_done,
2628c2ecf20Sopenharmony_ci						      &temp);
2638c2ecf20Sopenharmony_ci			if (ret)
2648c2ecf20Sopenharmony_ci				return ret;
2658c2ecf20Sopenharmony_ci			done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
2668c2ecf20Sopenharmony_ci				       << i2c_m_status_wdat_done_pos);
2678c2ecf20Sopenharmony_ci			if (done)
2688c2ecf20Sopenharmony_ci				break;
2698c2ecf20Sopenharmony_ci			fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
2708c2ecf20Sopenharmony_ci				       << i2c_m_status_wdat_fail_pos);
2718c2ecf20Sopenharmony_ci			if (fail)
2728c2ecf20Sopenharmony_ci				break;
2738c2ecf20Sopenharmony_ci			msleep(50);
2748c2ecf20Sopenharmony_ci		}
2758c2ecf20Sopenharmony_ci		if (i == 200)
2768c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
2778c2ecf20Sopenharmony_ci		if (fail) {
2788c2ecf20Sopenharmony_ci			/* clear write fail bit */
2798c2ecf20Sopenharmony_ci			af9005_write_register_bits(d,
2808c2ecf20Sopenharmony_ci						   xd_I2C_i2c_m_status_wdat_fail,
2818c2ecf20Sopenharmony_ci						   i2c_m_status_wdat_fail_pos,
2828c2ecf20Sopenharmony_ci						   i2c_m_status_wdat_fail_len,
2838c2ecf20Sopenharmony_ci						   1);
2848c2ecf20Sopenharmony_ci			return -EIO;
2858c2ecf20Sopenharmony_ci		}
2868c2ecf20Sopenharmony_ci		/* clear write done bit */
2878c2ecf20Sopenharmony_ci		ret =
2888c2ecf20Sopenharmony_ci		    af9005_write_register_bits(d,
2898c2ecf20Sopenharmony_ci					       xd_I2C_i2c_m_status_wdat_fail,
2908c2ecf20Sopenharmony_ci					       i2c_m_status_wdat_done_pos,
2918c2ecf20Sopenharmony_ci					       i2c_m_status_wdat_done_len, 1);
2928c2ecf20Sopenharmony_ci		if (ret)
2938c2ecf20Sopenharmony_ci			return ret;
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci	return 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciint af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
2998c2ecf20Sopenharmony_ci				u8 * values, int len)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	/* don't let the name of this function mislead you: it's just used
3028c2ecf20Sopenharmony_ci	   as an interface from the firmware to the i2c bus. The actual
3038c2ecf20Sopenharmony_ci	   i2c addresses are contained in the data */
3048c2ecf20Sopenharmony_ci	int ret, i;
3058c2ecf20Sopenharmony_ci	u8 temp, buf[2];
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	buf[0] = addr;		/* tuner i2c address */
3088c2ecf20Sopenharmony_ci	buf[1] = values[0];	/* tuner register */
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	values[0] = addr + 0x01;	/* i2c read address */
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
3138c2ecf20Sopenharmony_ci		/* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
3148c2ecf20Sopenharmony_ci		ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
3158c2ecf20Sopenharmony_ci		if (ret)
3168c2ecf20Sopenharmony_ci			return ret;
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* send read command to ofsm */
3208c2ecf20Sopenharmony_ci	ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
3218c2ecf20Sopenharmony_ci	if (ret)
3228c2ecf20Sopenharmony_ci		return ret;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* check if read done */
3258c2ecf20Sopenharmony_ci	for (i = 0; i < 200; i++) {
3268c2ecf20Sopenharmony_ci		ret = af9005_read_ofdm_register(d, 0xa408, &temp);
3278c2ecf20Sopenharmony_ci		if (ret)
3288c2ecf20Sopenharmony_ci			return ret;
3298c2ecf20Sopenharmony_ci		if (temp & 0x01)
3308c2ecf20Sopenharmony_ci			break;
3318c2ecf20Sopenharmony_ci		msleep(50);
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	if (i == 200)
3348c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* clear read done bit (by writing 1) */
3378c2ecf20Sopenharmony_ci	ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
3388c2ecf20Sopenharmony_ci	if (ret)
3398c2ecf20Sopenharmony_ci		return ret;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* get read data (available from 0xa400) */
3428c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
3438c2ecf20Sopenharmony_ci		ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
3448c2ecf20Sopenharmony_ci		if (ret)
3458c2ecf20Sopenharmony_ci			return ret;
3468c2ecf20Sopenharmony_ci		values[i] = temp;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci	return 0;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
3528c2ecf20Sopenharmony_ci			    u8 * data, int len)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	int ret, i;
3558c2ecf20Sopenharmony_ci	u8 buf[3];
3568c2ecf20Sopenharmony_ci	deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
3578c2ecf20Sopenharmony_ci		reg, len);
3588c2ecf20Sopenharmony_ci	debug_dump(data, len, deb_i2c);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
3618c2ecf20Sopenharmony_ci		buf[0] = i2caddr;
3628c2ecf20Sopenharmony_ci		buf[1] = reg + (u8) i;
3638c2ecf20Sopenharmony_ci		buf[2] = data[i];
3648c2ecf20Sopenharmony_ci		ret =
3658c2ecf20Sopenharmony_ci		    af9005_write_tuner_registers(d,
3668c2ecf20Sopenharmony_ci						 APO_REG_I2C_RW_SILICON_TUNER,
3678c2ecf20Sopenharmony_ci						 buf, 3);
3688c2ecf20Sopenharmony_ci		if (ret) {
3698c2ecf20Sopenharmony_ci			deb_i2c("i2c_write failed\n");
3708c2ecf20Sopenharmony_ci			return ret;
3718c2ecf20Sopenharmony_ci		}
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci	deb_i2c("i2c_write ok\n");
3748c2ecf20Sopenharmony_ci	return 0;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
3788c2ecf20Sopenharmony_ci			   u8 * data, int len)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	int ret, i;
3818c2ecf20Sopenharmony_ci	u8 temp;
3828c2ecf20Sopenharmony_ci	deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
3838c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
3848c2ecf20Sopenharmony_ci		temp = reg + i;
3858c2ecf20Sopenharmony_ci		ret =
3868c2ecf20Sopenharmony_ci		    af9005_read_tuner_registers(d,
3878c2ecf20Sopenharmony_ci						APO_REG_I2C_RW_SILICON_TUNER,
3888c2ecf20Sopenharmony_ci						i2caddr, &temp, 1);
3898c2ecf20Sopenharmony_ci		if (ret) {
3908c2ecf20Sopenharmony_ci			deb_i2c("i2c_read failed\n");
3918c2ecf20Sopenharmony_ci			return ret;
3928c2ecf20Sopenharmony_ci		}
3938c2ecf20Sopenharmony_ci		data[i] = temp;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci	deb_i2c("i2c data read: ");
3968c2ecf20Sopenharmony_ci	debug_dump(data, len, deb_i2c);
3978c2ecf20Sopenharmony_ci	return 0;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
4018c2ecf20Sopenharmony_ci			   int num)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	/* only implements what the mt2060 module does, don't know how
4048c2ecf20Sopenharmony_ci	   to make it really generic */
4058c2ecf20Sopenharmony_ci	struct dvb_usb_device *d = i2c_get_adapdata(adap);
4068c2ecf20Sopenharmony_ci	int ret;
4078c2ecf20Sopenharmony_ci	u8 reg, addr;
4088c2ecf20Sopenharmony_ci	u8 *value;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
4118c2ecf20Sopenharmony_ci		return -EAGAIN;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (num > 2)
4148c2ecf20Sopenharmony_ci		warn("more than 2 i2c messages at a time is not handled yet. TODO.");
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (num == 2) {
4178c2ecf20Sopenharmony_ci		/* reads a single register */
4188c2ecf20Sopenharmony_ci		reg = *msg[0].buf;
4198c2ecf20Sopenharmony_ci		addr = msg[0].addr;
4208c2ecf20Sopenharmony_ci		value = msg[1].buf;
4218c2ecf20Sopenharmony_ci		ret = af9005_i2c_read(d, addr, reg, value, 1);
4228c2ecf20Sopenharmony_ci		if (ret == 0)
4238c2ecf20Sopenharmony_ci			ret = 2;
4248c2ecf20Sopenharmony_ci	} else {
4258c2ecf20Sopenharmony_ci		if (msg[0].len < 2) {
4268c2ecf20Sopenharmony_ci			ret = -EOPNOTSUPP;
4278c2ecf20Sopenharmony_ci			goto unlock;
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		/* write one or more registers */
4308c2ecf20Sopenharmony_ci		reg = msg[0].buf[0];
4318c2ecf20Sopenharmony_ci		addr = msg[0].addr;
4328c2ecf20Sopenharmony_ci		value = &msg[0].buf[1];
4338c2ecf20Sopenharmony_ci		ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
4348c2ecf20Sopenharmony_ci		if (ret == 0)
4358c2ecf20Sopenharmony_ci			ret = 1;
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciunlock:
4398c2ecf20Sopenharmony_ci	mutex_unlock(&d->i2c_mutex);
4408c2ecf20Sopenharmony_ci	return ret;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic u32 af9005_i2c_func(struct i2c_adapter *adapter)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic struct i2c_algorithm af9005_i2c_algo = {
4498c2ecf20Sopenharmony_ci	.master_xfer = af9005_i2c_xfer,
4508c2ecf20Sopenharmony_ci	.functionality = af9005_i2c_func,
4518c2ecf20Sopenharmony_ci};
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ciint af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
4548c2ecf20Sopenharmony_ci			int wlen, u8 * rbuf, int rlen)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	struct af9005_device_state *st = d->priv;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	int ret, i, packet_len;
4598c2ecf20Sopenharmony_ci	u8 seq;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (wlen < 0) {
4628c2ecf20Sopenharmony_ci		err("send command, wlen less than 0 bytes. Makes no sense.");
4638c2ecf20Sopenharmony_ci		return -EINVAL;
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci	if (wlen > 54) {
4668c2ecf20Sopenharmony_ci		err("send command, wlen more than 54 bytes. Not supported.");
4678c2ecf20Sopenharmony_ci		return -EINVAL;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci	if (rlen > 54) {
4708c2ecf20Sopenharmony_ci		err("send command, rlen more than 54 bytes. Not supported.");
4718c2ecf20Sopenharmony_ci		return -EINVAL;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci	packet_len = wlen + 5;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	mutex_lock(&d->data_mutex);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	st->data[0] = (u8) (packet_len & 0xff);
4788c2ecf20Sopenharmony_ci	st->data[1] = (u8) ((packet_len & 0xff00) >> 8);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	st->data[2] = 0x26;		/* packet type */
4818c2ecf20Sopenharmony_ci	st->data[3] = wlen + 3;
4828c2ecf20Sopenharmony_ci	st->data[4] = seq = st->sequence++;
4838c2ecf20Sopenharmony_ci	st->data[5] = command;
4848c2ecf20Sopenharmony_ci	st->data[6] = wlen;
4858c2ecf20Sopenharmony_ci	for (i = 0; i < wlen; i++)
4868c2ecf20Sopenharmony_ci		st->data[7 + i] = wbuf[i];
4878c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(d, st->data, wlen + 7, st->data, rlen + 7, 0);
4888c2ecf20Sopenharmony_ci	if (st->data[2] != 0x27) {
4898c2ecf20Sopenharmony_ci		err("send command, wrong reply code.");
4908c2ecf20Sopenharmony_ci		ret = -EIO;
4918c2ecf20Sopenharmony_ci	} else if (st->data[4] != seq) {
4928c2ecf20Sopenharmony_ci		err("send command, wrong sequence in reply.");
4938c2ecf20Sopenharmony_ci		ret = -EIO;
4948c2ecf20Sopenharmony_ci	} else if (st->data[5] != 0x01) {
4958c2ecf20Sopenharmony_ci		err("send command, wrong status code in reply.");
4968c2ecf20Sopenharmony_ci		ret = -EIO;
4978c2ecf20Sopenharmony_ci	} else if (st->data[6] != rlen) {
4988c2ecf20Sopenharmony_ci		err("send command, invalid data length in reply.");
4998c2ecf20Sopenharmony_ci		ret = -EIO;
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci	if (!ret) {
5028c2ecf20Sopenharmony_ci		for (i = 0; i < rlen; i++)
5038c2ecf20Sopenharmony_ci			rbuf[i] = st->data[i + 7];
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	mutex_unlock(&d->data_mutex);
5078c2ecf20Sopenharmony_ci	return ret;
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ciint af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
5118c2ecf20Sopenharmony_ci		       int len)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct af9005_device_state *st = d->priv;
5148c2ecf20Sopenharmony_ci	u8 seq;
5158c2ecf20Sopenharmony_ci	int ret, i;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	mutex_lock(&d->data_mutex);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	memset(st->data, 0, sizeof(st->data));
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	st->data[0] = 14;		/* length of rest of packet low */
5228c2ecf20Sopenharmony_ci	st->data[1] = 0;		/* length of rest of packer high */
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	st->data[2] = 0x2a;		/* read/write eeprom */
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	st->data[3] = 12;		/* size */
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	st->data[4] = seq = st->sequence++;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	st->data[5] = 0;		/* read */
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	st->data[6] = len;
5338c2ecf20Sopenharmony_ci	st->data[7] = address;
5348c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(d, st->data, 16, st->data, 14, 0);
5358c2ecf20Sopenharmony_ci	if (st->data[2] != 0x2b) {
5368c2ecf20Sopenharmony_ci		err("Read eeprom, invalid reply code");
5378c2ecf20Sopenharmony_ci		ret = -EIO;
5388c2ecf20Sopenharmony_ci	} else if (st->data[3] != 10) {
5398c2ecf20Sopenharmony_ci		err("Read eeprom, invalid reply length");
5408c2ecf20Sopenharmony_ci		ret = -EIO;
5418c2ecf20Sopenharmony_ci	} else if (st->data[4] != seq) {
5428c2ecf20Sopenharmony_ci		err("Read eeprom, wrong sequence in reply ");
5438c2ecf20Sopenharmony_ci		ret = -EIO;
5448c2ecf20Sopenharmony_ci	} else if (st->data[5] != 1) {
5458c2ecf20Sopenharmony_ci		err("Read eeprom, wrong status in reply ");
5468c2ecf20Sopenharmony_ci		ret = -EIO;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (!ret) {
5508c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++)
5518c2ecf20Sopenharmony_ci			values[i] = st->data[6 + i];
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci	mutex_unlock(&d->data_mutex);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return ret;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic int af9005_boot_packet(struct usb_device *udev, int type, u8 *reply,
5598c2ecf20Sopenharmony_ci			      u8 *buf, int size)
5608c2ecf20Sopenharmony_ci{
5618c2ecf20Sopenharmony_ci	u16 checksum;
5628c2ecf20Sopenharmony_ci	int act_len = 0, i, ret;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	memset(buf, 0, size);
5658c2ecf20Sopenharmony_ci	buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
5668c2ecf20Sopenharmony_ci	buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
5678c2ecf20Sopenharmony_ci	switch (type) {
5688c2ecf20Sopenharmony_ci	case FW_CONFIG:
5698c2ecf20Sopenharmony_ci		buf[2] = 0x11;
5708c2ecf20Sopenharmony_ci		buf[3] = 0x04;
5718c2ecf20Sopenharmony_ci		buf[4] = 0x00;	/* sequence number, original driver doesn't increment it here */
5728c2ecf20Sopenharmony_ci		buf[5] = 0x03;
5738c2ecf20Sopenharmony_ci		checksum = buf[4] + buf[5];
5748c2ecf20Sopenharmony_ci		buf[6] = (u8) ((checksum >> 8) & 0xff);
5758c2ecf20Sopenharmony_ci		buf[7] = (u8) (checksum & 0xff);
5768c2ecf20Sopenharmony_ci		break;
5778c2ecf20Sopenharmony_ci	case FW_CONFIRM:
5788c2ecf20Sopenharmony_ci		buf[2] = 0x11;
5798c2ecf20Sopenharmony_ci		buf[3] = 0x04;
5808c2ecf20Sopenharmony_ci		buf[4] = 0x00;	/* sequence number, original driver doesn't increment it here */
5818c2ecf20Sopenharmony_ci		buf[5] = 0x01;
5828c2ecf20Sopenharmony_ci		checksum = buf[4] + buf[5];
5838c2ecf20Sopenharmony_ci		buf[6] = (u8) ((checksum >> 8) & 0xff);
5848c2ecf20Sopenharmony_ci		buf[7] = (u8) (checksum & 0xff);
5858c2ecf20Sopenharmony_ci		break;
5868c2ecf20Sopenharmony_ci	case FW_BOOT:
5878c2ecf20Sopenharmony_ci		buf[2] = 0x10;
5888c2ecf20Sopenharmony_ci		buf[3] = 0x08;
5898c2ecf20Sopenharmony_ci		buf[4] = 0x00;	/* sequence number, original driver doesn't increment it here */
5908c2ecf20Sopenharmony_ci		buf[5] = 0x97;
5918c2ecf20Sopenharmony_ci		buf[6] = 0xaa;
5928c2ecf20Sopenharmony_ci		buf[7] = 0x55;
5938c2ecf20Sopenharmony_ci		buf[8] = 0xa5;
5948c2ecf20Sopenharmony_ci		buf[9] = 0x5a;
5958c2ecf20Sopenharmony_ci		checksum = 0;
5968c2ecf20Sopenharmony_ci		for (i = 4; i <= 9; i++)
5978c2ecf20Sopenharmony_ci			checksum += buf[i];
5988c2ecf20Sopenharmony_ci		buf[10] = (u8) ((checksum >> 8) & 0xff);
5998c2ecf20Sopenharmony_ci		buf[11] = (u8) (checksum & 0xff);
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	default:
6028c2ecf20Sopenharmony_ci		err("boot packet invalid boot packet type");
6038c2ecf20Sopenharmony_ci		return -EINVAL;
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci	deb_fw(">>> ");
6068c2ecf20Sopenharmony_ci	debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	ret = usb_bulk_msg(udev,
6098c2ecf20Sopenharmony_ci			   usb_sndbulkpipe(udev, 0x02),
6108c2ecf20Sopenharmony_ci			   buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
6118c2ecf20Sopenharmony_ci	if (ret)
6128c2ecf20Sopenharmony_ci		err("boot packet bulk message failed: %d (%d/%d)", ret,
6138c2ecf20Sopenharmony_ci		    FW_BULKOUT_SIZE + 2, act_len);
6148c2ecf20Sopenharmony_ci	else
6158c2ecf20Sopenharmony_ci		ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
6168c2ecf20Sopenharmony_ci	if (ret)
6178c2ecf20Sopenharmony_ci		return ret;
6188c2ecf20Sopenharmony_ci	memset(buf, 0, 9);
6198c2ecf20Sopenharmony_ci	ret = usb_bulk_msg(udev,
6208c2ecf20Sopenharmony_ci			   usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
6218c2ecf20Sopenharmony_ci	if (ret) {
6228c2ecf20Sopenharmony_ci		err("boot packet recv bulk message failed: %d", ret);
6238c2ecf20Sopenharmony_ci		return ret;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci	deb_fw("<<< ");
6268c2ecf20Sopenharmony_ci	debug_dump(buf, act_len, deb_fw);
6278c2ecf20Sopenharmony_ci	checksum = 0;
6288c2ecf20Sopenharmony_ci	switch (type) {
6298c2ecf20Sopenharmony_ci	case FW_CONFIG:
6308c2ecf20Sopenharmony_ci		if (buf[2] != 0x11) {
6318c2ecf20Sopenharmony_ci			err("boot bad config header.");
6328c2ecf20Sopenharmony_ci			return -EIO;
6338c2ecf20Sopenharmony_ci		}
6348c2ecf20Sopenharmony_ci		if (buf[3] != 0x05) {
6358c2ecf20Sopenharmony_ci			err("boot bad config size.");
6368c2ecf20Sopenharmony_ci			return -EIO;
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci		if (buf[4] != 0x00) {
6398c2ecf20Sopenharmony_ci			err("boot bad config sequence.");
6408c2ecf20Sopenharmony_ci			return -EIO;
6418c2ecf20Sopenharmony_ci		}
6428c2ecf20Sopenharmony_ci		if (buf[5] != 0x04) {
6438c2ecf20Sopenharmony_ci			err("boot bad config subtype.");
6448c2ecf20Sopenharmony_ci			return -EIO;
6458c2ecf20Sopenharmony_ci		}
6468c2ecf20Sopenharmony_ci		for (i = 4; i <= 6; i++)
6478c2ecf20Sopenharmony_ci			checksum += buf[i];
6488c2ecf20Sopenharmony_ci		if (buf[7] * 256 + buf[8] != checksum) {
6498c2ecf20Sopenharmony_ci			err("boot bad config checksum.");
6508c2ecf20Sopenharmony_ci			return -EIO;
6518c2ecf20Sopenharmony_ci		}
6528c2ecf20Sopenharmony_ci		*reply = buf[6];
6538c2ecf20Sopenharmony_ci		break;
6548c2ecf20Sopenharmony_ci	case FW_CONFIRM:
6558c2ecf20Sopenharmony_ci		if (buf[2] != 0x11) {
6568c2ecf20Sopenharmony_ci			err("boot bad confirm header.");
6578c2ecf20Sopenharmony_ci			return -EIO;
6588c2ecf20Sopenharmony_ci		}
6598c2ecf20Sopenharmony_ci		if (buf[3] != 0x05) {
6608c2ecf20Sopenharmony_ci			err("boot bad confirm size.");
6618c2ecf20Sopenharmony_ci			return -EIO;
6628c2ecf20Sopenharmony_ci		}
6638c2ecf20Sopenharmony_ci		if (buf[4] != 0x00) {
6648c2ecf20Sopenharmony_ci			err("boot bad confirm sequence.");
6658c2ecf20Sopenharmony_ci			return -EIO;
6668c2ecf20Sopenharmony_ci		}
6678c2ecf20Sopenharmony_ci		if (buf[5] != 0x02) {
6688c2ecf20Sopenharmony_ci			err("boot bad confirm subtype.");
6698c2ecf20Sopenharmony_ci			return -EIO;
6708c2ecf20Sopenharmony_ci		}
6718c2ecf20Sopenharmony_ci		for (i = 4; i <= 6; i++)
6728c2ecf20Sopenharmony_ci			checksum += buf[i];
6738c2ecf20Sopenharmony_ci		if (buf[7] * 256 + buf[8] != checksum) {
6748c2ecf20Sopenharmony_ci			err("boot bad confirm checksum.");
6758c2ecf20Sopenharmony_ci			return -EIO;
6768c2ecf20Sopenharmony_ci		}
6778c2ecf20Sopenharmony_ci		*reply = buf[6];
6788c2ecf20Sopenharmony_ci		break;
6798c2ecf20Sopenharmony_ci	case FW_BOOT:
6808c2ecf20Sopenharmony_ci		if (buf[2] != 0x10) {
6818c2ecf20Sopenharmony_ci			err("boot bad boot header.");
6828c2ecf20Sopenharmony_ci			return -EIO;
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci		if (buf[3] != 0x05) {
6858c2ecf20Sopenharmony_ci			err("boot bad boot size.");
6868c2ecf20Sopenharmony_ci			return -EIO;
6878c2ecf20Sopenharmony_ci		}
6888c2ecf20Sopenharmony_ci		if (buf[4] != 0x00) {
6898c2ecf20Sopenharmony_ci			err("boot bad boot sequence.");
6908c2ecf20Sopenharmony_ci			return -EIO;
6918c2ecf20Sopenharmony_ci		}
6928c2ecf20Sopenharmony_ci		if (buf[5] != 0x01) {
6938c2ecf20Sopenharmony_ci			err("boot bad boot pattern 01.");
6948c2ecf20Sopenharmony_ci			return -EIO;
6958c2ecf20Sopenharmony_ci		}
6968c2ecf20Sopenharmony_ci		if (buf[6] != 0x10) {
6978c2ecf20Sopenharmony_ci			err("boot bad boot pattern 10.");
6988c2ecf20Sopenharmony_ci			return -EIO;
6998c2ecf20Sopenharmony_ci		}
7008c2ecf20Sopenharmony_ci		for (i = 4; i <= 6; i++)
7018c2ecf20Sopenharmony_ci			checksum += buf[i];
7028c2ecf20Sopenharmony_ci		if (buf[7] * 256 + buf[8] != checksum) {
7038c2ecf20Sopenharmony_ci			err("boot bad boot checksum.");
7048c2ecf20Sopenharmony_ci			return -EIO;
7058c2ecf20Sopenharmony_ci		}
7068c2ecf20Sopenharmony_ci		break;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	return 0;
7118c2ecf20Sopenharmony_ci}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_cistatic int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	int i, packets, ret, act_len;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	u8 *buf;
7188c2ecf20Sopenharmony_ci	u8 reply;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL);
7218c2ecf20Sopenharmony_ci	if (!buf)
7228c2ecf20Sopenharmony_ci		return -ENOMEM;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf,
7258c2ecf20Sopenharmony_ci				 FW_BULKOUT_SIZE + 2);
7268c2ecf20Sopenharmony_ci	if (ret)
7278c2ecf20Sopenharmony_ci		goto err;
7288c2ecf20Sopenharmony_ci	if (reply != 0x01) {
7298c2ecf20Sopenharmony_ci		err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
7308c2ecf20Sopenharmony_ci		ret = -EIO;
7318c2ecf20Sopenharmony_ci		goto err;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci	packets = fw->size / FW_BULKOUT_SIZE;
7348c2ecf20Sopenharmony_ci	buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
7358c2ecf20Sopenharmony_ci	buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
7368c2ecf20Sopenharmony_ci	for (i = 0; i < packets; i++) {
7378c2ecf20Sopenharmony_ci		memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
7388c2ecf20Sopenharmony_ci		       FW_BULKOUT_SIZE);
7398c2ecf20Sopenharmony_ci		deb_fw(">>> ");
7408c2ecf20Sopenharmony_ci		debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
7418c2ecf20Sopenharmony_ci		ret = usb_bulk_msg(udev,
7428c2ecf20Sopenharmony_ci				   usb_sndbulkpipe(udev, 0x02),
7438c2ecf20Sopenharmony_ci				   buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
7448c2ecf20Sopenharmony_ci		if (ret) {
7458c2ecf20Sopenharmony_ci			err("firmware download failed at packet %d with code %d", i, ret);
7468c2ecf20Sopenharmony_ci			goto err;
7478c2ecf20Sopenharmony_ci		}
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci	ret = af9005_boot_packet(udev, FW_CONFIRM, &reply,
7508c2ecf20Sopenharmony_ci				 buf, FW_BULKOUT_SIZE + 2);
7518c2ecf20Sopenharmony_ci	if (ret)
7528c2ecf20Sopenharmony_ci		goto err;
7538c2ecf20Sopenharmony_ci	if (reply != (u8) (packets & 0xff)) {
7548c2ecf20Sopenharmony_ci		err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
7558c2ecf20Sopenharmony_ci		ret = -EIO;
7568c2ecf20Sopenharmony_ci		goto err;
7578c2ecf20Sopenharmony_ci	}
7588c2ecf20Sopenharmony_ci	ret = af9005_boot_packet(udev, FW_BOOT, &reply, buf,
7598c2ecf20Sopenharmony_ci				 FW_BULKOUT_SIZE + 2);
7608c2ecf20Sopenharmony_ci	if (ret)
7618c2ecf20Sopenharmony_ci		goto err;
7628c2ecf20Sopenharmony_ci	ret = af9005_boot_packet(udev, FW_CONFIG, &reply, buf,
7638c2ecf20Sopenharmony_ci				 FW_BULKOUT_SIZE + 2);
7648c2ecf20Sopenharmony_ci	if (ret)
7658c2ecf20Sopenharmony_ci		goto err;
7668c2ecf20Sopenharmony_ci	if (reply != 0x02) {
7678c2ecf20Sopenharmony_ci		err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
7688c2ecf20Sopenharmony_ci		ret = -EIO;
7698c2ecf20Sopenharmony_ci		goto err;
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cierr:
7738c2ecf20Sopenharmony_ci	kfree(buf);
7748c2ecf20Sopenharmony_ci	return ret;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ciint af9005_led_control(struct dvb_usb_device *d, int onoff)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	struct af9005_device_state *st = d->priv;
7818c2ecf20Sopenharmony_ci	int temp, ret;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (onoff && dvb_usb_af9005_led)
7848c2ecf20Sopenharmony_ci		temp = 1;
7858c2ecf20Sopenharmony_ci	else
7868c2ecf20Sopenharmony_ci		temp = 0;
7878c2ecf20Sopenharmony_ci	if (st->led_state != temp) {
7888c2ecf20Sopenharmony_ci		ret =
7898c2ecf20Sopenharmony_ci		    af9005_write_register_bits(d, xd_p_reg_top_locken1,
7908c2ecf20Sopenharmony_ci					       reg_top_locken1_pos,
7918c2ecf20Sopenharmony_ci					       reg_top_locken1_len, temp);
7928c2ecf20Sopenharmony_ci		if (ret)
7938c2ecf20Sopenharmony_ci			return ret;
7948c2ecf20Sopenharmony_ci		ret =
7958c2ecf20Sopenharmony_ci		    af9005_write_register_bits(d, xd_p_reg_top_lock1,
7968c2ecf20Sopenharmony_ci					       reg_top_lock1_pos,
7978c2ecf20Sopenharmony_ci					       reg_top_lock1_len, temp);
7988c2ecf20Sopenharmony_ci		if (ret)
7998c2ecf20Sopenharmony_ci			return ret;
8008c2ecf20Sopenharmony_ci		st->led_state = temp;
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci	return 0;
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cistatic int af9005_frontend_attach(struct dvb_usb_adapter *adap)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	u8 buf[8];
8088c2ecf20Sopenharmony_ci	int i;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/* without these calls the first commands after downloading
8118c2ecf20Sopenharmony_ci	   the firmware fail. I put these calls here to simulate
8128c2ecf20Sopenharmony_ci	   what it is done in dvb-usb-init.c.
8138c2ecf20Sopenharmony_ci	 */
8148c2ecf20Sopenharmony_ci	struct usb_device *udev = adap->dev->udev;
8158c2ecf20Sopenharmony_ci	usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
8168c2ecf20Sopenharmony_ci	usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
8178c2ecf20Sopenharmony_ci	if (dvb_usb_af9005_dump_eeprom) {
8188c2ecf20Sopenharmony_ci		printk("EEPROM DUMP\n");
8198c2ecf20Sopenharmony_ci		for (i = 0; i < 255; i += 8) {
8208c2ecf20Sopenharmony_ci			af9005_read_eeprom(adap->dev, i, buf, 8);
8218c2ecf20Sopenharmony_ci			debug_dump(buf, 8, printk);
8228c2ecf20Sopenharmony_ci		}
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci	adap->fe_adap[0].fe = af9005_fe_attach(adap->dev);
8258c2ecf20Sopenharmony_ci	return 0;
8268c2ecf20Sopenharmony_ci}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_cistatic int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
8298c2ecf20Sopenharmony_ci{
8308c2ecf20Sopenharmony_ci	struct af9005_device_state *st = d->priv;
8318c2ecf20Sopenharmony_ci	int ret, len;
8328c2ecf20Sopenharmony_ci	u8 seq;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	*state = REMOTE_NO_KEY_PRESSED;
8358c2ecf20Sopenharmony_ci	if (rc_decode == NULL) {
8368c2ecf20Sopenharmony_ci		/* it shouldn't never come here */
8378c2ecf20Sopenharmony_ci		return 0;
8388c2ecf20Sopenharmony_ci	}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	mutex_lock(&d->data_mutex);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	/* deb_info("rc_query\n"); */
8438c2ecf20Sopenharmony_ci	st->data[0] = 3;		/* rest of packet length low */
8448c2ecf20Sopenharmony_ci	st->data[1] = 0;		/* rest of packet length high */
8458c2ecf20Sopenharmony_ci	st->data[2] = 0x40;		/* read remote */
8468c2ecf20Sopenharmony_ci	st->data[3] = 1;		/* rest of packet length */
8478c2ecf20Sopenharmony_ci	st->data[4] = seq = st->sequence++;	/* sequence number */
8488c2ecf20Sopenharmony_ci	ret = dvb_usb_generic_rw(d, st->data, 5, st->data, 256, 0);
8498c2ecf20Sopenharmony_ci	if (ret) {
8508c2ecf20Sopenharmony_ci		err("rc query failed");
8518c2ecf20Sopenharmony_ci		goto ret;
8528c2ecf20Sopenharmony_ci	}
8538c2ecf20Sopenharmony_ci	if (st->data[2] != 0x41) {
8548c2ecf20Sopenharmony_ci		err("rc query bad header.");
8558c2ecf20Sopenharmony_ci		ret = -EIO;
8568c2ecf20Sopenharmony_ci		goto ret;
8578c2ecf20Sopenharmony_ci	} else if (st->data[4] != seq) {
8588c2ecf20Sopenharmony_ci		err("rc query bad sequence.");
8598c2ecf20Sopenharmony_ci		ret = -EIO;
8608c2ecf20Sopenharmony_ci		goto ret;
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci	len = st->data[5];
8638c2ecf20Sopenharmony_ci	if (len > 246) {
8648c2ecf20Sopenharmony_ci		err("rc query invalid length");
8658c2ecf20Sopenharmony_ci		ret = -EIO;
8668c2ecf20Sopenharmony_ci		goto ret;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci	if (len > 0) {
8698c2ecf20Sopenharmony_ci		deb_rc("rc data (%d) ", len);
8708c2ecf20Sopenharmony_ci		debug_dump((st->data + 6), len, deb_rc);
8718c2ecf20Sopenharmony_ci		ret = rc_decode(d, &st->data[6], len, event, state);
8728c2ecf20Sopenharmony_ci		if (ret) {
8738c2ecf20Sopenharmony_ci			err("rc_decode failed");
8748c2ecf20Sopenharmony_ci			goto ret;
8758c2ecf20Sopenharmony_ci		} else {
8768c2ecf20Sopenharmony_ci			deb_rc("rc_decode state %x event %x\n", *state, *event);
8778c2ecf20Sopenharmony_ci			if (*state == REMOTE_KEY_REPEAT)
8788c2ecf20Sopenharmony_ci				*event = d->last_event;
8798c2ecf20Sopenharmony_ci		}
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ciret:
8838c2ecf20Sopenharmony_ci	mutex_unlock(&d->data_mutex);
8848c2ecf20Sopenharmony_ci	return ret;
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
8888c2ecf20Sopenharmony_ci{
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return 0;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
8948c2ecf20Sopenharmony_ci{
8958c2ecf20Sopenharmony_ci	int ret;
8968c2ecf20Sopenharmony_ci	deb_info("pid filter control  onoff %d\n", onoff);
8978c2ecf20Sopenharmony_ci	if (onoff) {
8988c2ecf20Sopenharmony_ci		ret =
8998c2ecf20Sopenharmony_ci		    af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
9008c2ecf20Sopenharmony_ci		if (ret)
9018c2ecf20Sopenharmony_ci			return ret;
9028c2ecf20Sopenharmony_ci		ret =
9038c2ecf20Sopenharmony_ci		    af9005_write_register_bits(adap->dev,
9048c2ecf20Sopenharmony_ci					       XD_MP2IF_DMX_CTRL, 1, 1, 1);
9058c2ecf20Sopenharmony_ci		if (ret)
9068c2ecf20Sopenharmony_ci			return ret;
9078c2ecf20Sopenharmony_ci		ret =
9088c2ecf20Sopenharmony_ci		    af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
9098c2ecf20Sopenharmony_ci	} else
9108c2ecf20Sopenharmony_ci		ret =
9118c2ecf20Sopenharmony_ci		    af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
9128c2ecf20Sopenharmony_ci	if (ret)
9138c2ecf20Sopenharmony_ci		return ret;
9148c2ecf20Sopenharmony_ci	deb_info("pid filter control ok\n");
9158c2ecf20Sopenharmony_ci	return 0;
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
9198c2ecf20Sopenharmony_ci			     u16 pid, int onoff)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	u8 cmd = index & 0x1f;
9228c2ecf20Sopenharmony_ci	int ret;
9238c2ecf20Sopenharmony_ci	deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
9248c2ecf20Sopenharmony_ci		 pid, onoff);
9258c2ecf20Sopenharmony_ci	if (onoff) {
9268c2ecf20Sopenharmony_ci		/* cannot use it as pid_filter_ctrl since it has to be done
9278c2ecf20Sopenharmony_ci		   before setting the first pid */
9288c2ecf20Sopenharmony_ci		if (adap->feedcount == 1) {
9298c2ecf20Sopenharmony_ci			deb_info("first pid set, enable pid table\n");
9308c2ecf20Sopenharmony_ci			ret = af9005_pid_filter_control(adap, onoff);
9318c2ecf20Sopenharmony_ci			if (ret)
9328c2ecf20Sopenharmony_ci				return ret;
9338c2ecf20Sopenharmony_ci		}
9348c2ecf20Sopenharmony_ci		ret =
9358c2ecf20Sopenharmony_ci		    af9005_write_ofdm_register(adap->dev,
9368c2ecf20Sopenharmony_ci					       XD_MP2IF_PID_DATA_L,
9378c2ecf20Sopenharmony_ci					       (u8) (pid & 0xff));
9388c2ecf20Sopenharmony_ci		if (ret)
9398c2ecf20Sopenharmony_ci			return ret;
9408c2ecf20Sopenharmony_ci		ret =
9418c2ecf20Sopenharmony_ci		    af9005_write_ofdm_register(adap->dev,
9428c2ecf20Sopenharmony_ci					       XD_MP2IF_PID_DATA_H,
9438c2ecf20Sopenharmony_ci					       (u8) (pid >> 8));
9448c2ecf20Sopenharmony_ci		if (ret)
9458c2ecf20Sopenharmony_ci			return ret;
9468c2ecf20Sopenharmony_ci		cmd |= 0x20 | 0x40;
9478c2ecf20Sopenharmony_ci	} else {
9488c2ecf20Sopenharmony_ci		if (adap->feedcount == 0) {
9498c2ecf20Sopenharmony_ci			deb_info("last pid unset, disable pid table\n");
9508c2ecf20Sopenharmony_ci			ret = af9005_pid_filter_control(adap, onoff);
9518c2ecf20Sopenharmony_ci			if (ret)
9528c2ecf20Sopenharmony_ci				return ret;
9538c2ecf20Sopenharmony_ci		}
9548c2ecf20Sopenharmony_ci	}
9558c2ecf20Sopenharmony_ci	ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
9568c2ecf20Sopenharmony_ci	if (ret)
9578c2ecf20Sopenharmony_ci		return ret;
9588c2ecf20Sopenharmony_ci	deb_info("set pid ok\n");
9598c2ecf20Sopenharmony_ci	return 0;
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_cistatic int af9005_identify_state(struct usb_device *udev,
9638c2ecf20Sopenharmony_ci				 const struct dvb_usb_device_properties *props,
9648c2ecf20Sopenharmony_ci				 const struct dvb_usb_device_description **desc,
9658c2ecf20Sopenharmony_ci				 int *cold)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	int ret;
9688c2ecf20Sopenharmony_ci	u8 reply, *buf;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	buf = kmalloc(FW_BULKOUT_SIZE + 2, GFP_KERNEL);
9718c2ecf20Sopenharmony_ci	if (!buf)
9728c2ecf20Sopenharmony_ci		return -ENOMEM;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	ret = af9005_boot_packet(udev, FW_CONFIG, &reply,
9758c2ecf20Sopenharmony_ci				 buf, FW_BULKOUT_SIZE + 2);
9768c2ecf20Sopenharmony_ci	if (ret)
9778c2ecf20Sopenharmony_ci		goto err;
9788c2ecf20Sopenharmony_ci	deb_info("result of FW_CONFIG in identify state %d\n", reply);
9798c2ecf20Sopenharmony_ci	if (reply == 0x01)
9808c2ecf20Sopenharmony_ci		*cold = 1;
9818c2ecf20Sopenharmony_ci	else if (reply == 0x02)
9828c2ecf20Sopenharmony_ci		*cold = 0;
9838c2ecf20Sopenharmony_ci	else
9848c2ecf20Sopenharmony_ci		ret = -EIO;
9858c2ecf20Sopenharmony_ci	if (!ret)
9868c2ecf20Sopenharmony_ci		deb_info("Identify state cold = %d\n", *cold);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cierr:
9898c2ecf20Sopenharmony_ci	kfree(buf);
9908c2ecf20Sopenharmony_ci	return ret;
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_cistatic struct dvb_usb_device_properties af9005_properties;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_cistatic int af9005_usb_probe(struct usb_interface *intf,
9968c2ecf20Sopenharmony_ci			    const struct usb_device_id *id)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	return dvb_usb_device_init(intf, &af9005_properties,
9998c2ecf20Sopenharmony_ci				  THIS_MODULE, NULL, adapter_nr);
10008c2ecf20Sopenharmony_ci}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_cienum af9005_usb_table_entry {
10038c2ecf20Sopenharmony_ci	AFATECH_AF9005,
10048c2ecf20Sopenharmony_ci	TERRATEC_AF9005,
10058c2ecf20Sopenharmony_ci	ANSONIC_AF9005,
10068c2ecf20Sopenharmony_ci};
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_cistatic struct usb_device_id af9005_usb_table[] = {
10098c2ecf20Sopenharmony_ci	[AFATECH_AF9005] = {USB_DEVICE(USB_VID_AFATECH,
10108c2ecf20Sopenharmony_ci				USB_PID_AFATECH_AF9005)},
10118c2ecf20Sopenharmony_ci	[TERRATEC_AF9005] = {USB_DEVICE(USB_VID_TERRATEC,
10128c2ecf20Sopenharmony_ci				USB_PID_TERRATEC_CINERGY_T_USB_XE)},
10138c2ecf20Sopenharmony_ci	[ANSONIC_AF9005] = {USB_DEVICE(USB_VID_ANSONIC,
10148c2ecf20Sopenharmony_ci				USB_PID_ANSONIC_DVBT_USB)},
10158c2ecf20Sopenharmony_ci	{ }
10168c2ecf20Sopenharmony_ci};
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, af9005_usb_table);
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_cistatic struct dvb_usb_device_properties af9005_properties = {
10218c2ecf20Sopenharmony_ci	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	.usb_ctrl = DEVICE_SPECIFIC,
10248c2ecf20Sopenharmony_ci	.firmware = "af9005.fw",
10258c2ecf20Sopenharmony_ci	.download_firmware = af9005_download_firmware,
10268c2ecf20Sopenharmony_ci	.no_reconnect = 1,
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	.size_of_priv = sizeof(struct af9005_device_state),
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	.num_adapters = 1,
10318c2ecf20Sopenharmony_ci	.adapter = {
10328c2ecf20Sopenharmony_ci		    {
10338c2ecf20Sopenharmony_ci		    .num_frontends = 1,
10348c2ecf20Sopenharmony_ci		    .fe = {{
10358c2ecf20Sopenharmony_ci		     .caps =
10368c2ecf20Sopenharmony_ci		     DVB_USB_ADAP_HAS_PID_FILTER |
10378c2ecf20Sopenharmony_ci		     DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
10388c2ecf20Sopenharmony_ci		     .pid_filter_count = 32,
10398c2ecf20Sopenharmony_ci		     .pid_filter = af9005_pid_filter,
10408c2ecf20Sopenharmony_ci		     /* .pid_filter_ctrl = af9005_pid_filter_control, */
10418c2ecf20Sopenharmony_ci		     .frontend_attach = af9005_frontend_attach,
10428c2ecf20Sopenharmony_ci		     /* .tuner_attach     = af9005_tuner_attach, */
10438c2ecf20Sopenharmony_ci		     /* parameter for the MPEG2-data transfer */
10448c2ecf20Sopenharmony_ci		     .stream = {
10458c2ecf20Sopenharmony_ci				.type = USB_BULK,
10468c2ecf20Sopenharmony_ci				.count = 10,
10478c2ecf20Sopenharmony_ci				.endpoint = 0x04,
10488c2ecf20Sopenharmony_ci				.u = {
10498c2ecf20Sopenharmony_ci				      .bulk = {
10508c2ecf20Sopenharmony_ci					       .buffersize = 4096,	/* actual size seen is 3948 */
10518c2ecf20Sopenharmony_ci					       }
10528c2ecf20Sopenharmony_ci				      }
10538c2ecf20Sopenharmony_ci				},
10548c2ecf20Sopenharmony_ci		     }},
10558c2ecf20Sopenharmony_ci		     }
10568c2ecf20Sopenharmony_ci		    },
10578c2ecf20Sopenharmony_ci	.power_ctrl = af9005_power_ctrl,
10588c2ecf20Sopenharmony_ci	.identify_state = af9005_identify_state,
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	.i2c_algo = &af9005_i2c_algo,
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	.rc.legacy = {
10638c2ecf20Sopenharmony_ci		.rc_interval = 200,
10648c2ecf20Sopenharmony_ci		.rc_map_table = NULL,
10658c2ecf20Sopenharmony_ci		.rc_map_size = 0,
10668c2ecf20Sopenharmony_ci		.rc_query = af9005_rc_query,
10678c2ecf20Sopenharmony_ci	},
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	.generic_bulk_ctrl_endpoint          = 2,
10708c2ecf20Sopenharmony_ci	.generic_bulk_ctrl_endpoint_response = 1,
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	.num_device_descs = 3,
10738c2ecf20Sopenharmony_ci	.devices = {
10748c2ecf20Sopenharmony_ci		    {.name = "Afatech DVB-T USB1.1 stick",
10758c2ecf20Sopenharmony_ci		     .cold_ids = {&af9005_usb_table[AFATECH_AF9005], NULL},
10768c2ecf20Sopenharmony_ci		     .warm_ids = {NULL},
10778c2ecf20Sopenharmony_ci		     },
10788c2ecf20Sopenharmony_ci		    {.name = "TerraTec Cinergy T USB XE",
10798c2ecf20Sopenharmony_ci		     .cold_ids = {&af9005_usb_table[TERRATEC_AF9005], NULL},
10808c2ecf20Sopenharmony_ci		     .warm_ids = {NULL},
10818c2ecf20Sopenharmony_ci		     },
10828c2ecf20Sopenharmony_ci		    {.name = "Ansonic DVB-T USB1.1 stick",
10838c2ecf20Sopenharmony_ci		     .cold_ids = {&af9005_usb_table[ANSONIC_AF9005], NULL},
10848c2ecf20Sopenharmony_ci		     .warm_ids = {NULL},
10858c2ecf20Sopenharmony_ci		     },
10868c2ecf20Sopenharmony_ci		    {NULL},
10878c2ecf20Sopenharmony_ci		    }
10888c2ecf20Sopenharmony_ci};
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci/* usb specific object needed to register this driver with the usb subsystem */
10918c2ecf20Sopenharmony_cistatic struct usb_driver af9005_usb_driver = {
10928c2ecf20Sopenharmony_ci	.name = "dvb_usb_af9005",
10938c2ecf20Sopenharmony_ci	.probe = af9005_usb_probe,
10948c2ecf20Sopenharmony_ci	.disconnect = dvb_usb_device_exit,
10958c2ecf20Sopenharmony_ci	.id_table = af9005_usb_table,
10968c2ecf20Sopenharmony_ci};
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci/* module stuff */
10998c2ecf20Sopenharmony_cistatic int __init af9005_usb_module_init(void)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	int result;
11028c2ecf20Sopenharmony_ci	if ((result = usb_register(&af9005_usb_driver))) {
11038c2ecf20Sopenharmony_ci		err("usb_register failed. (%d)", result);
11048c2ecf20Sopenharmony_ci		return result;
11058c2ecf20Sopenharmony_ci	}
11068c2ecf20Sopenharmony_ci#if IS_MODULE(CONFIG_DVB_USB_AF9005) || defined(CONFIG_DVB_USB_AF9005_REMOTE)
11078c2ecf20Sopenharmony_ci	/* FIXME: convert to todays kernel IR infrastructure */
11088c2ecf20Sopenharmony_ci	rc_decode = symbol_request(af9005_rc_decode);
11098c2ecf20Sopenharmony_ci	rc_keys = symbol_request(rc_map_af9005_table);
11108c2ecf20Sopenharmony_ci	rc_keys_size = symbol_request(rc_map_af9005_table_size);
11118c2ecf20Sopenharmony_ci#endif
11128c2ecf20Sopenharmony_ci	if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
11138c2ecf20Sopenharmony_ci		err("af9005_rc_decode function not found, disabling remote");
11148c2ecf20Sopenharmony_ci		af9005_properties.rc.legacy.rc_query = NULL;
11158c2ecf20Sopenharmony_ci	} else {
11168c2ecf20Sopenharmony_ci		af9005_properties.rc.legacy.rc_map_table = rc_keys;
11178c2ecf20Sopenharmony_ci		af9005_properties.rc.legacy.rc_map_size = *rc_keys_size;
11188c2ecf20Sopenharmony_ci	}
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	return 0;
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_cistatic void __exit af9005_usb_module_exit(void)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci	/* release rc decode symbols */
11268c2ecf20Sopenharmony_ci	if (rc_decode != NULL)
11278c2ecf20Sopenharmony_ci		symbol_put(af9005_rc_decode);
11288c2ecf20Sopenharmony_ci	if (rc_keys != NULL)
11298c2ecf20Sopenharmony_ci		symbol_put(rc_map_af9005_table);
11308c2ecf20Sopenharmony_ci	if (rc_keys_size != NULL)
11318c2ecf20Sopenharmony_ci		symbol_put(rc_map_af9005_table_size);
11328c2ecf20Sopenharmony_ci	/* deregister this driver from the USB subsystem */
11338c2ecf20Sopenharmony_ci	usb_deregister(&af9005_usb_driver);
11348c2ecf20Sopenharmony_ci}
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_cimodule_init(af9005_usb_module_init);
11378c2ecf20Sopenharmony_cimodule_exit(af9005_usb_module_exit);
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
11408c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
11418c2ecf20Sopenharmony_ciMODULE_VERSION("1.0");
11428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1143