pvrusb2-video-v4l.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 PVR2_CVAL_SRATE_48:
  80. val = 48000;
  81. break;
  82. case PVR2_CVAL_SRATE_44_1:
  83. val = 44100;
  84. break;
  85. }
  86. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
  87. }
  88. static int check_audio(struct pvr2_v4l_decoder *ctxt)
  89. {
  90. struct pvr2_hdw *hdw = ctxt->hdw;
  91. return hdw->srate_dirty != 0;
  92. }
  93. struct pvr2_v4l_decoder_ops {
  94. void (*update)(struct pvr2_v4l_decoder *);
  95. int (*check)(struct pvr2_v4l_decoder *);
  96. };
  97. static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
  98. { .update = set_input, .check = check_input},
  99. { .update = set_audio, .check = check_audio},
  100. };
  101. static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
  102. {
  103. ctxt->client->handler = 0;
  104. ctxt->hdw->decoder_ctrl = 0;
  105. kfree(ctxt);
  106. }
  107. static int decoder_check(struct pvr2_v4l_decoder *ctxt)
  108. {
  109. unsigned long msk;
  110. unsigned int idx;
  111. for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
  112. idx++) {
  113. msk = 1 << idx;
  114. if (ctxt->stale_mask & msk) continue;
  115. if (decoder_ops[idx].check(ctxt)) {
  116. ctxt->stale_mask |= msk;
  117. }
  118. }
  119. return ctxt->stale_mask != 0;
  120. }
  121. static void decoder_update(struct pvr2_v4l_decoder *ctxt)
  122. {
  123. unsigned long msk;
  124. unsigned int idx;
  125. for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
  126. idx++) {
  127. msk = 1 << idx;
  128. if (!(ctxt->stale_mask & msk)) continue;
  129. ctxt->stale_mask &= ~msk;
  130. decoder_ops[idx].update(ctxt);
  131. }
  132. }
  133. static int decoder_detect(struct pvr2_i2c_client *cp)
  134. {
  135. /* Attempt to query the decoder - let's see if it will answer */
  136. struct v4l2_tuner vt;
  137. int ret;
  138. memset(&vt,0,sizeof(vt));
  139. ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
  140. return ret == 0; /* Return true if it answered */
  141. }
  142. static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
  143. {
  144. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
  145. pvr2_v4l2_cmd_stream(ctxt->client,fl);
  146. }
  147. static int decoder_is_tuned(struct pvr2_v4l_decoder *ctxt)
  148. {
  149. struct v4l2_tuner vt;
  150. int ret;
  151. memset(&vt,0,sizeof(vt));
  152. ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
  153. if (ret < 0) return -EINVAL;
  154. return vt.signal ? 1 : 0;
  155. }
  156. static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
  157. {
  158. return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
  159. }
  160. const static struct pvr2_i2c_handler_functions hfuncs = {
  161. .detach = (void (*)(void *))decoder_detach,
  162. .check = (int (*)(void *))decoder_check,
  163. .update = (void (*)(void *))decoder_update,
  164. .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
  165. };
  166. int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
  167. struct pvr2_i2c_client *cp)
  168. {
  169. struct pvr2_v4l_decoder *ctxt;
  170. if (hdw->decoder_ctrl) return 0;
  171. if (cp->handler) return 0;
  172. if (!decoder_detect(cp)) return 0;
  173. ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
  174. if (!ctxt) return 0;
  175. memset(ctxt,0,sizeof(*ctxt));
  176. ctxt->handler.func_data = ctxt;
  177. ctxt->handler.func_table = &hfuncs;
  178. ctxt->ctrl.ctxt = ctxt;
  179. ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
  180. ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
  181. ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned;
  182. ctxt->client = cp;
  183. ctxt->hdw = hdw;
  184. ctxt->stale_mask = (1 << (sizeof(decoder_ops)/
  185. sizeof(decoder_ops[0]))) - 1;
  186. hdw->decoder_ctrl = &ctxt->ctrl;
  187. cp->handler = &ctxt->handler;
  188. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
  189. cp->client->addr);
  190. return !0;
  191. }
  192. /*
  193. Stuff for Emacs to see, in order to encourage consistent editing style:
  194. *** Local Variables: ***
  195. *** mode: c ***
  196. *** fill-column: 70 ***
  197. *** tab-width: 8 ***
  198. *** c-basic-offset: 8 ***
  199. *** End: ***
  200. */