xref: /openbmc/linux/include/uapi/linux/phonet.h (revision d2a3b501)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /**
3  * file phonet.h
4  *
5  * Phonet sockets kernel interface
6  *
7  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 
24 #ifndef _UAPILINUX_PHONET_H
25 #define _UAPILINUX_PHONET_H
26 
27 #include <linux/types.h>
28 #include <linux/socket.h>
29 
30 /* Automatic protocol selection */
31 #define PN_PROTO_TRANSPORT	0
32 /* Phonet datagram socket */
33 #define PN_PROTO_PHONET		1
34 /* Phonet pipe */
35 #define PN_PROTO_PIPE		2
36 #define PHONET_NPROTO		3
37 
38 /* Socket options for SOL_PNPIPE level */
39 #define PNPIPE_ENCAP		1
40 #define PNPIPE_IFINDEX		2
41 #define PNPIPE_HANDLE		3
42 #define PNPIPE_INITSTATE	4
43 
44 #define PNADDR_ANY		0
45 #define PNADDR_BROADCAST	0xFC
46 #define PNPORT_RESOURCE_ROUTING	0
47 
48 /* Values for PNPIPE_ENCAP option */
49 #define PNPIPE_ENCAP_NONE	0
50 #define PNPIPE_ENCAP_IP		1
51 
52 /* ioctls */
53 #define SIOCPNGETOBJECT		(SIOCPROTOPRIVATE + 0)
54 #define SIOCPNENABLEPIPE	(SIOCPROTOPRIVATE + 13)
55 #define SIOCPNADDRESOURCE	(SIOCPROTOPRIVATE + 14)
56 #define SIOCPNDELRESOURCE	(SIOCPROTOPRIVATE + 15)
57 
58 /* Phonet protocol header */
59 struct phonethdr {
60 	__u8	pn_rdev;
61 	__u8	pn_sdev;
62 	__u8	pn_res;
63 	__be16	pn_length;
64 	__u8	pn_robj;
65 	__u8	pn_sobj;
66 } __attribute__((packed));
67 
68 /* Common Phonet payload header */
69 struct phonetmsg {
70 	__u8	pn_trans_id;	/* transaction ID */
71 	__u8	pn_msg_id;	/* message type */
72 	union {
73 		struct {
74 			__u8	pn_submsg_id;	/* message subtype */
75 			__u8	pn_data[5];
76 		} base;
77 		struct {
78 			__u16	pn_e_res_id;	/* extended resource ID */
79 			__u8	pn_e_submsg_id;	/* message subtype */
80 			__u8	pn_e_data[3];
81 		} ext;
82 	} pn_msg_u;
83 };
84 #define PN_COMMON_MESSAGE	0xF0
85 #define PN_COMMGR		0x10
86 #define PN_PREFIX		0xE0 /* resource for extended messages */
87 #define pn_submsg_id		pn_msg_u.base.pn_submsg_id
88 #define pn_e_submsg_id		pn_msg_u.ext.pn_e_submsg_id
89 #define pn_e_res_id		pn_msg_u.ext.pn_e_res_id
90 #define pn_data			pn_msg_u.base.pn_data
91 #define pn_e_data		pn_msg_u.ext.pn_e_data
92 
93 /* data for unreachable errors */
94 #define PN_COMM_SERVICE_NOT_IDENTIFIED_RESP	0x01
95 #define PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP	0x14
96 #define pn_orig_msg_id		pn_data[0]
97 #define pn_status		pn_data[1]
98 #define pn_e_orig_msg_id	pn_e_data[0]
99 #define pn_e_status		pn_e_data[1]
100 
101 /* Phonet socket address structure */
102 struct sockaddr_pn {
103 	__kernel_sa_family_t spn_family;
104 	__u8 spn_obj;
105 	__u8 spn_dev;
106 	__u8 spn_resource;
107 	__u8 spn_zero[sizeof(struct sockaddr) - sizeof(__kernel_sa_family_t) - 3];
108 } __attribute__((packed));
109 
110 /* Well known address */
111 #define PN_DEV_PC	0x10
112 
113 static inline __u16 pn_object(__u8 addr, __u16 port)
114 {
115 	return (addr << 8) | (port & 0x3ff);
116 }
117 
118 static inline __u8 pn_obj(__u16 handle)
119 {
120 	return handle & 0xff;
121 }
122 
123 static inline __u8 pn_dev(__u16 handle)
124 {
125 	return handle >> 8;
126 }
127 
128 static inline __u16 pn_port(__u16 handle)
129 {
130 	return handle & 0x3ff;
131 }
132 
133 static inline __u8 pn_addr(__u16 handle)
134 {
135 	return (handle >> 8) & 0xfc;
136 }
137 
138 static inline void pn_sockaddr_set_addr(struct sockaddr_pn *spn, __u8 addr)
139 {
140 	spn->spn_dev &= 0x03;
141 	spn->spn_dev |= addr & 0xfc;
142 }
143 
144 static inline void pn_sockaddr_set_port(struct sockaddr_pn *spn, __u16 port)
145 {
146 	spn->spn_dev &= 0xfc;
147 	spn->spn_dev |= (port >> 8) & 0x03;
148 	spn->spn_obj = port & 0xff;
149 }
150 
151 static inline void pn_sockaddr_set_object(struct sockaddr_pn *spn,
152 						__u16 handle)
153 {
154 	spn->spn_dev = pn_dev(handle);
155 	spn->spn_obj = pn_obj(handle);
156 }
157 
158 static inline void pn_sockaddr_set_resource(struct sockaddr_pn *spn,
159 						__u8 resource)
160 {
161 	spn->spn_resource = resource;
162 }
163 
164 static inline __u8 pn_sockaddr_get_addr(const struct sockaddr_pn *spn)
165 {
166 	return spn->spn_dev & 0xfc;
167 }
168 
169 static inline __u16 pn_sockaddr_get_port(const struct sockaddr_pn *spn)
170 {
171 	return ((spn->spn_dev & 0x03) << 8) | spn->spn_obj;
172 }
173 
174 static inline __u16 pn_sockaddr_get_object(const struct sockaddr_pn *spn)
175 {
176 	return pn_object(spn->spn_dev, spn->spn_obj);
177 }
178 
179 static inline __u8 pn_sockaddr_get_resource(const struct sockaddr_pn *spn)
180 {
181 	return spn->spn_resource;
182 }
183 
184 /* Phonet device ioctl requests */
185 
186 #endif /* _UAPILINUX_PHONET_H */
187