aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/hdpvr
diff options
context:
space:
mode:
authorJanne Grunau <j@jannau.net>2009-03-18 17:10:04 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2009-03-30 11:43:28 -0400
commit9aba42efe85bc7a55e3fed0747ce14abc9ee96e7 (patch)
treeee27b9d4844da17b4d4bb80132991f29d04ff265 /drivers/media/video/hdpvr
parent8c84cfda3e37540abc38f42383b03dd69829faee (diff)
V4L/DVB (11096): V4L2 Driver for the Hauppauge HD PVR usb capture device
The device encodes component video up to 1080i to a MPEG-TS stream with H.264 video and stereo AAC audio. Newer firmwares accept also AC3 (up to 5.1) audio over optical SPDIF without reencoding. Firmware upgrade is unimplemeted but rather unimportant since the firmware sits on a flash chip. The I2C adapter to drive the integrated infrared receiver/sender is currently disabled due to a conflict with cx18-based devices. Tested-by: Jarod Wilson <jarod@redhat.com> Signed-off-by: Janne Grunau <j@jannau.net> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/video/hdpvr')
-rw-r--r--drivers/media/video/hdpvr/Kconfig10
-rw-r--r--drivers/media/video/hdpvr/Makefile7
-rw-r--r--drivers/media/video/hdpvr/hdpvr-control.c201
-rw-r--r--drivers/media/video/hdpvr/hdpvr-core.c439
-rw-r--r--drivers/media/video/hdpvr/hdpvr-i2c.c145
-rw-r--r--drivers/media/video/hdpvr/hdpvr-video.c1228
-rw-r--r--drivers/media/video/hdpvr/hdpvr.h298
7 files changed, 2328 insertions, 0 deletions
diff --git a/drivers/media/video/hdpvr/Kconfig b/drivers/media/video/hdpvr/Kconfig
new file mode 100644
index 000000000000..de247f3c7d05
--- /dev/null
+++ b/drivers/media/video/hdpvr/Kconfig
@@ -0,0 +1,10 @@
1
2config VIDEO_HDPVR
3 tristate "Hauppauge HD PVR support"
4 depends on VIDEO_DEV
5 ---help---
6 This is a video4linux driver for Hauppauge's HD PVR USB device.
7
8 To compile this driver as a module, choose M here: the
9 module will be called hdpvr
10
diff --git a/drivers/media/video/hdpvr/Makefile b/drivers/media/video/hdpvr/Makefile
new file mode 100644
index 000000000000..79ad2e16cb8f
--- /dev/null
+++ b/drivers/media/video/hdpvr/Makefile
@@ -0,0 +1,7 @@
1hdpvr-objs := hdpvr-control.o hdpvr-core.o hdpvr-i2c.o hdpvr-video.o
2
3obj-$(CONFIG_VIDEO_HDPVR) += hdpvr.o
4
5EXTRA_CFLAGS += -Idrivers/media/video
6
7EXTRA_CFLAGS += $(extra-cflags-y) $(extra-cflags-m)
diff --git a/drivers/media/video/hdpvr/hdpvr-control.c b/drivers/media/video/hdpvr/hdpvr-control.c
new file mode 100644
index 000000000000..ecf02c621f13
--- /dev/null
+++ b/drivers/media/video/hdpvr/hdpvr-control.c
@@ -0,0 +1,201 @@
1/*
2 * Hauppage 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
27int 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 dev_dbg(&dev->udev->dev,
44 "config call request for value 0x%x returned %d\n", value,
45 ret);
46
47 return ret < 0 ? ret : 0;
48}
49
50struct 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 dev_err(&dev->udev->dev, "out of memory");
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 dev_dbg(&dev->udev->dev, "get video info returned: %d, %s\n",
82 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 }
91err:
92 return vidinf;
93}
94
95int 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 dev_dbg(&dev->udev->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
125int 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
143int 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 dev_err(&dev->udev->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);
175error:
176 return ret;
177}
178
179int 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}
diff --git a/drivers/media/video/hdpvr/hdpvr-core.c b/drivers/media/video/hdpvr/hdpvr-core.c
new file mode 100644
index 000000000000..e7300b570bb7
--- /dev/null
+++ b/drivers/media/video/hdpvr/hdpvr-core.c
@@ -0,0 +1,439 @@
1/*
2 * Hauppage HD PVR USB driver
3 *
4 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
6 * Copyright (C) 2008 John Poet
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/uaccess.h>
20#include <asm/atomic.h>
21#include <linux/usb.h>
22#include <linux/mutex.h>
23#include <linux/i2c.h>
24
25#include <linux/videodev2.h>
26#include <media/v4l2-dev.h>
27#include <media/v4l2-common.h>
28
29#include "hdpvr.h"
30
31static int video_nr[HDPVR_MAX] = {[0 ... (HDPVR_MAX - 1)] = UNSET};
32module_param_array(video_nr, int, NULL, 0);
33MODULE_PARM_DESC(video_nr, "video device number (-1=Auto)");
34
35/* holds the number of currently registered devices */
36static atomic_t dev_nr = ATOMIC_INIT(-1);
37
38int hdpvr_debug;
39module_param(hdpvr_debug, int, S_IRUGO|S_IWUSR);
40MODULE_PARM_DESC(hdpvr_debug, "enable debugging output");
41
42uint default_video_input = HDPVR_VIDEO_INPUTS;
43module_param(default_video_input, uint, S_IRUGO|S_IWUSR);
44MODULE_PARM_DESC(default_video_input, "default video input: 0=Component / "
45 "1=S-Video / 2=Composite");
46
47uint default_audio_input = HDPVR_AUDIO_INPUTS;
48module_param(default_audio_input, uint, S_IRUGO|S_IWUSR);
49MODULE_PARM_DESC(default_audio_input, "default audio input: 0=RCA back / "
50 "1=RCA front / 2=S/PDIF");
51
52static int boost_audio;
53module_param(boost_audio, bool, S_IRUGO|S_IWUSR);
54MODULE_PARM_DESC(boost_audio, "boost the audio signal");
55
56
57/* table of devices that work with this driver */
58static struct usb_device_id hdpvr_table[] = {
59 { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID) },
60 { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID1) },
61 { USB_DEVICE(HD_PVR_VENDOR_ID, HD_PVR_PRODUCT_ID2) },
62 { } /* Terminating entry */
63};
64MODULE_DEVICE_TABLE(usb, hdpvr_table);
65
66
67void hdpvr_delete(struct hdpvr_device *dev)
68{
69 hdpvr_free_buffers(dev);
70
71 if (dev->video_dev)
72 video_device_release(dev->video_dev);
73
74 usb_put_dev(dev->udev);
75}
76
77static void challenge(u8 *bytes)
78{
79 u64 *i64P, tmp64;
80 uint i, idx;
81
82 for (idx = 0; idx < 32; ++idx) {
83
84 if (idx & 0x3)
85 bytes[(idx >> 3) + 3] = bytes[(idx >> 2) & 0x3];
86
87 switch (idx & 0x3) {
88 case 0x3:
89 bytes[2] += bytes[3] * 4 + bytes[4] + bytes[5];
90 bytes[4] += bytes[(idx & 0x1) * 2] * 9 + 9;
91 break;
92 case 0x1:
93 bytes[0] *= 8;
94 bytes[0] += 7*idx + 4;
95 bytes[6] += bytes[3] * 3;
96 break;
97 case 0x0:
98 bytes[3 - (idx >> 3)] = bytes[idx >> 2];
99 bytes[5] += bytes[6] * 3;
100 for (i = 0; i < 3; i++)
101 bytes[3] *= bytes[3] + 1;
102 break;
103 case 0x2:
104 for (i = 0; i < 3; i++)
105 bytes[1] *= bytes[6] + 1;
106 for (i = 0; i < 3; i++) {
107 i64P = (u64 *)bytes;
108 tmp64 = le64_to_cpup(i64P);
109 tmp64 <<= bytes[7] & 0x0f;
110 *i64P += cpu_to_le64(tmp64);
111 }
112 break;
113 }
114 }
115}
116
117/* try to init the device like the windows driver */
118static int device_authorization(struct hdpvr_device *dev)
119{
120
121 int ret, retval = -ENOMEM;
122 char request_type = 0x38, rcv_request = 0x81;
123 char *response;
124#ifdef HDPVR_DEBUG
125 size_t buf_size = 46;
126 char *print_buf = kzalloc(5*buf_size+1, GFP_KERNEL);
127 if (!print_buf) {
128 dev_err(&dev->udev->dev, "Out of memory");
129 goto error;
130 }
131#endif
132
133 mutex_lock(&dev->usbc_mutex);
134 ret = usb_control_msg(dev->udev,
135 usb_rcvctrlpipe(dev->udev, 0),
136 rcv_request, 0x80 | request_type,
137 0x0400, 0x0003,
138 dev->usbc_buf, 46,
139 10000);
140 if (ret != 46) {
141 dev_err(&dev->udev->dev,
142 "unexpected answer of status request, len %d", ret);
143 goto error;
144 }
145#ifdef HDPVR_DEBUG
146 else {
147 hex_dump_to_buffer(dev->usbc_buf, 46, 16, 1, print_buf,
148 sizeof(print_buf), 0);
149 dev_dbg(&dev->udev->dev,
150 "Status request returned, len %d: %s\n",
151 ret, print_buf);
152 }
153#endif
154 if (dev->usbc_buf[1] == HDPVR_FIRMWARE_VERSION) {
155 dev->flags &= ~HDPVR_FLAG_AC3_CAP;
156 } else if (dev->usbc_buf[1] == HDPVR_FIRMWARE_VERSION_AC3) {
157 dev->flags |= HDPVR_FLAG_AC3_CAP;
158 } else if (dev->usbc_buf[1] > HDPVR_FIRMWARE_VERSION_AC3) {
159 dev_notice(&dev->udev->dev, "untested firmware version 0x%x, "
160 "the driver might not work\n", dev->usbc_buf[1]);
161 dev->flags |= HDPVR_FLAG_AC3_CAP;
162 } else {
163 dev_err(&dev->udev->dev, "unknown firmware version 0x%x\n",
164 dev->usbc_buf[1]);
165 ret = -EINVAL;
166 goto error;
167 }
168
169 response = dev->usbc_buf+38;
170#ifdef HDPVR_DEBUG
171 hex_dump_to_buffer(response, 8, 16, 1, print_buf, sizeof(print_buf), 0);
172 dev_dbg(&dev->udev->dev, "challenge: %s\n", print_buf);
173#endif
174 challenge(response);
175#ifdef HDPVR_DEBUG
176 hex_dump_to_buffer(response, 8, 16, 1, print_buf, sizeof(print_buf), 0);
177 dev_dbg(&dev->udev->dev, " response: %s\n", print_buf);
178#endif
179
180 msleep(100);
181 ret = usb_control_msg(dev->udev,
182 usb_sndctrlpipe(dev->udev, 0),
183 0xd1, 0x00 | request_type,
184 0x0000, 0x0000,
185 response, 8,
186 10000);
187 dev_dbg(&dev->udev->dev, "magic request returned %d\n", ret);
188 mutex_unlock(&dev->usbc_mutex);
189
190 retval = ret != 8;
191error:
192 return retval;
193}
194
195static int hdpvr_device_init(struct hdpvr_device *dev)
196{
197 int ret;
198 u8 *buf;
199 struct hdpvr_video_info *vidinf;
200
201 if (device_authorization(dev))
202 return -EACCES;
203
204 /* default options for init */
205 hdpvr_set_options(dev);
206
207 /* set filter options */
208 mutex_lock(&dev->usbc_mutex);
209 buf = dev->usbc_buf;
210 buf[0] = 0x03; buf[1] = 0x03; buf[2] = 0x00; buf[3] = 0x00;
211 ret = usb_control_msg(dev->udev,
212 usb_sndctrlpipe(dev->udev, 0),
213 0x01, 0x38,
214 CTRL_LOW_PASS_FILTER_VALUE, CTRL_DEFAULT_INDEX,
215 buf, 4,
216 1000);
217 dev_dbg(&dev->udev->dev, "control request returned %d\n", ret);
218 mutex_unlock(&dev->usbc_mutex);
219
220 vidinf = get_video_info(dev);
221 if (!vidinf)
222 dev_dbg(&dev->udev->dev,
223 "no valid video signal or device init failed\n");
224 else
225 kfree(vidinf);
226
227 /* enable fan and bling leds */
228 mutex_lock(&dev->usbc_mutex);
229 buf[0] = 0x1;
230 ret = usb_control_msg(dev->udev,
231 usb_sndctrlpipe(dev->udev, 0),
232 0xd4, 0x38, 0, 0, buf, 1,
233 1000);
234 dev_dbg(&dev->udev->dev, "control request returned %d\n", ret);
235
236 /* boost analog audio */
237 buf[0] = boost_audio;
238 ret = usb_control_msg(dev->udev,
239 usb_sndctrlpipe(dev->udev, 0),
240 0xd5, 0x38, 0, 0, buf, 1,
241 1000);
242 dev_dbg(&dev->udev->dev, "control request returned %d\n", ret);
243 mutex_unlock(&dev->usbc_mutex);
244
245 dev->status = STATUS_IDLE;
246 return 0;
247}
248
249static const struct hdpvr_options hdpvr_default_options = {
250 .video_std = HDPVR_60HZ,
251 .video_input = HDPVR_COMPONENT,
252 .audio_input = HDPVR_RCA_BACK,
253 .bitrate = 65, /* 6 mbps */
254 .peak_bitrate = 90, /* 9 mbps */
255 .bitrate_mode = HDPVR_CONSTANT,
256 .gop_mode = HDPVR_SIMPLE_IDR_GOP,
257 .audio_codec = V4L2_MPEG_AUDIO_ENCODING_AAC,
258 .brightness = 0x86,
259 .contrast = 0x80,
260 .hue = 0x80,
261 .saturation = 0x80,
262 .sharpness = 0x80,
263};
264
265static int hdpvr_probe(struct usb_interface *interface,
266 const struct usb_device_id *id)
267{
268 struct hdpvr_device *dev;
269 struct usb_host_interface *iface_desc;
270 struct usb_endpoint_descriptor *endpoint;
271 size_t buffer_size;
272 int i;
273 int retval = -ENOMEM;
274
275 /* allocate memory for our device state and initialize it */
276 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
277 if (!dev) {
278 err("Out of memory");
279 goto error;
280 }
281 mutex_init(&dev->io_mutex);
282 mutex_init(&dev->i2c_mutex);
283 mutex_init(&dev->usbc_mutex);
284 dev->usbc_buf = kmalloc(64, GFP_KERNEL);
285 if (!dev->usbc_buf) {
286 dev_err(&dev->udev->dev, "Out of memory");
287 goto error;
288 }
289
290 init_waitqueue_head(&dev->wait_buffer);
291 init_waitqueue_head(&dev->wait_data);
292
293 dev->workqueue = create_singlethread_workqueue("hdpvr_buffer");
294 if (!dev->workqueue)
295 goto error;
296
297 /* init video transfer queues */
298 INIT_LIST_HEAD(&dev->free_buff_list);
299 INIT_LIST_HEAD(&dev->rec_buff_list);
300
301 dev->options = hdpvr_default_options;
302
303 if (default_video_input < HDPVR_VIDEO_INPUTS)
304 dev->options.video_input = default_video_input;
305
306 if (default_audio_input < HDPVR_AUDIO_INPUTS)
307 dev->options.audio_input = default_audio_input;
308
309 dev->udev = usb_get_dev(interface_to_usbdev(interface));
310
311 /* set up the endpoint information */
312 /* use only the first bulk-in and bulk-out endpoints */
313 iface_desc = interface->cur_altsetting;
314 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
315 endpoint = &iface_desc->endpoint[i].desc;
316
317 if (!dev->bulk_in_endpointAddr &&
318 usb_endpoint_is_bulk_in(endpoint)) {
319 /* USB interface description is buggy, reported max
320 * packet size is 512 bytes, windows driver uses 8192 */
321 buffer_size = 8192;
322 dev->bulk_in_size = buffer_size;
323 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
324 }
325
326 }
327 if (!dev->bulk_in_endpointAddr) {
328 err("Could not find bulk-in endpoint");
329 goto error;
330 }
331
332 /* init the device */
333 if (hdpvr_device_init(dev)) {
334 err("device init failed");
335 goto error;
336 }
337
338 mutex_lock(&dev->io_mutex);
339 if (hdpvr_alloc_buffers(dev, NUM_BUFFERS)) {
340 err("allocating transfer buffers failed");
341 goto error;
342 }
343 mutex_unlock(&dev->io_mutex);
344
345 if (hdpvr_register_videodev(dev,
346 video_nr[atomic_inc_return(&dev_nr)])) {
347 err("registering videodev failed");
348 goto error;
349 }
350
351
352 /* save our data pointer in this interface device */
353 usb_set_intfdata(interface, dev);
354
355 /* let the user know what node this device is now attached to */
356 v4l2_info(dev->video_dev, "device now attached to /dev/video%d\n",
357 dev->video_dev->minor);
358 return 0;
359
360error:
361 if (dev) {
362 mutex_unlock(&dev->io_mutex);
363 /* this frees allocated memory */
364 hdpvr_delete(dev);
365 }
366 return retval;
367}
368
369static void hdpvr_disconnect(struct usb_interface *interface)
370{
371 struct hdpvr_device *dev;
372 int minor;
373
374 dev = usb_get_intfdata(interface);
375 usb_set_intfdata(interface, NULL);
376
377 minor = dev->video_dev->minor;
378
379 /* prevent more I/O from starting and stop any ongoing */
380 mutex_lock(&dev->io_mutex);
381 dev->status = STATUS_DISCONNECTED;
382 video_unregister_device(dev->video_dev);
383 wake_up_interruptible(&dev->wait_data);
384 wake_up_interruptible(&dev->wait_buffer);
385 msleep(100);
386 flush_workqueue(dev->workqueue);
387 hdpvr_cancel_queue(dev);
388 destroy_workqueue(dev->workqueue);
389 mutex_unlock(&dev->io_mutex);
390
391 /* deregister I2C adapter */
392 mutex_lock(&dev->i2c_mutex);
393 if (dev->i2c_adapter)
394 i2c_del_adapter(dev->i2c_adapter);
395 kfree(dev->i2c_adapter);
396 dev->i2c_adapter = NULL;
397 mutex_unlock(&dev->i2c_mutex);
398
399 atomic_dec(&dev_nr);
400
401 printk(KERN_INFO "Hauppauge HD PVR: device /dev/video%d disconnected\n",
402 minor);
403
404 kfree(dev->usbc_buf);
405 kfree(dev);
406}
407
408
409static struct usb_driver hdpvr_usb_driver = {
410 .name = "hdpvr",
411 .probe = hdpvr_probe,
412 .disconnect = hdpvr_disconnect,
413 .id_table = hdpvr_table,
414};
415
416static int __init hdpvr_init(void)
417{
418 int result;
419
420 /* register this driver with the USB subsystem */
421 result = usb_register(&hdpvr_usb_driver);
422 if (result)
423 err("usb_register failed. Error number %d", result);
424
425 return result;
426}
427
428static void __exit hdpvr_exit(void)
429{
430 /* deregister this driver with the USB subsystem */
431 usb_deregister(&hdpvr_usb_driver);
432}
433
434module_init(hdpvr_init);
435module_exit(hdpvr_exit);
436
437MODULE_LICENSE("GPL");
438MODULE_AUTHOR("Janne Grunau");
439MODULE_DESCRIPTION("Hauppauge HD PVR driver");
diff --git a/drivers/media/video/hdpvr/hdpvr-i2c.c b/drivers/media/video/hdpvr/hdpvr-i2c.c
new file mode 100644
index 000000000000..35096dec2411
--- /dev/null
+++ b/drivers/media/video/hdpvr/hdpvr-i2c.c
@@ -0,0 +1,145 @@
1
2/*
3 * Hauppage HD PVR USB driver
4 *
5 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
10 *
11 */
12
13#include <linux/i2c.h>
14
15#include "hdpvr.h"
16
17#define CTRL_READ_REQUEST 0xb8
18#define CTRL_WRITE_REQUEST 0x38
19
20#define REQTYPE_I2C_READ 0xb1
21#define REQTYPE_I2C_WRITE 0xb0
22#define REQTYPE_I2C_WRITE_STATT 0xd0
23
24static int hdpvr_i2c_read(struct hdpvr_device *dev, unsigned char addr,
25 char *data, int len)
26{
27 int ret;
28 char *buf = kmalloc(len, GFP_KERNEL);
29 if (!buf)
30 return -ENOMEM;
31
32 ret = usb_control_msg(dev->udev,
33 usb_rcvctrlpipe(dev->udev, 0),
34 REQTYPE_I2C_READ, CTRL_READ_REQUEST,
35 0x100|addr, 0, buf, len, 1000);
36
37 if (ret == len) {
38 memcpy(data, buf, len);
39 ret = 0;
40 } else if (ret >= 0)
41 ret = -EIO;
42
43 kfree(buf);
44
45 return ret;
46}
47
48static int hdpvr_i2c_write(struct hdpvr_device *dev, unsigned char addr,
49 char *data, int len)
50{
51 int ret;
52 char *buf = kmalloc(len, GFP_KERNEL);
53 if (!buf)
54 return -ENOMEM;
55
56 memcpy(buf, data, len);
57 ret = usb_control_msg(dev->udev,
58 usb_sndctrlpipe(dev->udev, 0),
59 REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
60 0x100|addr, 0, buf, len, 1000);
61
62 if (ret < 0)
63 goto error;
64
65 ret = usb_control_msg(dev->udev,
66 usb_rcvctrlpipe(dev->udev, 0),
67 REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
68 0, 0, buf, 2, 1000);
69
70 if (ret == 2)
71 ret = 0;
72 else if (ret >= 0)
73 ret = -EIO;
74
75error:
76 kfree(buf);
77 return ret;
78}
79
80static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
81 int num)
82{
83 struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
84 int retval = 0, i, addr;
85
86 if (num <= 0)
87 return 0;
88
89 mutex_lock(&dev->i2c_mutex);
90
91 for (i = 0; i < num && !retval; i++) {
92 addr = msgs[i].addr << 1;
93
94 if (msgs[i].flags & I2C_M_RD)
95 retval = hdpvr_i2c_read(dev, addr, msgs[i].buf,
96 msgs[i].len);
97 else
98 retval = hdpvr_i2c_write(dev, addr, msgs[i].buf,
99 msgs[i].len);
100 }
101
102 mutex_unlock(&dev->i2c_mutex);
103
104 return retval ? retval : num;
105}
106
107static u32 hdpvr_functionality(struct i2c_adapter *adapter)
108{
109 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
110}
111
112static struct i2c_algorithm hdpvr_algo = {
113 .master_xfer = hdpvr_transfer,
114 .functionality = hdpvr_functionality,
115};
116
117int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
118{
119 struct i2c_adapter *i2c_adap;
120 int retval = -ENOMEM;
121
122 i2c_adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
123 if (i2c_adap == NULL)
124 goto error;
125
126 strlcpy(i2c_adap->name, "Hauppauge HD PVR I2C",
127 sizeof(i2c_adap->name));
128 i2c_adap->algo = &hdpvr_algo;
129 i2c_adap->class = I2C_CLASS_TV_ANALOG;
130 i2c_adap->id = I2C_HW_B_HDPVR;
131 i2c_adap->owner = THIS_MODULE;
132 i2c_adap->dev.parent = &dev->udev->dev;
133
134 i2c_set_adapdata(i2c_adap, dev);
135
136 retval = i2c_add_adapter(i2c_adap);
137
138 if (!retval)
139 dev->i2c_adapter = i2c_adap;
140 else
141 kfree(i2c_adap);
142
143error:
144 return retval;
145}
diff --git a/drivers/media/video/hdpvr/hdpvr-video.c b/drivers/media/video/hdpvr/hdpvr-video.c
new file mode 100644
index 000000000000..ee481495e4fc
--- /dev/null
+++ b/drivers/media/video/hdpvr/hdpvr-video.c
@@ -0,0 +1,1228 @@
1/*
2 * Hauppage 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/uaccess.h>
18#include <linux/usb.h>
19#include <linux/mutex.h>
20#include <linux/version.h>
21#include <linux/workqueue.h>
22
23#include <linux/videodev2.h>
24#include <media/v4l2-dev.h>
25#include <media/v4l2-common.h>
26#include <media/v4l2-ioctl.h>
27#include "hdpvr.h"
28
29#define BULK_URB_TIMEOUT 1250 /* 1.25 seconds */
30
31struct hdpvr_fh {
32 struct hdpvr_device *dev;
33};
34
35static uint list_size(struct list_head *list)
36{
37 struct list_head *tmp;
38 uint count = 0;
39
40 list_for_each(tmp, list) {
41 count++;
42 }
43
44 return count;
45}
46
47/*=========================================================================*/
48/* urb callback */
49static void hdpvr_read_bulk_callback(struct urb *urb)
50{
51 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
52 struct hdpvr_device *dev = buf->dev;
53
54 /* marking buffer as received and wake waiting */
55 buf->status = BUFSTAT_READY;
56 wake_up_interruptible(&dev->wait_data);
57}
58
59/*=========================================================================*/
60/* bufffer bits */
61
62/* function expects dev->io_mutex to be hold by caller */
63int hdpvr_cancel_queue(struct hdpvr_device *dev)
64{
65 struct hdpvr_buffer *buf;
66
67 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
68 usb_kill_urb(buf->urb);
69 buf->status = BUFSTAT_AVAILABLE;
70 }
71
72 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
73
74 return 0;
75}
76
77static int hdpvr_free_queue(struct list_head *q)
78{
79 struct list_head *tmp;
80 struct list_head *p;
81 struct hdpvr_buffer *buf;
82 struct urb *urb;
83
84 for (p = q->next; p != q;) {
85 buf = list_entry(p, struct hdpvr_buffer, buff_list);
86
87 urb = buf->urb;
88 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
89 urb->transfer_buffer, urb->transfer_dma);
90 usb_free_urb(urb);
91 tmp = p->next;
92 list_del(p);
93 kfree(buf);
94 p = tmp;
95 }
96
97 return 0;
98}
99
100/* function expects dev->io_mutex to be hold by caller */
101int hdpvr_free_buffers(struct hdpvr_device *dev)
102{
103 hdpvr_cancel_queue(dev);
104
105 hdpvr_free_queue(&dev->free_buff_list);
106 hdpvr_free_queue(&dev->rec_buff_list);
107
108 return 0;
109}
110
111/* function expects dev->io_mutex to be hold by caller */
112int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
113{
114 uint i;
115 int retval = -ENOMEM;
116 u8 *mem;
117 struct hdpvr_buffer *buf;
118 struct urb *urb;
119
120 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
121 "allocating %u buffers\n", count);
122
123 for (i = 0; i < count; i++) {
124
125 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
126 if (!buf) {
127 err("cannot allocate buffer");
128 goto exit;
129 }
130 buf->dev = dev;
131
132 urb = usb_alloc_urb(0, GFP_KERNEL);
133 if (!urb) {
134 err("cannot allocate urb");
135 goto exit;
136 }
137 buf->urb = urb;
138
139 mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
140 &urb->transfer_dma);
141 if (!mem) {
142 err("cannot allocate usb transfer buffer");
143 goto exit;
144 }
145
146 usb_fill_bulk_urb(buf->urb, dev->udev,
147 usb_rcvbulkpipe(dev->udev,
148 dev->bulk_in_endpointAddr),
149 mem, dev->bulk_in_size,
150 hdpvr_read_bulk_callback, buf);
151
152 buf->status = BUFSTAT_AVAILABLE;
153 list_add_tail(&buf->buff_list, &dev->free_buff_list);
154 }
155 return 0;
156exit:
157 hdpvr_free_buffers(dev);
158 return retval;
159}
160
161static int hdpvr_submit_buffers(struct hdpvr_device *dev)
162{
163 struct hdpvr_buffer *buf;
164 struct urb *urb;
165 int ret = 0, err_count = 0;
166
167 mutex_lock(&dev->io_mutex);
168
169 while (dev->status == STATUS_STREAMING &&
170 !list_empty(&dev->free_buff_list)) {
171
172 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
173 buff_list);
174 if (buf->status != BUFSTAT_AVAILABLE) {
175 err("buffer not marked as availbale");
176 ret = -EFAULT;
177 goto err;
178 }
179
180 urb = buf->urb;
181 urb->status = 0;
182 urb->actual_length = 0;
183 ret = usb_submit_urb(urb, GFP_KERNEL);
184 if (ret) {
185 err("usb_submit_urb in %s returned %d", __func__, ret);
186 if (++err_count > 2)
187 break;
188 continue;
189 }
190 buf->status = BUFSTAT_INPROGRESS;
191 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
192 }
193err:
194 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
195 "buffer queue stat: %d free, %d proc\n",
196 list_size(&dev->free_buff_list),
197 list_size(&dev->rec_buff_list));
198 mutex_unlock(&dev->io_mutex);
199 return ret;
200}
201
202static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
203{
204 struct hdpvr_buffer *buf;
205
206 mutex_lock(&dev->io_mutex);
207
208 if (list_empty(&dev->rec_buff_list)) {
209 mutex_unlock(&dev->io_mutex);
210 return NULL;
211 }
212
213 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
214 buff_list);
215 mutex_unlock(&dev->io_mutex);
216
217 return buf;
218}
219
220static void hdpvr_transmit_buffers(struct work_struct *work)
221{
222 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
223 worker);
224
225 while (dev->status == STATUS_STREAMING) {
226
227 if (hdpvr_submit_buffers(dev)) {
228 v4l2_err(dev->video_dev, "couldn't submit buffers\n");
229 goto error;
230 }
231 if (wait_event_interruptible(dev->wait_buffer,
232 !list_empty(&dev->free_buff_list) ||
233 dev->status != STATUS_STREAMING))
234 goto error;
235 }
236
237 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
238 "transmit worker exited\n");
239 return;
240error:
241 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
242 "transmit buffers errored\n");
243 dev->status = STATUS_ERROR;
244}
245
246/* function expects dev->io_mutex to be hold by caller */
247static int hdpvr_start_streaming(struct hdpvr_device *dev)
248{
249 int ret;
250 struct hdpvr_video_info *vidinf;
251
252 if (dev->status == STATUS_STREAMING)
253 return 0;
254 else if (dev->status != STATUS_IDLE)
255 return -EAGAIN;
256
257 vidinf = get_video_info(dev);
258
259 if (vidinf) {
260 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
261 "video signal: %dx%d@%dhz\n", vidinf->width,
262 vidinf->height, vidinf->fps);
263 kfree(vidinf);
264
265 /* start streaming 2 request */
266 ret = usb_control_msg(dev->udev,
267 usb_sndctrlpipe(dev->udev, 0),
268 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
269 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
270 "encoder start control request returned %d\n", ret);
271
272 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
273
274 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
275 queue_work(dev->workqueue, &dev->worker);
276
277 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
278 "streaming started\n");
279 dev->status = STATUS_STREAMING;
280
281 return 0;
282 }
283 msleep(250);
284 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
285 "no video signal at input %d\n", dev->options.video_input);
286 return -EAGAIN;
287}
288
289
290/* function expects dev->io_mutex to be hold by caller */
291static int hdpvr_stop_streaming(struct hdpvr_device *dev)
292{
293 if (dev->status == STATUS_IDLE)
294 return 0;
295 else if (dev->status != STATUS_STREAMING)
296 return -EAGAIN;
297
298 dev->status = STATUS_SHUTTING_DOWN;
299 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
300
301 wake_up_interruptible(&dev->wait_buffer);
302 msleep(50);
303
304 flush_workqueue(dev->workqueue);
305
306 /* kill the still outstanding urbs */
307 hdpvr_cancel_queue(dev);
308
309 dev->status = STATUS_IDLE;
310
311 return 0;
312}
313
314
315/*=======================================================================*/
316/*
317 * video 4 linux 2 file operations
318 */
319
320static int hdpvr_open(struct file *file)
321{
322 struct hdpvr_device *dev;
323 struct hdpvr_fh *fh;
324 int retval = -ENOMEM;
325
326 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
327 if (!dev) {
328 err("open failing with with ENODEV");
329 retval = -ENODEV;
330 goto err;
331 }
332
333 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
334 if (!fh) {
335 err("Out of memory?");
336 goto err;
337 }
338 /* lock the device to allow correctly handling errors
339 * in resumption */
340 mutex_lock(&dev->io_mutex);
341 dev->open_count++;
342
343 fh->dev = dev;
344
345 /* save our object in the file's private structure */
346 file->private_data = fh;
347
348 retval = 0;
349err:
350 mutex_unlock(&dev->io_mutex);
351 return retval;
352}
353
354static int hdpvr_release(struct file *file)
355{
356 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
357 struct hdpvr_device *dev = fh->dev;
358
359 if (!dev)
360 return -ENODEV;
361
362 mutex_lock(&dev->io_mutex);
363 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
364 hdpvr_stop_streaming(dev);
365
366 mutex_unlock(&dev->io_mutex);
367
368 return 0;
369}
370
371/*
372 * hdpvr_v4l2_read()
373 * will allocate buffers when called for the first time
374 */
375static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
376 loff_t *pos)
377{
378 struct hdpvr_fh *fh = file->private_data;
379 struct hdpvr_device *dev = fh->dev;
380 struct hdpvr_buffer *buf = NULL;
381 struct urb *urb;
382 unsigned int ret = 0;
383 int rem, cnt;
384
385 if (*pos)
386 return -ESPIPE;
387
388 if (!dev)
389 return -ENODEV;
390
391 mutex_lock(&dev->io_mutex);
392 if (dev->status == STATUS_IDLE) {
393 if (hdpvr_start_streaming(dev)) {
394 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
395 "start_streaming failed");
396 ret = -EIO;
397 msleep(200);
398 dev->status = STATUS_IDLE;
399 mutex_unlock(&dev->io_mutex);
400 goto err;
401 }
402
403 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
404 "buffer queue stat: %d free, %d proc\n",
405 list_size(&dev->free_buff_list),
406 list_size(&dev->rec_buff_list));
407 }
408 mutex_unlock(&dev->io_mutex);
409
410 /* wait for the first buffer */
411 if (!(file->f_flags & O_NONBLOCK)) {
412 if (wait_event_interruptible(dev->wait_data,
413 hdpvr_get_next_buffer(dev)))
414 return -ERESTARTSYS;
415 }
416
417 buf = hdpvr_get_next_buffer(dev);
418
419 while (count > 0 && buf) {
420
421 if (buf->status != BUFSTAT_READY &&
422 dev->status != STATUS_DISCONNECTED) {
423 /* return nonblocking */
424 if (file->f_flags & O_NONBLOCK) {
425 if (!ret)
426 ret = -EAGAIN;
427 goto err;
428 }
429
430 if (wait_event_interruptible(dev->wait_data,
431 buf->status == BUFSTAT_READY)) {
432 ret = -ERESTARTSYS;
433 goto err;
434 }
435 }
436
437 if (buf->status != BUFSTAT_READY)
438 break;
439
440 /* set remaining bytes to copy */
441 urb = buf->urb;
442 rem = urb->actual_length - buf->pos;
443 cnt = rem > count ? count : rem;
444
445 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
446 cnt)) {
447 err("read: copy_to_user failed");
448 if (!ret)
449 ret = -EFAULT;
450 goto err;
451 }
452
453 buf->pos += cnt;
454 count -= cnt;
455 buffer += cnt;
456 ret += cnt;
457
458 /* finished, take next buffer */
459 if (buf->pos == urb->actual_length) {
460 mutex_lock(&dev->io_mutex);
461 buf->pos = 0;
462 buf->status = BUFSTAT_AVAILABLE;
463
464 list_move_tail(&buf->buff_list, &dev->free_buff_list);
465
466 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
467 "buffer queue stat: %d free, %d proc\n",
468 list_size(&dev->free_buff_list),
469 list_size(&dev->rec_buff_list));
470
471 mutex_unlock(&dev->io_mutex);
472
473 wake_up_interruptible(&dev->wait_buffer);
474
475 buf = hdpvr_get_next_buffer(dev);
476 }
477 }
478err:
479 if (!ret && !buf)
480 ret = -EAGAIN;
481 return ret;
482}
483
484static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
485{
486 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
487 struct hdpvr_device *dev = fh->dev;
488 unsigned int mask = 0;
489
490 mutex_lock(&dev->io_mutex);
491
492 if (video_is_unregistered(dev->video_dev))
493 return -EIO;
494
495 if (dev->status == STATUS_IDLE) {
496 if (hdpvr_start_streaming(dev)) {
497 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
498 "start_streaming failed");
499 dev->status = STATUS_IDLE;
500 }
501
502 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
503 "buffer queue stat: %d free, %d proc\n",
504 list_size(&dev->free_buff_list),
505 list_size(&dev->rec_buff_list));
506 }
507 mutex_unlock(&dev->io_mutex);
508
509 poll_wait(filp, &dev->wait_data, wait);
510
511 mutex_lock(&dev->io_mutex);
512 if (!list_empty(&dev->rec_buff_list)) {
513
514 struct hdpvr_buffer *buf = list_entry(dev->rec_buff_list.next,
515 struct hdpvr_buffer,
516 buff_list);
517
518 if (buf->status == BUFSTAT_READY)
519 mask |= POLLIN | POLLRDNORM;
520 }
521 mutex_unlock(&dev->io_mutex);
522
523 return mask;
524}
525
526
527static long hdpvr_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
528{
529 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
530 struct hdpvr_device *dev = fh->dev;
531 int res;
532
533 if (video_is_unregistered(dev->video_dev))
534 return -EIO;
535
536 mutex_lock(&dev->io_mutex);
537 switch (cmd) {
538 case VIDIOC_TRY_ENCODER_CMD:
539 case VIDIOC_ENCODER_CMD: {
540 struct v4l2_encoder_cmd *enc = (struct v4l2_encoder_cmd *)arg;
541 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
542
543 memset(&enc->raw, 0, sizeof(enc->raw));
544 switch (enc->cmd) {
545 case V4L2_ENC_CMD_START:
546 enc->flags = 0;
547 if (try)
548 return 0;
549 res = hdpvr_start_streaming(dev);
550 break;
551 case V4L2_ENC_CMD_STOP:
552 if (try)
553 return 0;
554 res = hdpvr_stop_streaming(dev);
555 break;
556 default:
557 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
558 "Unsupported encoder cmd %d\n", enc->cmd);
559 return -EINVAL;
560 }
561 break;
562 }
563 default:
564 res = video_ioctl2(filp, cmd, arg);
565 }
566 mutex_unlock(&dev->io_mutex);
567 return res;
568}
569
570static const struct v4l2_file_operations hdpvr_fops = {
571 .owner = THIS_MODULE,
572 .open = hdpvr_open,
573 .release = hdpvr_release,
574 .read = hdpvr_read,
575 .poll = hdpvr_poll,
576 .unlocked_ioctl = hdpvr_ioctl,
577};
578
579/*=======================================================================*/
580/*
581 * V4L2 ioctl handling
582 */
583
584static int vidioc_querycap(struct file *file, void *priv,
585 struct v4l2_capability *cap)
586{
587 struct hdpvr_device *dev = video_drvdata(file);
588
589 strcpy(cap->driver, "hdpvr");
590 strcpy(cap->card, "Haupauge HD PVR");
591 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
592 cap->version = HDPVR_VERSION;
593 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
594 V4L2_CAP_AUDIO |
595 V4L2_CAP_READWRITE;
596 return 0;
597}
598
599static int vidioc_s_std(struct file *file, void *private_data,
600 v4l2_std_id *std)
601{
602 struct hdpvr_fh *fh = file->private_data;
603 struct hdpvr_device *dev = fh->dev;
604 u8 std_type = 1;
605
606 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
607 std_type = 0;
608
609 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
610}
611
612static const char *iname[] = {
613 [HDPVR_COMPONENT] = "Component",
614 [HDPVR_SVIDEO] = "S-Video",
615 [HDPVR_COMPOSITE] = "Composite",
616};
617
618static int vidioc_enum_input(struct file *file, void *priv,
619 struct v4l2_input *i)
620{
621 struct hdpvr_fh *fh = file->private_data;
622 struct hdpvr_device *dev = fh->dev;
623 unsigned int n;
624
625 n = i->index;
626 if (n >= HDPVR_VIDEO_INPUTS)
627 return -EINVAL;
628
629 i->type = V4L2_INPUT_TYPE_CAMERA;
630
631 strncpy(i->name, iname[n], sizeof(i->name) - 1);
632 i->name[sizeof(i->name) - 1] = '\0';
633
634 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
635
636 i->std = dev->video_dev->tvnorms;
637
638 return 0;
639}
640
641static int vidioc_s_input(struct file *file, void *private_data,
642 unsigned int index)
643{
644 struct hdpvr_fh *fh = file->private_data;
645 struct hdpvr_device *dev = fh->dev;
646 int retval;
647
648 if (index >= HDPVR_VIDEO_INPUTS)
649 return -EINVAL;
650
651 if (dev->status != STATUS_IDLE)
652 return -EAGAIN;
653
654 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
655 if (!retval)
656 dev->options.video_input = index;
657
658 return retval;
659}
660
661static int vidioc_g_input(struct file *file, void *private_data,
662 unsigned int *index)
663{
664 struct hdpvr_fh *fh = file->private_data;
665 struct hdpvr_device *dev = fh->dev;
666
667 *index = dev->options.video_input;
668 return 0;
669}
670
671
672static const char *audio_iname[] = {
673 [HDPVR_RCA_FRONT] = "RCA front",
674 [HDPVR_RCA_BACK] = "RCA back",
675 [HDPVR_SPDIF] = "SPDIF",
676};
677
678static int vidioc_enumaudio(struct file *file, void *priv,
679 struct v4l2_audio *audio)
680{
681 unsigned int n;
682
683 n = audio->index;
684 if (n >= HDPVR_AUDIO_INPUTS)
685 return -EINVAL;
686
687 audio->capability = V4L2_AUDCAP_STEREO;
688
689 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
690 audio->name[sizeof(audio->name) - 1] = '\0';
691
692 return 0;
693}
694
695static int vidioc_s_audio(struct file *file, void *private_data,
696 struct v4l2_audio *audio)
697{
698 struct hdpvr_fh *fh = file->private_data;
699 struct hdpvr_device *dev = fh->dev;
700 int retval;
701
702 if (audio->index >= HDPVR_AUDIO_INPUTS)
703 return -EINVAL;
704
705 if (dev->status != STATUS_IDLE)
706 return -EAGAIN;
707
708 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
709 if (!retval)
710 dev->options.audio_input = audio->index;
711
712 return retval;
713}
714
715static int vidioc_g_audio(struct file *file, void *private_data,
716 struct v4l2_audio *audio)
717{
718 struct hdpvr_fh *fh = file->private_data;
719 struct hdpvr_device *dev = fh->dev;
720
721 audio->index = dev->options.audio_input;
722 audio->capability = V4L2_AUDCAP_STEREO;
723 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
724 audio->name[sizeof(audio->name) - 1] = '\0';
725 return 0;
726}
727
728static const s32 supported_v4l2_ctrls[] = {
729 V4L2_CID_BRIGHTNESS,
730 V4L2_CID_CONTRAST,
731 V4L2_CID_SATURATION,
732 V4L2_CID_HUE,
733 V4L2_CID_SHARPNESS,
734 V4L2_CID_MPEG_AUDIO_ENCODING,
735 V4L2_CID_MPEG_VIDEO_ENCODING,
736 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
737 V4L2_CID_MPEG_VIDEO_BITRATE,
738 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
739};
740
741static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
742 int ac3)
743{
744 int err;
745
746 switch (qc->id) {
747 case V4L2_CID_BRIGHTNESS:
748 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
749 case V4L2_CID_CONTRAST:
750 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
751 case V4L2_CID_SATURATION:
752 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
753 case V4L2_CID_HUE:
754 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
755 case V4L2_CID_SHARPNESS:
756 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
757 case V4L2_CID_MPEG_AUDIO_ENCODING:
758 return v4l2_ctrl_query_fill(
759 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
760 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
761 : V4L2_MPEG_AUDIO_ENCODING_AAC,
762 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
763 case V4L2_CID_MPEG_VIDEO_ENCODING:
764 return v4l2_ctrl_query_fill(
765 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
766 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
767 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
768
769/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
770/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
771 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
772 return v4l2_ctrl_query_fill(
773 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
774 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
775 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
776
777 case V4L2_CID_MPEG_VIDEO_BITRATE:
778 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
779 6500000);
780 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
781 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
782 9000000);
783 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
784 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
785 return err;
786 default:
787 return -EINVAL;
788 }
789}
790
791static int vidioc_queryctrl(struct file *file, void *private_data,
792 struct v4l2_queryctrl *qc)
793{
794 struct hdpvr_fh *fh = file->private_data;
795 struct hdpvr_device *dev = fh->dev;
796 int i, next;
797 u32 id = qc->id;
798
799 memset(qc, 0, sizeof(*qc));
800
801 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
802 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
803
804 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
805 if (next) {
806 if (qc->id < supported_v4l2_ctrls[i])
807 qc->id = supported_v4l2_ctrls[i];
808 else
809 continue;
810 }
811
812 if (qc->id == supported_v4l2_ctrls[i])
813 return fill_queryctrl(&dev->options, qc,
814 dev->flags & HDPVR_FLAG_AC3_CAP);
815
816 if (qc->id < supported_v4l2_ctrls[i])
817 break;
818 }
819
820 return -EINVAL;
821}
822
823static int vidioc_g_ctrl(struct file *file, void *private_data,
824 struct v4l2_control *ctrl)
825{
826 struct hdpvr_fh *fh = file->private_data;
827 struct hdpvr_device *dev = fh->dev;
828
829 switch (ctrl->id) {
830 case V4L2_CID_BRIGHTNESS:
831 ctrl->value = dev->options.brightness;
832 break;
833 case V4L2_CID_CONTRAST:
834 ctrl->value = dev->options.contrast;
835 break;
836 case V4L2_CID_SATURATION:
837 ctrl->value = dev->options.saturation;
838 break;
839 case V4L2_CID_HUE:
840 ctrl->value = dev->options.hue;
841 break;
842 case V4L2_CID_SHARPNESS:
843 ctrl->value = dev->options.sharpness;
844 break;
845 default:
846 return -EINVAL;
847 }
848 return 0;
849}
850
851static int vidioc_s_ctrl(struct file *file, void *private_data,
852 struct v4l2_control *ctrl)
853{
854 struct hdpvr_fh *fh = file->private_data;
855 struct hdpvr_device *dev = fh->dev;
856 int retval;
857
858 switch (ctrl->id) {
859 case V4L2_CID_BRIGHTNESS:
860 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
861 if (!retval)
862 dev->options.brightness = ctrl->value;
863 break;
864 case V4L2_CID_CONTRAST:
865 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
866 if (!retval)
867 dev->options.contrast = ctrl->value;
868 break;
869 case V4L2_CID_SATURATION:
870 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
871 if (!retval)
872 dev->options.saturation = ctrl->value;
873 break;
874 case V4L2_CID_HUE:
875 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
876 if (!retval)
877 dev->options.hue = ctrl->value;
878 break;
879 case V4L2_CID_SHARPNESS:
880 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
881 if (!retval)
882 dev->options.sharpness = ctrl->value;
883 break;
884 default:
885 return -EINVAL;
886 }
887
888 return retval;
889}
890
891
892static int hdpvr_get_ctrl(struct hdpvr_options *opt,
893 struct v4l2_ext_control *ctrl)
894{
895 switch (ctrl->id) {
896 case V4L2_CID_MPEG_AUDIO_ENCODING:
897 ctrl->value = opt->audio_codec;
898 break;
899 case V4L2_CID_MPEG_VIDEO_ENCODING:
900 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
901 break;
902/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
903/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
904/* break; */
905 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
906 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
907 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
908 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
909 break;
910 case V4L2_CID_MPEG_VIDEO_BITRATE:
911 ctrl->value = opt->bitrate * 100000;
912 break;
913 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
914 ctrl->value = opt->peak_bitrate * 100000;
915 break;
916 case V4L2_CID_MPEG_STREAM_TYPE:
917 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
918 break;
919 default:
920 return -EINVAL;
921 }
922 return 0;
923}
924
925static int vidioc_g_ext_ctrls(struct file *file, void *priv,
926 struct v4l2_ext_controls *ctrls)
927{
928 struct hdpvr_fh *fh = file->private_data;
929 struct hdpvr_device *dev = fh->dev;
930 int i, err = 0;
931
932 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
933 for (i = 0; i < ctrls->count; i++) {
934 struct v4l2_ext_control *ctrl = ctrls->controls + i;
935
936 err = hdpvr_get_ctrl(&dev->options, ctrl);
937 if (err) {
938 ctrls->error_idx = i;
939 break;
940 }
941 }
942 return err;
943
944 }
945
946 return -EINVAL;
947}
948
949
950static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
951{
952 int ret = -EINVAL;
953
954 switch (ctrl->id) {
955 case V4L2_CID_MPEG_AUDIO_ENCODING:
956 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
957 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
958 ret = 0;
959 break;
960 case V4L2_CID_MPEG_VIDEO_ENCODING:
961 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
962 ret = 0;
963 break;
964/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
965/* if (ctrl->value == 0 || ctrl->value == 128) */
966/* ret = 0; */
967/* break; */
968 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
969 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
970 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
971 ret = 0;
972 break;
973 case V4L2_CID_MPEG_VIDEO_BITRATE:
974 {
975 uint bitrate = ctrl->value / 100000;
976 if (bitrate >= 10 && bitrate <= 135)
977 ret = 0;
978 break;
979 }
980 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
981 {
982 uint peak_bitrate = ctrl->value / 100000;
983 if (peak_bitrate >= 10 && peak_bitrate <= 202)
984 ret = 0;
985 break;
986 }
987 case V4L2_CID_MPEG_STREAM_TYPE:
988 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
989 ret = 0;
990 break;
991 default:
992 return -EINVAL;
993 }
994 return 0;
995}
996
997static int vidioc_try_ext_ctrls(struct file *file, void *priv,
998 struct v4l2_ext_controls *ctrls)
999{
1000 struct hdpvr_fh *fh = file->private_data;
1001 struct hdpvr_device *dev = fh->dev;
1002 int i, err = 0;
1003
1004 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1005 for (i = 0; i < ctrls->count; i++) {
1006 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1007
1008 err = hdpvr_try_ctrl(ctrl,
1009 dev->flags & HDPVR_FLAG_AC3_CAP);
1010 if (err) {
1011 ctrls->error_idx = i;
1012 break;
1013 }
1014 }
1015 return err;
1016 }
1017
1018 return -EINVAL;
1019}
1020
1021
1022static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1023 struct v4l2_ext_control *ctrl)
1024{
1025 struct hdpvr_options *opt = &dev->options;
1026 int ret = 0;
1027
1028 switch (ctrl->id) {
1029 case V4L2_CID_MPEG_AUDIO_ENCODING:
1030 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1031 opt->audio_codec = ctrl->value;
1032 ret = hdpvr_set_audio(dev, opt->audio_input,
1033 opt->audio_codec);
1034 }
1035 break;
1036 case V4L2_CID_MPEG_VIDEO_ENCODING:
1037 break;
1038/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1039/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1040/* opt->gop_mode |= 0x2; */
1041/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1042/* opt->gop_mode); */
1043/* } */
1044/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1045/* opt->gop_mode &= ~0x2; */
1046/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1047/* opt->gop_mode); */
1048/* } */
1049/* break; */
1050 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1051 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1052 opt->bitrate_mode != HDPVR_CONSTANT) {
1053 opt->bitrate_mode = HDPVR_CONSTANT;
1054 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1055 opt->bitrate_mode);
1056 }
1057 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1058 opt->bitrate_mode == HDPVR_CONSTANT) {
1059 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1060 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1061 opt->bitrate_mode);
1062 }
1063 break;
1064 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1065 uint bitrate = ctrl->value / 100000;
1066
1067 opt->bitrate = bitrate;
1068 if (bitrate >= opt->peak_bitrate)
1069 opt->peak_bitrate = bitrate+1;
1070
1071 hdpvr_set_bitrate(dev);
1072 break;
1073 }
1074 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1075 uint peak_bitrate = ctrl->value / 100000;
1076
1077 if (opt->bitrate_mode == HDPVR_CONSTANT)
1078 break;
1079
1080 if (opt->bitrate < peak_bitrate) {
1081 opt->peak_bitrate = peak_bitrate;
1082 hdpvr_set_bitrate(dev);
1083 } else
1084 ret = -EINVAL;
1085 break;
1086 }
1087 case V4L2_CID_MPEG_STREAM_TYPE:
1088 break;
1089 default:
1090 return -EINVAL;
1091 }
1092 return ret;
1093}
1094
1095static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1096 struct v4l2_ext_controls *ctrls)
1097{
1098 struct hdpvr_fh *fh = file->private_data;
1099 struct hdpvr_device *dev = fh->dev;
1100 int i, err = 0;
1101
1102 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1103 for (i = 0; i < ctrls->count; i++) {
1104 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1105
1106 err = hdpvr_try_ctrl(ctrl,
1107 dev->flags & HDPVR_FLAG_AC3_CAP);
1108 if (err) {
1109 ctrls->error_idx = i;
1110 break;
1111 }
1112 err = hdpvr_set_ctrl(dev, ctrl);
1113 if (err) {
1114 ctrls->error_idx = i;
1115 break;
1116 }
1117 }
1118 return err;
1119
1120 }
1121
1122 return -EINVAL;
1123}
1124
1125static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1126 struct v4l2_fmtdesc *f)
1127{
1128
1129 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1130 return -EINVAL;
1131
1132 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1133 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1134 f->pixelformat = V4L2_PIX_FMT_MPEG;
1135
1136 return 0;
1137}
1138
1139static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1140 struct v4l2_format *f)
1141{
1142 struct hdpvr_fh *fh = file->private_data;
1143 struct hdpvr_device *dev = fh->dev;
1144 struct hdpvr_video_info *vid_info;
1145
1146 if (!dev)
1147 return -ENODEV;
1148
1149 vid_info = get_video_info(dev);
1150 if (!vid_info)
1151 return -EFAULT;
1152
1153 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1154 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1155 f->fmt.pix.width = vid_info->width;
1156 f->fmt.pix.height = vid_info->height;
1157 f->fmt.pix.sizeimage = dev->bulk_in_size;
1158 f->fmt.pix.colorspace = 0;
1159 f->fmt.pix.bytesperline = 0;
1160 f->fmt.pix.field = V4L2_FIELD_ANY;
1161
1162 kfree(vid_info);
1163 return 0;
1164}
1165
1166
1167static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1168 .vidioc_querycap = vidioc_querycap,
1169 .vidioc_s_std = vidioc_s_std,
1170 .vidioc_enum_input = vidioc_enum_input,
1171 .vidioc_g_input = vidioc_g_input,
1172 .vidioc_s_input = vidioc_s_input,
1173 .vidioc_enumaudio = vidioc_enumaudio,
1174 .vidioc_g_audio = vidioc_g_audio,
1175 .vidioc_s_audio = vidioc_s_audio,
1176 .vidioc_queryctrl = vidioc_queryctrl,
1177 .vidioc_g_ctrl = vidioc_g_ctrl,
1178 .vidioc_s_ctrl = vidioc_s_ctrl,
1179 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1180 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1181 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1182 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1183 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1184};
1185
1186static void hdpvr_device_release(struct video_device *vdev)
1187{
1188 struct hdpvr_device *dev = video_get_drvdata(vdev);
1189
1190 hdpvr_delete(dev);
1191}
1192
1193static const struct video_device hdpvr_video_template = {
1194/* .type = VFL_TYPE_GRABBER, */
1195/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1196 .fops = &hdpvr_fops,
1197 .release = hdpvr_device_release,
1198 .ioctl_ops = &hdpvr_ioctl_ops,
1199 .tvnorms =
1200 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1201 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1202 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1203 V4L2_STD_PAL_60,
1204};
1205
1206int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1207{
1208 /* setup and register video device */
1209 dev->video_dev = video_device_alloc();
1210 if (!dev->video_dev) {
1211 err("video_device_alloc() failed");
1212 goto error;
1213 }
1214
1215 *(dev->video_dev) = hdpvr_video_template;
1216 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1217 dev->video_dev->parent = &dev->udev->dev;
1218 video_set_drvdata(dev->video_dev, dev);
1219
1220 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1221 err("V4L2 device registration failed");
1222 goto error;
1223 }
1224
1225 return 0;
1226error:
1227 return -ENOMEM;
1228}
diff --git a/drivers/media/video/hdpvr/hdpvr.h b/drivers/media/video/hdpvr/hdpvr.h
new file mode 100644
index 000000000000..17db74feb884
--- /dev/null
+++ b/drivers/media/video/hdpvr/hdpvr.h
@@ -0,0 +1,298 @@
1/*
2 * Hauppage HD PVR USB driver
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/usb.h>
13#include <linux/i2c.h>
14#include <linux/mutex.h>
15#include <linux/workqueue.h>
16#include <linux/videodev2.h>
17
18#define HDPVR_MAJOR_VERSION 0
19#define HDPVR_MINOR_VERSION 2
20#define HDPVR_RELEASE 0
21#define HDPVR_VERSION \
22 KERNEL_VERSION(HDPVR_MAJOR_VERSION, HDPVR_MINOR_VERSION, HDPVR_RELEASE)
23
24#define HDPVR_MAX 8
25
26/* Define these values to match your devices */
27#define HD_PVR_VENDOR_ID 0x2040
28#define HD_PVR_PRODUCT_ID 0x4900
29#define HD_PVR_PRODUCT_ID1 0x4901
30#define HD_PVR_PRODUCT_ID2 0x4902
31
32#define UNSET (-1U)
33
34#define NUM_BUFFERS 64
35
36#define HDPVR_FIRMWARE_VERSION 0x8
37#define HDPVR_FIRMWARE_VERSION_AC3 0xd
38
39/* #define HDPVR_DEBUG */
40
41extern int hdpvr_debug;
42
43#define MSG_INFO 1
44#define MSG_BUFFER 2
45
46struct hdpvr_options {
47 u8 video_std;
48 u8 video_input;
49 u8 audio_input;
50 u8 bitrate; /* in 100kbps */
51 u8 peak_bitrate; /* in 100kbps */
52 u8 bitrate_mode;
53 u8 gop_mode;
54 enum v4l2_mpeg_audio_encoding audio_codec;
55 u8 brightness;
56 u8 contrast;
57 u8 hue;
58 u8 saturation;
59 u8 sharpness;
60};
61
62/* Structure to hold all of our device specific stuff */
63struct hdpvr_device {
64 /* the v4l device for this device */
65 struct video_device *video_dev;
66 /* the usb device for this device */
67 struct usb_device *udev;
68
69 /* the max packet size of the bulk endpoint */
70 size_t bulk_in_size;
71 /* the address of the bulk in endpoint */
72 __u8 bulk_in_endpointAddr;
73
74 /* holds the current device status */
75 __u8 status;
76 /* count the number of openers */
77 uint open_count;
78
79 /* holds the cureent set options */
80 struct hdpvr_options options;
81
82 uint flags;
83
84 /* synchronize I/O */
85 struct mutex io_mutex;
86 /* available buffers */
87 struct list_head free_buff_list;
88 /* in progress buffers */
89 struct list_head rec_buff_list;
90 /* waitqueue for buffers */
91 wait_queue_head_t wait_buffer;
92 /* waitqueue for data */
93 wait_queue_head_t wait_data;
94 /**/
95 struct workqueue_struct *workqueue;
96 /**/
97 struct work_struct worker;
98
99 /* I2C adapter */
100 struct i2c_adapter *i2c_adapter;
101 /* I2C lock */
102 struct mutex i2c_mutex;
103
104 /* usb control transfer buffer and lock */
105 struct mutex usbc_mutex;
106 u8 *usbc_buf;
107};
108
109
110/* buffer one bulk urb of data */
111struct hdpvr_buffer {
112 struct list_head buff_list;
113
114 struct urb *urb;
115
116 struct hdpvr_device *dev;
117
118 uint pos;
119
120 __u8 status;
121};
122
123/* */
124
125struct hdpvr_video_info {
126 u16 width;
127 u16 height;
128 u8 fps;
129};
130
131enum {
132 STATUS_UNINITIALIZED = 0,
133 STATUS_IDLE,
134 STATUS_STARTING,
135 STATUS_SHUTTING_DOWN,
136 STATUS_STREAMING,
137 STATUS_ERROR,
138 STATUS_DISCONNECTED,
139};
140
141enum {
142 HDPVR_FLAG_AC3_CAP = 1,
143};
144
145enum {
146 BUFSTAT_UNINITIALIZED = 0,
147 BUFSTAT_AVAILABLE,
148 BUFSTAT_INPROGRESS,
149 BUFSTAT_READY,
150};
151
152#define CTRL_START_STREAMING_VALUE 0x0700
153#define CTRL_STOP_STREAMING_VALUE 0x0800
154#define CTRL_BITRATE_VALUE 0x1000
155#define CTRL_BITRATE_MODE_VALUE 0x1200
156#define CTRL_GOP_MODE_VALUE 0x1300
157#define CTRL_VIDEO_INPUT_VALUE 0x1500
158#define CTRL_VIDEO_STD_TYPE 0x1700
159#define CTRL_AUDIO_INPUT_VALUE 0x2500
160#define CTRL_BRIGHTNESS 0x2900
161#define CTRL_CONTRAST 0x2a00
162#define CTRL_HUE 0x2b00
163#define CTRL_SATURATION 0x2c00
164#define CTRL_SHARPNESS 0x2d00
165#define CTRL_LOW_PASS_FILTER_VALUE 0x3100
166
167#define CTRL_DEFAULT_INDEX 0x0003
168
169
170 /* :0 s 38 01 1000 0003 0004 4 = 0a00ca00
171 * BITRATE SETTING
172 * 1st and 2nd byte (little endian): average bitrate in 100 000 bit/s
173 * min: 1 mbit/s, max: 13.5 mbit/s
174 * 3rd and 4th byte (little endian): peak bitrate in 100 000 bit/s
175 * min: average + 100kbit/s,
176 * max: 20.2 mbit/s
177 */
178
179 /* :0 s 38 01 1200 0003 0001 1 = 02
180 * BIT RATE MODE
181 * constant = 1, variable (peak) = 2, variable (average) = 3
182 */
183
184 /* :0 s 38 01 1300 0003 0001 1 = 03
185 * GOP MODE (2 bit)
186 * low bit 0/1: advanced/simple GOP
187 * high bit 0/1: IDR(4/32/128) / no IDR (4/32/0)
188 */
189
190 /* :0 s 38 01 1700 0003 0001 1 = 00
191 * VIDEO STANDARD or FREQUNCY 0 = 60hz, 1 = 50hz
192 */
193
194 /* :0 s 38 01 3100 0003 0004 4 = 03030000
195 * FILTER CONTROL
196 * 1st byte luma low pass filter strength,
197 * 2nd byte chroma low pass filter strength,
198 * 3rd byte MF enable chroma, min=0, max=1
199 * 4th byte n
200 */
201
202
203 /* :0 s 38 b9 0001 0000 0000 0 */
204
205
206
207/* :0 s 38 d3 0000 0000 0001 1 = 00 */
208/* ret = usb_control_msg(dev->udev, */
209/* usb_sndctrlpipe(dev->udev, 0), */
210/* 0xd3, 0x38, */
211/* 0, 0, */
212/* "\0", 1, */
213/* 1000); */
214
215/* info("control request returned %d", ret); */
216/* msleep(5000); */
217
218
219 /* :0 s b8 81 1400 0003 0005 5 <
220 * :0 0 5 = d0024002 19
221 * QUERY FRAME SIZE AND RATE
222 * 1st and 2nd byte (little endian): horizontal resolution
223 * 3rd and 4th byte (little endian): vertical resolution
224 * 5th byte: frame rate
225 */
226
227 /* :0 s b8 81 1800 0003 0003 3 <
228 * :0 0 3 = 030104
229 * QUERY SIGNAL AND DETECTED LINES, maybe INPUT
230 */
231
232enum hdpvr_video_std {
233 HDPVR_60HZ = 0,
234 HDPVR_50HZ,
235};
236
237enum hdpvr_video_input {
238 HDPVR_COMPONENT = 0,
239 HDPVR_SVIDEO,
240 HDPVR_COMPOSITE,
241 HDPVR_VIDEO_INPUTS
242};
243
244enum hdpvr_audio_inputs {
245 HDPVR_RCA_BACK = 0,
246 HDPVR_RCA_FRONT,
247 HDPVR_SPDIF,
248 HDPVR_AUDIO_INPUTS
249};
250
251enum hdpvr_bitrate_mode {
252 HDPVR_CONSTANT = 1,
253 HDPVR_VARIABLE_PEAK,
254 HDPVR_VARIABLE_AVERAGE,
255};
256
257enum hdpvr_gop_mode {
258 HDPVR_ADVANCED_IDR_GOP = 0,
259 HDPVR_SIMPLE_IDR_GOP,
260 HDPVR_ADVANCED_NOIDR_GOP,
261 HDPVR_SIMPLE_NOIDR_GOP,
262};
263
264void hdpvr_delete(struct hdpvr_device *dev);
265
266/*========================================================================*/
267/* hardware control functions */
268int hdpvr_set_options(struct hdpvr_device *dev);
269
270int hdpvr_set_bitrate(struct hdpvr_device *dev);
271
272int hdpvr_set_audio(struct hdpvr_device *dev, u8 input,
273 enum v4l2_mpeg_audio_encoding codec);
274
275int hdpvr_config_call(struct hdpvr_device *dev, uint value,
276 unsigned char valbuf);
277
278struct hdpvr_video_info *get_video_info(struct hdpvr_device *dev);
279
280/* :0 s b8 81 1800 0003 0003 3 < */
281/* :0 0 3 = 0301ff */
282int get_input_lines_info(struct hdpvr_device *dev);
283
284
285/*========================================================================*/
286/* v4l2 registration */
287int hdpvr_register_videodev(struct hdpvr_device *dev, int devnumber);
288
289int hdpvr_cancel_queue(struct hdpvr_device *dev);
290
291/*========================================================================*/
292/* i2c adapter registration */
293int hdpvr_register_i2c_adapter(struct hdpvr_device *dev);
294
295/*========================================================================*/
296/* buffer management */
297int hdpvr_free_buffers(struct hdpvr_device *dev);
298int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count);