pvrusb2-context.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/slab.h>
  28. static void pvr2_context_destroy(struct pvr2_context *mp)
  29. {
  30. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr_main id=%p",mp);
  31. if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
  32. kfree(mp);
  33. }
  34. static void pvr2_context_state_check(struct pvr2_context *mp)
  35. {
  36. if (mp->init_flag) return;
  37. switch (pvr2_hdw_get_state(mp->hdw)) {
  38. case PVR2_STATE_WARM: break;
  39. case PVR2_STATE_ERROR: break;
  40. case PVR2_STATE_READY: break;
  41. case PVR2_STATE_RUN: break;
  42. default: return;
  43. }
  44. pvr2_context_enter(mp); do {
  45. mp->init_flag = !0;
  46. mp->video_stream.stream = pvr2_hdw_get_video_stream(mp->hdw);
  47. if (mp->setup_func) {
  48. mp->setup_func(mp);
  49. }
  50. } while (0); pvr2_context_exit(mp);
  51. }
  52. struct pvr2_context *pvr2_context_create(
  53. struct usb_interface *intf,
  54. const struct usb_device_id *devid,
  55. void (*setup_func)(struct pvr2_context *))
  56. {
  57. struct pvr2_context *mp = NULL;
  58. mp = kzalloc(sizeof(*mp),GFP_KERNEL);
  59. if (!mp) goto done;
  60. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_main id=%p",mp);
  61. mp->setup_func = setup_func;
  62. mutex_init(&mp->mutex);
  63. mp->hdw = pvr2_hdw_create(intf,devid);
  64. if (!mp->hdw) {
  65. pvr2_context_destroy(mp);
  66. mp = NULL;
  67. goto done;
  68. }
  69. pvr2_hdw_set_state_callback(mp->hdw,
  70. (void (*)(void *))pvr2_context_state_check,
  71. mp);
  72. pvr2_context_state_check(mp);
  73. done:
  74. return mp;
  75. }
  76. void pvr2_context_enter(struct pvr2_context *mp)
  77. {
  78. mutex_lock(&mp->mutex);
  79. pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_enter(id=%p)",mp);
  80. }
  81. void pvr2_context_exit(struct pvr2_context *mp)
  82. {
  83. int destroy_flag = 0;
  84. if (!(mp->mc_first || !mp->disconnect_flag)) {
  85. destroy_flag = !0;
  86. }
  87. pvr2_trace(PVR2_TRACE_CREG,"pvr2_context_exit(id=%p) outside",mp);
  88. mutex_unlock(&mp->mutex);
  89. if (destroy_flag) pvr2_context_destroy(mp);
  90. }
  91. static void pvr2_context_run_checks(struct pvr2_context *mp)
  92. {
  93. struct pvr2_channel *ch1,*ch2;
  94. for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
  95. ch2 = ch1->mc_next;
  96. if (ch1->check_func) {
  97. ch1->check_func(ch1);
  98. }
  99. }
  100. }
  101. void pvr2_context_disconnect(struct pvr2_context *mp)
  102. {
  103. pvr2_context_enter(mp); do {
  104. pvr2_hdw_disconnect(mp->hdw);
  105. mp->disconnect_flag = !0;
  106. pvr2_context_run_checks(mp);
  107. } while (0); pvr2_context_exit(mp);
  108. }
  109. void pvr2_channel_init(struct pvr2_channel *cp,struct pvr2_context *mp)
  110. {
  111. cp->hdw = mp->hdw;
  112. cp->mc_head = mp;
  113. cp->mc_next = NULL;
  114. cp->mc_prev = mp->mc_last;
  115. if (mp->mc_last) {
  116. mp->mc_last->mc_next = cp;
  117. } else {
  118. mp->mc_first = cp;
  119. }
  120. mp->mc_last = cp;
  121. }
  122. static void pvr2_channel_disclaim_stream(struct pvr2_channel *cp)
  123. {
  124. if (!cp->stream) return;
  125. pvr2_stream_kill(cp->stream->stream);
  126. cp->stream->user = NULL;
  127. cp->stream = NULL;
  128. }
  129. void pvr2_channel_done(struct pvr2_channel *cp)
  130. {
  131. struct pvr2_context *mp = cp->mc_head;
  132. pvr2_channel_disclaim_stream(cp);
  133. if (cp->mc_next) {
  134. cp->mc_next->mc_prev = cp->mc_prev;
  135. } else {
  136. mp->mc_last = cp->mc_prev;
  137. }
  138. if (cp->mc_prev) {
  139. cp->mc_prev->mc_next = cp->mc_next;
  140. } else {
  141. mp->mc_first = cp->mc_next;
  142. }
  143. cp->hdw = NULL;
  144. }
  145. int pvr2_channel_claim_stream(struct pvr2_channel *cp,
  146. struct pvr2_context_stream *sp)
  147. {
  148. int code = 0;
  149. pvr2_context_enter(cp->mc_head); do {
  150. if (sp == cp->stream) break;
  151. if (sp && sp->user) {
  152. code = -EBUSY;
  153. break;
  154. }
  155. pvr2_channel_disclaim_stream(cp);
  156. if (!sp) break;
  157. sp->user = cp;
  158. cp->stream = sp;
  159. } while (0); pvr2_context_exit(cp->mc_head);
  160. return code;
  161. }
  162. // This is the marker for the real beginning of a legitimate mpeg2 stream.
  163. static char stream_sync_key[] = {
  164. 0x00, 0x00, 0x01, 0xba,
  165. };
  166. struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
  167. struct pvr2_context_stream *sp)
  168. {
  169. struct pvr2_ioread *cp;
  170. cp = pvr2_ioread_create();
  171. if (!cp) return NULL;
  172. pvr2_ioread_setup(cp,sp->stream);
  173. pvr2_ioread_set_sync_key(cp,stream_sync_key,sizeof(stream_sync_key));
  174. return cp;
  175. }
  176. /*
  177. Stuff for Emacs to see, in order to encourage consistent editing style:
  178. *** Local Variables: ***
  179. *** mode: c ***
  180. *** fill-column: 75 ***
  181. *** tab-width: 8 ***
  182. *** c-basic-offset: 8 ***
  183. *** End: ***
  184. */