pvrusb2-video-v4l.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /*
  23. This source file is specifically designed to interface with the
  24. saa711x support that is available in the v4l available starting
  25. with linux 2.6.15.
  26. */
  27. #include "pvrusb2-video-v4l.h"
  28. #include "pvrusb2-i2c-cmd-v4l2.h"
  29. #include "pvrusb2-hdw-internal.h"
  30. #include "pvrusb2-debug.h"
  31. #include <linux/videodev2.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/saa7115.h>
  34. #include <linux/errno.h>
  35. #include <linux/slab.h>
  36. struct pvr2_v4l_decoder {
  37. struct pvr2_i2c_handler handler;
  38. struct pvr2_decoder_ctrl ctrl;
  39. struct pvr2_i2c_client *client;
  40. struct pvr2_hdw *hdw;
  41. unsigned long stale_mask;
  42. };
  43. static void set_input(struct pvr2_v4l_decoder *ctxt)
  44. {
  45. struct pvr2_hdw *hdw = ctxt->hdw;
  46. struct v4l2_routing route;
  47. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
  48. switch(hdw->input_val) {
  49. case PVR2_CVAL_INPUT_TV:
  50. route.input = SAA7115_COMPOSITE4;
  51. break;
  52. case PVR2_CVAL_INPUT_COMPOSITE:
  53. route.input = SAA7115_COMPOSITE5;
  54. break;
  55. case PVR2_CVAL_INPUT_SVIDEO:
  56. route.input = SAA7115_SVIDEO2;
  57. break;
  58. case PVR2_CVAL_INPUT_RADIO:
  59. // In radio mode, we mute the video, but point at one
  60. // spot just to stay consistent
  61. route.input = SAA7115_COMPOSITE5;
  62. default:
  63. return;
  64. }
  65. route.output = 0;
  66. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
  67. }
  68. static int check_input(struct pvr2_v4l_decoder *ctxt)
  69. {
  70. struct pvr2_hdw *hdw = ctxt->hdw;
  71. return hdw->input_dirty != 0;
  72. }
  73. static void set_audio(struct pvr2_v4l_decoder *ctxt)
  74. {
  75. u32 val;
  76. struct pvr2_hdw *hdw = ctxt->hdw;
  77. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
  78. hdw->srate_val);
  79. switch (hdw->srate_val) {
  80. default:
  81. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
  82. val = 48000;
  83. break;
  84. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
  85. val = 44100;
  86. break;
  87. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
  88. val = 32000;
  89. break;
  90. }
  91. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
  92. }
  93. static int check_audio(struct pvr2_v4l_decoder *ctxt)
  94. {
  95. struct pvr2_hdw *hdw = ctxt->hdw;
  96. return hdw->srate_dirty != 0;
  97. }
  98. struct pvr2_v4l_decoder_ops {
  99. void (*update)(struct pvr2_v4l_decoder *);
  100. int (*check)(struct pvr2_v4l_decoder *);
  101. };
  102. static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
  103. { .update = set_input, .check = check_input},
  104. { .update = set_audio, .check = check_audio},
  105. };
  106. static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
  107. {
  108. ctxt->client->handler = NULL;
  109. ctxt->hdw->decoder_ctrl = NULL;
  110. kfree(ctxt);
  111. }
  112. static int decoder_check(struct pvr2_v4l_decoder *ctxt)
  113. {
  114. unsigned long msk;
  115. unsigned int idx;
  116. for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
  117. msk = 1 << idx;
  118. if (ctxt->stale_mask & msk) continue;
  119. if (decoder_ops[idx].check(ctxt)) {
  120. ctxt->stale_mask |= msk;
  121. }
  122. }
  123. return ctxt->stale_mask != 0;
  124. }
  125. static void decoder_update(struct pvr2_v4l_decoder *ctxt)
  126. {
  127. unsigned long msk;
  128. unsigned int idx;
  129. for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
  130. msk = 1 << idx;
  131. if (!(ctxt->stale_mask & msk)) continue;
  132. ctxt->stale_mask &= ~msk;
  133. decoder_ops[idx].update(ctxt);
  134. }
  135. }
  136. static int decoder_detect(struct pvr2_i2c_client *cp)
  137. {
  138. /* Attempt to query the decoder - let's see if it will answer */
  139. struct v4l2_tuner vt;
  140. int ret;
  141. memset(&vt,0,sizeof(vt));
  142. ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
  143. return ret == 0; /* Return true if it answered */
  144. }
  145. static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
  146. {
  147. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
  148. pvr2_v4l2_cmd_stream(ctxt->client,fl);
  149. }
  150. static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
  151. {
  152. return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
  153. }
  154. static const struct pvr2_i2c_handler_functions hfuncs = {
  155. .detach = (void (*)(void *))decoder_detach,
  156. .check = (int (*)(void *))decoder_check,
  157. .update = (void (*)(void *))decoder_update,
  158. .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
  159. };
  160. int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
  161. struct pvr2_i2c_client *cp)
  162. {
  163. struct pvr2_v4l_decoder *ctxt;
  164. if (hdw->decoder_ctrl) return 0;
  165. if (cp->handler) return 0;
  166. if (!decoder_detect(cp)) return 0;
  167. ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
  168. if (!ctxt) return 0;
  169. ctxt->handler.func_data = ctxt;
  170. ctxt->handler.func_table = &hfuncs;
  171. ctxt->ctrl.ctxt = ctxt;
  172. ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
  173. ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
  174. ctxt->client = cp;
  175. ctxt->hdw = hdw;
  176. ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
  177. hdw->decoder_ctrl = &ctxt->ctrl;
  178. cp->handler = &ctxt->handler;
  179. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
  180. cp->client->addr);
  181. return !0;
  182. }
  183. /*
  184. Stuff for Emacs to see, in order to encourage consistent editing style:
  185. *** Local Variables: ***
  186. *** mode: c ***
  187. *** fill-column: 70 ***
  188. *** tab-width: 8 ***
  189. *** c-basic-offset: 8 ***
  190. *** End: ***
  191. */