V4L/DVB (8530): au0828: add support for new revision of HVR950Q
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / au0828 / au0828-dvb.c
blob584a83a94a2a276501f77864321c67f1ce065ca2
1 /*
2 * Driver for the Auvitek USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
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.
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
15 * GNU General Public License for more details.
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.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/device.h>
25 #include <linux/suspend.h>
26 #include <media/v4l2-common.h>
28 #include "au0828.h"
29 #include "au8522.h"
30 #include "xc5000.h"
31 #include "mxl5007t.h"
33 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35 #define _AU0828_BULKPIPE 0x83
36 #define _BULKPIPESIZE 0xe522
38 static struct au8522_config hauppauge_hvr950q_config = {
39 .demod_address = 0x8e >> 1,
40 .status_mode = AU8522_DEMODLOCKING,
43 static struct xc5000_config hauppauge_hvr950q_tunerconfig = {
44 .i2c_address = 0x61,
45 .if_khz = 6000,
46 .tuner_callback = au0828_tuner_callback
49 static struct mxl5007t_config mxl5007t_hvr950q_config = {
50 .xtal_freq_hz = MxL_XTAL_24_MHZ,
51 .if_freq_hz = MxL_IF_6_MHZ,
54 /*-------------------------------------------------------------------*/
55 static void urb_completion(struct urb *purb)
57 u8 *ptr;
58 struct au0828_dev *dev = purb->context;
59 int ptype = usb_pipetype(purb->pipe);
61 dprintk(2, "%s()\n", __func__);
63 if (!dev)
64 return;
66 if (dev->urb_streaming == 0)
67 return;
69 if (ptype != PIPE_BULK) {
70 printk(KERN_ERR "%s() Unsupported URB type %d\n",
71 __func__, ptype);
72 return;
75 ptr = (u8 *)purb->transfer_buffer;
77 /* Feed the transport payload into the kernel demux */
78 dvb_dmx_swfilter_packets(&dev->dvb.demux,
79 purb->transfer_buffer, purb->actual_length / 188);
81 /* Clean the buffer before we requeue */
82 memset(purb->transfer_buffer, 0, URB_BUFSIZE);
84 /* Requeue URB */
85 usb_submit_urb(purb, GFP_ATOMIC);
88 static int stop_urb_transfer(struct au0828_dev *dev)
90 int i;
92 dprintk(2, "%s()\n", __func__);
94 for (i = 0; i < URB_COUNT; i++) {
95 usb_kill_urb(dev->urbs[i]);
96 kfree(dev->urbs[i]->transfer_buffer);
97 usb_free_urb(dev->urbs[i]);
100 dev->urb_streaming = 0;
102 return 0;
105 static int start_urb_transfer(struct au0828_dev *dev)
107 struct urb *purb;
108 int i, ret = -ENOMEM;
110 dprintk(2, "%s()\n", __func__);
112 if (dev->urb_streaming) {
113 dprintk(2, "%s: iso xfer already running!\n", __func__);
114 return 0;
117 for (i = 0; i < URB_COUNT; i++) {
119 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
120 if (!dev->urbs[i])
121 goto err;
123 purb = dev->urbs[i];
125 purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
126 if (!purb->transfer_buffer) {
127 usb_free_urb(purb);
128 dev->urbs[i] = NULL;
129 goto err;
132 purb->status = -EINPROGRESS;
133 usb_fill_bulk_urb(purb,
134 dev->usbdev,
135 usb_rcvbulkpipe(dev->usbdev, _AU0828_BULKPIPE),
136 purb->transfer_buffer,
137 URB_BUFSIZE,
138 urb_completion,
139 dev);
143 for (i = 0; i < URB_COUNT; i++) {
144 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
145 if (ret != 0) {
146 stop_urb_transfer(dev);
147 printk(KERN_ERR "%s: failed urb submission, "
148 "err = %d\n", __func__, ret);
149 return ret;
153 dev->urb_streaming = 1;
154 ret = 0;
156 err:
157 return ret;
160 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
162 struct dvb_demux *demux = feed->demux;
163 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
164 struct au0828_dvb *dvb = &dev->dvb;
165 int ret = 0;
167 dprintk(1, "%s()\n", __func__);
169 if (!demux->dmx.frontend)
170 return -EINVAL;
172 if (dvb) {
173 mutex_lock(&dvb->lock);
174 if (dvb->feeding++ == 0) {
175 /* Start transport */
176 au0828_write(dev, 0x608, 0x90);
177 au0828_write(dev, 0x609, 0x72);
178 au0828_write(dev, 0x60a, 0x71);
179 au0828_write(dev, 0x60b, 0x01);
180 ret = start_urb_transfer(dev);
182 mutex_unlock(&dvb->lock);
185 return ret;
188 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
190 struct dvb_demux *demux = feed->demux;
191 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
192 struct au0828_dvb *dvb = &dev->dvb;
193 int ret = 0;
195 dprintk(1, "%s()\n", __func__);
197 if (dvb) {
198 mutex_lock(&dvb->lock);
199 if (--dvb->feeding == 0) {
200 /* Stop transport */
201 au0828_write(dev, 0x608, 0x00);
202 au0828_write(dev, 0x609, 0x00);
203 au0828_write(dev, 0x60a, 0x00);
204 au0828_write(dev, 0x60b, 0x00);
205 ret = stop_urb_transfer(dev);
207 mutex_unlock(&dvb->lock);
210 return ret;
213 static int dvb_register(struct au0828_dev *dev)
215 struct au0828_dvb *dvb = &dev->dvb;
216 int result;
218 dprintk(1, "%s()\n", __func__);
220 /* register adapter */
221 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
222 &dev->usbdev->dev, adapter_nr);
223 if (result < 0) {
224 printk(KERN_ERR "%s: dvb_register_adapter failed "
225 "(errno = %d)\n", DRIVER_NAME, result);
226 goto fail_adapter;
228 dvb->adapter.priv = dev;
230 /* register frontend */
231 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
232 if (result < 0) {
233 printk(KERN_ERR "%s: dvb_register_frontend failed "
234 "(errno = %d)\n", DRIVER_NAME, result);
235 goto fail_frontend;
238 /* register demux stuff */
239 dvb->demux.dmx.capabilities =
240 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
241 DMX_MEMORY_BASED_FILTERING;
242 dvb->demux.priv = dev;
243 dvb->demux.filternum = 256;
244 dvb->demux.feednum = 256;
245 dvb->demux.start_feed = au0828_dvb_start_feed;
246 dvb->demux.stop_feed = au0828_dvb_stop_feed;
247 result = dvb_dmx_init(&dvb->demux);
248 if (result < 0) {
249 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
250 DRIVER_NAME, result);
251 goto fail_dmx;
254 dvb->dmxdev.filternum = 256;
255 dvb->dmxdev.demux = &dvb->demux.dmx;
256 dvb->dmxdev.capabilities = 0;
257 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
258 if (result < 0) {
259 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
260 DRIVER_NAME, result);
261 goto fail_dmxdev;
264 dvb->fe_hw.source = DMX_FRONTEND_0;
265 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
266 if (result < 0) {
267 printk(KERN_ERR "%s: add_frontend failed "
268 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
269 goto fail_fe_hw;
272 dvb->fe_mem.source = DMX_MEMORY_FE;
273 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
274 if (result < 0) {
275 printk(KERN_ERR "%s: add_frontend failed "
276 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
277 goto fail_fe_mem;
280 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
281 if (result < 0) {
282 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
283 DRIVER_NAME, result);
284 goto fail_fe_conn;
287 /* register network adapter */
288 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
289 return 0;
291 fail_fe_conn:
292 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
293 fail_fe_mem:
294 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
295 fail_fe_hw:
296 dvb_dmxdev_release(&dvb->dmxdev);
297 fail_dmxdev:
298 dvb_dmx_release(&dvb->demux);
299 fail_dmx:
300 dvb_unregister_frontend(dvb->frontend);
301 fail_frontend:
302 dvb_frontend_detach(dvb->frontend);
303 dvb_unregister_adapter(&dvb->adapter);
304 fail_adapter:
305 return result;
308 void au0828_dvb_unregister(struct au0828_dev *dev)
310 struct au0828_dvb *dvb = &dev->dvb;
312 dprintk(1, "%s()\n", __func__);
314 if (dvb->frontend == NULL)
315 return;
317 dvb_net_release(&dvb->net);
318 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
319 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
320 dvb_dmxdev_release(&dvb->dmxdev);
321 dvb_dmx_release(&dvb->demux);
322 dvb_unregister_frontend(dvb->frontend);
323 dvb_frontend_detach(dvb->frontend);
324 dvb_unregister_adapter(&dvb->adapter);
327 /* All the DVB attach calls go here, this function get's modified
328 * for each new card. No other function in this file needs
329 * to change.
331 int au0828_dvb_register(struct au0828_dev *dev)
333 struct au0828_dvb *dvb = &dev->dvb;
334 int ret;
336 dprintk(1, "%s()\n", __func__);
338 /* init frontend */
339 switch (dev->board) {
340 case AU0828_BOARD_HAUPPAUGE_HVR850:
341 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
342 case AU0828_BOARD_DVICO_FUSIONHDTV7:
343 dvb->frontend = dvb_attach(au8522_attach,
344 &hauppauge_hvr950q_config,
345 &dev->i2c_adap);
346 if (dvb->frontend != NULL)
347 dvb_attach(xc5000_attach, dvb->frontend,
348 &dev->i2c_adap,
349 &hauppauge_hvr950q_tunerconfig, dev);
350 break;
351 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
352 dvb->frontend = dvb_attach(au8522_attach,
353 &hauppauge_hvr950q_config,
354 &dev->i2c_adap);
355 if (dvb->frontend != NULL)
356 dvb_attach(mxl5007t_attach, dvb->frontend,
357 &dev->i2c_adap, 0x60,
358 &mxl5007t_hvr950q_config);
359 break;
360 default:
361 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
362 "isn't supported yet\n");
363 break;
365 if (NULL == dvb->frontend) {
366 printk(KERN_ERR "%s() Frontend initialization failed\n",
367 __func__);
368 return -1;
371 /* register everything */
372 ret = dvb_register(dev);
373 if (ret < 0) {
374 if (dvb->frontend->ops.release)
375 dvb->frontend->ops.release(dvb->frontend);
376 return ret;
379 return 0;