ivtv-controls.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "ivtv-driver.h"
  19. #include "ivtv-cards.h"
  20. #include "ivtv-ioctl.h"
  21. #include "ivtv-routing.h"
  22. #include "ivtv-i2c.h"
  23. #include "ivtv-mailbox.h"
  24. #include "ivtv-controls.h"
  25. /* Must be sorted from low to high control ID! */
  26. static const u32 user_ctrls[] = {
  27. V4L2_CID_USER_CLASS,
  28. V4L2_CID_BRIGHTNESS,
  29. V4L2_CID_CONTRAST,
  30. V4L2_CID_SATURATION,
  31. V4L2_CID_HUE,
  32. V4L2_CID_AUDIO_VOLUME,
  33. V4L2_CID_AUDIO_BALANCE,
  34. V4L2_CID_AUDIO_BASS,
  35. V4L2_CID_AUDIO_TREBLE,
  36. V4L2_CID_AUDIO_MUTE,
  37. V4L2_CID_AUDIO_LOUDNESS,
  38. 0
  39. };
  40. static const u32 *ctrl_classes[] = {
  41. user_ctrls,
  42. cx2341x_mpeg_ctrls,
  43. NULL
  44. };
  45. int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
  46. {
  47. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  48. const char *name;
  49. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  50. if (qctrl->id == 0)
  51. return -EINVAL;
  52. switch (qctrl->id) {
  53. /* Standard V4L2 controls */
  54. case V4L2_CID_USER_CLASS:
  55. return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
  56. case V4L2_CID_BRIGHTNESS:
  57. case V4L2_CID_HUE:
  58. case V4L2_CID_SATURATION:
  59. case V4L2_CID_CONTRAST:
  60. if (v4l2_subdev_call(itv->sd_video, core, queryctrl, qctrl))
  61. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  62. return 0;
  63. case V4L2_CID_AUDIO_VOLUME:
  64. case V4L2_CID_AUDIO_MUTE:
  65. case V4L2_CID_AUDIO_BALANCE:
  66. case V4L2_CID_AUDIO_BASS:
  67. case V4L2_CID_AUDIO_TREBLE:
  68. case V4L2_CID_AUDIO_LOUDNESS:
  69. if (v4l2_subdev_call(itv->sd_audio, core, queryctrl, qctrl))
  70. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  71. return 0;
  72. default:
  73. if (cx2341x_ctrl_query(&itv->params, qctrl))
  74. qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
  75. return 0;
  76. }
  77. strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
  78. qctrl->name[sizeof(qctrl->name) - 1] = 0;
  79. return 0;
  80. }
  81. int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
  82. {
  83. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  84. struct v4l2_queryctrl qctrl;
  85. qctrl.id = qmenu->id;
  86. ivtv_queryctrl(file, fh, &qctrl);
  87. return v4l2_ctrl_query_menu(qmenu, &qctrl,
  88. cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
  89. }
  90. static int ivtv_try_ctrl(struct file *file, void *fh,
  91. struct v4l2_ext_control *vctrl)
  92. {
  93. struct v4l2_queryctrl qctrl;
  94. const char **menu_items = NULL;
  95. int err;
  96. qctrl.id = vctrl->id;
  97. err = ivtv_queryctrl(file, fh, &qctrl);
  98. if (err)
  99. return err;
  100. if (qctrl.type == V4L2_CTRL_TYPE_MENU)
  101. menu_items = v4l2_ctrl_get_menu(qctrl.id);
  102. return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
  103. }
  104. static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  105. {
  106. switch (vctrl->id) {
  107. /* Standard V4L2 controls */
  108. case V4L2_CID_BRIGHTNESS:
  109. case V4L2_CID_HUE:
  110. case V4L2_CID_SATURATION:
  111. case V4L2_CID_CONTRAST:
  112. return v4l2_subdev_call(itv->sd_video, core, s_ctrl, vctrl);
  113. case V4L2_CID_AUDIO_VOLUME:
  114. case V4L2_CID_AUDIO_MUTE:
  115. case V4L2_CID_AUDIO_BALANCE:
  116. case V4L2_CID_AUDIO_BASS:
  117. case V4L2_CID_AUDIO_TREBLE:
  118. case V4L2_CID_AUDIO_LOUDNESS:
  119. return v4l2_subdev_call(itv->sd_audio, core, s_ctrl, vctrl);
  120. default:
  121. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
  127. {
  128. switch (vctrl->id) {
  129. /* Standard V4L2 controls */
  130. case V4L2_CID_BRIGHTNESS:
  131. case V4L2_CID_HUE:
  132. case V4L2_CID_SATURATION:
  133. case V4L2_CID_CONTRAST:
  134. return v4l2_subdev_call(itv->sd_video, core, g_ctrl, vctrl);
  135. case V4L2_CID_AUDIO_VOLUME:
  136. case V4L2_CID_AUDIO_MUTE:
  137. case V4L2_CID_AUDIO_BALANCE:
  138. case V4L2_CID_AUDIO_BASS:
  139. case V4L2_CID_AUDIO_TREBLE:
  140. case V4L2_CID_AUDIO_LOUDNESS:
  141. return v4l2_subdev_call(itv->sd_audio, core, g_ctrl, vctrl);
  142. default:
  143. IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
  144. return -EINVAL;
  145. }
  146. return 0;
  147. }
  148. static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
  149. {
  150. if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
  151. return -EINVAL;
  152. if (atomic_read(&itv->capturing) > 0)
  153. return -EBUSY;
  154. /* First try to allocate sliced VBI buffers if needed. */
  155. if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
  156. int i;
  157. for (i = 0; i < IVTV_VBI_FRAMES; i++) {
  158. /* Yuck, hardcoded. Needs to be a define */
  159. itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
  160. if (itv->vbi.sliced_mpeg_data[i] == NULL) {
  161. while (--i >= 0) {
  162. kfree(itv->vbi.sliced_mpeg_data[i]);
  163. itv->vbi.sliced_mpeg_data[i] = NULL;
  164. }
  165. return -ENOMEM;
  166. }
  167. }
  168. }
  169. itv->vbi.insert_mpeg = fmt;
  170. if (itv->vbi.insert_mpeg == 0) {
  171. return 0;
  172. }
  173. /* Need sliced data for mpeg insertion */
  174. if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
  175. if (itv->is_60hz)
  176. itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
  177. else
  178. itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
  179. ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
  180. }
  181. return 0;
  182. }
  183. int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  184. {
  185. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  186. struct v4l2_control ctrl;
  187. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  188. int i;
  189. int err = 0;
  190. for (i = 0; i < c->count; i++) {
  191. ctrl.id = c->controls[i].id;
  192. ctrl.value = c->controls[i].value;
  193. err = ivtv_g_ctrl(itv, &ctrl);
  194. c->controls[i].value = ctrl.value;
  195. if (err) {
  196. c->error_idx = i;
  197. break;
  198. }
  199. }
  200. return err;
  201. }
  202. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  203. return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
  204. return -EINVAL;
  205. }
  206. int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  207. {
  208. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  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 = ivtv_s_ctrl(itv, &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. static u32 freqs[3] = { 44100, 48000, 32000 };
  227. struct cx2341x_mpeg_params p = itv->params;
  228. int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
  229. unsigned idx;
  230. if (err)
  231. return err;
  232. if (p.video_encoding != itv->params.video_encoding) {
  233. int is_mpeg1 = p.video_encoding ==
  234. V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  235. struct v4l2_format fmt;
  236. /* fix videodecoder resolution */
  237. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  238. fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
  239. fmt.fmt.pix.height = itv->params.height;
  240. v4l2_subdev_call(itv->sd_video, video, s_fmt, &fmt);
  241. }
  242. err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
  243. if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
  244. err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
  245. itv->params = p;
  246. itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
  247. idx = p.audio_properties & 0x03;
  248. /* The audio clock of the digitizer must match the codec sample
  249. rate otherwise you get some very strange effects. */
  250. if (idx < ARRAY_SIZE(freqs))
  251. ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
  252. return err;
  253. }
  254. return -EINVAL;
  255. }
  256. int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
  257. {
  258. struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
  259. if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
  260. int i;
  261. int err = 0;
  262. for (i = 0; i < c->count; i++) {
  263. err = ivtv_try_ctrl(file, fh, &c->controls[i]);
  264. if (err) {
  265. c->error_idx = i;
  266. break;
  267. }
  268. }
  269. return err;
  270. }
  271. if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
  272. return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
  273. return -EINVAL;
  274. }