Commit | Line | Data |
---|---|---|
781aa1d1 | 1 | /* |
483dfdb6 | 2 | * usbvision-core.c - driver for NT100x USB video capture devices |
7ca659e3 | 3 | * |
781aa1d1 MCC |
4 | * |
5 | * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de> | |
483dfdb6 | 6 | * Dwaine Garden <dwainegarden@rogers.com> |
781aa1d1 MCC |
7 | * |
8 | * This module is part of usbvision driver project. | |
483dfdb6 | 9 | * Updates to driver completed by Dwaine P. Garden |
781aa1d1 MCC |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
781aa1d1 MCC |
24 | */ |
25 | ||
26 | #include <linux/kernel.h> | |
781aa1d1 MCC |
27 | #include <linux/list.h> |
28 | #include <linux/timer.h> | |
5a0e3ad6 | 29 | #include <linux/gfp.h> |
781aa1d1 | 30 | #include <linux/mm.h> |
781aa1d1 | 31 | #include <linux/highmem.h> |
781aa1d1 MCC |
32 | #include <linux/vmalloc.h> |
33 | #include <linux/module.h> | |
34 | #include <linux/init.h> | |
35 | #include <linux/spinlock.h> | |
781aa1d1 MCC |
36 | #include <asm/io.h> |
37 | #include <linux/videodev2.h> | |
781aa1d1 | 38 | #include <linux/i2c.h> |
781aa1d1 | 39 | |
18d8a454 DG |
40 | #include <media/saa7115.h> |
41 | #include <media/v4l2-common.h> | |
781aa1d1 | 42 | #include <media/tuner.h> |
781aa1d1 | 43 | |
a1ed551c | 44 | #include <linux/workqueue.h> |
781aa1d1 | 45 | |
781aa1d1 | 46 | #include "usbvision.h" |
781aa1d1 | 47 | |
ff699e6b | 48 | static unsigned int core_debug; |
483dfdb6 TM |
49 | module_param(core_debug,int,0644); |
50 | MODULE_PARM_DESC(core_debug,"enable debug messages [core]"); | |
781aa1d1 | 51 | |
ff699e6b | 52 | static unsigned int force_testpattern; |
483dfdb6 TM |
53 | module_param(force_testpattern,int,0644); |
54 | MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]"); | |
55 | ||
5490a7cb HV |
56 | static int adjust_compression = 1; /* Set the compression to be adaptive */ |
57 | module_param(adjust_compression, int, 0444); | |
58 | MODULE_PARM_DESC(adjust_compression, " Set the ADPCM compression for the device. Default: 1 (On)"); | |
483dfdb6 | 59 | |
ff699e6b DSL |
60 | /* To help people with Black and White output with using s-video input. |
61 | * Some cables and input device are wired differently. */ | |
5490a7cb HV |
62 | static int switch_svideo_input; |
63 | module_param(switch_svideo_input, int, 0444); | |
64 | MODULE_PARM_DESC(switch_svideo_input, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)"); | |
781aa1d1 | 65 | |
5490a7cb HV |
66 | static unsigned int adjust_x_offset = -1; |
67 | module_param(adjust_x_offset, int, 0644); | |
68 | MODULE_PARM_DESC(adjust_x_offset, "adjust X offset display [core]"); | |
c6243d9c | 69 | |
5490a7cb HV |
70 | static unsigned int adjust_y_offset = -1; |
71 | module_param(adjust_y_offset, int, 0644); | |
72 | MODULE_PARM_DESC(adjust_y_offset, "adjust Y offset display [core]"); | |
c6243d9c TM |
73 | |
74 | ||
483dfdb6 | 75 | #define ENABLE_HEXDUMP 0 /* Enable if you need it */ |
781aa1d1 | 76 | |
781aa1d1 MCC |
77 | |
78 | #ifdef USBVISION_DEBUG | |
df18c319 TM |
79 | #define PDEBUG(level, fmt, args...) { \ |
80 | if (core_debug & (level)) \ | |
a482f327 GKH |
81 | printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \ |
82 | __func__, __LINE__ , ## args); \ | |
df18c319 | 83 | } |
781aa1d1 MCC |
84 | #else |
85 | #define PDEBUG(level, fmt, args...) do {} while(0) | |
86 | #endif | |
87 | ||
483dfdb6 TM |
88 | #define DBG_HEADER 1<<0 |
89 | #define DBG_IRQ 1<<1 | |
90 | #define DBG_ISOC 1<<2 | |
91 | #define DBG_PARSE 1<<3 | |
92 | #define DBG_SCRATCH 1<<4 | |
c876a346 | 93 | #define DBG_FUNC 1<<5 |
781aa1d1 MCC |
94 | |
95 | static const int max_imgwidth = MAX_FRAME_WIDTH; | |
96 | static const int max_imgheight = MAX_FRAME_HEIGHT; | |
97 | static const int min_imgwidth = MIN_FRAME_WIDTH; | |
98 | static const int min_imgheight = MIN_FRAME_HEIGHT; | |
99 | ||
483dfdb6 | 100 | /* The value of 'scratch_buf_size' affects quality of the picture |
781aa1d1 MCC |
101 | * in many ways. Shorter buffers may cause loss of data when client |
102 | * is too slow. Larger buffers are memory-consuming and take longer | |
103 | * to work with. This setting can be adjusted, but the default value | |
104 | * should be OK for most desktop users. | |
105 | */ | |
52cb0bf2 | 106 | #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) /* 128kB memory scratch buffer */ |
781aa1d1 MCC |
107 | static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE; |
108 | ||
52cb0bf2 | 109 | /* Function prototypes */ |
781aa1d1 MCC |
110 | static int usbvision_request_intra (struct usb_usbvision *usbvision); |
111 | static int usbvision_unrequest_intra (struct usb_usbvision *usbvision); | |
112 | static int usbvision_adjust_compression (struct usb_usbvision *usbvision); | |
113 | static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision); | |
781aa1d1 MCC |
114 | |
115 | /*******************************/ | |
116 | /* Memory management functions */ | |
117 | /*******************************/ | |
118 | ||
119 | /* | |
120 | * Here we want the physical address of the memory. | |
121 | * This is used when initializing the contents of the area. | |
122 | */ | |
123 | ||
0a0ceade | 124 | static void *usbvision_rvmalloc(unsigned long size) |
781aa1d1 MCC |
125 | { |
126 | void *mem; | |
127 | unsigned long adr; | |
128 | ||
129 | size = PAGE_ALIGN(size); | |
130 | mem = vmalloc_32(size); | |
131 | if (!mem) | |
132 | return NULL; | |
133 | ||
134 | memset(mem, 0, size); /* Clear the ram out, no junk to the user */ | |
483dfdb6 TM |
135 | adr = (unsigned long) mem; |
136 | while (size > 0) { | |
137 | SetPageReserved(vmalloc_to_page((void *)adr)); | |
138 | adr += PAGE_SIZE; | |
139 | size -= PAGE_SIZE; | |
140 | } | |
141 | ||
142 | return mem; | |
781aa1d1 MCC |
143 | } |
144 | ||
4a06b538 | 145 | static void usbvision_rvfree(void *mem, unsigned long size) |
781aa1d1 | 146 | { |
483dfdb6 TM |
147 | unsigned long adr; |
148 | ||
149 | if (!mem) | |
150 | return; | |
781aa1d1 | 151 | |
483dfdb6 | 152 | size = PAGE_ALIGN(size); |
781aa1d1 | 153 | |
483dfdb6 TM |
154 | adr = (unsigned long) mem; |
155 | while ((long) size > 0) { | |
156 | ClearPageReserved(vmalloc_to_page((void *)adr)); | |
157 | adr += PAGE_SIZE; | |
158 | size -= PAGE_SIZE; | |
159 | } | |
781aa1d1 | 160 | |
483dfdb6 TM |
161 | vfree(mem); |
162 | } | |
781aa1d1 MCC |
163 | |
164 | ||
781aa1d1 MCC |
165 | #if ENABLE_HEXDUMP |
166 | static void usbvision_hexdump(const unsigned char *data, int len) | |
167 | { | |
483dfdb6 TM |
168 | char tmp[80]; |
169 | int i, k; | |
170 | ||
171 | for (i = k = 0; len > 0; i++, len--) { | |
172 | if (i > 0 && (i % 16 == 0)) { | |
173 | printk("%s\n", tmp); | |
174 | k = 0; | |
175 | } | |
176 | k += sprintf(&tmp[k], "%02x ", data[i]); | |
177 | } | |
178 | if (k > 0) | |
179 | printk("%s\n", tmp); | |
781aa1d1 MCC |
180 | } |
181 | #endif | |
182 | ||
483dfdb6 TM |
183 | /******************************** |
184 | * scratch ring buffer handling | |
185 | ********************************/ | |
52cb0bf2 | 186 | static int scratch_len(struct usb_usbvision *usbvision) /* This returns the amount of data actually in the buffer */ |
781aa1d1 MCC |
187 | { |
188 | int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr; | |
189 | if (len < 0) { | |
190 | len += scratch_buf_size; | |
191 | } | |
192 | PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len); | |
193 | ||
194 | return len; | |
195 | } | |
196 | ||
197 | ||
198 | /* This returns the free space left in the buffer */ | |
0a0ceade | 199 | static int scratch_free(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
200 | { |
201 | int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr; | |
202 | if (free <= 0) { | |
203 | free += scratch_buf_size; | |
204 | } | |
205 | if (free) { | |
206 | free -= 1; /* at least one byte in the buffer must */ | |
207 | /* left blank, otherwise there is no chance to differ between full and empty */ | |
208 | } | |
209 | PDEBUG(DBG_SCRATCH, "return %d\n", free); | |
210 | ||
211 | return free; | |
212 | } | |
213 | ||
214 | ||
781aa1d1 | 215 | /* This puts data into the buffer */ |
0a0ceade AB |
216 | static int scratch_put(struct usb_usbvision *usbvision, unsigned char *data, |
217 | int len) | |
781aa1d1 MCC |
218 | { |
219 | int len_part; | |
220 | ||
221 | if (usbvision->scratch_write_ptr + len < scratch_buf_size) { | |
222 | memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len); | |
223 | usbvision->scratch_write_ptr += len; | |
224 | } | |
225 | else { | |
226 | len_part = scratch_buf_size - usbvision->scratch_write_ptr; | |
227 | memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part); | |
228 | if (len == len_part) { | |
229 | usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */ | |
230 | } | |
231 | else { | |
232 | memcpy(usbvision->scratch, data + len_part, len - len_part); | |
233 | usbvision->scratch_write_ptr = len - len_part; | |
234 | } | |
235 | } | |
236 | ||
237 | PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr); | |
238 | ||
239 | return len; | |
240 | } | |
241 | ||
242 | /* This marks the write_ptr as position of new frame header */ | |
0a0ceade | 243 | static void scratch_mark_header(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
244 | { |
245 | PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr); | |
246 | ||
247 | usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] = | |
248 | usbvision->scratch_write_ptr; | |
249 | usbvision->scratch_headermarker_write_ptr += 1; | |
250 | usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER; | |
251 | } | |
252 | ||
253 | /* This gets data from the buffer at the given "ptr" position */ | |
0a0ceade AB |
254 | static int scratch_get_extra(struct usb_usbvision *usbvision, |
255 | unsigned char *data, int *ptr, int len) | |
781aa1d1 MCC |
256 | { |
257 | int len_part; | |
258 | if (*ptr + len < scratch_buf_size) { | |
259 | memcpy(data, usbvision->scratch + *ptr, len); | |
260 | *ptr += len; | |
261 | } | |
262 | else { | |
263 | len_part = scratch_buf_size - *ptr; | |
264 | memcpy(data, usbvision->scratch + *ptr, len_part); | |
265 | if (len == len_part) { | |
266 | *ptr = 0; /* just set the y_ptr to zero */ | |
267 | } | |
268 | else { | |
269 | memcpy(data + len_part, usbvision->scratch, len - len_part); | |
270 | *ptr = len - len_part; | |
271 | } | |
272 | } | |
273 | ||
274 | PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr); | |
275 | ||
276 | return len; | |
277 | } | |
278 | ||
279 | ||
280 | /* This sets the scratch extra read pointer */ | |
0a0ceade AB |
281 | static void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr, |
282 | int len) | |
781aa1d1 MCC |
283 | { |
284 | *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size; | |
285 | ||
286 | PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr); | |
287 | } | |
288 | ||
289 | ||
52cb0bf2 | 290 | /* This increments the scratch extra read pointer */ |
0a0ceade | 291 | static void scratch_inc_extra_ptr(int *ptr, int len) |
781aa1d1 MCC |
292 | { |
293 | *ptr = (*ptr + len) % scratch_buf_size; | |
294 | ||
295 | PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr); | |
296 | } | |
297 | ||
298 | ||
299 | /* This gets data from the buffer */ | |
0a0ceade AB |
300 | static int scratch_get(struct usb_usbvision *usbvision, unsigned char *data, |
301 | int len) | |
781aa1d1 MCC |
302 | { |
303 | int len_part; | |
304 | if (usbvision->scratch_read_ptr + len < scratch_buf_size) { | |
305 | memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len); | |
306 | usbvision->scratch_read_ptr += len; | |
307 | } | |
308 | else { | |
309 | len_part = scratch_buf_size - usbvision->scratch_read_ptr; | |
310 | memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part); | |
311 | if (len == len_part) { | |
312 | usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */ | |
313 | } | |
314 | else { | |
315 | memcpy(data + len_part, usbvision->scratch, len - len_part); | |
316 | usbvision->scratch_read_ptr = len - len_part; | |
317 | } | |
318 | } | |
319 | ||
320 | PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr); | |
321 | ||
322 | return len; | |
323 | } | |
324 | ||
325 | ||
326 | /* This sets read pointer to next header and returns it */ | |
0a0ceade AB |
327 | static int scratch_get_header(struct usb_usbvision *usbvision, |
328 | struct usbvision_frame_header *header) | |
781aa1d1 | 329 | { |
5490a7cb | 330 | int err_code = 0; |
781aa1d1 MCC |
331 | |
332 | PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr); | |
333 | ||
334 | while (usbvision->scratch_headermarker_write_ptr - | |
335 | usbvision->scratch_headermarker_read_ptr != 0) { | |
336 | usbvision->scratch_read_ptr = | |
337 | usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr]; | |
338 | usbvision->scratch_headermarker_read_ptr += 1; | |
339 | usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER; | |
340 | scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH); | |
341 | if ((header->magic_1 == USBVISION_MAGIC_1) | |
342 | && (header->magic_2 == USBVISION_MAGIC_2) | |
5490a7cb HV |
343 | && (header->header_length == USBVISION_HEADER_LENGTH)) { |
344 | err_code = USBVISION_HEADER_LENGTH; | |
345 | header->frame_width = header->frame_width_lo + (header->frame_width_hi << 8); | |
346 | header->frame_height = header->frame_height_lo + (header->frame_height_hi << 8); | |
781aa1d1 MCC |
347 | break; |
348 | } | |
349 | } | |
350 | ||
5490a7cb | 351 | return err_code; |
781aa1d1 MCC |
352 | } |
353 | ||
354 | ||
52cb0bf2 | 355 | /* This removes len bytes of old data from the buffer */ |
0a0ceade | 356 | static void scratch_rm_old(struct usb_usbvision *usbvision, int len) |
781aa1d1 MCC |
357 | { |
358 | ||
359 | usbvision->scratch_read_ptr += len; | |
360 | usbvision->scratch_read_ptr %= scratch_buf_size; | |
361 | PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr); | |
362 | } | |
363 | ||
364 | ||
52cb0bf2 | 365 | /* This resets the buffer - kills all data in it too */ |
0a0ceade | 366 | static void scratch_reset(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
367 | { |
368 | PDEBUG(DBG_SCRATCH, "\n"); | |
369 | ||
370 | usbvision->scratch_read_ptr = 0; | |
371 | usbvision->scratch_write_ptr = 0; | |
372 | usbvision->scratch_headermarker_read_ptr = 0; | |
373 | usbvision->scratch_headermarker_write_ptr = 0; | |
5490a7cb | 374 | usbvision->isocstate = isoc_state_no_frame; |
781aa1d1 MCC |
375 | } |
376 | ||
483dfdb6 | 377 | int usbvision_scratch_alloc(struct usb_usbvision *usbvision) |
781aa1d1 | 378 | { |
38284ba3 | 379 | usbvision->scratch = vmalloc_32(scratch_buf_size); |
483dfdb6 TM |
380 | scratch_reset(usbvision); |
381 | if(usbvision->scratch == NULL) { | |
be9ed511 MCC |
382 | dev_err(&usbvision->dev->dev, |
383 | "%s: unable to allocate %d bytes for scratch\n", | |
384 | __func__, scratch_buf_size); | |
483dfdb6 | 385 | return -ENOMEM; |
781aa1d1 | 386 | } |
483dfdb6 | 387 | return 0; |
781aa1d1 MCC |
388 | } |
389 | ||
483dfdb6 | 390 | void usbvision_scratch_free(struct usb_usbvision *usbvision) |
781aa1d1 | 391 | { |
5e2c217e F |
392 | vfree(usbvision->scratch); |
393 | usbvision->scratch = NULL; | |
394 | ||
781aa1d1 MCC |
395 | } |
396 | ||
781aa1d1 MCC |
397 | /* |
398 | * usbvision_testpattern() | |
399 | * | |
400 | * Procedure forms a test pattern (yellow grid on blue background). | |
401 | * | |
402 | * Parameters: | |
403 | * fullframe: if TRUE then entire frame is filled, otherwise the procedure | |
18d8a454 | 404 | * continues from the current scanline. |
781aa1d1 | 405 | * pmode 0: fill the frame with solid blue color (like on VCR or TV) |
18d8a454 | 406 | * 1: Draw a colored grid |
781aa1d1 MCC |
407 | * |
408 | */ | |
0a0ceade AB |
409 | static void usbvision_testpattern(struct usb_usbvision *usbvision, |
410 | int fullframe, int pmode) | |
781aa1d1 MCC |
411 | { |
412 | static const char proc[] = "usbvision_testpattern"; | |
413 | struct usbvision_frame *frame; | |
414 | unsigned char *f; | |
415 | int num_cell = 0; | |
416 | int scan_length = 0; | |
ff699e6b | 417 | static int num_pass; |
781aa1d1 MCC |
418 | |
419 | if (usbvision == NULL) { | |
420 | printk(KERN_ERR "%s: usbvision == NULL\n", proc); | |
421 | return; | |
422 | } | |
5490a7cb HV |
423 | if (usbvision->cur_frame == NULL) { |
424 | printk(KERN_ERR "%s: usbvision->cur_frame is NULL.\n", proc); | |
781aa1d1 MCC |
425 | return; |
426 | } | |
427 | ||
428 | /* Grab the current frame */ | |
5490a7cb | 429 | frame = usbvision->cur_frame; |
781aa1d1 MCC |
430 | |
431 | /* Optionally start at the beginning */ | |
432 | if (fullframe) { | |
433 | frame->curline = 0; | |
434 | frame->scanlength = 0; | |
435 | } | |
436 | ||
437 | /* Form every scan line */ | |
438 | for (; frame->curline < frame->frmheight; frame->curline++) { | |
439 | int i; | |
440 | ||
441 | f = frame->data + (usbvision->curwidth * 3 * frame->curline); | |
442 | for (i = 0; i < usbvision->curwidth; i++) { | |
443 | unsigned char cb = 0x80; | |
444 | unsigned char cg = 0; | |
445 | unsigned char cr = 0; | |
446 | ||
447 | if (pmode == 1) { | |
448 | if (frame->curline % 32 == 0) | |
449 | cb = 0, cg = cr = 0xFF; | |
450 | else if (i % 32 == 0) { | |
451 | if (frame->curline % 32 == 1) | |
452 | num_cell++; | |
453 | cb = 0, cg = cr = 0xFF; | |
454 | } else { | |
455 | cb = | |
456 | ((num_cell * 7) + | |
457 | num_pass) & 0xFF; | |
458 | cg = | |
459 | ((num_cell * 5) + | |
460 | num_pass * 2) & 0xFF; | |
461 | cr = | |
462 | ((num_cell * 3) + | |
463 | num_pass * 3) & 0xFF; | |
464 | } | |
465 | } else { | |
466 | /* Just the blue screen */ | |
467 | } | |
468 | ||
469 | *f++ = cb; | |
470 | *f++ = cg; | |
471 | *f++ = cr; | |
472 | scan_length += 3; | |
473 | } | |
474 | } | |
475 | ||
5490a7cb | 476 | frame->grabstate = frame_state_done; |
781aa1d1 MCC |
477 | frame->scanlength += scan_length; |
478 | ++num_pass; | |
479 | ||
781aa1d1 MCC |
480 | } |
481 | ||
482 | /* | |
483dfdb6 TM |
483 | * usbvision_decompress_alloc() |
484 | * | |
485 | * allocates intermediate buffer for decompression | |
781aa1d1 | 486 | */ |
483dfdb6 TM |
487 | int usbvision_decompress_alloc(struct usb_usbvision *usbvision) |
488 | { | |
489 | int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2; | |
5490a7cb HV |
490 | usbvision->intra_frame_buffer = vmalloc_32(IFB_size); |
491 | if (usbvision->intra_frame_buffer == NULL) { | |
be9ed511 MCC |
492 | dev_err(&usbvision->dev->dev, |
493 | "%s: unable to allocate %d for compr. frame buffer\n", | |
494 | __func__, IFB_size); | |
483dfdb6 TM |
495 | return -ENOMEM; |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | /* | |
501 | * usbvision_decompress_free() | |
502 | * | |
503 | * frees intermediate buffer for decompression | |
504 | */ | |
505 | void usbvision_decompress_free(struct usb_usbvision *usbvision) | |
506 | { | |
5490a7cb HV |
507 | vfree(usbvision->intra_frame_buffer); |
508 | usbvision->intra_frame_buffer = NULL; | |
5e2c217e | 509 | |
483dfdb6 TM |
510 | } |
511 | ||
512 | /************************************************************ | |
513 | * Here comes the data parsing stuff that is run as interrupt | |
514 | ************************************************************/ | |
781aa1d1 MCC |
515 | /* |
516 | * usbvision_find_header() | |
517 | * | |
518 | * Locate one of supported header markers in the scratch buffer. | |
519 | */ | |
5490a7cb | 520 | static enum parse_state usbvision_find_header(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
521 | { |
522 | struct usbvision_frame *frame; | |
5490a7cb | 523 | int found_header = 0; |
781aa1d1 | 524 | |
5490a7cb | 525 | frame = usbvision->cur_frame; |
781aa1d1 | 526 | |
5490a7cb | 527 | while (scratch_get_header(usbvision, &frame->isoc_header) == USBVISION_HEADER_LENGTH) { |
52cb0bf2 | 528 | /* found header in scratch */ |
781aa1d1 | 529 | PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u", |
5490a7cb HV |
530 | frame->isoc_header.magic_2, |
531 | frame->isoc_header.magic_1, | |
532 | frame->isoc_header.header_length, | |
533 | frame->isoc_header.frame_num, | |
534 | frame->isoc_header.frame_phase, | |
535 | frame->isoc_header.frame_latency, | |
536 | frame->isoc_header.data_format, | |
537 | frame->isoc_header.format_param, | |
538 | frame->isoc_header.frame_width, | |
539 | frame->isoc_header.frame_height); | |
540 | ||
541 | if (usbvision->request_intra) { | |
542 | if (frame->isoc_header.format_param & 0x80) { | |
543 | found_header = 1; | |
52cb0bf2 | 544 | usbvision->last_isoc_frame_num = -1; /* do not check for lost frames this time */ |
781aa1d1 MCC |
545 | usbvision_unrequest_intra(usbvision); |
546 | break; | |
547 | } | |
548 | } | |
549 | else { | |
5490a7cb | 550 | found_header = 1; |
781aa1d1 MCC |
551 | break; |
552 | } | |
553 | } | |
554 | ||
5490a7cb HV |
555 | if (found_header) { |
556 | frame->frmwidth = frame->isoc_header.frame_width * usbvision->stretch_width; | |
557 | frame->frmheight = frame->isoc_header.frame_height * usbvision->stretch_height; | |
781aa1d1 | 558 | frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3; |
781aa1d1 | 559 | } |
52cb0bf2 | 560 | else { /* no header found */ |
781aa1d1 MCC |
561 | PDEBUG(DBG_HEADER, "skipping scratch data, no header"); |
562 | scratch_reset(usbvision); | |
5490a7cb | 563 | return parse_state_end_parse; |
781aa1d1 MCC |
564 | } |
565 | ||
52cb0bf2 | 566 | /* found header */ |
5490a7cb | 567 | if (frame->isoc_header.data_format==ISOC_MODE_COMPRESS) { |
52cb0bf2 | 568 | /* check isoc_header.frame_num for lost frames */ |
5490a7cb HV |
569 | if (usbvision->last_isoc_frame_num >= 0) { |
570 | if (((usbvision->last_isoc_frame_num + 1) % 32) != frame->isoc_header.frame_num) { | |
52cb0bf2 | 571 | /* unexpected frame drop: need to request new intra frame */ |
5490a7cb | 572 | PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isoc_header.frame_num); |
781aa1d1 | 573 | usbvision_request_intra(usbvision); |
5490a7cb | 574 | return parse_state_next_frame; |
781aa1d1 MCC |
575 | } |
576 | } | |
5490a7cb | 577 | usbvision->last_isoc_frame_num = frame->isoc_header.frame_num; |
781aa1d1 MCC |
578 | } |
579 | usbvision->header_count++; | |
5490a7cb | 580 | frame->scanstate = scan_state_lines; |
781aa1d1 MCC |
581 | frame->curline = 0; |
582 | ||
483dfdb6 | 583 | if (force_testpattern) { |
781aa1d1 | 584 | usbvision_testpattern(usbvision, 1, 1); |
5490a7cb | 585 | return parse_state_next_frame; |
781aa1d1 | 586 | } |
5490a7cb | 587 | return parse_state_continue; |
781aa1d1 MCC |
588 | } |
589 | ||
5490a7cb | 590 | static enum parse_state usbvision_parse_lines_422(struct usb_usbvision *usbvision, |
781aa1d1 MCC |
591 | long *pcopylen) |
592 | { | |
593 | volatile struct usbvision_frame *frame; | |
594 | unsigned char *f; | |
595 | int len; | |
596 | int i; | |
52cb0bf2 HV |
597 | unsigned char yuyv[4]={180, 128, 10, 128}; /* YUV components */ |
598 | unsigned char rv, gv, bv; /* RGB components */ | |
781aa1d1 | 599 | int clipmask_index, bytes_per_pixel; |
781aa1d1 MCC |
600 | int stretch_bytes, clipmask_add; |
601 | ||
5490a7cb | 602 | frame = usbvision->cur_frame; |
483dfdb6 | 603 | f = frame->data + (frame->v4l2_linesize * frame->curline); |
781aa1d1 MCC |
604 | |
605 | /* Make sure there's enough data for the entire line */ | |
5490a7cb | 606 | len = (frame->isoc_header.frame_width * 2)+5; |
781aa1d1 MCC |
607 | if (scratch_len(usbvision) < len) { |
608 | PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len); | |
5490a7cb | 609 | return parse_state_out; |
781aa1d1 MCC |
610 | } |
611 | ||
612 | if ((frame->curline + 1) >= frame->frmheight) { | |
5490a7cb | 613 | return parse_state_next_frame; |
781aa1d1 MCC |
614 | } |
615 | ||
616 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; | |
617 | stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel; | |
618 | clipmask_index = frame->curline * MAX_FRAME_WIDTH; | |
619 | clipmask_add = usbvision->stretch_width; | |
620 | ||
621 | for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) { | |
622 | ||
623 | scratch_get(usbvision, &yuyv[0], 4); | |
624 | ||
483dfdb6 | 625 | if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
52cb0bf2 HV |
626 | *f++ = yuyv[0]; /* Y */ |
627 | *f++ = yuyv[3]; /* U */ | |
781aa1d1 MCC |
628 | } |
629 | else { | |
630 | ||
631 | YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv); | |
632 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
633 | case V4L2_PIX_FMT_RGB565: |
634 | *f++ = (0x1F & rv) | | |
635 | (0xE0 & (gv << 5)); | |
636 | *f++ = (0x07 & (gv >> 3)) | | |
637 | (0xF8 & bv); | |
638 | break; | |
639 | case V4L2_PIX_FMT_RGB24: | |
640 | *f++ = rv; | |
641 | *f++ = gv; | |
642 | *f++ = bv; | |
643 | break; | |
644 | case V4L2_PIX_FMT_RGB32: | |
645 | *f++ = rv; | |
646 | *f++ = gv; | |
647 | *f++ = bv; | |
648 | f++; | |
649 | break; | |
650 | case V4L2_PIX_FMT_RGB555: | |
651 | *f++ = (0x1F & rv) | | |
652 | (0xE0 & (gv << 5)); | |
653 | *f++ = (0x03 & (gv >> 3)) | | |
654 | (0x7C & (bv << 2)); | |
655 | break; | |
781aa1d1 MCC |
656 | } |
657 | } | |
658 | clipmask_index += clipmask_add; | |
659 | f += stretch_bytes; | |
660 | ||
483dfdb6 | 661 | if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
52cb0bf2 HV |
662 | *f++ = yuyv[2]; /* Y */ |
663 | *f++ = yuyv[1]; /* V */ | |
781aa1d1 MCC |
664 | } |
665 | else { | |
666 | ||
667 | YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv); | |
668 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
669 | case V4L2_PIX_FMT_RGB565: |
670 | *f++ = (0x1F & rv) | | |
671 | (0xE0 & (gv << 5)); | |
672 | *f++ = (0x07 & (gv >> 3)) | | |
673 | (0xF8 & bv); | |
674 | break; | |
675 | case V4L2_PIX_FMT_RGB24: | |
676 | *f++ = rv; | |
677 | *f++ = gv; | |
678 | *f++ = bv; | |
679 | break; | |
680 | case V4L2_PIX_FMT_RGB32: | |
681 | *f++ = rv; | |
682 | *f++ = gv; | |
683 | *f++ = bv; | |
684 | f++; | |
685 | break; | |
686 | case V4L2_PIX_FMT_RGB555: | |
687 | *f++ = (0x1F & rv) | | |
688 | (0xE0 & (gv << 5)); | |
689 | *f++ = (0x03 & (gv >> 3)) | | |
690 | (0x7C & (bv << 2)); | |
691 | break; | |
781aa1d1 MCC |
692 | } |
693 | } | |
694 | clipmask_index += clipmask_add; | |
695 | f += stretch_bytes; | |
696 | } | |
697 | ||
698 | frame->curline += usbvision->stretch_height; | |
699 | *pcopylen += frame->v4l2_linesize * usbvision->stretch_height; | |
700 | ||
701 | if (frame->curline >= frame->frmheight) { | |
5490a7cb | 702 | return parse_state_next_frame; |
781aa1d1 MCC |
703 | } |
704 | else { | |
5490a7cb | 705 | return parse_state_continue; |
781aa1d1 MCC |
706 | } |
707 | } | |
708 | ||
483dfdb6 | 709 | /* The decompression routine */ |
5490a7cb HV |
710 | static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *compressed, |
711 | unsigned char *decompressed, int *start_pos, | |
712 | int *block_typestart_pos, int len) | |
781aa1d1 | 713 | { |
5490a7cb HV |
714 | int rest_pixel, idx, max_pos, pos, extra_pos, block_len, block_type_pos, block_type_len; |
715 | unsigned char block_byte, block_code, block_type, block_type_byte, integrator; | |
716 | ||
717 | integrator = 0; | |
718 | pos = *start_pos; | |
719 | block_type_pos = *block_typestart_pos; | |
52cb0bf2 | 720 | max_pos = 396; /* pos + len; */ |
5490a7cb HV |
721 | extra_pos = pos; |
722 | block_len = 0; | |
723 | block_byte = 0; | |
724 | block_code = 0; | |
725 | block_type = 0; | |
726 | block_type_byte = 0; | |
727 | block_type_len = 0; | |
728 | rest_pixel = len; | |
729 | ||
730 | for (idx = 0; idx < len; idx++) { | |
731 | ||
732 | if (block_len == 0) { | |
733 | if (block_type_len==0) { | |
734 | block_type_byte = compressed[block_type_pos]; | |
735 | block_type_pos++; | |
736 | block_type_len = 4; | |
781aa1d1 | 737 | } |
5490a7cb | 738 | block_type = (block_type_byte & 0xC0) >> 6; |
781aa1d1 | 739 | |
52cb0bf2 | 740 | /* statistic: */ |
5490a7cb HV |
741 | usbvision->compr_block_types[block_type]++; |
742 | ||
743 | pos = extra_pos; | |
744 | if (block_type == 0) { | |
745 | if(rest_pixel >= 24) { | |
746 | idx += 23; | |
747 | rest_pixel -= 24; | |
748 | integrator = decompressed[idx]; | |
781aa1d1 | 749 | } else { |
5490a7cb HV |
750 | idx += rest_pixel - 1; |
751 | rest_pixel = 0; | |
781aa1d1 MCC |
752 | } |
753 | } else { | |
5490a7cb HV |
754 | block_code = compressed[pos]; |
755 | pos++; | |
756 | if (rest_pixel >= 24) { | |
757 | block_len = 24; | |
781aa1d1 | 758 | } else { |
5490a7cb | 759 | block_len = rest_pixel; |
781aa1d1 | 760 | } |
5490a7cb HV |
761 | rest_pixel -= block_len; |
762 | extra_pos = pos + (block_len / 4); | |
781aa1d1 | 763 | } |
5490a7cb HV |
764 | block_type_byte <<= 2; |
765 | block_type_len -= 1; | |
781aa1d1 | 766 | } |
5490a7cb HV |
767 | if (block_len > 0) { |
768 | if ((block_len%4) == 0) { | |
769 | block_byte = compressed[pos]; | |
770 | pos++; | |
781aa1d1 | 771 | } |
52cb0bf2 | 772 | if (block_type == 1) { /* inter Block */ |
5490a7cb | 773 | integrator = decompressed[idx]; |
781aa1d1 | 774 | } |
5490a7cb | 775 | switch (block_byte & 0xC0) { |
781aa1d1 | 776 | case 0x03<<6: |
5490a7cb HV |
777 | integrator += compressed[extra_pos]; |
778 | extra_pos++; | |
781aa1d1 MCC |
779 | break; |
780 | case 0x02<<6: | |
5490a7cb | 781 | integrator += block_code; |
781aa1d1 MCC |
782 | break; |
783 | case 0x00: | |
5490a7cb | 784 | integrator -= block_code; |
781aa1d1 MCC |
785 | break; |
786 | } | |
5490a7cb HV |
787 | decompressed[idx] = integrator; |
788 | block_byte <<= 2; | |
789 | block_len -= 1; | |
781aa1d1 MCC |
790 | } |
791 | } | |
5490a7cb HV |
792 | *start_pos = extra_pos; |
793 | *block_typestart_pos = block_type_pos; | |
794 | return idx; | |
781aa1d1 MCC |
795 | } |
796 | ||
797 | ||
798 | /* | |
799 | * usbvision_parse_compress() | |
800 | * | |
801 | * Parse compressed frame from the scratch buffer, put | |
802 | * decoded RGB value into the current frame buffer and add the written | |
803 | * number of bytes (RGB) to the *pcopylen. | |
804 | * | |
805 | */ | |
5490a7cb | 806 | static enum parse_state usbvision_parse_compress(struct usb_usbvision *usbvision, |
781aa1d1 MCC |
807 | long *pcopylen) |
808 | { | |
809 | #define USBVISION_STRIP_MAGIC 0x5A | |
810 | #define USBVISION_STRIP_LEN_MAX 400 | |
811 | #define USBVISION_STRIP_HEADER_LEN 3 | |
812 | ||
813 | struct usbvision_frame *frame; | |
814 | unsigned char *f,*u = NULL ,*v = NULL; | |
5490a7cb HV |
815 | unsigned char strip_data[USBVISION_STRIP_LEN_MAX]; |
816 | unsigned char strip_header[USBVISION_STRIP_HEADER_LEN]; | |
817 | int idx, idx_end, strip_len, strip_ptr, Startblock_pos, block_pos, block_type_pos; | |
781aa1d1 | 818 | int clipmask_index, bytes_per_pixel, rc; |
5490a7cb | 819 | int image_size; |
781aa1d1 MCC |
820 | unsigned char rv, gv, bv; |
821 | static unsigned char *Y, *U, *V; | |
822 | ||
5490a7cb HV |
823 | frame = usbvision->cur_frame; |
824 | image_size = frame->frmwidth * frame->frmheight; | |
483dfdb6 | 825 | if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) || |
52cb0bf2 HV |
826 | (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) { /* this is a planar format */ |
827 | /* ... v4l2_linesize not used here. */ | |
483dfdb6 TM |
828 | f = frame->data + (frame->width * frame->curline); |
829 | } else | |
830 | f = frame->data + (frame->v4l2_linesize * frame->curline); | |
831 | ||
52cb0bf2 HV |
832 | if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ /* initialise u and v pointers */ |
833 | /* get base of u and b planes add halfoffset */ | |
483dfdb6 TM |
834 | |
835 | u = frame->data | |
5490a7cb | 836 | + image_size |
483dfdb6 | 837 | + (frame->frmwidth >>1) * frame->curline ; |
5490a7cb | 838 | v = u + (image_size >>1 ); |
483dfdb6 TM |
839 | |
840 | } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){ | |
841 | ||
5490a7cb HV |
842 | v = frame->data + image_size + ((frame->curline* (frame->width))>>2) ; |
843 | u = v + (image_size >>2) ; | |
781aa1d1 MCC |
844 | } |
845 | ||
846 | if (frame->curline == 0) { | |
847 | usbvision_adjust_compression(usbvision); | |
848 | } | |
849 | ||
850 | if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) { | |
5490a7cb | 851 | return parse_state_out; |
781aa1d1 MCC |
852 | } |
853 | ||
52cb0bf2 | 854 | /* get strip header without changing the scratch_read_ptr */ |
5490a7cb HV |
855 | scratch_set_extra_ptr(usbvision, &strip_ptr, 0); |
856 | scratch_get_extra(usbvision, &strip_header[0], &strip_ptr, | |
781aa1d1 MCC |
857 | USBVISION_STRIP_HEADER_LEN); |
858 | ||
5490a7cb | 859 | if (strip_header[0] != USBVISION_STRIP_MAGIC) { |
52cb0bf2 | 860 | /* wrong strip magic */ |
5490a7cb HV |
861 | usbvision->strip_magic_errors++; |
862 | return parse_state_next_frame; | |
781aa1d1 MCC |
863 | } |
864 | ||
5490a7cb | 865 | if (frame->curline != (int)strip_header[2]) { |
52cb0bf2 | 866 | /* line number missmatch error */ |
5490a7cb | 867 | usbvision->strip_line_number_errors++; |
781aa1d1 MCC |
868 | } |
869 | ||
5490a7cb HV |
870 | strip_len = 2 * (unsigned int)strip_header[1]; |
871 | if (strip_len > USBVISION_STRIP_LEN_MAX) { | |
52cb0bf2 HV |
872 | /* strip overrun */ |
873 | /* I think this never happens */ | |
781aa1d1 MCC |
874 | usbvision_request_intra(usbvision); |
875 | } | |
876 | ||
5490a7cb | 877 | if (scratch_len(usbvision) < strip_len) { |
52cb0bf2 | 878 | /* there is not enough data for the strip */ |
5490a7cb | 879 | return parse_state_out; |
781aa1d1 MCC |
880 | } |
881 | ||
5490a7cb HV |
882 | if (usbvision->intra_frame_buffer) { |
883 | Y = usbvision->intra_frame_buffer + frame->frmwidth * frame->curline; | |
884 | U = usbvision->intra_frame_buffer + image_size + (frame->frmwidth / 2) * (frame->curline / 2); | |
885 | V = usbvision->intra_frame_buffer + image_size / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2); | |
781aa1d1 MCC |
886 | } |
887 | else { | |
5490a7cb | 888 | return parse_state_next_frame; |
781aa1d1 MCC |
889 | } |
890 | ||
18d8a454 | 891 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; |
781aa1d1 MCC |
892 | clipmask_index = frame->curline * MAX_FRAME_WIDTH; |
893 | ||
5490a7cb | 894 | scratch_get(usbvision, strip_data, strip_len); |
781aa1d1 | 895 | |
5490a7cb HV |
896 | idx_end = frame->frmwidth; |
897 | block_type_pos = USBVISION_STRIP_HEADER_LEN; | |
898 | Startblock_pos = block_type_pos + (idx_end - 1) / 96 + (idx_end / 2 - 1) / 96 + 2; | |
899 | block_pos = Startblock_pos; | |
781aa1d1 | 900 | |
5490a7cb | 901 | usbvision->block_pos = block_pos; |
781aa1d1 | 902 | |
5490a7cb | 903 | if ((rc = usbvision_decompress(usbvision, strip_data, Y, &block_pos, &block_type_pos, idx_end)) != idx_end) { |
52cb0bf2 | 904 | /* return parse_state_continue; */ |
781aa1d1 | 905 | } |
5490a7cb HV |
906 | if (strip_len > usbvision->max_strip_len) { |
907 | usbvision->max_strip_len = strip_len; | |
781aa1d1 MCC |
908 | } |
909 | ||
910 | if (frame->curline%2) { | |
5490a7cb | 911 | if ((rc = usbvision_decompress(usbvision, strip_data, V, &block_pos, &block_type_pos, idx_end/2)) != idx_end/2) { |
52cb0bf2 | 912 | /* return parse_state_continue; */ |
781aa1d1 MCC |
913 | } |
914 | } | |
915 | else { | |
5490a7cb | 916 | if ((rc = usbvision_decompress(usbvision, strip_data, U, &block_pos, &block_type_pos, idx_end/2)) != idx_end/2) { |
52cb0bf2 | 917 | /* return parse_state_continue; */ |
781aa1d1 MCC |
918 | } |
919 | } | |
920 | ||
5490a7cb HV |
921 | if (block_pos > usbvision->comprblock_pos) { |
922 | usbvision->comprblock_pos = block_pos; | |
781aa1d1 | 923 | } |
5490a7cb HV |
924 | if (block_pos > strip_len) { |
925 | usbvision->strip_len_errors++; | |
781aa1d1 | 926 | } |
18d8a454 | 927 | |
5490a7cb | 928 | for (idx = 0; idx < idx_end; idx++) { |
483dfdb6 | 929 | if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
5490a7cb HV |
930 | *f++ = Y[idx]; |
931 | *f++ = idx & 0x01 ? U[idx/2] : V[idx/2]; | |
781aa1d1 MCC |
932 | } |
933 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) { | |
5490a7cb HV |
934 | *f++ = Y[idx]; |
935 | if ( idx & 0x01) | |
936 | *u++ = U[idx>>1] ; | |
781aa1d1 | 937 | else |
5490a7cb | 938 | *v++ = V[idx>>1]; |
781aa1d1 MCC |
939 | } |
940 | else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) { | |
5490a7cb HV |
941 | *f++ = Y [idx]; |
942 | if ( !(( idx & 0x01 ) | ( frame->curline & 0x01 )) ){ | |
18d8a454 | 943 | |
781aa1d1 MCC |
944 | /* only need do this for 1 in 4 pixels */ |
945 | /* intraframe buffer is YUV420 format */ | |
946 | ||
5490a7cb HV |
947 | *u++ = U[idx >>1]; |
948 | *v++ = V[idx >>1]; | |
781aa1d1 | 949 | } |
18d8a454 | 950 | |
781aa1d1 MCC |
951 | } |
952 | else { | |
5490a7cb | 953 | YUV_TO_RGB_BY_THE_BOOK(Y[idx], U[idx/2], V[idx/2], rv, gv, bv); |
781aa1d1 MCC |
954 | switch (frame->v4l2_format.format) { |
955 | case V4L2_PIX_FMT_GREY: | |
5490a7cb | 956 | *f++ = Y[idx]; |
781aa1d1 MCC |
957 | break; |
958 | case V4L2_PIX_FMT_RGB555: | |
6200bbaa TM |
959 | *f++ = (0x1F & rv) | |
960 | (0xE0 & (gv << 5)); | |
961 | *f++ = (0x03 & (gv >> 3)) | | |
962 | (0x7C & (bv << 2)); | |
781aa1d1 MCC |
963 | break; |
964 | case V4L2_PIX_FMT_RGB565: | |
6200bbaa TM |
965 | *f++ = (0x1F & rv) | |
966 | (0xE0 & (gv << 5)); | |
967 | *f++ = (0x07 & (gv >> 3)) | | |
968 | (0xF8 & bv); | |
781aa1d1 MCC |
969 | break; |
970 | case V4L2_PIX_FMT_RGB24: | |
781aa1d1 | 971 | *f++ = rv; |
6200bbaa TM |
972 | *f++ = gv; |
973 | *f++ = bv; | |
781aa1d1 MCC |
974 | break; |
975 | case V4L2_PIX_FMT_RGB32: | |
781aa1d1 | 976 | *f++ = rv; |
6200bbaa TM |
977 | *f++ = gv; |
978 | *f++ = bv; | |
781aa1d1 MCC |
979 | f++; |
980 | break; | |
981 | } | |
982 | } | |
983 | clipmask_index++; | |
984 | } | |
18d8a454 DG |
985 | /* Deal with non-integer no. of bytes for YUV420P */ |
986 | if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 ) | |
987 | *pcopylen += frame->v4l2_linesize; | |
988 | else | |
989 | *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1; | |
990 | ||
781aa1d1 MCC |
991 | frame->curline += 1; |
992 | ||
993 | if (frame->curline >= frame->frmheight) { | |
5490a7cb | 994 | return parse_state_next_frame; |
781aa1d1 MCC |
995 | } |
996 | else { | |
5490a7cb | 997 | return parse_state_continue; |
781aa1d1 MCC |
998 | } |
999 | ||
1000 | } | |
1001 | ||
1002 | ||
1003 | /* | |
1004 | * usbvision_parse_lines_420() | |
1005 | * | |
1006 | * Parse two lines from the scratch buffer, put | |
1007 | * decoded RGB value into the current frame buffer and add the written | |
1008 | * number of bytes (RGB) to the *pcopylen. | |
1009 | * | |
1010 | */ | |
5490a7cb | 1011 | static enum parse_state usbvision_parse_lines_420(struct usb_usbvision *usbvision, |
781aa1d1 MCC |
1012 | long *pcopylen) |
1013 | { | |
1014 | struct usbvision_frame *frame; | |
1015 | unsigned char *f_even = NULL, *f_odd = NULL; | |
1016 | unsigned int pixel_per_line, block; | |
1017 | int pixel, block_split; | |
1018 | int y_ptr, u_ptr, v_ptr, y_odd_offset; | |
1019 | const int y_block_size = 128; | |
1020 | const int uv_block_size = 64; | |
1021 | const int sub_block_size = 32; | |
1022 | const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4; | |
1023 | const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4; | |
1024 | unsigned char y[2], u, v; /* YUV components */ | |
1025 | int y_, u_, v_, vb, uvg, ur; | |
1026 | int r_, g_, b_; /* RGB components */ | |
1027 | unsigned char g; | |
1028 | int clipmask_even_index, clipmask_odd_index, bytes_per_pixel; | |
1029 | int clipmask_add, stretch_bytes; | |
781aa1d1 | 1030 | |
5490a7cb | 1031 | frame = usbvision->cur_frame; |
483dfdb6 TM |
1032 | f_even = frame->data + (frame->v4l2_linesize * frame->curline); |
1033 | f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height; | |
781aa1d1 MCC |
1034 | |
1035 | /* Make sure there's enough data for the entire line */ | |
1036 | /* In this mode usbvision transfer 3 bytes for every 2 pixels */ | |
1037 | /* I need two lines to decode the color */ | |
18d8a454 | 1038 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; |
781aa1d1 MCC |
1039 | stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel; |
1040 | clipmask_even_index = frame->curline * MAX_FRAME_WIDTH; | |
1041 | clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH; | |
1042 | clipmask_add = usbvision->stretch_width; | |
5490a7cb | 1043 | pixel_per_line = frame->isoc_header.frame_width; |
781aa1d1 MCC |
1044 | |
1045 | if (scratch_len(usbvision) < (int)pixel_per_line * 3) { | |
52cb0bf2 | 1046 | /* printk(KERN_DEBUG "out of data, need %d\n", len); */ |
5490a7cb | 1047 | return parse_state_out; |
781aa1d1 MCC |
1048 | } |
1049 | ||
1050 | if ((frame->curline + 1) >= frame->frmheight) { | |
5490a7cb | 1051 | return parse_state_next_frame; |
781aa1d1 MCC |
1052 | } |
1053 | ||
52cb0bf2 | 1054 | block_split = (pixel_per_line%y_block_size) ? 1 : 0; /* are some blocks splitted into different lines? */ |
781aa1d1 MCC |
1055 | |
1056 | y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size) | |
1057 | + block_split * uv_block_size; | |
1058 | ||
1059 | scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset); | |
1060 | scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size); | |
1061 | scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset | |
1062 | + (4 - block_split) * sub_block_size); | |
1063 | ||
1064 | for (block = 0; block < (pixel_per_line / sub_block_size); | |
1065 | block++) { | |
1066 | ||
1067 | ||
1068 | for (pixel = 0; pixel < sub_block_size; pixel +=2) { | |
1069 | scratch_get(usbvision, &y[0], 2); | |
1070 | scratch_get_extra(usbvision, &u, &u_ptr, 1); | |
1071 | scratch_get_extra(usbvision, &v, &v_ptr, 1); | |
1072 | ||
52cb0bf2 | 1073 | /* I don't use the YUV_TO_RGB macro for better performance */ |
781aa1d1 MCC |
1074 | v_ = v - 128; |
1075 | u_ = u - 128; | |
1076 | vb = 132252 * v_; | |
1077 | uvg= -53281 * u_ - 25625 * v_; | |
1078 | ur = 104595 * u_; | |
1079 | ||
483dfdb6 | 1080 | if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
781aa1d1 MCC |
1081 | *f_even++ = y[0]; |
1082 | *f_even++ = v; | |
1083 | } | |
1084 | else { | |
1085 | y_ = 76284 * (y[0] - 16); | |
1086 | ||
1087 | b_ = (y_ + vb) >> 16; | |
1088 | g_ = (y_ + uvg)>> 16; | |
1089 | r_ = (y_ + ur) >> 16; | |
1090 | ||
1091 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
1092 | case V4L2_PIX_FMT_RGB565: |
1093 | g = LIMIT_RGB(g_); | |
1094 | *f_even++ = | |
1095 | (0x1F & LIMIT_RGB(r_)) | | |
1096 | (0xE0 & (g << 5)); | |
1097 | *f_even++ = | |
1098 | (0x07 & (g >> 3)) | | |
1099 | (0xF8 & LIMIT_RGB(b_)); | |
1100 | break; | |
1101 | case V4L2_PIX_FMT_RGB24: | |
1102 | *f_even++ = LIMIT_RGB(r_); | |
1103 | *f_even++ = LIMIT_RGB(g_); | |
1104 | *f_even++ = LIMIT_RGB(b_); | |
1105 | break; | |
1106 | case V4L2_PIX_FMT_RGB32: | |
1107 | *f_even++ = LIMIT_RGB(r_); | |
1108 | *f_even++ = LIMIT_RGB(g_); | |
1109 | *f_even++ = LIMIT_RGB(b_); | |
1110 | f_even++; | |
1111 | break; | |
1112 | case V4L2_PIX_FMT_RGB555: | |
1113 | g = LIMIT_RGB(g_); | |
1114 | *f_even++ = (0x1F & LIMIT_RGB(r_)) | | |
1115 | (0xE0 & (g << 5)); | |
1116 | *f_even++ = (0x03 & (g >> 3)) | | |
1117 | (0x7C & (LIMIT_RGB(b_) << 2)); | |
1118 | break; | |
781aa1d1 MCC |
1119 | } |
1120 | } | |
1121 | clipmask_even_index += clipmask_add; | |
1122 | f_even += stretch_bytes; | |
1123 | ||
483dfdb6 | 1124 | if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
781aa1d1 MCC |
1125 | *f_even++ = y[1]; |
1126 | *f_even++ = u; | |
1127 | } | |
1128 | else { | |
1129 | y_ = 76284 * (y[1] - 16); | |
1130 | ||
1131 | b_ = (y_ + vb) >> 16; | |
1132 | g_ = (y_ + uvg)>> 16; | |
1133 | r_ = (y_ + ur) >> 16; | |
1134 | ||
1135 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
1136 | case V4L2_PIX_FMT_RGB565: |
1137 | g = LIMIT_RGB(g_); | |
1138 | *f_even++ = | |
1139 | (0x1F & LIMIT_RGB(r_)) | | |
1140 | (0xE0 & (g << 5)); | |
1141 | *f_even++ = | |
1142 | (0x07 & (g >> 3)) | | |
1143 | (0xF8 & LIMIT_RGB(b_)); | |
1144 | break; | |
1145 | case V4L2_PIX_FMT_RGB24: | |
1146 | *f_even++ = LIMIT_RGB(r_); | |
1147 | *f_even++ = LIMIT_RGB(g_); | |
1148 | *f_even++ = LIMIT_RGB(b_); | |
1149 | break; | |
1150 | case V4L2_PIX_FMT_RGB32: | |
1151 | *f_even++ = LIMIT_RGB(r_); | |
1152 | *f_even++ = LIMIT_RGB(g_); | |
1153 | *f_even++ = LIMIT_RGB(b_); | |
1154 | f_even++; | |
1155 | break; | |
1156 | case V4L2_PIX_FMT_RGB555: | |
1157 | g = LIMIT_RGB(g_); | |
1158 | *f_even++ = (0x1F & LIMIT_RGB(r_)) | | |
1159 | (0xE0 & (g << 5)); | |
1160 | *f_even++ = (0x03 & (g >> 3)) | | |
1161 | (0x7C & (LIMIT_RGB(b_) << 2)); | |
1162 | break; | |
781aa1d1 MCC |
1163 | } |
1164 | } | |
1165 | clipmask_even_index += clipmask_add; | |
1166 | f_even += stretch_bytes; | |
1167 | ||
1168 | scratch_get_extra(usbvision, &y[0], &y_ptr, 2); | |
1169 | ||
483dfdb6 | 1170 | if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
781aa1d1 MCC |
1171 | *f_odd++ = y[0]; |
1172 | *f_odd++ = v; | |
1173 | } | |
1174 | else { | |
1175 | y_ = 76284 * (y[0] - 16); | |
1176 | ||
1177 | b_ = (y_ + vb) >> 16; | |
1178 | g_ = (y_ + uvg)>> 16; | |
1179 | r_ = (y_ + ur) >> 16; | |
1180 | ||
1181 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
1182 | case V4L2_PIX_FMT_RGB565: |
1183 | g = LIMIT_RGB(g_); | |
1184 | *f_odd++ = | |
1185 | (0x1F & LIMIT_RGB(r_)) | | |
1186 | (0xE0 & (g << 5)); | |
1187 | *f_odd++ = | |
1188 | (0x07 & (g >> 3)) | | |
1189 | (0xF8 & LIMIT_RGB(b_)); | |
1190 | break; | |
1191 | case V4L2_PIX_FMT_RGB24: | |
1192 | *f_odd++ = LIMIT_RGB(r_); | |
1193 | *f_odd++ = LIMIT_RGB(g_); | |
1194 | *f_odd++ = LIMIT_RGB(b_); | |
1195 | break; | |
1196 | case V4L2_PIX_FMT_RGB32: | |
1197 | *f_odd++ = LIMIT_RGB(r_); | |
1198 | *f_odd++ = LIMIT_RGB(g_); | |
1199 | *f_odd++ = LIMIT_RGB(b_); | |
1200 | f_odd++; | |
1201 | break; | |
1202 | case V4L2_PIX_FMT_RGB555: | |
1203 | g = LIMIT_RGB(g_); | |
1204 | *f_odd++ = (0x1F & LIMIT_RGB(r_)) | | |
1205 | (0xE0 & (g << 5)); | |
1206 | *f_odd++ = (0x03 & (g >> 3)) | | |
1207 | (0x7C & (LIMIT_RGB(b_) << 2)); | |
1208 | break; | |
781aa1d1 MCC |
1209 | } |
1210 | } | |
1211 | clipmask_odd_index += clipmask_add; | |
1212 | f_odd += stretch_bytes; | |
1213 | ||
483dfdb6 | 1214 | if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
781aa1d1 MCC |
1215 | *f_odd++ = y[1]; |
1216 | *f_odd++ = u; | |
1217 | } | |
1218 | else { | |
1219 | y_ = 76284 * (y[1] - 16); | |
1220 | ||
1221 | b_ = (y_ + vb) >> 16; | |
1222 | g_ = (y_ + uvg)>> 16; | |
1223 | r_ = (y_ + ur) >> 16; | |
1224 | ||
1225 | switch (frame->v4l2_format.format) { | |
6200bbaa TM |
1226 | case V4L2_PIX_FMT_RGB565: |
1227 | g = LIMIT_RGB(g_); | |
1228 | *f_odd++ = | |
1229 | (0x1F & LIMIT_RGB(r_)) | | |
1230 | (0xE0 & (g << 5)); | |
1231 | *f_odd++ = | |
1232 | (0x07 & (g >> 3)) | | |
1233 | (0xF8 & LIMIT_RGB(b_)); | |
1234 | break; | |
1235 | case V4L2_PIX_FMT_RGB24: | |
1236 | *f_odd++ = LIMIT_RGB(r_); | |
1237 | *f_odd++ = LIMIT_RGB(g_); | |
1238 | *f_odd++ = LIMIT_RGB(b_); | |
1239 | break; | |
1240 | case V4L2_PIX_FMT_RGB32: | |
1241 | *f_odd++ = LIMIT_RGB(r_); | |
1242 | *f_odd++ = LIMIT_RGB(g_); | |
1243 | *f_odd++ = LIMIT_RGB(b_); | |
1244 | f_odd++; | |
1245 | break; | |
1246 | case V4L2_PIX_FMT_RGB555: | |
1247 | g = LIMIT_RGB(g_); | |
1248 | *f_odd++ = (0x1F & LIMIT_RGB(r_)) | | |
1249 | (0xE0 & (g << 5)); | |
1250 | *f_odd++ = (0x03 & (g >> 3)) | | |
1251 | (0x7C & (LIMIT_RGB(b_) << 2)); | |
1252 | break; | |
781aa1d1 MCC |
1253 | } |
1254 | } | |
1255 | clipmask_odd_index += clipmask_add; | |
1256 | f_odd += stretch_bytes; | |
1257 | } | |
1258 | ||
1259 | scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size); | |
1260 | scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size] | |
1261 | * sub_block_size); | |
1262 | scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size] | |
1263 | * sub_block_size); | |
1264 | scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size] | |
1265 | * sub_block_size); | |
1266 | } | |
1267 | ||
1268 | scratch_rm_old(usbvision, pixel_per_line * 3 / 2 | |
1269 | + block_split * sub_block_size); | |
1270 | ||
1271 | frame->curline += 2 * usbvision->stretch_height; | |
1272 | *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height; | |
1273 | ||
1274 | if (frame->curline >= frame->frmheight) | |
5490a7cb | 1275 | return parse_state_next_frame; |
781aa1d1 | 1276 | else |
5490a7cb | 1277 | return parse_state_continue; |
781aa1d1 MCC |
1278 | } |
1279 | ||
1280 | /* | |
1281 | * usbvision_parse_data() | |
1282 | * | |
1283 | * Generic routine to parse the scratch buffer. It employs either | |
1284 | * usbvision_find_header() or usbvision_parse_lines() to do most | |
1285 | * of work. | |
1286 | * | |
1287 | */ | |
1288 | static void usbvision_parse_data(struct usb_usbvision *usbvision) | |
1289 | { | |
1290 | struct usbvision_frame *frame; | |
5490a7cb | 1291 | enum parse_state newstate; |
781aa1d1 | 1292 | long copylen = 0; |
f2242ee5 | 1293 | unsigned long lock_flags; |
781aa1d1 | 1294 | |
5490a7cb | 1295 | frame = usbvision->cur_frame; |
781aa1d1 MCC |
1296 | |
1297 | PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision)); | |
1298 | ||
781aa1d1 MCC |
1299 | while (1) { |
1300 | ||
5490a7cb | 1301 | newstate = parse_state_out; |
781aa1d1 | 1302 | if (scratch_len(usbvision)) { |
5490a7cb | 1303 | if (frame->scanstate == scan_state_scanning) { |
781aa1d1 MCC |
1304 | newstate = usbvision_find_header(usbvision); |
1305 | } | |
5490a7cb HV |
1306 | else if (frame->scanstate == scan_state_lines) { |
1307 | if (usbvision->isoc_mode == ISOC_MODE_YUV420) { | |
781aa1d1 MCC |
1308 | newstate = usbvision_parse_lines_420(usbvision, ©len); |
1309 | } | |
5490a7cb | 1310 | else if (usbvision->isoc_mode == ISOC_MODE_YUV422) { |
781aa1d1 MCC |
1311 | newstate = usbvision_parse_lines_422(usbvision, ©len); |
1312 | } | |
5490a7cb | 1313 | else if (usbvision->isoc_mode == ISOC_MODE_COMPRESS) { |
781aa1d1 MCC |
1314 | newstate = usbvision_parse_compress(usbvision, ©len); |
1315 | } | |
1316 | ||
1317 | } | |
1318 | } | |
5490a7cb | 1319 | if (newstate == parse_state_continue) { |
781aa1d1 MCC |
1320 | continue; |
1321 | } | |
5490a7cb | 1322 | else if ((newstate == parse_state_next_frame) || (newstate == parse_state_out)) { |
781aa1d1 MCC |
1323 | break; |
1324 | } | |
1325 | else { | |
5490a7cb | 1326 | return; /* parse_state_end_parse */ |
781aa1d1 MCC |
1327 | } |
1328 | } | |
1329 | ||
5490a7cb HV |
1330 | if (newstate == parse_state_next_frame) { |
1331 | frame->grabstate = frame_state_done; | |
781aa1d1 MCC |
1332 | do_gettimeofday(&(frame->timestamp)); |
1333 | frame->sequence = usbvision->frame_num; | |
781aa1d1 | 1334 | |
483dfdb6 TM |
1335 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
1336 | list_move_tail(&(frame->frame), &usbvision->outqueue); | |
5490a7cb | 1337 | usbvision->cur_frame = NULL; |
483dfdb6 TM |
1338 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
1339 | ||
1340 | usbvision->frame_num++; | |
781aa1d1 MCC |
1341 | |
1342 | /* This will cause the process to request another frame. */ | |
f2242ee5 TM |
1343 | if (waitqueue_active(&usbvision->wait_frame)) { |
1344 | PDEBUG(DBG_PARSE, "Wake up !"); | |
1345 | wake_up_interruptible(&usbvision->wait_frame); | |
781aa1d1 MCC |
1346 | } |
1347 | } | |
f2242ee5 | 1348 | else |
5490a7cb | 1349 | frame->grabstate = frame_state_grabbing; |
f2242ee5 | 1350 | |
781aa1d1 MCC |
1351 | |
1352 | /* Update the frame's uncompressed length. */ | |
1353 | frame->scanlength += copylen; | |
1354 | } | |
1355 | ||
1356 | ||
1357 | /* | |
1358 | * Make all of the blocks of data contiguous | |
1359 | */ | |
1360 | static int usbvision_compress_isochronous(struct usb_usbvision *usbvision, | |
f2242ee5 | 1361 | struct urb *urb) |
781aa1d1 MCC |
1362 | { |
1363 | unsigned char *packet_data; | |
1364 | int i, totlen = 0; | |
1365 | ||
1366 | for (i = 0; i < urb->number_of_packets; i++) { | |
1367 | int packet_len = urb->iso_frame_desc[i].actual_length; | |
1368 | int packet_stat = urb->iso_frame_desc[i].status; | |
1369 | ||
1370 | packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
1371 | ||
1372 | /* Detect and ignore errored packets */ | |
52cb0bf2 | 1373 | if (packet_stat) { /* packet_stat != 0 ????????????? */ |
781aa1d1 | 1374 | PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat); |
5490a7cb | 1375 | usbvision->isoc_err_count++; |
781aa1d1 MCC |
1376 | continue; |
1377 | } | |
1378 | ||
1379 | /* Detect and ignore empty packets */ | |
1380 | if (packet_len < 0) { | |
1381 | PDEBUG(DBG_ISOC, "error packet [%d]", i); | |
5490a7cb | 1382 | usbvision->isoc_skip_count++; |
781aa1d1 MCC |
1383 | continue; |
1384 | } | |
1385 | else if (packet_len == 0) { /* Frame end ????? */ | |
1386 | PDEBUG(DBG_ISOC, "null packet [%d]", i); | |
5490a7cb HV |
1387 | usbvision->isocstate=isoc_state_no_frame; |
1388 | usbvision->isoc_skip_count++; | |
781aa1d1 MCC |
1389 | continue; |
1390 | } | |
5490a7cb HV |
1391 | else if (packet_len > usbvision->isoc_packet_size) { |
1392 | PDEBUG(DBG_ISOC, "packet[%d] > isoc_packet_size", i); | |
1393 | usbvision->isoc_skip_count++; | |
781aa1d1 MCC |
1394 | continue; |
1395 | } | |
1396 | ||
1397 | PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len); | |
1398 | ||
52cb0bf2 | 1399 | if (usbvision->isocstate==isoc_state_no_frame) { /* new frame begins */ |
5490a7cb | 1400 | usbvision->isocstate=isoc_state_in_frame; |
781aa1d1 MCC |
1401 | scratch_mark_header(usbvision); |
1402 | usbvision_measure_bandwidth(usbvision); | |
1403 | PDEBUG(DBG_ISOC, "packet with header"); | |
1404 | } | |
1405 | ||
1406 | /* | |
1407 | * If usbvision continues to feed us with data but there is no | |
1408 | * consumption (if, for example, V4L client fell asleep) we | |
1409 | * may overflow the buffer. We have to move old data over to | |
1410 | * free room for new data. This is bad for old data. If we | |
1411 | * just drop new data then it's bad for new data... choose | |
1412 | * your favorite evil here. | |
1413 | */ | |
1414 | if (scratch_free(usbvision) < packet_len) { | |
1415 | ||
1416 | usbvision->scratch_ovf_count++; | |
1417 | PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d", | |
1418 | scratch_len(usbvision), packet_len); | |
1419 | scratch_rm_old(usbvision, packet_len - scratch_free(usbvision)); | |
1420 | } | |
1421 | ||
1422 | /* Now we know that there is enough room in scratch buffer */ | |
1423 | scratch_put(usbvision, packet_data, packet_len); | |
1424 | totlen += packet_len; | |
5490a7cb HV |
1425 | usbvision->isoc_data_count += packet_len; |
1426 | usbvision->isoc_packet_count++; | |
781aa1d1 MCC |
1427 | } |
1428 | #if ENABLE_HEXDUMP | |
1429 | if (totlen > 0) { | |
ff699e6b | 1430 | static int foo; |
781aa1d1 MCC |
1431 | if (foo < 1) { |
1432 | printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen); | |
1433 | usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen); | |
1434 | ++foo; | |
1435 | } | |
1436 | } | |
1437 | #endif | |
1438 | return totlen; | |
1439 | } | |
1440 | ||
5490a7cb | 1441 | static void usbvision_isoc_irq(struct urb *urb) |
781aa1d1 | 1442 | { |
5490a7cb | 1443 | int err_code = 0; |
f2242ee5 TM |
1444 | int len; |
1445 | struct usb_usbvision *usbvision = urb->context; | |
1446 | int i; | |
5490a7cb | 1447 | unsigned long start_time = jiffies; |
f2242ee5 | 1448 | struct usbvision_frame **f; |
781aa1d1 | 1449 | |
f2242ee5 TM |
1450 | /* We don't want to do anything if we are about to be removed! */ |
1451 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
1452 | return; | |
781aa1d1 | 1453 | |
fe818d1d TM |
1454 | /* any urb with wrong status is ignored without acknowledgement */ |
1455 | if (urb->status == -ENOENT) { | |
1456 | return; | |
1457 | } | |
1458 | ||
5490a7cb | 1459 | f = &usbvision->cur_frame; |
781aa1d1 | 1460 | |
f2242ee5 | 1461 | /* Manage streaming interruption */ |
5490a7cb HV |
1462 | if (usbvision->streaming == stream_interrupt) { |
1463 | usbvision->streaming = stream_idle; | |
f2242ee5 | 1464 | if ((*f)) { |
5490a7cb HV |
1465 | (*f)->grabstate = frame_state_ready; |
1466 | (*f)->scanstate = scan_state_scanning; | |
f2242ee5 TM |
1467 | } |
1468 | PDEBUG(DBG_IRQ, "stream interrupted"); | |
1469 | wake_up_interruptible(&usbvision->wait_stream); | |
1470 | } | |
781aa1d1 | 1471 | |
f2242ee5 TM |
1472 | /* Copy the data received into our scratch buffer */ |
1473 | len = usbvision_compress_isochronous(usbvision, urb); | |
781aa1d1 | 1474 | |
5490a7cb | 1475 | usbvision->isoc_urb_count++; |
f2242ee5 | 1476 | usbvision->urb_length = len; |
781aa1d1 | 1477 | |
5490a7cb | 1478 | if (usbvision->streaming == stream_on) { |
f2242ee5 TM |
1479 | |
1480 | /* If we collected enough data let's parse! */ | |
fe818d1d TM |
1481 | if ((scratch_len(usbvision) > USBVISION_HEADER_LENGTH) && |
1482 | (!list_empty(&(usbvision->inqueue))) ) { | |
1483 | if (!(*f)) { | |
1484 | (*f) = list_entry(usbvision->inqueue.next, | |
1485 | struct usbvision_frame, | |
1486 | frame); | |
f2242ee5 | 1487 | } |
fe818d1d TM |
1488 | usbvision_parse_data(usbvision); |
1489 | } | |
1490 | else { | |
52cb0bf2 | 1491 | /* If we don't have a frame |
fe818d1d TM |
1492 | we're current working on, complain */ |
1493 | PDEBUG(DBG_IRQ, | |
1494 | "received data, but no one needs it"); | |
1495 | scratch_reset(usbvision); | |
781aa1d1 MCC |
1496 | } |
1497 | } | |
40bad678 TM |
1498 | else { |
1499 | PDEBUG(DBG_IRQ, "received data, but no one needs it"); | |
1500 | scratch_reset(usbvision); | |
1501 | } | |
f2242ee5 | 1502 | |
5490a7cb | 1503 | usbvision->time_in_irq += jiffies - start_time; |
f2242ee5 TM |
1504 | |
1505 | for (i = 0; i < USBVISION_URB_FRAMES; i++) { | |
1506 | urb->iso_frame_desc[i].status = 0; | |
1507 | urb->iso_frame_desc[i].actual_length = 0; | |
1508 | } | |
1509 | ||
1510 | urb->status = 0; | |
1511 | urb->dev = usbvision->dev; | |
5490a7cb | 1512 | err_code = usb_submit_urb (urb, GFP_ATOMIC); |
f2242ee5 | 1513 | |
5490a7cb | 1514 | if(err_code) { |
be9ed511 MCC |
1515 | dev_err(&usbvision->dev->dev, |
1516 | "%s: usb_submit_urb failed: error %d\n", | |
5490a7cb | 1517 | __func__, err_code); |
fe818d1d | 1518 | } |
f2242ee5 | 1519 | |
781aa1d1 MCC |
1520 | return; |
1521 | } | |
1522 | ||
1523 | /*************************************/ | |
1524 | /* Low level usbvision access functions */ | |
1525 | /*************************************/ | |
1526 | ||
1527 | /* | |
1528 | * usbvision_read_reg() | |
1529 | * | |
1530 | * return < 0 -> Error | |
1531 | * >= 0 -> Data | |
1532 | */ | |
1533 | ||
483dfdb6 | 1534 | int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg) |
781aa1d1 | 1535 | { |
5490a7cb | 1536 | int err_code = 0; |
781aa1d1 MCC |
1537 | unsigned char buffer[1]; |
1538 | ||
1539 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
1540 | return -1; | |
1541 | ||
5490a7cb | 1542 | err_code = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1), |
781aa1d1 MCC |
1543 | USBVISION_OP_CODE, |
1544 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, | |
1545 | 0, (__u16) reg, buffer, 1, HZ); | |
1546 | ||
5490a7cb | 1547 | if (err_code < 0) { |
be9ed511 | 1548 | dev_err(&usbvision->dev->dev, |
5490a7cb HV |
1549 | "%s: failed: error %d\n", __func__, err_code); |
1550 | return err_code; | |
781aa1d1 MCC |
1551 | } |
1552 | return buffer[0]; | |
1553 | } | |
1554 | ||
1555 | /* | |
1556 | * usbvision_write_reg() | |
1557 | * | |
1558 | * return 1 -> Reg written | |
1559 | * 0 -> usbvision is not yet ready | |
1560 | * -1 -> Something went wrong | |
1561 | */ | |
1562 | ||
483dfdb6 | 1563 | int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg, |
781aa1d1 MCC |
1564 | unsigned char value) |
1565 | { | |
5490a7cb | 1566 | int err_code = 0; |
781aa1d1 MCC |
1567 | |
1568 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
1569 | return 0; | |
1570 | ||
5490a7cb | 1571 | err_code = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
781aa1d1 MCC |
1572 | USBVISION_OP_CODE, |
1573 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
1574 | USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ); | |
1575 | ||
5490a7cb | 1576 | if (err_code < 0) { |
be9ed511 | 1577 | dev_err(&usbvision->dev->dev, |
5490a7cb | 1578 | "%s: failed: error %d\n", __func__, err_code); |
781aa1d1 | 1579 | } |
5490a7cb | 1580 | return err_code; |
781aa1d1 MCC |
1581 | } |
1582 | ||
1583 | ||
5490a7cb | 1584 | static void usbvision_ctrl_urb_complete(struct urb *urb) |
781aa1d1 MCC |
1585 | { |
1586 | struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context; | |
1587 | ||
1588 | PDEBUG(DBG_IRQ, ""); | |
5490a7cb HV |
1589 | usbvision->ctrl_urb_busy = 0; |
1590 | if (waitqueue_active(&usbvision->ctrl_urb_wq)) { | |
1591 | wake_up_interruptible(&usbvision->ctrl_urb_wq); | |
781aa1d1 MCC |
1592 | } |
1593 | } | |
1594 | ||
1595 | ||
1596 | static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address, | |
1597 | unsigned char *data, int len) | |
1598 | { | |
5490a7cb | 1599 | int err_code = 0; |
781aa1d1 MCC |
1600 | |
1601 | PDEBUG(DBG_IRQ, ""); | |
1602 | if (len > 8) { | |
1603 | return -EFAULT; | |
1604 | } | |
5490a7cb | 1605 | if (usbvision->ctrl_urb_busy) { |
781aa1d1 MCC |
1606 | return -EBUSY; |
1607 | } | |
5490a7cb | 1608 | usbvision->ctrl_urb_busy = 1; |
781aa1d1 | 1609 | |
5490a7cb HV |
1610 | usbvision->ctrl_urb_setup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT; |
1611 | usbvision->ctrl_urb_setup.bRequest = USBVISION_OP_CODE; | |
1612 | usbvision->ctrl_urb_setup.wValue = 0; | |
1613 | usbvision->ctrl_urb_setup.wIndex = cpu_to_le16(address); | |
1614 | usbvision->ctrl_urb_setup.wLength = cpu_to_le16(len); | |
1615 | usb_fill_control_urb (usbvision->ctrl_urb, usbvision->dev, | |
781aa1d1 | 1616 | usb_sndctrlpipe(usbvision->dev, 1), |
5490a7cb HV |
1617 | (unsigned char *)&usbvision->ctrl_urb_setup, |
1618 | (void *)usbvision->ctrl_urb_buffer, len, | |
1619 | usbvision_ctrl_urb_complete, | |
781aa1d1 MCC |
1620 | (void *)usbvision); |
1621 | ||
5490a7cb | 1622 | memcpy(usbvision->ctrl_urb_buffer, data, len); |
781aa1d1 | 1623 | |
5490a7cb HV |
1624 | err_code = usb_submit_urb(usbvision->ctrl_urb, GFP_ATOMIC); |
1625 | if (err_code < 0) { | |
52cb0bf2 | 1626 | /* error in usb_submit_urb() */ |
5490a7cb | 1627 | usbvision->ctrl_urb_busy = 0; |
781aa1d1 | 1628 | } |
5490a7cb HV |
1629 | PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, err_code); |
1630 | return err_code; | |
781aa1d1 MCC |
1631 | } |
1632 | ||
1633 | ||
781aa1d1 MCC |
1634 | static int usbvision_init_compression(struct usb_usbvision *usbvision) |
1635 | { | |
5490a7cb HV |
1636 | int err_code = 0; |
1637 | ||
1638 | usbvision->last_isoc_frame_num = -1; | |
1639 | usbvision->isoc_data_count = 0; | |
1640 | usbvision->isoc_packet_count = 0; | |
1641 | usbvision->isoc_skip_count = 0; | |
1642 | usbvision->compr_level = 50; | |
1643 | usbvision->last_compr_level = -1; | |
1644 | usbvision->isoc_urb_count = 0; | |
1645 | usbvision->request_intra = 1; | |
1646 | usbvision->isoc_measure_bandwidth_count = 0; | |
1647 | ||
1648 | return err_code; | |
781aa1d1 MCC |
1649 | } |
1650 | ||
1651 | /* this function measures the used bandwidth since last call | |
1652 | * return: 0 : no error | |
5490a7cb | 1653 | * sets used_bandwidth to 1-100 : 1-100% of full bandwidth resp. to isoc_packet_size |
781aa1d1 MCC |
1654 | */ |
1655 | static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision) | |
1656 | { | |
5490a7cb HV |
1657 | int err_code = 0; |
1658 | ||
52cb0bf2 | 1659 | if (usbvision->isoc_measure_bandwidth_count < 2) { /* this gives an average bandwidth of 3 frames */ |
5490a7cb HV |
1660 | usbvision->isoc_measure_bandwidth_count++; |
1661 | return err_code; | |
1662 | } | |
1663 | if ((usbvision->isoc_packet_size > 0) && (usbvision->isoc_packet_count > 0)) { | |
1664 | usbvision->used_bandwidth = usbvision->isoc_data_count / | |
1665 | (usbvision->isoc_packet_count + usbvision->isoc_skip_count) * | |
1666 | 100 / usbvision->isoc_packet_size; | |
1667 | } | |
1668 | usbvision->isoc_measure_bandwidth_count = 0; | |
1669 | usbvision->isoc_data_count = 0; | |
1670 | usbvision->isoc_packet_count = 0; | |
1671 | usbvision->isoc_skip_count = 0; | |
1672 | return err_code; | |
781aa1d1 MCC |
1673 | } |
1674 | ||
1675 | static int usbvision_adjust_compression (struct usb_usbvision *usbvision) | |
1676 | { | |
5490a7cb | 1677 | int err_code = 0; |
781aa1d1 MCC |
1678 | unsigned char buffer[6]; |
1679 | ||
1680 | PDEBUG(DBG_IRQ, ""); | |
5490a7cb HV |
1681 | if ((adjust_compression) && (usbvision->used_bandwidth > 0)) { |
1682 | usbvision->compr_level += (usbvision->used_bandwidth - 90) / 2; | |
1683 | RESTRICT_TO_RANGE(usbvision->compr_level, 0, 100); | |
1684 | if (usbvision->compr_level != usbvision->last_compr_level) { | |
781aa1d1 | 1685 | int distorsion; |
5490a7cb | 1686 | if (usbvision->bridge_type == BRIDGE_NT1004 || usbvision->bridge_type == BRIDGE_NT1005) { |
52cb0bf2 HV |
1687 | buffer[0] = (unsigned char)(4 + 16 * usbvision->compr_level / 100); /* PCM Threshold 1 */ |
1688 | buffer[1] = (unsigned char)(4 + 8 * usbvision->compr_level / 100); /* PCM Threshold 2 */ | |
5490a7cb | 1689 | distorsion = 7 + 248 * usbvision->compr_level / 100; |
52cb0bf2 HV |
1690 | buffer[2] = (unsigned char)(distorsion & 0xFF); /* Average distorsion Threshold (inter) */ |
1691 | buffer[3] = (unsigned char)(distorsion & 0xFF); /* Average distorsion Threshold (intra) */ | |
5490a7cb | 1692 | distorsion = 1 + 42 * usbvision->compr_level / 100; |
52cb0bf2 HV |
1693 | buffer[4] = (unsigned char)(distorsion & 0xFF); /* Maximum distorsion Threshold (inter) */ |
1694 | buffer[5] = (unsigned char)(distorsion & 0xFF); /* Maximum distorsion Threshold (intra) */ | |
781aa1d1 | 1695 | } |
52cb0bf2 HV |
1696 | else { /* BRIDGE_NT1003 */ |
1697 | buffer[0] = (unsigned char)(4 + 16 * usbvision->compr_level / 100); /* PCM threshold 1 */ | |
1698 | buffer[1] = (unsigned char)(4 + 8 * usbvision->compr_level / 100); /* PCM threshold 2 */ | |
5490a7cb | 1699 | distorsion = 2 + 253 * usbvision->compr_level / 100; |
52cb0bf2 HV |
1700 | buffer[2] = (unsigned char)(distorsion & 0xFF); /* distorsion threshold bit0-7 */ |
1701 | buffer[3] = 0; /* (unsigned char)((distorsion >> 8) & 0x0F); distorsion threshold bit 8-11 */ | |
5490a7cb | 1702 | distorsion = 0 + 43 * usbvision->compr_level / 100; |
52cb0bf2 HV |
1703 | buffer[4] = (unsigned char)(distorsion & 0xFF); /* maximum distorsion bit0-7 */ |
1704 | buffer[5] = 0; /* (unsigned char)((distorsion >> 8) & 0x01); maximum distorsion bit 8 */ | |
781aa1d1 | 1705 | } |
5490a7cb HV |
1706 | err_code = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6); |
1707 | if (err_code == 0){ | |
781aa1d1 MCC |
1708 | PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0], |
1709 | buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]); | |
5490a7cb | 1710 | usbvision->last_compr_level = usbvision->compr_level; |
781aa1d1 MCC |
1711 | } |
1712 | } | |
1713 | } | |
5490a7cb | 1714 | return err_code; |
781aa1d1 MCC |
1715 | } |
1716 | ||
1717 | static int usbvision_request_intra (struct usb_usbvision *usbvision) | |
1718 | { | |
5490a7cb | 1719 | int err_code = 0; |
781aa1d1 MCC |
1720 | unsigned char buffer[1]; |
1721 | ||
1722 | PDEBUG(DBG_IRQ, ""); | |
5490a7cb | 1723 | usbvision->request_intra = 1; |
781aa1d1 MCC |
1724 | buffer[0] = 1; |
1725 | usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1); | |
5490a7cb | 1726 | return err_code; |
781aa1d1 MCC |
1727 | } |
1728 | ||
1729 | static int usbvision_unrequest_intra (struct usb_usbvision *usbvision) | |
1730 | { | |
5490a7cb | 1731 | int err_code = 0; |
781aa1d1 MCC |
1732 | unsigned char buffer[1]; |
1733 | ||
1734 | PDEBUG(DBG_IRQ, ""); | |
5490a7cb | 1735 | usbvision->request_intra = 0; |
781aa1d1 MCC |
1736 | buffer[0] = 0; |
1737 | usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1); | |
5490a7cb | 1738 | return err_code; |
781aa1d1 MCC |
1739 | } |
1740 | ||
483dfdb6 TM |
1741 | /******************************* |
1742 | * usbvision utility functions | |
1743 | *******************************/ | |
781aa1d1 | 1744 | |
483dfdb6 | 1745 | int usbvision_power_off(struct usb_usbvision *usbvision) |
781aa1d1 | 1746 | { |
5490a7cb | 1747 | int err_code = 0; |
781aa1d1 MCC |
1748 | |
1749 | PDEBUG(DBG_FUNC, ""); | |
1750 | ||
5490a7cb HV |
1751 | err_code = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN); |
1752 | if (err_code == 1) { | |
781aa1d1 MCC |
1753 | usbvision->power = 0; |
1754 | } | |
5490a7cb HV |
1755 | PDEBUG(DBG_FUNC, "%s: err_code %d", (err_code!=1)?"ERROR":"power is off", err_code); |
1756 | return err_code; | |
781aa1d1 MCC |
1757 | } |
1758 | ||
781aa1d1 MCC |
1759 | /* |
1760 | * usbvision_set_video_format() | |
1761 | * | |
1762 | */ | |
1763 | static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format) | |
1764 | { | |
1765 | static const char proc[] = "usbvision_set_video_format"; | |
1766 | int rc; | |
1767 | unsigned char value[2]; | |
1768 | ||
1769 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
1770 | return 0; | |
1771 | ||
5490a7cb | 1772 | PDEBUG(DBG_FUNC, "isoc_mode %#02x", format); |
781aa1d1 MCC |
1773 | |
1774 | if ((format != ISOC_MODE_YUV422) | |
1775 | && (format != ISOC_MODE_YUV420) | |
1776 | && (format != ISOC_MODE_COMPRESS)) { | |
1777 | printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420", | |
1778 | format); | |
1779 | format = ISOC_MODE_YUV420; | |
1780 | } | |
52cb0bf2 HV |
1781 | value[0] = 0x0A; /* TODO: See the effect of the filter */ |
1782 | value[1] = format; /* Sets the VO_MODE register which follows FILT_CONT */ | |
781aa1d1 MCC |
1783 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
1784 | USBVISION_OP_CODE, | |
1785 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
1786 | USB_RECIP_ENDPOINT, 0, | |
1787 | (__u16) USBVISION_FILT_CONT, value, 2, HZ); | |
1788 | ||
1789 | if (rc < 0) { | |
1790 | printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - " | |
1791 | "reconnect or reload driver.\n", proc, rc); | |
1792 | } | |
5490a7cb | 1793 | usbvision->isoc_mode = format; |
781aa1d1 MCC |
1794 | return rc; |
1795 | } | |
1796 | ||
1797 | /* | |
1798 | * usbvision_set_output() | |
1799 | * | |
1800 | */ | |
1801 | ||
483dfdb6 TM |
1802 | int usbvision_set_output(struct usb_usbvision *usbvision, int width, |
1803 | int height) | |
781aa1d1 | 1804 | { |
5490a7cb HV |
1805 | int err_code = 0; |
1806 | int usb_width, usb_height; | |
1807 | unsigned int frame_rate=0, frame_drop=0; | |
781aa1d1 MCC |
1808 | unsigned char value[4]; |
1809 | ||
1810 | if (!USBVISION_IS_OPERATIONAL(usbvision)) { | |
1811 | return 0; | |
1812 | } | |
1813 | ||
1814 | if (width > MAX_USB_WIDTH) { | |
5490a7cb | 1815 | usb_width = width / 2; |
781aa1d1 MCC |
1816 | usbvision->stretch_width = 2; |
1817 | } | |
1818 | else { | |
5490a7cb | 1819 | usb_width = width; |
781aa1d1 MCC |
1820 | usbvision->stretch_width = 1; |
1821 | } | |
1822 | ||
1823 | if (height > MAX_USB_HEIGHT) { | |
5490a7cb | 1824 | usb_height = height / 2; |
781aa1d1 MCC |
1825 | usbvision->stretch_height = 2; |
1826 | } | |
1827 | else { | |
5490a7cb | 1828 | usb_height = height; |
781aa1d1 MCC |
1829 | usbvision->stretch_height = 1; |
1830 | } | |
1831 | ||
5490a7cb HV |
1832 | RESTRICT_TO_RANGE(usb_width, MIN_FRAME_WIDTH, MAX_USB_WIDTH); |
1833 | usb_width &= ~(MIN_FRAME_WIDTH-1); | |
1834 | RESTRICT_TO_RANGE(usb_height, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT); | |
1835 | usb_height &= ~(1); | |
781aa1d1 MCC |
1836 | |
1837 | PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d", | |
5490a7cb | 1838 | usb_width, usb_height, width, height, |
781aa1d1 MCC |
1839 | usbvision->stretch_width, usbvision->stretch_height); |
1840 | ||
1841 | /* I'll not rewrite the same values */ | |
5490a7cb | 1842 | if ((usb_width != usbvision->curwidth) || (usb_height != usbvision->curheight)) { |
52cb0bf2 HV |
1843 | value[0] = usb_width & 0xff; /* LSB */ |
1844 | value[1] = (usb_width >> 8) & 0x03; /* MSB */ | |
1845 | value[2] = usb_height & 0xff; /* LSB */ | |
1846 | value[3] = (usb_height >> 8) & 0x03; /* MSB */ | |
781aa1d1 | 1847 | |
5490a7cb | 1848 | err_code = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
781aa1d1 MCC |
1849 | USBVISION_OP_CODE, |
1850 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, | |
1851 | 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ); | |
1852 | ||
5490a7cb | 1853 | if (err_code < 0) { |
be9ed511 | 1854 | dev_err(&usbvision->dev->dev, |
5490a7cb HV |
1855 | "%s failed: error %d\n", __func__, err_code); |
1856 | return err_code; | |
781aa1d1 | 1857 | } |
5490a7cb HV |
1858 | usbvision->curwidth = usbvision->stretch_width * usb_width; |
1859 | usbvision->curheight = usbvision->stretch_height * usb_height; | |
781aa1d1 MCC |
1860 | } |
1861 | ||
5490a7cb HV |
1862 | if (usbvision->isoc_mode == ISOC_MODE_YUV422) { |
1863 | frame_rate = (usbvision->isoc_packet_size * 1000) / (usb_width * usb_height * 2); | |
781aa1d1 | 1864 | } |
5490a7cb HV |
1865 | else if (usbvision->isoc_mode == ISOC_MODE_YUV420) { |
1866 | frame_rate = (usbvision->isoc_packet_size * 1000) / ((usb_width * usb_height * 12) / 8); | |
781aa1d1 MCC |
1867 | } |
1868 | else { | |
5490a7cb | 1869 | frame_rate = FRAMERATE_MAX; |
781aa1d1 MCC |
1870 | } |
1871 | ||
5490a7cb HV |
1872 | if (usbvision->tvnorm_id & V4L2_STD_625_50) { |
1873 | frame_drop = frame_rate * 32 / 25 - 1; | |
781aa1d1 | 1874 | } |
5490a7cb HV |
1875 | else if (usbvision->tvnorm_id & V4L2_STD_525_60) { |
1876 | frame_drop = frame_rate * 32 / 30 - 1; | |
781aa1d1 MCC |
1877 | } |
1878 | ||
5490a7cb | 1879 | RESTRICT_TO_RANGE(frame_drop, FRAMERATE_MIN, FRAMERATE_MAX); |
781aa1d1 | 1880 | |
5490a7cb | 1881 | PDEBUG(DBG_FUNC, "frame_rate %d fps, frame_drop %d", frame_rate, frame_drop); |
781aa1d1 | 1882 | |
52cb0bf2 | 1883 | frame_drop = FRAMERATE_MAX; /* We can allow the maximum here, because dropping is controlled */ |
781aa1d1 | 1884 | |
5490a7cb HV |
1885 | /* frame_drop = 7; => frame_phase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ... |
1886 | => frame_skip = 4; | |
1887 | => frame_rate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25; | |
781aa1d1 | 1888 | |
5490a7cb HV |
1889 | frame_drop = 9; => frame_phase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ... |
1890 | => frame_skip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ... | |
1891 | => frame_rate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125; | |
781aa1d1 | 1892 | */ |
5490a7cb HV |
1893 | err_code = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frame_drop); |
1894 | return err_code; | |
781aa1d1 MCC |
1895 | } |
1896 | ||
1897 | ||
483dfdb6 TM |
1898 | /* |
1899 | * usbvision_frames_alloc | |
6f78e186 | 1900 | * allocate the required frames |
483dfdb6 | 1901 | */ |
6f78e186 | 1902 | int usbvision_frames_alloc(struct usb_usbvision *usbvision, int number_of_frames) |
483dfdb6 TM |
1903 | { |
1904 | int i; | |
1905 | ||
52cb0bf2 | 1906 | /* needs to be page aligned cause the buffers can be mapped individually! */ |
6f78e186 TM |
1907 | usbvision->max_frame_size = PAGE_ALIGN(usbvision->curwidth * |
1908 | usbvision->curheight * | |
1909 | usbvision->palette.bytes_per_pixel); | |
483dfdb6 | 1910 | |
6f78e186 TM |
1911 | /* Try to do my best to allocate the frames the user want in the remaining memory */ |
1912 | usbvision->num_frames = number_of_frames; | |
1913 | while (usbvision->num_frames > 0) { | |
1914 | usbvision->fbuf_size = usbvision->num_frames * usbvision->max_frame_size; | |
1915 | if((usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size))) { | |
1916 | break; | |
1917 | } | |
1918 | usbvision->num_frames--; | |
483dfdb6 | 1919 | } |
6f78e186 | 1920 | |
483dfdb6 TM |
1921 | spin_lock_init(&usbvision->queue_lock); |
1922 | init_waitqueue_head(&usbvision->wait_frame); | |
1923 | init_waitqueue_head(&usbvision->wait_stream); | |
1924 | ||
1925 | /* Allocate all buffers */ | |
6f78e186 | 1926 | for (i = 0; i < usbvision->num_frames; i++) { |
483dfdb6 | 1927 | usbvision->frame[i].index = i; |
5490a7cb | 1928 | usbvision->frame[i].grabstate = frame_state_unused; |
483dfdb6 TM |
1929 | usbvision->frame[i].data = usbvision->fbuf + |
1930 | i * usbvision->max_frame_size; | |
1931 | /* | |
1932 | * Set default sizes for read operation. | |
1933 | */ | |
1934 | usbvision->stretch_width = 1; | |
1935 | usbvision->stretch_height = 1; | |
1936 | usbvision->frame[i].width = usbvision->curwidth; | |
1937 | usbvision->frame[i].height = usbvision->curheight; | |
1938 | usbvision->frame[i].bytes_read = 0; | |
1939 | } | |
6f78e186 TM |
1940 | PDEBUG(DBG_FUNC, "allocated %d frames (%d bytes per frame)",usbvision->num_frames,usbvision->max_frame_size); |
1941 | return usbvision->num_frames; | |
483dfdb6 TM |
1942 | } |
1943 | ||
1944 | /* | |
1945 | * usbvision_frames_free | |
1946 | * frees memory allocated for the frames | |
1947 | */ | |
1948 | void usbvision_frames_free(struct usb_usbvision *usbvision) | |
1949 | { | |
1950 | /* Have to free all that memory */ | |
6f78e186 TM |
1951 | PDEBUG(DBG_FUNC, "free %d frames",usbvision->num_frames); |
1952 | ||
483dfdb6 TM |
1953 | if (usbvision->fbuf != NULL) { |
1954 | usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size); | |
1955 | usbvision->fbuf = NULL; | |
6f78e186 TM |
1956 | |
1957 | usbvision->num_frames = 0; | |
483dfdb6 TM |
1958 | } |
1959 | } | |
f2242ee5 TM |
1960 | /* |
1961 | * usbvision_empty_framequeues() | |
1962 | * prepare queues for incoming and outgoing frames | |
1963 | */ | |
483dfdb6 | 1964 | void usbvision_empty_framequeues(struct usb_usbvision *usbvision) |
f2242ee5 TM |
1965 | { |
1966 | u32 i; | |
1967 | ||
1968 | INIT_LIST_HEAD(&(usbvision->inqueue)); | |
1969 | INIT_LIST_HEAD(&(usbvision->outqueue)); | |
1970 | ||
1971 | for (i = 0; i < USBVISION_NUMFRAMES; i++) { | |
5490a7cb | 1972 | usbvision->frame[i].grabstate = frame_state_unused; |
f2242ee5 TM |
1973 | usbvision->frame[i].bytes_read = 0; |
1974 | } | |
1975 | } | |
1976 | ||
1977 | /* | |
1978 | * usbvision_stream_interrupt() | |
1979 | * stops streaming | |
1980 | */ | |
483dfdb6 | 1981 | int usbvision_stream_interrupt(struct usb_usbvision *usbvision) |
f2242ee5 TM |
1982 | { |
1983 | int ret = 0; | |
1984 | ||
1985 | /* stop reading from the device */ | |
1986 | ||
5490a7cb | 1987 | usbvision->streaming = stream_interrupt; |
f2242ee5 | 1988 | ret = wait_event_timeout(usbvision->wait_stream, |
5490a7cb | 1989 | (usbvision->streaming == stream_idle), |
f2242ee5 TM |
1990 | msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES)); |
1991 | return ret; | |
1992 | } | |
1993 | ||
781aa1d1 MCC |
1994 | /* |
1995 | * usbvision_set_compress_params() | |
1996 | * | |
1997 | */ | |
1998 | ||
1999 | static int usbvision_set_compress_params(struct usb_usbvision *usbvision) | |
2000 | { | |
2001 | static const char proc[] = "usbvision_set_compresion_params: "; | |
2002 | int rc; | |
2003 | unsigned char value[6]; | |
2004 | ||
52cb0bf2 HV |
2005 | value[0] = 0x0F; /* Intra-Compression cycle */ |
2006 | value[1] = 0x01; /* Reg.45 one line per strip */ | |
2007 | value[2] = 0x00; /* Reg.46 Force intra mode on all new frames */ | |
2008 | value[3] = 0x00; /* Reg.47 FORCE_UP <- 0 normal operation (not force) */ | |
2009 | value[4] = 0xA2; /* Reg.48 BUF_THR I'm not sure if this does something in not compressed mode. */ | |
2010 | value[5] = 0x00; /* Reg.49 DVI_YUV This has nothing to do with compression */ | |
2011 | ||
2012 | /* catched values for NT1004 */ | |
2013 | /* value[0] = 0xFF; Never apply intra mode automatically */ | |
2014 | /* value[1] = 0xF1; Use full frame height for virtual strip width; One line per strip */ | |
2015 | /* value[2] = 0x01; Force intra mode on all new frames */ | |
2016 | /* value[3] = 0x00; Strip size 400 Bytes; do not force up */ | |
2017 | /* value[4] = 0xA2; */ | |
781aa1d1 MCC |
2018 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
2019 | return 0; | |
2020 | ||
2021 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), | |
2022 | USBVISION_OP_CODE, | |
2023 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
2024 | USB_RECIP_ENDPOINT, 0, | |
2025 | (__u16) USBVISION_INTRA_CYC, value, 5, HZ); | |
2026 | ||
2027 | if (rc < 0) { | |
2028 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " | |
2029 | "reconnect or reload driver.\n", proc, rc); | |
2030 | return rc; | |
2031 | } | |
2032 | ||
5490a7cb | 2033 | if (usbvision->bridge_type == BRIDGE_NT1004) { |
52cb0bf2 HV |
2034 | value[0] = 20; /* PCM Threshold 1 */ |
2035 | value[1] = 12; /* PCM Threshold 2 */ | |
2036 | value[2] = 255; /* Distorsion Threshold inter */ | |
2037 | value[3] = 255; /* Distorsion Threshold intra */ | |
2038 | value[4] = 43; /* Max Distorsion inter */ | |
2039 | value[5] = 43; /* Max Distorsion intra */ | |
781aa1d1 MCC |
2040 | } |
2041 | else { | |
52cb0bf2 HV |
2042 | value[0] = 20; /* PCM Threshold 1 */ |
2043 | value[1] = 12; /* PCM Threshold 2 */ | |
2044 | value[2] = 255; /* Distorsion Threshold d7-d0 */ | |
2045 | value[3] = 0; /* Distorsion Threshold d11-d8 */ | |
2046 | value[4] = 43; /* Max Distorsion d7-d0 */ | |
2047 | value[5] = 0; /* Max Distorsion d8 */ | |
781aa1d1 MCC |
2048 | } |
2049 | ||
2050 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
2051 | return 0; | |
2052 | ||
2053 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), | |
2054 | USBVISION_OP_CODE, | |
2055 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
2056 | USB_RECIP_ENDPOINT, 0, | |
2057 | (__u16) USBVISION_PCM_THR1, value, 6, HZ); | |
2058 | ||
2059 | if (rc < 0) { | |
2060 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " | |
2061 | "reconnect or reload driver.\n", proc, rc); | |
2062 | return rc; | |
2063 | } | |
2064 | ||
2065 | ||
2066 | return rc; | |
2067 | } | |
2068 | ||
2069 | ||
2070 | /* | |
2071 | * usbvision_set_input() | |
2072 | * | |
2073 | * Set the input (saa711x, ...) size x y and other misc input params | |
2074 | * I've no idea if this parameters are right | |
2075 | * | |
2076 | */ | |
483dfdb6 | 2077 | int usbvision_set_input(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
2078 | { |
2079 | static const char proc[] = "usbvision_set_input: "; | |
2080 | int rc; | |
2081 | unsigned char value[8]; | |
2082 | unsigned char dvi_yuv_value; | |
2083 | ||
2084 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
2085 | return 0; | |
2086 | ||
2087 | /* Set input format expected from decoder*/ | |
5490a7cb HV |
2088 | if (usbvision_device_data[usbvision->dev_model].vin_reg1_override) { |
2089 | value[0] = usbvision_device_data[usbvision->dev_model].vin_reg1; | |
2090 | } else if(usbvision_device_data[usbvision->dev_model].codec == CODEC_SAA7113) { | |
781aa1d1 MCC |
2091 | /* SAA7113 uses 8 bit output */ |
2092 | value[0] = USBVISION_8_422_SYNC; | |
2093 | } else { | |
2094 | /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses | |
2095 | * as that is how saa7111 is configured */ | |
2096 | value[0] = USBVISION_16_422_SYNC; | |
2097 | /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/ | |
2098 | } | |
2099 | ||
2100 | rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]); | |
2101 | if (rc < 0) { | |
2102 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " | |
2103 | "reconnect or reload driver.\n", proc, rc); | |
2104 | return rc; | |
2105 | } | |
2106 | ||
2107 | ||
5490a7cb | 2108 | if (usbvision->tvnorm_id & V4L2_STD_PAL) { |
781aa1d1 | 2109 | value[0] = 0xC0; |
52cb0bf2 | 2110 | value[1] = 0x02; /* 0x02C0 -> 704 Input video line length */ |
781aa1d1 | 2111 | value[2] = 0x20; |
52cb0bf2 | 2112 | value[3] = 0x01; /* 0x0120 -> 288 Input video n. of lines */ |
781aa1d1 | 2113 | value[4] = 0x60; |
52cb0bf2 | 2114 | value[5] = 0x00; /* 0x0060 -> 96 Input video h offset */ |
781aa1d1 | 2115 | value[6] = 0x16; |
52cb0bf2 | 2116 | value[7] = 0x00; /* 0x0016 -> 22 Input video v offset */ |
5490a7cb | 2117 | } else if (usbvision->tvnorm_id & V4L2_STD_SECAM) { |
781aa1d1 | 2118 | value[0] = 0xC0; |
52cb0bf2 | 2119 | value[1] = 0x02; /* 0x02C0 -> 704 Input video line length */ |
781aa1d1 | 2120 | value[2] = 0x20; |
52cb0bf2 | 2121 | value[3] = 0x01; /* 0x0120 -> 288 Input video n. of lines */ |
781aa1d1 | 2122 | value[4] = 0x01; |
52cb0bf2 | 2123 | value[5] = 0x00; /* 0x0001 -> 01 Input video h offset */ |
781aa1d1 | 2124 | value[6] = 0x01; |
52cb0bf2 | 2125 | value[7] = 0x00; /* 0x0001 -> 01 Input video v offset */ |
781aa1d1 MCC |
2126 | } else { /* V4L2_STD_NTSC */ |
2127 | value[0] = 0xD0; | |
52cb0bf2 | 2128 | value[1] = 0x02; /* 0x02D0 -> 720 Input video line length */ |
781aa1d1 | 2129 | value[2] = 0xF0; |
52cb0bf2 | 2130 | value[3] = 0x00; /* 0x00F0 -> 240 Input video number of lines */ |
781aa1d1 | 2131 | value[4] = 0x50; |
52cb0bf2 | 2132 | value[5] = 0x00; /* 0x0050 -> 80 Input video h offset */ |
781aa1d1 | 2133 | value[6] = 0x10; |
52cb0bf2 | 2134 | value[7] = 0x00; /* 0x0010 -> 16 Input video v offset */ |
781aa1d1 MCC |
2135 | } |
2136 | ||
5490a7cb HV |
2137 | if (usbvision_device_data[usbvision->dev_model].x_offset >= 0) { |
2138 | value[4]=usbvision_device_data[usbvision->dev_model].x_offset & 0xff; | |
2139 | value[5]=(usbvision_device_data[usbvision->dev_model].x_offset & 0x0300) >> 8; | |
781aa1d1 MCC |
2140 | } |
2141 | ||
5490a7cb HV |
2142 | if (adjust_x_offset != -1) { |
2143 | value[4] = adjust_x_offset & 0xff; | |
2144 | value[5] = (adjust_x_offset & 0x0300) >> 8; | |
c6243d9c TM |
2145 | } |
2146 | ||
5490a7cb HV |
2147 | if (usbvision_device_data[usbvision->dev_model].y_offset >= 0) { |
2148 | value[6]=usbvision_device_data[usbvision->dev_model].y_offset & 0xff; | |
2149 | value[7]=(usbvision_device_data[usbvision->dev_model].y_offset & 0x0300) >> 8; | |
781aa1d1 MCC |
2150 | } |
2151 | ||
5490a7cb HV |
2152 | if (adjust_y_offset != -1) { |
2153 | value[6] = adjust_y_offset & 0xff; | |
2154 | value[7] = (adjust_y_offset & 0x0300) >> 8; | |
c6243d9c TM |
2155 | } |
2156 | ||
781aa1d1 MCC |
2157 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
2158 | USBVISION_OP_CODE, /* USBVISION specific code */ | |
2159 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0, | |
2160 | (__u16) USBVISION_LXSIZE_I, value, 8, HZ); | |
2161 | if (rc < 0) { | |
2162 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " | |
2163 | "reconnect or reload driver.\n", proc, rc); | |
2164 | return rc; | |
2165 | } | |
2166 | ||
2167 | ||
2168 | dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */ | |
2169 | ||
5490a7cb HV |
2170 | if(usbvision_device_data[usbvision->dev_model].dvi_yuv_override){ |
2171 | dvi_yuv_value = usbvision_device_data[usbvision->dev_model].dvi_yuv; | |
781aa1d1 | 2172 | } |
5490a7cb | 2173 | else if(usbvision_device_data[usbvision->dev_model].codec == CODEC_SAA7113) { |
18d8a454 | 2174 | /* This changes as the fine sync control changes. Further investigation necessary */ |
781aa1d1 MCC |
2175 | dvi_yuv_value = 0x06; |
2176 | } | |
2177 | ||
2178 | return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value)); | |
2179 | } | |
2180 | ||
2181 | ||
2182 | /* | |
2183 | * usbvision_set_dram_settings() | |
2184 | * | |
2185 | * Set the buffer address needed by the usbvision dram to operate | |
2186 | * This values has been taken with usbsnoop. | |
2187 | * | |
2188 | */ | |
2189 | ||
2190 | static int usbvision_set_dram_settings(struct usb_usbvision *usbvision) | |
2191 | { | |
2192 | int rc; | |
2193 | unsigned char value[8]; | |
2194 | ||
5490a7cb | 2195 | if (usbvision->isoc_mode == ISOC_MODE_COMPRESS) { |
781aa1d1 MCC |
2196 | value[0] = 0x42; |
2197 | value[1] = 0x71; | |
2198 | value[2] = 0xff; | |
2199 | value[3] = 0x00; | |
2200 | value[4] = 0x98; | |
2201 | value[5] = 0xe0; | |
2202 | value[6] = 0x71; | |
2203 | value[7] = 0xff; | |
52cb0bf2 HV |
2204 | /* UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte) */ |
2205 | /* FDL: 0x00000-0x0E099 = 57498 Words */ | |
2206 | /* VDW: 0x0E3FF-0x3FFFF */ | |
781aa1d1 MCC |
2207 | } |
2208 | else { | |
2209 | value[0] = 0x42; | |
2210 | value[1] = 0x00; | |
2211 | value[2] = 0xff; | |
2212 | value[3] = 0x00; | |
2213 | value[4] = 0x00; | |
2214 | value[5] = 0x00; | |
2215 | value[6] = 0x00; | |
2216 | value[7] = 0xff; | |
2217 | } | |
2218 | /* These are the values of the address of the video buffer, | |
2219 | * they have to be loaded into the USBVISION_DRM_PRM1-8 | |
2220 | * | |
2221 | * Start address of video output buffer for read: drm_prm1-2 -> 0x00000 | |
2222 | * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff | |
2223 | * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000 | |
2224 | * Only used in compressed mode | |
2225 | * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff | |
2226 | * Only used in compressed mode | |
2227 | * Start address of video output buffer for write: drm_prm1-7 -> 0x00000 | |
2228 | * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff | |
2229 | */ | |
2230 | ||
2231 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
2232 | return 0; | |
2233 | ||
2234 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), | |
2235 | USBVISION_OP_CODE, /* USBVISION specific code */ | |
2236 | USB_DIR_OUT | USB_TYPE_VENDOR | | |
2237 | USB_RECIP_ENDPOINT, 0, | |
2238 | (__u16) USBVISION_DRM_PRM1, value, 8, HZ); | |
2239 | ||
2240 | if (rc < 0) { | |
be9ed511 | 2241 | dev_err(&usbvision->dev->dev, "%sERROR=%d\n", __func__, rc); |
781aa1d1 MCC |
2242 | return rc; |
2243 | } | |
2244 | ||
2245 | /* Restart the video buffer logic */ | |
2246 | if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR | | |
2247 | USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0) | |
2248 | return rc; | |
2249 | rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00); | |
2250 | ||
2251 | return rc; | |
2252 | } | |
2253 | ||
2254 | /* | |
2255 | * () | |
2256 | * | |
2257 | * Power on the device, enables suspend-resume logic | |
2258 | * & reset the isoc End-Point | |
2259 | * | |
2260 | */ | |
2261 | ||
483dfdb6 | 2262 | int usbvision_power_on(struct usb_usbvision *usbvision) |
781aa1d1 | 2263 | { |
5490a7cb | 2264 | int err_code = 0; |
781aa1d1 MCC |
2265 | |
2266 | PDEBUG(DBG_FUNC, ""); | |
2267 | ||
2268 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN); | |
2269 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, | |
2270 | USBVISION_SSPND_EN | USBVISION_RES2); | |
38284ba3 | 2271 | |
781aa1d1 MCC |
2272 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
2273 | USBVISION_SSPND_EN | USBVISION_PWR_VID); | |
5490a7cb | 2274 | err_code = usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
781aa1d1 | 2275 | USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2); |
5490a7cb | 2276 | if (err_code == 1) { |
781aa1d1 MCC |
2277 | usbvision->power = 1; |
2278 | } | |
5490a7cb HV |
2279 | PDEBUG(DBG_FUNC, "%s: err_code %d", (err_code<0)?"ERROR":"power is on", err_code); |
2280 | return err_code; | |
781aa1d1 MCC |
2281 | } |
2282 | ||
2283 | ||
483dfdb6 TM |
2284 | /* |
2285 | * usbvision timer stuff | |
2286 | */ | |
2287 | ||
52cb0bf2 | 2288 | /* to call usbvision_power_off from task queue */ |
67952e8c | 2289 | static void call_usbvision_power_off(struct work_struct *work) |
483dfdb6 | 2290 | { |
5490a7cb | 2291 | struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, power_off_work); |
483dfdb6 TM |
2292 | |
2293 | PDEBUG(DBG_FUNC, ""); | |
c627b9d1 | 2294 | if (mutex_lock_interruptible(&usbvision->v4l2_lock)) |
289d4d2f | 2295 | return; |
289d4d2f | 2296 | |
483dfdb6 | 2297 | if(usbvision->user == 0) { |
1ff16c20 | 2298 | usbvision_i2c_unregister(usbvision); |
67952e8c | 2299 | |
483dfdb6 TM |
2300 | usbvision_power_off(usbvision); |
2301 | usbvision->initialized = 0; | |
2302 | } | |
c627b9d1 | 2303 | mutex_unlock(&usbvision->v4l2_lock); |
483dfdb6 TM |
2304 | } |
2305 | ||
5490a7cb | 2306 | static void usbvision_power_off_timer(unsigned long data) |
781aa1d1 MCC |
2307 | { |
2308 | struct usb_usbvision *usbvision = (void *) data; | |
2309 | ||
2310 | PDEBUG(DBG_FUNC, ""); | |
5490a7cb HV |
2311 | del_timer(&usbvision->power_off_timer); |
2312 | INIT_WORK(&usbvision->power_off_work, call_usbvision_power_off); | |
2313 | (void) schedule_work(&usbvision->power_off_work); | |
781aa1d1 MCC |
2314 | } |
2315 | ||
5490a7cb | 2316 | void usbvision_init_power_off_timer(struct usb_usbvision *usbvision) |
483dfdb6 | 2317 | { |
5490a7cb HV |
2318 | init_timer(&usbvision->power_off_timer); |
2319 | usbvision->power_off_timer.data = (long) usbvision; | |
2320 | usbvision->power_off_timer.function = usbvision_power_off_timer; | |
483dfdb6 TM |
2321 | } |
2322 | ||
5490a7cb | 2323 | void usbvision_set_power_off_timer(struct usb_usbvision *usbvision) |
483dfdb6 | 2324 | { |
5490a7cb | 2325 | mod_timer(&usbvision->power_off_timer, jiffies + USBVISION_POWEROFF_TIME); |
483dfdb6 TM |
2326 | } |
2327 | ||
5490a7cb | 2328 | void usbvision_reset_power_off_timer(struct usb_usbvision *usbvision) |
483dfdb6 | 2329 | { |
5490a7cb HV |
2330 | if (timer_pending(&usbvision->power_off_timer)) { |
2331 | del_timer(&usbvision->power_off_timer); | |
483dfdb6 TM |
2332 | } |
2333 | } | |
781aa1d1 MCC |
2334 | |
2335 | /* | |
2336 | * usbvision_begin_streaming() | |
2337 | * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no | |
2338 | * idea about the rest | |
2339 | */ | |
483dfdb6 | 2340 | int usbvision_begin_streaming(struct usb_usbvision *usbvision) |
781aa1d1 | 2341 | { |
5490a7cb | 2342 | int err_code = 0; |
781aa1d1 | 2343 | |
5490a7cb | 2344 | if (usbvision->isoc_mode == ISOC_MODE_COMPRESS) { |
781aa1d1 MCC |
2345 | usbvision_init_compression(usbvision); |
2346 | } | |
5490a7cb HV |
2347 | err_code = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID | |
2348 | usbvision->vin_reg2_preset); | |
2349 | return err_code; | |
781aa1d1 MCC |
2350 | } |
2351 | ||
2352 | /* | |
2353 | * usbvision_restart_isoc() | |
2354 | * Not sure yet if touching here PWR_REG make loose the config | |
2355 | */ | |
2356 | ||
483dfdb6 | 2357 | int usbvision_restart_isoc(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
2358 | { |
2359 | int ret; | |
2360 | ||
2361 | if ( | |
2362 | (ret = | |
2363 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, | |
2364 | USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0) | |
2365 | return ret; | |
2366 | if ( | |
2367 | (ret = | |
2368 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, | |
2369 | USBVISION_SSPND_EN | USBVISION_PWR_VID | | |
2370 | USBVISION_RES2)) < 0) | |
2371 | return ret; | |
2372 | if ( | |
2373 | (ret = | |
2374 | usbvision_write_reg(usbvision, USBVISION_VIN_REG2, | |
2375 | USBVISION_KEEP_BLANK | USBVISION_NOHVALID | | |
5490a7cb | 2376 | usbvision->vin_reg2_preset)) < 0) return ret; |
781aa1d1 MCC |
2377 | |
2378 | /* TODO: schedule timeout */ | |
70bdd9c8 | 2379 | while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) & 0x01) != 1); |
781aa1d1 MCC |
2380 | |
2381 | return 0; | |
2382 | } | |
2383 | ||
483dfdb6 | 2384 | int usbvision_audio_off(struct usb_usbvision *usbvision) |
781aa1d1 MCC |
2385 | { |
2386 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) { | |
2387 | printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n"); | |
2388 | return -1; | |
2389 | } | |
5490a7cb HV |
2390 | usbvision->audio_mute = 0; |
2391 | usbvision->audio_channel = USBVISION_AUDIO_MUTE; | |
781aa1d1 MCC |
2392 | return 0; |
2393 | } | |
2394 | ||
5490a7cb | 2395 | int usbvision_set_audio(struct usb_usbvision *usbvision, int audio_channel) |
781aa1d1 | 2396 | { |
5490a7cb HV |
2397 | if (!usbvision->audio_mute) { |
2398 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, audio_channel) < 0) { | |
781aa1d1 MCC |
2399 | printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n"); |
2400 | return -1; | |
2401 | } | |
2402 | } | |
5490a7cb | 2403 | usbvision->audio_channel = audio_channel; |
781aa1d1 MCC |
2404 | return 0; |
2405 | } | |
2406 | ||
483dfdb6 | 2407 | int usbvision_setup(struct usb_usbvision *usbvision,int format) |
781aa1d1 | 2408 | { |
483dfdb6 | 2409 | usbvision_set_video_format(usbvision, format); |
781aa1d1 MCC |
2410 | usbvision_set_dram_settings(usbvision); |
2411 | usbvision_set_compress_params(usbvision); | |
2412 | usbvision_set_input(usbvision); | |
2413 | usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT); | |
2414 | usbvision_restart_isoc(usbvision); | |
18d8a454 | 2415 | |
781aa1d1 MCC |
2416 | /* cosas del PCM */ |
2417 | return USBVISION_IS_OPERATIONAL(usbvision); | |
2418 | } | |
2419 | ||
2a9f8b5d TM |
2420 | int usbvision_set_alternate(struct usb_usbvision *dev) |
2421 | { | |
5490a7cb | 2422 | int err_code, prev_alt = dev->iface_alt; |
2a9f8b5d TM |
2423 | int i; |
2424 | ||
5490a7cb | 2425 | dev->iface_alt=0; |
2a9f8b5d | 2426 | for(i=0;i< dev->num_alt; i++) |
5490a7cb HV |
2427 | if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->iface_alt]) |
2428 | dev->iface_alt=i; | |
2429 | ||
2430 | if (dev->iface_alt != prev_alt) { | |
2431 | dev->isoc_packet_size = dev->alt_max_pkt_size[dev->iface_alt]; | |
2432 | PDEBUG(DBG_FUNC,"setting alternate %d with max_packet_size=%u", dev->iface_alt,dev->isoc_packet_size); | |
2433 | err_code = usb_set_interface(dev->dev, dev->iface, dev->iface_alt); | |
2434 | if (err_code < 0) { | |
be9ed511 MCC |
2435 | dev_err(&dev->dev->dev, |
2436 | "cannot change alternate number to %d (error=%i)\n", | |
5490a7cb HV |
2437 | dev->iface_alt, err_code); |
2438 | return err_code; | |
2a9f8b5d TM |
2439 | } |
2440 | } | |
2441 | ||
5490a7cb | 2442 | PDEBUG(DBG_ISOC, "ISO Packet Length:%d", dev->isoc_packet_size); |
2a9f8b5d TM |
2443 | |
2444 | return 0; | |
2445 | } | |
2446 | ||
483dfdb6 TM |
2447 | /* |
2448 | * usbvision_init_isoc() | |
2449 | * | |
2450 | */ | |
2451 | int usbvision_init_isoc(struct usb_usbvision *usbvision) | |
2452 | { | |
2453 | struct usb_device *dev = usbvision->dev; | |
5490a7cb | 2454 | int buf_idx, err_code, reg_value; |
fe818d1d | 2455 | int sb_size; |
483dfdb6 TM |
2456 | |
2457 | if (!USBVISION_IS_OPERATIONAL(usbvision)) | |
2458 | return -EFAULT; | |
2459 | ||
5490a7cb | 2460 | usbvision->cur_frame = NULL; |
483dfdb6 TM |
2461 | scratch_reset(usbvision); |
2462 | ||
2463 | /* Alternate interface 1 is is the biggest frame size */ | |
5490a7cb HV |
2464 | err_code = usbvision_set_alternate(usbvision); |
2465 | if (err_code < 0) { | |
2466 | usbvision->last_error = err_code; | |
781aa1d1 MCC |
2467 | return -EBUSY; |
2468 | } | |
5490a7cb | 2469 | sb_size = USBVISION_URB_FRAMES * usbvision->isoc_packet_size; |
781aa1d1 | 2470 | |
5490a7cb | 2471 | reg_value = (16 - usbvision_read_reg(usbvision, |
fe818d1d | 2472 | USBVISION_ALTER_REG)) & 0x0F; |
781aa1d1 | 2473 | |
5490a7cb | 2474 | usbvision->usb_bandwidth = reg_value >> 1; |
fe818d1d TM |
2475 | PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", |
2476 | usbvision->usb_bandwidth); | |
781aa1d1 MCC |
2477 | |
2478 | ||
2479 | ||
2480 | /* We double buffer the Iso lists */ | |
2481 | ||
5490a7cb | 2482 | for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) { |
781aa1d1 MCC |
2483 | int j, k; |
2484 | struct urb *urb; | |
2485 | ||
a1ed551c | 2486 | urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL); |
781aa1d1 | 2487 | if (urb == NULL) { |
be9ed511 MCC |
2488 | dev_err(&usbvision->dev->dev, |
2489 | "%s: usb_alloc_urb() failed\n", __func__); | |
781aa1d1 MCC |
2490 | return -ENOMEM; |
2491 | } | |
5490a7cb HV |
2492 | usbvision->sbuf[buf_idx].urb = urb; |
2493 | usbvision->sbuf[buf_idx].data = | |
997ea58e DM |
2494 | usb_alloc_coherent(usbvision->dev, |
2495 | sb_size, | |
2496 | GFP_KERNEL, | |
2497 | &urb->transfer_dma); | |
781aa1d1 MCC |
2498 | urb->dev = dev; |
2499 | urb->context = usbvision; | |
2500 | urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp); | |
f8768970 | 2501 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
781aa1d1 | 2502 | urb->interval = 1; |
5490a7cb HV |
2503 | urb->transfer_buffer = usbvision->sbuf[buf_idx].data; |
2504 | urb->complete = usbvision_isoc_irq; | |
781aa1d1 MCC |
2505 | urb->number_of_packets = USBVISION_URB_FRAMES; |
2506 | urb->transfer_buffer_length = | |
5490a7cb | 2507 | usbvision->isoc_packet_size * USBVISION_URB_FRAMES; |
781aa1d1 | 2508 | for (j = k = 0; j < USBVISION_URB_FRAMES; j++, |
5490a7cb | 2509 | k += usbvision->isoc_packet_size) { |
781aa1d1 | 2510 | urb->iso_frame_desc[j].offset = k; |
fe818d1d | 2511 | urb->iso_frame_desc[j].length = |
5490a7cb | 2512 | usbvision->isoc_packet_size; |
781aa1d1 MCC |
2513 | } |
2514 | } | |
2515 | ||
781aa1d1 | 2516 | /* Submit all URBs */ |
5490a7cb HV |
2517 | for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) { |
2518 | err_code = usb_submit_urb(usbvision->sbuf[buf_idx].urb, | |
fe818d1d | 2519 | GFP_KERNEL); |
5490a7cb | 2520 | if (err_code) { |
be9ed511 MCC |
2521 | dev_err(&usbvision->dev->dev, |
2522 | "%s: usb_submit_urb(%d) failed: error %d\n", | |
5490a7cb | 2523 | __func__, buf_idx, err_code); |
781aa1d1 MCC |
2524 | } |
2525 | } | |
2526 | ||
5490a7cb | 2527 | usbvision->streaming = stream_idle; |
fe818d1d | 2528 | PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x", |
d2db42dd | 2529 | __func__, |
fe818d1d | 2530 | usbvision->video_endp); |
781aa1d1 MCC |
2531 | return 0; |
2532 | } | |
2533 | ||
2534 | /* | |
2535 | * usbvision_stop_isoc() | |
2536 | * | |
2537 | * This procedure stops streaming and deallocates URBs. Then it | |
2538 | * activates zero-bandwidth alt. setting of the video interface. | |
2539 | * | |
2540 | */ | |
483dfdb6 | 2541 | void usbvision_stop_isoc(struct usb_usbvision *usbvision) |
781aa1d1 | 2542 | { |
5490a7cb HV |
2543 | int buf_idx, err_code, reg_value; |
2544 | int sb_size = USBVISION_URB_FRAMES * usbvision->isoc_packet_size; | |
781aa1d1 | 2545 | |
5490a7cb | 2546 | if ((usbvision->streaming == stream_off) || (usbvision->dev == NULL)) |
781aa1d1 MCC |
2547 | return; |
2548 | ||
2549 | /* Unschedule all of the iso td's */ | |
5490a7cb HV |
2550 | for (buf_idx = 0; buf_idx < USBVISION_NUMSBUF; buf_idx++) { |
2551 | usb_kill_urb(usbvision->sbuf[buf_idx].urb); | |
2552 | if (usbvision->sbuf[buf_idx].data){ | |
997ea58e DM |
2553 | usb_free_coherent(usbvision->dev, |
2554 | sb_size, | |
5490a7cb HV |
2555 | usbvision->sbuf[buf_idx].data, |
2556 | usbvision->sbuf[buf_idx].urb->transfer_dma); | |
38284ba3 | 2557 | } |
5490a7cb HV |
2558 | usb_free_urb(usbvision->sbuf[buf_idx].urb); |
2559 | usbvision->sbuf[buf_idx].urb = NULL; | |
18d8a454 | 2560 | } |
781aa1d1 | 2561 | |
5490a7cb HV |
2562 | PDEBUG(DBG_ISOC, "%s: streaming=stream_off\n", __func__); |
2563 | usbvision->streaming = stream_off; | |
781aa1d1 MCC |
2564 | |
2565 | if (!usbvision->remove_pending) { | |
2566 | ||
2567 | /* Set packet size to 0 */ | |
5490a7cb HV |
2568 | usbvision->iface_alt=0; |
2569 | err_code = usb_set_interface(usbvision->dev, usbvision->iface, | |
2570 | usbvision->iface_alt); | |
2571 | if (err_code < 0) { | |
be9ed511 MCC |
2572 | dev_err(&usbvision->dev->dev, |
2573 | "%s: usb_set_interface() failed: error %d\n", | |
5490a7cb HV |
2574 | __func__, err_code); |
2575 | usbvision->last_error = err_code; | |
781aa1d1 | 2576 | } |
5490a7cb HV |
2577 | reg_value = (16-usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F; |
2578 | usbvision->isoc_packet_size = | |
2579 | (reg_value == 0) ? 0 : (reg_value * 64) - 1; | |
fe818d1d | 2580 | PDEBUG(DBG_ISOC, "ISO Packet Length:%d", |
5490a7cb | 2581 | usbvision->isoc_packet_size); |
781aa1d1 | 2582 | |
5490a7cb | 2583 | usbvision->usb_bandwidth = reg_value >> 1; |
fe818d1d TM |
2584 | PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", |
2585 | usbvision->usb_bandwidth); | |
781aa1d1 MCC |
2586 | } |
2587 | } | |
2588 | ||
483dfdb6 | 2589 | int usbvision_muxsel(struct usb_usbvision *usbvision, int channel) |
781aa1d1 | 2590 | { |
66a17879 TM |
2591 | /* inputs #0 and #3 are constant for every SAA711x. */ |
2592 | /* inputs #1 and #2 are variable for SAA7111 and SAA7113 */ | |
2593 | int mode[4]= {SAA7115_COMPOSITE0, 0, 0, SAA7115_COMPOSITE3}; | |
18d8a454 | 2594 | int audio[]= {1, 0, 0, 0}; |
52cb0bf2 HV |
2595 | /* channel 0 is TV with audiochannel 1 (tuner mono) */ |
2596 | /* channel 1 is Composite with audio channel 0 (line in) */ | |
2597 | /* channel 2 is S-Video with audio channel 0 (line in) */ | |
2598 | /* channel 3 is additional video inputs to the device with audio channel 0 (line in) */ | |
781aa1d1 MCC |
2599 | |
2600 | RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs); | |
f2242ee5 | 2601 | usbvision->ctl_input = channel; |
781aa1d1 | 2602 | |
52cb0bf2 HV |
2603 | /* set the new channel */ |
2604 | /* Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video */ | |
2605 | /* Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red */ | |
781aa1d1 | 2606 | |
5490a7cb | 2607 | switch (usbvision_device_data[usbvision->dev_model].codec) { |
781aa1d1 | 2608 | case CODEC_SAA7113: |
66a17879 | 2609 | mode[1] = SAA7115_COMPOSITE2; |
5490a7cb | 2610 | if (switch_svideo_input) { |
66a17879 | 2611 | /* To handle problems with S-Video Input for |
5490a7cb | 2612 | * some devices. Use switch_svideo_input |
66a17879 TM |
2613 | * parameter when loading the module.*/ |
2614 | mode[2] = SAA7115_COMPOSITE1; | |
781aa1d1 MCC |
2615 | } |
2616 | else { | |
66a17879 | 2617 | mode[2] = SAA7115_SVIDEO1; |
781aa1d1 MCC |
2618 | } |
2619 | break; | |
2620 | case CODEC_SAA7111: | |
781aa1d1 | 2621 | default: |
66a17879 TM |
2622 | /* modes for saa7111 */ |
2623 | mode[1] = SAA7115_COMPOSITE1; | |
2624 | mode[2] = SAA7115_SVIDEO1; | |
2625 | break; | |
781aa1d1 | 2626 | } |
5325b427 | 2627 | call_all(usbvision, video, s_routing, mode[channel], 0, 0); |
781aa1d1 MCC |
2628 | usbvision_set_audio(usbvision, audio[channel]); |
2629 | return 0; | |
2630 | } | |
2631 | ||
781aa1d1 MCC |
2632 | /* |
2633 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
2634 | * --------------------------------------------------------------------------- | |
2635 | * Local variables: | |
2636 | * c-basic-offset: 8 | |
2637 | * End: | |
2638 | */ |