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