4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
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
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.
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
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>
32 static void pvr2_context_destroy(struct pvr2_context *mp)
34 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
35 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
37 flush_workqueue(mp->workqueue);
38 destroy_workqueue(mp->workqueue);
44 static void pvr2_context_trigger_poll(struct pvr2_context *mp)
46 queue_work(mp->workqueue,&mp->workpoll);
50 static void pvr2_context_poll(struct work_struct *work)
52 struct pvr2_context *mp =
53 container_of(work, struct pvr2_context, workpoll);
54 pvr2_context_enter(mp); do {
55 pvr2_hdw_poll(mp->hdw);
56 } while (0); pvr2_context_exit(mp);
60 static void pvr2_context_setup(struct work_struct *work)
62 struct pvr2_context *mp =
63 container_of(work, struct pvr2_context, workinit);
65 pvr2_context_enter(mp); do {
66 if (!pvr2_hdw_dev_ok(mp->hdw)) break;
67 pvr2_hdw_setup(mp->hdw);
68 pvr2_hdw_setup_poll_trigger(
70 (void (*)(void *))pvr2_context_trigger_poll,
72 if (!pvr2_hdw_dev_ok(mp->hdw)) break;
73 if (!pvr2_hdw_init_ok(mp->hdw)) break;
74 mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw);
78 } while (0); pvr2_context_exit(mp);
82 struct pvr2_context *pvr2_context_create(
83 struct usb_interface *intf,
84 const struct usb_device_id *devid,
85 void (*setup_func)(struct pvr2_context *))
87 struct pvr2_context *mp = NULL;
88 mp = kzalloc(sizeof(*mp),GFP_KERNEL);
90 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_main id=%p",mp);
91 mp->setup_func = setup_func;
92 mutex_init(&mp->mutex);
93 mp->hdw = pvr2_hdw_create(intf,devid);
95 pvr2_context_destroy(mp);
100 mp->workqueue = create_singlethread_workqueue("pvrusb2");
101 INIT_WORK(&mp->workinit, pvr2_context_setup);
102 INIT_WORK(&mp->workpoll, pvr2_context_poll);
103 queue_work(mp->workqueue,&mp->workinit);
109 void pvr2_context_enter(struct pvr2_context *mp)
111 mutex_lock(&mp->mutex);
112 pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_enter(id=%p)",mp);
116 void pvr2_context_exit(struct pvr2_context *mp)
118 int destroy_flag = 0;
119 if (!(mp->mc_first || !mp->disconnect_flag)) {
122 pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_exit(id=%p) outside",mp);
123 mutex_unlock(&mp->mutex);
124 if (destroy_flag) pvr2_context_destroy(mp);
128 static void pvr2_context_run_checks(struct pvr2_context *mp)
130 struct pvr2_channel *ch1,*ch2;
131 for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
133 if (ch1->check_func) {
134 ch1->check_func(ch1);
140 void pvr2_context_disconnect(struct pvr2_context *mp)
142 pvr2_context_enter(mp); do {
143 pvr2_hdw_disconnect(mp->hdw);
144 mp->disconnect_flag = !0;
145 pvr2_context_run_checks(mp);
146 } while (0); pvr2_context_exit(mp);
150 void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
155 cp->mc_prev = mp->mc_last;
157 mp->mc_last->mc_next = cp;
165 static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
167 if (!cp->stream) return;
168 pvr2_stream_kill(cp->stream->stream);
169 cp->stream->user = NULL;
174 void pvr2_channel_done(struct pvr2_channel *cp)
176 struct pvr2_context *mp = cp->mc_head;
177 pvr2_channel_disclaim_stream(cp);
179 cp->mc_next->mc_prev = cp->mc_prev;
181 mp->mc_last = cp->mc_prev;
184 cp->mc_prev->mc_next = cp->mc_next;
186 mp->mc_first = cp->mc_next;
192 int pvr2_channel_claim_stream(struct pvr2_channel *cp,
193 struct pvr2_context_stream *sp)
196 pvr2_context_enter(cp->mc_head); do {
197 if (sp == cp->stream) break;
202 pvr2_channel_disclaim_stream(cp);
206 } while (0); pvr2_context_exit(cp->mc_head);
211 // This is the marker for the real beginning of a legitimate mpeg2 stream.
212 static char stream_sync_key[] = {
213 0x00, 0x00, 0x01, 0xba,
216 struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
217 struct pvr2_context_stream *sp)
219 struct pvr2_ioread *cp;
220 cp = pvr2_ioread_create();
221 if (!cp) return NULL;
222 pvr2_ioread_setup(cp,sp->stream);
223 pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
229 Stuff for Emacs to see, in order to encourage consistent editing style:
230 *** Local Variables: ***
232 *** fill-column: 75 ***
234 *** c-basic-offset: 8 ***