This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / media / dvb / dibusb / dvb-dibusb-dvb.c
1 /*
2  * dvb-dibusb-dvb.c is part of the driver for mobile USB Budget DVB-T devices 
3  * based on reference design made by DiBcom (http://www.dibcom.fr/)
4  *
5  * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
6  *
7  * see dvb-dibusb-core.c for more copyright details.
8  *
9  * This file contains functions for initializing and handling the 
10  * linux-dvb API.
11  */
12 #include "dvb-dibusb.h"
13
14 #include <linux/usb.h>
15 #include <linux/version.h>
16
17 static u32 urb_compl_count;
18
19 /*
20  * MPEG2 TS DVB stuff 
21  */
22 void dibusb_urb_complete(struct urb *urb, struct pt_regs *ptregs)
23 {
24         struct usb_dibusb *dib = urb->context;
25         int ret;
26
27         deb_ts("urb complete feedcount: %d, status: %d, length: %d\n",dib->feedcount,urb->status,
28                         urb->actual_length);
29
30         urb_compl_count++;
31         if (urb_compl_count % 500 == 0)
32                 deb_info("%d urbs completed so far.\n",urb_compl_count);
33
34         switch (urb->status) {
35                 case 0:         /* success */
36                 case -ETIMEDOUT:    /* NAK */
37                         break;
38                 case -ECONNRESET:   /* unlink */
39                 case -ENOENT:
40                 case -ESHUTDOWN:
41                         return;
42                 default:        /* error */
43                         warn("urb completition error %d.", urb->status);
44         }
45
46         if (dib->feedcount > 0) {
47                 deb_ts("URB return len: %d\n",urb->actual_length);
48                 if (urb->actual_length % 188) 
49                         deb_ts("TS Packets: %d, %d\n", urb->actual_length/188,urb->actual_length % 188);
50
51                 /* Francois recommends to drop not full-filled packets, even if they may 
52                  * contain valid TS packets, at least for USB1.1
53                  *
54                  * if (urb->actual_length == dib->dibdev->parm->default_size && dib->dvb_is_ready) */
55                 if (dib->init_state & DIBUSB_STATE_DVB)
56                         dvb_dmx_swfilter(&dib->demux, (u8*) urb->transfer_buffer,urb->actual_length);
57                 else
58                         deb_ts("URB dropped because of the " 
59                                         "actual_length or !dvb_is_ready (%d).\n",dib->init_state & DIBUSB_STATE_DVB);
60         } else 
61                 deb_ts("URB dropped because of feedcount.\n");
62
63         ret = usb_submit_urb(urb,GFP_ATOMIC);
64         deb_ts("urb resubmitted, (%d)\n",ret);
65 }
66
67 static int dibusb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff) 
68 {
69         struct usb_dibusb *dib = dvbdmxfeed->demux->priv;
70         int newfeedcount;
71         
72         if (dib == NULL)
73                 return -ENODEV;
74
75         newfeedcount = dib->feedcount + (onoff ? 1 : -1);
76
77         /* 
78          * stop feed before setting a new pid if there will be no pid anymore 
79          */
80 //      if ((dib->dibdev->parm->firmware_bug && dib->feedcount) || 
81         if (newfeedcount == 0) {
82                 deb_ts("stop feeding\n");
83                 if (dib->xfer_ops.fifo_ctrl != NULL) {
84                         if (dib->xfer_ops.fifo_ctrl(dib->fe,0)) {
85                                 err("error while inhibiting fifo.");
86                                 return -ENODEV;
87                         }
88                 }
89         }
90         
91         dib->feedcount = newfeedcount;
92
93         /* get a free pid from the list and activate it on the device
94          * specific pid_filter
95          */
96         if (dib->pid_parse)
97                 dibusb_ctrl_pid(dib,dvbdmxfeed,onoff);
98
99         /* 
100          * start the feed, either if there is the firmware bug or 
101          * if this was the first pid to set and there is still a pid for 
102          * reception.
103          */
104         
105 //      if ((dib->dibdev->parm->firmware_bug)
106         if (dib->feedcount == onoff && dib->feedcount > 0) {
107
108                 deb_ts("controlling pid parser\n");
109                 if (dib->xfer_ops.pid_parse != NULL) {
110                         if (dib->xfer_ops.pid_parse(dib->fe,dib->pid_parse) < 0) {
111                                 err("could not handle pid_parser");
112                         }
113                 }
114                 
115                 deb_ts("start feeding\n");
116                 if (dib->xfer_ops.fifo_ctrl != NULL) {
117                         if (dib->xfer_ops.fifo_ctrl(dib->fe,1)) {
118                                 err("error while enabling fifo.");
119                                 return -ENODEV;
120                         }
121                 }
122                 dibusb_streaming(dib,1);
123         }
124         return 0;
125 }
126
127 static int dibusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
128 {
129         deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
130         return dibusb_ctrl_feed(dvbdmxfeed,1);
131 }
132
133 static int dibusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
134 {
135         deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
136         return dibusb_ctrl_feed(dvbdmxfeed,0);
137 }
138
139 int dibusb_dvb_init(struct usb_dibusb *dib)
140 {
141         int ret;
142
143         urb_compl_count = 0;
144
145 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,4)
146     if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC)) < 0) {
147 #else
148     if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC , 
149                         THIS_MODULE)) < 0) {
150 #endif
151                 deb_info("dvb_register_adapter failed: error %d", ret);
152                 goto err;
153         }
154         dib->adapter->priv = dib;
155         
156 /* i2c is done in dibusb_i2c_init */
157         
158         dib->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
159
160         dib->demux.priv = (void *)dib;
161         /* get pidcount from demod */
162         dib->demux.feednum = dib->demux.filternum = 255;
163         dib->demux.start_feed = dibusb_start_feed;
164         dib->demux.stop_feed = dibusb_stop_feed;
165         dib->demux.write_to_decoder = NULL;
166         if ((ret = dvb_dmx_init(&dib->demux)) < 0) {
167                 err("dvb_dmx_init failed: error %d",ret);
168                 goto err_dmx;
169         }
170
171         dib->dmxdev.filternum = dib->demux.filternum;
172         dib->dmxdev.demux = &dib->demux.dmx;
173         dib->dmxdev.capabilities = 0;
174         if ((ret = dvb_dmxdev_init(&dib->dmxdev, dib->adapter)) < 0) {
175                 err("dvb_dmxdev_init failed: error %d",ret);
176                 goto err_dmx_dev;
177         }
178
179         dvb_net_init(dib->adapter, &dib->dvb_net, &dib->demux.dmx);
180
181         goto success;
182 err_dmx_dev:
183         dvb_dmx_release(&dib->demux);
184 err_dmx:
185         dvb_unregister_adapter(dib->adapter);
186 err:
187         return ret;
188 success:
189         dib->init_state |= DIBUSB_STATE_DVB;
190         return 0;
191 }
192
193 int dibusb_dvb_exit(struct usb_dibusb *dib)
194 {
195         if (dib->init_state & DIBUSB_STATE_DVB) {
196                 dib->init_state &= ~DIBUSB_STATE_DVB;
197                 deb_info("unregistering DVB part\n");
198                 dvb_net_release(&dib->dvb_net);
199                 dib->demux.dmx.close(&dib->demux.dmx);
200                 dvb_dmxdev_release(&dib->dmxdev);
201                 dvb_dmx_release(&dib->demux);
202                 dvb_unregister_adapter(dib->adapter);
203         }
204         return 0;
205 }