cx18-controls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <linux/kernel.h>
  24. #include "cx18-driver.h"
  25. #include "cx18-cards.h"
  26. #include "cx18-ioctl.h"
  27. #include "cx18-audio.h"
  28. #include "cx18-mailbox.h"
  29. #include "cx18-controls.h"
  30. /* Must be sorted from low to high control ID! */
  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_USER_CLASS:
  60. return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
  61. case V4L2_CID_BRIGHTNESS:
  62. case V4L2_CID_HUE:
  63. case V4L2_CID_SATURATION:
  64. case V4L2_CID_CONTRAST:
  65. if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
  66. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  67. return 0;
  68. case V4L2_CID_AUDIO_VOLUME:
  69. case V4L2_CID_AUDIO_MUTE:
  70. case V4L2_CID_AUDIO_BALANCE:
  71. case V4L2_CID_AUDIO_BASS:
  72. case V4L2_CID_AUDIO_TREBLE:
  73. case V4L2_CID_AUDIO_LOUDNESS:
  74. if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
  75. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  76. return 0;
  77. default:
  78. if (cx2341x_ctrl_query(&cx->params, qctrl))
  79. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  80. return 0;
  81. }
  82. strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
  83. qctrl->name[sizeof(qctrl->name) - 1] = 0;
  84. return 0;
  85. }
  86. int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
  87. {
  88. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  89. struct v4l2_queryctrl qctrl;
  90. qctrl.id = qmenu->id;
  91. cx18_queryctrl(file, fh, &qctrl);
  92. return v4l2_ctrl_query_menu(qmenu, &qctrl,
  93. cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
  94. }
  95. static int cx18_try_ctrl(struct file *file, void *fh,
  96. struct v4l2_ext_control *vctrl)
  97. {
  98. struct v4l2_queryctrl qctrl;
  99. const char **menu_items = NULL;
  100. int err;
  101. qctrl.id = vctrl->id;
  102. err = cx18_queryctrl(file, fh, &qctrl);
  103. if (err)
  104. return err;
  105. if (qctrl.type == V4L2_CTRL_TYPE_MENU)
  106. menu_items = v4l2_ctrl_get_menu(qctrl.id);
  107. return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
  108. }
  109. static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
  110. {
  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 v4l2_subdev_call(cx->sd_av, core, s_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 v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
  125. default:
  126. CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
  132. {
  133. switch (vctrl->id) {
  134. /* Standard V4L2 controls */
  135. case V4L2_CID_BRIGHTNESS:
  136. case V4L2_CID_HUE:
  137. case V4L2_CID_SATURATION:
  138. case V4L2_CID_CONTRAST:
  139. return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
  140. case V4L2_CID_AUDIO_VOLUME:
  141. case V4L2_CID_AUDIO_MUTE:
  142. case V4L2_CID_AUDIO_BALANCE:
  143. case V4L2_CID_AUDIO_BASS:
  144. case V4L2_CID_AUDIO_TREBLE:
  145. case V4L2_CID_AUDIO_LOUDNESS:
  146. return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
  147. default:
  148. CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  149. return -EINVAL;
  150. }
  151. return 0;
  152. }
  153. static int cx18_setup_vbi_fmt(struct cx18 *cx,
  154. enum v4l2_mpeg_stream_vbi_fmt fmt,
  155. enum v4l2_mpeg_stream_type type)
  156. {
  157. if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  158. return -EINVAL;
  159. if (atomic_read(&cx->ana_capturing) > 0)
  160. return -EBUSY;
  161. if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
  162. !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
  163. type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
  164. type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
  165. /* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
  166. cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
  167. CX18_DEBUG_INFO("disabled insertion of sliced VBI data into "
  168. "the MPEG stream\n");
  169. return 0;
  170. }
  171. /* Allocate sliced VBI buffers if needed. */
  172. if (cx->vbi.sliced_mpeg_data[0] == NULL) {
  173. int i;
  174. for (i = 0; i < CX18_VBI_FRAMES; i++) {
  175. cx->vbi.sliced_mpeg_data[i] =
  176. kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
  177. if (cx->vbi.sliced_mpeg_data[i] == NULL) {
  178. while (--i >= 0) {
  179. kfree(cx->vbi.sliced_mpeg_data[i]);
  180. cx->vbi.sliced_mpeg_data[i] = NULL;
  181. }
  182. cx->vbi.insert_mpeg =
  183. V4L2_MPEG_STREAM_VBI_FMT_NONE;
  184. CX18_WARN("Unable to allocate buffers for "
  185. "sliced VBI data insertion\n");
  186. return -ENOMEM;
  187. }
  188. }
  189. }
  190. cx->vbi.insert_mpeg = fmt;
  191. CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,"
  192. "when sliced VBI is enabled\n");
  193. /*
  194. * If our current settings have no lines set for capture, store a valid,
  195. * default set of service lines to capture, in our current settings.
  196. */
  197. if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
  198. if (cx->is_60hz)
  199. cx->vbi.sliced_in->service_set =
  200. V4L2_SLICED_CAPTION_525;
  201. else
  202. cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  203. cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
  204. }
  205. return 0;
  206. }
  207. int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  208. {
  209. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  210. struct v4l2_control ctrl;
  211. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  212. int i;
  213. int err = 0;
  214. for (i = 0; i < c->count; i++) {
  215. ctrl.id = c->controls[i].id;
  216. ctrl.value = c->controls[i].value;
  217. err = cx18_g_ctrl(cx, &ctrl);
  218. c->controls[i].value = ctrl.value;
  219. if (err) {
  220. c->error_idx = i;
  221. break;
  222. }
  223. }
  224. return err;
  225. }
  226. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  227. return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
  228. return -EINVAL;
  229. }
  230. int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  231. {
  232. struct cx18_open_id *id = fh;
  233. struct cx18 *cx = id->cx;
  234. int ret;
  235. struct v4l2_control ctrl;
  236. ret = v4l2_prio_check(&cx->prio, &id->prio);
  237. if (ret)
  238. return ret;
  239. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  240. int i;
  241. int err = 0;
  242. for (i = 0; i < c->count; i++) {
  243. ctrl.id = c->controls[i].id;
  244. ctrl.value = c->controls[i].value;
  245. err = cx18_s_ctrl(cx, &ctrl);
  246. c->controls[i].value = ctrl.value;
  247. if (err) {
  248. c->error_idx = i;
  249. break;
  250. }
  251. }
  252. return err;
  253. }
  254. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  255. static u32 freqs[3] = { 44100, 48000, 32000 };
  256. struct cx18_api_func_private priv;
  257. struct cx2341x_mpeg_params p = cx->params;
  258. int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
  259. c, VIDIOC_S_EXT_CTRLS);
  260. unsigned int idx;
  261. if (err)
  262. return err;
  263. if (p.video_encoding != cx->params.video_encoding) {
  264. int is_mpeg1 = p.video_encoding ==
  265. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  266. struct v4l2_format fmt;
  267. /* fix videodecoder resolution */
  268. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  269. fmt.fmt.pix.width = cx->params.width
  270. / (is_mpeg1 ? 2 : 1);
  271. fmt.fmt.pix.height = cx->params.height;
  272. v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
  273. }
  274. priv.cx = cx;
  275. priv.s = &cx->streams[id->type];
  276. err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
  277. if (!err &&
  278. (cx->params.stream_vbi_fmt != p.stream_vbi_fmt ||
  279. cx->params.stream_type != p.stream_type))
  280. err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt,
  281. p.stream_type);
  282. cx->params = p;
  283. cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  284. idx = p.audio_properties & 0x03;
  285. /* The audio clock of the digitizer must match the codec sample
  286. rate otherwise you get some very strange effects. */
  287. if (idx < ARRAY_SIZE(freqs))
  288. cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
  289. return err;
  290. }
  291. return -EINVAL;
  292. }
  293. int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  294. {
  295. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  296. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  297. int i;
  298. int err = 0;
  299. for (i = 0; i < c->count; i++) {
  300. err = cx18_try_ctrl(file, fh, &c->controls[i]);
  301. if (err) {
  302. c->error_idx = i;
  303. break;
  304. }
  305. }
  306. return err;
  307. }
  308. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  309. return cx2341x_ext_ctrls(&cx->params,
  310. atomic_read(&cx->ana_capturing),
  311. c, VIDIOC_TRY_EXT_CTRLS);
  312. return -EINVAL;
  313. }