1 // Copyright (c) 2018 The rust-gpio-cdev Project Developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use crate::IoctlKind;
10 
11 pub const GPIOHANDLES_MAX: usize = 64;
12 
13 // struct gpiochip_info
14 #[repr(C)]
15 pub struct gpiochip_info {
16     pub name: [libc::c_char; 32],
17     pub label: [libc::c_char; 32],
18     pub lines: u32,
19 }
20 
21 #[repr(C)]
22 pub struct gpioline_info {
23     pub line_offset: u32,
24     pub flags: u32,
25     pub name: [libc::c_char; 32],
26     pub consumer: [libc::c_char; 32],
27 }
28 
29 #[repr(C)]
30 pub struct gpiohandle_request {
31     pub lineoffsets: [u32; GPIOHANDLES_MAX],
32     pub flags: u32,
33     pub default_values: [u8; GPIOHANDLES_MAX],
34     pub consumer_label: [libc::c_char; 32],
35     pub lines: u32,
36     pub fd: libc::c_int,
37 }
38 
39 #[repr(C)]
40 pub struct gpiohandle_data {
41     pub values: [u8; GPIOHANDLES_MAX],
42 }
43 
44 #[repr(C)]
45 pub struct gpioevent_request {
46     pub lineoffset: u32,
47     pub handleflags: u32,
48     pub eventflags: u32,
49     pub consumer_label: [libc::c_char; 32],
50     pub fd: libc::c_int,
51 }
52 
53 #[repr(C)]
54 pub struct gpioevent_data {
55     pub timestamp: u64,
56     pub id: u32,
57 }
58 
59 macro_rules! wrap_ioctl {
60     ($ioctl_macro:ident!($name:ident, $ioty:expr, $nr:expr, $ty:ident), $ioctl_error_type:expr) => {
61         mod $name {
62             $ioctl_macro!($name, $ioty, $nr, super::$ty);
63         }
64 
65         pub(crate) fn $name(fd: libc::c_int, data: &mut $ty) -> crate::errors::Result<libc::c_int> {
66             unsafe {
67                 $name::$name(fd, data).map_err(|e| crate::errors::ioctl_err($ioctl_error_type, e))
68             }
69         }
70     };
71 }
72 
73 wrap_ioctl!(
74     ioctl_read!(gpio_get_chipinfo_ioctl, 0xB4, 0x01, gpiochip_info),
75     IoctlKind::ChipInfo
76 );
77 wrap_ioctl!(
78     ioctl_readwrite!(gpio_get_lineinfo_ioctl, 0xB4, 0x02, gpioline_info),
79     IoctlKind::LineInfo
80 );
81 wrap_ioctl!(
82     ioctl_readwrite!(gpio_get_linehandle_ioctl, 0xB4, 0x03, gpiohandle_request),
83     IoctlKind::LineHandle
84 );
85 wrap_ioctl!(
86     ioctl_readwrite!(gpio_get_lineevent_ioctl, 0xB4, 0x04, gpioevent_request),
87     IoctlKind::LineEvent
88 );
89 
90 wrap_ioctl!(
91     ioctl_readwrite!(
92         gpiohandle_get_line_values_ioctl,
93         0xB4,
94         0x08,
95         gpiohandle_data
96     ),
97     IoctlKind::GetLine
98 );
99 wrap_ioctl!(
100     ioctl_readwrite!(
101         gpiohandle_set_line_values_ioctl,
102         0xB4,
103         0x09,
104         gpiohandle_data
105     ),
106     IoctlKind::SetLine
107 );
108