]> Pileus Git - ~andy/linux/blob - drivers/media/video/pvrusb2/pvrusb2-context.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/hskinnemoen...
[~andy/linux] / 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
30
31 static void pvr2_context_destroy(struct pvr2_context *mp)
32 {
33         pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
34         if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
35         kfree(mp);
36 }
37
38
39 static void pvr2_context_state_check(struct pvr2_context *mp)
40 {
41         if (mp->init_flag) return;
42
43         switch (pvr2_hdw_get_state(mp->hdw)) {
44         case PVR2_STATE_WARM: break;
45         case PVR2_STATE_ERROR: break;
46         case PVR2_STATE_READY: break;
47         case PVR2_STATE_RUN: break;
48         default: return;
49         }
50
51         pvr2_context_enter(mp); do {
52                 mp->init_flag = !0;
53                 mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw);
54                 if (mp->setup_func) {
55                         mp->setup_func(mp);
56                 }
57         } while (0); pvr2_context_exit(mp);
58  }
59
60
61 struct pvr2_context *pvr2_context_create(
62         struct usb_interface *intf,
63         const struct usb_device_id *devid,
64         void (*setup_func)(struct pvr2_context *))
65 {
66         struct pvr2_context *mp = NULL;
67         mp = kzalloc(sizeof(*mp),GFP_KERNEL);
68         if (!mp) goto done;
69         pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_main id=%p",mp);
70         mp->setup_func = setup_func;
71         mutex_init(&mp->mutex);
72         mp->hdw = pvr2_hdw_create(intf,devid);
73         if (!mp->hdw) {
74                 pvr2_context_destroy(mp);
75                 mp = NULL;
76                 goto done;
77         }
78         pvr2_hdw_set_state_callback(mp->hdw,
79                                     (void (*)(void *))pvr2_context_state_check,
80                                     mp);
81         pvr2_context_state_check(mp);
82  done:
83         return mp;
84 }
85
86
87 void pvr2_context_enter(struct pvr2_context *mp)
88 {
89         mutex_lock(&mp->mutex);
90         pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_enter(id=%p)",mp);
91 }
92
93
94 void pvr2_context_exit(struct pvr2_context *mp)
95 {
96         int destroy_flag = 0;
97         if (!(mp->mc_first || !mp->disconnect_flag)) {
98                 destroy_flag = !0;
99         }
100         pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_exit(id=%p) outside",mp);
101         mutex_unlock(&mp->mutex);
102         if (destroy_flag) pvr2_context_destroy(mp);
103 }
104
105
106 static void pvr2_context_run_checks(struct pvr2_context *mp)
107 {
108         struct pvr2_channel *ch1,*ch2;
109         for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
110                 ch2 = ch1->mc_next;
111                 if (ch1->check_func) {
112                         ch1->check_func(ch1);
113                 }
114         }
115 }
116
117
118 void pvr2_context_disconnect(struct pvr2_context *mp)
119 {
120         pvr2_context_enter(mp); do {
121                 pvr2_hdw_disconnect(mp->hdw);
122                 mp->disconnect_flag = !0;
123                 pvr2_context_run_checks(mp);
124         } while (0); pvr2_context_exit(mp);
125 }
126
127
128 void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
129 {
130         cp->hdw = mp->hdw;
131         cp->mc_head = mp;
132         cp->mc_next = NULL;
133         cp->mc_prev = mp->mc_last;
134         if (mp->mc_last) {
135                 mp->mc_last->mc_next = cp;
136         } else {
137                 mp->mc_first = cp;
138         }
139         mp->mc_last = cp;
140 }
141
142
143 static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
144 {
145         if (!cp->stream) return;
146         pvr2_stream_kill(cp->stream->stream);
147         cp->stream->user = NULL;
148         cp->stream = NULL;
149 }
150
151
152 void pvr2_channel_done(struct pvr2_channel *cp)
153 {
154         struct pvr2_context *mp = cp->mc_head;
155         pvr2_channel_disclaim_stream(cp);
156         if (cp->mc_next) {
157                 cp->mc_next->mc_prev = cp->mc_prev;
158         } else {
159                 mp->mc_last = cp->mc_prev;
160         }
161         if (cp->mc_prev) {
162                 cp->mc_prev->mc_next = cp->mc_next;
163         } else {
164                 mp->mc_first = cp->mc_next;
165         }
166         cp->hdw = NULL;
167 }
168
169
170 int pvr2_channel_claim_stream(struct pvr2_channel *cp,
171                               struct pvr2_context_stream *sp)
172 {
173         int code = 0;
174         pvr2_context_enter(cp->mc_head); do {
175                 if (sp == cp->stream) break;
176                 if (sp->user) {
177                         code = -EBUSY;
178                         break;
179                 }
180                 pvr2_channel_disclaim_stream(cp);
181                 if (!sp) break;
182                 sp->user = cp;
183                 cp->stream = sp;
184         } while (0); pvr2_context_exit(cp->mc_head);
185         return code;
186 }
187
188
189 // This is the marker for the real beginning of a legitimate mpeg2 stream.
190 static char stream_sync_key[] = {
191         0x00, 0x00, 0x01, 0xba,
192 };
193
194 struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
195         struct pvr2_context_stream *sp)
196 {
197         struct pvr2_ioread *cp;
198         cp = pvr2_ioread_create();
199         if (!cp) return NULL;
200         pvr2_ioread_setup(cp,sp->stream);
201         pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
202         return cp;
203 }
204
205
206 /*
207   Stuff for Emacs to see, in order to encourage consistent editing style:
208   *** Local Variables: ***
209   *** mode: c ***
210   *** fill-column: 75 ***
211   *** tab-width: 8 ***
212   *** c-basic-offset: 8 ***
213   *** End: ***
214   */