26 #include <linux/kernel.h>
27 #include <linux/list.h>
33 #include <linux/module.h>
37 #include <linux/videodev2.h>
38 #include <linux/i2c.h>
48 static unsigned int core_debug;
52 static int adjust_compression = 1;
54 MODULE_PARM_DESC(adjust_compression,
" Set the ADPCM compression for the device. Default: 1 (On)");
58 static int switch_svideo_input;
60 MODULE_PARM_DESC(switch_svideo_input,
" Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
62 static unsigned int adjust_x_offset = -1;
66 static unsigned int adjust_y_offset = -1;
71 #define ENABLE_HEXDUMP 0
74 #ifdef USBVISION_DEBUG
75 #define PDEBUG(level, fmt, args...) { \
76 if (core_debug & (level)) \
77 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
78 __func__, __LINE__ , ## args); \
81 #define PDEBUG(level, fmt, args...) do {} while (0)
84 #define DBG_HEADER (1 << 0)
85 #define DBG_IRQ (1 << 1)
86 #define DBG_ISOC (1 << 2)
87 #define DBG_PARSE (1 << 3)
88 #define DBG_SCRATCH (1 << 4)
89 #define DBG_FUNC (1 << 5)
102 #define DEFAULT_SCRATCH_BUF_SIZE (0x20000)
106 static int usbvision_request_intra(
struct usb_usbvision *usbvision);
107 static int usbvision_unrequest_intra(
struct usb_usbvision *usbvision);
108 static int usbvision_adjust_compression(
struct usb_usbvision *usbvision);
109 static int usbvision_measure_bandwidth(
struct usb_usbvision *usbvision);
120 static void *usbvision_rvmalloc(
unsigned long size)
131 adr = (
unsigned long) mem;
141 static void usbvision_rvfree(
void *mem,
unsigned long size)
150 adr = (
unsigned long) mem;
151 while ((
long) size > 0) {
162 static void usbvision_hexdump(
const unsigned char *
data,
int len)
167 for (i = k = 0; len > 0; i++, len--) {
168 if (i > 0 && (i % 16 == 0)) {
172 k +=
sprintf(&tmp[k],
"%02x ", data[i]);
187 len += scratch_buf_size;
199 free += scratch_buf_size;
211 static int scratch_put(
struct usb_usbvision *usbvision,
unsigned char *data,
222 if (len == len_part) {
236 static void scratch_mark_header(
struct usb_usbvision *usbvision)
247 static int scratch_get_extra(
struct usb_usbvision *usbvision,
248 unsigned char *data,
int *
ptr,
int len)
252 if (*ptr + len < scratch_buf_size) {
256 len_part = scratch_buf_size - *
ptr;
258 if (len == len_part) {
262 *ptr = len - len_part;
273 static void scratch_set_extra_ptr(
struct usb_usbvision *usbvision,
int *ptr,
283 static void scratch_inc_extra_ptr(
int *ptr,
int len)
285 *ptr = (*ptr + len) % scratch_buf_size;
292 static int scratch_get(
struct usb_usbvision *usbvision,
unsigned char *data,
303 if (len == len_part) {
318 static int scratch_get_header(
struct usb_usbvision *usbvision,
347 static void scratch_rm_old(
struct usb_usbvision *usbvision,
int len)
370 scratch_reset(usbvision);
373 "%s: unable to allocate %d bytes for scratch\n",
374 __func__, scratch_buf_size);
398 "%s: unable to allocate %d for compr. frame buffer\n",
428 int found_header = 0;
450 usbvision_unrequest_intra(usbvision);
465 scratch_reset(usbvision);
476 usbvision_request_intra(usbvision);
496 unsigned char yuyv[4] = { 180, 128, 10, 128 };
497 unsigned char rv, gv, bv;
498 int clipmask_index, bytes_per_pixel;
499 int stretch_bytes, clipmask_add;
506 if (scratch_len(usbvision) < len) {
514 bytes_per_pixel = frame->
v4l2_format.bytes_per_pixel;
515 stretch_bytes = (usbvision->
stretch_width - 1) * bytes_per_pixel;
520 scratch_get(usbvision, &yuyv[0], 4);
531 *f++ = (0x07 & (gv >> 3)) |
548 *f++ = (0x03 & (gv >> 3)) |
553 clipmask_index += clipmask_add;
565 *f++ = (0x07 & (gv >> 3)) |
582 *f++ = (0x03 & (gv >> 3)) |
587 clipmask_index += clipmask_add;
600 static int usbvision_decompress(
struct usb_usbvision *usbvision,
unsigned char *compressed,
601 unsigned char *decompressed,
int *start_pos,
602 int *block_typestart_pos,
int len)
604 int rest_pixel,
idx,
pos, extra_pos, block_len, block_type_pos, block_type_len;
605 unsigned char block_byte, block_code, block_type, block_type_byte, integrator;
609 block_type_pos = *block_typestart_pos;
619 for (idx = 0; idx < len; idx++) {
620 if (block_len == 0) {
621 if (block_type_len == 0) {
622 block_type_byte = compressed[block_type_pos];
626 block_type = (block_type_byte & 0xC0) >> 6;
632 if (block_type == 0) {
633 if (rest_pixel >= 24) {
636 integrator = decompressed[
idx];
638 idx += rest_pixel - 1;
642 block_code = compressed[
pos];
644 if (rest_pixel >= 24)
647 block_len = rest_pixel;
648 rest_pixel -= block_len;
649 extra_pos = pos + (block_len / 4);
651 block_type_byte <<= 2;
655 if ((block_len % 4) == 0) {
656 block_byte = compressed[
pos];
660 integrator = decompressed[
idx];
661 switch (block_byte & 0xC0) {
663 integrator += compressed[extra_pos];
667 integrator += block_code;
670 integrator -= block_code;
673 decompressed[
idx] = integrator;
678 *start_pos = extra_pos;
679 *block_typestart_pos = block_type_pos;
695 #define USBVISION_STRIP_MAGIC 0x5A
696 #define USBVISION_STRIP_LEN_MAX 400
697 #define USBVISION_STRIP_HEADER_LEN 3
703 int idx, idx_end, strip_len, strip_ptr, startblock_pos, block_pos, block_type_pos;
706 unsigned char rv, gv, bv;
707 static unsigned char *Y, *
U, *
V;
723 v = u + (image_size >> 1);
726 u = v + (image_size >> 2);
730 usbvision_adjust_compression(usbvision);
732 if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN)
736 scratch_set_extra_ptr(usbvision, &strip_ptr, 0);
737 scratch_get_extra(usbvision, &strip_header[0], &strip_ptr,
738 USBVISION_STRIP_HEADER_LEN);
740 if (strip_header[0] != USBVISION_STRIP_MAGIC) {
746 if (frame->
curline != (
int)strip_header[2]) {
751 strip_len = 2 * (
unsigned int)strip_header[1];
752 if (strip_len > USBVISION_STRIP_LEN_MAX) {
755 usbvision_request_intra(usbvision);
758 if (scratch_len(usbvision) < strip_len) {
773 scratch_get(usbvision, strip_data, strip_len);
777 startblock_pos = block_type_pos + (idx_end - 1) / 96 + (idx_end / 2 - 1) / 96 + 2;
778 block_pos = startblock_pos;
782 usbvision_decompress(usbvision, strip_data, Y, &block_pos, &block_type_pos, idx_end);
787 usbvision_decompress(usbvision, strip_data, V, &block_pos, &block_type_pos, idx_end / 2);
789 usbvision_decompress(usbvision, strip_data, U, &block_pos, &block_type_pos, idx_end / 2);
793 if (block_pos > strip_len)
796 for (idx = 0; idx < idx_end; idx++) {
799 *f++ = idx & 0x01 ? U[idx / 2] : V[idx / 2];
808 if (!((idx & 0x01) | (frame->
curline & 0x01))) {
823 *f++ = (0x03 & (gv >> 3)) |
829 *f++ = (0x07 & (gv >> 3)) |
874 unsigned char *f_even =
NULL, *f_odd =
NULL;
875 unsigned int pixel_per_line,
block;
876 int pixel, block_split;
877 int y_ptr, u_ptr, v_ptr, y_odd_offset;
878 const int y_block_size = 128;
879 const int uv_block_size = 64;
880 const int sub_block_size = 32;
881 const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
882 const int uv_step[] = { 0, 0, 0, 4 }, uv_step_size = 4;
883 unsigned char y[2],
u,
v;
884 int y_, u_, v_, vb, uvg, ur;
887 int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
888 int clipmask_add, stretch_bytes;
897 bytes_per_pixel = frame->
v4l2_format.bytes_per_pixel;
898 stretch_bytes = (usbvision->
stretch_width - 1) * bytes_per_pixel;
904 if (scratch_len(usbvision) < (
int)pixel_per_line * 3) {
912 block_split = (pixel_per_line%y_block_size) ? 1 : 0;
914 y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
915 + block_split * uv_block_size;
917 scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
918 scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
919 scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
920 + (4 - block_split) * sub_block_size);
922 for (block = 0; block < (pixel_per_line / sub_block_size); block++) {
923 for (pixel = 0; pixel < sub_block_size; pixel += 2) {
924 scratch_get(usbvision, &y[0], 2);
925 scratch_get_extra(usbvision, &u, &u_ptr, 1);
926 scratch_get_extra(usbvision, &v, &v_ptr, 1);
932 uvg = -53281 * u_ - 25625 * v_;
939 y_ = 76284 * (y[0] - 16);
941 b_ = (y_ + vb) >> 16;
942 g_ = (y_ + uvg) >> 16;
943 r_ = (y_ + ur) >> 16;
970 *f_even++ = (0x03 & (g >> 3)) |
975 clipmask_even_index += clipmask_add;
976 f_even += stretch_bytes;
982 y_ = 76284 * (y[1] - 16);
984 b_ = (y_ + vb) >> 16;
985 g_ = (y_ + uvg) >> 16;
986 r_ = (y_ + ur) >> 16;
1013 *f_even++ = (0x03 & (g >> 3)) |
1018 clipmask_even_index += clipmask_add;
1019 f_even += stretch_bytes;
1021 scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
1027 y_ = 76284 * (y[0] - 16);
1029 b_ = (y_ + vb) >> 16;
1030 g_ = (y_ + uvg) >> 16;
1031 r_ = (y_ + ur) >> 16;
1058 *f_odd++ = (0x03 & (g >> 3)) |
1063 clipmask_odd_index += clipmask_add;
1064 f_odd += stretch_bytes;
1070 y_ = 76284 * (y[1] - 16);
1072 b_ = (y_ + vb) >> 16;
1073 g_ = (y_ + uvg) >> 16;
1074 r_ = (y_ + ur) >> 16;
1101 *f_odd++ = (0x03 & (g >> 3)) |
1106 clipmask_odd_index += clipmask_add;
1107 f_odd += stretch_bytes;
1110 scratch_rm_old(usbvision, y_step[block % y_step_size] * sub_block_size);
1111 scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
1113 scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
1115 scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
1119 scratch_rm_old(usbvision, pixel_per_line * 3 / 2
1120 + block_split * sub_block_size);
1138 static void usbvision_parse_data(
struct usb_usbvision *usbvision)
1151 if (scratch_len(usbvision)) {
1153 newstate = usbvision_find_header(usbvision);
1156 newstate = usbvision_parse_lines_420(usbvision, ©len);
1158 newstate = usbvision_parse_lines_422(usbvision, ©len);
1160 newstate = usbvision_parse_compress(usbvision, ©len);
1178 spin_unlock_irqrestore(&usbvision->
queue_lock, lock_flags);
1183 if (waitqueue_active(&usbvision->
wait_frame)) {
1199 static int usbvision_compress_isochronous(
struct usb_usbvision *usbvision,
1205 for (i = 0; i < urb->number_of_packets; i++) {
1206 int packet_len = urb->iso_frame_desc[
i].actual_length;
1207 int packet_stat = urb->iso_frame_desc[
i].status;
1209 packet_data = urb->transfer_buffer + urb->iso_frame_desc[
i].offset;
1213 PDEBUG(
DBG_ISOC,
"data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
1219 if (packet_len < 0) {
1223 }
else if (packet_len == 0) {
1238 scratch_mark_header(usbvision);
1239 usbvision_measure_bandwidth(usbvision);
1251 if (scratch_free(usbvision) < packet_len) {
1254 scratch_len(usbvision), packet_len);
1255 scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
1259 scratch_put(usbvision, packet_data, packet_len);
1260 totlen += packet_len;
1270 usbvision_hexdump(
data0, (totlen > 64) ? 64 : totlen);
1278 static void usbvision_isoc_irq(
struct urb *urb)
1292 if (urb->status == -
ENOENT)
1309 len = usbvision_compress_isochronous(usbvision, urb);
1317 !list_empty(&(usbvision->inqueue))) {
1323 usbvision_parse_data(usbvision);
1328 "received data, but no one needs it");
1329 scratch_reset(usbvision);
1333 scratch_reset(usbvision);
1339 urb->iso_frame_desc[
i].status = 0;
1340 urb->iso_frame_desc[
i].actual_length = 0;
1344 urb->dev = usbvision->
dev;
1349 "%s: usb_submit_urb failed: error %d\n",
1350 __func__, err_code);
1378 0, (
__u16) reg, buffer, 1,
HZ);
1382 "%s: failed: error %d\n", __func__, err_code);
1397 unsigned char value)
1411 "%s: failed: error %d\n", __func__, err_code);
1417 static void usbvision_ctrl_urb_complete(
struct urb *urb)
1429 unsigned char *data,
int len)
1445 usb_fill_control_urb(usbvision->
ctrl_urb, usbvision->
dev,
1446 usb_sndctrlpipe(usbvision->
dev, 1),
1449 usbvision_ctrl_urb_complete,
1464 static int usbvision_init_compression(
struct usb_usbvision *usbvision)
1485 static int usbvision_measure_bandwidth(
struct usb_usbvision *usbvision)
1505 static int usbvision_adjust_compression(
struct usb_usbvision *usbvision)
1520 distortion = 7 + 248 * usbvision->
compr_level / 100;
1521 buffer[2] = (
unsigned char)(distortion & 0xFF);
1522 buffer[3] = (
unsigned char)(distortion & 0xFF);
1523 distortion = 1 + 42 * usbvision->
compr_level / 100;
1524 buffer[4] = (
unsigned char)(distortion & 0xFF);
1525 buffer[5] = (
unsigned char)(distortion & 0xFF);
1529 distortion = 2 + 253 * usbvision->
compr_level / 100;
1530 buffer[2] = (
unsigned char)(distortion & 0xFF);
1532 distortion = 0 + 43 * usbvision->
compr_level / 100;
1533 buffer[4] = (
unsigned char)(distortion & 0xFF);
1537 if (err_code == 0) {
1538 PDEBUG(
DBG_IRQ,
"new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0],
1539 buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
1547 static int usbvision_request_intra(
struct usb_usbvision *usbvision)
1550 unsigned char buffer[1];
1559 static int usbvision_unrequest_intra(
struct usb_usbvision *usbvision)
1562 unsigned char buffer[1];
1583 usbvision->
power = 0;
1584 PDEBUG(
DBG_FUNC,
"%s: err_code %d", (err_code != 1) ?
"ERROR" :
"power is off", err_code);
1589 static int usbvision_init_webcam(
struct usb_usbvision *usbvision)
1593 static char init_values[38][3] = {
1594 { 0x04, 0x12, 0x08 }, { 0x05, 0xff, 0xc8 }, { 0x06, 0x18, 0x07 }, { 0x07, 0x90, 0x00 },
1595 { 0x09, 0x00, 0x00 }, { 0x0a, 0x00, 0x00 }, { 0x0b, 0x08, 0x00 }, { 0x0d, 0xcc, 0xcc },
1596 { 0x0e, 0x13, 0x14 }, { 0x10, 0x9b, 0x83 }, { 0x11, 0x5a, 0x3f }, { 0x12, 0xe4, 0x73 },
1597 { 0x13, 0x88, 0x84 }, { 0x14, 0x89, 0x80 }, { 0x15, 0x00, 0x20 }, { 0x16, 0x00, 0x00 },
1598 { 0x17, 0xff, 0xa0 }, { 0x18, 0x6b, 0x20 }, { 0x19, 0x22, 0x40 }, { 0x1a, 0x10, 0x07 },
1599 { 0x1b, 0x00, 0x47 }, { 0x1c, 0x03, 0xe0 }, { 0x1d, 0x00, 0x00 }, { 0x1e, 0x00, 0x00 },
1600 { 0x1f, 0x00, 0x00 }, { 0x20, 0x00, 0x00 }, { 0x21, 0x00, 0x00 }, { 0x22, 0x00, 0x00 },
1601 { 0x23, 0x00, 0x00 }, { 0x24, 0x00, 0x00 }, { 0x25, 0x00, 0x00 }, { 0x26, 0x00, 0x00 },
1602 { 0x27, 0x00, 0x00 }, { 0x28, 0x00, 0x00 }, { 0x29, 0x00, 0x00 }, { 0x08, 0x80, 0x60 },
1603 { 0x0f, 0x2d, 0x24 }, { 0x0c, 0x80, 0x80 }
1609 init_values[4][1] = 0x34;
1611 for (i = 0; i <
sizeof(init_values) / 3; i++) {
1613 memcpy(value, init_values[i], 3);
1615 usb_sndctrlpipe(usbvision->
dev, 1),
1643 static const char proc[] =
"usbvision_set_video_format";
1645 unsigned char value[2];
1655 printk(
KERN_ERR "usbvision: unknown video format %02x, using default YUV420",
1669 "reconnect or reload driver.\n", proc, rc);
1684 int usb_width, usb_height;
1685 unsigned int frame_rate = 0, frame_drop = 0;
1686 unsigned char value[4];
1692 usb_width = width / 2;
1700 usb_height = height / 2;
1713 usb_width, usb_height, width, height,
1717 if ((usb_width != usbvision->
curwidth) || (usb_height != usbvision->
curheight)) {
1718 value[0] = usb_width & 0xff;
1719 value[1] = (usb_width >> 8) & 0x03;
1720 value[2] = usb_height & 0xff;
1721 value[3] = (usb_height >> 8) & 0x03;
1730 "%s failed: error %d\n", __func__, err_code);
1738 frame_rate = (usbvision->
isoc_packet_size * 1000) / (usb_width * usb_height * 2);
1740 frame_rate = (usbvision->
isoc_packet_size * 1000) / ((usb_width * usb_height * 12) / 8);
1745 frame_drop = frame_rate * 32 / 25 - 1;
1747 frame_drop = frame_rate * 32 / 30 - 1;
1751 PDEBUG(
DBG_FUNC,
"frame_rate %d fps, frame_drop %d", frame_rate, frame_drop);
1786 usbvision->
palette.bytes_per_pixel);
1793 if (usbvision->
fbuf)
1803 for (i = 0; i < usbvision->
num_frames; i++) {
1804 usbvision->
frame[
i].index =
i;
1815 usbvision->
frame[
i].bytes_read = 0;
1846 INIT_LIST_HEAD(&(usbvision->inqueue));
1847 INIT_LIST_HEAD(&(usbvision->
outqueue));
1851 usbvision->
frame[
i].bytes_read = 0;
1877 static int usbvision_set_compress_params(
struct usb_usbvision *usbvision)
1879 static const char proc[] =
"usbvision_set_compresion_params: ";
1881 unsigned char value[6];
1907 "reconnect or reload driver.\n", proc, rc);
1938 "reconnect or reload driver.\n", proc, rc);
1953 static const char proc[] =
"usbvision_set_input: ";
1955 unsigned char value[8];
1956 unsigned char dvi_yuv_value;
1977 "reconnect or reload driver.\n", proc, rc);
2022 if (adjust_x_offset != -1) {
2023 value[4] = adjust_x_offset & 0xff;
2024 value[5] = (adjust_x_offset & 0x0300) >> 8;
2032 if (adjust_y_offset != -1) {
2033 value[6] = adjust_y_offset & 0xff;
2034 value[7] = (adjust_y_offset & 0x0300) >> 8;
2043 "reconnect or reload driver.\n", proc, rc);
2048 dvi_yuv_value = 0x00;
2054 dvi_yuv_value = 0x06;
2069 static int usbvision_set_dram_settings(
struct usb_usbvision *usbvision)
2072 unsigned char value[8];
2119 dev_err(&usbvision->
dev->dev,
"%s: ERROR=%d\n", __func__, rc);
2163 usbvision->
power = 1;
2164 PDEBUG(
DBG_FUNC,
"%s: err_code %d", (err_code < 0) ?
"ERROR" :
"power is on", err_code);
2182 if (usbvision->
user == 0) {
2191 static void usbvision_power_off_timer(
unsigned long data)
2227 usbvision_init_compression(usbvision);
2278 printk(
KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
2289 usbvision_init_webcam(usbvision);
2290 usbvision_set_video_format(usbvision, format);
2291 usbvision_set_dram_settings(usbvision);
2292 usbvision_set_compress_params(usbvision);
2307 for (i = 0; i < dev->
num_alt; i++)
2318 "cannot change alternate number to %d (error=%i)\n",
2335 struct usb_device *
dev = usbvision->
dev;
2343 scratch_reset(usbvision);
2371 "%s: usb_alloc_urb() failed\n", __func__);
2374 usbvision->
sbuf[buf_idx].urb =
urb;
2375 usbvision->
sbuf[buf_idx].data =
2379 &urb->transfer_dma);
2381 urb->context = usbvision;
2382 urb->pipe = usb_rcvisocpipe(dev, usbvision->
video_endp);
2383 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
2385 urb->transfer_buffer = usbvision->
sbuf[buf_idx].data;
2386 urb->complete = usbvision_isoc_irq;
2388 urb->transfer_buffer_length =
2392 urb->iso_frame_desc[
j].offset =
k;
2393 urb->iso_frame_desc[
j].length =
2404 "%s: usb_submit_urb(%d) failed: error %d\n",
2405 __func__, buf_idx, err_code);
2434 if (usbvision->
sbuf[buf_idx].data) {
2437 usbvision->
sbuf[buf_idx].data,
2438 usbvision->
sbuf[buf_idx].urb->transfer_dma);
2441 usbvision->
sbuf[buf_idx].urb =
NULL;
2454 "%s: usb_set_interface() failed: error %d\n",
2455 __func__, err_code);
2460 (reg_value == 0) ? 0 : (reg_value * 64) - 1;
2475 int audio[] = { 1, 0, 0, 0 };
2491 if (switch_svideo_input) {
2507 call_all(usbvision, video, s_routing, mode[channel], 0, 0);