cx18-controls.c 9.3 KB

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