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

📄 cpia_usb.c

📁 cpia usb摄像头的驱动程序源码。需要video4linux和i2c-core支持
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * cpia_usb CPiA USB driver * * Supports CPiA based parallel port Video Camera's. * * Copyright (C) 1999        Jochen Scharrlach <Jochen.Scharrlach@schwaben.de> * Copyright (C) 1999, 2000  Johannes Erdfelt <johannes@erdfelt.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* define _CPIA_DEBUG_ for verbose debug output (see cpia.h) *//* #define _CPIA_DEBUG_  1 */  #include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/wait.h>#include <linux/list.h>#include <linux/slab.h>#include <linux/vmalloc.h>#include <linux/usb.h>#include "cpia.h"#define USB_REQ_CPIA_GRAB_FRAME			0xC1#define USB_REQ_CPIA_UPLOAD_FRAME		0xC2#define  WAIT_FOR_NEXT_FRAME			0#define  FORCE_FRAME_UPLOAD			1#define FRAMES_PER_DESC		10#define CPIA_NUMSBUF		2#define STREAM_BUF_SIZE		(PAGE_SIZE * 4)#define SCRATCH_BUF_SIZE	(STREAM_BUF_SIZE * 2)struct cpia_sbuf {	char *data;#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))	urb_t *urb;#else	struct urb *urb;#endif};#define FRAMEBUF_LEN (CPIA_MAX_FRAME_SIZE+100)enum framebuf_status {	FRAME_EMPTY,	FRAME_READING,	FRAME_READY,	FRAME_ERROR,};struct framebuf {	int length;	enum framebuf_status status;	u8 data[FRAMEBUF_LEN];	struct framebuf *next;};struct usb_cpia {	/* Device structure */	struct usb_device *dev;	unsigned char iface;	unsigned int alt;	wait_queue_head_t wq_stream;	int cursbuf;		/* Current receiving sbuf */	struct cpia_sbuf sbuf[CPIA_NUMSBUF];		/* Double buffering */	int streaming;	int open;	int present;	struct framebuf *buffers[3];	struct framebuf *curbuff, *workbuff;};static int cpia_usb_open(void *privdata, int alt);static int cpia_usb_registerCallback(void *privdata, void (*cb) (void *cbdata),			             void *cbdata);static int cpia_usb_transferCmd(void *privdata, u8 *command, u8 *data);static int cpia_usb_streamStart(void *privdata);static int cpia_usb_streamStop(void *privdata);static int cpia_usb_streamRead(void *privdata, u8 *frame, int noblock);static int cpia_usb_close(void *privdata);static int cpia_usb_upload_mode(void *privdata);#define ABOUT "USB driver for Vision CPiA based cameras"static struct cpia_camera_ops cpia_usb_ops = {	cpia_usb_open,	cpia_usb_registerCallback,	cpia_usb_transferCmd,	cpia_usb_streamStart,	cpia_usb_streamStop,	cpia_usb_streamRead,	cpia_usb_close,	CAM_TYPE_USB,	cpia_usb_upload_mode,	0,	THIS_MODULE};static LIST_HEAD(cam_list);static spinlock_t cam_list_lock_usb;#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))static void cpia_usb_complete(struct urb *urb)#elsestatic void cpia_usb_complete(struct urb *urb, struct pt_regs *regs)#endif{	int i;	char *cdata;	struct usb_cpia *ucpia;	if (!urb || !urb->context)		return;	ucpia = (struct usb_cpia *) urb->context;	if (!ucpia->dev || !ucpia->streaming || !ucpia->present || !ucpia->open)		return;	if (ucpia->workbuff->status == FRAME_EMPTY) {		ucpia->workbuff->status = FRAME_READING;		ucpia->workbuff->length = 0;	} 		  	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;		cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;		if (st)			printk(KERN_DEBUG "cpia data error: [%d] len=%d, status=%X\n", i, n, st);		if (FRAMEBUF_LEN < ucpia->workbuff->length + n) {			printk(KERN_DEBUG "cpia: scratch buf overflow!scr_len: %d, n: %d\n", ucpia->workbuff->length, n);			return;		}	    		if (n) {			if ((ucpia->workbuff->length > 0) || 			    (0x19 == cdata[0] && 0x68 == cdata[1])) {				memcpy(ucpia->workbuff->data + ucpia->workbuff->length, cdata, n);				ucpia->workbuff->length += n;			} else				DBG("Ignoring packet!\n");		} else {			if (ucpia->workbuff->length > 4 &&			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-1] &&			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-2] &&			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-3] &&			    0xff == ucpia->workbuff->data[ucpia->workbuff->length-4]) {				ucpia->workbuff->status = FRAME_READY;				ucpia->curbuff = ucpia->workbuff;				ucpia->workbuff = ucpia->workbuff->next;				ucpia->workbuff->status = FRAME_EMPTY;				ucpia->workbuff->length = 0;		  				if (waitqueue_active(&ucpia->wq_stream))					wake_up_interruptible(&ucpia->wq_stream);			}		}	}#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))	/* resubmit */	urb->dev = ucpia->dev;	if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)		printk(KERN_ERR "%s: usb_submit_urb ret %d\n", __FUNCTION__,  i);#endif}static int cpia_usb_upload_mode (void *privdata ){	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;	return ucpia->alt;}static int cpia_usb_open(void *privdata, int usb_alt_interface_setting ){	struct usb_cpia *ucpia = (struct usb_cpia *) privdata;#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))	urb_t *urb;#else	struct urb *urb;#endif	int ret, retval = 0, fx, err;	int alt, frame_size_per_desc;		if (!ucpia)		return -EINVAL;		/*select alternate usb interface setting */		switch (usb_alt_interface_setting) {	case 3:		frame_size_per_desc = CPIA_USB_MAX_PACKET_SIZE_ALT_3;		alt = 3;		break;	case 2:		frame_size_per_desc = CPIA_USB_MAX_PACKET_SIZE_ALT_2;		alt = 2;		break;	case 1:		frame_size_per_desc = CPIA_USB_MAX_PACKET_SIZE_ALT_1;		alt = 1;		break;	default:		return -EINVAL;		break;	}		ucpia->sbuf[0].data = kmalloc(FRAMES_PER_DESC * frame_size_per_desc, GFP_KERNEL);	if (!ucpia->sbuf[0].data)		return -EINVAL;	ucpia->sbuf[1].data = kmalloc(FRAMES_PER_DESC * frame_size_per_desc, GFP_KERNEL);	if (!ucpia->sbuf[1].data) {		retval = -EINVAL;		goto error_0;	}		ret = usb_set_interface(ucpia->dev, ucpia->iface, alt);	if (ret < 0) {		printk(KERN_ERR "cpia_usb_open: usb_set_interface error (ret = %d)\n", ret);		retval = -EBUSY;		goto error_1;	}	ucpia->alt = alt;	ucpia->buffers[0]->status = FRAME_EMPTY;	ucpia->buffers[0]->length = 0;	ucpia->buffers[1]->status = FRAME_EMPTY;	ucpia->buffers[1]->length = 0;	ucpia->buffers[2]->status = FRAME_EMPTY;	ucpia->buffers[2]->length = 0;	ucpia->curbuff = ucpia->buffers[0];	ucpia->workbuff = ucpia->buffers[1];#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	/* We double buffer the Iso lists */	urb = usb_alloc_urb(FRAMES_PER_DESC);#else	/* We double buffer the Iso lists, and also know the polling	 * interval is every frame (1 == (1 << (bInterval -1))).	 */	urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);#endif	if (!urb) {		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 0\n");		retval = -ENOMEM;		goto error_1;	}	ucpia->sbuf[0].urb = urb;	urb->dev = ucpia->dev;	urb->context = ucpia;	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	urb->transfer_flags = USB_ISO_ASAP;#else	urb->transfer_flags = URB_ISO_ASAP;#endif	urb->transfer_buffer = ucpia->sbuf[0].data;	urb->complete = cpia_usb_complete;	urb->number_of_packets = FRAMES_PER_DESC;#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))	urb->interval = 1;#endif	urb->transfer_buffer_length = frame_size_per_desc * FRAMES_PER_DESC;	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {		urb->iso_frame_desc[fx].offset = frame_size_per_desc * fx;		urb->iso_frame_desc[fx].length = frame_size_per_desc;	}#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	urb = usb_alloc_urb(FRAMES_PER_DESC);#else	urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);#endif	if (!urb) {		printk(KERN_ERR "cpia_init_isoc: usb_alloc_urb 1\n");		retval = -ENOMEM;		goto error_urb0;	}	ucpia->sbuf[1].urb = urb;	urb->dev = ucpia->dev;	urb->context = ucpia;	urb->pipe = usb_rcvisocpipe(ucpia->dev, 1);#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	urb->transfer_flags = USB_ISO_ASAP;#else	urb->transfer_flags = URB_ISO_ASAP;#endif	urb->transfer_buffer = ucpia->sbuf[1].data;	urb->complete = cpia_usb_complete;	urb->number_of_packets = FRAMES_PER_DESC;#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))	urb->interval = 1;#endif	urb->transfer_buffer_length = frame_size_per_desc * FRAMES_PER_DESC;	for (fx = 0; fx < FRAMES_PER_DESC; fx++) {		urb->iso_frame_desc[fx].offset = frame_size_per_desc * fx;		urb->iso_frame_desc[fx].length = frame_size_per_desc;	}#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	ucpia->sbuf[1].urb->next = ucpia->sbuf[0].urb;	ucpia->sbuf[0].urb->next = ucpia->sbuf[1].urb;		err = usb_submit_urb(ucpia->sbuf[0].urb);#else	/* queue the ISO urbs, and resubmit in the completion handler */	err = usb_submit_urb(ucpia->sbuf[0].urb, GFP_KERNEL);#endif	if (err) {		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 0 ret %d\n",			err);		goto error_urb1;	}#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))	err = usb_submit_urb(ucpia->sbuf[1].urb);#else	err = usb_submit_urb(ucpia->sbuf[1].urb, GFP_KERNEL);#endif	if (err) {		printk(KERN_ERR "cpia_init_isoc: usb_submit_urb 1 ret %d\n",			err);		goto error_urb1;	}	ucpia->streaming = 1;	ucpia->open = 1;	return 0;error_urb1:		/* free urb 1 */	usb_free_urb(ucpia->sbuf[1].urb);	ucpia->sbuf[1].urb = NULL;error_urb0:		/* free urb 0 */	usb_free_urb(ucpia->sbuf[0].urb);	ucpia->sbuf[0].urb = NULL;error_1:	kfree (ucpia->sbuf[1].data);	ucpia->sbuf[1].data = NULL;error_0:	kfree (ucpia->sbuf[0].data);	ucpia->sbuf[0].data = NULL;		return retval;}//// convenience functions///**************************************************************************** * *  WritePacket * ***************************************************************************/static int WritePacket(struct usb_device *udev, const u8 *packet, u8 *buf, size_t size){	if (!packet)		return -EINVAL;	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),			 packet[1] + (packet[0] << 8),			 USB_TYPE_VENDOR | USB_RECIP_DEVICE,			 packet[2] + (packet[3] << 8), 			 packet[4] + (packet[5] << 8), buf, size, HZ);}/**************************************************************************** * *  ReadPacket * ***************************************************************************/static int ReadPacket(struct usb_device *udev, u8 *packet, u8 *buf, size_t size){	if (!packet || size <= 0)		return -EINVAL;

⌨️ 快捷键说明

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