cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dib0700_core.c (24220B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/* Linux driver for devices based on the DiBcom DiB0700 USB bridge
      3 *
      4 *  Copyright (C) 2005-6 DiBcom, SA
      5 */
      6#include "dib0700.h"
      7
      8/* debug */
      9int dvb_usb_dib0700_debug;
     10module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
     11MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);
     12
     13static int nb_packet_buffer_size = 21;
     14module_param(nb_packet_buffer_size, int, 0644);
     15MODULE_PARM_DESC(nb_packet_buffer_size,
     16	"Set the dib0700 driver data buffer size. This parameter corresponds to the number of TS packets. The actual size of the data buffer corresponds to this parameter multiplied by 188 (default: 21)");
     17
     18DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
     19
     20
     21int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
     22			u32 *romversion, u32 *ramversion, u32 *fwtype)
     23{
     24	struct dib0700_state *st = d->priv;
     25	int ret;
     26
     27	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
     28		err("could not acquire lock");
     29		return -EINTR;
     30	}
     31
     32	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
     33				  REQUEST_GET_VERSION,
     34				  USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
     35				  st->buf, 16, USB_CTRL_GET_TIMEOUT);
     36	if (hwversion != NULL)
     37		*hwversion  = (st->buf[0] << 24)  | (st->buf[1] << 16)  |
     38			(st->buf[2] << 8)  | st->buf[3];
     39	if (romversion != NULL)
     40		*romversion = (st->buf[4] << 24)  | (st->buf[5] << 16)  |
     41			(st->buf[6] << 8)  | st->buf[7];
     42	if (ramversion != NULL)
     43		*ramversion = (st->buf[8] << 24)  | (st->buf[9] << 16)  |
     44			(st->buf[10] << 8) | st->buf[11];
     45	if (fwtype != NULL)
     46		*fwtype     = (st->buf[12] << 24) | (st->buf[13] << 16) |
     47			(st->buf[14] << 8) | st->buf[15];
     48	mutex_unlock(&d->usb_mutex);
     49	return ret;
     50}
     51
     52/* expecting rx buffer: request data[0] data[1] ... data[2] */
     53static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
     54{
     55	int status;
     56
     57	deb_data(">>> ");
     58	debug_dump(tx, txlen, deb_data);
     59
     60	status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
     61		tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
     62		USB_CTRL_GET_TIMEOUT);
     63
     64	if (status != txlen)
     65		deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
     66
     67	return status < 0 ? status : 0;
     68}
     69
     70/* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
     71int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
     72{
     73	u16 index, value;
     74	int status;
     75
     76	if (txlen < 2) {
     77		err("tx buffer length is smaller than 2. Makes no sense.");
     78		return -EINVAL;
     79	}
     80	if (txlen > 4) {
     81		err("tx buffer length is larger than 4. Not supported.");
     82		return -EINVAL;
     83	}
     84
     85	deb_data(">>> ");
     86	debug_dump(tx,txlen,deb_data);
     87
     88	value = ((txlen - 2) << 8) | tx[1];
     89	index = 0;
     90	if (txlen > 2)
     91		index |= (tx[2] << 8);
     92	if (txlen > 3)
     93		index |= tx[3];
     94
     95	status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
     96			USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
     97			USB_CTRL_GET_TIMEOUT);
     98
     99	if (status < 0)
    100		deb_info("ep 0 read error (status = %d)\n",status);
    101
    102	deb_data("<<< ");
    103	debug_dump(rx, rxlen, deb_data);
    104
    105	return status; /* length in case of success */
    106}
    107
    108int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
    109{
    110	struct dib0700_state *st = d->priv;
    111	int ret;
    112
    113	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    114		err("could not acquire lock");
    115		return -EINTR;
    116	}
    117
    118	st->buf[0] = REQUEST_SET_GPIO;
    119	st->buf[1] = gpio;
    120	st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6);
    121
    122	ret = dib0700_ctrl_wr(d, st->buf, 3);
    123
    124	mutex_unlock(&d->usb_mutex);
    125	return ret;
    126}
    127
    128static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets)
    129{
    130	struct dib0700_state *st = d->priv;
    131	int ret;
    132
    133	if (st->fw_version >= 0x10201) {
    134		if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    135			err("could not acquire lock");
    136			return -EINTR;
    137		}
    138
    139		st->buf[0] = REQUEST_SET_USB_XFER_LEN;
    140		st->buf[1] = (nb_ts_packets >> 8) & 0xff;
    141		st->buf[2] = nb_ts_packets & 0xff;
    142
    143		deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets);
    144
    145		ret = dib0700_ctrl_wr(d, st->buf, 3);
    146		mutex_unlock(&d->usb_mutex);
    147	} else {
    148		deb_info("this firmware does not allow to change the USB xfer len\n");
    149		ret = -EIO;
    150	}
    151
    152	return ret;
    153}
    154
    155/*
    156 * I2C master xfer function (supported in 1.20 firmware)
    157 */
    158static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
    159				int num)
    160{
    161	/* The new i2c firmware messages are more reliable and in particular
    162	   properly support i2c read calls not preceded by a write */
    163
    164	struct dvb_usb_device *d = i2c_get_adapdata(adap);
    165	struct dib0700_state *st = d->priv;
    166	uint8_t bus_mode = 1;  /* 0=eeprom bus, 1=frontend bus */
    167	uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
    168	uint8_t en_start = 0;
    169	uint8_t en_stop = 0;
    170	int result, i;
    171
    172	/* Ensure nobody else hits the i2c bus while we're sending our
    173	   sequence of messages, (such as the remote control thread) */
    174	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
    175		return -EINTR;
    176
    177	for (i = 0; i < num; i++) {
    178		if (i == 0) {
    179			/* First message in the transaction */
    180			en_start = 1;
    181		} else if (!(msg[i].flags & I2C_M_NOSTART)) {
    182			/* Device supports repeated-start */
    183			en_start = 1;
    184		} else {
    185			/* Not the first packet and device doesn't support
    186			   repeated start */
    187			en_start = 0;
    188		}
    189		if (i == (num - 1)) {
    190			/* Last message in the transaction */
    191			en_stop = 1;
    192		}
    193
    194		if (msg[i].flags & I2C_M_RD) {
    195			/* Read request */
    196			u16 index, value;
    197			uint8_t i2c_dest;
    198
    199			i2c_dest = (msg[i].addr << 1);
    200			value = ((en_start << 7) | (en_stop << 6) |
    201				 (msg[i].len & 0x3F)) << 8 | i2c_dest;
    202			/* I2C ctrl + FE bus; */
    203			index = ((gen_mode << 6) & 0xC0) |
    204				((bus_mode << 4) & 0x30);
    205
    206			result = usb_control_msg(d->udev,
    207						 usb_rcvctrlpipe(d->udev, 0),
    208						 REQUEST_NEW_I2C_READ,
    209						 USB_TYPE_VENDOR | USB_DIR_IN,
    210						 value, index, st->buf,
    211						 msg[i].len,
    212						 USB_CTRL_GET_TIMEOUT);
    213			if (result < 0) {
    214				deb_info("i2c read error (status = %d)\n", result);
    215				goto unlock;
    216			}
    217
    218			if (msg[i].len > sizeof(st->buf)) {
    219				deb_info("buffer too small to fit %d bytes\n",
    220					 msg[i].len);
    221				result = -EIO;
    222				goto unlock;
    223			}
    224
    225			memcpy(msg[i].buf, st->buf, msg[i].len);
    226
    227			deb_data("<<< ");
    228			debug_dump(msg[i].buf, msg[i].len, deb_data);
    229
    230		} else {
    231			/* Write request */
    232			if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    233				err("could not acquire lock");
    234				result = -EINTR;
    235				goto unlock;
    236			}
    237			st->buf[0] = REQUEST_NEW_I2C_WRITE;
    238			st->buf[1] = msg[i].addr << 1;
    239			st->buf[2] = (en_start << 7) | (en_stop << 6) |
    240				(msg[i].len & 0x3F);
    241			/* I2C ctrl + FE bus; */
    242			st->buf[3] = ((gen_mode << 6) & 0xC0) |
    243				 ((bus_mode << 4) & 0x30);
    244
    245			if (msg[i].len > sizeof(st->buf) - 4) {
    246				deb_info("i2c message to big: %d\n",
    247					 msg[i].len);
    248				mutex_unlock(&d->usb_mutex);
    249				result = -EIO;
    250				goto unlock;
    251			}
    252
    253			/* The Actual i2c payload */
    254			memcpy(&st->buf[4], msg[i].buf, msg[i].len);
    255
    256			deb_data(">>> ");
    257			debug_dump(st->buf, msg[i].len + 4, deb_data);
    258
    259			result = usb_control_msg(d->udev,
    260						 usb_sndctrlpipe(d->udev, 0),
    261						 REQUEST_NEW_I2C_WRITE,
    262						 USB_TYPE_VENDOR | USB_DIR_OUT,
    263						 0, 0, st->buf, msg[i].len + 4,
    264						 USB_CTRL_GET_TIMEOUT);
    265			mutex_unlock(&d->usb_mutex);
    266			if (result < 0) {
    267				deb_info("i2c write error (status = %d)\n", result);
    268				break;
    269			}
    270		}
    271	}
    272	result = i;
    273
    274unlock:
    275	mutex_unlock(&d->i2c_mutex);
    276	return result;
    277}
    278
    279/*
    280 * I2C master xfer function (pre-1.20 firmware)
    281 */
    282static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
    283				   struct i2c_msg *msg, int num)
    284{
    285	struct dvb_usb_device *d = i2c_get_adapdata(adap);
    286	struct dib0700_state *st = d->priv;
    287	int i, len, result;
    288
    289	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
    290		return -EINTR;
    291	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    292		err("could not acquire lock");
    293		mutex_unlock(&d->i2c_mutex);
    294		return -EINTR;
    295	}
    296
    297	for (i = 0; i < num; i++) {
    298		/* fill in the address */
    299		st->buf[1] = msg[i].addr << 1;
    300		/* fill the buffer */
    301		if (msg[i].len > sizeof(st->buf) - 2) {
    302			deb_info("i2c xfer to big: %d\n",
    303				msg[i].len);
    304			result = -EIO;
    305			goto unlock;
    306		}
    307		memcpy(&st->buf[2], msg[i].buf, msg[i].len);
    308
    309		/* write/read request */
    310		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
    311			st->buf[0] = REQUEST_I2C_READ;
    312			st->buf[1] |= 1;
    313
    314			/* special thing in the current firmware: when length is zero the read-failed */
    315			len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
    316					      st->buf, msg[i + 1].len);
    317			if (len <= 0) {
    318				deb_info("I2C read failed on address 0x%02x\n",
    319						msg[i].addr);
    320				result = -EIO;
    321				goto unlock;
    322			}
    323
    324			if (msg[i + 1].len > sizeof(st->buf)) {
    325				deb_info("i2c xfer buffer to small for %d\n",
    326					msg[i].len);
    327				result = -EIO;
    328				goto unlock;
    329			}
    330			memcpy(msg[i + 1].buf, st->buf, msg[i + 1].len);
    331
    332			msg[i+1].len = len;
    333
    334			i++;
    335		} else {
    336			st->buf[0] = REQUEST_I2C_WRITE;
    337			result = dib0700_ctrl_wr(d, st->buf, msg[i].len + 2);
    338			if (result < 0)
    339				goto unlock;
    340		}
    341	}
    342	result = i;
    343unlock:
    344	mutex_unlock(&d->usb_mutex);
    345	mutex_unlock(&d->i2c_mutex);
    346
    347	return result;
    348}
    349
    350static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
    351			    int num)
    352{
    353	struct dvb_usb_device *d = i2c_get_adapdata(adap);
    354	struct dib0700_state *st = d->priv;
    355
    356	if (st->fw_use_new_i2c_api == 1) {
    357		/* User running at least fw 1.20 */
    358		return dib0700_i2c_xfer_new(adap, msg, num);
    359	} else {
    360		/* Use legacy calls */
    361		return dib0700_i2c_xfer_legacy(adap, msg, num);
    362	}
    363}
    364
    365static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
    366{
    367	return I2C_FUNC_I2C;
    368}
    369
    370struct i2c_algorithm dib0700_i2c_algo = {
    371	.master_xfer   = dib0700_i2c_xfer,
    372	.functionality = dib0700_i2c_func,
    373};
    374
    375int dib0700_identify_state(struct usb_device *udev,
    376			   const struct dvb_usb_device_properties *props,
    377			   const struct dvb_usb_device_description **desc,
    378			   int *cold)
    379{
    380	s16 ret;
    381	u8 *b;
    382
    383	b = kmalloc(16, GFP_KERNEL);
    384	if (!b)
    385		return	-ENOMEM;
    386
    387
    388	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
    389		REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
    390
    391	deb_info("FW GET_VERSION length: %d\n",ret);
    392
    393	*cold = ret <= 0;
    394	deb_info("cold: %d\n", *cold);
    395
    396	kfree(b);
    397	return 0;
    398}
    399
    400static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
    401	u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
    402	u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
    403{
    404	struct dib0700_state *st = d->priv;
    405	int ret;
    406
    407	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    408		err("could not acquire lock");
    409		return -EINTR;
    410	}
    411
    412	st->buf[0] = REQUEST_SET_CLOCK;
    413	st->buf[1] = (en_pll << 7) | (pll_src << 6) |
    414		(pll_range << 5) | (clock_gpio3 << 4);
    415	st->buf[2] = (pll_prediv >> 8)  & 0xff; /* MSB */
    416	st->buf[3] =  pll_prediv        & 0xff; /* LSB */
    417	st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
    418	st->buf[5] =  pll_loopdiv       & 0xff; /* LSB */
    419	st->buf[6] = (free_div >> 8)    & 0xff; /* MSB */
    420	st->buf[7] =  free_div          & 0xff; /* LSB */
    421	st->buf[8] = (dsuScaler >> 8)   & 0xff; /* MSB */
    422	st->buf[9] =  dsuScaler         & 0xff; /* LSB */
    423
    424	ret = dib0700_ctrl_wr(d, st->buf, 10);
    425	mutex_unlock(&d->usb_mutex);
    426
    427	return ret;
    428}
    429
    430int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
    431{
    432	struct dib0700_state *st = d->priv;
    433	u16 divider;
    434	int ret;
    435
    436	if (scl_kHz == 0)
    437		return -EINVAL;
    438
    439	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    440		err("could not acquire lock");
    441		return -EINTR;
    442	}
    443
    444	st->buf[0] = REQUEST_SET_I2C_PARAM;
    445	divider = (u16) (30000 / scl_kHz);
    446	st->buf[1] = 0;
    447	st->buf[2] = (u8) (divider >> 8);
    448	st->buf[3] = (u8) (divider & 0xff);
    449	divider = (u16) (72000 / scl_kHz);
    450	st->buf[4] = (u8) (divider >> 8);
    451	st->buf[5] = (u8) (divider & 0xff);
    452	divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
    453	st->buf[6] = (u8) (divider >> 8);
    454	st->buf[7] = (u8) (divider & 0xff);
    455
    456	deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
    457		(st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
    458		st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
    459
    460	ret = dib0700_ctrl_wr(d, st->buf, 8);
    461	mutex_unlock(&d->usb_mutex);
    462
    463	return ret;
    464}
    465
    466
    467int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
    468{
    469	switch (clk_MHz) {
    470		case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
    471		default: return -EINVAL;
    472	}
    473	return 0;
    474}
    475
    476static int dib0700_jumpram(struct usb_device *udev, u32 address)
    477{
    478	int ret = 0, actlen;
    479	u8 *buf;
    480
    481	buf = kmalloc(8, GFP_KERNEL);
    482	if (!buf)
    483		return -ENOMEM;
    484	buf[0] = REQUEST_JUMPRAM;
    485	buf[1] = 0;
    486	buf[2] = 0;
    487	buf[3] = 0;
    488	buf[4] = (address >> 24) & 0xff;
    489	buf[5] = (address >> 16) & 0xff;
    490	buf[6] = (address >> 8)  & 0xff;
    491	buf[7] =  address        & 0xff;
    492
    493	if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
    494		deb_fw("jumpram to 0x%x failed\n",address);
    495		goto out;
    496	}
    497	if (actlen != 8) {
    498		deb_fw("jumpram to 0x%x failed\n",address);
    499		ret = -EIO;
    500		goto out;
    501	}
    502out:
    503	kfree(buf);
    504	return ret;
    505}
    506
    507int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
    508{
    509	struct hexline hx;
    510	int pos = 0, ret, act_len, i, adap_num;
    511	u8 *buf;
    512	u32 fw_version;
    513
    514	buf = kmalloc(260, GFP_KERNEL);
    515	if (!buf)
    516		return -ENOMEM;
    517
    518	while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
    519		deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
    520				hx.addr, hx.len, hx.chk);
    521
    522		buf[0] = hx.len;
    523		buf[1] = (hx.addr >> 8) & 0xff;
    524		buf[2] =  hx.addr       & 0xff;
    525		buf[3] = hx.type;
    526		memcpy(&buf[4],hx.data,hx.len);
    527		buf[4+hx.len] = hx.chk;
    528
    529		ret = usb_bulk_msg(udev,
    530			usb_sndbulkpipe(udev, 0x01),
    531			buf,
    532			hx.len + 5,
    533			&act_len,
    534			1000);
    535
    536		if (ret < 0) {
    537			err("firmware download failed at %d with %d",pos,ret);
    538			goto out;
    539		}
    540	}
    541
    542	if (ret == 0) {
    543		/* start the firmware */
    544		if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
    545			info("firmware started successfully.");
    546			msleep(500);
    547		}
    548	} else
    549		ret = -EIO;
    550
    551	/* the number of ts packet has to be at least 1 */
    552	if (nb_packet_buffer_size < 1)
    553		nb_packet_buffer_size = 1;
    554
    555	/* get the firmware version */
    556	usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
    557				  REQUEST_GET_VERSION,
    558				  USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
    559				  buf, 16, USB_CTRL_GET_TIMEOUT);
    560	fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
    561
    562	/* set the buffer size - DVB-USB is allocating URB buffers
    563	 * only after the firwmare download was successful */
    564	for (i = 0; i < dib0700_device_count; i++) {
    565		for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
    566				adap_num++) {
    567			if (fw_version >= 0x10201) {
    568				dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
    569			} else {
    570				/* for fw version older than 1.20.1,
    571				 * the buffersize has to be n times 512 */
    572				dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
    573				if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512)
    574					dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512;
    575			}
    576		}
    577	}
    578out:
    579	kfree(buf);
    580	return ret;
    581}
    582
    583int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
    584{
    585	struct dib0700_state *st = adap->dev->priv;
    586	int ret, adapt_nr;
    587
    588	if ((onoff != 0) && (st->fw_version >= 0x10201)) {
    589		/* for firmware later than 1.20.1,
    590		 * the USB xfer length can be set  */
    591		ret = dib0700_set_usb_xfer_len(adap->dev,
    592			st->nb_packet_buffer_size);
    593		if (ret < 0) {
    594			deb_info("can not set the USB xfer len\n");
    595			return ret;
    596		}
    597	}
    598
    599	mutex_lock(&adap->dev->usb_mutex);
    600
    601	st->buf[0] = REQUEST_ENABLE_VIDEO;
    602	/* this bit gives a kind of command,
    603	 * rather than enabling something or not */
    604	st->buf[1] = (onoff << 4) | 0x00;
    605
    606	if (st->disable_streaming_master_mode == 1)
    607		st->buf[2] = 0x00;
    608	else
    609		st->buf[2] = 0x01 << 4; /* Master mode */
    610
    611	st->buf[3] = 0x00;
    612
    613	if ((adap->fe_adap[0].stream.props.endpoint != 2)
    614	    && (adap->fe_adap[0].stream.props.endpoint != 3)) {
    615		deb_info("the endpoint number (%i) is not correct, use the adapter id instead\n",
    616			 adap->fe_adap[0].stream.props.endpoint);
    617		adapt_nr = adap->id;
    618	} else {
    619		adapt_nr = adap->fe_adap[0].stream.props.endpoint - 2;
    620	}
    621
    622	if (onoff)
    623		st->channel_state |= 1 << adapt_nr;
    624	else
    625		st->channel_state &= ~(1 << adapt_nr);
    626
    627	st->buf[2] |= st->channel_state;
    628
    629	deb_info("adapter %d, streaming %s: %*ph\n",
    630		adapt_nr, onoff ? "ON" : "OFF", 3, st->buf);
    631
    632	ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
    633	mutex_unlock(&adap->dev->usb_mutex);
    634
    635	return ret;
    636}
    637
    638int dib0700_change_protocol(struct rc_dev *rc, u64 *rc_proto)
    639{
    640	struct dvb_usb_device *d = rc->priv;
    641	struct dib0700_state *st = d->priv;
    642	int new_proto, ret;
    643
    644	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
    645		err("could not acquire lock");
    646		return -EINTR;
    647	}
    648
    649	st->buf[0] = REQUEST_SET_RC;
    650	st->buf[1] = 0;
    651	st->buf[2] = 0;
    652
    653	/* Set the IR mode */
    654	if (*rc_proto & RC_PROTO_BIT_RC5) {
    655		new_proto = 1;
    656		*rc_proto = RC_PROTO_BIT_RC5;
    657	} else if (*rc_proto & RC_PROTO_BIT_NEC) {
    658		new_proto = 0;
    659		*rc_proto = RC_PROTO_BIT_NEC;
    660	} else if (*rc_proto & RC_PROTO_BIT_RC6_MCE) {
    661		if (st->fw_version < 0x10200) {
    662			ret = -EINVAL;
    663			goto out;
    664		}
    665		new_proto = 2;
    666		*rc_proto = RC_PROTO_BIT_RC6_MCE;
    667	} else {
    668		ret = -EINVAL;
    669		goto out;
    670	}
    671
    672	st->buf[1] = new_proto;
    673
    674	ret = dib0700_ctrl_wr(d, st->buf, 3);
    675	if (ret < 0) {
    676		err("ir protocol setup failed");
    677		goto out;
    678	}
    679
    680	d->props.rc.core.protocol = *rc_proto;
    681
    682out:
    683	mutex_unlock(&d->usb_mutex);
    684	return ret;
    685}
    686
    687/* This is the structure of the RC response packet starting in firmware 1.20 */
    688struct dib0700_rc_response {
    689	u8 report_id;
    690	u8 data_state;
    691	union {
    692		struct {
    693			u8 system;
    694			u8 not_system;
    695			u8 data;
    696			u8 not_data;
    697		} nec;
    698		struct {
    699			u8 not_used;
    700			u8 system;
    701			u8 data;
    702			u8 not_data;
    703		} rc5;
    704	};
    705};
    706#define RC_MSG_SIZE_V1_20 6
    707
    708static void dib0700_rc_urb_completion(struct urb *purb)
    709{
    710	struct dvb_usb_device *d = purb->context;
    711	struct dib0700_rc_response *poll_reply;
    712	enum rc_proto protocol;
    713	u32 keycode;
    714	u8 toggle;
    715
    716	deb_info("%s()\n", __func__);
    717	if (d->rc_dev == NULL) {
    718		/* This will occur if disable_rc_polling=1 */
    719		kfree(purb->transfer_buffer);
    720		usb_free_urb(purb);
    721		return;
    722	}
    723
    724	poll_reply = purb->transfer_buffer;
    725
    726	if (purb->status < 0) {
    727		deb_info("discontinuing polling\n");
    728		kfree(purb->transfer_buffer);
    729		usb_free_urb(purb);
    730		return;
    731	}
    732
    733	if (purb->actual_length != RC_MSG_SIZE_V1_20) {
    734		deb_info("malformed rc msg size=%d\n", purb->actual_length);
    735		goto resubmit;
    736	}
    737
    738	deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
    739		 poll_reply->report_id, poll_reply->data_state,
    740		 poll_reply->nec.system, poll_reply->nec.not_system,
    741		 poll_reply->nec.data, poll_reply->nec.not_data,
    742		 purb->actual_length);
    743
    744	switch (d->props.rc.core.protocol) {
    745	case RC_PROTO_BIT_NEC:
    746		toggle = 0;
    747
    748		/* NEC protocol sends repeat code as 0 0 0 FF */
    749		if (poll_reply->nec.system     == 0x00 &&
    750		    poll_reply->nec.not_system == 0x00 &&
    751		    poll_reply->nec.data       == 0x00 &&
    752		    poll_reply->nec.not_data   == 0xff) {
    753			poll_reply->data_state = 2;
    754			rc_repeat(d->rc_dev);
    755			goto resubmit;
    756		}
    757
    758		if ((poll_reply->nec.data ^ poll_reply->nec.not_data) != 0xff) {
    759			deb_data("NEC32 protocol\n");
    760			keycode = RC_SCANCODE_NEC32(poll_reply->nec.system     << 24 |
    761						     poll_reply->nec.not_system << 16 |
    762						     poll_reply->nec.data       << 8  |
    763						     poll_reply->nec.not_data);
    764			protocol = RC_PROTO_NEC32;
    765		} else if ((poll_reply->nec.system ^ poll_reply->nec.not_system) != 0xff) {
    766			deb_data("NEC extended protocol\n");
    767			keycode = RC_SCANCODE_NECX(poll_reply->nec.system << 8 |
    768						    poll_reply->nec.not_system,
    769						    poll_reply->nec.data);
    770
    771			protocol = RC_PROTO_NECX;
    772		} else {
    773			deb_data("NEC normal protocol\n");
    774			keycode = RC_SCANCODE_NEC(poll_reply->nec.system,
    775						   poll_reply->nec.data);
    776			protocol = RC_PROTO_NEC;
    777		}
    778
    779		break;
    780	default:
    781		deb_data("RC5 protocol\n");
    782		protocol = RC_PROTO_RC5;
    783		toggle = poll_reply->report_id;
    784		keycode = RC_SCANCODE_RC5(poll_reply->rc5.system, poll_reply->rc5.data);
    785
    786		if ((poll_reply->rc5.data ^ poll_reply->rc5.not_data) != 0xff) {
    787			/* Key failed integrity check */
    788			err("key failed integrity check: %02x %02x %02x %02x",
    789			    poll_reply->rc5.not_used, poll_reply->rc5.system,
    790			    poll_reply->rc5.data, poll_reply->rc5.not_data);
    791			goto resubmit;
    792		}
    793
    794		break;
    795	}
    796
    797	rc_keydown(d->rc_dev, protocol, keycode, toggle);
    798
    799resubmit:
    800	/* Clean the buffer before we requeue */
    801	memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);
    802
    803	/* Requeue URB */
    804	usb_submit_urb(purb, GFP_ATOMIC);
    805}
    806
    807int dib0700_rc_setup(struct dvb_usb_device *d, struct usb_interface *intf)
    808{
    809	struct dib0700_state *st = d->priv;
    810	struct urb *purb;
    811	const struct usb_endpoint_descriptor *e;
    812	int ret, rc_ep = 1;
    813	unsigned int pipe = 0;
    814
    815	/* Poll-based. Don't initialize bulk mode */
    816	if (st->fw_version < 0x10200 || !intf)
    817		return 0;
    818
    819	/* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
    820
    821	if (intf->cur_altsetting->desc.bNumEndpoints < rc_ep + 1)
    822		return -ENODEV;
    823
    824	purb = usb_alloc_urb(0, GFP_KERNEL);
    825	if (purb == NULL)
    826		return -ENOMEM;
    827
    828	purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
    829	if (purb->transfer_buffer == NULL) {
    830		err("rc kzalloc failed");
    831		usb_free_urb(purb);
    832		return -ENOMEM;
    833	}
    834
    835	purb->status = -EINPROGRESS;
    836
    837	/*
    838	 * Some devices like the Hauppauge NovaTD model 52009 use an interrupt
    839	 * endpoint, while others use a bulk one.
    840	 */
    841	e = &intf->cur_altsetting->endpoint[rc_ep].desc;
    842	if (usb_endpoint_dir_in(e)) {
    843		if (usb_endpoint_xfer_bulk(e)) {
    844			pipe = usb_rcvbulkpipe(d->udev, rc_ep);
    845			usb_fill_bulk_urb(purb, d->udev, pipe,
    846					  purb->transfer_buffer,
    847					  RC_MSG_SIZE_V1_20,
    848					  dib0700_rc_urb_completion, d);
    849
    850		} else if (usb_endpoint_xfer_int(e)) {
    851			pipe = usb_rcvintpipe(d->udev, rc_ep);
    852			usb_fill_int_urb(purb, d->udev, pipe,
    853					  purb->transfer_buffer,
    854					  RC_MSG_SIZE_V1_20,
    855					  dib0700_rc_urb_completion, d, 1);
    856		}
    857	}
    858
    859	if (!pipe) {
    860		err("There's no endpoint for remote controller");
    861		kfree(purb->transfer_buffer);
    862		usb_free_urb(purb);
    863		return 0;
    864	}
    865
    866	ret = usb_submit_urb(purb, GFP_ATOMIC);
    867	if (ret) {
    868		err("rc submit urb failed");
    869		kfree(purb->transfer_buffer);
    870		usb_free_urb(purb);
    871	}
    872
    873	return ret;
    874}
    875
    876static int dib0700_probe(struct usb_interface *intf,
    877		const struct usb_device_id *id)
    878{
    879	int i;
    880	struct dvb_usb_device *dev;
    881
    882	for (i = 0; i < dib0700_device_count; i++)
    883		if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
    884		    &dev, adapter_nr) == 0) {
    885			struct dib0700_state *st = dev->priv;
    886			u32 hwversion, romversion, fw_version, fwtype;
    887
    888			dib0700_get_version(dev, &hwversion, &romversion,
    889				&fw_version, &fwtype);
    890
    891			deb_info("Firmware version: %x, %d, 0x%x, %d\n",
    892				hwversion, romversion, fw_version, fwtype);
    893
    894			st->fw_version = fw_version;
    895			st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;
    896
    897			/* Disable polling mode on newer firmwares */
    898			if (st->fw_version >= 0x10200)
    899				dev->props.rc.core.bulk_mode = true;
    900			else
    901				dev->props.rc.core.bulk_mode = false;
    902
    903			dib0700_rc_setup(dev, intf);
    904
    905			return 0;
    906		}
    907
    908	return -ENODEV;
    909}
    910
    911static void dib0700_disconnect(struct usb_interface *intf)
    912{
    913	struct dvb_usb_device *d = usb_get_intfdata(intf);
    914	struct dib0700_state *st = d->priv;
    915	struct i2c_client *client;
    916
    917	/* remove I2C client for tuner */
    918	client = st->i2c_client_tuner;
    919	if (client) {
    920		module_put(client->dev.driver->owner);
    921		i2c_unregister_device(client);
    922	}
    923
    924	/* remove I2C client for demodulator */
    925	client = st->i2c_client_demod;
    926	if (client) {
    927		module_put(client->dev.driver->owner);
    928		i2c_unregister_device(client);
    929	}
    930
    931	dvb_usb_device_exit(intf);
    932}
    933
    934
    935static struct usb_driver dib0700_driver = {
    936	.name       = "dvb_usb_dib0700",
    937	.probe      = dib0700_probe,
    938	.disconnect = dib0700_disconnect,
    939	.id_table   = dib0700_usb_id_table,
    940};
    941
    942module_usb_driver(dib0700_driver);
    943
    944MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw");
    945MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
    946MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
    947MODULE_VERSION("1.0");
    948MODULE_LICENSE("GPL");