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