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