]>
Commit | Line | Data |
---|---|---|
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 */ | |
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 | ||
58e6f95e | 16 | int dvb_usb_dib0700_ir_proto = 1; |
89f4267d JG |
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 | ||
78e92006 JG |
20 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
21 | ||
b7f54910 PB |
22 | /* expecting rx buffer: request data[0] data[1] ... data[2] */ |
23 | static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen) | |
24 | { | |
25 | int status; | |
26 | ||
27 | deb_data(">>> "); | |
28 | debug_dump(tx,txlen,deb_data); | |
29 | ||
30 | status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0), | |
31 | tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen, | |
32 | USB_CTRL_GET_TIMEOUT); | |
33 | ||
34 | if (status != txlen) | |
6958effe | 35 | deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen); |
b7f54910 PB |
36 | |
37 | return status < 0 ? status : 0; | |
38 | } | |
39 | ||
40 | /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */ | |
54d75eba | 41 | int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen) |
b7f54910 PB |
42 | { |
43 | u16 index, value; | |
44 | int status; | |
45 | ||
46 | if (txlen < 2) { | |
47 | err("tx buffer length is smaller than 2. Makes no sense."); | |
48 | return -EINVAL; | |
49 | } | |
50 | if (txlen > 4) { | |
51 | err("tx buffer length is larger than 4. Not supported."); | |
52 | return -EINVAL; | |
53 | } | |
54 | ||
55 | deb_data(">>> "); | |
56 | debug_dump(tx,txlen,deb_data); | |
57 | ||
58 | value = ((txlen - 2) << 8) | tx[1]; | |
59 | index = 0; | |
60 | if (txlen > 2) | |
61 | index |= (tx[2] << 8); | |
62 | if (txlen > 3) | |
63 | index |= tx[3]; | |
64 | ||
b7f54910 PB |
65 | status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0], |
66 | USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen, | |
67 | USB_CTRL_GET_TIMEOUT); | |
68 | ||
69 | if (status < 0) | |
6958effe | 70 | deb_info("ep 0 read error (status = %d)\n",status); |
b7f54910 PB |
71 | |
72 | deb_data("<<< "); | |
73 | debug_dump(rx,rxlen,deb_data); | |
74 | ||
75 | return status; /* length in case of success */ | |
76 | } | |
77 | ||
78 | int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val) | |
79 | { | |
80 | u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) }; | |
81 | return dib0700_ctrl_wr(d,buf,3); | |
82 | } | |
83 | ||
84 | /* | |
bdc203e1 | 85 | * I2C master xfer function (supported in 1.20 firmware) |
b7f54910 | 86 | */ |
bdc203e1 DH |
87 | static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg, |
88 | int num) | |
89 | { | |
90 | /* The new i2c firmware messages are more reliable and in particular | |
91 | properly support i2c read calls not preceded by a write */ | |
92 | ||
93 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
94 | uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */ | |
95 | uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */ | |
96 | uint8_t en_start = 0; | |
97 | uint8_t en_stop = 0; | |
98 | uint8_t buf[255]; /* TBV: malloc ? */ | |
99 | int result, i; | |
100 | ||
101 | /* Ensure nobody else hits the i2c bus while we're sending our | |
102 | sequence of messages, (such as the remote control thread) */ | |
103 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) | |
104 | return -EAGAIN; | |
105 | ||
106 | for (i = 0; i < num; i++) { | |
107 | if (i == 0) { | |
108 | /* First message in the transaction */ | |
109 | en_start = 1; | |
110 | } else if (!(msg[i].flags & I2C_M_NOSTART)) { | |
111 | /* Device supports repeated-start */ | |
112 | en_start = 1; | |
113 | } else { | |
114 | /* Not the first packet and device doesn't support | |
115 | repeated start */ | |
116 | en_start = 0; | |
117 | } | |
118 | if (i == (num - 1)) { | |
119 | /* Last message in the transaction */ | |
120 | en_stop = 1; | |
121 | } | |
122 | ||
123 | if (msg[i].flags & I2C_M_RD) { | |
124 | /* Read request */ | |
125 | u16 index, value; | |
126 | uint8_t i2c_dest; | |
127 | ||
128 | i2c_dest = (msg[i].addr << 1); | |
129 | value = ((en_start << 7) | (en_stop << 6) | | |
130 | (msg[i].len & 0x3F)) << 8 | i2c_dest; | |
131 | /* I2C ctrl + FE bus; */ | |
132 | index = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30); | |
133 | ||
134 | result = usb_control_msg(d->udev, | |
135 | usb_rcvctrlpipe(d->udev, 0), | |
136 | REQUEST_NEW_I2C_READ, | |
137 | USB_TYPE_VENDOR | USB_DIR_IN, | |
138 | value, index, msg[i].buf, | |
139 | msg[i].len, | |
140 | USB_CTRL_GET_TIMEOUT); | |
141 | if (result < 0) { | |
142 | err("i2c read error (status = %d)\n", result); | |
143 | break; | |
144 | } | |
145 | } else { | |
146 | /* Write request */ | |
147 | buf[0] = REQUEST_NEW_I2C_WRITE; | |
148 | buf[1] = (msg[i].addr << 1); | |
149 | buf[2] = (en_start << 7) | (en_stop << 6) | | |
150 | (msg[i].len & 0x3F); | |
151 | /* I2C ctrl + FE bus; */ | |
152 | buf[3] = ((gen_mode<<6)&0xC0) | ((bus_mode<<4)&0x30); | |
153 | /* The Actual i2c payload */ | |
154 | memcpy(&buf[4], msg[i].buf, msg[i].len); | |
155 | ||
156 | result = usb_control_msg(d->udev, | |
157 | usb_sndctrlpipe(d->udev, 0), | |
158 | REQUEST_NEW_I2C_WRITE, | |
159 | USB_TYPE_VENDOR | USB_DIR_OUT, | |
160 | 0, 0, buf, msg[i].len + 4, | |
161 | USB_CTRL_GET_TIMEOUT); | |
162 | if (result < 0) { | |
163 | err("i2c write error (status = %d)\n", result); | |
164 | break; | |
165 | } | |
166 | } | |
167 | } | |
168 | mutex_unlock(&d->i2c_mutex); | |
169 | return i; | |
170 | } | |
171 | ||
172 | /* | |
173 | * I2C master xfer function (pre-1.20 firmware) | |
174 | */ | |
175 | static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap, | |
176 | struct i2c_msg *msg, int num) | |
b7f54910 PB |
177 | { |
178 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
179 | int i,len; | |
180 | u8 buf[255]; | |
181 | ||
182 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) | |
183 | return -EAGAIN; | |
184 | ||
185 | for (i = 0; i < num; i++) { | |
186 | /* fill in the address */ | |
187 | buf[1] = (msg[i].addr << 1); | |
188 | /* fill the buffer */ | |
189 | memcpy(&buf[2], msg[i].buf, msg[i].len); | |
190 | ||
191 | /* write/read request */ | |
192 | if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) { | |
193 | buf[0] = REQUEST_I2C_READ; | |
194 | buf[1] |= 1; | |
195 | ||
196 | /* special thing in the current firmware: when length is zero the read-failed */ | |
303cbeaa PB |
197 | if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) { |
198 | deb_info("I2C read failed on address %x\n", msg[i].addr); | |
b7f54910 | 199 | break; |
303cbeaa | 200 | } |
b7f54910 PB |
201 | |
202 | msg[i+1].len = len; | |
203 | ||
204 | i++; | |
205 | } else { | |
206 | buf[0] = REQUEST_I2C_WRITE; | |
207 | if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0) | |
208 | break; | |
209 | } | |
210 | } | |
211 | ||
212 | mutex_unlock(&d->i2c_mutex); | |
213 | return i; | |
214 | } | |
215 | ||
bdc203e1 DH |
216 | static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, |
217 | int num) | |
218 | { | |
219 | struct dvb_usb_device *d = i2c_get_adapdata(adap); | |
220 | struct dib0700_state *st = d->priv; | |
221 | ||
222 | if (st->fw_use_new_i2c_api == 1) { | |
223 | /* User running at least fw 1.20 */ | |
224 | return dib0700_i2c_xfer_new(adap, msg, num); | |
225 | } else { | |
226 | /* Use legacy calls */ | |
227 | return dib0700_i2c_xfer_legacy(adap, msg, num); | |
228 | } | |
229 | } | |
230 | ||
b7f54910 PB |
231 | static u32 dib0700_i2c_func(struct i2c_adapter *adapter) |
232 | { | |
233 | return I2C_FUNC_I2C; | |
234 | } | |
235 | ||
236 | struct i2c_algorithm dib0700_i2c_algo = { | |
237 | .master_xfer = dib0700_i2c_xfer, | |
238 | .functionality = dib0700_i2c_func, | |
239 | }; | |
240 | ||
6958effe PB |
241 | int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props, |
242 | struct dvb_usb_device_description **desc, int *cold) | |
243 | { | |
a75763ff PB |
244 | u8 b[16]; |
245 | s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0), | |
246 | REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT); | |
247 | ||
248 | deb_info("FW GET_VERSION length: %d\n",ret); | |
249 | ||
250 | *cold = ret <= 0; | |
6958effe PB |
251 | |
252 | deb_info("cold: %d\n", *cold); | |
6958effe PB |
253 | return 0; |
254 | } | |
255 | ||
a75763ff PB |
256 | static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll, |
257 | u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv, | |
258 | u16 pll_loopdiv, u16 free_div, u16 dsuScaler) | |
259 | { | |
260 | u8 b[10]; | |
261 | b[0] = REQUEST_SET_CLOCK; | |
262 | b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4); | |
263 | b[2] = (pll_prediv >> 8) & 0xff; // MSB | |
264 | b[3] = pll_prediv & 0xff; // LSB | |
265 | b[4] = (pll_loopdiv >> 8) & 0xff; // MSB | |
266 | b[5] = pll_loopdiv & 0xff; // LSB | |
267 | b[6] = (free_div >> 8) & 0xff; // MSB | |
268 | b[7] = free_div & 0xff; // LSB | |
269 | b[8] = (dsuScaler >> 8) & 0xff; // MSB | |
270 | b[9] = dsuScaler & 0xff; // LSB | |
271 | ||
272 | return dib0700_ctrl_wr(d, b, 10); | |
273 | } | |
274 | ||
275 | int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3) | |
276 | { | |
277 | switch (clk_MHz) { | |
278 | case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break; | |
279 | default: return -EINVAL; | |
280 | } | |
281 | return 0; | |
282 | } | |
283 | ||
b7f54910 PB |
284 | static int dib0700_jumpram(struct usb_device *udev, u32 address) |
285 | { | |
286 | int ret, actlen; | |
287 | u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0, | |
288 | (address >> 24) & 0xff, | |
289 | (address >> 16) & 0xff, | |
290 | (address >> 8) & 0xff, | |
291 | address & 0xff }; | |
292 | ||
293 | if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) { | |
294 | deb_fw("jumpram to 0x%x failed\n",address); | |
295 | return ret; | |
296 | } | |
297 | if (actlen != 8) { | |
298 | deb_fw("jumpram to 0x%x failed\n",address); | |
299 | return -EIO; | |
300 | } | |
301 | return 0; | |
302 | } | |
303 | ||
304 | int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw) | |
305 | { | |
306 | struct hexline hx; | |
307 | int pos = 0, ret, act_len; | |
308 | ||
309 | u8 buf[260]; | |
310 | ||
311 | while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) { | |
5bc63607 | 312 | deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk); |
b7f54910 PB |
313 | |
314 | buf[0] = hx.len; | |
5bc63607 PB |
315 | buf[1] = (hx.addr >> 8) & 0xff; |
316 | buf[2] = hx.addr & 0xff; | |
b7f54910 PB |
317 | buf[3] = hx.type; |
318 | memcpy(&buf[4],hx.data,hx.len); | |
319 | buf[4+hx.len] = hx.chk; | |
320 | ||
321 | ret = usb_bulk_msg(udev, | |
322 | usb_sndbulkpipe(udev, 0x01), | |
323 | buf, | |
324 | hx.len + 5, | |
325 | &act_len, | |
326 | 1000); | |
327 | ||
328 | if (ret < 0) { | |
329 | err("firmware download failed at %d with %d",pos,ret); | |
330 | return ret; | |
331 | } | |
332 | } | |
333 | ||
334 | if (ret == 0) { | |
335 | /* start the firmware */ | |
6958effe | 336 | if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) { |
b7f54910 | 337 | info("firmware started successfully."); |
a75763ff | 338 | msleep(500); |
6958effe | 339 | } |
b7f54910 PB |
340 | } else |
341 | ret = -EIO; | |
342 | ||
343 | return ret; | |
344 | } | |
345 | ||
346 | int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) | |
347 | { | |
348 | struct dib0700_state *st = adap->dev->priv; | |
349 | u8 b[4]; | |
350 | ||
351 | b[0] = REQUEST_ENABLE_VIDEO; | |
a162abb0 | 352 | b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */ |
b7f54910 PB |
353 | b[2] = (0x01 << 4); /* Master mode */ |
354 | b[3] = 0x00; | |
355 | ||
356 | deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id); | |
357 | ||
358 | if (onoff) | |
359 | st->channel_state |= 1 << adap->id; | |
360 | else | |
361 | st->channel_state &= ~(1 << adap->id); | |
362 | ||
363 | b[2] |= st->channel_state; | |
364 | ||
b7f54910 PB |
365 | deb_info("data for streaming: %x %x\n",b[1],b[2]); |
366 | ||
367 | return dib0700_ctrl_wr(adap->dev, b, 4); | |
368 | } | |
369 | ||
58e6f95e | 370 | int dib0700_rc_setup(struct dvb_usb_device *d) |
89f4267d JG |
371 | { |
372 | u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0}; | |
373 | int i = dib0700_ctrl_wr(d, rc_setup, 3); | |
374 | if (i<0) { | |
375 | err("ir protocol setup failed"); | |
376 | return -1; | |
377 | } | |
378 | return 0; | |
379 | } | |
380 | ||
b7f54910 PB |
381 | static int dib0700_probe(struct usb_interface *intf, |
382 | const struct usb_device_id *id) | |
383 | { | |
384 | int i; | |
89f4267d | 385 | struct dvb_usb_device *dev; |
b7f54910 PB |
386 | |
387 | for (i = 0; i < dib0700_device_count; i++) | |
78e92006 JG |
388 | if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, |
389 | &dev, adapter_nr) == 0) | |
89f4267d JG |
390 | { |
391 | dib0700_rc_setup(dev); | |
b7f54910 | 392 | return 0; |
89f4267d | 393 | } |
b7f54910 PB |
394 | |
395 | return -ENODEV; | |
396 | } | |
397 | ||
398 | static struct usb_driver dib0700_driver = { | |
399 | .name = "dvb_usb_dib0700", | |
400 | .probe = dib0700_probe, | |
401 | .disconnect = dvb_usb_device_exit, | |
402 | .id_table = dib0700_usb_id_table, | |
403 | }; | |
404 | ||
405 | /* module stuff */ | |
406 | static int __init dib0700_module_init(void) | |
407 | { | |
408 | int result; | |
409 | info("loaded with support for %d different device-types", dib0700_device_count); | |
410 | if ((result = usb_register(&dib0700_driver))) { | |
411 | err("usb_register failed. Error number %d",result); | |
412 | return result; | |
413 | } | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
418 | static void __exit dib0700_module_exit(void) | |
419 | { | |
420 | /* deregister this driver from the USB subsystem */ | |
421 | usb_deregister(&dib0700_driver); | |
422 | } | |
423 | ||
424 | module_init (dib0700_module_init); | |
425 | module_exit (dib0700_module_exit); | |
426 | ||
427 | MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>"); | |
428 | MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge"); | |
429 | MODULE_VERSION("1.0"); | |
430 | MODULE_LICENSE("GPL"); |