]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/pvrusb2/pvrusb2-context.c
V4L/DVB (7320): pvrusb2: Eliminate timer race during tear-down
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / pvrusb2 / pvrusb2-context.c
CommitLineData
d855497e
MI
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>
d855497e
MI
29
30
31static void pvr2_context_destroy(struct pvr2_context *mp)
32{
d855497e 33 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
681c7399 34 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
d855497e
MI
35 kfree(mp);
36}
37
38
681c7399 39static void pvr2_context_state_check(struct pvr2_context *mp)
d855497e 40{
681c7399
MI
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 }
c4028958 50
d855497e 51 pvr2_context_enter(mp); do {
681c7399 52 mp->init_flag = !0;
d855497e
MI
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);
681c7399 58 }
d855497e
MI
59
60
61struct 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{
a0fd1cb1 66 struct pvr2_context *mp = NULL;
ca545f7c 67 mp = kzalloc(sizeof(*mp),GFP_KERNEL);
d855497e 68 if (!mp) goto done;
d855497e
MI
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);
a0fd1cb1 75 mp = NULL;
d855497e
MI
76 goto done;
77 }
c4a8828d
MI
78 pvr2_hdw_initialize(mp->hdw,
79 (void (*)(void *))pvr2_context_state_check,
80 mp);
d855497e
MI
81 done:
82 return mp;
83}
84
85
86void pvr2_context_enter(struct pvr2_context *mp)
87{
88 mutex_lock(&mp->mutex);
89 pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_enter(id=%p)",mp);
90}
91
92
93void pvr2_context_exit(struct pvr2_context *mp)
94{
95 int destroy_flag = 0;
96 if (!(mp->mc_first || !mp->disconnect_flag)) {
97 destroy_flag = !0;
98 }
99 pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_exit(id=%p) outside",mp);
100 mutex_unlock(&mp->mutex);
101 if (destroy_flag) pvr2_context_destroy(mp);
102}
103
104
105static void pvr2_context_run_checks(struct pvr2_context *mp)
106{
107 struct pvr2_channel *ch1,*ch2;
108 for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
109 ch2 = ch1->mc_next;
110 if (ch1->check_func) {
111 ch1->check_func(ch1);
112 }
113 }
114}
115
116
117void pvr2_context_disconnect(struct pvr2_context *mp)
118{
119 pvr2_context_enter(mp); do {
120 pvr2_hdw_disconnect(mp->hdw);
121 mp->disconnect_flag = !0;
122 pvr2_context_run_checks(mp);
123 } while (0); pvr2_context_exit(mp);
124}
125
126
127void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
128{
129 cp->hdw = mp->hdw;
130 cp->mc_head = mp;
a0fd1cb1 131 cp->mc_next = NULL;
d855497e
MI
132 cp->mc_prev = mp->mc_last;
133 if (mp->mc_last) {
134 mp->mc_last->mc_next = cp;
135 } else {
136 mp->mc_first = cp;
137 }
138 mp->mc_last = cp;
139}
140
141
142static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
143{
144 if (!cp->stream) return;
145 pvr2_stream_kill(cp->stream->stream);
a0fd1cb1
MI
146 cp->stream->user = NULL;
147 cp->stream = NULL;
d855497e
MI
148}
149
150
151void pvr2_channel_done(struct pvr2_channel *cp)
152{
153 struct pvr2_context *mp = cp->mc_head;
154 pvr2_channel_disclaim_stream(cp);
155 if (cp->mc_next) {
156 cp->mc_next->mc_prev = cp->mc_prev;
157 } else {
158 mp->mc_last = cp->mc_prev;
159 }
160 if (cp->mc_prev) {
161 cp->mc_prev->mc_next = cp->mc_next;
162 } else {
163 mp->mc_first = cp->mc_next;
164 }
a0fd1cb1 165 cp->hdw = NULL;
d855497e
MI
166}
167
168
169int pvr2_channel_claim_stream(struct pvr2_channel *cp,
170 struct pvr2_context_stream *sp)
171{
172 int code = 0;
173 pvr2_context_enter(cp->mc_head); do {
174 if (sp == cp->stream) break;
99a6acf9 175 if (sp && sp->user) {
d855497e
MI
176 code = -EBUSY;
177 break;
178 }
179 pvr2_channel_disclaim_stream(cp);
180 if (!sp) break;
181 sp->user = cp;
182 cp->stream = sp;
183 } while (0); pvr2_context_exit(cp->mc_head);
184 return code;
185}
186
187
188// This is the marker for the real beginning of a legitimate mpeg2 stream.
189static char stream_sync_key[] = {
190 0x00, 0x00, 0x01, 0xba,
191};
192
193struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
194 struct pvr2_context_stream *sp)
195{
196 struct pvr2_ioread *cp;
197 cp = pvr2_ioread_create();
a0fd1cb1 198 if (!cp) return NULL;
d855497e
MI
199 pvr2_ioread_setup(cp,sp->stream);
200 pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
201 return cp;
202}
203
204
205/*
206 Stuff for Emacs to see, in order to encourage consistent editing style:
207 *** Local Variables: ***
208 *** mode: c ***
209 *** fill-column: 75 ***
210 *** tab-width: 8 ***
211 *** c-basic-offset: 8 ***
212 *** End: ***
213 */