ivtv-controls.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. ioctl control functions
  3. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  4. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include "ivtv-driver.h"
  18. #include "ivtv-cards.h"
  19. #include "ivtv-ioctl.h"
  20. #include "ivtv-routing.h"
  21. #include "ivtv-i2c.h"
  22. #include "ivtv-mailbox.h"
  23. #include "ivtv-controls.h"
  24. static const u32 user_ctrls[] = {
  25. V4L2_CID_USER_CLASS,
  26. V4L2_CID_BRIGHTNESS,
  27. V4L2_CID_CONTRAST,
  28. V4L2_CID_SATURATION,
  29. V4L2_CID_HUE,
  30. V4L2_CID_AUDIO_VOLUME,
  31. V4L2_CID_AUDIO_BALANCE,
  32. V4L2_CID_AUDIO_BASS,
  33. V4L2_CID_AUDIO_TREBLE,
  34. V4L2_CID_AUDIO_MUTE,
  35. V4L2_CID_AUDIO_LOUDNESS,
  36. 0
  37. };
  38. static const u32 *ctrl_classes[] = {
  39. user_ctrls,
  40. cx2341x_mpeg_ctrls,
  41. NULL
  42. };
  43. int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
  44. {
  45. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  46. const char *name;
  47. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  48. if (qctrl->id == 0)
  49. return -EINVAL;
  50. switch (qctrl->id) {
  51. /* Standard V4L2 controls */
  52. case V4L2_CID_BRIGHTNESS:
  53. case V4L2_CID_HUE:
  54. case V4L2_CID_SATURATION:
  55. case V4L2_CID_CONTRAST:
  56. if (itv->video_dec_func(itv, VIDIOC_QUERYCTRL, qctrl))
  57. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  58. return 0;
  59. case V4L2_CID_AUDIO_VOLUME:
  60. case V4L2_CID_AUDIO_MUTE:
  61. case V4L2_CID_AUDIO_BALANCE:
  62. case V4L2_CID_AUDIO_BASS:
  63. case V4L2_CID_AUDIO_TREBLE:
  64. case V4L2_CID_AUDIO_LOUDNESS:
  65. if (ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
  66. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  67. return 0;
  68. default:
  69. if (cx2341x_ctrl_query(&itv->params, qctrl))
  70. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  71. return 0;
  72. }
  73. strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
  74. qctrl->name[sizeof(qctrl->name) - 1] = 0;
  75. return 0;
  76. }
  77. int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
  78. {
  79. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  80. struct v4l2_queryctrl qctrl;
  81. qctrl.id = qmenu->id;
  82. ivtv_queryctrl(file, fh, &qctrl);
  83. return v4l2_ctrl_query_menu(qmenu, &qctrl,
  84. cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
  85. }
  86. int ivtv_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
  87. {
  88. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  89. switch (vctrl->id) {
  90. /* Standard V4L2 controls */
  91. case V4L2_CID_BRIGHTNESS:
  92. case V4L2_CID_HUE:
  93. case V4L2_CID_SATURATION:
  94. case V4L2_CID_CONTRAST:
  95. return itv->video_dec_func(itv, VIDIOC_S_CTRL, vctrl);
  96. case V4L2_CID_AUDIO_VOLUME:
  97. case V4L2_CID_AUDIO_MUTE:
  98. case V4L2_CID_AUDIO_BALANCE:
  99. case V4L2_CID_AUDIO_BASS:
  100. case V4L2_CID_AUDIO_TREBLE:
  101. case V4L2_CID_AUDIO_LOUDNESS:
  102. return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
  103. default:
  104. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  105. return -EINVAL;
  106. }
  107. return 0;
  108. }
  109. int ivtv_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
  110. {
  111. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  112. switch (vctrl->id) {
  113. /* Standard V4L2 controls */
  114. case V4L2_CID_BRIGHTNESS:
  115. case V4L2_CID_HUE:
  116. case V4L2_CID_SATURATION:
  117. case V4L2_CID_CONTRAST:
  118. return itv->video_dec_func(itv, VIDIOC_G_CTRL, vctrl);
  119. case V4L2_CID_AUDIO_VOLUME:
  120. case V4L2_CID_AUDIO_MUTE:
  121. case V4L2_CID_AUDIO_BALANCE:
  122. case V4L2_CID_AUDIO_BASS:
  123. case V4L2_CID_AUDIO_TREBLE:
  124. case V4L2_CID_AUDIO_LOUDNESS:
  125. return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
  126. default:
  127. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
  133. {
  134. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  135. return -EINVAL;
  136. if (atomic_read(&itv->capturing) > 0)
  137. return -EBUSY;
  138. /* First try to allocate sliced VBI buffers if needed. */
  139. if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
  140. int i;
  141. for (i = 0; i < IVTV_VBI_FRAMES; i++) {
  142. /* Yuck, hardcoded. Needs to be a define */
  143. itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
  144. if (itv->vbi.sliced_mpeg_data[i] == NULL) {
  145. while (--i >= 0) {
  146. kfree(itv->vbi.sliced_mpeg_data[i]);
  147. itv->vbi.sliced_mpeg_data[i] = NULL;
  148. }
  149. return -ENOMEM;
  150. }
  151. }
  152. }
  153. itv->vbi.insert_mpeg = fmt;
  154. if (itv->vbi.insert_mpeg == 0) {
  155. return 0;
  156. }
  157. /* Need sliced data for mpeg insertion */
  158. if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
  159. if (itv->is_60hz)
  160. itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  161. else
  162. itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  163. ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
  164. }
  165. return 0;
  166. }
  167. int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  168. {
  169. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  170. struct v4l2_control ctrl;
  171. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  172. int i;
  173. int err = 0;
  174. for (i = 0; i < c->count; i++) {
  175. ctrl.id = c->controls[i].id;
  176. ctrl.value = c->controls[i].value;
  177. err = ivtv_g_ctrl(file, fh, &ctrl);
  178. c->controls[i].value = ctrl.value;
  179. if (err) {
  180. c->error_idx = i;
  181. break;
  182. }
  183. }
  184. return err;
  185. }
  186. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  187. return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
  188. return -EINVAL;
  189. }
  190. int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  191. {
  192. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  193. struct v4l2_control ctrl;
  194. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  195. int i;
  196. int err = 0;
  197. for (i = 0; i < c->count; i++) {
  198. ctrl.id = c->controls[i].id;
  199. ctrl.value = c->controls[i].value;
  200. err = ivtv_s_ctrl(file, fh, &ctrl);
  201. c->controls[i].value = ctrl.value;
  202. if (err) {
  203. c->error_idx = i;
  204. break;
  205. }
  206. }
  207. return err;
  208. }
  209. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  210. static u32 freqs[3] = { 44100, 48000, 32000 };
  211. struct cx2341x_mpeg_params p = itv->params;
  212. int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
  213. unsigned idx;
  214. if (err)
  215. return err;
  216. if (p.video_encoding != itv->params.video_encoding) {
  217. int is_mpeg1 = p.video_encoding ==
  218. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  219. struct v4l2_format fmt;
  220. /* fix videodecoder resolution */
  221. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  222. fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
  223. fmt.fmt.pix.height = itv->params.height;
  224. itv->video_dec_func(itv, VIDIOC_S_FMT, &fmt);
  225. }
  226. err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
  227. if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
  228. err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
  229. itv->params = p;
  230. itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  231. idx = p.audio_properties & 0x03;
  232. /* The audio clock of the digitizer must match the codec sample
  233. rate otherwise you get some very strange effects. */
  234. if (idx < sizeof(freqs))
  235. ivtv_call_i2c_clients(itv, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freqs[idx]);
  236. return err;
  237. }
  238. return -EINVAL;
  239. }
  240. int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  241. {
  242. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  243. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  244. return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
  245. return -EINVAL;
  246. }