pvrusb2-video-v4l.c 6.2 KB

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