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