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
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).");
19
20 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
21
22
dib0700_get_version(struct dvb_usb_device * d,u32 * hwversion,u32 * romversion,u32 * ramversion,u32 * fwtype)23 int 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
38 /* expecting rx buffer: request data[0] data[1] ... data[2] */
dib0700_ctrl_wr(struct dvb_usb_device * d,u8 * tx,u8 txlen)39 static 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)
51 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
52
53 return status < 0 ? status : 0;
54 }
55
56 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
dib0700_ctrl_rd(struct dvb_usb_device * d,u8 * tx,u8 txlen,u8 * rx,u8 rxlen)57 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
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
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)
86 deb_info("ep 0 read error (status = %d)\n",status);
87
88 deb_data("<<< ");
89 debug_dump(rx,rxlen,deb_data);
90
91 return status; /* length in case of success */
92 }
93
dib0700_set_gpio(struct dvb_usb_device * d,enum dib07x0_gpios gpio,u8 gpio_dir,u8 gpio_val)94 int 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 /*
101 * I2C master xfer function (supported in 1.20 firmware)
102 */
dib0700_i2c_xfer_new(struct i2c_adapter * adap,struct i2c_msg * msg,int num)103 static 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 */
dib0700_i2c_xfer_legacy(struct i2c_adapter * adap,struct i2c_msg * msg,int num)191 static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
192 struct i2c_msg *msg, int num)
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 */
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);
215 break;
216 }
217
218 msg[i+1].len = len;
219
220 i++;
221 } else {
222 buf[0] = REQUEST_I2C_WRITE;
223 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
224 break;
225 }
226 }
227
228 mutex_unlock(&d->i2c_mutex);
229 return i;
230 }
231
dib0700_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msg,int num)232 static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
233 int num)
234 {
235 struct dvb_usb_device *d = i2c_get_adapdata(adap);
236 struct dib0700_state *st = d->priv;
237
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);
241 } else {
242 /* Use legacy calls */
243 return dib0700_i2c_xfer_legacy(adap, msg, num);
244 }
245 }
246
dib0700_i2c_func(struct i2c_adapter * adapter)247 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
248 {
249 return I2C_FUNC_I2C;
250 }
251
252 struct i2c_algorithm dib0700_i2c_algo = {
253 .master_xfer = dib0700_i2c_xfer,
254 .functionality = dib0700_i2c_func,
255 };
256
dib0700_identify_state(struct usb_device * udev,struct dvb_usb_device_properties * props,struct dvb_usb_device_description ** desc,int * cold)257 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
258 struct dvb_usb_device_description **desc, int *cold)
259 {
260 u8 b[16];
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);
263
264 deb_info("FW GET_VERSION length: %d\n",ret);
265
266 *cold = ret <= 0;
267
268 deb_info("cold: %d\n", *cold);
269 return 0;
270 }
271
dib0700_set_clock(struct dvb_usb_device * d,u8 en_pll,u8 pll_src,u8 pll_range,u8 clock_gpio3,u16 pll_prediv,u16 pll_loopdiv,u16 free_div,u16 dsuScaler)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)
275 {
276 u8 b[10];
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
287
288 return dib0700_ctrl_wr(d, b, 10);
289 }
290
dib0700_ctrl_clock(struct dvb_usb_device * d,u32 clk_MHz,u8 clock_out_gp3)291 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
292 {
293 switch (clk_MHz) {
294 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
295 default: return -EINVAL;
296 }
297 return 0;
298 }
299
dib0700_jumpram(struct usb_device * udev,u32 address)300 static int dib0700_jumpram(struct usb_device *udev, u32 address)
301 {
302 int ret, actlen;
303 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
304 (address >> 24) & 0xff,
305 (address >> 16) & 0xff,
306 (address >> 8) & 0xff,
307 address & 0xff };
308
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);
311 return ret;
312 }
313 if (actlen != 8) {
314 deb_fw("jumpram to 0x%x failed\n",address);
315 return -EIO;
316 }
317 return 0;
318 }
319
dib0700_download_firmware(struct usb_device * udev,const struct firmware * fw)320 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
321 {
322 struct hexline hx;
323 int pos = 0, ret, act_len;
324
325 u8 buf[260];
326
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);
329
330 buf[0] = hx.len;
331 buf[1] = (hx.addr >> 8) & 0xff;
332 buf[2] = hx.addr & 0xff;
333 buf[3] = hx.type;
334 memcpy(&buf[4],hx.data,hx.len);
335 buf[4+hx.len] = hx.chk;
336
337 ret = usb_bulk_msg(udev,
338 usb_sndbulkpipe(udev, 0x01),
339 buf,
340 hx.len + 5,
341 &act_len,
342 1000);
343
344 if (ret < 0) {
345 err("firmware download failed at %d with %d",pos,ret);
346 return ret;
347 }
348 }
349
350 if (ret == 0) {
351 /* start the firmware */
352 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
353 info("firmware started successfully.");
354 msleep(500);
355 }
356 } else
357 ret = -EIO;
358
359 return ret;
360 }
361
dib0700_streaming_ctrl(struct dvb_usb_adapter * adap,int onoff)362 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
363 {
364 struct dib0700_state *st = adap->dev->priv;
365 u8 b[4];
366
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 */
369
370 if (st->disable_streaming_master_mode == 1)
371 b[2] = 0x00;
372 else
373 b[2] = (0x01 << 4); /* Master mode */
374
375 b[3] = 0x00;
376
377 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
378
379 if (onoff)
380 st->channel_state |= 1 << adap->id;
381 else
382 st->channel_state &= ~(1 << adap->id);
383
384 b[2] |= st->channel_state;
385
386 deb_info("data for streaming: %x %x\n",b[1],b[2]);
387
388 return dib0700_ctrl_wr(adap->dev, b, 4);
389 }
390
dib0700_rc_setup(struct dvb_usb_device * d)391 int dib0700_rc_setup(struct dvb_usb_device *d)
392 {
393 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
394 int i = dib0700_ctrl_wr(d, rc_setup, 3);
395 if (i<0) {
396 err("ir protocol setup failed");
397 return -1;
398 }
399 return 0;
400 }
401
dib0700_probe(struct usb_interface * intf,const struct usb_device_id * id)402 static int dib0700_probe(struct usb_interface *intf,
403 const struct usb_device_id *id)
404 {
405 int i;
406 struct dvb_usb_device *dev;
407
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)
411 {
412 dib0700_rc_setup(dev);
413 return 0;
414 }
415
416 return -ENODEV;
417 }
418
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,
424 };
425
426 /* module stuff */
dib0700_module_init(void)427 static int __init dib0700_module_init(void)
428 {
429 int result;
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);
433 return result;
434 }
435
436 return 0;
437 }
438
dib0700_module_exit(void)439 static void __exit dib0700_module_exit(void)
440 {
441 /* deregister this driver from the USB subsystem */
442 usb_deregister(&dib0700_driver);
443 }
444
445 module_init (dib0700_module_init);
446 module_exit (dib0700_module_exit);
447
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");
452