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