dib0700_core.c 21.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Linux driver for devices based on the DiBcom DiB0700 USB bridge
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, version 2.
 *
 *  Copyright (C) 2005-6 DiBcom, SA
 */
#include "dib0700.h"

/* debug */
int dvb_usb_dib0700_debug;
module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);

16 17 18 19 20 21 22 23
static int nb_packet_buffer_size = 21;
module_param(nb_packet_buffer_size, int, 0644);
MODULE_PARM_DESC(nb_packet_buffer_size,
	"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)");

24 25
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

26 27 28 29

int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
			u32 *romversion, u32 *ramversion, u32 *fwtype)
{
30 31 32
	struct dib0700_state *st = d->priv;
	int ret;

33
	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
34
		err("could not acquire lock");
35 36 37
		return 0;
	}

38
	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
39 40
				  REQUEST_GET_VERSION,
				  USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
41
				  st->buf, 16, USB_CTRL_GET_TIMEOUT);
42
	if (hwversion != NULL)
43 44
		*hwversion  = (st->buf[0] << 24)  | (st->buf[1] << 16)  |
			(st->buf[2] << 8)  | st->buf[3];
45
	if (romversion != NULL)
46 47
		*romversion = (st->buf[4] << 24)  | (st->buf[5] << 16)  |
			(st->buf[6] << 8)  | st->buf[7];
48
	if (ramversion != NULL)
49 50
		*ramversion = (st->buf[8] << 24)  | (st->buf[9] << 16)  |
			(st->buf[10] << 8) | st->buf[11];
51
	if (fwtype != NULL)
52 53
		*fwtype     = (st->buf[12] << 24) | (st->buf[13] << 16) |
			(st->buf[14] << 8) | st->buf[15];
54
	mutex_unlock(&d->usb_mutex);
55 56 57
	return ret;
}

58 59 60 61 62 63
/* expecting rx buffer: request data[0] data[1] ... data[2] */
static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
{
	int status;

	deb_data(">>> ");
64
	debug_dump(tx, txlen, deb_data);
65 66 67 68 69 70

	status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
		tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
		USB_CTRL_GET_TIMEOUT);

	if (status != txlen)
71
		deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
72 73 74 75 76

	return status < 0 ? status : 0;
}

/* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
77
int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
	u16 index, value;
	int status;

	if (txlen < 2) {
		err("tx buffer length is smaller than 2. Makes no sense.");
		return -EINVAL;
	}
	if (txlen > 4) {
		err("tx buffer length is larger than 4. Not supported.");
		return -EINVAL;
	}

	deb_data(">>> ");
	debug_dump(tx,txlen,deb_data);

	value = ((txlen - 2) << 8) | tx[1];
	index = 0;
	if (txlen > 2)
		index |= (tx[2] << 8);
	if (txlen > 3)
		index |= tx[3];

	status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
			USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
			USB_CTRL_GET_TIMEOUT);

	if (status < 0)
106
		deb_info("ep 0 read error (status = %d)\n",status);
107 108

	deb_data("<<< ");
109
	debug_dump(rx, rxlen, deb_data);
110 111 112 113 114 115

	return status; /* length in case of success */
}

int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
{
116
	struct dib0700_state *st = d->priv;
117 118 119
	int ret;

	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
120
		err("could not acquire lock");
121 122
		return 0;
	}
123 124 125 126 127 128 129

	st->buf[0] = REQUEST_SET_GPIO;
	st->buf[1] = gpio;
	st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6);

	ret = dib0700_ctrl_wr(d, st->buf, 3);

130
	mutex_unlock(&d->usb_mutex);
131
	return ret;
132 133
}

134 135
static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets)
{
136 137 138 139
	struct dib0700_state *st = d->priv;
	int ret;

	if (st->fw_version >= 0x10201) {
140
		if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
141
			err("could not acquire lock");
142 143 144
			return 0;
		}

145 146 147
		st->buf[0] = REQUEST_SET_USB_XFER_LEN;
		st->buf[1] = (nb_ts_packets >> 8) & 0xff;
		st->buf[2] = nb_ts_packets & 0xff;
148 149 150

		deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets);

151
		ret = dib0700_ctrl_wr(d, st->buf, 3);
152
		mutex_unlock(&d->usb_mutex);
153 154 155 156 157 158
	} else {
		deb_info("this firmware does not allow to change the USB xfer len\n");
		ret = -EIO;
	}

	return ret;
