Merge with Linu 2.4.0-test6-pre6.
[linux-2.6/linux-mips.git] / drivers / ieee1394 / video1394.c
blobb42ce0b460c3cf035219a556f25033c42abd5e46
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 struct it_dma_prg {
61 struct dma_cmd begin;
62 quadlet_t data[4];
63 struct dma_cmd end;
64 quadlet_t pad[4]; /* FIXME: quick hack for memory alignment */
67 struct dma_iso_ctx {
68 struct ti_ohci *ohci;
69 int ctx;
70 int channel;
71 int last_buffer;
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;
77 unsigned int nb_cmd;
78 unsigned char *buf;
79 struct dma_cmd **ir_prg;
80 struct it_dma_prg **it_prg;
81 unsigned int *buffer_status;
82 int ctrlClear;
83 int ctrlSet;
84 int cmdPtr;
85 int ctxMatch;
86 wait_queue_head_t waitq;
89 struct video_card {
90 struct ti_ohci *ohci;
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
99 #endif
101 #ifdef DBGMSG
102 #undef DBGMSG
103 #endif
105 #ifdef VIDEO1394_DEBUG
106 #define DBGMSG(card, fmt, args...) \
107 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
108 #else
109 #define DBGMSG(card, fmt, args...)
110 #endif
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))
148 #endif
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;
156 pmd_t *pmd;
157 pte_t *ptep, pte;
159 if (!pgd_none(*pgd)) {
160 pmd = pmd_offset(pgd, adr);
161 if (!pmd_none(*pmd)) {
162 ptep = pte_offset(pmd, adr);
163 pte = *ptep;
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));
171 return 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));
181 return 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));
192 return 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);
205 ret = __pa(kva);
206 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
207 return ret;
210 static void * rvmalloc(unsigned long size)
212 void * mem;
213 unsigned long adr, page;
215 mem=vmalloc(size);
216 if (mem)
218 memset(mem, 0, size); /* Clear the ram out,
219 no junk to the user */
220 adr=(unsigned long) mem;
221 while (size > 0)
223 page = kvirt_to_pa(adr);
224 mem_map_reserve(MAP_NR(__va(page)));
225 adr+=PAGE_SIZE;
226 size-=PAGE_SIZE;
229 return mem;
232 static void rvfree(void * mem, unsigned long size)
234 unsigned long adr, page;
236 if (mem)
238 adr=(unsigned long) mem;
239 while (size > 0)
241 page = kvirt_to_pa(adr);
242 mem_map_unreserve(MAP_NR(__va(page)));
243 adr+=PAGE_SIZE;
244 size-=PAGE_SIZE;
246 vfree(mem);
250 static int free_dma_iso_ctx(struct dma_iso_ctx **d)
252 int i;
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);
266 if ((*d)->ir_prg) {
267 for (i=0;i<(*d)->num_desc;i++)
268 if ((*d)->ir_prg[i]) kfree((*d)->ir_prg[i]);
269 kfree((*d)->ir_prg);
272 if ((*d)->it_prg) {
273 for (i=0;i<(*d)->num_desc;i++)
274 if ((*d)->it_prg[i]) kfree((*d)->it_prg[i]);
275 kfree((*d)->it_prg);
278 if ((*d)->buffer_status)
279 kfree((*d)->buffer_status);
281 kfree(*d);
282 *d = NULL;
284 return 0;
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;
292 int i;
294 d = (struct dma_iso_ctx *)kmalloc(sizeof(struct dma_iso_ctx),
295 GFP_KERNEL);
296 memset(d, 0, sizeof(struct dma_iso_ctx));
298 if (d==NULL) {
299 PRINT(KERN_ERR, ohci->id, "failed to allocate dma_iso_ctx");
300 return NULL;
303 d->ohci = (void *)ohci;
304 d->ctx = ctx;
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);
310 else
311 d->buf_size = buf_size;
312 d->last_buffer = -1;
313 d->buf = NULL;
314 d->ir_prg = NULL;
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);
322 return NULL;
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 *),
333 GFP_KERNEL);
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);
339 return NULL;
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),
350 GFP_KERNEL);
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);
355 return NULL;
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 *),
365 GFP_KERNEL);
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);
371 return NULL;
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",
380 packet_size);
381 free_dma_iso_ctx(&d);
382 return NULL;
385 d->nb_cmd = d->frame_size / d->packet_size;
386 if (d->frame_size % d->packet_size) {
387 d->nb_cmd++;
388 d->left_size = d->frame_size % d->packet_size;
390 else
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),
396 GFP_KERNEL);
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);
401 return NULL;
406 d->buffer_status = kmalloc(d->num_desc * sizeof(unsigned int),
407 GFP_KERNEL);
409 if (d->buffer_status == NULL) {
410 PRINT(KERN_ERR, ohci->id, "failed to allocate dma ir prg");
411 free_dma_iso_ctx(&d);
412 return NULL;
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);
421 return d;
424 static void reset_ir_status(struct dma_iso_ctx *d, int n)
426 int i;
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;
438 int i;
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))
444 & 0xfffffff0) | 0x1;
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))
450 & 0xfffffff0) | 0x1;
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))
458 & 0xfffffff0) | 0x1;
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;
469 int i;
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)
492 int i;
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)
498 return i;
501 PRINT(KERN_ERR, ohci->id,
502 "no iso context is listening to channel %d",
503 channel);
504 return -1;
507 int it_ctx_talking(struct video_card *video, int channel)
509 int i;
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)
515 return i;
518 PRINT(KERN_ERR, ohci->id,
519 "no iso context is talking to channel %d",
520 channel);
521 return -1;
524 int wakeup_dma_ir_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
526 int i;
528 if (d==NULL) {
529 PRINT(KERN_ERR, ohci->id, "Iso receive event received but "
530 "context not allocated");
531 return -EFAULT;
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);
541 return 0;
544 int wakeup_dma_it_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
546 int i;
548 if (d==NULL) {
549 PRINT(KERN_ERR, ohci->id, "Iso transmit event received but "
550 "context not allocated");
551 return -EFAULT;
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);
561 return 0;
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;
568 int i;
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 */
578 it_prg[i].data[0] =
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);
589 if (i<d->nb_cmd-1) {
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))
593 & 0xfffffff0) | 0x3;
594 it_prg[i].end.branchAddress =
595 (virt_to_bus(&(it_prg[i+1].begin.control))
596 & 0xfffffff0) | 0x3;
598 else {
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;
607 #if 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]);
614 #endif
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;
621 int i;
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",
641 d->ctx);
642 return -EINVAL;
644 if (!d->buf) {
645 PRINT(KERN_ERR, ohci->id,
646 "iso context %d is not allocated", d->ctx);
647 return -EINVAL;
650 pos=(unsigned long) d->buf;
651 while (size > 0) {
652 page = kvirt_to_pa(pos);
653 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
654 return -EAGAIN;
655 start+=PAGE_SIZE;
656 pos+=PAGE_SIZE;
657 size-=PAGE_SIZE;
659 return 0;
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;
668 switch(cmd)
670 case VIDEO1394_LISTEN_CHANNEL:
671 case VIDEO1394_TALK_CHANNEL:
673 struct video1394_mmap v;
674 int i;
676 if(copy_from_user(&v, (void *)arg, sizeof(v)))
677 return -EFAULT;
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);
681 return -EFAULT;
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);
686 return -EFAULT;
689 if (v.buf_size<=0) {
690 PRINT(KERN_ERR, ohci->id,
691 "Invalid %d length buffer requested",v.buf_size);
692 return -EFAULT;
695 if (v.nb_buffers<=0) {
696 PRINT(KERN_ERR, ohci->id,
697 "Invalid %d buffers requested",v.nb_buffers);
698 return -EFAULT;
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);
705 return -EFAULT;
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");
716 return -EFAULT;
719 video->ir_context[i] =
720 alloc_dma_iso_ctx(ohci, ISO_RECEIVE, i+1,
721 v.nb_buffers, v.buf_size,
722 v.channel, 0);
724 if (video->ir_context[i] == NULL) {
725 PRINT(KERN_ERR, ohci->id,
726 "Couldn't allocate ir context");
727 return -EFAULT;
729 initialize_dma_ir_ctx(video->ir_context[i],
730 v.sync_tag);
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,
738 v.channel);
740 else {
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");
748 return -EFAULT;
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");
759 return -EFAULT;
761 initialize_dma_it_ctx(video->it_context[i],
762 v.sync_tag);
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,
770 v.channel);
773 if(copy_to_user((void *)arg, &v, sizeof(v)))
774 return -EFAULT;
776 return 0;
778 case VIDEO1394_UNLISTEN_CHANNEL:
779 case VIDEO1394_UNTALK_CHANNEL:
781 int channel;
782 int i;
784 if(copy_from_user(&channel, (void *)arg, sizeof(int)))
785 return -EFAULT;
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);
790 return -EFAULT;
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",
801 i+1, channel);
803 else {
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",
811 i, channel);
814 return 0;
816 case VIDEO1394_LISTEN_QUEUE_BUFFER:
818 struct video1394_wait v;
819 struct dma_iso_ctx *d;
820 int i;
822 if(copy_from_user(&v, (void *)arg, sizeof(v)))
823 return -EFAULT;
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);
832 return -EFAULT;
835 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
836 PRINT(KERN_ERR, ohci->id,
837 "buffer %d is already used",v.buffer);
838 return -EFAULT;
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))
846 & 0xfffffff0) | 0x1;
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);
860 /* Run IR context */
861 reg_write(ohci, d->ctrlSet, 0x8000);
863 else {
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);
871 return 0;
874 case VIDEO1394_LISTEN_WAIT_BUFFER:
876 struct video1394_wait v;
877 struct dma_iso_ctx *d;
878 int i;
880 if(copy_from_user(&v, (void *)arg, sizeof(v)))
881 return -EFAULT;
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);
890 return -EFAULT;
893 switch(d->buffer_status[v.buffer]) {
894 case VIDEO1394_BUFFER_READY:
895 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
896 return 0;
897 case VIDEO1394_BUFFER_QUEUED:
898 #if 1
899 while(d->buffer_status[v.buffer]!=
900 VIDEO1394_BUFFER_READY) {
901 interruptible_sleep_on(&d->waitq);
902 if(signal_pending(current)) return -EINTR;
904 #else
905 if (wait_event_interruptible(d->waitq,
906 d->buffer_status[v.buffer]
907 == VIDEO1394_BUFFER_READY)
908 == -ERESTARTSYS)
909 return -EINTR;
910 #endif
911 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
912 return 0;
913 default:
914 PRINT(KERN_ERR, ohci->id,
915 "buffer %d is not queued",v.buffer);
916 return -EFAULT;
919 case VIDEO1394_TALK_QUEUE_BUFFER:
921 struct video1394_wait v;
922 struct dma_iso_ctx *d;
923 int i;
925 if(copy_from_user(&v, (void *)arg, sizeof(v)))
926 return -EFAULT;
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);
935 return -EFAULT;
938 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
939 PRINT(KERN_ERR, ohci->id,
940 "buffer %d is already used",v.buffer);
941 return -EFAULT;
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))
950 & 0xfffffff0) | 0x3;
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))
955 & 0xfffffff0) | 0x3;
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",
964 d->ctx);
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);
970 /* Run IT context */
971 reg_write(ohci, d->ctrlSet, 0x8000);
973 else {
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",
978 d->ctx);
979 reg_write(ohci, d->ctrlSet, 0x1000);
982 return 0;
985 case VIDEO1394_TALK_WAIT_BUFFER:
987 struct video1394_wait v;
988 struct dma_iso_ctx *d;
989 int i;
991 if(copy_from_user(&v, (void *)arg, sizeof(v)))
992 return -EFAULT;
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);
1001 return -EFAULT;
1004 switch(d->buffer_status[v.buffer]) {
1005 case VIDEO1394_BUFFER_READY:
1006 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1007 return 0;
1008 case VIDEO1394_BUFFER_QUEUED:
1009 #if 1
1010 while(d->buffer_status[v.buffer]!=
1011 VIDEO1394_BUFFER_READY) {
1012 interruptible_sleep_on(&d->waitq);
1013 if(signal_pending(current)) return -EINTR;
1015 #else
1016 if (wait_event_interruptible(d->waitq,
1017 d->buffer_status[v.buffer]
1018 == VIDEO1394_BUFFER_READY)
1019 == -ERESTARTSYS)
1020 return -EINTR;
1021 #endif
1022 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1023 return 0;
1024 default:
1025 PRINT(KERN_ERR, ohci->id,
1026 "buffer %d is not queued",v.buffer);
1027 return -EFAULT;
1030 default:
1031 return -EINVAL;
1036 * This maps the vmalloced and reserved buffer to user space.
1038 * FIXME:
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;
1049 int res = -EINVAL;
1051 lock_kernel();
1052 ohci = video->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");
1056 } else
1057 res = do_iso_mmap(ohci, video->current_ctx,
1058 (char *)vma->vm_start,
1059 (unsigned long)(vma->vm_end-vma->vm_start));
1060 unlock_kernel();
1061 return res;
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);
1070 return -EIO;
1073 V22_COMPAT_MOD_INC_USE_COUNT;
1075 PRINT(KERN_INFO, i, "open");
1077 return 0;
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;
1084 int i;
1086 lock_kernel();
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");
1123 unlock_kernel();
1124 return 0;
1127 void irq_handler(int card, quadlet_t isoRecvIntEvent,
1128 quadlet_t isoXmitIntEvent)
1130 int i;
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=
1149 OWNER_THIS_MODULE
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");
1162 return -1;
1165 video->ohci = ohci;
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 *));
1174 else {
1175 PRINT(KERN_ERR, ohci->id, "Cannot allocate ir_context");
1176 return -1;
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 *));
1186 else {
1187 PRINT(KERN_ERR, ohci->id, "Cannot allocate it_context");
1188 return -1;
1191 return 0;
1194 static void remove_card(struct video_card *video)
1196 int i;
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);
1217 #ifdef MODULE
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)
1227 int i;
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;
1239 int i;
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);
1246 if (ohci) {
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");
1254 return -EIO;
1257 if (register_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME,
1258 &video1394_fops)) {
1259 printk("video1394: unable to get major %d\n",
1260 VIDEO1394_MAJOR);
1261 return -EIO;
1264 PRINT_G(KERN_INFO, "initialized with %d ohci cards",
1265 num_of_video_cards);
1267 return 0;
1270 #endif /* MODULE */