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