hamradio/scc: add missing block braces to multi-statement if
[linux/fpc-iii.git] / drivers / media / dvb / dvb-usb / dib0700_core.c
blob595a04696c87af2590588bf87013a44b8e44aa7c
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);
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)
25 int status;
27 deb_data(">>> ");
28 debug_dump(tx,txlen,deb_data);
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);
34 if (status != txlen)
35 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
37 return status < 0 ? status : 0;
40 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
41 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
43 u16 index, value;
44 int status;
46 if (txlen < 2) {
47 err("tx buffer length is smaller than 2. Makes no sense.");
48 return -EINVAL;
50 if (txlen > 4) {
51 err("tx buffer length is larger than 4. Not supported.");
52 return -EINVAL;
55 deb_data(">>> ");
56 debug_dump(tx,txlen,deb_data);
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];
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);
69 if (status < 0)
70 deb_info("ep 0 read error (status = %d)\n",status);
72 deb_data("<<< ");
73 debug_dump(rx,rxlen,deb_data);
75 return status; /* length in case of success */
78 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
80 u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
81 return dib0700_ctrl_wr(d,buf,3);
85 * I2C master xfer function
87 static int dib0700_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg *msg,int num)
89 struct dvb_usb_device *d = i2c_get_adapdata(adap);
90 int i,len;
91 u8 buf[255];
93 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
94 return -EAGAIN;
96 for (i = 0; i < num; i++) {
97 /* fill in the address */
98 buf[1] = (msg[i].addr << 1);
99 /* fill the buffer */
100 memcpy(&buf[2], msg[i].buf, msg[i].len);
102 /* write/read request */
103 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
104 buf[0] = REQUEST_I2C_READ;
105 buf[1] |= 1;
107 /* special thing in the current firmware: when length is zero the read-failed */
108 if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
109 deb_info("I2C read failed on address %x\n", msg[i].addr);
110 break;
113 msg[i+1].len = len;
115 i++;
116 } else {
117 buf[0] = REQUEST_I2C_WRITE;
118 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
119 break;
123 mutex_unlock(&d->i2c_mutex);
124 return i;
127 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
129 return I2C_FUNC_I2C;
132 struct i2c_algorithm dib0700_i2c_algo = {
133 .master_xfer = dib0700_i2c_xfer,
134 .functionality = dib0700_i2c_func,
137 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
138 struct dvb_usb_device_description **desc, int *cold)
140 u8 b[16];
141 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
142 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
144 deb_info("FW GET_VERSION length: %d\n",ret);
146 *cold = ret <= 0;
148 deb_info("cold: %d\n", *cold);
149 return 0;
152 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
153 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
154 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
156 u8 b[10];
157 b[0] = REQUEST_SET_CLOCK;
158 b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
159 b[2] = (pll_prediv >> 8) & 0xff; // MSB
160 b[3] = pll_prediv & 0xff; // LSB
161 b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
162 b[5] = pll_loopdiv & 0xff; // LSB
163 b[6] = (free_div >> 8) & 0xff; // MSB
164 b[7] = free_div & 0xff; // LSB
165 b[8] = (dsuScaler >> 8) & 0xff; // MSB
166 b[9] = dsuScaler & 0xff; // LSB
168 return dib0700_ctrl_wr(d, b, 10);
171 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
173 switch (clk_MHz) {
174 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
175 default: return -EINVAL;
177 return 0;
180 static int dib0700_jumpram(struct usb_device *udev, u32 address)
182 int ret, actlen;
183 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
184 (address >> 24) & 0xff,
185 (address >> 16) & 0xff,
186 (address >> 8) & 0xff,
187 address & 0xff };
189 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
190 deb_fw("jumpram to 0x%x failed\n",address);
191 return ret;
193 if (actlen != 8) {
194 deb_fw("jumpram to 0x%x failed\n",address);
195 return -EIO;
197 return 0;
200 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
202 struct hexline hx;
203 int pos = 0, ret, act_len;
205 u8 buf[260];
207 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
208 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
210 buf[0] = hx.len;
211 buf[1] = (hx.addr >> 8) & 0xff;
212 buf[2] = hx.addr & 0xff;
213 buf[3] = hx.type;
214 memcpy(&buf[4],hx.data,hx.len);
215 buf[4+hx.len] = hx.chk;
217 ret = usb_bulk_msg(udev,
218 usb_sndbulkpipe(udev, 0x01),
219 buf,
220 hx.len + 5,
221 &act_len,
222 1000);
224 if (ret < 0) {
225 err("firmware download failed at %d with %d",pos,ret);
226 return ret;
230 if (ret == 0) {
231 /* start the firmware */
232 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
233 info("firmware started successfully.");
234 msleep(500);
236 } else
237 ret = -EIO;
239 return ret;
242 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
244 struct dib0700_state *st = adap->dev->priv;
245 u8 b[4];
247 b[0] = REQUEST_ENABLE_VIDEO;
248 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
249 b[2] = (0x01 << 4); /* Master mode */
250 b[3] = 0x00;
252 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
254 if (onoff)
255 st->channel_state |= 1 << adap->id;
256 else
257 st->channel_state &= ~(1 << adap->id);
259 b[2] |= st->channel_state;
261 deb_info("data for streaming: %x %x\n",b[1],b[2]);
263 return dib0700_ctrl_wr(adap->dev, b, 4);
266 int dib0700_rc_setup(struct dvb_usb_device *d)
268 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
269 int i = dib0700_ctrl_wr(d, rc_setup, 3);
270 if (i<0) {
271 err("ir protocol setup failed");
272 return -1;
274 return 0;
277 static int dib0700_probe(struct usb_interface *intf,
278 const struct usb_device_id *id)
280 int i;
281 struct dvb_usb_device *dev;
283 for (i = 0; i < dib0700_device_count; i++)
284 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
285 &dev, adapter_nr) == 0)
287 dib0700_rc_setup(dev);
288 return 0;
291 return -ENODEV;
294 static struct usb_driver dib0700_driver = {
295 .name = "dvb_usb_dib0700",
296 .probe = dib0700_probe,
297 .disconnect = dvb_usb_device_exit,
298 .id_table = dib0700_usb_id_table,
301 /* module stuff */
302 static int __init dib0700_module_init(void)
304 int result;
305 info("loaded with support for %d different device-types", dib0700_device_count);
306 if ((result = usb_register(&dib0700_driver))) {
307 err("usb_register failed. Error number %d",result);
308 return result;
311 return 0;
314 static void __exit dib0700_module_exit(void)
316 /* deregister this driver from the USB subsystem */
317 usb_deregister(&dib0700_driver);
320 module_init (dib0700_module_init);
321 module_exit (dib0700_module_exit);
323 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
324 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
325 MODULE_VERSION("1.0");
326 MODULE_LICENSE("GPL");