xref: /linux/drivers/media/usb/dvb-usb/dtv5100.c (revision 073350da0aa2aead9df7927a1c1046ebf5cdd816)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DVB USB Linux driver for AME DTV-5100 USB2.0 DVB-T
4  *
5  * Copyright (C) 2008  Antoine Jacquet <royale@zerezo.com>
6  * http://royale.zerezo.com/dtv5100/
7  *
8  * Inspired by gl861.c and au6610.c drivers
9  */
10 
11 #include "dtv5100.h"
12 #include "zl10353.h"
13 #include "qt1010.h"
14 
15 /* debug */
16 static int dvb_usb_dtv5100_debug;
17 module_param_named(debug, dvb_usb_dtv5100_debug, int, 0644);
18 MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
19 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
20 
21 struct dtv5100_state {
22 	unsigned char data[80];
23 };
24 
25 static int dtv5100_i2c_msg(struct dvb_usb_device *d, u8 addr,
26 			   u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
27 {
28 	struct dtv5100_state *st = d->priv;
29 	unsigned int pipe;
30 	u8 request;
31 	u8 type;
32 	u16 value;
33 	u16 index;
34 
35 	switch (wlen) {
36 	case 1:
37 		/* write { reg }, read { value } */
38 		pipe = usb_rcvctrlpipe(d->udev, 0);
39 		request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_READ :
40 							DTV5100_TUNER_READ);
41 		type = USB_TYPE_VENDOR | USB_DIR_IN;
42 		value = 0;
43 		break;
44 	case 2:
45 		/* write { reg, value } */
46 		pipe = usb_sndctrlpipe(d->udev, 0);
47 		request = (addr == DTV5100_DEMOD_ADDR ? DTV5100_DEMOD_WRITE :
48 							DTV5100_TUNER_WRITE);
49 		type = USB_TYPE_VENDOR | USB_DIR_OUT;
50 		value = wbuf[1];
51 		break;
52 	default:
53 		warn("wlen = %x, aborting.", wlen);
54 		return -EINVAL;
55 	}
56 	index = (addr << 8) + wbuf[0];
57 
58 	memcpy(st->data, rbuf, rlen);
59 	msleep(1); /* avoid I2C errors */
60 	return usb_control_msg(d->udev, pipe, request,
61 			       type, value, index, st->data, rlen,
62 			       DTV5100_USB_TIMEOUT);
63 }
64 
65 /* I2C */
66 static int dtv5100_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
67 			    int num)
68 {
69 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
70 	int i;
71 
72 	if (num > 2)
73 		return -EINVAL;
74 
75 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
76 		return -EAGAIN;
77 
78 	for (i = 0; i < num; i++) {
79 		/* write/read request */
80 		if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
81 			if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
82 					    msg[i].len, msg[i+1].buf,
83 					    msg[i+1].len) < 0)
84 				break;
85 			i++;
86 		} else if (dtv5100_i2c_msg(d, msg[i].addr, msg[i].buf,
87 					   msg[i].len, NULL, 0) < 0)
88 				break;
89 	}
90 
91 	mutex_unlock(&d->i2c_mutex);
92 	return i;
93 }
94 
95 static u32 dtv5100_i2c_func(struct i2c_adapter *adapter)
96 {
97 	return I2C_FUNC_I2C;
98 }
99 
100 static struct i2c_algorithm dtv5100_i2c_algo = {
101 	.master_xfer   = dtv5100_i2c_xfer,
102 	.functionality = dtv5100_i2c_func,
103 };
104 
105 /* Callbacks for DVB USB */
106 static struct zl10353_config dtv5100_zl10353_config = {
107 	.demod_address = DTV5100_DEMOD_ADDR,
108 	.no_tuner = 1,
109 	.parallel_ts = 1,
110 };
111 
112 static int dtv5100_frontend_attach(struct dvb_usb_adapter *adap)
113 {
114 	adap->fe_adap[0].fe = dvb_attach(zl10353_attach, &dtv5100_zl10353_config,
115 			      &adap->dev->i2c_adap);
116 	if (adap->fe_adap[0].fe == NULL)
117 		return -EIO;
118 
119 	/* disable i2c gate, or it won't work... is this safe? */
120 	adap->fe_adap[0].fe->ops.i2c_gate_ctrl = NULL;
121 
122 	return 0;
123 }
124 
125 static struct qt1010_config dtv5100_qt1010_config = {
126 	.i2c_address = DTV5100_TUNER_ADDR
127 };
128 
129 static int dtv5100_tuner_attach(struct dvb_usb_adapter *adap)
130 {
131 	return dvb_attach(qt1010_attach,
132 			  adap->fe_adap[0].fe, &adap->dev->i2c_adap,
133 			  &dtv5100_qt1010_config) == NULL ? -ENODEV : 0;
134 }
135 
136 /* DVB USB Driver stuff */
137 static struct dvb_usb_device_properties dtv5100_properties;
138 
139 static int dtv5100_probe(struct usb_interface *intf,
140 			 const struct usb_device_id *id)
141 {
142 	int i, ret;
143 	struct usb_device *udev = interface_to_usbdev(intf);
144 
145 	/* initialize non qt1010/zl10353 part? */
146 	for (i = 0; dtv5100_init[i].request; i++) {
147 		ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
148 				      dtv5100_init[i].request,
149 				      USB_TYPE_VENDOR | USB_DIR_OUT,
150 				      dtv5100_init[i].value,
151 				      dtv5100_init[i].index, NULL, 0,
152 				      DTV5100_USB_TIMEOUT);
153 		if (ret)
154 			return ret;
155 	}
156 
157 	ret = dvb_usb_device_init(intf, &dtv5100_properties,
158 				  THIS_MODULE, NULL, adapter_nr);
159 	if (ret)
160 		return ret;
161 
162 	return 0;
163 }
164 
165 enum {
166 	AME_DTV5100,
167 };
168 
169 static struct usb_device_id dtv5100_table[] = {
170 	DVB_USB_DEV(AME, AME_DTV5100),
171 	{ }
172 };
173 
174 MODULE_DEVICE_TABLE(usb, dtv5100_table);
175 
176 static struct dvb_usb_device_properties dtv5100_properties = {
177 	.caps = DVB_USB_IS_AN_I2C_ADAPTER,
178 	.usb_ctrl = DEVICE_SPECIFIC,
179 
180 	.size_of_priv = sizeof(struct dtv5100_state),
181 
182 	.num_adapters = 1,
183 	.adapter = {{
184 		.num_frontends = 1,
185 		.fe = {{
186 		.frontend_attach = dtv5100_frontend_attach,
187 		.tuner_attach    = dtv5100_tuner_attach,
188 
189 		.stream = {
190 			.type = USB_BULK,
191 			.count = 8,
192 			.endpoint = 0x82,
193 			.u = {
194 				.bulk = {
195 					.buffersize = 4096,
196 				}
197 			}
198 		},
199 		}},
200 	} },
201 
202 	.i2c_algo = &dtv5100_i2c_algo,
203 
204 	.num_device_descs = 1,
205 	.devices = {
206 		{
207 			.name = "AME DTV-5100 USB2.0 DVB-T",
208 			.cold_ids = { NULL },
209 			.warm_ids = { &dtv5100_table[AME_DTV5100], NULL },
210 		},
211 	}
212 };
213 
214 static struct usb_driver dtv5100_driver = {
215 	.name		= "dvb_usb_dtv5100",
216 	.probe		= dtv5100_probe,
217 	.disconnect	= dvb_usb_device_exit,
218 	.id_table	= dtv5100_table,
219 };
220 
221 module_usb_driver(dtv5100_driver);
222 
223 MODULE_AUTHOR(DRIVER_AUTHOR);
224 MODULE_DESCRIPTION(DRIVER_DESC);
225 MODULE_LICENSE("GPL");
226