ivtv-controls.c 8.2 KB

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