powerpc/mm/4k: don't allocate larger pmd page table for 4k
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb / dib0700_core.c
blobdd5edd3a17ee4deddd769da0325b9a99de161670
1 /* Linux driver for devices based on the DiBcom DiB0700 USB bridge
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.
7 * Copyright (C) 2005-6 DiBcom, SA
8 */
9 #include "dib0700.h"
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);
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 corresponds to the number of TS packets. The actual size of the data buffer corresponds to this parameter multiplied by 188 (default: 21)");
21 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
24 int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
25 u32 *romversion, u32 *ramversion, u32 *fwtype)
27 struct dib0700_state *st = d->priv;
28 int ret;
30 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
31 err("could not acquire lock");
32 return -EINTR;
35 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 st->buf, 16, USB_CTRL_GET_TIMEOUT);
39 if (hwversion != NULL)
40 *hwversion = (st->buf[0] << 24) | (st->buf[1] << 16) |
41 (st->buf[2] << 8) | st->buf[3];
42 if (romversion != NULL)
43 *romversion = (st->buf[4] << 24) | (st->buf[5] << 16) |
44 (st->buf[6] << 8) | st->buf[7];
45 if (ramversion != NULL)
46 *ramversion = (st->buf[8] << 24) | (st->buf[9] << 16) |
47 (st->buf[10] << 8) | st->buf[11];
48 if (fwtype != NULL)
49 *fwtype = (st->buf[12] << 24) | (st->buf[13] << 16) |
50 (st->buf[14] << 8) | st->buf[15];
51 mutex_unlock(&d->usb_mutex);
52 return ret;
55 /* expecting rx buffer: request data[0] data[1] ... data[2] */
56 static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
58 int status;
60 deb_data(">>> ");
61 debug_dump(tx, txlen, deb_data);
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);
67 if (status != txlen)
68 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
70 return status < 0 ? status : 0;
73 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
74 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
76 u16 index, value;
77 int status;
79 if (txlen < 2) {
80 err("tx buffer length is smaller than 2. Makes no sense.");
81 return -EINVAL;
83 if (txlen > 4) {
84 err("tx buffer length is larger than 4. Not supported.");
85 return -EINVAL;
88 deb_data(">>> ");
89 debug_dump(tx,txlen,deb_data);
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];
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);
102 if (status < 0)
103 deb_info("ep 0 read error (status = %d)\n",status);
105 deb_data("<<< ");
106 debug_dump(rx, rxlen, deb_data);
108 return status; /* length in case of success */
111 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
113 struct dib0700_state *st = d->priv;
114 int ret;
116 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
117 err("could not acquire lock");
118 return -EINTR;
121 st->buf[0] = REQUEST_SET_GPIO;
122 st->buf[1] = gpio;
123 st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6);
125 ret = dib0700_ctrl_wr(d, st->buf, 3);
127 mutex_unlock(&d->usb_mutex);
128 return ret;
131 static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets)
133 struct dib0700_state *st = d->priv;
134 int ret;
136 if (st->fw_version >= 0x10201) {
137 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
138 err("could not acquire lock");
139 return -EINTR;
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;
146 deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets);
148 ret = dib0700_ctrl_wr(d, st->buf, 3);
149 mutex_unlock(&d->usb_mutex);
150 } else {
151 deb_info("this firmware does not allow to change the USB xfer len\n");
152 ret = -EIO;
155 return ret;
159 * I2C master xfer function (supported in 1.20 firmware)
161 static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
162 int num)
164 /* The new i2c firmware messages are more reliable and in particular
165 properly support i2c read calls not preceded by a write */
167 struct dvb_usb_device *d = i2c_get_adapdata(adap);
168 struct dib0700_state *st = d->priv;
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;
173 int result, i;
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)
178 return -EINTR;
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;
192 if (i == (num - 1)) {
193 /* Last message in the transaction */
194 en_stop = 1;
197 if (msg[i].flags & I2C_M_RD) {
198 /* Read request */
199 u16 index, value;
200 uint8_t i2c_dest;
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; */
206 index = ((gen_mode << 6) & 0xC0) |
207 ((bus_mode << 4) & 0x30);
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,
213 value, index, st->buf,
214 msg[i].len,
215 USB_CTRL_GET_TIMEOUT);
216 if (result < 0) {
217 deb_info("i2c read error (status = %d)\n", result);
218 break;
221 if (msg[i].len > sizeof(st->buf)) {
222 deb_info("buffer too small to fit %d bytes\n",
223 msg[i].len);
224 return -EIO;
227 memcpy(msg[i].buf, st->buf, msg[i].len);
229 deb_data("<<< ");
230 debug_dump(msg[i].buf, msg[i].len, deb_data);
232 } else {
233 /* Write request */
234 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
235 err("could not acquire lock");
236 mutex_unlock(&d->i2c_mutex);
237 return -EINTR;
239 st->buf[0] = REQUEST_NEW_I2C_WRITE;
240 st->buf[1] = msg[i].addr << 1;
241 st->buf[2] = (en_start << 7) | (en_stop << 6) |
242 (msg[i].len & 0x3F);
243 /* I2C ctrl + FE bus; */
244 st->buf[3] = ((gen_mode << 6) & 0xC0) |
245 ((bus_mode << 4) & 0x30);
247 if (msg[i].len > sizeof(st->buf) - 4) {
248 deb_info("i2c message to big: %d\n",
249 msg[i].len);
250 return -EIO;
253 /* The Actual i2c payload */
254 memcpy(&st->buf[4], msg[i].buf, msg[i].len);
256 deb_data(">>> ");
257 debug_dump(st->buf, msg[i].len + 4, deb_data);
259 result = usb_control_msg(d->udev,
260 usb_sndctrlpipe(d->udev, 0),
261 REQUEST_NEW_I2C_WRITE,
262 USB_TYPE_VENDOR | USB_DIR_OUT,
263 0, 0, st->buf, msg[i].len + 4,
264 USB_CTRL_GET_TIMEOUT);
265 mutex_unlock(&d->usb_mutex);
266 if (result < 0) {
267 deb_info("i2c write error (status = %d)\n", result);
268 break;
272 mutex_unlock(&d->i2c_mutex);
273 return i;
277 * I2C master xfer function (pre-1.20 firmware)
279 static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
280 struct i2c_msg *msg, int num)
282 struct dvb_usb_device *d = i2c_get_adapdata(adap);
283 struct dib0700_state *st = d->priv;
284 int i,len;
286 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
287 return -EINTR;
288 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
289 err("could not acquire lock");
290 mutex_unlock(&d->i2c_mutex);
291 return -EINTR;
294 for (i = 0; i < num; i++) {
295 /* fill in the address */
296 st->buf[1] = msg[i].addr << 1;
297 /* fill the buffer */
298 if (msg[i].len > sizeof(st->buf) - 2) {
299 deb_info("i2c xfer to big: %d\n",
300 msg[i].len);
301 return -EIO;
303 memcpy(&st->buf[2], msg[i].buf, msg[i].len);
305 /* write/read request */
306 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
307 st->buf[0] = REQUEST_I2C_READ;
308 st->buf[1] |= 1;
310 /* special thing in the current firmware: when length is zero the read-failed */
311 len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
312 st->buf, msg[i + 1].len);
313 if (len <= 0) {
314 deb_info("I2C read failed on address 0x%02x\n",
315 msg[i].addr);
316 break;
319 if (msg[i + 1].len > sizeof(st->buf)) {
320 deb_info("i2c xfer buffer to small for %d\n",
321 msg[i].len);
322 return -EIO;
324 memcpy(msg[i + 1].buf, st->buf, msg[i + 1].len);
326 msg[i+1].len = len;
328 i++;
329 } else {
330 st->buf[0] = REQUEST_I2C_WRITE;
331 if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0)
332 break;
335 mutex_unlock(&d->usb_mutex);
336 mutex_unlock(&d->i2c_mutex);
338 return i;
341 static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
342 int num)
344 struct dvb_usb_device *d = i2c_get_adapdata(adap);
345 struct dib0700_state *st = d->priv;
347 if (st->fw_use_new_i2c_api == 1) {
348 /* User running at least fw 1.20 */
349 return dib0700_i2c_xfer_new(adap, msg, num);
350 } else {
351 /* Use legacy calls */
352 return dib0700_i2c_xfer_legacy(adap, msg, num);
356 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
358 return I2C_FUNC_I2C;
361 struct i2c_algorithm dib0700_i2c_algo = {
362 .master_xfer = dib0700_i2c_xfer,
363 .functionality = dib0700_i2c_func,
366 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
367 struct dvb_usb_device_description **desc, int *cold)
369 s16 ret;
370 u8 *b;
372 b = kmalloc(16, GFP_KERNEL);
373 if (!b)
374 return -ENOMEM;
377 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
378 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
380 deb_info("FW GET_VERSION length: %d\n",ret);
382 *cold = ret <= 0;
383 deb_info("cold: %d\n", *cold);
385 kfree(b);
386 return 0;
389 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
390 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
391 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
393 struct dib0700_state *st = d->priv;
394 int ret;
396 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
397 err("could not acquire lock");
398 return -EINTR;
401 st->buf[0] = REQUEST_SET_CLOCK;
402 st->buf[1] = (en_pll << 7) | (pll_src << 6) |
403 (pll_range << 5) | (clock_gpio3 << 4);
404 st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */
405 st->buf[3] = pll_prediv & 0xff; /* LSB */
406 st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
407 st->buf[5] = pll_loopdiv & 0xff; /* LSB */
408 st->buf[6] = (free_div >> 8) & 0xff; /* MSB */
409 st->buf[7] = free_div & 0xff; /* LSB */
410 st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */
411 st->buf[9] = dsuScaler & 0xff; /* LSB */
413 ret = dib0700_ctrl_wr(d, st->buf, 10);
414 mutex_unlock(&d->usb_mutex);
416 return ret;
419 int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
421 struct dib0700_state *st = d->priv;
422 u16 divider;
423 int ret;
425 if (scl_kHz == 0)
426 return -EINVAL;
428 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
429 err("could not acquire lock");
430 return -EINTR;
433 st->buf[0] = REQUEST_SET_I2C_PARAM;
434 divider = (u16) (30000 / scl_kHz);
435 st->buf[1] = 0;
436 st->buf[2] = (u8) (divider >> 8);
437 st->buf[3] = (u8) (divider & 0xff);
438 divider = (u16) (72000 / scl_kHz);
439 st->buf[4] = (u8) (divider >> 8);
440 st->buf[5] = (u8) (divider & 0xff);
441 divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
442 st->buf[6] = (u8) (divider >> 8);
443 st->buf[7] = (u8) (divider & 0xff);
445 deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
446 (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
447 st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
449 ret = dib0700_ctrl_wr(d, st->buf, 8);
450 mutex_unlock(&d->usb_mutex);
452 return ret;
456 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
458 switch (clk_MHz) {
459 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
460 default: return -EINVAL;
462 return 0;
465 static int dib0700_jumpram(struct usb_device *udev, u32 address)
467 int ret = 0, actlen;
468 u8 *buf;
470 buf = kmalloc(8, GFP_KERNEL);
471 if (!buf)
472 return -ENOMEM;
473 buf[0] = REQUEST_JUMPRAM;
474 buf[1] = 0;
475 buf[2] = 0;
476 buf[3] = 0;
477 buf[4] = (address >> 24) & 0xff;
478 buf[5] = (address >> 16) & 0xff;
479 buf[6] = (address >> 8) & 0xff;
480 buf[7] = address & 0xff;
482 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
483 deb_fw("jumpram to 0x%x failed\n",address);
484 goto out;
486 if (actlen != 8) {
487 deb_fw("jumpram to 0x%x failed\n",address);
488 ret = -EIO;
489 goto out;
491 out:
492 kfree(buf);
493 return ret;
496 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
498 struct hexline hx;
499 int pos = 0, ret, act_len, i, adap_num;
500 u8 *buf;
501 u32 fw_version;
503 buf = kmalloc(260, GFP_KERNEL);
504 if (!buf)
505 return -ENOMEM;
507 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
508 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
509 hx.addr, hx.len, hx.chk);
511 buf[0] = hx.len;
512 buf[1] = (hx.addr >> 8) & 0xff;
513 buf[2] = hx.addr & 0xff;
514 buf[3] = hx.type;
515 memcpy(&buf[4],hx.data,hx.len);
516 buf[4+hx.len] = hx.chk;
518 ret = usb_bulk_msg(udev,
519 usb_sndbulkpipe(udev, 0x01),
520 buf,
521 hx.len + 5,
522 &act_len,
523 1000);
525 if (ret < 0) {
526 err("firmware download failed at %d with %d",pos,ret);
527 goto out;
531 if (ret == 0) {
532 /* start the firmware */
533 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
534 info("firmware started successfully.");
535 msleep(500);
537 } else
538 ret = -EIO;
540 /* the number of ts packet has to be at least 1 */
541 if (nb_packet_buffer_size < 1)
542 nb_packet_buffer_size = 1;
544 /* get the firmware version */
545 usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
546 REQUEST_GET_VERSION,
547 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
548 buf, 16, USB_CTRL_GET_TIMEOUT);
549 fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
551 /* set the buffer size - DVB-USB is allocating URB buffers
552 * only after the firwmare download was successful */
553 for (i = 0; i < dib0700_device_count; i++) {
554 for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
555 adap_num++) {
556 if (fw_version >= 0x10201) {
557 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
558 } else {
559 /* for fw version older than 1.20.1,
560 * the buffersize has to be n times 512 */
561 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
562 if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512)
563 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512;
567 out:
568 kfree(buf);
569 return ret;
572 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
574 struct dib0700_state *st = adap->dev->priv;
575 int ret;
577 if ((onoff != 0) && (st->fw_version >= 0x10201)) {
578 /* for firmware later than 1.20.1,
579 * the USB xfer length can be set */
580 ret = dib0700_set_usb_xfer_len(adap->dev,
581 st->nb_packet_buffer_size);
582 if (ret < 0) {
583 deb_info("can not set the USB xfer len\n");
584 return ret;
588 mutex_lock(&adap->dev->usb_mutex);
590 st->buf[0] = REQUEST_ENABLE_VIDEO;
591 /* this bit gives a kind of command,
592 * rather than enabling something or not */
593 st->buf[1] = (onoff << 4) | 0x00;
595 if (st->disable_streaming_master_mode == 1)
596 st->buf[2] = 0x00;
597 else
598 st->buf[2] = 0x01 << 4; /* Master mode */
600 st->buf[3] = 0x00;
602 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
604 st->channel_state &= ~0x3;
605 if ((adap->fe_adap[0].stream.props.endpoint != 2)
606 && (adap->fe_adap[0].stream.props.endpoint != 3)) {
607 deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->fe_adap[0].stream.props.endpoint);
608 if (onoff)
609 st->channel_state |= 1 << (adap->id);
610 else
611 st->channel_state |= 1 << ~(adap->id);
612 } else {
613 if (onoff)
614 st->channel_state |= 1 << (adap->fe_adap[0].stream.props.endpoint-2);
615 else
616 st->channel_state |= 1 << (3-adap->fe_adap[0].stream.props.endpoint);
619 st->buf[2] |= st->channel_state;
621 deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]);
623 ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
624 mutex_unlock(&adap->dev->usb_mutex);
626 return ret;
629 int dib0700_change_protocol(struct rc_dev *rc, u64 *rc_type)
631 struct dvb_usb_device *d = rc->priv;
632 struct dib0700_state *st = d->priv;
633 int new_proto, ret;
635 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
636 err("could not acquire lock");
637 return -EINTR;
640 st->buf[0] = REQUEST_SET_RC;
641 st->buf[1] = 0;
642 st->buf[2] = 0;
644 /* Set the IR mode */
645 if (*rc_type & RC_BIT_RC5) {
646 new_proto = 1;
647 *rc_type = RC_BIT_RC5;
648 } else if (*rc_type & RC_BIT_NEC) {
649 new_proto = 0;
650 *rc_type = RC_BIT_NEC;
651 } else if (*rc_type & RC_BIT_RC6_MCE) {
652 if (st->fw_version < 0x10200) {
653 ret = -EINVAL;
654 goto out;
656 new_proto = 2;
657 *rc_type = RC_BIT_RC6_MCE;
658 } else {
659 ret = -EINVAL;
660 goto out;
663 st->buf[1] = new_proto;
665 ret = dib0700_ctrl_wr(d, st->buf, 3);
666 if (ret < 0) {
667 err("ir protocol setup failed");
668 goto out;
671 d->props.rc.core.protocol = *rc_type;
673 out:
674 mutex_unlock(&d->usb_mutex);
675 return ret;
678 /* This is the structure of the RC response packet starting in firmware 1.20 */
679 struct dib0700_rc_response {
680 u8 report_id;
681 u8 data_state;
682 union {
683 struct {
684 u8 system;
685 u8 not_system;
686 u8 data;
687 u8 not_data;
688 } nec;
689 struct {
690 u8 not_used;
691 u8 system;
692 u8 data;
693 u8 not_data;
694 } rc5;
697 #define RC_MSG_SIZE_V1_20 6
699 static void dib0700_rc_urb_completion(struct urb *purb)
701 struct dvb_usb_device *d = purb->context;
702 struct dib0700_rc_response *poll_reply;
703 enum rc_type protocol;
704 u32 keycode;
705 u8 toggle;
707 deb_info("%s()\n", __func__);
708 if (d->rc_dev == NULL) {
709 /* This will occur if disable_rc_polling=1 */
710 kfree(purb->transfer_buffer);
711 usb_free_urb(purb);
712 return;
715 poll_reply = purb->transfer_buffer;
717 if (purb->status < 0) {
718 deb_info("discontinuing polling\n");
719 kfree(purb->transfer_buffer);
720 usb_free_urb(purb);
721 return;
724 if (purb->actual_length != RC_MSG_SIZE_V1_20) {
725 deb_info("malformed rc msg size=%d\n", purb->actual_length);
726 goto resubmit;
729 deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
730 poll_reply->report_id, poll_reply->data_state,
731 poll_reply->nec.system, poll_reply->nec.not_system,
732 poll_reply->nec.data, poll_reply->nec.not_data,
733 purb->actual_length);
735 switch (d->props.rc.core.protocol) {
736 case RC_BIT_NEC:
737 toggle = 0;
739 /* NEC protocol sends repeat code as 0 0 0 FF */
740 if (poll_reply->nec.system == 0x00 &&
741 poll_reply->nec.not_system == 0x00 &&
742 poll_reply->nec.data == 0x00 &&
743 poll_reply->nec.not_data == 0xff) {
744 poll_reply->data_state = 2;
745 rc_repeat(d->rc_dev);
746 goto resubmit;
749 if ((poll_reply->nec.data ^ poll_reply->nec.not_data) != 0xff) {
750 deb_data("NEC32 protocol\n");
751 keycode = RC_SCANCODE_NEC32(poll_reply->nec.system << 24 |
752 poll_reply->nec.not_system << 16 |
753 poll_reply->nec.data << 8 |
754 poll_reply->nec.not_data);
755 protocol = RC_TYPE_NEC32;
756 } else if ((poll_reply->nec.system ^ poll_reply->nec.not_system) != 0xff) {
757 deb_data("NEC extended protocol\n");
758 keycode = RC_SCANCODE_NECX(poll_reply->nec.system << 8 |
759 poll_reply->nec.not_system,
760 poll_reply->nec.data);
762 protocol = RC_TYPE_NECX;
763 } else {
764 deb_data("NEC normal protocol\n");
765 keycode = RC_SCANCODE_NEC(poll_reply->nec.system,
766 poll_reply->nec.data);
767 protocol = RC_TYPE_NEC;
770 break;
771 default:
772 deb_data("RC5 protocol\n");
773 protocol = RC_TYPE_RC5;
774 toggle = poll_reply->report_id;
775 keycode = RC_SCANCODE_RC5(poll_reply->rc5.system, poll_reply->rc5.data);
777 if ((poll_reply->rc5.data ^ poll_reply->rc5.not_data) != 0xff) {
778 /* Key failed integrity check */
779 err("key failed integrity check: %02x %02x %02x %02x",
780 poll_reply->rc5.not_used, poll_reply->rc5.system,
781 poll_reply->rc5.data, poll_reply->rc5.not_data);
782 goto resubmit;
785 break;
788 rc_keydown(d->rc_dev, protocol, keycode, toggle);
790 resubmit:
791 /* Clean the buffer before we requeue */
792 memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);
794 /* Requeue URB */
795 usb_submit_urb(purb, GFP_ATOMIC);
798 int dib0700_rc_setup(struct dvb_usb_device *d, struct usb_interface *intf)
800 struct dib0700_state *st = d->priv;
801 struct urb *purb;
802 const struct usb_endpoint_descriptor *e;
803 int ret, rc_ep = 1;
804 unsigned int pipe = 0;
806 /* Poll-based. Don't initialize bulk mode */
807 if (st->fw_version < 0x10200 || !intf)
808 return 0;
810 /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
812 purb = usb_alloc_urb(0, GFP_KERNEL);
813 if (purb == NULL)
814 return -ENOMEM;
816 purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
817 if (purb->transfer_buffer == NULL) {
818 err("rc kzalloc failed");
819 usb_free_urb(purb);
820 return -ENOMEM;
823 purb->status = -EINPROGRESS;
826 * Some devices like the Hauppauge NovaTD model 52009 use an interrupt
827 * endpoint, while others use a bulk one.
829 e = &intf->altsetting[0].endpoint[rc_ep].desc;
830 if (usb_endpoint_dir_in(e)) {
831 if (usb_endpoint_xfer_bulk(e)) {
832 pipe = usb_rcvbulkpipe(d->udev, rc_ep);
833 usb_fill_bulk_urb(purb, d->udev, pipe,
834 purb->transfer_buffer,
835 RC_MSG_SIZE_V1_20,
836 dib0700_rc_urb_completion, d);
838 } else if (usb_endpoint_xfer_int(e)) {
839 pipe = usb_rcvintpipe(d->udev, rc_ep);
840 usb_fill_int_urb(purb, d->udev, pipe,
841 purb->transfer_buffer,
842 RC_MSG_SIZE_V1_20,
843 dib0700_rc_urb_completion, d, 1);
847 if (!pipe) {
848 err("There's no endpoint for remote controller");
849 kfree(purb->transfer_buffer);
850 usb_free_urb(purb);
851 return 0;
854 ret = usb_submit_urb(purb, GFP_ATOMIC);
855 if (ret) {
856 err("rc submit urb failed");
857 kfree(purb->transfer_buffer);
858 usb_free_urb(purb);
861 return ret;
864 static int dib0700_probe(struct usb_interface *intf,
865 const struct usb_device_id *id)
867 int i;
868 struct dvb_usb_device *dev;
870 for (i = 0; i < dib0700_device_count; i++)
871 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
872 &dev, adapter_nr) == 0) {
873 struct dib0700_state *st = dev->priv;
874 u32 hwversion, romversion, fw_version, fwtype;
876 dib0700_get_version(dev, &hwversion, &romversion,
877 &fw_version, &fwtype);
879 deb_info("Firmware version: %x, %d, 0x%x, %d\n",
880 hwversion, romversion, fw_version, fwtype);
882 st->fw_version = fw_version;
883 st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;
885 /* Disable polling mode on newer firmwares */
886 if (st->fw_version >= 0x10200)
887 dev->props.rc.core.bulk_mode = true;
888 else
889 dev->props.rc.core.bulk_mode = false;
891 dib0700_rc_setup(dev, intf);
893 return 0;
896 return -ENODEV;
899 static struct usb_driver dib0700_driver = {
900 .name = "dvb_usb_dib0700",
901 .probe = dib0700_probe,
902 .disconnect = dvb_usb_device_exit,
903 .id_table = dib0700_usb_id_table,
906 module_usb_driver(dib0700_driver);
908 MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw");
909 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
910 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
911 MODULE_VERSION("1.0");
912 MODULE_LICENSE("GPL");