cx18-controls.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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, enum v4l2_mpeg_stream_vbi_fmt fmt)
  151. {
  152. if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  153. return -EINVAL;
  154. if (atomic_read(&cx->ana_capturing) > 0)
  155. return -EBUSY;
  156. /* First try to allocate sliced VBI buffers if needed. */
  157. if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
  158. int i;
  159. for (i = 0; i < CX18_VBI_FRAMES; i++) {
  160. cx->vbi.sliced_mpeg_data[i] =
  161. kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
  162. if (cx->vbi.sliced_mpeg_data[i] == NULL) {
  163. while (--i >= 0) {
  164. kfree(cx->vbi.sliced_mpeg_data[i]);
  165. cx->vbi.sliced_mpeg_data[i] = NULL;
  166. }
  167. return -ENOMEM;
  168. }
  169. }
  170. }
  171. cx->vbi.insert_mpeg = fmt;
  172. if (cx->vbi.insert_mpeg == 0)
  173. return 0;
  174. /* Need sliced data for mpeg insertion */
  175. if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
  176. if (cx->is_60hz)
  177. cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  178. else
  179. cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  180. cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
  181. }
  182. return 0;
  183. }
  184. int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  185. {
  186. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  187. struct v4l2_control ctrl;
  188. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  189. int i;
  190. int err = 0;
  191. for (i = 0; i < c->count; i++) {
  192. ctrl.id = c->controls[i].id;
  193. ctrl.value = c->controls[i].value;
  194. err = cx18_g_ctrl(cx, &ctrl);
  195. c->controls[i].value = ctrl.value;
  196. if (err) {
  197. c->error_idx = i;
  198. break;
  199. }
  200. }
  201. return err;
  202. }
  203. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  204. return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
  205. return -EINVAL;
  206. }
  207. int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  208. {
  209. struct cx18_open_id *id = fh;
  210. struct cx18 *cx = id->cx;
  211. int ret;
  212. struct v4l2_control ctrl;
  213. ret = v4l2_prio_check(&cx->prio, &id->prio);
  214. if (ret)
  215. return ret;
  216. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  217. int i;
  218. int err = 0;
  219. for (i = 0; i < c->count; i++) {
  220. ctrl.id = c->controls[i].id;
  221. ctrl.value = c->controls[i].value;
  222. err = cx18_s_ctrl(cx, &ctrl);
  223. c->controls[i].value = ctrl.value;
  224. if (err) {
  225. c->error_idx = i;
  226. break;
  227. }
  228. }
  229. return err;
  230. }
  231. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
  232. static u32 freqs[3] = { 44100, 48000, 32000 };
  233. struct cx18_api_func_private priv;
  234. struct cx2341x_mpeg_params p = cx->params;
  235. int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
  236. c, VIDIOC_S_EXT_CTRLS);
  237. unsigned int idx;
  238. if (err)
  239. return err;
  240. if (p.video_encoding != cx->params.video_encoding) {
  241. int is_mpeg1 = p.video_encoding ==
  242. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  243. struct v4l2_format fmt;
  244. /* fix videodecoder resolution */
  245. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  246. fmt.fmt.pix.width = cx->params.width
  247. / (is_mpeg1 ? 2 : 1);
  248. fmt.fmt.pix.height = cx->params.height;
  249. v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
  250. }
  251. priv.cx = cx;
  252. priv.s = &cx->streams[id->type];
  253. err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
  254. if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
  255. err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
  256. cx->params = p;
  257. cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  258. idx = p.audio_properties & 0x03;
  259. /* The audio clock of the digitizer must match the codec sample
  260. rate otherwise you get some very strange effects. */
  261. if (idx < sizeof(freqs))
  262. cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
  263. return err;
  264. }
  265. return -EINVAL;
  266. }
  267. int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  268. {
  269. struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
  270. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  271. int i;
  272. int err = 0;
  273. for (i = 0; i < c->count; i++) {
  274. err = cx18_try_ctrl(file, fh, &c->controls[i]);
  275. if (err) {
  276. c->error_idx = i;
  277. break;
  278. }
  279. }
  280. return err;
  281. }
  282. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  283. return cx2341x_ext_ctrls(&cx->params,
  284. atomic_read(&cx->ana_capturing),
  285. c, VIDIOC_TRY_EXT_CTRLS);
  286. return -EINVAL;
  287. }