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
5  *	Free Software Foundation, version 2.
6  *
7  * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
8  */
9 #include "gl861.h"
10 
11 #include "zl10353.h"
12 #include "qt1010.h"
13 
14 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
15 
16 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
17 			 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
18 {
19 	u16 index;
20 	u16 value = addr << (8 + 1);
21 	int wo = (rbuf == NULL || rlen == 0); /* write-only */
22 	u8 req, type;
23 	u8 *buf;
24 	int ret;
25 
26 	if (wo) {
27 		req = GL861_REQ_I2C_WRITE;
28 		type = GL861_WRITE;
29 	} else { /* rw */
30 		req = GL861_REQ_I2C_READ;
31 		type = GL861_READ;
32 	}
33 
34 	switch (wlen) {
35 	case 1:
36 		index = wbuf[0];
37 		break;
38 	case 2:
39 		index = wbuf[0];
40 		value = value + wbuf[1];
41 		break;
42 	default:
43 		dev_err(&d->udev->dev, "%s: wlen=%d, aborting\n",
44 				KBUILD_MODNAME, wlen);
45 		return -EINVAL;
46 	}
47 	buf = NULL;
48 	if (rlen > 0) {
49 		buf = kmalloc(rlen, GFP_KERNEL);
50 		if (!buf)
51 			return -ENOMEM;
52 	}
53 	usleep_range(1000, 2000); /* avoid I2C errors */
54 
55 	ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
56 			      value, index, buf, rlen, 2000);
57 	if (rlen > 0) {
58 		if (ret > 0)
59 			memcpy(rbuf, buf, rlen);
60 		kfree(buf);
61 	}
62 
63 	return ret;
64 }
65 
66 /* I2C */
67 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
68 			  int num)
69 {
70 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
71 	int i;
72 
73 	if (num > 2)
74 		return -EINVAL;
75 
76 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
77 		return -EAGAIN;
78 
79 	for (i = 0; i < num; i++) {
80 		/* write/read request */
81 		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
82 			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
83 				msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
84 				break;
85 			i++;
86 		} else
87 			if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
88 					  msg[i].len, NULL, 0) < 0)
89 				break;
90 	}
91 
92 	mutex_unlock(&d->i2c_mutex);
93 	return i;
94 }
95 
96 static u32 gl861_i2c_func(struct i2c_adapter *adapter)
97 {
98 	return I2C_FUNC_I2C;
99 }
100 
101 static struct i2c_algorithm gl861_i2c_algo = {
102 	.master_xfer   = gl861_i2c_xfer,
103 	.functionality = gl861_i2c_func,
104 };
105 
106 /* Callbacks for DVB USB */
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 
116 	adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
117 		&adap_to_d(adap)->i2c_adap);
118 	if (adap->fe[0] == NULL)
119 		return -EIO;
120 
121 	return 0;
122 }
123 
124 static struct qt1010_config gl861_qt1010_config = {
125 	.i2c_address = 0x62
126 };
127 
128 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
129 {
130 	return dvb_attach(qt1010_attach,
131 			  adap->fe[0], &adap_to_d(adap)->i2c_adap,
132 			  &gl861_qt1010_config) == NULL ? -ENODEV : 0;
133 }
134 
135 static int gl861_init(struct dvb_usb_device *d)
136 {
137 	/*
138 	 * There is 2 interfaces. Interface 0 is for TV and interface 1 is
139 	 * for HID remote controller. Interface 0 has 2 alternate settings.
140 	 * For some reason we need to set interface explicitly, defaulted
141 	 * as alternate setting 1?
142 	 */
143 	return usb_set_interface(d->udev, 0, 0);
144 }
145 
146 /* DVB USB Driver stuff */
147 static struct dvb_usb_device_properties gl861_props = {
148 	.driver_name = KBUILD_MODNAME,
149 	.owner = THIS_MODULE,
150 	.adapter_nr = adapter_nr,
151 
152 	.i2c_algo = &gl861_i2c_algo,
153 	.frontend_attach = gl861_frontend_attach,
154 	.tuner_attach = gl861_tuner_attach,
155 	.init = gl861_init,
156 
157 	.num_adapters = 1,
158 	.adapter = {
159 		{
160 			.stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
161 		}
162 	}
163 };
164 
165 static const struct usb_device_id gl861_id_table[] = {
166 	{ DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
167 		&gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
168 	{ DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
169 		&gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
170 	{ }
171 };
172 MODULE_DEVICE_TABLE(usb, gl861_id_table);
173 
174 static struct usb_driver gl861_usb_driver = {
175 	.name = KBUILD_MODNAME,
176 	.id_table = gl861_id_table,
177 	.probe = dvb_usbv2_probe,
178 	.disconnect = dvb_usbv2_disconnect,
179 	.suspend = dvb_usbv2_suspend,
180 	.resume = dvb_usbv2_resume,
181 	.reset_resume = dvb_usbv2_reset_resume,
182 	.no_dynamic_id = 1,
183 	.soft_unbind = 1,
184 };
185 
186 module_usb_driver(gl861_usb_driver);
187 
188 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
189 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
190 MODULE_VERSION("0.1");
191 MODULE_LICENSE("GPL");
192