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
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 int dvb_usb_dib0700_ir_proto = 1;
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).");
20 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
23 int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
24 u32 *romversion, u32 *ramversion, u32 *fwtype)
27 int ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
29 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
30 b, sizeof(b), USB_CTRL_GET_TIMEOUT);
31 *hwversion = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
32 *romversion = (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | b[7];
33 *ramversion = (b[8] << 24) | (b[9] << 16) | (b[10] << 8) | b[11];
34 *fwtype = (b[12] << 24) | (b[13] << 16) | (b[14] << 8) | b[15];
38 /* expecting rx buffer: request data[0] data[1] ... data[2] */
39 static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
44 debug_dump(tx,txlen,deb_data);
46 status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
47 tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
48 USB_CTRL_GET_TIMEOUT);
51 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
53 return status < 0 ? status : 0;
56 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
57 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
63 err("tx buffer length is smaller than 2. Makes no sense.");
67 err("tx buffer length is larger than 4. Not supported.");
72 debug_dump(tx,txlen,deb_data);
74 value = ((txlen - 2) << 8) | tx[1];
77 index |= (tx[2] << 8);
81 status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
82 USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
83 USB_CTRL_GET_TIMEOUT);
86 deb_info("ep 0 read error (status = %d)\n",status);
89 debug_dump(rx,rxlen,deb_data);
91 return status; /* length in case of success */
94 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
96 u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
97 return dib0700_ctrl_wr(d,buf,3);
101 * I2C master xfer function (supported in 1.20 firmware)
103 static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
106 /* The new i2c firmware messages are more reliable and in particular
107 properly support i2c read calls not preceded by a write */
109 struct dvb_usb_device *d = i2c_get_adapdata(adap);
110 uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */
111 uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
112 uint8_t en_start = 0;
114 uint8_t buf[255]; /* TBV: malloc ? */
117 /* Ensure nobody else hits the i2c bus while we're sending our
118 sequence of messages, (such as the remote control thread) */
119 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
122 for (i = 0; i < num; i++) {
124 /* First message in the transaction */
126 } else if (!(msg[i].flags & I2C_M_NOSTART)) {
127 /* Device supports repeated-start */
130 /* Not the first packet and device doesn't support
134 if (i == (num - 1)) {
135 /* Last message in the transaction */
139 if (msg[i].flags & I2C_M_RD) {
144 i2c_dest = (msg[i].addr << 1);
145 value = ((en_start << 7) | (en_stop << 6) |
146 (msg[i].len & 0x3F)) << 8 | i2c_dest;
147 /* I2C ctrl + FE bus; */
148 index = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30);
150 result = usb_control_msg(d->udev,
151 usb_rcvctrlpipe(d->udev, 0),
152 REQUEST_NEW_I2C_READ,
153 USB_TYPE_VENDOR | USB_DIR_IN,
154 value, index, msg[i].buf,
156 USB_CTRL_GET_TIMEOUT);
158 err("i2c read error (status = %d)\n", result);
163 buf[0] = REQUEST_NEW_I2C_WRITE;
164 buf[1] = (msg[i].addr << 1);
165 buf[2] = (en_start << 7) | (en_stop << 6) |
167 /* I2C ctrl + FE bus; */
168 buf[3] = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30);
169 /* The Actual i2c payload */
170 memcpy(&buf[4], msg[i].buf, msg[i].len);
172 result = usb_control_msg(d->udev,
173 usb_sndctrlpipe(d->udev, 0),
174 REQUEST_NEW_I2C_WRITE,
175 USB_TYPE_VENDOR | USB_DIR_OUT,
176 0, 0, buf, msg[i].len + 4,
177 USB_CTRL_GET_TIMEOUT);
179 err("i2c write error (status = %d)\n", result);
184 mutex_unlock(&d->i2c_mutex);
189 * I2C master xfer function (pre-1.20 firmware)
191 static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
192 struct i2c_msg *msg, int num)
194 struct dvb_usb_device *d = i2c_get_adapdata(adap);
198 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
201 for (i = 0; i < num; i++) {
202 /* fill in the address */
203 buf[1] = (msg[i].addr << 1);
204 /* fill the buffer */
205 memcpy(&buf[2], msg[i].buf, msg[i].len);
207 /* write/read request */
208 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
209 buf[0] = REQUEST_I2C_READ;
212 /* special thing in the current firmware: when length is zero the read-failed */
213 if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
214 deb_info("I2C read failed on address %x\n", msg[i].addr);
222 buf[0] = REQUEST_I2C_WRITE;
223 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
228 mutex_unlock(&d->i2c_mutex);
232 static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
235 struct dvb_usb_device *d = i2c_get_adapdata(adap);
236 struct dib0700_state *st = d->priv;
238 if (st->fw_use_new_i2c_api == 1) {
239 /* User running at least fw 1.20 */
240 return dib0700_i2c_xfer_new(adap, msg, num);
242 /* Use legacy calls */
243 return dib0700_i2c_xfer_legacy(adap, msg, num);
247 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
252 struct i2c_algorithm dib0700_i2c_algo = {
253 .master_xfer = dib0700_i2c_xfer,
254 .functionality = dib0700_i2c_func,
257 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
258 struct dvb_usb_device_description **desc, int *cold)
261 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
262 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
264 deb_info("FW GET_VERSION length: %d\n",ret);
268 deb_info("cold: %d\n", *cold);
272 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
273 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
274 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
277 b[0] = REQUEST_SET_CLOCK;
278 b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
279 b[2] = (pll_prediv >> 8) & 0xff; // MSB
280 b[3] = pll_prediv & 0xff; // LSB
281 b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
282 b[5] = pll_loopdiv & 0xff; // LSB
283 b[6] = (free_div >> 8) & 0xff; // MSB
284 b[7] = free_div & 0xff; // LSB
285 b[8] = (dsuScaler >> 8) & 0xff; // MSB
286 b[9] = dsuScaler & 0xff; // LSB
288 return dib0700_ctrl_wr(d, b, 10);
291 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
294 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
295 default: return -EINVAL;
300 static int dib0700_jumpram(struct usb_device *udev, u32 address)
303 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
304 (address >> 24) & 0xff,
305 (address >> 16) & 0xff,
306 (address >> 8) & 0xff,
309 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
310 deb_fw("jumpram to 0x%x failed\n",address);
314 deb_fw("jumpram to 0x%x failed\n",address);
320 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
323 int pos = 0, ret, act_len;
327 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
328 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
331 buf[1] = (hx.addr >> 8) & 0xff;
332 buf[2] = hx.addr & 0xff;
334 memcpy(&buf[4],hx.data,hx.len);
335 buf[4+hx.len] = hx.chk;
337 ret = usb_bulk_msg(udev,
338 usb_sndbulkpipe(udev, 0x01),
345 err("firmware download failed at %d with %d",pos,ret);
351 /* start the firmware */
352 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
353 info("firmware started successfully.");
362 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
364 struct dib0700_state *st = adap->dev->priv;
367 b[0] = REQUEST_ENABLE_VIDEO;
368 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
370 if (st->disable_streaming_master_mode == 1)
373 b[2] = (0x01 << 4); /* Master mode */
377 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
380 st->channel_state |= 1 << adap->id;
382 st->channel_state &= ~(1 << adap->id);
384 b[2] |= st->channel_state;
386 deb_info("data for streaming: %x %x\n",b[1],b[2]);
388 return dib0700_ctrl_wr(adap->dev, b, 4);
391 int dib0700_rc_setup(struct dvb_usb_device *d)
393 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
394 int i = dib0700_ctrl_wr(d, rc_setup, 3);
396 err("ir protocol setup failed");
402 static int dib0700_probe(struct usb_interface *intf,
403 const struct usb_device_id *id)
406 struct dvb_usb_device *dev;
408 for (i = 0; i < dib0700_device_count; i++)
409 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
410 &dev, adapter_nr) == 0)
412 dib0700_rc_setup(dev);
419 static struct usb_driver dib0700_driver = {
420 .name = "dvb_usb_dib0700",
421 .probe = dib0700_probe,
422 .disconnect = dvb_usb_device_exit,
423 .id_table = dib0700_usb_id_table,
427 static int __init dib0700_module_init(void)
430 info("loaded with support for %d different device-types", dib0700_device_count);
431 if ((result = usb_register(&dib0700_driver))) {
432 err("usb_register failed. Error number %d",result);
439 static void __exit dib0700_module_exit(void)
441 /* deregister this driver from the USB subsystem */
442 usb_deregister(&dib0700_driver);
445 module_init (dib0700_module_init);
446 module_exit (dib0700_module_exit);
448 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
449 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
450 MODULE_VERSION("1.0");
451 MODULE_LICENSE("GPL");