Merge branch 'for-linus' of git://git.monstr.eu/linux-2.6-microblaze
[linux-2.6] / drivers / media / video / hdpvr / hdpvr-control.c
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         msleep(10);
33
34         mutex_lock(&dev->usbc_mutex);
35         dev->usbc_buf[0] = valbuf;
36         ret = usb_control_msg(dev->udev,
37                               usb_sndctrlpipe(dev->udev, 0),
38                               snd_request, 0x00 | request_type,
39                               value, CTRL_DEFAULT_INDEX,
40                               dev->usbc_buf, 1, 10000);
41
42         mutex_unlock(&dev->usbc_mutex);
43         v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
44                  "config call request for value 0x%x returned %d\n", value,
45                  ret);
46
47         return ret < 0 ? ret : 0;
48 }
49
50 struct hdpvr_video_info *get_video_info(struct hdpvr_device *dev)
51 {
52         struct hdpvr_video_info *vidinf = NULL;
53 #ifdef HDPVR_DEBUG
54         char print_buf[15];
55 #endif
56         int ret;
57
58         vidinf = kzalloc(sizeof(struct hdpvr_video_info), GFP_KERNEL);
59         if (!vidinf) {
60                 v4l2_err(&dev->v4l2_dev, "out of memory\n");
61                 goto err;
62         }
63
64         mutex_lock(&dev->usbc_mutex);
65         ret = usb_control_msg(dev->udev,
66                               usb_rcvctrlpipe(dev->udev, 0),
67                               0x81, 0x80 | 0x38,
68                               0x1400, 0x0003,
69                               dev->usbc_buf, 5,
70                               1000);
71         if (ret == 5) {
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         }
76
77 #ifdef HDPVR_DEBUG
78         if (hdpvr_debug & MSG_INFO) {
79                 hex_dump_to_buffer(dev->usbc_buf, 5, 16, 1, print_buf,
80                                    sizeof(print_buf), 0);
81                 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
82                          "get video info returned: %d, %s\n", ret, print_buf);
83         }
84 #endif
85         mutex_unlock(&dev->usbc_mutex);
86
87         if (!vidinf->width || !vidinf->height || !vidinf->fps) {
88                 kfree(vidinf);
89                 vidinf = NULL;
90         }
91 err:
92         return vidinf;
93 }
94
95 int get_input_lines_info(struct hdpvr_device *dev)
96 {
97 #ifdef HDPVR_DEBUG
98         char print_buf[9];
99 #endif
100         int ret, lines;
101
102         mutex_lock(&dev->usbc_mutex);
103         ret = usb_control_msg(dev->udev,
104                               usb_rcvctrlpipe(dev->udev, 0),
105                               0x81, 0x80 | 0x38,
106                               0x1800, 0x0003,
107                               dev->usbc_buf, 3,
108                               1000);
109
110 #ifdef HDPVR_DEBUG
111         if (hdpvr_debug & MSG_INFO) {
112                 hex_dump_to_buffer(dev->usbc_buf, 3, 16, 1, print_buf,
113                                    sizeof(print_buf), 0);
114                 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
115                          "get input lines info returned: %d, %s\n", ret,
116                          print_buf);
117         }
118 #endif
119         lines = dev->usbc_buf[1] << 8 | dev->usbc_buf[0];
120         mutex_unlock(&dev->usbc_mutex);
121         return lines;
122 }
123
124
125 int hdpvr_set_bitrate(struct hdpvr_device *dev)
126 {
127         int ret;
128
129         mutex_lock(&dev->usbc_mutex);
130         memset(dev->usbc_buf, 0, 4);
131         dev->usbc_buf[0] = dev->options.bitrate;
132         dev->usbc_buf[2] = dev->options.peak_bitrate;
133
134         ret = usb_control_msg(dev->udev,
135                               usb_sndctrlpipe(dev->udev, 0),
136                               0x01, 0x38, CTRL_BITRATE_VALUE,
137                               CTRL_DEFAULT_INDEX, dev->usbc_buf, 4, 1000);
138         mutex_unlock(&dev->usbc_mutex);
139
140         return ret;
141 }
142
143 int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
144                     enum v4l2_mpeg_audio_encoding codec)
145 {
146         int ret = 0;
147
148         if (dev->flags & HDPVR_FLAG_AC3_CAP) {
149                 mutex_lock(&dev->usbc_mutex);
150                 memset(dev->usbc_buf, 0, 2);
151                 dev->usbc_buf[0] = input;
152                 if (codec == V4L2_MPEG_AUDIO_ENCODING_AAC)
153                         dev->usbc_buf[1] = 0;
154                 else if (codec == V4L2_MPEG_AUDIO_ENCODING_AC3)
155                         dev->usbc_buf[1] = 1;
156                 else {
157                         mutex_unlock(&dev->usbc_mutex);
158                         v4l2_err(&dev->v4l2_dev, "invalid audio codec %d\n",
159                                  codec);
160                         ret = -EINVAL;
161                         goto error;
162                 }
163
164                 ret = usb_control_msg(dev->udev,
165                                       usb_sndctrlpipe(dev->udev, 0),
166                                       0x01, 0x38, CTRL_AUDIO_INPUT_VALUE,
167                                       CTRL_DEFAULT_INDEX, dev->usbc_buf, 2,
168                                       1000);
169                 mutex_unlock(&dev->usbc_mutex);
170                 if (ret == 2)
171                         ret = 0;
172         } else
173                 ret = hdpvr_config_call(dev, CTRL_AUDIO_INPUT_VALUE,
174                                         dev->options.audio_input+1);
175 error:
176         return ret;
177 }
178
179 int hdpvr_set_options(struct hdpvr_device *dev)
180 {
181        hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, dev->options.video_std);
182
183        hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE,
184                          dev->options.video_input+1);
185
186        hdpvr_set_audio(dev, dev->options.audio_input+1,
187                        dev->options.audio_codec);
188
189        hdpvr_set_bitrate(dev);
190        hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
191                          dev->options.bitrate_mode);
192        hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, dev->options.gop_mode);
193
194        hdpvr_config_call(dev, CTRL_BRIGHTNESS, dev->options.brightness);
195        hdpvr_config_call(dev, CTRL_CONTRAST,   dev->options.contrast);
196        hdpvr_config_call(dev, CTRL_HUE,        dev->options.hue);
197        hdpvr_config_call(dev, CTRL_SATURATION, dev->options.saturation);
198        hdpvr_config_call(dev, CTRL_SHARPNESS,  dev->options.sharpness);
199
200        return 0;
201 }