hdpvr-control.c 4.9 KB

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