pvrusb2-cx2584x-v4l.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. cx2584x, in kernels 2.6.16 or newer.
  25. */
  26. #include "pvrusb2-cx2584x-v4l.h"
  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 <media/cx25840.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-common.h>
  34. #include <linux/errno.h>
  35. #include <linux/slab.h>
  36. struct pvr2_v4l_cx2584x {
  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_cx2584x *ctxt)
  44. {
  45. struct pvr2_hdw *hdw = ctxt->hdw;
  46. struct v4l2_routing route;
  47. enum cx25840_video_input vid_input;
  48. enum cx25840_audio_input aud_input;
  49. memset(&route,0,sizeof(route));
  50. switch(hdw->input_val) {
  51. case PVR2_CVAL_INPUT_TV:
  52. vid_input = CX25840_COMPOSITE7;
  53. aud_input = CX25840_AUDIO8;
  54. break;
  55. case PVR2_CVAL_INPUT_COMPOSITE:
  56. vid_input = CX25840_COMPOSITE3;
  57. aud_input = CX25840_AUDIO_SERIAL;
  58. break;
  59. case PVR2_CVAL_INPUT_SVIDEO:
  60. vid_input = CX25840_SVIDEO1;
  61. aud_input = CX25840_AUDIO_SERIAL;
  62. break;
  63. case PVR2_CVAL_INPUT_RADIO:
  64. default:
  65. // Just set it to be composite input for now...
  66. vid_input = CX25840_COMPOSITE3;
  67. aud_input = CX25840_AUDIO_SERIAL;
  68. break;
  69. }
  70. pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x",
  71. vid_input,aud_input);
  72. route.input = (u32)vid_input;
  73. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
  74. route.input = (u32)aud_input;
  75. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
  76. }
  77. static int check_input(struct pvr2_v4l_cx2584x *ctxt)
  78. {
  79. struct pvr2_hdw *hdw = ctxt->hdw;
  80. return hdw->input_dirty != 0;
  81. }
  82. static void set_audio(struct pvr2_v4l_cx2584x *ctxt)
  83. {
  84. u32 val;
  85. struct pvr2_hdw *hdw = ctxt->hdw;
  86. pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_audio %d",
  87. hdw->srate_val);
  88. switch (hdw->srate_val) {
  89. default:
  90. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
  91. val = 48000;
  92. break;
  93. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
  94. val = 44100;
  95. break;
  96. case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
  97. val = 32000;
  98. break;
  99. }
  100. pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
  101. }
  102. static int check_audio(struct pvr2_v4l_cx2584x *ctxt)
  103. {
  104. struct pvr2_hdw *hdw = ctxt->hdw;
  105. return hdw->srate_dirty != 0;
  106. }
  107. struct pvr2_v4l_cx2584x_ops {
  108. void (*update)(struct pvr2_v4l_cx2584x *);
  109. int (*check)(struct pvr2_v4l_cx2584x *);
  110. };
  111. static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = {
  112. { .update = set_input, .check = check_input},
  113. { .update = set_audio, .check = check_audio},
  114. };
  115. static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt)
  116. {
  117. ctxt->client->handler = NULL;
  118. ctxt->hdw->decoder_ctrl = NULL;
  119. kfree(ctxt);
  120. }
  121. static int decoder_check(struct pvr2_v4l_cx2584x *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. if (decoder_ops[idx].check(ctxt)) {
  130. ctxt->stale_mask |= msk;
  131. }
  132. }
  133. return ctxt->stale_mask != 0;
  134. }
  135. static void decoder_update(struct pvr2_v4l_cx2584x *ctxt)
  136. {
  137. unsigned long msk;
  138. unsigned int idx;
  139. for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
  140. idx++) {
  141. msk = 1 << idx;
  142. if (!(ctxt->stale_mask & msk)) continue;
  143. ctxt->stale_mask &= ~msk;
  144. decoder_ops[idx].update(ctxt);
  145. }
  146. }
  147. static void decoder_enable(struct pvr2_v4l_cx2584x *ctxt,int fl)
  148. {
  149. pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_enable(%d)",fl);
  150. pvr2_v4l2_cmd_stream(ctxt->client,fl);
  151. }
  152. static int decoder_detect(struct pvr2_i2c_client *cp)
  153. {
  154. int ret;
  155. /* Attempt to query the decoder - let's see if it will answer */
  156. struct v4l2_queryctrl qc;
  157. memset(&qc,0,sizeof(qc));
  158. qc.id = V4L2_CID_BRIGHTNESS;
  159. ret = pvr2_i2c_client_cmd(cp,VIDIOC_QUERYCTRL,&qc);
  160. return ret == 0; /* Return true if it answered */
  161. }
  162. static int decoder_is_tuned(struct pvr2_v4l_cx2584x *ctxt)
  163. {
  164. struct v4l2_tuner vt;
  165. int ret;
  166. memset(&vt,0,sizeof(vt));
  167. ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
  168. if (ret < 0) return -EINVAL;
  169. return vt.signal ? 1 : 0;
  170. }
  171. static unsigned int decoder_describe(struct pvr2_v4l_cx2584x *ctxt,
  172. char *buf,unsigned int cnt)
  173. {
  174. return scnprintf(buf,cnt,"handler: pvrusb2-cx2584x-v4l");
  175. }
  176. static void decoder_reset(struct pvr2_v4l_cx2584x *ctxt)
  177. {
  178. int ret;
  179. ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_RESET,0);
  180. pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_reset (ret=%d)",ret);
  181. }
  182. const static struct pvr2_i2c_handler_functions hfuncs = {
  183. .detach = (void (*)(void *))decoder_detach,
  184. .check = (int (*)(void *))decoder_check,
  185. .update = (void (*)(void *))decoder_update,
  186. .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
  187. };
  188. int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw,
  189. struct pvr2_i2c_client *cp)
  190. {
  191. struct pvr2_v4l_cx2584x *ctxt;
  192. if (hdw->decoder_ctrl) return 0;
  193. if (cp->handler) return 0;
  194. if (!decoder_detect(cp)) return 0;
  195. ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
  196. if (!ctxt) return 0;
  197. memset(ctxt,0,sizeof(*ctxt));
  198. ctxt->handler.func_data = ctxt;
  199. ctxt->handler.func_table = &hfuncs;
  200. ctxt->ctrl.ctxt = ctxt;
  201. ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
  202. ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
  203. ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned;
  204. ctxt->ctrl.force_reset = (void (*)(void*))decoder_reset;
  205. ctxt->client = cp;
  206. ctxt->hdw = hdw;
  207. ctxt->stale_mask = (1 << (sizeof(decoder_ops)/
  208. sizeof(decoder_ops[0]))) - 1;
  209. hdw->decoder_ctrl = &ctxt->ctrl;
  210. cp->handler = &ctxt->handler;
  211. pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x cx2584x V4L2 handler set up",
  212. cp->client->addr);
  213. return !0;
  214. }
  215. /*
  216. Stuff for Emacs to see, in order to encourage consistent editing style:
  217. *** Local Variables: ***
  218. *** mode: c ***
  219. *** fill-column: 70 ***
  220. *** tab-width: 8 ***
  221. *** c-basic-offset: 8 ***
  222. *** End: ***
  223. */