Merge branch 'for-rmk' of git://git.marvell.com/orion
[linux-2.6] / drivers / media / video / au0828 / au0828-dvb.c
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/init.h>
24 #include <linux/device.h>
25 #include <linux/suspend.h>
26 #include <media/v4l2-common.h>
27
28 #include "au0828.h"
29 #include "au8522.h"
30 #include "xc5000.h"
31 #include "mxl5007t.h"
32 #include "tda18271.h"
33
34 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35
36 #define _AU0828_BULKPIPE 0x83
37 #define _BULKPIPESIZE 0xe522
38
39 static struct au8522_config hauppauge_hvr950q_config = {
40         .demod_address = 0x8e >> 1,
41         .status_mode   = AU8522_DEMODLOCKING,
42         .qam_if        = AU8522_IF_6MHZ,
43         .vsb_if        = AU8522_IF_6MHZ,
44 };
45
46 static struct au8522_config hauppauge_woodbury_config = {
47         .demod_address = 0x8e >> 1,
48         .status_mode   = AU8522_DEMODLOCKING,
49         .qam_if        = AU8522_IF_4MHZ,
50         .vsb_if        = AU8522_IF_3_25MHZ,
51 };
52
53 static struct xc5000_config hauppauge_hvr950q_tunerconfig = {
54         .i2c_address      = 0x61,
55         .if_khz           = 6000,
56         .tuner_callback   = au0828_tuner_callback
57 };
58
59 static struct mxl5007t_config mxl5007t_hvr950q_config = {
60         .xtal_freq_hz = MxL_XTAL_24_MHZ,
61         .if_freq_hz = MxL_IF_6_MHZ,
62 };
63
64 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
65         .gate    = TDA18271_GATE_DIGITAL,
66 };
67
68 /*-------------------------------------------------------------------*/
69 static void urb_completion(struct urb *purb)
70 {
71         u8 *ptr;
72         struct au0828_dev *dev = purb->context;
73         int ptype = usb_pipetype(purb->pipe);
74
75         dprintk(2, "%s()\n", __func__);
76
77         if (!dev)
78                 return;
79
80         if (dev->urb_streaming == 0)
81                 return;
82
83         if (ptype != PIPE_BULK) {
84                 printk(KERN_ERR "%s() Unsupported URB type %d\n",
85                        __func__, ptype);
86                 return;
87         }
88
89         ptr = (u8 *)purb->transfer_buffer;
90
91         /* Feed the transport payload into the kernel demux */
92         dvb_dmx_swfilter_packets(&dev->dvb.demux,
93                 purb->transfer_buffer, purb->actual_length / 188);
94
95         /* Clean the buffer before we requeue */
96         memset(purb->transfer_buffer, 0, URB_BUFSIZE);
97
98         /* Requeue URB */
99         usb_submit_urb(purb, GFP_ATOMIC);
100 }
101
102 static int stop_urb_transfer(struct au0828_dev *dev)
103 {
104         int i;
105
106         dprintk(2, "%s()\n", __func__);
107
108         for (i = 0; i < URB_COUNT; i++) {
109                 usb_kill_urb(dev->urbs[i]);
110                 kfree(dev->urbs[i]->transfer_buffer);
111                 usb_free_urb(dev->urbs[i]);
112         }
113
114         dev->urb_streaming = 0;
115
116         return 0;
117 }
118
119 static int start_urb_transfer(struct au0828_dev *dev)
120 {
121         struct urb *purb;
122         int i, ret = -ENOMEM;
123
124         dprintk(2, "%s()\n", __func__);
125
126         if (dev->urb_streaming) {
127                 dprintk(2, "%s: iso xfer already running!\n", __func__);
128                 return 0;
129         }
130
131         for (i = 0; i < URB_COUNT; i++) {
132
133                 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
134                 if (!dev->urbs[i])
135                         goto err;
136
137                 purb = dev->urbs[i];
138
139                 purb->transfer_buffer = kzalloc(URB_BUFSIZE, GFP_KERNEL);
140                 if (!purb->transfer_buffer) {
141                         usb_free_urb(purb);
142                         dev->urbs[i] = NULL;
143                         goto err;
144                 }
145
146                 purb->status = -EINPROGRESS;
147                 usb_fill_bulk_urb(purb,
148                                   dev->usbdev,
149                                   usb_rcvbulkpipe(dev->usbdev, _AU0828_BULKPIPE),
150                                   purb->transfer_buffer,
151                                   URB_BUFSIZE,
152                                   urb_completion,
153                                   dev);
154
155         }
156
157         for (i = 0; i < URB_COUNT; i++) {
158                 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
159                 if (ret != 0) {
160                         stop_urb_transfer(dev);
161                         printk(KERN_ERR "%s: failed urb submission, "
162                                "err = %d\n", __func__, ret);
163                         return ret;
164                 }
165         }
166
167         dev->urb_streaming = 1;
168         ret = 0;
169
170 err:
171         return ret;
172 }
173
174 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
175 {
176         struct dvb_demux *demux = feed->demux;
177         struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
178         struct au0828_dvb *dvb = &dev->dvb;
179         int ret = 0;
180
181         dprintk(1, "%s()\n", __func__);
182
183         if (!demux->dmx.frontend)
184                 return -EINVAL;
185
186         if (dvb) {
187                 mutex_lock(&dvb->lock);
188                 if (dvb->feeding++ == 0) {
189                         /* Start transport */
190                         au0828_write(dev, 0x608, 0x90);
191                         au0828_write(dev, 0x609, 0x72);
192                         au0828_write(dev, 0x60a, 0x71);
193                         au0828_write(dev, 0x60b, 0x01);
194                         ret = start_urb_transfer(dev);
195                 }
196                 mutex_unlock(&dvb->lock);
197         }
198
199         return ret;
200 }
201
202 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
203 {
204         struct dvb_demux *demux = feed->demux;
205         struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
206         struct au0828_dvb *dvb = &dev->dvb;
207         int ret = 0;
208
209         dprintk(1, "%s()\n", __func__);
210
211         if (dvb) {
212                 mutex_lock(&dvb->lock);
213                 if (--dvb->feeding == 0) {
214                         /* Stop transport */
215                         au0828_write(dev, 0x608, 0x00);
216                         au0828_write(dev, 0x609, 0x00);
217                         au0828_write(dev, 0x60a, 0x00);
218                         au0828_write(dev, 0x60b, 0x00);
219                         ret = stop_urb_transfer(dev);
220                 }
221                 mutex_unlock(&dvb->lock);
222         }
223
224         return ret;
225 }
226
227 static int dvb_register(struct au0828_dev *dev)
228 {
229         struct au0828_dvb *dvb = &dev->dvb;
230         int result;
231
232         dprintk(1, "%s()\n", __func__);
233
234         /* register adapter */
235         result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
236                                       &dev->usbdev->dev, adapter_nr);
237         if (result < 0) {
238                 printk(KERN_ERR "%s: dvb_register_adapter failed "
239                        "(errno = %d)\n", DRIVER_NAME, result);
240                 goto fail_adapter;
241         }
242         dvb->adapter.priv = dev;
243
244         /* register frontend */
245         result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
246         if (result < 0) {
247                 printk(KERN_ERR "%s: dvb_register_frontend failed "
248                        "(errno = %d)\n", DRIVER_NAME, result);
249                 goto fail_frontend;
250         }
251
252         /* register demux stuff */
253         dvb->demux.dmx.capabilities =
254                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
255                 DMX_MEMORY_BASED_FILTERING;
256         dvb->demux.priv       = dev;
257         dvb->demux.filternum  = 256;
258         dvb->demux.feednum    = 256;
259         dvb->demux.start_feed = au0828_dvb_start_feed;
260         dvb->demux.stop_feed  = au0828_dvb_stop_feed;
261         result = dvb_dmx_init(&dvb->demux);
262         if (result < 0) {
263                 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
264                        DRIVER_NAME, result);
265                 goto fail_dmx;
266         }
267
268         dvb->dmxdev.filternum    = 256;
269         dvb->dmxdev.demux        = &dvb->demux.dmx;
270         dvb->dmxdev.capabilities = 0;
271         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
272         if (result < 0) {
273                 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
274                        DRIVER_NAME, result);
275                 goto fail_dmxdev;
276         }
277
278         dvb->fe_hw.source = DMX_FRONTEND_0;
279         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
280         if (result < 0) {
281                 printk(KERN_ERR "%s: add_frontend failed "
282                        "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
283                 goto fail_fe_hw;
284         }
285
286         dvb->fe_mem.source = DMX_MEMORY_FE;
287         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
288         if (result < 0) {
289                 printk(KERN_ERR "%s: add_frontend failed "
290                        "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
291                 goto fail_fe_mem;
292         }
293
294         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
295         if (result < 0) {
296                 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
297                        DRIVER_NAME, result);
298                 goto fail_fe_conn;
299         }
300
301         /* register network adapter */
302         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
303         return 0;
304
305 fail_fe_conn:
306         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
307 fail_fe_mem:
308         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
309 fail_fe_hw:
310         dvb_dmxdev_release(&dvb->dmxdev);
311 fail_dmxdev:
312         dvb_dmx_release(&dvb->demux);
313 fail_dmx:
314         dvb_unregister_frontend(dvb->frontend);
315 fail_frontend:
316         dvb_frontend_detach(dvb->frontend);
317         dvb_unregister_adapter(&dvb->adapter);
318 fail_adapter:
319         return result;
320 }
321
322 void au0828_dvb_unregister(struct au0828_dev *dev)
323 {
324         struct au0828_dvb *dvb = &dev->dvb;
325
326         dprintk(1, "%s()\n", __func__);
327
328         if (dvb->frontend == NULL)
329                 return;
330
331         dvb_net_release(&dvb->net);
332         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
333         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
334         dvb_dmxdev_release(&dvb->dmxdev);
335         dvb_dmx_release(&dvb->demux);
336         dvb_unregister_frontend(dvb->frontend);
337         dvb_frontend_detach(dvb->frontend);
338         dvb_unregister_adapter(&dvb->adapter);
339 }
340
341 /* All the DVB attach calls go here, this function get's modified
342  * for each new card. No other function in this file needs
343  * to change.
344  */
345 int au0828_dvb_register(struct au0828_dev *dev)
346 {
347         struct au0828_dvb *dvb = &dev->dvb;
348         int ret;
349
350         dprintk(1, "%s()\n", __func__);
351
352         /* init frontend */
353         switch (dev->board) {
354         case AU0828_BOARD_HAUPPAUGE_HVR850:
355         case AU0828_BOARD_HAUPPAUGE_HVR950Q:
356         case AU0828_BOARD_DVICO_FUSIONHDTV7:
357                 dvb->frontend = dvb_attach(au8522_attach,
358                                 &hauppauge_hvr950q_config,
359                                 &dev->i2c_adap);
360                 if (dvb->frontend != NULL)
361                         dvb_attach(xc5000_attach, dvb->frontend,
362                                 &dev->i2c_adap,
363                                 &hauppauge_hvr950q_tunerconfig, dev);
364                 break;
365         case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
366                 dvb->frontend = dvb_attach(au8522_attach,
367                                 &hauppauge_hvr950q_config,
368                                 &dev->i2c_adap);
369                 if (dvb->frontend != NULL)
370                         dvb_attach(mxl5007t_attach, dvb->frontend,
371                                    &dev->i2c_adap, 0x60,
372                                    &mxl5007t_hvr950q_config);
373                 break;
374         case AU0828_BOARD_HAUPPAUGE_WOODBURY:
375                 dvb->frontend = dvb_attach(au8522_attach,
376                                 &hauppauge_woodbury_config,
377                                 &dev->i2c_adap);
378                 if (dvb->frontend != NULL)
379                         dvb_attach(tda18271_attach, dvb->frontend,
380                                    0x60, &dev->i2c_adap,
381                                    &hauppauge_woodbury_tunerconfig);
382                 break;
383         default:
384                 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
385                        "isn't supported yet\n");
386                 break;
387         }
388         if (NULL == dvb->frontend) {
389                 printk(KERN_ERR "%s() Frontend initialization failed\n",
390                        __func__);
391                 return -1;
392         }
393
394         /* register everything */
395         ret = dvb_register(dev);
396         if (ret < 0) {
397                 if (dvb->frontend->ops.release)
398                         dvb->frontend->ops.release(dvb->frontend);
399                 return ret;
400         }
401
402         return 0;
403 }