]> err.no Git - linux-2.6/blob - drivers/media/dvb/dvb-usb/dib0700_core.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6
[linux-2.6] / drivers / media / dvb / dvb-usb / dib0700_core.c
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 /* expecting rx buffer: request data[0] data[1] ... data[2] */
17 static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
18 {
19         int status;
20
21         deb_data(">>> ");
22         debug_dump(tx,txlen,deb_data);
23
24         status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
25                 tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
26                 USB_CTRL_GET_TIMEOUT);
27
28         if (status != txlen)
29                 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
30
31         return status < 0 ? status : 0;
32 }
33
34 /* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
35 static int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
36 {
37         u16 index, value;
38         int status;
39
40         if (txlen < 2) {
41                 err("tx buffer length is smaller than 2. Makes no sense.");
42                 return -EINVAL;
43         }
44         if (txlen > 4) {
45                 err("tx buffer length is larger than 4. Not supported.");
46                 return -EINVAL;
47         }
48
49         deb_data(">>> ");
50         debug_dump(tx,txlen,deb_data);
51
52         value = ((txlen - 2) << 8) | tx[1];
53         index = 0;
54         if (txlen > 2)
55                 index |= (tx[2] << 8);
56         if (txlen > 3)
57                 index |= tx[3];
58
59         /* think about swapping here */
60         value = le16_to_cpu(value);
61         index = le16_to_cpu(index);
62
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);
66
67         if (status < 0)
68                 deb_info("ep 0 read error (status = %d)\n",status);
69
70         deb_data("<<< ");
71         debug_dump(rx,rxlen,deb_data);
72
73         return status; /* length in case of success */
74 }
75
76 int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
77 {
78         u8 buf[3] = { REQUEST_SET_GPIO, gpio, ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6) };
79         return dib0700_ctrl_wr(d,buf,3);
80 }
81
82 /*
83  * I2C master xfer function
84  */
85 static int dib0700_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg *msg,int num)
86 {
87         struct dvb_usb_device *d = i2c_get_adapdata(adap);
88         int i,len;
89         u8 buf[255];
90
91         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
92                 return -EAGAIN;
93
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);
99
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;
104
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;
109                         }
110
111                         msg[i+1].len = len;
112
113                         i++;
114                 } else {
115                         buf[0] = REQUEST_I2C_WRITE;
116                         if (dib0700_ctrl_wr(d, buf, msg[i].len + 2) < 0)
117                                 break;
118                 }
119         }
120
121         mutex_unlock(&d->i2c_mutex);
122         return i;
123 }
124
125 static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
126 {
127         return I2C_FUNC_I2C;
128 }
129
130 struct i2c_algorithm dib0700_i2c_algo = {
131         .master_xfer   = dib0700_i2c_xfer,
132         .functionality = dib0700_i2c_func,
133 };
134
135 int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
136                         struct dvb_usb_device_description **desc, int *cold)
137 {
138         u8 buf[3] = { REQUEST_SET_GPIO, 4, (GPIO_IN << 7) | (0 << 6) }; // GPIO4 is save - used for I2C
139         *cold = usb_control_msg(udev, usb_sndctrlpipe(udev,0),
140                 buf[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, buf, 3, USB_CTRL_GET_TIMEOUT) != 3;
141
142         deb_info("cold: %d\n", *cold);
143         return 0;
144 }
145
146 static int dib0700_jumpram(struct usb_device *udev, u32 address)
147 {
148         int ret, actlen;
149         u8 buf[8] = { REQUEST_JUMPRAM, 0, 0, 0,
150                 (address >> 24) & 0xff,
151                 (address >> 16) & 0xff,
152                 (address >> 8)  & 0xff,
153                  address        & 0xff };
154
155         if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
156                 deb_fw("jumpram to 0x%x failed\n",address);
157                 return ret;
158         }
159         if (actlen != 8) {
160                 deb_fw("jumpram to 0x%x failed\n",address);
161                 return -EIO;
162         }
163         return 0;
164 }
165
166 int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
167 {
168         struct hexline hx;
169         int pos = 0, ret, act_len;
170
171         u8 buf[260];
172
173         while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
174                 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",hx.addr, hx.len, hx.chk);
175
176                 buf[0] = hx.len;
177                 buf[1] = (hx.addr >> 8) & 0xff;
178                 buf[2] =  hx.addr       & 0xff;
179                 buf[3] = hx.type;
180                 memcpy(&buf[4],hx.data,hx.len);
181                 buf[4+hx.len] = hx.chk;
182
183                 ret = usb_bulk_msg(udev,
184                         usb_sndbulkpipe(udev, 0x01),
185                         buf,
186                         hx.len + 5,
187                         &act_len,
188                         1000);
189
190                 if (ret < 0) {
191                         err("firmware download failed at %d with %d",pos,ret);
192                         return ret;
193                 }
194         }
195
196         if (ret == 0) {
197                 /* start the firmware */
198                 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
199                         info("firmware started successfully.");
200                         msleep(100);
201                 }
202         } else
203                 ret = -EIO;
204
205         return ret;
206 }
207
208 int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
209 {
210         struct dib0700_state *st = adap->dev->priv;
211         u8 b[4];
212
213         b[0] = REQUEST_ENABLE_VIDEO;
214         b[1] = 0x00;
215         b[2] = (0x01 << 4); /* Master mode */
216         b[3] = 0x00;
217
218         deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
219
220         if (onoff)
221                 st->channel_state |=   1 << adap->id;
222         else
223                 st->channel_state &= ~(1 << adap->id);
224
225         b[2] |= st->channel_state;
226
227         if (st->channel_state) /* if at least one channel is active */
228                 b[1] = (0x01 << 4) | 0x00;
229
230         deb_info("data for streaming: %x %x\n",b[1],b[2]);
231
232         return dib0700_ctrl_wr(adap->dev, b, 4);
233 }
234
235 static int dib0700_probe(struct usb_interface *intf,
236                 const struct usb_device_id *id)
237 {
238         int i;
239
240         for (i = 0; i < dib0700_device_count; i++)
241                 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE, NULL) == 0)
242                         return 0;
243
244         return -ENODEV;
245 }
246
247 static struct usb_driver dib0700_driver = {
248         .name       = "dvb_usb_dib0700",
249         .probe      = dib0700_probe,
250         .disconnect = dvb_usb_device_exit,
251         .id_table   = dib0700_usb_id_table,
252 };
253
254 /* module stuff */
255 static int __init dib0700_module_init(void)
256 {
257         int result;
258         info("loaded with support for %d different device-types", dib0700_device_count);
259         if ((result = usb_register(&dib0700_driver))) {
260                 err("usb_register failed. Error number %d",result);
261                 return result;
262         }
263
264         return 0;
265 }
266
267 static void __exit dib0700_module_exit(void)
268 {
269         /* deregister this driver from the USB subsystem */
270         usb_deregister(&dib0700_driver);
271 }
272
273 module_init (dib0700_module_init);
274 module_exit (dib0700_module_exit);
275
276 MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
277 MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
278 MODULE_VERSION("1.0");
279 MODULE_LICENSE("GPL");