V4L/DVB (11230): hdpvr: return immediately from hdpvr_poll if data is available
[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
d211bfcb
JG
31#define print_buffer_status() v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_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
9aba42ef
JG
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_buffer_free(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->video_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 err("cannot allocate buffer");
134 goto exit;
135 }
136 buf->dev = dev;
137
138 urb = usb_alloc_urb(0, GFP_KERNEL);
139 if (!urb) {
140 err("cannot allocate urb");
141 goto exit;
142 }
143 buf->urb = urb;
144
145 mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
146 &urb->transfer_dma);
147 if (!mem) {
148 err("cannot allocate usb transfer buffer");
149 goto exit;
150 }
151
152 usb_fill_bulk_urb(buf->urb, dev->udev,
153 usb_rcvbulkpipe(dev->udev,
154 dev->bulk_in_endpointAddr),
155 mem, dev->bulk_in_size,
156 hdpvr_read_bulk_callback, buf);
157
158 buf->status = BUFSTAT_AVAILABLE;
159 list_add_tail(&buf->buff_list, &dev->free_buff_list);
160 }
161 return 0;
162exit:
163 hdpvr_free_buffers(dev);
164 return retval;
165}
166
167static int hdpvr_submit_buffers(struct hdpvr_device *dev)
168{
169 struct hdpvr_buffer *buf;
170 struct urb *urb;
171 int ret = 0, err_count = 0;
172
173 mutex_lock(&dev->io_mutex);
174
175 while (dev->status == STATUS_STREAMING &&
176 !list_empty(&dev->free_buff_list)) {
177
178 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
179 buff_list);
180 if (buf->status != BUFSTAT_AVAILABLE) {
181 err("buffer not marked as availbale");
182 ret = -EFAULT;
183 goto err;
184 }
185
186 urb = buf->urb;
187 urb->status = 0;
188 urb->actual_length = 0;
189 ret = usb_submit_urb(urb, GFP_KERNEL);
190 if (ret) {
191 err("usb_submit_urb in %s returned %d", __func__, ret);
192 if (++err_count > 2)
193 break;
194 continue;
195 }
196 buf->status = BUFSTAT_INPROGRESS;
197 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
198 }
199err:
d211bfcb 200 print_buffer_status();
9aba42ef
JG
201 mutex_unlock(&dev->io_mutex);
202 return ret;
203}
204
205static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
206{
207 struct hdpvr_buffer *buf;
208
209 mutex_lock(&dev->io_mutex);
210
211 if (list_empty(&dev->rec_buff_list)) {
212 mutex_unlock(&dev->io_mutex);
213 return NULL;
214 }
215
216 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
217 buff_list);
218 mutex_unlock(&dev->io_mutex);
219
220 return buf;
221}
222
223static void hdpvr_transmit_buffers(struct work_struct *work)
224{
225 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
226 worker);
227
228 while (dev->status == STATUS_STREAMING) {
229
230 if (hdpvr_submit_buffers(dev)) {
231 v4l2_err(dev->video_dev, "couldn't submit buffers\n");
232 goto error;
233 }
234 if (wait_event_interruptible(dev->wait_buffer,
235 !list_empty(&dev->free_buff_list) ||
236 dev->status != STATUS_STREAMING))
237 goto error;
238 }
239
240 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
241 "transmit worker exited\n");
242 return;
243error:
244 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
245 "transmit buffers errored\n");
246 dev->status = STATUS_ERROR;
247}
248
249/* function expects dev->io_mutex to be hold by caller */
250static int hdpvr_start_streaming(struct hdpvr_device *dev)
251{
252 int ret;
253 struct hdpvr_video_info *vidinf;
254
255 if (dev->status == STATUS_STREAMING)
256 return 0;
257 else if (dev->status != STATUS_IDLE)
258 return -EAGAIN;
259
260 vidinf = get_video_info(dev);
261
262 if (vidinf) {
263 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
264 "video signal: %dx%d@%dhz\n", vidinf->width,
265 vidinf->height, vidinf->fps);
266 kfree(vidinf);
267
268 /* start streaming 2 request */
269 ret = usb_control_msg(dev->udev,
270 usb_sndctrlpipe(dev->udev, 0),
271 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
272 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
273 "encoder start control request returned %d\n", ret);
274
275 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
276
277 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
278 queue_work(dev->workqueue, &dev->worker);
279
280 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
281 "streaming started\n");
282 dev->status = STATUS_STREAMING;
283
284 return 0;
285 }
286 msleep(250);
287 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
288 "no video signal at input %d\n", dev->options.video_input);
289 return -EAGAIN;
290}
291
292
293/* function expects dev->io_mutex to be hold by caller */
294static int hdpvr_stop_streaming(struct hdpvr_device *dev)
295{
296 if (dev->status == STATUS_IDLE)
297 return 0;
298 else if (dev->status != STATUS_STREAMING)
299 return -EAGAIN;
300
301 dev->status = STATUS_SHUTTING_DOWN;
302 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
303
304 wake_up_interruptible(&dev->wait_buffer);
305 msleep(50);
306
307 flush_workqueue(dev->workqueue);
308
309 /* kill the still outstanding urbs */
310 hdpvr_cancel_queue(dev);
311
312 dev->status = STATUS_IDLE;
313
314 return 0;
315}
316
317
318/*=======================================================================*/
319/*
320 * video 4 linux 2 file operations
321 */
322
323static int hdpvr_open(struct file *file)
324{
325 struct hdpvr_device *dev;
326 struct hdpvr_fh *fh;
327 int retval = -ENOMEM;
328
329 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
330 if (!dev) {
331 err("open failing with with ENODEV");
332 retval = -ENODEV;
333 goto err;
334 }
335
336 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
337 if (!fh) {
338 err("Out of memory?");
339 goto err;
340 }
341 /* lock the device to allow correctly handling errors
342 * in resumption */
343 mutex_lock(&dev->io_mutex);
344 dev->open_count++;
345
346 fh->dev = dev;
347
348 /* save our object in the file's private structure */
349 file->private_data = fh;
350
351 retval = 0;
352err:
353 mutex_unlock(&dev->io_mutex);
354 return retval;
355}
356
357static int hdpvr_release(struct file *file)
358{
359 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
360 struct hdpvr_device *dev = fh->dev;
361
362 if (!dev)
363 return -ENODEV;
364
365 mutex_lock(&dev->io_mutex);
366 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
367 hdpvr_stop_streaming(dev);
368
369 mutex_unlock(&dev->io_mutex);
370
371 return 0;
372}
373
374/*
375 * hdpvr_v4l2_read()
376 * will allocate buffers when called for the first time
377 */
378static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
379 loff_t *pos)
380{
381 struct hdpvr_fh *fh = file->private_data;
382 struct hdpvr_device *dev = fh->dev;
383 struct hdpvr_buffer *buf = NULL;
384 struct urb *urb;
385 unsigned int ret = 0;
386 int rem, cnt;
387
388 if (*pos)
389 return -ESPIPE;
390
391 if (!dev)
392 return -ENODEV;
393
394 mutex_lock(&dev->io_mutex);
395 if (dev->status == STATUS_IDLE) {
396 if (hdpvr_start_streaming(dev)) {
397 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
398 "start_streaming failed");
399 ret = -EIO;
400 msleep(200);
401 dev->status = STATUS_IDLE;
402 mutex_unlock(&dev->io_mutex);
403 goto err;
404 }
d211bfcb 405 print_buffer_status();
9aba42ef
JG
406 }
407 mutex_unlock(&dev->io_mutex);
408
409 /* wait for the first buffer */
410 if (!(file->f_flags & O_NONBLOCK)) {
411 if (wait_event_interruptible(dev->wait_data,
412 hdpvr_get_next_buffer(dev)))
413 return -ERESTARTSYS;
414 }
415
416 buf = hdpvr_get_next_buffer(dev);
417
418 while (count > 0 && buf) {
419
420 if (buf->status != BUFSTAT_READY &&
421 dev->status != STATUS_DISCONNECTED) {
422 /* return nonblocking */
423 if (file->f_flags & O_NONBLOCK) {
424 if (!ret)
425 ret = -EAGAIN;
426 goto err;
427 }
428
429 if (wait_event_interruptible(dev->wait_data,
430 buf->status == BUFSTAT_READY)) {
431 ret = -ERESTARTSYS;
432 goto err;
433 }
434 }
435
436 if (buf->status != BUFSTAT_READY)
437 break;
438
439 /* set remaining bytes to copy */
440 urb = buf->urb;
441 rem = urb->actual_length - buf->pos;
442 cnt = rem > count ? count : rem;
443
444 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
445 cnt)) {
446 err("read: copy_to_user failed");
447 if (!ret)
448 ret = -EFAULT;
449 goto err;
450 }
451
452 buf->pos += cnt;
453 count -= cnt;
454 buffer += cnt;
455 ret += cnt;
456
457 /* finished, take next buffer */
458 if (buf->pos == urb->actual_length) {
459 mutex_lock(&dev->io_mutex);
460 buf->pos = 0;
461 buf->status = BUFSTAT_AVAILABLE;
462
463 list_move_tail(&buf->buff_list, &dev->free_buff_list);
464
d211bfcb 465 print_buffer_status();
9aba42ef
JG
466
467 mutex_unlock(&dev->io_mutex);
468
469 wake_up_interruptible(&dev->wait_buffer);
470
471 buf = hdpvr_get_next_buffer(dev);
472 }
473 }
474err:
475 if (!ret && !buf)
476 ret = -EAGAIN;
477 return ret;
478}
479
480static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
481{
d2ff3ec8 482 struct hdpvr_buffer *buf = NULL;
9aba42ef
JG
483 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
484 struct hdpvr_device *dev = fh->dev;
485 unsigned int mask = 0;
486
487 mutex_lock(&dev->io_mutex);
488
489 if (video_is_unregistered(dev->video_dev))
490 return -EIO;
491
492 if (dev->status == STATUS_IDLE) {
493 if (hdpvr_start_streaming(dev)) {
494 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
495 "start_streaming failed");
496 dev->status = STATUS_IDLE;
497 }
498
d211bfcb 499 print_buffer_status();
9aba42ef
JG
500 }
501 mutex_unlock(&dev->io_mutex);
502
d2ff3ec8
JG
503 buf = hdpvr_get_next_buffer(dev);
504 /* only wait if no data is available */
505 if (!buf || buf->status != BUFSTAT_READY) {
506 poll_wait(filp, &dev->wait_data, wait);
507 buf = hdpvr_get_next_buffer(dev);
9aba42ef 508 }
d2ff3ec8
JG
509 if (buf && buf->status == BUFSTAT_READY)
510 mask |= POLLIN | POLLRDNORM;
9aba42ef
JG
511
512 return mask;
513}
514
515
9aba42ef
JG
516static const struct v4l2_file_operations hdpvr_fops = {
517 .owner = THIS_MODULE,
518 .open = hdpvr_open,
519 .release = hdpvr_release,
520 .read = hdpvr_read,
521 .poll = hdpvr_poll,
76717b88 522 .unlocked_ioctl = video_ioctl2,
9aba42ef
JG
523};
524
525/*=======================================================================*/
526/*
527 * V4L2 ioctl handling
528 */
529
530static int vidioc_querycap(struct file *file, void *priv,
531 struct v4l2_capability *cap)
532{
533 struct hdpvr_device *dev = video_drvdata(file);
534
535 strcpy(cap->driver, "hdpvr");
536 strcpy(cap->card, "Haupauge HD PVR");
537 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
538 cap->version = HDPVR_VERSION;
539 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
540 V4L2_CAP_AUDIO |
541 V4L2_CAP_READWRITE;
542 return 0;
543}
544
545static int vidioc_s_std(struct file *file, void *private_data,
546 v4l2_std_id *std)
547{
548 struct hdpvr_fh *fh = file->private_data;
549 struct hdpvr_device *dev = fh->dev;
550 u8 std_type = 1;
551
552 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
553 std_type = 0;
554
555 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
556}
557
558static const char *iname[] = {
559 [HDPVR_COMPONENT] = "Component",
560 [HDPVR_SVIDEO] = "S-Video",
561 [HDPVR_COMPOSITE] = "Composite",
562};
563
564static int vidioc_enum_input(struct file *file, void *priv,
565 struct v4l2_input *i)
566{
567 struct hdpvr_fh *fh = file->private_data;
568 struct hdpvr_device *dev = fh->dev;
569 unsigned int n;
570
571 n = i->index;
572 if (n >= HDPVR_VIDEO_INPUTS)
573 return -EINVAL;
574
575 i->type = V4L2_INPUT_TYPE_CAMERA;
576
577 strncpy(i->name, iname[n], sizeof(i->name) - 1);
578 i->name[sizeof(i->name) - 1] = '\0';
579
580 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
581
582 i->std = dev->video_dev->tvnorms;
583
584 return 0;
585}
586
587static int vidioc_s_input(struct file *file, void *private_data,
588 unsigned int index)
589{
590 struct hdpvr_fh *fh = file->private_data;
591 struct hdpvr_device *dev = fh->dev;
592 int retval;
593
594 if (index >= HDPVR_VIDEO_INPUTS)
595 return -EINVAL;
596
597 if (dev->status != STATUS_IDLE)
598 return -EAGAIN;
599
600 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
601 if (!retval)
602 dev->options.video_input = index;
603
604 return retval;
605}
606
607static int vidioc_g_input(struct file *file, void *private_data,
608 unsigned int *index)
609{
610 struct hdpvr_fh *fh = file->private_data;
611 struct hdpvr_device *dev = fh->dev;
612
613 *index = dev->options.video_input;
614 return 0;
615}
616
617
618static const char *audio_iname[] = {
619 [HDPVR_RCA_FRONT] = "RCA front",
620 [HDPVR_RCA_BACK] = "RCA back",
621 [HDPVR_SPDIF] = "SPDIF",
622};
623
624static int vidioc_enumaudio(struct file *file, void *priv,
625 struct v4l2_audio *audio)
626{
627 unsigned int n;
628
629 n = audio->index;
630 if (n >= HDPVR_AUDIO_INPUTS)
631 return -EINVAL;
632
633 audio->capability = V4L2_AUDCAP_STEREO;
634
635 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
636 audio->name[sizeof(audio->name) - 1] = '\0';
637
638 return 0;
639}
640
641static int vidioc_s_audio(struct file *file, void *private_data,
642 struct v4l2_audio *audio)
643{
644 struct hdpvr_fh *fh = file->private_data;
645 struct hdpvr_device *dev = fh->dev;
646 int retval;
647
648 if (audio->index >= HDPVR_AUDIO_INPUTS)
649 return -EINVAL;
650
651 if (dev->status != STATUS_IDLE)
652 return -EAGAIN;
653
654 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
655 if (!retval)
656 dev->options.audio_input = audio->index;
657
658 return retval;
659}
660
661static int vidioc_g_audio(struct file *file, void *private_data,
662 struct v4l2_audio *audio)
663{
664 struct hdpvr_fh *fh = file->private_data;
665 struct hdpvr_device *dev = fh->dev;
666
667 audio->index = dev->options.audio_input;
668 audio->capability = V4L2_AUDCAP_STEREO;
669 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
670 audio->name[sizeof(audio->name) - 1] = '\0';
671 return 0;
672}
673
674static const s32 supported_v4l2_ctrls[] = {
675 V4L2_CID_BRIGHTNESS,
676 V4L2_CID_CONTRAST,
677 V4L2_CID_SATURATION,
678 V4L2_CID_HUE,
679 V4L2_CID_SHARPNESS,
680 V4L2_CID_MPEG_AUDIO_ENCODING,
681 V4L2_CID_MPEG_VIDEO_ENCODING,
682 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
683 V4L2_CID_MPEG_VIDEO_BITRATE,
684 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
685};
686
687static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
688 int ac3)
689{
690 int err;
691
692 switch (qc->id) {
693 case V4L2_CID_BRIGHTNESS:
694 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
695 case V4L2_CID_CONTRAST:
696 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
697 case V4L2_CID_SATURATION:
698 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
699 case V4L2_CID_HUE:
700 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
701 case V4L2_CID_SHARPNESS:
702 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
703 case V4L2_CID_MPEG_AUDIO_ENCODING:
704 return v4l2_ctrl_query_fill(
705 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
706 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
707 : V4L2_MPEG_AUDIO_ENCODING_AAC,
708 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
709 case V4L2_CID_MPEG_VIDEO_ENCODING:
710 return v4l2_ctrl_query_fill(
711 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
712 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
713 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
714
715/* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
716/* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
717 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
718 return v4l2_ctrl_query_fill(
719 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
720 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
721 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
722
723 case V4L2_CID_MPEG_VIDEO_BITRATE:
724 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
725 6500000);
726 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
727 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
728 9000000);
729 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
730 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
731 return err;
732 default:
733 return -EINVAL;
734 }
735}
736
737static int vidioc_queryctrl(struct file *file, void *private_data,
738 struct v4l2_queryctrl *qc)
739{
740 struct hdpvr_fh *fh = file->private_data;
741 struct hdpvr_device *dev = fh->dev;
742 int i, next;
743 u32 id = qc->id;
744
745 memset(qc, 0, sizeof(*qc));
746
747 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
748 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
749
750 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
751 if (next) {
752 if (qc->id < supported_v4l2_ctrls[i])
753 qc->id = supported_v4l2_ctrls[i];
754 else
755 continue;
756 }
757
758 if (qc->id == supported_v4l2_ctrls[i])
759 return fill_queryctrl(&dev->options, qc,
760 dev->flags & HDPVR_FLAG_AC3_CAP);
761
762 if (qc->id < supported_v4l2_ctrls[i])
763 break;
764 }
765
766 return -EINVAL;
767}
768
769static int vidioc_g_ctrl(struct file *file, void *private_data,
770 struct v4l2_control *ctrl)
771{
772 struct hdpvr_fh *fh = file->private_data;
773 struct hdpvr_device *dev = fh->dev;
774
775 switch (ctrl->id) {
776 case V4L2_CID_BRIGHTNESS:
777 ctrl->value = dev->options.brightness;
778 break;
779 case V4L2_CID_CONTRAST:
780 ctrl->value = dev->options.contrast;
781 break;
782 case V4L2_CID_SATURATION:
783 ctrl->value = dev->options.saturation;
784 break;
785 case V4L2_CID_HUE:
786 ctrl->value = dev->options.hue;
787 break;
788 case V4L2_CID_SHARPNESS:
789 ctrl->value = dev->options.sharpness;
790 break;
791 default:
792 return -EINVAL;
793 }
794 return 0;
795}
796
797static int vidioc_s_ctrl(struct file *file, void *private_data,
798 struct v4l2_control *ctrl)
799{
800 struct hdpvr_fh *fh = file->private_data;
801 struct hdpvr_device *dev = fh->dev;
802 int retval;
803
804 switch (ctrl->id) {
805 case V4L2_CID_BRIGHTNESS:
806 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
807 if (!retval)
808 dev->options.brightness = ctrl->value;
809 break;
810 case V4L2_CID_CONTRAST:
811 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
812 if (!retval)
813 dev->options.contrast = ctrl->value;
814 break;
815 case V4L2_CID_SATURATION:
816 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
817 if (!retval)
818 dev->options.saturation = ctrl->value;
819 break;
820 case V4L2_CID_HUE:
821 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
822 if (!retval)
823 dev->options.hue = ctrl->value;
824 break;
825 case V4L2_CID_SHARPNESS:
826 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
827 if (!retval)
828 dev->options.sharpness = ctrl->value;
829 break;
830 default:
831 return -EINVAL;
832 }
833
834 return retval;
835}
836
837
838static int hdpvr_get_ctrl(struct hdpvr_options *opt,
839 struct v4l2_ext_control *ctrl)
840{
841 switch (ctrl->id) {
842 case V4L2_CID_MPEG_AUDIO_ENCODING:
843 ctrl->value = opt->audio_codec;
844 break;
845 case V4L2_CID_MPEG_VIDEO_ENCODING:
846 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
847 break;
848/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
849/* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
850/* break; */
851 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
852 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
853 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
854 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
855 break;
856 case V4L2_CID_MPEG_VIDEO_BITRATE:
857 ctrl->value = opt->bitrate * 100000;
858 break;
859 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
860 ctrl->value = opt->peak_bitrate * 100000;
861 break;
862 case V4L2_CID_MPEG_STREAM_TYPE:
863 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
864 break;
865 default:
866 return -EINVAL;
867 }
868 return 0;
869}
870
871static int vidioc_g_ext_ctrls(struct file *file, void *priv,
872 struct v4l2_ext_controls *ctrls)
873{
874 struct hdpvr_fh *fh = file->private_data;
875 struct hdpvr_device *dev = fh->dev;
876 int i, err = 0;
877
878 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
879 for (i = 0; i < ctrls->count; i++) {
880 struct v4l2_ext_control *ctrl = ctrls->controls + i;
881
882 err = hdpvr_get_ctrl(&dev->options, ctrl);
883 if (err) {
884 ctrls->error_idx = i;
885 break;
886 }
887 }
888 return err;
889
890 }
891
892 return -EINVAL;
893}
894
895
896static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
897{
898 int ret = -EINVAL;
899
900 switch (ctrl->id) {
901 case V4L2_CID_MPEG_AUDIO_ENCODING:
902 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
903 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
904 ret = 0;
905 break;
906 case V4L2_CID_MPEG_VIDEO_ENCODING:
907 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
908 ret = 0;
909 break;
910/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
911/* if (ctrl->value == 0 || ctrl->value == 128) */
912/* ret = 0; */
913/* break; */
914 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
915 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
916 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
917 ret = 0;
918 break;
919 case V4L2_CID_MPEG_VIDEO_BITRATE:
920 {
921 uint bitrate = ctrl->value / 100000;
922 if (bitrate >= 10 && bitrate <= 135)
923 ret = 0;
924 break;
925 }
926 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
927 {
928 uint peak_bitrate = ctrl->value / 100000;
929 if (peak_bitrate >= 10 && peak_bitrate <= 202)
930 ret = 0;
931 break;
932 }
933 case V4L2_CID_MPEG_STREAM_TYPE:
934 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
935 ret = 0;
936 break;
937 default:
938 return -EINVAL;
939 }
940 return 0;
941}
942
943static int vidioc_try_ext_ctrls(struct file *file, void *priv,
944 struct v4l2_ext_controls *ctrls)
945{
946 struct hdpvr_fh *fh = file->private_data;
947 struct hdpvr_device *dev = fh->dev;
948 int i, err = 0;
949
950 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
951 for (i = 0; i < ctrls->count; i++) {
952 struct v4l2_ext_control *ctrl = ctrls->controls + i;
953
954 err = hdpvr_try_ctrl(ctrl,
955 dev->flags & HDPVR_FLAG_AC3_CAP);
956 if (err) {
957 ctrls->error_idx = i;
958 break;
959 }
960 }
961 return err;
962 }
963
964 return -EINVAL;
965}
966
967
968static int hdpvr_set_ctrl(struct hdpvr_device *dev,
969 struct v4l2_ext_control *ctrl)
970{
971 struct hdpvr_options *opt = &dev->options;
972 int ret = 0;
973
974 switch (ctrl->id) {
975 case V4L2_CID_MPEG_AUDIO_ENCODING:
976 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
977 opt->audio_codec = ctrl->value;
978 ret = hdpvr_set_audio(dev, opt->audio_input,
979 opt->audio_codec);
980 }
981 break;
982 case V4L2_CID_MPEG_VIDEO_ENCODING:
983 break;
984/* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
985/* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
986/* opt->gop_mode |= 0x2; */
987/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
988/* opt->gop_mode); */
989/* } */
990/* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
991/* opt->gop_mode &= ~0x2; */
992/* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
993/* opt->gop_mode); */
994/* } */
995/* break; */
996 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
997 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
998 opt->bitrate_mode != HDPVR_CONSTANT) {
999 opt->bitrate_mode = HDPVR_CONSTANT;
1000 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1001 opt->bitrate_mode);
1002 }
1003 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1004 opt->bitrate_mode == HDPVR_CONSTANT) {
1005 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1006 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1007 opt->bitrate_mode);
1008 }
1009 break;
1010 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1011 uint bitrate = ctrl->value / 100000;
1012
1013 opt->bitrate = bitrate;
1014 if (bitrate >= opt->peak_bitrate)
1015 opt->peak_bitrate = bitrate+1;
1016
1017 hdpvr_set_bitrate(dev);
1018 break;
1019 }
1020 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1021 uint peak_bitrate = ctrl->value / 100000;
1022
1023 if (opt->bitrate_mode == HDPVR_CONSTANT)
1024 break;
1025
1026 if (opt->bitrate < peak_bitrate) {
1027 opt->peak_bitrate = peak_bitrate;
1028 hdpvr_set_bitrate(dev);
1029 } else
1030 ret = -EINVAL;
1031 break;
1032 }
1033 case V4L2_CID_MPEG_STREAM_TYPE:
1034 break;
1035 default:
1036 return -EINVAL;
1037 }
1038 return ret;
1039}
1040
1041static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1042 struct v4l2_ext_controls *ctrls)
1043{
1044 struct hdpvr_fh *fh = file->private_data;
1045 struct hdpvr_device *dev = fh->dev;
1046 int i, err = 0;
1047
1048 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1049 for (i = 0; i < ctrls->count; i++) {
1050 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1051
1052 err = hdpvr_try_ctrl(ctrl,
1053 dev->flags & HDPVR_FLAG_AC3_CAP);
1054 if (err) {
1055 ctrls->error_idx = i;
1056 break;
1057 }
1058 err = hdpvr_set_ctrl(dev, ctrl);
1059 if (err) {
1060 ctrls->error_idx = i;
1061 break;
1062 }
1063 }
1064 return err;
1065
1066 }
1067
1068 return -EINVAL;
1069}
1070
1071static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1072 struct v4l2_fmtdesc *f)
1073{
1074
1075 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1076 return -EINVAL;
1077
1078 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1079 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1080 f->pixelformat = V4L2_PIX_FMT_MPEG;
1081
1082 return 0;
1083}
1084
1085static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1086 struct v4l2_format *f)
1087{
1088 struct hdpvr_fh *fh = file->private_data;
1089 struct hdpvr_device *dev = fh->dev;
1090 struct hdpvr_video_info *vid_info;
1091
1092 if (!dev)
1093 return -ENODEV;
1094
1095 vid_info = get_video_info(dev);
1096 if (!vid_info)
1097 return -EFAULT;
1098
1099 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1100 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1101 f->fmt.pix.width = vid_info->width;
1102 f->fmt.pix.height = vid_info->height;
1103 f->fmt.pix.sizeimage = dev->bulk_in_size;
1104 f->fmt.pix.colorspace = 0;
1105 f->fmt.pix.bytesperline = 0;
1106 f->fmt.pix.field = V4L2_FIELD_ANY;
1107
1108 kfree(vid_info);
1109 return 0;
1110}
1111
76717b88
JG
1112static int vidioc_encoder_cmd(struct file *filp, void *priv,
1113 struct v4l2_encoder_cmd *a)
1114{
1115 struct hdpvr_fh *fh = filp->private_data;
1116 struct hdpvr_device *dev = fh->dev;
1117 int res;
1118
1119 mutex_lock(&dev->io_mutex);
1120
1121 memset(&a->raw, 0, sizeof(a->raw));
1122 switch (a->cmd) {
1123 case V4L2_ENC_CMD_START:
1124 a->flags = 0;
1125 res = hdpvr_start_streaming(dev);
1126 break;
1127 case V4L2_ENC_CMD_STOP:
1128 res = hdpvr_stop_streaming(dev);
1129 break;
1130 default:
1131 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
1132 "Unsupported encoder cmd %d\n", a->cmd);
1133 return -EINVAL;
1134 }
1135 mutex_unlock(&dev->io_mutex);
1136 return res;
1137}
1138
1139static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1140 struct v4l2_encoder_cmd *a)
1141{
1142 switch (a->cmd) {
1143 case V4L2_ENC_CMD_START:
1144 case V4L2_ENC_CMD_STOP:
1145 return 0;
1146 default:
1147 return -EINVAL;
1148 }
1149}
9aba42ef
JG
1150
1151static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1152 .vidioc_querycap = vidioc_querycap,
1153 .vidioc_s_std = vidioc_s_std,
1154 .vidioc_enum_input = vidioc_enum_input,
1155 .vidioc_g_input = vidioc_g_input,
1156 .vidioc_s_input = vidioc_s_input,
1157 .vidioc_enumaudio = vidioc_enumaudio,
1158 .vidioc_g_audio = vidioc_g_audio,
1159 .vidioc_s_audio = vidioc_s_audio,
1160 .vidioc_queryctrl = vidioc_queryctrl,
1161 .vidioc_g_ctrl = vidioc_g_ctrl,
1162 .vidioc_s_ctrl = vidioc_s_ctrl,
1163 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1164 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1165 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1166 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1167 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
76717b88
JG
1168 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1169 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
9aba42ef
JG
1170};
1171
1172static void hdpvr_device_release(struct video_device *vdev)
1173{
1174 struct hdpvr_device *dev = video_get_drvdata(vdev);
1175
1176 hdpvr_delete(dev);
1177}
1178
1179static const struct video_device hdpvr_video_template = {
1180/* .type = VFL_TYPE_GRABBER, */
1181/* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1182 .fops = &hdpvr_fops,
1183 .release = hdpvr_device_release,
1184 .ioctl_ops = &hdpvr_ioctl_ops,
1185 .tvnorms =
1186 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1187 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1188 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1189 V4L2_STD_PAL_60,
1190};
1191
a50ab291
JG
1192int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1193 int devnum)
9aba42ef
JG
1194{
1195 /* setup and register video device */
1196 dev->video_dev = video_device_alloc();
1197 if (!dev->video_dev) {
1198 err("video_device_alloc() failed");
1199 goto error;
1200 }
1201
1202 *(dev->video_dev) = hdpvr_video_template;
1203 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
a50ab291 1204 dev->video_dev->parent = parent;
9aba42ef
JG
1205 video_set_drvdata(dev->video_dev, dev);
1206
1207 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1208 err("V4L2 device registration failed");
1209 goto error;
1210 }
1211
1212 return 0;
1213error:
1214 return -ENOMEM;
1215}
This page took 0.074982 seconds and 5 git commands to generate.