pvrusb2-context.c 10 KB

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