ivtv-controls.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. ioctl control functions
  3. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  4. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include "ivtv-driver.h"
  20. #include "ivtv-cards.h"
  21. #include "ivtv-ioctl.h"
  22. #include "ivtv-routing.h"
  23. #include "ivtv-i2c.h"
  24. #include "ivtv-mailbox.h"
  25. #include "ivtv-controls.h"
  26. /* Must be sorted from low to high control ID! */
  27. static const u32 user_ctrls[] = {
  28. V4L2_CID_USER_CLASS,
  29. V4L2_CID_BRIGHTNESS,
  30. V4L2_CID_CONTRAST,
  31. V4L2_CID_SATURATION,
  32. V4L2_CID_HUE,
  33. V4L2_CID_AUDIO_VOLUME,
  34. V4L2_CID_AUDIO_BALANCE,
  35. V4L2_CID_AUDIO_BASS,
  36. V4L2_CID_AUDIO_TREBLE,
  37. V4L2_CID_AUDIO_MUTE,
  38. V4L2_CID_AUDIO_LOUDNESS,
  39. 0
  40. };
  41. static const u32 *ctrl_classes[] = {
  42. user_ctrls,
  43. cx2341x_mpeg_ctrls,
  44. NULL
  45. };
  46. int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
  47. {
  48. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  49. const char *name;
  50. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  51. if (qctrl->id == 0)
  52. return -EINVAL;
  53. switch (qctrl->id) {
  54. /* Standard V4L2 controls */
  55. case V4L2_CID_USER_CLASS:
  56. return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
  57. case V4L2_CID_BRIGHTNESS:
  58. case V4L2_CID_HUE:
  59. case V4L2_CID_SATURATION:
  60. case V4L2_CID_CONTRAST:
  61. if (v4l2_subdev_call(itv->sd_video, core, queryctrl, qctrl))
  62. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  63. return 0;
  64. case V4L2_CID_AUDIO_VOLUME:
  65. case V4L2_CID_AUDIO_MUTE:
  66. case V4L2_CID_AUDIO_BALANCE:
  67. case V4L2_CID_AUDIO_BASS:
  68. case V4L2_CID_AUDIO_TREBLE:
  69. case V4L2_CID_AUDIO_LOUDNESS:
  70. if (v4l2_subdev_call(itv->sd_audio, core, queryctrl, qctrl))
  71. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  72. return 0;
  73. default:
  74. if (cx2341x_ctrl_query(&itv->params, qctrl))
  75. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  76. return 0;
  77. }
  78. strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
  79. qctrl->name[sizeof(qctrl->name) - 1] = 0;
  80. return 0;
  81. }
  82. int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
  83. {
  84. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  85. struct v4l2_queryctrl qctrl;
  86. qctrl.id = qmenu->id;
  87. ivtv_queryctrl(file, fh, &qctrl);
  88. return v4l2_ctrl_query_menu(qmenu, &qctrl,
  89. cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
  90. }
  91. static int ivtv_try_ctrl(struct file *file, void *fh,
  92. struct v4l2_ext_control *vctrl)
  93. {
  94. struct v4l2_queryctrl qctrl;
  95. const char **menu_items = NULL;
  96. int err;
  97. qctrl.id = vctrl->id;
  98. err = ivtv_queryctrl(file, fh, &qctrl);
  99. if (err)
  100. return err;
  101. if (qctrl.type == V4L2_CTRL_TYPE_MENU)
  102. menu_items = v4l2_ctrl_get_menu(qctrl.id);
  103. return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
  104. }
  105. static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  106. {
  107. switch (vctrl->id) {
  108. /* Standard V4L2 controls */
  109. case V4L2_CID_BRIGHTNESS:
  110. case V4L2_CID_HUE:
  111. case V4L2_CID_SATURATION:
  112. case V4L2_CID_CONTRAST:
  113. return v4l2_subdev_call(itv->sd_video, core, s_ctrl, vctrl);
  114. case V4L2_CID_AUDIO_VOLUME:
  115. case V4L2_CID_AUDIO_MUTE:
  116. case V4L2_CID_AUDIO_BALANCE:
  117. case V4L2_CID_AUDIO_BASS:
  118. case V4L2_CID_AUDIO_TREBLE:
  119. case V4L2_CID_AUDIO_LOUDNESS:
  120. return v4l2_subdev_call(itv->sd_audio, core, s_ctrl, vctrl);
  121. default:
  122. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  123. return -EINVAL;
  124. }
  125. return 0;
  126. }
  127. static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  128. {
  129. switch (vctrl->id) {
  130. /* Standard V4L2 controls */
  131. case V4L2_CID_BRIGHTNESS:
  132. case V4L2_CID_HUE:
  133. case V4L2_CID_SATURATION:
  134. case V4L2_CID_CONTRAST:
  135. return v4l2_subdev_call(itv->sd_video, core, g_ctrl, vctrl);
  136. case V4L2_CID_AUDIO_VOLUME:
  137. case V4L2_CID_AUDIO_MUTE:
  138. case V4L2_CID_AUDIO_BALANCE:
  139. case V4L2_CID_AUDIO_BASS:
  140. case V4L2_CID_AUDIO_TREBLE:
  141. case V4L2_CID_AUDIO_LOUDNESS:
  142. return v4l2_subdev_call(itv->sd_audio, core, g_ctrl, vctrl);
  143. default:
  144. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
  150. {
  151. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  152. return -EINVAL;
  153. if (atomic_read(&itv->capturing) > 0)
  154. return -EBUSY;
  155. /* First try to allocate sliced VBI buffers if needed. */
  156. if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
  157. int i;
  158. for (i = 0; i < IVTV_VBI_FRAMES; i++) {
  159. /* Yuck, hardcoded. Needs to be a define */
  160. itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
  161. if (itv->vbi.sliced_mpeg_data[i] == NULL) {
  162. while (--i >= 0) {
  163. kfree(itv->vbi.sliced_mpeg_data[i]);
  164. itv->vbi.sliced_mpeg_data[i] = NULL;
  165. }
  166. return -ENOMEM;
  167. }
  168. }
  169. }
  170. itv->vbi.insert_mpeg = fmt;
  171. if (itv->vbi.insert_mpeg == 0) {
  172. return 0;
  173. }
  174. /* Need sliced data for mpeg insertion */
  175. if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
  176. if (itv->is_60hz)
  177. itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  178. else
  179. itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  180. ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
  181. }
  182. return 0;
  183. }
  184. int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  185. {
  186. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  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 = ivtv_g_ctrl(itv, &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(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
  205. return -EINVAL;
  206. }
  207. int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  208. {
  209. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  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 = ivtv_s_ctrl(itv, &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. static u32 freqs[3] = { 44100, 48000, 32000 };
  228. struct cx2341x_mpeg_params p = itv->params;
  229. int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
  230. unsigned idx;
  231. if (err)
  232. return err;
  233. if (p.video_encoding != itv->params.video_encoding) {
  234. int is_mpeg1 = p.video_encoding ==
  235. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  236. struct v4l2_mbus_framefmt fmt;
  237. /* fix videodecoder resolution */
  238. fmt.width = itv->params.width / (is_mpeg1 ? 2 : 1);
  239. fmt.height = itv->params.height;
  240. fmt.code = V4L2_MBUS_FMT_FIXED;
  241. v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
  242. }
  243. err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
  244. if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
  245. err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
  246. itv->params = p;
  247. itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  248. idx = p.audio_properties & 0x03;
  249. /* The audio clock of the digitizer must match the codec sample
  250. rate otherwise you get some very strange effects. */
  251. if (idx < ARRAY_SIZE(freqs))
  252. ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
  253. return err;
  254. }
  255. return -EINVAL;
  256. }
  257. int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  258. {
  259. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  260. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  261. int i;
  262. int err = 0;
  263. for (i = 0; i < c->count; i++) {
  264. err = ivtv_try_ctrl(file, fh, &c->controls[i]);
  265. if (err) {
  266. c->error_idx = i;
  267. break;
  268. }
  269. }
  270. return err;
  271. }
  272. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  273. return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
  274. return -EINVAL;
  275. }