V4L/DVB (6691): pvrusb2: Rework pipeline state control
[safe/jmp/linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-context.c
1 /*
2  *  $Id$
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "pvrusb2-context.h"
22 #include "pvrusb2-io.h"
23 #include "pvrusb2-ioread.h"
24 #include "pvrusb2-hdw.h"
25 #include "pvrusb2-debug.h"
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <asm/semaphore.h>
30
31
32 static void pvr2_context_destroy(struct pvr2_context *mp)
33 {
34         pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
35         if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
36         kfree(mp);
37 }
38
39
40 static void pvr2_context_state_check(struct pvr2_context *mp)
41 {
42         if (mp->init_flag) return;
43
44         switch (pvr2_hdw_get_state(mp->hdw)) {
45         case PVR2_STATE_WARM: break;
46         case PVR2_STATE_ERROR: break;
47         case PVR2_STATE_READY: break;
48         case PVR2_STATE_RUN: break;
49         default: return;
50         }
51
52         pvr2_context_enter(mp); do {
53                 mp->init_flag = !0;
54                 mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw);
55                 if (mp->setup_func) {
56                         mp->setup_func(mp);
57                 }
58         } while (0); pvr2_context_exit(mp);
59  }
60
61
62 struct pvr2_context *pvr2_context_create(
63         struct usb_interface *intf,
64         const struct usb_device_id *devid,
65         void (*setup_func)(struct pvr2_context *))
66 {
67         struct pvr2_context *mp = NULL;
68         mp = kzalloc(sizeof(*mp),GFP_KERNEL);
69         if (!mp) goto done;
70         pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_main id=%p",mp);
71         mp->setup_func = setup_func;
72         mutex_init(&mp->mutex);
73         mp->hdw = pvr2_hdw_create(intf,devid);
74         if (!mp->hdw) {
75                 pvr2_context_destroy(mp);
76                 mp = NULL;
77                 goto done;
78         }
79         pvr2_hdw_set_state_callback(mp->hdw,
80                                     (void (*)(void *))pvr2_context_state_check,
81                                     mp);
82         pvr2_context_state_check(mp);
83  done:
84         return mp;
85 }
86
87
88 void pvr2_context_enter(struct pvr2_context *mp)
89 {
90         mutex_lock(&mp->mutex);
91         pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_enter(id=%p)",mp);
92 }
93
94
95 void pvr2_context_exit(struct pvr2_context *mp)
96 {
97         int destroy_flag = 0;
98         if (!(mp->mc_first || !mp->disconnect_flag)) {
99                 destroy_flag = !0;
100         }
101         pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_exit(id=%p) outside",mp);
102         mutex_unlock(&mp->mutex);
103         if (destroy_flag) pvr2_context_destroy(mp);
104 }
105
106
107 static void pvr2_context_run_checks(struct pvr2_context *mp)
108 {
109         struct pvr2_channel *ch1,*ch2;
110         for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
111                 ch2 = ch1->mc_next;
112                 if (ch1->check_func) {
113                         ch1->check_func(ch1);
114                 }
115         }
116 }
117
118
119 void pvr2_context_disconnect(struct pvr2_context *mp)
120 {
121         pvr2_context_enter(mp); do {
122                 pvr2_hdw_disconnect(mp->hdw);
123                 mp->disconnect_flag = !0;
124                 pvr2_context_run_checks(mp);
125         } while (0); pvr2_context_exit(mp);
126 }
127
128
129 void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
130 {
131         cp->hdw = mp->hdw;
132         cp->mc_head = mp;
133         cp->mc_next = NULL;
134         cp->mc_prev = mp->mc_last;
135         if (mp->mc_last) {
136                 mp->mc_last->mc_next = cp;
137         } else {
138                 mp->mc_first = cp;
139         }
140         mp->mc_last = cp;
141 }
142
143
144 static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
145 {
146         if (!cp->stream) return;
147         pvr2_stream_kill(cp->stream->stream);
148         cp->stream->user = NULL;
149         cp->stream = NULL;
150 }
151
152
153 void pvr2_channel_done(struct pvr2_channel *cp)
154 {
155         struct pvr2_context *mp = cp->mc_head;
156         pvr2_channel_disclaim_stream(cp);
157         if (cp->mc_next) {
158                 cp->mc_next->mc_prev = cp->mc_prev;
159         } else {
160                 mp->mc_last = cp->mc_prev;
161         }
162         if (cp->mc_prev) {
163                 cp->mc_prev->mc_next = cp->mc_next;
164         } else {
165                 mp->mc_first = cp->mc_next;
166         }
167         cp->hdw = NULL;
168 }
169
170
171 int pvr2_channel_claim_stream(struct pvr2_channel *cp,
172                               struct pvr2_context_stream *sp)
173 {
174         int code = 0;
175         pvr2_context_enter(cp->mc_head); do {
176                 if (sp == cp->stream) break;
177                 if (sp->user) {
178                         code = -EBUSY;
179                         break;
180                 }
181                 pvr2_channel_disclaim_stream(cp);
182                 if (!sp) break;
183                 sp->user = cp;
184                 cp->stream = sp;
185         } while (0); pvr2_context_exit(cp->mc_head);
186         return code;
187 }
188
189
190 // This is the marker for the real beginning of a legitimate mpeg2 stream.
191 static char stream_sync_key[] = {
192         0x00, 0x00, 0x01, 0xba,
193 };
194
195 struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
196         struct pvr2_context_stream *sp)
197 {
198         struct pvr2_ioread *cp;
199         cp = pvr2_ioread_create();
200         if (!cp) return NULL;
201         pvr2_ioread_setup(cp,sp->stream);
202         pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
203         return cp;
204 }
205
206
207 /*
208   Stuff for Emacs to see, in order to encourage consistent editing style:
209   *** Local Variables: ***
210   *** mode: c ***
211   *** fill-column: 75 ***
212   *** tab-width: 8 ***
213   *** c-basic-offset: 8 ***
214   *** End: ***
215   */