pvrusb2-cx2584x-v4l.c 6.6 KB

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