ivtv-controls.c 8.2 KB

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