blob: 200ba13d1d19aabe6759961e3b43e459d5b69012 [file] [log] [blame]
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -03001/* 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 */
15int dvb_usb_gl861_debug;
16module_param_named(debug,dvb_usb_gl861_debug, int, 0644);
17MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
18
19static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
Michael Krufky1f788672006-10-03 17:21:13 -030020 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030021{
22 u16 index;
23 u16 value = addr << 8;
24 int wo = (rbuf == NULL || rlen == 0); /* write-only */
25 u8 req, type;
26
27 if (wo) {
28 req = GL861_REQ_I2C_WRITE;
29 type = GL861_WRITE;
30 } else { /* rw */
31 req = GL861_REQ_I2C_READ;
32 type = GL861_READ;
33 }
34
35 switch (wlen) {
Michael Krufky1f788672006-10-03 17:21:13 -030036 case 1:
37 index = wbuf[0];
38 break;
39 case 2:
40 index = wbuf[0];
41 value = value + wbuf[1];
42 break;
43 default:
44 warn("wlen = %x, aborting.", wlen);
45 return -EINVAL;
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030046 }
47
48 return usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
Michael Krufky1f788672006-10-03 17:21:13 -030049 value, index, rbuf, rlen, 2000);
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030050}
51
52/* I2C */
53static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
Michael Krufky1f788672006-10-03 17:21:13 -030054 int num)
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030055{
56 struct dvb_usb_device *d = i2c_get_adapdata(adap);
57 int i;
58
59 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
60 return -EAGAIN;
61
62 if (num > 2)
63 return -EINVAL;
64
65 for (i = 0; i < num; i++) {
66 /* write/read request */
67 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
68 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
Michael Krufky1f788672006-10-03 17:21:13 -030069 msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030070 break;
71 i++;
72 } else
73 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
Michael Krufky1f788672006-10-03 17:21:13 -030074 msg[i].len, NULL, 0) < 0)
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -030075 break;
76 }
77
78 mutex_unlock(&d->i2c_mutex);
79 return i;
80}
81
82static u32 gl861_i2c_func(struct i2c_adapter *adapter)
83{
84 return I2C_FUNC_I2C;
85}
86
87static struct i2c_algorithm gl861_i2c_algo = {
88 .master_xfer = gl861_i2c_xfer,
89 .functionality = gl861_i2c_func,
90};
91
92/* Callbacks for DVB USB */
93static int gl861_identify_state(struct usb_device *udev,
94 struct dvb_usb_device_properties *props,
95 struct dvb_usb_device_description **desc,
96 int *cold)
97{
98 *cold = 0;
99
100 return 0;
101}
102
103static struct zl10353_config gl861_zl10353_config = {
104 .demod_address = 0x1e,
105 .no_tuner = 1,
Carl Lundqvist4131fd42006-10-09 12:49:17 -0300106 .parallel_ts = 1,
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300107};
108
109static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
110{
111 if ((adap->fe = dvb_attach(zl10353_attach, &gl861_zl10353_config,
Michael Krufky1f788672006-10-03 17:21:13 -0300112 &adap->dev->i2c_adap)) != NULL) {
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300113 return 0;
114 }
115
116 return -EIO;
117}
118
Antti Palosaari4c7e3ea2007-01-21 15:56:10 -0300119static struct qt1010_config gl861_qt1010_config = {
120 .i2c_address = 0xc4
121};
122
123static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
124{
125 /* TODO FIXME; probably I2C gate.
126 QT1010 tuner does not respond before we write 0x1a to ZL10353 demodulator
127 register 0x62. This ought to be done somewhere in demodulator initialization.
128 This solution is temporary hack. */
129 u8 buf[2] = { 0x62, 0x1a };
130 struct i2c_msg msg = {
131 .addr = gl861_zl10353_config.demod_address, .flags = 0, .buf = buf, .len = 2
132 };
133 if (i2c_transfer(&adap->dev->i2c_adap, &msg, 1) != 1) {
134 printk(KERN_WARNING "gl861 tuner attach failed\n");
135 return -EREMOTEIO;
136 }
137 return dvb_attach(qt1010_attach,
138 adap->fe, &adap->dev->i2c_adap,
139 &gl861_qt1010_config) == NULL ? -ENODEV : 0;
140}
141
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300142/* DVB USB Driver stuff */
143static struct dvb_usb_device_properties gl861_properties;
144
145static int gl861_probe(struct usb_interface *intf,
Michael Krufky1f788672006-10-03 17:21:13 -0300146 const struct usb_device_id *id)
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300147{
148 struct dvb_usb_device *d;
149 struct usb_host_interface *alt;
150 int ret;
151
152 if (intf->num_altsetting < 2)
153 return -ENODEV;
154
Michael Krufky1f788672006-10-03 17:21:13 -0300155 if ((ret = dvb_usb_device_init(intf, &gl861_properties, THIS_MODULE, &d)) == 0) {
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300156 alt = usb_altnum_to_altsetting(intf, 0);
157
158 if (alt == NULL) {
159 deb_rc("not alt found!\n");
160 return -ENODEV;
161 }
162
163 ret = usb_set_interface(d->udev, alt->desc.bInterfaceNumber,
Michael Krufky1f788672006-10-03 17:21:13 -0300164 alt->desc.bAlternateSetting);
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300165 }
166
167 return ret;
168}
169
170static struct usb_device_id gl861_table [] = {
Michael Krufky6f7880f2006-10-03 17:12:14 -0300171 { USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801) },
Michael Krufky1f788672006-10-03 17:21:13 -0300172 { } /* Terminating entry */
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300173};
174MODULE_DEVICE_TABLE (usb, gl861_table);
175
176static struct dvb_usb_device_properties gl861_properties = {
Jan Nijsb3b2b8b2006-10-07 01:25:53 -0300177 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300178 .usb_ctrl = DEVICE_SPECIFIC,
179
180 .size_of_priv = 0,
181
182 .identify_state = gl861_identify_state,
183 .num_adapters = 1,
184 .adapter = {{
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300185
186 .frontend_attach = gl861_frontend_attach,
Antti Palosaari4c7e3ea2007-01-21 15:56:10 -0300187 .tuner_attach = gl861_tuner_attach,
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300188
189 .stream = {
190 .type = USB_BULK,
191 .count = 7,
192 .endpoint = 0x81,
193 .u = {
194 .bulk = {
195 .buffersize = 512,
196 }
197 }
198 },
199 }},
200 .i2c_algo = &gl861_i2c_algo,
201
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300202 .num_device_descs = 1,
203 .devices = {
204 { "MSI Mega Sky 55801 DVB-T USB2.0",
205 { &gl861_table[0], NULL },
206 { NULL },
207 },
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300208 }
209};
210
211static struct usb_driver gl861_driver = {
Michael Krufky05ec6cc2006-10-03 17:15:26 -0300212 .name = "dvb_usb_gl861",
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300213 .probe = gl861_probe,
214 .disconnect = dvb_usb_device_exit,
215 .id_table = gl861_table,
216};
217
218/* module stuff */
219static int __init gl861_module_init(void)
220{
221 int ret;
222
223 if ((ret = usb_register(&gl861_driver))) {
224 err("usb_register failed. Error number %d", ret);
225 return ret;
226 }
227
228 return 0;
229}
230
231static void __exit gl861_module_exit(void)
232{
233 /* deregister this driver from the USB subsystem */
234 usb_deregister(&gl861_driver);
235}
236
237module_init (gl861_module_init);
238module_exit (gl861_module_exit);
239
Michael Krufky947af8f2006-10-03 17:14:07 -0300240MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300241MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
Carl Lundqvist4131fd42006-10-09 12:49:17 -0300242MODULE_VERSION("0.1");
Carl Lundqvistf0c3a2c2006-10-03 17:09:30 -0300243MODULE_LICENSE("GPL");