- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / ieee1394 / video1394.c
blobd8d82a2a6e33292974a7968e43eaf779c15ada90
1 /*
2 * video1394.c - video driver for OHCI 1394 boards
3 * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/wait.h>
25 #include <linux/errno.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/fs.h>
29 #include <linux/poll.h>
30 #include <linux/smp_lock.h>
31 #include <asm/byteorder.h>
32 #include <asm/atomic.h>
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35 #include <linux/proc_fs.h>
36 #include <linux/tqueue.h>
37 #include <linux/delay.h>
39 #include <asm/pgtable.h>
40 #include <asm/page.h>
41 #include <linux/sched.h>
42 #include <asm/segment.h>
43 #include <linux/types.h>
44 #include <linux/wrapper.h>
45 #include <linux/vmalloc.h>
47 #include "ieee1394.h"
48 #include "ieee1394_types.h"
49 #include "hosts.h"
50 #include "ieee1394_core.h"
51 #include "video1394.h"
53 #include "ohci1394.h"
55 #define VIDEO1394_MAJOR 172
56 #define ISO_CHANNELS 64
57 #define ISO_RECEIVE 0
58 #define ISO_TRANSMIT 1
60 #ifndef virt_to_page
61 #define virt_to_page(x) MAP_NR(x)
62 #endif
64 struct it_dma_prg {
65 struct dma_cmd begin;
66 quadlet_t data[4];
67 struct dma_cmd end;
68 quadlet_t pad[4]; /* FIXME: quick hack for memory alignment */
71 struct dma_iso_ctx {
72 struct ti_ohci *ohci;
73 int ctx;
74 int channel;
75 int last_buffer;
76 unsigned int num_desc;
77 unsigned int buf_size;
78 unsigned int frame_size;
79 unsigned int packet_size;
80 unsigned int left_size;
81 unsigned int nb_cmd;
82 unsigned char *buf;
83 struct dma_cmd **ir_prg;
84 struct it_dma_prg **it_prg;
85 unsigned int *buffer_status;
86 int ctrlClear;
87 int ctrlSet;
88 int cmdPtr;
89 int ctxMatch;
90 wait_queue_head_t waitq;
91 spinlock_t lock;
94 struct video_card {
95 struct ti_ohci *ohci;
97 struct dma_iso_ctx **ir_context;
98 struct dma_iso_ctx **it_context;
99 struct dma_iso_ctx *current_ctx;
102 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
103 #define VIDEO1394_DEBUG
104 #endif
106 #ifdef DBGMSG
107 #undef DBGMSG
108 #endif
110 #ifdef VIDEO1394_DEBUG
111 #define DBGMSG(card, fmt, args...) \
112 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
113 #else
114 #define DBGMSG(card, fmt, args...)
115 #endif
117 /* print general (card independent) information */
118 #define PRINT_G(level, fmt, args...) \
119 printk(level "video1394: " fmt "\n" , ## args)
121 /* print card specific information */
122 #define PRINT(level, card, fmt, args...) \
123 printk(level "video1394_%d: " fmt "\n" , card , ## args)
125 void irq_handler(int card, quadlet_t isoRecvIntEvent,
126 quadlet_t isoXmitIntEvent);
128 static struct video_card video_cards[MAX_OHCI1394_CARDS];
129 static int num_of_video_cards = 0;
130 static struct video_template video_tmpl = { irq_handler };
132 /* Taken from bttv.c */
133 /*******************************/
134 /* Memory management functions */
135 /*******************************/
137 #define MDEBUG(x) do { } while(0) /* Debug memory management */
139 /* [DaveM] I've recoded most of this so that:
140 * 1) It's easier to tell what is happening
141 * 2) It's more portable, especially for translating things
142 * out of vmalloc mapped areas in the kernel.
143 * 3) Less unnecessary translations happen.
145 * The code used to assume that the kernel vmalloc mappings
146 * existed in the page tables of every process, this is simply
147 * not guarenteed. We now use pgd_offset_k which is the
148 * defined way to get at the kernel page tables.
151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
152 #define page_address(x) (x)
153 #endif
155 /* Given PGD from the address space's page table, return the kernel
156 * virtual mapping of the physical memory mapped at ADR.
158 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
160 unsigned long ret = 0UL;
161 pmd_t *pmd;
162 pte_t *ptep, pte;
164 if (!pgd_none(*pgd)) {
165 pmd = pmd_offset(pgd, adr);
166 if (!pmd_none(*pmd)) {
167 ptep = pte_offset(pmd, adr);
168 pte = *ptep;
169 if(pte_present(pte)) {
170 ret = (unsigned long) page_address(pte_page(pte));
171 ret |= (adr & (PAGE_SIZE - 1));
175 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
176 return ret;
179 static inline unsigned long uvirt_to_bus(unsigned long adr)
181 unsigned long kva, ret;
183 kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
184 ret = virt_to_bus((void *)kva);
185 MDEBUG(printk("uv2b(%lx-->%lx)", adr, ret));
186 return ret;
189 static inline unsigned long kvirt_to_bus(unsigned long adr)
191 unsigned long va, kva, ret;
193 va = VMALLOC_VMADDR(adr);
194 kva = uvirt_to_kva(pgd_offset_k(va), va);
195 ret = virt_to_bus((void *)kva);
196 MDEBUG(printk("kv2b(%lx-->%lx)", adr, ret));
197 return ret;
200 /* Here we want the physical address of the memory.
201 * This is used when initializing the contents of the
202 * area and marking the pages as reserved.
204 static inline unsigned long kvirt_to_pa(unsigned long adr)
206 unsigned long va, kva, ret;
208 va = VMALLOC_VMADDR(adr);
209 kva = uvirt_to_kva(pgd_offset_k(va), va);
210 ret = __pa(kva);
211 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
212 return ret;
215 static void * rvmalloc(unsigned long size)
217 void * mem;
218 unsigned long adr, page;
220 mem=vmalloc_32(size);
221 if (mem)
223 memset(mem, 0, size); /* Clear the ram out,
224 no junk to the user */
225 adr=(unsigned long) mem;
226 while (size > 0)
228 page = kvirt_to_pa(adr);
229 mem_map_reserve(virt_to_page(__va(page)));
230 adr+=PAGE_SIZE;
231 size-=PAGE_SIZE;
234 return mem;
237 static void rvfree(void * mem, unsigned long size)
239 unsigned long adr, page;
241 if (mem)
243 adr=(unsigned long) mem;
244 while (size > 0)
246 page = kvirt_to_pa(adr);
247 mem_map_unreserve(virt_to_page(__va(page)));
248 adr+=PAGE_SIZE;
249 size-=PAGE_SIZE;
251 vfree(mem);
255 static int free_dma_iso_ctx(struct dma_iso_ctx **d)
257 int i;
258 struct ti_ohci *ohci;
260 if ((*d)==NULL) return -1;
262 ohci = (struct ti_ohci *)(*d)->ohci;
264 DBGMSG(ohci->id, "Freeing dma_iso_ctx %d", (*d)->ctx);
266 ohci1394_stop_context(ohci, (*d)->ctrlClear, NULL);
268 if ((*d)->buf) rvfree((void *)(*d)->buf,
269 (*d)->num_desc * (*d)->buf_size);
271 if ((*d)->ir_prg) {
272 for (i=0;i<(*d)->num_desc;i++)
273 if ((*d)->ir_prg[i]) kfree((*d)->ir_prg[i]);
274 kfree((*d)->ir_prg);
277 if ((*d)->it_prg) {
278 for (i=0;i<(*d)->num_desc;i++)
279 if ((*d)->it_prg[i]) kfree((*d)->it_prg[i]);
280 kfree((*d)->it_prg);
283 if ((*d)->buffer_status)
284 kfree((*d)->buffer_status);
286 kfree(*d);
287 *d = NULL;
289 return 0;
292 static struct dma_iso_ctx *
293 alloc_dma_iso_ctx(struct ti_ohci *ohci, int type, int ctx, int num_desc,
294 int buf_size, int channel, unsigned int packet_size)
296 struct dma_iso_ctx *d=NULL;
297 int i;
299 d = (struct dma_iso_ctx *)kmalloc(sizeof(struct dma_iso_ctx),
300 GFP_KERNEL);
301 memset(d, 0, sizeof(struct dma_iso_ctx));
303 if (d==NULL) {
304 PRINT(KERN_ERR, ohci->id, "failed to allocate dma_iso_ctx");
305 return NULL;
308 d->ohci = (void *)ohci;
309 d->ctx = ctx;
310 d->channel = channel;
311 d->num_desc = num_desc;
312 d->frame_size = buf_size;
313 if (buf_size%PAGE_SIZE)
314 d->buf_size = buf_size + PAGE_SIZE - (buf_size%PAGE_SIZE);
315 else
316 d->buf_size = buf_size;
317 d->last_buffer = -1;
318 d->buf = NULL;
319 d->ir_prg = NULL;
320 init_waitqueue_head(&d->waitq);
322 d->buf = rvmalloc(d->num_desc * d->buf_size);
324 if (d->buf == NULL) {
325 PRINT(KERN_ERR, ohci->id, "failed to allocate dma buffer");
326 free_dma_iso_ctx(&d);
327 return NULL;
329 memset(d->buf, 0, d->num_desc * d->buf_size);
331 if (type == ISO_RECEIVE) {
332 d->ctrlSet = OHCI1394_IsoRcvContextControlSet+32*d->ctx;
333 d->ctrlClear = OHCI1394_IsoRcvContextControlClear+32*d->ctx;
334 d->cmdPtr = OHCI1394_IsoRcvCommandPtr+32*d->ctx;
335 d->ctxMatch = OHCI1394_IsoRcvContextMatch+32*d->ctx;
337 d->ir_prg = kmalloc(d->num_desc * sizeof(struct dma_cmd *),
338 GFP_KERNEL);
340 if (d->ir_prg == NULL) {
341 PRINT(KERN_ERR, ohci->id,
342 "failed to allocate dma ir prg");
343 free_dma_iso_ctx(&d);
344 return NULL;
346 memset(d->ir_prg, 0, d->num_desc * sizeof(struct dma_cmd *));
348 d->nb_cmd = d->buf_size / PAGE_SIZE + 1;
349 d->left_size = (d->frame_size % PAGE_SIZE) ?
350 d->frame_size % PAGE_SIZE : PAGE_SIZE;
352 for (i=0;i<d->num_desc;i++) {
353 d->ir_prg[i] = kmalloc(d->nb_cmd *
354 sizeof(struct dma_cmd),
355 GFP_KERNEL);
356 if (d->ir_prg[i] == NULL) {
357 PRINT(KERN_ERR, ohci->id,
358 "failed to allocate dma ir prg");
359 free_dma_iso_ctx(&d);
360 return NULL;
364 else { /* ISO_TRANSMIT */
365 d->ctrlSet = OHCI1394_IsoXmitContextControlSet+16*d->ctx;
366 d->ctrlClear = OHCI1394_IsoXmitContextControlClear+16*d->ctx;
367 d->cmdPtr = OHCI1394_IsoXmitCommandPtr+16*d->ctx;
369 d->it_prg = kmalloc(d->num_desc * sizeof(struct it_dma_prg *),
370 GFP_KERNEL);
372 if (d->it_prg == NULL) {
373 PRINT(KERN_ERR, ohci->id,
374 "failed to allocate dma it prg");
375 free_dma_iso_ctx(&d);
376 return NULL;
378 memset(d->it_prg, 0, d->num_desc*sizeof(struct it_dma_prg *));
380 d->packet_size = packet_size;
382 if (PAGE_SIZE % packet_size || packet_size>4096) {
383 PRINT(KERN_ERR, ohci->id,
384 "Packet size %d not yet supported\n",
385 packet_size);
386 free_dma_iso_ctx(&d);
387 return NULL;
390 d->nb_cmd = d->frame_size / d->packet_size;
391 if (d->frame_size % d->packet_size) {
392 d->nb_cmd++;
393 d->left_size = d->frame_size % d->packet_size;
395 else
396 d->left_size = d->packet_size;
398 for (i=0;i<d->num_desc;i++) {
399 d->it_prg[i] = kmalloc(d->nb_cmd *
400 sizeof(struct it_dma_prg),
401 GFP_KERNEL);
402 if (d->it_prg[i] == NULL) {
403 PRINT(KERN_ERR, ohci->id,
404 "failed to allocate dma it prg");
405 free_dma_iso_ctx(&d);
406 return NULL;
411 d->buffer_status = kmalloc(d->num_desc * sizeof(unsigned int),
412 GFP_KERNEL);
414 if (d->buffer_status == NULL) {
415 PRINT(KERN_ERR, ohci->id, "failed to allocate dma ir prg");
416 free_dma_iso_ctx(&d);
417 return NULL;
419 memset(d->buffer_status, 0, d->num_desc * sizeof(unsigned int));
421 spin_lock_init(&d->lock);
423 PRINT(KERN_INFO, ohci->id, "Iso %s DMA: %d buffers "
424 "of size %d allocated for a frame size %d, each with %d prgs",
425 (type==ISO_RECEIVE) ? "receive" : "transmit",
426 d->num_desc, d->buf_size, d->frame_size, d->nb_cmd);
428 return d;
431 static void reset_ir_status(struct dma_iso_ctx *d, int n)
433 int i;
434 d->ir_prg[n][0].status = 4;
435 d->ir_prg[n][1].status = PAGE_SIZE-4;
436 for (i=2;i<d->nb_cmd-1;i++)
437 d->ir_prg[n][i].status = PAGE_SIZE;
438 d->ir_prg[n][i].status = d->left_size;
441 static void initialize_dma_ir_prg(struct dma_iso_ctx *d, int n)
443 struct dma_cmd *ir_prg = d->ir_prg[n];
444 unsigned long buf = (unsigned long)d->buf+n*d->buf_size;
445 int i;
447 /* the first descriptor will sync and read only 4 bytes */
448 ir_prg[0].control = (0x280F << 16) | 4;
449 ir_prg[0].address = kvirt_to_bus(buf);
450 ir_prg[0].branchAddress = (virt_to_bus(&(ir_prg[1].control))
451 & 0xfffffff0) | 0x1;
453 /* the second descriptor will read PAGE_SIZE-4 bytes */
454 ir_prg[1].control = (0x280C << 16) | (PAGE_SIZE-4);
455 ir_prg[1].address = kvirt_to_bus(buf+4);
456 ir_prg[1].branchAddress = (virt_to_bus(&(ir_prg[2].control))
457 & 0xfffffff0) | 0x1;
459 for (i=2;i<d->nb_cmd-1;i++) {
460 ir_prg[i].control = (0x280C << 16) | PAGE_SIZE;
461 ir_prg[i].address = kvirt_to_bus(buf+(i-1)*PAGE_SIZE);
463 ir_prg[i].branchAddress =
464 (virt_to_bus(&(ir_prg[i+1].control))
465 & 0xfffffff0) | 0x1;
468 /* the last descriptor will generate an interrupt */
469 ir_prg[i].control = (0x283C << 16) | d->left_size;
470 ir_prg[i].address = kvirt_to_bus(buf+(i-1)*PAGE_SIZE);
473 static void initialize_dma_ir_ctx(struct dma_iso_ctx *d, int tag)
475 struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
476 int i;
478 ohci1394_stop_context(ohci, d->ctrlClear, NULL);
480 for (i=0;i<d->num_desc;i++) {
481 initialize_dma_ir_prg(d, i);
482 reset_ir_status(d, i);
485 /* Set bufferFill, no header */
486 reg_write(ohci, d->ctrlSet, 0x80000000);
488 /* Set the context match register to match on all tags,
489 sync for sync tag, and listen to d->channel */
490 reg_write(ohci, d->ctxMatch, 0xf0000000|((tag&0xf)<<8)|d->channel);
492 /* Set up isoRecvIntMask to generate interrupts */
493 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1<<d->ctx);
496 /* find which context is listening to this channel */
497 int ir_ctx_listening(struct video_card *video, int channel)
499 int i;
500 struct ti_ohci *ohci = video->ohci;
502 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
503 if (video->ir_context[i]) {
504 if (video->ir_context[i]->channel==channel)
505 return i;
508 PRINT(KERN_ERR, ohci->id,
509 "no iso context is listening to channel %d",
510 channel);
511 return -1;
514 int it_ctx_talking(struct video_card *video, int channel)
516 int i;
517 struct ti_ohci *ohci = video->ohci;
519 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
520 if (video->it_context[i]) {
521 if (video->it_context[i]->channel==channel)
522 return i;
525 PRINT(KERN_ERR, ohci->id,
526 "no iso context is talking to channel %d",
527 channel);
528 return -1;
531 int wakeup_dma_ir_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
533 int i;
535 if (d==NULL) {
536 PRINT(KERN_ERR, ohci->id, "Iso receive event received but "
537 "context not allocated");
538 return -EFAULT;
541 spin_lock(&d->lock);
542 for (i=0;i<d->num_desc;i++) {
543 if (d->ir_prg[i][d->nb_cmd-1].status & 0xFFFF0000) {
544 reset_ir_status(d, i);
545 d->buffer_status[i] = VIDEO1394_BUFFER_READY;
548 spin_unlock(&d->lock);
549 if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq);
550 return 0;
553 int wakeup_dma_it_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
555 int i;
557 if (d==NULL) {
558 PRINT(KERN_ERR, ohci->id, "Iso transmit event received but "
559 "context not allocated");
560 return -EFAULT;
563 spin_lock(&d->lock);
564 for (i=0;i<d->num_desc;i++) {
565 if (d->it_prg[i][d->nb_cmd-1].end.status & 0xFFFF0000) {
566 d->it_prg[i][d->nb_cmd-1].end.status = 0;
567 d->buffer_status[i] = VIDEO1394_BUFFER_READY;
570 spin_unlock(&d->lock);
571 if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq);
572 return 0;
575 static void initialize_dma_it_prg(struct dma_iso_ctx *d, int n, int sync_tag)
577 struct it_dma_prg *it_prg = d->it_prg[n];
578 unsigned long buf = (unsigned long)d->buf+n*d->buf_size;
579 int i;
581 for (i=0;i<d->nb_cmd;i++) {
583 it_prg[i].begin.control = OUTPUT_MORE_IMMEDIATE | 8 ;
584 it_prg[i].begin.address = 0;
586 it_prg[i].begin.status = 0;
588 /* FIXME: what is the tag value + speed selection */
589 it_prg[i].data[0] =
590 (DMA_SPEED_400<<16) | (d->channel<<8) | 0xa0;
591 if (i==0) it_prg[i].data[0] |= sync_tag;
592 it_prg[i].data[1] = d->packet_size << 16;
593 it_prg[i].data[2] = 0;
594 it_prg[i].data[3] = 0;
596 it_prg[i].end.control = 0x100c0000;
597 it_prg[i].end.address =
598 kvirt_to_bus(buf+i*d->packet_size);
600 if (i<d->nb_cmd-1) {
601 it_prg[i].end.control |= d->packet_size;
602 it_prg[i].begin.branchAddress =
603 (virt_to_bus(&(it_prg[i+1].begin.control))
604 & 0xfffffff0) | 0x3;
605 it_prg[i].end.branchAddress =
606 (virt_to_bus(&(it_prg[i+1].begin.control))
607 & 0xfffffff0) | 0x3;
609 else {
610 /* the last prg generates an interrupt */
611 it_prg[i].end.control |= 0x08300000 | d->left_size;
612 /* the last prg doesn't branch */
613 it_prg[i].begin.branchAddress = 0;
614 it_prg[i].end.branchAddress = 0;
616 it_prg[i].end.status = 0;
618 #if 0
619 printk("%d:%d: %08x-%08x ctrl %08x brch %08x d0 %08x d1 %08x\n",n,i,
620 virt_to_bus(&(it_prg[i].begin.control)),
621 virt_to_bus(&(it_prg[i].end.control)),
622 it_prg[i].end.control,
623 it_prg[i].end.branchAddress,
624 it_prg[i].data[0], it_prg[i].data[1]);
625 #endif
629 static void initialize_dma_it_ctx(struct dma_iso_ctx *d, int sync_tag)
631 struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
632 int i;
634 ohci1394_stop_context(ohci, d->ctrlClear, NULL);
636 for (i=0;i<d->num_desc;i++)
637 initialize_dma_it_prg(d, i, sync_tag);
639 /* Set up isoRecvIntMask to generate interrupts */
640 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1<<d->ctx);
643 static int do_iso_mmap(struct ti_ohci *ohci, struct dma_iso_ctx *d,
644 const char *adr, unsigned long size)
646 unsigned long start=(unsigned long) adr;
647 unsigned long page,pos;
649 if (size>d->num_desc * d->buf_size) {
650 PRINT(KERN_ERR, ohci->id,
651 "iso context %d buf size is different from mmap size",
652 d->ctx);
653 return -EINVAL;
655 if (!d->buf) {
656 PRINT(KERN_ERR, ohci->id,
657 "iso context %d is not allocated", d->ctx);
658 return -EINVAL;
661 pos=(unsigned long) d->buf;
662 while (size > 0) {
663 page = kvirt_to_pa(pos);
664 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
665 return -EAGAIN;
666 start+=PAGE_SIZE;
667 pos+=PAGE_SIZE;
668 size-=PAGE_SIZE;
670 return 0;
673 static int video1394_ioctl(struct inode *inode, struct file *file,
674 unsigned int cmd, unsigned long arg)
676 struct video_card *video = &video_cards[MINOR(inode->i_rdev)];
677 struct ti_ohci *ohci= video->ohci;
678 unsigned long flags;
680 switch(cmd)
682 case VIDEO1394_LISTEN_CHANNEL:
683 case VIDEO1394_TALK_CHANNEL:
685 struct video1394_mmap v;
686 int i;
688 if(copy_from_user(&v, (void *)arg, sizeof(v)))
689 return -EFAULT;
690 if (v.channel<0 || v.channel>(ISO_CHANNELS-1)) {
691 PRINT(KERN_ERR, ohci->id,
692 "iso channel %d out of bound", v.channel);
693 return -EFAULT;
695 if (test_and_set_bit(v.channel, &ohci->IR_channel_usage)) {
696 PRINT(KERN_ERR, ohci->id,
697 "channel %d is already taken", v.channel);
698 return -EFAULT;
701 if (v.buf_size<=0) {
702 PRINT(KERN_ERR, ohci->id,
703 "Invalid %d length buffer requested",v.buf_size);
704 return -EFAULT;
707 if (v.nb_buffers<=0) {
708 PRINT(KERN_ERR, ohci->id,
709 "Invalid %d buffers requested",v.nb_buffers);
710 return -EFAULT;
713 if (v.nb_buffers * v.buf_size > VIDEO1394_MAX_SIZE) {
714 PRINT(KERN_ERR, ohci->id,
715 "%d buffers of size %d bytes is too big",
716 v.nb_buffers, v.buf_size);
717 return -EFAULT;
720 if (cmd == VIDEO1394_LISTEN_CHANNEL) {
721 /* find a free iso receive context */
722 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
723 if (video->ir_context[i]==NULL) break;
725 if (i==(ohci->nb_iso_rcv_ctx-1)) {
726 PRINT(KERN_ERR, ohci->id,
727 "no iso context available");
728 return -EFAULT;
731 video->ir_context[i] =
732 alloc_dma_iso_ctx(ohci, ISO_RECEIVE, i+1,
733 v.nb_buffers, v.buf_size,
734 v.channel, 0);
736 if (video->ir_context[i] == NULL) {
737 PRINT(KERN_ERR, ohci->id,
738 "Couldn't allocate ir context");
739 return -EFAULT;
741 initialize_dma_ir_ctx(video->ir_context[i],
742 v.sync_tag);
744 video->current_ctx = video->ir_context[i];
746 v.buf_size = video->ir_context[i]->buf_size;
748 PRINT(KERN_INFO, ohci->id,
749 "iso context %d listen on channel %d", i+1,
750 v.channel);
752 else {
753 /* find a free iso transmit context */
754 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
755 if (video->it_context[i]==NULL) break;
757 if (i==ohci->nb_iso_xmit_ctx) {
758 PRINT(KERN_ERR, ohci->id,
759 "no iso context available");
760 return -EFAULT;
763 video->it_context[i] =
764 alloc_dma_iso_ctx(ohci, ISO_TRANSMIT, i,
765 v.nb_buffers, v.buf_size,
766 v.channel, v.packet_size);
768 if (video->it_context[i] == NULL) {
769 PRINT(KERN_ERR, ohci->id,
770 "Couldn't allocate it context");
771 return -EFAULT;
773 initialize_dma_it_ctx(video->it_context[i],
774 v.sync_tag);
776 video->current_ctx = video->it_context[i];
778 v.buf_size = video->it_context[i]->buf_size;
780 PRINT(KERN_INFO, ohci->id,
781 "iso context %d talk on channel %d", i,
782 v.channel);
785 if(copy_to_user((void *)arg, &v, sizeof(v)))
786 return -EFAULT;
788 return 0;
790 case VIDEO1394_UNLISTEN_CHANNEL:
791 case VIDEO1394_UNTALK_CHANNEL:
793 int channel;
794 int i;
796 if(copy_from_user(&channel, (void *)arg, sizeof(int)))
797 return -EFAULT;
799 if (!test_and_clear_bit(channel, &ohci->IR_channel_usage)) {
800 PRINT(KERN_ERR, ohci->id,
801 "channel %d is not being used", channel);
802 return -EFAULT;
805 if (cmd == VIDEO1394_UNLISTEN_CHANNEL) {
806 i = ir_ctx_listening(video, channel);
807 if (i<0) return -EFAULT;
809 free_dma_iso_ctx(&video->ir_context[i]);
811 PRINT(KERN_INFO, ohci->id,
812 "iso context %d stop listening on channel %d",
813 i+1, channel);
815 else {
816 i = it_ctx_talking(video, channel);
817 if (i<0) return -EFAULT;
819 free_dma_iso_ctx(&video->it_context[i]);
821 PRINT(KERN_INFO, ohci->id,
822 "iso context %d stop talking on channel %d",
823 i, channel);
826 return 0;
828 case VIDEO1394_LISTEN_QUEUE_BUFFER:
830 struct video1394_wait v;
831 struct dma_iso_ctx *d;
832 int i;
834 if(copy_from_user(&v, (void *)arg, sizeof(v)))
835 return -EFAULT;
837 i = ir_ctx_listening(video, v.channel);
838 if (i<0) return -EFAULT;
839 d = video->ir_context[i];
841 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
842 PRINT(KERN_ERR, ohci->id,
843 "buffer %d out of range",v.buffer);
844 return -EFAULT;
847 spin_lock_irqsave(&d->lock,flags);
849 if (d->buffer_status[v.buffer]==VIDEO1394_BUFFER_QUEUED) {
850 PRINT(KERN_ERR, ohci->id,
851 "buffer %d is already used",v.buffer);
852 spin_unlock_irqrestore(&d->lock,flags);
853 return -EFAULT;
856 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
858 if (d->last_buffer>=0)
859 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress =
860 (virt_to_bus(&(d->ir_prg[v.buffer][0].control))
861 & 0xfffffff0) | 0x1;
863 d->last_buffer = v.buffer;
865 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = 0;
867 spin_unlock_irqrestore(&d->lock,flags);
869 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
871 DBGMSG(ohci->id, "Starting iso DMA ctx=%d",d->ctx);
873 /* Tell the controller where the first program is */
874 reg_write(ohci, d->cmdPtr,
875 virt_to_bus(&(d->ir_prg[v.buffer][0]))|0x1);
877 /* Run IR context */
878 reg_write(ohci, d->ctrlSet, 0x8000);
880 else {
881 /* Wake up dma context if necessary */
882 if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
883 PRINT(KERN_INFO, ohci->id,
884 "Waking up iso dma ctx=%d", d->ctx);
885 reg_write(ohci, d->ctrlSet, 0x1000);
888 return 0;
891 case VIDEO1394_LISTEN_WAIT_BUFFER:
893 struct video1394_wait v;
894 struct dma_iso_ctx *d;
895 int i;
897 if(copy_from_user(&v, (void *)arg, sizeof(v)))
898 return -EFAULT;
900 i = ir_ctx_listening(video, v.channel);
901 if (i<0) return -EFAULT;
902 d = video->ir_context[i];
904 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
905 PRINT(KERN_ERR, ohci->id,
906 "buffer %d out of range",v.buffer);
907 return -EFAULT;
911 * I change the way it works so that it returns
912 * the last received frame.
914 spin_lock_irqsave(&d->lock, flags);
915 switch(d->buffer_status[v.buffer]) {
916 case VIDEO1394_BUFFER_READY:
917 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
918 break;
919 case VIDEO1394_BUFFER_QUEUED:
920 #if 1
921 while(d->buffer_status[v.buffer]!=
922 VIDEO1394_BUFFER_READY) {
923 spin_unlock_irqrestore(&d->lock, flags);
924 interruptible_sleep_on(&d->waitq);
925 spin_lock_irqsave(&d->lock, flags);
926 if(signal_pending(current)) {
927 spin_unlock_irqrestore(&d->lock,flags);
928 return -EINTR;
931 #else
932 if (wait_event_interruptible(d->waitq,
933 d->buffer_status[v.buffer]
934 == VIDEO1394_BUFFER_READY)
935 == -ERESTARTSYS)
936 return -EINTR;
937 #endif
938 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
939 break;
940 default:
941 PRINT(KERN_ERR, ohci->id,
942 "buffer %d is not queued",v.buffer);
943 spin_unlock_irqrestore(&d->lock, flags);
944 return -EFAULT;
948 * Look ahead to see how many more buffers have been received
950 i=0;
951 while (d->buffer_status[(v.buffer+1)%d->num_desc]==
952 VIDEO1394_BUFFER_READY) {
953 v.buffer=(v.buffer+1)%d->num_desc;
954 i++;
956 spin_unlock_irqrestore(&d->lock, flags);
958 v.buffer=i;
959 if(copy_to_user((void *)arg, &v, sizeof(v)))
960 return -EFAULT;
962 return 0;
964 case VIDEO1394_TALK_QUEUE_BUFFER:
966 struct video1394_wait v;
967 struct dma_iso_ctx *d;
968 int i;
970 if(copy_from_user(&v, (void *)arg, sizeof(v)))
971 return -EFAULT;
973 i = it_ctx_talking(video, v.channel);
974 if (i<0) return -EFAULT;
975 d = video->it_context[i];
977 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
978 PRINT(KERN_ERR, ohci->id,
979 "buffer %d out of range",v.buffer);
980 return -EFAULT;
983 spin_lock_irqsave(&d->lock,flags);
985 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
986 PRINT(KERN_ERR, ohci->id,
987 "buffer %d is already used",v.buffer);
988 spin_unlock_irqrestore(&d->lock,flags);
989 return -EFAULT;
992 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
994 if (d->last_buffer>=0) {
995 d->it_prg[d->last_buffer]
996 [d->nb_cmd-1].end.branchAddress =
997 (virt_to_bus(&(d->it_prg[v.buffer][0].begin.control))
998 & 0xfffffff0) | 0x3;
1000 d->it_prg[d->last_buffer]
1001 [d->nb_cmd-1].begin.branchAddress =
1002 (virt_to_bus(&(d->it_prg[v.buffer][0].begin.control))
1003 & 0xfffffff0) | 0x3;
1005 d->last_buffer = v.buffer;
1007 d->it_prg[d->last_buffer][d->nb_cmd-1].end.branchAddress = 0;
1009 spin_unlock_irqrestore(&d->lock,flags);
1011 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
1013 DBGMSG(ohci->id, "Starting iso transmit DMA ctx=%d",
1014 d->ctx);
1016 /* Tell the controller where the first program is */
1017 reg_write(ohci, d->cmdPtr,
1018 virt_to_bus(&(d->it_prg[v.buffer][0]))|0x3);
1020 /* Run IT context */
1021 reg_write(ohci, d->ctrlSet, 0x8000);
1023 else {
1024 /* Wake up dma context if necessary */
1025 if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
1026 PRINT(KERN_INFO, ohci->id,
1027 "Waking up iso transmit dma ctx=%d",
1028 d->ctx);
1029 reg_write(ohci, d->ctrlSet, 0x1000);
1032 return 0;
1035 case VIDEO1394_TALK_WAIT_BUFFER:
1037 struct video1394_wait v;
1038 struct dma_iso_ctx *d;
1039 int i;
1041 if(copy_from_user(&v, (void *)arg, sizeof(v)))
1042 return -EFAULT;
1044 i = it_ctx_talking(video, v.channel);
1045 if (i<0) return -EFAULT;
1046 d = video->it_context[i];
1048 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1049 PRINT(KERN_ERR, ohci->id,
1050 "buffer %d out of range",v.buffer);
1051 return -EFAULT;
1054 switch(d->buffer_status[v.buffer]) {
1055 case VIDEO1394_BUFFER_READY:
1056 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1057 return 0;
1058 case VIDEO1394_BUFFER_QUEUED:
1059 #if 1
1060 while(d->buffer_status[v.buffer]!=
1061 VIDEO1394_BUFFER_READY) {
1062 interruptible_sleep_on(&d->waitq);
1063 if(signal_pending(current)) return -EINTR;
1065 #else
1066 if (wait_event_interruptible(d->waitq,
1067 d->buffer_status[v.buffer]
1068 == VIDEO1394_BUFFER_READY)
1069 == -ERESTARTSYS)
1070 return -EINTR;
1071 #endif
1072 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1073 return 0;
1074 default:
1075 PRINT(KERN_ERR, ohci->id,
1076 "buffer %d is not queued",v.buffer);
1077 return -EFAULT;
1080 default:
1081 return -EINVAL;
1086 * This maps the vmalloced and reserved buffer to user space.
1088 * FIXME:
1089 * - PAGE_READONLY should suffice!?
1090 * - remap_page_range is kind of inefficient for page by page remapping.
1091 * But e.g. pte_alloc() does not work in modules ... :-(
1094 int video1394_mmap(struct file *file, struct vm_area_struct *vma)
1096 struct video_card *video =
1097 &video_cards[MINOR(file->f_dentry->d_inode->i_rdev)];
1098 struct ti_ohci *ohci;
1099 int res = -EINVAL;
1101 lock_kernel();
1102 ohci = video->ohci;
1103 PRINT(KERN_INFO, ohci->id, "mmap");
1104 if (video->current_ctx == NULL) {
1105 PRINT(KERN_ERR, ohci->id, "current iso context not set");
1106 } else
1107 res = do_iso_mmap(ohci, video->current_ctx,
1108 (char *)vma->vm_start,
1109 (unsigned long)(vma->vm_end-vma->vm_start));
1110 unlock_kernel();
1111 return res;
1114 static int video1394_open(struct inode *inode, struct file *file)
1116 int i = MINOR(inode->i_rdev);
1118 if (i<0 || i>=num_of_video_cards) {
1119 PRINT(KERN_ERR, i, "ohci card %d not found", i);
1120 return -EIO;
1123 V22_COMPAT_MOD_INC_USE_COUNT;
1125 PRINT(KERN_INFO, i, "open");
1127 return 0;
1130 static int video1394_release(struct inode *inode, struct file *file)
1132 struct video_card *video = &video_cards[MINOR(inode->i_rdev)];
1133 struct ti_ohci *ohci= video->ohci;
1134 int i;
1136 lock_kernel();
1137 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
1138 if (video->ir_context[i]) {
1139 if (!test_and_clear_bit(
1140 video->ir_context[i]->channel,
1141 &ohci->IR_channel_usage)) {
1142 PRINT(KERN_ERR, ohci->id,
1143 "channel %d is not being used",
1144 video->ir_context[i]->channel);
1146 PRINT(KERN_INFO, ohci->id,
1147 "iso receive context %d stop listening "
1148 "on channel %d", i+1,
1149 video->ir_context[i]->channel);
1150 free_dma_iso_ctx(&video->ir_context[i]);
1153 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
1154 if (video->it_context[i]) {
1155 if (!test_and_clear_bit(
1156 video->it_context[i]->channel,
1157 &ohci->IR_channel_usage)) {
1158 PRINT(KERN_ERR, ohci->id,
1159 "channel %d is not being used",
1160 video->it_context[i]->channel);
1162 PRINT(KERN_INFO, ohci->id,
1163 "iso transmit context %d stop talking "
1164 "on channel %d", i+1,
1165 video->it_context[i]->channel);
1166 free_dma_iso_ctx(&video->it_context[i]);
1170 V22_COMPAT_MOD_DEC_USE_COUNT;
1172 PRINT(KERN_INFO, ohci->id, "release");
1173 unlock_kernel();
1174 return 0;
1177 void irq_handler(int card, quadlet_t isoRecvIntEvent,
1178 quadlet_t isoXmitIntEvent)
1180 int i;
1181 struct video_card *video = &video_cards[card];
1183 DBGMSG(card, "Iso event Recv: %08x Xmit: %08x",
1184 isoRecvIntEvent, isoXmitIntEvent);
1186 for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++)
1187 if (isoRecvIntEvent & (1<<(i+1)))
1188 wakeup_dma_ir_ctx(video->ohci,
1189 video->ir_context[i]);
1191 for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++)
1192 if (isoXmitIntEvent & (1<<i))
1193 wakeup_dma_it_ctx(video->ohci,
1194 video->it_context[i]);
1197 static struct file_operations video1394_fops=
1199 OWNER_THIS_MODULE
1200 ioctl: video1394_ioctl,
1201 mmap: video1394_mmap,
1202 open: video1394_open,
1203 release: video1394_release
1206 static int video1394_init(int i, struct ti_ohci *ohci)
1208 struct video_card *video = &video_cards[i];
1210 if (ohci1394_register_video(ohci, &video_tmpl)<0) {
1211 PRINT(KERN_ERR, i, "register_video failed");
1212 return -1;
1215 video->ohci = ohci;
1217 /* Iso receive dma contexts */
1218 video->ir_context = (struct dma_iso_ctx **)
1219 kmalloc((ohci->nb_iso_rcv_ctx-1)*
1220 sizeof(struct dma_iso_ctx *), GFP_KERNEL);
1221 if (video->ir_context)
1222 memset(video->ir_context, 0,
1223 (ohci->nb_iso_rcv_ctx-1)*sizeof(struct dma_iso_ctx *));
1224 else {
1225 PRINT(KERN_ERR, ohci->id, "Cannot allocate ir_context");
1226 return -1;
1229 /* Iso transmit dma contexts */
1230 video->it_context = (struct dma_iso_ctx **)
1231 kmalloc(ohci->nb_iso_xmit_ctx *
1232 sizeof(struct dma_iso_ctx *), GFP_KERNEL);
1233 if (video->it_context)
1234 memset(video->it_context, 0,
1235 ohci->nb_iso_xmit_ctx * sizeof(struct dma_iso_ctx *));
1236 else {
1237 PRINT(KERN_ERR, ohci->id, "Cannot allocate it_context");
1238 return -1;
1241 return 0;
1244 static void remove_card(struct video_card *video)
1246 int i;
1248 ohci1394_unregister_video(video->ohci, &video_tmpl);
1250 /* Free the iso receive contexts */
1251 if (video->ir_context) {
1252 for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++) {
1253 free_dma_iso_ctx(&video->ir_context[i]);
1255 kfree(video->ir_context);
1258 /* Free the iso transmit contexts */
1259 if (video->it_context) {
1260 for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++) {
1261 free_dma_iso_ctx(&video->it_context[i]);
1263 kfree(video->it_context);
1267 #ifdef MODULE
1269 /* EXPORT_NO_SYMBOLS; */
1271 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1272 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1273 MODULE_SUPPORTED_DEVICE("video1394");
1275 void cleanup_module(void)
1277 int i;
1278 unregister_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME);
1280 for (i=0; i<num_of_video_cards; i++)
1281 remove_card(&video_cards[i]);
1283 printk(KERN_INFO "removed " VIDEO1394_DRIVER_NAME " module\n");
1286 int init_module(void)
1288 struct ti_ohci *ohci;
1289 int i;
1291 memset(video_cards, 0, MAX_OHCI1394_CARDS * sizeof(struct video_card));
1292 num_of_video_cards = 0;
1294 for (i=0; i<MAX_OHCI1394_CARDS; i++) {
1295 ohci=ohci1394_get_struct(i);
1296 if (ohci) {
1297 num_of_video_cards++;
1298 video1394_init(i, ohci);
1302 if (!num_of_video_cards) {
1303 PRINT_G(KERN_INFO, "no ohci card found... init failed");
1304 return -EIO;
1307 if (register_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME,
1308 &video1394_fops)) {
1309 printk("video1394: unable to get major %d\n",
1310 VIDEO1394_MAJOR);
1311 return -EIO;
1314 PRINT_G(KERN_INFO, "initialized with %d ohci cards",
1315 num_of_video_cards);
1317 return 0;
1320 #endif /* MODULE */