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