cx18-controls.c 9.2 KB

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