aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/nirio/niriok_proxy_impl_v2.cpp
blob: 8fb82708ea0b58d213ccfbc99ed8675e50845625 (plain)
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
//
// Copyright 2013-2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//


#include <uhd/transport/nirio/niriok_proxy_impl_v2.h>
#include <cstring>

#ifdef __clang__
    #pragma GCC diagnostic push ignored "-Wmissing-field-initializers"
#elif defined(__GNUC__)
    #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

namespace uhd { namespace niusrprio
{
    //-------------------------------------------------------
    // niriok_proxy_impl_v2
    //-------------------------------------------------------
    niriok_proxy_impl_v2::niriok_proxy_impl_v2()
    {
    }

    niriok_proxy_impl_v2::~niriok_proxy_impl_v2()
    {
        close();
    }

    nirio_status niriok_proxy_impl_v2::open(const std::string& interface_path)
    {
        WRITER_LOCK

        if (interface_path.empty()) return NiRio_Status_ResourceNotFound;

        //close if already open.
        // use non-locking _close since we already have the lock
        _close();

        in_transport_post_open_t in = {};
        out_transport_post_open_t out = {};

        in.status = NiRio_Status_Success;

        nirio_status status = NiRio_Status_Success;
        nirio_status_chain(nirio_driver_iface::rio_open(
        interface_path, _device_handle), status);
        if (nirio_status_not_fatal(status)) 
        {
            nirio_status_chain(nirio_driver_iface::rio_ioctl(_device_handle,
                                        IOCTL_TRANSPORT_POST_OPEN,
                                        &in, sizeof(in), &out, sizeof(out)), status);
            if (nirio_status_fatal(status)) _close();
        }
        return status;
    }

    void niriok_proxy_impl_v2::close(void)
    {
       WRITER_LOCK

       _close();
    }

    // this protected _close doesn't acquire the lock, so it can be used in methods 
    // that already have the lock
    void niriok_proxy_impl_v2::_close()
    {
        if(nirio_driver_iface::rio_isopen(_device_handle))
        {
            in_transport_pre_close_t in = {};
            out_transport_pre_close_t out = {};

            in.status = NiRio_Status_Success;

            nirio_driver_iface::rio_ioctl(
            _device_handle, IOCTL_TRANSPORT_PRE_CLOSE, &in, sizeof(in), &out, sizeof(out));
            nirio_driver_iface::rio_close(_device_handle);
       }
    }

