pvrusb2-video-v4l.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. struct routing_scheme {
  44. const int *def;
  45. unsigned int cnt;
  46. };
  47. static const int routing_scheme0[] = {
  48. [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
  49. /* In radio mode, we mute the video, but point at one
  50. spot just to stay consistent */
  51. [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
  52. [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
  53. [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
  54. };
  55. static const struct routing_scheme routing_schemes[] = {
  56. [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
  57. .def = routing_scheme0,
  58. .cnt = ARRAY_SIZE(routing_scheme0),
  59. },
  60. };
  61. static void set_input(struct pvr2_v4l_decoder *ctxt)
  62. {
  63. struct pvr2_hdw *hdw = ctxt->hdw;
  64. struct v4l2_routing route;
  65. const struct routing_scheme *sp;
  66. unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
  67. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
  68. if ((sid < ARRAY_SIZE(routing_schemes)) &&
  69. ((sp = routing_schemes + sid) != 0) &&
  70. (hdw->input_val >= 0) &&
  71. (hdw->input_val < sp->cnt)) {
  72. route.input = sp->def[hdw->input_val];
  73. } else {
  74. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  75. "*** WARNING *** i2c v4l2 set_input:"
  76. " Invalid routing scheme (%u) and/or input (%d)",
  77. sid,hdw->input_val);
  78. return;
  79. }
  80. route.output = 0;
  81. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
  82. }
  83. static int check_input(struct pvr2_v4l_decoder *ctxt)
  84. {
  85. struct pvr2_hdw *hdw = ctxt->hdw;
  86. return hdw->input_dirty != 0;
  87. }
  88. static void set_audio(struct pvr2_v4l_decoder *ctxt)
  89. {
  90. u32 val;
  91. struct pvr2_hdw *hdw = ctxt->hdw;
  92. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
  93. hdw->srate_val);
  94. switch (hdw->srate_val) {
  95. default:
  96. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
  97. val = 48000;
  98. break;
  99. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
  100. val = 44100;
  101. break;
  102. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
  103. val = 32000;
  104. break;
  105. }
  106. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
  107. }
  108. static int check_audio(struct pvr2_v4l_decoder *ctxt)
  109. {
  110. struct pvr2_hdw *hdw = ctxt->hdw;
  111. return hdw->srate_dirty != 0;
  112. }
  113. struct pvr2_v4l_decoder_ops {
  114. void (*update)(struct pvr2_v4l_decoder *);
  115. int (*check)(struct pvr2_v4l_decoder *);
  116. };
  117. static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
  118. { .update = set_input, .check = check_input},
  119. { .update = set_audio, .check = check_audio},
  120. };
  121. static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
  122. {
  123. ctxt->client->handler = NULL;
  124. pvr2_hdw_set_decoder(ctxt->hdw,NULL);
  125. kfree(ctxt);
  126. }
  127. static int decoder_check(struct pvr2_v4l_decoder *ctxt)
  128. {
  129. unsigned long msk;
  130. unsigned int idx;
  131. for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
  132. msk = 1 << idx;
  133. if (ctxt->stale_mask & msk) continue;
  134. if (decoder_ops[idx].check(ctxt)) {
  135. ctxt->stale_mask |= msk;
  136. }
  137. }
  138. return ctxt->stale_mask != 0;
  139. }
  140. static void decoder_update(struct pvr2_v4l_decoder *ctxt)
  141. {
  142. unsigned long msk;
  143. unsigned int idx;
  144. for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
  145. msk = 1 << idx;
  146. if (!(ctxt->stale_mask & msk)) continue;
  147. ctxt->stale_mask &= ~msk;
  148. decoder_ops[idx].update(ctxt);
  149. }
  150. }
  151. static int decoder_detect(struct pvr2_i2c_client *cp)
  152. {
  153. /* Attempt to query the decoder - let's see if it will answer */
  154. struct v4l2_tuner vt;
  155. int ret;
  156. memset(&vt,0,sizeof(vt));
  157. ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
  158. return ret == 0; /* Return true if it answered */
  159. }
  160. static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
  161. {
  162. pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
  163. pvr2_v4l2_cmd_stream(ctxt->client,fl);
  164. }
  165. static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
  166. {
  167. return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
  168. }
  169. static const struct pvr2_i2c_handler_functions hfuncs = {
  170. .detach = (void (*)(void *))decoder_detach,
  171. .check = (int (*)(void *))decoder_check,
  172. .update = (void (*)(void *))decoder_update,
  173. .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
  174. };
  175. int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
  176. struct pvr2_i2c_client *cp)
  177. {
  178. struct pvr2_v4l_decoder *ctxt;
  179. if (hdw->decoder_ctrl) return 0;
  180. if (cp->handler) return 0;
  181. if (!decoder_detect(cp)) return 0;
  182. ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
  183. if (!ctxt) return 0;
  184. ctxt->handler.func_data = ctxt;
  185. ctxt->handler.func_table = &hfuncs;
  186. ctxt->ctrl.ctxt = ctxt;
  187. ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
  188. ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
  189. ctxt->client = cp;
  190. ctxt->hdw = hdw;
  191. ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
  192. pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
  193. cp->handler = &ctxt->handler;
  194. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
  195. cp->client->addr);
  196. return !0;
  197. }
  198. /*
  199. Stuff for Emacs to see, in order to encourage consistent editing style:
  200. *** Local Variables: ***
  201. *** mode: c ***
  202. *** fill-column: 70 ***
  203. *** tab-width: 8 ***
  204. *** c-basic-offset: 8 ***
  205. *** End: ***
  206. */