pvrusb2-context.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include "pvrusb2-context.h"
  21. #include "pvrusb2-io.h"
  22. #include "pvrusb2-ioread.h"
  23. #include "pvrusb2-hdw.h"
  24. #include "pvrusb2-debug.h"
  25. #include <linux/kthread.h>
  26. #include <linux/errno.h>
  27. #include <linux/string.h>
  28. #include <linux/slab.h>
  29. static void pvr2_context_destroy(struct pvr2_context *mp)
  30. {
  31. pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (destroy)",mp);
  32. if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
  33. kfree(mp);
  34. }
  35. static void pvr2_context_notify(struct pvr2_context *mp)
  36. {
  37. mp->notify_flag = !0;
  38. wake_up(&mp->wait_data);
  39. }
  40. static int pvr2_context_thread(void *_mp)
  41. {
  42. struct pvr2_channel *ch1,*ch2;
  43. struct pvr2_context *mp = _mp;
  44. pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread start)",mp);
  45. /* Finish hardware initialization */
  46. if (pvr2_hdw_initialize(mp->hdw,
  47. (void (*)(void *))pvr2_context_notify,mp)) {
  48. mp->video_stream.stream =
  49. pvr2_hdw_get_video_stream(mp->hdw);
  50. /* Trigger interface initialization. By doing this here
  51. initialization runs in our own safe and cozy thread
  52. context. */
  53. if (mp->setup_func) mp->setup_func(mp);
  54. } else {
  55. pvr2_trace(PVR2_TRACE_CTXT,
  56. "pvr2_context %p (thread skipping setup)",mp);
  57. /* Even though initialization did not succeed, we're still
  58. going to enter the wait loop anyway. We need to do this
  59. in order to await the expected disconnect (which we will
  60. detect in the normal course of operation). */
  61. }
  62. /* Now just issue callbacks whenever hardware state changes or if
  63. there is a disconnect. If there is a disconnect and there are
  64. no channels left, then there's no reason to stick around anymore
  65. so we'll self-destruct - tearing down the rest of this driver
  66. instance along the way. */
  67. pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread enter loop)",mp);
  68. while (!mp->disconnect_flag || mp->mc_first) {
  69. if (mp->notify_flag) {
  70. mp->notify_flag = 0;
  71. pvr2_trace(PVR2_TRACE_CTXT,
  72. "pvr2_context %p (thread notify)",mp);
  73. for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
  74. ch2 = ch1->mc_next;
  75. if (ch1->check_func) ch1->check_func(ch1);
  76. }
  77. }
  78. wait_event_interruptible(mp->wait_data, mp->notify_flag);
  79. }
  80. pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread end)",mp);
  81. pvr2_context_destroy(mp);
  82. return 0;
  83. }
  84. struct pvr2_context *pvr2_context_create(
  85. struct usb_interface *intf,
  86. const struct usb_device_id *devid,
  87. void (*setup_func)(struct pvr2_context *))
  88. {
  89. struct task_struct *thread;
  90. struct pvr2_context *mp = NULL;
  91. mp = kzalloc(sizeof(*mp),GFP_KERNEL);
  92. if (!mp) goto done;
  93. pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (create)",mp);
  94. init_waitqueue_head(&mp->wait_data);
  95. mp->setup_func = setup_func;
  96. mutex_init(&mp->mutex);
  97. mp->hdw = pvr2_hdw_create(intf,devid);
  98. if (!mp->hdw) {
  99. pvr2_context_destroy(mp);
  100. mp = NULL;
  101. goto done;
  102. }
  103. thread = kthread_run(pvr2_context_thread, mp, "pvrusb2-context");
  104. if (!thread) {
  105. pvr2_context_destroy(mp);
  106. mp = NULL;
  107. goto done;
  108. }
  109. done:
  110. return mp;
  111. }
  112. static void pvr2_context_enter(struct pvr2_context *mp)
  113. {
  114. mutex_lock(&mp->mutex);
  115. }
  116. static void pvr2_context_exit(struct pvr2_context *mp)
  117. {
  118. int destroy_flag = 0;
  119. if (!(mp->mc_first || !mp->disconnect_flag)) {
  120. destroy_flag = !0;
  121. }
  122. mutex_unlock(&mp->mutex);
  123. if (destroy_flag) pvr2_context_notify(mp);
  124. }
  125. void pvr2_context_disconnect(struct pvr2_context *mp)
  126. {
  127. pvr2_hdw_disconnect(mp->hdw);
  128. mp->disconnect_flag = !0;
  129. pvr2_context_notify(mp);
  130. }
  131. void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
  132. {
  133. pvr2_context_enter(mp);
  134. cp->hdw = mp->hdw;
  135. cp->mc_head = mp;
  136. cp->mc_next = NULL;
  137. cp->mc_prev = mp->mc_last;
  138. if (mp->mc_last) {
  139. mp->mc_last->mc_next = cp;
  140. } else {
  141. mp->mc_first = cp;
  142. }
  143. mp->mc_last = cp;
  144. pvr2_context_exit(mp);
  145. }
  146. static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
  147. {
  148. if (!cp->stream) return;
  149. pvr2_stream_kill(cp->stream->stream);
  150. cp->stream->user = NULL;
  151. cp->stream = NULL;
  152. }
  153. void pvr2_channel_done(struct pvr2_channel *cp)
  154. {
  155. struct pvr2_context *mp = cp->mc_head;
  156. pvr2_context_enter(mp);
  157. pvr2_channel_disclaim_stream(cp);
  158. if (cp->mc_next) {
  159. cp->mc_next->mc_prev = cp->mc_prev;
  160. } else {
  161. mp->mc_last = cp->mc_prev;
  162. }
  163. if (cp->mc_prev) {
  164. cp->mc_prev->mc_next = cp->mc_next;
  165. } else {
  166. mp->mc_first = cp->mc_next;
  167. }
  168. cp->hdw = NULL;
  169. pvr2_context_exit(mp);
  170. }
  171. int pvr2_channel_claim_stream(struct pvr2_channel *cp,
  172. struct pvr2_context_stream *sp)
  173. {
  174. int code = 0;
  175. pvr2_context_enter(cp->mc_head); do {
  176. if (sp == cp->stream) break;
  177. if (sp && sp->user) {
  178. code = -EBUSY;
  179. break;
  180. }
  181. pvr2_channel_disclaim_stream(cp);
  182. if (!sp) break;
  183. sp->user = cp;
  184. cp->stream = sp;
  185. } while (0); pvr2_context_exit(cp->mc_head);
  186. return code;
  187. }
  188. // This is the marker for the real beginning of a legitimate mpeg2 stream.
  189. static char stream_sync_key[] = {
  190. 0x00, 0x00, 0x01, 0xba,
  191. };
  192. struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
  193. struct pvr2_context_stream *sp)
  194. {
  195. struct pvr2_ioread *cp;
  196. cp = pvr2_ioread_create();
  197. if (!cp) return NULL;
  198. pvr2_ioread_setup(cp,sp->stream);
  199. pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
  200. return cp;
  201. }
  202. /*
  203. Stuff for Emacs to see, in order to encourage consistent editing style:
  204. *** Local Variables: ***
  205. *** mode: c ***
  206. *** fill-column: 75 ***
  207. *** tab-width: 8 ***
  208. *** c-basic-offset: 8 ***
  209. *** End: ***
  210. */