File: /usr/src/linux/drivers/ieee1394/video1394.c
1 /*
2 * video1394.c - video driver for OHCI 1394 boards
3 * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
4 * Peter Schlaile <udbz@rz.uni-karlsruhe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <linux/config.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/wait.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/fs.h>
32 #include <linux/poll.h>
33 #include <linux/smp_lock.h>
34 #include <asm/byteorder.h>
35 #include <asm/atomic.h>
36 #include <asm/io.h>
37 #include <asm/uaccess.h>
38 #include <linux/proc_fs.h>
39 #include <linux/tqueue.h>
40 #include <linux/delay.h>
41 #include <linux/devfs_fs_kernel.h>
42
43 #include <asm/pgtable.h>
44 #include <asm/page.h>
45 #include <linux/sched.h>
46 #include <asm/segment.h>
47 #include <linux/types.h>
48 #include <linux/wrapper.h>
49 #include <linux/vmalloc.h>
50
51 #include "ieee1394.h"
52 #include "ieee1394_types.h"
53 #include "hosts.h"
54 #include "ieee1394_core.h"
55 #include "highlevel.h"
56 #include "video1394.h"
57
58 #include "ohci1394.h"
59
60 #define VIDEO1394_MAJOR 172
61 #define ISO_CHANNELS 64
62 #define ISO_RECEIVE 0
63 #define ISO_TRANSMIT 1
64
65 #ifndef virt_to_page
66 #define virt_to_page(x) MAP_NR(x)
67 #endif
68
69 #ifndef vmalloc_32
70 #define vmalloc_32(x) vmalloc(x)
71 #endif
72
73 struct it_dma_prg {
74 struct dma_cmd begin;
75 quadlet_t data[4];
76 struct dma_cmd end;
77 quadlet_t pad[4]; /* FIXME: quick hack for memory alignment */
78 };
79
80 struct dma_iso_ctx {
81 struct ti_ohci *ohci;
82 int ctx;
83 int channel;
84 int last_buffer;
85 int * next_buffer; /* For ISO Transmit of video packets
86 to write the correct SYT field
87 into the next block */
88 unsigned int num_desc;
89 unsigned int buf_size;
90 unsigned int frame_size;
91 unsigned int packet_size;
92 unsigned int left_size;
93 unsigned int nb_cmd;
94 unsigned char *buf;
95 struct dma_cmd **ir_prg;
96 struct it_dma_prg **it_prg;
97 unsigned int *buffer_status;
98 unsigned int *last_used_cmd; /* For ISO Transmit with
99 variable sized packets only ! */
100 int ctrlClear;
101 int ctrlSet;
102 int cmdPtr;
103 int ctxMatch;
104 wait_queue_head_t waitq;
105 spinlock_t lock;
106 unsigned int syt_offset;
107 int flags;
108 };
109
110 struct video_card {
111 struct ti_ohci *ohci;
112 struct list_head list;
113 int id;
114 devfs_handle_t devfs;
115
116 struct dma_iso_ctx **ir_context;
117 struct dma_iso_ctx **it_context;
118 struct dma_iso_ctx *current_ctx;
119 };
120
121 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
122 #define VIDEO1394_DEBUG
123 #endif
124
125 #ifdef DBGMSG
126 #undef DBGMSG
127 #endif
128
129 #ifdef VIDEO1394_DEBUG
130 #define DBGMSG(card, fmt, args...) \
131 printk(KERN_INFO "video1394_%d: " fmt "\n" , card , ## args)
132 #else
133 #define DBGMSG(card, fmt, args...)
134 #endif
135
136 /* print general (card independent) information */
137 #define PRINT_G(level, fmt, args...) \
138 printk(level "video1394: " fmt "\n" , ## args)
139
140 /* print card specific information */
141 #define PRINT(level, card, fmt, args...) \
142 printk(level "video1394_%d: " fmt "\n" , card , ## args)
143
144 static void irq_handler(int card, quadlet_t isoRecvIntEvent,
145 quadlet_t isoXmitIntEvent);
146
147 static LIST_HEAD(video1394_cards);
148 static spinlock_t video1394_cards_lock = SPIN_LOCK_UNLOCKED;
149
150 static devfs_handle_t devfs_handle;
151 static struct hpsb_highlevel *hl_handle = NULL;
152
153 static struct video_template video_tmpl = { irq_handler };
154
155 /* Code taken from bttv.c */
156
157 /*******************************/
158 /* Memory management functions */
159 /*******************************/
160
161 #define MDEBUG(x) do { } while(0) /* Debug memory management */
162
163 /* [DaveM] I've recoded most of this so that:
164 * 1) It's easier to tell what is happening
165 * 2) It's more portable, especially for translating things
166 * out of vmalloc mapped areas in the kernel.
167 * 3) Less unnecessary translations happen.
168 *
169 * The code used to assume that the kernel vmalloc mappings
170 * existed in the page tables of every process, this is simply
171 * not guaranteed. We now use pgd_offset_k which is the
172 * defined way to get at the kernel page tables.
173 */
174
175 /* Given PGD from the address space's page table, return the kernel
176 * virtual mapping of the physical memory mapped at ADR.
177 */
178 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
179 {
180 unsigned long ret = 0UL;
181 pmd_t *pmd;
182 pte_t *ptep, pte;
183
184 if (!pgd_none(*pgd)) {
185 pmd = pmd_offset(pgd, adr);
186 if (!pmd_none(*pmd)) {
187 ptep = pte_offset(pmd, adr);
188 pte = *ptep;
189 if(pte_present(pte)) {
190 ret = (unsigned long)
191 page_address(pte_page(pte));
192 ret |= (adr & (PAGE_SIZE - 1));
193 }
194 }
195 }
196 MDEBUG(printk("uv2kva(%lx-->%lx)", adr, ret));
197 return ret;
198 }
199
200 static inline unsigned long uvirt_to_bus(unsigned long adr)
201 {
202 unsigned long kva, ret;
203
204 kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
205 ret = virt_to_bus((void *)kva);
206 MDEBUG(printk("uv2b(%lx-->%lx)", adr, ret));
207 return ret;
208 }
209
210 static inline unsigned long kvirt_to_bus(unsigned long adr)
211 {
212 unsigned long va, kva, ret;
213
214 va = VMALLOC_VMADDR(adr);
215 kva = uvirt_to_kva(pgd_offset_k(va), va);
216 ret = virt_to_bus((void *)kva);
217 MDEBUG(printk("kv2b(%lx-->%lx)", adr, ret));
218 return ret;
219 }
220
221 /* Here we want the physical address of the memory.
222 * This is used when initializing the contents of the
223 * area and marking the pages as reserved.
224 */
225 static inline unsigned long kvirt_to_pa(unsigned long adr)
226 {
227 unsigned long va, kva, ret;
228
229 va = VMALLOC_VMADDR(adr);
230 kva = uvirt_to_kva(pgd_offset_k(va), va);
231 ret = __pa(kva);
232 MDEBUG(printk("kv2pa(%lx-->%lx)", adr, ret));
233 return ret;
234 }
235
236 static void * rvmalloc(unsigned long size)
237 {
238 void * mem;
239 unsigned long adr, page;
240
241 mem=vmalloc_32(size);
242 if (mem)
243 {
244 memset(mem, 0, size); /* Clear the ram out,
245 no junk to the user */
246 adr=(unsigned long) mem;
247 while (size > 0)
248 {
249 page = kvirt_to_pa(adr);
250 mem_map_reserve(virt_to_page(__va(page)));
251 adr+=PAGE_SIZE;
252 size-=PAGE_SIZE;
253 }
254 }
255 return mem;
256 }
257
258 static void rvfree(void * mem, unsigned long size)
259 {
260 unsigned long adr, page;
261
262 if (mem)
263 {
264 adr=(unsigned long) mem;
265 while (size > 0)
266 {
267 page = kvirt_to_pa(adr);
268 mem_map_unreserve(virt_to_page(__va(page)));
269 adr+=PAGE_SIZE;
270 size-=PAGE_SIZE;
271 }
272 vfree(mem);
273 }
274 }
275 /* End of code taken from bttv.c */
276
277 static int free_dma_iso_ctx(struct dma_iso_ctx **d)
278 {
279 int i;
280 struct ti_ohci *ohci;
281
282 if ((*d)==NULL) return -1;
283
284 ohci = (struct ti_ohci *)(*d)->ohci;
285
286 DBGMSG(ohci->id, "Freeing dma_iso_ctx %d", (*d)->ctx);
287
288 ohci1394_stop_context(ohci, (*d)->ctrlClear, NULL);
289
290 if ((*d)->buf) rvfree((void *)(*d)->buf,
291 (*d)->num_desc * (*d)->buf_size);
292
293 if ((*d)->ir_prg) {
294 for (i=0;i<(*d)->num_desc;i++)
295 if ((*d)->ir_prg[i]) kfree((*d)->ir_prg[i]);
296 kfree((*d)->ir_prg);
297 }
298
299 if ((*d)->it_prg) {
300 for (i=0;i<(*d)->num_desc;i++)
301 if ((*d)->it_prg[i]) kfree((*d)->it_prg[i]);
302 kfree((*d)->it_prg);
303 }
304
305 if ((*d)->buffer_status)
306 kfree((*d)->buffer_status);
307 if ((*d)->last_used_cmd)
308 kfree((*d)->last_used_cmd);
309 if ((*d)->next_buffer)
310 kfree((*d)->next_buffer);
311
312 kfree(*d);
313 *d = NULL;
314
315 return 0;
316 }
317
318 static struct dma_iso_ctx *
319 alloc_dma_iso_ctx(struct ti_ohci *ohci, int type, int ctx, int num_desc,
320 int buf_size, int channel, unsigned int packet_size)
321 {
322 struct dma_iso_ctx *d=NULL;
323 int i;
324
325 d = (struct dma_iso_ctx *)kmalloc(sizeof(struct dma_iso_ctx),
326 GFP_KERNEL);
327 if (d==NULL) {
328 PRINT(KERN_ERR, ohci->id, "Failed to allocate dma_iso_ctx");
329 return NULL;
330 }
331
332 memset(d, 0, sizeof(struct dma_iso_ctx));
333
334 d->ohci = (void *)ohci;
335 d->ctx = ctx;
336 d->channel = channel;
337 d->num_desc = num_desc;
338 d->frame_size = buf_size;
339 if (buf_size%PAGE_SIZE)
340 d->buf_size = buf_size + PAGE_SIZE - (buf_size%PAGE_SIZE);
341 else
342 d->buf_size = buf_size;
343 d->last_buffer = -1;
344 d->buf = NULL;
345 d->ir_prg = NULL;
346 init_waitqueue_head(&d->waitq);
347
348 d->buf = rvmalloc(d->num_desc * d->buf_size);
349
350 if (d->buf == NULL) {
351 PRINT(KERN_ERR, ohci->id, "Failed to allocate dma buffer");
352 free_dma_iso_ctx(&d);
353 return NULL;
354 }
355 memset(d->buf, 0, d->num_desc * d->buf_size);
356
357 if (type == ISO_RECEIVE) {
358 d->ctrlSet = OHCI1394_IsoRcvContextControlSet+32*d->ctx;
359 d->ctrlClear = OHCI1394_IsoRcvContextControlClear+32*d->ctx;
360 d->cmdPtr = OHCI1394_IsoRcvCommandPtr+32*d->ctx;
361 d->ctxMatch = OHCI1394_IsoRcvContextMatch+32*d->ctx;
362
363 d->ir_prg = kmalloc(d->num_desc * sizeof(struct dma_cmd *),
364 GFP_KERNEL);
365
366 if (d->ir_prg == NULL) {
367 PRINT(KERN_ERR, ohci->id,
368 "Failed to allocate dma ir prg");
369 free_dma_iso_ctx(&d);
370 return NULL;
371 }
372 memset(d->ir_prg, 0, d->num_desc * sizeof(struct dma_cmd *));
373
374 d->nb_cmd = d->buf_size / PAGE_SIZE + 1;
375 d->left_size = (d->frame_size % PAGE_SIZE) ?
376 d->frame_size % PAGE_SIZE : PAGE_SIZE;
377
378 for (i=0;i<d->num_desc;i++) {
379 d->ir_prg[i] = kmalloc(d->nb_cmd *
380 sizeof(struct dma_cmd),
381 GFP_KERNEL);
382 if (d->ir_prg[i] == NULL) {
383 PRINT(KERN_ERR, ohci->id,
384 "Failed to allocate dma ir prg");
385 free_dma_iso_ctx(&d);
386 return NULL;
387 }
388 }
389 }
390 else { /* ISO_TRANSMIT */
391 d->ctrlSet = OHCI1394_IsoXmitContextControlSet+16*d->ctx;
392 d->ctrlClear = OHCI1394_IsoXmitContextControlClear+16*d->ctx;
393 d->cmdPtr = OHCI1394_IsoXmitCommandPtr+16*d->ctx;
394
395 d->it_prg = kmalloc(d->num_desc * sizeof(struct it_dma_prg *),
396 GFP_KERNEL);
397
398 if (d->it_prg == NULL) {
399 PRINT(KERN_ERR, ohci->id,
400 "Failed to allocate dma it prg");
401 free_dma_iso_ctx(&d);
402 return NULL;
403 }
404 memset(d->it_prg, 0, d->num_desc*sizeof(struct it_dma_prg *));
405
406 d->packet_size = packet_size;
407
408 if (PAGE_SIZE % packet_size || packet_size>4096) {
409 PRINT(KERN_ERR, ohci->id,
410 "Packet size %d (page_size: %ld) "
411 "not yet supported\n",
412 packet_size, PAGE_SIZE);
413 free_dma_iso_ctx(&d);
414 return NULL;
415 }
416
417 d->nb_cmd = d->frame_size / d->packet_size;
418 if (d->frame_size % d->packet_size) {
419 d->nb_cmd++;
420 d->left_size = d->frame_size % d->packet_size;
421 }
422 else
423 d->left_size = d->packet_size;
424
425 for (i=0;i<d->num_desc;i++) {
426 d->it_prg[i] = kmalloc(d->nb_cmd *
427 sizeof(struct it_dma_prg),
428 GFP_KERNEL);
429 if (d->it_prg[i] == NULL) {
430 PRINT(KERN_ERR, ohci->id,
431 "Failed to allocate dma it prg");
432 free_dma_iso_ctx(&d);
433 return NULL;
434 }
435 }
436 }
437
438 d->buffer_status = kmalloc(d->num_desc * sizeof(unsigned int),
439 GFP_KERNEL);
440 d->last_used_cmd = kmalloc(d->num_desc * sizeof(unsigned int),
441 GFP_KERNEL);
442 d->next_buffer = kmalloc(d->num_desc * sizeof(int),
443 GFP_KERNEL);
444
445 if (d->buffer_status == NULL) {
446 PRINT(KERN_ERR, ohci->id, "Failed to allocate buffer_status");
447 free_dma_iso_ctx(&d);
448 return NULL;
449 }
450 if (d->last_used_cmd == NULL) {
451 PRINT(KERN_ERR, ohci->id, "Failed to allocate last_used_cmd");
452 free_dma_iso_ctx(&d);
453 return NULL;
454 }
455 if (d->next_buffer == NULL) {
456 PRINT(KERN_ERR, ohci->id, "Failed to allocate next_buffer");
457 free_dma_iso_ctx(&d);
458 return NULL;
459 }
460 memset(d->buffer_status, 0, d->num_desc * sizeof(unsigned int));
461 memset(d->last_used_cmd, 0, d->num_desc * sizeof(unsigned int));
462 memset(d->next_buffer, -1, d->num_desc * sizeof(int));
463
464 spin_lock_init(&d->lock);
465
466 PRINT(KERN_INFO, ohci->id, "Iso %s DMA: %d buffers "
467 "of size %d allocated for a frame size %d, each with %d prgs",
468 (type==ISO_RECEIVE) ? "receive" : "transmit",
469 d->num_desc, d->buf_size, d->frame_size, d->nb_cmd);
470
471 return d;
472 }
473
474 static void reset_ir_status(struct dma_iso_ctx *d, int n)
475 {
476 int i;
477 d->ir_prg[n][0].status = 4;
478 d->ir_prg[n][1].status = PAGE_SIZE-4;
479 for (i=2;i<d->nb_cmd-1;i++)
480 d->ir_prg[n][i].status = PAGE_SIZE;
481 d->ir_prg[n][i].status = d->left_size;
482 }
483
484 static void initialize_dma_ir_prg(struct dma_iso_ctx *d, int n, int flags)
485 {
486 struct dma_cmd *ir_prg = d->ir_prg[n];
487 unsigned long buf = (unsigned long)d->buf+n*d->buf_size;
488 int i;
489
490 /* the first descriptor will read only 4 bytes */
491 ir_prg[0].control = (0x280C << 16) | 4;
492
493 /* set the sync flag */
494 if (flags & VIDEO1394_SYNC_FRAMES)
495 ir_prg[0].control |= 0x00030000;
496
497 ir_prg[0].address = kvirt_to_bus(buf);
498 ir_prg[0].branchAddress = (virt_to_bus(&(ir_prg[1].control))
499 & 0xfffffff0) | 0x1;
500
501 /* the second descriptor will read PAGE_SIZE-4 bytes */
502 ir_prg[1].control = (0x280C << 16) | (PAGE_SIZE-4);
503 ir_prg[1].address = kvirt_to_bus(buf+4);
504 ir_prg[1].branchAddress = (virt_to_bus(&(ir_prg[2].control))
505 & 0xfffffff0) | 0x1;
506
507 for (i=2;i<d->nb_cmd-1;i++) {
508 ir_prg[i].control = (0x280C << 16) | PAGE_SIZE;
509 ir_prg[i].address = kvirt_to_bus(buf+(i-1)*PAGE_SIZE);
510
511 ir_prg[i].branchAddress =
512 (virt_to_bus(&(ir_prg[i+1].control))
513 & 0xfffffff0) | 0x1;
514 }
515
516 /* the last descriptor will generate an interrupt */
517 ir_prg[i].control = (0x283C << 16) | d->left_size;
518 ir_prg[i].address = kvirt_to_bus(buf+(i-1)*PAGE_SIZE);
519 }
520
521 static void initialize_dma_ir_ctx(struct dma_iso_ctx *d, int tag, int flags)
522 {
523 struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
524 int i;
525
526 d->flags = flags;
527
528 ohci1394_stop_context(ohci, d->ctrlClear, NULL);
529
530 for (i=0;i<d->num_desc;i++) {
531 initialize_dma_ir_prg(d, i, flags);
532 reset_ir_status(d, i);
533 }
534
535 /* reset the ctrl register */
536 reg_write(ohci, d->ctrlClear, 0xf0000000);
537
538 /* Set bufferFill */
539 reg_write(ohci, d->ctrlSet, 0x80000000);
540
541 /* Set isoch header */
542 if (flags & VIDEO1394_INCLUDE_ISO_HEADERS)
543 reg_write(ohci, d->ctrlSet, 0x40000000);
544
545 /* Set the context match register to match on all tags,
546 sync for sync tag, and listen to d->channel */
547 reg_write(ohci, d->ctxMatch, 0xf0000000|((tag&0xf)<<8)|d->channel);
548
549 /* Set up isoRecvIntMask to generate interrupts */
550 reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1<<d->ctx);
551 }
552
553 /* find which context is listening to this channel */
554 int ir_ctx_listening(struct video_card *video, int channel)
555 {
556 int i;
557 struct ti_ohci *ohci = video->ohci;
558
559 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
560 if (video->ir_context[i]) {
561 if (video->ir_context[i]->channel==channel)
562 return i;
563 }
564
565 PRINT(KERN_ERR, ohci->id, "No iso context is listening to channel %d",
566 channel);
567
568 return -1;
569 }
570
571 int it_ctx_talking(struct video_card *video, int channel)
572 {
573 int i;
574 struct ti_ohci *ohci = video->ohci;
575
576 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
577 if (video->it_context[i]) {
578 if (video->it_context[i]->channel==channel)
579 return i;
580 }
581
582 PRINT(KERN_ERR, ohci->id, "No iso context is talking to channel %d",
583 channel);
584
585 return -1;
586 }
587
588 int wakeup_dma_ir_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
589 {
590 int i;
591
592 if (d==NULL) {
593 PRINT(KERN_ERR, ohci->id, "Iso receive event received but "
594 "context not allocated");
595 return -EFAULT;
596 }
597
598 spin_lock(&d->lock);
599 for (i=0;i<d->num_desc;i++) {
600 if (d->ir_prg[i][d->nb_cmd-1].status & 0xFFFF0000) {
601 reset_ir_status(d, i);
602 d->buffer_status[i] = VIDEO1394_BUFFER_READY;
603 }
604 }
605 spin_unlock(&d->lock);
606 if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq);
607 return 0;
608 }
609
610 static inline void put_timestamp(struct ti_ohci *ohci, struct dma_iso_ctx * d,
611 int n)
612 {
613 unsigned char* buf = d->buf + n * d->buf_size;
614 u32 cycleTimer;
615 u32 timeStamp;
616
617 if (n == -1) {
618 return;
619 }
620
621 cycleTimer = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
622
623 timeStamp = ((cycleTimer & 0x0fff) + d->syt_offset); /* 11059 = 450 us */
624 timeStamp = (timeStamp % 3072 + ((timeStamp / 3072) << 12)
625 + (cycleTimer & 0xf000)) & 0xffff;
626
627 buf[6] = timeStamp >> 8;
628 buf[7] = timeStamp & 0xff;
629
630 /* if first packet is empty packet, then put timestamp into the next full one too */
631 if ( (d->it_prg[n][0].data[1] >>16) == 0x008) {
632 buf += d->packet_size;
633 buf[6] = timeStamp >> 8;
634 buf[7] = timeStamp & 0xff;
635 }
636
637 /* do the next buffer frame too in case of irq latency */
638 n = d->next_buffer[n];
639 if (n == -1) {
640 return;
641 }
642 buf = d->buf + n * d->buf_size;
643
644 timeStamp += (d->last_used_cmd[n] << 12) & 0xffff;
645
646 buf[6] = timeStamp >> 8;
647 buf[7] = timeStamp & 0xff;
648
649 /* if first packet is empty packet, then put timestamp into the next full one too */
650 if ( (d->it_prg[n][0].data[1] >>16) == 0x008) {
651 buf += d->packet_size;
652 buf[6] = timeStamp >> 8;
653 buf[7] = timeStamp & 0xff;
654 }
655
656 #if 0
657 printk("curr: %d, next: %d, cycleTimer: %08x timeStamp: %08x\n",
658 curr, n, cycleTimer, timeStamp);
659 #endif
660 }
661
662 int wakeup_dma_it_ctx(struct ti_ohci *ohci, struct dma_iso_ctx *d)
663 {
664 int i;
665
666 if (d==NULL) {
667 PRINT(KERN_ERR, ohci->id, "Iso transmit event received but "
668 "context not allocated");
669 return -EFAULT;
670 }
671
672 spin_lock(&d->lock);
673 for (i=0;i<d->num_desc;i++) {
674 if (d->it_prg[i][d->last_used_cmd[i]].end.status& 0xFFFF0000) {
675 int next = d->next_buffer[i];
676 put_timestamp(ohci, d, next);
677 d->it_prg[i][d->last_used_cmd[i]].end.status = 0;
678 d->buffer_status[i] = VIDEO1394_BUFFER_READY;
679 }
680 }
681 spin_unlock(&d->lock);
682 if (waitqueue_active(&d->waitq)) wake_up_interruptible(&d->waitq);
683 return 0;
684 }
685
686 static void initialize_dma_it_prg(struct dma_iso_ctx *d, int n, int sync_tag)
687 {
688 struct it_dma_prg *it_prg = d->it_prg[n];
689 unsigned long buf = (unsigned long)d->buf+n*d->buf_size;
690 int i;
691 d->last_used_cmd[n] = d->nb_cmd - 1;
692 for (i=0;i<d->nb_cmd;i++) {
693
694 it_prg[i].begin.control = OUTPUT_MORE_IMMEDIATE | 8 ;
695 it_prg[i].begin.address = 0;
696
697 it_prg[i].begin.status = 0;
698
699 it_prg[i].data[0] =
700 (DMA_SPEED_100 << 16)
701 | (/* tag */ 1 << 14)
702 | (d->channel << 8)
703 | (TCODE_ISO_DATA << 4);
704 if (i==0) it_prg[i].data[0] |= sync_tag;
705 it_prg[i].data[1] = d->packet_size << 16;
706 it_prg[i].data[2] = 0;
707 it_prg[i].data[3] = 0;
708
709 it_prg[i].end.control = 0x100c0000;
710 it_prg[i].end.address =
711 kvirt_to_bus(buf+i*d->packet_size);
712
713 if (i<d->nb_cmd-1) {
714 it_prg[i].end.control |= d->packet_size;
715 it_prg[i].begin.branchAddress =
716 (virt_to_bus(&(it_prg[i+1].begin.control))
717 & 0xfffffff0) | 0x3;
718 it_prg[i].end.branchAddress =
719 (virt_to_bus(&(it_prg[i+1].begin.control))
720 & 0xfffffff0) | 0x3;
721 }
722 else {
723 /* the last prg generates an interrupt */
724 it_prg[i].end.control |= 0x08300000 | d->left_size;
725 /* the last prg doesn't branch */
726 it_prg[i].begin.branchAddress = 0;
727 it_prg[i].end.branchAddress = 0;
728 }
729 it_prg[i].end.status = 0;
730
731 #if 0
732 printk("%d:%d: %08x-%08x ctrl %08x brch %08x d0 %08x d1 %08x\n",n,i,
733 virt_to_bus(&(it_prg[i].begin.control)),
734 virt_to_bus(&(it_prg[i].end.control)),
735 it_prg[i].end.control,
736 it_prg[i].end.branchAddress,
737 it_prg[i].data[0], it_prg[i].data[1]);
738 #endif
739 }
740 }
741
742 static void initialize_dma_it_prg_var_packet_queue(
743 struct dma_iso_ctx *d, int n, unsigned int * packet_sizes,
744 struct ti_ohci *ohci)
745 {
746 struct it_dma_prg *it_prg = d->it_prg[n];
747 int i;
748
749 #if 0
750 if (n != -1) {
751 put_timestamp(ohci, d, n);
752 }
753 #endif
754 d->last_used_cmd[n] = d->nb_cmd - 1;
755
756 for (i = 0; i < d->nb_cmd; i++) {
757 unsigned int size;
758 if (packet_sizes[i] > d->packet_size) {
759 size = d->packet_size;
760 } else {
761 size = packet_sizes[i];
762 }
763 it_prg[i].data[1] = size << 16;
764 it_prg[i].end.control = 0x100c0000;
765
766 if (i < d->nb_cmd-1 && packet_sizes[i+1] != 0) {
767 it_prg[i].end.control |= size;
768 it_prg[i].begin.branchAddress =
769 (virt_to_bus(&(it_prg[i+1].begin.control))
770 & 0xfffffff0) | 0x3;
771 it_prg[i].end.branchAddress =
772 (virt_to_bus(&(it_prg[i+1].begin.control))
773 & 0xfffffff0) | 0x3;
774 } else {
775 /* the last prg generates an interrupt */
776 it_prg[i].end.control |= 0x08300000 | size;
777 /* the last prg doesn't branch */
778 it_prg[i].begin.branchAddress = 0;
779 it_prg[i].end.branchAddress = 0;
780 d->last_used_cmd[n] = i;
781 break;
782 }
783 }
784 }
785
786 static void initialize_dma_it_ctx(struct dma_iso_ctx *d, int sync_tag,
787 unsigned int syt_offset, int flags)
788 {
789 struct ti_ohci *ohci = (struct ti_ohci *)d->ohci;
790 int i;
791
792 d->flags = flags;
793 d->syt_offset = (syt_offset == 0 ? 11000 : syt_offset);
794
795 ohci1394_stop_context(ohci, d->ctrlClear, NULL);
796
797 for (i=0;i<d->num_desc;i++)
798 initialize_dma_it_prg(d, i, sync_tag);
799
800 /* Set up isoRecvIntMask to generate interrupts */
801 reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1<<d->ctx);
802 }
803
804 static int do_iso_mmap(struct ti_ohci *ohci, struct dma_iso_ctx *d,
805 const char *adr, unsigned long size)
806 {
807 unsigned long start=(unsigned long) adr;
808 unsigned long page,pos;
809
810 if (size>d->num_desc * d->buf_size) {
811 PRINT(KERN_ERR, ohci->id,
812 "iso context %d buf size is different from mmap size",
813 d->ctx);
814 return -EINVAL;
815 }
816 if (!d->buf) {
817 PRINT(KERN_ERR, ohci->id,
818 "iso context %d is not allocated", d->ctx);
819 return -EINVAL;
820 }
821
822 pos=(unsigned long) d->buf;
823 while (size > 0) {
824 page = kvirt_to_pa(pos);
825 if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED))
826 return -EAGAIN;
827 start+=PAGE_SIZE;
828 pos+=PAGE_SIZE;
829 size-=PAGE_SIZE;
830 }
831 return 0;
832 }
833
834 static int video1394_ioctl(struct inode *inode, struct file *file,
835 unsigned int cmd, unsigned long arg)
836 {
837 struct video_card *video = NULL;
838 struct ti_ohci *ohci = NULL;
839 unsigned long flags;
840 struct list_head *lh;
841
842 spin_lock_irqsave(&video1394_cards_lock, flags);
843 if (!list_empty(&video1394_cards)) {
844 struct video_card *p;
845 list_for_each(lh, &video1394_cards) {
846 p = list_entry(lh, struct video_card, list);
847 if (p->id == MINOR(inode->i_rdev)) {
848 video = p;
849 ohci = video->ohci;
850 break;
851 }
852 }
853 }
854 spin_unlock_irqrestore(&video1394_cards_lock, flags);
855
856 if (video == NULL) {
857 PRINT_G(KERN_ERR, __FUNCTION__": Unknown video card for minor %d", MINOR(inode->i_rdev));
858 return -EFAULT;
859 }
860
861 switch(cmd)
862 {
863 case VIDEO1394_LISTEN_CHANNEL:
864 case VIDEO1394_TALK_CHANNEL:
865 {
866 struct video1394_mmap v;
867 u64 mask;
868 int i;
869
870 if(copy_from_user(&v, (void *)arg, sizeof(v)))
871 return -EFAULT;
872 if (v.channel<0 || v.channel>(ISO_CHANNELS-1)) {
873 PRINT(KERN_ERR, ohci->id,
874 "Iso channel %d out of bound", v.channel);
875 return -EFAULT;
876 }
877 mask = (u64)0x1<<v.channel;
878 printk("mask: %08X%08X usage: %08X%08X\n",
879 (u32)(mask>>32),(u32)(mask&0xffffffff),
880 (u32)(ohci->ISO_channel_usage>>32),
881 (u32)(ohci->ISO_channel_usage&0xffffffff));
882 if (ohci->ISO_channel_usage & mask) {
883 PRINT(KERN_ERR, ohci->id,
884 "Channel %d is already taken", v.channel);
885 return -EFAULT;
886 }
887 ohci->ISO_channel_usage |= mask;
888
889 if (v.buf_size<=0) {
890 PRINT(KERN_ERR, ohci->id,
891 "Invalid %d length buffer requested",v.buf_size);
892 return -EFAULT;
893 }
894
895 if (v.nb_buffers<=0) {
896 PRINT(KERN_ERR, ohci->id,
897 "Invalid %d buffers requested",v.nb_buffers);
898 return -EFAULT;
899 }
900
901 if (v.nb_buffers * v.buf_size > VIDEO1394_MAX_SIZE) {
902 PRINT(KERN_ERR, ohci->id,
903 "%d buffers of size %d bytes is too big",
904 v.nb_buffers, v.buf_size);
905 return -EFAULT;
906 }
907
908 if (cmd == VIDEO1394_LISTEN_CHANNEL) {
909 /* find a free iso receive context */
910 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
911 if (video->ir_context[i]==NULL) break;
912
913 if (i==(ohci->nb_iso_rcv_ctx-1)) {
914 PRINT(KERN_ERR, ohci->id,
915 "No iso context available");
916 return -EFAULT;
917 }
918
919 video->ir_context[i] =
920 alloc_dma_iso_ctx(ohci, ISO_RECEIVE, i+1,
921 v.nb_buffers, v.buf_size,
922 v.channel, 0);
923
924 if (video->ir_context[i] == NULL) {
925 PRINT(KERN_ERR, ohci->id,
926 "Couldn't allocate ir context");
927 return -EFAULT;
928 }
929 initialize_dma_ir_ctx(video->ir_context[i],
930 v.sync_tag, v.flags);
931
932 video->current_ctx = video->ir_context[i];
933
934 v.buf_size = video->ir_context[i]->buf_size;
935
936 PRINT(KERN_INFO, ohci->id,
937 "iso context %d listen on channel %d", i+1,
938 v.channel);
939 }
940 else {
941 /* find a free iso transmit context */
942 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
943 if (video->it_context[i]==NULL) break;
944
945 if (i==ohci->nb_iso_xmit_ctx) {
946 PRINT(KERN_ERR, ohci->id,
947 "No iso context available");
948 return -EFAULT;
949 }
950
951 video->it_context[i] =
952 alloc_dma_iso_ctx(ohci, ISO_TRANSMIT, i,
953 v.nb_buffers, v.buf_size,
954 v.channel, v.packet_size);
955
956 if (video->it_context[i] == NULL) {
957 PRINT(KERN_ERR, ohci->id,
958 "Couldn't allocate it context");
959 return -EFAULT;
960 }
961 initialize_dma_it_ctx(video->it_context[i],
962 v.sync_tag, v.syt_offset, v.flags);
963
964 video->current_ctx = video->it_context[i];
965
966 v.buf_size = video->it_context[i]->buf_size;
967
968 PRINT(KERN_INFO, ohci->id,
969 "Iso context %d talk on channel %d", i,
970 v.channel);
971 }
972
973 if(copy_to_user((void *)arg, &v, sizeof(v)))
974 return -EFAULT;
975
976 return 0;
977 }
978 case VIDEO1394_UNLISTEN_CHANNEL:
979 case VIDEO1394_UNTALK_CHANNEL:
980 {
981 int channel;
982 u64 mask;
983 int i;
984
985 if(copy_from_user(&channel, (void *)arg, sizeof(int)))
986 return -EFAULT;
987
988 if (channel<0 || channel>(ISO_CHANNELS-1)) {
989 PRINT(KERN_ERR, ohci->id,
990 "Iso channel %d out of bound", channel);
991 return -EFAULT;
992 }
993 mask = (u64)0x1<<channel;
994 if (!(ohci->ISO_channel_usage & mask)) {
995 PRINT(KERN_ERR, ohci->id,
996 "Channel %d is not being used", channel);
997 return -EFAULT;
998 }
999 ohci->ISO_channel_usage &= ~mask;
1000
1001 if (cmd == VIDEO1394_UNLISTEN_CHANNEL) {
1002 i = ir_ctx_listening(video, channel);
1003 if (i<0) return -EFAULT;
1004
1005 free_dma_iso_ctx(&video->ir_context[i]);
1006
1007 PRINT(KERN_INFO, ohci->id,
1008 "Iso context %d stop listening on channel %d",
1009 i+1, channel);
1010 }
1011 else {
1012 i = it_ctx_talking(video, channel);
1013 if (i<0) return -EFAULT;
1014
1015 free_dma_iso_ctx(&video->it_context[i]);
1016
1017 PRINT(KERN_INFO, ohci->id,
1018 "Iso context %d stop talking on channel %d",
1019 i, channel);
1020 }
1021
1022 return 0;
1023 }
1024 case VIDEO1394_LISTEN_QUEUE_BUFFER:
1025 {
1026 struct video1394_wait v;
1027 struct dma_iso_ctx *d;
1028 int i;
1029
1030 if(copy_from_user(&v, (void *)arg, sizeof(v)))
1031 return -EFAULT;
1032
1033 i = ir_ctx_listening(video, v.channel);
1034 if (i<0) return -EFAULT;
1035 d = video->ir_context[i];
1036
1037 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1038 PRINT(KERN_ERR, ohci->id,
1039 "Buffer %d out of range",v.buffer);
1040 return -EFAULT;
1041 }
1042
1043 spin_lock_irqsave(&d->lock,flags);
1044
1045 if (d->buffer_status[v.buffer]==VIDEO1394_BUFFER_QUEUED) {
1046 PRINT(KERN_ERR, ohci->id,
1047 "Buffer %d is already used",v.buffer);
1048 spin_unlock_irqrestore(&d->lock,flags);
1049 return -EFAULT;
1050 }
1051
1052 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
1053
1054 if (d->last_buffer>=0)
1055 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress =
1056 (virt_to_bus(&(d->ir_prg[v.buffer][0].control))
1057 & 0xfffffff0) | 0x1;
1058
1059 d->last_buffer = v.buffer;
1060
1061 d->ir_prg[d->last_buffer][d->nb_cmd-1].branchAddress = 0;
1062
1063 spin_unlock_irqrestore(&d->lock,flags);
1064
1065 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
1066 {
1067 DBGMSG(ohci->id, "Starting iso DMA ctx=%d",d->ctx);
1068
1069 /* Tell the controller where the first program is */
1070 reg_write(ohci, d->cmdPtr,
1071 virt_to_bus(&(d->ir_prg[v.buffer][0]))|0x1);
1072
1073 /* Run IR context */
1074 reg_write(ohci, d->ctrlSet, 0x8000);
1075 }
1076 else {
1077 /* Wake up dma context if necessary */
1078 if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
1079 PRINT(KERN_INFO, ohci->id,
1080 "Waking up iso dma ctx=%d", d->ctx);
1081 reg_write(ohci, d->ctrlSet, 0x1000);
1082 }
1083 }
1084 return 0;
1085
1086 }
1087 case VIDEO1394_LISTEN_WAIT_BUFFER:
1088 {
1089 struct video1394_wait v;
1090 struct dma_iso_ctx *d;
1091 int i;
1092
1093 if(copy_from_user(&v, (void *)arg, sizeof(v)))
1094 return -EFAULT;
1095
1096 i = ir_ctx_listening(video, v.channel);
1097 if (i<0) return -EFAULT;
1098 d = video->ir_context[i];
1099
1100 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1101 PRINT(KERN_ERR, ohci->id,
1102 "Buffer %d out of range",v.buffer);
1103 return -EFAULT;
1104 }
1105
1106 /*
1107 * I change the way it works so that it returns
1108 * the last received frame.
1109 */
1110 spin_lock_irqsave(&d->lock, flags);
1111 switch(d->buffer_status[v.buffer]) {
1112 case VIDEO1394_BUFFER_READY:
1113 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1114 break;
1115 case VIDEO1394_BUFFER_QUEUED:
1116 #if 1
1117 while(d->buffer_status[v.buffer]!=
1118 VIDEO1394_BUFFER_READY) {
1119 spin_unlock_irqrestore(&d->lock, flags);
1120 interruptible_sleep_on(&d->waitq);
1121 spin_lock_irqsave(&d->lock, flags);
1122 if(signal_pending(current)) {
1123 spin_unlock_irqrestore(&d->lock,flags);
1124 return -EINTR;
1125 }
1126 }
1127 #else
1128 if (wait_event_interruptible(d->waitq,
1129 d->buffer_status[v.buffer]
1130 == VIDEO1394_BUFFER_READY)
1131 == -ERESTARTSYS)
1132 return -EINTR;
1133 #endif
1134 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1135 break;
1136 default:
1137 PRINT(KERN_ERR, ohci->id,
1138 "Buffer %d is not queued",v.buffer);
1139 spin_unlock_irqrestore(&d->lock, flags);
1140 return -EFAULT;
1141 }
1142
1143 /*
1144 * Look ahead to see how many more buffers have been received
1145 */
1146 i=0;
1147 while (d->buffer_status[(v.buffer+1)%d->num_desc]==
1148 VIDEO1394_BUFFER_READY) {
1149 v.buffer=(v.buffer+1)%d->num_desc;
1150 i++;
1151 }
1152 spin_unlock_irqrestore(&d->lock, flags);
1153
1154 v.buffer=i;
1155 if(copy_to_user((void *)arg, &v, sizeof(v)))
1156 return -EFAULT;
1157
1158 return 0;
1159 }
1160 case VIDEO1394_TALK_QUEUE_BUFFER:
1161 {
1162 struct video1394_wait v;
1163 struct video1394_queue_variable qv;
1164 struct dma_iso_ctx *d;
1165 int i;
1166
1167 if(copy_from_user(&v, (void *)arg, sizeof(v)))
1168 return -EFAULT;
1169
1170 i = it_ctx_talking(video, v.channel);
1171 if (i<0) return -EFAULT;
1172 d = video->it_context[i];
1173
1174 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1175 PRINT(KERN_ERR, ohci->id,
1176 "Buffer %d out of range",v.buffer);
1177 return -EFAULT;
1178 }
1179
1180 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1181 if (copy_from_user(&qv, (void *)arg, sizeof(qv)))
1182 return -EFAULT;
1183 if (!access_ok(VERIFY_READ, qv.packet_sizes,
1184 d->nb_cmd * sizeof(unsigned int))) {
1185 return -EFAULT;
1186 }
1187 }
1188
1189 spin_lock_irqsave(&d->lock,flags);
1190
1191 if (d->buffer_status[v.buffer]!=VIDEO1394_BUFFER_FREE) {
1192 PRINT(KERN_ERR, ohci->id,
1193 "Buffer %d is already used",v.buffer);
1194 spin_unlock_irqrestore(&d->lock,flags);
1195 return -EFAULT;
1196 }
1197
1198 if (d->flags & VIDEO1394_VARIABLE_PACKET_SIZE) {
1199 initialize_dma_it_prg_var_packet_queue(
1200 d, v.buffer, qv.packet_sizes,
1201 ohci);
1202 }
1203
1204 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_QUEUED;
1205
1206 if (d->last_buffer>=0) {
1207 d->it_prg[d->last_buffer]
1208 [ d->last_used_cmd[d->last_buffer]
1209 ].end.branchAddress =
1210 (virt_to_bus(&(d->it_prg[v.buffer][0].begin.control))
1211 & 0xfffffff0) | 0x3;
1212
1213 d->it_prg[d->last_buffer]
1214 [d->last_used_cmd[d->last_buffer]
1215 ].begin.branchAddress =
1216 (virt_to_bus(&(d->it_prg[v.buffer][0].begin.control))
1217 & 0xfffffff0) | 0x3;
1218 d->next_buffer[d->last_buffer] = v.buffer;
1219 }
1220 d->last_buffer = v.buffer;
1221 d->next_buffer[d->last_buffer] = -1;
1222
1223 d->it_prg[d->last_buffer][d->last_used_cmd[d->last_buffer]].end.branchAddress = 0;
1224
1225 spin_unlock_irqrestore(&d->lock,flags);
1226
1227 if (!(reg_read(ohci, d->ctrlSet) & 0x8000))
1228 {
1229 DBGMSG(ohci->id, "Starting iso transmit DMA ctx=%d",
1230 d->ctx);
1231 put_timestamp(ohci, d, d->last_buffer);
1232
1233 /* Tell the controller where the first program is */
1234 reg_write(ohci, d->cmdPtr,
1235 virt_to_bus(&(d->it_prg[v.buffer][0]))|0x3);
1236
1237 /* Run IT context */
1238 reg_write(ohci, d->ctrlSet, 0x8000);
1239 }
1240 else {
1241 /* Wake up dma context if necessary */
1242 if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
1243 PRINT(KERN_INFO, ohci->id,
1244 "Waking up iso transmit dma ctx=%d",
1245 d->ctx);
1246 put_timestamp(ohci, d, d->last_buffer);
1247 reg_write(ohci, d->ctrlSet, 0x1000);
1248 }
1249 }
1250 return 0;
1251
1252 }
1253 case VIDEO1394_TALK_WAIT_BUFFER:
1254 {
1255 struct video1394_wait v;
1256 struct dma_iso_ctx *d;
1257 int i;
1258
1259 if(copy_from_user(&v, (void *)arg, sizeof(v)))
1260 return -EFAULT;
1261
1262 i = it_ctx_talking(video, v.channel);
1263 if (i<0) return -EFAULT;
1264 d = video->it_context[i];
1265
1266 if ((v.buffer<0) || (v.buffer>d->num_desc)) {
1267 PRINT(KERN_ERR, ohci->id,
1268 "Buffer %d out of range",v.buffer);
1269 return -EFAULT;
1270 }
1271
1272 switch(d->buffer_status[v.buffer]) {
1273 case VIDEO1394_BUFFER_READY:
1274 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1275 return 0;
1276 case VIDEO1394_BUFFER_QUEUED:
1277 #if 1
1278 while(d->buffer_status[v.buffer]!=
1279 VIDEO1394_BUFFER_READY) {
1280 interruptible_sleep_on(&d->waitq);
1281 if(signal_pending(current)) return -EINTR;
1282 }
1283 #else
1284 if (wait_event_interruptible(d->waitq,
1285 d->buffer_status[v.buffer]
1286 == VIDEO1394_BUFFER_READY)
1287 == -ERESTARTSYS)
1288 return -EINTR;
1289 #endif
1290 d->buffer_status[v.buffer]=VIDEO1394_BUFFER_FREE;
1291 return 0;
1292 default:
1293 PRINT(KERN_ERR, ohci->id,
1294 "Buffer %d is not queued",v.buffer);
1295 return -EFAULT;
1296 }
1297 }
1298 default:
1299 return -EINVAL;
1300 }
1301 }
1302
1303 /*
1304 * This maps the vmalloced and reserved buffer to user space.
1305 *
1306 * FIXME:
1307 * - PAGE_READONLY should suffice!?
1308 * - remap_page_range is kind of inefficient for page by page remapping.
1309 * But e.g. pte_alloc() does not work in modules ... :-(
1310 */
1311
1312 int video1394_mmap(struct file *file, struct vm_area_struct *vma)
1313 {
1314 struct video_card *video = NULL;
1315 struct ti_ohci *ohci;
1316 int res = -EINVAL;
1317 unsigned long flags;
1318 struct list_head *lh;
1319
1320 spin_lock_irqsave(&video1394_cards_lock, flags);
1321 if (!list_empty(&video1394_cards)) {
1322 struct video_card *p;
1323 list_for_each(lh, &video1394_cards) {
1324 p = list_entry(lh, struct video_card, list);
1325 if (p->id == MINOR(file->f_dentry->d_inode->i_rdev)) {
1326 video = p;
1327 break;
1328 }
1329 }
1330 }
1331 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1332
1333 if (video == NULL) {
1334 PRINT_G(KERN_ERR, __FUNCTION__": Unknown video card for minor %d",
1335 MINOR(file->f_dentry->d_inode->i_rdev));
1336 return -EFAULT;
1337 }
1338
1339 lock_kernel();
1340 ohci = video->ohci;
1341
1342 if (video->current_ctx == NULL) {
1343 PRINT(KERN_ERR, ohci->id, "Current iso context not set");
1344 } else
1345 res = do_iso_mmap(ohci, video->current_ctx,
1346 (char *)vma->vm_start,
1347 (unsigned long)(vma->vm_end-vma->vm_start));
1348 unlock_kernel();
1349 return res;
1350 }
1351
1352 static int video1394_open(struct inode *inode, struct file *file)
1353 {
1354 int i = MINOR(inode->i_rdev);
1355 unsigned long flags;
1356 struct video_card *video = NULL;
1357 struct list_head *lh;
1358
1359 spin_lock_irqsave(&video1394_cards_lock, flags);
1360 if (!list_empty(&video1394_cards)) {
1361 struct video_card *p;
1362 list_for_each(lh, &video1394_cards) {
1363 p = list_entry(lh, struct video_card, list);
1364 if (p->id == i) {
1365 video = p;
1366 break;
1367 }
1368 }
1369 }
1370 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1371
1372 if (video == NULL)
1373 return -EIO;
1374
1375 V22_COMPAT_MOD_INC_USE_COUNT;
1376
1377 return 0;
1378 }
1379
1380 static int video1394_release(struct inode *inode, struct file *file)
1381 {
1382 struct video_card *video = NULL;
1383 struct ti_ohci *ohci;
1384 u64 mask;
1385 int i;
1386 unsigned long flags;
1387 struct list_head *lh;
1388
1389 spin_lock_irqsave(&video1394_cards_lock, flags);
1390 if (!list_empty(&video1394_cards)) {
1391 struct video_card *p;
1392 list_for_each(lh, &video1394_cards) {
1393 p = list_entry(lh, struct video_card, list);
1394 if (p->id == MINOR(inode->i_rdev)) {
1395 video = p;
1396 break;
1397 }
1398 }
1399 }
1400 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1401
1402 if (video == NULL) {
1403 PRINT_G(KERN_ERR, __FUNCTION__": Unknown device for minor %d",
1404 MINOR(inode->i_rdev));
1405 return 1;
1406 }
1407
1408 ohci = video->ohci;
1409
1410 lock_kernel();
1411 for (i=0;i<ohci->nb_iso_rcv_ctx-1;i++)
1412 if (video->ir_context[i]) {
1413 mask = (u64)0x1<<video->ir_context[i]->channel;
1414 if (!(ohci->ISO_channel_usage & mask))
1415 PRINT(KERN_ERR, ohci->id,
1416 "Channel %d is not being used",
1417 video->ir_context[i]->channel);
1418 else
1419 ohci->ISO_channel_usage &= ~mask;
1420 PRINT(KERN_INFO, ohci->id,
1421 "Iso receive context %d stop listening "
1422 "on channel %d", i+1,
1423 video->ir_context[i]->channel);
1424 free_dma_iso_ctx(&video->ir_context[i]);
1425 }
1426
1427 for (i=0;i<ohci->nb_iso_xmit_ctx;i++)
1428 if (video->it_context[i]) {
1429 mask = (u64)0x1<<video->it_context[i]->channel;
1430 if (!(ohci->ISO_channel_usage & mask))
1431 PRINT(KERN_ERR, ohci->id,
1432 "Channel %d is not being used",
1433 video->it_context[i]->channel);
1434 else
1435 ohci->ISO_channel_usage &= ~mask;
1436 PRINT(KERN_INFO, ohci->id,
1437 "Iso transmit context %d stop talking "
1438 "on channel %d", i+1,
1439 video->it_context[i]->channel);
1440 free_dma_iso_ctx(&video->it_context[i]);
1441 }
1442
1443 V22_COMPAT_MOD_DEC_USE_COUNT;
1444
1445 unlock_kernel();
1446 return 0;
1447 }
1448
1449 static void irq_handler(int card, quadlet_t isoRecvIntEvent,
1450 quadlet_t isoXmitIntEvent)
1451 {
1452 int i;
1453 unsigned long flags;
1454 struct video_card *video = NULL;
1455 struct list_head *lh;
1456
1457 spin_lock_irqsave(&video1394_cards_lock, flags);
1458 if (!list_empty(&video1394_cards)) {
1459 struct video_card *p;
1460 list_for_each(lh, &video1394_cards) {
1461 p = list_entry(lh, struct video_card, list);
1462 if (p->id == card) {
1463 video = p;
1464 break;
1465 }
1466 }
1467 }
1468 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1469
1470 if (video == NULL) {
1471 PRINT_G(KERN_ERR, __FUNCTION__": Unknown card number %d!!",
1472 card);
1473 return;
1474 }
1475
1476 DBGMSG(card, "Iso event Recv: %08x Xmit: %08x",
1477 isoRecvIntEvent, isoXmitIntEvent);
1478
1479 for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++)
1480 if (isoRecvIntEvent & (1<<(i+1)))
1481 wakeup_dma_ir_ctx(video->ohci,
1482 video->ir_context[i]);
1483
1484 for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++)
1485 if (isoXmitIntEvent & (1<<i))
1486 wakeup_dma_it_ctx(video->ohci,
1487 video->it_context[i]);
1488 }
1489
1490 static struct file_operations video1394_fops=
1491 {
1492 OWNER_THIS_MODULE
1493 ioctl: video1394_ioctl,
1494 mmap: video1394_mmap,
1495 open: video1394_open,
1496 release: video1394_release
1497 };
1498
1499 static int video1394_init(struct ti_ohci *ohci)
1500 {
1501 struct video_card *video = kmalloc(sizeof(struct video_card), GFP_KERNEL);
1502 unsigned long flags;
1503 char name[16];
1504
1505 if (video == NULL) {
1506 PRINT(KERN_ERR, ohci->id, "Cannot allocate video_card");
1507 return -1;
1508 }
1509
1510 memset(video, 0, sizeof(struct video_card));
1511
1512 spin_lock_irqsave(&video1394_cards_lock, flags);
1513 INIT_LIST_HEAD(&video->list);
1514 list_add_tail(&video->list, &video1394_cards);
1515 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1516
1517 if (ohci1394_register_video(ohci, &video_tmpl)<0) {
1518 PRINT(KERN_ERR, ohci->id, "Register_video failed");
1519 return -1;
1520 }
1521
1522 video->id = ohci->id;
1523 video->ohci = ohci;
1524
1525 /* Iso receive dma contexts */
1526 video->ir_context = (struct dma_iso_ctx **)
1527 kmalloc((ohci->nb_iso_rcv_ctx-1)*
1528 sizeof(struct dma_iso_ctx *), GFP_KERNEL);
1529 if (video->ir_context)
1530 memset(video->ir_context, 0,
1531 (ohci->nb_iso_rcv_ctx-1)*sizeof(struct dma_iso_ctx *));
1532 else {
1533 PRINT(KERN_ERR, ohci->id, "Cannot allocate ir_context");
1534 return -1;
1535 }
1536
1537 /* Iso transmit dma contexts */
1538 video->it_context = (struct dma_iso_ctx **)
1539 kmalloc(ohci->nb_iso_xmit_ctx *
1540 sizeof(struct dma_iso_ctx *), GFP_KERNEL);
1541 if (video->it_context)
1542 memset(video->it_context, 0,
1543 ohci->nb_iso_xmit_ctx * sizeof(struct dma_iso_ctx *));
1544 else {
1545 PRINT(KERN_ERR, ohci->id, "Cannot allocate it_context");
1546 return -1;
1547 }
1548
1549 sprintf(name, "%d", video->id);
1550 video->devfs = devfs_register(devfs_handle, name,
1551 DEVFS_FL_AUTO_OWNER,
1552 VIDEO1394_MAJOR, 0,
1553 S_IFCHR | S_IRUSR | S_IWUSR,
1554 &video1394_fops, NULL);
1555
1556 return 0;
1557 }
1558
1559 /* Must be called under spinlock */
1560 static void remove_card(struct video_card *video)
1561 {
1562 int i;
1563 unsigned long flags;
1564
1565 ohci1394_unregister_video(video->ohci, &video_tmpl);
1566
1567 devfs_unregister(video->devfs);
1568
1569 /* Free the iso receive contexts */
1570 if (video->ir_context) {
1571 for (i=0;i<video->ohci->nb_iso_rcv_ctx-1;i++) {
1572 free_dma_iso_ctx(&video->ir_context[i]);
1573 }
1574 kfree(video->ir_context);
1575 }
1576
1577 /* Free the iso transmit contexts */
1578 if (video->it_context) {
1579 for (i=0;i<video->ohci->nb_iso_xmit_ctx;i++) {
1580 free_dma_iso_ctx(&video->it_context[i]);
1581 }
1582 kfree(video->it_context);
1583 }
1584 spin_lock_irqsave(&video1394_cards_lock, flags);
1585 list_del(&video->list);
1586 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1587
1588 kfree(video);
1589 }
1590
1591 static void video1394_remove_host (struct hpsb_host *host)
1592 {
1593 struct ti_ohci *ohci;
1594 unsigned long flags;
1595 struct list_head *lh;
1596
1597 /* We only work with the OHCI-1394 driver */
1598 if (strcmp(host->template->name, OHCI1394_DRIVER_NAME))
1599 return;
1600
1601 ohci = (struct ti_ohci *)host->hostdata;
1602
1603 spin_lock_irqsave(&video1394_cards_lock, flags);
1604 if (!list_empty(&video1394_cards)) {
1605 struct video_card *p;
1606 list_for_each(lh, &video1394_cards) {
1607 p = list_entry(lh, struct video_card, list);
1608 if (p ->ohci == ohci) {
1609 remove_card(p);
1610 return;
1611 }
1612 }
1613 }
1614 spin_unlock_irqrestore(&video1394_cards_lock, flags);
1615
1616 return;
1617 }
1618
1619 static void video1394_add_host (struct hpsb_host *host)
1620 {
1621 struct ti_ohci *ohci;
1622
1623 /* We only work with the OHCI-1394 driver */
1624 if (strcmp(host->template->name, OHCI1394_DRIVER_NAME))
1625 return;
1626
1627 ohci = (struct ti_ohci *)host->hostdata;
1628
1629 video1394_init(ohci);
1630
1631 return;
1632 }
1633
1634 static struct hpsb_highlevel_ops hl_ops = {
1635 add_host: video1394_add_host,
1636 remove_host: video1394_remove_host,
1637 };
1638
1639 MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
1640 MODULE_DESCRIPTION("driver for digital video on OHCI board");
1641 MODULE_SUPPORTED_DEVICE(VIDEO1394_DRIVER_NAME);
1642
1643 static void __exit video1394_exit_module (void)
1644 {
1645 hpsb_unregister_highlevel (hl_handle);
1646
1647 devfs_unregister(devfs_handle);
1648 devfs_unregister_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME);
1649
1650 PRINT_G(KERN_INFO, "Removed " VIDEO1394_DRIVER_NAME " module\n");
1651 }
1652
1653 static int __init video1394_init_module (void)
1654 {
1655 if (devfs_register_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME,
1656 &video1394_fops)) {
1657 PRINT_G(KERN_ERR, "video1394: unable to get major %d\n",
1658 VIDEO1394_MAJOR);
1659 return -EIO;
1660 }
1661 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)
1662 devfs_handle = devfs_mk_dir(NULL, VIDEO1394_DRIVER_NAME,
1663 strlen(VIDEO1394_DRIVER_NAME), NULL);
1664 #else
1665 devfs_handle = devfs_mk_dir(NULL, VIDEO1394_DRIVER_NAME, NULL);
1666 #endif
1667
1668 hl_handle = hpsb_register_highlevel (VIDEO1394_DRIVER_NAME, &hl_ops);
1669 if (hl_handle == NULL) {
1670 PRINT_G(KERN_ERR, "No more memory for driver\n");
1671 devfs_unregister(devfs_handle);
1672 devfs_unregister_chrdev(VIDEO1394_MAJOR, VIDEO1394_DRIVER_NAME);
1673 return -ENOMEM;
1674 }
1675
1676 return 0;
1677 }
1678
1679 module_init(video1394_init_module);
1680 module_exit(video1394_exit_module);
1681