📄 video1394.c
字号:
for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++) if (video->ir_context[i]) { if (video->ir_context[i]->channel==channel) return i; } PRINT(KERN_ERR, ohci->id, "No iso context is listening to channel %d", channel); return -1;}int it_ctx_talking(struct video_card *video, int channel){ int i; struct ti_ohci *ohci = video->ohci; for (i=0;i<ohci->nb_iso_xmit_ctx;i++) if (video->it_context[i]) { if (video->it_context[i]->channel==channel) return i; } PRINT(KERN_ERR, ohci->id, "No iso context is talking to channel %d", channel); return -1;}int wakeup_dma_ir_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d) { int i; if (d==NULL) { PRINT(KERN_ERR, ohci->id, "Iso receive event received but " "context not allocated"); return -EFAULT; } spin_lock(&d->lock); for (i=0;i<d->num_desc;i++) { if (d->ir_prg[i][d->nb_cmd-1].status & 0xFFFF0000) { reset_ir_status(d, i); d->buffer_status[i] = VIDEO1394_BUFFER_READY; do_gettimeofday(&d->buffer_time[i]); } } spin_unlock(&d->lock); if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq); return 0;}static inline void put_timestamp(struct ti_ohci *ohci, struct dma_iso_ctx * d, int n){ unsigned char* buf = d->buf + n * d->buf_size; u32 cycleTimer; u32 timeStamp; if (n == -1) { return; } cycleTimer = reg_read(ohci, OHCI1394_IsochronousCycleTimer); timeStamp = ((cycleTimer & 0x0fff) + d->syt_offset); /* 11059 = 450 us */ timeStamp = (timeStamp % 3072 + ((timeStamp / 3072) << 12) + (cycleTimer & 0xf000)) & 0xffff; buf[6] = timeStamp >> 8; buf[7] = timeStamp & 0xff; /* if first packet is empty packet, then put timestamp into the next full one too */ if ( (d->it_prg[n][0].data[1] >>16) == 0x008) { buf += d->packet_size; buf[6] = timeStamp >> 8; buf[7] = timeStamp & 0xff; } /* do the next buffer frame too in case of irq latency */ n = d->next_buffer[n]; if (n == -1) { return; } buf = d->buf + n * d->buf_size; timeStamp += (d->last_used_cmd[n] << 12) & 0xffff; buf[6] = timeStamp >> 8; buf[7] = timeStamp & 0xff; /* if first packet is empty packet, then put timestamp into the next full one too */ if ( (d->it_prg[n][0].data[1] >>16) == 0x008) { buf += d->packet_size; buf[6] = timeStamp >> 8; buf[7] = timeStamp & 0xff; }#if 0 printk("curr: %d, next: %d, cycleTimer: %08x timeStamp: %08x\n", curr, n, cycleTimer, timeStamp);#endif }int wakeup_dma_it_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d) { int i; if (d==NULL) { PRINT(KERN_ERR, ohci->id, "Iso transmit event received but " "context not allocated"); return -EFAULT; } spin_lock(&d->lock); for (i=0;i<d->num_desc;i++) { if (d->it_prg[i][d->last_used_cmd[i]].end.status& 0xFFFF0000) { int next = d->next_buffer[i]; put_timestamp(ohci, d, next); d->it_prg[i][d->last_used_cmd[i]].end.status = 0; d->buffer_status[i] = VIDEO1394_BUFFER_READY; } } spin_unlock(&d->lock); if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq); return 0;}static void initialize_dma_it_prg(struct dma_iso_ctx *d, int n, int sync_tag){ struct it_dma_prg *it_prg = d->it_prg[n]; unsigned long buf = (unsigned long)d->buf+n*d->buf_size; int i; d->last_used_cmd[n] = d->nb_cmd - 1; for (i=0;i<d->nb_cmd;i++) { it_prg[i].begin.control = DMA_CTL_OUTPUT_MORE | DMA_CTL_IMMEDIATE | 8 ; it_prg[i].begin.address = 0; it_prg[i].begin.status = 0; it_prg[i].data[0] = (SPEED_100 << 16) | (/* tag */ 1 << 14) | (d->channel << 8) | (TCODE_ISO_DATA << 4); if (i==0) it_prg[i].data[0] |= sync_tag; it_prg[i].data[1] = d->packet_size << 16; it_prg[i].data[2] = 0; it_prg[i].data[3] = 0; it_prg[i].end.control = DMA_CTL_OUTPUT_LAST | DMA_CTL_BRANCH; it_prg[i].end.address = kvirt_to_bus(buf+i*d->packet_size); if (i<d->nb_cmd-1) { it_prg[i].end.control |= d->packet_size; it_prg[i].begin.branchAddress = (virt_to_bus(&(it_prg[i+1].begin.control)) & 0xfffffff0) | 0x3; it_prg[i].end.branchAddress = (virt_to_bus(&(it_prg[i+1].begin.control)) & 0xfffffff0) | 0x3; } else { /* the last prg generates an interrupt */ it_prg[i].end.control |= DMA_CTL_UPDATE | DMA_CTL_IRQ | d->left_size; /* the last prg doesn't branch */ it_prg[i].begin.branchAddress = 0; it_prg[i].end.branchAddress = 0; } it_prg[i].end.status = 0;#if 0 printk("%d:%d: %08x-%08x ctrl %08x brch %08x d0 %08x d1 %08x\n",n,i, virt_to_bus(&(it_prg[i].begin.control)), virt_to_bus(&(it_prg[i].end.control)), it_prg[i].end.control, it_prg[i].end.branchAddress, it_prg[i].data[0], it_prg[i].data[1]);#endif }}static void initialize_dma_it_prg_var_packet_queue( struct dma_iso_ctx *d, int n, unsigned int * packet_sizes, struct ti_ohci *ohci){ struct it_dma_prg *it_prg = d->it_prg[n]; int i;#if 0 if (n != -1) { put_timestamp(ohci, d, n); }#endif d->last_used_cmd[n] = d->nb_cmd - 1; for (i = 0; i < d->nb_cmd; i++) { unsigned int size; if (packet_sizes[i] > d->packet_size) { size = d->packet_size; } else { size = packet_sizes[i]; } it_prg[i].data[1] = size << 16; it_prg[i].end.control = DMA_CTL_OUTPUT_LAST | DMA_CTL_BRANCH; if (i < d->nb_cmd-1 && packet_sizes[i+1] != 0) { it_prg[i].end.control |= size; it_prg[i].begin.branchAddress = (virt_to_bus(&(it_prg[i+1].begin.control)) & 0xfffffff0) | 0x3; it_prg[i].end.branchAddress = (virt_to_bus(&(it_prg[i+1].begin.control)) & 0xfffffff0) | 0x3; } else { /* the last prg generates an interrupt */ it_prg[i].end.control |= DMA_CTL_UPDATE | DMA_CTL_IRQ | size; /* the last prg doesn't branch */ it_prg[i].begin.branchAddress = 0; it_prg[i].end.branchAddress = 0; d->last_used_cmd[n] = i; break; } }}static void initialize_dma_it_ctx(struct dma_iso_ctx *d, int sync_tag, unsigned int syt_offset, int flags){ struct ti_ohci *ohci = (struct ti_ohci *)d->ohci; int i; d->flags = flags; d->syt_offset = (syt_offset == 0 ? 11000 : syt_offset); ohci1394_stop_context(ohci, d->ctrlClear, NULL); for (i=0;i<d->num_desc;i++) initialize_dma_it_prg(d, i, sync_tag); /* Set up isoRecvIntMask to generate interrupts */ reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1<<d->ctx);}static int do_iso_mmap(struct ti_ohci *ohci, struct dma_iso_ctx *d, const char *adr, unsigned long size){ unsigned long start=(unsigned long) adr; unsigned long page,pos; if (size>d->num_desc * d->buf_size) { PRINT(KERN_ERR, ohci->id, "iso context %d buf size is different from mmap size", d->ctx); return -EINVAL; } if (!d->buf) { PRINT(KERN_ERR, ohci->id, "iso context %d is not allocated", d->ctx); return -EINVAL; } pos=(unsigned long) d->buf; while (size > 0) { page = kvirt_to_pa(pos); if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED)) return -EAGAIN; start+=PAGE_SIZE; pos+=PAGE_SIZE; size-=PAGE_SIZE; } return 0;}static int video1394_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct video_card *video = NULL; struct ti_ohci *ohci = NULL; 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; ohci = video->ohci; break; } } } spin_unlock_irqrestore(&video1394_cards_lock, flags); if (video == NULL) { PRINT_G(KERN_ERR, __FUNCTION__": Unknown video card for minor %d", MINOR(inode->i_rdev)); return -EFAULT; } switch(cmd) { case VIDEO1394_LISTEN_CHANNEL: case VIDEO1394_TALK_CHANNEL: { struct video1394_mmap v; u64 mask; int i; if(copy_from_user(&v, (void *)arg, sizeof(v))) return -EFAULT; /* if channel < 0, find lowest available one */ if (v.channel < 0) { mask = (u64)0x1; for (i=0; i<ISO_CHANNELS; i++) { if (!(ohci->ISO_channel_usage & mask)) { v.channel = i; PRINT(KERN_INFO, ohci->id, "Found free channel %d\n", i); break; } mask = mask << 1; } } if (v.channel<0 || v.channel>(ISO_CHANNELS-1)) { PRINT(KERN_ERR, ohci->id, "Iso channel %d out of bounds", v.channel); return -EFAULT; } mask = (u64)0x1<<v.channel; printk("mask: %08X%08X usage: %08X%08X\n", (u32)(mask>>32),(u32)(mask&0xffffffff), (u32)(ohci->ISO_channel_usage>>32), (u32)(ohci->ISO_channel_usage&0xffffffff)); if (ohci->ISO_channel_usage & mask) { PRINT(KERN_ERR, ohci->id, "Channel %d is already taken", v.channel); return -EFAULT; } ohci->ISO_channel_usage |= mask; if (v.buf_size<=0) { PRINT(KERN_ERR, ohci->id, "Invalid %d length buffer requested",v.buf_size); return -EFAULT; } if (v.nb_buffers<=0) { PRINT(KERN_ERR, ohci->id, "Invalid %d buffers requested",v.nb_buffers); return -EFAULT; } if (v.nb_buffers * v.buf_size > VIDEO1394_MAX_SIZE) { PRINT(KERN_ERR, ohci->id, "%d buffers of size %d bytes is too big", v.nb_buffers, v.buf_size); return -EFAULT; } if (cmd == VIDEO1394_LISTEN_CHANNEL) { /* find a free iso receive context */ for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++) if (video->ir_context[i]==NULL) break; if (i==(ohci->nb_iso_rcv_ctx-1)) { PRINT(KERN_ERR, ohci->id, "No iso context available"); return -EFAULT; } video->ir_context[i] = alloc_dma_iso_ctx(ohci, ISO_RECEIVE, i+1, v.nb_buffers, v.buf_size, v.channel, 0); if (video->ir_context[i] == NULL) { PRINT(KERN_ERR, ohci->id, "Couldn't allocate ir context"); return -EFAULT; } initialize_dma_ir_ctx(video->ir_context[i], v.sync_tag, v.flags); video->current_ctx = video->ir_context[i]; v.buf_size = video->ir_context[i]->buf_size; PRINT(KERN_INFO, ohci->id, "iso context %d listen on channel %d", i+1, v.channel); } else { /* find a free iso transmit context */ for (i=0;i<ohci->nb_iso_xmit_ctx;i++) if (video->it_context[i]==NULL) break; if (i==ohci->nb_iso_xmit_ctx) { PRINT(KERN_ERR, ohci->id, "No iso context available"); return -EFAULT; } video->it_context[i] = alloc_dma_iso_ctx(ohci, ISO_TRANSMIT, i, v.nb_buffers, v.buf_size, v.channel, v.packet_size); if (video->it_context[i] == NULL) { PRINT(KERN_ERR, ohci->id, "Couldn't allocate it context"); return -EFAULT; } initialize_dma_it_ctx(video->it_context[i], v.sync_tag, v.syt_offset, v.flags); video->current_ctx = video->it_context[i]; v.buf_size = video->it_context[i]->buf_size; PRINT(KERN_INFO, ohci->id, "Iso context %d talk on channel %d", i, v.channel); } if(copy_to_user((void *)arg, &v, sizeof(v))) return -EFAULT; return 0; } case VIDEO1394_UNLISTEN_CHANNEL: case VIDEO1394_UNTALK_CHANNEL: { int channel; u64 mask; int i; if(copy_from_user(&channel, (void *)arg, sizeof(int))) return -EFAULT; if (channel<0 || channel>(ISO_CHANNELS-1)) { PRINT(KERN_ERR, ohci->id, "Iso channel %d out of bound", channel); return -EFAULT; } mask = (u64)0x1<<channel; if (!(ohci->ISO_channel_usage & mask)) { PRINT(KERN_ERR, ohci->id, "Channel %d is not being used", channel); return -EFAULT; } ohci->ISO_channel_usage &= ~mask; if (cmd == VIDEO1394_UNLISTEN_CHANNEL) { i = ir_ctx_listening(video, channel); if (i<0) return -EFAULT; free_dma_iso_ctx(&video->ir_context[i]); PRINT(KERN_INFO, ohci->id, "Iso context %d stop listening on channel %d", i+1, channel); } else { i = it_ctx_talking(video, channel); if (i<0) return -EFAULT; free_dma_iso_ctx(&video->it_context[i]); PRINT(KERN_INFO, ohci->id, "Iso context %d stop talking on channel %d", i, channel); } return 0; } case VIDEO1394_LISTEN_QUEUE_BUFFER: { struct video1394_wait v; struct dma_iso_ctx *d; int i; if(copy_from_user(&v, (void *)arg, sizeof(v))) return -EFAULT; i = ir_ctx_listening(video, v.channel); if (i<0) return -EFAULT; d = video->ir_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; } spin_lock_irqsave(&d->lock,flags); if (d->buffer_status[v.buffer]==VIDEO1394_BUFFER_QUEUED) { PRINT(KERN_ERR, ohci->id, "Buffer %d is already used",v.buffer); spin_unlock_irqrestore(&d->lock,flags); return -EFAULT; } d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED; if (d->last_buffer>=0) d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = (virt_to_bus(&(d->ir_prg[v.buffer][0].control)) & 0xfffffff0) | 0x1; d->last_buffer = v.buffer; d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = 0; spin_unlock_irqrestore(&d->lock,flags); if (!(reg_read(ohci, d->ctrlSet) & 0x8000)) { DBGMSG(ohci->id, "Starting iso DMA ctx=%d",d->ctx); /* Tell the controller where the first program is */ reg_write(ohci, d->cmdPtr, virt_to_bus(&(d->ir_prg[v.buffer][0]))|0x1); /* Run IR 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 dma ctx=%d", d->ctx); reg_write(ohci, d->ctrlSet, 0x1000); } } return 0; } case VIDEO1394_LISTEN_WAIT_BUFFER: case VIDEO1394_LISTEN_POLL_BUFFER: { struct video1394_wait v; struct dma_iso_ctx *d; int i; if(copy_from_user(&v, (void *)arg, sizeof(v))) return -EFAULT; i = ir_ctx_listening(video, v.channel); if (i<0) return -EFAULT; d = video->ir_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; } /* * I change the way it works so that it returns * the last received frame. */ spin_lock_irqsave(&d->lock, flags); switch(d->buffer_status[v.buffer]) { case VIDEO1394_BUFFER_READY: d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -