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
64 quadlet_t pad
[4]; /* FIXME: quick hack for memory alignment */
72 unsigned int num_desc
;
73 unsigned int buf_size
;
74 unsigned int frame_size
;
75 unsigned int packet_size
;
76 unsigned int left_size
;
79 struct dma_cmd
**ir_prg
;
80 struct it_dma_prg
**it_prg
;
81 unsigned int *buffer_status
;
86 wait_queue_head_t waitq
;
92 struct dma_iso_ctx
**ir_context
;
93 struct dma_iso_ctx
**it_context
;
94 struct dma_iso_ctx
*current_ctx
;
97 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
98 #define VIDEO1394_DEBUG
105 #ifdef VIDEO1394_DEBUG
106 #define DBGMSG(card, fmt, args...) \
107 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
109 #define DBGMSG(card, fmt, args...)
112 /* print general (card independent) information */
113 #define PRINT_G(level, fmt, args...) \
114 printk(level "video1394: " fmt "\n" , ## args)
116 /* print card specific information */
117 #define PRINT(level, card, fmt, args...) \
118 printk(level "video1394_%d: " fmt "\n" , card , ## args)
120 void irq_handler(int card
, quadlet_t isoRecvIntEvent
,
121 quadlet_t isoXmitIntEvent
);
123 static struct video_card video_cards
[MAX_OHCI1394_CARDS
];
124 static int num_of_video_cards
= 0;
125 static struct video_template video_tmpl
= { irq_handler
};
127 /* Taken from bttv.c */
128 /*******************************/
129 /* Memory management functions */
130 /*******************************/
132 #define MDEBUG(x) do { } while(0) /* Debug memory management */
134 /* [DaveM] I've recoded most of this so that:
135 * 1) It's easier to tell what is happening
136 * 2) It's more portable, especially for translating things
137 * out of vmalloc mapped areas in the kernel.
138 * 3) Less unnecessary translations happen.
140 * The code used to assume that the kernel vmalloc mappings
141 * existed in the page tables of every process, this is simply
142 * not guarenteed. We now use pgd_offset_k which is the
143 * defined way to get at the kernel page tables.
146 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
147 #define page_address(x) ((void *) (x))
150 /* Given PGD from the address space's page table, return the kernel
151 * virtual mapping of the physical memory mapped at ADR.
153 static inline unsigned long uvirt_to_kva(pgd_t
*pgd
, unsigned long adr
)
155 unsigned long ret
= 0UL;
159 if (!pgd_none(*pgd
)) {
160 pmd
= pmd_offset(pgd
, adr
);
161 if (!pmd_none(*pmd
)) {
162 ptep
= pte_offset(pmd
, adr
);
164 if(pte_present(pte
)) {
165 ret
= (unsigned long) page_address(pte_page(pte
));
166 ret
|= (adr
& (PAGE_SIZE
- 1));
170 MDEBUG(printk("uv2kva(%lx-->%lx)", adr
, ret
));
174 static inline unsigned long uvirt_to_bus(unsigned long adr
)
176 unsigned long kva
, ret
;
178 kva
= uvirt_to_kva(pgd_offset(current
->mm
, adr
), adr
);
179 ret
= virt_to_bus((void *)kva
);
180 MDEBUG(printk("uv2b(%lx-->%lx)", adr
, ret
));
184 static inline unsigned long kvirt_to_bus(unsigned long adr
)
186 unsigned long va
, kva
, ret
;
188 va
= VMALLOC_VMADDR(adr
);
189 kva
= uvirt_to_kva(pgd_offset_k(va
), va
);
190 ret
= virt_to_bus((void *)kva
);
191 MDEBUG(printk("kv2b(%lx-->%lx)", adr
, ret
));
195 /* Here we want the physical address of the memory.
196 * This is used when initializing the contents of the
197 * area and marking the pages as reserved.
199 static inline unsigned long kvirt_to_pa(unsigned long adr
)
201 unsigned long va
, kva
, ret
;
203 va
= VMALLOC_VMADDR(adr
);
204 kva
= uvirt_to_kva(pgd_offset_k(va
), va
);
206 MDEBUG(printk("kv2pa(%lx-->%lx)", adr
, ret
));
210 static void * rvmalloc(unsigned long size
)
213 unsigned long adr
, page
;
218 memset(mem
, 0, size
); /* Clear the ram out,
219 no junk to the user */
220 adr
=(unsigned long) mem
;
223 page
= kvirt_to_pa(adr
);
224 mem_map_reserve(MAP_NR(__va(page
)));
232 static void rvfree(void * mem
, unsigned long size
)
234 unsigned long adr
, page
;
238 adr
=(unsigned long) mem
;
241 page
= kvirt_to_pa(adr
);
242 mem_map_unreserve(MAP_NR(__va(page
)));
250 static int free_dma_iso_ctx(struct dma_iso_ctx
**d
)
253 struct ti_ohci
*ohci
;
255 if ((*d
)==NULL
) return -1;
257 ohci
= (struct ti_ohci
*)(*d
)->ohci
;
259 DBGMSG(ohci
->id
, "Freeing dma_iso_ctx %d", (*d
)->ctx
);
261 ohci1394_stop_context(ohci
, (*d
)->ctrlClear
, NULL
);
263 if ((*d
)->buf
) rvfree((void *)(*d
)->buf
,
264 (*d
)->num_desc
* (*d
)->buf_size
);
267 for (i
=0;i
<(*d
)->num_desc
;i
++)
268 if ((*d
)->ir_prg
[i
]) kfree((*d
)->ir_prg
[i
]);
273 for (i
=0;i
<(*d
)->num_desc
;i
++)
274 if ((*d
)->it_prg
[i
]) kfree((*d
)->it_prg
[i
]);
278 if ((*d
)->buffer_status
)
279 kfree((*d
)->buffer_status
);
287 static struct dma_iso_ctx
*
288 alloc_dma_iso_ctx(struct ti_ohci
*ohci
, int type
, int ctx
, int num_desc
,
289 int buf_size
, int channel
, unsigned int packet_size
)
291 struct dma_iso_ctx
*d
=NULL
;
294 d
= (struct dma_iso_ctx
*)kmalloc(sizeof(struct dma_iso_ctx
),
296 memset(d
, 0, sizeof(struct dma_iso_ctx
));
299 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma_iso_ctx");
303 d
->ohci
= (void *)ohci
;
305 d
->channel
= channel
;
306 d
->num_desc
= num_desc
;
307 d
->frame_size
= buf_size
;
308 if (buf_size
%PAGE_SIZE
)
309 d
->buf_size
= buf_size
+ PAGE_SIZE
- (buf_size
%PAGE_SIZE
);
311 d
->buf_size
= buf_size
;
315 init_waitqueue_head(&d
->waitq
);
317 d
->buf
= rvmalloc(d
->num_desc
* d
->buf_size
);
319 if (d
->buf
== NULL
) {
320 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma buffer");
321 free_dma_iso_ctx(&d
);
324 memset(d
->buf
, 0, d
->num_desc
* d
->buf_size
);
326 if (type
== ISO_RECEIVE
) {
327 d
->ctrlSet
= OHCI1394_IsoRcvContextControlSet
+32*d
->ctx
;
328 d
->ctrlClear
= OHCI1394_IsoRcvContextControlClear
+32*d
->ctx
;
329 d
->cmdPtr
= OHCI1394_IsoRcvCommandPtr
+32*d
->ctx
;
330 d
->ctxMatch
= OHCI1394_IsoRcvContextMatch
+32*d
->ctx
;
332 d
->ir_prg
= kmalloc(d
->num_desc
* sizeof(struct dma_cmd
*),
335 if (d
->ir_prg
== NULL
) {
336 PRINT(KERN_ERR
, ohci
->id
,
337 "failed to allocate dma ir prg");
338 free_dma_iso_ctx(&d
);
341 memset(d
->ir_prg
, 0, d
->num_desc
* sizeof(struct dma_cmd
*));
343 d
->nb_cmd
= d
->buf_size
/ PAGE_SIZE
+ 1;
344 d
->left_size
= (d
->frame_size
% PAGE_SIZE
) ?
345 d
->frame_size
% PAGE_SIZE
: PAGE_SIZE
;
347 for (i
=0;i
<d
->num_desc
;i
++) {
348 d
->ir_prg
[i
] = kmalloc(d
->nb_cmd
*
349 sizeof(struct dma_cmd
),
351 if (d
->ir_prg
[i
] == NULL
) {
352 PRINT(KERN_ERR
, ohci
->id
,
353 "failed to allocate dma ir prg");
354 free_dma_iso_ctx(&d
);
359 else { /* ISO_TRANSMIT */
360 d
->ctrlSet
= OHCI1394_IsoXmitContextControlSet
+16*d
->ctx
;
361 d
->ctrlClear
= OHCI1394_IsoXmitContextControlClear
+16*d
->ctx
;
362 d
->cmdPtr
= OHCI1394_IsoXmitCommandPtr
+16*d
->ctx
;
364 d
->it_prg
= kmalloc(d
->num_desc
* sizeof(struct it_dma_prg
*),
367 if (d
->it_prg
== NULL
) {
368 PRINT(KERN_ERR
, ohci
->id
,
369 "failed to allocate dma it prg");
370 free_dma_iso_ctx(&d
);
373 memset(d
->it_prg
, 0, d
->num_desc
*sizeof(struct it_dma_prg
*));
375 d
->packet_size
= packet_size
;
377 if (PAGE_SIZE
% packet_size
|| packet_size
>2048) {
378 PRINT(KERN_ERR
, ohci
->id
,
379 "Packet size %d not yet supported\n",
381 free_dma_iso_ctx(&d
);
385 d
->nb_cmd
= d
->frame_size
/ d
->packet_size
;
386 if (d
->frame_size
% d
->packet_size
) {
388 d
->left_size
= d
->frame_size
% d
->packet_size
;
391 d
->left_size
= d
->packet_size
;
393 for (i
=0;i
<d
->num_desc
;i
++) {
394 d
->it_prg
[i
] = kmalloc(d
->nb_cmd
*
395 sizeof(struct it_dma_prg
),
397 if (d
->it_prg
[i
] == NULL
) {
398 PRINT(KERN_ERR
, ohci
->id
,
399 "failed to allocate dma it prg");
400 free_dma_iso_ctx(&d
);
406 d
->buffer_status
= kmalloc(d
->num_desc
* sizeof(unsigned int),
409 if (d
->buffer_status
== NULL
) {
410 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma ir prg");
411 free_dma_iso_ctx(&d
);
414 memset(d
->buffer_status
, 0, d
->num_desc
* sizeof(unsigned int));
416 PRINT(KERN_INFO
, ohci
->id
, "Iso %s DMA: %d buffers "
417 "of size %d allocated for a frame size %d, each with %d prgs",
418 (type
==ISO_RECEIVE
) ? "receive" : "transmit",
419 d
->num_desc
, d
->buf_size
, d
->frame_size
, d
->nb_cmd
);
424 static void reset_ir_status(struct dma_iso_ctx
*d
, int n
)
427 d
->ir_prg
[n
][0].status
= 4;
428 d
->ir_prg
[n
][1].status
= PAGE_SIZE
-4;
429 for (i
=2;i
<d
->nb_cmd
-1;i
++)
430 d
->ir_prg
[n
][i
].status
= PAGE_SIZE
;
431 d
->ir_prg
[n
][i
].status
= d
->left_size
;
434 static void initialize_dma_ir_prg(struct dma_iso_ctx
*d
, int n
)
436 struct dma_cmd
*ir_prg
= d
->ir_prg
[n
];
437 unsigned long buf
= (unsigned long)d
->buf
+n
*d
->buf_size
;
440 /* the first descriptor will sync and read only 4 bytes */
441 ir_prg
[0].control
= (0x280F << 16) | 4;
442 ir_prg
[0].address
= kvirt_to_bus(buf
);
443 ir_prg
[0].branchAddress
= (virt_to_bus(&(ir_prg
[1].control
))
446 /* the second descriptor will read PAGE_SIZE-4 bytes */
447 ir_prg
[1].control
= (0x280C << 16) | (PAGE_SIZE
-4);
448 ir_prg
[1].address
= kvirt_to_bus(buf
+4);
449 ir_prg
[1].branchAddress
= (virt_to_bus(&(ir_prg
[2].control
))
452 for (i
=2;i
<d
->nb_cmd
-1;i
++) {
453 ir_prg
[i
].control
= (0x280C << 16) | PAGE_SIZE
;
454 ir_prg
[i
].address
= kvirt_to_bus(buf
+(i
-1)*PAGE_SIZE
);
456 ir_prg
[i
].branchAddress
=
457 (virt_to_bus(&(ir_prg
[i
+1].control
))
461 /* the last descriptor will generate an interrupt */
462 ir_prg
[i
].control
= (0x283C << 16) | d
->left_size
;
463 ir_prg
[i
].address
= kvirt_to_bus(buf
+(i
-1)*PAGE_SIZE
);
466 static void initialize_dma_ir_ctx(struct dma_iso_ctx
*d
, int tag
)
468 struct ti_ohci
*ohci
= (struct ti_ohci
*)d
->ohci
;
471 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
473 for (i
=0;i
<d
->num_desc
;i
++) {
474 initialize_dma_ir_prg(d
, i
);
475 reset_ir_status(d
, i
);
478 /* Set bufferFill, no header */
479 reg_write(ohci
, d
->ctrlSet
, 0x80000000);
481 /* Set the context match register to match on all tags,
482 sync for sync tag, and listen to d->channel */
483 reg_write(ohci
, d
->ctxMatch
, 0xf0000000|((tag
&0xf)<<8)|d
->channel
);
485 /* Set up isoRecvIntMask to generate interrupts */
486 reg_write(ohci
, OHCI1394_IsoRecvIntMaskSet
, 1<<d
->ctx
);
489 /* find which context is listening to this channel */
490 int ir_ctx_listening(struct video_card
*video
, int channel
)
493 struct ti_ohci
*ohci
= video
->ohci
;
495 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
496 if (video
->ir_context
[i
]) {
497 if (video
->ir_context
[i
]->channel
==channel
)
501 PRINT(KERN_ERR
, ohci
->id
,
502 "no iso context is listening to channel %d",
507 int it_ctx_talking(struct video_card
*video
, int channel
)
510 struct ti_ohci
*ohci
= video
->ohci
;
512 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
513 if (video
->it_context
[i
]) {
514 if (video
->it_context
[i
]->channel
==channel
)
518 PRINT(KERN_ERR
, ohci
->id
,
519 "no iso context is talking to channel %d",
524 int wakeup_dma_ir_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
529 PRINT(KERN_ERR
, ohci
->id
, "Iso receive event received but "
530 "context not allocated");
534 for (i
=0;i
<d
->num_desc
;i
++) {
535 if (d
->ir_prg
[i
][d
->nb_cmd
-1].status
& 0xFFFF0000) {
536 reset_ir_status(d
, i
);
537 d
->buffer_status
[i
] = VIDEO1394_BUFFER_READY
;
540 if (waitqueue_active(&d
->waitq
)) wake_up_interruptible(&d
->waitq
);
544 int wakeup_dma_it_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
549 PRINT(KERN_ERR
, ohci
->id
, "Iso transmit event received but "
550 "context not allocated");
554 for (i
=0;i
<d
->num_desc
;i
++) {
555 if (d
->it_prg
[i
][d
->nb_cmd
-1].end
.status
& 0xFFFF0000) {
556 d
->it_prg
[i
][d
->nb_cmd
-1].end
.status
= 0;
557 d
->buffer_status
[i
] = VIDEO1394_BUFFER_READY
;
560 if (waitqueue_active(&d
->waitq
)) wake_up_interruptible(&d
->waitq
);
564 static void initialize_dma_it_prg(struct dma_iso_ctx
*d
, int n
, int sync_tag
)
566 struct it_dma_prg
*it_prg
= d
->it_prg
[n
];
567 unsigned long buf
= (unsigned long)d
->buf
+n
*d
->buf_size
;
570 for (i
=0;i
<d
->nb_cmd
;i
++) {
572 it_prg
[i
].begin
.control
= OUTPUT_MORE_IMMEDIATE
| 8 ;
573 it_prg
[i
].begin
.address
= 0;
575 it_prg
[i
].begin
.status
= 0;
577 /* FIXME: what is the tag value + speed selection */
579 (DMA_SPEED_400
<<16) | (d
->channel
<<8) | 0xa0;
580 if (i
==0) it_prg
[i
].data
[0] |= sync_tag
;
581 it_prg
[i
].data
[1] = d
->packet_size
<< 16;
582 it_prg
[i
].data
[2] = 0;
583 it_prg
[i
].data
[3] = 0;
585 it_prg
[i
].end
.control
= 0x100c0000;
586 it_prg
[i
].end
.address
=
587 kvirt_to_bus(buf
+i
*d
->packet_size
);
590 it_prg
[i
].end
.control
|= d
->packet_size
;
591 it_prg
[i
].begin
.branchAddress
=
592 (virt_to_bus(&(it_prg
[i
+1].begin
.control
))
594 it_prg
[i
].end
.branchAddress
=
595 (virt_to_bus(&(it_prg
[i
+1].begin
.control
))
599 /* the last prg generates an interrupt */
600 it_prg
[i
].end
.control
|= 0x08300000 | d
->left_size
;
601 /* the last prg doesn't branch */
602 it_prg
[i
].begin
.branchAddress
= 0;
603 it_prg
[i
].end
.branchAddress
= 0;
605 it_prg
[i
].end
.status
= 0;
608 printk("%d:%d: %08x-%08x ctrl %08x brch %08x d0 %08x d1 %08x\n",n
,i
,
609 virt_to_bus(&(it_prg
[i
].begin
.control
)),
610 virt_to_bus(&(it_prg
[i
].end
.control
)),
611 it_prg
[i
].end
.control
,
612 it_prg
[i
].end
.branchAddress
,
613 it_prg
[i
].data
[0], it_prg
[i
].data
[1]);
618 static void initialize_dma_it_ctx(struct dma_iso_ctx
*d
, int sync_tag
)
620 struct ti_ohci
*ohci
= (struct ti_ohci
*)d
->ohci
;
623 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
625 for (i
=0;i
<d
->num_desc
;i
++)
626 initialize_dma_it_prg(d
, i
, sync_tag
);
628 /* Set up isoRecvIntMask to generate interrupts */
629 reg_write(ohci
, OHCI1394_IsoXmitIntMaskSet
, 1<<d
->ctx
);
632 static int do_iso_mmap(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
,
633 const char *adr
, unsigned long size
)
635 unsigned long start
=(unsigned long) adr
;
636 unsigned long page
,pos
;
638 if (size
>d
->num_desc
* d
->buf_size
) {
639 PRINT(KERN_ERR
, ohci
->id
,
640 "iso context %d buf size is different from mmap size",
645 PRINT(KERN_ERR
, ohci
->id
,
646 "iso context %d is not allocated", d
->ctx
);
650 pos
=(unsigned long) d
->buf
;
652 page
= kvirt_to_pa(pos
);
653 if (remap_page_range(start
, page
, PAGE_SIZE
, PAGE_SHARED
))
662 static int video1394_ioctl(struct inode
*inode
, struct file
*file
,
663 unsigned int cmd
, unsigned long arg
)
665 struct video_card
*video
= &video_cards
[MINOR(inode
->i_rdev
)];
666 struct ti_ohci
*ohci
= video
->ohci
;
670 case VIDEO1394_LISTEN_CHANNEL
:
671 case VIDEO1394_TALK_CHANNEL
:
673 struct video1394_mmap v
;
676 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
678 if (v
.channel
<0 || v
.channel
>(ISO_CHANNELS
-1)) {
679 PRINT(KERN_ERR
, ohci
->id
,
680 "iso channel %d out of bound", v
.channel
);
683 if (test_and_set_bit(v
.channel
, &ohci
->IR_channel_usage
)) {
684 PRINT(KERN_ERR
, ohci
->id
,
685 "channel %d is already taken", v
.channel
);
690 PRINT(KERN_ERR
, ohci
->id
,
691 "Invalid %d length buffer requested",v
.buf_size
);
695 if (v
.nb_buffers
<=0) {
696 PRINT(KERN_ERR
, ohci
->id
,
697 "Invalid %d buffers requested",v
.nb_buffers
);
701 if (v
.nb_buffers
* v
.buf_size
> VIDEO1394_MAX_SIZE
) {
702 PRINT(KERN_ERR
, ohci
->id
,
703 "%d buffers of size %d bytes is too big",
704 v
.nb_buffers
, v
.buf_size
);
708 if (cmd
== VIDEO1394_LISTEN_CHANNEL
) {
709 /* find a free iso receive context */
710 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
711 if (video
->ir_context
[i
]==NULL
) break;
713 if (i
==(ohci
->nb_iso_rcv_ctx
-1)) {
714 PRINT(KERN_ERR
, ohci
->id
,
715 "no iso context available");
719 video
->ir_context
[i
] =
720 alloc_dma_iso_ctx(ohci
, ISO_RECEIVE
, i
+1,
721 v
.nb_buffers
, v
.buf_size
,
724 if (video
->ir_context
[i
] == NULL
) {
725 PRINT(KERN_ERR
, ohci
->id
,
726 "Couldn't allocate ir context");
729 initialize_dma_ir_ctx(video
->ir_context
[i
],
732 video
->current_ctx
= video
->ir_context
[i
];
734 v
.buf_size
= video
->ir_context
[i
]->buf_size
;
736 PRINT(KERN_INFO
, ohci
->id
,
737 "iso context %d listen on channel %d", i
+1,
741 /* find a free iso transmit context */
742 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
743 if (video
->it_context
[i
]==NULL
) break;
745 if (i
==ohci
->nb_iso_xmit_ctx
) {
746 PRINT(KERN_ERR
, ohci
->id
,
747 "no iso context available");
751 video
->it_context
[i
] =
752 alloc_dma_iso_ctx(ohci
, ISO_TRANSMIT
, i
,
753 v
.nb_buffers
, v
.buf_size
,
754 v
.channel
, v
.packet_size
);
756 if (video
->it_context
[i
] == NULL
) {
757 PRINT(KERN_ERR
, ohci
->id
,
758 "Couldn't allocate it context");
761 initialize_dma_it_ctx(video
->it_context
[i
],
764 video
->current_ctx
= video
->it_context
[i
];
766 v
.buf_size
= video
->it_context
[i
]->buf_size
;
768 PRINT(KERN_INFO
, ohci
->id
,
769 "iso context %d talk on channel %d", i
,
773 if(copy_to_user((void *)arg
, &v
, sizeof(v
)))
778 case VIDEO1394_UNLISTEN_CHANNEL
:
779 case VIDEO1394_UNTALK_CHANNEL
:
784 if(copy_from_user(&channel
, (void *)arg
, sizeof(int)))
787 if (!test_and_clear_bit(channel
, &ohci
->IR_channel_usage
)) {
788 PRINT(KERN_ERR
, ohci
->id
,
789 "channel %d is not being used", channel
);
793 if (cmd
== VIDEO1394_UNLISTEN_CHANNEL
) {
794 i
= ir_ctx_listening(video
, channel
);
795 if (i
<0) return -EFAULT
;
797 free_dma_iso_ctx(&video
->ir_context
[i
]);
799 PRINT(KERN_INFO
, ohci
->id
,
800 "iso context %d stop listening on channel %d",
804 i
= it_ctx_talking(video
, channel
);
805 if (i
<0) return -EFAULT
;
807 free_dma_iso_ctx(&video
->it_context
[i
]);
809 PRINT(KERN_INFO
, ohci
->id
,
810 "iso context %d stop talking on channel %d",
816 case VIDEO1394_LISTEN_QUEUE_BUFFER
:
818 struct video1394_wait v
;
819 struct dma_iso_ctx
*d
;
822 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
825 i
= ir_ctx_listening(video
, v
.channel
);
826 if (i
<0) return -EFAULT
;
827 d
= video
->ir_context
[i
];
829 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
830 PRINT(KERN_ERR
, ohci
->id
,
831 "buffer %d out of range",v
.buffer
);
835 if (d
->buffer_status
[v
.buffer
]!=VIDEO1394_BUFFER_FREE
) {
836 PRINT(KERN_ERR
, ohci
->id
,
837 "buffer %d is already used",v
.buffer
);
841 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_QUEUED
;
843 if (d
->last_buffer
>=0)
844 d
->ir_prg
[d
->last_buffer
][d
->nb_cmd
-1].branchAddress
=
845 (virt_to_bus(&(d
->ir_prg
[v
.buffer
][0].control
))
848 d
->last_buffer
= v
.buffer
;
850 d
->ir_prg
[d
->last_buffer
][d
->nb_cmd
-1].branchAddress
= 0;
852 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x8000))
854 DBGMSG(ohci
->id
, "Starting iso DMA ctx=%d",d
->ctx
);
856 /* Tell the controller where the first program is */
857 reg_write(ohci
, d
->cmdPtr
,
858 virt_to_bus(&(d
->ir_prg
[v
.buffer
][0]))|0x1);
861 reg_write(ohci
, d
->ctrlSet
, 0x8000);
864 /* Wake up dma context if necessary */
865 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400)) {
866 PRINT(KERN_INFO
, ohci
->id
,
867 "Waking up iso dma ctx=%d", d
->ctx
);
868 reg_write(ohci
, d
->ctrlSet
, 0x1000);
874 case VIDEO1394_LISTEN_WAIT_BUFFER
:
876 struct video1394_wait v
;
877 struct dma_iso_ctx
*d
;
880 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
883 i
= ir_ctx_listening(video
, v
.channel
);
884 if (i
<0) return -EFAULT
;
885 d
= video
->ir_context
[i
];
887 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
888 PRINT(KERN_ERR
, ohci
->id
,
889 "buffer %d out of range",v
.buffer
);
893 switch(d
->buffer_status
[v
.buffer
]) {
894 case VIDEO1394_BUFFER_READY
:
895 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
897 case VIDEO1394_BUFFER_QUEUED
:
899 while(d
->buffer_status
[v
.buffer
]!=
900 VIDEO1394_BUFFER_READY
) {
901 interruptible_sleep_on(&d
->waitq
);
902 if(signal_pending(current
)) return -EINTR
;
905 if (wait_event_interruptible(d
->waitq
,
906 d
->buffer_status
[v
.buffer
]
907 == VIDEO1394_BUFFER_READY
)
911 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
914 PRINT(KERN_ERR
, ohci
->id
,
915 "buffer %d is not queued",v
.buffer
);
919 case VIDEO1394_TALK_QUEUE_BUFFER
:
921 struct video1394_wait v
;
922 struct dma_iso_ctx
*d
;
925 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
928 i
= it_ctx_talking(video
, v
.channel
);
929 if (i
<0) return -EFAULT
;
930 d
= video
->it_context
[i
];
932 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
933 PRINT(KERN_ERR
, ohci
->id
,
934 "buffer %d out of range",v
.buffer
);
938 if (d
->buffer_status
[v
.buffer
]!=VIDEO1394_BUFFER_FREE
) {
939 PRINT(KERN_ERR
, ohci
->id
,
940 "buffer %d is already used",v
.buffer
);
944 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_QUEUED
;
946 if (d
->last_buffer
>=0) {
947 d
->it_prg
[d
->last_buffer
]
948 [d
->nb_cmd
-1].end
.branchAddress
=
949 (virt_to_bus(&(d
->it_prg
[v
.buffer
][0].begin
.control
))
952 d
->it_prg
[d
->last_buffer
]
953 [d
->nb_cmd
-1].begin
.branchAddress
=
954 (virt_to_bus(&(d
->it_prg
[v
.buffer
][0].begin
.control
))
957 d
->last_buffer
= v
.buffer
;
959 d
->it_prg
[d
->last_buffer
][d
->nb_cmd
-1].end
.branchAddress
= 0;
961 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x8000))
963 DBGMSG(ohci
->id
, "Starting iso transmit DMA ctx=%d",
966 /* Tell the controller where the first program is */
967 reg_write(ohci
, d
->cmdPtr
,
968 virt_to_bus(&(d
->it_prg
[v
.buffer
][0]))|0x3);
971 reg_write(ohci
, d
->ctrlSet
, 0x8000);
974 /* Wake up dma context if necessary */
975 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400)) {
976 PRINT(KERN_INFO
, ohci
->id
,
977 "Waking up iso transmit dma ctx=%d",
979 reg_write(ohci
, d
->ctrlSet
, 0x1000);
985 case VIDEO1394_TALK_WAIT_BUFFER
:
987 struct video1394_wait v
;
988 struct dma_iso_ctx
*d
;
991 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
994 i
= it_ctx_talking(video
, v
.channel
);
995 if (i
<0) return -EFAULT
;
996 d
= video
->it_context
[i
];
998 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
999 PRINT(KERN_ERR
, ohci
->id
,
1000 "buffer %d out of range",v
.buffer
);
1004 switch(d
->buffer_status
[v
.buffer
]) {
1005 case VIDEO1394_BUFFER_READY
:
1006 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1008 case VIDEO1394_BUFFER_QUEUED
:
1010 while(d
->buffer_status
[v
.buffer
]!=
1011 VIDEO1394_BUFFER_READY
) {
1012 interruptible_sleep_on(&d
->waitq
);
1013 if(signal_pending(current
)) return -EINTR
;
1016 if (wait_event_interruptible(d
->waitq
,
1017 d
->buffer_status
[v
.buffer
]
1018 == VIDEO1394_BUFFER_READY
)
1022 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1025 PRINT(KERN_ERR
, ohci
->id
,
1026 "buffer %d is not queued",v
.buffer
);
1036 * This maps the vmalloced and reserved buffer to user space.
1039 * - PAGE_READONLY should suffice!?
1040 * - remap_page_range is kind of inefficient for page by page remapping.
1041 * But e.g. pte_alloc() does not work in modules ... :-(
1044 int video1394_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1046 struct video_card
*video
=
1047 &video_cards
[MINOR(file
->f_dentry
->d_inode
->i_rdev
)];
1048 struct ti_ohci
*ohci
;
1053 PRINT(KERN_INFO
, ohci
->id
, "mmap");
1054 if (video
->current_ctx
== NULL
) {
1055 PRINT(KERN_ERR
, ohci
->id
, "current iso context not set");
1057 res
= do_iso_mmap(ohci
, video
->current_ctx
,
1058 (char *)vma
->vm_start
,
1059 (unsigned long)(vma
->vm_end
-vma
->vm_start
));
1064 static int video1394_open(struct inode
*inode
, struct file
*file
)
1066 int i
= MINOR(inode
->i_rdev
);
1068 if (i
<0 || i
>=num_of_video_cards
) {
1069 PRINT(KERN_ERR
, i
, "ohci card %d not found", i
);
1073 V22_COMPAT_MOD_INC_USE_COUNT
;
1075 PRINT(KERN_INFO
, i
, "open");
1080 static int video1394_release(struct inode
*inode
, struct file
*file
)
1082 struct video_card
*video
= &video_cards
[MINOR(inode
->i_rdev
)];
1083 struct ti_ohci
*ohci
= video
->ohci
;
1087 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
1088 if (video
->ir_context
[i
]) {
1089 if (!test_and_clear_bit(
1090 video
->ir_context
[i
]->channel
,
1091 &ohci
->IR_channel_usage
)) {
1092 PRINT(KERN_ERR
, ohci
->id
,
1093 "channel %d is not being used",
1094 video
->ir_context
[i
]->channel
);
1096 PRINT(KERN_INFO
, ohci
->id
,
1097 "iso receive context %d stop listening "
1098 "on channel %d", i
+1,
1099 video
->ir_context
[i
]->channel
);
1100 free_dma_iso_ctx(&video
->ir_context
[i
]);
1103 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
1104 if (video
->it_context
[i
]) {
1105 if (!test_and_clear_bit(
1106 video
->it_context
[i
]->channel
,
1107 &ohci
->IR_channel_usage
)) {
1108 PRINT(KERN_ERR
, ohci
->id
,
1109 "channel %d is not being used",
1110 video
->it_context
[i
]->channel
);
1112 PRINT(KERN_INFO
, ohci
->id
,
1113 "iso transmit context %d stop talking "
1114 "on channel %d", i
+1,
1115 video
->it_context
[i
]->channel
);
1116 free_dma_iso_ctx(&video
->it_context
[i
]);
1120 V22_COMPAT_MOD_DEC_USE_COUNT
;
1122 PRINT(KERN_INFO
, ohci
->id
, "release");
1127 void irq_handler(int card
, quadlet_t isoRecvIntEvent
,
1128 quadlet_t isoXmitIntEvent
)
1131 struct video_card
*video
= &video_cards
[card
];
1133 DBGMSG(card
, "Iso event Recv: %08x Xmit: %08x",
1134 isoRecvIntEvent
, isoXmitIntEvent
);
1136 for (i
=0;i
<video
->ohci
->nb_iso_rcv_ctx
-1;i
++)
1137 if (isoRecvIntEvent
& (1<<(i
+1)))
1138 wakeup_dma_ir_ctx(video
->ohci
,
1139 video
->ir_context
[i
]);
1141 for (i
=0;i
<video
->ohci
->nb_iso_xmit_ctx
;i
++)
1142 if (isoXmitIntEvent
& (1<<i
))
1143 wakeup_dma_it_ctx(video
->ohci
,
1144 video
->it_context
[i
]);
1147 static struct file_operations video1394_fops
=
1150 ioctl
: video1394_ioctl
,
1151 mmap
: video1394_mmap
,
1152 open
: video1394_open
,
1153 release
: video1394_release
1156 static int video1394_init(int i
, struct ti_ohci
*ohci
)
1158 struct video_card
*video
= &video_cards
[i
];
1160 if (ohci1394_register_video(ohci
, &video_tmpl
)<0) {
1161 PRINT(KERN_ERR
, i
, "register_video failed");
1167 /* Iso receive dma contexts */
1168 video
->ir_context
= (struct dma_iso_ctx
**)
1169 kmalloc((ohci
->nb_iso_rcv_ctx
-1)*
1170 sizeof(struct dma_iso_ctx
*), GFP_KERNEL
);
1171 if (video
->ir_context
)
1172 memset(video
->ir_context
, 0,
1173 (ohci
->nb_iso_rcv_ctx
-1)*sizeof(struct dma_iso_ctx
*));
1175 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate ir_context");
1179 /* Iso transmit dma contexts */
1180 video
->it_context
= (struct dma_iso_ctx
**)
1181 kmalloc(ohci
->nb_iso_xmit_ctx
*
1182 sizeof(struct dma_iso_ctx
*), GFP_KERNEL
);
1183 if (video
->it_context
)
1184 memset(video
->it_context
, 0,
1185 ohci
->nb_iso_xmit_ctx
* sizeof(struct dma_iso_ctx
*));
1187 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate it_context");
1194 static void remove_card(struct video_card
*video
)
1198 ohci1394_unregister_video(video
->ohci
, &video_tmpl
);
1200 /* Free the iso receive contexts */
1201 if (video
->ir_context
) {
1202 for (i
=0;i
<video
->ohci
->nb_iso_rcv_ctx
-1;i
++) {
1203 free_dma_iso_ctx(&video
->ir_context
[i
]);
1205 kfree(video
->ir_context
);
1208 /* Free the iso transmit contexts */
1209 if (video
->it_context
) {
1210 for (i
=0;i
<video
->ohci
->nb_iso_xmit_ctx
;i
++) {
1211 free_dma_iso_ctx(&video
->it_context
[i
]);
1213 kfree(video
->it_context
);
1219 /* EXPORT_NO_SYMBOLS; */
1221 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1222 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1223 MODULE_SUPPORTED_DEVICE("video1394");
1225 void cleanup_module(void)
1228 unregister_chrdev(VIDEO1394_MAJOR
, VIDEO1394_DRIVER_NAME
);
1230 for (i
=0; i
<num_of_video_cards
; i
++)
1231 remove_card(&video_cards
[i
]);
1233 printk(KERN_INFO
"removed " VIDEO1394_DRIVER_NAME
" module\n");
1236 int init_module(void)
1238 struct ti_ohci
*ohci
;
1241 memset(video_cards
, 0, MAX_OHCI1394_CARDS
* sizeof(struct video_card
));
1242 num_of_video_cards
= 0;
1244 for (i
=0; i
<MAX_OHCI1394_CARDS
; i
++) {
1245 ohci
=ohci1394_get_struct(i
);
1247 num_of_video_cards
++;
1248 video1394_init(i
, ohci
);
1252 if (!num_of_video_cards
) {
1253 PRINT_G(KERN_INFO
, "no ohci card found... init failed");
1257 if (register_chrdev(VIDEO1394_MAJOR
, VIDEO1394_DRIVER_NAME
,
1259 printk("video1394: unable to get major %d\n",
1264 PRINT_G(KERN_INFO
, "initialized with %d ohci cards",
1265 num_of_video_cards
);