hdpvr-control.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. vidinf->valid = false;
  42. mutex_lock(&dev->usbc_mutex);
  43. ret = usb_control_msg(dev->udev,
  44. usb_rcvctrlpipe(dev->udev, 0),
  45. 0x81, 0x80 | 0x38,
  46. 0x1400, 0x0003,
  47. dev->usbc_buf, 5,
  48. 1000);
  49. #ifdef HDPVR_DEBUG
  50. if (hdpvr_debug & MSG_INFO) {
  51. char print_buf[15];
  52. hex_dump_to_buffer(dev->usbc_buf, 5, 16, 1, print_buf,
  53. sizeof(print_buf), 0);
  54. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  55. "get video info returned: %d, %s\n", ret, print_buf);
  56. }
  57. #endif
  58. mutex_unlock(&dev->usbc_mutex);
  59. if (ret < 0)
  60. return ret;
  61. vidinf->width = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
  62. vidinf->height = dev->usbc_buf[3] << 8 | dev->usbc_buf[2];
  63. vidinf->fps = dev->usbc_buf[4];
  64. vidinf->valid = vidinf->width && vidinf->height && vidinf->fps;
  65. return 0;
  66. }
  67. int get_input_lines_info(struct hdpvr_device *dev)
  68. {
  69. #ifdef HDPVR_DEBUG
  70. char print_buf[9];
  71. #endif
  72. int ret, lines;
  73. mutex_lock(&dev->usbc_mutex);
  74. ret = usb_control_msg(dev->udev,
  75. usb_rcvctrlpipe(dev->udev, 0),
  76. 0x81, 0x80 | 0x38,
  77. 0x1800, 0x0003,
  78. dev->usbc_buf, 3,
  79. 1000);
  80. #ifdef HDPVR_DEBUG
  81. if (hdpvr_debug & MSG_INFO) {
  82. hex_dump_to_buffer(dev->usbc_buf, 3, 16, 1, print_buf,
  83. sizeof(print_buf), 0);
  84. v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
  85. "get input lines info returned: %d, %s\n", ret,
  86. print_buf);
  87. }
  88. #else
  89. (void)ret; /* suppress compiler warning */
  90. #endif
  91. lines = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
  92. mutex_unlock(&dev->usbc_mutex);
  93. return lines;
  94. }
  95. int hdpvr_set_bitrate(struct hdpvr_device *dev)
  96. {
  97. int ret;
  98. mutex_lock(&dev->usbc_mutex);
  99. memset(dev->usbc_buf, 0, 4);
  100. dev->usbc_buf[0] = dev->options.bitrate;
  101. dev->usbc_buf[2] = dev->options.peak_bitrate;
  102. ret = usb_control_msg(dev->udev,
  103. usb_sndctrlpipe(dev->udev, 0),
  104. 0x01, 0x38, CTRL_BITRATE_VALUE,
  105. CTRL_DEFAULT_INDEX, dev->usbc_buf, 4, 1000);
  106. mutex_unlock(&dev->usbc_mutex);
  107. return ret;
  108. }
  109. int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
  110. enum v4l2_mpeg_audio_encoding codec)
  111. {
  112. int ret = 0;
  113. if (dev->flags & HDPVR_FLAG_AC3_CAP) {
  114. mutex_lock(&dev->usbc_mutex);
  115. memset(dev->usbc_buf, 0, 2);
  116. dev->usbc_buf[0] = input;
  117. if (codec == V4L2_MPEG_AUDIO_ENCODING_AAC)
  118. dev->usbc_buf[1] = 0;
  119. else if (codec == V4L2_MPEG_AUDIO_ENCODING_AC3)
  120. dev->usbc_buf[1] = 1;
  121. else {
  122. mutex_unlock(&dev->usbc_mutex);
  123. v4l2_err(&dev->v4l2_dev, "invalid audio codec %d\n",
  124. codec);
  125. ret = -EINVAL;
  126. goto error;
  127. }
  128. ret = usb_control_msg(dev->udev,
  129. usb_sndctrlpipe(dev->udev, 0),
  130. 0x01, 0x38, CTRL_AUDIO_INPUT_VALUE,
  131. CTRL_DEFAULT_INDEX, dev->usbc_buf, 2,
  132. 1000);
  133. mutex_unlock(&dev->usbc_mutex);
  134. if (ret == 2)
  135. ret = 0;
  136. } else
  137. ret = hdpvr_config_call(dev, CTRL_AUDIO_INPUT_VALUE, input);
  138. error:
  139. return ret;
  140. }
  141. int hdpvr_set_options(struct hdpvr_device *dev)
  142. {
  143. hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, dev->options.video_std);
  144. hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE,
  145. dev->options.video_input+1);
  146. hdpvr_set_audio(dev, dev->options.audio_input+1,
  147. dev->options.audio_codec);
  148. hdpvr_set_bitrate(dev);
  149. hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
  150. dev->options.bitrate_mode);
  151. hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, dev->options.gop_mode);
  152. hdpvr_config_call(dev, CTRL_BRIGHTNESS, dev->options.brightness);
  153. hdpvr_config_call(dev, CTRL_CONTRAST, dev->options.contrast);
  154. hdpvr_config_call(dev, CTRL_HUE, dev->options.hue);
  155. hdpvr_config_call(dev, CTRL_SATURATION, dev->options.saturation);
  156. hdpvr_config_call(dev, CTRL_SHARPNESS, dev->options.sharpness);
  157. return 0;
  158. }