cx18-controls.c 9.0 KB

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