forked from aja-video/ntv2-v4l2
/
ntv2_device.c
853 lines (723 loc) · 23.7 KB
/
ntv2_device.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*
* NTV2 device interface
*
* Copyright 2016 AJA Video Systems Inc. All rights reserved.
*
* This program is free software; you may redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "ntv2_device.h"
#include "ntv2_video.h"
#include "ntv2_audio.h"
#include "ntv2_serial.h"
#include "ntv2_channel.h"
#include "ntv2_register.h"
#include "ntv2_pci.h"
#include "ntv2_konareg.h"
#include "ntv2_features.h"
#include "ntv2_input.h"
#include "ntv2_chrdev.h"
static int ntv2_device_pci_configure(struct ntv2_device *ntv2_dev, struct pci_dev *pdev);
static void ntv2_device_pci_release(struct ntv2_device *ntv2_dev);
static int ntv2_device_dma_configure(struct ntv2_device *ntv2_dev);
static void ntv2_device_dma_release(struct ntv2_device *ntv2_dev);
static int ntv2_device_irq_configure(struct ntv2_device *ntv2_dev);
static void ntv2_device_irq_release(struct ntv2_device *ntv2_dev);
static irqreturn_t ntv2_device_interrupt(int irq, void* dev_id);
static void ntv2_device_init_hardware(struct ntv2_device *ntv2_dev);
static void ntv2_device_monitor(unsigned long data);
/*
* Initialize device info
*/
struct ntv2_device *ntv2_device_open(struct ntv2_module *ntv2_mod,
const char *name, int index)
{
struct ntv2_device *ntv2_dev = NULL;
ntv2_dev = kzalloc(sizeof(struct ntv2_device), GFP_KERNEL);
if (ntv2_dev == NULL) {
NTV2_MSG_ERROR("%s: ntv2_device instance memory allocation failed\n", ntv2_mod->name);
return NULL;
}
ntv2_dev->index = index;
snprintf(ntv2_dev->name, NTV2_STRING_SIZE, "%s-%s%d", ntv2_mod->name, name, index);
INIT_LIST_HEAD(&ntv2_dev->list);
ntv2_dev->ntv2_dev = ntv2_dev;
ntv2_dev->ntv2_mod = ntv2_mod;
/* video list */
INIT_LIST_HEAD(&ntv2_dev->video_list);
spin_lock_init(&ntv2_dev->video_lock);
atomic_set(&ntv2_dev->video_index, 0);
/* audio list */
INIT_LIST_HEAD(&ntv2_dev->audio_list);
spin_lock_init(&ntv2_dev->audio_lock);
atomic_set(&ntv2_dev->audio_index, 0);
/* serial list */
INIT_LIST_HEAD(&ntv2_dev->serial_list);
spin_lock_init(&ntv2_dev->serial_lock);
atomic_set(&ntv2_dev->serial_index, 0);
/* channel list */
INIT_LIST_HEAD(&ntv2_dev->channel_list);
spin_lock_init(&ntv2_dev->channel_lock);
atomic_set(&ntv2_dev->channel_index, 0);
NTV2_MSG_DEVICE_INFO("%s: open ntv2_device\n", ntv2_dev->name);
return ntv2_dev;
}
/*
* Release device info
*/
void ntv2_device_close(struct ntv2_device *ntv2_dev)
{
struct list_head *ptr;
struct list_head *next;
struct ntv2_serial *ser;
struct ntv2_audio *aud;
struct ntv2_video *vid;
struct ntv2_channel *chn;
unsigned long flags;
if (ntv2_dev == NULL)
return;
NTV2_MSG_DEVICE_INFO("%s: close ntv2_device\n", ntv2_dev->name);
ntv2_pci_disable(ntv2_dev->pci_dma);
/* delete all serial objects */
list_for_each_safe(ptr, next, &ntv2_dev->serial_list) {
ser = list_entry(ptr, struct ntv2_serial, list);
spin_lock_irqsave(&ntv2_dev->serial_lock, flags);
list_del_init(&ser->list);
spin_unlock_irqrestore(&ntv2_dev->serial_lock, flags);
ntv2_serial_close(ser);
}
/* delete all audio objects */
list_for_each_safe(ptr, next, &ntv2_dev->audio_list) {
aud = list_entry(ptr, struct ntv2_audio, list);
spin_lock_irqsave(&ntv2_dev->audio_lock, flags);
list_del_init(&aud->list);
spin_unlock_irqrestore(&ntv2_dev->audio_lock, flags);
ntv2_audio_close(aud);
}
/* delete all video objects */
list_for_each_safe(ptr, next, &ntv2_dev->video_list) {
vid = list_entry(ptr, struct ntv2_video, list);
spin_lock_irqsave(&ntv2_dev->video_lock, flags);
list_del_init(&vid->list);
spin_unlock_irqrestore(&ntv2_dev->video_lock, flags);
ntv2_video_close(vid);
}
/* delete all channel objects */
list_for_each_safe(ptr, next, &ntv2_dev->channel_list) {
chn = list_entry(ptr, struct ntv2_channel, list);
spin_lock_irqsave(&ntv2_dev->channel_lock, flags);
list_del_init(&chn->list);
spin_unlock_irqrestore(&ntv2_dev->channel_lock, flags);
ntv2_channel_close(chn);
}
/* free the audio system */
if (ntv2_dev->snd_card != NULL)
snd_card_free(ntv2_dev->snd_card);
/* free the input monitor */
ntv2_input_close(ntv2_dev->inp_mon);
/* close the character device */
ntv2_chrdev_close(ntv2_dev->chr_dev);
/* release the resources */
ntv2_device_irq_release(ntv2_dev);
ntv2_pci_close(ntv2_dev->pci_dma);
ntv2_device_dma_release(ntv2_dev);
ntv2_register_close(ntv2_dev->pci_reg);
ntv2_register_close(ntv2_dev->vid_reg);
ntv2_device_pci_release(ntv2_dev);
ntv2_features_close(ntv2_dev->features);
memset(ntv2_dev, 0, sizeof(struct ntv2_device));
kfree(ntv2_dev);
}
/*
* Configure device
*/
int ntv2_device_configure(struct ntv2_device *ntv2_dev,
struct pci_dev *pdev)
{
struct ntv2_channel *ntv2_chn;
struct ntv2_video *ntv2_vid;
struct ntv2_audio *ntv2_aud;
struct ntv2_serial *ntv2_ser;
unsigned long flags;
u32 device_id;
int num_channels;
int num_video;
int num_audio;
int num_serial;
int index;
int result;
int i;
if ((ntv2_dev == NULL) || (pdev == NULL))
return -EPERM;
/* initialize register data */
ntv2_kona_register_initialize();
/* configure ntv2 pci resources */
result = ntv2_device_pci_configure(ntv2_dev, pdev);
if (result != 0)
return result;
/* initialize register access */
if (ntv2_dev->pci_region) {
ntv2_dev->pci_reg = ntv2_register_open((struct ntv2_object*)ntv2_dev, ntv2_pci_name(ntv2_dev->pci_type), 0);
if (ntv2_dev->pci_reg == NULL)
return -ENOMEM;
result = ntv2_register_configure(ntv2_dev->pci_reg,
ntv2_dev->pci_base,
ntv2_dev->pci_size);
if (result != 0)
return result;
result = ntv2_register_enable(ntv2_dev->pci_reg);
if (result != 0)
return result;
}
if (ntv2_dev->vid_region) {
ntv2_dev->vid_reg = ntv2_register_open((struct ntv2_object*)ntv2_dev, "avr", 0);
if (ntv2_dev->vid_reg == NULL)
return -ENOMEM;
result = ntv2_register_configure(ntv2_dev->vid_reg,
ntv2_dev->vid_base,
ntv2_dev->vid_size);
if (result != 0)
return result;
result = ntv2_register_enable(ntv2_dev->vid_reg);
if (result != 0)
return result;
}
/* read device id */
device_id = ntv2_reg_read(ntv2_dev->vid_reg, ntv2_kona_reg_device_id, 0);
/* configure ntv2 features */
ntv2_dev->features = ntv2_features_open((struct ntv2_object*)ntv2_dev, "ftr", 0);
if (ntv2_dev->features == NULL)
return -ENOMEM;
result = ntv2_features_configure(ntv2_dev->features, device_id);
if (result != 0) {
NTV2_MSG_DEVICE_ERROR("%s: *error* no features for device id %08x\n",
ntv2_dev->name, device_id);
return result;
}
NTV2_MSG_DEVICE_INFO("%s: device name %s device id %08x\n",
ntv2_dev->name,
ntv2_dev->features->device_name,
ntv2_dev->features->device_id);
/* initialize ntv2 hardware */
ntv2_device_init_hardware(ntv2_dev);
/* configure ntv2 dma resources */
result = ntv2_device_dma_configure(ntv2_dev);
if (result != 0)
return result;
/* configure ntv2 irq resources */
result = ntv2_device_irq_configure(ntv2_dev);
if (result != 0)
return result;
/* initialize ntv2 dma engines */
ntv2_dev->pci_dma = ntv2_pci_open((struct ntv2_object*)ntv2_dev, "pci", 0);
if (ntv2_dev->pci_dma == NULL)
return -ENOMEM;
result = ntv2_pci_configure(ntv2_dev->pci_dma, ntv2_dev->pci_type, ntv2_dev->pci_reg);
if (result != 0)
return result;
/* initialize ntv2 input monitor */
ntv2_dev->inp_mon = ntv2_input_open((struct ntv2_object*)ntv2_dev, "inp", 0);
if (ntv2_dev->inp_mon == NULL)
return -ENOMEM;
result = ntv2_input_configure(ntv2_dev->inp_mon,
ntv2_dev->features,
ntv2_dev->vid_reg);
if (result != 0)
return result;
/* enable input detection */
ntv2_input_enable(ntv2_dev->inp_mon);
/* initialize character device */
ntv2_dev->chr_dev = ntv2_chrdev_open((struct ntv2_object*)ntv2_dev, "chr", 0);
if (ntv2_dev->chr_dev == NULL)
return -ENOMEM;
result = ntv2_chrdev_configure(ntv2_dev->chr_dev,
ntv2_dev->features,
ntv2_dev->vid_reg);
if (result != 0)
return result;
ntv2_chrdev_enable(ntv2_dev->chr_dev);
/* create the audio system */
#ifdef NTV2_USE_SND_CARD_NEW
result = snd_card_new(&ntv2_dev->pci_dev->dev,
SNDRV_DEFAULT_IDX1,
SNDRV_DEFAULT_STR1,
THIS_MODULE,
0,
&ntv2_dev->snd_card);
#else
result = snd_card_create(SNDRV_DEFAULT_IDX1,
SNDRV_DEFAULT_STR1,
THIS_MODULE,
0,
&ntv2_dev->snd_card);
#endif
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: *error* snd_card_new/create failed code %d\n",
ntv2_dev->name, result);
return result;
}
strcpy(ntv2_dev->snd_card->driver, ntv2_dev->ntv2_mod->name);
strcpy(ntv2_dev->snd_card->shortname, ntv2_dev->features->device_name);
snprintf(ntv2_dev->snd_card->longname,
sizeof(ntv2_dev->snd_card->longname),
"AJA %s IO Board on %s irq %d",
ntv2_dev->snd_card->shortname,
pci_name(ntv2_dev->pci_dev),
ntv2_dev->pci_dev->irq);
num_video = ntv2_dev->features->num_video_channels;
num_audio = ntv2_dev->features->num_audio_channels;
num_channels = max(num_video, num_audio);
num_serial = ntv2_dev->features->num_serial_ports;
for (i = 0; i < num_channels; i++) {
/* allocate and initialize channel instance */
index = atomic_inc_return(&ntv2_dev->channel_index) - 1;
ntv2_chn = ntv2_channel_open((struct ntv2_object*)ntv2_dev, "chn", index);
/* configure channel */
result = ntv2_channel_configure(ntv2_chn,
ntv2_dev->features,
ntv2_dev->vid_reg);
if (result != 0) {
ntv2_channel_close(ntv2_chn);
return result;
}
/* add to the channel list */
spin_lock_irqsave(&ntv2_dev->channel_lock, flags);
list_add_tail(&ntv2_chn->list, &ntv2_dev->channel_list);
spin_unlock_irqrestore(&ntv2_dev->channel_lock, flags);
if (i < num_video) {
/* allocate and initialize video device instance */
index = atomic_inc_return(&ntv2_dev->video_index) - 1;
ntv2_vid = ntv2_video_open((struct ntv2_object*)ntv2_dev, "vid", index);
/* configure video device */
result = ntv2_video_configure(ntv2_vid,
ntv2_dev->features,
ntv2_chn,
ntv2_dev->inp_mon,
ntv2_dev->pci_dma);
if (result != 0) {
ntv2_video_close(ntv2_vid);
return result;
}
/* add to the video list */
spin_lock_irqsave(&ntv2_dev->video_lock, flags);
list_add_tail(&ntv2_vid->list, &ntv2_dev->video_list);
spin_unlock_irqrestore(&ntv2_dev->video_lock, flags);
}
if (i < num_audio) {
/* allocate and initialize audio device instance */
index = atomic_inc_return(&ntv2_dev->audio_index) - 1;
ntv2_aud = ntv2_audio_open((struct ntv2_object*)ntv2_dev, "aud", index);
/* configure audio device */
result = ntv2_audio_configure(ntv2_aud,
ntv2_dev->features,
ntv2_dev->snd_card,
ntv2_chn,
ntv2_dev->inp_mon,
ntv2_dev->pci_dma);
if (result != 0) {
ntv2_audio_close(ntv2_aud);
return result;
}
/* add to the audio list */
spin_lock_irqsave(&ntv2_dev->audio_lock, flags);
list_add_tail(&ntv2_aud->list, &ntv2_dev->audio_list);
spin_unlock_irqrestore(&ntv2_dev->audio_lock, flags);
}
}
/* register the pcm devices */
if (num_audio > 0) {
result = snd_card_register(ntv2_dev->snd_card);
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: *error* snd_card_register failed code %d\n",
ntv2_dev->name, result);
}
}
for (i = 0; i < num_serial; i++) {
/* allocate and initialize serial device instance */
index = atomic_inc_return(&ntv2_dev->serial_index) - 1;
ntv2_ser = ntv2_serial_open((struct ntv2_object*)ntv2_dev, "ser", index);
/* configure serial device */
result = ntv2_serial_configure(ntv2_ser,
ntv2_dev->features,
ntv2_dev->vid_reg);
if (result != 0) {
ntv2_serial_close(ntv2_ser);
return result;
}
/* add to the serial list */
spin_lock_irqsave(&ntv2_dev->serial_lock, flags);
list_add_tail(&ntv2_ser->list, &ntv2_dev->serial_list);
spin_unlock_irqrestore(&ntv2_dev->serial_lock, flags);
}
/* enable interrupts */
result = ntv2_pci_enable(ntv2_dev->pci_dma);
if (result != 0)
return result;
return 0;
}
void ntv2_device_suspend(struct ntv2_device *ntv2_dev)
{
struct list_head *ptr;
struct list_head *next;
struct ntv2_audio *aud;
struct ntv2_video *vid;
struct ntv2_channel *chn;
if (ntv2_dev == NULL)
return;
/* suspend audio */
if (ntv2_dev->snd_card != NULL)
snd_power_change_state(ntv2_dev->snd_card, SNDRV_CTL_POWER_D3hot);
/* disable all streaming */
list_for_each_safe(ptr, next, &ntv2_dev->audio_list) {
aud = list_entry(ptr, struct ntv2_audio, list);
if ((aud != NULL) && (aud->pcm != NULL)) {
snd_pcm_suspend_all(aud->pcm);
}
ntv2_audio_disable_all(aud);
}
list_for_each_safe(ptr, next, &ntv2_dev->video_list) {
vid = list_entry(ptr, struct ntv2_video, list);
ntv2_video_disable(vid);
}
list_for_each_safe(ptr, next, &ntv2_dev->channel_list) {
chn = list_entry(ptr, struct ntv2_channel, list);
ntv2_channel_disable_all(chn);
}
/* disable hardware stuff */
ntv2_chrdev_disable(ntv2_dev->chr_dev);
ntv2_input_disable(ntv2_dev->inp_mon);
ntv2_pci_disable(ntv2_dev->pci_dma);
ntv2_register_disable(ntv2_dev->vid_reg);
ntv2_register_disable(ntv2_dev->pci_reg);
}
void ntv2_device_resume(struct ntv2_device *ntv2_dev)
{
if (ntv2_dev == NULL)
return;
/* enable hardware */
ntv2_register_enable(ntv2_dev->pci_reg);
ntv2_register_enable(ntv2_dev->vid_reg);
ntv2_pci_enable(ntv2_dev->pci_dma);
ntv2_input_enable(ntv2_dev->inp_mon);
ntv2_chrdev_enable(ntv2_dev->chr_dev);
/* resume audio */
if (ntv2_dev->snd_card != NULL)
snd_power_change_state(ntv2_dev->snd_card, SNDRV_CTL_POWER_D0);
}
/*
* Configure pci resources
*/
static int ntv2_device_pci_configure(struct ntv2_device *ntv2_dev, struct pci_dev *pdev)
{
enum ntv2_pci_type pci_type = ntv2_pci_type_unknown;
bool pci_region = false;
bool vid_region = false;
int pci_bar = 0;
int vid_bar = 0;
int result = -EPERM;
NTV2_MSG_DEVICE_INFO("%s: configure pci resources\n", ntv2_dev->name);
ntv2_dev->pci_dev = pdev;
/* determine barness */
switch (pdev->device)
{
case NTV2_PCI_DEVID_KONA4:
case NTV2_PCI_DEVID_CORVID88:
case NTV2_PCI_DEVID_CORVID44:
case NTV2_PCI_DEVID_CORVIDHBR:
case NTV2_PCI_DEVID_KONA1:
pci_type = ntv2_pci_type_nwl;
pci_region = true;
vid_region = true;
pci_bar = 0;
vid_bar = 1;
break;
case NTV2_PCI_DEVID_KONAHDMI:
pci_type = ntv2_pci_type_xlx;
pci_region = true;
vid_region = true;
pci_bar = 1;
vid_bar = 0;
break;
default:
NTV2_MSG_ERROR("%s: *error* unrecognized pci device %04x\n", ntv2_dev->name, pdev->device);
return -ENOMEM;
}
/* request nwl pci bar */
if (pci_type == ntv2_pci_type_nwl) {
result = pci_request_region(pdev, pci_bar, ntv2_dev->name);
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: request nwl bar failed code %d\n",
ntv2_dev->name, result);
return result;
}
ntv2_dev->pci_type = pci_type;
ntv2_dev->pci_region = true;
ntv2_dev->pci_bar = pci_bar;
}
/* request video pci bar */
if (vid_region) {
result = pci_request_region(pdev, vid_bar, ntv2_dev->name);
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: request video bar failed code %d\n",
ntv2_dev->name, result);
return result;
}
ntv2_dev->vid_region = true;
ntv2_dev->vid_bar = vid_bar;
}
/* request xlx pci bar */
if (pci_type == ntv2_pci_type_xlx) {
result = pci_request_region(pdev, pci_bar, ntv2_dev->name);
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: request xlx bar failed code %d\n",
ntv2_dev->name, result);
return result;
}
ntv2_dev->pci_type = pci_type;
ntv2_dev->pci_region = true;
ntv2_dev->pci_bar = pci_bar;
}
/* map video bar */
if (vid_region) {
#ifdef NTV2_USE_IOREMAP
ntv2_dev->vid_base = ioremap(pci_resource_start(pdev, vid_bar),
pci_resource_len(pdev, vid_bar));
#else
ntv2_dev->vid_base = ioremap_nocache(pci_resource_start(pdev, vid_bar),
pci_resource_len(pdev, vid_bar));
#endif
if (ntv2_dev->vid_base == 0) {
NTV2_MSG_ERROR("%s: *error* video ioremap failed\n", ntv2_dev->name);
return -ENOMEM;
}
ntv2_dev->vid_size = pci_resource_len(pdev, vid_bar);
NTV2_MSG_DEVICE_INFO("%s: map vid pci bar %d phys 0x%08x address 0x%px size 0x%08x\n",
ntv2_dev->name,
vid_bar,
(u32)pci_resource_start(pdev, vid_bar),
ntv2_dev->vid_base,
ntv2_dev->vid_size);
}
/* map dma bar */
if (pci_region) {
#ifdef NTV2_USE_IOREMAP
ntv2_dev->pci_base = ioremap(pci_resource_start(pdev, pci_bar),
pci_resource_len(pdev, pci_bar));
#else
ntv2_dev->pci_base = ioremap_nocache(pci_resource_start(pdev, pci_bar),
pci_resource_len(pdev, pci_bar));
#endif
if (ntv2_dev->pci_base == 0) {
NTV2_MSG_ERROR("%s: *error* %s ioremap failed\n", ntv2_dev->name, ntv2_pci_name(pci_type));
return -ENOMEM;
}
ntv2_dev->pci_size = (u32)pci_resource_len(pdev, pci_bar);
NTV2_MSG_DEVICE_INFO("%s: map %s pci bar %d phys 0x%08x address 0x%px size 0x%08x\n",
ntv2_dev->name,
ntv2_pci_name(pci_type),
pci_bar,
(u32)pci_resource_start(pdev, pci_bar),
ntv2_dev->pci_base,
ntv2_dev->pci_size);
}
return 0;
}
/*
* Release pci resources
*/
static void ntv2_device_pci_release(struct ntv2_device *ntv2_dev)
{
struct pci_dev* pdev = ntv2_dev->pci_dev;
NTV2_MSG_DEVICE_INFO("%s: release pci resources\n", ntv2_dev->name);
if (ntv2_dev->pci_base != NULL) {
NTV2_MSG_DEVICE_INFO("%s: unmap %s bar %d address 0x%px\n",
ntv2_dev->name, ntv2_pci_name(ntv2_dev->pci_type),
ntv2_dev->pci_bar, ntv2_dev->pci_base);
iounmap(ntv2_dev->pci_base);
ntv2_dev->pci_base = NULL;
ntv2_dev->pci_size = 0;
}
if (ntv2_dev->vid_base != NULL) {
NTV2_MSG_DEVICE_INFO("%s: unmap vid bar %d address 0x%px\n",
ntv2_dev->name, ntv2_dev->vid_bar, ntv2_dev->vid_base);
iounmap(ntv2_dev->vid_base);
ntv2_dev->vid_base = NULL;
ntv2_dev->vid_size = 0;
}
if (ntv2_dev->pci_type == ntv2_pci_type_xlx) {
release_mem_region(pci_resource_start(pdev, ntv2_dev->pci_bar),
pci_resource_len(pdev, ntv2_dev->pci_bar));
ntv2_dev->pci_type = ntv2_pci_type_unknown;
}
if (ntv2_dev->vid_region) {
release_mem_region(pci_resource_start(pdev, ntv2_dev->vid_bar),
pci_resource_len(pdev, ntv2_dev->vid_bar));
ntv2_dev->vid_region = false;
}
if (ntv2_dev->pci_type == ntv2_pci_type_nwl) {
release_mem_region(pci_resource_start(pdev, ntv2_dev->pci_bar),
pci_resource_len(pdev, ntv2_dev->pci_bar));
ntv2_dev->pci_type = ntv2_pci_type_unknown;
}
return;
}
/*
* Configure dma resources
*/
static int ntv2_device_dma_configure(struct ntv2_device *ntv2_dev)
{
struct pci_dev* pdev = ntv2_dev->pci_dev;
int result = 0;
NTV2_MSG_DEVICE_INFO("%s: configure dma resources\n", ntv2_dev->name);
/* we are a pci dma master */
pci_set_master(pdev);
/* set dma mask to 64 bits with fallback to 32 bits*/
result = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
if (result == 0) {
result = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: set consistent dma mask to 64 bit failed code %d\n",
ntv2_dev->name, result);
return result;
}
NTV2_MSG_DEVICE_INFO("%s: pci dma mask = 64 bit\n", ntv2_dev->name);
}
else {
result = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: set dma mask to 32 bit failed code %d\n",
ntv2_dev->name, result);
return result;
}
result = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
if (result < 0) {
NTV2_MSG_DEVICE_ERROR("%s: set consistent dma mask to 32 bit failed code %d\n",
ntv2_dev->name, result);
return result;
}
NTV2_MSG_DEVICE_INFO("%s: pci dma mask = 32 bit\n", ntv2_dev->name);
}
return 0;
}
/*
* Release dma resources
*/
static void ntv2_device_dma_release(struct ntv2_device *ntv2_dev)
{
NTV2_MSG_DEVICE_INFO("%s: release dma resources\n", ntv2_dev->name);
return;
}
/*
* Configure irq resources
*/
static int ntv2_device_irq_configure(struct ntv2_device *ntv2_dev)
{
struct pci_dev* pdev = ntv2_dev->pci_dev;
int result = 0;
NTV2_MSG_DEVICE_INFO("%s: configure irq resources\n", ntv2_dev->name);
/* we are msi */
result = pci_enable_msi(pdev);
if (result != 0) {
NTV2_MSG_DEVICE_ERROR("%s: *error* can not enable msi irq\n", ntv2_dev->name);
return -EPERM;
}
ntv2_dev->irq_msi = true;
/* connect interrupt routine to irq */
result = request_irq(pdev->irq, ntv2_device_interrupt, 0, ntv2_dev->name, (void*)ntv2_dev);
if (result != 0) {
NTV2_MSG_DEVICE_ERROR("%s: *error* request irq %d failed\n",
ntv2_dev->name, pdev->irq);
return -EPERM;
}
ntv2_dev->irq_handler = ntv2_device_interrupt;
NTV2_MSG_DEVICE_INFO("%s: pci msi irq %d\n", ntv2_dev->name, pdev->irq);
return 0;
}
/*
* Release irq resources
*/
static void ntv2_device_irq_release(struct ntv2_device *ntv2_dev)
{
struct pci_dev* pdev = ntv2_dev->pci_dev;
NTV2_MSG_DEVICE_INFO("%s: release irq (%d) resources\n",
ntv2_dev->name, pdev->irq);
if (ntv2_dev->irq_handler != NULL) {
free_irq(pdev->irq, (void*)ntv2_dev);
ntv2_dev->irq_handler = NULL;
}
if (ntv2_dev->irq_msi) {
pci_disable_msi(pdev);
ntv2_dev->irq_msi = false;
}
return;
}
static irqreturn_t ntv2_device_interrupt(int irq, void* dev_id)
{
struct ntv2_device *ntv2_dev = (struct ntv2_device*)dev_id;
struct ntv2_interrupt_status irq_status;
struct list_head *ptr;
struct ntv2_channel *chn;
struct ntv2_serial *ser;
int result = IRQ_NONE;
int res;
if (ntv2_dev == NULL)
return IRQ_NONE;
/* timestamp the interrupt */
#ifdef NTV2_USE_VB2_BUFFER_TIMESTAMP
irq_status.v4l2_time = ktime_get_ns();
#else
v4l2_get_timestamp(&irq_status.v4l2_time);
#endif
/* read the video interrupt status registers */
ntv2_video_read_interrupt_status(ntv2_dev->vid_reg, &irq_status);
// NTV2_MSG_DEVICE_INFO("%s: irq status %08x %08x\n",
// ntv2_dev->name, irq_status.interrupt_status[0], irq_status.interrupt_status[1]);
/* process dma interrupt */
res = ntv2_pci_interrupt(ntv2_dev->pci_dma);
if (res == IRQ_HANDLED)
result = IRQ_HANDLED;
/* process video interrupts */
list_for_each(ptr, &ntv2_dev->channel_list) {
chn = list_entry(ptr, struct ntv2_channel, list);
res = ntv2_channel_interrupt(chn, &irq_status);
if (res == IRQ_HANDLED)
result = IRQ_HANDLED;
}
/* process uart interrupts */
list_for_each(ptr, &ntv2_dev->serial_list) {
ser = list_entry(ptr, struct ntv2_serial, list);
res = ntv2_serial_interrupt(ser, &irq_status);
if (res == IRQ_HANDLED)
result = IRQ_HANDLED;
}
return result;
}
static void ntv2_device_init_hardware(struct ntv2_device *ntv2_dev)
{
int num;
int i;
NTV2_MSG_DEVICE_INFO("%s: initialize ntv2 hardware\n", ntv2_dev->name);
/* disable fpga interrupts */
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_interrupt_control, 0, 0);
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_interrupt_control2, 0, 0);
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_interrupt_control3, 0, 0);
/* disable fpga frame writes */
num = NTV2_REG_COUNT(ntv2_kona_reg_frame_control);
for (i = 0; i < num; i++) {
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_frame_control, i, 0);
}
/* initialize frame buffer dma offset */
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_frame_buffer_offset, 0, 0);
/* disable sdi outputs */
ntv2_reg_write(ntv2_dev->vid_reg, ntv2_kona_reg_sdi_transmit_control, 0, 0);
}