    nirio_status niriok_proxy_impl_v2::reset()
    {
        READER_LOCK

        in_transport_reset_t in = {};
        out_transport_reset_t out = {};

        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_RESET,
                                    &in, sizeof(in), &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::get_cached_session(
        uint32_t& session)
    {
        READER_LOCK

        nirio_ioctl_packet_t out(&session, sizeof(session), 0);
        return nirio_driver_iface::rio_ioctl(_device_handle,
                                    nirio_driver_iface::NIRIO_IOCTL_GET_SESSION,
                                    NULL, 0,
                                    &out, sizeof(out));
    }

    nirio_status niriok_proxy_impl_v2::get_version(
        nirio_version_t type,
        uint32_t& major,
        uint32_t& upgrade,
        uint32_t& maintenance,
        char& phase,
        uint32_t& build)
    {
        nirio_device_attribute32_t version_attr = (type==CURRENT)?RIO_CURRENT_VERSION:RIO_OLDEST_COMPATIBLE_VERSION;
        uint32_t raw_version = 0;
        nirio_status status = get_attribute(version_attr, raw_version);

        major       = (raw_version & VERSION_MAJOR_MASK) >> VERSION_MAJOR_SHIFT;
        upgrade     = (raw_version & VERSION_UPGRD_MASK) >> VERSION_UPGRD_SHIFT;
        maintenance = (raw_version & VERSION_MAINT_MASK) >> VERSION_MAINT_SHIFT;
        build       = (raw_version & VERSION_BUILD_MASK) >> VERSION_BUILD_SHIFT;

        uint32_t phase_num = (raw_version & VERSION_PHASE_MASK) >> VERSION_PHASE_SHIFT;
        switch (phase_num) {
            case 0: phase = 'd'; break;
            case 1: phase = 'a'; break;
            case 2: phase = 'b'; break;
            case 3: phase = 'f'; break;
        }

        return status;
    }

    nirio_status niriok_proxy_impl_v2::get_attribute(
        const nirio_device_attribute32_t attribute,
        uint32_t& attrValue)
    {
        READER_LOCK

        in_transport_get32_t in = {};
        out_transport_get32_t out = {};

        in.attribute = attribute;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                 IOCTL_TRANSPORT_GET32,
                                 &in, sizeof(in),
                                 &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        attrValue = out.retVal__;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::set_attribute(
        const nirio_device_attribute32_t attribute,
        const uint32_t value)
    {
        READER_LOCK

        in_transport_set32_t in = {};
        out_transport_set32_t out = {};

        in.attribute = attribute;
        in.value  = value;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                 IOCTL_TRANSPORT_SET32,
                                 &in, sizeof(in),
                                 &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::peek(uint32_t offset, uint32_t& value)
    {
        READER_LOCK

        if (offset % 4 != 0) return NiRio_Status_MisalignedAccess;
        
        in_transport_peek32_t in = {};
        out_transport_peek32_t out = {};
                
        in.offset = offset;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_PEEK32,
                                    &in, sizeof(in),
                                    &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        value = out.retVal__;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::peek(uint32_t offset, uint64_t& value)
    {
        READER_LOCK

        if (offset % 8 != 0) return NiRio_Status_MisalignedAccess;
        in_transport_peek64_t in = {};
        out_transport_peek64_t out = {};

        in.offset = offset;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_PEEK64,
                                    &in, sizeof(in),
                                    &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        value = out.retVal__;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::poke(uint32_t offset, const uint32_t& value)
    {
        READER_LOCK

        if (offset % 4 != 0) return NiRio_Status_MisalignedAccess;

        in_transport_poke32_t in = {};
        out_transport_poke32_t out = {};

        in.offset = offset;
        in.value = value;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_POKE32,
                                    &in, sizeof(in),
                                    &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::poke(uint32_t offset, const uint64_t& value)
    {
        READER_LOCK

        if (offset % 8 != 0) return NiRio_Status_MisalignedAccess;

        in_transport_poke64_t in = {};
        out_transport_poke64_t out = {};

        in.offset = offset;
        in.value = value;
        in.status = NiRio_Status_Success;

        nirio_status ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_POKE64,
                                    &in, sizeof(in),
                                    &out, sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::map_fifo_memory(
        uint32_t fifo_instance,
        size_t size,
        nirio_driver_iface::rio_mmap_t& map)
    {
        READER_LOCK

        return nirio_driver_iface::rio_mmap(_device_handle,
                GET_FIFO_MEMORY_TYPE(fifo_instance),
                size, true, map);
    }

    nirio_status niriok_proxy_impl_v2::unmap_fifo_memory(
        nirio_driver_iface::rio_mmap_t& map)
    {
        READER_LOCK

        return nirio_driver_iface::rio_munmap(map);
    }

    nirio_status niriok_proxy_impl_v2::stop_all_fifos()
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_stop_all_t in = {};
        out_transport_fifo_stop_all_t out = {};

        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_STOP_ALL,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

   nirio_status niriok_proxy_impl_v2::add_fifo_resource(const nirio_fifo_info_t& fifo_info)
   {
      READER_LOCK

      nirio_status status = NiRio_Status_Success;
      nirio_status ioctl_status = NiRio_Status_Success;

      switch(fifo_info.direction)
      {
         case INPUT_FIFO:
         {
            in_transport_add_input_fifo_resource_t in = {};
            out_transport_add_input_fifo_resource_t out = {};

            in.channel = fifo_info.channel;
            in.baseAddress = fifo_info.base_addr;
            in.depthInSamples = fifo_info.depth;
            in.dataType.scalarType = fifo_info.scalar_type;
            in.dataType.bitWidth = fifo_info.bitWidth;
            in.dataType.integerWordLength = fifo_info.integerWordLength; 
            in.version = fifo_info.version;
            in.status = NiRio_Status_Success;

            ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_ADD_INPUT_FIFO_RESOURCE,
                                    &in, sizeof(in),
                                    &out, sizeof(out));

            status = nirio_status_fatal(ioctl_status) ? ioctl_status : out.status;
            break;
         }
         case OUTPUT_FIFO:
         {
            in_transport_add_output_fifo_resource_t in = {};
            out_transport_add_output_fifo_resource_t out = {};

            in.channel = fifo_info.channel;
            in.baseAddress = fifo_info.base_addr;
            in.depthInSamples = fifo_info.depth;
            in.dataType.scalarType = fifo_info.scalar_type;
            in.dataType.bitWidth = fifo_info.bitWidth;
            in.dataType.integerWordLength = fifo_info.integerWordLength;
            in.version = fifo_info.version;
            in.status = NiRio_Status_Success;
           
            ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                                    IOCTL_TRANSPORT_ADD_OUTPUT_FIFO_RESOURCE,
                                    &in, sizeof(in),
                                    &out, sizeof(out));

            status = nirio_status_fatal(ioctl_status) ? ioctl_status : out.status;
            break;
         }
         default:
            status = NiRio_Status_SoftwareFault;
         }
         
         return status;
    }

   nirio_status niriok_proxy_impl_v2::set_device_config()
   {
      READER_LOCK

      nirio_status ioctl_status = NiRio_Status_Success;

      in_transport_set_device_config_t in = {};
      out_transport_set_device_config_t out = {};

      in.attribute = 0;  //this is unused in the kernel
      in.status = NiRio_Status_Success;

      ioctl_status = nirio_driver_iface::rio_ioctl(_device_handle,
                              IOCTL_TRANSPORT_SET_DEVICE_CONFIG,
                              &in, sizeof(in),
                              &out, sizeof(out));

      return nirio_status_fatal(ioctl_status) ? ioctl_status : out.status;
   }

    nirio_status niriok_proxy_impl_v2::start_fifo(
       uint32_t channel)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_start_t in = {};
        out_transport_fifo_start_t out = {};

        in.channel = channel;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_START,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::stop_fifo(
        uint32_t channel)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_stop_t in = {};
        out_transport_fifo_stop_t out = {};

        in.channel = channel;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_STOP,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::configure_fifo(
       uint32_t channel,
       uint32_t requested_depth,
       uint8_t /*requires_actuals*/, //Unused
       uint32_t& actual_depth,
       uint32_t& actual_size)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_config_t in = {};
        out_transport_fifo_config_t out = {};

        in.channel = channel;
        in.requestedDepth = requested_depth;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_CONFIG,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        UHD_ASSERT_THROW(out.actualDepth <= std::numeric_limits<uint32_t>::max());
        actual_depth = static_cast<uint32_t>(out.actualDepth);
        UHD_ASSERT_THROW(out.actualSize <= std::numeric_limits<uint32_t>::max());
        actual_size = static_cast<uint32_t>(out.actualSize);
        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::wait_on_fifo(
        uint32_t channel,
        uint32_t elements_requested,
        uint32_t scalar_type,
        uint32_t bit_width,
        uint32_t timeout,
        uint8_t output,
        void*& data_pointer,
        uint32_t& elements_acquired,
        uint32_t& elements_remaining)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_wait_t in = {};
        out_transport_fifo_wait_t out = {};

        in.channel = channel;
        in.elementsRequested = elements_requested;
        in.dataType.scalarType = map_int_to_scalar_type(scalar_type);
        in.dataType.bitWidth = bit_width;
        in.dataType.integerWordLength = bit_width; // same as bit_width for all types except fixed point, which is not supported
        in.output = (output != 0);
        in.timeout = timeout;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_WAIT,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        data_pointer = reinterpret_cast<void*>(out.elements);
        UHD_ASSERT_THROW(out.elementsAcquired <= std::numeric_limits<uint32_t>::max());
        elements_acquired = static_cast<uint32_t>(out.elementsAcquired);
        UHD_ASSERT_THROW(out.elementsRemaining <= std::numeric_limits<uint32_t>::max());
        elements_remaining = static_cast<uint32_t>(out.elementsRemaining);
        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::grant_fifo(
       uint32_t channel,
       uint32_t elements_to_grant)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_grant_t in = {};
        out_transport_fifo_grant_t out = {};

        in.channel = channel;
        in.elements = elements_to_grant;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_GRANT,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::read_fifo(
        uint32_t channel,
        uint32_t elements_to_read,
        void* buffer,
        uint32_t /*buffer_datatype_width*/, //Unused
        uint32_t scalar_type,
        uint32_t bit_width,
        uint32_t timeout,
        uint32_t& number_read,
        uint32_t& number_remaining)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_read_t in = {};
        out_transport_fifo_read_t out = {};

        in.channel = channel;
        in.buf = reinterpret_cast<tAlignedU64>(buffer);
        in.numberElements = elements_to_read;
        in.dataType.scalarType = map_int_to_scalar_type(scalar_type);
        in.dataType.bitWidth = bit_width;
        in.dataType.integerWordLength = bit_width; // same as bit_width for all types except fixed point, which is not supported
        in.timeout = timeout;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_READ,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        number_read = out.read;
        number_remaining = out.remaining;
        return out.status;
    }

    nirio_status niriok_proxy_impl_v2::write_fifo(
        uint32_t channel,
        uint32_t elements_to_write,
        void* buffer,
        uint32_t /*buffer_datatype_width*/, //Unused
        uint32_t scalar_type,
        uint32_t bit_width,
        uint32_t timeout,
        uint32_t& number_remaining)
    {
        READER_LOCK

        nirio_status ioctl_status = NiRio_Status_Success;
        in_transport_fifo_write_t in = {};
        out_transport_fifo_write_t out = {};

        in.channel = channel;
        in.buf = reinterpret_cast<tAlignedU64>(buffer);
        in.numberElements = elements_to_write;
        in.dataType.scalarType = map_int_to_scalar_type(scalar_type);
        in.dataType.bitWidth = bit_width;
        in.dataType.integerWordLength = bit_width; // same as bit_width for all types except fixed point, which is not supported
        in.timeout = timeout;
        in.status = NiRio_Status_Success;

        ioctl_status = 
           nirio_driver_iface::rio_ioctl(
               _device_handle,
               IOCTL_TRANSPORT_FIFO_WRITE,
               &in,
               sizeof(in),
               &out,
               sizeof(out));
        if (nirio_status_fatal(ioctl_status)) return ioctl_status;

        number_remaining = out.remaining;
        return out.status;
    }

}}

#ifdef __GNUC__
    #pragma GCC diagnostic pop
#endif