ivtv-controls.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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-audio.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. static int ivtv_queryctrl(struct ivtv *itv, struct v4l2_queryctrl *qctrl)
  44. {
  45. const char *name;
  46. IVTV_DEBUG_IOCTL("VIDIOC_QUERYCTRL(%08x)\n", qctrl->id);
  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. static int ivtv_querymenu(struct ivtv *itv, struct v4l2_querymenu *qmenu)
  78. {
  79. struct v4l2_queryctrl qctrl;
  80. qctrl.id = qmenu->id;
  81. ivtv_queryctrl(itv, &qctrl);
  82. return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
  83. }
  84. static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  85. {
  86. s32 v = vctrl->value;
  87. IVTV_DEBUG_IOCTL("VIDIOC_S_CTRL(%08x, %x)\n", vctrl->id, v);
  88. switch (vctrl->id) {
  89. /* Standard V4L2 controls */
  90. case V4L2_CID_BRIGHTNESS:
  91. case V4L2_CID_HUE:
  92. case V4L2_CID_SATURATION:
  93. case V4L2_CID_CONTRAST:
  94. return itv->video_dec_func(itv, VIDIOC_S_CTRL, vctrl);
  95. case V4L2_CID_AUDIO_VOLUME:
  96. case V4L2_CID_AUDIO_MUTE:
  97. case V4L2_CID_AUDIO_BALANCE:
  98. case V4L2_CID_AUDIO_BASS:
  99. case V4L2_CID_AUDIO_TREBLE:
  100. case V4L2_CID_AUDIO_LOUDNESS:
  101. return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
  102. default:
  103. IVTV_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
  104. return -EINVAL;
  105. }
  106. return 0;
  107. }
  108. static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  109. {
  110. IVTV_DEBUG_IOCTL("VIDIOC_G_CTRL(%08x)\n", vctrl->id);
  111. switch (vctrl->id) {
  112. /* Standard V4L2 controls */
  113. case V4L2_CID_BRIGHTNESS:
  114. case V4L2_CID_HUE:
  115. case V4L2_CID_SATURATION:
  116. case V4L2_CID_CONTRAST:
  117. return itv->video_dec_func(itv, VIDIOC_G_CTRL, vctrl);
  118. case V4L2_CID_AUDIO_VOLUME:
  119. case V4L2_CID_AUDIO_MUTE:
  120. case V4L2_CID_AUDIO_BALANCE:
  121. case V4L2_CID_AUDIO_BASS:
  122. case V4L2_CID_AUDIO_TREBLE:
  123. case V4L2_CID_AUDIO_LOUDNESS:
  124. return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
  125. default:
  126. IVTV_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
  132. {
  133. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  134. return -EINVAL;
  135. if (atomic_read(&itv->capturing) > 0)
  136. return -EBUSY;
  137. /* First try to allocate sliced VBI buffers if needed. */
  138. if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
  139. int i;
  140. for (i = 0; i < IVTV_VBI_FRAMES; i++) {
  141. /* Yuck, hardcoded. Needs to be a define */
  142. itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
  143. if (itv->vbi.sliced_mpeg_data[i] == NULL) {
  144. while (--i >= 0) {
  145. kfree(itv->vbi.sliced_mpeg_data[i]);
  146. itv->vbi.sliced_mpeg_data[i] = NULL;
  147. }
  148. return -ENOMEM;
  149. }
  150. }
  151. }
  152. itv->vbi.insert_mpeg = fmt;
  153. if (itv->vbi.insert_mpeg == 0) {
  154. return 0;
  155. }
  156. /* Need sliced data for mpeg insertion */
  157. if (get_service_set(itv->vbi.sliced_in) == 0) {
  158. if (itv->is_60hz)
  159. itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  160. else
  161. itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  162. expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
  163. }
  164. return 0;
  165. }
  166. int ivtv_control_ioctls(struct ivtv *itv, unsigned int cmd, void *arg)
  167. {
  168. struct v4l2_control ctrl;
  169. switch (cmd) {
  170. case VIDIOC_QUERYMENU:
  171. IVTV_DEBUG_IOCTL("VIDIOC_QUERYMENU\n");
  172. return ivtv_querymenu(itv, arg);
  173. case VIDIOC_QUERYCTRL:
  174. return ivtv_queryctrl(itv, arg);
  175. case VIDIOC_S_CTRL:
  176. return ivtv_s_ctrl(itv, arg);
  177. case VIDIOC_G_CTRL:
  178. return ivtv_g_ctrl(itv, arg);
  179. case VIDIOC_S_EXT_CTRLS:
  180. {
  181. struct v4l2_ext_controls *c = arg;
  182. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  183. int i;
  184. int err = 0;
  185. for (i = 0; i < c->count; i++) {
  186. ctrl.id = c->controls[i].id;
  187. ctrl.value = c->controls[i].value;
  188. err = ivtv_s_ctrl(itv, &ctrl);
  189. c->controls[i].value = ctrl.value;
  190. if (err) {
  191. c->error_idx = i;
  192. break;
  193. }
  194. }
  195. return err;
  196. }
  197. IVTV_DEBUG_IOCTL("VIDIOC_S_EXT_CTRLS\n");
  198. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  199. struct cx2341x_mpeg_params p = itv->params;
  200. int err = cx2341x_ext_ctrls(&p, arg, cmd);
  201. if (err)
  202. return err;
  203. if (p.video_encoding != itv->params.video_encoding) {
  204. int is_mpeg1 = p.video_encoding ==
  205. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  206. struct v4l2_format fmt;
  207. /* fix videodecoder resolution */
  208. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  209. fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
  210. fmt.fmt.pix.height = itv->params.height;
  211. itv->video_dec_func(itv, VIDIOC_S_FMT, &fmt);
  212. }
  213. err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
  214. if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt) {
  215. err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
  216. }
  217. itv->params = p;
  218. itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  219. ivtv_audio_set_audio_clock_freq(itv, p.audio_properties & 0x03);
  220. return err;
  221. }
  222. return -EINVAL;
  223. }
  224. case VIDIOC_G_EXT_CTRLS:
  225. {
  226. struct v4l2_ext_controls *c = arg;
  227. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  228. int i;
  229. int err = 0;
  230. for (i = 0; i < c->count; i++) {
  231. ctrl.id = c->controls[i].id;
  232. ctrl.value = c->controls[i].value;
  233. err = ivtv_g_ctrl(itv, &ctrl);
  234. c->controls[i].value = ctrl.value;
  235. if (err) {
  236. c->error_idx = i;
  237. break;
  238. }
  239. }
  240. return err;
  241. }
  242. IVTV_DEBUG_IOCTL("VIDIOC_G_EXT_CTRLS\n");
  243. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  244. return cx2341x_ext_ctrls(&itv->params, arg, cmd);
  245. return -EINVAL;
  246. }
  247. case VIDIOC_TRY_EXT_CTRLS:
  248. {
  249. struct v4l2_ext_controls *c = arg;
  250. IVTV_DEBUG_IOCTL("VIDIOC_TRY_EXT_CTRLS\n");
  251. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  252. return cx2341x_ext_ctrls(&itv->params, arg, cmd);
  253. return -EINVAL;
  254. }
  255. default:
  256. return -EINVAL;
  257. }
  258. return 0;
  259. }