V4L/DVB (7593): em28xx: add a module to handle dvb
[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  Based on cx88-dvb and saa7134-dvb originally written by:
7         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
8         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/usb.h>
17
18 #include "em28xx.h"
19 #include <media/v4l2-common.h>
20 #include <media/videobuf-vmalloc.h>
21
22 #include "lgdt330x.h"
23 #include "tuner-xc2028.h"
24 #include "tuner-xc2028-types.h"
25
26 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
27 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
28 MODULE_LICENSE("GPL");
29
30 static unsigned int debug;
31 module_param(debug, int, 0644);
32 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
33
34 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35
36 #define dprintk(level, fmt, arg...) do {                        \
37 if (debug >= level)                                             \
38         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg)  \
39 } while (0)
40
41 static int
42 buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
43 {
44         struct em28xx_fh *fh = vq->priv_data;
45         struct em28xx        *dev = fh->dev;
46
47         *size = 16 * fh->dev->width * fh->dev->height >> 3;
48         if (0 == *count)
49                 *count = EM28XX_DEF_BUF;
50
51         if (*count < EM28XX_MIN_BUF)
52                 *count = EM28XX_MIN_BUF;
53
54         dev->mode = EM28XX_DIGITAL_MODE;
55
56         return 0;
57 }
58
59 /* ------------------------------------------------------------------ */
60
61 /* Add demods here */
62
63 /* ------------------------------------------------------------------ */
64
65 static int attach_xc3028(u8 addr, struct em28xx *dev)
66 {
67         struct dvb_frontend *fe;
68         struct xc2028_ctrl ctl;
69         struct xc2028_config cfg = {
70                 .i2c_adap  = &dev->i2c_adap,
71                 .i2c_addr  = addr,
72                 .ctrl      = &ctl,
73         };
74
75         if (!dev->dvb.frontend) {
76                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
77                                 "Can't attach xc3028\n",
78                        dev->name);
79                 return -EINVAL;
80         }
81
82         fe = dvb_attach(xc2028_attach, dev->dvb.frontend, &cfg);
83         if (!fe) {
84                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
85                        dev->name);
86                 dvb_frontend_detach(dev->dvb.frontend);
87                 dvb_unregister_frontend(dev->dvb.frontend);
88                 dev->dvb.frontend = NULL;
89                 return -EINVAL;
90         }
91
92         printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
93
94         return 0;
95 }
96
97 static int dvb_init(struct em28xx *dev)
98 {
99         /* init struct videobuf_dvb */
100         dev->dvb.name = dev->name;
101
102         dev->qops->buf_setup = buffer_setup;
103
104         videobuf_queue_vmalloc_init(&dev->dvb.dvbq, dev->qops,
105                         &dev->udev->dev, &dev->slock,
106                         V4L2_BUF_TYPE_VIDEO_CAPTURE,
107                         V4L2_FIELD_ALTERNATE,
108                         sizeof(struct em28xx_buffer), dev);
109
110         /* init frontend */
111         switch (dev->model) {
112         default:
113                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
114                                 " isn't supported yet\n",
115                        dev->name);
116                 break;
117         }
118         if (NULL == dev->dvb.frontend) {
119                 printk(KERN_ERR
120                        "%s/2: frontend initialization failed\n",
121                        dev->name);
122                 return -EINVAL;
123         }
124
125         /* register everything */
126         return videobuf_dvb_register(&dev->dvb, THIS_MODULE, dev,
127                                      &dev->udev->dev,
128                                      adapter_nr);
129 }
130
131 static int dvb_fini(struct em28xx *dev)
132 {
133         if (dev->dvb.frontend)
134                 videobuf_dvb_unregister(&dev->dvb);
135
136         return 0;
137 }
138
139 static struct em28xx_ops dvb_ops = {
140         .id   = EM28XX_DVB,
141         .name = "Em28xx dvb Extension",
142         .init = dvb_init,
143         .fini = dvb_fini,
144 };
145
146 static int __init em28xx_dvb_register(void)
147 {
148         return em28xx_register_extension(&dvb_ops);
149 }
150
151 static void __exit em28xx_dvb_unregister(void)
152 {
153         em28xx_unregister_extension(&dvb_ops);
154 }
155
156 module_init(em28xx_dvb_register);
157 module_exit(em28xx_dvb_unregister);