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

📄 video1394.c

📁 idt mips 32365上面移植实现的iee1394驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
		case VIDEO1394_BUFFER_QUEUED:			if (cmd == VIDEO1394_LISTEN_POLL_BUFFER) {			    /* for polling, return error code EINTR */			    spin_unlock_irqrestore(&d->lock, flags);			    return -EINTR;			}#if 1			while(d->buffer_status[v.buffer]!=			      VIDEO1394_BUFFER_READY) {				spin_unlock_irqrestore(&d->lock, flags);				interruptible_sleep_on(&d->waitq);				spin_lock_irqsave(&d->lock, flags);				if(signal_pending(current)) {					spin_unlock_irqrestore(&d->lock,flags);					return -EINTR;				}			}#else			if (wait_event_interruptible(d->waitq, 						     d->buffer_status[v.buffer]						     == VIDEO1394_BUFFER_READY)			    == -ERESTARTSYS)				return -EINTR;#endif			d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;			break;		default:			PRINT(KERN_ERR, ohci->id, 			      "Buffer %d is not queued",v.buffer);			spin_unlock_irqrestore(&d->lock, flags);			return -EFAULT;		}		/* set time of buffer */		v.filltime = d->buffer_time[v.buffer];//		printk("Buffer %d time %d\n", v.buffer, (d->buffer_time[v.buffer]).tv_usec);		/*		 * Look ahead to see how many more buffers have been received		 */		i=0;		while (d->buffer_status[(v.buffer+1)%d->num_desc]==		       VIDEO1394_BUFFER_READY) {			v.buffer=(v.buffer+1)%d->num_desc;			i++;		}		spin_unlock_irqrestore(&d->lock, flags);		v.buffer=i;		if(copy_to_user((void *)arg, &v, sizeof(v)))			return -EFAULT;		return 0;	}	case VIDEO1394_TALK_QUEUE_BUFFER:	{		struct video1394_wait v;		struct video1394_queue_variable qv;		struct dma_iso_ctx *d;		int i;		if(copy_from_user(&v, (void *)arg, sizeof(v)))			return -EFAULT;		i = it_ctx_talking(video, v.channel);		if (i<0) return -EFAULT;		d = video->it_context[i];		if ((v.buffer<0) || (v.buffer>d->num_desc)) {			PRINT(KERN_ERR, ohci->id, 			      "Buffer %d out of range",v.buffer);			return -EFAULT;		}				if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {			if (copy_from_user(&qv, (void *)arg, sizeof(qv))) 				return -EFAULT;			if (!access_ok(VERIFY_READ, qv.packet_sizes, 				       d->nb_cmd * sizeof(unsigned int))) {				return -EFAULT;			}		}		spin_lock_irqsave(&d->lock,flags);		if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {			PRINT(KERN_ERR, ohci->id, 			      "Buffer %d is already used",v.buffer);			spin_unlock_irqrestore(&d->lock,flags);			return -EFAULT;		}				if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {			initialize_dma_it_prg_var_packet_queue(				d, v.buffer, qv.packet_sizes,				ohci);		}		d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;		if (d->last_buffer>=0) {			d->it_prg[d->last_buffer]				[ d->last_used_cmd[d->last_buffer]					].end.branchAddress = 				(virt_to_bus(&(d->it_prg[v.buffer][0].begin.control)) 				 & 0xfffffff0) | 0x3;			d->it_prg[d->last_buffer]				[d->last_used_cmd[d->last_buffer]					].begin.branchAddress = 				(virt_to_bus(&(d->it_prg[v.buffer][0].begin.control)) 				 & 0xfffffff0) | 0x3;			d->next_buffer[d->last_buffer] = v.buffer;		}		d->last_buffer = v.buffer;		d->next_buffer[d->last_buffer] = -1;		d->it_prg[d->last_buffer][d->last_used_cmd[d->last_buffer]].end.branchAddress = 0;		spin_unlock_irqrestore(&d->lock,flags);		if (!(reg_read(ohci, d->ctrlSet) & 0x8000)) 		{			DBGMSG(ohci->id, "Starting iso transmit DMA ctx=%d",			       d->ctx);			put_timestamp(ohci, d, d->last_buffer);			/* Tell the controller where the first program is */			reg_write(ohci, d->cmdPtr, 				  virt_to_bus(&(d->it_prg[v.buffer][0]))|0x3);						/* Run IT context */			reg_write(ohci, d->ctrlSet, 0x8000);		}		else {			/* Wake up dma context if necessary */			if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {				PRINT(KERN_INFO, ohci->id, 				      "Waking up iso transmit dma ctx=%d", 				      d->ctx);				put_timestamp(ohci, d, d->last_buffer);				reg_write(ohci, d->ctrlSet, 0x1000);			}		}		return 0;			}	case VIDEO1394_TALK_WAIT_BUFFER:	{		struct video1394_wait v;		struct dma_iso_ctx *d;		int i;		if(copy_from_user(&v, (void *)arg, sizeof(v)))			return -EFAULT;		i = it_ctx_talking(video, v.channel);		if (i<0) return -EFAULT;		d = video->it_context[i];		if ((v.buffer<0) || (v.buffer>d->num_desc)) {			PRINT(KERN_ERR, ohci->id, 			      "Buffer %d out of range",v.buffer);			return -EFAULT;		}		switch(d->buffer_status[v.buffer]) {		case VIDEO1394_BUFFER_READY:			d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;			return 0;		case VIDEO1394_BUFFER_QUEUED:#if 1			while(d->buffer_status[v.buffer]!=			      VIDEO1394_BUFFER_READY) {				interruptible_sleep_on(&d->waitq);				if(signal_pending(current)) return -EINTR;			}#else			if (wait_event_interruptible(d->waitq, 						     d->buffer_status[v.buffer]						     == VIDEO1394_BUFFER_READY)			    == -ERESTARTSYS)				return -EINTR;#endif			d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;			return 0;		default:			PRINT(KERN_ERR, ohci->id, 			      "Buffer %d is not queued",v.buffer);			return -EFAULT;		}	}	default:		return -EINVAL;	}}/* *	This maps the vmalloced and reserved buffer to user space. * *  FIXME:  *  - PAGE_READONLY should suffice!? *  - remap_page_range is kind of inefficient for page by page remapping. *    But e.g. pte_alloc() does not work in modules ... :-( */int video1394_mmap(struct file *file, struct vm_area_struct *vma){	struct video_card *video = NULL;	struct ti_ohci *ohci;	int res = -EINVAL;	unsigned long flags;	struct list_head *lh;        spin_lock_irqsave(&video1394_cards_lock, flags);        if (!list_empty(&video1394_cards)) {		struct video_card *p;		list_for_each(lh, &video1394_cards) {			p = list_entry(lh, struct video_card, list);			if (p->id == MINOR(file->f_dentry->d_inode->i_rdev)) {				video = p;				break;			}                }        }        spin_unlock_irqrestore(&video1394_cards_lock, flags);	if (video == NULL) {		PRINT_G(KERN_ERR, __FUNCTION__": Unknown video card for minor %d",			MINOR(file->f_dentry->d_inode->i_rdev));		return -EFAULT;	}	lock_kernel();	ohci = video->ohci;	if (video->current_ctx == NULL) {		PRINT(KERN_ERR, ohci->id, "Current iso context not set");	} else		res = do_iso_mmap(ohci, video->current_ctx, 			   (char *)vma->vm_start, 			   (unsigned long)(vma->vm_end-vma->vm_start));	unlock_kernel();	return res;}static int video1394_open(struct inode *inode, struct file *file){	int i = MINOR(inode->i_rdev);	unsigned long flags;	struct video_card *video = NULL;	struct list_head *lh;	spin_lock_irqsave(&video1394_cards_lock, flags);	if (!list_empty(&video1394_cards)) {		struct video_card *p;		list_for_each(lh, &video1394_cards) {			p = list_entry(lh, struct video_card, list);			if (p->id == i) {				video = p;				break;			}		}	}	spin_unlock_irqrestore(&video1394_cards_lock, flags);        if (video == NULL)                return -EIO;	V22_COMPAT_MOD_INC_USE_COUNT;	return 0;}static int video1394_release(struct inode *inode, struct file *file){	struct video_card *video = NULL;	struct ti_ohci *ohci;	u64 mask;	int i;	unsigned long flags;	struct list_head *lh;        spin_lock_irqsave(&video1394_cards_lock, flags);        if (!list_empty(&video1394_cards)) {		struct video_card *p;		list_for_each(lh, &video1394_cards) {			p = list_entry(lh, struct video_card, list);			if (p->id == MINOR(inode->i_rdev)) {				video = p;				break;			}		}	}        spin_unlock_irqrestore(&video1394_cards_lock, flags);	if (video == NULL) {		PRINT_G(KERN_ERR, __FUNCTION__": Unknown device for minor %d",				MINOR(inode->i_rdev));		return 1;	}	ohci = video->ohci;	lock_kernel();	for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++) 		if (video->ir_context[i]) {			mask = (u64)0x1<<video->ir_context[i]->channel;			if (!(ohci->ISO_channel_usage & mask))				PRINT(KERN_ERR, ohci->id, 				      "Channel %d is not being used", 				      video->ir_context[i]->channel);			else				ohci->ISO_channel_usage &= ~mask;			PRINT(KERN_INFO, ohci->id, 			      "Iso receive context %d stop listening "			      "on channel %d", i+1, 			      video->ir_context[i]->channel);			free_dma_iso_ctx(&video->ir_context[i]);		}		for (i=0;i<ohci->nb_iso_xmit_ctx;i++) 		if (video->it_context[i]) {			mask = (u64)0x1<<video->it_context[i]->channel;			if (!(ohci->ISO_channel_usage & mask))				PRINT(KERN_ERR, ohci->id, 				      "Channel %d is not being used", 				      video->it_context[i]->channel);			else				ohci->ISO_channel_usage &= ~mask;			PRINT(KERN_INFO, ohci->id, 			      "Iso transmit context %d stop talking "			      "on channel %d", i+1, 			      video->it_context[i]->channel);			free_dma_iso_ctx(&video->it_context[i]);		}	V22_COMPAT_MOD_DEC_USE_COUNT;	unlock_kernel();	return 0;}static void irq_handler(int card, quadlet_t isoRecvIntEvent, 		 quadlet_t isoXmitIntEvent){	int i;	unsigned long flags;	struct video_card *video = NULL;	struct list_head *lh;	spin_lock_irqsave(&video1394_cards_lock, flags);	if (!list_empty(&video1394_cards)) {		struct video_card *p;		list_for_each(lh, &video1394_cards) {			p = list_entry(lh, struct video_card, list);			if (p->id == card) {				video = p;				break;			}                }        }        spin_unlock_irqrestore(&video1394_cards_lock, flags);	if (video == NULL) {		PRINT_G(KERN_ERR, __FUNCTION__": Unknown card number %d!!",				card);		return;	}		DBGMSG(card, "Iso event Recv: %08x Xmit: %08x",	       isoRecvIntEvent, isoXmitIntEvent);	for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++)		if (isoRecvIntEvent & (1<<(i+1))) 			wakeup_dma_ir_ctx(video->ohci,					  video->ir_context[i]);	for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++)		if (isoXmitIntEvent & (1<<i)) 			wakeup_dma_it_ctx(video->ohci,					  video->it_context[i]);}static struct file_operations video1394_fops={        OWNER_THIS_MODULE	ioctl:		video1394_ioctl,	mmap:		video1394_mmap,	open:		video1394_open,	release:	video1394_release};static int video1394_init(struct ti_ohci *ohci){	struct video_card *video = kmalloc(sizeof(struct video_card), GFP_KERNEL);	unsigned long flags;	char name[16];	if (video == NULL) {		PRINT(KERN_ERR, ohci->id, "Cannot allocate video_card");		return -1;	}	memset(video, 0, sizeof(struct video_card));	spin_lock_irqsave(&video1394_cards_lock, flags);	INIT_LIST_HEAD(&video->list);	list_add_tail(&video->list, &video1394_cards);	spin_unlock_irqrestore(&video1394_cards_lock, flags);	if (ohci1394_register_video(ohci, &video_tmpl)<0) {		PRINT(KERN_ERR, ohci->id, "Register_video failed");		return -1;	}	video->id = ohci->id;	video->ohci = ohci;	/* Iso receive dma contexts */	video->ir_context = (struct dma_iso_ctx **)		kmalloc((ohci->nb_iso_rcv_ctx-1)*			sizeof(struct dma_iso_ctx *), GFP_KERNEL);	if (video->ir_context) 		memset(video->ir_context, 0,		       (ohci->nb_iso_rcv_ctx-1)*sizeof(struct dma_iso_ctx *));	else {		PRINT(KERN_ERR, ohci->id, "Cannot allocate ir_context");		return -1;	}	/* Iso transmit dma contexts */	video->it_context = (struct dma_iso_ctx **)		kmalloc(ohci->nb_iso_xmit_ctx *			sizeof(struct dma_iso_ctx *), GFP_KERNEL);	if (video->it_context) 		memset(video->it_context, 0,		       ohci->nb_iso_xmit_ctx * sizeof(struct dma_iso_ctx *));	else {		PRINT(KERN_ERR, ohci->id, "Cannot allocate it_context");		return -1;	}	sprintf(name, "%d", video->id);	video->devfs = devfs_register(devfs_handle, name,				      DEVFS_FL_AUTO_OWNER,				      VIDEO1394_MAJOR, 0,				      S_IFCHR | S_IRUSR | S_IWUSR,				      &video1394_fops, NULL);	return 0;}/* Must be called under spinlock */static void remove_card(struct video_card *video){	int i;	ohci1394_unregister_video(video->ohci, &video_tmpl);	devfs_unregister(video->devfs);	/* Free the iso receive contexts */	if (video->ir_context) {		for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++) {			free_dma_iso_ctx(&video->ir_context[i]);		}		kfree(video->ir_context);	}	/* Free the iso transmit contexts */	if (video->it_context) {		for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++) {			free_dma_iso_ctx(&video->it_context[i]);		}		kfree(video->it_context);	}	list_del(&video->list);	kfree(video);}static void video1394_remove_host (struct hpsb_host *host){	struct ti_ohci *ohci;	unsigned long flags;	struct list_head *lh, *next;	struct video_card *p;	/* We only work with the OHCI-1394 driver */	if (strcmp(host->template->name, OHCI1394_DRIVER_NAME))		return;	ohci = (struct ti_ohci *)host->hostdata;        spin_lock_irqsave(&video1394_cards_lock, flags);	list_for_each_safe(lh, next, &video1394_cards) {		p = list_entry(lh, struct video_card, list);		if (p->ohci == ohci) {			remove_card(p);			break;		}	}	spin_unlock_irqrestore(&video1394_cards_lock, flags);	return;}static void video1394_add_host (struct hpsb_host *host){	struct ti_ohci *ohci;	/* We only work with the OHCI-1394 driver */	if (strcmp(host->template->name, OHCI1394_DRIVER_NAME))		return;	ohci = (struct ti_ohci *)host->hostdata;	video1394_init(ohci);		return;}static struct hpsb_highlevel_ops hl_ops = {	add_host:	video1394_add_host,	remove_host:	video1394_remove_host,};MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");MODULE_DESCRIPTION("driver for digital video on OHCI board");MODULE_SUPPORTED_DEVICE(VIDEO1394_DRIVER_NAME);MODULE_LICENSE("GPL");static void __exit video1394_exit_module (void){	hpsb_unregister_highlevel (hl_handle);	devfs_unregister(devfs_handle);	devfs_unregister_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME);	PRINT_G(KERN_INFO, "Removed " VIDEO1394_DRIVER_NAME " module");}static int __init video1394_init_module (void){	if (devfs_register_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME,				&video1394_fops)) {		PRINT_G(KERN_ERR, "video1394: unable to get major %d\n",				VIDEO1394_MAJOR);		return -EIO;	}#if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)	devfs_handle = devfs_mk_dir(NULL, VIDEO1394_DRIVER_NAME,			strlen(VIDEO1394_DRIVER_NAME), NULL);#else	devfs_handle = devfs_mk_dir(NULL, VIDEO1394_DRIVER_NAME, NULL);#endif	hl_handle = hpsb_register_highlevel (VIDEO1394_DRIVER_NAME, &hl_ops);	if (hl_handle == NULL) {		PRINT_G(KERN_ERR, "No more memory for driver\n");		devfs_unregister(devfs_handle);		devfs_unregister_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME);		return -ENOMEM;	}	PRINT_G(KERN_INFO, "Installed " VIDEO1394_DRIVER_NAME " module");	return 0;}module_init(video1394_init_module);module_exit(video1394_exit_module);

⌨️ 快捷键说明

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