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