1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86#include <linux/kernel.h>
87#include <linux/list.h>
88#include <linux/slab.h>
89#include <linux/interrupt.h>
90#include <linux/wait.h>
91#include <linux/errno.h>
92#include <linux/module.h>
93#include <linux/init.h>
94#include <linux/pci.h>
95#include <linux/fs.h>
96#include <linux/poll.h>
97#include <linux/mutex.h>
98#include <linux/bitops.h>
99#include <asm/byteorder.h>
100#include <asm/atomic.h>
101#include <asm/io.h>
102#include <asm/uaccess.h>
103#include <linux/delay.h>
104#include <asm/pgtable.h>
105#include <asm/page.h>
106#include <linux/sched.h>
107#include <linux/types.h>
108#include <linux/vmalloc.h>
109#include <linux/string.h>
110#include <linux/compat.h>
111#include <linux/cdev.h>
112
113#include "dv1394.h"
114#include "dv1394-private.h"
115#include "highlevel.h"
116#include "hosts.h"
117#include "ieee1394.h"
118#include "ieee1394_core.h"
119#include "ieee1394_hotplug.h"
120#include "ieee1394_types.h"
121#include "nodemgr.h"
122#include "ohci1394.h"
123
124
125
126
127
128
129
130
131#define DV1394_DEBUG_LEVEL 0
132
133
134
135
136#if DV1394_DEBUG_LEVEL >= 2
137#define irq_printk( args... ) printk( args )
138#else
139#define irq_printk( args... ) do {} while (0)
140#endif
141
142#if DV1394_DEBUG_LEVEL >= 1
143#define debug_printk( args... ) printk( args)
144#else
145#define debug_printk( args... ) do {} while (0)
146#endif
147
148
149
150
151static inline void flush_pci_write(struct ti_ohci *ohci)
152{
153 mb();
154 reg_read(ohci, OHCI1394_IsochronousCycleTimer);
155}
156
157static void it_tasklet_func(unsigned long data);
158static void ir_tasklet_func(unsigned long data);
159
160#ifdef CONFIG_COMPAT
161static long dv1394_compat_ioctl(struct file *file, unsigned int cmd,
162 unsigned long arg);
163#endif
164
165
166
167
168static LIST_HEAD(dv1394_cards);
169static DEFINE_SPINLOCK(dv1394_cards_lock);
170
171
172
173static inline struct video_card* file_to_video_card(struct file *file)
174{
175 return (struct video_card*) file->private_data;
176}
177
178
179
180static void frame_reset(struct frame *f)
181{
182 f->state = FRAME_CLEAR;
183 f->done = 0;
184 f->n_packets = 0;
185 f->frame_begin_timestamp = NULL;
186 f->assigned_timestamp = 0;
187 f->cip_syt1 = NULL;
188 f->cip_syt2 = NULL;
189 f->mid_frame_timestamp = NULL;
190 f->frame_end_timestamp = NULL;
191 f->frame_end_branch = NULL;
192}
193
194static struct frame* frame_new(unsigned int frame_num, struct video_card *video)
195{
196 struct frame *f = kmalloc(sizeof(*f), GFP_KERNEL);
197 if (!f)
198 return NULL;
199
200 f->video = video;
201 f->frame_num = frame_num;
202
203 f->header_pool = pci_alloc_consistent(f->video->ohci->dev, PAGE_SIZE, &f->header_pool_dma);
204 if (!f->header_pool) {
205 printk(KERN_ERR "dv1394: failed to allocate CIP header pool\n");
206 kfree(f);
207 return NULL;
208 }
209
210 debug_printk("dv1394: frame_new: allocated CIP header pool at virt 0x%08lx (contig) dma 0x%08lx size %ld\n",
211 (unsigned long) f->header_pool, (unsigned long) f->header_pool_dma, PAGE_SIZE);
212
213 f->descriptor_pool_size = MAX_PACKETS * sizeof(struct DMA_descriptor_block);
214
215 f->descriptor_pool_size += PAGE_SIZE - (f->descriptor_pool_size%PAGE_SIZE);
216
217 f->descriptor_pool = pci_alloc_consistent(f->video->ohci->dev,
218 f->descriptor_pool_size,
219 &f->descriptor_pool_dma);
220 if (!f->descriptor_pool) {
221 pci_free_consistent(f->video->ohci->dev, PAGE_SIZE, f->header_pool, f->header_pool_dma);
222 kfree(f);
223 return NULL;
224 }
225
226 debug_printk("dv1394: frame_new: allocated DMA program memory at virt 0x%08lx (contig) dma 0x%08lx size %ld\n",
227 (unsigned long) f->descriptor_pool, (unsigned long) f->descriptor_pool_dma, f->descriptor_pool_size);
228
229 f->data = 0;
230 frame_reset(f);
231
232 return f;
233}
234
235static void frame_delete(struct frame *f)
236{
237 pci_free_consistent(f->video->ohci->dev, PAGE_SIZE, f->header_pool, f->header_pool_dma);
238 pci_free_consistent(f->video->ohci->dev, f->descriptor_pool_size, f->descriptor_pool, f->descriptor_pool_dma);
239 kfree(f);
240}
241
242
243
244
245
246
247
248
249
250
251
252
253static void frame_prepare(struct video_card *video, unsigned int this_frame)
254{
255 struct frame *f = video->frames[this_frame];
256 int last_frame;
257
258 struct DMA_descriptor_block *block;
259 dma_addr_t block_dma;
260 struct CIP_header *cip;
261 dma_addr_t cip_dma;
262
263 unsigned int n_descriptors, full_packets, packets_per_frame, payload_size;
264
265
266 int empty_packet, first_packet, last_packet, mid_packet;
267
268 __le32 *branch_address, *last_branch_address = NULL;
269 unsigned long data_p;
270 int first_packet_empty = 0;
271 u32 cycleTimer, ct_sec, ct_cyc, ct_off;
272 unsigned long irq_flags;
273
274 irq_printk("frame_prepare( %d ) ---------------------\n", this_frame);
275
276 full_packets = 0;
277
278
279
280 if (video->pal_or_ntsc == DV1394_PAL)
281 packets_per_frame = DV1394_PAL_PACKETS_PER_FRAME;
282 else
283 packets_per_frame = DV1394_NTSC_PACKETS_PER_FRAME;
284
285 while ( full_packets < packets_per_frame ) {
286 empty_packet = first_packet = last_packet = mid_packet = 0;
287
288 data_p = f->data + full_packets * 480;
289
290
291
292
293
294
295
296 if (f->n_packets >= MAX_PACKETS) {
297 printk(KERN_ERR "dv1394: FATAL ERROR: max packet count exceeded\n");
298 return;
299 }
300
301
302
303 block = &(f->descriptor_pool[f->n_packets]);
304
305
306
307
308 block_dma = ((unsigned long) block - (unsigned long) f->descriptor_pool) + f->descriptor_pool_dma;
309
310
311
312 if ( ((unsigned long) &(f->header_pool[f->n_packets]) - (unsigned long) f->header_pool)
313 > PAGE_SIZE) {
314 printk(KERN_ERR "dv1394: FATAL ERROR: no room to allocate CIP header\n");
315 return;
316 }
317
318 cip = &(f->header_pool[f->n_packets]);
319
320
321
322
323 cip_dma = (unsigned long) cip % PAGE_SIZE + f->header_pool_dma;
324
325
326
327 if (video->cip_accum > (video->cip_d - video->cip_n)) {
328 empty_packet = 1;
329 payload_size = 8;
330 video->cip_accum -= (video->cip_d - video->cip_n);
331 } else {
332 payload_size = 488;
333 video->cip_accum += video->cip_n;
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 if (f->n_packets == 0) {
361 first_packet = 1;
362 } else if ( full_packets == (packets_per_frame-1) ) {
363 last_packet = 1;
364 } else if (f->n_packets == packets_per_frame) {
365 mid_packet = 1;
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379 if (first_packet) {
380 f->cip_syt1 = cip;
381 if (empty_packet)
382 first_packet_empty = 1;
383
384 } else if (first_packet_empty && (f->n_packets == 1) ) {
385
386
387 f->cip_syt2 = cip;
388 }
389
390 fill_cip_header(cip,
391
392 reg_read(video->ohci, OHCI1394_NodeID) & 0x3F,
393 video->continuity_counter,
394 video->pal_or_ntsc,
395 0xFFFF );
396
397
398 if ( ! empty_packet )
399 video->continuity_counter++;
400
401
402
403
404
405
406 fill_output_more_immediate( &(block->u.out.omi), 1, video->channel, 0, payload_size);
407
408 if (empty_packet) {
409
410 fill_output_last( &(block->u.out.u.empty.ol),
411
412
413 (first_packet || mid_packet || last_packet) ? 1 : 0,
414
415
416 (first_packet || mid_packet || last_packet) ? 1 : 0,
417
418 sizeof(struct CIP_header),
419 cip_dma);
420
421 if (first_packet)
422 f->frame_begin_timestamp = &(block->u.out.u.empty.ol.q[3]);
423 else if (mid_packet)
424 f->mid_frame_timestamp = &(block->u.out.u.empty.ol.q[3]);
425 else if (last_packet) {
426 f->frame_end_timestamp = &(block->u.out.u.empty.ol.q[3]);
427 f->frame_end_branch = &(block->u.out.u.empty.ol.q[2]);
428 }
429
430 branch_address = &(block->u.out.u.empty.ol.q[2]);
431 n_descriptors = 3;
432 if (first_packet)
433 f->first_n_descriptors = n_descriptors;
434
435 } else {
436
437
438 fill_output_more( &(block->u.out.u.full.om),
439 sizeof(struct CIP_header),
440 cip_dma);
441
442
443
444
445
446
447
448 if ( (PAGE_SIZE- ((unsigned long)data_p % PAGE_SIZE) ) < 480 ) {
449
450
451
452 fill_output_more( &(block->u.out.u.full.u.cross.om),
453
454 PAGE_SIZE - (data_p % PAGE_SIZE),
455
456
457 dma_region_offset_to_bus(&video->dv_buf,
458 data_p - (unsigned long) video->dv_buf.kvirt));
459
460 fill_output_last( &(block->u.out.u.full.u.cross.ol),
461
462
463 (first_packet || mid_packet || last_packet) ? 1 : 0,
464
465
466 (first_packet || mid_packet || last_packet) ? 1 : 0,
467
468
469 480 - (PAGE_SIZE - (data_p % PAGE_SIZE)),
470
471
472 dma_region_offset_to_bus(&video->dv_buf,
473 data_p + PAGE_SIZE - (data_p % PAGE_SIZE) - (unsigned long) video->dv_buf.kvirt));
474
475 if (first_packet)
476 f->frame_begin_timestamp = &(block->u.out.u.full.u.cross.ol.q[3]);
477 else if (mid_packet)
478 f->mid_frame_timestamp = &(block->u.out.u.full.u.cross.ol.q[3]);
479 else if (last_packet) {
480 f->frame_end_timestamp = &(block->u.out.u.full.u.cross.ol.q[3]);
481 f->frame_end_branch = &(block->u.out.u.full.u.cross.ol.q[2]);
482 }
483
484 branch_address = &(block->u.out.u.full.u.cross.ol.q[2]);
485
486 n_descriptors = 5;
487 if (first_packet)
488 f->first_n_descriptors = n_descriptors;
489
490 full_packets++;
491
492 } else {
493
494
495 fill_output_last( &(block->u.out.u.full.u.nocross.ol),
496
497
498 (first_packet || mid_packet || last_packet) ? 1 : 0,
499
500
501 (first_packet || mid_packet || last_packet) ? 1 : 0,
502
503 480,
504
505
506
507 dma_region_offset_to_bus(&video->dv_buf,
508 data_p - (unsigned long) video->dv_buf.kvirt));
509
510 if (first_packet)
511 f->frame_begin_timestamp = &(block->u.out.u.full.u.nocross.ol.q[3]);
512 else if (mid_packet)
513 f->mid_frame_timestamp = &(block->u.out.u.full.u.nocross.ol.q[3]);
514 else if (last_packet) {
515 f->frame_end_timestamp = &(block->u.out.u.full.u.nocross.ol.q[3]);
516 f->frame_end_branch = &(block->u.out.u.full.u.nocross.ol.q[2]);
517 }
518
519 branch_address = &(block->u.out.u.full.u.nocross.ol.q[2]);
520
521 n_descriptors = 4;
522 if (first_packet)
523 f->first_n_descriptors = n_descriptors;
524
525 full_packets++;
526 }
527 }
528
529
530
531
532
533
534 if (last_branch_address) {
535 *(last_branch_address) = cpu_to_le32(block_dma | n_descriptors);
536 }
537
538 last_branch_address = branch_address;
539
540
541 f->n_packets++;
542
543 }
544
545
546
547 *(f->frame_end_branch) = cpu_to_le32(f->descriptor_pool_dma | f->first_n_descriptors);
548
549
550 dma_region_sync_for_device(&video->dv_buf, f->data - (unsigned long) video->dv_buf.kvirt, video->frame_size);
551
552
553 spin_lock_irqsave(&video->spinlock, irq_flags);
554
555 f->state = FRAME_READY;
556
557 video->n_clear_frames--;
558
559 last_frame = video->first_clear_frame - 1;
560 if (last_frame == -1)
561 last_frame = video->n_frames-1;
562
563 video->first_clear_frame = (video->first_clear_frame + 1) % video->n_frames;
564
565 irq_printk(" frame %d prepared, active_frame = %d, n_clear_frames = %d, first_clear_frame = %d\n last=%d\n",
566 this_frame, video->active_frame, video->n_clear_frames, video->first_clear_frame, last_frame);
567
568 irq_printk(" begin_ts %08lx mid_ts %08lx end_ts %08lx end_br %08lx\n",
569 (unsigned long) f->frame_begin_timestamp,
570 (unsigned long) f->mid_frame_timestamp,
571 (unsigned long) f->frame_end_timestamp,
572 (unsigned long) f->frame_end_branch);
573
574 if (video->active_frame != -1) {
575
576
577
578 if (video->frames[last_frame]->frame_end_branch) {
579 u32 temp;
580
581
582 *(video->frames[last_frame]->frame_end_branch) = cpu_to_le32(f->descriptor_pool_dma | f->first_n_descriptors);
583
584
585 wmb();
586
587
588 temp = le32_to_cpu(*(video->frames[last_frame]->frame_end_branch - 2));
589 temp &= 0xF7CFFFFF;
590 *(video->frames[last_frame]->frame_end_branch - 2) = cpu_to_le32(temp);
591
592
593 flush_pci_write(video->ohci);
594
595
596
597
598
599
600
601
602
603
604 irq_printk(" new frame %d linked onto DMA chain\n", this_frame);
605
606 } else {
607 printk(KERN_ERR "dv1394: last frame not ready???\n");
608 }
609
610 } else {
611
612 u32 transmit_sec, transmit_cyc;
613 u32 ts_cyc, ts_off;
614
615
616 video->active_frame = this_frame;
617
618
619 reg_write(video->ohci, video->ohci_IsoXmitCommandPtr,
620 video->frames[video->active_frame]->descriptor_pool_dma |
621 f->first_n_descriptors);
622
623
624
625
626
627 cycleTimer = reg_read(video->ohci, OHCI1394_IsochronousCycleTimer);
628
629 ct_sec = cycleTimer >> 25;
630 ct_cyc = (cycleTimer >> 12) & 0x1FFF;
631 ct_off = cycleTimer & 0xFFF;
632
633 transmit_sec = ct_sec;
634 transmit_cyc = ct_cyc + 100;
635
636 transmit_sec += transmit_cyc/8000;
637 transmit_cyc %= 8000;
638
639 ts_off = ct_off;
640 ts_cyc = transmit_cyc + 3;
641 ts_cyc %= 8000;
642
643 f->assigned_timestamp = (ts_cyc&0xF) << 12;
644
645
646 if (f->cip_syt1) {
647 f->cip_syt1->b[6] = f->assigned_timestamp >> 8;
648 f->cip_syt1->b[7] = f->assigned_timestamp & 0xFF;
649 }
650 if (f->cip_syt2) {
651 f->cip_syt2->b[6] = f->assigned_timestamp >> 8;
652 f->cip_syt2->b[7] = f->assigned_timestamp & 0xFF;
653 }
654
655
656
657
658
659 reg_write(video->ohci, video->ohci_IsoXmitContextControlClear, 0xFFFFFFFF);
660 wmb();
661
662
663
664
665
666
667
668
669
670
671
672
673
674#if 0
675 reg_write(video->ohci, video->ohci_IsoXmitContextControlSet,
676 (1 << 31)
677 | ( (transmit_sec & 0x3) << 29)
678 | (transmit_cyc << 16));
679 wmb();
680#endif
681
682 video->dma_running = 1;
683
684
685 reg_write(video->ohci, video->ohci_IsoXmitContextControlSet, 0x8000);
686 flush_pci_write(video->ohci);
687
688
689
690 debug_printk(" Cycle = %4u ContextControl = %08x CmdPtr = %08x\n",
691 (reg_read(video->ohci, OHCI1394_IsochronousCycleTimer) >> 12) & 0x1FFF,
692 reg_read(video->ohci, video->ohci_IsoXmitContextControlSet),
693 reg_read(video->ohci, video->ohci_IsoXmitCommandPtr));
694
695 debug_printk(" DMA start - current cycle %4u, transmit cycle %4u (%2u), assigning ts cycle %2u\n",
696 ct_cyc, transmit_cyc, transmit_cyc & 0xF, ts_cyc & 0xF);
697
698#if DV1394_DEBUG_LEVEL >= 2
699 {
700
701 int i = 0;
702 while (i < 20) {
703 mb();
704 mdelay(1);
705 if (reg_read(video->ohci, video->ohci_IsoXmitContextControlSet) & (1 << 10)) {
706 printk("DMA ACTIVE after %d msec\n", i);
707 break;
708 }
709 i++;
710 }
711
712 printk("set = %08x, cmdPtr = %08x\n",
713 reg_read(video->ohci, video->ohci_IsoXmitContextControlSet),
714 reg_read(video->ohci, video->ohci_IsoXmitCommandPtr)
715 );
716
717 if ( ! (reg_read(video->ohci, video->ohci_IsoXmitContextControlSet) & (1 << 10)) ) {
718 printk("DMA did NOT go active after 20ms, event = %x\n",
719 reg_read(video->ohci, video->ohci_IsoXmitContextControlSet) & 0x1F);
720 } else
721 printk("DMA is RUNNING!\n");
722 }
723#endif
724
725 }
726
727
728 spin_unlock_irqrestore(&video->spinlock, irq_flags);
729}
730
731
732
733
734
735
736
737
738
739
740
741
742static void inline
743frame_put_packet (struct frame *f, struct packet *p)
744{
745 int section_type = p->data[0] >> 5;
746 int dif_sequence = p->data[1] >> 4;
747 int dif_block = p->data[2];
748
749
750 if (dif_sequence > 11 || dif_block > 149) return;
751
752 switch (section_type) {
753 case 0:
754 memcpy( (void *) f->data + dif_sequence * 150 * 80, p->data, 480);
755 break;
756
757 case 1:
758 memcpy( (void *) f->data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p->data, 480);
759 break;
760
761 case 2:
762 memcpy( (void *) f->data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p->data, 480);
763 break;
764
765 case 3:
766 memcpy( (void *) f->data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p->data, 480);
767 break;
768
769 case 4:
770 memcpy( (void *) f->data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) + dif_block) * 80, p->data, 480);
771 break;
772
773 default:
774 break;
775 }
776}
777
778
779static void start_dma_receive(struct video_card *video)
780{
781 if (video->first_run == 1) {
782 video->first_run = 0;
783
784
785 video->n_clear_frames = 0;
786 video->first_clear_frame = -1;
787 video->current_packet = 0;
788 video->active_frame = 0;
789
790
791 reg_write(video->ohci, video->ohci_IsoRcvContextControlClear, 0xFFFFFFFF);
792 wmb();
793
794
795 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, 0x40000000);
796
797
798 reg_write(video->ohci, video->ohci_IsoRcvContextMatch, 0xf0000000 | video->channel);
799
800
801 reg_write(video->ohci, video->ohci_IsoRcvCommandPtr,
802 video->frames[0]->descriptor_pool_dma | 1);
803 wmb();
804
805 video->dma_running = 1;
806
807
808 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, 0x8000);
809 flush_pci_write(video->ohci);
810
811 debug_printk("dv1394: DMA started\n");
812
813#if DV1394_DEBUG_LEVEL >= 2
814 {
815 int i;
816
817 for (i = 0; i < 1000; ++i) {
818 mdelay(1);
819 if (reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & (1 << 10)) {
820 printk("DMA ACTIVE after %d msec\n", i);
821 break;
822 }
823 }
824 if ( reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & (1 << 11) ) {
825 printk("DEAD, event = %x\n",
826 reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & 0x1F);
827 } else
828 printk("RUNNING!\n");
829 }
830#endif
831 } else if ( reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & (1 << 11) ) {
832 debug_printk("DEAD, event = %x\n",
833 reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & 0x1F);
834
835
836 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, (1 << 12));
837 }
838}
839
840
841
842
843
844
845static void receive_packets(struct video_card *video)
846{
847 struct DMA_descriptor_block *block = NULL;
848 dma_addr_t block_dma = 0;
849 struct packet *data = NULL;
850 dma_addr_t data_dma = 0;
851 __le32 *last_branch_address = NULL;
852 unsigned long irq_flags;
853 int want_interrupt = 0;
854 struct frame *f = NULL;
855 int i, j;
856
857 spin_lock_irqsave(&video->spinlock, irq_flags);
858
859 for (j = 0; j < video->n_frames; j++) {
860
861
862 if (j > 0 && f != NULL && f->frame_end_branch != NULL)
863 *(f->frame_end_branch) = cpu_to_le32(video->frames[j]->descriptor_pool_dma | 1);
864
865 f = video->frames[j];
866
867 for (i = 0; i < MAX_PACKETS; i++) {
868
869 block = &(f->descriptor_pool[i]);
870 block_dma = ((unsigned long) block - (unsigned long) f->descriptor_pool) + f->descriptor_pool_dma;
871
872 data = ((struct packet*)video->packet_buf.kvirt) + f->frame_num * MAX_PACKETS + i;
873 data_dma = dma_region_offset_to_bus( &video->packet_buf,
874 ((unsigned long) data - (unsigned long) video->packet_buf.kvirt) );
875
876
877 want_interrupt = ((i % (MAX_PACKETS/2)) == 0 || i == (MAX_PACKETS-1));
878 fill_input_last( &(block->u.in.il), want_interrupt, 512, data_dma);
879
880
881 last_branch_address = f->frame_end_branch;
882
883 if (last_branch_address != NULL)
884 *(last_branch_address) = cpu_to_le32(block_dma | 1);
885
886 f->frame_end_branch = &(block->u.in.il.q[2]);
887 }
888
889 }
890
891 spin_unlock_irqrestore(&video->spinlock, irq_flags);
892
893}
894
895
896
897
898
899static int do_dv1394_init(struct video_card *video, struct dv1394_init *init)
900{
901 unsigned long flags, new_buf_size;
902 int i;
903 u64 chan_mask;
904 int retval = -EINVAL;
905
906 debug_printk("dv1394: initialising %d\n", video->id);
907 if (init->api_version != DV1394_API_VERSION)
908 return -EINVAL;
909
910
911 if ( (init->n_frames < 2) || (init->n_frames > DV1394_MAX_FRAMES) )
912 return -EINVAL;
913
914 if ( (init->format != DV1394_NTSC) && (init->format != DV1394_PAL) )
915 return -EINVAL;
916
917 if ( (init->syt_offset == 0) || (init->syt_offset > 50) )
918
919 init->syt_offset = 3;
920
921 if (init->channel > 63)
922 init->channel = 63;
923
924 chan_mask = (u64)1 << init->channel;
925
926
927 if (init->format == DV1394_NTSC)
928 new_buf_size = DV1394_NTSC_FRAME_SIZE * init->n_frames;
929 else
930 new_buf_size = DV1394_PAL_FRAME_SIZE * init->n_frames;
931
932
933 if (new_buf_size % PAGE_SIZE) new_buf_size += PAGE_SIZE - (new_buf_size % PAGE_SIZE);
934
935
936 if (video->dv_buf.kvirt && video->dv_buf_size != new_buf_size) {
937 printk("dv1394: re-sizing the DMA buffer is not allowed\n");
938 return -EINVAL;
939 }
940
941
942
943
944 do_dv1394_shutdown(video, 0);
945
946
947 spin_lock_irqsave(&video->ohci->IR_channel_lock, flags);
948 if (video->ohci->ISO_channel_usage & chan_mask) {
949 spin_unlock_irqrestore(&video->ohci->IR_channel_lock, flags);
950 retval = -EBUSY;
951 goto err;
952 }
953 video->ohci->ISO_channel_usage |= chan_mask;
954 spin_unlock_irqrestore(&video->ohci->IR_channel_lock, flags);
955
956 video->channel = init->channel;
957
958
959 video->n_frames = init->n_frames;
960 video->pal_or_ntsc = init->format;
961
962 video->cip_accum = 0;
963 video->continuity_counter = 0;
964
965 video->active_frame = -1;
966 video->first_clear_frame = 0;
967 video->n_clear_frames = video->n_frames;
968 video->dropped_frames = 0;
969
970 video->write_off = 0;
971
972 video->first_run = 1;
973 video->current_packet = -1;
974 video->first_frame = 0;
975
976 if (video->pal_or_ntsc == DV1394_NTSC) {
977 video->cip_n = init->cip_n != 0 ? init->cip_n : CIP_N_NTSC;
978 video->cip_d = init->cip_d != 0 ? init->cip_d : CIP_D_NTSC;
979 video->frame_size = DV1394_NTSC_FRAME_SIZE;
980 } else {
981 video->cip_n = init->cip_n != 0 ? init->cip_n : CIP_N_PAL;
982 video->cip_d = init->cip_d != 0 ? init->cip_d : CIP_D_PAL;
983 video->frame_size = DV1394_PAL_FRAME_SIZE;
984 }
985
986 video->syt_offset = init->syt_offset;
987
988
989
990 if (video->ohci_it_ctx == -1) {
991 ohci1394_init_iso_tasklet(&video->it_tasklet, OHCI_ISO_TRANSMIT,
992 it_tasklet_func, (unsigned long) video);
993
994 if (ohci1394_register_iso_tasklet(video->ohci, &video->it_tasklet) < 0) {
995 printk(KERN_ERR "dv1394: could not find an available IT DMA context\n");
996 retval = -EBUSY;
997 goto err;
998 }
999
1000 video->ohci_it_ctx = video->it_tasklet.context;
1001 debug_printk("dv1394: claimed IT DMA context %d\n", video->ohci_it_ctx);
1002 }
1003
1004 if (video->ohci_ir_ctx == -1) {
1005 ohci1394_init_iso_tasklet(&video->ir_tasklet, OHCI_ISO_RECEIVE,
1006 ir_tasklet_func, (unsigned long) video);
1007
1008 if (ohci1394_register_iso_tasklet(video->ohci, &video->ir_tasklet) < 0) {
1009 printk(KERN_ERR "dv1394: could not find an available IR DMA context\n");
1010 retval = -EBUSY;
1011 goto err;
1012 }
1013 video->ohci_ir_ctx = video->ir_tasklet.context;
1014 debug_printk("dv1394: claimed IR DMA context %d\n", video->ohci_ir_ctx);
1015 }
1016
1017
1018 for (i = 0; i < init->n_frames; i++) {
1019 video->frames[i] = frame_new(i, video);
1020
1021 if (!video->frames[i]) {
1022 printk(KERN_ERR "dv1394: Cannot allocate frame structs\n");
1023 retval = -ENOMEM;
1024 goto err;
1025 }
1026 }
1027
1028 if (!video->dv_buf.kvirt) {
1029
1030 retval = dma_region_alloc(&video->dv_buf, new_buf_size, video->ohci->dev, PCI_DMA_TODEVICE);
1031 if (retval)
1032 goto err;
1033
1034 video->dv_buf_size = new_buf_size;
1035
1036 debug_printk("dv1394: Allocated %d frame buffers, total %u pages (%u DMA pages), %lu bytes\n",
1037 video->n_frames, video->dv_buf.n_pages,
1038 video->dv_buf.n_dma_pages, video->dv_buf_size);
1039 }
1040
1041
1042 for (i = 0; i < video->n_frames; i++)
1043 video->frames[i]->data = (unsigned long) video->dv_buf.kvirt + i * video->frame_size;
1044
1045 if (!video->packet_buf.kvirt) {
1046
1047 video->packet_buf_size = sizeof(struct packet) * video->n_frames * MAX_PACKETS;
1048 if (video->packet_buf_size % PAGE_SIZE)
1049 video->packet_buf_size += PAGE_SIZE - (video->packet_buf_size % PAGE_SIZE);
1050
1051 retval = dma_region_alloc(&video->packet_buf, video->packet_buf_size,
1052 video->ohci->dev, PCI_DMA_FROMDEVICE);
1053 if (retval)
1054 goto err;
1055
1056 debug_printk("dv1394: Allocated %d packets in buffer, total %u pages (%u DMA pages), %lu bytes\n",
1057 video->n_frames*MAX_PACKETS, video->packet_buf.n_pages,
1058 video->packet_buf.n_dma_pages, video->packet_buf_size);
1059 }
1060
1061
1062
1063 video->ohci_IsoXmitContextControlSet = OHCI1394_IsoXmitContextControlSet+16*video->ohci_it_ctx;
1064 video->ohci_IsoXmitContextControlClear = OHCI1394_IsoXmitContextControlClear+16*video->ohci_it_ctx;
1065 video->ohci_IsoXmitCommandPtr = OHCI1394_IsoXmitCommandPtr+16*video->ohci_it_ctx;
1066
1067
1068 reg_write(video->ohci, OHCI1394_IsoXmitIntMaskSet, (1 << video->ohci_it_ctx));
1069 debug_printk("dv1394: interrupts enabled for IT context %d\n", video->ohci_it_ctx);
1070
1071
1072
1073 video->ohci_IsoRcvContextControlSet = OHCI1394_IsoRcvContextControlSet+32*video->ohci_ir_ctx;
1074 video->ohci_IsoRcvContextControlClear = OHCI1394_IsoRcvContextControlClear+32*video->ohci_ir_ctx;
1075 video->ohci_IsoRcvCommandPtr = OHCI1394_IsoRcvCommandPtr+32*video->ohci_ir_ctx;
1076 video->ohci_IsoRcvContextMatch = OHCI1394_IsoRcvContextMatch+32*video->ohci_ir_ctx;
1077
1078
1079 reg_write(video->ohci, OHCI1394_IsoRecvIntMaskSet, (1 << video->ohci_ir_ctx) );
1080 debug_printk("dv1394: interrupts enabled for IR context %d\n", video->ohci_ir_ctx);
1081
1082 return 0;
1083
1084err:
1085 do_dv1394_shutdown(video, 1);
1086 return retval;
1087}
1088
1089
1090
1091
1092static int do_dv1394_init_default(struct video_card *video)
1093{
1094 struct dv1394_init init;
1095
1096 init.api_version = DV1394_API_VERSION;
1097 init.n_frames = DV1394_MAX_FRAMES / 4;
1098 init.channel = video->channel;
1099 init.format = video->pal_or_ntsc;
1100 init.cip_n = video->cip_n;
1101 init.cip_d = video->cip_d;
1102 init.syt_offset = video->syt_offset;
1103
1104 return do_dv1394_init(video, &init);
1105}
1106
1107
1108static void stop_dma(struct video_card *video)
1109{
1110 unsigned long flags;
1111 int i;
1112
1113
1114 spin_lock_irqsave(&video->spinlock, flags);
1115
1116 video->dma_running = 0;
1117
1118 if ( (video->ohci_it_ctx == -1) && (video->ohci_ir_ctx == -1) )
1119 goto out;
1120
1121
1122 if ( (video->active_frame != -1) ||
1123 (reg_read(video->ohci, video->ohci_IsoXmitContextControlClear) & (1 << 10)) ||
1124 (reg_read(video->ohci, video->ohci_IsoRcvContextControlClear) & (1 << 10)) ) {
1125
1126
1127 reg_write(video->ohci, video->ohci_IsoXmitContextControlClear, (1 << 15));
1128 reg_write(video->ohci, video->ohci_IsoRcvContextControlClear, (1 << 15));
1129 flush_pci_write(video->ohci);
1130
1131 video->active_frame = -1;
1132 video->first_run = 1;
1133
1134
1135 i = 0;
1136 while (i < 1000) {
1137
1138
1139 udelay(100);
1140
1141 if ( (reg_read(video->ohci, video->ohci_IsoXmitContextControlClear) & (1 << 10)) ||
1142 (reg_read(video->ohci, video->ohci_IsoRcvContextControlClear) & (1 << 10)) ) {
1143
1144 debug_printk("dv1394: stop_dma: DMA not stopped yet\n" );
1145 mb();
1146 } else {
1147 debug_printk("dv1394: stop_dma: DMA stopped safely after %d ms\n", i/10);
1148 break;
1149 }
1150
1151 i++;
1152 }
1153
1154 if (i == 1000) {
1155 printk(KERN_ERR "dv1394: stop_dma: DMA still going after %d ms!\n", i/10);
1156 }
1157 }
1158 else
1159 debug_printk("dv1394: stop_dma: already stopped.\n");
1160
1161out:
1162 spin_unlock_irqrestore(&video->spinlock, flags);
1163}
1164
1165
1166
1167static void do_dv1394_shutdown(struct video_card *video, int free_dv_buf)
1168{
1169 int i;
1170
1171 debug_printk("dv1394: shutdown...\n");
1172
1173
1174 stop_dma(video);
1175
1176
1177 if (video->ohci_it_ctx != -1) {
1178 video->ohci_IsoXmitContextControlSet = 0;
1179 video->ohci_IsoXmitContextControlClear = 0;
1180 video->ohci_IsoXmitCommandPtr = 0;
1181
1182
1183 reg_write(video->ohci, OHCI1394_IsoXmitIntMaskClear, (1 << video->ohci_it_ctx));
1184
1185
1186 ohci1394_unregister_iso_tasklet(video->ohci, &video->it_tasklet);
1187 debug_printk("dv1394: IT context %d released\n", video->ohci_it_ctx);
1188 video->ohci_it_ctx = -1;
1189 }
1190
1191 if (video->ohci_ir_ctx != -1) {
1192 video->ohci_IsoRcvContextControlSet = 0;
1193 video->ohci_IsoRcvContextControlClear = 0;
1194 video->ohci_IsoRcvCommandPtr = 0;
1195 video->ohci_IsoRcvContextMatch = 0;
1196
1197
1198 reg_write(video->ohci, OHCI1394_IsoRecvIntMaskClear, (1 << video->ohci_ir_ctx));
1199
1200
1201 ohci1394_unregister_iso_tasklet(video->ohci, &video->ir_tasklet);
1202 debug_printk("dv1394: IR context %d released\n", video->ohci_ir_ctx);
1203 video->ohci_ir_ctx = -1;
1204 }
1205
1206
1207 if (video->channel != -1) {
1208 u64 chan_mask;
1209 unsigned long flags;
1210
1211 chan_mask = (u64)1 << video->channel;
1212
1213 spin_lock_irqsave(&video->ohci->IR_channel_lock, flags);
1214 video->ohci->ISO_channel_usage &= ~(chan_mask);
1215 spin_unlock_irqrestore(&video->ohci->IR_channel_lock, flags);
1216
1217 video->channel = -1;
1218 }
1219
1220
1221 for (i = 0; i < DV1394_MAX_FRAMES; i++) {
1222 if (video->frames[i])
1223 frame_delete(video->frames[i]);
1224 video->frames[i] = NULL;
1225 }
1226
1227 video->n_frames = 0;
1228
1229
1230
1231
1232 if (free_dv_buf) {
1233 dma_region_free(&video->dv_buf);
1234 video->dv_buf_size = 0;
1235 }
1236
1237
1238 dma_region_free(&video->packet_buf);
1239 video->packet_buf_size = 0;
1240
1241 debug_printk("dv1394: shutdown OK\n");
1242}
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268static int dv1394_mmap(struct file *file, struct vm_area_struct *vma)
1269{
1270 struct video_card *video = file_to_video_card(file);
1271 int retval = -EINVAL;
1272
1273
1274
1275
1276
1277
1278
1279 if (!mutex_trylock(&video->mtx))
1280 return -EAGAIN;
1281
1282 if ( ! video_card_initialized(video) ) {
1283 retval = do_dv1394_init_default(video);
1284 if (retval)
1285 goto out;
1286 }
1287
1288 retval = dma_region_mmap(&video->dv_buf, file, vma);
1289out:
1290 mutex_unlock(&video->mtx);
1291 return retval;
1292}
1293
1294
1295
1296
1297static unsigned int dv1394_poll(struct file *file, struct poll_table_struct *wait)
1298{
1299 struct video_card *video = file_to_video_card(file);
1300 unsigned int mask = 0;
1301 unsigned long flags;
1302
1303 poll_wait(file, &video->waitq, wait);
1304
1305 spin_lock_irqsave(&video->spinlock, flags);
1306 if ( video->n_frames == 0 ) {
1307
1308 } else if ( video->active_frame == -1 ) {
1309
1310 mask |= POLLOUT;
1311 } else {
1312
1313 if (video->n_clear_frames >0)
1314 mask |= POLLOUT | POLLIN;
1315 }
1316 spin_unlock_irqrestore(&video->spinlock, flags);
1317
1318 return mask;
1319}
1320
1321static int dv1394_fasync(int fd, struct file *file, int on)
1322{
1323
1324
1325
1326 struct video_card *video = file_to_video_card(file);
1327
1328 return fasync_helper(fd, file, on, &video->fasync);
1329}
1330
1331static ssize_t dv1394_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1332{
1333 struct video_card *video = file_to_video_card(file);
1334 DECLARE_WAITQUEUE(wait, current);
1335 ssize_t ret;
1336 size_t cnt;
1337 unsigned long flags;
1338 int target_frame;
1339
1340
1341 if (file->f_flags & O_NONBLOCK) {
1342 if (!mutex_trylock(&video->mtx))
1343 return -EAGAIN;
1344 } else {
1345 if (mutex_lock_interruptible(&video->mtx))
1346 return -ERESTARTSYS;
1347 }
1348
1349 if ( !video_card_initialized(video) ) {
1350 ret = do_dv1394_init_default(video);
1351 if (ret) {
1352 mutex_unlock(&video->mtx);
1353 return ret;
1354 }
1355 }
1356
1357 ret = 0;
1358 add_wait_queue(&video->waitq, &wait);
1359
1360 while (count > 0) {
1361
1362
1363
1364
1365
1366 set_current_state(TASK_INTERRUPTIBLE);
1367
1368 spin_lock_irqsave(&video->spinlock, flags);
1369
1370 target_frame = video->first_clear_frame;
1371
1372 spin_unlock_irqrestore(&video->spinlock, flags);
1373
1374 if (video->frames[target_frame]->state == FRAME_CLEAR) {
1375
1376
1377 cnt = video->frame_size - (video->write_off - target_frame * video->frame_size);
1378
1379 } else {
1380
1381 cnt = 0;
1382 }
1383
1384 if (cnt > count)
1385 cnt = count;
1386
1387 if (cnt <= 0) {
1388
1389 if (file->f_flags & O_NONBLOCK) {
1390 if (!ret)
1391 ret = -EAGAIN;
1392 break;
1393 }
1394 if (signal_pending(current)) {
1395 if (!ret)
1396 ret = -ERESTARTSYS;
1397 break;
1398 }
1399
1400 schedule();
1401
1402 continue;
1403 }
1404
1405 if (copy_from_user(video->dv_buf.kvirt + video->write_off, buffer, cnt)) {
1406 if (!ret)
1407 ret = -EFAULT;
1408 break;
1409 }
1410
1411 video->write_off = (video->write_off + cnt) % (video->n_frames * video->frame_size);
1412
1413 count -= cnt;
1414 buffer += cnt;
1415 ret += cnt;
1416
1417 if (video->write_off == video->frame_size * ((target_frame + 1) % video->n_frames))
1418 frame_prepare(video, target_frame);
1419 }
1420
1421 remove_wait_queue(&video->waitq, &wait);
1422 set_current_state(TASK_RUNNING);
1423 mutex_unlock(&video->mtx);
1424 return ret;
1425}
1426
1427
1428static ssize_t dv1394_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1429{
1430 struct video_card *video = file_to_video_card(file);
1431 DECLARE_WAITQUEUE(wait, current);
1432 ssize_t ret;
1433 size_t cnt;
1434 unsigned long flags;
1435 int target_frame;
1436
1437
1438 if (file->f_flags & O_NONBLOCK) {
1439 if (!mutex_trylock(&video->mtx))
1440 return -EAGAIN;
1441 } else {
1442 if (mutex_lock_interruptible(&video->mtx))
1443 return -ERESTARTSYS;
1444 }
1445
1446 if ( !video_card_initialized(video) ) {
1447 ret = do_dv1394_init_default(video);
1448 if (ret) {
1449 mutex_unlock(&video->mtx);
1450 return ret;
1451 }
1452 video->continuity_counter = -1;
1453
1454 receive_packets(video);
1455
1456 start_dma_receive(video);
1457 }
1458
1459 ret = 0;
1460 add_wait_queue(&video->waitq, &wait);
1461
1462 while (count > 0) {
1463
1464
1465
1466
1467
1468 set_current_state(TASK_INTERRUPTIBLE);
1469
1470 spin_lock_irqsave(&video->spinlock, flags);
1471
1472 target_frame = video->first_clear_frame;
1473
1474 spin_unlock_irqrestore(&video->spinlock, flags);
1475
1476 if (target_frame >= 0 &&
1477 video->n_clear_frames > 0 &&
1478 video->frames[target_frame]->state == FRAME_CLEAR) {
1479
1480
1481 cnt = video->frame_size - (video->write_off - target_frame * video->frame_size);
1482
1483 } else {
1484
1485 cnt = 0;
1486 }
1487
1488 if (cnt > count)
1489 cnt = count;
1490
1491 if (cnt <= 0) {
1492
1493 if (file->f_flags & O_NONBLOCK) {
1494 if (!ret)
1495 ret = -EAGAIN;
1496 break;
1497 }
1498 if (signal_pending(current)) {
1499 if (!ret)
1500 ret = -ERESTARTSYS;
1501 break;
1502 }
1503
1504 schedule();
1505
1506 continue;
1507 }
1508
1509 if (copy_to_user(buffer, video->dv_buf.kvirt + video->write_off, cnt)) {
1510 if (!ret)
1511 ret = -EFAULT;
1512 break;
1513 }
1514
1515 video->write_off = (video->write_off + cnt) % (video->n_frames * video->frame_size);
1516
1517 count -= cnt;
1518 buffer += cnt;
1519 ret += cnt;
1520
1521 if (video->write_off == video->frame_size * ((target_frame + 1) % video->n_frames)) {
1522 spin_lock_irqsave(&video->spinlock, flags);
1523 video->n_clear_frames--;
1524 video->first_clear_frame = (video->first_clear_frame + 1) % video->n_frames;
1525 spin_unlock_irqrestore(&video->spinlock, flags);
1526 }
1527 }
1528
1529 remove_wait_queue(&video->waitq, &wait);
1530 set_current_state(TASK_RUNNING);
1531 mutex_unlock(&video->mtx);
1532 return ret;
1533}
1534
1535
1536
1537
1538static long dv1394_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1539{
1540 struct video_card *video = file_to_video_card(file);
1541 unsigned long flags;
1542 int ret = -EINVAL;
1543 void __user *argp = (void __user *)arg;
1544
1545 DECLARE_WAITQUEUE(wait, current);
1546
1547
1548 if (file->f_flags & O_NONBLOCK) {
1549 if (!mutex_trylock(&video->mtx))
1550 return -EAGAIN;
1551 } else {
1552 if (mutex_lock_interruptible(&video->mtx))
1553 return -ERESTARTSYS;
1554 }
1555
1556 switch(cmd)
1557 {
1558 case DV1394_IOC_SUBMIT_FRAMES: {
1559 unsigned int n_submit;
1560
1561 if ( !video_card_initialized(video) ) {
1562 ret = do_dv1394_init_default(video);
1563 if (ret)
1564 goto out;
1565 }
1566
1567 n_submit = (unsigned int) arg;
1568
1569 if (n_submit > video->n_frames) {
1570 ret = -EINVAL;
1571 goto out;
1572 }
1573
1574 while (n_submit > 0) {
1575
1576 add_wait_queue(&video->waitq, &wait);
1577 set_current_state(TASK_INTERRUPTIBLE);
1578
1579 spin_lock_irqsave(&video->spinlock, flags);
1580
1581
1582 while (video->frames[video->first_clear_frame]->state != FRAME_CLEAR) {
1583
1584 spin_unlock_irqrestore(&video->spinlock, flags);
1585
1586 if (signal_pending(current)) {
1587 remove_wait_queue(&video->waitq, &wait);
1588 set_current_state(TASK_RUNNING);
1589 ret = -EINTR;
1590 goto out;
1591 }
1592
1593 schedule();
1594 set_current_state(TASK_INTERRUPTIBLE);
1595
1596 spin_lock_irqsave(&video->spinlock, flags);
1597 }
1598 spin_unlock_irqrestore(&video->spinlock, flags);
1599
1600 remove_wait_queue(&video->waitq, &wait);
1601 set_current_state(TASK_RUNNING);
1602
1603 frame_prepare(video, video->first_clear_frame);
1604
1605 n_submit--;
1606 }
1607
1608 ret = 0;
1609 break;
1610 }
1611
1612 case DV1394_IOC_WAIT_FRAMES: {
1613 unsigned int n_wait;
1614
1615 if ( !video_card_initialized(video) ) {
1616 ret = -EINVAL;
1617 goto out;
1618 }
1619
1620 n_wait = (unsigned int) arg;
1621
1622
1623
1624
1625
1626 if (n_wait > (video->n_frames-1) ) {
1627 ret = -EINVAL;
1628 goto out;
1629 }
1630
1631 add_wait_queue(&video->waitq, &wait);
1632 set_current_state(TASK_INTERRUPTIBLE);
1633
1634 spin_lock_irqsave(&video->spinlock, flags);
1635
1636 while (video->n_clear_frames < n_wait) {
1637
1638 spin_unlock_irqrestore(&video->spinlock, flags);
1639
1640 if (signal_pending(current)) {
1641 remove_wait_queue(&video->waitq, &wait);
1642 set_current_state(TASK_RUNNING);
1643 ret = -EINTR;
1644 goto out;
1645 }
1646
1647 schedule();
1648 set_current_state(TASK_INTERRUPTIBLE);
1649
1650 spin_lock_irqsave(&video->spinlock, flags);
1651 }
1652
1653 spin_unlock_irqrestore(&video->spinlock, flags);
1654
1655 remove_wait_queue(&video->waitq, &wait);
1656 set_current_state(TASK_RUNNING);
1657 ret = 0;
1658 break;
1659 }
1660
1661 case DV1394_IOC_RECEIVE_FRAMES: {
1662 unsigned int n_recv;
1663
1664 if ( !video_card_initialized(video) ) {
1665 ret = -EINVAL;
1666 goto out;
1667 }
1668
1669 n_recv = (unsigned int) arg;
1670
1671
1672 if (n_recv > (video->n_frames-1) ) {
1673 ret = -EINVAL;
1674 goto out;
1675 }
1676
1677 spin_lock_irqsave(&video->spinlock, flags);
1678
1679
1680 video->n_clear_frames -= n_recv;
1681
1682
1683 video->first_clear_frame = (video->first_clear_frame + n_recv) % video->n_frames;
1684
1685
1686 video->dropped_frames = 0;
1687
1688 spin_unlock_irqrestore(&video->spinlock, flags);
1689
1690 ret = 0;
1691 break;
1692 }
1693
1694 case DV1394_IOC_START_RECEIVE: {
1695 if ( !video_card_initialized(video) ) {
1696 ret = do_dv1394_init_default(video);
1697 if (ret)
1698 goto out;
1699 }
1700
1701 video->continuity_counter = -1;
1702
1703 receive_packets(video);
1704
1705 start_dma_receive(video);
1706
1707 ret = 0;
1708 break;
1709 }
1710
1711 case DV1394_IOC_INIT: {
1712 struct dv1394_init init;
1713 if (!argp) {
1714 ret = do_dv1394_init_default(video);
1715 } else {
1716 if (copy_from_user(&init, argp, sizeof(init))) {
1717 ret = -EFAULT;
1718 goto out;
1719 }
1720 ret = do_dv1394_init(video, &init);
1721 }
1722 break;
1723 }
1724
1725 case DV1394_IOC_SHUTDOWN:
1726 do_dv1394_shutdown(video, 0);
1727 ret = 0;
1728 break;
1729
1730
1731 case DV1394_IOC_GET_STATUS: {
1732 struct dv1394_status status;
1733
1734 if ( !video_card_initialized(video) ) {
1735 ret = -EINVAL;
1736 goto out;
1737 }
1738
1739 status.init.api_version = DV1394_API_VERSION;
1740 status.init.channel = video->channel;
1741 status.init.n_frames = video->n_frames;
1742 status.init.format = video->pal_or_ntsc;
1743 status.init.cip_n = video->cip_n;
1744 status.init.cip_d = video->cip_d;
1745 status.init.syt_offset = video->syt_offset;
1746
1747 status.first_clear_frame = video->first_clear_frame;
1748
1749
1750 spin_lock_irqsave(&video->spinlock, flags);
1751
1752 status.active_frame = video->active_frame;
1753 status.n_clear_frames = video->n_clear_frames;
1754
1755 status.dropped_frames = video->dropped_frames;
1756
1757
1758 video->dropped_frames = 0;
1759
1760 spin_unlock_irqrestore(&video->spinlock, flags);
1761
1762 if (copy_to_user(argp, &status, sizeof(status))) {
1763 ret = -EFAULT;
1764 goto out;
1765 }
1766
1767 ret = 0;
1768 break;
1769 }
1770
1771 default:
1772 break;
1773 }
1774
1775 out:
1776 mutex_unlock(&video->mtx);
1777 return ret;
1778}
1779
1780
1781
1782static int dv1394_open(struct inode *inode, struct file *file)
1783{
1784 struct video_card *video = NULL;
1785
1786 if (file->private_data) {
1787 video = (struct video_card*) file->private_data;
1788
1789 } else {
1790
1791 unsigned long flags;
1792 int idx = ieee1394_file_to_instance(file);
1793
1794 spin_lock_irqsave(&dv1394_cards_lock, flags);
1795 if (!list_empty(&dv1394_cards)) {
1796 struct video_card *p;
1797 list_for_each_entry(p, &dv1394_cards, list) {
1798 if ((p->id) == idx) {
1799 video = p;
1800 break;
1801 }
1802 }
1803 }
1804 spin_unlock_irqrestore(&dv1394_cards_lock, flags);
1805
1806 if (!video) {
1807 debug_printk("dv1394: OHCI card %d not found", idx);
1808 return -ENODEV;
1809 }
1810
1811 file->private_data = (void*) video;
1812 }
1813
1814#ifndef DV1394_ALLOW_MORE_THAN_ONE_OPEN
1815
1816 if ( test_and_set_bit(0, &video->open) ) {
1817
1818 return -EBUSY;
1819 }
1820
1821#endif
1822
1823 printk(KERN_INFO "%s: NOTE, the dv1394 interface is unsupported "
1824 "and will not be available in the new firewire driver stack. "
1825 "Try libraw1394 based programs instead.\n", current->comm);
1826
1827 return 0;
1828}
1829
1830
1831static int dv1394_release(struct inode *inode, struct file *file)
1832{
1833 struct video_card *video = file_to_video_card(file);
1834
1835
1836 do_dv1394_shutdown(video, 1);
1837
1838
1839 clear_bit(0, &video->open);
1840
1841 return 0;
1842}
1843
1844
1845
1846
1847static void it_tasklet_func(unsigned long data)
1848{
1849 int wake = 0;
1850 struct video_card *video = (struct video_card*) data;
1851
1852 spin_lock(&video->spinlock);
1853
1854 if (!video->dma_running)
1855 goto out;
1856
1857 irq_printk("ContextControl = %08x, CommandPtr = %08x\n",
1858 reg_read(video->ohci, video->ohci_IsoXmitContextControlSet),
1859 reg_read(video->ohci, video->ohci_IsoXmitCommandPtr)
1860 );
1861
1862
1863 if ( (video->ohci_it_ctx != -1) &&
1864 (reg_read(video->ohci, video->ohci_IsoXmitContextControlSet) & (1 << 10)) ) {
1865
1866 struct frame *f;
1867 unsigned int frame, i;
1868
1869
1870 if (video->active_frame == -1)
1871 frame = 0;
1872 else
1873 frame = video->active_frame;
1874
1875
1876 for (i = 0; i < video->n_frames; i++, frame = (frame+1) % video->n_frames) {
1877
1878 irq_printk("IRQ checking frame %d...", frame);
1879 f = video->frames[frame];
1880 if (f->state != FRAME_READY) {
1881 irq_printk("clear, skipping\n");
1882
1883 continue;
1884 }
1885
1886 irq_printk("DMA\n");
1887
1888
1889 if ( *(f->frame_begin_timestamp) ) {
1890 int prev_frame;
1891 struct frame *prev_f;
1892
1893
1894
1895
1896 irq_printk(" BEGIN\n");
1897
1898 prev_frame = frame - 1;
1899 if (prev_frame == -1)
1900 prev_frame += video->n_frames;
1901 prev_f = video->frames[prev_frame];
1902
1903
1904
1905 if ( (prev_f->state == FRAME_READY) &&
1906 prev_f->done && (!f->done) )
1907 {
1908 frame_reset(prev_f);
1909 video->n_clear_frames++;
1910 wake = 1;
1911 video->active_frame = frame;
1912
1913 irq_printk(" BEGIN - freeing previous frame %d, new active frame is %d\n", prev_frame, frame);
1914 } else {
1915 irq_printk(" BEGIN - can't free yet\n");
1916 }
1917
1918 f->done = 1;
1919 }
1920
1921
1922
1923 if ( *(f->mid_frame_timestamp) ) {
1924 struct frame *next_frame;
1925 u32 begin_ts, ts_cyc, ts_off;
1926
1927 *(f->mid_frame_timestamp) = 0;
1928
1929 begin_ts = le32_to_cpu(*(f->frame_begin_timestamp));
1930
1931 irq_printk(" MIDDLE - first packet was sent at cycle %4u (%2u), assigned timestamp was (%2u) %4u\n",
1932 begin_ts & 0x1FFF, begin_ts & 0xF,
1933 f->assigned_timestamp >> 12, f->assigned_timestamp & 0xFFF);
1934
1935
1936 next_frame = video->frames[ (frame+1) % video->n_frames ];
1937
1938 if (next_frame->state == FRAME_READY) {
1939 irq_printk(" MIDDLE - next frame is ready, good\n");
1940 } else {
1941 debug_printk("dv1394: Underflow! At least one frame has been dropped.\n");
1942 next_frame = f;
1943 }
1944
1945
1946
1947 ts_cyc = begin_ts & 0xF;
1948
1949 ts_cyc += f->n_packets + video->syt_offset ;
1950
1951 ts_off = 0;
1952
1953 ts_cyc += ts_off/3072;
1954 ts_off %= 3072;
1955
1956 next_frame->assigned_timestamp = ((ts_cyc&0xF) << 12) + ts_off;
1957 if (next_frame->cip_syt1) {
1958 next_frame->cip_syt1->b[6] = next_frame->assigned_timestamp >> 8;
1959 next_frame->cip_syt1->b[7] = next_frame->assigned_timestamp & 0xFF;
1960 }
1961 if (next_frame->cip_syt2) {
1962 next_frame->cip_syt2->b[6] = next_frame->assigned_timestamp >> 8;
1963 next_frame->cip_syt2->b[7] = next_frame->assigned_timestamp & 0xFF;
1964 }
1965
1966 }
1967
1968
1969 if ( *(f->frame_end_timestamp) ) {
1970
1971 *(f->frame_end_timestamp) = 0;
1972
1973 debug_printk(" END - the frame looped at least once\n");
1974
1975 video->dropped_frames++;
1976 }
1977
1978 }
1979 }
1980
1981 if (wake) {
1982 kill_fasync(&video->fasync, SIGIO, POLL_OUT);
1983
1984
1985 wake_up_interruptible(&video->waitq);
1986 }
1987
1988out:
1989 spin_unlock(&video->spinlock);
1990}
1991
1992static void ir_tasklet_func(unsigned long data)
1993{
1994 int wake = 0;
1995 struct video_card *video = (struct video_card*) data;
1996
1997 spin_lock(&video->spinlock);
1998
1999 if (!video->dma_running)
2000 goto out;
2001
2002 if ( (video->ohci_ir_ctx != -1) &&
2003 (reg_read(video->ohci, video->ohci_IsoRcvContextControlSet) & (1 << 10)) ) {
2004
2005 int sof=0;
2006 struct frame *f;
2007 u16 packet_length, packet_time;
2008 int i, dbc=0;
2009 struct DMA_descriptor_block *block = NULL;
2010 u16 xferstatus;
2011
2012 int next_i, prev_i;
2013 struct DMA_descriptor_block *next = NULL;
2014 dma_addr_t next_dma = 0;
2015 struct DMA_descriptor_block *prev = NULL;
2016
2017
2018 for (i = 0; i < video->n_frames*MAX_PACKETS; i++) {
2019 struct packet *p = dma_region_i(&video->packet_buf, struct packet, video->current_packet);
2020
2021
2022 dma_region_sync_for_cpu(&video->packet_buf,
2023 (unsigned long) p - (unsigned long) video->packet_buf.kvirt,
2024 sizeof(struct packet));
2025
2026 packet_length = le16_to_cpu(p->data_length);
2027 packet_time = le16_to_cpu(p->timestamp);
2028
2029 irq_printk("received packet %02d, timestamp=%04x, length=%04x, sof=%02x%02x\n", video->current_packet,
2030 packet_time, packet_length,
2031 p->data[0], p->data[1]);
2032
2033
2034 f = video->frames[video->current_packet / MAX_PACKETS];
2035 block = &(f->descriptor_pool[video->current_packet % MAX_PACKETS]);
2036 xferstatus = le32_to_cpu(block->u.in.il.q[3]) >> 16;
2037 xferstatus &= 0x1F;
2038 irq_printk("ir_tasklet_func: xferStatus/resCount [%d] = 0x%08x\n", i, le32_to_cpu(block->u.in.il.q[3]) );
2039
2040
2041 f = video->frames[video->active_frame];
2042
2043
2044 if (packet_length > 8 && xferstatus == 0x11) {
2045
2046
2047
2048 sof = ( (p->data[0] >> 5) == 0 && (p->data[1] >> 4) == 0);
2049
2050 dbc = (int) (p->cip_h1 >> 24);
2051 if ( video->continuity_counter != -1 && dbc > ((video->continuity_counter + 1) % 256) )
2052 {
2053 printk(KERN_WARNING "dv1394: discontinuity detected, dropping all frames\n" );
2054 video->dropped_frames += video->n_clear_frames + 1;
2055 video->first_frame = 0;
2056 video->n_clear_frames = 0;
2057 video->first_clear_frame = -1;
2058 }
2059 video->continuity_counter = dbc;
2060
2061 if (!video->first_frame) {
2062 if (sof) {
2063 video->first_frame = 1;
2064 }
2065
2066 } else if (sof) {
2067
2068 frame_reset(f);
2069 video->n_clear_frames++;
2070 if (video->n_clear_frames > video->n_frames) {
2071 video->dropped_frames++;
2072 printk(KERN_WARNING "dv1394: dropped a frame during reception\n" );
2073 video->n_clear_frames = video->n_frames-1;
2074 video->first_clear_frame = (video->first_clear_frame + 1) % video->n_frames;
2075 }
2076 if (video->first_clear_frame == -1)
2077 video->first_clear_frame = video->active_frame;
2078
2079
2080 video->active_frame = (video->active_frame + 1) % video->n_frames;
2081 f = video->frames[video->active_frame];
2082 irq_printk(" frame received, active_frame = %d, n_clear_frames = %d, first_clear_frame = %d\n",
2083 video->active_frame, video->n_clear_frames, video->first_clear_frame);
2084 }
2085 if (video->first_frame) {
2086 if (sof) {
2087
2088 f->state = FRAME_READY;
2089 }
2090
2091
2092 if (f->n_packets > (video->frame_size / 480)) {
2093 printk(KERN_ERR "frame buffer overflow during receive\n");
2094 }
2095
2096 frame_put_packet(f, p);
2097
2098 }
2099 }
2100
2101
2102 else if (xferstatus == 0) {
2103 break;
2104 }
2105
2106
2107 block->u.in.il.q[3] = cpu_to_le32(512);
2108
2109
2110 next_i = video->current_packet;
2111 f = video->frames[next_i / MAX_PACKETS];
2112 next = &(f->descriptor_pool[next_i % MAX_PACKETS]);
2113 next_dma = ((unsigned long) block - (unsigned long) f->descriptor_pool) + f->descriptor_pool_dma;
2114 next->u.in.il.q[0] |= cpu_to_le32(3 << 20);
2115 next->u.in.il.q[2] = cpu_to_le32(0);
2116
2117
2118 prev_i = (next_i == 0) ? (MAX_PACKETS * video->n_frames - 1) : (next_i - 1);
2119 f = video->frames[prev_i / MAX_PACKETS];
2120 prev = &(f->descriptor_pool[prev_i % MAX_PACKETS]);
2121 if (prev_i % (MAX_PACKETS/2)) {
2122 prev->u.in.il.q[0] &= ~cpu_to_le32(3 << 20);
2123 } else {
2124 prev->u.in.il.q[0] |= cpu_to_le32(3 << 20);
2125 }
2126 prev->u.in.il.q[2] = cpu_to_le32(next_dma | 1);
2127 wmb();
2128
2129
2130 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, (1 << 12));
2131
2132
2133 video->current_packet = (video->current_packet + 1) % (MAX_PACKETS * video->n_frames);
2134
2135 }
2136
2137 wake = 1;
2138
2139 }
2140
2141 if (wake) {
2142 kill_fasync(&video->fasync, SIGIO, POLL_IN);
2143
2144
2145 wake_up_interruptible(&video->waitq);
2146 }
2147
2148out:
2149 spin_unlock(&video->spinlock);
2150}
2151
2152static struct cdev dv1394_cdev;
2153static const struct file_operations dv1394_fops=
2154{
2155 .owner = THIS_MODULE,
2156 .poll = dv1394_poll,
2157 .unlocked_ioctl = dv1394_ioctl,
2158#ifdef CONFIG_COMPAT
2159 .compat_ioctl = dv1394_compat_ioctl,
2160#endif
2161 .mmap = dv1394_mmap,
2162 .open = dv1394_open,
2163 .write = dv1394_write,
2164 .read = dv1394_read,
2165 .release = dv1394_release,
2166 .fasync = dv1394_fasync,
2167};
2168
2169
2170
2171
2172
2173
2174#ifdef MODULE
2175static const struct ieee1394_device_id dv1394_id_table[] = {
2176 {
2177 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2178 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY & 0xffffff,
2179 .version = AVC_SW_VERSION_ENTRY & 0xffffff
2180 },
2181 { }
2182};
2183
2184MODULE_DEVICE_TABLE(ieee1394, dv1394_id_table);
2185#endif
2186
2187static struct hpsb_protocol_driver dv1394_driver = {
2188 .name = "dv1394",
2189};
2190
2191
2192
2193
2194static int dv1394_init(struct ti_ohci *ohci, enum pal_or_ntsc format, enum modes mode)
2195{
2196 struct video_card *video;
2197 unsigned long flags;
2198 int i;
2199
2200 video = kzalloc(sizeof(*video), GFP_KERNEL);
2201 if (!video) {
2202 printk(KERN_ERR "dv1394: cannot allocate video_card\n");
2203 return -1;
2204 }
2205
2206 video->ohci = ohci;
2207
2208
2209 video->id = ohci->host->id << 2;
2210 if (format == DV1394_NTSC)
2211 video->id |= mode;
2212 else
2213 video->id |= 2 + mode;
2214
2215 video->ohci_it_ctx = -1;
2216 video->ohci_ir_ctx = -1;
2217
2218 video->ohci_IsoXmitContextControlSet = 0;
2219 video->ohci_IsoXmitContextControlClear = 0;
2220 video->ohci_IsoXmitCommandPtr = 0;
2221
2222 video->ohci_IsoRcvContextControlSet = 0;
2223 video->ohci_IsoRcvContextControlClear = 0;
2224 video->ohci_IsoRcvCommandPtr = 0;
2225 video->ohci_IsoRcvContextMatch = 0;
2226
2227 video->n_frames = 0;
2228 video->channel = 63;
2229 video->active_frame = -1;
2230
2231
2232 video->pal_or_ntsc = format;
2233 video->cip_n = 0;
2234 video->cip_d = 0;
2235 video->syt_offset = 0;
2236 video->mode = mode;
2237
2238 for (i = 0; i < DV1394_MAX_FRAMES; i++)
2239 video->frames[i] = NULL;
2240
2241 dma_region_init(&video->dv_buf);
2242 video->dv_buf_size = 0;
2243 dma_region_init(&video->packet_buf);
2244 video->packet_buf_size = 0;
2245
2246 clear_bit(0, &video->open);
2247 spin_lock_init(&video->spinlock);
2248 video->dma_running = 0;
2249 mutex_init(&video->mtx);
2250 init_waitqueue_head(&video->waitq);
2251 video->fasync = NULL;
2252
2253 spin_lock_irqsave(&dv1394_cards_lock, flags);
2254 INIT_LIST_HEAD(&video->list);
2255 list_add_tail(&video->list, &dv1394_cards);
2256 spin_unlock_irqrestore(&dv1394_cards_lock, flags);
2257
2258 debug_printk("dv1394: dv1394_init() OK on ID %d\n", video->id);
2259 return 0;
2260}
2261
2262static void dv1394_remove_host(struct hpsb_host *host)
2263{
2264 struct video_card *video, *tmp_video;
2265 unsigned long flags;
2266 int found_ohci_card = 0;
2267
2268 do {
2269 video = NULL;
2270 spin_lock_irqsave(&dv1394_cards_lock, flags);
2271 list_for_each_entry(tmp_video, &dv1394_cards, list) {
2272 if ((tmp_video->id >> 2) == host->id) {
2273 list_del(&tmp_video->list);
2274 video = tmp_video;
2275 found_ohci_card = 1;
2276 break;
2277 }
2278 }
2279 spin_unlock_irqrestore(&dv1394_cards_lock, flags);
2280
2281 if (video) {
2282 do_dv1394_shutdown(video, 1);
2283 kfree(video);
2284 }
2285 } while (video);
2286
2287 if (found_ohci_card)
2288 device_destroy(hpsb_protocol_class, MKDEV(IEEE1394_MAJOR,
2289 IEEE1394_MINOR_BLOCK_DV1394 * 16 + (host->id << 2)));
2290}
2291
2292static void dv1394_add_host(struct hpsb_host *host)
2293{
2294 struct ti_ohci *ohci;
2295 int id = host->id;
2296
2297
2298 if (strcmp(host->driver->name, OHCI1394_DRIVER_NAME))
2299 return;
2300
2301 ohci = (struct ti_ohci *)host->hostdata;
2302
2303 device_create(hpsb_protocol_class, NULL,
2304 MKDEV(IEEE1394_MAJOR,
2305 IEEE1394_MINOR_BLOCK_DV1394 * 16 + (id<<2)),
2306 NULL, "dv1394-%d", id);
2307
2308 dv1394_init(ohci, DV1394_NTSC, MODE_RECEIVE);
2309 dv1394_init(ohci, DV1394_NTSC, MODE_TRANSMIT);
2310 dv1394_init(ohci, DV1394_PAL, MODE_RECEIVE);
2311 dv1394_init(ohci, DV1394_PAL, MODE_TRANSMIT);
2312}
2313
2314
2315
2316
2317
2318
2319
2320static void dv1394_host_reset(struct hpsb_host *host)
2321{
2322 struct ti_ohci *ohci;
2323 struct video_card *video = NULL, *tmp_vid;
2324 unsigned long flags;
2325
2326
2327 if (strcmp(host->driver->name, OHCI1394_DRIVER_NAME))
2328 return;
2329
2330 ohci = (struct ti_ohci *)host->hostdata;
2331
2332
2333
2334 spin_lock_irqsave(&dv1394_cards_lock, flags);
2335 list_for_each_entry(tmp_vid, &dv1394_cards, list) {
2336 if ((tmp_vid->id >> 2) == host->id) {
2337 video = tmp_vid;
2338 break;
2339 }
2340 }
2341 spin_unlock_irqrestore(&dv1394_cards_lock, flags);
2342
2343 if (!video)
2344 return;
2345
2346
2347 spin_lock_irqsave(&video->spinlock, flags);
2348
2349 if (!video->dma_running)
2350 goto out;
2351
2352
2353 if (video->ohci_it_ctx != -1) {
2354 u32 ctx;
2355
2356 ctx = reg_read(video->ohci, video->ohci_IsoXmitContextControlSet);
2357
2358
2359 if ( (ctx & (1<<15)) &&
2360 !(ctx & (1<<10)) ) {
2361
2362 debug_printk("dv1394: IT context stopped due to bus reset; waking it up\n");
2363
2364
2365
2366 video->dropped_frames++;
2367
2368
2369
2370
2371 reg_write(video->ohci, video->ohci_IsoXmitContextControlClear, (1 << 15));
2372 flush_pci_write(video->ohci);
2373
2374
2375 reg_write(video->ohci, video->ohci_IsoXmitContextControlSet, (1 << 15));
2376 flush_pci_write(video->ohci);
2377
2378
2379 reg_write(video->ohci, video->ohci_IsoXmitContextControlSet, (1 << 12));
2380 flush_pci_write(video->ohci);
2381
2382 irq_printk("dv1394: AFTER IT restart ctx 0x%08x ptr 0x%08x\n",
2383 reg_read(video->ohci, video->ohci_IsoXmitContextControlSet),
2384 reg_read(video->ohci, video->ohci_IsoXmitCommandPtr));
2385 }
2386 }
2387
2388
2389 if (video->ohci_ir_ctx != -1) {
2390 u32 ctx;
2391
2392 ctx = reg_read(video->ohci, video->ohci_IsoRcvContextControlSet);
2393
2394
2395 if ( (ctx & (1<<15)) &&
2396 !(ctx & (1<<10)) ) {
2397
2398 debug_printk("dv1394: IR context stopped due to bus reset; waking it up\n");
2399
2400
2401
2402 video->dropped_frames++;
2403
2404
2405
2406
2407
2408 reg_write(video->ohci, video->ohci_IsoRcvContextControlClear, (1 << 15));
2409 flush_pci_write(video->ohci);
2410
2411
2412 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, (1 << 15));
2413 flush_pci_write(video->ohci);
2414
2415
2416 reg_write(video->ohci, video->ohci_IsoRcvContextControlSet, (1 << 12));
2417 flush_pci_write(video->ohci);
2418
2419 irq_printk("dv1394: AFTER IR restart ctx 0x%08x ptr 0x%08x\n",
2420 reg_read(video->ohci, video->ohci_IsoRcvContextControlSet),
2421 reg_read(video->ohci, video->ohci_IsoRcvCommandPtr));
2422 }
2423 }
2424
2425out:
2426 spin_unlock_irqrestore(&video->spinlock, flags);
2427
2428
2429 wake_up_interruptible(&video->waitq);
2430}
2431
2432static struct hpsb_highlevel dv1394_highlevel = {
2433 .name = "dv1394",
2434 .add_host = dv1394_add_host,
2435 .remove_host = dv1394_remove_host,
2436 .host_reset = dv1394_host_reset,
2437};
2438
2439#ifdef CONFIG_COMPAT
2440
2441#define DV1394_IOC32_INIT _IOW('#', 0x06, struct dv1394_init32)
2442#define DV1394_IOC32_GET_STATUS _IOR('#', 0x0c, struct dv1394_status32)
2443
2444struct dv1394_init32 {
2445 u32 api_version;
2446 u32 channel;
2447 u32 n_frames;
2448 u32 format;
2449 u32 cip_n;
2450 u32 cip_d;
2451 u32 syt_offset;
2452};
2453
2454struct dv1394_status32 {
2455 struct dv1394_init32 init;
2456 s32 active_frame;
2457 u32 first_clear_frame;
2458 u32 n_clear_frames;
2459 u32 dropped_frames;
2460};
2461
2462
2463
2464static int handle_dv1394_init(struct file *file, unsigned int cmd, unsigned long arg)
2465{
2466 struct dv1394_init32 dv32;
2467 struct dv1394_init dv;
2468 mm_segment_t old_fs;
2469 int ret;
2470
2471 if (file->f_op->unlocked_ioctl != dv1394_ioctl)
2472 return -EFAULT;
2473
2474 if (copy_from_user(&dv32, (void __user *)arg, sizeof(dv32)))
2475 return -EFAULT;
2476
2477 dv.api_version = dv32.api_version;
2478 dv.channel = dv32.channel;
2479 dv.n_frames = dv32.n_frames;
2480 dv.format = dv32.format;
2481 dv.cip_n = (unsigned long)dv32.cip_n;
2482 dv.cip_d = (unsigned long)dv32.cip_d;
2483 dv.syt_offset = dv32.syt_offset;
2484
2485 old_fs = get_fs();
2486 set_fs(KERNEL_DS);
2487 ret = dv1394_ioctl(file, DV1394_IOC_INIT, (unsigned long)&dv);
2488 set_fs(old_fs);
2489
2490 return ret;
2491}
2492
2493static int handle_dv1394_get_status(struct file *file, unsigned int cmd, unsigned long arg)
2494{
2495 struct dv1394_status32 dv32;
2496 struct dv1394_status dv;
2497 mm_segment_t old_fs;
2498 int ret;
2499
2500 if (file->f_op->unlocked_ioctl != dv1394_ioctl)
2501 return -EFAULT;
2502
2503 old_fs = get_fs();
2504 set_fs(KERNEL_DS);
2505 ret = dv1394_ioctl(file, DV1394_IOC_GET_STATUS, (unsigned long)&dv);
2506 set_fs(old_fs);
2507
2508 if (!ret) {
2509 dv32.init.api_version = dv.init.api_version;
2510 dv32.init.channel = dv.init.channel;
2511 dv32.init.n_frames = dv.init.n_frames;
2512 dv32.init.format = dv.init.format;
2513 dv32.init.cip_n = (u32)dv.init.cip_n;
2514 dv32.init.cip_d = (u32)dv.init.cip_d;
2515 dv32.init.syt_offset = dv.init.syt_offset;
2516 dv32.active_frame = dv.active_frame;
2517 dv32.first_clear_frame = dv.first_clear_frame;
2518 dv32.n_clear_frames = dv.n_clear_frames;
2519 dv32.dropped_frames = dv.dropped_frames;
2520
2521 if (copy_to_user((struct dv1394_status32 __user *)arg, &dv32, sizeof(dv32)))
2522 ret = -EFAULT;
2523 }
2524
2525 return ret;
2526}
2527
2528
2529
2530static long dv1394_compat_ioctl(struct file *file, unsigned int cmd,
2531 unsigned long arg)
2532{
2533 switch (cmd) {
2534 case DV1394_IOC_SHUTDOWN:
2535 case DV1394_IOC_SUBMIT_FRAMES:
2536 case DV1394_IOC_WAIT_FRAMES:
2537 case DV1394_IOC_RECEIVE_FRAMES:
2538 case DV1394_IOC_START_RECEIVE:
2539 return dv1394_ioctl(file, cmd, arg);
2540
2541 case DV1394_IOC32_INIT:
2542 return handle_dv1394_init(file, cmd, arg);
2543 case DV1394_IOC32_GET_STATUS:
2544 return handle_dv1394_get_status(file, cmd, arg);
2545 default:
2546 return -ENOIOCTLCMD;
2547 }
2548}
2549
2550#endif
2551
2552
2553
2554
2555MODULE_AUTHOR("Dan Maas <dmaas@dcine.com>, Dan Dennedy <dan@dennedy.org>");
2556MODULE_DESCRIPTION("driver for DV input/output on OHCI board");
2557MODULE_SUPPORTED_DEVICE("dv1394");
2558MODULE_LICENSE("GPL");
2559
2560static void __exit dv1394_exit_module(void)
2561{
2562 hpsb_unregister_protocol(&dv1394_driver);
2563 hpsb_unregister_highlevel(&dv1394_highlevel);
2564 cdev_del(&dv1394_cdev);
2565}
2566
2567static int __init dv1394_init_module(void)
2568{
2569 int ret;
2570
2571 cdev_init(&dv1394_cdev, &dv1394_fops);
2572 dv1394_cdev.owner = THIS_MODULE;
2573 ret = cdev_add(&dv1394_cdev, IEEE1394_DV1394_DEV, 16);
2574 if (ret) {
2575 printk(KERN_ERR "dv1394: unable to register character device\n");
2576 return ret;
2577 }
2578
2579 hpsb_register_highlevel(&dv1394_highlevel);
2580
2581 ret = hpsb_register_protocol(&dv1394_driver);
2582 if (ret) {
2583 printk(KERN_ERR "dv1394: failed to register protocol\n");
2584 hpsb_unregister_highlevel(&dv1394_highlevel);
2585 cdev_del(&dv1394_cdev);
2586 return ret;
2587 }
2588
2589 return 0;
2590}
2591
2592module_init(dv1394_init_module);
2593module_exit(dv1394_exit_module);
2594