159 160
}

161
/*
162
 * I2C master xfer function (supported in 1.20 firmware)
163
 */
164 165 166 167 168 169 170
static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
				int num)
{
	/* The new i2c firmware messages are more reliable and in particular
	   properly support i2c read calls not preceded by a write */

	struct dvb_usb_device *d = i2c_get_adapdata(adap);
171
	struct dib0700_state *st = d->priv;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	uint8_t bus_mode = 1;  /* 0=eeprom bus, 1=frontend bus */
	uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
	uint8_t en_start = 0;
	uint8_t en_stop = 0;
	int result, i;

	/* Ensure nobody else hits the i2c bus while we're sending our
	   sequence of messages, (such as the remote control thread) */
	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	for (i = 0; i < num; i++) {
		if (i == 0) {
			/* First message in the transaction */
			en_start = 1;
		} else if (!(msg[i].flags & I2C_M_NOSTART)) {
			/* Device supports repeated-start */
			en_start = 1;
		} else {
			/* Not the first packet and device doesn't support
			   repeated start */
			en_start = 0;
		}
		if (i == (num - 1)) {
			/* Last message in the transaction */
			en_stop = 1;
		}

		if (msg[i].flags & I2C_M_RD) {
			/* Read request */
			u16 index, value;
			uint8_t i2c_dest;

			i2c_dest = (msg[i].addr << 1);
			value = ((en_start << 7) | (en_stop << 6) |
				 (msg[i].len & 0x3F)) << 8 | i2c_dest;
			/* I2C ctrl + FE bus; */
209 210
			index = ((gen_mode << 6) & 0xC0) |
				((bus_mode << 4) & 0x30);
211 212 213 214 215 216 217 218 219

			result = usb_control_msg(d->udev,
						 usb_rcvctrlpipe(d->udev, 0),
						 REQUEST_NEW_I2C_READ,
						 USB_TYPE_VENDOR | USB_DIR_IN,
						 value, index, msg[i].buf,
						 msg[i].len,
						 USB_CTRL_GET_TIMEOUT);
			if (result < 0) {
220
				deb_info("i2c read error (status = %d)\n", result);
221 222
				break;
			}
223 224 225 226

			deb_data("<<< ");
			debug_dump(msg[i].buf, msg[i].len, deb_data);

227 228
		} else {
			/* Write request */
229
			if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
230
				err("could not acquire lock");
231 232
				return 0;
			}
233 234 235
			st->buf[0] = REQUEST_NEW_I2C_WRITE;
			st->buf[1] = msg[i].addr << 1;
			st->buf[2] = (en_start << 7) | (en_stop << 6) |
236 237
				(msg[i].len & 0x3F);
			/* I2C ctrl + FE bus; */
238
			st->buf[3] = ((gen_mode << 6) & 0xC0) |
239
				 ((bus_mode << 4) & 0x30);
240
			/* The Actual i2c payload */
241
			memcpy(&st->buf[4], msg[i].buf, msg[i].len);
242

243
			deb_data(">>> ");
244
			debug_dump(st->buf, msg[i].len + 4, deb_data);
245

246 247 248 249
			result = usb_control_msg(d->udev,
						 usb_sndctrlpipe(d->udev, 0),
						 REQUEST_NEW_I2C_WRITE,
						 USB_TYPE_VENDOR | USB_DIR_OUT,
250
						 0, 0, st->buf, msg[i].len + 4,
251
						 USB_CTRL_GET_TIMEOUT);
252
			mutex_unlock(&d->usb_mutex);
253
			if (result < 0) {
254
				deb_info("i2c write error (status = %d)\n", result);
255 256 257 258 259 260 261 262 263 264 265 266 267
				break;
			}
		}
	}
	mutex_unlock(&d->i2c_mutex);
	return i;
}

/*
 * I2C master xfer function (pre-1.20 firmware)
 */
static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
				   struct i2c_msg *msg, int num)
268 269
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
270
	struct dib0700_state *st = d->priv;
271 272 273 274
	int i,len;

	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;
275
	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
276
		err("could not acquire lock");
277 278
		return 0;
	}
