[PATCH] tpm: device attribute fixes
[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
26         deb_ts("urb complete feedcount: %d, status: %d, length: %d\n",dib->feedcount,urb->status,
27                         urb->actual_length);
28
29         urb_compl_count++;
30         if (urb_compl_count % 1000 == 0)
31                 deb_info("%d urbs completed so far.\n",urb_compl_count);
32
33         switch (urb->status) {
34                 case 0:         /* success */
35                 case -ETIMEDOUT:    /* NAK */
36                         break;
37                 case -ECONNRESET:   /* kill */
38                 case -ENOENT:
39                 case -ESHUTDOWN:
40                         return;
41                 default:        /* error */
42                         deb_ts("urb completition error %d.", urb->status);
43                         break;
44         }
45
46         if (dib->feedcount > 0 && urb->actual_length > 0) {
47                 if (dib->init_state & DIBUSB_STATE_DVB)
48                         dvb_dmx_swfilter(&dib->demux, (u8*) urb->transfer_buffer,urb->actual_length);
49         } else 
50                 deb_ts("URB dropped because of feedcount.\n");
51
52         usb_submit_urb(urb,GFP_ATOMIC);
53 }
54
55 static int dibusb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff) 
56 {
57         struct usb_dibusb *dib = dvbdmxfeed->demux->priv;
58         int newfeedcount;
59         
60         if (dib == NULL)
61                 return -ENODEV;
62
63         newfeedcount = dib->feedcount + (onoff ? 1 : -1);
64
65         /* 
66          * stop feed before setting a new pid if there will be no pid anymore 
67          */
68         if (newfeedcount == 0) {
69                 deb_ts("stop feeding\n");
70                 if (dib->xfer_ops.fifo_ctrl != NULL) {
71                         if (dib->xfer_ops.fifo_ctrl(dib->fe,0)) {
72                                 err("error while inhibiting fifo.");
73                                 return -ENODEV;
74                         }
75                 }
76                 dibusb_streaming(dib,0);
77         }
78         
79         dib->feedcount = newfeedcount;
80
81         /* activate the pid on the device specific pid_filter */
82         deb_ts("setting pid: %5d %04x at index %d '%s'\n",dvbdmxfeed->pid,dvbdmxfeed->pid,dvbdmxfeed->index,onoff ? "on" : "off");
83         if (dib->pid_parse && dib->xfer_ops.pid_ctrl != NULL)
84                 dib->xfer_ops.pid_ctrl(dib->fe,dvbdmxfeed->index,dvbdmxfeed->pid,onoff);
85
86         /* 
87          * start the feed if this was the first pid to set and there is still a pid
88          * for reception.
89          */
90         if (dib->feedcount == onoff && dib->feedcount > 0) {
91
92                 deb_ts("controlling pid parser\n");
93                 if (dib->xfer_ops.pid_parse != NULL) {
94                         if (dib->xfer_ops.pid_parse(dib->fe,dib->pid_parse) < 0) {
95                                 err("could not handle pid_parser");
96                         }
97                 }
98                 
99                 deb_ts("start feeding\n");
100                 if (dib->xfer_ops.fifo_ctrl != NULL) {
101                         if (dib->xfer_ops.fifo_ctrl(dib->fe,1)) {
102                                 err("error while enabling fifo.");
103                                 return -ENODEV;
104                         }
105                 }
106                 dibusb_streaming(dib,1);
107         }
108         return 0;
109 }
110
111 static int dibusb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
112 {
113         deb_ts("start pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid,dvbdmxfeed->type);
114         return dibusb_ctrl_feed(dvbdmxfeed,1);
115 }
116
117 static int dibusb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
118 {
119         deb_ts("stop pid: 0x%04x, feedtype: %d\n", dvbdmxfeed->pid, dvbdmxfeed->type);
120         return dibusb_ctrl_feed(dvbdmxfeed,0);
121 }
122
123 int dibusb_dvb_init(struct usb_dibusb *dib)
124 {
125         int ret;
126
127         urb_compl_count = 0;
128
129         if ((ret = dvb_register_adapter(&dib->adapter, DRIVER_DESC,
130                         THIS_MODULE)) < 0) {
131                 deb_info("dvb_register_adapter failed: error %d", ret);
132                 goto err;
133         }
134         dib->adapter.priv = dib;
135         
136 /* i2c is done in dibusb_i2c_init */
137         
138         dib->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING;
139
140         dib->demux.priv = (void *)dib;
141         /* get pidcount from demod */
142         dib->demux.feednum = dib->demux.filternum = 255;
143         dib->demux.start_feed = dibusb_start_feed;
144         dib->demux.stop_feed = dibusb_stop_feed;
145         dib->demux.write_to_decoder = NULL;
146         if ((ret = dvb_dmx_init(&dib->demux)) < 0) {
147                 err("dvb_dmx_init failed: error %d",ret);
148                 goto err_dmx;
149         }
150
151         dib->dmxdev.filternum = dib->demux.filternum;
152         dib->dmxdev.demux = &dib->demux.dmx;
153         dib->dmxdev.capabilities = 0;
154         if ((ret = dvb_dmxdev_init(&dib->dmxdev, &dib->adapter)) < 0) {
155                 err("dvb_dmxdev_init failed: error %d",ret);
156                 goto err_dmx_dev;
157         }
158
159         dvb_net_init(&dib->adapter, &dib->dvb_net, &dib->demux.dmx);
160
161         goto success;
162 err_dmx_dev:
163         dvb_dmx_release(&dib->demux);
164 err_dmx:
165         dvb_unregister_adapter(&dib->adapter);
166 err:
167         return ret;
168 success:
169         dib->init_state |= DIBUSB_STATE_DVB;
170         return 0;
171 }
172
173 int dibusb_dvb_exit(struct usb_dibusb *dib)
174 {
175         if (dib->init_state & DIBUSB_STATE_DVB) {
176                 dib->init_state &= ~DIBUSB_STATE_DVB;
177                 deb_info("unregistering DVB part\n");
178                 dvb_net_release(&dib->dvb_net);
179                 dib->demux.dmx.close(&dib->demux.dmx);
180                 dvb_dmxdev_release(&dib->dmxdev);
181                 dvb_dmx_release(&dib->demux);
182                 dvb_unregister_adapter(&dib->adapter);
183         }
184         return 0;
185 }