pvrusb2-video-v4l.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // ????? No idea yet what to do here
  60. default:
  61. return;
  62. }
  63. route.output = 0;
  64. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
  65. }
  66. static int check_input(struct pvr2_v4l_decoder *ctxt)
  67. {
  68. struct pvr2_hdw *hdw = ctxt->hdw;
  69. return hdw->input_dirty != 0;
  70. }
  71. static void set_audio(struct pvr2_v4l_decoder *ctxt)
  72. {
  73. u32 val;
  74. struct pvr2_hdw *hdw = ctxt->hdw;
  75. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
  76. hdw->srate_val);
  77. switch (hdw->srate_val) {
  78. default:
  79. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
  80. val = 48000;
  81. break;
  82. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
  83. val = 44100;
  84. break;
  85. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
  86. val = 32000;
  87. break;
  88. }
  89. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
  90. }
  91. static int check_audio(struct pvr2_v4l_decoder *ctxt)
  92. {
  93. struct pvr2_hdw *hdw = ctxt->hdw;
  94. return hdw->srate_dirty != 0;
  95. }
  96. struct pvr2_v4l_decoder_ops {
  97. void (*update)(struct pvr2_v4l_decoder *);
  98. int (*check)(struct pvr2_v4l_decoder *);
  99. };
  100. static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
  101. { .update = set_input, .check = check_input},
  102. { .update = set_audio, .check = check_audio},
  103. };
  104. static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
  105. {
  106. ctxt->client->handler = NULL;
  107. ctxt->hdw->decoder_ctrl = NULL;
  108. kfree(ctxt);
  109. }
  110. static int decoder_check(struct pvr2_v4l_decoder *ctxt)
  111. {
  112. unsigned long msk;
  113. unsigned int idx;
  114. for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
  115. idx++) {
  116. msk = 1 << idx;
  117. if (ctxt->stale_mask & msk) continue;
  118. if (decoder_ops[idx].check(ctxt)) {
  119. ctxt->stale_mask |= msk;
  120. }
  121. }
  122. return ctxt->stale_mask != 0;
  123. }
  124. static void decoder_update(struct pvr2_v4l_decoder *ctxt)
  125. {
  126. unsigned long msk;
  127. unsigned int idx;
  128. for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
  129. 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 int decoder_is_tuned(struct pvr2_v4l_decoder *ctxt)
  151. {
  152. struct v4l2_tuner vt;
  153. int ret;
  154. memset(&vt,0,sizeof(vt));
  155. ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
  156. if (ret < 0) return -EINVAL;
  157. return vt.signal ? 1 : 0;
  158. }
  159. static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
  160. {
  161. return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
  162. }
  163. const static struct pvr2_i2c_handler_functions hfuncs = {
  164. .detach = (void (*)(void *))decoder_detach,
  165. .check = (int (*)(void *))decoder_check,
  166. .update = (void (*)(void *))decoder_update,
  167. .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
  168. };
  169. int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
  170. struct pvr2_i2c_client *cp)
  171. {
  172. struct pvr2_v4l_decoder *ctxt;
  173. if (hdw->decoder_ctrl) return 0;
  174. if (cp->handler) return 0;
  175. if (!decoder_detect(cp)) return 0;
  176. ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
  177. if (!ctxt) return 0;
  178. memset(ctxt,0,sizeof(*ctxt));
  179. ctxt->handler.func_data = ctxt;
  180. ctxt->handler.func_table = &hfuncs;
  181. ctxt->ctrl.ctxt = ctxt;
  182. ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
  183. ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
  184. ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned;
  185. ctxt->client = cp;
  186. ctxt->hdw = hdw;
  187. ctxt->stale_mask = (1 << (sizeof(decoder_ops)/
  188. sizeof(decoder_ops[0]))) - 1;
  189. hdw->decoder_ctrl = &ctxt->ctrl;
  190. cp->handler = &ctxt->handler;
  191. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
  192. cp->client->addr);
  193. return !0;
  194. }
  195. /*
  196. Stuff for Emacs to see, in order to encourage consistent editing style:
  197. *** Local Variables: ***
  198. *** mode: c ***
  199. *** fill-column: 70 ***
  200. *** tab-width: 8 ***
  201. *** c-basic-offset: 8 ***
  202. *** End: ***
  203. */