⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sn9c102_core.c

📁 linux内核源码
💻 C
📖 第 1 页 / 共 5 页
字号:
	for (i = 0; i < len; i++) {		size_t b;		/* Read the variable part of the header */		if (unlikely(cam->sof.bytesread >= sizeof(marker))) {			cam->sof.header[cam->sof.bytesread] = *(m+i);			if (++cam->sof.bytesread == soflen) {				cam->sof.bytesread = 0;				return mem + i;			}			continue;		}		/* Search for the SOF marker (fixed part) in the header */		for (j = 0, b=cam->sof.bytesread; j+b < sizeof(marker); j++) {			if (unlikely(i+j) == len)				return NULL;			if (*(m+i+j) == marker[cam->sof.bytesread]) {				cam->sof.header[cam->sof.bytesread] = *(m+i+j);				if (++cam->sof.bytesread == sizeof(marker)) {					PDBGG("Bytes to analyze: %zd. SOF "					      "starts at byte #%zd", len, i);					i += j+1;					break;				}			} else {				cam->sof.bytesread = 0;				break;			}		}	}	return NULL;}static void*sn9c102_find_eof_header(struct sn9c102_device* cam, void* mem, size_t len){	static const u8 eof_header[4][4] = {		{0x00, 0x00, 0x00, 0x00},		{0x40, 0x00, 0x00, 0x00},		{0x80, 0x00, 0x00, 0x00},		{0xc0, 0x00, 0x00, 0x00},	};	size_t i, j;	/* The EOF header does not exist in compressed data */	if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_SN9C10X ||	    cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)		return NULL;	/*	   The EOF header might cross the packet boundary, but this is not a	   problem, since the end of a frame is determined by checking its size	   in the first place.	*/	for (i = 0; (len >= 4) && (i <= len - 4); i++)		for (j = 0; j < ARRAY_SIZE(eof_header); j++)			if (!memcmp(mem + i, eof_header[j], 4))				return mem + i;	return NULL;}static voidsn9c102_write_jpegheader(struct sn9c102_device* cam, struct sn9c102_frame_t* f){	static const u8 jpeg_header[589] = {		0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00, 0x06, 0x04, 0x05,		0x06, 0x05, 0x04, 0x06, 0x06, 0x05, 0x06, 0x07, 0x07, 0x06,		0x08, 0x0a, 0x10, 0x0a, 0x0a, 0x09, 0x09, 0x0a, 0x14, 0x0e,		0x0f, 0x0c, 0x10, 0x17, 0x14, 0x18, 0x18, 0x17, 0x14, 0x16,		0x16, 0x1a, 0x1d, 0x25, 0x1f, 0x1a, 0x1b, 0x23, 0x1c, 0x16,		0x16, 0x20, 0x2c, 0x20, 0x23, 0x26, 0x27, 0x29, 0x2a, 0x29,		0x19, 0x1f, 0x2d, 0x30, 0x2d, 0x28, 0x30, 0x25, 0x28, 0x29,		0x28, 0x01, 0x07, 0x07, 0x07, 0x0a, 0x08, 0x0a, 0x13, 0x0a,		0x0a, 0x13, 0x28, 0x1a, 0x16, 0x1a, 0x28, 0x28, 0x28, 0x28,		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28,		0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0xff, 0xc4, 0x01, 0xa2,		0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,		0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01,		0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,		0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x10, 0x00,		0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,		0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,		0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,		0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,		0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62,		0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25,		0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38,		0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,		0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,		0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,		0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,		0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,		0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,		0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,		0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,		0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3,		0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3,		0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0x11, 0x00, 0x02,		0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04,		0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,		0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,		0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1,		0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1,		0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19,		0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,		0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,		0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64,		0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76,		0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,		0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,		0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,		0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,		0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,		0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3,		0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4,		0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc0, 0x00, 0x11,		0x08, 0x01, 0xe0, 0x02, 0x80, 0x03, 0x01, 0x21, 0x00, 0x02,		0x11, 0x01, 0x03, 0x11, 0x01, 0xff, 0xda, 0x00, 0x0c, 0x03,		0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00	};	u8 *pos = f->bufmem;	memcpy(pos, jpeg_header, sizeof(jpeg_header));	*(pos + 6) = 0x00;	*(pos + 7 + 64) = 0x01;	if (cam->compression.quality == 0) {		memcpy(pos + 7, SN9C102_Y_QTABLE0, 64);		memcpy(pos + 8 + 64, SN9C102_UV_QTABLE0, 64);	} else if (cam->compression.quality == 1) {		memcpy(pos + 7, SN9C102_Y_QTABLE1, 64);		memcpy(pos + 8 + 64, SN9C102_UV_QTABLE1, 64);	}	*(pos + 564) = cam->sensor.pix_format.width & 0xFF;	*(pos + 563) = (cam->sensor.pix_format.width >> 8) & 0xFF;	*(pos + 562) = cam->sensor.pix_format.height & 0xFF;	*(pos + 561) = (cam->sensor.pix_format.height >> 8) & 0xFF;	*(pos + 567) = 0x21;	f->buf.bytesused += sizeof(jpeg_header);}static void sn9c102_urb_complete(struct urb *urb){	struct sn9c102_device* cam = urb->context;	struct sn9c102_frame_t** f;	size_t imagesize, soflen;	u8 i;	int err = 0;	if (urb->status == -ENOENT)		return;	f = &cam->frame_current;	if (cam->stream == STREAM_INTERRUPT) {		cam->stream = STREAM_OFF;		if ((*f))			(*f)->state = F_QUEUED;		cam->sof.bytesread = 0;		DBG(3, "Stream interrupted by application");		wake_up(&cam->wait_stream);	}	if (cam->state & DEV_DISCONNECTED)		return;	if (cam->state & DEV_MISCONFIGURED) {		wake_up_interruptible(&cam->wait_frame);		return;	}	if (cam->stream == STREAM_OFF || list_empty(&cam->inqueue))		goto resubmit_urb;	if (!(*f))		(*f) = list_entry(cam->inqueue.next, struct sn9c102_frame_t,				  frame);	imagesize = (cam->sensor.pix_format.width *		     cam->sensor.pix_format.height *		     cam->sensor.pix_format.priv) / 8;	if (cam->sensor.pix_format.pixelformat == V4L2_PIX_FMT_JPEG)		imagesize += 589; /* length of jpeg header */	soflen = sn9c102_sof_length(cam);	for (i = 0; i < urb->number_of_packets; i++) {		unsigned int img, len, status;		void *pos, *sof, *eof;		len = urb->iso_frame_desc[i].actual_length;		status = urb->iso_frame_desc[i].status;		pos = urb->iso_frame_desc[i].offset + urb->transfer_buffer;		if (status) {			DBG(3, "Error in isochronous frame");			(*f)->state = F_ERROR;			cam->sof.bytesread = 0;			continue;		}		PDBGG("Isochrnous frame: length %u, #%u i", len, i);redo:		sof = sn9c102_find_sof_header(cam, pos, len);		if (likely(!sof)) {			eof = sn9c102_find_eof_header(cam, pos, len);			if ((*f)->state == F_GRABBING) {end_of_frame:				img = len;				if (eof)					img = (eof > pos) ? eof - pos - 1 : 0;				if ((*f)->buf.bytesused + img > imagesize) {					u32 b;					b = (*f)->buf.bytesused + img -					    imagesize;					img = imagesize - (*f)->buf.bytesused;					PDBGG("Expected EOF not found: video "					      "frame cut");					if (eof)						DBG(3, "Exceeded limit: +%u "						       "bytes", (unsigned)(b));				}				memcpy((*f)->bufmem + (*f)->buf.bytesused, pos,				       img);				if ((*f)->buf.bytesused == 0)					do_gettimeofday(&(*f)->buf.timestamp);				(*f)->buf.bytesused += img;				if ((*f)->buf.bytesused == imagesize ||				    ((cam->sensor.pix_format.pixelformat ==				      V4L2_PIX_FMT_SN9C10X ||				      cam->sensor.pix_format.pixelformat ==				      V4L2_PIX_FMT_JPEG) && eof)) {					u32 b;					b = (*f)->buf.bytesused;					(*f)->state = F_DONE;					(*f)->buf.sequence= ++cam->frame_count;					spin_lock(&cam->queue_lock);					list_move_tail(&(*f)->frame,						       &cam->outqueue);					if (!list_empty(&cam->inqueue))						(*f) = list_entry(							cam->inqueue.next,							struct sn9c102_frame_t,							frame );					else						(*f) = NULL;					spin_unlock(&cam->queue_lock);					memcpy(cam->sysfs.frame_header,					       cam->sof.header, soflen);					DBG(3, "Video frame captured: %lu "					       "bytes", (unsigned long)(b));					if (!(*f))						goto resubmit_urb;				} else if (eof) {					(*f)->state = F_ERROR;					DBG(3, "Not expected EOF after %lu "					       "bytes of image data",					    (unsigned long)					    ((*f)->buf.bytesused));				}				if (sof) /* (1) */					goto start_of_frame;			} else if (eof) {				DBG(3, "EOF without SOF");				continue;			} else {				PDBGG("Ignoring pointless isochronous frame");				continue;			}		} else if ((*f)->state == F_QUEUED || (*f)->state == F_ERROR) {start_of_frame:			(*f)->state = F_GRABBING;			(*f)->buf.bytesused = 0;			len -= (sof - pos);			pos = sof;			if (cam->sensor.pix_format.pixelformat ==			    V4L2_PIX_FMT_JPEG)				sn9c102_write_jpegheader(cam, (*f));			DBG(3, "SOF detected: new video frame");			if (len)				goto redo;		} else if ((*f)->state == F_GRABBING) {			eof = sn9c102_find_eof_header(cam, pos, len);			if (eof && eof < sof)				goto end_of_frame; /* (1) */			else {				if (cam->sensor.pix_format.pixelformat ==				    V4L2_PIX_FMT_SN9C10X ||				    cam->sensor.pix_format.pixelformat ==				    V4L2_PIX_FMT_JPEG) {					if (sof - pos >= soflen) {						eof = sof - soflen;					} else { /* remove header */						eof = pos;						(*f)->buf.bytesused -=							(soflen - (sof - pos));					}					goto end_of_frame;				} else {					DBG(3, "SOF before expected EOF after "					       "%lu bytes of image data",					    (unsigned long)					    ((*f)->buf.bytesused));					goto start_of_frame;				}			}		}	}resubmit_urb:	urb->dev = cam->usbdev;	err = usb_submit_urb(urb, GFP_ATOMIC);	if (err < 0 && err != -EPERM) {		cam->state |= DEV_MISCONFIGURED;		DBG(1, "usb_submit_urb() failed");	}	wake_up_interruptible(&cam->wait_frame);}static int sn9c102_start_transfer(struct sn9c102_device* cam){	struct usb_device *udev = cam->usbdev;	struct urb* urb;	struct usb_host_interface* altsetting = usb_altnum_to_altsetting(						    usb_ifnum_to_if(udev, 0),						    SN9C102_ALTERNATE_SETTING);	const unsigned int psz = le16_to_cpu(altsetting->					     endpoint[0].desc.wMaxPacketSize);	s8 i, j;	int err = 0;	for (i = 0; i < SN9C102_URBS; i++) {		cam->transfer_buffer[i] = kzalloc(SN9C102_ISO_PACKETS * psz,						  GFP_KERNEL);		if (!cam->transfer_buffer[i]) {			err = -ENOMEM;			DBG(1, "Not enough memory");			goto free_buffers;		}	}	for (i = 0; i < SN9C102_URBS; i++) {		urb = usb_alloc_urb(SN9C102_ISO_PACKETS, GFP_KERNEL);		cam->urb[i] = urb;		if (!urb) {			err = -ENOMEM;			DBG(1, "usb_alloc_urb() failed");			goto free_urbs;		}		urb->dev = udev;		urb->context = cam;		urb->pipe = usb_rcvisocpipe(udev, 1);		urb->transfer_flags = URB_ISO_ASAP;		urb->number_of_packets = SN9C102_ISO_PACKETS;		urb->complete = sn9c102_urb_complete;		urb->transfer_buffer = cam->transfer_buffer[i];		urb->transfer_buffer_length = psz * SN9C102_ISO_PACKETS;		urb->interval = 1;		for (j = 0; j < SN9C102_ISO_PACKETS; j++) {			urb->iso_frame_desc[j].offset = psz * j;			urb->iso_frame_desc[j].length = psz;		}	}	/* Enable video */	if (!(cam->reg[0x01] & 0x04)) {		err = sn9c102_write_reg(cam, cam->reg[0x01] | 0x04, 0x01);		if (err) {			err = -EIO;			DBG(1, "I/O hardware error");			goto free_urbs;		}	}	err = usb_set_interface(udev, 0, SN9C102_ALTERNATE_SETTING);	if (err) {		DBG(1, "usb_set_interface() failed");		goto free_urbs;	}	cam->frame_current = NULL;	cam->sof.bytesread = 0;	for (i = 0; i < SN9C102_URBS; i++) {		err = usb_submit_urb(cam->urb[i], GFP_KERNEL);		if (err) {			for (j = i-1; j >= 0; j--)				usb_kill_urb(cam->urb[j]);			DBG(1, "usb_submit_urb() failed, error %d", err);			goto free_urbs;		}	}	return 0;free_urbs:	for (i = 0; (i < SN9C102_URBS) && cam->urb[i]; i++)		usb_free_urb(cam->urb[i]);free_buffers:	for (i = 0; (i < SN9C102_URBS) && cam->transfer_buffer[i]; i++)		kfree(cam->transfer_buffer[i]);	return err;}static int sn9c102_stop_transfer(struct sn9c102_device* cam){	struct usb_device *udev = cam->usbdev;	s8 i;	int err = 0;	if (cam->state & DEV_DISCONNECTED)		return 0;	for (i = SN9C102_URBS-1; i >= 0; i--) {		usb_kill_urb(cam->urb[i]);		usb_free_urb(cam->urb[i]);		kfree(cam->transfer_buffer[i]);	}	err = usb_set_interface(udev, 0, 0); /* 0 Mb/s */	if (err)		DBG(3, "usb_set_interface() failed");	return err;}static int sn9c102_stream_interrupt(struct sn9c102_device* cam){	long timeout;	cam->stream = STREAM_INTERRUPT;	timeout = wait_event_timeout(cam->wait_stream,				     (cam->stream == STREAM_OFF) ||				     (cam->state & DEV_DISCONNECTED),				     SN9C102_URB_TIMEOUT);	if (cam->state & DEV_DISCONNECTED)		return -ENODEV;	else if (cam->stream != STREAM_OFF) {		cam->state |= DEV_MISCONFIGURED;		DBG(1, "URB timeout reached. The camera is misconfigured. "		       "To use it, close and open /dev/video%d again.",		    cam->v4ldev->minor);		return -EIO;	}	return 0;}/*****************************************************************************/#ifdef CONFIG_VIDEO_ADV_DEBUGstatic u16 sn9c102_strtou16(const char* buff, size_t len, ssize_t* count){	char str[7];	char* endp;	unsigned long val;	if (len < 6) {		strncpy(str, buff, len);		str[len] = '\0';	} else {		strncpy(str, buff, 6);		str[6] = '\0';	}	val = simple_strtoul(str, &endp, 0);	*count = 0;	if (val <= 0xffff)		*count = (ssize_t)(endp - str);	if ((*count) && (len == *count+1) && (buff[*count] == '\n'))		*count += 1;	return (u16)val;}/*   NOTE 1: being inside one of the following methods implies that the v4l	   device exists for sure (see kobjects and reference counters)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -