First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / media / dvb / dvb-usb / dib0700_core.c
blobdb7f7f79a66ce5796998629fe63ab2e812f34124
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 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)
26 u8 b[16];
27 int ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
28 REQUEST_GET_VERSION,
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];
35 return ret;
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)
41 int status;
43 deb_data(">>> ");
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);
50 if (status != txlen)
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)
59 u16 index, value;
60 int status;
62 if (txlen < 2) {
63 err("tx buffer length is smaller than 2. Makes no sense.");
64 return -EINVAL;
66 if (txlen > 4) {
67 err("tx buffer length is larger than 4. Not supported.");
68 return -EINVAL;
71 deb_data(">>> ");
72 debug_dump(tx,txlen,deb_data);
74 value = ((txlen - 2) << 8) | tx[1];
75 index = 0;
76 if (txlen > 2)
77 index |= (tx[2] << 8);
78 if (txlen > 3)
79 index |= tx[3];
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);
85 if (status < 0)
86 deb_info("ep 0 read error (status = %d)\n",status);
88 deb_data("<<< ");
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,
104 int num)
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;
113 uint8_t en_stop = 0;
114 uint8_t buf[255]; /* TBV: malloc ? */
115 int result, i;
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)
120 return -EAGAIN;
122 for (i = 0; i < num; i++) {
123 if (i == 0) {
124 /* First message in the transaction */
125 en_start = 1;
126 } else if (!(msg[i].flags & I2C_M_NOSTART)) {
127 /* Device supports repeated-start */
128 en_start = 1;
129 } else {
130 /* Not the first packet and device doesn't support
131 repeated start */
132 en_start = 0;
134 if (i == (num - 1)) {
135 /* Last message in the transaction */
136 en_stop = 1;
139 if (msg[i].flags & I2C_M_RD) {
140 /* Read request */
141 u16 index, value;
142 uint8_t i2c_dest;
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,
155 msg[i].len,
156 USB_CTRL_GET_TIMEOUT);
157 if (result < 0) {
158 err("i2c read error (status = %d)\n", result);
159 break;
162 deb_data("<<< ");
163 debug_dump(msg[i].buf, msg[i].len, deb_data);
165 } else {
166 /* Write request */
167 buf[0] = REQUEST_NEW_I2C_WRITE;
168 buf[1] = (msg[i].addr << 1);
169 buf[2] = (en_start << 7) | (en_stop << 6) |
170 (msg[i].len & 0x3F);
171 /* I2C ctrl + FE bus; */
172 buf[3] = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30);
173 /* The Actual i2c payload */
174 memcpy(&buf[4], msg[i].buf, msg[i].len);
176 deb_data(">>> ");
177 debug_dump(buf, msg[i].len + 4, deb_data);
179 result = usb_control_msg(d->udev,
180 usb_sndctrlpipe(d->udev, 0),
181 REQUEST_NEW_I2C_WRITE,
182 USB_TYPE_VENDOR | USB_DIR_OUT,
183 0, 0, buf, msg[i].len + 4,
184 USB_CTRL_GET_TIMEOUT);
185 if (result < 0) {
186 err("i2c write error (status = %d)\n", result);
187 break;
191 mutex_unlock(&d->i2c_mutex);
192 return i;
196 * I2C master xfer function (pre-1.20 firmware)
198 static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
199 struct i2c_msg *msg, int num)
201 struct dvb_usb_device *d = i2c_get_adapdata(adap);
202 int i,len;
203 u8 buf[255];
205 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
206 return -EAGAIN;
208 for (i = 0; i < num; i++) {
209 /* fill in the address */
210 buf[1] = (msg[i].addr << 1);
211 /* fill the buffer */
212 memcpy(&buf[2], msg[i].buf, msg[i].len);
214 /* write/read request */
215 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
216 buf[0] = REQUEST_I2C_READ;
217 buf[1] |= 1;
219 /* special thing in the current firmware: when length is zero the read-failed */
220 if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
221 deb_info("I2C read failed on address 0x%02x\n",
222 msg[i].addr);
223 break;
226 msg[i+1].len = len;
228 i++;
229 } else {
230 buf[0] = REQUEST_I2C_WRITE;
231 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
232 break;
236 mutex_unlock(&d->i2c_mutex);
237 return i;
240 static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
241 int num)
243 struct dvb_usb_device *d = i2c_get_adapdata(adap);
244 struct dib0700_state *st = d->priv;
246 if (st->fw_use_new_i2c_api == 1) {
247 /* User running at least fw 1.20 */
248 return dib0700_i2c_xfer_new(adap, msg, num);
249 } else {
250 /* Use legacy calls */
251 return dib0700_i2c_xfer_legacy(adap, msg, num);
255 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
257 return I2C_FUNC_I2C;
260 struct i2c_algorithm dib0700_i2c_algo = {
261 .master_xfer = dib0700_i2c_xfer,
262 .functionality = dib0700_i2c_func,
265 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
266 struct dvb_usb_device_description **desc, int *cold)
268 u8 b[16];
269 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
270 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
272 deb_info("FW GET_VERSION length: %d\n",ret);
274 *cold = ret <= 0;
276 deb_info("cold: %d\n", *cold);
277 return 0;
280 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
281 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
282 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
284 u8 b[10];
285 b[0] = REQUEST_SET_CLOCK;
286 b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
287 b[2] = (pll_prediv >> 8) & 0xff; // MSB
288 b[3] = pll_prediv & 0xff; // LSB
289 b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
290 b[5] = pll_loopdiv & 0xff; // LSB
291 b[6] = (free_div >> 8) & 0xff; // MSB
292 b[7] = free_div & 0xff; // LSB
293 b[8] = (dsuScaler >> 8) & 0xff; // MSB
294 b[9] = dsuScaler & 0xff; // LSB
296 return dib0700_ctrl_wr(d, b, 10);
299 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
301 switch (clk_MHz) {
302 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
303 default: return -EINVAL;
305 return 0;
308 static int dib0700_jumpram(struct usb_device *udev, u32 address)
310 int ret, actlen;
311 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
312 (address >> 24) & 0xff,
313 (address >> 16) & 0xff,
314 (address >> 8) & 0xff,
315 address & 0xff };
317 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
318 deb_fw("jumpram to 0x%x failed\n",address);
319 return ret;
321 if (actlen != 8) {
322 deb_fw("jumpram to 0x%x failed\n",address);
323 return -EIO;
325 return 0;
328 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
330 struct hexline hx;
331 int pos = 0, ret, act_len;
333 u8 buf[260];
335 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
336 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
338 buf[0] = hx.len;
339 buf[1] = (hx.addr >> 8) & 0xff;
340 buf[2] = hx.addr & 0xff;
341 buf[3] = hx.type;
342 memcpy(&buf[4],hx.data,hx.len);
343 buf[4+hx.len] = hx.chk;
345 ret = usb_bulk_msg(udev,
346 usb_sndbulkpipe(udev, 0x01),
347 buf,
348 hx.len + 5,
349 &act_len,
350 1000);
352 if (ret < 0) {
353 err("firmware download failed at %d with %d",pos,ret);
354 return ret;
358 if (ret == 0) {
359 /* start the firmware */
360 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
361 info("firmware started successfully.");
362 msleep(500);
364 } else
365 ret = -EIO;
367 return ret;
370 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
372 struct dib0700_state *st = adap->dev->priv;
373 u8 b[4];
375 b[0] = REQUEST_ENABLE_VIDEO;
376 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
378 if (st->disable_streaming_master_mode == 1)
379 b[2] = 0x00;
380 else
381 b[2] = (0x01 << 4); /* Master mode */
383 b[3] = 0x00;
385 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
387 if (onoff)
388 st->channel_state |= 1 << adap->id;
389 else
390 st->channel_state &= ~(1 << adap->id);
392 b[2] |= st->channel_state;
394 deb_info("data for streaming: %x %x\n",b[1],b[2]);
396 return dib0700_ctrl_wr(adap->dev, b, 4);
399 int dib0700_rc_setup(struct dvb_usb_device *d)
401 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
402 int i = dib0700_ctrl_wr(d, rc_setup, 3);
403 if (i<0) {
404 err("ir protocol setup failed");
405 return -1;
407 return 0;
410 static int dib0700_probe(struct usb_interface *intf,
411 const struct usb_device_id *id)
413 int i;
414 struct dvb_usb_device *dev;
416 for (i = 0; i < dib0700_device_count; i++)
417 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
418 &dev, adapter_nr) == 0)
420 dib0700_rc_setup(dev);
421 return 0;
424 return -ENODEV;
427 static struct usb_driver dib0700_driver = {
428 .name = "dvb_usb_dib0700",
429 .probe = dib0700_probe,
430 .disconnect = dvb_usb_device_exit,
431 .id_table = dib0700_usb_id_table,
434 /* module stuff */
435 static int __init dib0700_module_init(void)
437 int result;
438 info("loaded with support for %d different device-types", dib0700_device_count);
439 if ((result = usb_register(&dib0700_driver))) {
440 err("usb_register failed. Error number %d",result);
441 return result;
444 return 0;
447 static void __exit dib0700_module_exit(void)
449 /* deregister this driver from the USB subsystem */
450 usb_deregister(&dib0700_driver);
453 module_init (dib0700_module_init);
454 module_exit (dib0700_module_exit);
456 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
457 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
458 MODULE_VERSION("1.0");
459 MODULE_LICENSE("GPL");