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