]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/dvb/dvb-usb/dib0700_core.c
V4L/DVB (10413): Bug fix: Restore HVR-4000 tuning.
[net-next-2.6.git] / drivers / media / dvb / dvb-usb / dib0700_core.c
CommitLineData
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 */
12int dvb_usb_dib0700_debug;
13module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
14MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);
15
58e6f95e 16int dvb_usb_dib0700_ir_proto = 1;
89f4267d
JG
17module_param(dvb_usb_dib0700_ir_proto, int, 0644);
18MODULE_PARM_DESC(dvb_usb_dib0700_ir_proto, "set ir protocol (0=NEC, 1=RC5 (default), 2=RC6).");
19
78e92006
JG
20DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
21
99afb989
DH
22
23int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
24 u32 *romversion, u32 *ramversion, u32 *fwtype)
25{
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;
36}
37
b7f54910
PB
38/* expecting rx buffer: request data[0] data[1] ... data[2] */
39static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
40{
41 int status;
42
43 deb_data(">>> ");
44 debug_dump(tx,txlen,deb_data);
45
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);
49
50 if (status != txlen)
6958effe 51 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
b7f54910
PB
52
53 return status < 0 ? status : 0;
54}
55
56/* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
54d75eba 57int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
b7f54910
PB
58{
59 u16 index, value;
60 int status;
61
62 if (txlen < 2) {
63 err("tx buffer length is smaller than 2. Makes no sense.");
64 return -EINVAL;
65 }
66 if (txlen > 4) {
67 err("tx buffer length is larger than 4. Not supported.");
68 return -EINVAL;
69 }
70
71 deb_data(">>> ");
72 debug_dump(tx,txlen,deb_data);
73
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];
80
b7f54910
PB
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);
84
85 if (status < 0)
6958effe 86 deb_info("ep 0 read error (status = %d)\n",status);
b7f54910
PB
87
88 deb_data("<<< ");
89 debug_dump(rx,rxlen,deb_data);
90
91 return status; /* length in case of success */
92}
93
94int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
95{
96 u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
97 return dib0700_ctrl_wr(d,buf,3);
98}
99
100/*
bdc203e1 101 * I2C master xfer function (supported in 1.20 firmware)
b7f54910 102 */
bdc203e1
DH
103static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
104 int num)
105{
106 /* The new i2c firmware messages are more reliable and in particular
107 properly support i2c read calls not preceded by a write */
108
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;
116
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;
121
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;
133 }
134 if (i == (num - 1)) {
135 /* Last message in the transaction */
136 en_stop = 1;
137 }
138
139 if (msg[i].flags & I2C_M_RD) {
140 /* Read request */
141 u16 index, value;
142 uint8_t i2c_dest;
143
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);
149
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;
160 }
161 } else {
162 /* Write request */
163 buf[0] = REQUEST_NEW_I2C_WRITE;
164 buf[1] = (msg[i].addr << 1);
165 buf[2] = (en_start << 7) | (en_stop << 6) |
166 (msg[i].len & 0x3F);
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);
171
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);
178 if (result < 0) {
179 err("i2c write error (status = %d)\n", result);
180 break;
181 }
182 }
183 }
184 mutex_unlock(&d->i2c_mutex);
185 return i;
186}
187
188/*
189 * I2C master xfer function (pre-1.20 firmware)
190 */
191static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
192 struct i2c_msg *msg, int num)
b7f54910
PB
193{
194 struct dvb_usb_device *d = i2c_get_adapdata(adap);
195 int i,len;
196 u8 buf[255];
197
198 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
199 return -EAGAIN;
200
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);
206
207 /* write/read request */
208 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
209 buf[0] = REQUEST_I2C_READ;
210 buf[1] |= 1;
211
212 /* special thing in the current firmware: when length is zero the read-failed */
303cbeaa 213 if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
8db12cdf
DH
214 deb_info("I2C read failed on address 0x%02x\n",
215 msg[i].addr);
b7f54910 216 break;
303cbeaa 217 }
b7f54910
PB
218
219 msg[i+1].len = len;
220
221 i++;
222 } else {
223 buf[0] = REQUEST_I2C_WRITE;
224 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
225 break;
226 }
227 }
228
229 mutex_unlock(&d->i2c_mutex);
230 return i;
231}
232
bdc203e1
DH
233static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
234 int num)
235{
236 struct dvb_usb_device *d = i2c_get_adapdata(adap);
237 struct dib0700_state *st = d->priv;
238
239 if (st->fw_use_new_i2c_api == 1) {
240 /* User running at least fw 1.20 */
241 return dib0700_i2c_xfer_new(adap, msg, num);
242 } else {
243 /* Use legacy calls */
244 return dib0700_i2c_xfer_legacy(adap, msg, num);
245 }
246}
247
b7f54910
PB
248static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
249{
250 return I2C_FUNC_I2C;
251}
252
253struct i2c_algorithm dib0700_i2c_algo = {
254 .master_xfer = dib0700_i2c_xfer,
255 .functionality = dib0700_i2c_func,
256};
257
6958effe
PB
258int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
259 struct dvb_usb_device_description **desc, int *cold)
260{
a75763ff
PB
261 u8 b[16];
262 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
263 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
264
265 deb_info("FW GET_VERSION length: %d\n",ret);
266
267 *cold = ret <= 0;
6958effe
PB
268
269 deb_info("cold: %d\n", *cold);
6958effe
PB
270 return 0;
271}
272
a75763ff
PB
273static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
274 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
275 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
276{
277 u8 b[10];
278 b[0] = REQUEST_SET_CLOCK;
279 b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
280 b[2] = (pll_prediv >> 8) & 0xff; // MSB
281 b[3] = pll_prediv & 0xff; // LSB
282 b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
283 b[5] = pll_loopdiv & 0xff; // LSB
284 b[6] = (free_div >> 8) & 0xff; // MSB
285 b[7] = free_div & 0xff; // LSB
286 b[8] = (dsuScaler >> 8) & 0xff; // MSB
287 b[9] = dsuScaler & 0xff; // LSB
288
289 return dib0700_ctrl_wr(d, b, 10);
290}
291
292int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
293{
294 switch (clk_MHz) {
295 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
296 default: return -EINVAL;
297 }
298 return 0;
299}
300
b7f54910
PB
301static int dib0700_jumpram(struct usb_device *udev, u32 address)
302{
303 int ret, actlen;
304 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
305 (address >> 24) & 0xff,
306 (address >> 16) & 0xff,
307 (address >> 8) & 0xff,
308 address & 0xff };
309
310 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
311 deb_fw("jumpram to 0x%x failed\n",address);
312 return ret;
313 }
314 if (actlen != 8) {
315 deb_fw("jumpram to 0x%x failed\n",address);
316 return -EIO;
317 }
318 return 0;
319}
320
321int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
322{
323 struct hexline hx;
324 int pos = 0, ret, act_len;
325
326 u8 buf[260];
327
328 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
5bc63607 329 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
b7f54910
PB
330
331 buf[0] = hx.len;
5bc63607
PB
332 buf[1] = (hx.addr >> 8) & 0xff;
333 buf[2] = hx.addr & 0xff;
b7f54910
PB
334 buf[3] = hx.type;
335 memcpy(&buf[4],hx.data,hx.len);
336 buf[4+hx.len] = hx.chk;
337
338 ret = usb_bulk_msg(udev,
339 usb_sndbulkpipe(udev, 0x01),
340 buf,
341 hx.len + 5,
342 &act_len,
343 1000);
344
345 if (ret < 0) {
346 err("firmware download failed at %d with %d",pos,ret);
347 return ret;
348 }
349 }
350
351 if (ret == 0) {
352 /* start the firmware */
6958effe 353 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
b7f54910 354 info("firmware started successfully.");
a75763ff 355 msleep(500);
6958effe 356 }
b7f54910
PB
357 } else
358 ret = -EIO;
359
360 return ret;
361}
362
363int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
364{
365 struct dib0700_state *st = adap->dev->priv;
366 u8 b[4];
367
368 b[0] = REQUEST_ENABLE_VIDEO;
a162abb0 369 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
cb22cb52
DH
370
371 if (st->disable_streaming_master_mode == 1)
372 b[2] = 0x00;
373 else
374 b[2] = (0x01 << 4); /* Master mode */
375
b7f54910
PB
376 b[3] = 0x00;
377
378 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
379
380 if (onoff)
381 st->channel_state |= 1 << adap->id;
382 else
383 st->channel_state &= ~(1 << adap->id);
384
385 b[2] |= st->channel_state;
386
b7f54910
PB
387 deb_info("data for streaming: %x %x\n",b[1],b[2]);
388
389 return dib0700_ctrl_wr(adap->dev, b, 4);
390}
391
58e6f95e 392int dib0700_rc_setup(struct dvb_usb_device *d)
89f4267d
JG
393{
394 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
395 int i = dib0700_ctrl_wr(d, rc_setup, 3);
396 if (i<0) {
397 err("ir protocol setup failed");
398 return -1;
399 }
400 return 0;
401}
402
b7f54910
PB
403static int dib0700_probe(struct usb_interface *intf,
404 const struct usb_device_id *id)
405{
406 int i;
89f4267d 407 struct dvb_usb_device *dev;
b7f54910
PB
408
409 for (i = 0; i < dib0700_device_count; i++)
78e92006
JG
410 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
411 &dev, adapter_nr) == 0)
89f4267d
JG
412 {
413 dib0700_rc_setup(dev);
b7f54910 414 return 0;
89f4267d 415 }
b7f54910
PB
416
417 return -ENODEV;
418}
419
420static struct usb_driver dib0700_driver = {
421 .name = "dvb_usb_dib0700",
422 .probe = dib0700_probe,
423 .disconnect = dvb_usb_device_exit,
424 .id_table = dib0700_usb_id_table,
425};
426
427/* module stuff */
428static int __init dib0700_module_init(void)
429{
430 int result;
431 info("loaded with support for %d different device-types", dib0700_device_count);
432 if ((result = usb_register(&dib0700_driver))) {
433 err("usb_register failed. Error number %d",result);
434 return result;
435 }
436
437 return 0;
438}
439
440static void __exit dib0700_module_exit(void)
441{
442 /* deregister this driver from the USB subsystem */
443 usb_deregister(&dib0700_driver);
444}
445
446module_init (dib0700_module_init);
447module_exit (dib0700_module_exit);
448
449MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
450MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
451MODULE_VERSION("1.0");
452MODULE_LICENSE("GPL");