Commit | Line | Data |
---|---|---|
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 */ | |
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 | ||
acc5c9ee OG |
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 | ||
78e92006 JG |
24 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
25 | ||
99afb989 DH |
26 | |
27 | int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion, | |
28 | u32 *romversion, u32 *ramversion, u32 *fwtype) | |
29 | { | |
ffa5899c OG |
30 | struct dib0700_state *st = d->priv; |
31 | int ret; | |
32 | ||
33 | ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), | |
99afb989 DH |
34 | REQUEST_GET_VERSION, |
35 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, | |
ffa5899c | 36 | st->buf, 16, USB_CTRL_GET_TIMEOUT); |
acc5c9ee | 37 | if (hwversion != NULL) |
ffa5899c OG |
38 | *hwversion = (st->buf[0] << 24) | (st->buf[1] << 16) | |
39 | (st->buf[2] << 8) | st->buf[3]; | |
acc5c9ee | 40 | if (romversion != NULL) |
ffa5899c OG |
41 | *romversion = (st->buf[4] << 24) | (st->buf[5] << 16) | |
42 | (st->buf[6] << 8) | st->buf[7]; | |
acc5c9ee | 43 | if (ramversion != NULL) |
ffa5899c OG |
44 | *ramversion = (st->buf[8] << 24) | (st->buf[9] << 16) | |
45 | (st->buf[10] << 8) | st->buf[11]; | |
acc5c9ee | 46 | if (fwtype != NULL) |
ffa5899c OG |
47 | *fwtype = (st->buf[12] << 24) | (st->buf[13] << 16) | |
48 | (st->buf[14] << 8) | st->buf[15]; | |
99afb989 DH |
49 | return ret; |
50 | } | |
51 | ||
b7f54910 PB |
52 | /* expecting rx buffer: request data[0] data[1] ... data[2] */ |
53 | static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen) | |
54 | { | |
55 | int status; | |
56 | ||
57 | deb_data(">>> "); | |
230b27cd | 58 | debug_dump(tx, txlen, deb_data); |
b7f54910 PB |
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) | |
6958effe | 65 | deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen); |
b7f54910 PB |
66 | |
67 | return status < 0 ? status : 0; | |
68 | } | |
69 | ||
70 | /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */ | |
54d75eba | 71 | int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen) |
b7f54910 PB |
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 | ||
b7f54910 PB |
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) | |
6958effe | 100 | deb_info("ep 0 read error (status = %d)\n",status); |
b7f54910 PB |
101 | |
102 | deb_data("<<< "); | |
230b27cd | 103 | debug_dump(rx, rxlen, deb_data); |
b7f54910 PB |
104 | |
105 | return status; /* length in case of success */ | |
106 | } | |
107 | ||
108 | int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val) | |
109 | { | |
ffa5899c OG |
110 | struct dib0700_state *st = d->priv; |
111 | s16 ret; | |
112 | ||
113 | st->buf[0] = REQUEST_SET_GPIO; | |
114 | st->buf[1] = gpio; | |
115 | st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6); | |
116 | ||
117 | ret = dib0700_ctrl_wr(d, st->buf, 3); | |
118 | ||
119 | return ret; | |
b7f54910 PB |
120 | } |
121 | ||
acc5c9ee OG |
122 | static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets) |
123 | { | |
230b27cd | 124 | struct dib0700_state *st = d->priv; |
230b27cd DM |
125 | int ret; |
126 | ||
127 | if (st->fw_version >= 0x10201) { | |
ffa5899c OG |
128 | st->buf[0] = REQUEST_SET_USB_XFER_LEN; |
129 | st->buf[1] = (nb_ts_packets >> 8) & 0xff; | |
130 | st->buf[2] = nb_ts_packets & 0xff; | |
230b27cd DM |
131 | |
132 | deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets); | |
133 | ||
ffa5899c | 134 | ret = dib0700_ctrl_wr(d, st->buf, 3); |
230b27cd DM |
135 | } else { |
136 | deb_info("this firmware does not allow to change the USB xfer len\n"); | |
137 | ret = -EIO; | |
138 | } | |
139 | ||
140 | return ret; | |
acc5c9ee OG |
141 | } |
142 | ||
b7f54910 | 143 | /* |
bdc203e1 | 144 | * I2C master xfer function (supported in 1.20 firmware) |
b7f54910 | 145 | */ |
bdc203e1 DH |
146 | static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg, |
147 | int num) | |
148 | { | |
149 | /* The new i2c firmware messages are more reliable and in particular | |
150 | properly support i2c read calls not preceded by a write */ | |
151 | ||
152 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
ffa5899c | 153 | struct dib0700_state *st = d->priv; |
bdc203e1 DH |
154 | uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */ |
155 | uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */ | |
156 | uint8_t en_start = 0; | |
157 | uint8_t en_stop = 0; | |
bdc203e1 DH |
158 | int result, i; |
159 | ||
160 | /* Ensure nobody else hits the i2c bus while we're sending our | |
161 | sequence of messages, (such as the remote control thread) */ | |
162 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) | |
163 | return -EAGAIN; | |
164 | ||
165 | for (i = 0; i < num; i++) { | |
166 | if (i == 0) { | |
167 | /* First message in the transaction */ | |
168 | en_start = 1; | |
169 | } else if (!(msg[i].flags & I2C_M_NOSTART)) { | |
170 | /* Device supports repeated-start */ | |
171 | en_start = 1; | |
172 | } else { | |
173 | /* Not the first packet and device doesn't support | |
174 | repeated start */ | |
175 | en_start = 0; | |
176 | } | |
177 | if (i == (num - 1)) { | |
178 | /* Last message in the transaction */ | |
179 | en_stop = 1; | |
180 | } | |
181 | ||
182 | if (msg[i].flags & I2C_M_RD) { | |
183 | /* Read request */ | |
184 | u16 index, value; | |
185 | uint8_t i2c_dest; | |
186 | ||
187 | i2c_dest = (msg[i].addr << 1); | |
188 | value = ((en_start << 7) | (en_stop << 6) | | |
189 | (msg[i].len & 0x3F)) << 8 | i2c_dest; | |
190 | /* I2C ctrl + FE bus; */ | |
230b27cd DM |
191 | index = ((gen_mode << 6) & 0xC0) | |
192 | ((bus_mode << 4) & 0x30); | |
bdc203e1 DH |
193 | |
194 | result = usb_control_msg(d->udev, | |
195 | usb_rcvctrlpipe(d->udev, 0), | |
196 | REQUEST_NEW_I2C_READ, | |
197 | USB_TYPE_VENDOR | USB_DIR_IN, | |
198 | value, index, msg[i].buf, | |
199 | msg[i].len, | |
200 | USB_CTRL_GET_TIMEOUT); | |
201 | if (result < 0) { | |
be9bae10 | 202 | deb_info("i2c read error (status = %d)\n", result); |
bdc203e1 DH |
203 | break; |
204 | } | |
d2514991 MK |
205 | |
206 | deb_data("<<< "); | |
207 | debug_dump(msg[i].buf, msg[i].len, deb_data); | |
208 | ||
bdc203e1 DH |
209 | } else { |
210 | /* Write request */ | |
ffa5899c OG |
211 | st->buf[0] = REQUEST_NEW_I2C_WRITE; |
212 | st->buf[1] = msg[i].addr << 1; | |
213 | st->buf[2] = (en_start << 7) | (en_stop << 6) | | |
bdc203e1 DH |
214 | (msg[i].len & 0x3F); |
215 | /* I2C ctrl + FE bus; */ | |
ffa5899c | 216 | st->buf[3] = ((gen_mode << 6) & 0xC0) | |
230b27cd | 217 | ((bus_mode << 4) & 0x30); |
bdc203e1 | 218 | /* The Actual i2c payload */ |
ffa5899c | 219 | memcpy(&st->buf[4], msg[i].buf, msg[i].len); |
bdc203e1 | 220 | |
d2514991 | 221 | deb_data(">>> "); |
ffa5899c | 222 | debug_dump(st->buf, msg[i].len + 4, deb_data); |
d2514991 | 223 | |
bdc203e1 DH |
224 | result = usb_control_msg(d->udev, |
225 | usb_sndctrlpipe(d->udev, 0), | |
226 | REQUEST_NEW_I2C_WRITE, | |
227 | USB_TYPE_VENDOR | USB_DIR_OUT, | |
ffa5899c | 228 | 0, 0, st->buf, msg[i].len + 4, |
bdc203e1 DH |
229 | USB_CTRL_GET_TIMEOUT); |
230 | if (result < 0) { | |
be9bae10 | 231 | deb_info("i2c write error (status = %d)\n", result); |
bdc203e1 DH |
232 | break; |
233 | } | |
234 | } | |
235 | } | |
236 | mutex_unlock(&d->i2c_mutex); | |
237 | return i; | |
238 | } | |
239 | ||
240 | /* | |
241 | * I2C master xfer function (pre-1.20 firmware) | |
242 | */ | |
243 | static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap, | |
244 | struct i2c_msg *msg, int num) | |
b7f54910 PB |
245 | { |
246 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
ffa5899c | 247 | struct dib0700_state *st = d->priv; |
b7f54910 | 248 | int i,len; |
b7f54910 PB |
249 | |
250 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) | |
251 | return -EAGAIN; | |
252 | ||
253 | for (i = 0; i < num; i++) { | |
254 | /* fill in the address */ | |
ffa5899c | 255 | st->buf[1] = msg[i].addr << 1; |
b7f54910 | 256 | /* fill the buffer */ |
ffa5899c | 257 | memcpy(&st->buf[2], msg[i].buf, msg[i].len); |
b7f54910 PB |
258 | |
259 | /* write/read request */ | |
260 | if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { | |
ffa5899c OG |
261 | st->buf[0] = REQUEST_I2C_READ; |
262 | st->buf[1] |= 1; | |
b7f54910 PB |
263 | |
264 | /* special thing in the current firmware: when length is zero the read-failed */ | |
ffa5899c OG |
265 | len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2, |
266 | msg[i+1].buf, msg[i+1].len); | |
267 | if (len <= 0) { | |
8db12cdf | 268 | deb_info("I2C read failed on address 0x%02x\n", |
ffa5899c | 269 | msg[i].addr); |
b7f54910 | 270 | break; |
303cbeaa | 271 | } |
b7f54910 PB |
272 | |
273 | msg[i+1].len = len; | |
274 | ||
275 | i++; | |
276 | } else { | |
ffa5899c OG |
277 | st->buf[0] = REQUEST_I2C_WRITE; |
278 | if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0) | |
b7f54910 PB |
279 | break; |
280 | } | |
281 | } | |
b7f54910 | 282 | mutex_unlock(&d->i2c_mutex); |
ffa5899c | 283 | |
b7f54910 PB |
284 | return i; |
285 | } | |
286 | ||
bdc203e1 DH |
287 | static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, |
288 | int num) | |
289 | { | |
290 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
291 | struct dib0700_state *st = d->priv; | |
292 | ||
293 | if (st->fw_use_new_i2c_api == 1) { | |
294 | /* User running at least fw 1.20 */ | |
295 | return dib0700_i2c_xfer_new(adap, msg, num); | |
296 | } else { | |
297 | /* Use legacy calls */ | |
298 | return dib0700_i2c_xfer_legacy(adap, msg, num); | |
299 | } | |
300 | } | |
301 | ||
b7f54910 PB |
302 | static u32 dib0700_i2c_func(struct i2c_adapter *adapter) |
303 | { | |
304 | return I2C_FUNC_I2C; | |
305 | } | |
306 | ||
307 | struct i2c_algorithm dib0700_i2c_algo = { | |
308 | .master_xfer = dib0700_i2c_xfer, | |
309 | .functionality = dib0700_i2c_func, | |
310 | }; | |
311 | ||
6958effe PB |
312 | int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props, |
313 | struct dvb_usb_device_description **desc, int *cold) | |
314 | { | |
ffa5899c OG |
315 | s16 ret; |
316 | u8 *b; | |
317 | ||
318 | b = kmalloc(16, GFP_KERNEL); | |
319 | if (!b) | |
320 | return -ENOMEM; | |
321 | ||
322 | ||
323 | ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
a75763ff PB |
324 | REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT); |
325 | ||
326 | deb_info("FW GET_VERSION length: %d\n",ret); | |
327 | ||
328 | *cold = ret <= 0; | |
6958effe | 329 | deb_info("cold: %d\n", *cold); |
ffa5899c OG |
330 | |
331 | kfree(b); | |
6958effe PB |
332 | return 0; |
333 | } | |
334 | ||
a75763ff PB |
335 | static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll, |
336 | u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv, | |
337 | u16 pll_loopdiv, u16 free_div, u16 dsuScaler) | |
338 | { | |
ffa5899c OG |
339 | struct dib0700_state *st = d->priv; |
340 | s16 ret; | |
341 | ||
342 | st->buf[0] = REQUEST_SET_CLOCK; | |
343 | st->buf[1] = (en_pll << 7) | (pll_src << 6) | | |
344 | (pll_range << 5) | (clock_gpio3 << 4); | |
345 | st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */ | |
346 | st->buf[3] = pll_prediv & 0xff; /* LSB */ | |
347 | st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */ | |
348 | st->buf[5] = pll_loopdiv & 0xff; /* LSB */ | |
349 | st->buf[6] = (free_div >> 8) & 0xff; /* MSB */ | |
350 | st->buf[7] = free_div & 0xff; /* LSB */ | |
351 | st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */ | |
352 | st->buf[9] = dsuScaler & 0xff; /* LSB */ | |
353 | ||
354 | ret = dib0700_ctrl_wr(d, st->buf, 10); | |
355 | ||
356 | return ret; | |
a75763ff PB |
357 | } |
358 | ||
7757ddda OG |
359 | int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz) |
360 | { | |
ffa5899c | 361 | struct dib0700_state *st = d->priv; |
7757ddda | 362 | u16 divider; |
7757ddda OG |
363 | |
364 | if (scl_kHz == 0) | |
365 | return -EINVAL; | |
366 | ||
ffa5899c | 367 | st->buf[0] = REQUEST_SET_I2C_PARAM; |
7757ddda | 368 | divider = (u16) (30000 / scl_kHz); |
ffa5899c OG |
369 | st->buf[1] = 0; |
370 | st->buf[2] = (u8) (divider >> 8); | |
371 | st->buf[3] = (u8) (divider & 0xff); | |
7757ddda | 372 | divider = (u16) (72000 / scl_kHz); |
ffa5899c OG |
373 | st->buf[4] = (u8) (divider >> 8); |
374 | st->buf[5] = (u8) (divider & 0xff); | |
7757ddda | 375 | divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */ |
ffa5899c OG |
376 | st->buf[6] = (u8) (divider >> 8); |
377 | st->buf[7] = (u8) (divider & 0xff); | |
7757ddda | 378 | |
b4d6046e | 379 | deb_info("setting I2C speed: %04x %04x %04x (%d kHz).", |
ffa5899c OG |
380 | (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) | |
381 | st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz); | |
382 | return dib0700_ctrl_wr(d, st->buf, 8); | |
7757ddda OG |
383 | } |
384 | ||
385 | ||
a75763ff PB |
386 | int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3) |
387 | { | |
388 | switch (clk_MHz) { | |
389 | case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break; | |
390 | default: return -EINVAL; | |
391 | } | |
392 | return 0; | |
393 | } | |
394 | ||
b7f54910 PB |
395 | static int dib0700_jumpram(struct usb_device *udev, u32 address) |
396 | { | |
ffa5899c OG |
397 | int ret = 0, actlen; |
398 | u8 *buf; | |
399 | ||
400 | buf = kmalloc(8, GFP_KERNEL); | |
401 | if (!buf) | |
402 | return -ENOMEM; | |
403 | buf[0] = REQUEST_JUMPRAM; | |
404 | buf[1] = 0; | |
405 | buf[2] = 0; | |
406 | buf[3] = 0; | |
407 | buf[4] = (address >> 24) & 0xff; | |
408 | buf[5] = (address >> 16) & 0xff; | |
409 | buf[6] = (address >> 8) & 0xff; | |
410 | buf[7] = address & 0xff; | |
b7f54910 PB |
411 | |
412 | if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) { | |
413 | deb_fw("jumpram to 0x%x failed\n",address); | |
ffa5899c | 414 | goto out; |
b7f54910 PB |
415 | } |
416 | if (actlen != 8) { | |
417 | deb_fw("jumpram to 0x%x failed\n",address); | |
ffa5899c OG |
418 | ret = -EIO; |
419 | goto out; | |
b7f54910 | 420 | } |
ffa5899c OG |
421 | out: |
422 | kfree(buf); | |
423 | return ret; | |
b7f54910 PB |
424 | } |
425 | ||
426 | int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw) | |
427 | { | |
428 | struct hexline hx; | |
acc5c9ee | 429 | int pos = 0, ret, act_len, i, adap_num; |
ffa5899c | 430 | u8 *buf; |
acc5c9ee | 431 | u32 fw_version; |
b7f54910 | 432 | |
ffa5899c OG |
433 | buf = kmalloc(260, GFP_KERNEL); |
434 | if (!buf) | |
435 | return -ENOMEM; | |
b7f54910 PB |
436 | |
437 | while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) { | |
230b27cd DM |
438 | deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n", |
439 | hx.addr, hx.len, hx.chk); | |
b7f54910 PB |
440 | |
441 | buf[0] = hx.len; | |
5bc63607 PB |
442 | buf[1] = (hx.addr >> 8) & 0xff; |
443 | buf[2] = hx.addr & 0xff; | |
b7f54910 PB |
444 | buf[3] = hx.type; |
445 | memcpy(&buf[4],hx.data,hx.len); | |
446 | buf[4+hx.len] = hx.chk; | |
447 | ||
448 | ret = usb_bulk_msg(udev, | |
449 | usb_sndbulkpipe(udev, 0x01), | |
450 | buf, | |
451 | hx.len + 5, | |
452 | &act_len, | |
453 | 1000); | |
454 | ||
455 | if (ret < 0) { | |
456 | err("firmware download failed at %d with %d",pos,ret); | |
ffa5899c | 457 | goto out; |
b7f54910 PB |
458 | } |
459 | } | |
460 | ||
461 | if (ret == 0) { | |
462 | /* start the firmware */ | |
6958effe | 463 | if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) { |
b7f54910 | 464 | info("firmware started successfully."); |
a75763ff | 465 | msleep(500); |
6958effe | 466 | } |
b7f54910 PB |
467 | } else |
468 | ret = -EIO; | |
469 | ||
acc5c9ee OG |
470 | /* the number of ts packet has to be at least 1 */ |
471 | if (nb_packet_buffer_size < 1) | |
472 | nb_packet_buffer_size = 1; | |
473 | ||
474 | /* get the fimware version */ | |
475 | usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
476 | REQUEST_GET_VERSION, | |
477 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, | |
ffa5899c OG |
478 | buf, 16, USB_CTRL_GET_TIMEOUT); |
479 | fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; | |
acc5c9ee OG |
480 | |
481 | /* set the buffer size - DVB-USB is allocating URB buffers | |
482 | * only after the firwmare download was successful */ | |
483 | for (i = 0; i < dib0700_device_count; i++) { | |
484 | for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters; | |
485 | adap_num++) { | |
230b27cd | 486 | if (fw_version >= 0x10201) { |
acc5c9ee | 487 | dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = 188*nb_packet_buffer_size; |
230b27cd | 488 | } else { |
acc5c9ee OG |
489 | /* for fw version older than 1.20.1, |
490 | * the buffersize has to be n times 512 */ | |
491 | dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512; | |
492 | if (dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize < 512) | |
493 | dib0700_devices[i].adapter[adap_num].stream.u.bulk.buffersize = 512; | |
494 | } | |
495 | } | |
496 | } | |
ffa5899c OG |
497 | out: |
498 | kfree(buf); | |
b7f54910 PB |
499 | return ret; |
500 | } | |
501 | ||
502 | int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) | |
503 | { | |
504 | struct dib0700_state *st = adap->dev->priv; | |
acc5c9ee OG |
505 | int ret; |
506 | ||
507 | if ((onoff != 0) && (st->fw_version >= 0x10201)) { | |
508 | /* for firmware later than 1.20.1, | |
509 | * the USB xfer length can be set */ | |
510 | ret = dib0700_set_usb_xfer_len(adap->dev, | |
511 | st->nb_packet_buffer_size); | |
512 | if (ret < 0) { | |
513 | deb_info("can not set the USB xfer len\n"); | |
514 | return ret; | |
515 | } | |
516 | } | |
b7f54910 | 517 | |
ffa5899c OG |
518 | st->buf[0] = REQUEST_ENABLE_VIDEO; |
519 | /* this bit gives a kind of command, | |
520 | * rather than enabling something or not */ | |
521 | st->buf[1] = (onoff << 4) | 0x00; | |
cb22cb52 DH |
522 | |
523 | if (st->disable_streaming_master_mode == 1) | |
ffa5899c | 524 | st->buf[2] = 0x00; |
cb22cb52 | 525 | else |
ffa5899c | 526 | st->buf[2] = 0x01 << 4; /* Master mode */ |
cb22cb52 | 527 | |
ffa5899c | 528 | st->buf[3] = 0x00; |
b7f54910 PB |
529 | |
530 | deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id); | |
531 | ||
7757ddda | 532 | st->channel_state &= ~0x3; |
b4d6046e OG |
533 | if ((adap->stream.props.endpoint != 2) |
534 | && (adap->stream.props.endpoint != 3)) { | |
7757ddda OG |
535 | deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->stream.props.endpoint); |
536 | if (onoff) | |
537 | st->channel_state |= 1 << (adap->id); | |
538 | else | |
539 | st->channel_state |= 1 << ~(adap->id); | |
b4d6046e | 540 | } else { |
7757ddda OG |
541 | if (onoff) |
542 | st->channel_state |= 1 << (adap->stream.props.endpoint-2); | |
543 | else | |
544 | st->channel_state |= 1 << (3-adap->stream.props.endpoint); | |
545 | } | |
b7f54910 | 546 | |
ffa5899c | 547 | st->buf[2] |= st->channel_state; |
b7f54910 | 548 | |
ffa5899c | 549 | deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]); |
b7f54910 | 550 | |
ffa5899c | 551 | return dib0700_ctrl_wr(adap->dev, st->buf, 4); |
b7f54910 PB |
552 | } |
553 | ||
52b66144 | 554 | int dib0700_change_protocol(struct rc_dev *rc, u64 rc_type) |
0ffd1ab3 | 555 | { |
d8b4b582 | 556 | struct dvb_usb_device *d = rc->priv; |
0ffd1ab3 | 557 | struct dib0700_state *st = d->priv; |
0ffd1ab3 MCC |
558 | int new_proto, ret; |
559 | ||
ffa5899c OG |
560 | st->buf[0] = REQUEST_SET_RC; |
561 | st->buf[1] = 0; | |
562 | st->buf[2] = 0; | |
563 | ||
0ffd1ab3 | 564 | /* Set the IR mode */ |
52b66144 | 565 | if (rc_type == RC_TYPE_RC5) |
0ffd1ab3 | 566 | new_proto = 1; |
52b66144 | 567 | else if (rc_type == RC_TYPE_NEC) |
0ffd1ab3 | 568 | new_proto = 0; |
52b66144 | 569 | else if (rc_type == RC_TYPE_RC6) { |
0ffd1ab3 MCC |
570 | if (st->fw_version < 0x10200) |
571 | return -EINVAL; | |
572 | ||
573 | new_proto = 2; | |
574 | } else | |
575 | return -EINVAL; | |
576 | ||
ffa5899c | 577 | st->buf[1] = new_proto; |
0ffd1ab3 | 578 | |
ffa5899c | 579 | ret = dib0700_ctrl_wr(d, st->buf, 3); |
0ffd1ab3 MCC |
580 | if (ret < 0) { |
581 | err("ir protocol setup failed"); | |
582 | return ret; | |
583 | } | |
584 | ||
52b66144 | 585 | d->props.rc.core.protocol = rc_type; |
0ffd1ab3 MCC |
586 | |
587 | return ret; | |
588 | } | |
589 | ||
6a207100 DH |
590 | /* Number of keypresses to ignore before start repeating */ |
591 | #define RC_REPEAT_DELAY_V1_20 10 | |
592 | ||
593 | /* This is the structure of the RC response packet starting in firmware 1.20 */ | |
594 | struct dib0700_rc_response { | |
595 | u8 report_id; | |
596 | u8 data_state; | |
d3c501d1 MCC |
597 | union { |
598 | u16 system16; | |
599 | struct { | |
d3c501d1 | 600 | u8 not_system; |
59aa3460 | 601 | u8 system; |
d3c501d1 MCC |
602 | }; |
603 | }; | |
6a207100 DH |
604 | u8 data; |
605 | u8 not_data; | |
606 | }; | |
607 | #define RC_MSG_SIZE_V1_20 6 | |
608 | ||
609 | static void dib0700_rc_urb_completion(struct urb *purb) | |
610 | { | |
611 | struct dvb_usb_device *d = purb->context; | |
d3c501d1 MCC |
612 | struct dib0700_rc_response *poll_reply; |
613 | u32 uninitialized_var(keycode); | |
72b39310 | 614 | u8 toggle; |
6a207100 DH |
615 | |
616 | deb_info("%s()\n", __func__); | |
617 | if (d == NULL) | |
618 | return; | |
619 | ||
d8b4b582 | 620 | if (d->rc_dev == NULL) { |
6a207100 DH |
621 | /* This will occur if disable_rc_polling=1 */ |
622 | usb_free_urb(purb); | |
623 | return; | |
624 | } | |
625 | ||
d3c501d1 | 626 | poll_reply = purb->transfer_buffer; |
6a207100 DH |
627 | |
628 | if (purb->status < 0) { | |
629 | deb_info("discontinuing polling\n"); | |
630 | usb_free_urb(purb); | |
631 | return; | |
632 | } | |
633 | ||
634 | if (purb->actual_length != RC_MSG_SIZE_V1_20) { | |
635 | deb_info("malformed rc msg size=%d\n", purb->actual_length); | |
636 | goto resubmit; | |
637 | } | |
638 | ||
d3c501d1 MCC |
639 | deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n", |
640 | poll_reply->report_id, poll_reply->data_state, | |
641 | poll_reply->system, poll_reply->not_system, | |
642 | poll_reply->data, poll_reply->not_data, | |
643 | purb->actual_length); | |
6a207100 | 644 | |
0ffd1ab3 | 645 | switch (d->props.rc.core.protocol) { |
52b66144 | 646 | case RC_TYPE_NEC: |
72b39310 | 647 | toggle = 0; |
6a207100 DH |
648 | |
649 | /* NEC protocol sends repeat code as 0 0 0 FF */ | |
d3c501d1 MCC |
650 | if ((poll_reply->system == 0x00) && (poll_reply->data == 0x00) |
651 | && (poll_reply->not_data == 0xff)) { | |
652 | poll_reply->data_state = 2; | |
6a207100 DH |
653 | break; |
654 | } | |
72b39310 | 655 | |
d3c501d1 MCC |
656 | if ((poll_reply->system ^ poll_reply->not_system) != 0xff) { |
657 | deb_data("NEC extended protocol\n"); | |
658 | /* NEC extended code - 24 bits */ | |
59aa3460 | 659 | keycode = be16_to_cpu(poll_reply->system16) << 8 | poll_reply->data; |
d3c501d1 MCC |
660 | } else { |
661 | deb_data("NEC normal protocol\n"); | |
662 | /* normal NEC code - 16 bits */ | |
663 | keycode = poll_reply->system << 8 | poll_reply->data; | |
664 | } | |
665 | ||
6a207100 DH |
666 | break; |
667 | default: | |
d3c501d1 | 668 | deb_data("RC5 protocol\n"); |
6a207100 | 669 | /* RC5 Protocol */ |
d3c501d1 | 670 | toggle = poll_reply->report_id; |
59aa3460 | 671 | keycode = poll_reply->system << 8 | poll_reply->data; |
72b39310 | 672 | |
6a207100 DH |
673 | break; |
674 | } | |
675 | ||
d3c501d1 | 676 | if ((poll_reply->data + poll_reply->not_data) != 0xff) { |
6a207100 DH |
677 | /* Key failed integrity check */ |
678 | err("key failed integrity check: %04x %02x %02x", | |
d3c501d1 MCC |
679 | poll_reply->system, |
680 | poll_reply->data, poll_reply->not_data); | |
6a207100 DH |
681 | goto resubmit; |
682 | } | |
683 | ||
ca86674b | 684 | rc_keydown(d->rc_dev, keycode, toggle); |
6a207100 DH |
685 | |
686 | resubmit: | |
687 | /* Clean the buffer before we requeue */ | |
688 | memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20); | |
689 | ||
690 | /* Requeue URB */ | |
691 | usb_submit_urb(purb, GFP_ATOMIC); | |
692 | } | |
693 | ||
58e6f95e | 694 | int dib0700_rc_setup(struct dvb_usb_device *d) |
89f4267d | 695 | { |
6a207100 | 696 | struct dib0700_state *st = d->priv; |
6a207100 DH |
697 | struct urb *purb; |
698 | int ret; | |
6a207100 | 699 | |
0ffd1ab3 | 700 | /* Poll-based. Don't initialize bulk mode */ |
6a207100 DH |
701 | if (st->fw_version < 0x10200) |
702 | return 0; | |
703 | ||
704 | /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */ | |
705 | purb = usb_alloc_urb(0, GFP_KERNEL); | |
706 | if (purb == NULL) { | |
707 | err("rc usb alloc urb failed\n"); | |
8871c85d | 708 | return -ENOMEM; |
6a207100 DH |
709 | } |
710 | ||
711 | purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL); | |
712 | if (purb->transfer_buffer == NULL) { | |
713 | err("rc kzalloc failed\n"); | |
714 | usb_free_urb(purb); | |
8871c85d | 715 | return -ENOMEM; |
6a207100 DH |
716 | } |
717 | ||
718 | purb->status = -EINPROGRESS; | |
719 | usb_fill_bulk_urb(purb, d->udev, usb_rcvbulkpipe(d->udev, 1), | |
720 | purb->transfer_buffer, RC_MSG_SIZE_V1_20, | |
721 | dib0700_rc_urb_completion, d); | |
722 | ||
723 | ret = usb_submit_urb(purb, GFP_ATOMIC); | |
8871c85d | 724 | if (ret) |
6a207100 | 725 | err("rc submit urb failed\n"); |
6a207100 | 726 | |
8871c85d | 727 | return ret; |
89f4267d JG |
728 | } |
729 | ||
b7f54910 PB |
730 | static int dib0700_probe(struct usb_interface *intf, |
731 | const struct usb_device_id *id) | |
732 | { | |
733 | int i; | |
89f4267d | 734 | struct dvb_usb_device *dev; |
b7f54910 PB |
735 | |
736 | for (i = 0; i < dib0700_device_count; i++) | |
78e92006 | 737 | if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, |
acc5c9ee OG |
738 | &dev, adapter_nr) == 0) { |
739 | struct dib0700_state *st = dev->priv; | |
740 | u32 hwversion, romversion, fw_version, fwtype; | |
741 | ||
742 | dib0700_get_version(dev, &hwversion, &romversion, | |
743 | &fw_version, &fwtype); | |
744 | ||
745 | deb_info("Firmware version: %x, %d, 0x%x, %d\n", | |
746 | hwversion, romversion, fw_version, fwtype); | |
747 | ||
748 | st->fw_version = fw_version; | |
749 | st->nb_packet_buffer_size = (u32)nb_packet_buffer_size; | |
750 | ||
72b39310 MCC |
751 | /* Disable polling mode on newer firmwares */ |
752 | if (st->fw_version >= 0x10200) | |
753 | dev->props.rc.core.bulk_mode = true; | |
754 | else | |
755 | dev->props.rc.core.bulk_mode = false; | |
756 | ||
89f4267d | 757 | dib0700_rc_setup(dev); |
acc5c9ee | 758 | |
b7f54910 | 759 | return 0; |
89f4267d | 760 | } |
b7f54910 PB |
761 | |
762 | return -ENODEV; | |
763 | } | |
764 | ||
765 | static struct usb_driver dib0700_driver = { | |
766 | .name = "dvb_usb_dib0700", | |
767 | .probe = dib0700_probe, | |
768 | .disconnect = dvb_usb_device_exit, | |
769 | .id_table = dib0700_usb_id_table, | |
770 | }; | |
771 | ||
772 | /* module stuff */ | |
773 | static int __init dib0700_module_init(void) | |
774 | { | |
775 | int result; | |
776 | info("loaded with support for %d different device-types", dib0700_device_count); | |
777 | if ((result = usb_register(&dib0700_driver))) { | |
778 | err("usb_register failed. Error number %d",result); | |
779 | return result; | |
780 | } | |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
785 | static void __exit dib0700_module_exit(void) | |
786 | { | |
787 | /* deregister this driver from the USB subsystem */ | |
788 | usb_deregister(&dib0700_driver); | |
789 | } | |
790 | ||
791 | module_init (dib0700_module_init); | |
792 | module_exit (dib0700_module_exit); | |
793 | ||
794 | MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); | |
795 | MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge"); | |
796 | MODULE_VERSION("1.0"); | |
797 | MODULE_LICENSE("GPL"); |