cx18-controls.c 8.0 KB

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