cx18-controls.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
  51. {
  52. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  53. const char *name;
  54. CX18_DEBUG_IOCTL("VIDIOC_QUERYCTRL(%08x)\n", qctrl->id);
  55. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  56. if (qctrl->id == 0)
  57. return -EINVAL;
  58. switch (qctrl->id) {
  59. /* Standard V4L2 controls */
  60. case V4L2_CID_BRIGHTNESS:
  61. case V4L2_CID_HUE:
  62. case V4L2_CID_SATURATION:
  63. case V4L2_CID_CONTRAST:
  64. if (cx18_av_cmd(cx, VIDIOC_QUERYCTRL, qctrl))
  65. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  66. return 0;
  67. case V4L2_CID_AUDIO_VOLUME:
  68. case V4L2_CID_AUDIO_MUTE:
  69. case V4L2_CID_AUDIO_BALANCE:
  70. case V4L2_CID_AUDIO_BASS:
  71. case V4L2_CID_AUDIO_TREBLE:
  72. case V4L2_CID_AUDIO_LOUDNESS:
  73. if (cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
  74. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  75. return 0;
  76. default:
  77. if (cx2341x_ctrl_query(&cx->params, qctrl))
  78. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  79. return 0;
  80. }
  81. strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
  82. qctrl->name[sizeof(qctrl->name) - 1] = 0;
  83. return 0;
  84. }
  85. int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
  86. {
  87. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  88. struct v4l2_queryctrl qctrl;
  89. CX18_DEBUG_IOCTL("VIDIOC_QUERYMENU\n");
  90. qctrl.id = qmenu->id;
  91. cx18_queryctrl(file, fh, &qctrl);
  92. return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
  93. }
  94. int cx18_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
  95. {
  96. struct cx18_open_id *id = fh;
  97. struct cx18 *cx = id->cx;
  98. int ret;
  99. s32 v = vctrl->value;
  100. ret = v4l2_prio_check(&cx->prio, &id->prio);
  101. if (ret)
  102. return ret;
  103. CX18_DEBUG_IOCTL("VIDIOC_S_CTRL(%08x, %x)\n", vctrl->id, v);
  104. switch (vctrl->id) {
  105. /* Standard V4L2 controls */
  106. case V4L2_CID_BRIGHTNESS:
  107. case V4L2_CID_HUE:
  108. case V4L2_CID_SATURATION:
  109. case V4L2_CID_CONTRAST:
  110. return cx18_av_cmd(cx, VIDIOC_S_CTRL, vctrl);
  111. case V4L2_CID_AUDIO_VOLUME:
  112. case V4L2_CID_AUDIO_MUTE:
  113. case V4L2_CID_AUDIO_BALANCE:
  114. case V4L2_CID_AUDIO_BASS:
  115. case V4L2_CID_AUDIO_TREBLE:
  116. case V4L2_CID_AUDIO_LOUDNESS:
  117. return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
  118. default:
  119. CX18_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. int cx18_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
  125. {
  126. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  127. CX18_DEBUG_IOCTL("VIDIOC_G_CTRL(%08x)\n", vctrl->id);
  128. switch (vctrl->id) {
  129. /* Standard V4L2 controls */
  130. case V4L2_CID_BRIGHTNESS:
  131. case V4L2_CID_HUE:
  132. case V4L2_CID_SATURATION:
  133. case V4L2_CID_CONTRAST:
  134. return cx18_av_cmd(cx, VIDIOC_G_CTRL, vctrl);
  135. case V4L2_CID_AUDIO_VOLUME:
  136. case V4L2_CID_AUDIO_MUTE:
  137. case V4L2_CID_AUDIO_BALANCE:
  138. case V4L2_CID_AUDIO_BASS:
  139. case V4L2_CID_AUDIO_TREBLE:
  140. case V4L2_CID_AUDIO_LOUDNESS:
  141. return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
  142. default:
  143. CX18_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
  144. return -EINVAL;
  145. }
  146. return 0;
  147. }
  148. static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt)
  149. {
  150. if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  151. return -EINVAL;
  152. if (atomic_read(&cx->ana_capturing) > 0)
  153. return -EBUSY;
  154. /* First try to allocate sliced VBI buffers if needed. */
  155. if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
  156. int i;
  157. for (i = 0; i < CX18_VBI_FRAMES; i++) {
  158. /* Yuck, hardcoded. Needs to be a define */
  159. cx->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
  160. if (cx->vbi.sliced_mpeg_data[i] == NULL) {
  161. while (--i >= 0) {
  162. kfree(cx->vbi.sliced_mpeg_data[i]);
  163. cx->vbi.sliced_mpeg_data[i] = NULL;
  164. }
  165. return -ENOMEM;
  166. }
  167. }
  168. }
  169. cx->vbi.insert_mpeg = fmt;
  170. if (cx->vbi.insert_mpeg == 0)
  171. return 0;
  172. /* Need sliced data for mpeg insertion */
  173. if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
  174. if (cx->is_60hz)
  175. cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  176. else
  177. cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  178. cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
  179. }
  180. return 0;
  181. }
  182. int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  183. {
  184. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  185. struct v4l2_control ctrl;
  186. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  187. int i;
  188. int err = 0;
  189. for (i = 0; i < c->count; i++) {
  190. ctrl.id = c->controls[i].id;
  191. ctrl.value = c->controls[i].value;
  192. err = cx18_g_ctrl(file, fh, &ctrl);
  193. c->controls[i].value = ctrl.value;
  194. if (err) {
  195. c->error_idx = i;
  196. break;
  197. }
  198. }
  199. return err;
  200. }
  201. CX18_DEBUG_IOCTL("VIDIOC_G_EXT_CTRLS\n");
  202. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  203. return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
  204. return -EINVAL;
  205. }
  206. int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  207. {
  208. struct cx18_open_id *id = fh;
  209. struct cx18 *cx = id->cx;
  210. int ret;
  211. struct v4l2_control ctrl;
  212. ret = v4l2_prio_check(&cx->prio, &id->prio);
  213. if (ret)
  214. return ret;
  215. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  216. int i;
  217. int err = 0;
  218. for (i = 0; i < c->count; i++) {
  219. ctrl.id = c->controls[i].id;
  220. ctrl.value = c->controls[i].value;
  221. err = cx18_s_ctrl(file, fh, &ctrl);
  222. c->controls[i].value = ctrl.value;
  223. if (err) {
  224. c->error_idx = i;
  225. break;
  226. }
  227. }
  228. return err;
  229. }
  230. CX18_DEBUG_IOCTL("VIDIOC_S_EXT_CTRLS\n");
  231. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  232. struct cx2341x_mpeg_params p = cx->params;
  233. int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
  234. c, VIDIOC_S_EXT_CTRLS);
  235. if (err)
  236. return err;
  237. if (p.video_encoding != cx->params.video_encoding) {
  238. int is_mpeg1 = p.video_encoding ==
  239. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  240. struct v4l2_format fmt;
  241. /* fix videodecoder resolution */
  242. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  243. fmt.fmt.pix.width = cx->params.width
  244. / (is_mpeg1 ? 2 : 1);
  245. fmt.fmt.pix.height = cx->params.height;
  246. cx18_av_cmd(cx, VIDIOC_S_FMT, &fmt);
  247. }
  248. err = cx2341x_update(cx, cx18_api_func, &cx->params, &p);
  249. if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
  250. err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
  251. cx->params = p;
  252. cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  253. cx18_audio_set_audio_clock_freq(cx, p.audio_properties & 0x03);
  254. return err;
  255. }
  256. return -EINVAL;
  257. }
  258. int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  259. {
  260. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  261. CX18_DEBUG_IOCTL("VIDIOC_TRY_EXT_CTRLS\n");
  262. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  263. return cx2341x_ext_ctrls(&cx->params,
  264. atomic_read(&cx->ana_capturing),
  265. c, VIDIOC_TRY_EXT_CTRLS);
  266. return -EINVAL;
  267. }