ivtv-controls.c 8.3 KB

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