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>
29 #include <linux/poll.h>
30 #include <linux/smp_lock.h>
31 #include <asm/byteorder.h>
32 #include <asm/atomic.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>
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>
48 #include "ieee1394_types.h"
50 #include "ieee1394_core.h"
51 #include "video1394.h"
55 #define VIDEO1394_MAJOR 172
56 #define ISO_CHANNELS 64
58 #define ISO_TRANSMIT 1
61 #define virt_to_page(x) MAP_NR(x)
68 quadlet_t pad
[4]; /* FIXME: quick hack for memory alignment */
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
;
83 struct dma_cmd
**ir_prg
;
84 struct it_dma_prg
**it_prg
;
85 unsigned int *buffer_status
;
90 wait_queue_head_t waitq
;
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
110 #ifdef VIDEO1394_DEBUG
111 #define DBGMSG(card, fmt, args...) \
112 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
114 #define DBGMSG(card, fmt, args...)
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)
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;
164 if (!pgd_none(*pgd
)) {
165 pmd
= pmd_offset(pgd
, adr
);
166 if (!pmd_none(*pmd
)) {
167 ptep
= pte_offset(pmd
, adr
);
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
));
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
));
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
));
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
);
211 MDEBUG(printk("kv2pa(%lx-->%lx)", adr
, ret
));
215 static void * rvmalloc(unsigned long size
)
218 unsigned long adr
, page
;
220 mem
=vmalloc_32(size
);
223 memset(mem
, 0, size
); /* Clear the ram out,
224 no junk to the user */
225 adr
=(unsigned long) mem
;
228 page
= kvirt_to_pa(adr
);
229 mem_map_reserve(virt_to_page(__va(page
)));
237 static void rvfree(void * mem
, unsigned long size
)
239 unsigned long adr
, page
;
243 adr
=(unsigned long) mem
;
246 page
= kvirt_to_pa(adr
);
247 mem_map_unreserve(virt_to_page(__va(page
)));
255 static int free_dma_iso_ctx(struct dma_iso_ctx
**d
)
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
);
272 for (i
=0;i
<(*d
)->num_desc
;i
++)
273 if ((*d
)->ir_prg
[i
]) kfree((*d
)->ir_prg
[i
]);
278 for (i
=0;i
<(*d
)->num_desc
;i
++)
279 if ((*d
)->it_prg
[i
]) kfree((*d
)->it_prg
[i
]);
283 if ((*d
)->buffer_status
)
284 kfree((*d
)->buffer_status
);
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
;
299 d
= (struct dma_iso_ctx
*)kmalloc(sizeof(struct dma_iso_ctx
),
301 memset(d
, 0, sizeof(struct dma_iso_ctx
));
304 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma_iso_ctx");
308 d
->ohci
= (void *)ohci
;
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
);
316 d
->buf_size
= buf_size
;
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
);
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
*),
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
);
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
),
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
);
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
*),
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
);
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",
386 free_dma_iso_ctx(&d
);
390 d
->nb_cmd
= d
->frame_size
/ d
->packet_size
;
391 if (d
->frame_size
% d
->packet_size
) {
393 d
->left_size
= d
->frame_size
% d
->packet_size
;
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
),
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
);
411 d
->buffer_status
= kmalloc(d
->num_desc
* sizeof(unsigned int),
414 if (d
->buffer_status
== NULL
) {
415 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma ir prg");
416 free_dma_iso_ctx(&d
);
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
);
431 static void reset_ir_status(struct dma_iso_ctx
*d
, int n
)
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
;
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
))
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
))
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
))
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
;
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
)
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
)
508 PRINT(KERN_ERR
, ohci
->id
,
509 "no iso context is listening to channel %d",
514 int it_ctx_talking(struct video_card
*video
, int channel
)
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
)
525 PRINT(KERN_ERR
, ohci
->id
,
526 "no iso context is talking to channel %d",
531 int wakeup_dma_ir_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
536 PRINT(KERN_ERR
, ohci
->id
, "Iso receive event received but "
537 "context not allocated");
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
);
553 int wakeup_dma_it_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
558 PRINT(KERN_ERR
, ohci
->id
, "Iso transmit event received but "
559 "context not allocated");
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
);
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
;
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 */
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
);
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
))
605 it_prg
[i
].end
.branchAddress
=
606 (virt_to_bus(&(it_prg
[i
+1].begin
.control
))
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;
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]);
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
;
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",
656 PRINT(KERN_ERR
, ohci
->id
,
657 "iso context %d is not allocated", d
->ctx
);
661 pos
=(unsigned long) d
->buf
;
663 page
= kvirt_to_pa(pos
);
664 if (remap_page_range(start
, page
, PAGE_SIZE
, PAGE_SHARED
))
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
;
682 case VIDEO1394_LISTEN_CHANNEL
:
683 case VIDEO1394_TALK_CHANNEL
:
685 struct video1394_mmap v
;
688 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
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
);
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
);
702 PRINT(KERN_ERR
, ohci
->id
,
703 "Invalid %d length buffer requested",v
.buf_size
);
707 if (v
.nb_buffers
<=0) {
708 PRINT(KERN_ERR
, ohci
->id
,
709 "Invalid %d buffers requested",v
.nb_buffers
);
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
);
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");
731 video
->ir_context
[i
] =
732 alloc_dma_iso_ctx(ohci
, ISO_RECEIVE
, i
+1,
733 v
.nb_buffers
, v
.buf_size
,
736 if (video
->ir_context
[i
] == NULL
) {
737 PRINT(KERN_ERR
, ohci
->id
,
738 "Couldn't allocate ir context");
741 initialize_dma_ir_ctx(video
->ir_context
[i
],
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,
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");
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");
773 initialize_dma_it_ctx(video
->it_context
[i
],
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
,
785 if(copy_to_user((void *)arg
, &v
, sizeof(v
)))
790 case VIDEO1394_UNLISTEN_CHANNEL
:
791 case VIDEO1394_UNTALK_CHANNEL
:
796 if(copy_from_user(&channel
, (void *)arg
, sizeof(int)))
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
);
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",
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",
828 case VIDEO1394_LISTEN_QUEUE_BUFFER
:
830 struct video1394_wait v
;
831 struct dma_iso_ctx
*d
;
834 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
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
);
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
);
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
))
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);
878 reg_write(ohci
, d
->ctrlSet
, 0x8000);
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);
891 case VIDEO1394_LISTEN_WAIT_BUFFER
:
893 struct video1394_wait v
;
894 struct dma_iso_ctx
*d
;
897 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
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
);
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
;
919 case VIDEO1394_BUFFER_QUEUED
:
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
);
932 if (wait_event_interruptible(d
->waitq
,
933 d
->buffer_status
[v
.buffer
]
934 == VIDEO1394_BUFFER_READY
)
938 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
941 PRINT(KERN_ERR
, ohci
->id
,
942 "buffer %d is not queued",v
.buffer
);
943 spin_unlock_irqrestore(&d
->lock
, flags
);
948 * Look ahead to see how many more buffers have been received
951 while (d
->buffer_status
[(v
.buffer
+1)%d
->num_desc
]==
952 VIDEO1394_BUFFER_READY
) {
953 v
.buffer
=(v
.buffer
+1)%d
->num_desc
;
956 spin_unlock_irqrestore(&d
->lock
, flags
);
959 if(copy_to_user((void *)arg
, &v
, sizeof(v
)))
964 case VIDEO1394_TALK_QUEUE_BUFFER
:
966 struct video1394_wait v
;
967 struct dma_iso_ctx
*d
;
970 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
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
);
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
);
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
))
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",
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);
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",
1029 reg_write(ohci
, d
->ctrlSet
, 0x1000);
1035 case VIDEO1394_TALK_WAIT_BUFFER
:
1037 struct video1394_wait v
;
1038 struct dma_iso_ctx
*d
;
1041 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
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
);
1054 switch(d
->buffer_status
[v
.buffer
]) {
1055 case VIDEO1394_BUFFER_READY
:
1056 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1058 case VIDEO1394_BUFFER_QUEUED
:
1060 while(d
->buffer_status
[v
.buffer
]!=
1061 VIDEO1394_BUFFER_READY
) {
1062 interruptible_sleep_on(&d
->waitq
);
1063 if(signal_pending(current
)) return -EINTR
;
1066 if (wait_event_interruptible(d
->waitq
,
1067 d
->buffer_status
[v
.buffer
]
1068 == VIDEO1394_BUFFER_READY
)
1072 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1075 PRINT(KERN_ERR
, ohci
->id
,
1076 "buffer %d is not queued",v
.buffer
);
1086 * This maps the vmalloced and reserved buffer to user space.
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
;
1103 PRINT(KERN_INFO
, ohci
->id
, "mmap");
1104 if (video
->current_ctx
== NULL
) {
1105 PRINT(KERN_ERR
, ohci
->id
, "current iso context not set");
1107 res
= do_iso_mmap(ohci
, video
->current_ctx
,
1108 (char *)vma
->vm_start
,
1109 (unsigned long)(vma
->vm_end
-vma
->vm_start
));
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
);
1123 V22_COMPAT_MOD_INC_USE_COUNT
;
1125 PRINT(KERN_INFO
, i
, "open");
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
;
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");
1177 void irq_handler(int card
, quadlet_t isoRecvIntEvent
,
1178 quadlet_t isoXmitIntEvent
)
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
=
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");
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
*));
1225 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate ir_context");
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
*));
1237 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate it_context");
1244 static void remove_card(struct video_card
*video
)
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
);
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)
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
;
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
);
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");
1307 if (register_chrdev(VIDEO1394_MAJOR
, VIDEO1394_DRIVER_NAME
,
1309 printk("video1394: unable to get major %d\n",
1314 PRINT_G(KERN_INFO
, "initialized with %d ohci cards",
1315 num_of_video_cards
);