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