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