V4L/DVB (8012): gl861: sleep a little to avoid I2C errors
[linux-2.6] / drivers / media / dvb / dvb-usb / gl861.c
1 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
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  * see Documentation/dvb/README.dvb-usb for more information
8  */
9 #include "gl861.h"
10
11 #include "zl10353.h"
12 #include "qt1010.h"
13
14 /* debug */
15 static int dvb_usb_gl861_debug;
16 module_param_named(debug,dvb_usb_gl861_debug, int, 0644);
17 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
18
19 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
20
21 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
22                          u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
23 {
24         u16 index;
25         u16 value = addr << (8 + 1);
26         int wo = (rbuf == NULL || rlen == 0); /* write-only */
27         u8 req, type;
28
29         if (wo) {
30                 req = GL861_REQ_I2C_WRITE;
31                 type = GL861_WRITE;
32         } else { /* rw */
33                 req = GL861_REQ_I2C_READ;
34                 type = GL861_READ;
35         }
36
37         switch (wlen) {
38         case 1:
39                 index = wbuf[0];
40                 break;
41         case 2:
42                 index = wbuf[0];
43                 value = value + wbuf[1];
44                 break;
45         default:
46                 warn("wlen = %x, aborting.", wlen);
47                 return -EINVAL;
48         }
49
50         msleep(0); /* avoid I2C errors */
51
52         return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
53                                value, index, rbuf, rlen, 2000);
54 }
55
56 /* I2C */
57 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
58                           int num)
59 {
60         struct dvb_usb_device *d = i2c_get_adapdata(adap);
61         int i;
62
63         if (num > 2)
64                 return -EINVAL;
65
66         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
67                 return -EAGAIN;
68
69         for (i = 0; i < num; i++) {
70                 /* write/read request */
71                 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
72                         if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
73                                           msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
74                                 break;
75                         i++;
76                 } else
77                         if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
78                                           msg[i].len, NULL, 0) < 0)
79                                 break;
80         }
81
82         mutex_unlock(&d->i2c_mutex);
83         return i;
84 }
85
86 static u32 gl861_i2c_func(struct i2c_adapter *adapter)
87 {
88         return I2C_FUNC_I2C;
89 }
90
91 static struct i2c_algorithm gl861_i2c_algo = {
92         .master_xfer   = gl861_i2c_xfer,
93         .functionality = gl861_i2c_func,
94 };
95
96 /* Callbacks for DVB USB */
97 static int gl861_identify_state(struct usb_device *udev,
98                                 struct dvb_usb_device_properties *props,
99                                 struct dvb_usb_device_description **desc,
100                                 int *cold)
101 {
102         *cold = 0;
103
104         return 0;
105 }
106
107 static struct zl10353_config gl861_zl10353_config = {
108         .demod_address = 0x0f,
109         .no_tuner = 1,
110         .parallel_ts = 1,
111 };
112
113 static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
114 {
115         if ((adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config,
116                                    &adap->dev->i2c_adap)) != NULL) {
117                 return 0;
118         }
119
120         return -EIO;
121 }
122
123 static struct qt1010_config gl861_qt1010_config = {
124         .i2c_address = 0x62
125 };
126
127 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
128 {
129         return dvb_attach(qt1010_attach,
130                           adap->fe, &adap->dev->i2c_adap,
131                           &gl861_qt1010_config) == NULL ? -ENODEV : 0;
132 }
133
134 /* DVB USB Driver stuff */
135 static struct dvb_usb_device_properties gl861_properties;
136
137 static int gl861_probe(struct usb_interface *intf,
138                        const struct usb_device_id *id)
139 {
140         struct dvb_usb_device *d;
141         struct usb_host_interface *alt;
142         int ret;
143
144         if (intf->num_altsetting < 2)
145                 return -ENODEV;
146
147         ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d,
148                                   adapter_nr);
149         if (ret == 0) {
150                 alt = usb_altnum_to_altsetting(intf, 0);
151
152                 if (alt == NULL) {
153                         deb_rc("not alt found!\n");
154                         return -ENODEV;
155                 }
156
157                 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
158                                         alt->desc.bAlternateSetting);
159         }
160
161         return ret;
162 }
163
164 static struct usb_device_id gl861_table [] = {
165                 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) },
166                 { USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU) },
167                 { }             /* Terminating entry */
168 };
169 MODULE_DEVICE_TABLE (usb, gl861_table);
170
171 static struct dvb_usb_device_properties gl861_properties = {
172         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
173         .usb_ctrl = DEVICE_SPECIFIC,
174
175         .size_of_priv     = 0,
176
177         .identify_state   = gl861_identify_state,
178         .num_adapters = 1,
179         .adapter = {{
180
181                 .frontend_attach  = gl861_frontend_attach,
182                 .tuner_attach     = gl861_tuner_attach,
183
184                 .stream = {
185                         .type = USB_BULK,
186                         .count = 7,
187                         .endpoint = 0x81,
188                         .u = {
189                                 .bulk = {
190                                         .buffersize = 512,
191                                 }
192                         }
193                 },
194         }},
195         .i2c_algo         = &gl861_i2c_algo,
196
197         .num_device_descs = 2,
198         .devices = {
199                 {   "MSI Mega Sky 55801 DVB-T USB2.0",
200                         { &gl861_table[0], NULL },
201                         { NULL },
202                 },
203                 {   "A-LINK DTU DVB-T USB2.0",
204                         { &gl861_table[1], NULL },
205                         { NULL },
206                 },
207         }
208 };
209
210 static struct usb_driver gl861_driver = {
211         .name           = "dvb_usb_gl861",
212         .probe          = gl861_probe,
213         .disconnect     = dvb_usb_device_exit,
214         .id_table       = gl861_table,
215 };
216
217 /* module stuff */
218 static int __init gl861_module_init(void)
219 {
220         int ret;
221
222         if ((ret = usb_register(&gl861_driver))) {
223                 err("usb_register failed. Error number %d", ret);
224                 return ret;
225         }
226
227         return 0;
228 }
229
230 static void __exit gl861_module_exit(void)
231 {
232         /* deregister this driver from the USB subsystem */
233         usb_deregister(&gl861_driver);
234 }
235
236 module_init (gl861_module_init);
237 module_exit (gl861_module_exit);
238
239 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
240 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
241 MODULE_VERSION("0.1");
242 MODULE_LICENSE("GPL");