V4L/DVB (10715): zoran: clean up some old V4L1 left-overs and remove the MAP_NR macro.
[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,
177                                         _AU0828_BULKPIPE),
178                                   purb->transfer_buffer,
179                                   URB_BUFSIZE,
180                                   urb_completion,
181                                   dev);
182
183         }
184
185         for (i = 0; i < URB_COUNT; i++) {
186                 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
187                 if (ret != 0) {
188                         stop_urb_transfer(dev);
189                         printk(KERN_ERR "%s: failed urb submission, "
190                                "err = %d\n", __func__, ret);
191                         return ret;
192                 }
193         }
194
195         dev->urb_streaming = 1;
196         ret = 0;
197
198 err:
199         return ret;
200 }
201
202 static int au0828_dvb_start_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 (!demux->dmx.frontend)
212                 return -EINVAL;
213
214         if (dvb) {
215                 mutex_lock(&dvb->lock);
216                 if (dvb->feeding++ == 0) {
217                         /* Start transport */
218                         au0828_write(dev, 0x608, 0x90);
219                         au0828_write(dev, 0x609, 0x72);
220                         au0828_write(dev, 0x60a, 0x71);
221                         au0828_write(dev, 0x60b, 0x01);
222                         ret = start_urb_transfer(dev);
223                 }
224                 mutex_unlock(&dvb->lock);
225         }
226
227         return ret;
228 }
229
230 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
231 {
232         struct dvb_demux *demux = feed->demux;
233         struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
234         struct au0828_dvb *dvb = &dev->dvb;
235         int ret = 0;
236
237         dprintk(1, "%s()\n", __func__);
238
239         if (dvb) {
240                 mutex_lock(&dvb->lock);
241                 if (--dvb->feeding == 0) {
242                         /* Stop transport */
243                         au0828_write(dev, 0x608, 0x00);
244                         au0828_write(dev, 0x609, 0x00);
245                         au0828_write(dev, 0x60a, 0x00);
246                         au0828_write(dev, 0x60b, 0x00);
247                         ret = stop_urb_transfer(dev);
248                 }
249                 mutex_unlock(&dvb->lock);
250         }
251
252         return ret;
253 }
254
255 static int dvb_register(struct au0828_dev *dev)
256 {
257         struct au0828_dvb *dvb = &dev->dvb;
258         int result;
259
260         dprintk(1, "%s()\n", __func__);
261
262         /* register adapter */
263         result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
264                                       &dev->usbdev->dev, adapter_nr);
265         if (result < 0) {
266                 printk(KERN_ERR "%s: dvb_register_adapter failed "
267                        "(errno = %d)\n", DRIVER_NAME, result);
268                 goto fail_adapter;
269         }
270         dvb->adapter.priv = dev;
271
272         /* register frontend */
273         result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
274         if (result < 0) {
275                 printk(KERN_ERR "%s: dvb_register_frontend failed "
276                        "(errno = %d)\n", DRIVER_NAME, result);
277                 goto fail_frontend;
278         }
279
280         /* register demux stuff */
281         dvb->demux.dmx.capabilities =
282                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
283                 DMX_MEMORY_BASED_FILTERING;
284         dvb->demux.priv       = dev;
285         dvb->demux.filternum  = 256;
286         dvb->demux.feednum    = 256;
287         dvb->demux.start_feed = au0828_dvb_start_feed;
288         dvb->demux.stop_feed  = au0828_dvb_stop_feed;
289         result = dvb_dmx_init(&dvb->demux);
290         if (result < 0) {
291                 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
292                        DRIVER_NAME, result);
293                 goto fail_dmx;
294         }
295
296         dvb->dmxdev.filternum    = 256;
297         dvb->dmxdev.demux        = &dvb->demux.dmx;
298         dvb->dmxdev.capabilities = 0;
299         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
300         if (result < 0) {
301                 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
302                        DRIVER_NAME, result);
303                 goto fail_dmxdev;
304         }
305
306         dvb->fe_hw.source = DMX_FRONTEND_0;
307         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
308         if (result < 0) {
309                 printk(KERN_ERR "%s: add_frontend failed "
310                        "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
311                 goto fail_fe_hw;
312         }
313
314         dvb->fe_mem.source = DMX_MEMORY_FE;
315         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
316         if (result < 0) {
317                 printk(KERN_ERR "%s: add_frontend failed "
318                        "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
319                 goto fail_fe_mem;
320         }
321
322         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
323         if (result < 0) {
324                 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
325                        DRIVER_NAME, result);
326                 goto fail_fe_conn;
327         }
328
329         /* register network adapter */
330         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
331         return 0;
332
333 fail_fe_conn:
334         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
335 fail_fe_mem:
336         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
337 fail_fe_hw:
338         dvb_dmxdev_release(&dvb->dmxdev);
339 fail_dmxdev:
340         dvb_dmx_release(&dvb->demux);
341 fail_dmx:
342         dvb_unregister_frontend(dvb->frontend);
343 fail_frontend:
344         dvb_frontend_detach(dvb->frontend);
345         dvb_unregister_adapter(&dvb->adapter);
346 fail_adapter:
347         return result;
348 }
349
350 void au0828_dvb_unregister(struct au0828_dev *dev)
351 {
352         struct au0828_dvb *dvb = &dev->dvb;
353
354         dprintk(1, "%s()\n", __func__);
355
356         if (dvb->frontend == NULL)
357                 return;
358
359         dvb_net_release(&dvb->net);
360         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
361         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
362         dvb_dmxdev_release(&dvb->dmxdev);
363         dvb_dmx_release(&dvb->demux);
364         dvb_unregister_frontend(dvb->frontend);
365         dvb_frontend_detach(dvb->frontend);
366         dvb_unregister_adapter(&dvb->adapter);
367 }
368
369 /* All the DVB attach calls go here, this function get's modified
370  * for each new card. No other function in this file needs
371  * to change.
372  */
373 int au0828_dvb_register(struct au0828_dev *dev)
374 {
375         struct au0828_dvb *dvb = &dev->dvb;
376         int ret;
377
378         dprintk(1, "%s()\n", __func__);
379
380         /* init frontend */
381         switch (dev->board) {
382         case AU0828_BOARD_HAUPPAUGE_HVR850:
383         case AU0828_BOARD_HAUPPAUGE_HVR950Q:
384                 dvb->frontend = dvb_attach(au8522_attach,
385                                 &hauppauge_hvr950q_config,
386                                 &dev->i2c_adap);
387                 if (dvb->frontend != NULL)
388                         dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap,
389                                    &hauppauge_hvr950q_tunerconfig);
390                 break;
391         case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
392                 dvb->frontend = dvb_attach(au8522_attach,
393                                 &hauppauge_hvr950q_config,
394                                 &dev->i2c_adap);
395                 if (dvb->frontend != NULL)
396                         dvb_attach(mxl5007t_attach, dvb->frontend,
397                                    &dev->i2c_adap, 0x60,
398                                    &mxl5007t_hvr950q_config);
399                 break;
400         case AU0828_BOARD_HAUPPAUGE_WOODBURY:
401                 dvb->frontend = dvb_attach(au8522_attach,
402                                 &hauppauge_woodbury_config,
403                                 &dev->i2c_adap);
404                 if (dvb->frontend != NULL)
405                         dvb_attach(tda18271_attach, dvb->frontend,
406                                    0x60, &dev->i2c_adap,
407                                    &hauppauge_woodbury_tunerconfig);
408                 break;
409         case AU0828_BOARD_DVICO_FUSIONHDTV7:
410                 dvb->frontend = dvb_attach(au8522_attach,
411                                 &fusionhdtv7usb_config,
412                                 &dev->i2c_adap);
413                 if (dvb->frontend != NULL) {
414                         dvb_attach(xc5000_attach, dvb->frontend,
415                                 &dev->i2c_adap,
416                                 &hauppauge_hvr950q_tunerconfig);
417                 }
418                 break;
419         default:
420                 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
421                        "isn't supported yet\n");
422                 break;
423         }
424         if (NULL == dvb->frontend) {
425                 printk(KERN_ERR "%s() Frontend initialization failed\n",
426                        __func__);
427                 return -1;
428         }
429         /* define general-purpose callback pointer */
430         dvb->frontend->callback = au0828_tuner_callback;
431
432         /* register everything */
433         ret = dvb_register(dev);
434         if (ret < 0) {
435                 if (dvb->frontend->ops.release)
436                         dvb->frontend->ops.release(dvb->frontend);
437                 return ret;
438         }
439
440         return 0;
441 }