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