Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "pvrusb2-context.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "pvrusb2-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "pvrusb2-ioread.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "pvrusb2-hdw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "pvrusb2-debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct pvr2_context *pvr2_context_exist_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static struct pvr2_context *pvr2_context_exist_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static struct pvr2_context *pvr2_context_notify_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static struct pvr2_context *pvr2_context_notify_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static DEFINE_MUTEX(pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_sync_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_cleanup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int pvr2_context_cleanup_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int pvr2_context_cleaned_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static struct task_struct *pvr2_context_thread_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static void pvr2_context_set_notify(struct pvr2_context *mp, int fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int signal_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	mutex_lock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	if (fl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		if (!mp->notify_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			signal_flag = (pvr2_context_notify_first == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			mp->notify_prev = pvr2_context_notify_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			mp->notify_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 			pvr2_context_notify_last = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			if (mp->notify_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 				mp->notify_prev->notify_next = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				pvr2_context_notify_first = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			mp->notify_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (mp->notify_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			mp->notify_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			if (mp->notify_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				mp->notify_next->notify_prev = mp->notify_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 				pvr2_context_notify_last = mp->notify_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			if (mp->notify_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 				mp->notify_prev->notify_next = mp->notify_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 				pvr2_context_notify_first = mp->notify_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	mutex_unlock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (signal_flag) wake_up(&pvr2_context_sync_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void pvr2_context_destroy(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (destroy)",mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	pvr2_hdw_destroy(mp->hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	pvr2_context_set_notify(mp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	mutex_lock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (mp->exist_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		mp->exist_next->exist_prev = mp->exist_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		pvr2_context_exist_last = mp->exist_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (mp->exist_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		mp->exist_prev->exist_next = mp->exist_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		pvr2_context_exist_first = mp->exist_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!pvr2_context_exist_first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		/* Trigger wakeup on control thread in case it is waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		   for an exit condition. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		wake_up(&pvr2_context_sync_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	mutex_unlock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	kfree(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void pvr2_context_notify(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	pvr2_context_set_notify(mp,!0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static void pvr2_context_check(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct pvr2_channel *ch1, *ch2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	pvr2_trace(PVR2_TRACE_CTXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		   "pvr2_context %p (notify)", mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (!mp->initialized_flag && !mp->disconnect_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		mp->initialized_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		pvr2_trace(PVR2_TRACE_CTXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			   "pvr2_context %p (initialize)", mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		/* Finish hardware initialization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (pvr2_hdw_initialize(mp->hdw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 					(void (*)(void *))pvr2_context_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 					mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			mp->video_stream.stream =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				pvr2_hdw_get_video_stream(mp->hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			/* Trigger interface initialization.  By doing this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			   here initialization runs in our own safe and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			   cozy thread context. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			if (mp->setup_func) mp->setup_func(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			pvr2_trace(PVR2_TRACE_CTXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				   "pvr2_context %p (thread skipping setup)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				   mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			/* Even though initialization did not succeed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			   we're still going to continue anyway.  We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			   to do this in order to await the expected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			   disconnect (which we will detect in the normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			   course of operation). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		ch2 = ch1->mc_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (ch1->check_func) ch1->check_func(ch1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (mp->disconnect_flag && !mp->mc_first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		/* Go away... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		pvr2_context_destroy(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int pvr2_context_shutok(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return pvr2_context_cleanup_flag && (pvr2_context_exist_first == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int pvr2_context_thread_func(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct pvr2_context *mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		while ((mp = pvr2_context_notify_first) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			pvr2_context_set_notify(mp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			pvr2_context_check(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			pvr2_context_sync_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			((pvr2_context_notify_first != NULL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			 pvr2_context_shutok()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	} while (!pvr2_context_shutok());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	pvr2_context_cleaned_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	wake_up(&pvr2_context_cleanup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread cleaned up");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		pvr2_context_sync_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		kthread_should_stop());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread end");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int pvr2_context_global_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	pvr2_context_thread_ptr = kthread_run(pvr2_context_thread_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 					      NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 					      "pvrusb2-context");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return IS_ERR(pvr2_context_thread_ptr) ? -ENOMEM : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void pvr2_context_global_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	pvr2_context_cleanup_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	wake_up(&pvr2_context_sync_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		pvr2_context_cleanup_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		pvr2_context_cleaned_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	kthread_stop(pvr2_context_thread_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct pvr2_context *pvr2_context_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct usb_interface *intf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	const struct usb_device_id *devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	void (*setup_func)(struct pvr2_context *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct pvr2_context *mp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	mp = kzalloc(sizeof(*mp),GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (!mp) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (create)",mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	mp->setup_func = setup_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	mutex_init(&mp->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_lock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	mp->exist_prev = pvr2_context_exist_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	mp->exist_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	pvr2_context_exist_last = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (mp->exist_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		mp->exist_prev->exist_next = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		pvr2_context_exist_first = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	mutex_unlock(&pvr2_context_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	mp->hdw = pvr2_hdw_create(intf,devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (!mp->hdw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		pvr2_context_destroy(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		mp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	pvr2_context_set_notify(mp, !0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static void pvr2_context_reset_input_limits(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	unsigned int tmsk,mmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct pvr2_channel *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct pvr2_hdw *hdw = mp->hdw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	mmsk = pvr2_hdw_get_input_available(hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	tmsk = mmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	for (cp = mp->mc_first; cp; cp = cp->mc_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (!cp->input_mask) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		tmsk &= cp->input_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	pvr2_hdw_set_input_allowed(hdw,mmsk,tmsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	pvr2_hdw_commit_ctl(hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static void pvr2_context_enter(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	mutex_lock(&mp->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void pvr2_context_exit(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	int destroy_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!(mp->mc_first || !mp->disconnect_flag)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		destroy_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	mutex_unlock(&mp->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (destroy_flag) pvr2_context_notify(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void pvr2_context_disconnect(struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	pvr2_hdw_disconnect(mp->hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	mp->disconnect_flag = !0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	pvr2_context_notify(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	pvr2_context_enter(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	cp->hdw = mp->hdw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	cp->mc_head = mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	cp->mc_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	cp->mc_prev = mp->mc_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (mp->mc_last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		mp->mc_last->mc_next = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		mp->mc_first = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	mp->mc_last = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	pvr2_context_exit(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (!cp->stream) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	pvr2_stream_kill(cp->stream->stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	cp->stream->user = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	cp->stream = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) void pvr2_channel_done(struct pvr2_channel *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct pvr2_context *mp = cp->mc_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	pvr2_context_enter(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	cp->input_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	pvr2_channel_disclaim_stream(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	pvr2_context_reset_input_limits(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (cp->mc_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		cp->mc_next->mc_prev = cp->mc_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		mp->mc_last = cp->mc_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (cp->mc_prev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		cp->mc_prev->mc_next = cp->mc_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		mp->mc_first = cp->mc_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	cp->hdw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	pvr2_context_exit(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int pvr2_channel_limit_inputs(struct pvr2_channel *cp,unsigned int cmsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	unsigned int tmsk,mmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct pvr2_channel *p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct pvr2_hdw *hdw = cp->hdw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	mmsk = pvr2_hdw_get_input_available(hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	cmsk &= mmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (cmsk == cp->input_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		/* No change; nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	pvr2_context_enter(cp->mc_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (!cmsk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			cp->input_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			pvr2_context_reset_input_limits(cp->mc_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		tmsk = mmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		for (p2 = cp->mc_head->mc_first; p2; p2 = p2->mc_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			if (p2 == cp) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			if (!p2->input_mask) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			tmsk &= p2->input_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (!(tmsk & cmsk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		tmsk &= cmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		if ((ret = pvr2_hdw_set_input_allowed(hdw,mmsk,tmsk)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			/* Internal failure changing allowed list; probably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			   should not happen, but react if it does. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		cp->input_mask = cmsk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		pvr2_hdw_commit_ctl(hdw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	} while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	pvr2_context_exit(cp->mc_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) unsigned int pvr2_channel_get_limited_inputs(struct pvr2_channel *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return cp->input_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int pvr2_channel_claim_stream(struct pvr2_channel *cp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			      struct pvr2_context_stream *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	int code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	pvr2_context_enter(cp->mc_head); do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		if (sp == cp->stream) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		if (sp && sp->user) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			code = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		pvr2_channel_disclaim_stream(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if (!sp) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		sp->user = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		cp->stream = sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	} while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	pvr2_context_exit(cp->mc_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) // This is the marker for the real beginning of a legitimate mpeg2 stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static char stream_sync_key[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	0x00, 0x00, 0x01, 0xba,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	struct pvr2_context_stream *sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	struct pvr2_ioread *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	cp = pvr2_ioread_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!cp) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	pvr2_ioread_setup(cp,sp->stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }