This is pre8 ...
[linux-2.6/linux-mips.git] / drivers / ieee1394 / video1394.c
blob9f00fd3f06b74b2cb9997965eb5e15095ce93064
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 <asm/byteorder.h>
31 #include <asm/atomic.h>
32 #include <asm/io.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>
39 #include <asm/page.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>
46 #include "ieee1394.h"
47 #include "ieee1394_types.h"
48 #include "hosts.h"
49 #include "ieee1394_core.h"
50 #include "video1394.h"
52 #include "ohci1394.h"
54 #define VIDEO1394_MAJOR 172
55 #define ISO_CHANNELS 64
56 #define ISO_RECEIVE 0
57 #define ISO_TRANSMIT 1
59 struct it_dma_prg {
60 struct dma_cmd begin;
61 quadlet_t data[4];
62 struct dma_cmd end;
63 quadlet_t pad[4]; /* FIXME: quick hack for memory alignment */
66 struct dma_iso_ctx {
67 struct ti_ohci *ohci;
68 int ctx;
69 int channel;
70 int last_buffer;
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;
76 unsigned int nb_cmd;
77 unsigned char *buf;
78 struct dma_cmd **ir_prg;
79 struct it_dma_prg **it_prg;
80 unsigned int *buffer_status;
81 int ctrlClear;
82 int ctrlSet;
83 int cmdPtr;
84 int ctxMatch;
85 wait_queue_head_t waitq;
88 struct video_card {
89 struct ti_ohci *ohci;
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
98 #endif
100 #ifdef DBGMSG
101 #undef DBGMSG
102 #endif
104 #ifdef VIDEO1394_DEBUG
105 #define DBGMSG(card, fmt, args...) \
106 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
107 #else
108 #define DBGMSG(card, fmt, args...)
109 #endif
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)
147 #endif
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;
155 pmd_t *pmd;
156 pte_t *ptep, pte;
158 if (!pgd_none(*pgd)) {
159 pmd = pmd_offset(pgd, adr);
160 if (!pmd_none(*pmd)) {
161 ptep = pte_offset(pmd, adr);
162 pte = *ptep;
163 if(pte_present(pte))
164 ret = (page_address(pte_page(pte))|
165 (adr&(PAGE_SIZE-1)));
168 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
169 return 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));
179 return 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));
190 return 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);
203 ret = __pa(kva);
204 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
205 return ret;
208 static void * rvmalloc(unsigned long size)
210 void * mem;
211 unsigned long adr, page;
213 mem=vmalloc(size);
214 if (mem)
216 memset(mem, 0, size); /* Clear the ram out,
217 no junk to the user */
218 adr=(unsigned long) mem;
219 while (size > 0)
221 page = kvirt_to_pa(adr);
222 mem_map_reserve(MAP_NR(__va(page)));
223 adr+=PAGE_SIZE;
224 size-=PAGE_SIZE;
227 return mem;
230 static void rvfree(void * mem, unsigned long size)
232 unsigned long adr, page;
234 if (mem)
236 adr=(unsigned long) mem;
237 while (size > 0)
239 page = kvirt_to_pa(adr);
240 mem_map_unreserve(MAP_NR(__va(page)));
241 adr+=PAGE_SIZE;
242 size-=PAGE_SIZE;
244 vfree(mem);
248 static int free_dma_iso_ctx(struct dma_iso_ctx **d)
250 int i;
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);
264 if ((*d)->ir_prg) {
265 for (i=0;i<(*d)->num_desc;i++)
266 if ((*d)->ir_prg[i]) kfree((*d)->ir_prg[i]);
267 kfree((*d)->ir_prg);
270 if ((*d)->it_prg) {
271 for (i=0;i<(*d)->num_desc;i++)
272 if ((*d)->it_prg[i]) kfree((*d)->it_prg[i]);
273 kfree((*d)->it_prg);
276 if ((*d)->buffer_status)
277 kfree((*d)->buffer_status);
279 kfree(*d);
280 *d = NULL;
282 return 0;
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;
290 int i;
292 d = (struct dma_iso_ctx *)kmalloc(sizeof(struct dma_iso_ctx),
293 GFP_KERNEL);
294 memset(d, 0, sizeof(struct dma_iso_ctx));
296 if (d==NULL) {
297 PRINT(KERN_ERR, ohci->id, "failed to allocate dma_iso_ctx");
298 return NULL;
301 d->ohci = (void *)ohci;
302 d->ctx = ctx;
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);
308 else
309 d->buf_size = buf_size;
310 d->last_buffer = -1;
311 d->buf = NULL;
312 d->ir_prg = NULL;
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);
320 return NULL;
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 *),
331 GFP_KERNEL);
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);
337 return NULL;
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),
348 GFP_KERNEL);
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);
353 return NULL;
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 *),
363 GFP_KERNEL);
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);
369 return NULL;
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",
378 packet_size);
379 free_dma_iso_ctx(&d);
380 return NULL;
383 d->nb_cmd = d->frame_size / d->packet_size;
384 if (d->frame_size % d->packet_size) {
385 d->nb_cmd++;
386 d->left_size = d->frame_size % d->packet_size;
388 else
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),
394 GFP_KERNEL);
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);
399 return NULL;
404 d->buffer_status = kmalloc(d->num_desc * sizeof(unsigned int),
405 GFP_KERNEL);
407 if (d->buffer_status == NULL) {
408 PRINT(KERN_ERR, ohci->id, "failed to allocate dma ir prg");
409 free_dma_iso_ctx(&d);
410 return NULL;
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);
419 return d;
422 static void reset_ir_status(struct dma_iso_ctx *d, int n)
424 int i;
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;
436 int i;
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))
442 & 0xfffffff0) | 0x1;
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))
448 & 0xfffffff0) | 0x1;
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))
456 & 0xfffffff0) | 0x1;
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;
467 int i;
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)
490 int i;
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)
496 return i;
499 PRINT(KERN_ERR, ohci->id,
500 "no iso context is listening to channel %d",
501 channel);
502 return -1;
505 int it_ctx_talking(struct video_card *video, int channel)
507 int i;
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)
513 return i;
516 PRINT(KERN_ERR, ohci->id,
517 "no iso context is talking to channel %d",
518 channel);
519 return -1;
522 int wakeup_dma_ir_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
524 int i;
526 if (d==NULL) {
527 PRINT(KERN_ERR, ohci->id, "Iso receive event received but "
528 "context not allocated");
529 return -EFAULT;
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);
539 return 0;
542 int wakeup_dma_it_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
544 int i;
546 if (d==NULL) {
547 PRINT(KERN_ERR, ohci->id, "Iso transmit event received but "
548 "context not allocated");
549 return -EFAULT;
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);
559 return 0;
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;
566 int i;
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 */
576 it_prg[i].data[0] =
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);
587 if (i<d->nb_cmd-1) {
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))
591 & 0xfffffff0) | 0x3;
592 it_prg[i].end.branchAddress =
593 (virt_to_bus(&(it_prg[i+1].begin.control))
594 & 0xfffffff0) | 0x3;
596 else {
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;
605 #if 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]);
612 #endif
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;
619 int i;
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",
639 d->ctx);
640 return -EINVAL;
642 if (!d->buf) {
643 PRINT(KERN_ERR, ohci->id,
644 "iso context %d is not allocated", d->ctx);
645 return -EINVAL;
648 pos=(unsigned long) d->buf;
649 while (size > 0) {
650 page = kvirt_to_pa(pos);
651 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
652 return -EAGAIN;
653 start+=PAGE_SIZE;
654 pos+=PAGE_SIZE;
655 size-=PAGE_SIZE;
657 return 0;
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;
666 switch(cmd)
668 case VIDEO1394_LISTEN_CHANNEL:
669 case VIDEO1394_TALK_CHANNEL:
671 struct video1394_mmap v;
672 int i;
674 if(copy_from_user(&v, (void *)arg, sizeof(v)))
675 return -EFAULT;
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);
679 return -EFAULT;
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);
684 return -EFAULT;
687 if (v.buf_size<=0) {
688 PRINT(KERN_ERR, ohci->id,
689 "Invalid %d length buffer requested",v.buf_size);
690 return -EFAULT;
693 if (v.nb_buffers<=0) {
694 PRINT(KERN_ERR, ohci->id,
695 "Invalid %d buffers requested",v.nb_buffers);
696 return -EFAULT;
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);
703 return -EFAULT;
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");
714 return -EFAULT;
717 video->ir_context[i] =
718 alloc_dma_iso_ctx(ohci, ISO_RECEIVE, i+1,
719 v.nb_buffers, v.buf_size,
720 v.channel, 0);
722 if (video->ir_context[i] == NULL) {
723 PRINT(KERN_ERR, ohci->id,
724 "Couldn't allocate ir context");
725 return -EFAULT;
727 initialize_dma_ir_ctx(video->ir_context[i],
728 v.sync_tag);
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,
736 v.channel);
738 else {
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");
746 return -EFAULT;
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");
757 return -EFAULT;
759 initialize_dma_it_ctx(video->it_context[i],
760 v.sync_tag);
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,
768 v.channel);
771 if(copy_to_user((void *)arg, &v, sizeof(v)))
772 return -EFAULT;
774 return 0;
776 case VIDEO1394_UNLISTEN_CHANNEL:
777 case VIDEO1394_UNTALK_CHANNEL:
779 int channel;
780 int i;
782 if(copy_from_user(&channel, (void *)arg, sizeof(int)))
783 return -EFAULT;
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);
788 return -EFAULT;
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",
799 i+1, channel);
801 else {
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",
809 i, channel);
812 return 0;
814 case VIDEO1394_LISTEN_QUEUE_BUFFER:
816 struct video1394_wait v;
817 struct dma_iso_ctx *d;
818 int i;
820 if(copy_from_user(&v, (void *)arg, sizeof(v)))
821 return -EFAULT;
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);
830 return -EFAULT;
833 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
834 PRINT(KERN_ERR, ohci->id,
835 "buffer %d is already used",v.buffer);
836 return -EFAULT;
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))
844 & 0xfffffff0) | 0x1;
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);
858 /* Run IR context */
859 reg_write(ohci, d->ctrlSet, 0x8000);
861 else {
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);
869 return 0;
872 case VIDEO1394_LISTEN_WAIT_BUFFER:
874 struct video1394_wait v;
875 struct dma_iso_ctx *d;
876 int i;
878 if(copy_from_user(&v, (void *)arg, sizeof(v)))
879 return -EFAULT;
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);
888 return -EFAULT;
891 switch(d->buffer_status[v.buffer]) {
892 case VIDEO1394_BUFFER_READY:
893 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
894 return 0;
895 case VIDEO1394_BUFFER_QUEUED:
896 #if 1
897 while(d->buffer_status[v.buffer]!=
898 VIDEO1394_BUFFER_READY) {
899 interruptible_sleep_on(&d->waitq);
900 if(signal_pending(current)) return -EINTR;
902 #else
903 if (wait_event_interruptible(d->waitq,
904 d->buffer_status[v.buffer]
905 == VIDEO1394_BUFFER_READY)
906 == -ERESTARTSYS)
907 return -EINTR;
908 #endif
909 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
910 return 0;
911 default:
912 PRINT(KERN_ERR, ohci->id,
913 "buffer %d is not queued",v.buffer);
914 return -EFAULT;
917 case VIDEO1394_TALK_QUEUE_BUFFER:
919 struct video1394_wait v;
920 struct dma_iso_ctx *d;
921 int i;
923 if(copy_from_user(&v, (void *)arg, sizeof(v)))
924 return -EFAULT;
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);
933 return -EFAULT;
936 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
937 PRINT(KERN_ERR, ohci->id,
938 "buffer %d is already used",v.buffer);
939 return -EFAULT;
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))
948 & 0xfffffff0) | 0x3;
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))
953 & 0xfffffff0) | 0x3;
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",
962 d->ctx);
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);
968 /* Run IT context */
969 reg_write(ohci, d->ctrlSet, 0x8000);
971 else {
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",
976 d->ctx);
977 reg_write(ohci, d->ctrlSet, 0x1000);
980 return 0;
983 case VIDEO1394_TALK_WAIT_BUFFER:
985 struct video1394_wait v;
986 struct dma_iso_ctx *d;
987 int i;
989 if(copy_from_user(&v, (void *)arg, sizeof(v)))
990 return -EFAULT;
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);
999 return -EFAULT;
1002 switch(d->buffer_status[v.buffer]) {
1003 case VIDEO1394_BUFFER_READY:
1004 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1005 return 0;
1006 case VIDEO1394_BUFFER_QUEUED:
1007 #if 1
1008 while(d->buffer_status[v.buffer]!=
1009 VIDEO1394_BUFFER_READY) {
1010 interruptible_sleep_on(&d->waitq);
1011 if(signal_pending(current)) return -EINTR;
1013 #else
1014 if (wait_event_interruptible(d->waitq,
1015 d->buffer_status[v.buffer]
1016 == VIDEO1394_BUFFER_READY)
1017 == -ERESTARTSYS)
1018 return -EINTR;
1019 #endif
1020 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1021 return 0;
1022 default:
1023 PRINT(KERN_ERR, ohci->id,
1024 "buffer %d is not queued",v.buffer);
1025 return -EFAULT;
1028 default:
1029 return -EINVAL;
1034 * This maps the vmalloced and reserved buffer to user space.
1036 * FIXME:
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");
1051 return -EINVAL;
1054 return do_iso_mmap(ohci, video->current_ctx,
1055 (char *)vma->vm_start,
1056 (unsigned long)(vma->vm_end-vma->vm_start));
1057 return 0;
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);
1066 return -EIO;
1069 V22_COMPAT_MOD_INC_USE_COUNT;
1071 PRINT(KERN_INFO, i, "open");
1073 return 0;
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;
1080 int i;
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");
1118 return 0;
1121 void irq_handler(int card, quadlet_t isoRecvIntEvent,
1122 quadlet_t isoXmitIntEvent)
1124 int i;
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=
1143 OWNER_THIS_MODULE
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");
1156 return -1;
1159 video->ohci = ohci;
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 *));
1168 else {
1169 PRINT(KERN_ERR, ohci->id, "Cannot allocate ir_context");
1170 return -1;
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 *));
1180 else {
1181 PRINT(KERN_ERR, ohci->id, "Cannot allocate it_context");
1182 return -1;
1185 return 0;
1188 static void remove_card(struct video_card *video)
1190 int i;
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);
1211 #ifdef MODULE
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)
1221 int i;
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;
1233 int i;
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);
1240 if (ohci) {
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");
1248 return -EIO;
1251 if (register_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME,
1252 &video1394_fops)) {
1253 printk("video1394: unable to get major %d\n",
1254 VIDEO1394_MAJOR);
1255 return -EIO;
1258 PRINT_G(KERN_INFO, "initialized with %d ohci cards",
1259 num_of_video_cards);
1261 return 0;
1264 #endif /* MODULE */