hdpvr-control.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hauppauge HD PVR USB driver - video 4 linux 2 interface
  4. *
  5. * Copyright (C) 2008 Janne Grunau ([email protected])
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/usb.h>
  13. #include <linux/mutex.h>
  14. #include <linux/videodev2.h>
  15. #include <media/v4l2-common.h>
  16. #include "hdpvr.h"
  17. int hdpvr_config_call(struct hdpvr_device *dev, uint value, u8 valbuf)
  18. {
  19. int ret;
  20. char request_type = 0x38, snd_request = 0x01;
  21. mutex_lock(&dev->usbc_mutex);
  22. dev->usbc_buf[0] = valbuf;
  23. ret = usb_control_msg(dev->udev,
  24. usb_sndctrlpipe(dev->udev, 0),
  25. snd_request, 0x00 | request_type,
  26. value, CTRL_DEFAULT_INDEX,
  27. dev->usbc_buf, 1, 10000);
  28. mutex_unlock(&dev->usbc_mutex);
  29. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  30. "config call request for value 0x%x returned %d\n", value,
  31. ret);
  32. return ret < 0 ? ret : 0;
  33. }
  34. int get_video_info(struct hdpvr_device *dev, struct hdpvr_video_info *vidinf)
  35. {
  36. int ret;
  37. vidinf->valid = false;
  38. mutex_lock(&dev->usbc_mutex);
  39. ret = usb_control_msg(dev->udev,
  40. usb_rcvctrlpipe(dev->udev, 0),
  41. 0x81, 0x80 | 0x38,
  42. 0x1400, 0x0003,
  43. dev->usbc_buf, 5,
  44. 1000);
  45. #ifdef HDPVR_DEBUG
  46. if (hdpvr_debug & MSG_INFO)
  47. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  48. "get video info returned: %d, %5ph\n", ret,
  49. dev->usbc_buf);
  50. #endif
  51. mutex_unlock(&dev->usbc_mutex);
  52. if (ret < 0)
  53. return ret;
  54. vidinf->width = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
  55. vidinf->height = dev->usbc_buf[3] << 8 | dev->usbc_buf[2];
  56. vidinf->fps = dev->usbc_buf[4];
  57. vidinf->valid = vidinf->width && vidinf->height && vidinf->fps;
  58. return 0;
  59. }
  60. int get_input_lines_info(struct hdpvr_device *dev)
  61. {
  62. int ret, lines;
  63. mutex_lock(&dev->usbc_mutex);
  64. ret = usb_control_msg(dev->udev,
  65. usb_rcvctrlpipe(dev->udev, 0),
  66. 0x81, 0x80 | 0x38,
  67. 0x1800, 0x0003,
  68. dev->usbc_buf, 3,
  69. 1000);
  70. #ifdef HDPVR_DEBUG
  71. if (hdpvr_debug & MSG_INFO)
  72. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  73. "get input lines info returned: %d, %3ph\n", ret,
  74. dev->usbc_buf);
  75. #else
  76. (void)ret; /* suppress compiler warning */
  77. #endif
  78. lines = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
  79. mutex_unlock(&dev->usbc_mutex);
  80. return lines;
  81. }
  82. int hdpvr_set_bitrate(struct hdpvr_device *dev)
  83. {
  84. int ret;
  85. mutex_lock(&dev->usbc_mutex);
  86. memset(dev->usbc_buf, 0, 4);
  87. dev->usbc_buf[0] = dev->options.bitrate;
  88. dev->usbc_buf[2] = dev->options.peak_bitrate;
  89. ret = usb_control_msg(dev->udev,
  90. usb_sndctrlpipe(dev->udev, 0),
  91. 0x01, 0x38, CTRL_BITRATE_VALUE,
  92. CTRL_DEFAULT_INDEX, dev->usbc_buf, 4, 1000);
  93. mutex_unlock(&dev->usbc_mutex);
  94. return ret;
  95. }
  96. int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
  97. enum v4l2_mpeg_audio_encoding codec)
  98. {
  99. int ret = 0;
  100. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  101. mutex_lock(&dev->usbc_mutex);
  102. memset(dev->usbc_buf, 0, 2);
  103. dev->usbc_buf[0] = input;
  104. if (codec == V4L2_MPEG_AUDIO_ENCODING_AAC)
  105. dev->usbc_buf[1] = 0;
  106. else if (codec == V4L2_MPEG_AUDIO_ENCODING_AC3)
  107. dev->usbc_buf[1] = 1;
  108. else {
  109. mutex_unlock(&dev->usbc_mutex);
  110. v4l2_err(&dev->v4l2_dev, "invalid audio codec %d\n",
  111. codec);
  112. ret = -EINVAL;
  113. goto error;
  114. }
  115. ret = usb_control_msg(dev->udev,
  116. usb_sndctrlpipe(dev->udev, 0),
  117. 0x01, 0x38, CTRL_AUDIO_INPUT_VALUE,
  118. CTRL_DEFAULT_INDEX, dev->usbc_buf, 2,
  119. 1000);
  120. mutex_unlock(&dev->usbc_mutex);
  121. if (ret == 2)
  122. ret = 0;
  123. } else
  124. ret = hdpvr_config_call(dev, CTRL_AUDIO_INPUT_VALUE, input);
  125. error:
  126. return ret;
  127. }
  128. int hdpvr_set_options(struct hdpvr_device *dev)
  129. {
  130. hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, dev->options.video_std);
  131. hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE,
  132. dev->options.video_input+1);
  133. hdpvr_set_audio(dev, dev->options.audio_input+1,
  134. dev->options.audio_codec);
  135. hdpvr_set_bitrate(dev);
  136. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  137. dev->options.bitrate_mode);
  138. hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, dev->options.gop_mode);
  139. hdpvr_config_call(dev, CTRL_BRIGHTNESS, dev->options.brightness);
  140. hdpvr_config_call(dev, CTRL_CONTRAST, dev->options.contrast);
  141. hdpvr_config_call(dev, CTRL_HUE, dev->options.hue);
  142. hdpvr_config_call(dev, CTRL_SATURATION, dev->options.saturation);
  143. hdpvr_config_call(dev, CTRL_SHARPNESS, dev->options.sharpness);
  144. return 0;
  145. }