Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / dvb / dvb-usb / dib0700_core.c
blobc9857d5c69829e68bbc9e82af68026ebc4d4288c
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 static 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 /* expecting rx buffer: request data[0] data[1] ... data[2] */
21 static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
23 int status;
25 deb_data(">>> ");
26 debug_dump(tx,txlen,deb_data);
28 status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
29 tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
30 USB_CTRL_GET_TIMEOUT);
32 if (status != txlen)
33 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
35 return status < 0 ? status : 0;
38 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
39 int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
41 u16 index, value;
42 int status;
44 if (txlen < 2) {
45 err("tx buffer length is smaller than 2. Makes no sense.");
46 return -EINVAL;
48 if (txlen > 4) {
49 err("tx buffer length is larger than 4. Not supported.");
50 return -EINVAL;
53 deb_data(">>> ");
54 debug_dump(tx,txlen,deb_data);
56 value = ((txlen - 2) << 8) | tx[1];
57 index = 0;
58 if (txlen > 2)
59 index |= (tx[2] << 8);
60 if (txlen > 3)
61 index |= tx[3];
63 status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
64 USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
65 USB_CTRL_GET_TIMEOUT);
67 if (status < 0)
68 deb_info("ep 0 read error (status = %d)\n",status);
70 deb_data("<<< ");
71 debug_dump(rx,rxlen,deb_data);
73 return status; /* length in case of success */
76 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
78 u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
79 return dib0700_ctrl_wr(d,buf,3);
83 * I2C master xfer function
85 static int dib0700_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg *msg,int num)
87 struct dvb_usb_device *d = i2c_get_adapdata(adap);
88 int i,len;
89 u8 buf[255];
91 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
92 return -EAGAIN;
94 for (i = 0; i < num; i++) {
95 /* fill in the address */
96 buf[1] = (msg[i].addr << 1);
97 /* fill the buffer */
98 memcpy(&buf[2], msg[i].buf, msg[i].len);
100 /* write/read request */
101 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
102 buf[0] = REQUEST_I2C_READ;
103 buf[1] |= 1;
105 /* special thing in the current firmware: when length is zero the read-failed */
106 if ((len = dib0700_ctrl_rd(d, buf, msg[i].len + 2, msg[i+1].buf, msg[i+1].len)) <= 0) {
107 deb_info("I2C read failed on address %x\n", msg[i].addr);
108 break;
111 msg[i+1].len = len;
113 i++;
114 } else {
115 buf[0] = REQUEST_I2C_WRITE;
116 if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
117 break;
121 mutex_unlock(&d->i2c_mutex);
122 return i;
125 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
127 return I2C_FUNC_I2C;
130 struct i2c_algorithm dib0700_i2c_algo = {
131 .master_xfer = dib0700_i2c_xfer,
132 .functionality = dib0700_i2c_func,
135 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
136 struct dvb_usb_device_description **desc, int *cold)
138 u8 b[16];
139 s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0),
140 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
142 deb_info("FW GET_VERSION length: %d\n",ret);
144 *cold = ret <= 0;
146 deb_info("cold: %d\n", *cold);
147 return 0;
150 static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
151 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
152 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
154 u8 b[10];
155 b[0] = REQUEST_SET_CLOCK;
156 b[1] = (en_pll << 7) | (pll_src << 6) | (pll_range << 5) | (clock_gpio3 << 4);
157 b[2] = (pll_prediv >> 8) & 0xff; // MSB
158 b[3] = pll_prediv & 0xff; // LSB
159 b[4] = (pll_loopdiv >> 8) & 0xff; // MSB
160 b[5] = pll_loopdiv & 0xff; // LSB
161 b[6] = (free_div >> 8) & 0xff; // MSB
162 b[7] = free_div & 0xff; // LSB
163 b[8] = (dsuScaler >> 8) & 0xff; // MSB
164 b[9] = dsuScaler & 0xff; // LSB
166 return dib0700_ctrl_wr(d, b, 10);
169 int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
171 switch (clk_MHz) {
172 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
173 default: return -EINVAL;
175 return 0;
178 static int dib0700_jumpram(struct usb_device *udev, u32 address)
180 int ret, actlen;
181 u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
182 (address >> 24) & 0xff,
183 (address >> 16) & 0xff,
184 (address >> 8) & 0xff,
185 address & 0xff };
187 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
188 deb_fw("jumpram to 0x%x failed\n",address);
189 return ret;
191 if (actlen != 8) {
192 deb_fw("jumpram to 0x%x failed\n",address);
193 return -EIO;
195 return 0;
198 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
200 struct hexline hx;
201 int pos = 0, ret, act_len;
203 u8 buf[260];
205 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
206 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
208 buf[0] = hx.len;
209 buf[1] = (hx.addr >> 8) & 0xff;
210 buf[2] = hx.addr & 0xff;
211 buf[3] = hx.type;
212 memcpy(&buf[4],hx.data,hx.len);
213 buf[4+hx.len] = hx.chk;
215 ret = usb_bulk_msg(udev,
216 usb_sndbulkpipe(udev, 0x01),
217 buf,
218 hx.len + 5,
219 &act_len,
220 1000);
222 if (ret < 0) {
223 err("firmware download failed at %d with %d",pos,ret);
224 return ret;
228 if (ret == 0) {
229 /* start the firmware */
230 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
231 info("firmware started successfully.");
232 msleep(500);
234 } else
235 ret = -EIO;
237 return ret;
240 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
242 struct dib0700_state *st = adap->dev->priv;
243 u8 b[4];
245 b[0] = REQUEST_ENABLE_VIDEO;
246 b[1] = (onoff << 4) | 0x00; /* this bit gives a kind of command, rather than enabling something or not */
247 b[2] = (0x01 << 4); /* Master mode */
248 b[3] = 0x00;
250 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
252 if (onoff)
253 st->channel_state |= 1 << adap->id;
254 else
255 st->channel_state &= ~(1 << adap->id);
257 b[2] |= st->channel_state;
259 deb_info("data for streaming: %x %x\n",b[1],b[2]);
261 return dib0700_ctrl_wr(adap->dev, b, 4);
264 static int dib0700_rc_setup(struct dvb_usb_device *d)
266 u8 rc_setup[3] = {REQUEST_SET_RC, dvb_usb_dib0700_ir_proto, 0};
267 int i = dib0700_ctrl_wr(d, rc_setup, 3);
268 if (i<0) {
269 err("ir protocol setup failed");
270 return -1;
272 return 0;
275 static int dib0700_probe(struct usb_interface *intf,
276 const struct usb_device_id *id)
278 int i;
279 struct dvb_usb_device *dev;
281 for (i = 0; i < dib0700_device_count; i++)
282 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, &dev) == 0)
284 dib0700_rc_setup(dev);
285 return 0;
288 return -ENODEV;
291 static struct usb_driver dib0700_driver = {
292 .name = "dvb_usb_dib0700",
293 .probe = dib0700_probe,
294 .disconnect = dvb_usb_device_exit,
295 .id_table = dib0700_usb_id_table,
298 /* module stuff */
299 static int __init dib0700_module_init(void)
301 int result;
302 info("loaded with support for %d different device-types", dib0700_device_count);
303 if ((result = usb_register(&dib0700_driver))) {
304 err("usb_register failed. Error number %d",result);
305 return result;
308 return 0;
311 static void __exit dib0700_module_exit(void)
313 /* deregister this driver from the USB subsystem */
314 usb_deregister(&dib0700_driver);
317 module_init (dib0700_module_init);
318 module_exit (dib0700_module_exit);
320 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
321 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
322 MODULE_VERSION("1.0");
323 MODULE_LICENSE("GPL");