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 <asm/byteorder.h>
31 #include <asm/atomic.h>
33 #include <asm/uaccess.h>
34 #include <linux/proc_fs.h>
35 #include <linux/tqueue.h>
36 #include <linux/delay.h>
38 #include <asm/pgtable.h>
40 #include <linux/sched.h>
41 #include <asm/segment.h>
42 #include <linux/types.h>
43 #include <linux/wrapper.h>
44 #include <linux/vmalloc.h>
47 #include "ieee1394_types.h"
49 #include "ieee1394_core.h"
50 #include "video1394.h"
54 #define VIDEO1394_MAJOR 172
55 #define ISO_CHANNELS 64
57 #define ISO_TRANSMIT 1
63 quadlet_t pad
[4]; /* FIXME: quick hack for memory alignment */
71 unsigned int num_desc
;
72 unsigned int buf_size
;
73 unsigned int frame_size
;
74 unsigned int packet_size
;
75 unsigned int left_size
;
78 struct dma_cmd
**ir_prg
;
79 struct it_dma_prg
**it_prg
;
80 unsigned int *buffer_status
;
85 wait_queue_head_t waitq
;
91 struct dma_iso_ctx
**ir_context
;
92 struct dma_iso_ctx
**it_context
;
93 struct dma_iso_ctx
*current_ctx
;
96 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
97 #define VIDEO1394_DEBUG
104 #ifdef VIDEO1394_DEBUG
105 #define DBGMSG(card, fmt, args...) \
106 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
108 #define DBGMSG(card, fmt, args...)
111 /* print general (card independent) information */
112 #define PRINT_G(level, fmt, args...) \
113 printk(level "video1394: " fmt "\n" , ## args)
115 /* print card specific information */
116 #define PRINT(level, card, fmt, args...) \
117 printk(level "video1394_%d: " fmt "\n" , card , ## args)
119 void irq_handler(int card
, quadlet_t isoRecvIntEvent
,
120 quadlet_t isoXmitIntEvent
);
122 static struct video_card video_cards
[MAX_OHCI1394_CARDS
];
123 static int num_of_video_cards
= 0;
124 static struct video_template video_tmpl
= { irq_handler
};
126 /* Taken from bttv.c */
127 /*******************************/
128 /* Memory management functions */
129 /*******************************/
131 #define MDEBUG(x) do { } while(0) /* Debug memory management */
133 /* [DaveM] I've recoded most of this so that:
134 * 1) It's easier to tell what is happening
135 * 2) It's more portable, especially for translating things
136 * out of vmalloc mapped areas in the kernel.
137 * 3) Less unnecessary translations happen.
139 * The code used to assume that the kernel vmalloc mappings
140 * existed in the page tables of every process, this is simply
141 * not guarenteed. We now use pgd_offset_k which is the
142 * defined way to get at the kernel page tables.
145 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
146 #define page_address(x) (x)
149 /* Given PGD from the address space's page table, return the kernel
150 * virtual mapping of the physical memory mapped at ADR.
152 static inline unsigned long uvirt_to_kva(pgd_t
*pgd
, unsigned long adr
)
154 unsigned long ret
= 0UL;
158 if (!pgd_none(*pgd
)) {
159 pmd
= pmd_offset(pgd
, adr
);
160 if (!pmd_none(*pmd
)) {
161 ptep
= pte_offset(pmd
, adr
);
164 ret
= (page_address(pte_page(pte
))|
165 (adr
&(PAGE_SIZE
-1)));
168 MDEBUG(printk("uv2kva(%lx-->%lx)", adr
, ret
));
172 static inline unsigned long uvirt_to_bus(unsigned long adr
)
174 unsigned long kva
, ret
;
176 kva
= uvirt_to_kva(pgd_offset(current
->mm
, adr
), adr
);
177 ret
= virt_to_bus((void *)kva
);
178 MDEBUG(printk("uv2b(%lx-->%lx)", adr
, ret
));
182 static inline unsigned long kvirt_to_bus(unsigned long adr
)
184 unsigned long va
, kva
, ret
;
186 va
= VMALLOC_VMADDR(adr
);
187 kva
= uvirt_to_kva(pgd_offset_k(va
), va
);
188 ret
= virt_to_bus((void *)kva
);
189 MDEBUG(printk("kv2b(%lx-->%lx)", adr
, ret
));
193 /* Here we want the physical address of the memory.
194 * This is used when initializing the contents of the
195 * area and marking the pages as reserved.
197 static inline unsigned long kvirt_to_pa(unsigned long adr
)
199 unsigned long va
, kva
, ret
;
201 va
= VMALLOC_VMADDR(adr
);
202 kva
= uvirt_to_kva(pgd_offset_k(va
), va
);
204 MDEBUG(printk("kv2pa(%lx-->%lx)", adr
, ret
));
208 static void * rvmalloc(unsigned long size
)
211 unsigned long adr
, page
;
216 memset(mem
, 0, size
); /* Clear the ram out,
217 no junk to the user */
218 adr
=(unsigned long) mem
;
221 page
= kvirt_to_pa(adr
);
222 mem_map_reserve(MAP_NR(__va(page
)));
230 static void rvfree(void * mem
, unsigned long size
)
232 unsigned long adr
, page
;
236 adr
=(unsigned long) mem
;
239 page
= kvirt_to_pa(adr
);
240 mem_map_unreserve(MAP_NR(__va(page
)));
248 static int free_dma_iso_ctx(struct dma_iso_ctx
**d
)
251 struct ti_ohci
*ohci
;
253 if ((*d
)==NULL
) return -1;
255 ohci
= (struct ti_ohci
*)(*d
)->ohci
;
257 DBGMSG(ohci
->id
, "Freeing dma_iso_ctx %d", (*d
)->ctx
);
259 ohci1394_stop_context(ohci
, (*d
)->ctrlClear
, NULL
);
261 if ((*d
)->buf
) rvfree((void *)(*d
)->buf
,
262 (*d
)->num_desc
* (*d
)->buf_size
);
265 for (i
=0;i
<(*d
)->num_desc
;i
++)
266 if ((*d
)->ir_prg
[i
]) kfree((*d
)->ir_prg
[i
]);
271 for (i
=0;i
<(*d
)->num_desc
;i
++)
272 if ((*d
)->it_prg
[i
]) kfree((*d
)->it_prg
[i
]);
276 if ((*d
)->buffer_status
)
277 kfree((*d
)->buffer_status
);
285 static struct dma_iso_ctx
*
286 alloc_dma_iso_ctx(struct ti_ohci
*ohci
, int type
, int ctx
, int num_desc
,
287 int buf_size
, int channel
, unsigned int packet_size
)
289 struct dma_iso_ctx
*d
=NULL
;
292 d
= (struct dma_iso_ctx
*)kmalloc(sizeof(struct dma_iso_ctx
),
294 memset(d
, 0, sizeof(struct dma_iso_ctx
));
297 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma_iso_ctx");
301 d
->ohci
= (void *)ohci
;
303 d
->channel
= channel
;
304 d
->num_desc
= num_desc
;
305 d
->frame_size
= buf_size
;
306 if (buf_size
%PAGE_SIZE
)
307 d
->buf_size
= buf_size
+ PAGE_SIZE
- (buf_size
%PAGE_SIZE
);
309 d
->buf_size
= buf_size
;
313 init_waitqueue_head(&d
->waitq
);
315 d
->buf
= rvmalloc(d
->num_desc
* d
->buf_size
);
317 if (d
->buf
== NULL
) {
318 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma buffer");
319 free_dma_iso_ctx(&d
);
322 memset(d
->buf
, 0, d
->num_desc
* d
->buf_size
);
324 if (type
== ISO_RECEIVE
) {
325 d
->ctrlSet
= OHCI1394_IsoRcvContextControlSet
+32*d
->ctx
;
326 d
->ctrlClear
= OHCI1394_IsoRcvContextControlClear
+32*d
->ctx
;
327 d
->cmdPtr
= OHCI1394_IsoRcvCommandPtr
+32*d
->ctx
;
328 d
->ctxMatch
= OHCI1394_IsoRcvContextMatch
+32*d
->ctx
;
330 d
->ir_prg
= kmalloc(d
->num_desc
* sizeof(struct dma_cmd
*),
333 if (d
->ir_prg
== NULL
) {
334 PRINT(KERN_ERR
, ohci
->id
,
335 "failed to allocate dma ir prg");
336 free_dma_iso_ctx(&d
);
339 memset(d
->ir_prg
, 0, d
->num_desc
* sizeof(struct dma_cmd
*));
341 d
->nb_cmd
= d
->buf_size
/ PAGE_SIZE
+ 1;
342 d
->left_size
= (d
->frame_size
% PAGE_SIZE
) ?
343 d
->frame_size
% PAGE_SIZE
: PAGE_SIZE
;
345 for (i
=0;i
<d
->num_desc
;i
++) {
346 d
->ir_prg
[i
] = kmalloc(d
->nb_cmd
*
347 sizeof(struct dma_cmd
),
349 if (d
->ir_prg
[i
] == NULL
) {
350 PRINT(KERN_ERR
, ohci
->id
,
351 "failed to allocate dma ir prg");
352 free_dma_iso_ctx(&d
);
357 else { /* ISO_TRANSMIT */
358 d
->ctrlSet
= OHCI1394_IsoXmitContextControlSet
+16*d
->ctx
;
359 d
->ctrlClear
= OHCI1394_IsoXmitContextControlClear
+16*d
->ctx
;
360 d
->cmdPtr
= OHCI1394_IsoXmitCommandPtr
+16*d
->ctx
;
362 d
->it_prg
= kmalloc(d
->num_desc
* sizeof(struct it_dma_prg
*),
365 if (d
->it_prg
== NULL
) {
366 PRINT(KERN_ERR
, ohci
->id
,
367 "failed to allocate dma it prg");
368 free_dma_iso_ctx(&d
);
371 memset(d
->it_prg
, 0, d
->num_desc
*sizeof(struct it_dma_prg
*));
373 d
->packet_size
= packet_size
;
375 if (PAGE_SIZE
% packet_size
|| packet_size
>2048) {
376 PRINT(KERN_ERR
, ohci
->id
,
377 "Packet size %d not yet supported\n",
379 free_dma_iso_ctx(&d
);
383 d
->nb_cmd
= d
->frame_size
/ d
->packet_size
;
384 if (d
->frame_size
% d
->packet_size
) {
386 d
->left_size
= d
->frame_size
% d
->packet_size
;
389 d
->left_size
= d
->packet_size
;
391 for (i
=0;i
<d
->num_desc
;i
++) {
392 d
->it_prg
[i
] = kmalloc(d
->nb_cmd
*
393 sizeof(struct it_dma_prg
),
395 if (d
->it_prg
[i
] == NULL
) {
396 PRINT(KERN_ERR
, ohci
->id
,
397 "failed to allocate dma it prg");
398 free_dma_iso_ctx(&d
);
404 d
->buffer_status
= kmalloc(d
->num_desc
* sizeof(unsigned int),
407 if (d
->buffer_status
== NULL
) {
408 PRINT(KERN_ERR
, ohci
->id
, "failed to allocate dma ir prg");
409 free_dma_iso_ctx(&d
);
412 memset(d
->buffer_status
, 0, d
->num_desc
* sizeof(unsigned int));
414 PRINT(KERN_INFO
, ohci
->id
, "Iso %s DMA: %d buffers "
415 "of size %d allocated for a frame size %d, each with %d prgs",
416 (type
==ISO_RECEIVE
) ? "receive" : "transmit",
417 d
->num_desc
, d
->buf_size
, d
->frame_size
, d
->nb_cmd
);
422 static void reset_ir_status(struct dma_iso_ctx
*d
, int n
)
425 d
->ir_prg
[n
][0].status
= 4;
426 d
->ir_prg
[n
][1].status
= PAGE_SIZE
-4;
427 for (i
=2;i
<d
->nb_cmd
-1;i
++)
428 d
->ir_prg
[n
][i
].status
= PAGE_SIZE
;
429 d
->ir_prg
[n
][i
].status
= d
->left_size
;
432 static void initialize_dma_ir_prg(struct dma_iso_ctx
*d
, int n
)
434 struct dma_cmd
*ir_prg
= d
->ir_prg
[n
];
435 unsigned long buf
= (unsigned long)d
->buf
+n
*d
->buf_size
;
438 /* the first descriptor will sync and read only 4 bytes */
439 ir_prg
[0].control
= (0x280F << 16) | 4;
440 ir_prg
[0].address
= kvirt_to_bus(buf
);
441 ir_prg
[0].branchAddress
= (virt_to_bus(&(ir_prg
[1].control
))
444 /* the second descriptor will read PAGE_SIZE-4 bytes */
445 ir_prg
[1].control
= (0x280C << 16) | (PAGE_SIZE
-4);
446 ir_prg
[1].address
= kvirt_to_bus(buf
+4);
447 ir_prg
[1].branchAddress
= (virt_to_bus(&(ir_prg
[2].control
))
450 for (i
=2;i
<d
->nb_cmd
-1;i
++) {
451 ir_prg
[i
].control
= (0x280C << 16) | PAGE_SIZE
;
452 ir_prg
[i
].address
= kvirt_to_bus(buf
+(i
-1)*PAGE_SIZE
);
454 ir_prg
[i
].branchAddress
=
455 (virt_to_bus(&(ir_prg
[i
+1].control
))
459 /* the last descriptor will generate an interrupt */
460 ir_prg
[i
].control
= (0x283C << 16) | d
->left_size
;
461 ir_prg
[i
].address
= kvirt_to_bus(buf
+(i
-1)*PAGE_SIZE
);
464 static void initialize_dma_ir_ctx(struct dma_iso_ctx
*d
, int tag
)
466 struct ti_ohci
*ohci
= (struct ti_ohci
*)d
->ohci
;
469 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
471 for (i
=0;i
<d
->num_desc
;i
++) {
472 initialize_dma_ir_prg(d
, i
);
473 reset_ir_status(d
, i
);
476 /* Set bufferFill, no header */
477 reg_write(ohci
, d
->ctrlSet
, 0x80000000);
479 /* Set the context match register to match on all tags,
480 sync for sync tag, and listen to d->channel */
481 reg_write(ohci
, d
->ctxMatch
, 0xf0000000|((tag
&0xf)<<8)|d
->channel
);
483 /* Set up isoRecvIntMask to generate interrupts */
484 reg_write(ohci
, OHCI1394_IsoRecvIntMaskSet
, 1<<d
->ctx
);
487 /* find which context is listening to this channel */
488 int ir_ctx_listening(struct video_card
*video
, int channel
)
491 struct ti_ohci
*ohci
= video
->ohci
;
493 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
494 if (video
->ir_context
[i
]) {
495 if (video
->ir_context
[i
]->channel
==channel
)
499 PRINT(KERN_ERR
, ohci
->id
,
500 "no iso context is listening to channel %d",
505 int it_ctx_talking(struct video_card
*video
, int channel
)
508 struct ti_ohci
*ohci
= video
->ohci
;
510 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
511 if (video
->it_context
[i
]) {
512 if (video
->it_context
[i
]->channel
==channel
)
516 PRINT(KERN_ERR
, ohci
->id
,
517 "no iso context is talking to channel %d",
522 int wakeup_dma_ir_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
527 PRINT(KERN_ERR
, ohci
->id
, "Iso receive event received but "
528 "context not allocated");
532 for (i
=0;i
<d
->num_desc
;i
++) {
533 if (d
->ir_prg
[i
][d
->nb_cmd
-1].status
& 0xFFFF0000) {
534 reset_ir_status(d
, i
);
535 d
->buffer_status
[i
] = VIDEO1394_BUFFER_READY
;
538 if (waitqueue_active(&d
->waitq
)) wake_up_interruptible(&d
->waitq
);
542 int wakeup_dma_it_ctx(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
)
547 PRINT(KERN_ERR
, ohci
->id
, "Iso transmit event received but "
548 "context not allocated");
552 for (i
=0;i
<d
->num_desc
;i
++) {
553 if (d
->it_prg
[i
][d
->nb_cmd
-1].end
.status
& 0xFFFF0000) {
554 d
->it_prg
[i
][d
->nb_cmd
-1].end
.status
= 0;
555 d
->buffer_status
[i
] = VIDEO1394_BUFFER_READY
;
558 if (waitqueue_active(&d
->waitq
)) wake_up_interruptible(&d
->waitq
);
562 static void initialize_dma_it_prg(struct dma_iso_ctx
*d
, int n
, int sync_tag
)
564 struct it_dma_prg
*it_prg
= d
->it_prg
[n
];
565 unsigned long buf
= (unsigned long)d
->buf
+n
*d
->buf_size
;
568 for (i
=0;i
<d
->nb_cmd
;i
++) {
570 it_prg
[i
].begin
.control
= OUTPUT_MORE_IMMEDIATE
| 8 ;
571 it_prg
[i
].begin
.address
= 0;
573 it_prg
[i
].begin
.status
= 0;
575 /* FIXME: what is the tag value + speed selection */
577 (DMA_SPEED_400
<<16) | (d
->channel
<<8) | 0xa0;
578 if (i
==0) it_prg
[i
].data
[0] |= sync_tag
;
579 it_prg
[i
].data
[1] = d
->packet_size
<< 16;
580 it_prg
[i
].data
[2] = 0;
581 it_prg
[i
].data
[3] = 0;
583 it_prg
[i
].end
.control
= 0x100c0000;
584 it_prg
[i
].end
.address
=
585 kvirt_to_bus(buf
+i
*d
->packet_size
);
588 it_prg
[i
].end
.control
|= d
->packet_size
;
589 it_prg
[i
].begin
.branchAddress
=
590 (virt_to_bus(&(it_prg
[i
+1].begin
.control
))
592 it_prg
[i
].end
.branchAddress
=
593 (virt_to_bus(&(it_prg
[i
+1].begin
.control
))
597 /* the last prg generates an interrupt */
598 it_prg
[i
].end
.control
|= 0x08300000 | d
->left_size
;
599 /* the last prg doesn't branch */
600 it_prg
[i
].begin
.branchAddress
= 0;
601 it_prg
[i
].end
.branchAddress
= 0;
603 it_prg
[i
].end
.status
= 0;
606 printk("%d:%d: %08x-%08x ctrl %08x brch %08x d0 %08x d1 %08x\n",n
,i
,
607 virt_to_bus(&(it_prg
[i
].begin
.control
)),
608 virt_to_bus(&(it_prg
[i
].end
.control
)),
609 it_prg
[i
].end
.control
,
610 it_prg
[i
].end
.branchAddress
,
611 it_prg
[i
].data
[0], it_prg
[i
].data
[1]);
616 static void initialize_dma_it_ctx(struct dma_iso_ctx
*d
, int sync_tag
)
618 struct ti_ohci
*ohci
= (struct ti_ohci
*)d
->ohci
;
621 ohci1394_stop_context(ohci
, d
->ctrlClear
, NULL
);
623 for (i
=0;i
<d
->num_desc
;i
++)
624 initialize_dma_it_prg(d
, i
, sync_tag
);
626 /* Set up isoRecvIntMask to generate interrupts */
627 reg_write(ohci
, OHCI1394_IsoXmitIntMaskSet
, 1<<d
->ctx
);
630 static int do_iso_mmap(struct ti_ohci
*ohci
, struct dma_iso_ctx
*d
,
631 const char *adr
, unsigned long size
)
633 unsigned long start
=(unsigned long) adr
;
634 unsigned long page
,pos
;
636 if (size
>d
->num_desc
* d
->buf_size
) {
637 PRINT(KERN_ERR
, ohci
->id
,
638 "iso context %d buf size is different from mmap size",
643 PRINT(KERN_ERR
, ohci
->id
,
644 "iso context %d is not allocated", d
->ctx
);
648 pos
=(unsigned long) d
->buf
;
650 page
= kvirt_to_pa(pos
);
651 if (remap_page_range(start
, page
, PAGE_SIZE
, PAGE_SHARED
))
660 static int video1394_ioctl(struct inode
*inode
, struct file
*file
,
661 unsigned int cmd
, unsigned long arg
)
663 struct video_card
*video
= &video_cards
[MINOR(inode
->i_rdev
)];
664 struct ti_ohci
*ohci
= video
->ohci
;
668 case VIDEO1394_LISTEN_CHANNEL
:
669 case VIDEO1394_TALK_CHANNEL
:
671 struct video1394_mmap v
;
674 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
676 if (v
.channel
<0 || v
.channel
>(ISO_CHANNELS
-1)) {
677 PRINT(KERN_ERR
, ohci
->id
,
678 "iso channel %d out of bound", v
.channel
);
681 if (test_and_set_bit(v
.channel
, &ohci
->IR_channel_usage
)) {
682 PRINT(KERN_ERR
, ohci
->id
,
683 "channel %d is already taken", v
.channel
);
688 PRINT(KERN_ERR
, ohci
->id
,
689 "Invalid %d length buffer requested",v
.buf_size
);
693 if (v
.nb_buffers
<=0) {
694 PRINT(KERN_ERR
, ohci
->id
,
695 "Invalid %d buffers requested",v
.nb_buffers
);
699 if (v
.nb_buffers
* v
.buf_size
> VIDEO1394_MAX_SIZE
) {
700 PRINT(KERN_ERR
, ohci
->id
,
701 "%d buffers of size %d bytes is too big",
702 v
.nb_buffers
, v
.buf_size
);
706 if (cmd
== VIDEO1394_LISTEN_CHANNEL
) {
707 /* find a free iso receive context */
708 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
709 if (video
->ir_context
[i
]==NULL
) break;
711 if (i
==(ohci
->nb_iso_rcv_ctx
-1)) {
712 PRINT(KERN_ERR
, ohci
->id
,
713 "no iso context available");
717 video
->ir_context
[i
] =
718 alloc_dma_iso_ctx(ohci
, ISO_RECEIVE
, i
+1,
719 v
.nb_buffers
, v
.buf_size
,
722 if (video
->ir_context
[i
] == NULL
) {
723 PRINT(KERN_ERR
, ohci
->id
,
724 "Couldn't allocate ir context");
727 initialize_dma_ir_ctx(video
->ir_context
[i
],
730 video
->current_ctx
= video
->ir_context
[i
];
732 v
.buf_size
= video
->ir_context
[i
]->buf_size
;
734 PRINT(KERN_INFO
, ohci
->id
,
735 "iso context %d listen on channel %d", i
+1,
739 /* find a free iso transmit context */
740 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
741 if (video
->it_context
[i
]==NULL
) break;
743 if (i
==ohci
->nb_iso_xmit_ctx
) {
744 PRINT(KERN_ERR
, ohci
->id
,
745 "no iso context available");
749 video
->it_context
[i
] =
750 alloc_dma_iso_ctx(ohci
, ISO_TRANSMIT
, i
,
751 v
.nb_buffers
, v
.buf_size
,
752 v
.channel
, v
.packet_size
);
754 if (video
->it_context
[i
] == NULL
) {
755 PRINT(KERN_ERR
, ohci
->id
,
756 "Couldn't allocate it context");
759 initialize_dma_it_ctx(video
->it_context
[i
],
762 video
->current_ctx
= video
->it_context
[i
];
764 v
.buf_size
= video
->it_context
[i
]->buf_size
;
766 PRINT(KERN_INFO
, ohci
->id
,
767 "iso context %d talk on channel %d", i
,
771 if(copy_to_user((void *)arg
, &v
, sizeof(v
)))
776 case VIDEO1394_UNLISTEN_CHANNEL
:
777 case VIDEO1394_UNTALK_CHANNEL
:
782 if(copy_from_user(&channel
, (void *)arg
, sizeof(int)))
785 if (!test_and_clear_bit(channel
, &ohci
->IR_channel_usage
)) {
786 PRINT(KERN_ERR
, ohci
->id
,
787 "channel %d is not being used", channel
);
791 if (cmd
== VIDEO1394_UNLISTEN_CHANNEL
) {
792 i
= ir_ctx_listening(video
, channel
);
793 if (i
<0) return -EFAULT
;
795 free_dma_iso_ctx(&video
->ir_context
[i
]);
797 PRINT(KERN_INFO
, ohci
->id
,
798 "iso context %d stop listening on channel %d",
802 i
= it_ctx_talking(video
, channel
);
803 if (i
<0) return -EFAULT
;
805 free_dma_iso_ctx(&video
->it_context
[i
]);
807 PRINT(KERN_INFO
, ohci
->id
,
808 "iso context %d stop talking on channel %d",
814 case VIDEO1394_LISTEN_QUEUE_BUFFER
:
816 struct video1394_wait v
;
817 struct dma_iso_ctx
*d
;
820 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
823 i
= ir_ctx_listening(video
, v
.channel
);
824 if (i
<0) return -EFAULT
;
825 d
= video
->ir_context
[i
];
827 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
828 PRINT(KERN_ERR
, ohci
->id
,
829 "buffer %d out of range",v
.buffer
);
833 if (d
->buffer_status
[v
.buffer
]!=VIDEO1394_BUFFER_FREE
) {
834 PRINT(KERN_ERR
, ohci
->id
,
835 "buffer %d is already used",v
.buffer
);
839 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_QUEUED
;
841 if (d
->last_buffer
>=0)
842 d
->ir_prg
[d
->last_buffer
][d
->nb_cmd
-1].branchAddress
=
843 (virt_to_bus(&(d
->ir_prg
[v
.buffer
][0].control
))
846 d
->last_buffer
= v
.buffer
;
848 d
->ir_prg
[d
->last_buffer
][d
->nb_cmd
-1].branchAddress
= 0;
850 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x8000))
852 DBGMSG(ohci
->id
, "Starting iso DMA ctx=%d",d
->ctx
);
854 /* Tell the controller where the first program is */
855 reg_write(ohci
, d
->cmdPtr
,
856 virt_to_bus(&(d
->ir_prg
[v
.buffer
][0]))|0x1);
859 reg_write(ohci
, d
->ctrlSet
, 0x8000);
862 /* Wake up dma context if necessary */
863 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400)) {
864 PRINT(KERN_INFO
, ohci
->id
,
865 "Waking up iso dma ctx=%d", d
->ctx
);
866 reg_write(ohci
, d
->ctrlSet
, 0x1000);
872 case VIDEO1394_LISTEN_WAIT_BUFFER
:
874 struct video1394_wait v
;
875 struct dma_iso_ctx
*d
;
878 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
881 i
= ir_ctx_listening(video
, v
.channel
);
882 if (i
<0) return -EFAULT
;
883 d
= video
->ir_context
[i
];
885 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
886 PRINT(KERN_ERR
, ohci
->id
,
887 "buffer %d out of range",v
.buffer
);
891 switch(d
->buffer_status
[v
.buffer
]) {
892 case VIDEO1394_BUFFER_READY
:
893 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
895 case VIDEO1394_BUFFER_QUEUED
:
897 while(d
->buffer_status
[v
.buffer
]!=
898 VIDEO1394_BUFFER_READY
) {
899 interruptible_sleep_on(&d
->waitq
);
900 if(signal_pending(current
)) return -EINTR
;
903 if (wait_event_interruptible(d
->waitq
,
904 d
->buffer_status
[v
.buffer
]
905 == VIDEO1394_BUFFER_READY
)
909 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
912 PRINT(KERN_ERR
, ohci
->id
,
913 "buffer %d is not queued",v
.buffer
);
917 case VIDEO1394_TALK_QUEUE_BUFFER
:
919 struct video1394_wait v
;
920 struct dma_iso_ctx
*d
;
923 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
926 i
= it_ctx_talking(video
, v
.channel
);
927 if (i
<0) return -EFAULT
;
928 d
= video
->it_context
[i
];
930 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
931 PRINT(KERN_ERR
, ohci
->id
,
932 "buffer %d out of range",v
.buffer
);
936 if (d
->buffer_status
[v
.buffer
]!=VIDEO1394_BUFFER_FREE
) {
937 PRINT(KERN_ERR
, ohci
->id
,
938 "buffer %d is already used",v
.buffer
);
942 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_QUEUED
;
944 if (d
->last_buffer
>=0) {
945 d
->it_prg
[d
->last_buffer
]
946 [d
->nb_cmd
-1].end
.branchAddress
=
947 (virt_to_bus(&(d
->it_prg
[v
.buffer
][0].begin
.control
))
950 d
->it_prg
[d
->last_buffer
]
951 [d
->nb_cmd
-1].begin
.branchAddress
=
952 (virt_to_bus(&(d
->it_prg
[v
.buffer
][0].begin
.control
))
955 d
->last_buffer
= v
.buffer
;
957 d
->it_prg
[d
->last_buffer
][d
->nb_cmd
-1].end
.branchAddress
= 0;
959 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x8000))
961 DBGMSG(ohci
->id
, "Starting iso transmit DMA ctx=%d",
964 /* Tell the controller where the first program is */
965 reg_write(ohci
, d
->cmdPtr
,
966 virt_to_bus(&(d
->it_prg
[v
.buffer
][0]))|0x3);
969 reg_write(ohci
, d
->ctrlSet
, 0x8000);
972 /* Wake up dma context if necessary */
973 if (!(reg_read(ohci
, d
->ctrlSet
) & 0x400)) {
974 PRINT(KERN_INFO
, ohci
->id
,
975 "Waking up iso transmit dma ctx=%d",
977 reg_write(ohci
, d
->ctrlSet
, 0x1000);
983 case VIDEO1394_TALK_WAIT_BUFFER
:
985 struct video1394_wait v
;
986 struct dma_iso_ctx
*d
;
989 if(copy_from_user(&v
, (void *)arg
, sizeof(v
)))
992 i
= it_ctx_talking(video
, v
.channel
);
993 if (i
<0) return -EFAULT
;
994 d
= video
->it_context
[i
];
996 if ((v
.buffer
<0) || (v
.buffer
>d
->num_desc
)) {
997 PRINT(KERN_ERR
, ohci
->id
,
998 "buffer %d out of range",v
.buffer
);
1002 switch(d
->buffer_status
[v
.buffer
]) {
1003 case VIDEO1394_BUFFER_READY
:
1004 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1006 case VIDEO1394_BUFFER_QUEUED
:
1008 while(d
->buffer_status
[v
.buffer
]!=
1009 VIDEO1394_BUFFER_READY
) {
1010 interruptible_sleep_on(&d
->waitq
);
1011 if(signal_pending(current
)) return -EINTR
;
1014 if (wait_event_interruptible(d
->waitq
,
1015 d
->buffer_status
[v
.buffer
]
1016 == VIDEO1394_BUFFER_READY
)
1020 d
->buffer_status
[v
.buffer
]=VIDEO1394_BUFFER_FREE
;
1023 PRINT(KERN_ERR
, ohci
->id
,
1024 "buffer %d is not queued",v
.buffer
);
1034 * This maps the vmalloced and reserved buffer to user space.
1037 * - PAGE_READONLY should suffice!?
1038 * - remap_page_range is kind of inefficient for page by page remapping.
1039 * But e.g. pte_alloc() does not work in modules ... :-(
1042 int video1394_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1044 struct video_card
*video
=
1045 &video_cards
[MINOR(file
->f_dentry
->d_inode
->i_rdev
)];
1046 struct ti_ohci
*ohci
= video
->ohci
;
1048 PRINT(KERN_INFO
, ohci
->id
, "mmap");
1049 if (video
->current_ctx
== NULL
) {
1050 PRINT(KERN_ERR
, ohci
->id
, "current iso context not set");
1054 return do_iso_mmap(ohci
, video
->current_ctx
,
1055 (char *)vma
->vm_start
,
1056 (unsigned long)(vma
->vm_end
-vma
->vm_start
));
1060 static int video1394_open(struct inode
*inode
, struct file
*file
)
1062 int i
= MINOR(inode
->i_rdev
);
1064 if (i
<0 || i
>=num_of_video_cards
) {
1065 PRINT(KERN_ERR
, i
, "ohci card %d not found", i
);
1069 V22_COMPAT_MOD_INC_USE_COUNT
;
1071 PRINT(KERN_INFO
, i
, "open");
1076 static int video1394_release(struct inode
*inode
, struct file
*file
)
1078 struct video_card
*video
= &video_cards
[MINOR(inode
->i_rdev
)];
1079 struct ti_ohci
*ohci
= video
->ohci
;
1082 for (i
=0;i
<ohci
->nb_iso_rcv_ctx
-1;i
++)
1083 if (video
->ir_context
[i
]) {
1084 if (!test_and_clear_bit(
1085 video
->ir_context
[i
]->channel
,
1086 &ohci
->IR_channel_usage
)) {
1087 PRINT(KERN_ERR
, ohci
->id
,
1088 "channel %d is not being used",
1089 video
->ir_context
[i
]->channel
);
1091 PRINT(KERN_INFO
, ohci
->id
,
1092 "iso receive context %d stop listening "
1093 "on channel %d", i
+1,
1094 video
->ir_context
[i
]->channel
);
1095 free_dma_iso_ctx(&video
->ir_context
[i
]);
1098 for (i
=0;i
<ohci
->nb_iso_xmit_ctx
;i
++)
1099 if (video
->it_context
[i
]) {
1100 if (!test_and_clear_bit(
1101 video
->it_context
[i
]->channel
,
1102 &ohci
->IR_channel_usage
)) {
1103 PRINT(KERN_ERR
, ohci
->id
,
1104 "channel %d is not being used",
1105 video
->it_context
[i
]->channel
);
1107 PRINT(KERN_INFO
, ohci
->id
,
1108 "iso transmit context %d stop talking "
1109 "on channel %d", i
+1,
1110 video
->it_context
[i
]->channel
);
1111 free_dma_iso_ctx(&video
->it_context
[i
]);
1115 V22_COMPAT_MOD_DEC_USE_COUNT
;
1117 PRINT(KERN_INFO
, ohci
->id
, "release");
1121 void irq_handler(int card
, quadlet_t isoRecvIntEvent
,
1122 quadlet_t isoXmitIntEvent
)
1125 struct video_card
*video
= &video_cards
[card
];
1127 DBGMSG(card
, "Iso event Recv: %08x Xmit: %08x",
1128 isoRecvIntEvent
, isoXmitIntEvent
);
1130 for (i
=0;i
<video
->ohci
->nb_iso_rcv_ctx
-1;i
++)
1131 if (isoRecvIntEvent
& (1<<(i
+1)))
1132 wakeup_dma_ir_ctx(video
->ohci
,
1133 video
->ir_context
[i
]);
1135 for (i
=0;i
<video
->ohci
->nb_iso_xmit_ctx
;i
++)
1136 if (isoXmitIntEvent
& (1<<i
))
1137 wakeup_dma_it_ctx(video
->ohci
,
1138 video
->it_context
[i
]);
1141 static struct file_operations video1394_fops
=
1144 ioctl
: video1394_ioctl
,
1145 mmap
: video1394_mmap
,
1146 open
: video1394_open
,
1147 release
: video1394_release
1150 static int video1394_init(int i
, struct ti_ohci
*ohci
)
1152 struct video_card
*video
= &video_cards
[i
];
1154 if (ohci1394_register_video(ohci
, &video_tmpl
)<0) {
1155 PRINT(KERN_ERR
, i
, "register_video failed");
1161 /* Iso receive dma contexts */
1162 video
->ir_context
= (struct dma_iso_ctx
**)
1163 kmalloc((ohci
->nb_iso_rcv_ctx
-1)*
1164 sizeof(struct dma_iso_ctx
*), GFP_KERNEL
);
1165 if (video
->ir_context
)
1166 memset(video
->ir_context
, 0,
1167 (ohci
->nb_iso_rcv_ctx
-1)*sizeof(struct dma_iso_ctx
*));
1169 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate ir_context");
1173 /* Iso transmit dma contexts */
1174 video
->it_context
= (struct dma_iso_ctx
**)
1175 kmalloc(ohci
->nb_iso_xmit_ctx
*
1176 sizeof(struct dma_iso_ctx
*), GFP_KERNEL
);
1177 if (video
->it_context
)
1178 memset(video
->it_context
, 0,
1179 ohci
->nb_iso_xmit_ctx
* sizeof(struct dma_iso_ctx
*));
1181 PRINT(KERN_ERR
, ohci
->id
, "Cannot allocate it_context");
1188 static void remove_card(struct video_card
*video
)
1192 ohci1394_unregister_video(video
->ohci
, &video_tmpl
);
1194 /* Free the iso receive contexts */
1195 if (video
->ir_context
) {
1196 for (i
=0;i
<video
->ohci
->nb_iso_rcv_ctx
-1;i
++) {
1197 free_dma_iso_ctx(&video
->ir_context
[i
]);
1199 kfree(video
->ir_context
);
1202 /* Free the iso transmit contexts */
1203 if (video
->it_context
) {
1204 for (i
=0;i
<video
->ohci
->nb_iso_xmit_ctx
;i
++) {
1205 free_dma_iso_ctx(&video
->it_context
[i
]);
1207 kfree(video
->it_context
);
1213 /* EXPORT_NO_SYMBOLS; */
1215 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1216 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1217 MODULE_SUPPORTED_DEVICE("video1394");
1219 void cleanup_module(void)
1222 unregister_chrdev(VIDEO1394_MAJOR
, VIDEO1394_DRIVER_NAME
);
1224 for (i
=0; i
<num_of_video_cards
; i
++)
1225 remove_card(&video_cards
[i
]);
1227 printk(KERN_INFO
"removed " VIDEO1394_DRIVER_NAME
" module\n");
1230 int init_module(void)
1232 struct ti_ohci
*ohci
;
1235 memset(video_cards
, 0, MAX_OHCI1394_CARDS
* sizeof(struct video_card
));
1236 num_of_video_cards
= 0;
1238 for (i
=0; i
<MAX_OHCI1394_CARDS
; i
++) {
1239 ohci
=ohci1394_get_struct(i
);
1241 num_of_video_cards
++;
1242 video1394_init(i
, ohci
);
1246 if (!num_of_video_cards
) {
1247 PRINT_G(KERN_INFO
, "no ohci card found... init failed");
1251 if (register_chrdev(VIDEO1394_MAJOR
, VIDEO1394_DRIVER_NAME
,
1253 printk("video1394: unable to get major %d\n",
1258 PRINT_G(KERN_INFO
, "initialized with %d ohci cards",
1259 num_of_video_cards
);