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