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