cx18-controls.c 7.6 KB

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