1 /*
2  *  Driver for the Auvitek USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/suspend.h>
27 #include <media/v4l2-common.h>
28 
29 #include "au0828.h"
30 #include "au8522.h"
31 #include "xc5000.h"
32 #include "mxl5007t.h"
33 #include "tda18271.h"
34 
35 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
36 
37 #define _AU0828_BULKPIPE 0x83
38 #define _BULKPIPESIZE 0xe522
39 
40 static u8 hauppauge_hvr950q_led_states[] = {
41 	0x00, /* off */
42 	0x02, /* yellow */
43 	0x04, /* green */
44 };
45 
46 static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
47 	.gpio_output = 0x00e0,
48 	.gpio_output_enable  = 0x6006,
49 	.gpio_output_disable = 0x0660,
50 
51 	.gpio_leds = 0x00e2,
52 	.led_states  = hauppauge_hvr950q_led_states,
53 	.num_led_states = sizeof(hauppauge_hvr950q_led_states),
54 
55 	.vsb8_strong   = 20 /* dB */ * 10,
56 	.qam64_strong  = 25 /* dB */ * 10,
57 	.qam256_strong = 32 /* dB */ * 10,
58 };
59 
60 static struct au8522_config hauppauge_hvr950q_config = {
61 	.demod_address = 0x8e >> 1,
62 	.status_mode   = AU8522_DEMODLOCKING,
63 	.qam_if        = AU8522_IF_6MHZ,
64 	.vsb_if        = AU8522_IF_6MHZ,
65 	.led_cfg       = &hauppauge_hvr950q_led_cfg,
66 };
67 
68 static struct au8522_config fusionhdtv7usb_config = {
69 	.demod_address = 0x8e >> 1,
70 	.status_mode   = AU8522_DEMODLOCKING,
71 	.qam_if        = AU8522_IF_6MHZ,
72 	.vsb_if        = AU8522_IF_6MHZ,
73 };
74 
75 static struct au8522_config hauppauge_woodbury_config = {
76 	.demod_address = 0x8e >> 1,
77 	.status_mode   = AU8522_DEMODLOCKING,
78 	.qam_if        = AU8522_IF_4MHZ,
79 	.vsb_if        = AU8522_IF_3_25MHZ,
80 };
81 
82 static struct xc5000_config hauppauge_hvr950q_tunerconfig = {
83 	.i2c_address      = 0x61,
84 	.if_khz           = 6000,
85 };
86 
87 static struct mxl5007t_config mxl5007t_hvr950q_config = {
88 	.xtal_freq_hz = MxL_XTAL_24_MHZ,
89 	.if_freq_hz = MxL_IF_6_MHZ,
90 };
91 
92 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
93 	.gate    = TDA18271_GATE_DIGITAL,
94 };
95 
96 /*-------------------------------------------------------------------*/
urb_completion(struct urb * purb)97 static void urb_completion(struct urb *purb)
98 {
99 	struct au0828_dev *dev = purb->context;
100 	int ptype = usb_pipetype(purb->pipe);
101 
102 	dprintk(2, "%s()\n", __func__);
103 
104 	if (!dev)
105 		return;
106 
107 	if (dev->urb_streaming == 0)
108 		return;
109 
110 	if (ptype != PIPE_BULK) {
111 		printk(KERN_ERR "%s() Unsupported URB type %d\n",
112 		       __func__, ptype);
113 		return;
114 	}
115 
116 	/* Feed the transport payload into the kernel demux */
117 	dvb_dmx_swfilter_packets(&dev->dvb.demux,
118 		purb->transfer_buffer, purb->actual_length / 188);
119 
120 	/* Clean the buffer before we requeue */
121 	memset(purb->transfer_buffer, 0, URB_BUFSIZE);
122 
123 	/* Requeue URB */
124 	usb_submit_urb(purb, GFP_ATOMIC);
125 }
126 
stop_urb_transfer(struct au0828_dev * dev)127 static int stop_urb_transfer(struct au0828_dev *dev)
128 {
129 	int i;
130 
131 	dprintk(2, "%s()\n", __func__);
132 
133 	for (i = 0; i < URB_COUNT; i++) {
134 		usb_kill_urb(dev->urbs[i]);
135 		kfree(dev->urbs[i]->transfer_buffer);
136 		usb_free_urb(dev->urbs[i]);
137 	}
138 
139 	dev->urb_streaming = 0;
140 
141 	return 0;
142 }
143 
start_urb_transfer(struct au0828_dev * dev)144 static int start_urb_transfer(struct au0828_dev *dev)
145 {
146 	struct urb *purb;
147 	int i, ret = -ENOMEM;
148 
149 	dprintk(2, "%s()\n", __func__);
150 
151 	if (dev->urb_streaming) {
152 		dprintk(2, "%s: bulk xfer already running!\n", __func__);
153 		return 0;
154 	}
155 
156 	for (i = 0; i < URB_COUNT; i++) {
157 
158 		dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
159 		if (!dev->urbs[i])
160 			goto err;
161 
162 		purb = dev->urbs[i];
163 
164 		purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
165 		if (!purb->transfer_buffer) {
166 			usb_free_urb(purb);
167 			dev->urbs[i] = NULL;
168 			goto err;
169 		}
170 
171 		purb->status = -EINPROGRESS;
172 		usb_fill_bulk_urb(purb,
173 				  dev->usbdev,
174 				  usb_rcvbulkpipe(dev->usbdev,
175 					_AU0828_BULKPIPE),
176 				  purb->transfer_buffer,
177 				  URB_BUFSIZE,
178 				  urb_completion,
179 				  dev);
180 
181 	}
182 
183 	for (i = 0; i < URB_COUNT; i++) {
184 		ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
185 		if (ret != 0) {
186 			stop_urb_transfer(dev);
187 			printk(KERN_ERR "%s: failed urb submission, "
188 			       "err = %d\n", __func__, ret);
189 			return ret;
190 		}
191 	}
192 
193 	dev->urb_streaming = 1;
194 	ret = 0;
195 
196 err:
197 	return ret;
198 }
199 
au0828_dvb_start_feed(struct dvb_demux_feed * feed)200 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
201 {
202 	struct dvb_demux *demux = feed->demux;
203 	struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
204 	struct au0828_dvb *dvb = &dev->dvb;
205 	int ret = 0;
206 
207 	dprintk(1, "%s()\n", __func__);
208 
209 	if (!demux->dmx.frontend)
210 		return -EINVAL;
211 
212 	if (dvb) {
213 		mutex_lock(&dvb->lock);
214 		if (dvb->feeding++ == 0) {
215 			/* Start transport */
216 			au0828_write(dev, 0x608, 0x90);
217 			au0828_write(dev, 0x609, 0x72);
218 			au0828_write(dev, 0x60a, 0x71);
219 			au0828_write(dev, 0x60b, 0x01);
220 			ret = start_urb_transfer(dev);
221 		}
222 		mutex_unlock(&dvb->lock);
223 	}
224 
225 	return ret;
226 }
227 
au0828_dvb_stop_feed(struct dvb_demux_feed * feed)228 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
229 {
230 	struct dvb_demux *demux = feed->demux;
231 	struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
232 	struct au0828_dvb *dvb = &dev->dvb;
233 	int ret = 0;
234 
235 	dprintk(1, "%s()\n", __func__);
236 
237 	if (dvb) {
238 		mutex_lock(&dvb->lock);
239 		if (--dvb->feeding == 0) {
240 			/* Stop transport */
241 			au0828_write(dev, 0x608, 0x00);
242 			au0828_write(dev, 0x609, 0x00);
243 			au0828_write(dev, 0x60a, 0x00);
244 			au0828_write(dev, 0x60b, 0x00);
245 			ret = stop_urb_transfer(dev);
246 		}
247 		mutex_unlock(&dvb->lock);
248 	}
249 
250 	return ret;
251 }
252 
dvb_register(struct au0828_dev * dev)253 static int dvb_register(struct au0828_dev *dev)
254 {
255 	struct au0828_dvb *dvb = &dev->dvb;
256 	int result;
257 
258 	dprintk(1, "%s()\n", __func__);
259 
260 	/* register adapter */
261 	result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
262 				      &dev->usbdev->dev, adapter_nr);
263 	if (result < 0) {
264 		printk(KERN_ERR "%s: dvb_register_adapter failed "
265 		       "(errno = %d)\n", DRIVER_NAME, result);
266 		goto fail_adapter;
267 	}
268 	dvb->adapter.priv = dev;
269 
270 	/* register frontend */
271 	result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
272 	if (result < 0) {
273 		printk(KERN_ERR "%s: dvb_register_frontend failed "
274 		       "(errno = %d)\n", DRIVER_NAME, result);
275 		goto fail_frontend;
276 	}
277 
278 	/* register demux stuff */
279 	dvb->demux.dmx.capabilities =
280 		DMX_TS_FILTERING | DMX_SECTION_FILTERING |
281 		DMX_MEMORY_BASED_FILTERING;
282 	dvb->demux.priv       = dev;
283 	dvb->demux.filternum  = 256;
284 	dvb->demux.feednum    = 256;
285 	dvb->demux.start_feed = au0828_dvb_start_feed;
286 	dvb->demux.stop_feed  = au0828_dvb_stop_feed;
287 	result = dvb_dmx_init(&dvb->demux);
288 	if (result < 0) {
289 		printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
290 		       DRIVER_NAME, result);
291 		goto fail_dmx;
292 	}
293 
294 	dvb->dmxdev.filternum    = 256;
295 	dvb->dmxdev.demux        = &dvb->demux.dmx;
296 	dvb->dmxdev.capabilities = 0;
297 	result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
298 	if (result < 0) {
299 		printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
300 		       DRIVER_NAME, result);
301 		goto fail_dmxdev;
302 	}
303 
304 	dvb->fe_hw.source = DMX_FRONTEND_0;
305 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
306 	if (result < 0) {
307 		printk(KERN_ERR "%s: add_frontend failed "
308 		       "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
309 		goto fail_fe_hw;
310 	}
311 
312 	dvb->fe_mem.source = DMX_MEMORY_FE;
313 	result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
314 	if (result < 0) {
315 		printk(KERN_ERR "%s: add_frontend failed "
316 		       "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
317 		goto fail_fe_mem;
318 	}
319 
320 	result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
321 	if (result < 0) {
322 		printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
323 		       DRIVER_NAME, result);
324 		goto fail_fe_conn;
325 	}
326 
327 	/* register network adapter */
328 	dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
329 	return 0;
330 
331 fail_fe_conn:
332 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
333 fail_fe_mem:
334 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
335 fail_fe_hw:
336 	dvb_dmxdev_release(&dvb->dmxdev);
337 fail_dmxdev:
338 	dvb_dmx_release(&dvb->demux);
339 fail_dmx:
340 	dvb_unregister_frontend(dvb->frontend);
341 fail_frontend:
342 	dvb_frontend_detach(dvb->frontend);
343 	dvb_unregister_adapter(&dvb->adapter);
344 fail_adapter:
345 	return result;
346 }
347 
au0828_dvb_unregister(struct au0828_dev * dev)348 void au0828_dvb_unregister(struct au0828_dev *dev)
349 {
350 	struct au0828_dvb *dvb = &dev->dvb;
351 
352 	dprintk(1, "%s()\n", __func__);
353 
354 	if (dvb->frontend == NULL)
355 		return;
356 
357 	dvb_net_release(&dvb->net);
358 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
359 	dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
360 	dvb_dmxdev_release(&dvb->dmxdev);
361 	dvb_dmx_release(&dvb->demux);
362 	dvb_unregister_frontend(dvb->frontend);
363 	dvb_frontend_detach(dvb->frontend);
364 	dvb_unregister_adapter(&dvb->adapter);
365 }
366 
367 /* All the DVB attach calls go here, this function get's modified
368  * for each new card. No other function in this file needs
369  * to change.
370  */
au0828_dvb_register(struct au0828_dev * dev)371 int au0828_dvb_register(struct au0828_dev *dev)
372 {
373 	struct au0828_dvb *dvb = &dev->dvb;
374 	int ret;
375 
376 	dprintk(1, "%s()\n", __func__);
377 
378 	/* init frontend */
379 	switch (dev->boardnr) {
380 	case AU0828_BOARD_HAUPPAUGE_HVR850:
381 	case AU0828_BOARD_HAUPPAUGE_HVR950Q:
382 		dvb->frontend = dvb_attach(au8522_attach,
383 				&hauppauge_hvr950q_config,
384 				&dev->i2c_adap);
385 		if (dvb->frontend != NULL)
386 			dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap,
387 				   &hauppauge_hvr950q_tunerconfig);
388 		break;
389 	case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
390 		dvb->frontend = dvb_attach(au8522_attach,
391 				&hauppauge_hvr950q_config,
392 				&dev->i2c_adap);
393 		if (dvb->frontend != NULL)
394 			dvb_attach(mxl5007t_attach, dvb->frontend,
395 				   &dev->i2c_adap, 0x60,
396 				   &mxl5007t_hvr950q_config);
397 		break;
398 	case AU0828_BOARD_HAUPPAUGE_WOODBURY:
399 		dvb->frontend = dvb_attach(au8522_attach,
400 				&hauppauge_woodbury_config,
401 				&dev->i2c_adap);
402 		if (dvb->frontend != NULL)
403 			dvb_attach(tda18271_attach, dvb->frontend,
404 				   0x60, &dev->i2c_adap,
405 				   &hauppauge_woodbury_tunerconfig);
406 		break;
407 	case AU0828_BOARD_DVICO_FUSIONHDTV7:
408 		dvb->frontend = dvb_attach(au8522_attach,
409 				&fusionhdtv7usb_config,
410 				&dev->i2c_adap);
411 		if (dvb->frontend != NULL) {
412 			dvb_attach(xc5000_attach, dvb->frontend,
413 				&dev->i2c_adap,
414 				&hauppauge_hvr950q_tunerconfig);
415 		}
416 		break;
417 	default:
418 		printk(KERN_WARNING "The frontend of your DVB/ATSC card "
419 		       "isn't supported yet\n");
420 		break;
421 	}
422 	if (NULL == dvb->frontend) {
423 		printk(KERN_ERR "%s() Frontend initialization failed\n",
424 		       __func__);
425 		return -1;
426 	}
427 	/* define general-purpose callback pointer */
428 	dvb->frontend->callback = au0828_tuner_callback;
429 
430 	/* register everything */
431 	ret = dvb_register(dev);
432 	if (ret < 0) {
433 		if (dvb->frontend->ops.release)
434 			dvb->frontend->ops.release(dvb->frontend);
435 		return ret;
436 	}
437 
438 	return 0;
439 }
440