ov511.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:177k
- }
- if (ov511->decomp_ops) {
- if (!ov511->decomp_ops->decomp_lock) {
- ov511->decomp_ops = NULL;
- unlock_kernel();
- return -ENOSYS;
- }
- ov511->decomp_ops->decomp_lock();
- unlock_kernel();
- return 0;
- } else {
- unlock_kernel();
- return -ENXIO;
- }
- }
- /* Unlocks decompression module and nulls ov511->decomp_ops. Safe to call even
- * if ov511->decomp_ops is NULL.
- */
- static void
- ov51x_release_decompressor(struct usb_ov511 *ov511)
- {
- int released = 0; /* Did we actually do anything? */
- if (!ov511)
- return;
- lock_kernel();
- if (ov511->decomp_ops && ov511->decomp_ops->decomp_unlock) {
- ov511->decomp_ops->decomp_unlock();
- released = 1;
- }
- ov511->decomp_ops = NULL;
-
- unlock_kernel();
- if (released)
- PDEBUG(3, "Decompressor released");
- }
- static void
- ov51x_decompress(struct usb_ov511 *ov511, struct ov511_frame *frame,
- unsigned char *pIn0, unsigned char *pOut0)
- {
- if (!ov511->decomp_ops)
- if (ov51x_request_decompressor(ov511))
- return;
- PDEBUG(4, "Decompressing %d bytes", frame->bytes_recvd);
- if (frame->format == VIDEO_PALETTE_GREY
- && ov511->decomp_ops->decomp_400) {
- int ret = ov511->decomp_ops->decomp_400(
- pIn0,
- pOut0,
- frame->rawwidth,
- frame->rawheight,
- frame->bytes_recvd);
- PDEBUG(4, "DEBUG: decomp_400 returned %d", ret);
- } else if (ov511->decomp_ops->decomp_420) {
- int ret = ov511->decomp_ops->decomp_420(
- pIn0,
- pOut0,
- frame->rawwidth,
- frame->rawheight,
- frame->bytes_recvd);
- PDEBUG(4, "DEBUG: decomp_420 returned %d", ret);
- } else {
- err("Decompressor does not support this format");
- }
- }
- /**********************************************************************
- *
- * Format conversion
- *
- **********************************************************************/
- /* Converts from planar YUV420 to RGB24. */
- static void
- yuv420p_to_rgb(struct ov511_frame *frame,
- unsigned char *pIn0, unsigned char *pOut0, int bits)
- {
- const int numpix = frame->width * frame->height;
- const int bytes = bits >> 3;
- int i, j, y00, y01, y10, y11, u, v;
- unsigned char *pY = pIn0;
- unsigned char *pU = pY + numpix;
- unsigned char *pV = pU + numpix / 4;
- unsigned char *pOut = pOut0;
- for (j = 0; j <= frame->height - 2; j += 2) {
- for (i = 0; i <= frame->width - 2; i += 2) {
- y00 = *pY;
- y01 = *(pY + 1);
- y10 = *(pY + frame->width);
- y11 = *(pY + frame->width + 1);
- u = (*pU++) - 128;
- v = (*pV++) - 128;
- ov511_move_420_block(y00, y01, y10, y11, u, v,
- frame->width, pOut, bits);
-
- pY += 2;
- pOut += 2 * bytes;
- }
- pY += frame->width;
- pOut += frame->width * bytes;
- }
- }
- /* Converts from planar YUV420 to YUV422 (YUYV). */
- static void
- yuv420p_to_yuv422(struct ov511_frame *frame,
- unsigned char *pIn0, unsigned char *pOut0)
- {
- const int numpix = frame->width * frame->height;
- int i, j;
- unsigned char *pY = pIn0;
- unsigned char *pU = pY + numpix;
- unsigned char *pV = pU + numpix / 4;
- unsigned char *pOut = pOut0;
- for (i = 0; i < numpix; i++) {
- *pOut = *(pY + i);
- pOut += 2;
- }
- pOut = pOut0 + 1;
- for (j = 0; j <= frame->height - 2 ; j += 2) {
- for (i = 0; i <= frame->width - 2; i += 2) {
- int u = *pU++;
- int v = *pV++;
-
- *pOut = u;
- *(pOut+2) = v;
- *(pOut+frame->width*2) = u;
- *(pOut+frame->width*2+2) = v;
- pOut += 4;
- }
- pOut += (frame->width * 2);
- }
- }
- /* Converts pData from planar YUV420 to planar YUV422 **in place**. */
- static void
- yuv420p_to_yuv422p(struct ov511_frame *frame, unsigned char *pData)
- {
- const int numpix = frame->width * frame->height;
- const int w = frame->width;
- int j;
- unsigned char *pIn, *pOut;
- /* Clear U and V */
- memset(pData + numpix + numpix / 2, 127, numpix / 2);
- /* Convert V starting from beginning and working forward */
- pIn = pData + numpix + numpix / 4;
- pOut = pData + numpix +numpix / 2;
- for (j = 0; j <= frame->height - 2; j += 2) {
- memmove(pOut, pIn, w/2);
- memmove(pOut + w/2, pIn, w/2);
- pIn += w/2;
- pOut += w;
- }
- /* Convert U, starting from end and working backward */
- pIn = pData + numpix + numpix / 4;
- pOut = pData + numpix + numpix / 2;
- for (j = 0; j <= frame->height - 2; j += 2) {
- pIn -= w/2;
- pOut -= w;
- memmove(pOut, pIn, w/2);
- memmove(pOut + w/2, pIn, w/2);
- }
- }
- /* Fuses even and odd fields together, and doubles width.
- * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
- * OUTPUT: a normal YUV planar image, with correct aspect ratio
- */
- static void
- deinterlace(struct ov511_frame *frame, int rawformat,
- unsigned char *pIn0, unsigned char *pOut0)
- {
- const int fieldheight = frame->rawheight / 2;
- const int fieldpix = fieldheight * frame->rawwidth;
- const int w = frame->width;
- int x, y;
- unsigned char *pInEven, *pInOdd, *pOut;
- PDEBUG(5, "fieldheight=%d", fieldheight);
- if (frame->rawheight != frame->height) {
- err("invalid height");
- return;
- }
- if ((frame->rawwidth * 2) != frame->width) {
- err("invalid width");
- return;
- }
- /* Y */
- pInOdd = pIn0;
- pInEven = pInOdd + fieldpix;
- pOut = pOut0;
- for (y = 0; y < fieldheight; y++) {
- for (x = 0; x < frame->rawwidth; x++) {
- *pOut = *pInEven;
- *(pOut+1) = *pInEven++;
- *(pOut+w) = *pInOdd;
- *(pOut+w+1) = *pInOdd++;
- pOut += 2;
- }
- pOut += w;
- }
- if (rawformat == RAWFMT_YUV420) {
- /* U */
- pInOdd = pIn0 + fieldpix * 2;
- pInEven = pInOdd + fieldpix / 4;
- for (y = 0; y < fieldheight / 2; y++) {
- for (x = 0; x < frame->rawwidth / 2; x++) {
- *pOut = *pInEven;
- *(pOut+1) = *pInEven++;
- *(pOut+w/2) = *pInOdd;
- *(pOut+w/2+1) = *pInOdd++;
- pOut += 2;
- }
- pOut += w/2;
- }
- /* V */
- pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
- pInEven = pInOdd + fieldpix / 4;
- for (y = 0; y < fieldheight / 2; y++) {
- for (x = 0; x < frame->rawwidth / 2; x++) {
- *pOut = *pInEven;
- *(pOut+1) = *pInEven++;
- *(pOut+w/2) = *pInOdd;
- *(pOut+w/2+1) = *pInOdd++;
- pOut += 2;
- }
- pOut += w/2;
- }
- }
- }
- /* Post-processes the specified frame. This consists of:
- * 1. Decompress frame, if necessary
- * 2. Deinterlace frame and scale to proper size, if necessary
- * 3. Convert from YUV planar to destination format, if necessary
- * 4. Fix the RGB offset, if necessary
- */
- static void
- ov511_postprocess(struct usb_ov511 *ov511, struct ov511_frame *frame)
- {
- if (dumppix) {
- memset(frame->data, 0,
- MAX_DATA_SIZE(ov511->maxwidth, ov511->maxheight));
- PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
- memmove(frame->data, frame->rawdata, frame->bytes_recvd);
- return;
- }
- /* YUV400 must be handled separately */
- if (frame->format == VIDEO_PALETTE_GREY) {
- /* Deinterlace frame, if necessary */
- if (ov511->sensor == SEN_SAA7111A && frame->rawheight == 480) {
- if (frame->compressed)
- ov51x_decompress(ov511, frame, frame->rawdata,
- frame->tempdata);
- else
- yuv400raw_to_yuv400p(frame, frame->rawdata,
- frame->tempdata);
- deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
- frame->data);
- } else {
- if (frame->compressed)
- ov51x_decompress(ov511, frame, frame->rawdata,
- frame->data);
- else
- yuv400raw_to_yuv400p(frame, frame->rawdata,
- frame->data);
- }
- return;
- }
- /* Process frame->data to frame->rawdata */
- if (frame->compressed)
- ov51x_decompress(ov511, frame, frame->rawdata, frame->tempdata);
- else
- yuv420raw_to_yuv420p(frame, frame->rawdata, frame->tempdata);
- /* Deinterlace frame, if necessary */
- if (ov511->sensor == SEN_SAA7111A && frame->rawheight == 480) {
- memmove(frame->rawdata, frame->tempdata,
- MAX_RAW_DATA_SIZE(frame->width, frame->height));
- deinterlace(frame, RAWFMT_YUV420, frame->rawdata,
- frame->tempdata);
- }
- /* Frame should be (width x height) and not (rawwidth x rawheight) at
- * this point. */
- #if 0
- /* Clear output buffer for testing purposes */
- memset(frame->data, 0, MAX_DATA_SIZE(frame->width, frame->height));
- #endif
- /* Process frame->tempdata to frame->data */
- switch (frame->format) {
- case VIDEO_PALETTE_RGB565:
- yuv420p_to_rgb(frame, frame->tempdata, frame->data, 16);
- break;
- case VIDEO_PALETTE_RGB24:
- yuv420p_to_rgb(frame, frame->tempdata, frame->data, 24);
- break;
- case VIDEO_PALETTE_YUV422:
- case VIDEO_PALETTE_YUYV:
- yuv420p_to_yuv422(frame, frame->tempdata, frame->data);
- break;
- case VIDEO_PALETTE_YUV420:
- case VIDEO_PALETTE_YUV420P:
- memmove(frame->data, frame->tempdata,
- MAX_RAW_DATA_SIZE(frame->width, frame->height));
- break;
- case VIDEO_PALETTE_YUV422P:
- /* Data is converted in place, so copy it in advance */
- memmove(frame->data, frame->tempdata,
- MAX_RAW_DATA_SIZE(frame->width, frame->height));
- yuv420p_to_yuv422p(frame, frame->data);
- break;
- default:
- err("Cannot convert data to this format");
- }
- if (fix_rgb_offset)
- fixFrameRGBoffset(frame);
- }
- /**********************************************************************
- *
- * OV51x data transfer, IRQ handler
- *
- **********************************************************************/
- static int
- ov511_move_data(struct usb_ov511 *ov511, urb_t *urb)
- {
- unsigned char *cdata;
- int data_size, num, offset, i, totlen = 0;
- int aPackNum[FRAMES_PER_DESC];
- struct ov511_frame *frame;
- struct timeval *ts;
- PDEBUG(5, "Moving %d packets", urb->number_of_packets);
- data_size = ov511->packet_size - 1;
- for (i = 0; i < urb->number_of_packets; i++) {
- int n = urb->iso_frame_desc[i].actual_length;
- int st = urb->iso_frame_desc[i].status;
- urb->iso_frame_desc[i].actual_length = 0;
- urb->iso_frame_desc[i].status = 0;
- cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
- aPackNum[i] = n ? cdata[ov511->packet_size - 1] : -1;
- if (!n || ov511->curframe == -1)
- continue;
- if (st)
- PDEBUG(2, "data error: [%d] len=%d, status=%d", i, n, st);
- frame = &ov511->frame[ov511->curframe];
- /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
- * byte non-zero. The EOF packet has image width/height in the
- * 10th and 11th bytes. The 9th byte is given as follows:
- *
- * bit 7: EOF
- * 6: compression enabled
- * 5: 422/420/400 modes
- * 4: 422/420/400 modes
- * 3: 1
- * 2: snapshot button on
- * 1: snapshot frame
- * 0: even/odd field
- */
- if (printph) {
- info("packet header (%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
- cdata[ov511->packet_size - 1],
- cdata[0], cdata[1], cdata[2], cdata[3], cdata[4], cdata[5],
- cdata[6], cdata[7], cdata[8], cdata[9], cdata[10], cdata[11]);
- }
- /* Check for SOF/EOF packet */
- if ((cdata[0] | cdata[1] | cdata[2] | cdata[3] |
- cdata[4] | cdata[5] | cdata[6] | cdata[7]) ||
- (~cdata[8] & 0x08))
- goto check_middle;
- /* Frame end */
- if (cdata[8] & 0x80) {
- ts = (struct timeval *)(frame->data
- + MAX_FRAME_SIZE(ov511->maxwidth, ov511->maxheight));
- do_gettimeofday(ts);
- /* Get the actual frame size from the EOF header */
- frame->rawwidth = ((int)(cdata[9]) + 1) * 8;
- frame->rawheight = ((int)(cdata[10]) + 1) * 8;
- PDEBUG(4, "Frame end, curframe = %d, packnum=%d, hw=%d, vw=%d, recvd=%d",
- ov511->curframe,
- (int)(cdata[ov511->packet_size - 1]),
- frame->rawwidth,
- frame->rawheight,
- frame->bytes_recvd);
- /* Validate the header data */
- RESTRICT_TO_RANGE(frame->rawwidth, ov511->minwidth, ov511->maxwidth);
- RESTRICT_TO_RANGE(frame->rawheight, ov511->minheight, ov511->maxheight);
- /* Don't allow byte count to exceed buffer size */
- RESTRICT_TO_RANGE(frame->bytes_recvd,
- 8,
- MAX_RAW_DATA_SIZE(ov511->maxwidth,
- ov511->maxheight));
- if (frame->scanstate == STATE_LINES) {
- int iFrameNext;
- frame->grabstate = FRAME_DONE; // FIXME: Is this right?
- if (waitqueue_active(&frame->wq)) {
- frame->grabstate = FRAME_DONE;
- wake_up_interruptible(&frame->wq);
- }
- /* If next frame is ready or grabbing,
- * point to it */
- iFrameNext = (ov511->curframe + 1) % OV511_NUMFRAMES;
- if (ov511->frame[iFrameNext].grabstate == FRAME_READY
- || ov511->frame[iFrameNext].grabstate == FRAME_GRABBING) {
- ov511->curframe = iFrameNext;
- ov511->frame[iFrameNext].scanstate = STATE_SCANNING;
- } else {
- if (frame->grabstate == FRAME_DONE) {
- PDEBUG(4, "Frame done! congratulations");
- } else {
- PDEBUG(4, "Frame not ready? state = %d",
- ov511->frame[iFrameNext].grabstate);
- }
- ov511->curframe = -1;
- }
- } else {
- PDEBUG(5, "Frame done, but not scanning");
- }
- /* Image corruption caused by misplaced frame->segment = 0
- * fixed by carlosf@conectiva.com.br
- */
- } else {
- /* Frame start */
- PDEBUG(4, "Frame start, framenum = %d", ov511->curframe);
- /* Check to see if it's a snapshot frame */
- /* FIXME?? Should the snapshot reset go here? Performance? */
- if (cdata[8] & 0x02) {
- frame->snapshot = 1;
- PDEBUG(3, "snapshot detected");
- }
- frame->scanstate = STATE_LINES;
- frame->bytes_recvd = 0;
- frame->compressed = cdata[8] & 0x40;
- }
- check_middle:
- /* Are we in a frame? */
- if (frame->scanstate != STATE_LINES) {
- PDEBUG(5, "Not in a frame; packet skipped");
- continue;
- }
- #if 0
- /* Skip packet if first 9 bytes are zero. These are common, so
- * we use a less expensive test here instead of later */
- if (frame->compressed) {
- int b, skip = 1;
- for (b = 0; b < 9; b++) {
- if (cdata[b])
- skip=0;
- }
- if (skip) {
- PDEBUG(5, "Skipping packet (all zero)");
- continue;
- }
- }
- #endif
- /* If frame start, skip header */
- if (frame->bytes_recvd == 0)
- offset = 9;
- else
- offset = 0;
- num = n - offset - 1;
- /* Dump all data exactly as received */
- if (dumppix == 2) {
- frame->bytes_recvd += n - 1;
- if (frame->bytes_recvd <= MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight))
- memmove(frame->rawdata + frame->bytes_recvd - (n - 1),
- &cdata[0], n - 1);
- else
- PDEBUG(3, "Raw data buffer overrun!! (%d)",
- frame->bytes_recvd
- - MAX_RAW_DATA_SIZE(ov511->maxwidth,
- ov511->maxheight));
- } else if (!frame->compressed && !remove_zeros) {
- frame->bytes_recvd += num;
- if (frame->bytes_recvd <= MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight))
- memmove(frame->rawdata + frame->bytes_recvd - num,
- &cdata[offset], num);
- else
- PDEBUG(3, "Raw data buffer overrun!! (%d)",
- frame->bytes_recvd
- - MAX_RAW_DATA_SIZE(ov511->maxwidth,
- ov511->maxheight));
- } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
- int b, in = 0, allzero, copied=0;
- if (offset) {
- frame->bytes_recvd += 32 - offset; // Bytes out
- memmove(frame->rawdata,
- &cdata[offset], 32 - offset);
- in += 32;
- }
- while (in < n - 1) {
- allzero = 1;
- for (b = 0; b < 32; b++) {
- if (cdata[in + b]) {
- allzero = 0;
- break;
- }
- }
- if (allzero) {
- /* Don't copy it */
- } else {
- if (frame->bytes_recvd + copied + 32
- <= MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight)) {
- memmove(frame->rawdata + frame->bytes_recvd + copied,
- &cdata[in], 32);
- copied += 32;
- } else {
- PDEBUG(3, "Raw data buffer overrun!!");
- }
- }
- in += 32;
- }
- frame->bytes_recvd += copied;
- }
- }
- PDEBUG(5, "pn: %d %d %d %d %d %d %d %d %d %d",
- aPackNum[0], aPackNum[1], aPackNum[2], aPackNum[3], aPackNum[4],
- aPackNum[5],aPackNum[6], aPackNum[7], aPackNum[8], aPackNum[9]);
- return totlen;
- }
- static int
- ov518_move_data(struct usb_ov511 *ov511, urb_t *urb)
- {
- unsigned char *cdata;
- int i, data_size, totlen = 0;
- struct ov511_frame *frame;
- struct timeval *ts;
- PDEBUG(5, "Moving %d packets", urb->number_of_packets);
- /* OV518(+) has no packet numbering */
- data_size = ov511->packet_size;
- for (i = 0; i < urb->number_of_packets; i++) {
- int n = urb->iso_frame_desc[i].actual_length;
- int st = urb->iso_frame_desc[i].status;
- urb->iso_frame_desc[i].actual_length = 0;
- urb->iso_frame_desc[i].status = 0;
- cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
- if (!n) {
- PDEBUG(4, "Zero-length packet");
- continue;
- }
- if (ov511->curframe == -1) {
- PDEBUG(4, "No frame currently active");
- continue;
- }
- if (st)
- PDEBUG(2, "data error: [%d] len=%d, status=%d", i, n, st);
- frame = &ov511->frame[ov511->curframe];
- #if 0
- {
- int d;
- /* Print all data */
- for (d = 0; d <= data_size - 16; d += 16) {
- info("%4x: %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x", d,
- cdata[d], cdata[d+1], cdata[d+2], cdata[d+3],
- cdata[d+4], cdata[d+5], cdata[d+6], cdata[d+7],
- cdata[d+8], cdata[d+9], cdata[d+10], cdata[d+11],
- cdata[d+12], cdata[d+13], cdata[d+14], cdata[d+15]);
- }
- }
- #endif
- if (printph) {
- info("packet header: %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
- cdata[0], cdata[1], cdata[2], cdata[3], cdata[4], cdata[5],
- cdata[6], cdata[7], cdata[8], cdata[9], cdata[10], cdata[11]);
- }
- /* A false positive here is likely, until OVT gives me
- * the definitive SOF/EOF format */
- if ((!(cdata[0] | cdata[1] | cdata[2] | cdata[3] |
- cdata[5])) && cdata[6]) {
-
- if (frame->scanstate == STATE_LINES) {
- PDEBUG(4, "Detected frame end/start");
- goto eof;
- } else { //scanstate == STATE_SCANNING
- /* Frame start */
- PDEBUG(4, "Frame start, framenum = %d", ov511->curframe);
- goto sof;
- }
- } else {
- goto check_middle;
- }
-
- eof:
- ts = (struct timeval *)(frame->data
- + MAX_FRAME_SIZE(ov511->maxwidth, ov511->maxheight));
- do_gettimeofday(ts);
- PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
- ov511->curframe,
- (int)(cdata[9]), (int)(cdata[10]), frame->bytes_recvd);
- // FIXME: Since we don't know the header formats yet,
- // there is no way to know what the actual image size is
- frame->rawwidth = frame->width;
- frame->rawheight = frame->height;
- /* Validate the header data */
- RESTRICT_TO_RANGE(frame->rawwidth, ov511->minwidth, ov511->maxwidth);
- RESTRICT_TO_RANGE(frame->rawheight, ov511->minheight, ov511->maxheight);
- /* Don't allow byte count to exceed buffer size */
- RESTRICT_TO_RANGE(frame->bytes_recvd,
- 8,
- MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight));
- if (frame->scanstate == STATE_LINES) {
- int iFrameNext;
- frame->grabstate = FRAME_DONE; // FIXME: Is this right?
- if (waitqueue_active(&frame->wq)) {
- frame->grabstate = FRAME_DONE;
- wake_up_interruptible(&frame->wq);
- }
- /* If next frame is ready or grabbing,
- * point to it */
- iFrameNext = (ov511->curframe + 1) % OV511_NUMFRAMES;
- if (ov511->frame[iFrameNext].grabstate == FRAME_READY
- || ov511->frame[iFrameNext].grabstate == FRAME_GRABBING) {
- ov511->curframe = iFrameNext;
- ov511->frame[iFrameNext].scanstate = STATE_SCANNING;
- frame = &ov511->frame[iFrameNext];
- } else {
- if (frame->grabstate == FRAME_DONE) {
- PDEBUG(4, "Frame done! congratulations");
- } else {
- PDEBUG(4, "Frame not ready? state = %d",
- ov511->frame[iFrameNext].grabstate);
- }
- ov511->curframe = -1;
- PDEBUG(4, "SOF dropped (no active frame)");
- continue; /* Nowhere to store this frame */
- }
- }
- /* Image corruption caused by misplaced frame->segment = 0
- * fixed by carlosf@conectiva.com.br
- */
- sof:
- PDEBUG(4, "Starting capture on frame %d", frame->framenum);
- // Snapshot not reverse-engineered yet.
- #if 0
- /* Check to see if it's a snapshot frame */
- /* FIXME?? Should the snapshot reset go here? Performance? */
- if (cdata[8] & 0x02) {
- frame->snapshot = 1;
- PDEBUG(3, "snapshot detected");
- }
- #endif
- frame->scanstate = STATE_LINES;
- frame->bytes_recvd = 0;
- // frame->compressed = 1;
- check_middle:
- /* Are we in a frame? */
- if (frame->scanstate != STATE_LINES) {
- PDEBUG(4, "scanstate: no SOF yet");
- continue;
- }
- /* Dump all data exactly as received */
- if (dumppix == 2) {
- frame->bytes_recvd += n;
- if (frame->bytes_recvd <= MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight))
- memmove(frame->rawdata + frame->bytes_recvd - n,
- &cdata[0], n);
- else
- PDEBUG(3, "Raw data buffer overrun!! (%d)",
- frame->bytes_recvd
- - MAX_RAW_DATA_SIZE(ov511->maxwidth,
- ov511->maxheight));
- } else {
- /* All incoming data are divided into 8-byte segments. If the
- * segment contains all zero bytes, it must be skipped. These
- * zero-segments allow the OV518 to mainain a constant data rate
- * regardless of the effectiveness of the compression. Segments
- * are aligned relative to the beginning of each isochronous
- * packet. The first segment is a header.
- */
- int b, in = 0, allzero, copied=0;
- // Decompressor expects the header
- #if 0
- if (frame->bytes_recvd == 0)
- in += 8; /* Skip header */
- #endif
- while (in < n) {
- allzero = 1;
- for (b = 0; b < 8; b++) {
- if (cdata[in + b]) {
- allzero = 0;
- break;
- }
- }
- if (allzero) {
- /* Don't copy it */
- } else {
- if (frame->bytes_recvd + copied + 8
- <= MAX_RAW_DATA_SIZE(ov511->maxwidth, ov511->maxheight)) {
- memmove(frame->rawdata + frame->bytes_recvd + copied,
- &cdata[in], 8);
- copied += 8;
- } else {
- PDEBUG(3, "Raw data buffer overrun!!");
- }
- }
- in += 8;
- }
- frame->bytes_recvd += copied;
- }
- }
- return totlen;
- }
- static void
- ov511_isoc_irq(struct urb *urb)
- {
- int len;
- struct usb_ov511 *ov511;
- if (!urb->context) {
- PDEBUG(4, "no context");
- return;
- }
- ov511 = (struct usb_ov511 *) urb->context;
- if (!ov511->dev || !ov511->user) {
- PDEBUG(4, "no device, or not open");
- return;
- }
- if (!ov511->streaming) {
- PDEBUG(4, "hmmm... not streaming, but got interrupt");
- return;
- }
- /* Copy the data received into our frame buffer */
- if (ov511->curframe >= 0) {
- if (ov511->bridge == BRG_OV511 ||
- ov511->bridge == BRG_OV511PLUS)
- len = ov511_move_data(ov511, urb);
- else if (ov511->bridge == BRG_OV518 ||
- ov511->bridge == BRG_OV518PLUS)
- len = ov518_move_data(ov511, urb);
- else
- err("Unknown bridge device (%d)", ov511->bridge);
- } else if (waitqueue_active(&ov511->wq)) {
- wake_up_interruptible(&ov511->wq);
- }
- urb->dev = ov511->dev;
- return;
- }
- /****************************************************************************
- *
- * Stream initialization and termination
- *
- ***************************************************************************/
- static int
- ov511_init_isoc(struct usb_ov511 *ov511)
- {
- urb_t *urb;
- int fx, err, n, size;
- PDEBUG(3, "*** Initializing capture ***");
- ov511->curframe = -1;
- if (ov511->bridge == BRG_OV511) {
- if (cams == 1) size = 993;
- else if (cams == 2) size = 513;
- else if (cams == 3 || cams == 4) size = 257;
- else {
- err(""cams" parameter too high!");
- return -1;
- }
- } else if (ov511->bridge == BRG_OV511PLUS) {
- if (cams == 1) size = 961;
- else if (cams == 2) size = 513;
- else if (cams == 3 || cams == 4) size = 257;
- else if (cams >= 5 && cams <= 8) size = 129;
- else if (cams >= 9 && cams <= 31) size = 33;
- else {
- err(""cams" parameter too high!");
- return -1;
- }
- } else if (ov511->bridge == BRG_OV518 ||
- ov511->bridge == BRG_OV518PLUS) {
- if (cams == 1) size = 896;
- else if (cams == 2) size = 512;
- else if (cams == 3 || cams == 4) size = 256;
- else if (cams >= 5 && cams <= 8) size = 128;
- else {
- err(""cams" parameter too high!");
- return -1;
- }
- } else {
- err("invalid bridge type");
- return -1;
- }
- if (packetsize == -1) {
- // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
- if (ov511->bridge == BRG_OV518 ||
- ov511->bridge == BRG_OV518PLUS)
- ov511_set_packet_size(ov511, 640);
- else
- ov511_set_packet_size(ov511, size);
- } else {
- info("Forcing packet size to %d", packetsize);
- ov511_set_packet_size(ov511, packetsize);
- }
- for (n = 0; n < OV511_NUMSBUF; n++) {
- urb = usb_alloc_urb(FRAMES_PER_DESC);
-
- if (!urb) {
- err("init isoc: usb_alloc_urb ret. NULL");
- return -ENOMEM;
- }
- ov511->sbuf[n].urb = urb;
- urb->dev = ov511->dev;
- urb->context = ov511;
- urb->pipe = usb_rcvisocpipe(ov511->dev, OV511_ENDPOINT_ADDRESS);
- urb->transfer_flags = USB_ISO_ASAP;
- urb->transfer_buffer = ov511->sbuf[n].data;
- urb->complete = ov511_isoc_irq;
- urb->number_of_packets = FRAMES_PER_DESC;
- urb->transfer_buffer_length =
- ov511->packet_size * FRAMES_PER_DESC;
- for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
- urb->iso_frame_desc[fx].offset =
- ov511->packet_size * fx;
- urb->iso_frame_desc[fx].length = ov511->packet_size;
- }
- }
- ov511->streaming = 1;
- ov511->sbuf[OV511_NUMSBUF - 1].urb->next = ov511->sbuf[0].urb;
- for (n = 0; n < OV511_NUMSBUF - 1; n++)
- ov511->sbuf[n].urb->next = ov511->sbuf[n+1].urb;
- for (n = 0; n < OV511_NUMSBUF; n++) {
- ov511->sbuf[n].urb->dev = ov511->dev;
- err = usb_submit_urb(ov511->sbuf[n].urb);
- if (err)
- err("init isoc: usb_submit_urb(%d) ret %d", n, err);
- }
- return 0;
- }
- static void
- ov511_stop_isoc(struct usb_ov511 *ov511)
- {
- int n;
- if (!ov511->streaming || !ov511->dev)
- return;
- PDEBUG(3, "*** Stopping capture ***");
- ov511_set_packet_size(ov511, 0);
- ov511->streaming = 0;
- /* Unschedule all of the iso td's */
- for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
- if (ov511->sbuf[n].urb) {
- ov511->sbuf[n].urb->next = NULL;
- usb_unlink_urb(ov511->sbuf[n].urb);
- usb_free_urb(ov511->sbuf[n].urb);
- ov511->sbuf[n].urb = NULL;
- }
- }
- }
- static int
- ov511_new_frame(struct usb_ov511 *ov511, int framenum)
- {
- struct ov511_frame *frame;
- int newnum;
- PDEBUG(4, "ov511->curframe = %d, framenum = %d", ov511->curframe,
- framenum);
- if (!ov511->dev)
- return -1;
- /* If we're not grabbing a frame right now and the other frame is */
- /* ready to be grabbed into, then use it instead */
- if (ov511->curframe == -1) {
- newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
- if (ov511->frame[newnum].grabstate == FRAME_READY)
- framenum = newnum;
- } else
- return 0;
- frame = &ov511->frame[framenum];
- PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
- frame->width, frame->height);
- frame->grabstate = FRAME_GRABBING;
- frame->scanstate = STATE_SCANNING;
- frame->snapshot = 0;
- ov511->curframe = framenum;
- /* Make sure it's not too big */
- if (frame->width > ov511->maxwidth)
- frame->width = ov511->maxwidth;
- frame->width &= ~7L; /* Multiple of 8 */
- if (frame->height > ov511->maxheight)
- frame->height = ov511->maxheight;
- frame->height &= ~3L; /* Multiple of 4 */
- return 0;
- }
- /****************************************************************************
- *
- * Buffer management
- *
- ***************************************************************************/
- static int
- ov511_alloc(struct usb_ov511 *ov511)
- {
- int i;
- int w = ov511->maxwidth;
- int h = ov511->maxheight;
- PDEBUG(4, "entered");
- down(&ov511->buf_lock);
- if (ov511->buf_state == BUF_PEND_DEALLOC) {
- ov511->buf_state = BUF_ALLOCATED;
- del_timer(&ov511->buf_timer);
- }
- if (ov511->buf_state == BUF_ALLOCATED)
- goto out;
- ov511->fbuf = rvmalloc(OV511_NUMFRAMES * MAX_DATA_SIZE(w, h));
- if (!ov511->fbuf)
- goto error;
- ov511->rawfbuf = vmalloc(OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h));
- if (!ov511->rawfbuf) {
- rvfree(ov511->fbuf, OV511_NUMFRAMES * MAX_DATA_SIZE(w, h));
- ov511->fbuf = NULL;
- goto error;
- }
- memset(ov511->rawfbuf, 0, OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h));
- ov511->tempfbuf = vmalloc(OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h));
- if (!ov511->tempfbuf) {
- vfree(ov511->rawfbuf);
- ov511->rawfbuf = NULL;
- rvfree(ov511->fbuf, OV511_NUMFRAMES * MAX_DATA_SIZE(w, h));
- ov511->fbuf = NULL;
- goto error;
- }
- memset(ov511->tempfbuf, 0, OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h));
- for (i = 0; i < OV511_NUMSBUF; i++) {
- ov511->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
- MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
- if (!ov511->sbuf[i].data) {
- while (--i) {
- kfree(ov511->sbuf[i].data);
- ov511->sbuf[i].data = NULL;
- }
- vfree(ov511->tempfbuf);
- ov511->tempfbuf = NULL;
- vfree(ov511->rawfbuf);
- ov511->rawfbuf = NULL;
- rvfree(ov511->fbuf,
- OV511_NUMFRAMES * MAX_DATA_SIZE(w, h));
- ov511->fbuf = NULL;
- goto error;
- }
- PDEBUG(4, "sbuf[%d] @ %p", i, ov511->sbuf[i].data);
- }
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].data = ov511->fbuf + i * MAX_DATA_SIZE(w, h);
- ov511->frame[i].rawdata = ov511->rawfbuf
- + i * MAX_RAW_DATA_SIZE(w, h);
- ov511->frame[i].tempdata = ov511->tempfbuf
- + i * MAX_RAW_DATA_SIZE(w, h);
- PDEBUG(4, "frame[%d] @ %p", i, ov511->frame[i].data);
- }
- ov511->buf_state = BUF_ALLOCATED;
- out:
- up(&ov511->buf_lock);
- PDEBUG(4, "leaving");
- return 0;
- error:
- ov511->buf_state = BUF_NOT_ALLOCATED;
- up(&ov511->buf_lock);
- PDEBUG(4, "errored");
- return -ENOMEM;
- }
- /*
- * - You must acquire buf_lock before entering this function.
- * - Because this code will free any non-null pointer, you must be sure to null
- * them if you explicitly free them somewhere else!
- */
- static void
- ov511_do_dealloc(struct usb_ov511 *ov511)
- {
- int i;
- PDEBUG(4, "entered");
- if (ov511->fbuf) {
- rvfree(ov511->fbuf, OV511_NUMFRAMES
- * MAX_DATA_SIZE(ov511->maxwidth, ov511->maxheight));
- ov511->fbuf = NULL;
- }
- if (ov511->rawfbuf) {
- vfree(ov511->rawfbuf);
- ov511->rawfbuf = NULL;
- }
- if (ov511->tempfbuf) {
- vfree(ov511->tempfbuf);
- ov511->tempfbuf = NULL;
- }
- for (i = 0; i < OV511_NUMSBUF; i++) {
- if (ov511->sbuf[i].data) {
- kfree(ov511->sbuf[i].data);
- ov511->sbuf[i].data = NULL;
- }
- }
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].data = NULL;
- ov511->frame[i].rawdata = NULL;
- ov511->frame[i].tempdata = NULL;
- }
- PDEBUG(4, "buffer memory deallocated");
- ov511->buf_state = BUF_NOT_ALLOCATED;
- PDEBUG(4, "leaving");
- }
- static void
- ov511_buf_callback(unsigned long data)
- {
- struct usb_ov511 *ov511 = (struct usb_ov511 *)data;
- PDEBUG(4, "entered");
- down(&ov511->buf_lock);
- if (ov511->buf_state == BUF_PEND_DEALLOC)
- ov511_do_dealloc(ov511);
- up(&ov511->buf_lock);
- PDEBUG(4, "leaving");
- }
- static void
- ov511_dealloc(struct usb_ov511 *ov511, int now)
- {
- struct timer_list *bt = &(ov511->buf_timer);
- PDEBUG(4, "entered");
- down(&ov511->buf_lock);
- PDEBUG(4, "deallocating buffer memory %s", now ? "now" : "later");
- if (ov511->buf_state == BUF_PEND_DEALLOC) {
- ov511->buf_state = BUF_ALLOCATED;
- del_timer(bt);
- }
- if (now)
- ov511_do_dealloc(ov511);
- else {
- ov511->buf_state = BUF_PEND_DEALLOC;
- init_timer(bt);
- bt->function = ov511_buf_callback;
- bt->data = (unsigned long)ov511;
- bt->expires = jiffies + buf_timeout * HZ;
- add_timer(bt);
- }
- up(&ov511->buf_lock);
- PDEBUG(4, "leaving");
- }
- /****************************************************************************
- *
- * V4L API
- *
- ***************************************************************************/
- static int
- ov511_open(struct video_device *vdev, int flags)
- {
- struct usb_ov511 *ov511 = vdev->priv;
- int err, i;
- PDEBUG(4, "opening");
- down(&ov511->lock);
- err = -EBUSY;
- if (ov511->user)
- goto out;
- err = -ENOMEM;
- if (ov511_alloc(ov511))
- goto out;
- ov511->sub_flag = 0;
- /* In case app doesn't set them... */
- if (ov51x_set_default_params(ov511) < 0)
- goto out;
- /* Make sure frames are reset */
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].grabstate = FRAME_UNUSED;
- ov511->frame[i].bytes_read = 0;
- }
- /* If compression is on, make sure now that a
- * decompressor can be loaded */
- if (ov511->compress && !ov511->decomp_ops) {
- err = ov51x_request_decompressor(ov511);
- if (err)
- goto out;
- }
- err = ov511_init_isoc(ov511);
- if (err) {
- ov511_dealloc(ov511, 0);
- goto out;
- }
- ov511->user++;
-
- if (ov511->led_policy == LED_AUTO)
- ov51x_led_control(ov511, 1);
- out:
- up(&ov511->lock);
- return err;
- }
- static void
- ov511_close(struct video_device *dev)
- {
- struct usb_ov511 *ov511 = (struct usb_ov511 *)dev;
- PDEBUG(4, "ov511_close");
-
- down(&ov511->lock);
- ov511->user--;
- ov511_stop_isoc(ov511);
- ov51x_release_decompressor(ov511);
- if (ov511->led_policy == LED_AUTO)
- ov51x_led_control(ov511, 0);
- if (ov511->dev)
- ov511_dealloc(ov511, 0);
- up(&ov511->lock);
- /* Device unplugged while open. Only a minimum of unregistration is done
- * here; the disconnect callback already did the rest. */
- if (!ov511->dev) {
- ov511_dealloc(ov511, 1);
- video_unregister_device(&ov511->vdev);
- kfree(ov511);
- ov511 = NULL;
- }
- }
- static int
- ov511_init_done(struct video_device *vdev)
- {
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- create_proc_ov511_cam((struct usb_ov511 *)vdev);
- #endif
- return 0;
- }
- static long
- ov511_write(struct video_device *vdev, const char *buf,
- unsigned long count, int noblock)
- {
- return -EINVAL;
- }
- /* Do not call this function directly! */
- static int
- ov511_ioctl_internal(struct video_device *vdev, unsigned int cmd, void *arg)
- {
- struct usb_ov511 *ov511 = (struct usb_ov511 *)vdev;
- PDEBUG(5, "IOCtl: 0x%X", cmd);
- if (!ov511->dev)
- return -EIO;
- switch (cmd) {
- case VIDIOCGCAP:
- {
- struct video_capability b;
- PDEBUG(4, "VIDIOCGCAP");
- memset(&b, 0, sizeof(b));
- sprintf(b.name, "%s USB Camera",
- ov511->bridge == BRG_OV511 ? "OV511" :
- ov511->bridge == BRG_OV511PLUS ? "OV511+" :
- ov511->bridge == BRG_OV518 ? "OV518" :
- ov511->bridge == BRG_OV518PLUS ? "OV518+" :
- "unknown");
- b.type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
- if (ov511->has_tuner)
- b.type |= VID_TYPE_TUNER;
- b.channels = ov511->num_inputs;
- b.audios = ov511->has_audio_proc ? 1:0;
- b.maxwidth = ov511->maxwidth;
- b.maxheight = ov511->maxheight;
- b.minwidth = ov511->minwidth;
- b.minheight = ov511->minheight;
- if (copy_to_user(arg, &b, sizeof(b)))
- return -EFAULT;
-
- return 0;
- }
- case VIDIOCGCHAN:
- {
- struct video_channel v;
- PDEBUG(4, "VIDIOCGCHAN");
- if (copy_from_user(&v, arg, sizeof(v)))
- return -EFAULT;
- if ((unsigned)(v.channel) >= ov511->num_inputs) {
- err("Invalid channel (%d)", v.channel);
- return -EINVAL;
- }
- v.norm = ov511->norm;
- v.type = (ov511->has_tuner) ? VIDEO_TYPE_TV : VIDEO_TYPE_CAMERA;
- v.flags = (ov511->has_tuner) ? VIDEO_VC_TUNER : 0;
- v.flags |= (ov511->has_audio_proc) ? VIDEO_VC_AUDIO : 0;
- // v.flags |= (ov511->has_decoder) ? VIDEO_VC_NORM : 0;
- v.tuners = (ov511->has_tuner) ? 1:0;
- decoder_get_input_name(ov511, v.channel, v.name);
- if (copy_to_user(arg, &v, sizeof(v)))
- return -EFAULT;
-
- return 0;
- }
- case VIDIOCSCHAN:
- {
- struct video_channel v;
- int err;
- PDEBUG(4, "VIDIOCSCHAN");
- if (copy_from_user(&v, arg, sizeof(v)))
- return -EFAULT;
- /* Make sure it's not a camera */
- if (!ov511->has_decoder) {
- if (v.channel == 0)
- return 0;
- else
- return -EINVAL;
- }
- if (v.norm != VIDEO_MODE_PAL &&
- v.norm != VIDEO_MODE_NTSC &&
- v.norm != VIDEO_MODE_SECAM &&
- v.norm != VIDEO_MODE_AUTO) {
- err("Invalid norm (%d)", v.norm);
- return -EINVAL;
- }
- if ((unsigned)(v.channel) >= ov511->num_inputs) {
- err("Invalid channel (%d)", v.channel);
- return -EINVAL;
- }
- err = decoder_set_input(ov511, v.channel);
- if (err)
- return err;
- err = decoder_set_norm(ov511, v.norm);
- if (err)
- return err;
- return 0;
- }
- case VIDIOCGPICT:
- {
- struct video_picture p;
- PDEBUG(4, "VIDIOCGPICT");
- memset(&p, 0, sizeof(p));
- if (sensor_get_picture(ov511, &p))
- return -EIO;
- if (copy_to_user(arg, &p, sizeof(p)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCSPICT:
- {
- struct video_picture p;
- int i;
- PDEBUG(4, "VIDIOCSPICT");
- if (copy_from_user(&p, arg, sizeof(p)))
- return -EFAULT;
- if (!ov511_get_depth(p.palette))
- return -EINVAL;
- if (sensor_set_picture(ov511, &p))
- return -EIO;
- if (force_palette && p.palette != force_palette) {
- info("Palette rejected (%d)", p.palette);
- return -EINVAL;
- }
- // FIXME: Format should be independent of frames
- if (p.palette != ov511->frame[0].format) {
- PDEBUG(4, "Detected format change");
- /* If we're collecting previous frame wait
- before changing modes */
- interruptible_sleep_on(&ov511->wq);
- if (signal_pending(current)) return -EINTR;
- mode_init_regs(ov511, ov511->frame[0].width,
- ov511->frame[0].height, p.palette,
- ov511->sub_flag);
- }
- PDEBUG(4, "Setting depth=%d, palette=%d", p.depth, p.palette);
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].depth = p.depth;
- ov511->frame[i].format = p.palette;
- }
- return 0;
- }
- case VIDIOCGCAPTURE:
- {
- int vf;
- PDEBUG(4, "VIDIOCGCAPTURE");
- if (copy_from_user(&vf, arg, sizeof(vf)))
- return -EFAULT;
- ov511->sub_flag = vf;
- return 0;
- }
- case VIDIOCSCAPTURE:
- {
- struct video_capture vc;
- PDEBUG(4, "VIDIOCSCAPTURE");
- if (copy_from_user(&vc, arg, sizeof(vc)))
- return -EFAULT;
- if (vc.flags)
- return -EINVAL;
- if (vc.decimation)
- return -EINVAL;
- vc.x &= ~3L;
- vc.y &= ~1L;
- vc.y &= ~31L;
- if (vc.width == 0)
- vc.width = 32;
- vc.height /= 16;
- vc.height *= 16;
- if (vc.height == 0)
- vc.height = 16;
- ov511->subx = vc.x;
- ov511->suby = vc.y;
- ov511->subw = vc.width;
- ov511->subh = vc.height;
- return 0;
- }
- case VIDIOCSWIN:
- {
- struct video_window vw;
- int i, result;
- if (copy_from_user(&vw, arg, sizeof(vw)))
- return -EFAULT;
- PDEBUG(4, "VIDIOCSWIN: width=%d, height=%d",
- vw.width, vw.height);
- #if 0
- if (vw.flags)
- return -EINVAL;
- if (vw.clipcount)
- return -EINVAL;
- if (vw.height != ov511->maxheight)
- return -EINVAL;
- if (vw.width != ov511->maxwidth)
- return -EINVAL;
- #endif
- /* If we're collecting previous frame wait
- before changing modes */
- interruptible_sleep_on(&ov511->wq);
- if (signal_pending(current)) return -EINTR;
- result = mode_init_regs(ov511, vw.width, vw.height,
- ov511->frame[0].format, ov511->sub_flag);
- if (result < 0)
- return result;
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].width = vw.width;
- ov511->frame[i].height = vw.height;
- }
- return 0;
- }
- case VIDIOCGWIN:
- {
- struct video_window vw;
- memset(&vw, 0, sizeof(vw));
- vw.x = 0; /* FIXME */
- vw.y = 0;
- vw.width = ov511->frame[0].width;
- vw.height = ov511->frame[0].height;
- vw.flags = 30;
- PDEBUG(4, "VIDIOCGWIN: %dx%d", vw.width, vw.height);
- if (copy_to_user(arg, &vw, sizeof(vw)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCGMBUF:
- {
- struct video_mbuf vm;
- int i;
- PDEBUG(4, "VIDIOCGMBUF");
- memset(&vm, 0, sizeof(vm));
- vm.size = OV511_NUMFRAMES
- * MAX_DATA_SIZE(ov511->maxwidth, ov511->maxheight);
- vm.frames = OV511_NUMFRAMES;
- vm.offsets[0] = 0;
- for (i = 1; i < OV511_NUMFRAMES; i++) {
- vm.offsets[i] = vm.offsets[i-1]
- + MAX_DATA_SIZE(ov511->maxwidth, ov511->maxheight);
- }
- if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCMCAPTURE:
- {
- struct video_mmap vm;
- int ret, depth;
- if (copy_from_user((void *)&vm, (void *)arg, sizeof(vm)))
- return -EFAULT;
- PDEBUG(4, "CMCAPTURE");
- PDEBUG(4, "frame: %d, size: %dx%d, format: %d",
- vm.frame, vm.width, vm.height, vm.format);
- depth = ov511_get_depth(vm.format);
- if (!depth) {
- err("VIDIOCMCAPTURE: invalid format (%d)", vm.format);
- return -EINVAL;
- }
- if ((unsigned)vm.frame >= OV511_NUMFRAMES) {
- err("VIDIOCMCAPTURE: invalid frame (%d)", vm.frame);
- return -EINVAL;
- }
- if (vm.width > ov511->maxwidth
- || vm.height > ov511->maxheight) {
- err("VIDIOCMCAPTURE: requested dimensions too big");
- return -EINVAL;
- }
- if (ov511->frame[vm.frame].grabstate == FRAME_GRABBING) {
- PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
- return -EBUSY;
- }
- if (force_palette && vm.format != force_palette) {
- info("palette rejected (%d)", vm.format);
- return -EINVAL;
- }
- if ((ov511->frame[vm.frame].width != vm.width) ||
- (ov511->frame[vm.frame].height != vm.height) ||
- (ov511->frame[vm.frame].format != vm.format) ||
- (ov511->frame[vm.frame].sub_flag != ov511->sub_flag) ||
- (ov511->frame[vm.frame].depth != depth)) {
- PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
- /* If we're collecting previous frame wait
- before changing modes */
- interruptible_sleep_on(&ov511->wq);
- if (signal_pending(current)) return -EINTR;
- ret = mode_init_regs(ov511, vm.width, vm.height,
- vm.format, ov511->sub_flag);
- #if 0
- if (ret < 0) {
- PDEBUG(1, "Got error while initializing regs ");
- return ret;
- }
- #endif
- ov511->frame[vm.frame].width = vm.width;
- ov511->frame[vm.frame].height = vm.height;
- ov511->frame[vm.frame].format = vm.format;
- ov511->frame[vm.frame].sub_flag = ov511->sub_flag;
- ov511->frame[vm.frame].depth = depth;
- }
- /* Mark it as ready */
- ov511->frame[vm.frame].grabstate = FRAME_READY;
- PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", vm.frame);
- return ov511_new_frame(ov511, vm.frame);
- }
- case VIDIOCSYNC:
- {
- int fnum, rc;
- struct ov511_frame *frame;
- if (copy_from_user((void *)&fnum, arg, sizeof(int)))
- return -EFAULT;
- if ((unsigned)fnum >= OV511_NUMFRAMES) {
- err("VIDIOCSYNC: invalid frame (%d)", fnum);
- return -EINVAL;
- }
- frame = &ov511->frame[fnum];
- PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
- frame->grabstate);
- switch (frame->grabstate) {
- case FRAME_UNUSED:
- return -EINVAL;
- case FRAME_READY:
- case FRAME_GRABBING:
- case FRAME_ERROR:
- redo:
- if (!ov511->dev)
- return -EIO;
- rc = wait_event_interruptible(frame->wq,
- (frame->grabstate == FRAME_DONE)
- || (frame->grabstate == FRAME_ERROR));
- if (rc)
- return rc;
- if (frame->grabstate == FRAME_ERROR) {
- int ret;
- if ((ret = ov511_new_frame(ov511, fnum)) < 0)
- return ret;
- goto redo;
- }
- /* Fall through */
- case FRAME_DONE:
- if (ov511->snap_enabled && !frame->snapshot) {
- int ret;
- if ((ret = ov511_new_frame(ov511, fnum)) < 0)
- return ret;
- goto redo;
- }
- frame->grabstate = FRAME_UNUSED;
- /* Reset the hardware snapshot button */
- /* FIXME - Is this the best place for this? */
- if ((ov511->snap_enabled) && (frame->snapshot)) {
- frame->snapshot = 0;
- ov51x_clear_snapshot(ov511);
- }
- /* Decompression, format conversion, etc... */
- ov511_postprocess(ov511, frame);
- break;
- } /* end switch */
- return 0;
- }
- case VIDIOCGFBUF:
- {
- struct video_buffer vb;
- PDEBUG(4, "VIDIOCSCHAN");
- memset(&vb, 0, sizeof(vb));
- vb.base = NULL; /* frame buffer not supported, not used */
- if (copy_to_user((void *)arg, (void *)&vb, sizeof(vb)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCGUNIT:
- {
- struct video_unit vu;
- PDEBUG(4, "VIDIOCGUNIT");
- memset(&vu, 0, sizeof(vu));
- vu.video = ov511->vdev.minor; /* Video minor */
- vu.vbi = VIDEO_NO_UNIT; /* VBI minor */
- vu.radio = VIDEO_NO_UNIT; /* Radio minor */
- vu.audio = VIDEO_NO_UNIT; /* Audio minor */
- vu.teletext = VIDEO_NO_UNIT; /* Teletext minor */
- if (copy_to_user((void *)arg, (void *)&vu, sizeof(vu)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCGTUNER:
- {
- struct video_tuner v;
- PDEBUG(4, "VIDIOCGTUNER");
- if (copy_from_user(&v, arg, sizeof(v)))
- return -EFAULT;
- if (!ov511->has_tuner || v.tuner) // Only tuner 0
- return -EINVAL;
- strcpy(v.name, "Television");
- // FIXME: Need a way to get the real values
- v.rangelow = 0;
- v.rangehigh = ~0;
- v.flags = VIDEO_TUNER_PAL | VIDEO_TUNER_NTSC |
- VIDEO_TUNER_SECAM;
- v.mode = 0; /* FIXME: Not sure what this is yet */
- v.signal = 0xFFFF; /* unknown */
- call_i2c_clients(ov511, cmd, &v);
- if (copy_to_user(arg, &v, sizeof(v)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCSTUNER:
- {
- struct video_tuner v;
- int err;
- PDEBUG(4, "VIDIOCSTUNER");
- if (copy_from_user(&v, arg, sizeof(v)))
- return -EFAULT;
- /* Only no or one tuner for now */
- if (!ov511->has_tuner || v.tuner)
- return -EINVAL;
- /* and it only has certain valid modes */
- if (v.mode != VIDEO_MODE_PAL &&
- v.mode != VIDEO_MODE_NTSC &&
- v.mode != VIDEO_MODE_SECAM) return -EOPNOTSUPP;
- /* Is this right/necessary? */
- err = decoder_set_norm(ov511, v.mode);
- if (err)
- return err;
- call_i2c_clients(ov511, cmd, &v);
- return 0;
- }
- case VIDIOCGFREQ:
- {
- unsigned long v = ov511->freq;
- PDEBUG(4, "VIDIOCGFREQ");
- if (!ov511->has_tuner)
- return -EINVAL;
- #if 0
- /* FIXME: this is necessary for testing */
- v = 46*16;
- #endif
- if (copy_to_user(arg, &v, sizeof(v)))
- return -EFAULT;
- return 0;
- }
- case VIDIOCSFREQ:
- {
- unsigned long v;
- if (!ov511->has_tuner)
- return -EINVAL;
- if (copy_from_user(&v, arg, sizeof(v)))
- return -EFAULT;
- PDEBUG(4, "VIDIOCSFREQ: %lx", v);
- ov511->freq = v;
- call_i2c_clients(ov511, cmd, &v);
- return 0;
- }
- case VIDIOCGAUDIO:
- case VIDIOCSAUDIO:
- {
- /* FIXME: Implement this... */
- return 0;
- }
- default:
- PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
- return -ENOIOCTLCMD;
- } /* end switch */
- return 0;
- }
- static int
- ov511_ioctl(struct video_device *vdev, unsigned int cmd, void *arg)
- {
- int rc;
- struct usb_ov511 *ov511 = vdev->priv;
- if (down_interruptible(&ov511->lock))
- return -EINTR;
- rc = ov511_ioctl_internal(vdev, cmd, arg);
- up(&ov511->lock);
- return rc;
- }
- static inline long
- ov511_read(struct video_device *vdev, char *buf, unsigned long count,
- int noblock)
- {
- struct usb_ov511 *ov511 = vdev->priv;
- int i, rc = 0, frmx = -1;
- struct ov511_frame *frame;
- if (down_interruptible(&ov511->lock))
- return -EINTR;
- PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
- if (!vdev || !buf) {
- rc = -EFAULT;
- goto error;
- }
- if (!ov511->dev) {
- rc = -EIO;
- goto error;
- }
- // FIXME: Only supports two frames
- /* See if a frame is completed, then use it. */
- if (ov511->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
- frmx = 0;
- else if (ov511->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
- frmx = 1;
- /* If nonblocking we return immediately */
- if (noblock && (frmx == -1)) {
- rc = -EAGAIN;
- goto error;
- }
- /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
- /* See if a frame is in process (grabbing), then use it. */
- if (frmx == -1) {
- if (ov511->frame[0].grabstate == FRAME_GRABBING)
- frmx = 0;
- else if (ov511->frame[1].grabstate == FRAME_GRABBING)
- frmx = 1;
- }
- /* If no frame is active, start one. */
- if (frmx == -1) {
- if ((rc = ov511_new_frame(ov511, frmx = 0))) {
- err("read: ov511_new_frame error");
- goto error;
- }
- }
- frame = &ov511->frame[frmx];
- restart:
- if (!ov511->dev) {
- rc = -EIO;
- goto error;
- }
- /* Wait while we're grabbing the image */
- PDEBUG(4, "Waiting image grabbing");
- rc = wait_event_interruptible(frame->wq,
- (frame->grabstate == FRAME_DONE)
- || (frame->grabstate == FRAME_ERROR));
- if (rc)
- goto error;
- PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
- PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
- if (frame->grabstate == FRAME_ERROR) {
- frame->bytes_read = 0;
- err("** ick! ** Errored frame %d", ov511->curframe);
- if (ov511_new_frame(ov511, frmx)) {
- err("read: ov511_new_frame error");
- goto error;
- }
- goto restart;
- }
- /* Repeat until we get a snapshot frame */
- if (ov511->snap_enabled)
- PDEBUG(4, "Waiting snapshot frame");
- if (ov511->snap_enabled && !frame->snapshot) {
- frame->bytes_read = 0;
- if ((rc = ov511_new_frame(ov511, frmx))) {
- err("read: ov511_new_frame error");
- goto error;
- }
- goto restart;
- }
- /* Clear the snapshot */
- if (ov511->snap_enabled && frame->snapshot) {
- frame->snapshot = 0;
- ov51x_clear_snapshot(ov511);
- }
- /* Decompression, format conversion, etc... */
- ov511_postprocess(ov511, frame);
- PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
- frame->bytes_read,
- get_frame_length(frame));
- /* copy bytes to user space; we allow for partials reads */
- // if ((count + frame->bytes_read)
- // > get_frame_length((struct ov511_frame *)frame))
- // count = frame->scanlength - frame->bytes_read;
- /* FIXME - count hardwired to be one frame... */
- count = get_frame_length(frame);
- PDEBUG(4, "Copy to user space: %ld bytes", count);
- if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
- PDEBUG(4, "Copy failed! %d bytes not copied", i);
- rc = -EFAULT;
- goto error;
- }
- frame->bytes_read += count;
- PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
- count, frame->bytes_read);
- /* If all data has been read... */
- if (frame->bytes_read
- >= get_frame_length(frame)) {
- frame->bytes_read = 0;
- // FIXME: Only supports two frames
- /* Mark it as available to be used again. */
- ov511->frame[frmx].grabstate = FRAME_UNUSED;
- if ((rc = ov511_new_frame(ov511, !frmx))) {
- err("ov511_new_frame returned error");
- goto error;
- }
- }
- PDEBUG(4, "read finished, returning %ld (sweet)", count);
- up(&ov511->lock);
- return count;
- error:
- up(&ov511->lock);
- return rc;
- }
- static int
- ov511_mmap(struct video_device *vdev, const char *adr, unsigned long size)
- {
- struct usb_ov511 *ov511 = vdev->priv;
- unsigned long start = (unsigned long)adr;
- unsigned long page, pos;
- if (ov511->dev == NULL)
- return -EIO;
- PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
- if (size > (((OV511_NUMFRAMES
- * MAX_DATA_SIZE(ov511->maxwidth, ov511->maxheight)
- + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
- return -EINVAL;
- if (down_interruptible(&ov511->lock))
- return -EINTR;
- pos = (unsigned long)ov511->fbuf;
- while (size > 0) {
- page = kvirt_to_pa(pos);
- if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED)) {
- up(&ov511->lock);
- return -EAGAIN;
- }
- start += PAGE_SIZE;
- pos += PAGE_SIZE;
- if (size > PAGE_SIZE)
- size -= PAGE_SIZE;
- else
- size = 0;
- }
- up(&ov511->lock);
- return 0;
- }
- static struct video_device ov511_template = {
- owner: THIS_MODULE,
- name: "OV511 USB Camera",
- type: VID_TYPE_CAPTURE,
- hardware: VID_HARDWARE_OV511,
- open: ov511_open,
- close: ov511_close,
- read: ov511_read,
- write: ov511_write,
- ioctl: ov511_ioctl,
- mmap: ov511_mmap,
- initialize: ov511_init_done,
- };
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- static int
- ov511_control_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
- unsigned long ularg)
- {
- struct proc_dir_entry *pde;
- struct usb_ov511 *ov511;
- void *arg = (void *) ularg;
- int rc;
- pde = (struct proc_dir_entry *) inode->u.generic_ip;
- if (!pde)
- return -ENOENT;
- ov511 = (struct usb_ov511 *) pde->data;
- if (!ov511)
- return -ENODEV;
- if (!ov511->dev)
- return -EIO;
- /* Should we pass through standard V4L IOCTLs? */
- switch (cmd) {
- case OV511IOC_GINTVER:
- {
- int ver = OV511_INTERFACE_VER;
- PDEBUG(4, "Get interface version: %d", ver);
- if (copy_to_user(arg, &ver, sizeof(ver)))
- return -EFAULT;
- return 0;
- }
- case OV511IOC_GUSHORT:
- {
- struct ov511_ushort_opt opt;
- if (copy_from_user(&opt, arg, sizeof(opt)))
- return -EFAULT;
- switch (opt.optnum) {
- case OV511_USOPT_BRIGHT:
- rc = sensor_get_brightness(ov511, &(opt.val));
- if (rc) return rc;
- break;
- case OV511_USOPT_SAT:
- rc = sensor_get_saturation(ov511, &(opt.val));
- if (rc) return rc;
- break;
- case OV511_USOPT_HUE:
- rc = sensor_get_hue(ov511, &(opt.val));
- if (rc) return rc;
- break;
- case OV511_USOPT_CONTRAST:
- rc = sensor_get_contrast(ov511, &(opt.val));
- if (rc) return rc;
- break;
- default:
- err("Invalid get short option number");
- return -EINVAL;
- }
- if (copy_to_user(arg, &opt, sizeof(opt)))
- return -EFAULT;
- return 0;
- }
- case OV511IOC_SUSHORT:
- {
- struct ov511_ushort_opt opt;
- if (copy_from_user(&opt, arg, sizeof(opt)))
- return -EFAULT;
- switch (opt.optnum) {
- case OV511_USOPT_BRIGHT:
- rc = sensor_set_brightness(ov511, opt.val);
- if (rc) return rc;
- break;
- case OV511_USOPT_SAT:
- rc = sensor_set_saturation(ov511, opt.val);
- if (rc) return rc;
- break;
- case OV511_USOPT_HUE:
- rc = sensor_set_hue(ov511, opt.val);
- if (rc) return rc;
- break;
- case OV511_USOPT_CONTRAST:
- rc = sensor_set_contrast(ov511, opt.val);
- if (rc) return rc;
- break;
- default:
- err("Invalid set short option number");
- return -EINVAL;
- }
- return 0;
- }
- case OV511IOC_GUINT:
- {
- struct ov511_uint_opt opt;
- if (copy_from_user(&opt, arg, sizeof(opt)))
- return -EFAULT;
- switch (opt.optnum) {
- case OV511_UIOPT_POWER_FREQ:
- opt.val = ov511->lightfreq;
- break;
- case OV511_UIOPT_BFILTER:
- opt.val = ov511->bandfilt;
- break;
- case OV511_UIOPT_LED:
- opt.val = ov511->led_policy;
- break;
- case OV511_UIOPT_DEBUG:
- opt.val = debug;
- break;
- case OV511_UIOPT_COMPRESS:
- opt.val = ov511->compress;
- break;
- default:
- err("Invalid get int option number");
- return -EINVAL;
- }
- if (copy_to_user(arg, &opt, sizeof(opt)))
- return -EFAULT;
- return 0;
- }
- case OV511IOC_SUINT:
- {
- struct ov511_uint_opt opt;
- if (copy_from_user(&opt, arg, sizeof(opt)))
- return -EFAULT;
- switch (opt.optnum) {
- case OV511_UIOPT_POWER_FREQ:
- rc = sensor_set_light_freq(ov511, opt.val);
- if (rc) return rc;
- break;
- case OV511_UIOPT_BFILTER:
- rc = sensor_set_banding_filter(ov511, opt.val);
- if (rc) return rc;
- break;
- case OV511_UIOPT_LED:
- if (opt.val <= 2) {
- ov511->led_policy = opt.val;
- if (ov511->led_policy == LED_OFF)
- ov51x_led_control(ov511, 0);
- else if (ov511->led_policy == LED_ON)
- ov51x_led_control(ov511, 1);
- } else {
- return -EINVAL;
- }
- break;
- case OV511_UIOPT_DEBUG:
- if (opt.val <= 5)
- debug = opt.val;
- else
- return -EINVAL;
- break;
- case OV511_UIOPT_COMPRESS:
- ov511->compress = opt.val;
- if (ov511->compress) {
- if (ov511->bridge == BRG_OV511 ||
- ov511->bridge == BRG_OV511PLUS)
- ov511_init_compression(ov511);
- else if (ov511->bridge == BRG_OV518 ||
- ov511->bridge == BRG_OV518PLUS)
- ov518_init_compression(ov511);
- }
- break;
- default:
- err("Invalid get int option number");
- return -EINVAL;
- }
- return 0;
- }
- case OV511IOC_WI2C:
- {
- struct ov511_i2c_struct w;
- if (copy_from_user(&w, arg, sizeof(w)))
- return -EFAULT;
- return ov51x_i2c_write_slave(ov511, w.slave, w.reg, w.value,
- w.mask);
- }
- case OV511IOC_RI2C:
- {
- struct ov511_i2c_struct r;
- if (copy_from_user(&r, arg, sizeof(r)))
- return -EFAULT;
- rc = ov51x_i2c_read_slave(ov511, r.slave, r.reg);
- if (rc < 0)
- return rc;
- r.value = rc;
- if (copy_to_user(arg, &r, sizeof(r)))
- return -EFAULT;
- return 0;
- }
- default:
- return -EINVAL;
- } /* end switch */
- return 0;
- }
- #endif
- /****************************************************************************
- *
- * OV511 and sensor configuration
- *
- ***************************************************************************/
- /* This initializes the OV7610, OV7620, or OV7620AE sensor. The OV7620AE uses
- * the same register settings as the OV7610, since they are very similar.
- */
- static int
- ov7xx0_configure(struct usb_ov511 *ov511)
- {
- int i, success;
- int rc;
- /* Lawrence Glaister <lg@jfm.bc.ca> reports:
- *
- * Register 0x0f in the 7610 has the following effects:
- *
- * 0x85 (AEC method 1): Best overall, good contrast range
- * 0x45 (AEC method 2): Very overexposed
- * 0xa5 (spec sheet default): Ok, but the black level is
- * shifted resulting in loss of contrast
- * 0x05 (old driver setting): very overexposed, too much
- * contrast
- */
- static struct ov511_regvals aRegvalsNorm7610[] = {
- { OV511_I2C_BUS, 0x10, 0xff },
- { OV511_I2C_BUS, 0x16, 0x06 },
- { OV511_I2C_BUS, 0x28, 0x24 },
- { OV511_I2C_BUS, 0x2b, 0xac },
- { OV511_I2C_BUS, 0x12, 0x00 },
- { OV511_I2C_BUS, 0x38, 0x81 },
- { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
- { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
- { OV511_I2C_BUS, 0x15, 0x01 },
- { OV511_I2C_BUS, 0x20, 0x1c },
- { OV511_I2C_BUS, 0x23, 0x2a },
- { OV511_I2C_BUS, 0x24, 0x10 },
- { OV511_I2C_BUS, 0x25, 0x8a },
- { OV511_I2C_BUS, 0x26, 0xa2 },
- { OV511_I2C_BUS, 0x27, 0xc2 },
- { OV511_I2C_BUS, 0x2a, 0x04 },
- { OV511_I2C_BUS, 0x2c, 0xfe },
- { OV511_I2C_BUS, 0x2d, 0x93 },
- { OV511_I2C_BUS, 0x30, 0x71 },
- { OV511_I2C_BUS, 0x31, 0x60 },
- { OV511_I2C_BUS, 0x32, 0x26 },
- { OV511_I2C_BUS, 0x33, 0x20 },
- { OV511_I2C_BUS, 0x34, 0x48 },
- { OV511_I2C_BUS, 0x12, 0x24 },
- { OV511_I2C_BUS, 0x11, 0x01 },
- { OV511_I2C_BUS, 0x0c, 0x24 },
- { OV511_I2C_BUS, 0x0d, 0x24 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- static struct ov511_regvals aRegvalsNorm7620[] = {
- { OV511_I2C_BUS, 0x00, 0x00 },
- { OV511_I2C_BUS, 0x01, 0x80 },
- { OV511_I2C_BUS, 0x02, 0x80 },
- { OV511_I2C_BUS, 0x03, 0xc0 },
- { OV511_I2C_BUS, 0x06, 0x60 },
- { OV511_I2C_BUS, 0x07, 0x00 },
- { OV511_I2C_BUS, 0x0c, 0x24 },
- { OV511_I2C_BUS, 0x0c, 0x24 },
- { OV511_I2C_BUS, 0x0d, 0x24 },
- { OV511_I2C_BUS, 0x11, 0x01 },
- { OV511_I2C_BUS, 0x12, 0x24 },
- { OV511_I2C_BUS, 0x13, 0x01 },
- { OV511_I2C_BUS, 0x14, 0x84 },
- { OV511_I2C_BUS, 0x15, 0x01 },
- { OV511_I2C_BUS, 0x16, 0x03 },
- { OV511_I2C_BUS, 0x17, 0x2f },
- { OV511_I2C_BUS, 0x18, 0xcf },
- { OV511_I2C_BUS, 0x19, 0x06 },
- { OV511_I2C_BUS, 0x1a, 0xf5 },
- { OV511_I2C_BUS, 0x1b, 0x00 },
- { OV511_I2C_BUS, 0x20, 0x18 },
- { OV511_I2C_BUS, 0x21, 0x80 },
- { OV511_I2C_BUS, 0x22, 0x80 },
- { OV511_I2C_BUS, 0x23, 0x00 },
- { OV511_I2C_BUS, 0x26, 0xa2 },
- { OV511_I2C_BUS, 0x27, 0xea },
- { OV511_I2C_BUS, 0x28, 0x20 },
- { OV511_I2C_BUS, 0x29, 0x00 },
- { OV511_I2C_BUS, 0x2a, 0x10 },
- { OV511_I2C_BUS, 0x2b, 0x00 },
- { OV511_I2C_BUS, 0x2c, 0x88 },
- { OV511_I2C_BUS, 0x2d, 0x91 },
- { OV511_I2C_BUS, 0x2e, 0x80 },
- { OV511_I2C_BUS, 0x2f, 0x44 },
- { OV511_I2C_BUS, 0x60, 0x27 },
- { OV511_I2C_BUS, 0x61, 0x02 },
- { OV511_I2C_BUS, 0x62, 0x5f },
- { OV511_I2C_BUS, 0x63, 0xd5 },
- { OV511_I2C_BUS, 0x64, 0x57 },
- { OV511_I2C_BUS, 0x65, 0x83 },
- { OV511_I2C_BUS, 0x66, 0x55 },
- { OV511_I2C_BUS, 0x67, 0x92 },
- { OV511_I2C_BUS, 0x68, 0xcf },
- { OV511_I2C_BUS, 0x69, 0x76 },
- { OV511_I2C_BUS, 0x6a, 0x22 },
- { OV511_I2C_BUS, 0x6b, 0x00 },
- { OV511_I2C_BUS, 0x6c, 0x02 },
- { OV511_I2C_BUS, 0x6d, 0x44 },
- { OV511_I2C_BUS, 0x6e, 0x80 },
- { OV511_I2C_BUS, 0x6f, 0x1d },
- { OV511_I2C_BUS, 0x70, 0x8b },
- { OV511_I2C_BUS, 0x71, 0x00 },
- { OV511_I2C_BUS, 0x72, 0x14 },
- { OV511_I2C_BUS, 0x73, 0x54 },
- { OV511_I2C_BUS, 0x74, 0x00 },
- { OV511_I2C_BUS, 0x75, 0x8e },
- { OV511_I2C_BUS, 0x76, 0x00 },
- { OV511_I2C_BUS, 0x77, 0xff },
- { OV511_I2C_BUS, 0x78, 0x80 },
- { OV511_I2C_BUS, 0x79, 0x80 },
- { OV511_I2C_BUS, 0x7a, 0x80 },
- { OV511_I2C_BUS, 0x7b, 0xe2 },
- { OV511_I2C_BUS, 0x7c, 0x00 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- PDEBUG(4, "starting configuration");
- /* This looks redundant, but is necessary for WebCam 3 */
- ov511->primary_i2c_slave = OV7xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV7xx0_I2C_WRITE_ID,
- OV7xx0_I2C_READ_ID) < 0)
- return -1;
- if (ov51x_init_ov_sensor(ov511) >= 0) {
- PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
- } else {
- /* Reset the 76xx */
- if (ov51x_i2c_write(ov511, 0x12, 0x80) < 0) return -1;
- /* Wait for it to initialize */
- schedule_timeout(1 + 150 * HZ / 1000);
- i = 0;
- success = 0;
- while (i <= i2c_detect_tries) {
- if ((ov51x_i2c_read(ov511,
- OV7610_REG_ID_HIGH) == 0x7F) &&
- (ov51x_i2c_read(ov511,
- OV7610_REG_ID_LOW) == 0xA2)) {
- success = 1;
- break;
- } else {
- i++;
- }
- }
- // Was (i == i2c_detect_tries) previously. This obviously used to always report
- // success. Whether anyone actually depended on that bug is unknown
- if ((i >= i2c_detect_tries) && (success == 0)) {
- err("Failed to read sensor ID. You might not have an");
- err("OV7610/20, or it may be not responding. Report");
- err("this to " EMAIL);
- err("This is only a warning. You can attempt to use");
- err("your camera anyway");
- // Only issue a warning for now
- // return -1;
- } else {
- PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
- }
- }
- /* Detect sensor (sub)type */
- rc = ov51x_i2c_read(ov511, OV7610_REG_COM_I);
- if (rc < 0) {
- err("Error detecting sensor type");
- return -1;
- } else if ((rc & 3) == 3) {
- info("Sensor is an OV7610");
- ov511->sensor = SEN_OV7610;
- } else if ((rc & 3) == 1) {
- /* I don't know what's different about the 76BE yet */
- if (ov51x_i2c_read(ov511, 0x15) & 1)
- info("Sensor is an OV7620AE");
- else
- info("Sensor is an OV76BE");
- /* OV511+ will return all zero isoc data unless we
- * configure the sensor as a 7620. Someone needs to
- * find the exact reg. setting that causes this. */
- if (ov511->bridge == BRG_OV511PLUS) {
- info("Enabling 511+/7620AE workaround");
- ov511->sensor = SEN_OV7620;
- } else {
- ov511->sensor = SEN_OV7620AE;
- }
- } else if ((rc & 3) == 0) {
- info("Sensor is an OV7620");
- ov511->sensor = SEN_OV7620;
- } else {
- err("Unknown image sensor version: %d", rc & 3);
- return -1;
- }
- if (ov511->sensor == SEN_OV7620) {
- PDEBUG(4, "Writing 7620 registers");
- if (ov511_write_regvals(ov511, aRegvalsNorm7620))
- return -1;
- } else {
- PDEBUG(4, "Writing 7610 registers");
- if (ov511_write_regvals(ov511, aRegvalsNorm7610))
- return -1;
- }
- /* Set sensor-specific vars */
- ov511->maxwidth = 640;
- ov511->maxheight = 480;
- ov511->minwidth = 64;
- ov511->minheight = 48;
- // FIXME: These do not match the actual settings yet
- ov511->brightness = 0x80 << 8;
- ov511->contrast = 0x80 << 8;
- ov511->colour = 0x80 << 8;
- ov511->hue = 0x80 << 8;
- return 0;
- }
- /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
- static int
- ov6xx0_configure(struct usb_ov511 *ov511)
- {
- int rc;
- static struct ov511_regvals aRegvalsNorm6x20[] = {
- { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
- { OV511_I2C_BUS, 0x11, 0x01 },
- { OV511_I2C_BUS, 0x03, 0x60 },
- { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
- { OV511_I2C_BUS, 0x07, 0xa8 },
- /* The ratio of 0x0c and 0x0d controls the white point */
- { OV511_I2C_BUS, 0x0c, 0x24 },
- { OV511_I2C_BUS, 0x0d, 0x24 },
- { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
- { OV511_I2C_BUS, 0x14, 0x04 },
- /* 0x16: 0x06 helps frame stability with moving objects */
- { OV511_I2C_BUS, 0x16, 0x06 },
- // { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
- { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
- /* 0x28: 0x05 Selects RGB format if RGB on */
- { OV511_I2C_BUS, 0x28, 0x05 },
- { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
- // { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
- { OV511_I2C_BUS, 0x2d, 0x99 },
- { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
- { OV511_I2C_BUS, 0x38, 0x8b },
- { OV511_I2C_BUS, 0x39, 0x40 },
-
- { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
- { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
- { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
- { OV511_I2C_BUS, 0x3d, 0x80 },
- /* These next two registers (0x4a, 0x4b) are undocumented. They
- * control the color balance */
- { OV511_I2C_BUS, 0x4a, 0x80 },
- { OV511_I2C_BUS, 0x4b, 0x80 },
- { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
- { OV511_I2C_BUS, 0x4e, 0xc1 },
- { OV511_I2C_BUS, 0x4f, 0x04 },
- // Do 50-53 have any effect?
- // Toggle 0x12[2] off and on here?
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- /* This chip is undocumented so many of these are guesses. OK=verified,
- * A=Added since 6620, U=unknown function (not a 6620 reg) */
- static struct ov511_regvals aRegvalsNorm6x30[] = {
- /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
- /*00?*/ { OV511_I2C_BUS, 0x11, 0x01 },
- /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
- /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
- { OV511_I2C_BUS, 0x07, 0xa8 },
- /* The ratio of 0x0c and 0x0d controls the white point */
- /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
- /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
- /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
- // /*24?*/ { OV511_I2C_BUS, 0x12, 0x28 }, /* Enable AGC */
- // { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
- // /*A*/ { OV511_I2C_BUS, 0x13, 0x21 },
- // /*A*/ { OV511_I2C_BUS, 0x13, 0x25 }, /* Tristate Y and UV busses */
- // /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
- /* 0x16: 0x06 helps frame stability with moving objects */
- /*03?*/ { OV511_I2C_BUS, 0x16, 0x06 },
- // /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
- // 21 & 22? The suggested values look wrong. Go with default
- /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
- /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
- // /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
- /* 0x28: 0x05 Selects RGB format if RGB on */
- // /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
- // /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
- /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
- // /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
- // /*U*/ { OV511_I2C_BUS, 0x2c, 0xa0 },
- { OV511_I2C_BUS, 0x2d, 0x99 },
- // /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
- // /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
- // /*U*/ { OV511_I2C_BUS, 0x36, 0x8f }, // May not be necessary
- // /*U*/ { OV511_I2C_BUS, 0x37, 0x80 }, // May not be necessary
- // /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
- // /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
- // { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
- // { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
- // { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
- /*OK*/ { OV511_I2C_BUS, 0x3d, 0x80 },
- // /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
- // /*U*/ { OV511_I2C_BUS, 0x40, 0x00 },
- // /*U*/ { OV511_I2C_BUS, 0x41, 0x00 },
- // /*U*/ { OV511_I2C_BUS, 0x42, 0x80 },
- // /*U*/ { OV511_I2C_BUS, 0x43, 0x3f },
- // /*U*/ { OV511_I2C_BUS, 0x44, 0x80 },
- // /*U*/ { OV511_I2C_BUS, 0x45, 0x20 },
- // /*U*/ { OV511_I2C_BUS, 0x46, 0x20 },
- // /*U*/ { OV511_I2C_BUS, 0x47, 0x80 },
- // /*U*/ { OV511_I2C_BUS, 0x48, 0x7f },
- // /*U*/ { OV511_I2C_BUS, 0x49, 0x00 },
- /* These next two registers (0x4a, 0x4b) are undocumented. They
- * control the color balance */
- // /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
- // /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
- // /*U*/ { OV511_I2C_BUS, 0x4c, 0xd0 },
- /*d2?*/ { OV511_I2C_BUS, 0x4d, 0x10 }, /* This reduces noise a bit */
- /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
- /*04?*/ { OV511_I2C_BUS, 0x4f, 0x07 },
- // /*U*/ { OV511_I2C_BUS, 0x50, 0xff },
- /*U*/ { OV511_I2C_BUS, 0x54, 0x23 },
- // /*U*/ { OV511_I2C_BUS, 0x55, 0xff },
- // /*U*/ { OV511_I2C_BUS, 0x56, 0x12 },
- /*U*/ { OV511_I2C_BUS, 0x57, 0x81 },
- // /*U*/ { OV511_I2C_BUS, 0x58, 0x75 },
- /*U*/ { OV511_I2C_BUS, 0x59, 0x01 },
- /*U*/ { OV511_I2C_BUS, 0x5a, 0x2c },
- /*U*/ { OV511_I2C_BUS, 0x5b, 0x0f },
- // /*U*/ { OV511_I2C_BUS, 0x5c, 0x10 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- PDEBUG(4, "starting sensor configuration");
-
- if (ov51x_init_ov_sensor(ov511) < 0) {
- err("Failed to read sensor ID. You might not have an OV6xx0,");
- err("or it may be not responding. Report this to " EMAIL);
- return -1;
- } else {
- PDEBUG(1, "OV6xx0 sensor detected");
- }
- /* Detect sensor (sub)type */
- rc = ov51x_i2c_read(ov511, OV7610_REG_COM_I);
- if (rc < 0) {
- err("Error detecting sensor type");
- return -1;
- } else if ((rc & 3) == 0) {
- info("Sensor is an OV6630");
- ov511->sensor = SEN_OV6630;
- } else if ((rc & 3) == 1) {
- info("Sensor is an OV6620");
- ov511->sensor = SEN_OV6620;
- } else if ((rc & 3) == 2) {
- info("Sensor is an OV6630AE");
- ov511->sensor = SEN_OV6630;
- } else if ((rc & 3) == 3) {
- info("Sensor is an OV6630AF");
- ov511->sensor = SEN_OV6630;
- }
- /* Set sensor-specific vars */
- if (ov511->sensor == SEN_OV6620) {
- ov511->maxwidth = 352;
- ov511->maxheight = 288;
- } else {
- /* 352x288 not working with OV518 yet */
- ov511->maxwidth = 320;
- ov511->maxheight = 240;
- }
- ov511->minwidth = 64;
- ov511->minheight = 48;
- // FIXME: These do not match the actual settings yet
- ov511->brightness = 0x80 << 8;
- ov511->contrast = 0x80 << 8;
- ov511->colour = 0x80 << 8;
- ov511->hue = 0x80 << 8;
- if (ov511->sensor == SEN_OV6620) {
- PDEBUG(4, "Writing 6x20 registers");
- if (ov511_write_regvals(ov511, aRegvalsNorm6x20))
- return -1;
- } else {
- PDEBUG(4, "Writing 6x30 registers");
- if (ov511_write_regvals(ov511, aRegvalsNorm6x30))
- return -1;
- }
-
- return 0;
- }
- /* This initializes the KS0127 and KS0127B video decoders. */
- static int
- ks0127_configure(struct usb_ov511 *ov511)
- {
- int rc;
- // FIXME: I don't know how to sync or reset it yet
- #if 0
- if (ov51x_init_ks_sensor(ov511) < 0) {
- err("Failed to initialize the KS0127");
- return -1;
- } else {
- PDEBUG(1, "KS012x(B) sensor detected");
- }
- #endif
- /* Detect decoder subtype */
- rc = ov51x_i2c_read(ov511, 0x00);
- if (rc < 0) {
- err("Error detecting sensor type");
- return -1;
- } else if (rc & 0x08) {
- rc = ov51x_i2c_read(ov511, 0x3d);
- if (rc < 0) {
- err("Error detecting sensor type");
- return -1;
- } else if ((rc & 0x0f) == 0) {
- info("Sensor is a KS0127");
- ov511->sensor = SEN_KS0127;
- } else if ((rc & 0x0f) == 9) {
- info("Sensor is a KS0127B Rev. A");
- ov511->sensor = SEN_KS0127B;
- }
- } else {
- err("Error: Sensor is an unsupported KS0122");
- return -1;
- }
- /* Set sensor-specific vars */
- ov511->maxwidth = 640;
- ov511->maxheight = 480;
- ov511->minwidth = 64;
- ov511->minheight = 48;
- // FIXME: These do not match the actual settings yet
- ov511->brightness = 0x80 << 8;
- ov511->contrast = 0x80 << 8;
- ov511->colour = 0x80 << 8;
- ov511->hue = 0x80 << 8;
- /* This device is not supported yet. Bail out now... */
- err("This sensor is not supported yet.");
- return -1;
- return 0;
- }
- /* This initializes the SAA7111A video decoder. */
- static int
- saa7111a_configure(struct usb_ov511 *ov511)
- {
- struct usb_device *dev = ov511->dev;
- int rc;
- /* Since there is no register reset command, all registers must be
- * written, otherwise gives erratic results */
- static struct ov511_regvals aRegvalsNormSAA7111A[] = {
- { OV511_I2C_BUS, 0x06, 0xce },
- { OV511_I2C_BUS, 0x07, 0x00 },
- { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
- { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
- { OV511_I2C_BUS, 0x00, 0x00 },
- { OV511_I2C_BUS, 0x01, 0x00 },
- { OV511_I2C_BUS, 0x03, 0x23 },
- { OV511_I2C_BUS, 0x04, 0x00 },
- { OV511_I2C_BUS, 0x05, 0x00 },
- { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
- { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
- { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
- { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
- { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
- { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
- { OV511_I2C_BUS, 0x0f, 0x00 },
- { OV511_I2C_BUS, 0x11, 0x0c },
- { OV511_I2C_BUS, 0x12, 0x00 },
- { OV511_I2C_BUS, 0x13, 0x00 },
- { OV511_I2C_BUS, 0x14, 0x00 },
- { OV511_I2C_BUS, 0x15, 0x00 },
- { OV511_I2C_BUS, 0x16, 0x00 },
- { OV511_I2C_BUS, 0x17, 0x00 },
- { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- // FIXME: I don't know how to sync or reset it yet
- #if 0
- if (ov51x_init_saa_sensor(ov511) < 0) {
- err("Failed to initialize the SAA7111A");
- return -1;
- } else {
- PDEBUG(1, "SAA7111A sensor detected");
- }
- #endif
- /* Set sensor-specific vars */
- ov511->maxwidth = 640;
- ov511->maxheight = 480; /* Even/Odd fields */
- ov511->minwidth = 320;
- ov511->minheight = 240; /* Even field only */
- ov511->has_decoder = 1;
- ov511->num_inputs = 8;
- ov511->norm = VIDEO_MODE_AUTO;
- ov511->stop_during_set = 0; /* Decoder guarantees stable image */
- /* Decoder doesn't change these values, so we use these instead of
- * acutally reading the registers (which doesn't work) */
- ov511->brightness = 0x80 << 8;
- ov511->contrast = 0x40 << 9;
- ov511->colour = 0x40 << 9;
- ov511->hue = 32768;
- PDEBUG(4, "Writing SAA7111A registers");
- if (ov511_write_regvals(ov511, aRegvalsNormSAA7111A))
- return -1;
- /* Detect version of decoder. This must be done after writing the
- * initial regs or the decoder will lock up. */
- rc = ov51x_i2c_read(ov511, 0x00);
- if (rc < 0) {
- err("Error detecting sensor version");
- return -1;
- } else {
- info("Sensor is an SAA7111A (version 0x%x)", rc);
- ov511->sensor = SEN_SAA7111A;
- }
- // FIXME: Fix this for OV518(+)
- /* Latch to negative edge of clock. Otherwise, we get incorrect
- * colors and jitter in the digital signal. */
- if (ov511->bridge == BRG_OV511 || ov511->bridge == BRG_OV511PLUS)
- ov511_reg_write(dev, 0x11, 0x00);
- else
- warn("SAA7111A not yet supported with OV518/OV518+");
- return 0;
- }
- /* This initializes the OV511/OV511+ and the sensor */
- static int
- ov511_configure(struct usb_ov511 *ov511)
- {
- struct usb_device *dev = ov511->dev;
- int i;
- static struct ov511_regvals aRegvalsInit511[] = {
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x7f },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x7f },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x3f },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x3d },
- { OV511_DONE_BUS, 0x0, 0x00},
- };
- static struct ov511_regvals aRegvalsNorm511[] = {
- { OV511_REG_BUS, OV511_REG_DRAM_ENABLE_FLOW_CONTROL, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x03 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x01 },
- { OV511_REG_BUS, OV511_REG_FIFO_BITMASK, 0x1f },
- { OV511_REG_BUS, OV511_OMNICE_ENABLE, 0x00 },
- { OV511_REG_BUS, OV511_OMNICE_LUT_ENABLE, 0x03 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- static struct ov511_regvals aRegvalsNorm511Plus[] = {
- { OV511_REG_BUS, OV511_REG_DRAM_ENABLE_FLOW_CONTROL, 0xff },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x01 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x03 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_SNAPSHOT, 0x01 },
- { OV511_REG_BUS, OV511_REG_FIFO_BITMASK, 0xff },
- { OV511_REG_BUS, OV511_OMNICE_ENABLE, 0x00 },
- { OV511_REG_BUS, OV511_OMNICE_LUT_ENABLE, 0x03 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- PDEBUG(4, "");
- ov511->customid = ov511_reg_read(dev, OV511_REG_SYSTEM_CUSTOM_ID);
- if (ov511->customid < 0) {
- err("Unable to read camera bridge registers");
- goto error;
- }
- ov511->desc = -1;
- PDEBUG (1, "CustomID = %d", ov511->customid);
- for (i = 0; clist[i].id >= 0; i++) {
- if (ov511->customid == clist[i].id) {
- info("model: %s", clist[i].description);
- ov511->desc = i;
- break;
- }
- }
- if (clist[i].id == -1) {
- err("Camera type (%d) not recognized", ov511->customid);
- err("Please notify " EMAIL " of the name,");
- err("manufacturer, model, and this number of your camera.");
- err("Also include the output of the detection process.");
- }
- if (clist[i].id == 6) { /* USB Life TV (NTSC) */
- ov511->tuner_type = 8; /* Temic 4036FY5 3X 1981 */
- }
- if (ov511_write_regvals(ov511, aRegvalsInit511)) goto error;
- if (ov511->led_policy == LED_OFF || ov511->led_policy == LED_AUTO)
- ov51x_led_control(ov511, 0);
- /* The OV511+ has undocumented bits in the flow control register.
- * Setting it to 0xff fixes the corruption with moving objects. */
- if (ov511->bridge == BRG_OV511) {
- if (ov511_write_regvals(ov511, aRegvalsNorm511)) goto error;
- } else if (ov511->bridge == BRG_OV511PLUS) {
- if (ov511_write_regvals(ov511, aRegvalsNorm511Plus)) goto error;
- } else {
- err("Invalid bridge");
- }
- if (ov511_init_compression(ov511)) goto error;
- ov511_set_packet_size(ov511, 0);
- ov511->snap_enabled = snapshot;
- /* Test for 7xx0 */
- PDEBUG(3, "Testing for 0V7xx0");
- ov511->primary_i2c_slave = OV7xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV7xx0_I2C_WRITE_ID,
- OV7xx0_I2C_READ_ID) < 0)
- goto error;
- if (ov51x_i2c_write(ov511, 0x12, 0x80) < 0) {
- /* Test for 6xx0 */
- PDEBUG(3, "Testing for 0V6xx0");
- ov511->primary_i2c_slave = OV6xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV6xx0_I2C_WRITE_ID,
- OV6xx0_I2C_READ_ID) < 0)
- goto error;
- if (ov51x_i2c_write(ov511, 0x12, 0x80) < 0) {
- /* Test for 8xx0 */
- PDEBUG(3, "Testing for 0V8xx0");
- ov511->primary_i2c_slave = OV8xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV8xx0_I2C_WRITE_ID,
- OV8xx0_I2C_READ_ID))
- goto error;
- if (ov51x_i2c_write(ov511, 0x12, 0x80) < 0) {
- /* Test for SAA7111A */
- PDEBUG(3, "Testing for SAA7111A");
- ov511->primary_i2c_slave = SAA7111A_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, SAA7111A_I2C_WRITE_ID,
- SAA7111A_I2C_READ_ID))
- goto error;
- if (ov51x_i2c_write(ov511, 0x0d, 0x00) < 0) {
- /* Test for KS0127 */
- PDEBUG(3, "Testing for KS0127");
- ov511->primary_i2c_slave = KS0127_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, KS0127_I2C_WRITE_ID,
- KS0127_I2C_READ_ID))
- goto error;
- if (ov51x_i2c_write(ov511, 0x10, 0x00) < 0) {
- err("Can't determine sensor slave IDs");
- goto error;
- } else {
- if(ks0127_configure(ov511) < 0) {
- err("Failed to configure KS0127");
- goto error;
- }
- }
- } else {
- if(saa7111a_configure(ov511) < 0) {
- err("Failed to configure SAA7111A");
- goto error;
- }
- }
- } else {
- err("Detected unsupported OV8xx0 sensor");
- goto error;
- }
- } else {
- if(ov6xx0_configure(ov511) < 0) {
- err("Failed to configure OV6xx0");
- goto error;
- }
- }
- } else {
- if(ov7xx0_configure(ov511) < 0) {
- err("Failed to configure OV7xx0");
- goto error;
- }
- }
- return 0;
- error:
- err("OV511 Config failed");
- return -EBUSY;
- }
- /* This initializes the OV518/OV518+ and the sensor */
- static int
- ov518_configure(struct usb_ov511 *ov511)
- {
- struct usb_device *dev = ov511->dev;
- static struct ov511_regvals aRegvalsInit518[] = {
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x40 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0xe1 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x3e },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0xe1 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_RESET, 0x00 },
- { OV511_REG_BUS, OV511_REG_SYSTEM_INIT, 0xe1 },
- { OV511_REG_BUS, 0x46, 0x00 },
- { OV511_REG_BUS, 0x5d, 0x03 },
- { OV511_DONE_BUS, 0x0, 0x00},
- };
- /* New values, based on Windows driver. Since what they do is not
- * known yet, this may be incorrect. */
- static struct ov511_regvals aRegvalsNorm518[] = {
- { OV511_REG_BUS, 0x52, 0x02 }, /* Reset snapshot */
- { OV511_REG_BUS, 0x52, 0x01 }, /* Enable snapshot */
- { OV511_REG_BUS, 0x31, 0x0f },
- { OV511_REG_BUS, 0x5d, 0x03 },
- { OV511_REG_BUS, 0x24, 0x9f },
- { OV511_REG_BUS, 0x25, 0x90 },
- { OV511_REG_BUS, 0x20, 0x00 }, /* Was 0x08 */
- { OV511_REG_BUS, 0x51, 0x04 },
- { OV511_REG_BUS, 0x71, 0x19 },
- { OV511_DONE_BUS, 0x0, 0x00 },
- };
- PDEBUG(4, "");
- /* First 5 bits of custom ID reg are a revision ID on OV518 */
- info("Device revision %d",
- 0x1F & ov511_reg_read(dev, OV511_REG_SYSTEM_CUSTOM_ID));
- if (ov511_write_regvals(ov511, aRegvalsInit518)) goto error;
- /* Set LED GPIO pin to output mode */
- if (ov511_reg_write_mask(dev, 0x57,0x00, 0x02) < 0) goto error;
- /* LED is off by default with OV518; have to explicitly turn it on */
- if (ov511->led_policy == LED_OFF || ov511->led_policy == LED_AUTO)
- ov51x_led_control(ov511, 0);
- else
- ov51x_led_control(ov511, 1);
- /* Don't require compression if dumppix is enabled; otherwise it's
- * required. OV518 has no uncompressed mode, to save RAM. */
- if (!dumppix && !ov511->compress) {
- ov511->compress = 1;
- warn("Compression required with OV518...enabling");
- }
- if (ov511_write_regvals(ov511, aRegvalsNorm518)) goto error;
- if (ov511_reg_write(dev, 0x2f,0x80) < 0) goto error;
- if (ov518_init_compression(ov511)) goto error;
- ov511_set_packet_size(ov511, 0);
- ov511->snap_enabled = snapshot;
- /* Test for 76xx */
- ov511->primary_i2c_slave = OV7xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV7xx0_I2C_WRITE_ID,
- OV7xx0_I2C_READ_ID) < 0)
- goto error;
- /* The OV518 must be more aggressive about sensor detection since
- * I2C write will never fail if the sensor is not present. We have
- * to try to initialize the sensor to detect its presence */
- if (ov51x_init_ov_sensor(ov511) < 0) {
- /* Test for 6xx0 */
- ov511->primary_i2c_slave = OV6xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV6xx0_I2C_WRITE_ID,
- OV6xx0_I2C_READ_ID) < 0)
- goto error;
- if (ov51x_init_ov_sensor(ov511) < 0) {
- /* Test for 8xx0 */
- ov511->primary_i2c_slave = OV8xx0_I2C_WRITE_ID;
- if (ov51x_set_slave_ids(ov511, OV8xx0_I2C_WRITE_ID,
- OV8xx0_I2C_READ_ID) < 0)
- goto error;
- if (ov51x_init_ov_sensor(ov511) < 0) {
- err("Can't determine sensor slave IDs");
- goto error;
- } else {
- err("Detected unsupported OV8xx0 sensor");
- goto error;
- }
- } else {
- if (ov6xx0_configure(ov511) < 0) {
- err("Failed to configure OV6xx0");
- goto error;
- }
- }
- } else {
- if (ov7xx0_configure(ov511) < 0) {
- err("Failed to configure OV7xx0");
- goto error;
- }
- }
- // The OV518 cannot go as low as the sensor can
- ov511->minwidth = 160;
- ov511->minheight = 120;
- return 0;
- error:
- err("OV518 Config failed");
- return -EBUSY;
- }
- /****************************************************************************
- *
- * USB routines
- *
- ***************************************************************************/
- static void *
- ov51x_probe(struct usb_device *dev, unsigned int ifnum,
- const struct usb_device_id *id)
- {
- struct usb_interface_descriptor *interface;
- struct usb_ov511 *ov511;
- int i;
- int registered = 0;
- PDEBUG(1, "probing for device...");
- /* We don't handle multi-config cameras */
- if (dev->descriptor.bNumConfigurations != 1)
- return NULL;
- interface = &dev->actconfig->interface[ifnum].altsetting[0];
- /* Checking vendor/product should be enough, but what the hell */
- if (interface->bInterfaceClass != 0xFF)
- return NULL;
- if (interface->bInterfaceSubClass != 0x00)
- return NULL;
- /* Since code below may sleep, we use this as a lock */
- MOD_INC_USE_COUNT;
- if ((ov511 = kmalloc(sizeof(*ov511), GFP_KERNEL)) == NULL) {
- err("couldn't kmalloc ov511 struct");
- goto error_unlock;
- }
- memset(ov511, 0, sizeof(*ov511));
- ov511->dev = dev;
- ov511->iface = interface->bInterfaceNumber;
- ov511->led_policy = led;
- ov511->compress = compress;
- ov511->lightfreq = lightfreq;
- ov511->num_inputs = 1; /* Video decoder init functs. change this */
- ov511->stop_during_set = !fastset;
- ov511->tuner_type = tuner;
- ov511->backlight = backlight;
- ov511->auto_brt = autobright;
- ov511->auto_gain = autogain;
- ov511->auto_exp = autoexp;
- switch (dev->descriptor.idProduct) {
- case PROD_OV511:
- info("USB OV511 camera found");
- ov511->bridge = BRG_OV511;
- ov511->bclass = BCL_OV511;
- break;
- case PROD_OV511PLUS:
- info("USB OV511+ camera found");
- ov511->bridge = BRG_OV511PLUS;
- ov511->bclass = BCL_OV511;
- break;
- case PROD_OV518:
- info("USB OV518 camera found");
- ov511->bridge = BRG_OV518;
- ov511->bclass = BCL_OV518;
- break;
- case PROD_OV518PLUS:
- info("USB OV518+ camera found");
- ov511->bridge = BRG_OV518PLUS;
- ov511->bclass = BCL_OV518;
- break;
- case PROD_ME2CAM:
- if (dev->descriptor.idVendor != VEND_MATTEL)
- goto error;
- info("Intel Play Me2Cam (OV511+) found");
- ov511->bridge = BRG_OV511PLUS;
- ov511->bclass = BCL_OV511;
- break;
- default:
- err("Unknown product ID 0x%x", dev->descriptor.idProduct);
- goto error_dealloc;
- }
- /* Workaround for some applications that want data in RGB
- * instead of BGR. */
- if (force_rgb)
- info("data format set to RGB");
- init_waitqueue_head(&ov511->wq);
- init_MUTEX(&ov511->lock); /* to 1 == available */
- init_MUTEX(&ov511->buf_lock);
- init_MUTEX(&ov511->param_lock);
- init_MUTEX(&ov511->i2c_lock);
- ov511->buf_state = BUF_NOT_ALLOCATED;
- if (ov511->bridge == BRG_OV518 ||
- ov511->bridge == BRG_OV518PLUS) {
- if (ov518_configure(ov511) < 0)
- goto error;
- } else {
- if (ov511_configure(ov511) < 0)
- goto error;
- }
- for (i = 0; i < OV511_NUMFRAMES; i++) {
- ov511->frame[i].framenum = i;
- init_waitqueue_head(&ov511->frame[i].wq);
- }
- /* Unnecessary? (This is done on open(). Need to make sure variables
- * are properly initialized without this before removing it, though). */
- if (ov51x_set_default_params(ov511) < 0)
- goto error;
- #ifdef OV511_DEBUG
- if (dump_bridge)
- ov511_dump_regs(dev);
- #endif
- memcpy(&ov511->vdev, &ov511_template, sizeof(ov511_template));
- ov511->vdev.priv = ov511;
- for (i = 0; i < OV511_MAX_UNIT_VIDEO; i++) {
- /* Minor 0 cannot be specified; assume user wants autodetect */
- if (unit_video[i] == 0)
- break;
- if (video_register_device(&ov511->vdev, VFL_TYPE_GRABBER,
- unit_video[i]) >= 0) {
- registered = 1;
- break;
- }
- }
- /* Use the next available one */
- if (!registered &&
- video_register_device(&ov511->vdev, VFL_TYPE_GRABBER, -1) < 0) {
- err("video_register_device failed");
- goto error;
- }
- info("Device registered on minor %d", ov511->vdev.minor);
- MOD_DEC_USE_COUNT;
- return ov511;
- error:
- err("Camera initialization failed");
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- /* Safe to call even if entry doesn't exist */
- destroy_proc_ov511_cam(ov511);
- #endif
- usb_driver_release_interface(&ov511_driver,
- &dev->actconfig->interface[ov511->iface]);
- error_dealloc:
- if (ov511) {
- kfree(ov511);
- ov511 = NULL;
- }
- error_unlock:
- MOD_DEC_USE_COUNT;
- return NULL;
- }
- static void
- ov51x_disconnect(struct usb_device *dev, void *ptr)
- {
- struct usb_ov511 *ov511 = (struct usb_ov511 *) ptr;
- int n;
- MOD_INC_USE_COUNT;
- PDEBUG(3, "");
- /* We don't want people trying to open up the device */
- if (!ov511->user)
- video_unregister_device(&ov511->vdev);
- else
- PDEBUG(3, "Device open...deferring video_unregister_device");
- for (n = 0; n < OV511_NUMFRAMES; n++)
- ov511->frame[n].grabstate = FRAME_ERROR;
- ov511->curframe = -1;
- /* This will cause the process to request another frame */
- for (n = 0; n < OV511_NUMFRAMES; n++)
- if (waitqueue_active(&ov511->frame[n].wq))
- wake_up_interruptible(&ov511->frame[n].wq);
- if (waitqueue_active(&ov511->wq))
- wake_up_interruptible(&ov511->wq);
- ov511->streaming = 0;
- /* Unschedule all of the iso td's */
- for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
- if (ov511->sbuf[n].urb) {
- ov511->sbuf[n].urb->next = NULL;
- usb_unlink_urb(ov511->sbuf[n].urb);
- usb_free_urb(ov511->sbuf[n].urb);
- ov511->sbuf[n].urb = NULL;
- }
- }
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- destroy_proc_ov511_cam(ov511);
- #endif
- usb_driver_release_interface(&ov511_driver,
- &ov511->dev->actconfig->interface[ov511->iface]);
- ov511->dev = NULL;
- /* Free the memory */
- if (ov511 && !ov511->user) {
- ov511_dealloc(ov511, 1);
- kfree(ov511);
- ov511 = NULL;
- }
- MOD_DEC_USE_COUNT;
- }
- static struct usb_driver ov511_driver = {
- name: "ov511",
- id_table: device_table,
- probe: ov51x_probe,
- disconnect: ov51x_disconnect
- };
- /****************************************************************************
- *
- * Module routines
- *
- ***************************************************************************/
- /* Returns 0 for success */
- int
- ov511_register_decomp_module(int ver, struct ov51x_decomp_ops *ops, int ov518,
- int mmx)
- {
- if (ver != DECOMP_INTERFACE_VER) {
- err("Decompression module has incompatible");
- err("interface version %d", ver);
- err("Interface version %d is required", DECOMP_INTERFACE_VER);
- return -EINVAL;
- }
- if (!ops)
- return -EFAULT;
- if (mmx && !ov51x_mmx_available) {
- err("MMX not available on this system or kernel");
- return -EINVAL;
- }
- lock_kernel();
- if (ov518) {
- if (mmx) {
- if (ov518_mmx_decomp_ops)
- goto err_in_use;
- else
- ov518_mmx_decomp_ops = ops;
- } else {
- if (ov518_decomp_ops)
- goto err_in_use;
- else
- ov518_decomp_ops = ops;
- }
- } else {
- if (mmx) {
- if (ov511_mmx_decomp_ops)
- goto err_in_use;
- else
- ov511_mmx_decomp_ops = ops;
- } else {
- if (ov511_decomp_ops)
- goto err_in_use;
- else
- ov511_decomp_ops = ops;
- }
- }
- MOD_INC_USE_COUNT;
- unlock_kernel();
- return 0;
- err_in_use:
- unlock_kernel();
- return -EBUSY;
- }
- void
- ov511_deregister_decomp_module(int ov518, int mmx)
- {
- lock_kernel();
- if (ov518) {
- if (mmx)
- ov518_mmx_decomp_ops = NULL;
- else
- ov518_decomp_ops = NULL;
- } else {
- if (mmx)
- ov511_mmx_decomp_ops = NULL;
- else
- ov511_decomp_ops = NULL;
- }
-
- MOD_DEC_USE_COUNT;
- unlock_kernel();
- }
- static int __init
- usb_ov511_init(void)
- {
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- proc_ov511_create();
- #endif
- if (usb_register(&ov511_driver) < 0)
- return -1;
- // FIXME: Don't know how to determine this yet
- ov51x_mmx_available = 0;
- #if defined (__i386__)
- if (test_bit(X86_FEATURE_MMX, &boot_cpu_data.x86_capability))
- ov51x_mmx_available = 1;
- #endif
- info(DRIVER_VERSION " : " DRIVER_DESC);
- return 0;
- }
- static void __exit
- usb_ov511_exit(void)
- {
- usb_deregister(&ov511_driver);
- info("driver deregistered");
- #if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)
- proc_ov511_destroy();
- #endif
- }
- module_init(usb_ov511_init);
- module_exit(usb_ov511_exit);
- /* No version, for compatibility with binary-only modules */
- EXPORT_SYMBOL_NOVERS(ov511_register_decomp_module);
- EXPORT_SYMBOL_NOVERS(ov511_deregister_decomp_module);