1607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/**
2607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * file phonet.h
3607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells *
4607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * Phonet sockets kernel interface
5607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells *
6607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * Copyright (C) 2008 Nokia Corporation. All rights reserved.
7607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells *
8607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * This program is free software; you can redistribute it and/or
9607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * modify it under the terms of the GNU General Public License
10607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * version 2 as published by the Free Software Foundation.
11607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells *
12607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * This program is distributed in the hope that it will be useful, but
13607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * WITHOUT ANY WARRANTY; without even the implied warranty of
14607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * General Public License for more details.
16607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells *
17607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * You should have received a copy of the GNU General Public License
18607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * along with this program; if not, write to the Free Software
19607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells * 02110-1301 USA
21607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells */
22607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
23607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#ifndef _UAPILINUX_PHONET_H
24607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define _UAPILINUX_PHONET_H
25607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
26607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#include <linux/types.h>
27607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#include <linux/socket.h>
28607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
29607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Automatic protocol selection */
30607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_PROTO_TRANSPORT	0
31607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Phonet datagram socket */
32607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_PROTO_PHONET		1
33607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Phonet pipe */
34607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_PROTO_PIPE		2
35607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PHONET_NPROTO		3
36607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
37607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Socket options for SOL_PNPIPE level */
38607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_ENCAP		1
39607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_IFINDEX		2
40607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_HANDLE		3
41607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_INITSTATE	4
42607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
43607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNADDR_ANY		0
44607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNADDR_BROADCAST	0xFC
45607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPORT_RESOURCE_ROUTING	0
46607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
47607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Values for PNPIPE_ENCAP option */
48607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_ENCAP_NONE	0
49607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PNPIPE_ENCAP_IP		1
50607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
51607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* ioctls */
52607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define SIOCPNGETOBJECT		(SIOCPROTOPRIVATE + 0)
53607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define SIOCPNENABLEPIPE	(SIOCPROTOPRIVATE + 13)
54607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define SIOCPNADDRESOURCE	(SIOCPROTOPRIVATE + 14)
55607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define SIOCPNDELRESOURCE	(SIOCPROTOPRIVATE + 15)
56607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
57607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Phonet protocol header */
58607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstruct phonethdr {
59607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_rdev;
60607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_sdev;
61607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_res;
62607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__be16	pn_length;
63607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_robj;
64607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_sobj;
65607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells} __attribute__((packed));
66607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
67607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Common Phonet payload header */
68607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstruct phonetmsg {
69607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_trans_id;	/* transaction ID */
70607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8	pn_msg_id;	/* message type */
71607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	union {
72607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells		struct {
73607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells			__u8	pn_submsg_id;	/* message subtype */
74607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells			__u8	pn_data[5];
75607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells		} base;
76607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells		struct {
77607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells			__u16	pn_e_res_id;	/* extended resource ID */
78607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells			__u8	pn_e_submsg_id;	/* message subtype */
79607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells			__u8	pn_e_data[3];
80607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells		} ext;
81607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	} pn_msg_u;
82607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells};
83607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_COMMON_MESSAGE	0xF0
84607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_COMMGR		0x10
85607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_PREFIX		0xE0 /* resource for extended messages */
86607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_submsg_id		pn_msg_u.base.pn_submsg_id
87607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_e_submsg_id		pn_msg_u.ext.pn_e_submsg_id
88607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_e_res_id		pn_msg_u.ext.pn_e_res_id
89607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_data			pn_msg_u.base.pn_data
90607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_e_data		pn_msg_u.ext.pn_e_data
91607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
92607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* data for unreachable errors */
93607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_COMM_SERVICE_NOT_IDENTIFIED_RESP	0x01
94607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_COMM_ISA_ENTITY_NOT_REACHABLE_RESP	0x14
95607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_orig_msg_id		pn_data[0]
96607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_status		pn_data[1]
97607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_e_orig_msg_id	pn_e_data[0]
98607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define pn_e_status		pn_e_data[1]
99607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
100607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Phonet socket address structure */
101607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstruct sockaddr_pn {
102607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__kernel_sa_family_t spn_family;
103607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8 spn_obj;
104607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8 spn_dev;
105607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8 spn_resource;
106607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	__u8 spn_zero[sizeof(struct sockaddr) - sizeof(__kernel_sa_family_t) - 3];
107607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells} __attribute__((packed));
108607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
109607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Well known address */
110607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#define PN_DEV_PC	0x10
111607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
112607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u16 pn_object(__u8 addr, __u16 port)
113607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
114607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return (addr << 8) | (port & 0x3ff);
115607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
116607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
117607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u8 pn_obj(__u16 handle)
118607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
119607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return handle & 0xff;
120607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
121607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
122607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u8 pn_dev(__u16 handle)
123607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
124607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return handle >> 8;
125607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
126607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
127607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u16 pn_port(__u16 handle)
128607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
129607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return handle & 0x3ff;
130607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
131607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
132607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u8 pn_addr(__u16 handle)
133607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
134607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return (handle >> 8) & 0xfc;
135607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
136607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
137607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline void pn_sockaddr_set_addr(struct sockaddr_pn *spn, __u8 addr)
138607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
139607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_dev &= 0x03;
140607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_dev |= addr & 0xfc;
141607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
142607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
143607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline void pn_sockaddr_set_port(struct sockaddr_pn *spn, __u16 port)
144607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
145607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_dev &= 0xfc;
146607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_dev |= (port >> 8) & 0x03;
147607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_obj = port & 0xff;
148607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
149607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
150607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline void pn_sockaddr_set_object(struct sockaddr_pn *spn,
151607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells						__u16 handle)
152607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
153607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_dev = pn_dev(handle);
154607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_obj = pn_obj(handle);
155607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
156607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
157607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline void pn_sockaddr_set_resource(struct sockaddr_pn *spn,
158607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells						__u8 resource)
159607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
160607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	spn->spn_resource = resource;
161607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
162607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
163607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u8 pn_sockaddr_get_addr(const struct sockaddr_pn *spn)
164607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
165607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return spn->spn_dev & 0xfc;
166607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
167607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
168607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u16 pn_sockaddr_get_port(const struct sockaddr_pn *spn)
169607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
170607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return ((spn->spn_dev & 0x03) << 8) | spn->spn_obj;
171607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
172607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
173607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u16 pn_sockaddr_get_object(const struct sockaddr_pn *spn)
174607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
175607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return pn_object(spn->spn_dev, spn->spn_obj);
176607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
177607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
178607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howellsstatic inline __u8 pn_sockaddr_get_resource(const struct sockaddr_pn *spn)
179607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells{
180607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells	return spn->spn_resource;
181607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells}
182607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
183607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells/* Phonet device ioctl requests */
184607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells
185607ca46e97a1b6594b29647d98a32d545c24bdffDavid Howells#endif /* _UAPILINUX_PHONET_H */
186