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