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