279 280 281

	for (i = 0; i < num; i++) {
		/* fill in the address */
282
		st->buf[1] = msg[i].addr << 1;
283
		/* fill the buffer */
284
		memcpy(&st->buf[2], msg[i].buf, msg[i].len);
285 286 287

		/* write/read request */
		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
288 289
			st->buf[0] = REQUEST_I2C_READ;
			st->buf[1] |= 1;
290 291

			/* special thing in the current firmware: when length is zero the read-failed */
292 293 294
			len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
					msg[i+1].buf, msg[i+1].len);
			if (len <= 0) {
295
				deb_info("I2C read failed on address 0x%02x\n",
296
						msg[i].addr);
297
				break;
298
			}
299 300 301 302 303

			msg[i+1].len = len;

			i++;
		} else {
304 305
			st->buf[0] = REQUEST_I2C_WRITE;
			if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0)
306 307 308
				break;
		}
	}
309
	mutex_unlock(&d->usb_mutex);
310
	mutex_unlock(&d->i2c_mutex);
311

312 313 314
	return i;
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
			    int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	struct dib0700_state *st = d->priv;

	if (st->fw_use_new_i2c_api == 1) {
		/* User running at least fw 1.20 */
		return dib0700_i2c_xfer_new(adap, msg, num);
	} else {
		/* Use legacy calls */
		return dib0700_i2c_xfer_legacy(adap, msg, num);
	}
}

330 331 332 333 334 335 336 337 338 339
static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

struct i2c_algorithm dib0700_i2c_algo = {
	.master_xfer   = dib0700_i2c_xfer,
	.functionality = dib0700_i2c_func,
};

340 341 342
int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
			struct dvb_usb_device_description **desc, int *cold)
{
343 344 345 346 347 348 349 350 351
	s16 ret;
	u8 *b;

	b = kmalloc(16, GFP_KERNEL);
	if (!b)
		return	-ENOMEM;


	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
352 353 354 355 356
		REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);

	deb_info("FW GET_VERSION length: %d\n",ret);

	*cold = ret <= 0;
357
	deb_info("cold: %d\n", *cold);
358 359

	kfree(b);
360 361 362
	return 0;
}

363 364 365 366
static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
	u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
	u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
{
367
	struct dib0700_state *st = d->priv;
368 369 370
	int ret;

	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
371
		err("could not acquire lock");
372 373
		return 0;
	}
374 375 376 377 378 379 380 381 382 383 384 385 386 387

	st->buf[0] = REQUEST_SET_CLOCK;
	st->buf[1] = (en_pll << 7) | (pll_src << 6) |
		(pll_range << 5) | (clock_gpio3 << 4);
	st->buf[2] = (pll_prediv >> 8)  & 0xff; /* MSB */
	st->buf[3] =  pll_prediv        & 0xff; /* LSB */
	st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
	st->buf[5] =  pll_loopdiv       & 0xff; /* LSB */
	st->buf[6] = (free_div >> 8)    & 0xff; /* MSB */
	st->buf[7] =  free_div          & 0xff; /* LSB */
	st->buf[8] = (dsuScaler >> 8)   & 0xff; /* MSB */
	st->buf[9] =  dsuScaler         & 0xff; /* LSB */

	ret = dib0700_ctrl_wr(d, st->buf, 10);
388
	mutex_unlock(&d->usb_mutex);
389 390

	return ret;
391 392
}

393 394
int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
{
395
	struct dib0700_state *st = d->priv;
396
	u16 divider;
397
	int ret;
398 399 400 401

	if (scl_kHz == 0)
		return -EINVAL;

402
	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
403
		err("could not acquire lock");
404 405 406
		return 0;
	}

407
	st->buf[0] = REQUEST_SET_I2C_PARAM;
408
	divider = (u16) (30000 / scl_kHz);
409 410 411
	st->buf[1] = 0;
	st->buf[2] = (u8) (divider >> 8);
	st->buf[3] = (u8) (divider & 0xff);
412
	divider = (u16) (72000 / scl_kHz);
413 414
	st->buf[4] = (u8) (divider >> 8);
	st->buf[5] = (u8) (divider & 0xff);
415
	divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
416 417
	st->buf[6] = (u8) (divider >> 8);
	st->buf[7] = (u8) (divider & 0xff);
418

419
	deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
420 421
		(st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
		st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
422 423 424 425 426

	ret = dib0700_ctrl_wr(d, st->buf, 8);
	mutex_unlock(&d->usb_mutex);

	return ret;
427 428 429
}


