]>
Commit | Line | Data |
---|---|---|
9701dc94 MCC |
1 | /* |
2 | tm6000-video.c - driver for TM5600/TM6000 USB video capture devices | |
3 | ||
4 | Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org> | |
5 | ||
7c3f53ec ML |
6 | Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com> |
7 | - Fixed module load/unload | |
8 | ||
9701dc94 MCC |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation version 2 | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | #include <linux/module.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/kernel.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/mm.h> | |
29 | #include <linux/ioport.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/sched.h> | |
32 | #include <linux/random.h> | |
33 | #include <linux/version.h> | |
34 | #include <linux/usb.h> | |
35 | #include <linux/videodev2.h> | |
36 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | |
37 | #include <linux/videodev.h> | |
38 | #endif | |
39 | #include <linux/interrupt.h> | |
40 | #include <linux/kthread.h> | |
41 | #include <linux/highmem.h> | |
42 | #include <linux/freezer.h> | |
43 | ||
44 | #include "tm6000-regs.h" | |
45 | #include "tm6000.h" | |
46 | ||
47 | #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ | |
48 | ||
49 | /* Declare static vars that will be used as parameters */ | |
50 | static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ | |
51 | static int video_nr = -1; /* /dev/videoN, -1 for autodetect */ | |
52 | ||
53 | unsigned long tm6000_devused; | |
54 | ||
55 | /* Debug level */ | |
56 | int tm6000_debug; | |
57 | ||
58 | /* supported controls */ | |
59 | static struct v4l2_queryctrl tm6000_qctrl[] = { | |
60 | { | |
61 | .id = V4L2_CID_BRIGHTNESS, | |
62 | .type = V4L2_CTRL_TYPE_INTEGER, | |
63 | .name = "Brightness", | |
64 | .minimum = 0, | |
65 | .maximum = 255, | |
66 | .step = 1, | |
67 | .default_value = 54, | |
68 | .flags = 0, | |
69 | }, { | |
70 | .id = V4L2_CID_CONTRAST, | |
71 | .type = V4L2_CTRL_TYPE_INTEGER, | |
72 | .name = "Contrast", | |
73 | .minimum = 0, | |
74 | .maximum = 255, | |
75 | .step = 0x1, | |
76 | .default_value = 119, | |
77 | .flags = 0, | |
78 | }, { | |
79 | .id = V4L2_CID_SATURATION, | |
80 | .type = V4L2_CTRL_TYPE_INTEGER, | |
81 | .name = "Saturation", | |
82 | .minimum = 0, | |
83 | .maximum = 255, | |
84 | .step = 0x1, | |
85 | .default_value = 112, | |
86 | .flags = 0, | |
87 | }, { | |
88 | .id = V4L2_CID_HUE, | |
89 | .type = V4L2_CTRL_TYPE_INTEGER, | |
90 | .name = "Hue", | |
91 | .minimum = -128, | |
92 | .maximum = 127, | |
93 | .step = 0x1, | |
94 | .default_value = 0, //4 ? | |
95 | .flags = 0, | |
96 | } | |
97 | }; | |
98 | ||
99 | static int qctl_regs[ARRAY_SIZE(tm6000_qctrl)]; | |
100 | ||
101 | static struct tm6000_fmt format[] = { | |
102 | { | |
103 | .name = "4:2:2, packed, YVY2", | |
104 | .fourcc = V4L2_PIX_FMT_YUYV, | |
105 | .depth = 16, | |
106 | },{ | |
107 | .name = "4:2:2, packed, UYVY", | |
108 | .fourcc = V4L2_PIX_FMT_UYVY, | |
109 | .depth = 16, | |
110 | },{ | |
111 | .name = "A/V + VBI mux packet", | |
112 | .fourcc = V4L2_PIX_FMT_TM6000, | |
113 | .depth = 16, | |
114 | } | |
115 | }; | |
116 | ||
117 | static LIST_HEAD(tm6000_corelist); | |
118 | ||
119 | /* ------------------------------------------------------------------ | |
120 | DMA and thread functions | |
121 | ------------------------------------------------------------------*/ | |
122 | ||
123 | #define norm_maxw(a) 720 | |
124 | #define norm_maxh(a) 480 | |
125 | ||
126 | //#define norm_minw(a) norm_maxw(a) | |
127 | #define norm_minw(a) norm_maxw(a) | |
128 | #define norm_minh(a) norm_maxh(a) | |
129 | ||
130 | /* | |
131 | * video-buf generic routine to get the next available buffer | |
132 | */ | |
133 | static int inline get_next_buf (struct tm6000_dmaqueue *dma_q, | |
134 | struct tm6000_buffer **buf) | |
135 | { | |
136 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
137 | ||
138 | if (list_empty(&dma_q->active)) { | |
139 | dprintk(dev, V4L2_DEBUG_QUEUE,"No active queue to serve\n"); | |
140 | return 0; | |
141 | } | |
142 | ||
143 | *buf = list_entry(dma_q->active.next, | |
144 | struct tm6000_buffer, vb.queue); | |
145 | ||
146 | /* Nobody is waiting something to be done, just return */ | |
147 | if (!waitqueue_active(&(*buf)->vb.done)) { | |
148 | mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); | |
149 | return -1; | |
150 | } | |
151 | ||
152 | return 1; | |
153 | } | |
154 | ||
155 | /* | |
156 | * Announces that a buffer were filled and request the next | |
157 | */ | |
158 | static void inline buffer_filled (struct tm6000_core *dev, | |
159 | struct tm6000_buffer *buf) | |
160 | { | |
161 | /* Advice that buffer was filled */ | |
162 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] wakeup\n",buf,buf->vb.i); | |
163 | buf->vb.state = STATE_DONE; | |
164 | buf->vb.field_count++; | |
165 | do_gettimeofday(&buf->vb.ts); | |
166 | ||
167 | list_del(&buf->vb.queue); | |
168 | wake_up(&buf->vb.done); | |
169 | } | |
170 | ||
171 | /* | |
172 | * Macro to allow copying data into the proper memory type | |
173 | */ | |
174 | ||
175 | #define bufcpy(buf,out_ptr,in_ptr,size) \ | |
176 | { \ | |
177 | if (__copy_to_user(out_ptr,in_ptr,size)!=0) \ | |
178 | tm6000_err("copy_to_user failed.\n"); \ | |
179 | } | |
180 | ||
181 | /* | |
182 | * Identify the tm5600/6000 buffer header type and properly handles | |
183 | */ | |
184 | static int copy_streams(u8 *data, u8 *out_p, unsigned long len, | |
185 | struct urb *urb, struct tm6000_buffer **buf) | |
186 | { | |
187 | struct tm6000_dmaqueue *dma_q = urb->context; | |
188 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
189 | u8 *ptr=data, *endp=data+len; | |
190 | u8 c; | |
191 | unsigned int cmd, cpysize, pktsize, size, field, block, line, pos=0; | |
192 | unsigned long header; | |
193 | int rc=0; | |
194 | ||
195 | /* FIXME: this is the hardcoded window size | |
196 | */ | |
197 | unsigned int linesize=720*2; | |
198 | ||
199 | //static int last_line=-2; | |
200 | ||
201 | for (ptr=data; ptr<endp;) { | |
202 | if (!dev->isoc_ctl.cmd) { | |
203 | /* Seek for sync */ | |
204 | for (ptr+=3;ptr<endp;ptr++) { | |
205 | if (*ptr==0x47) { | |
206 | ptr-=3; | |
207 | break; | |
208 | } | |
209 | } | |
210 | if (ptr>=endp) | |
211 | return rc; | |
212 | ||
213 | /* Get message header */ | |
214 | header=*(unsigned long *)ptr; | |
215 | ptr+=4; | |
216 | c=(header>>24) & 0xff; | |
217 | ||
218 | /* split the header fields */ | |
219 | size = (((header & 0x7e)<<1) -1) *4; | |
220 | block = (header>>7) & 0xf; | |
221 | field = (header>>11) & 0x1; | |
222 | line = (header>>12) & 0x1ff; | |
223 | cmd = (header>>21) & 0x7; | |
224 | ||
225 | /* FIXME: Maximum possible line is 511. | |
226 | * This doesn't seem to be enough for PAL standards | |
227 | */ | |
228 | ||
229 | /* Validates header fields */ | |
230 | if(size>TM6000_URB_MSG_LEN) | |
231 | size=TM6000_URB_MSG_LEN; | |
232 | if(block>=8) | |
233 | cmd = TM6000_URB_MSG_ERR; | |
234 | ||
235 | /* FIXME: Mounts the image as field0+field1 | |
236 | * It should, instead, check if the user selected | |
237 | * entrelaced or non-entrelaced mode | |
238 | */ | |
239 | pos=((line<<1)+field)*linesize+ | |
240 | block*TM6000_URB_MSG_LEN; | |
241 | ||
242 | ||
243 | ||
244 | /* Don't allow to write out of the buffer */ | |
245 | if (pos+TM6000_URB_MSG_LEN > (*buf)->vb.size) | |
246 | cmd = TM6000_URB_MSG_ERR; | |
247 | ||
248 | /* Prints debug info */ | |
249 | dprintk(dev, V4L2_DEBUG_ISOC, "size=%d, num=%d, " | |
250 | " line=%d, field=%d\n", | |
251 | size, block, line, field); | |
252 | ||
253 | dev->isoc_ctl.cmd = cmd; | |
254 | dev->isoc_ctl.size = size; | |
255 | dev->isoc_ctl.pos = pos; | |
256 | dev->isoc_ctl.pktsize = pktsize = TM6000_URB_MSG_LEN; | |
257 | } else { | |
258 | cmd = dev->isoc_ctl.cmd; | |
259 | size= dev->isoc_ctl.size; | |
260 | pos = dev->isoc_ctl.pos; | |
261 | pktsize = dev->isoc_ctl.pktsize; | |
262 | } | |
263 | cpysize=(endp-ptr>size)?size:endp-ptr; | |
264 | ||
265 | if (cpysize) { | |
266 | /* handles each different URB message */ | |
267 | switch(cmd) { | |
268 | case TM6000_URB_MSG_VIDEO: | |
269 | /* Fills video buffer */ | |
270 | bufcpy(*buf,&out_p[pos],ptr,cpysize); | |
271 | break; | |
272 | } | |
273 | } | |
274 | if (cpysize<size) { | |
275 | /* End of URB packet, but cmd processing is not | |
276 | * complete. Preserve the state for a next packet | |
277 | */ | |
278 | dev->isoc_ctl.pos = pos+cpysize; | |
279 | dev->isoc_ctl.size= size-cpysize; | |
280 | dev->isoc_ctl.cmd = cmd; | |
281 | dev->isoc_ctl.pktsize = pktsize-cpysize; | |
282 | ptr+=cpysize; | |
283 | } else { | |
284 | dev->isoc_ctl.cmd = 0; | |
285 | ptr+=pktsize; | |
286 | } | |
287 | } | |
288 | ||
289 | return rc; | |
290 | } | |
291 | /* | |
292 | * Identify the tm5600/6000 buffer header type and properly handles | |
293 | */ | |
294 | static int copy_multiplexed(u8 *ptr, u8 *out_p, unsigned long len, | |
295 | struct urb *urb, struct tm6000_buffer **buf) | |
296 | { | |
297 | struct tm6000_dmaqueue *dma_q = urb->context; | |
298 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
299 | unsigned int pos=dev->isoc_ctl.pos,cpysize; | |
300 | int rc=1; | |
301 | ||
302 | while (len>0) { | |
303 | cpysize=min(len,(*buf)->vb.size-pos); | |
304 | //printk("Copying %d bytes (max=%lu) from %p to %p[%u]\n",cpysize,(*buf)->vb.size,ptr,out_p,pos); | |
305 | bufcpy(*buf,&out_p[pos],ptr,cpysize); | |
306 | pos+=cpysize; | |
307 | ptr+=cpysize; | |
308 | len-=cpysize; | |
309 | if (pos >= (*buf)->vb.size) { | |
310 | pos=0; | |
311 | /* Announces that a new buffer were filled */ | |
312 | buffer_filled (dev, *buf); | |
313 | dprintk(dev, V4L2_DEBUG_QUEUE, "new buffer filled\n"); | |
314 | ||
315 | rc=get_next_buf (dma_q, buf); | |
316 | if (rc<=0) { | |
317 | *buf=NULL; | |
318 | printk(KERN_ERR "tm6000: buffer underrun\n"); | |
319 | break; | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
324 | dev->isoc_ctl.pos=pos; | |
325 | return rc; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Controls the isoc copy of each urb packet | |
330 | */ | |
331 | static inline int tm6000_isoc_copy(struct urb *urb, struct tm6000_buffer **buf) | |
332 | { | |
333 | struct tm6000_dmaqueue *dma_q = urb->context; | |
334 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
335 | void *outp=videobuf_to_vmalloc (&((*buf)->vb)); | |
336 | int i, len=0, rc=1; | |
337 | int size=(*buf)->vb.size; | |
338 | char *p; | |
339 | unsigned long copied; | |
340 | ||
341 | copied=0; | |
342 | ||
343 | ||
344 | for (i = 0; i < urb->number_of_packets; i++) { | |
345 | int status = urb->iso_frame_desc[i].status; | |
346 | char *errmsg = "Unknown"; | |
347 | ||
348 | switch(status) { | |
349 | case -ENOENT: | |
350 | errmsg = "unlinked synchronuously"; | |
351 | break; | |
352 | case -ECONNRESET: | |
353 | errmsg = "unlinked asynchronuously"; | |
354 | break; | |
355 | case -ENOSR: | |
356 | errmsg = "Buffer error (overrun)"; | |
357 | break; | |
358 | case -EPIPE: | |
359 | errmsg = "Stalled (device not responding)"; | |
360 | break; | |
361 | case -EOVERFLOW: | |
362 | errmsg = "Babble (bad cable?)"; | |
363 | break; | |
364 | case -EPROTO: | |
365 | errmsg = "Bit-stuff error (bad cable?)"; | |
366 | break; | |
367 | case -EILSEQ: | |
368 | errmsg = "CRC/Timeout (could be anything)"; | |
369 | break; | |
370 | case -ETIME: | |
371 | errmsg = "Device does not respond"; | |
372 | break; | |
373 | } | |
374 | dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n", | |
375 | status, errmsg); | |
376 | ||
377 | if (status<0) | |
378 | continue; | |
379 | ||
380 | len=urb->iso_frame_desc[i].actual_length; | |
381 | ||
382 | if (len>=TM6000_URB_MSG_LEN) { | |
383 | p=urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
384 | if (!urb->iso_frame_desc[i].status) { | |
385 | if (((*buf)->fmt->fourcc)==V4L2_PIX_FMT_TM6000) { | |
386 | rc=copy_multiplexed(p,outp,len,urb,buf); | |
387 | if (rc<=0) | |
388 | return rc; | |
389 | } else { | |
390 | rc=copy_streams(p,outp,len,urb,buf); | |
391 | } | |
392 | } | |
393 | copied += len; | |
394 | if (copied>=size) | |
395 | break; | |
396 | } | |
397 | } | |
398 | ||
399 | if (((*buf)->fmt->fourcc)!=V4L2_PIX_FMT_TM6000) { | |
400 | buffer_filled (dev, *buf); | |
401 | dprintk(dev, V4L2_DEBUG_QUEUE, "new buffer filled\n"); | |
402 | } | |
403 | ||
404 | return rc; | |
405 | } | |
406 | ||
407 | /* ------------------------------------------------------------------ | |
408 | URB control | |
409 | ------------------------------------------------------------------*/ | |
410 | ||
411 | /* | |
412 | * IRQ callback, called by URB callback | |
413 | */ | |
414 | static void tm6000_irq_callback(struct urb *urb) | |
415 | { | |
416 | struct tm6000_buffer *buf; | |
417 | struct tm6000_dmaqueue *dma_q = urb->context; | |
418 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
419 | int rc,i; | |
420 | unsigned long flags; | |
421 | ||
422 | spin_lock_irqsave(&dev->slock,flags); | |
423 | ||
424 | rc=get_next_buf (dma_q, &buf); | |
425 | if (rc<=0) | |
426 | goto ret; | |
427 | ||
428 | /* Copy data from URB */ | |
429 | rc=tm6000_isoc_copy(urb, &buf); | |
430 | ||
431 | ret: | |
432 | /* Reset urb buffers */ | |
433 | for (i = 0; i < urb->number_of_packets; i++) { | |
434 | urb->iso_frame_desc[i].status = 0; | |
435 | urb->iso_frame_desc[i].actual_length = 0; | |
436 | } | |
437 | urb->status = 0; | |
438 | ||
439 | if ((urb->status = usb_submit_urb(urb, GFP_ATOMIC))) { | |
440 | tm6000_err("urb resubmit failed (error=%i)\n", | |
441 | urb->status); | |
442 | } | |
443 | ||
444 | if (rc>=0) { | |
445 | if (!rc) { | |
446 | dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n"); | |
447 | del_timer(&dma_q->timeout); | |
448 | } else { | |
449 | /* Data filled, reset watchdog */ | |
450 | mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); | |
451 | } | |
452 | } | |
453 | spin_unlock_irqrestore(&dev->slock,flags); | |
454 | } | |
455 | ||
456 | /* | |
457 | * Stop and Deallocate URBs | |
458 | */ | |
459 | static void tm6000_uninit_isoc(struct tm6000_core *dev) | |
460 | { | |
461 | struct urb *urb; | |
462 | int i; | |
463 | ||
464 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
465 | urb=dev->isoc_ctl.urb[i]; | |
466 | if (urb) { | |
467 | usb_kill_urb(urb); | |
468 | usb_unlink_urb(urb); | |
469 | if (dev->isoc_ctl.transfer_buffer[i]) { | |
470 | usb_buffer_free(dev->udev, | |
471 | urb->transfer_buffer_length, | |
472 | dev->isoc_ctl.transfer_buffer[i], | |
473 | urb->transfer_dma); | |
474 | } | |
475 | usb_free_urb(urb); | |
476 | dev->isoc_ctl.urb[i] = NULL; | |
477 | } | |
478 | dev->isoc_ctl.transfer_buffer[i] = NULL; | |
479 | } | |
480 | ||
481 | kfree (dev->isoc_ctl.urb); | |
482 | kfree (dev->isoc_ctl.transfer_buffer); | |
483 | dev->isoc_ctl.urb=NULL; | |
484 | dev->isoc_ctl.transfer_buffer=NULL; | |
485 | ||
486 | dev->isoc_ctl.num_bufs=0; | |
487 | } | |
488 | ||
489 | /* | |
490 | * Stop video thread - FIXME: Can be easily removed | |
491 | */ | |
492 | static void tm6000_stop_thread(struct tm6000_dmaqueue *dma_q) | |
493 | { | |
494 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
495 | ||
496 | tm6000_uninit_isoc(dev); | |
497 | } | |
498 | ||
499 | ||
500 | /* | |
501 | * Allocate URBs and start IRQ | |
502 | */ | |
503 | static int tm6000_prepare_isoc(struct tm6000_core *dev, | |
504 | int max_packets, int num_bufs) | |
505 | { | |
506 | struct tm6000_dmaqueue *dma_q = &dev->vidq; | |
507 | int i; | |
508 | int sb_size, pipe; | |
509 | struct urb *urb; | |
510 | int j, k; | |
511 | ||
512 | /* De-allocates all pending stuff */ | |
513 | tm6000_uninit_isoc(dev); | |
514 | ||
515 | dev->isoc_ctl.num_bufs=num_bufs; | |
516 | ||
517 | dev->isoc_ctl.urb=kmalloc(sizeof(void *)*num_bufs, | |
518 | GFP_KERNEL); | |
519 | if (!dev->isoc_ctl.urb) { | |
520 | tm6000_err("cannot alloc memory for usb buffers\n"); | |
521 | return -ENOMEM; | |
522 | } | |
523 | ||
524 | dev->isoc_ctl.transfer_buffer=kmalloc(sizeof(void *)*num_bufs, | |
525 | GFP_KERNEL); | |
526 | if (!dev->isoc_ctl.urb) { | |
527 | tm6000_err("cannot allocate memory for usbtransfer\n"); | |
528 | kfree(dev->isoc_ctl.urb); | |
529 | return -ENOMEM; | |
530 | } | |
531 | ||
532 | dev->isoc_ctl.max_pkt_size=dev->max_isoc_in; | |
533 | ||
534 | sb_size = max_packets * dev->isoc_ctl.max_pkt_size; | |
535 | ||
536 | ||
537 | /* allocate urbs and transfer buffers */ | |
538 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
539 | urb = usb_alloc_urb(max_packets, GFP_KERNEL); | |
540 | if (!urb) { | |
541 | tm6000_err("cannot alloc isoc_ctl.urb %i\n", i); | |
542 | tm6000_uninit_isoc(dev); | |
543 | return -ENOMEM; | |
544 | } | |
545 | dev->isoc_ctl.urb[i] = urb; | |
546 | ||
547 | dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev, | |
548 | sb_size, GFP_KERNEL, | |
549 | &dev->isoc_ctl.urb[i]->transfer_dma); | |
550 | if (!dev->isoc_ctl.transfer_buffer[i]) { | |
551 | tm6000_err ("unable to allocate %i bytes for transfer" | |
552 | " buffer %i\n", sb_size, i); | |
553 | tm6000_uninit_isoc(dev); | |
554 | return -ENOMEM; | |
555 | } | |
556 | memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size); | |
557 | ||
558 | pipe=usb_rcvisocpipe(dev->udev, | |
559 | dev->isoc_in->desc.bEndpointAddress & | |
560 | USB_ENDPOINT_NUMBER_MASK); | |
561 | usb_fill_int_urb(urb, dev->udev, pipe, | |
562 | dev->isoc_ctl.transfer_buffer[i],sb_size, | |
563 | tm6000_irq_callback, dma_q, | |
564 | dev->isoc_in->desc.bInterval); | |
565 | ||
566 | urb->number_of_packets = max_packets; | |
567 | urb->transfer_flags = URB_ISO_ASAP; | |
568 | ||
569 | k = 0; | |
570 | for (j = 0; j < max_packets; j++) { | |
571 | urb->iso_frame_desc[j].offset = k; | |
572 | urb->iso_frame_desc[j].length = | |
573 | dev->isoc_ctl.max_pkt_size; | |
574 | k += dev->isoc_ctl.max_pkt_size; | |
575 | } | |
576 | } | |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static int tm6000_start_thread( struct tm6000_dmaqueue *dma_q, | |
582 | struct tm6000_buffer *buf) | |
583 | { | |
584 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
585 | int i,rc; | |
586 | ||
587 | dma_q->frame=0; | |
588 | dma_q->ini_jiffies=jiffies; | |
589 | ||
590 | init_waitqueue_head(&dma_q->wq); | |
591 | ||
592 | /* submit urbs and enables IRQ */ | |
593 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { | |
2cd4fd1e | 594 | rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); |
9701dc94 MCC |
595 | if (rc) { |
596 | tm6000_err("submit of urb %i failed (error=%i)\n", i, | |
597 | rc); | |
598 | tm6000_uninit_isoc(dev); | |
599 | return rc; | |
600 | } | |
601 | } | |
602 | ||
603 | if (rc<0) | |
604 | return rc; | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
609 | static int restart_video_queue(struct tm6000_dmaqueue *dma_q) | |
610 | { | |
611 | struct tm6000_core *dev= container_of(dma_q,struct tm6000_core,vidq); | |
612 | ||
613 | struct tm6000_buffer *buf, *prev; | |
614 | struct list_head *item; | |
615 | ||
616 | dprintk(dev, V4L2_DEBUG_QUEUE, "%s dma_q=0x%08lx\n", | |
617 | __FUNCTION__,(unsigned long)dma_q); | |
618 | ||
619 | if (!list_empty(&dma_q->active)) { | |
620 | buf = list_entry(dma_q->active.next, struct tm6000_buffer, vb.queue); | |
621 | dprintk(dev, V4L2_DEBUG_QUEUE, | |
622 | "restart_queue [%p/%d]: restart dma\n", buf, buf->vb.i); | |
623 | ||
624 | dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n"); | |
625 | tm6000_stop_thread(dma_q); | |
626 | tm6000_start_thread(dma_q, buf); | |
627 | ||
628 | /* cancel all outstanding capture / vbi requests */ | |
629 | list_for_each(item,&dma_q->active) { | |
630 | buf = list_entry(item, struct tm6000_buffer, vb.queue); | |
631 | ||
632 | list_del(&buf->vb.queue); | |
633 | buf->vb.state = STATE_ERROR; | |
634 | wake_up(&buf->vb.done); | |
635 | } | |
636 | mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); | |
637 | ||
638 | return 0; | |
639 | } | |
640 | ||
641 | prev = NULL; | |
642 | for (;;) { | |
643 | if (list_empty(&dma_q->queued)) | |
644 | return 0; | |
645 | buf = list_entry(dma_q->queued.next, struct tm6000_buffer, vb.queue); | |
646 | if (NULL == prev) { | |
647 | list_del(&buf->vb.queue); | |
648 | list_add_tail(&buf->vb.queue,&dma_q->active); | |
649 | ||
650 | dprintk(dev, V4L2_DEBUG_QUEUE, "Restarting video dma\n"); | |
651 | tm6000_stop_thread(dma_q); | |
652 | tm6000_start_thread(dma_q, buf); | |
653 | ||
654 | buf->vb.state = STATE_ACTIVE; | |
655 | mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT); | |
656 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -" | |
657 | " first active\n", buf, buf->vb.i); | |
658 | ||
659 | } else if (prev->vb.width == buf->vb.width && | |
660 | prev->vb.height == buf->vb.height && | |
661 | prev->fmt == buf->fmt) { | |
662 | list_del(&buf->vb.queue); | |
663 | list_add_tail(&buf->vb.queue,&dma_q->active); | |
664 | buf->vb.state = STATE_ACTIVE; | |
665 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] restart_queue -" | |
666 | " move to active\n",buf,buf->vb.i); | |
667 | } else { | |
668 | return 0; | |
669 | } | |
670 | prev = buf; | |
671 | } | |
672 | } | |
673 | ||
674 | static void tm6000_vid_timeout(unsigned long data) | |
675 | { | |
676 | struct tm6000_core *dev = (struct tm6000_core*)data; | |
677 | struct tm6000_dmaqueue *vidq = &dev->vidq; | |
678 | struct tm6000_buffer *buf; | |
679 | unsigned long flags; | |
680 | ||
681 | spin_lock_irqsave(&dev->slock,flags); | |
682 | while (!list_empty(&vidq->active)) { | |
683 | buf = list_entry(vidq->active.next, struct tm6000_buffer, | |
684 | vb.queue); | |
685 | list_del(&buf->vb.queue); | |
686 | buf->vb.state = STATE_ERROR; | |
687 | wake_up(&buf->vb.done); | |
688 | dprintk(dev, V4L2_DEBUG_QUEUE, "tm6000/0: [%p/%d] timeout\n", | |
689 | buf, buf->vb.i); | |
690 | } | |
691 | ||
692 | restart_video_queue(vidq); | |
693 | spin_unlock_irqrestore(&dev->slock,flags); | |
694 | } | |
695 | ||
696 | /* ------------------------------------------------------------------ | |
697 | Videobuf operations | |
698 | ------------------------------------------------------------------*/ | |
699 | static int | |
700 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) | |
701 | { | |
702 | struct tm6000_fh *fh = vq->priv_data; | |
703 | ||
704 | *size = fh->fmt->depth * fh->width * fh->height >> 3; | |
705 | if (0 == *count) | |
706 | *count = 32; | |
707 | while (*size * *count > vid_limit * 1024 * 1024) | |
708 | (*count)--; | |
709 | return 0; | |
710 | } | |
711 | ||
712 | static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf) | |
713 | { | |
714 | if (in_interrupt()) | |
715 | BUG(); | |
716 | ||
717 | videobuf_waiton(&buf->vb,0,0); | |
718 | videobuf_vmalloc_free(&buf->vb); | |
719 | buf->vb.state = STATE_NEEDS_INIT; | |
720 | } | |
721 | ||
722 | static int | |
723 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, | |
724 | enum v4l2_field field) | |
725 | { | |
726 | struct tm6000_fh *fh = vq->priv_data; | |
727 | struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb); | |
728 | struct tm6000_core *dev = fh->dev; | |
729 | int rc=0, urbsize, urb_init=0; | |
730 | ||
731 | BUG_ON(NULL == fh->fmt); | |
732 | ||
733 | if (fh->width < norm_minw(core) || fh->width > norm_maxw(core) || | |
734 | fh->height < norm_minh(core) || fh->height > norm_maxh(core)) { | |
735 | dprintk(dev, V4L2_DEBUG_QUEUE, "Window size (%dx%d) is out of " | |
736 | "supported range\n", fh->width, fh->height); | |
737 | dprintk(dev, V4L2_DEBUG_QUEUE, "Valid range is from (%dx%d) to " | |
738 | "(%dx%d)\n", norm_minw(core), norm_minh(core), | |
739 | norm_maxw(core),norm_maxh(core)); | |
740 | return -EINVAL; | |
741 | } | |
742 | ||
743 | /* FIXME: It assumes depth=2 */ | |
744 | /* The only currently supported format is 16 bits/pixel */ | |
745 | buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3; | |
746 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) | |
747 | return -EINVAL; | |
748 | ||
749 | if (buf->fmt != fh->fmt || | |
750 | buf->vb.width != fh->width || | |
751 | buf->vb.height != fh->height || | |
752 | buf->vb.field != field) { | |
753 | buf->fmt = fh->fmt; | |
754 | buf->vb.width = fh->width; | |
755 | buf->vb.height = fh->height; | |
756 | buf->vb.field = field; | |
757 | buf->vb.state = STATE_NEEDS_INIT; | |
758 | } | |
759 | ||
760 | if (STATE_NEEDS_INIT == buf->vb.state) { | |
761 | if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL))) | |
762 | goto fail; | |
763 | urb_init=1; | |
764 | } | |
765 | ||
766 | ||
767 | if (!dev->isoc_ctl.num_bufs) | |
768 | urb_init=1; | |
769 | ||
770 | if (urb_init) { | |
771 | /* Should allocate/request at least h | |
772 | res x v res x 2 bytes/pixel */ | |
773 | urbsize=(buf->vb.size+dev->max_isoc_in-1)/dev->max_isoc_in; | |
774 | ||
775 | /* Hack to allocate memory for Video + Audio */ | |
776 | /* FIXME: should also consider header ovehead of | |
777 | 4 bytes/180 bytes */ | |
778 | urbsize+=((48000*4+24)/25+dev->max_isoc_in-1)/dev->max_isoc_in; | |
779 | ||
780 | dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d packets to handle " | |
781 | "%lu size\n", urbsize,buf->vb.size); | |
782 | rc = tm6000_prepare_isoc(dev, urbsize, 2); | |
783 | ||
784 | if (rc<0) | |
785 | goto fail; | |
786 | } | |
787 | ||
788 | buf->vb.state = STATE_PREPARED; | |
789 | return 0; | |
790 | ||
791 | fail: | |
792 | free_buffer(vq,buf); | |
793 | return rc; | |
794 | } | |
795 | ||
796 | static void | |
797 | buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) | |
798 | { | |
799 | struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb); | |
800 | struct tm6000_fh *fh = vq->priv_data; | |
801 | struct tm6000_core *dev = fh->dev; | |
802 | struct tm6000_dmaqueue *vidq = &dev->vidq; | |
803 | struct tm6000_buffer *prev; | |
804 | ||
805 | if (!list_empty(&vidq->queued)) { | |
806 | list_add_tail(&buf->vb.queue,&vidq->queued); | |
807 | buf->vb.state = STATE_QUEUED; | |
808 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - " | |
809 | "append to queued\n", buf, buf->vb.i); | |
810 | } else if (list_empty(&vidq->active)) { | |
811 | list_add_tail(&buf->vb.queue,&vidq->active); | |
812 | buf->vb.state = STATE_ACTIVE; | |
813 | mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT); | |
814 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue - " | |
815 | "first active\n", buf, buf->vb.i); | |
816 | tm6000_start_thread(vidq, buf); | |
817 | } else { | |
818 | prev = list_entry(vidq->active.prev, struct tm6000_buffer, vb.queue); | |
819 | if (prev->vb.width == buf->vb.width && | |
820 | prev->vb.height == buf->vb.height && | |
821 | prev->fmt == buf->fmt) { | |
822 | list_add_tail(&buf->vb.queue,&vidq->active); | |
823 | buf->vb.state = STATE_ACTIVE; | |
824 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -" | |
825 | " append to active\n", buf, buf->vb.i); | |
826 | } else { | |
827 | list_add_tail(&buf->vb.queue,&vidq->queued); | |
828 | buf->vb.state = STATE_QUEUED; | |
829 | dprintk(dev, V4L2_DEBUG_QUEUE, "[%p/%d] buffer_queue -" | |
830 | " first queued\n", buf, buf->vb.i); | |
831 | } | |
832 | } | |
833 | } | |
834 | ||
835 | static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) | |
836 | { | |
837 | struct tm6000_buffer *buf = container_of(vb,struct tm6000_buffer,vb); | |
838 | struct tm6000_fh *fh = vq->priv_data; | |
839 | struct tm6000_core *dev = (struct tm6000_core*)fh->dev; | |
840 | struct tm6000_dmaqueue *vidq = &dev->vidq; | |
841 | ||
842 | tm6000_stop_thread(vidq); | |
843 | ||
844 | free_buffer(vq,buf); | |
845 | } | |
846 | ||
847 | static struct videobuf_queue_ops tm6000_video_qops = { | |
848 | .buf_setup = buffer_setup, | |
849 | .buf_prepare = buffer_prepare, | |
850 | .buf_queue = buffer_queue, | |
851 | .buf_release = buffer_release, | |
852 | }; | |
853 | ||
854 | /* ------------------------------------------------------------------ | |
855 | IOCTL handling | |
856 | ------------------------------------------------------------------*/ | |
857 | ||
858 | static int res_get(struct tm6000_core *dev, struct tm6000_fh *fh) | |
859 | { | |
860 | /* is it free? */ | |
861 | mutex_lock(&dev->lock); | |
862 | if (dev->resources) { | |
863 | /* no, someone else uses it */ | |
864 | mutex_unlock(&dev->lock); | |
865 | return 0; | |
866 | } | |
867 | /* it's free, grab it */ | |
868 | dev->resources =1; | |
869 | dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n"); | |
870 | mutex_unlock(&dev->lock); | |
871 | return 1; | |
872 | } | |
873 | ||
874 | static int res_locked(struct tm6000_core *dev) | |
875 | { | |
876 | return (dev->resources); | |
877 | } | |
878 | ||
879 | static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh) | |
880 | { | |
881 | mutex_lock(&dev->lock); | |
882 | dev->resources = 0; | |
883 | dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n"); | |
884 | mutex_unlock(&dev->lock); | |
885 | } | |
886 | ||
887 | /* ------------------------------------------------------------------ | |
888 | IOCTL vidioc handling | |
889 | ------------------------------------------------------------------*/ | |
890 | static int vidioc_querycap (struct file *file, void *priv, | |
891 | struct v4l2_capability *cap) | |
892 | { | |
893 | // struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev; | |
894 | ||
895 | strlcpy(cap->driver, "tm6000", sizeof(cap->driver)); | |
896 | strlcpy(cap->card,"Trident TVMaster TM5600/6000", sizeof(cap->card)); | |
897 | // strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info)); | |
898 | cap->version = TM6000_VERSION; | |
899 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | | |
900 | V4L2_CAP_STREAMING | | |
901 | V4L2_CAP_TUNER | | |
902 | V4L2_CAP_READWRITE; | |
903 | return 0; | |
904 | } | |
905 | ||
906 | static int vidioc_enum_fmt_cap (struct file *file, void *priv, | |
907 | struct v4l2_fmtdesc *f) | |
908 | { | |
909 | if (unlikely(f->index >= ARRAY_SIZE(format))) | |
910 | return -EINVAL; | |
911 | ||
912 | strlcpy(f->description,format[f->index].name,sizeof(f->description)); | |
913 | f->pixelformat = format[f->index].fourcc; | |
914 | return 0; | |
915 | } | |
916 | ||
917 | static int vidioc_g_fmt_cap (struct file *file, void *priv, | |
918 | struct v4l2_format *f) | |
919 | { | |
920 | struct tm6000_fh *fh=priv; | |
921 | ||
922 | f->fmt.pix.width = fh->width; | |
923 | f->fmt.pix.height = fh->height; | |
924 | f->fmt.pix.field = fh->vb_vidq.field; | |
925 | f->fmt.pix.pixelformat = fh->fmt->fourcc; | |
926 | f->fmt.pix.bytesperline = | |
927 | (f->fmt.pix.width * fh->fmt->depth) >> 3; | |
928 | f->fmt.pix.sizeimage = | |
929 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
930 | ||
931 | return (0); | |
932 | } | |
933 | ||
934 | static struct tm6000_fmt* format_by_fourcc(unsigned int fourcc) | |
935 | { | |
936 | unsigned int i; | |
937 | ||
938 | for (i = 0; i < ARRAY_SIZE(format); i++) | |
939 | if (format[i].fourcc == fourcc) | |
940 | return format+i; | |
941 | return NULL; | |
942 | } | |
943 | ||
944 | static int vidioc_try_fmt_cap (struct file *file, void *priv, | |
945 | struct v4l2_format *f) | |
946 | { | |
947 | struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev; | |
948 | struct tm6000_fmt *fmt; | |
949 | enum v4l2_field field; | |
950 | ||
951 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
952 | if (NULL == fmt) { | |
953 | dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)" | |
954 | " invalid.\n", f->fmt.pix.pixelformat); | |
955 | return -EINVAL; | |
956 | } | |
957 | ||
958 | field = f->fmt.pix.field; | |
959 | ||
960 | if (field == V4L2_FIELD_ANY) { | |
961 | // field=V4L2_FIELD_INTERLACED; | |
962 | field=V4L2_FIELD_SEQ_TB; | |
963 | } else if (V4L2_FIELD_INTERLACED != field) { | |
964 | dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Field type invalid.\n"); | |
965 | return -EINVAL; | |
966 | } | |
967 | ||
968 | if (f->fmt.pix.width < norm_minw(core)) | |
969 | f->fmt.pix.width = norm_minw(core); | |
970 | ||
971 | if (f->fmt.pix.width > norm_maxw(core)) | |
972 | f->fmt.pix.width = norm_maxw(core); | |
973 | ||
974 | if (f->fmt.pix.height < norm_minh(core)) | |
975 | f->fmt.pix.height = norm_minh(core); | |
976 | ||
977 | if (f->fmt.pix.height > norm_maxh(core)) | |
978 | f->fmt.pix.height = norm_maxh(core); | |
979 | ||
980 | f->fmt.pix.width &= ~0x01; | |
981 | ||
982 | f->fmt.pix.field = field; | |
983 | ||
984 | f->fmt.pix.bytesperline = | |
985 | (f->fmt.pix.width * fmt->depth) >> 3; | |
986 | f->fmt.pix.sizeimage = | |
987 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
988 | ||
989 | return 0; | |
990 | } | |
991 | ||
992 | /*FIXME: This seems to be generic enough to be at videodev2 */ | |
993 | static int vidioc_s_fmt_cap (struct file *file, void *priv, | |
994 | struct v4l2_format *f) | |
995 | { | |
996 | struct tm6000_fh *fh=priv; | |
997 | struct tm6000_core *dev = fh->dev; | |
998 | int ret = vidioc_try_fmt_cap(file,fh,f); | |
999 | if (ret < 0) | |
1000 | return (ret); | |
1001 | ||
1002 | fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
1003 | fh->width = f->fmt.pix.width; | |
1004 | fh->height = f->fmt.pix.height; | |
1005 | fh->vb_vidq.field = f->fmt.pix.field; | |
1006 | fh->type = f->type; | |
1007 | ||
1008 | dev->fourcc = f->fmt.pix.pixelformat; | |
1009 | ||
1010 | tm6000_set_fourcc_format(dev); | |
1011 | ||
1012 | return (0); | |
1013 | } | |
1014 | ||
1015 | static int vidioc_reqbufs (struct file *file, void *priv, | |
1016 | struct v4l2_requestbuffers *p) | |
1017 | { | |
1018 | struct tm6000_fh *fh=priv; | |
1019 | ||
1020 | return (videobuf_reqbufs(&fh->vb_vidq, p)); | |
1021 | } | |
1022 | ||
1023 | static int vidioc_querybuf (struct file *file, void *priv, | |
1024 | struct v4l2_buffer *p) | |
1025 | { | |
1026 | struct tm6000_fh *fh=priv; | |
1027 | ||
1028 | return (videobuf_querybuf(&fh->vb_vidq, p)); | |
1029 | } | |
1030 | ||
1031 | static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p) | |
1032 | { | |
1033 | struct tm6000_fh *fh=priv; | |
1034 | ||
1035 | return (videobuf_qbuf(&fh->vb_vidq, p)); | |
1036 | } | |
1037 | ||
1038 | static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p) | |
1039 | { | |
1040 | struct tm6000_fh *fh=priv; | |
1041 | ||
1042 | return (videobuf_dqbuf(&fh->vb_vidq, p, | |
1043 | file->f_flags & O_NONBLOCK)); | |
1044 | } | |
1045 | ||
1046 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | |
1047 | static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf) | |
1048 | { | |
1049 | struct tm6000_fh *fh=priv; | |
1050 | ||
1051 | return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8); | |
1052 | } | |
1053 | #endif | |
1054 | ||
1055 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) | |
1056 | { | |
1057 | struct tm6000_fh *fh=priv; | |
1058 | struct tm6000_core *dev = fh->dev; | |
1059 | ||
1060 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1061 | return -EINVAL; | |
1062 | if (i != fh->type) | |
1063 | return -EINVAL; | |
1064 | ||
1065 | if (!res_get(dev,fh)) | |
1066 | return -EBUSY; | |
1067 | return (videobuf_streamon(&fh->vb_vidq)); | |
1068 | } | |
1069 | ||
1070 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) | |
1071 | { | |
1072 | struct tm6000_fh *fh=priv; | |
1073 | struct tm6000_core *dev = fh->dev; | |
1074 | ||
1075 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1076 | return -EINVAL; | |
1077 | if (i != fh->type) | |
1078 | return -EINVAL; | |
1079 | ||
1080 | videobuf_streamoff(&fh->vb_vidq); | |
1081 | res_free(dev,fh); | |
1082 | ||
1083 | return (0); | |
1084 | } | |
1085 | ||
1086 | static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *norm) | |
1087 | { | |
1088 | int rc=0; | |
1089 | struct tm6000_fh *fh=priv; | |
1090 | struct tm6000_core *dev = fh->dev; | |
1091 | ||
1092 | rc=tm6000_set_standard (dev, norm); | |
1093 | if (rc<0) | |
1094 | return rc; | |
1095 | ||
1096 | tm6000_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm); | |
1097 | ||
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | static int vidioc_enum_input (struct file *file, void *priv, | |
1102 | struct v4l2_input *inp) | |
1103 | { | |
1104 | switch (inp->index) { | |
1105 | case TM6000_INPUT_TV: | |
1106 | inp->type = V4L2_INPUT_TYPE_TUNER; | |
1107 | strcpy(inp->name,"Television"); | |
1108 | break; | |
1109 | case TM6000_INPUT_COMPOSITE: | |
1110 | inp->type = V4L2_INPUT_TYPE_CAMERA; | |
1111 | strcpy(inp->name,"Composite"); | |
1112 | break; | |
1113 | case TM6000_INPUT_SVIDEO: | |
1114 | inp->type = V4L2_INPUT_TYPE_CAMERA; | |
1115 | strcpy(inp->name,"S-Video"); | |
1116 | break; | |
1117 | default: | |
1118 | return -EINVAL; | |
1119 | } | |
1120 | inp->std = TM6000_STD; | |
1121 | ||
1122 | return 0; | |
1123 | } | |
1124 | ||
1125 | static int vidioc_g_input (struct file *file, void *priv, unsigned int *i) | |
1126 | { | |
1127 | struct tm6000_fh *fh=priv; | |
1128 | struct tm6000_core *dev = fh->dev; | |
1129 | ||
1130 | *i=dev->input; | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | static int vidioc_s_input (struct file *file, void *priv, unsigned int i) | |
1135 | { | |
1136 | struct tm6000_fh *fh=priv; | |
1137 | struct tm6000_core *dev = fh->dev; | |
1138 | int rc=0; | |
1139 | char buf[1]; | |
1140 | ||
1141 | switch (i) { | |
1142 | case TM6000_INPUT_TV: | |
1143 | dev->input=i; | |
1144 | *buf=0; | |
1145 | break; | |
1146 | case TM6000_INPUT_COMPOSITE: | |
1147 | case TM6000_INPUT_SVIDEO: | |
1148 | dev->input=i; | |
1149 | *buf=1; | |
1150 | break; | |
1151 | default: | |
1152 | return -EINVAL; | |
1153 | } | |
1154 | rc=tm6000_read_write_usb (dev, USB_DIR_OUT | USB_TYPE_VENDOR, | |
1155 | REQ_03_SET_GET_MCU_PIN, 0x03, 1, buf, 1); | |
1156 | ||
1157 | if (!rc) { | |
1158 | dev->input=i; | |
7c3f53ec | 1159 | rc=vidioc_s_std (file, priv, &dev->vfd->current_norm); |
9701dc94 MCC |
1160 | } |
1161 | ||
1162 | return (rc); | |
1163 | } | |
1164 | ||
1165 | /* --- controls ---------------------------------------------- */ | |
1166 | static int vidioc_queryctrl (struct file *file, void *priv, | |
1167 | struct v4l2_queryctrl *qc) | |
1168 | { | |
1169 | int i; | |
1170 | ||
1171 | for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++) | |
1172 | if (qc->id && qc->id == tm6000_qctrl[i].id) { | |
1173 | memcpy(qc, &(tm6000_qctrl[i]), | |
1174 | sizeof(*qc)); | |
1175 | return (0); | |
1176 | } | |
1177 | ||
1178 | return -EINVAL; | |
1179 | } | |
1180 | ||
1181 | static int vidioc_g_ctrl (struct file *file, void *priv, | |
1182 | struct v4l2_control *ctrl) | |
1183 | { | |
1184 | struct tm6000_fh *fh=priv; | |
1185 | struct tm6000_core *dev = fh->dev; | |
1186 | int val; | |
1187 | ||
1188 | /* FIXME: Probably, those won't work! Maybe we need shadow regs */ | |
1189 | switch (ctrl->id) { | |
1190 | case V4L2_CID_CONTRAST: | |
1191 | val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x08, 0); | |
1192 | break; | |
1193 | case V4L2_CID_BRIGHTNESS: | |
1194 | val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x09, 0); | |
1195 | return 0; | |
1196 | case V4L2_CID_SATURATION: | |
1197 | val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, 0); | |
1198 | return 0; | |
1199 | case V4L2_CID_HUE: | |
1200 | val=tm6000_get_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, 0); | |
1201 | return 0; | |
1202 | default: | |
1203 | return -EINVAL; | |
1204 | } | |
1205 | ||
1206 | if (val<0) | |
1207 | return val; | |
1208 | ||
1209 | ctrl->value=val; | |
1210 | ||
1211 | return 0; | |
1212 | } | |
1213 | static int vidioc_s_ctrl (struct file *file, void *priv, | |
1214 | struct v4l2_control *ctrl) | |
1215 | { | |
1216 | struct tm6000_fh *fh =priv; | |
1217 | struct tm6000_core *dev = fh->dev; | |
1218 | u8 val=ctrl->value; | |
1219 | ||
1220 | switch (ctrl->id) { | |
1221 | case V4L2_CID_CONTRAST: | |
1222 | tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x08, val); | |
1223 | return 0; | |
1224 | case V4L2_CID_BRIGHTNESS: | |
1225 | tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x09, val); | |
1226 | return 0; | |
1227 | case V4L2_CID_SATURATION: | |
1228 | tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0a, val); | |
1229 | return 0; | |
1230 | case V4L2_CID_HUE: | |
1231 | tm6000_set_reg (dev, REQ_07_SET_GET_AVREG, 0x0b, val); | |
1232 | return 0; | |
1233 | } | |
1234 | return -EINVAL; | |
1235 | } | |
1236 | ||
1237 | static int vidioc_g_tuner (struct file *file, void *priv, | |
1238 | struct v4l2_tuner *t) | |
1239 | { | |
1240 | struct tm6000_fh *fh =priv; | |
1241 | struct tm6000_core *dev = fh->dev; | |
1242 | ||
1243 | if (unlikely(UNSET == dev->tuner_type)) | |
1244 | return -EINVAL; | |
1245 | if (0 != t->index) | |
1246 | return -EINVAL; | |
1247 | ||
1248 | strcpy(t->name, "Television"); | |
1249 | t->type = V4L2_TUNER_ANALOG_TV; | |
1250 | t->capability = V4L2_TUNER_CAP_NORM; | |
1251 | t->rangehigh = 0xffffffffUL; | |
1252 | t->rxsubchans = V4L2_TUNER_SUB_MONO; | |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static int vidioc_s_tuner (struct file *file, void *priv, | |
1258 | struct v4l2_tuner *t) | |
1259 | { | |
1260 | struct tm6000_fh *fh =priv; | |
1261 | struct tm6000_core *dev = fh->dev; | |
1262 | ||
1263 | if (UNSET == dev->tuner_type) | |
1264 | return -EINVAL; | |
1265 | if (0 != t->index) | |
1266 | return -EINVAL; | |
1267 | ||
1268 | return 0; | |
1269 | } | |
1270 | ||
1271 | static int vidioc_g_frequency (struct file *file, void *priv, | |
1272 | struct v4l2_frequency *f) | |
1273 | { | |
1274 | struct tm6000_fh *fh =priv; | |
1275 | struct tm6000_core *dev = fh->dev; | |
1276 | ||
1277 | if (unlikely(UNSET == dev->tuner_type)) | |
1278 | return -EINVAL; | |
1279 | ||
1280 | f->type = V4L2_TUNER_ANALOG_TV; | |
1281 | f->frequency = dev->freq; | |
1282 | ||
1283 | tm6000_i2c_call_clients(dev,VIDIOC_G_FREQUENCY,f); | |
1284 | ||
1285 | return 0; | |
1286 | } | |
1287 | ||
1288 | static int vidioc_s_frequency (struct file *file, void *priv, | |
1289 | struct v4l2_frequency *f) | |
1290 | { | |
1291 | struct tm6000_fh *fh =priv; | |
1292 | struct tm6000_core *dev = fh->dev; | |
1293 | ||
1294 | if (unlikely(f->type != V4L2_TUNER_ANALOG_TV)) | |
1295 | return -EINVAL; | |
1296 | ||
1297 | if (unlikely(UNSET == dev->tuner_type)) | |
1298 | return -EINVAL; | |
1299 | if (unlikely(f->tuner != 0)) | |
1300 | return -EINVAL; | |
1301 | ||
1302 | // mutex_lock(&dev->lock); | |
1303 | dev->freq = f->frequency; | |
1304 | tm6000_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f); | |
1305 | // mutex_unlock(&dev->lock); | |
1306 | ||
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | /* ------------------------------------------------------------------ | |
1311 | File operations for the device | |
1312 | ------------------------------------------------------------------*/ | |
1313 | ||
1314 | static int tm6000_open(struct inode *inode, struct file *file) | |
1315 | { | |
1316 | int minor = iminor(inode); | |
1317 | struct tm6000_core *h,*dev = NULL; | |
1318 | struct tm6000_fh *fh; | |
1319 | struct list_head *list; | |
1320 | enum v4l2_buf_type type = 0; | |
1321 | int i,rc; | |
1322 | ||
7c3f53ec ML |
1323 | printk(KERN_INFO "tm6000: open called (minor=%d)\n",minor); |
1324 | ||
1325 | ||
9701dc94 MCC |
1326 | dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called " |
1327 | "(minor=%d)\n",minor); | |
1328 | ||
1329 | list_for_each(list,&tm6000_corelist) { | |
1330 | h = list_entry(list, struct tm6000_core, tm6000_corelist); | |
7c3f53ec | 1331 | if (h->vfd->minor == minor) { |
9701dc94 MCC |
1332 | dev = h; |
1333 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1334 | } | |
1335 | } | |
1336 | if (NULL == dev) | |
1337 | return -ENODEV; | |
1338 | ||
1339 | ||
1340 | /* If more than one user, mutex should be added */ | |
1341 | dev->users++; | |
1342 | ||
1343 | dprintk(dev, V4L2_DEBUG_OPEN, "open minor=%d type=%s users=%d\n", | |
1344 | minor,v4l2_type_names[type],dev->users); | |
1345 | ||
1346 | /* allocate + initialize per filehandle data */ | |
1347 | fh = kzalloc(sizeof(*fh),GFP_KERNEL); | |
1348 | if (NULL == fh) { | |
1349 | dev->users--; | |
1350 | return -ENOMEM; | |
1351 | } | |
1352 | ||
1353 | file->private_data = fh; | |
1354 | fh->dev = dev; | |
1355 | ||
1356 | fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1357 | dev->fourcc = format[0].fourcc; | |
1358 | ||
1359 | fh->fmt = format_by_fourcc(dev->fourcc); | |
1360 | fh->width = norm_maxw(); | |
1361 | fh->height = norm_maxh(); | |
1362 | ||
1363 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, " | |
1364 | "dev->vidq=0x%08lx\n", | |
1365 | (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq); | |
1366 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty " | |
1367 | "queued=%d\n",list_empty(&dev->vidq.queued)); | |
1368 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty " | |
1369 | "active=%d\n",list_empty(&dev->vidq.active)); | |
1370 | ||
1371 | /* initialize hardware on analog mode */ | |
1372 | if (dev->mode!=TM6000_MODE_ANALOG) { | |
1373 | rc=tm6000_init_analog_mode (dev); | |
1374 | if (rc<0) | |
1375 | return rc; | |
1376 | ||
1377 | /* Put all controls at a sane state */ | |
1378 | for (i = 0; i < ARRAY_SIZE(tm6000_qctrl); i++) | |
1379 | qctl_regs[i] =tm6000_qctrl[i].default_value; | |
1380 | ||
1381 | dev->mode=TM6000_MODE_ANALOG; | |
1382 | } | |
1383 | ||
1384 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops, | |
1385 | NULL, &dev->slock, | |
1386 | fh->type, | |
1387 | V4L2_FIELD_INTERLACED, | |
1388 | sizeof(struct tm6000_buffer),fh); | |
1389 | ||
1390 | return 0; | |
1391 | } | |
1392 | ||
1393 | static ssize_t | |
1394 | tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos) | |
1395 | { | |
1396 | struct tm6000_fh *fh = file->private_data; | |
1397 | ||
1398 | if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) { | |
1399 | if (res_locked(fh->dev)) | |
1400 | return -EBUSY; | |
1401 | ||
1402 | return videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0, | |
1403 | file->f_flags & O_NONBLOCK); | |
1404 | } | |
1405 | return 0; | |
1406 | } | |
1407 | ||
1408 | static unsigned int | |
1409 | tm6000_poll(struct file *file, struct poll_table_struct *wait) | |
1410 | { | |
1411 | struct tm6000_fh *fh = file->private_data; | |
1412 | struct tm6000_buffer *buf; | |
1413 | ||
1414 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) | |
1415 | return POLLERR; | |
1416 | ||
1417 | if (res_get(fh->dev,fh)) { | |
1418 | /* streaming capture */ | |
1419 | if (list_empty(&fh->vb_vidq.stream)) | |
1420 | return POLLERR; | |
1421 | buf = list_entry(fh->vb_vidq.stream.next,struct tm6000_buffer,vb.stream); | |
1422 | } else { | |
1423 | /* read() capture */ | |
1424 | buf = (struct tm6000_buffer*)fh->vb_vidq.read_buf; | |
1425 | if (NULL == buf) | |
1426 | return POLLERR; | |
1427 | } | |
1428 | poll_wait(file, &buf->vb.done, wait); | |
1429 | if (buf->vb.state == STATE_DONE || | |
1430 | buf->vb.state == STATE_ERROR) | |
1431 | return POLLIN|POLLRDNORM; | |
1432 | return 0; | |
1433 | } | |
1434 | ||
1435 | static int tm6000_release(struct inode *inode, struct file *file) | |
1436 | { | |
1437 | struct tm6000_fh *fh = file->private_data; | |
1438 | struct tm6000_core *dev = fh->dev; | |
1439 | struct tm6000_dmaqueue *vidq = &dev->vidq; | |
1440 | int minor = iminor(inode); | |
1441 | ||
7c3f53ec ML |
1442 | dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (minor=%d, users=%d)\n",minor,dev->users); |
1443 | ||
a58d35cb MCC |
1444 | dev->users--; |
1445 | ||
1446 | if (!dev->users) { | |
1447 | tm6000_stop_thread(vidq); | |
1448 | videobuf_mmap_free(&fh->vb_vidq); | |
1449 | } | |
9701dc94 MCC |
1450 | |
1451 | kfree (fh); | |
1452 | ||
9701dc94 MCC |
1453 | return 0; |
1454 | } | |
1455 | ||
1456 | static int tm6000_mmap(struct file *file, struct vm_area_struct * vma) | |
1457 | { | |
1458 | struct tm6000_fh *fh = file->private_data; | |
1459 | int ret; | |
1460 | ||
1461 | ret=videobuf_mmap_mapper(&fh->vb_vidq, vma); | |
1462 | ||
1463 | return ret; | |
1464 | } | |
1465 | ||
1466 | static struct file_operations tm6000_fops = { | |
1467 | .owner = THIS_MODULE, | |
1468 | .open = tm6000_open, | |
1469 | .release = tm6000_release, | |
1470 | .ioctl = video_ioctl2, /* V4L2 ioctl handler */ | |
1471 | .read = tm6000_read, | |
1472 | .poll = tm6000_poll, | |
1473 | .mmap = tm6000_mmap, | |
1474 | .llseek = no_llseek, | |
1475 | }; | |
1476 | ||
1477 | static struct video_device tm6000_template = { | |
1478 | .name = "tm6000", | |
1479 | .type = VID_TYPE_CAPTURE, | |
1480 | .fops = &tm6000_fops, | |
1481 | .minor = -1, | |
1482 | .release = video_device_release, | |
1483 | ||
1484 | .vidioc_querycap = vidioc_querycap, | |
1485 | .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, | |
1486 | .vidioc_g_fmt_cap = vidioc_g_fmt_cap, | |
1487 | .vidioc_try_fmt_cap = vidioc_try_fmt_cap, | |
1488 | .vidioc_s_fmt_cap = vidioc_s_fmt_cap, | |
1489 | .vidioc_s_std = vidioc_s_std, | |
1490 | .vidioc_enum_input = vidioc_enum_input, | |
1491 | .vidioc_g_input = vidioc_g_input, | |
1492 | .vidioc_s_input = vidioc_s_input, | |
1493 | .vidioc_queryctrl = vidioc_queryctrl, | |
1494 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
1495 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1496 | .vidioc_g_tuner = vidioc_g_tuner, | |
1497 | .vidioc_s_tuner = vidioc_s_tuner, | |
1498 | .vidioc_g_frequency = vidioc_g_frequency, | |
1499 | .vidioc_s_frequency = vidioc_s_frequency, | |
1500 | .vidioc_streamon = vidioc_streamon, | |
1501 | .vidioc_streamoff = vidioc_streamoff, | |
1502 | .vidioc_reqbufs = vidioc_reqbufs, | |
1503 | .vidioc_querybuf = vidioc_querybuf, | |
1504 | .vidioc_qbuf = vidioc_qbuf, | |
1505 | .vidioc_dqbuf = vidioc_dqbuf, | |
1506 | #ifdef CONFIG_VIDEO_V4L1_COMPAT | |
1507 | .vidiocgmbuf = vidiocgmbuf, | |
1508 | #endif | |
1509 | .tvnorms = TM6000_STD, | |
1510 | .current_norm = V4L2_STD_NTSC_M, | |
1511 | }; | |
1512 | /* ----------------------------------------------------------------- | |
1513 | Initialization and module stuff | |
1514 | ------------------------------------------------------------------*/ | |
1515 | ||
1516 | int tm6000_v4l2_register(struct tm6000_core *dev) | |
1517 | { | |
7c3f53ec ML |
1518 | int ret = -1; |
1519 | struct video_device *vfd; | |
1520 | ||
1521 | vfd = video_device_alloc(); | |
1522 | if(!vfd) { | |
1523 | return -ENOMEM; | |
1524 | } | |
1525 | dev->vfd = vfd; | |
9701dc94 MCC |
1526 | |
1527 | list_add_tail(&dev->tm6000_corelist,&tm6000_corelist); | |
1528 | ||
1529 | /* init video dma queues */ | |
1530 | INIT_LIST_HEAD(&dev->vidq.active); | |
1531 | INIT_LIST_HEAD(&dev->vidq.queued); | |
1532 | ||
1533 | dev->vidq.timeout.function = tm6000_vid_timeout; | |
1534 | dev->vidq.timeout.data = (unsigned long)dev; | |
1535 | init_timer(&dev->vidq.timeout); | |
1536 | ||
7c3f53ec ML |
1537 | memcpy (dev->vfd, &tm6000_template, sizeof(*(dev->vfd))); |
1538 | dev->vfd->debug=tm6000_debug; | |
9701dc94 | 1539 | |
7c3f53ec | 1540 | ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr); |
9701dc94 MCC |
1541 | printk(KERN_INFO "Trident TVMaster TM5600/TM6000 USB2 board (Load status: %d)\n", ret); |
1542 | return ret; | |
1543 | } | |
1544 | ||
1545 | int tm6000_v4l2_unregister(struct tm6000_core *dev) | |
1546 | { | |
1547 | struct tm6000_core *h; | |
22927e8e | 1548 | struct list_head *pos, *tmp; |
9701dc94 | 1549 | |
7c3f53ec | 1550 | video_unregister_device(dev->vfd); |
22927e8e ML |
1551 | |
1552 | list_for_each_safe(pos, tmp, &tm6000_corelist) { | |
1553 | h = list_entry(pos, struct tm6000_core, tm6000_corelist); | |
9701dc94 | 1554 | if (h == dev) { |
8c9d26fd | 1555 | list_del(pos); |
9701dc94 MCC |
1556 | } |
1557 | } | |
1558 | ||
1559 | return 0; | |
1560 | } | |
1561 | ||
1562 | int tm6000_v4l2_exit(void) | |
1563 | { | |
1564 | return 0; | |
1565 | } | |
1566 | ||
1567 | module_param(video_nr, int, 0); | |
1568 | MODULE_PARM_DESC(video_nr,"Allow changing video device number"); | |
1569 | ||
1570 | module_param_named (debug, tm6000_debug, int, 0444); | |
1571 | MODULE_PARM_DESC(debug,"activates debug info"); | |
1572 | ||
1573 | module_param(vid_limit,int,0644); | |
1574 | MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes"); | |
1575 |