pvrusb2-context.c 4.9 KB

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