Commit | Line | Data |
---|---|---|
8b2f0795 DH |
1 | /* |
2 | * Auvitek AU0828 USB Bridge (Analog video support) | |
3 | * | |
4 | * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org> | |
5 | * Copyright (C) 2005-2008 Auvitek International, Ltd. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * As published by the Free Software Foundation; either version 2 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | * 02110-1301, USA. | |
21 | */ | |
22 | ||
23 | /* Developer Notes: | |
24 | * | |
8b2f0795 DH |
25 | * The hardware scaler supported is unimplemented |
26 | * AC97 audio support is unimplemented (only i2s audio mode) | |
27 | * | |
28 | */ | |
29 | ||
83afb32a | 30 | #include "au0828.h" |
50512333 | 31 | #include "au8522.h" |
83afb32a | 32 | |
8b2f0795 | 33 | #include <linux/module.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
8b2f0795 DH |
35 | #include <linux/init.h> |
36 | #include <linux/device.h> | |
8b2f0795 | 37 | #include <media/v4l2-common.h> |
9822f417 | 38 | #include <media/v4l2-mc.h> |
8b2f0795 | 39 | #include <media/v4l2-ioctl.h> |
83b09422 | 40 | #include <media/v4l2-event.h> |
8b2f0795 | 41 | #include <media/tuner.h> |
8b2f0795 DH |
42 | #include "au0828-reg.h" |
43 | ||
8b2f0795 DH |
44 | static DEFINE_MUTEX(au0828_sysfs_lock); |
45 | ||
8b2f0795 DH |
46 | /* ------------------------------------------------------------------ |
47 | Videobuf operations | |
48 | ------------------------------------------------------------------*/ | |
49 | ||
50 | static unsigned int isoc_debug; | |
51 | module_param(isoc_debug, int, 0644); | |
52 | MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); | |
53 | ||
54 | #define au0828_isocdbg(fmt, arg...) \ | |
55 | do {\ | |
56 | if (isoc_debug) { \ | |
83afb32a | 57 | pr_info("au0828 %s :"fmt, \ |
8b2f0795 DH |
58 | __func__ , ##arg); \ |
59 | } \ | |
60 | } while (0) | |
61 | ||
fa09cb9a HV |
62 | static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val) |
63 | { | |
64 | if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl) | |
65 | dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val); | |
66 | } | |
67 | ||
8b2f0795 DH |
68 | static inline void print_err_status(struct au0828_dev *dev, |
69 | int packet, int status) | |
70 | { | |
71 | char *errmsg = "Unknown"; | |
72 | ||
73 | switch (status) { | |
74 | case -ENOENT: | |
75 | errmsg = "unlinked synchronuously"; | |
76 | break; | |
77 | case -ECONNRESET: | |
78 | errmsg = "unlinked asynchronuously"; | |
79 | break; | |
80 | case -ENOSR: | |
81 | errmsg = "Buffer error (overrun)"; | |
82 | break; | |
83 | case -EPIPE: | |
84 | errmsg = "Stalled (device not responding)"; | |
85 | break; | |
86 | case -EOVERFLOW: | |
87 | errmsg = "Babble (bad cable?)"; | |
88 | break; | |
89 | case -EPROTO: | |
90 | errmsg = "Bit-stuff error (bad cable?)"; | |
91 | break; | |
92 | case -EILSEQ: | |
93 | errmsg = "CRC/Timeout (could be anything)"; | |
94 | break; | |
95 | case -ETIME: | |
96 | errmsg = "Device does not respond"; | |
97 | break; | |
98 | } | |
99 | if (packet < 0) { | |
100 | au0828_isocdbg("URB status %d [%s].\n", status, errmsg); | |
101 | } else { | |
102 | au0828_isocdbg("URB packet %d, status %d [%s].\n", | |
103 | packet, status, errmsg); | |
104 | } | |
105 | } | |
106 | ||
107 | static int check_dev(struct au0828_dev *dev) | |
108 | { | |
109 | if (dev->dev_state & DEV_DISCONNECTED) { | |
83afb32a | 110 | pr_info("v4l2 ioctl: device not present\n"); |
8b2f0795 DH |
111 | return -ENODEV; |
112 | } | |
113 | ||
114 | if (dev->dev_state & DEV_MISCONFIGURED) { | |
83afb32a | 115 | pr_info("v4l2 ioctl: device is misconfigured; " |
8b2f0795 DH |
116 | "close and open it again\n"); |
117 | return -EIO; | |
118 | } | |
119 | return 0; | |
120 | } | |
121 | ||
122 | /* | |
123 | * IRQ callback, called by URB callback | |
124 | */ | |
125 | static void au0828_irq_callback(struct urb *urb) | |
126 | { | |
127 | struct au0828_dmaqueue *dma_q = urb->context; | |
128 | struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq); | |
78ca5005 | 129 | unsigned long flags = 0; |
e92ba283 | 130 | int i; |
8b2f0795 DH |
131 | |
132 | switch (urb->status) { | |
133 | case 0: /* success */ | |
134 | case -ETIMEDOUT: /* NAK */ | |
135 | break; | |
136 | case -ECONNRESET: /* kill */ | |
137 | case -ENOENT: | |
138 | case -ESHUTDOWN: | |
139 | au0828_isocdbg("au0828_irq_callback called: status kill\n"); | |
140 | return; | |
141 | default: /* unknown error */ | |
142 | au0828_isocdbg("urb completition error %d.\n", urb->status); | |
143 | break; | |
144 | } | |
145 | ||
146 | /* Copy data from URB */ | |
78ca5005 | 147 | spin_lock_irqsave(&dev->slock, flags); |
e92ba283 | 148 | dev->isoc_ctl.isoc_copy(dev, urb); |
78ca5005 | 149 | spin_unlock_irqrestore(&dev->slock, flags); |
8b2f0795 DH |
150 | |
151 | /* Reset urb buffers */ | |
152 | for (i = 0; i < urb->number_of_packets; i++) { | |
153 | urb->iso_frame_desc[i].status = 0; | |
154 | urb->iso_frame_desc[i].actual_length = 0; | |
155 | } | |
156 | urb->status = 0; | |
157 | ||
158 | urb->status = usb_submit_urb(urb, GFP_ATOMIC); | |
159 | if (urb->status) { | |
160 | au0828_isocdbg("urb resubmit failed (error=%i)\n", | |
161 | urb->status); | |
162 | } | |
e2147d0a | 163 | dev->stream_state = STREAM_ON; |
8b2f0795 DH |
164 | } |
165 | ||
166 | /* | |
167 | * Stop and Deallocate URBs | |
168 | */ | |
a094ca46 | 169 | static void au0828_uninit_isoc(struct au0828_dev *dev) |
8b2f0795 DH |
170 | { |
171 | struct urb *urb; | |
172 | int i; | |
173 | ||
174 | au0828_isocdbg("au0828: called au0828_uninit_isoc\n"); | |
175 | ||
176 | dev->isoc_ctl.nfields = -1; | |
177 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
178 | urb = dev->isoc_ctl.urb[i]; | |
179 | if (urb) { | |
180 | if (!irqs_disabled()) | |
181 | usb_kill_urb(urb); | |
182 | else | |
183 | usb_unlink_urb(urb); | |
184 | ||
185 | if (dev->isoc_ctl.transfer_buffer[i]) { | |
997ea58e | 186 | usb_free_coherent(dev->usbdev, |
8b2f0795 DH |
187 | urb->transfer_buffer_length, |
188 | dev->isoc_ctl.transfer_buffer[i], | |
189 | urb->transfer_dma); | |
190 | } | |
191 | usb_free_urb(urb); | |
192 | dev->isoc_ctl.urb[i] = NULL; | |
193 | } | |
194 | dev->isoc_ctl.transfer_buffer[i] = NULL; | |
195 | } | |
196 | ||
197 | kfree(dev->isoc_ctl.urb); | |
198 | kfree(dev->isoc_ctl.transfer_buffer); | |
199 | ||
200 | dev->isoc_ctl.urb = NULL; | |
201 | dev->isoc_ctl.transfer_buffer = NULL; | |
202 | dev->isoc_ctl.num_bufs = 0; | |
e2147d0a MCC |
203 | |
204 | dev->stream_state = STREAM_OFF; | |
8b2f0795 DH |
205 | } |
206 | ||
207 | /* | |
208 | * Allocate URBs and start IRQ | |
209 | */ | |
a094ca46 MCC |
210 | static int au0828_init_isoc(struct au0828_dev *dev, int max_packets, |
211 | int num_bufs, int max_pkt_size, | |
212 | int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb)) | |
8b2f0795 DH |
213 | { |
214 | struct au0828_dmaqueue *dma_q = &dev->vidq; | |
215 | int i; | |
216 | int sb_size, pipe; | |
217 | struct urb *urb; | |
218 | int j, k; | |
219 | int rc; | |
220 | ||
221 | au0828_isocdbg("au0828: called au0828_prepare_isoc\n"); | |
222 | ||
8b2f0795 DH |
223 | dev->isoc_ctl.isoc_copy = isoc_copy; |
224 | dev->isoc_ctl.num_bufs = num_bufs; | |
225 | ||
226 | dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL); | |
227 | if (!dev->isoc_ctl.urb) { | |
228 | au0828_isocdbg("cannot alloc memory for usb buffers\n"); | |
229 | return -ENOMEM; | |
230 | } | |
231 | ||
232 | dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs, | |
233 | GFP_KERNEL); | |
234 | if (!dev->isoc_ctl.transfer_buffer) { | |
235 | au0828_isocdbg("cannot allocate memory for usb transfer\n"); | |
236 | kfree(dev->isoc_ctl.urb); | |
237 | return -ENOMEM; | |
238 | } | |
239 | ||
240 | dev->isoc_ctl.max_pkt_size = max_pkt_size; | |
241 | dev->isoc_ctl.buf = NULL; | |
242 | ||
243 | sb_size = max_packets * dev->isoc_ctl.max_pkt_size; | |
244 | ||
245 | /* allocate urbs and transfer buffers */ | |
246 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
247 | urb = usb_alloc_urb(max_packets, GFP_KERNEL); | |
248 | if (!urb) { | |
249 | au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i); | |
250 | au0828_uninit_isoc(dev); | |
251 | return -ENOMEM; | |
252 | } | |
253 | dev->isoc_ctl.urb[i] = urb; | |
254 | ||
997ea58e | 255 | dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev, |
8b2f0795 DH |
256 | sb_size, GFP_KERNEL, &urb->transfer_dma); |
257 | if (!dev->isoc_ctl.transfer_buffer[i]) { | |
258 | printk("unable to allocate %i bytes for transfer" | |
259 | " buffer %i%s\n", | |
260 | sb_size, i, | |
261 | in_interrupt() ? " while in int" : ""); | |
262 | au0828_uninit_isoc(dev); | |
263 | return -ENOMEM; | |
264 | } | |
265 | memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size); | |
266 | ||
267 | pipe = usb_rcvisocpipe(dev->usbdev, | |
268 | dev->isoc_in_endpointaddr), | |
269 | ||
270 | usb_fill_int_urb(urb, dev->usbdev, pipe, | |
271 | dev->isoc_ctl.transfer_buffer[i], sb_size, | |
272 | au0828_irq_callback, dma_q, 1); | |
273 | ||
274 | urb->number_of_packets = max_packets; | |
fadadb7d | 275 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
8b2f0795 DH |
276 | |
277 | k = 0; | |
278 | for (j = 0; j < max_packets; j++) { | |
279 | urb->iso_frame_desc[j].offset = k; | |
280 | urb->iso_frame_desc[j].length = | |
281 | dev->isoc_ctl.max_pkt_size; | |
282 | k += dev->isoc_ctl.max_pkt_size; | |
283 | } | |
284 | } | |
285 | ||
8b2f0795 DH |
286 | /* submit urbs and enables IRQ */ |
287 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
288 | rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); | |
289 | if (rc) { | |
290 | au0828_isocdbg("submit of urb %i failed (error=%i)\n", | |
291 | i, rc); | |
292 | au0828_uninit_isoc(dev); | |
293 | return rc; | |
294 | } | |
295 | } | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | /* | |
301 | * Announces that a buffer were filled and request the next | |
302 | */ | |
303 | static inline void buffer_filled(struct au0828_dev *dev, | |
c5036d61 LP |
304 | struct au0828_dmaqueue *dma_q, |
305 | struct au0828_buffer *buf) | |
8b2f0795 | 306 | { |
2d700715 JS |
307 | struct vb2_v4l2_buffer *vb = &buf->vb; |
308 | struct vb2_queue *q = vb->vb2_buf.vb2_queue; | |
8b2f0795 | 309 | |
7f8eacd2 | 310 | /* Advice that buffer was filled */ |
05439b1a | 311 | au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field); |
7f8eacd2 | 312 | |
c5036d61 | 313 | if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
2d700715 | 314 | vb->sequence = dev->frame_count++; |
c5036d61 | 315 | else |
2d700715 | 316 | vb->sequence = dev->vbi_frame_count++; |
c5036d61 | 317 | |
2d700715 | 318 | vb->field = V4L2_FIELD_INTERLACED; |
d6dd645e | 319 | vb->vb2_buf.timestamp = ktime_get_ns(); |
2d700715 | 320 | vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE); |
7f8eacd2 DH |
321 | } |
322 | ||
8b2f0795 DH |
323 | /* |
324 | * Identify the buffer header type and properly handles | |
325 | */ | |
326 | static void au0828_copy_video(struct au0828_dev *dev, | |
327 | struct au0828_dmaqueue *dma_q, | |
328 | struct au0828_buffer *buf, | |
329 | unsigned char *p, | |
330 | unsigned char *outp, unsigned long len) | |
331 | { | |
332 | void *fieldstart, *startwrite, *startread; | |
333 | int linesdone, currlinedone, offset, lencopy, remain; | |
334 | int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */ | |
335 | ||
7f8eacd2 DH |
336 | if (len == 0) |
337 | return; | |
338 | ||
05439b1a SK |
339 | if (dma_q->pos + len > buf->length) |
340 | len = buf->length - dma_q->pos; | |
8b2f0795 DH |
341 | |
342 | startread = p; | |
343 | remain = len; | |
344 | ||
345 | /* Interlaces frame */ | |
346 | if (buf->top_field) | |
347 | fieldstart = outp; | |
348 | else | |
349 | fieldstart = outp + bytesperline; | |
350 | ||
351 | linesdone = dma_q->pos / bytesperline; | |
352 | currlinedone = dma_q->pos % bytesperline; | |
353 | offset = linesdone * bytesperline * 2 + currlinedone; | |
354 | startwrite = fieldstart + offset; | |
355 | lencopy = bytesperline - currlinedone; | |
356 | lencopy = lencopy > remain ? remain : lencopy; | |
357 | ||
05439b1a | 358 | if ((char *)startwrite + lencopy > (char *)outp + buf->length) { |
8b2f0795 DH |
359 | au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n", |
360 | ((char *)startwrite + lencopy) - | |
05439b1a SK |
361 | ((char *)outp + buf->length)); |
362 | remain = (char *)outp + buf->length - (char *)startwrite; | |
8b2f0795 DH |
363 | lencopy = remain; |
364 | } | |
365 | if (lencopy <= 0) | |
366 | return; | |
367 | memcpy(startwrite, startread, lencopy); | |
368 | ||
369 | remain -= lencopy; | |
370 | ||
371 | while (remain > 0) { | |
372 | startwrite += lencopy + bytesperline; | |
373 | startread += lencopy; | |
374 | if (bytesperline > remain) | |
375 | lencopy = remain; | |
376 | else | |
377 | lencopy = bytesperline; | |
378 | ||
379 | if ((char *)startwrite + lencopy > (char *)outp + | |
05439b1a | 380 | buf->length) { |
62899a28 | 381 | au0828_isocdbg("Overflow %zi bytes past buf end (2)\n", |
8b2f0795 | 382 | ((char *)startwrite + lencopy) - |
05439b1a SK |
383 | ((char *)outp + buf->length)); |
384 | lencopy = remain = (char *)outp + buf->length - | |
8b2f0795 DH |
385 | (char *)startwrite; |
386 | } | |
387 | if (lencopy <= 0) | |
388 | break; | |
389 | ||
390 | memcpy(startwrite, startread, lencopy); | |
391 | ||
392 | remain -= lencopy; | |
393 | } | |
394 | ||
395 | if (offset > 1440) { | |
396 | /* We have enough data to check for greenscreen */ | |
62899a28 | 397 | if (outp[0] < 0x60 && outp[1440] < 0x60) |
8b2f0795 | 398 | dev->greenscreen_detected = 1; |
8b2f0795 DH |
399 | } |
400 | ||
401 | dma_q->pos += len; | |
402 | } | |
403 | ||
404 | /* | |
405 | * video-buf generic routine to get the next available buffer | |
406 | */ | |
407 | static inline void get_next_buf(struct au0828_dmaqueue *dma_q, | |
408 | struct au0828_buffer **buf) | |
409 | { | |
410 | struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq); | |
411 | ||
412 | if (list_empty(&dma_q->active)) { | |
413 | au0828_isocdbg("No active queue to serve\n"); | |
414 | dev->isoc_ctl.buf = NULL; | |
415 | *buf = NULL; | |
416 | return; | |
417 | } | |
418 | ||
419 | /* Get the next buffer */ | |
05439b1a SK |
420 | *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); |
421 | /* Cleans up buffer - Useful for testing for frame/URB loss */ | |
422 | list_del(&(*buf)->list); | |
423 | dma_q->pos = 0; | |
424 | (*buf)->vb_buf = (*buf)->mem; | |
8b2f0795 DH |
425 | dev->isoc_ctl.buf = *buf; |
426 | ||
427 | return; | |
428 | } | |
429 | ||
7f8eacd2 DH |
430 | static void au0828_copy_vbi(struct au0828_dev *dev, |
431 | struct au0828_dmaqueue *dma_q, | |
432 | struct au0828_buffer *buf, | |
433 | unsigned char *p, | |
434 | unsigned char *outp, unsigned long len) | |
435 | { | |
436 | unsigned char *startwrite, *startread; | |
b5f5933a | 437 | int bytesperline; |
7f8eacd2 DH |
438 | int i, j = 0; |
439 | ||
440 | if (dev == NULL) { | |
441 | au0828_isocdbg("dev is null\n"); | |
442 | return; | |
443 | } | |
444 | ||
445 | if (dma_q == NULL) { | |
446 | au0828_isocdbg("dma_q is null\n"); | |
447 | return; | |
448 | } | |
449 | if (buf == NULL) | |
450 | return; | |
451 | if (p == NULL) { | |
452 | au0828_isocdbg("p is null\n"); | |
453 | return; | |
454 | } | |
455 | if (outp == NULL) { | |
456 | au0828_isocdbg("outp is null\n"); | |
457 | return; | |
458 | } | |
459 | ||
b5f5933a DC |
460 | bytesperline = dev->vbi_width; |
461 | ||
05439b1a SK |
462 | if (dma_q->pos + len > buf->length) |
463 | len = buf->length - dma_q->pos; | |
7f8eacd2 DH |
464 | |
465 | startread = p; | |
466 | startwrite = outp + (dma_q->pos / 2); | |
467 | ||
468 | /* Make sure the bottom field populates the second half of the frame */ | |
469 | if (buf->top_field == 0) | |
470 | startwrite += bytesperline * dev->vbi_height; | |
471 | ||
472 | for (i = 0; i < len; i += 2) | |
473 | startwrite[j++] = startread[i+1]; | |
474 | ||
475 | dma_q->pos += len; | |
476 | } | |
477 | ||
478 | ||
479 | /* | |
480 | * video-buf generic routine to get the next available VBI buffer | |
481 | */ | |
482 | static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q, | |
483 | struct au0828_buffer **buf) | |
484 | { | |
485 | struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq); | |
7f8eacd2 DH |
486 | |
487 | if (list_empty(&dma_q->active)) { | |
488 | au0828_isocdbg("No active queue to serve\n"); | |
489 | dev->isoc_ctl.vbi_buf = NULL; | |
490 | *buf = NULL; | |
491 | return; | |
492 | } | |
493 | ||
494 | /* Get the next buffer */ | |
05439b1a | 495 | *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); |
25985edc | 496 | /* Cleans up buffer - Useful for testing for frame/URB loss */ |
05439b1a SK |
497 | list_del(&(*buf)->list); |
498 | dma_q->pos = 0; | |
499 | (*buf)->vb_buf = (*buf)->mem; | |
7f8eacd2 | 500 | dev->isoc_ctl.vbi_buf = *buf; |
7f8eacd2 DH |
501 | return; |
502 | } | |
503 | ||
8b2f0795 DH |
504 | /* |
505 | * Controls the isoc copy of each urb packet | |
506 | */ | |
507 | static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb) | |
508 | { | |
509 | struct au0828_buffer *buf; | |
7f8eacd2 | 510 | struct au0828_buffer *vbi_buf; |
8b2f0795 | 511 | struct au0828_dmaqueue *dma_q = urb->context; |
7f8eacd2 | 512 | struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq; |
8b2f0795 | 513 | unsigned char *outp = NULL; |
7f8eacd2 | 514 | unsigned char *vbioutp = NULL; |
8b2f0795 DH |
515 | int i, len = 0, rc = 1; |
516 | unsigned char *p; | |
517 | unsigned char fbyte; | |
7f8eacd2 DH |
518 | unsigned int vbi_field_size; |
519 | unsigned int remain, lencopy; | |
8b2f0795 DH |
520 | |
521 | if (!dev) | |
522 | return 0; | |
523 | ||
524 | if ((dev->dev_state & DEV_DISCONNECTED) || | |
525 | (dev->dev_state & DEV_MISCONFIGURED)) | |
526 | return 0; | |
527 | ||
528 | if (urb->status < 0) { | |
529 | print_err_status(dev, -1, urb->status); | |
530 | if (urb->status == -ENOENT) | |
531 | return 0; | |
532 | } | |
533 | ||
534 | buf = dev->isoc_ctl.buf; | |
535 | if (buf != NULL) | |
2d700715 | 536 | outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
8b2f0795 | 537 | |
7f8eacd2 DH |
538 | vbi_buf = dev->isoc_ctl.vbi_buf; |
539 | if (vbi_buf != NULL) | |
2d700715 | 540 | vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0); |
7f8eacd2 | 541 | |
8b2f0795 DH |
542 | for (i = 0; i < urb->number_of_packets; i++) { |
543 | int status = urb->iso_frame_desc[i].status; | |
544 | ||
545 | if (status < 0) { | |
546 | print_err_status(dev, i, status); | |
547 | if (urb->iso_frame_desc[i].status != -EPROTO) | |
548 | continue; | |
549 | } | |
550 | ||
62899a28 | 551 | if (urb->iso_frame_desc[i].actual_length <= 0) |
8b2f0795 | 552 | continue; |
62899a28 | 553 | |
8b2f0795 DH |
554 | if (urb->iso_frame_desc[i].actual_length > |
555 | dev->max_pkt_size) { | |
556 | au0828_isocdbg("packet bigger than packet size"); | |
557 | continue; | |
558 | } | |
559 | ||
560 | p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
561 | fbyte = p[0]; | |
562 | len = urb->iso_frame_desc[i].actual_length - 4; | |
563 | p += 4; | |
564 | ||
565 | if (fbyte & 0x80) { | |
566 | len -= 4; | |
567 | p += 4; | |
568 | au0828_isocdbg("Video frame %s\n", | |
569 | (fbyte & 0x40) ? "odd" : "even"); | |
bde3bb9a | 570 | if (fbyte & 0x40) { |
7f8eacd2 DH |
571 | /* VBI */ |
572 | if (vbi_buf != NULL) | |
c5036d61 | 573 | buffer_filled(dev, vbi_dma_q, vbi_buf); |
7f8eacd2 DH |
574 | vbi_get_next_buf(vbi_dma_q, &vbi_buf); |
575 | if (vbi_buf == NULL) | |
576 | vbioutp = NULL; | |
577 | else | |
05439b1a | 578 | vbioutp = vb2_plane_vaddr( |
2d700715 | 579 | &vbi_buf->vb.vb2_buf, 0); |
7f8eacd2 DH |
580 | |
581 | /* Video */ | |
8b2f0795 DH |
582 | if (buf != NULL) |
583 | buffer_filled(dev, dma_q, buf); | |
584 | get_next_buf(dma_q, &buf); | |
62899a28 | 585 | if (buf == NULL) |
8b2f0795 | 586 | outp = NULL; |
62899a28 | 587 | else |
2d700715 JS |
588 | outp = vb2_plane_vaddr( |
589 | &buf->vb.vb2_buf, 0); | |
78ca5005 DH |
590 | |
591 | /* As long as isoc traffic is arriving, keep | |
592 | resetting the timer */ | |
593 | if (dev->vid_timeout_running) | |
594 | mod_timer(&dev->vid_timeout, | |
595 | jiffies + (HZ / 10)); | |
596 | if (dev->vbi_timeout_running) | |
597 | mod_timer(&dev->vbi_timeout, | |
598 | jiffies + (HZ / 10)); | |
8b2f0795 DH |
599 | } |
600 | ||
601 | if (buf != NULL) { | |
62899a28 | 602 | if (fbyte & 0x40) |
8b2f0795 | 603 | buf->top_field = 1; |
62899a28 | 604 | else |
8b2f0795 | 605 | buf->top_field = 0; |
8b2f0795 DH |
606 | } |
607 | ||
7f8eacd2 DH |
608 | if (vbi_buf != NULL) { |
609 | if (fbyte & 0x40) | |
610 | vbi_buf->top_field = 1; | |
611 | else | |
612 | vbi_buf->top_field = 0; | |
613 | } | |
614 | ||
615 | dev->vbi_read = 0; | |
616 | vbi_dma_q->pos = 0; | |
8b2f0795 DH |
617 | dma_q->pos = 0; |
618 | } | |
7f8eacd2 DH |
619 | |
620 | vbi_field_size = dev->vbi_width * dev->vbi_height * 2; | |
621 | if (dev->vbi_read < vbi_field_size) { | |
622 | remain = vbi_field_size - dev->vbi_read; | |
623 | if (len < remain) | |
624 | lencopy = len; | |
625 | else | |
626 | lencopy = remain; | |
627 | ||
628 | if (vbi_buf != NULL) | |
629 | au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p, | |
630 | vbioutp, len); | |
631 | ||
632 | len -= lencopy; | |
633 | p += lencopy; | |
634 | dev->vbi_read += lencopy; | |
635 | } | |
636 | ||
637 | if (dev->vbi_read >= vbi_field_size && buf != NULL) | |
8b2f0795 | 638 | au0828_copy_video(dev, dma_q, buf, p, outp, len); |
8b2f0795 DH |
639 | } |
640 | return rc; | |
641 | } | |
642 | ||
7b606ffd MCC |
643 | void au0828_usb_v4l2_media_release(struct au0828_dev *dev) |
644 | { | |
645 | #ifdef CONFIG_MEDIA_CONTROLLER | |
646 | int i; | |
647 | ||
648 | for (i = 0; i < AU0828_MAX_INPUT; i++) { | |
649 | if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED) | |
650 | return; | |
651 | media_device_unregister_entity(&dev->input_ent[i]); | |
652 | } | |
653 | #endif | |
654 | } | |
655 | ||
7b606ffd MCC |
656 | static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev) |
657 | { | |
658 | struct au0828_dev *dev = | |
659 | container_of(v4l2_dev, struct au0828_dev, v4l2_dev); | |
660 | ||
661 | v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl); | |
662 | v4l2_device_unregister(&dev->v4l2_dev); | |
663 | au0828_usb_v4l2_media_release(dev); | |
664 | au0828_usb_release(dev); | |
665 | } | |
666 | ||
667 | int au0828_v4l2_device_register(struct usb_interface *interface, | |
668 | struct au0828_dev *dev) | |
669 | { | |
670 | int retval; | |
671 | ||
672 | if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) | |
673 | return 0; | |
674 | ||
675 | /* Create the v4l2_device */ | |
676 | #ifdef CONFIG_MEDIA_CONTROLLER | |
677 | dev->v4l2_dev.mdev = dev->media_dev; | |
678 | #endif | |
679 | retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev); | |
680 | if (retval) { | |
681 | pr_err("%s() v4l2_device_register failed\n", | |
682 | __func__); | |
683 | mutex_unlock(&dev->lock); | |
684 | kfree(dev); | |
685 | return retval; | |
686 | } | |
687 | ||
688 | dev->v4l2_dev.release = au0828_usb_v4l2_release; | |
689 | ||
690 | /* This control handler will inherit the controls from au8522 */ | |
691 | retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4); | |
692 | if (retval) { | |
693 | pr_err("%s() v4l2_ctrl_handler_init failed\n", | |
694 | __func__); | |
695 | mutex_unlock(&dev->lock); | |
696 | kfree(dev); | |
697 | return retval; | |
698 | } | |
699 | dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl; | |
700 | ||
701 | return 0; | |
702 | } | |
703 | ||
df9ecb0c | 704 | static int queue_setup(struct vb2_queue *vq, |
05439b1a SK |
705 | unsigned int *nbuffers, unsigned int *nplanes, |
706 | unsigned int sizes[], void *alloc_ctxs[]) | |
8b2f0795 | 707 | { |
05439b1a | 708 | struct au0828_dev *dev = vb2_get_drv_priv(vq); |
df9ecb0c | 709 | unsigned long size = dev->height * dev->bytesperline; |
8b2f0795 | 710 | |
df9ecb0c HV |
711 | if (*nplanes) |
712 | return sizes[0] < size ? -EINVAL : 0; | |
05439b1a SK |
713 | *nplanes = 1; |
714 | sizes[0] = size; | |
05439b1a | 715 | return 0; |
8b2f0795 DH |
716 | } |
717 | ||
718 | static int | |
05439b1a | 719 | buffer_prepare(struct vb2_buffer *vb) |
8b2f0795 | 720 | { |
2d700715 JS |
721 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
722 | struct au0828_buffer *buf = container_of(vbuf, | |
723 | struct au0828_buffer, vb); | |
05439b1a | 724 | struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
8b2f0795 | 725 | |
05439b1a | 726 | buf->length = dev->height * dev->bytesperline; |
8b2f0795 | 727 | |
05439b1a SK |
728 | if (vb2_plane_size(vb, 0) < buf->length) { |
729 | pr_err("%s data will not fit into plane (%lu < %lu)\n", | |
730 | __func__, vb2_plane_size(vb, 0), buf->length); | |
8b2f0795 | 731 | return -EINVAL; |
8b2f0795 | 732 | } |
2d700715 | 733 | vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length); |
8b2f0795 | 734 | return 0; |
8b2f0795 DH |
735 | } |
736 | ||
737 | static void | |
05439b1a | 738 | buffer_queue(struct vb2_buffer *vb) |
8b2f0795 | 739 | { |
2d700715 JS |
740 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
741 | struct au0828_buffer *buf = container_of(vbuf, | |
8b2f0795 DH |
742 | struct au0828_buffer, |
743 | vb); | |
05439b1a | 744 | struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
8b2f0795 | 745 | struct au0828_dmaqueue *vidq = &dev->vidq; |
05439b1a | 746 | unsigned long flags = 0; |
8b2f0795 | 747 | |
05439b1a SK |
748 | buf->mem = vb2_plane_vaddr(vb, 0); |
749 | buf->length = vb2_plane_size(vb, 0); | |
8b2f0795 | 750 | |
05439b1a SK |
751 | spin_lock_irqsave(&dev->slock, flags); |
752 | list_add_tail(&buf->list, &vidq->active); | |
753 | spin_unlock_irqrestore(&dev->slock, flags); | |
8b2f0795 DH |
754 | } |
755 | ||
8b2f0795 DH |
756 | static int au0828_i2s_init(struct au0828_dev *dev) |
757 | { | |
758 | /* Enable i2s mode */ | |
759 | au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01); | |
760 | return 0; | |
761 | } | |
762 | ||
763 | /* | |
764 | * Auvitek au0828 analog stream enable | |
8b2f0795 | 765 | */ |
a094ca46 | 766 | static int au0828_analog_stream_enable(struct au0828_dev *d) |
8b2f0795 | 767 | { |
64ea37bb | 768 | struct usb_interface *iface; |
1fe3a8fe | 769 | int ret, h, w; |
64ea37bb | 770 | |
8b2f0795 | 771 | dprintk(1, "au0828_analog_stream_enable called\n"); |
64ea37bb MCC |
772 | |
773 | iface = usb_ifnum_to_if(d->usbdev, 0); | |
774 | if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) { | |
775 | dprintk(1, "Changing intf#0 to alt 5\n"); | |
776 | /* set au0828 interface0 to AS5 here again */ | |
777 | ret = usb_set_interface(d->usbdev, 0, 5); | |
778 | if (ret < 0) { | |
83afb32a | 779 | pr_info("Au0828 can't set alt setting to 5!\n"); |
64ea37bb MCC |
780 | return -EBUSY; |
781 | } | |
782 | } | |
783 | ||
1fe3a8fe MCC |
784 | h = d->height / 2 + 2; |
785 | w = d->width * 2; | |
64ea37bb | 786 | |
8b2f0795 DH |
787 | au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00); |
788 | au0828_writereg(d, 0x106, 0x00); | |
789 | /* set x position */ | |
790 | au0828_writereg(d, 0x110, 0x00); | |
791 | au0828_writereg(d, 0x111, 0x00); | |
1fe3a8fe MCC |
792 | au0828_writereg(d, 0x114, w & 0xff); |
793 | au0828_writereg(d, 0x115, w >> 8); | |
8b2f0795 | 794 | /* set y position */ |
7f8eacd2 | 795 | au0828_writereg(d, 0x112, 0x00); |
8b2f0795 | 796 | au0828_writereg(d, 0x113, 0x00); |
1fe3a8fe MCC |
797 | au0828_writereg(d, 0x116, h & 0xff); |
798 | au0828_writereg(d, 0x117, h >> 8); | |
8b2f0795 DH |
799 | au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3); |
800 | ||
801 | return 0; | |
802 | } | |
803 | ||
05439b1a | 804 | static int au0828_analog_stream_disable(struct au0828_dev *d) |
8b2f0795 DH |
805 | { |
806 | dprintk(1, "au0828_analog_stream_disable called\n"); | |
807 | au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0); | |
808 | return 0; | |
809 | } | |
810 | ||
a094ca46 | 811 | static void au0828_analog_stream_reset(struct au0828_dev *dev) |
8b2f0795 DH |
812 | { |
813 | dprintk(1, "au0828_analog_stream_reset called\n"); | |
814 | au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0); | |
815 | mdelay(30); | |
816 | au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3); | |
817 | } | |
818 | ||
819 | /* | |
820 | * Some operations needs to stop current streaming | |
821 | */ | |
822 | static int au0828_stream_interrupt(struct au0828_dev *dev) | |
823 | { | |
824 | int ret = 0; | |
825 | ||
826 | dev->stream_state = STREAM_INTERRUPT; | |
62899a28 | 827 | if (dev->dev_state == DEV_DISCONNECTED) |
8b2f0795 | 828 | return -ENODEV; |
62899a28 | 829 | else if (ret) { |
8b2f0795 | 830 | dev->dev_state = DEV_MISCONFIGURED; |
62899a28 | 831 | dprintk(1, "%s device is misconfigured!\n", __func__); |
8b2f0795 DH |
832 | return ret; |
833 | } | |
834 | return 0; | |
835 | } | |
836 | ||
05439b1a | 837 | int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count) |
8b2f0795 | 838 | { |
05439b1a SK |
839 | struct au0828_dev *dev = vb2_get_drv_priv(vq); |
840 | int rc = 0; | |
8b2f0795 | 841 | |
05439b1a SK |
842 | dprintk(1, "au0828_start_analog_streaming called %d\n", |
843 | dev->streaming_users); | |
8b2f0795 | 844 | |
05439b1a SK |
845 | if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
846 | dev->frame_count = 0; | |
847 | else | |
848 | dev->vbi_frame_count = 0; | |
849 | ||
850 | if (dev->streaming_users == 0) { | |
851 | /* If we were doing ac97 instead of i2s, it would go here...*/ | |
852 | au0828_i2s_init(dev); | |
853 | rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB, | |
854 | AU0828_MAX_ISO_BUFS, dev->max_pkt_size, | |
855 | au0828_isoc_copy); | |
856 | if (rc < 0) { | |
857 | pr_info("au0828_init_isoc failed\n"); | |
858 | return rc; | |
859 | } | |
8b2f0795 | 860 | |
05439b1a SK |
861 | if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
862 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, | |
863 | s_stream, 1); | |
864 | dev->vid_timeout_running = 1; | |
865 | mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); | |
866 | } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) { | |
867 | dev->vbi_timeout_running = 1; | |
868 | mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); | |
869 | } | |
870 | } | |
871 | dev->streaming_users++; | |
872 | return rc; | |
873 | } | |
8b2f0795 | 874 | |
05439b1a | 875 | static void au0828_stop_streaming(struct vb2_queue *vq) |
8b2f0795 | 876 | { |
05439b1a SK |
877 | struct au0828_dev *dev = vb2_get_drv_priv(vq); |
878 | struct au0828_dmaqueue *vidq = &dev->vidq; | |
879 | unsigned long flags = 0; | |
8b2f0795 | 880 | |
05439b1a | 881 | dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users); |
8b2f0795 | 882 | |
05439b1a SK |
883 | if (dev->streaming_users-- == 1) |
884 | au0828_uninit_isoc(dev); | |
885 | ||
886 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); | |
887 | dev->vid_timeout_running = 0; | |
888 | del_timer_sync(&dev->vid_timeout); | |
889 | ||
890 | spin_lock_irqsave(&dev->slock, flags); | |
891 | if (dev->isoc_ctl.buf != NULL) { | |
2d700715 JS |
892 | vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf, |
893 | VB2_BUF_STATE_ERROR); | |
05439b1a | 894 | dev->isoc_ctl.buf = NULL; |
7f8eacd2 | 895 | } |
05439b1a SK |
896 | while (!list_empty(&vidq->active)) { |
897 | struct au0828_buffer *buf; | |
549ee4df | 898 | |
05439b1a | 899 | buf = list_entry(vidq->active.next, struct au0828_buffer, list); |
2d700715 | 900 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
05439b1a SK |
901 | list_del(&buf->list); |
902 | } | |
903 | spin_unlock_irqrestore(&dev->slock, flags); | |
7f8eacd2 | 904 | } |
8b2f0795 | 905 | |
05439b1a | 906 | void au0828_stop_vbi_streaming(struct vb2_queue *vq) |
7f8eacd2 | 907 | { |
05439b1a SK |
908 | struct au0828_dev *dev = vb2_get_drv_priv(vq); |
909 | struct au0828_dmaqueue *vbiq = &dev->vbiq; | |
910 | unsigned long flags = 0; | |
8b2f0795 | 911 | |
05439b1a SK |
912 | dprintk(1, "au0828_stop_vbi_streaming called %d\n", |
913 | dev->streaming_users); | |
8b2f0795 | 914 | |
05439b1a SK |
915 | if (dev->streaming_users-- == 1) |
916 | au0828_uninit_isoc(dev); | |
7f8eacd2 | 917 | |
05439b1a SK |
918 | spin_lock_irqsave(&dev->slock, flags); |
919 | if (dev->isoc_ctl.vbi_buf != NULL) { | |
2d700715 | 920 | vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf, |
05439b1a SK |
921 | VB2_BUF_STATE_ERROR); |
922 | dev->isoc_ctl.vbi_buf = NULL; | |
923 | } | |
924 | while (!list_empty(&vbiq->active)) { | |
925 | struct au0828_buffer *buf; | |
926 | ||
927 | buf = list_entry(vbiq->active.next, struct au0828_buffer, list); | |
928 | list_del(&buf->list); | |
2d700715 | 929 | vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); |
05439b1a SK |
930 | } |
931 | spin_unlock_irqrestore(&dev->slock, flags); | |
8b2f0795 | 932 | |
05439b1a SK |
933 | dev->vbi_timeout_running = 0; |
934 | del_timer_sync(&dev->vbi_timeout); | |
7f8eacd2 DH |
935 | } |
936 | ||
05439b1a SK |
937 | static struct vb2_ops au0828_video_qops = { |
938 | .queue_setup = queue_setup, | |
939 | .buf_prepare = buffer_prepare, | |
940 | .buf_queue = buffer_queue, | |
941 | .start_streaming = au0828_start_analog_streaming, | |
942 | .stop_streaming = au0828_stop_streaming, | |
943 | .wait_prepare = vb2_ops_wait_prepare, | |
944 | .wait_finish = vb2_ops_wait_finish, | |
945 | }; | |
946 | ||
947 | /* ------------------------------------------------------------------ | |
948 | V4L2 interface | |
949 | ------------------------------------------------------------------*/ | |
950 | /* | |
951 | * au0828_analog_unregister | |
952 | * unregister v4l2 devices | |
953 | */ | |
7b606ffd | 954 | int au0828_analog_unregister(struct au0828_dev *dev) |
7f8eacd2 | 955 | { |
05439b1a | 956 | dprintk(1, "au0828_analog_unregister called\n"); |
7b606ffd MCC |
957 | |
958 | /* No analog TV */ | |
959 | if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) | |
960 | return 0; | |
961 | ||
05439b1a | 962 | mutex_lock(&au0828_sysfs_lock); |
41071bb8 SK |
963 | video_unregister_device(&dev->vdev); |
964 | video_unregister_device(&dev->vbi_dev); | |
05439b1a | 965 | mutex_unlock(&au0828_sysfs_lock); |
7b606ffd MCC |
966 | |
967 | v4l2_device_disconnect(&dev->v4l2_dev); | |
968 | v4l2_device_put(&dev->v4l2_dev); | |
969 | ||
970 | return 1; | |
8b2f0795 DH |
971 | } |
972 | ||
6e04b7b9 DH |
973 | /* This function ensures that video frames continue to be delivered even if |
974 | the ITU-656 input isn't receiving any data (thereby preventing applications | |
975 | such as tvtime from hanging) */ | |
a094ca46 | 976 | static void au0828_vid_buffer_timeout(unsigned long data) |
6e04b7b9 DH |
977 | { |
978 | struct au0828_dev *dev = (struct au0828_dev *) data; | |
979 | struct au0828_dmaqueue *dma_q = &dev->vidq; | |
980 | struct au0828_buffer *buf; | |
981 | unsigned char *vid_data; | |
78ca5005 | 982 | unsigned long flags = 0; |
6e04b7b9 | 983 | |
78ca5005 | 984 | spin_lock_irqsave(&dev->slock, flags); |
6e04b7b9 DH |
985 | |
986 | buf = dev->isoc_ctl.buf; | |
987 | if (buf != NULL) { | |
2d700715 | 988 | vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
05439b1a | 989 | memset(vid_data, 0x00, buf->length); /* Blank green frame */ |
6e04b7b9 | 990 | buffer_filled(dev, dma_q, buf); |
6e04b7b9 | 991 | } |
78ca5005 DH |
992 | get_next_buf(dma_q, &buf); |
993 | ||
994 | if (dev->vid_timeout_running == 1) | |
995 | mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); | |
6e04b7b9 | 996 | |
78ca5005 | 997 | spin_unlock_irqrestore(&dev->slock, flags); |
6e04b7b9 DH |
998 | } |
999 | ||
a094ca46 | 1000 | static void au0828_vbi_buffer_timeout(unsigned long data) |
6e04b7b9 DH |
1001 | { |
1002 | struct au0828_dev *dev = (struct au0828_dev *) data; | |
1003 | struct au0828_dmaqueue *dma_q = &dev->vbiq; | |
1004 | struct au0828_buffer *buf; | |
1005 | unsigned char *vbi_data; | |
78ca5005 | 1006 | unsigned long flags = 0; |
6e04b7b9 | 1007 | |
78ca5005 | 1008 | spin_lock_irqsave(&dev->slock, flags); |
6e04b7b9 DH |
1009 | |
1010 | buf = dev->isoc_ctl.vbi_buf; | |
1011 | if (buf != NULL) { | |
2d700715 | 1012 | vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); |
05439b1a | 1013 | memset(vbi_data, 0x00, buf->length); |
c5036d61 | 1014 | buffer_filled(dev, dma_q, buf); |
6e04b7b9 | 1015 | } |
78ca5005 | 1016 | vbi_get_next_buf(dma_q, &buf); |
6e04b7b9 | 1017 | |
78ca5005 DH |
1018 | if (dev->vbi_timeout_running == 1) |
1019 | mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); | |
1020 | spin_unlock_irqrestore(&dev->slock, flags); | |
6e04b7b9 DH |
1021 | } |
1022 | ||
8b2f0795 DH |
1023 | static int au0828_v4l2_open(struct file *filp) |
1024 | { | |
63b0d5ad | 1025 | struct au0828_dev *dev = video_drvdata(filp); |
05439b1a | 1026 | int ret; |
8b2f0795 | 1027 | |
05439b1a SK |
1028 | dprintk(1, |
1029 | "%s called std_set %d dev_state %d stream users %d users %d\n", | |
1030 | __func__, dev->std_set_in_tuner_core, dev->dev_state, | |
1031 | dev->streaming_users, dev->users); | |
8b2f0795 | 1032 | |
05439b1a | 1033 | if (mutex_lock_interruptible(&dev->lock)) |
ea86968f | 1034 | return -ERESTARTSYS; |
05439b1a SK |
1035 | |
1036 | ret = v4l2_fh_open(filp); | |
1037 | if (ret) { | |
1038 | au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n", | |
1039 | __func__, ret); | |
1040 | mutex_unlock(&dev->lock); | |
1041 | return ret; | |
ea86968f | 1042 | } |
05439b1a | 1043 | |
ea86968f | 1044 | if (dev->users == 0) { |
8b2f0795 DH |
1045 | au0828_analog_stream_enable(dev); |
1046 | au0828_analog_stream_reset(dev); | |
8b2f0795 DH |
1047 | dev->stream_state = STREAM_OFF; |
1048 | dev->dev_state |= DEV_INITIALIZED; | |
1049 | } | |
8b2f0795 | 1050 | dev->users++; |
ea86968f | 1051 | mutex_unlock(&dev->lock); |
8b2f0795 DH |
1052 | return ret; |
1053 | } | |
1054 | ||
1055 | static int au0828_v4l2_close(struct file *filp) | |
1056 | { | |
1057 | int ret; | |
05439b1a SK |
1058 | struct au0828_dev *dev = video_drvdata(filp); |
1059 | struct video_device *vdev = video_devdata(filp); | |
1060 | ||
1061 | dprintk(1, | |
1062 | "%s called std_set %d dev_state %d stream users %d users %d\n", | |
1063 | __func__, dev->std_set_in_tuner_core, dev->dev_state, | |
1064 | dev->streaming_users, dev->users); | |
8b2f0795 | 1065 | |
ea86968f | 1066 | mutex_lock(&dev->lock); |
05439b1a | 1067 | if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) { |
78ca5005 DH |
1068 | /* Cancel timeout thread in case they didn't call streamoff */ |
1069 | dev->vid_timeout_running = 0; | |
1070 | del_timer_sync(&dev->vid_timeout); | |
05439b1a SK |
1071 | } else if (vdev->vfl_type == VFL_TYPE_VBI && |
1072 | dev->vbi_timeout_running) { | |
78ca5005 DH |
1073 | /* Cancel timeout thread in case they didn't call streamoff */ |
1074 | dev->vbi_timeout_running = 0; | |
1075 | del_timer_sync(&dev->vbi_timeout); | |
7f8eacd2 DH |
1076 | } |
1077 | ||
ed940cd2 | 1078 | if (dev->dev_state & DEV_DISCONNECTED) |
05439b1a | 1079 | goto end; |
8b2f0795 | 1080 | |
05439b1a | 1081 | if (dev->users == 1) { |
b19581a9 SK |
1082 | /* |
1083 | * Avoid putting tuner in sleep if DVB or ALSA are | |
1084 | * streaming. | |
1085 | * | |
1086 | * On most USB devices like au0828 the tuner can | |
1087 | * be safely put in sleep stare here if ALSA isn't | |
1088 | * streaming. Exceptions are some very old USB tuner | |
1089 | * models such as em28xx-based WinTV USB2 which have | |
1090 | * a separate audio output jack. The devices that have | |
1091 | * a separate audio output jack have analog tuners, | |
1092 | * like Philips FM1236. Those devices are always on, | |
1093 | * so the s_power callback are silently ignored. | |
1094 | * So, the current logic here does the following: | |
1095 | * Disable (put tuner to sleep) when | |
1096 | * - ALSA and DVB aren't not streaming; | |
1097 | * - the last V4L2 file handler is closed. | |
1098 | * | |
1099 | * FIXME: | |
1100 | * | |
1101 | * Additionally, this logic could be improved to | |
1102 | * disable the media source if the above conditions | |
1103 | * are met and if the device: | |
1104 | * - doesn't have a separate audio out plug (or | |
1105 | * - doesn't use a silicon tuner like xc2028/3028/4000/5000). | |
1106 | * | |
1107 | * Once this additional logic is in place, a callback | |
1108 | * is needed to enable the media source and power on | |
1109 | * the tuner, for radio to work. | |
1110 | */ | |
1111 | ret = v4l_enable_media_source(vdev); | |
1112 | if (ret == 0) | |
1113 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, | |
1114 | s_power, 0); | |
ea86968f | 1115 | dev->std_set_in_tuner_core = 0; |
e4b8bc52 | 1116 | |
8b2f0795 DH |
1117 | /* When close the device, set the usb intf0 into alt0 to free |
1118 | USB bandwidth */ | |
1119 | ret = usb_set_interface(dev->usbdev, 0, 0); | |
62899a28 | 1120 | if (ret < 0) |
83afb32a | 1121 | pr_info("Au0828 can't set alternate to 0!\n"); |
8b2f0795 | 1122 | } |
05439b1a SK |
1123 | end: |
1124 | _vb2_fop_release(filp, NULL); | |
8b2f0795 | 1125 | dev->users--; |
05439b1a | 1126 | mutex_unlock(&dev->lock); |
8b2f0795 DH |
1127 | return 0; |
1128 | } | |
1129 | ||
ea86968f HV |
1130 | /* Must be called with dev->lock held */ |
1131 | static void au0828_init_tuner(struct au0828_dev *dev) | |
1132 | { | |
1133 | struct v4l2_frequency f = { | |
1134 | .frequency = dev->ctrl_freq, | |
1135 | .type = V4L2_TUNER_ANALOG_TV, | |
1136 | }; | |
1137 | ||
05439b1a SK |
1138 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1139 | dev->std_set_in_tuner_core, dev->dev_state); | |
1140 | ||
ea86968f HV |
1141 | if (dev->std_set_in_tuner_core) |
1142 | return; | |
1143 | dev->std_set_in_tuner_core = 1; | |
1144 | i2c_gate_ctrl(dev, 1); | |
1145 | /* If we've never sent the standard in tuner core, do so now. | |
1146 | We don't do this at device probe because we don't want to | |
1147 | incur the cost of a firmware load */ | |
8774bed9 | 1148 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std); |
ea86968f HV |
1149 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); |
1150 | i2c_gate_ctrl(dev, 0); | |
1151 | } | |
1152 | ||
8b2f0795 DH |
1153 | static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd, |
1154 | struct v4l2_format *format) | |
1155 | { | |
1156 | int ret; | |
1157 | int width = format->fmt.pix.width; | |
1158 | int height = format->fmt.pix.height; | |
8b2f0795 | 1159 | |
8b2f0795 DH |
1160 | /* If they are demanding a format other than the one we support, |
1161 | bail out (tvtime asks for UYVY and then retries with YUYV) */ | |
62899a28 | 1162 | if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY) |
8b2f0795 | 1163 | return -EINVAL; |
8b2f0795 DH |
1164 | |
1165 | /* format->fmt.pix.width only support 720 and height 480 */ | |
62899a28 | 1166 | if (width != 720) |
8b2f0795 | 1167 | width = 720; |
62899a28 | 1168 | if (height != 480) |
8b2f0795 DH |
1169 | height = 480; |
1170 | ||
1171 | format->fmt.pix.width = width; | |
1172 | format->fmt.pix.height = height; | |
1173 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; | |
1174 | format->fmt.pix.bytesperline = width * 2; | |
1175 | format->fmt.pix.sizeimage = width * height * 2; | |
1176 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1177 | format->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
8d86e4e8 | 1178 | format->fmt.pix.priv = 0; |
8b2f0795 | 1179 | |
62899a28 | 1180 | if (cmd == VIDIOC_TRY_FMT) |
8b2f0795 DH |
1181 | return 0; |
1182 | ||
1183 | /* maybe set new image format, driver current only support 720*480 */ | |
1184 | dev->width = width; | |
1185 | dev->height = height; | |
1186 | dev->frame_size = width * height * 2; | |
1187 | dev->field_size = width * height; | |
1188 | dev->bytesperline = width * 2; | |
1189 | ||
62899a28 | 1190 | if (dev->stream_state == STREAM_ON) { |
8b2f0795 | 1191 | dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n"); |
62899a28 DH |
1192 | ret = au0828_stream_interrupt(dev); |
1193 | if (ret != 0) { | |
8b2f0795 DH |
1194 | dprintk(1, "error interrupting video stream!\n"); |
1195 | return ret; | |
1196 | } | |
1197 | } | |
1198 | ||
8b2f0795 DH |
1199 | au0828_analog_stream_enable(dev); |
1200 | ||
1201 | return 0; | |
1202 | } | |
1203 | ||
8b2f0795 DH |
1204 | static int vidioc_querycap(struct file *file, void *priv, |
1205 | struct v4l2_capability *cap) | |
1206 | { | |
59e05484 | 1207 | struct video_device *vdev = video_devdata(file); |
05439b1a SK |
1208 | struct au0828_dev *dev = video_drvdata(file); |
1209 | ||
1210 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1211 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 | 1212 | |
8b2f0795 | 1213 | strlcpy(cap->driver, "au0828", sizeof(cap->driver)); |
f1add5b5 | 1214 | strlcpy(cap->card, dev->board.name, sizeof(cap->card)); |
59e05484 | 1215 | usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info)); |
8b2f0795 | 1216 | |
59e05484 HV |
1217 | /* set the device capabilities */ |
1218 | cap->device_caps = V4L2_CAP_AUDIO | | |
8b2f0795 DH |
1219 | V4L2_CAP_READWRITE | |
1220 | V4L2_CAP_STREAMING | | |
1221 | V4L2_CAP_TUNER; | |
59e05484 HV |
1222 | if (vdev->vfl_type == VFL_TYPE_GRABBER) |
1223 | cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE; | |
1224 | else | |
1225 | cap->device_caps |= V4L2_CAP_VBI_CAPTURE; | |
1226 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS | | |
1227 | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE; | |
8b2f0795 DH |
1228 | return 0; |
1229 | } | |
1230 | ||
1231 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | |
1232 | struct v4l2_fmtdesc *f) | |
1233 | { | |
62899a28 | 1234 | if (f->index) |
8b2f0795 DH |
1235 | return -EINVAL; |
1236 | ||
05439b1a SK |
1237 | dprintk(1, "%s called\n", __func__); |
1238 | ||
8b2f0795 DH |
1239 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1240 | strcpy(f->description, "Packed YUV2"); | |
1241 | ||
1242 | f->flags = 0; | |
1243 | f->pixelformat = V4L2_PIX_FMT_UYVY; | |
1244 | ||
8b2f0795 DH |
1245 | return 0; |
1246 | } | |
1247 | ||
1248 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | |
1249 | struct v4l2_format *f) | |
1250 | { | |
05439b1a SK |
1251 | struct au0828_dev *dev = video_drvdata(file); |
1252 | ||
1253 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1254 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 DH |
1255 | |
1256 | f->fmt.pix.width = dev->width; | |
1257 | f->fmt.pix.height = dev->height; | |
1258 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; | |
1259 | f->fmt.pix.bytesperline = dev->bytesperline; | |
1260 | f->fmt.pix.sizeimage = dev->frame_size; | |
1261 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */ | |
1262 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
8d86e4e8 | 1263 | f->fmt.pix.priv = 0; |
8b2f0795 DH |
1264 | return 0; |
1265 | } | |
1266 | ||
1267 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | |
1268 | struct v4l2_format *f) | |
1269 | { | |
05439b1a SK |
1270 | struct au0828_dev *dev = video_drvdata(file); |
1271 | ||
1272 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1273 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 DH |
1274 | |
1275 | return au0828_set_format(dev, VIDIOC_TRY_FMT, f); | |
1276 | } | |
1277 | ||
1278 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | |
1279 | struct v4l2_format *f) | |
1280 | { | |
05439b1a | 1281 | struct au0828_dev *dev = video_drvdata(file); |
8b2f0795 DH |
1282 | int rc; |
1283 | ||
05439b1a SK |
1284 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1285 | dev->std_set_in_tuner_core, dev->dev_state); | |
1286 | ||
7f8eacd2 DH |
1287 | rc = check_dev(dev); |
1288 | if (rc < 0) | |
1289 | return rc; | |
1290 | ||
05439b1a | 1291 | if (vb2_is_busy(&dev->vb_vidq)) { |
83afb32a | 1292 | pr_info("%s queue busy\n", __func__); |
8b2f0795 DH |
1293 | rc = -EBUSY; |
1294 | goto out; | |
1295 | } | |
1296 | ||
7f8eacd2 | 1297 | rc = au0828_set_format(dev, VIDIOC_S_FMT, f); |
8b2f0795 DH |
1298 | out: |
1299 | return rc; | |
1300 | } | |
1301 | ||
314527ac | 1302 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) |
8b2f0795 | 1303 | { |
05439b1a SK |
1304 | struct au0828_dev *dev = video_drvdata(file); |
1305 | ||
1306 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1307 | dev->std_set_in_tuner_core, dev->dev_state); | |
1308 | ||
1309 | if (norm == dev->std) | |
1310 | return 0; | |
1311 | ||
1312 | if (dev->streaming_users > 0) | |
1313 | return -EBUSY; | |
8b2f0795 | 1314 | |
ea86968f HV |
1315 | dev->std = norm; |
1316 | ||
1317 | au0828_init_tuner(dev); | |
1318 | ||
fa09cb9a | 1319 | i2c_gate_ctrl(dev, 1); |
e58071f0 | 1320 | |
f2fd7ce6 MCC |
1321 | /* |
1322 | * FIXME: when we support something other than 60Hz standards, | |
1323 | * we are going to have to make the au0828 bridge adjust the size | |
1324 | * of its capture buffer, which is currently hardcoded at 720x480 | |
1325 | */ | |
8b2f0795 | 1326 | |
8774bed9 | 1327 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm); |
e58071f0 | 1328 | |
fa09cb9a | 1329 | i2c_gate_ctrl(dev, 0); |
e58071f0 | 1330 | |
8b2f0795 DH |
1331 | return 0; |
1332 | } | |
1333 | ||
33b6b89b HV |
1334 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) |
1335 | { | |
05439b1a SK |
1336 | struct au0828_dev *dev = video_drvdata(file); |
1337 | ||
1338 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1339 | dev->std_set_in_tuner_core, dev->dev_state); | |
33b6b89b HV |
1340 | |
1341 | *norm = dev->std; | |
1342 | return 0; | |
1343 | } | |
1344 | ||
8b2f0795 DH |
1345 | static int vidioc_enum_input(struct file *file, void *priv, |
1346 | struct v4l2_input *input) | |
1347 | { | |
05439b1a | 1348 | struct au0828_dev *dev = video_drvdata(file); |
8b2f0795 DH |
1349 | unsigned int tmp; |
1350 | ||
1351 | static const char *inames[] = { | |
3d62287e | 1352 | [AU0828_VMUX_UNDEFINED] = "Undefined", |
8b2f0795 DH |
1353 | [AU0828_VMUX_COMPOSITE] = "Composite", |
1354 | [AU0828_VMUX_SVIDEO] = "S-Video", | |
1355 | [AU0828_VMUX_CABLE] = "Cable TV", | |
1356 | [AU0828_VMUX_TELEVISION] = "Television", | |
1357 | [AU0828_VMUX_DVB] = "DVB", | |
8b2f0795 DH |
1358 | }; |
1359 | ||
05439b1a SK |
1360 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1361 | dev->std_set_in_tuner_core, dev->dev_state); | |
1362 | ||
8b2f0795 DH |
1363 | tmp = input->index; |
1364 | ||
f5e20c34 | 1365 | if (tmp >= AU0828_MAX_INPUT) |
8b2f0795 | 1366 | return -EINVAL; |
62899a28 | 1367 | if (AUVI_INPUT(tmp).type == 0) |
8b2f0795 DH |
1368 | return -EINVAL; |
1369 | ||
8b2f0795 | 1370 | input->index = tmp; |
f1add5b5 | 1371 | strcpy(input->name, inames[AUVI_INPUT(tmp).type]); |
62899a28 | 1372 | if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) || |
a2cf96f9 | 1373 | (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) { |
8b2f0795 | 1374 | input->type |= V4L2_INPUT_TYPE_TUNER; |
a2cf96f9 HV |
1375 | input->audioset = 1; |
1376 | } else { | |
8b2f0795 | 1377 | input->type |= V4L2_INPUT_TYPE_CAMERA; |
a2cf96f9 HV |
1378 | input->audioset = 2; |
1379 | } | |
8b2f0795 | 1380 | |
41071bb8 | 1381 | input->std = dev->vdev.tvnorms; |
8b2f0795 DH |
1382 | |
1383 | return 0; | |
1384 | } | |
1385 | ||
1386 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) | |
1387 | { | |
05439b1a SK |
1388 | struct au0828_dev *dev = video_drvdata(file); |
1389 | ||
1390 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1391 | dev->std_set_in_tuner_core, dev->dev_state); | |
1392 | ||
8b2f0795 DH |
1393 | *i = dev->ctrl_input; |
1394 | return 0; | |
1395 | } | |
1396 | ||
56230d1e | 1397 | static void au0828_s_input(struct au0828_dev *dev, int index) |
8b2f0795 | 1398 | { |
8b2f0795 | 1399 | int i; |
8b2f0795 | 1400 | |
05439b1a SK |
1401 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1402 | dev->std_set_in_tuner_core, dev->dev_state); | |
1403 | ||
62899a28 | 1404 | switch (AUVI_INPUT(index).type) { |
8b2f0795 | 1405 | case AU0828_VMUX_SVIDEO: |
8b2f0795 | 1406 | dev->input_type = AU0828_VMUX_SVIDEO; |
a2cf96f9 | 1407 | dev->ctrl_ainput = 1; |
8b2f0795 | 1408 | break; |
8b2f0795 | 1409 | case AU0828_VMUX_COMPOSITE: |
8b2f0795 | 1410 | dev->input_type = AU0828_VMUX_COMPOSITE; |
a2cf96f9 | 1411 | dev->ctrl_ainput = 1; |
8b2f0795 | 1412 | break; |
8b2f0795 | 1413 | case AU0828_VMUX_TELEVISION: |
8b2f0795 | 1414 | dev->input_type = AU0828_VMUX_TELEVISION; |
a2cf96f9 | 1415 | dev->ctrl_ainput = 0; |
8b2f0795 | 1416 | break; |
8b2f0795 | 1417 | default: |
56230d1e | 1418 | dprintk(1, "unknown input type set [%d]\n", |
a1094c4c | 1419 | AUVI_INPUT(index).type); |
174ced21 | 1420 | return; |
8b2f0795 DH |
1421 | } |
1422 | ||
174ced21 SK |
1423 | dev->ctrl_input = index; |
1424 | ||
5325b427 HV |
1425 | v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing, |
1426 | AUVI_INPUT(index).vmux, 0, 0); | |
8b2f0795 DH |
1427 | |
1428 | for (i = 0; i < AU0828_MAX_INPUT; i++) { | |
1429 | int enable = 0; | |
62899a28 | 1430 | if (AUVI_INPUT(i).audio_setup == NULL) |
8b2f0795 | 1431 | continue; |
8b2f0795 DH |
1432 | |
1433 | if (i == index) | |
1434 | enable = 1; | |
1435 | else | |
1436 | enable = 0; | |
1437 | if (enable) { | |
f1add5b5 | 1438 | (AUVI_INPUT(i).audio_setup)(dev, enable); |
8b2f0795 DH |
1439 | } else { |
1440 | /* Make sure we leave it turned on if some | |
1441 | other input is routed to this callback */ | |
f1add5b5 DH |
1442 | if ((AUVI_INPUT(i).audio_setup) != |
1443 | ((AUVI_INPUT(index).audio_setup))) { | |
1444 | (AUVI_INPUT(i).audio_setup)(dev, enable); | |
8b2f0795 DH |
1445 | } |
1446 | } | |
1447 | } | |
1448 | ||
5325b427 HV |
1449 | v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing, |
1450 | AUVI_INPUT(index).amux, 0, 0); | |
56230d1e HV |
1451 | } |
1452 | ||
1453 | static int vidioc_s_input(struct file *file, void *priv, unsigned int index) | |
1454 | { | |
05439b1a | 1455 | struct au0828_dev *dev = video_drvdata(file); |
050b6c98 | 1456 | struct video_device *vfd = video_devdata(file); |
56230d1e HV |
1457 | |
1458 | dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__, | |
1459 | index); | |
1460 | if (index >= AU0828_MAX_INPUT) | |
1461 | return -EINVAL; | |
1462 | if (AUVI_INPUT(index).type == 0) | |
1463 | return -EINVAL; | |
174ced21 SK |
1464 | |
1465 | if (dev->ctrl_input == index) | |
1466 | return 0; | |
1467 | ||
56230d1e | 1468 | au0828_s_input(dev, index); |
050b6c98 SK |
1469 | |
1470 | /* | |
1471 | * Input has been changed. Disable the media source | |
1472 | * associated with the old input and enable source | |
1473 | * for the newly set input | |
1474 | */ | |
1475 | v4l_disable_media_source(vfd); | |
1476 | return v4l_enable_media_source(vfd); | |
8b2f0795 DH |
1477 | } |
1478 | ||
a2cf96f9 HV |
1479 | static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a) |
1480 | { | |
1481 | if (a->index > 1) | |
1482 | return -EINVAL; | |
1483 | ||
05439b1a SK |
1484 | dprintk(1, "%s called\n", __func__); |
1485 | ||
a2cf96f9 HV |
1486 | if (a->index == 0) |
1487 | strcpy(a->name, "Television"); | |
1488 | else | |
1489 | strcpy(a->name, "Line in"); | |
1490 | ||
1491 | a->capability = V4L2_AUDCAP_STEREO; | |
1492 | return 0; | |
1493 | } | |
1494 | ||
8b2f0795 DH |
1495 | static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) |
1496 | { | |
05439b1a SK |
1497 | struct au0828_dev *dev = video_drvdata(file); |
1498 | ||
1499 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1500 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 | 1501 | |
a2cf96f9 HV |
1502 | a->index = dev->ctrl_ainput; |
1503 | if (a->index == 0) | |
8b2f0795 DH |
1504 | strcpy(a->name, "Television"); |
1505 | else | |
1506 | strcpy(a->name, "Line in"); | |
1507 | ||
1508 | a->capability = V4L2_AUDCAP_STEREO; | |
8b2f0795 DH |
1509 | return 0; |
1510 | } | |
1511 | ||
0e8025b9 | 1512 | static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a) |
8b2f0795 | 1513 | { |
05439b1a | 1514 | struct au0828_dev *dev = video_drvdata(file); |
a2cf96f9 | 1515 | |
62899a28 | 1516 | if (a->index != dev->ctrl_ainput) |
8b2f0795 | 1517 | return -EINVAL; |
05439b1a SK |
1518 | |
1519 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1520 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 DH |
1521 | return 0; |
1522 | } | |
1523 | ||
8b2f0795 DH |
1524 | static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) |
1525 | { | |
05439b1a | 1526 | struct au0828_dev *dev = video_drvdata(file); |
b19581a9 SK |
1527 | struct video_device *vfd = video_devdata(file); |
1528 | int ret; | |
8b2f0795 | 1529 | |
62899a28 | 1530 | if (t->index != 0) |
8b2f0795 DH |
1531 | return -EINVAL; |
1532 | ||
b19581a9 SK |
1533 | ret = v4l_enable_media_source(vfd); |
1534 | if (ret) | |
1535 | return ret; | |
1536 | ||
05439b1a SK |
1537 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1538 | dev->std_set_in_tuner_core, dev->dev_state); | |
1539 | ||
8b2f0795 | 1540 | strcpy(t->name, "Auvitek tuner"); |
ea86968f HV |
1541 | |
1542 | au0828_init_tuner(dev); | |
1543 | i2c_gate_ctrl(dev, 1); | |
2689d3dc | 1544 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); |
ea86968f | 1545 | i2c_gate_ctrl(dev, 0); |
8b2f0795 DH |
1546 | return 0; |
1547 | } | |
1548 | ||
1549 | static int vidioc_s_tuner(struct file *file, void *priv, | |
2f73c7c5 | 1550 | const struct v4l2_tuner *t) |
8b2f0795 | 1551 | { |
05439b1a | 1552 | struct au0828_dev *dev = video_drvdata(file); |
8b2f0795 | 1553 | |
62899a28 | 1554 | if (t->index != 0) |
8b2f0795 DH |
1555 | return -EINVAL; |
1556 | ||
05439b1a SK |
1557 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1558 | dev->std_set_in_tuner_core, dev->dev_state); | |
1559 | ||
ea86968f | 1560 | au0828_init_tuner(dev); |
fa09cb9a | 1561 | i2c_gate_ctrl(dev, 1); |
2689d3dc | 1562 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); |
fa09cb9a | 1563 | i2c_gate_ctrl(dev, 0); |
e58071f0 | 1564 | |
8b2f0795 DH |
1565 | dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal, |
1566 | t->afc); | |
e58071f0 | 1567 | |
8b2f0795 DH |
1568 | return 0; |
1569 | ||
1570 | } | |
1571 | ||
1572 | static int vidioc_g_frequency(struct file *file, void *priv, | |
1573 | struct v4l2_frequency *freq) | |
1574 | { | |
05439b1a | 1575 | struct au0828_dev *dev = video_drvdata(file); |
c888923d | 1576 | |
e1cf6587 HV |
1577 | if (freq->tuner != 0) |
1578 | return -EINVAL; | |
05439b1a SK |
1579 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1580 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 DH |
1581 | freq->frequency = dev->ctrl_freq; |
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | static int vidioc_s_frequency(struct file *file, void *priv, | |
b530a447 | 1586 | const struct v4l2_frequency *freq) |
8b2f0795 | 1587 | { |
05439b1a | 1588 | struct au0828_dev *dev = video_drvdata(file); |
e1cf6587 | 1589 | struct v4l2_frequency new_freq = *freq; |
8b2f0795 | 1590 | |
62899a28 | 1591 | if (freq->tuner != 0) |
8b2f0795 | 1592 | return -EINVAL; |
8b2f0795 | 1593 | |
05439b1a SK |
1594 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1595 | dev->std_set_in_tuner_core, dev->dev_state); | |
1596 | ||
ea86968f | 1597 | au0828_init_tuner(dev); |
fa09cb9a | 1598 | i2c_gate_ctrl(dev, 1); |
4a03dafc | 1599 | |
2689d3dc | 1600 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq); |
e1cf6587 HV |
1601 | /* Get the actual set (and possibly clamped) frequency */ |
1602 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq); | |
1603 | dev->ctrl_freq = new_freq.frequency; | |
8b2f0795 | 1604 | |
fa09cb9a | 1605 | i2c_gate_ctrl(dev, 0); |
4a03dafc | 1606 | |
8b2f0795 DH |
1607 | au0828_analog_stream_reset(dev); |
1608 | ||
1609 | return 0; | |
1610 | } | |
1611 | ||
7f8eacd2 DH |
1612 | |
1613 | /* RAW VBI ioctls */ | |
1614 | ||
1615 | static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, | |
1616 | struct v4l2_format *format) | |
1617 | { | |
05439b1a SK |
1618 | struct au0828_dev *dev = video_drvdata(file); |
1619 | ||
1620 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1621 | dev->std_set_in_tuner_core, dev->dev_state); | |
7f8eacd2 DH |
1622 | |
1623 | format->fmt.vbi.samples_per_line = dev->vbi_width; | |
1624 | format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; | |
1625 | format->fmt.vbi.offset = 0; | |
1626 | format->fmt.vbi.flags = 0; | |
1627 | format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; | |
1628 | ||
1629 | format->fmt.vbi.count[0] = dev->vbi_height; | |
1630 | format->fmt.vbi.count[1] = dev->vbi_height; | |
1631 | format->fmt.vbi.start[0] = 21; | |
1632 | format->fmt.vbi.start[1] = 284; | |
8d86e4e8 | 1633 | memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved)); |
7f8eacd2 DH |
1634 | |
1635 | return 0; | |
1636 | } | |
1637 | ||
8b2f0795 DH |
1638 | static int vidioc_cropcap(struct file *file, void *priv, |
1639 | struct v4l2_cropcap *cc) | |
1640 | { | |
05439b1a | 1641 | struct au0828_dev *dev = video_drvdata(file); |
8b2f0795 | 1642 | |
62899a28 | 1643 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
8b2f0795 DH |
1644 | return -EINVAL; |
1645 | ||
05439b1a SK |
1646 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, |
1647 | dev->std_set_in_tuner_core, dev->dev_state); | |
1648 | ||
8b2f0795 DH |
1649 | cc->bounds.left = 0; |
1650 | cc->bounds.top = 0; | |
1651 | cc->bounds.width = dev->width; | |
1652 | cc->bounds.height = dev->height; | |
1653 | ||
1654 | cc->defrect = cc->bounds; | |
1655 | ||
1656 | cc->pixelaspect.numerator = 54; | |
1657 | cc->pixelaspect.denominator = 59; | |
1658 | ||
1659 | return 0; | |
1660 | } | |
1661 | ||
80c6e358 | 1662 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
8b2f0795 DH |
1663 | static int vidioc_g_register(struct file *file, void *priv, |
1664 | struct v4l2_dbg_register *reg) | |
1665 | { | |
05439b1a SK |
1666 | struct au0828_dev *dev = video_drvdata(file); |
1667 | ||
1668 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1669 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 | 1670 | |
364d2db2 | 1671 | reg->val = au0828_read(dev, reg->reg); |
ead5bc4e | 1672 | reg->size = 1; |
364d2db2 | 1673 | return 0; |
8b2f0795 DH |
1674 | } |
1675 | ||
1676 | static int vidioc_s_register(struct file *file, void *priv, | |
977ba3b1 | 1677 | const struct v4l2_dbg_register *reg) |
8b2f0795 | 1678 | { |
05439b1a SK |
1679 | struct au0828_dev *dev = video_drvdata(file); |
1680 | ||
1681 | dprintk(1, "%s called std_set %d dev_state %d\n", __func__, | |
1682 | dev->std_set_in_tuner_core, dev->dev_state); | |
8b2f0795 | 1683 | |
364d2db2 | 1684 | return au0828_writereg(dev, reg->reg, reg->val); |
8b2f0795 | 1685 | } |
80c6e358 | 1686 | #endif |
8b2f0795 | 1687 | |
83b09422 HV |
1688 | static int vidioc_log_status(struct file *file, void *fh) |
1689 | { | |
1690 | struct video_device *vdev = video_devdata(file); | |
1691 | ||
05439b1a SK |
1692 | dprintk(1, "%s called\n", __func__); |
1693 | ||
83b09422 HV |
1694 | v4l2_ctrl_log_status(file, fh); |
1695 | v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status); | |
1696 | return 0; | |
1697 | } | |
1698 | ||
1a1ba95e MCC |
1699 | void au0828_v4l2_suspend(struct au0828_dev *dev) |
1700 | { | |
1701 | struct urb *urb; | |
1702 | int i; | |
1703 | ||
81187240 MCC |
1704 | pr_info("stopping V4L2\n"); |
1705 | ||
1a1ba95e | 1706 | if (dev->stream_state == STREAM_ON) { |
81187240 | 1707 | pr_info("stopping V4L2 active URBs\n"); |
1a1ba95e MCC |
1708 | au0828_analog_stream_disable(dev); |
1709 | /* stop urbs */ | |
1710 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
1711 | urb = dev->isoc_ctl.urb[i]; | |
1712 | if (urb) { | |
1713 | if (!irqs_disabled()) | |
1714 | usb_kill_urb(urb); | |
1715 | else | |
1716 | usb_unlink_urb(urb); | |
1717 | } | |
1718 | } | |
1719 | } | |
1720 | ||
1721 | if (dev->vid_timeout_running) | |
1722 | del_timer_sync(&dev->vid_timeout); | |
1723 | if (dev->vbi_timeout_running) | |
1724 | del_timer_sync(&dev->vbi_timeout); | |
1725 | } | |
1726 | ||
1727 | void au0828_v4l2_resume(struct au0828_dev *dev) | |
1728 | { | |
1729 | int i, rc; | |
1730 | ||
81187240 MCC |
1731 | pr_info("restarting V4L2\n"); |
1732 | ||
1a1ba95e MCC |
1733 | if (dev->stream_state == STREAM_ON) { |
1734 | au0828_stream_interrupt(dev); | |
1735 | au0828_init_tuner(dev); | |
1736 | } | |
1737 | ||
1738 | if (dev->vid_timeout_running) | |
1739 | mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); | |
1740 | if (dev->vbi_timeout_running) | |
1741 | mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); | |
1742 | ||
1743 | /* If we were doing ac97 instead of i2s, it would go here...*/ | |
1744 | au0828_i2s_init(dev); | |
1745 | ||
1746 | au0828_analog_stream_enable(dev); | |
1747 | ||
1748 | if (!(dev->stream_state == STREAM_ON)) { | |
1749 | au0828_analog_stream_reset(dev); | |
1750 | /* submit urbs */ | |
1751 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
1752 | rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); | |
1753 | if (rc) { | |
1754 | au0828_isocdbg("submit of urb %i failed (error=%i)\n", | |
1755 | i, rc); | |
1756 | au0828_uninit_isoc(dev); | |
1757 | } | |
1758 | } | |
1759 | } | |
1760 | } | |
1761 | ||
8b2f0795 DH |
1762 | static struct v4l2_file_operations au0828_v4l_fops = { |
1763 | .owner = THIS_MODULE, | |
1764 | .open = au0828_v4l2_open, | |
1765 | .release = au0828_v4l2_close, | |
05439b1a SK |
1766 | .read = vb2_fop_read, |
1767 | .poll = vb2_fop_poll, | |
1768 | .mmap = vb2_fop_mmap, | |
549ee4df | 1769 | .unlocked_ioctl = video_ioctl2, |
8b2f0795 DH |
1770 | }; |
1771 | ||
1772 | static const struct v4l2_ioctl_ops video_ioctl_ops = { | |
1773 | .vidioc_querycap = vidioc_querycap, | |
1774 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | |
1775 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
1776 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
1777 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
5a5a4e16 | 1778 | .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, |
8d86e4e8 | 1779 | .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, |
7f8eacd2 | 1780 | .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, |
a2cf96f9 | 1781 | .vidioc_enumaudio = vidioc_enumaudio, |
8b2f0795 DH |
1782 | .vidioc_g_audio = vidioc_g_audio, |
1783 | .vidioc_s_audio = vidioc_s_audio, | |
1784 | .vidioc_cropcap = vidioc_cropcap, | |
05439b1a SK |
1785 | |
1786 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1787 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1788 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
1789 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1790 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1791 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1792 | .vidioc_expbuf = vb2_ioctl_expbuf, | |
1793 | ||
8b2f0795 | 1794 | .vidioc_s_std = vidioc_s_std, |
33b6b89b | 1795 | .vidioc_g_std = vidioc_g_std, |
8b2f0795 DH |
1796 | .vidioc_enum_input = vidioc_enum_input, |
1797 | .vidioc_g_input = vidioc_g_input, | |
1798 | .vidioc_s_input = vidioc_s_input, | |
05439b1a SK |
1799 | |
1800 | .vidioc_streamon = vb2_ioctl_streamon, | |
1801 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1802 | ||
8b2f0795 DH |
1803 | .vidioc_g_tuner = vidioc_g_tuner, |
1804 | .vidioc_s_tuner = vidioc_s_tuner, | |
1805 | .vidioc_g_frequency = vidioc_g_frequency, | |
1806 | .vidioc_s_frequency = vidioc_s_frequency, | |
1807 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
1808 | .vidioc_g_register = vidioc_g_register, | |
1809 | .vidioc_s_register = vidioc_s_register, | |
8b2f0795 | 1810 | #endif |
83b09422 HV |
1811 | .vidioc_log_status = vidioc_log_status, |
1812 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
1813 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
8b2f0795 DH |
1814 | }; |
1815 | ||
1816 | static const struct video_device au0828_video_template = { | |
1817 | .fops = &au0828_v4l_fops, | |
41071bb8 | 1818 | .release = video_device_release_empty, |
8b2f0795 | 1819 | .ioctl_ops = &video_ioctl_ops, |
f2fd7ce6 | 1820 | .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M, |
8b2f0795 DH |
1821 | }; |
1822 | ||
05439b1a SK |
1823 | static int au0828_vb2_setup(struct au0828_dev *dev) |
1824 | { | |
1825 | int rc; | |
1826 | struct vb2_queue *q; | |
1827 | ||
1828 | /* Setup Videobuf2 for Video capture */ | |
1829 | q = &dev->vb_vidq; | |
1830 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1831 | q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1832 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1833 | q->drv_priv = dev; | |
1834 | q->buf_struct_size = sizeof(struct au0828_buffer); | |
1835 | q->ops = &au0828_video_qops; | |
1836 | q->mem_ops = &vb2_vmalloc_memops; | |
1837 | ||
1838 | rc = vb2_queue_init(q); | |
1839 | if (rc < 0) | |
1840 | return rc; | |
1841 | ||
1842 | /* Setup Videobuf2 for VBI capture */ | |
1843 | q = &dev->vb_vbiq; | |
1844 | q->type = V4L2_BUF_TYPE_VBI_CAPTURE; | |
1845 | q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1846 | q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1847 | q->drv_priv = dev; | |
1848 | q->buf_struct_size = sizeof(struct au0828_buffer); | |
1849 | q->ops = &au0828_vbi_qops; | |
1850 | q->mem_ops = &vb2_vmalloc_memops; | |
1851 | ||
1852 | rc = vb2_queue_init(q); | |
1853 | if (rc < 0) | |
1854 | return rc; | |
1855 | ||
1856 | return 0; | |
1857 | } | |
1858 | ||
d1f33737 MCC |
1859 | static void au0828_analog_create_entities(struct au0828_dev *dev) |
1860 | { | |
1861 | #if defined(CONFIG_MEDIA_CONTROLLER) | |
1862 | static const char * const inames[] = { | |
1863 | [AU0828_VMUX_COMPOSITE] = "Composite", | |
1864 | [AU0828_VMUX_SVIDEO] = "S-Video", | |
1865 | [AU0828_VMUX_CABLE] = "Cable TV", | |
1866 | [AU0828_VMUX_TELEVISION] = "Television", | |
1867 | [AU0828_VMUX_DVB] = "DVB", | |
d1f33737 MCC |
1868 | }; |
1869 | int ret, i; | |
1870 | ||
1871 | /* Initialize Video and VBI pads */ | |
1872 | dev->video_pad.flags = MEDIA_PAD_FL_SINK; | |
ab22e77c | 1873 | ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad); |
d1f33737 MCC |
1874 | if (ret < 0) |
1875 | pr_err("failed to initialize video media entity!\n"); | |
1876 | ||
1877 | dev->vbi_pad.flags = MEDIA_PAD_FL_SINK; | |
ab22e77c | 1878 | ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad); |
d1f33737 MCC |
1879 | if (ret < 0) |
1880 | pr_err("failed to initialize vbi media entity!\n"); | |
1881 | ||
1882 | /* Create entities for each input connector */ | |
1883 | for (i = 0; i < AU0828_MAX_INPUT; i++) { | |
1884 | struct media_entity *ent = &dev->input_ent[i]; | |
1885 | ||
1886 | if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED) | |
1887 | break; | |
1888 | ||
1889 | ent->name = inames[AUVI_INPUT(i).type]; | |
1890 | ent->flags = MEDIA_ENT_FL_CONNECTOR; | |
1891 | dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE; | |
1892 | ||
1893 | switch (AUVI_INPUT(i).type) { | |
1894 | case AU0828_VMUX_COMPOSITE: | |
4ca72efa | 1895 | ent->function = MEDIA_ENT_F_CONN_COMPOSITE; |
d1f33737 MCC |
1896 | break; |
1897 | case AU0828_VMUX_SVIDEO: | |
4ca72efa | 1898 | ent->function = MEDIA_ENT_F_CONN_SVIDEO; |
d1f33737 MCC |
1899 | break; |
1900 | case AU0828_VMUX_CABLE: | |
1901 | case AU0828_VMUX_TELEVISION: | |
1902 | case AU0828_VMUX_DVB: | |
34ac2532 | 1903 | default: /* Just to shut up a warning */ |
4ca72efa | 1904 | ent->function = MEDIA_ENT_F_CONN_RF; |
d1f33737 | 1905 | break; |
d1f33737 MCC |
1906 | } |
1907 | ||
ab22e77c | 1908 | ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]); |
d1f33737 MCC |
1909 | if (ret < 0) |
1910 | pr_err("failed to initialize input pad[%d]!\n", i); | |
1911 | ||
1912 | ret = media_device_register_entity(dev->media_dev, ent); | |
1913 | if (ret < 0) | |
1914 | pr_err("failed to register input entity %d!\n", i); | |
1915 | } | |
1916 | #endif | |
1917 | } | |
1918 | ||
8b2f0795 DH |
1919 | /**************************************************************************/ |
1920 | ||
fc4ce6cd DH |
1921 | int au0828_analog_register(struct au0828_dev *dev, |
1922 | struct usb_interface *interface) | |
8b2f0795 DH |
1923 | { |
1924 | int retval = -ENOMEM; | |
fc4ce6cd DH |
1925 | struct usb_host_interface *iface_desc; |
1926 | struct usb_endpoint_descriptor *endpoint; | |
d63b21bf | 1927 | int i, ret; |
8b2f0795 | 1928 | |
1fe3a8fe MCC |
1929 | dprintk(1, "au0828_analog_register called for intf#%d!\n", |
1930 | interface->cur_altsetting->desc.bInterfaceNumber); | |
8b2f0795 | 1931 | |
7b606ffd MCC |
1932 | /* No analog TV */ |
1933 | if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED) | |
1934 | return 0; | |
1935 | ||
fc4ce6cd DH |
1936 | /* set au0828 usb interface0 to as5 */ |
1937 | retval = usb_set_interface(dev->usbdev, | |
62899a28 | 1938 | interface->cur_altsetting->desc.bInterfaceNumber, 5); |
fc4ce6cd | 1939 | if (retval != 0) { |
83afb32a | 1940 | pr_info("Failure setting usb interface0 to as5\n"); |
fc4ce6cd DH |
1941 | return retval; |
1942 | } | |
1943 | ||
1944 | /* Figure out which endpoint has the isoc interface */ | |
1945 | iface_desc = interface->cur_altsetting; | |
62899a28 | 1946 | for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) { |
fc4ce6cd | 1947 | endpoint = &iface_desc->endpoint[i].desc; |
62899a28 DH |
1948 | if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
1949 | == USB_DIR_IN) && | |
1950 | ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
1951 | == USB_ENDPOINT_XFER_ISOC)) { | |
fc4ce6cd DH |
1952 | |
1953 | /* we find our isoc in endpoint */ | |
1954 | u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize); | |
62899a28 DH |
1955 | dev->max_pkt_size = (tmp & 0x07ff) * |
1956 | (((tmp & 0x1800) >> 11) + 1); | |
fc4ce6cd | 1957 | dev->isoc_in_endpointaddr = endpoint->bEndpointAddress; |
1fe3a8fe MCC |
1958 | dprintk(1, |
1959 | "Found isoc endpoint 0x%02x, max size = %d\n", | |
1960 | dev->isoc_in_endpointaddr, dev->max_pkt_size); | |
fc4ce6cd DH |
1961 | } |
1962 | } | |
62899a28 | 1963 | if (!(dev->isoc_in_endpointaddr)) { |
83afb32a | 1964 | pr_info("Could not locate isoc endpoint\n"); |
fc4ce6cd DH |
1965 | return -ENODEV; |
1966 | } | |
1967 | ||
8b2f0795 DH |
1968 | init_waitqueue_head(&dev->open); |
1969 | spin_lock_init(&dev->slock); | |
8b2f0795 | 1970 | |
7f8eacd2 | 1971 | /* init video dma queues */ |
8b2f0795 | 1972 | INIT_LIST_HEAD(&dev->vidq.active); |
7f8eacd2 | 1973 | INIT_LIST_HEAD(&dev->vbiq.active); |
8b2f0795 | 1974 | |
30a8f2a0 JL |
1975 | setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout, |
1976 | (unsigned long)dev); | |
1977 | setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout, | |
1978 | (unsigned long)dev); | |
78ca5005 | 1979 | |
8b2f0795 DH |
1980 | dev->width = NTSC_STD_W; |
1981 | dev->height = NTSC_STD_H; | |
1982 | dev->field_size = dev->width * dev->height; | |
1983 | dev->frame_size = dev->field_size << 1; | |
1984 | dev->bytesperline = dev->width << 1; | |
de8d2bbf HV |
1985 | dev->vbi_width = 720; |
1986 | dev->vbi_height = 1; | |
8b2f0795 | 1987 | dev->ctrl_ainput = 0; |
e1cf6587 | 1988 | dev->ctrl_freq = 960; |
33b6b89b | 1989 | dev->std = V4L2_STD_NTSC_M; |
174ced21 | 1990 | /* Default input is TV Tuner */ |
56230d1e | 1991 | au0828_s_input(dev, 0); |
8b2f0795 | 1992 | |
05439b1a SK |
1993 | mutex_init(&dev->vb_queue_lock); |
1994 | mutex_init(&dev->vb_vbi_queue_lock); | |
1995 | ||
8b2f0795 | 1996 | /* Fill the video capture device struct */ |
41071bb8 SK |
1997 | dev->vdev = au0828_video_template; |
1998 | dev->vdev.v4l2_dev = &dev->v4l2_dev; | |
1999 | dev->vdev.lock = &dev->lock; | |
2000 | dev->vdev.queue = &dev->vb_vidq; | |
2001 | dev->vdev.queue->lock = &dev->vb_queue_lock; | |
2002 | strcpy(dev->vdev.name, "au0828a video"); | |
8b2f0795 DH |
2003 | |
2004 | /* Setup the VBI device */ | |
41071bb8 SK |
2005 | dev->vbi_dev = au0828_video_template; |
2006 | dev->vbi_dev.v4l2_dev = &dev->v4l2_dev; | |
2007 | dev->vbi_dev.lock = &dev->lock; | |
2008 | dev->vbi_dev.queue = &dev->vb_vbiq; | |
2009 | dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock; | |
2010 | strcpy(dev->vbi_dev.name, "au0828a vbi"); | |
8b2f0795 | 2011 | |
d1f33737 MCC |
2012 | /* Init entities at the Media Controller */ |
2013 | au0828_analog_create_entities(dev); | |
bed69196 | 2014 | |
05439b1a SK |
2015 | /* initialize videobuf2 stuff */ |
2016 | retval = au0828_vb2_setup(dev); | |
2017 | if (retval != 0) { | |
2018 | dprintk(1, "unable to setup videobuf2 queues (error = %d).\n", | |
2019 | retval); | |
41071bb8 | 2020 | return -ENODEV; |
05439b1a SK |
2021 | } |
2022 | ||
8b2f0795 | 2023 | /* Register the v4l2 device */ |
41071bb8 SK |
2024 | video_set_drvdata(&dev->vdev, dev); |
2025 | retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); | |
62899a28 DH |
2026 | if (retval != 0) { |
2027 | dprintk(1, "unable to register video device (error = %d).\n", | |
2028 | retval); | |
d63b21bf | 2029 | ret = -ENODEV; |
05439b1a | 2030 | goto err_reg_vdev; |
8b2f0795 DH |
2031 | } |
2032 | ||
2033 | /* Register the vbi device */ | |
41071bb8 SK |
2034 | video_set_drvdata(&dev->vbi_dev, dev); |
2035 | retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1); | |
62899a28 DH |
2036 | if (retval != 0) { |
2037 | dprintk(1, "unable to register vbi device (error = %d).\n", | |
2038 | retval); | |
d63b21bf | 2039 | ret = -ENODEV; |
05439b1a | 2040 | goto err_reg_vbi_dev; |
8b2f0795 | 2041 | } |
9822f417 MCC |
2042 | |
2043 | #ifdef CONFIG_MEDIA_CONTROLLER | |
2044 | retval = v4l2_mc_create_media_graph(dev->media_dev); | |
7b606ffd MCC |
2045 | if (retval) { |
2046 | pr_err("%s() au0282_dev_register failed to create graph\n", | |
2047 | __func__); | |
2048 | ret = -ENODEV; | |
2049 | goto err_reg_vbi_dev; | |
2050 | } | |
9822f417 | 2051 | #endif |
8b2f0795 | 2052 | |
62899a28 | 2053 | dprintk(1, "%s completed!\n", __func__); |
8b2f0795 DH |
2054 | |
2055 | return 0; | |
d63b21bf | 2056 | |
05439b1a | 2057 | err_reg_vbi_dev: |
41071bb8 | 2058 | video_unregister_device(&dev->vdev); |
05439b1a SK |
2059 | err_reg_vdev: |
2060 | vb2_queue_release(&dev->vb_vidq); | |
2061 | vb2_queue_release(&dev->vb_vbiq); | |
d63b21bf | 2062 | return ret; |
8b2f0795 DH |
2063 | } |
2064 |