430 431 432 433 434 435 436 437 438
int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
{
	switch (clk_MHz) {
		case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
		default: return -EINVAL;
	}
	return 0;
}

439 440
static int dib0700_jumpram(struct usb_device *udev, u32 address)
{
441 442 443 444 445 446 447 448 449 450 451 452 453 454
	int ret = 0, actlen;
	u8 *buf;

	buf = kmalloc(8, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
	buf[0] = REQUEST_JUMPRAM;
	buf[1] = 0;
	buf[2] = 0;
	buf[3] = 0;
	buf[4] = (address >> 24) & 0xff;
	buf[5] = (address >> 16) & 0xff;
	buf[6] = (address >> 8)  & 0xff;
	buf[7] =  address        & 0xff;
455 456 457

	if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
		deb_fw("jumpram to 0x%x failed\n",address);
458
		goto out;
459 460 461
	}
	if (actlen != 8) {
		deb_fw("jumpram to 0x%x failed\n",address);
462 463
		ret = -EIO;
		goto out;
464
	}
465 466 467
out:
	kfree(buf);
	return ret;
468 469 470 471 472
}

int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
{
	struct hexline hx;
473
	int pos = 0, ret, act_len, i, adap_num;
474
	u8 *buf;
475
	u32 fw_version;
476

477 478 479
	buf = kmalloc(260, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
480 481

	while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
482 483
		deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
				hx.addr, hx.len, hx.chk);
484 485

		buf[0] = hx.len;
486 487
		buf[1] = (hx.addr >> 8) & 0xff;
		buf[2] =  hx.addr       & 0xff;
488 489 490 491 492 493 494 495 496 497 498 499 500
		buf[3] = hx.type;
		memcpy(&buf[4],hx.data,hx.len);
		buf[4+hx.len] = hx.chk;

		ret = usb_bulk_msg(udev,
			usb_sndbulkpipe(udev, 0x01),
			buf,
			hx.len + 5,
			&act_len,
			1000);

		if (ret < 0) {
			err("firmware download failed at %d with %d",pos,ret);
501
			goto out;
502 503 504 505 506
		}
	}

	if (ret == 0) {
		/* start the firmware */
507
		if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
508
			info("firmware started successfully.");
509
			msleep(500);
510
		}
511 512 513
	} else
		ret = -EIO;

514 515 516 517 518 519 520 521
	/* the number of ts packet has to be at least 1 */
	if (nb_packet_buffer_size < 1)
		nb_packet_buffer_size = 1;

	/* get the fimware version */
	usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
				  REQUEST_GET_VERSION,
				  USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
522 523
				  buf, 16, USB_CTRL_GET_TIMEOUT);
	fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
524 525 526 527 528 529

	/* set the buffer size - DVB-USB is allocating URB buffers
	 * only after the firwmare download was successful */
	for (i = 0; i < dib0700_device_count; i++) {
		for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
				adap_num++) {
530
			if (fw_version >= 0x10201) {
531
				dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
532
			} else {
533 534
				/* for fw version older than 1.20.1,
				 * the buffersize has to be n times 512 */
535 536 537
				dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
				if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512)
					dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512;
538 539 540
			}
		}
	}
541 542
out:
	kfree(buf);
543 544 545 546 547 548
	return ret;
}

int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	struct dib0700_state *st = adap->dev->priv;
549 550 551 552 553 554 555 556 557 558 559 560
	int ret;

	if ((onoff != 0) && (st->fw_version >= 0x10201)) {
		/* for firmware later than 1.20.1,
		 * the USB xfer length can be set  */
		ret = dib0700_set_usb_xfer_len(adap->dev,
			st->nb_packet_buffer_size);
		if (ret < 0) {
			deb_info("can not set the USB xfer len\n");
			return ret;
		}
	}
561

562
	if (mutex_lock_interruptible(&adap->dev->usb_mutex) < 0) {
563
		err("could not acquire lock");
564 565 566
		return 0;
	}

567 568 569 570
	st->buf[0] = REQUEST_ENABLE_VIDEO;
	/* this bit gives a kind of command,
	 * rather than enabling something or not */
	st->buf[1] = (onoff << 4) | 0x00;
571 572

	if (st->disable_streaming_master_mode == 1)
573
		st->buf[2] = 0x00;
574
	else
575
		st->buf[2] = 0x01 << 4; /* Master mode */
