V4L/DVB (7601): em28xx-dvb: add support for the HVR-900
[safe/jmp/linux-2.6] / drivers / media / video / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7         - Fixes for the driver to properly work with HVR-950
8
9  Based on cx88-dvb and saa7134-dvb originally written by:
10         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
11         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
12
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation; either version 2 of the License.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/usb.h>
20
21 #include "em28xx.h"
22 #include <media/v4l2-common.h>
23 #include <media/videobuf-vmalloc.h>
24
25 #include "lgdt330x.h"
26 #include "zl10353.h"
27
28 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
29 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
30 MODULE_LICENSE("GPL");
31
32 static unsigned int debug;
33 module_param(debug, int, 0644);
34 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
35
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37
38 #define dprintk(level, fmt, arg...) do {                        \
39 if (debug >= level)                                             \
40         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg)  \
41 } while (0)
42
43 static int
44 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
45 {
46         struct em28xx_fh *fh = vq->priv_data;
47         struct em28xx        *dev = fh->dev;
48
49         /* FIXME: The better would be to allocate a smaller buffer */
50         *size = 16 * fh->dev->width * fh->dev->height >> 3;
51         if (0 == *count)
52                 *count = EM28XX_DEF_BUF;
53
54         if (*count < EM28XX_MIN_BUF)
55                 *count = EM28XX_MIN_BUF;
56
57         dev->mode = EM28XX_DIGITAL_MODE;
58
59         return 0;
60 }
61
62 /* ------------------------------------------------------------------ */
63
64 static struct lgdt330x_config em2880_lgdt3303_dev = {
65         .demod_address = 0x0e,
66         .demod_chip = LGDT3303,
67 };
68
69 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
70         .demod_address = (0x1e >> 1),
71         .no_tuner = 1,
72         .parallel_ts = 1,
73         .if2 = 45600,
74 };
75
76 /* ------------------------------------------------------------------ */
77
78 static int attach_xc3028(u8 addr, struct em28xx *dev)
79 {
80         struct dvb_frontend *fe;
81         struct xc2028_ctrl ctl;
82         struct xc2028_config cfg;
83
84         memset (&cfg, 0, sizeof(cfg));
85         cfg.i2c_adap  = &dev->i2c_adap;
86         cfg.i2c_addr  = addr;
87         cfg.ctrl      = &ctl;
88         cfg.callback  = em28xx_tuner_callback;
89
90         em28xx_setup_xc3028(dev, &ctl);
91
92         if (!dev->dvb.frontend) {
93                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
94                                 "Can't attach xc3028\n",
95                        dev->name);
96                 return -EINVAL;
97         }
98
99         fe = dvb_attach(xc2028_attach, dev->dvb.frontend, &cfg);
100         if (!fe) {
101                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
102                        dev->name);
103                 dvb_frontend_detach(dev->dvb.frontend);
104                 dvb_unregister_frontend(dev->dvb.frontend);
105                 dev->dvb.frontend = NULL;
106                 return -EINVAL;
107         }
108
109         printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
110
111         return 0;
112 }
113
114 static int dvb_init(struct em28xx *dev)
115 {
116         /* init struct videobuf_dvb */
117         dev->dvb.name = dev->name;
118
119         dev->qops->buf_setup = buffer_setup;
120
121         /* FIXME: Do we need more initialization here? */
122         memset(&dev->dvb_fh, 0, sizeof (dev->dvb_fh));
123         dev->dvb_fh.dev = dev;
124         dev->dvb_fh.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
125
126         videobuf_queue_vmalloc_init(&dev->dvb.dvbq, dev->qops,
127                         &dev->udev->dev, &dev->slock,
128                         V4L2_BUF_TYPE_VIDEO_CAPTURE,
129                         V4L2_FIELD_ALTERNATE,
130                         sizeof(struct em28xx_buffer), &dev->dvb_fh);
131
132         /* init frontend */
133         switch (dev->model) {
134         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
135                 /* Enable lgdt330x */
136                 dev->mode = EM28XX_DIGITAL_MODE;
137                 em28xx_tuner_callback(dev, XC2028_TUNER_RESET, 0);
138
139                 dev->dvb.frontend = dvb_attach(lgdt330x_attach,
140                                                &em2880_lgdt3303_dev,
141                                                &dev->i2c_adap);
142                 if (attach_xc3028(0x61, dev) < 0)
143                         return -EINVAL;
144                 break;
145         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
146                 /* Enable zl10353 */
147                 dev->mode = EM28XX_DIGITAL_MODE;
148                 em28xx_tuner_callback(dev, XC2028_TUNER_RESET, 0);
149                 dev->dvb.frontend = dvb_attach(zl10353_attach,
150                                                &em28xx_zl10353_with_xc3028,
151                                                &dev->i2c_adap);
152                 if (attach_xc3028(0x61, dev) < 0)
153                         return -EINVAL;
154                 break;
155         default:
156                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
157                                 " isn't supported yet\n",
158                        dev->name);
159                 break;
160         }
161         if (NULL == dev->dvb.frontend) {
162                 printk(KERN_ERR
163                        "%s/2: frontend initialization failed\n",
164                        dev->name);
165                 return -EINVAL;
166         }
167
168         /* register everything */
169         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev,
170                                      &dev->udev->dev,
171                                      adapter_nr);
172 }
173
174 static int dvb_fini(struct em28xx *dev)
175 {
176         if (dev->dvb.frontend)
177                 videobuf_dvb_unregister(&dev->dvb);
178
179         return 0;
180 }
181
182 static struct em28xx_ops dvb_ops = {
183         .id   = EM28XX_DVB,
184         .name = "Em28xx dvb Extension",
185         .init = dvb_init,
186         .fini = dvb_fini,
187 };
188
189 static int __init em28xx_dvb_register(void)
190 {
191         return em28xx_register_extension(&dvb_ops);
192 }
193
194 static void __exit em28xx_dvb_unregister(void)
195 {
196         em28xx_unregister_extension(&dvb_ops);
197 }
198
199 module_init(em28xx_dvb_register);
200 module_exit(em28xx_dvb_unregister);