xref: /linux/drivers/media/usb/hdpvr/hdpvr-control.c (revision fcc8487d477a3452a1d0ccbdd4c5e0e1e3cb8bed)
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 
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/mutex.h>
19 
20 #include <linux/videodev2.h>
21 
22 #include <media/v4l2-common.h>
23 
24 #include "hdpvr.h"
25 
26 
27 int hdpvr_config_call(struct hdpvr_device *dev, uint value, u8 valbuf)
28 {
29 	int ret;
30 	char request_type = 0x38, snd_request = 0x01;
31 
32 	mutex_lock(&dev->usbc_mutex);
33 	dev->usbc_buf[0] = valbuf;
34 	ret = usb_control_msg(dev->udev,
35 			      usb_sndctrlpipe(dev->udev, 0),
36 			      snd_request, 0x00 | request_type,
37 			      value, CTRL_DEFAULT_INDEX,
38 			      dev->usbc_buf, 1, 10000);
39 
40 	mutex_unlock(&dev->usbc_mutex);
41 	v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
42 		 "config call request for value 0x%x returned %d\n", value,
43 		 ret);
44 
45 	return ret < 0 ? ret : 0;
46 }
47 
48 int get_video_info(struct hdpvr_device *dev, struct hdpvr_video_info *vidinf)
49 {
50 	int ret;
51 
52 	vidinf->valid = false;
53 	mutex_lock(&dev->usbc_mutex);
54 	ret = usb_control_msg(dev->udev,
55 			      usb_rcvctrlpipe(dev->udev, 0),
56 			      0x81, 0x80 | 0x38,
57 			      0x1400, 0x0003,
58 			      dev->usbc_buf, 5,
59 			      1000);
60 
61 #ifdef HDPVR_DEBUG
62 	if (hdpvr_debug & MSG_INFO)
63 		v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
64 			 "get video info returned: %d, %5ph\n", ret,
65 			 dev->usbc_buf);
66 #endif
67 	mutex_unlock(&dev->usbc_mutex);
68 
69 	if (ret < 0)
70 		return ret;
71 
72 	vidinf->width	= dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
73 	vidinf->height	= dev->usbc_buf[3] << 8 | dev->usbc_buf[2];
74 	vidinf->fps	= dev->usbc_buf[4];
75 	vidinf->valid   = vidinf->width && vidinf->height && vidinf->fps;
76 
77 	return 0;
78 }
79 
80 int get_input_lines_info(struct hdpvr_device *dev)
81 {
82 	int ret, lines;
83 
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 
92 #ifdef HDPVR_DEBUG
93 	if (hdpvr_debug & MSG_INFO)
94 		v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
95 			 "get input lines info returned: %d, %3ph\n", ret,
96 			 dev->usbc_buf);
97 #else
98 	(void)ret;	/* suppress compiler warning */
99 #endif
100 	lines = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
101 	mutex_unlock(&dev->usbc_mutex);
102 	return lines;
103 }
104 
105 
106 int hdpvr_set_bitrate(struct hdpvr_device *dev)
107 {
108 	int ret;
109 
110 	mutex_lock(&dev->usbc_mutex);
111 	memset(dev->usbc_buf, 0, 4);
112 	dev->usbc_buf[0] = dev->options.bitrate;
113 	dev->usbc_buf[2] = dev->options.peak_bitrate;
114 
115 	ret = usb_control_msg(dev->udev,
116 			      usb_sndctrlpipe(dev->udev, 0),
117 			      0x01, 0x38, CTRL_BITRATE_VALUE,
118 			      CTRL_DEFAULT_INDEX, dev->usbc_buf, 4, 1000);
119 	mutex_unlock(&dev->usbc_mutex);
120 
121 	return ret;
122 }
123 
124 int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
125 		    enum v4l2_mpeg_audio_encoding codec)
126 {
127 	int ret = 0;
128 
129 	if (dev->flags & HDPVR_FLAG_AC3_CAP) {
130 		mutex_lock(&dev->usbc_mutex);
131 		memset(dev->usbc_buf, 0, 2);
132 		dev->usbc_buf[0] = input;
133 		if (codec == V4L2_MPEG_AUDIO_ENCODING_AAC)
134 			dev->usbc_buf[1] = 0;
135 		else if (codec == V4L2_MPEG_AUDIO_ENCODING_AC3)
136 			dev->usbc_buf[1] = 1;
137 		else {
138 			mutex_unlock(&dev->usbc_mutex);
139 			v4l2_err(&dev->v4l2_dev, "invalid audio codec %d\n",
140 				 codec);
141 			ret = -EINVAL;
142 			goto error;
143 		}
144 
145 		ret = usb_control_msg(dev->udev,
146 				      usb_sndctrlpipe(dev->udev, 0),
147 				      0x01, 0x38, CTRL_AUDIO_INPUT_VALUE,
148 				      CTRL_DEFAULT_INDEX, dev->usbc_buf, 2,
149 				      1000);
150 		mutex_unlock(&dev->usbc_mutex);
151 		if (ret == 2)
152 			ret = 0;
153 	} else
154 		ret = hdpvr_config_call(dev, CTRL_AUDIO_INPUT_VALUE, input);
155 error:
156 	return ret;
157 }
158 
159 int hdpvr_set_options(struct hdpvr_device *dev)
160 {
161 	hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, dev->options.video_std);
162 
163 	hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE,
164 			 dev->options.video_input+1);
165 
166 	hdpvr_set_audio(dev, dev->options.audio_input+1,
167 		       dev->options.audio_codec);
168 
169 	hdpvr_set_bitrate(dev);
170 	hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
171 			 dev->options.bitrate_mode);
172 	hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, dev->options.gop_mode);
173 
174 	hdpvr_config_call(dev, CTRL_BRIGHTNESS, dev->options.brightness);
175 	hdpvr_config_call(dev, CTRL_CONTRAST,   dev->options.contrast);
176 	hdpvr_config_call(dev, CTRL_HUE,        dev->options.hue);
177 	hdpvr_config_call(dev, CTRL_SATURATION, dev->options.saturation);
178 	hdpvr_config_call(dev, CTRL_SHARPNESS,  dev->options.sharpness);
179 
180 	return 0;
181 }
182