576

577
	st->buf[3] = 0x00;
578 579 580

	deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);

581
	st->channel_state &= ~0x3;
582 583 584
	if ((adap->fe_adap[0].stream.props.endpoint != 2)
			&& (adap->fe_adap[0].stream.props.endpoint != 3)) {
		deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->fe_adap[0].stream.props.endpoint);
585 586 587 588
		if (onoff)
			st->channel_state |=	1 << (adap->id);
		else
			st->channel_state |=	1 << ~(adap->id);
589
	} else {
590
		if (onoff)
591
			st->channel_state |=	1 << (adap->fe_adap[0].stream.props.endpoint-2);
592
		else
593
			st->channel_state |=	1 << (3-adap->fe_adap[0].stream.props.endpoint);
594
	}
595

596
	st->buf[2] |= st->channel_state;
597

598
	deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]);
599

600 601 602 603
	ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
	mutex_unlock(&adap->dev->usb_mutex);

	return ret;
604 605
}

606
int dib0700_change_protocol(struct rc_dev *rc, u64 rc_type)
607
{
608
	struct dvb_usb_device *d = rc->priv;
609 610 611
	struct dib0700_state *st = d->priv;
	int new_proto, ret;

612
	if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
613
		err("could not acquire lock");
614 615 616
		return 0;
	}

617 618 619 620
	st->buf[0] = REQUEST_SET_RC;
	st->buf[1] = 0;
	st->buf[2] = 0;

621
	/* Set the IR mode */
622
	if (rc_type == RC_TYPE_RC5)
623
		new_proto = 1;
624
	else if (rc_type == RC_TYPE_NEC)
625
		new_proto = 0;
626
	else if (rc_type == RC_TYPE_RC6) {
627 628 629 630
		if (st->fw_version < 0x10200) {
			ret = -EINVAL;
			goto out;
		}
631 632

		new_proto = 2;
633 634 635 636
	} else {
		ret = -EINVAL;
		goto out;
	}
637

638
	st->buf[1] = new_proto;
639

640
	ret = dib0700_ctrl_wr(d, st->buf, 3);
641 642
	if (ret < 0) {
		err("ir protocol setup failed");
643
		goto out;
644 645
	}

646
	d->props.rc.core.protocol = rc_type;
647

648 649
out:
	mutex_unlock(&d->usb_mutex);
650 651 652
	return ret;
}

653 654 655 656 657 658 659
/* Number of keypresses to ignore before start repeating */
#define RC_REPEAT_DELAY_V1_20 10

/* This is the structure of the RC response packet starting in firmware 1.20 */
struct dib0700_rc_response {
	u8 report_id;
	u8 data_state;
660 661 662 663
	union {
		u16 system16;
		struct {
			u8 not_system;
664
			u8 system;
665 666
		};
	};
667 668 669 670 671 672 673 674
	u8 data;
	u8 not_data;
};
#define RC_MSG_SIZE_V1_20 6

static void dib0700_rc_urb_completion(struct urb *purb)
{
	struct dvb_usb_device *d = purb->context;
675 676
	struct dib0700_rc_response *poll_reply;
	u32 uninitialized_var(keycode);
677
	u8 toggle;
678 679

	deb_info("%s()\n", __func__);
680
	if (d->rc_dev == NULL) {
681
		/* This will occur if disable_rc_polling=1 */
682
		kfree(purb->transfer_buffer);
683 684 685 686
		usb_free_urb(purb);
		return;
	}

687
	poll_reply = purb->transfer_buffer;
688 689 690

	if (purb->status < 0) {
		deb_info("discontinuing polling\n");
691
		kfree(purb->transfer_buffer);
692 693 694 695 696 697 698 699 700
		usb_free_urb(purb);
		return;
	}

	if (purb->actual_length != RC_MSG_SIZE_V1_20) {
		deb_info("malformed rc msg size=%d\n", purb->actual_length);
		goto resubmit;
	}

701 702 703 704 705
	deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
		 poll_reply->report_id, poll_reply->data_state,
		 poll_reply->system, poll_reply->not_system,
		 poll_reply->data, poll_reply->not_data,
		 purb->actual_length);
706

