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