hdpvr-control.c 4.9 KB

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