707
	switch (d->props.rc.core.protocol) {
708
	case RC_TYPE_NEC:
709
		toggle = 0;
710 711

		/* NEC protocol sends repeat code as 0 0 0 FF */
712 713 714
		if ((poll_reply->system == 0x00) && (poll_reply->data == 0x00)
		    && (poll_reply->not_data == 0xff)) {
			poll_reply->data_state = 2;
715 716
			break;
		}
717

718 719 720
		if ((poll_reply->system ^ poll_reply->not_system) != 0xff) {
			deb_data("NEC extended protocol\n");
			/* NEC extended code - 24 bits */
721
			keycode = be16_to_cpu(poll_reply->system16) << 8 | poll_reply->data;
722 723 724 725 726 727
		} else {
			deb_data("NEC normal protocol\n");
			/* normal NEC code - 16 bits */
			keycode = poll_reply->system << 8 | poll_reply->data;
		}

728 729
		break;
	default:
730
		deb_data("RC5 protocol\n");
731
		/* RC5 Protocol */
732
		toggle = poll_reply->report_id;
733
		keycode = poll_reply->system << 8 | poll_reply->data;
734

735 736 737
		break;
	}

738
	if ((poll_reply->data + poll_reply->not_data) != 0xff) {
739 740
		/* Key failed integrity check */
		err("key failed integrity check: %04x %02x %02x",
741 742
		    poll_reply->system,
		    poll_reply->data, poll_reply->not_data);
743 744 745
		goto resubmit;
	}

746
	rc_keydown(d->rc_dev, keycode, toggle);
747 748 749 750 751 752 753 754 755

resubmit:
	/* Clean the buffer before we requeue */
	memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);

	/* Requeue URB */
	usb_submit_urb(purb, GFP_ATOMIC);
}

756
int dib0700_rc_setup(struct dvb_usb_device *d)
757
{
758 759 760 761
	struct dib0700_state *st = d->priv;
	struct urb *purb;
	int ret;

762
	/* Poll-based. Don't initialize bulk mode */
763 764 765 766 767 768 769
	if (st->fw_version < 0x10200)
		return 0;

	/* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
	purb = usb_alloc_urb(0, GFP_KERNEL);
	if (purb == NULL) {
		err("rc usb alloc urb failed\n");
770
		return -ENOMEM;
771 772 773 774 775 776
	}

	purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
	if (purb->transfer_buffer == NULL) {
		err("rc kzalloc failed\n");
		usb_free_urb(purb);
777
		return -ENOMEM;
778 779 780 781 782 783 784 785
	}

	purb->status = -EINPROGRESS;
	usb_fill_bulk_urb(purb, d->udev, usb_rcvbulkpipe(d->udev, 1),
			  purb->transfer_buffer, RC_MSG_SIZE_V1_20,
			  dib0700_rc_urb_completion, d);

	ret = usb_submit_urb(purb, GFP_ATOMIC);
786
	if (ret) {
787
		err("rc submit urb failed\n");
788 789 790
		kfree(purb->transfer_buffer);
		usb_free_urb(purb);
	}
791

792
	return ret;
793 794
}

795 796 797 798
static int dib0700_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
	int i;
799
	struct dvb_usb_device *dev;
800 801

	for (i = 0; i < dib0700_device_count; i++)
802
		if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
803 804 805 806 807 808 809 810 811 812 813 814 815
		    &dev, adapter_nr) == 0) {
			struct dib0700_state *st = dev->priv;
			u32 hwversion, romversion, fw_version, fwtype;

			dib0700_get_version(dev, &hwversion, &romversion,
				&fw_version, &fwtype);

			deb_info("Firmware version: %x, %d, 0x%x, %d\n",
				hwversion, romversion, fw_version, fwtype);

			st->fw_version = fw_version;
			st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;

816 817 818 819 820 821
			/* Disable polling mode on newer firmwares */
			if (st->fw_version >= 0x10200)
				dev->props.rc.core.bulk_mode = true;
			else
				dev->props.rc.core.bulk_mode = false;

822
			dib0700_rc_setup(dev);
823

824
			return 0;
825
		}
826 827 828 829 830 831 832 833 834 835 836

	return -ENODEV;
}

static struct usb_driver dib0700_driver = {
	.name       = "dvb_usb_dib0700",
	.probe      = dib0700_probe,
	.disconnect = dvb_usb_device_exit,
	.id_table   = dib0700_usb_id_table,
};

837
module_usb_driver(dib0700_driver);
838

839
MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw");
840 841 842 843
MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");