V4L/DVB (11125): fix mispelled Hauppauge in HD PVR and PVR USB2 driver comments
[deliverable/linux.git] / drivers / media / video / hdpvr / hdpvr-video.c
CommitLineData
9aba42ef 1/*
e86da6f0 2 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
9aba42ef
JG
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
9aba42ef
JG
527static const struct v4l2_file_operations hdpvr_fops = {
528 .owner = THIS_MODULE,
529 .open = hdpvr_open,
530 .release = hdpvr_release,
531 .read = hdpvr_read,
532 .poll = hdpvr_poll,
76717b88 533 .unlocked_ioctl = video_ioctl2,
9aba42ef
JG
534};
535
536/*=======================================================================*/
537/*
538 * V4L2 ioctl handling
539 */
540
541static int vidioc_querycap(struct file *file, void *priv,
542 struct v4l2_capability *cap)
543{
544 struct hdpvr_device *dev = video_drvdata(file);
545
546 strcpy(cap->driver, "hdpvr");
547 strcpy(cap->card, "Haupauge HD PVR");
548 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
549 cap->version = HDPVR_VERSION;
550 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
551 V4L2_CAP_AUDIO |
552 V4L2_CAP_READWRITE;
553 return 0;
554}
555
556static int vidioc_s_std(struct file *file, void *private_data,
557 v4l2_std_id *std)
558{
559 struct hdpvr_fh *fh = file->private_data;
560 struct hdpvr_device *dev = fh->dev;
561 u8 std_type = 1;
562
563 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
564 std_type = 0;
565
566 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
567}
568
569static const char *iname[] = {
570 [HDPVR_COMPONENT] = "Component",
571 [HDPVR_SVIDEO] = "S-Video",
572 [HDPVR_COMPOSITE] = "Composite",
573};
574
575static int vidioc_enum_input(struct file *file, void *priv,
576 struct v4l2_input *i)
577{
578 struct hdpvr_fh *fh = file->private_data;
579 struct hdpvr_device *dev = fh->dev;
580 unsigned int n;
581
582 n = i->index;
583 if (n >= HDPVR_VIDEO_INPUTS)
584 return -EINVAL;
585
586 i->type = V4L2_INPUT_TYPE_CAMERA;
587
588 strncpy(i->name, iname[n], sizeof(i->name) - 1);
589 i->name[sizeof(i->name) - 1] = '\0';
590
591 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
592
593 i->std = dev->video_dev->tvnorms;
594
595 return 0;
596}
597
598static int vidioc_s_input(struct file *file, void *private_data,
599 unsigned int index)
600{
601 struct hdpvr_fh *fh = file->private_data;
602 struct hdpvr_device *dev = fh->dev;
603 int retval;
604
605 if (index >= HDPVR_VIDEO_INPUTS)
606 return -EINVAL;
607
608 if (dev->status != STATUS_IDLE)
609 return -EAGAIN;
610
611 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
612 if (!retval)
613 dev->options.video_input = index;
614
615 return retval;
616}
617
618static int vidioc_g_input(struct file *file, void *private_data,
619 unsigned int *index)
620{
621 struct hdpvr_fh *fh = file->private_data;
622 struct hdpvr_device *dev = fh->dev;
623
624 *index = dev->options.video_input;
625 return 0;
626}
627
628
629static const char *audio_iname[] = {
630 [HDPVR_RCA_FRONT] = "RCA front",
631 [HDPVR_RCA_BACK] = "RCA back",
632 [HDPVR_SPDIF] = "SPDIF",
633};
634
635static int vidioc_enumaudio(struct file *file, void *priv,
636 struct v4l2_audio *audio)
637{
638 unsigned int n;
639
640 n = audio->index;
641 if (n >= HDPVR_AUDIO_INPUTS)
642 return -EINVAL;
643
644 audio->capability = V4L2_AUDCAP_STEREO;
645
646 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
647 audio->name[sizeof(audio->name) - 1] = '\0';
648
649 return 0;
650}
651
652static int vidioc_s_audio(struct file *file, void *private_data,
653 struct v4l2_audio *audio)
654{
655 struct hdpvr_fh *fh = file->private_data;
656 struct hdpvr_device *dev = fh->dev;
657 int retval;
658
659 if (audio->index >= HDPVR_AUDIO_INPUTS)
660 return -EINVAL;
661
662 if (dev->status != STATUS_IDLE)
663 return -EAGAIN;
664
665 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
666 if (!retval)
667 dev->options.audio_input = audio->index;
668
669 return retval;
670}
671
672static int vidioc_g_audio(struct file *file, void *private_data,
673 struct v4l2_audio *audio)
674{
675 struct hdpvr_fh *fh = file->private_data;
676 struct hdpvr_device *dev = fh->dev;
677
678 audio->index = dev->options.audio_input;
679 audio->capability = V4L2_AUDCAP_STEREO;
680 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
681 audio->name[sizeof(audio->name) - 1] = '\0';
682 return 0;
683}
684
685static const s32 supported_v4l2_ctrls[] = {
686 V4L2_CID_BRIGHTNESS,
687 V4L2_CID_CONTRAST,
688 V4L2_CID_SATURATION,
689 V4L2_CID_HUE,
690 V4L2_CID_SHARPNESS,
691 V4L2_CID_MPEG_AUDIO_ENCODING,
692 V4L2_CID_MPEG_VIDEO_ENCODING,
693 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
694 V4L2_CID_MPEG_VIDEO_BITRATE,
695 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
696};
697
698static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
699 int ac3)
700{
701 int err;
702
703 switch (qc->id) {
704 case V4L2_CID_BRIGHTNESS:
705 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
706 case V4L2_CID_CONTRAST:
707 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
708 case V4L2_CID_SATURATION:
709 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
710 case V4L2_CID_HUE:
711 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
712 case V4L2_CID_SHARPNESS:
713 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
714 case V4L2_CID_MPEG_AUDIO_ENCODING:
715 return v4l2_ctrl_query_fill(
716 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
717 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
718 : V4L2_MPEG_AUDIO_ENCODING_AAC,
719 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
720 case V4L2_CID_MPEG_VIDEO_ENCODING:
721 return v4l2_ctrl_query_fill(
722 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
723 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
724 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
725
726/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
727/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
728 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
729 return v4l2_ctrl_query_fill(
730 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
731 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
732 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
733
734 case V4L2_CID_MPEG_VIDEO_BITRATE:
735 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
736 6500000);
737 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
738 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
739 9000000);
740 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
741 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
742 return err;
743 default:
744 return -EINVAL;
745 }
746}
747
748static int vidioc_queryctrl(struct file *file, void *private_data,
749 struct v4l2_queryctrl *qc)
750{
751 struct hdpvr_fh *fh = file->private_data;
752 struct hdpvr_device *dev = fh->dev;
753 int i, next;
754 u32 id = qc->id;
755
756 memset(qc, 0, sizeof(*qc));
757
758 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
759 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
760
761 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
762 if (next) {
763 if (qc->id < supported_v4l2_ctrls[i])
764 qc->id = supported_v4l2_ctrls[i];
765 else
766 continue;
767 }
768
769 if (qc->id == supported_v4l2_ctrls[i])
770 return fill_queryctrl(&dev->options, qc,
771 dev->flags & HDPVR_FLAG_AC3_CAP);
772
773 if (qc->id < supported_v4l2_ctrls[i])
774 break;
775 }
776
777 return -EINVAL;
778}
779
780static int vidioc_g_ctrl(struct file *file, void *private_data,
781 struct v4l2_control *ctrl)
782{
783 struct hdpvr_fh *fh = file->private_data;
784 struct hdpvr_device *dev = fh->dev;
785
786 switch (ctrl->id) {
787 case V4L2_CID_BRIGHTNESS:
788 ctrl->value = dev->options.brightness;
789 break;
790 case V4L2_CID_CONTRAST:
791 ctrl->value = dev->options.contrast;
792 break;
793 case V4L2_CID_SATURATION:
794 ctrl->value = dev->options.saturation;
795 break;
796 case V4L2_CID_HUE:
797 ctrl->value = dev->options.hue;
798 break;
799 case V4L2_CID_SHARPNESS:
800 ctrl->value = dev->options.sharpness;
801 break;
802 default:
803 return -EINVAL;
804 }
805 return 0;
806}
807
808static int vidioc_s_ctrl(struct file *file, void *private_data,
809 struct v4l2_control *ctrl)
810{
811 struct hdpvr_fh *fh = file->private_data;
812 struct hdpvr_device *dev = fh->dev;
813 int retval;
814
815 switch (ctrl->id) {
816 case V4L2_CID_BRIGHTNESS:
817 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
818 if (!retval)
819 dev->options.brightness = ctrl->value;
820 break;
821 case V4L2_CID_CONTRAST:
822 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
823 if (!retval)
824 dev->options.contrast = ctrl->value;
825 break;
826 case V4L2_CID_SATURATION:
827 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
828 if (!retval)
829 dev->options.saturation = ctrl->value;
830 break;
831 case V4L2_CID_HUE:
832 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
833 if (!retval)
834 dev->options.hue = ctrl->value;
835 break;
836 case V4L2_CID_SHARPNESS:
837 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
838 if (!retval)
839 dev->options.sharpness = ctrl->value;
840 break;
841 default:
842 return -EINVAL;
843 }
844
845 return retval;
846}
847
848
849static int hdpvr_get_ctrl(struct hdpvr_options *opt,
850 struct v4l2_ext_control *ctrl)
851{
852 switch (ctrl->id) {
853 case V4L2_CID_MPEG_AUDIO_ENCODING:
854 ctrl->value = opt->audio_codec;
855 break;
856 case V4L2_CID_MPEG_VIDEO_ENCODING:
857 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
858 break;
859/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
860/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
861/* break; */
862 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
863 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
864 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
865 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
866 break;
867 case V4L2_CID_MPEG_VIDEO_BITRATE:
868 ctrl->value = opt->bitrate * 100000;
869 break;
870 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
871 ctrl->value = opt->peak_bitrate * 100000;
872 break;
873 case V4L2_CID_MPEG_STREAM_TYPE:
874 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
875 break;
876 default:
877 return -EINVAL;
878 }
879 return 0;
880}
881
882static int vidioc_g_ext_ctrls(struct file *file, void *priv,
883 struct v4l2_ext_controls *ctrls)
884{
885 struct hdpvr_fh *fh = file->private_data;
886 struct hdpvr_device *dev = fh->dev;
887 int i, err = 0;
888
889 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
890 for (i = 0; i < ctrls->count; i++) {
891 struct v4l2_ext_control *ctrl = ctrls->controls + i;
892
893 err = hdpvr_get_ctrl(&dev->options, ctrl);
894 if (err) {
895 ctrls->error_idx = i;
896 break;
897 }
898 }
899 return err;
900
901 }
902
903 return -EINVAL;
904}
905
906
907static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
908{
909 int ret = -EINVAL;
910
911 switch (ctrl->id) {
912 case V4L2_CID_MPEG_AUDIO_ENCODING:
913 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
914 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
915 ret = 0;
916 break;
917 case V4L2_CID_MPEG_VIDEO_ENCODING:
918 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
919 ret = 0;
920 break;
921/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
922/* if (ctrl->value == 0 || ctrl->value == 128) */
923/* ret = 0; */
924/* break; */
925 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
926 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
927 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
928 ret = 0;
929 break;
930 case V4L2_CID_MPEG_VIDEO_BITRATE:
931 {
932 uint bitrate = ctrl->value / 100000;
933 if (bitrate >= 10 && bitrate <= 135)
934 ret = 0;
935 break;
936 }
937 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
938 {
939 uint peak_bitrate = ctrl->value / 100000;
940 if (peak_bitrate >= 10 && peak_bitrate <= 202)
941 ret = 0;
942 break;
943 }
944 case V4L2_CID_MPEG_STREAM_TYPE:
945 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
946 ret = 0;
947 break;
948 default:
949 return -EINVAL;
950 }
951 return 0;
952}
953
954static int vidioc_try_ext_ctrls(struct file *file, void *priv,
955 struct v4l2_ext_controls *ctrls)
956{
957 struct hdpvr_fh *fh = file->private_data;
958 struct hdpvr_device *dev = fh->dev;
959 int i, err = 0;
960
961 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
962 for (i = 0; i < ctrls->count; i++) {
963 struct v4l2_ext_control *ctrl = ctrls->controls + i;
964
965 err = hdpvr_try_ctrl(ctrl,
966 dev->flags & HDPVR_FLAG_AC3_CAP);
967 if (err) {
968 ctrls->error_idx = i;
969 break;
970 }
971 }
972 return err;
973 }
974
975 return -EINVAL;
976}
977
978
979static int hdpvr_set_ctrl(struct hdpvr_device *dev,
980 struct v4l2_ext_control *ctrl)
981{
982 struct hdpvr_options *opt = &dev->options;
983 int ret = 0;
984
985 switch (ctrl->id) {
986 case V4L2_CID_MPEG_AUDIO_ENCODING:
987 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
988 opt->audio_codec = ctrl->value;
989 ret = hdpvr_set_audio(dev, opt->audio_input,
990 opt->audio_codec);
991 }
992 break;
993 case V4L2_CID_MPEG_VIDEO_ENCODING:
994 break;
995/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
996/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
997/* opt->gop_mode |= 0x2; */
998/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
999/* opt->gop_mode); */
1000/* } */
1001/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1002/* opt->gop_mode &= ~0x2; */
1003/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1004/* opt->gop_mode); */
1005/* } */
1006/* break; */
1007 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1008 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1009 opt->bitrate_mode != HDPVR_CONSTANT) {
1010 opt->bitrate_mode = HDPVR_CONSTANT;
1011 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1012 opt->bitrate_mode);
1013 }
1014 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1015 opt->bitrate_mode == HDPVR_CONSTANT) {
1016 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1017 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1018 opt->bitrate_mode);
1019 }
1020 break;
1021 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1022 uint bitrate = ctrl->value / 100000;
1023
1024 opt->bitrate = bitrate;
1025 if (bitrate >= opt->peak_bitrate)
1026 opt->peak_bitrate = bitrate+1;
1027
1028 hdpvr_set_bitrate(dev);
1029 break;
1030 }
1031 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1032 uint peak_bitrate = ctrl->value / 100000;
1033
1034 if (opt->bitrate_mode == HDPVR_CONSTANT)
1035 break;
1036
1037 if (opt->bitrate < peak_bitrate) {
1038 opt->peak_bitrate = peak_bitrate;
1039 hdpvr_set_bitrate(dev);
1040 } else
1041 ret = -EINVAL;
1042 break;
1043 }
1044 case V4L2_CID_MPEG_STREAM_TYPE:
1045 break;
1046 default:
1047 return -EINVAL;
1048 }
1049 return ret;
1050}
1051
1052static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1053 struct v4l2_ext_controls *ctrls)
1054{
1055 struct hdpvr_fh *fh = file->private_data;
1056 struct hdpvr_device *dev = fh->dev;
1057 int i, err = 0;
1058
1059 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1060 for (i = 0; i < ctrls->count; i++) {
1061 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1062
1063 err = hdpvr_try_ctrl(ctrl,
1064 dev->flags & HDPVR_FLAG_AC3_CAP);
1065 if (err) {
1066 ctrls->error_idx = i;
1067 break;
1068 }
1069 err = hdpvr_set_ctrl(dev, ctrl);
1070 if (err) {
1071 ctrls->error_idx = i;
1072 break;
1073 }
1074 }
1075 return err;
1076
1077 }
1078
1079 return -EINVAL;
1080}
1081
1082static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1083 struct v4l2_fmtdesc *f)
1084{
1085
1086 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 return -EINVAL;
1088
1089 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1090 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1091 f->pixelformat = V4L2_PIX_FMT_MPEG;
1092
1093 return 0;
1094}
1095
1096static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1097 struct v4l2_format *f)
1098{
1099 struct hdpvr_fh *fh = file->private_data;
1100 struct hdpvr_device *dev = fh->dev;
1101 struct hdpvr_video_info *vid_info;
1102
1103 if (!dev)
1104 return -ENODEV;
1105
1106 vid_info = get_video_info(dev);
1107 if (!vid_info)
1108 return -EFAULT;
1109
1110 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1111 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1112 f->fmt.pix.width = vid_info->width;
1113 f->fmt.pix.height = vid_info->height;
1114 f->fmt.pix.sizeimage = dev->bulk_in_size;
1115 f->fmt.pix.colorspace = 0;
1116 f->fmt.pix.bytesperline = 0;
1117 f->fmt.pix.field = V4L2_FIELD_ANY;
1118
1119 kfree(vid_info);
1120 return 0;
1121}
1122
76717b88
JG
1123static int vidioc_encoder_cmd(struct file *filp, void *priv,
1124 struct v4l2_encoder_cmd *a)
1125{
1126 struct hdpvr_fh *fh = filp->private_data;
1127 struct hdpvr_device *dev = fh->dev;
1128 int res;
1129
1130 mutex_lock(&dev->io_mutex);
1131
1132 memset(&a->raw, 0, sizeof(a->raw));
1133 switch (a->cmd) {
1134 case V4L2_ENC_CMD_START:
1135 a->flags = 0;
1136 res = hdpvr_start_streaming(dev);
1137 break;
1138 case V4L2_ENC_CMD_STOP:
1139 res = hdpvr_stop_streaming(dev);
1140 break;
1141 default:
1142 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
1143 "Unsupported encoder cmd %d\n", a->cmd);
1144 return -EINVAL;
1145 }
1146 mutex_unlock(&dev->io_mutex);
1147 return res;
1148}
1149
1150static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1151 struct v4l2_encoder_cmd *a)
1152{
1153 switch (a->cmd) {
1154 case V4L2_ENC_CMD_START:
1155 case V4L2_ENC_CMD_STOP:
1156 return 0;
1157 default:
1158 return -EINVAL;
1159 }
1160}
9aba42ef
JG
1161
1162static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1163 .vidioc_querycap = vidioc_querycap,
1164 .vidioc_s_std = vidioc_s_std,
1165 .vidioc_enum_input = vidioc_enum_input,
1166 .vidioc_g_input = vidioc_g_input,
1167 .vidioc_s_input = vidioc_s_input,
1168 .vidioc_enumaudio = vidioc_enumaudio,
1169 .vidioc_g_audio = vidioc_g_audio,
1170 .vidioc_s_audio = vidioc_s_audio,
1171 .vidioc_queryctrl = vidioc_queryctrl,
1172 .vidioc_g_ctrl = vidioc_g_ctrl,
1173 .vidioc_s_ctrl = vidioc_s_ctrl,
1174 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1175 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1176 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1177 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1178 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
76717b88
JG
1179 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1180 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
9aba42ef
JG
1181};
1182
1183static void hdpvr_device_release(struct video_device *vdev)
1184{
1185 struct hdpvr_device *dev = video_get_drvdata(vdev);
1186
1187 hdpvr_delete(dev);
1188}
1189
1190static const struct video_device hdpvr_video_template = {
1191/* .type = VFL_TYPE_GRABBER, */
1192/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1193 .fops = &hdpvr_fops,
1194 .release = hdpvr_device_release,
1195 .ioctl_ops = &hdpvr_ioctl_ops,
1196 .tvnorms =
1197 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1198 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1199 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1200 V4L2_STD_PAL_60,
1201};
1202
1203int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1204{
1205 /* setup and register video device */
1206 dev->video_dev = video_device_alloc();
1207 if (!dev->video_dev) {
1208 err("video_device_alloc() failed");
1209 goto error;
1210 }
1211
1212 *(dev->video_dev) = hdpvr_video_template;
1213 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1214 dev->video_dev->parent = &dev->udev->dev;
1215 video_set_drvdata(dev->video_dev, dev);
1216
1217 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1218 err("V4L2 device registration failed");
1219 goto error;
1220 }
1221
1222 return 0;
1223error:
1224 return -ENOMEM;
1225}
This page took 0.124808 seconds and 5 git commands to generate.