1 /* Copyright (C) 2010 - 2015 UNISYS CORPORATION
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for more
15 #ifndef __VBUSCHANNEL_H__
16 #define __VBUSCHANNEL_H__
18 /* The vbus channel is the channel area provided via the BUS_CREATE controlvm
19 * message for each virtual bus. This channel area is provided to both server
20 * and client ends of the bus. The channel header area is initialized by
21 * the server, and the remaining information is filled in by the client.
22 * We currently use this for the client to provide various information about
23 * the client devices and client drivers for the server end to see.
25 #include <linux/uuid.h>
28 /* {193b331b-c58f-11da-95a9-00e08161165f} */
29 #define SPAR_VBUS_CHANNEL_PROTOCOL_UUID \
30 UUID_LE(0x193b331b, 0xc58f, 0x11da, \
31 0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f)
32 static const uuid_le spar_vbus_channel_protocol_uuid =
33 SPAR_VBUS_CHANNEL_PROTOCOL_UUID;
35 #define SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE ULTRA_CHANNEL_PROTOCOL_SIGNATURE
37 /* Must increment this whenever you insert or delete fields within this channel
38 * struct. Also increment whenever you change the meaning of fields within this
39 * channel struct so as to break pre-existing software. Note that you can
40 * usually add fields to the END of the channel struct withOUT needing to
43 #define SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID 1
45 #define SPAR_VBUS_CHANNEL_OK_CLIENT(ch) \
46 spar_check_channel_client(ch, \
47 spar_vbus_channel_protocol_uuid, \
49 sizeof(struct spar_vbus_channel_protocol),\
50 SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID, \
51 SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE)
53 #define SPAR_VBUS_CHANNEL_OK_SERVER(actual_bytes) \
54 (spar_check_channel_server(spar_vbus_channel_protocol_uuid, \
56 sizeof(struct spar_vbus_channel_protocol),\
59 #pragma pack(push, 1) /* both GCC and VC now allow this pragma */
62 * An array of this struct is present in the channel area for each vbus.
63 * (See vbuschannel.h.)
64 * It is filled in by the client side to provide info about the device
65 * and driver from the client's perspective.
67 struct ultra_vbus_deviceinfo {
68 u8 devtype[16]; /* short string identifying the device type */
69 u8 drvname[16]; /* driver .sys file name */
70 u8 infostrs[96]; /* kernel version */
71 u8 reserved[128]; /* pad size to 256 bytes */
75 * vbuschannel_sanitize_buffer() - remove non-printable chars from buffer
76 * @p: destination buffer where chars are written to
77 * @remain: number of bytes that can be written starting at #p
78 * @src: pointer to source buffer
79 * @srcmax: number of valid characters at #src
81 * Reads chars from the buffer at @src for @srcmax bytes, and writes to
82 * the buffer at @p, which is @remain bytes long, ensuring never to
83 * overflow the buffer at @p, using the following rules:
84 * - printable characters are simply copied from the buffer at @src to the
86 * - intervening streaks of non-printable characters in the buffer at @src
87 * are replaced with a single space in the buffer at @p
88 * Note that we pay no attention to '\0'-termination.
90 * Pass @p == NULL and @remain == 0 for this special behavior -- In this
91 * case, we simply return the number of bytes that WOULD HAVE been written
92 * to a buffer at @p, had it been infinitely big.
94 * Return: the number of bytes written to @p (or WOULD HAVE been written to
95 * @p, as described in the previous paragraph)
98 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
101 int nonprintable_streak = 0;
104 if ((*src >= ' ') && (*src < 0x7f)) {
105 if (nonprintable_streak) {
114 nonprintable_streak = 0;
125 nonprintable_streak = 1;
133 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
138 p++; chars++; remain--; \
142 * vbuschannel_itoa() - convert non-negative int to string
143 * @p: destination string
144 * @remain: max number of bytes that can be written to @p
145 * @num: input int to convert
147 * Converts the non-negative value at @num to an ascii decimal string
148 * at @p, writing at most @remain bytes. Note there is NO '\0' termination
151 * Return: number of bytes written to @p
155 vbuschannel_itoa(char *p, int remain, int num)
162 /* '0' is a special case */
168 /* form a backwards decimal ascii string in <s> */
170 if (digits >= (int)sizeof(s))
172 s[digits++] = (num % 10) + '0';
175 if (remain < digits) {
176 /* not enough room left at <p> to hold number, so fill with
179 for (i = 0; i < remain; i++, p++)
183 /* plug in the decimal ascii string representing the number, by */
184 /* reversing the string we just built in <s> */
195 * vbuschannel_devinfo_to_string() - format a struct ultra_vbus_deviceinfo
196 * to a printable string
197 * @devinfo: the struct ultra_vbus_deviceinfo to format
198 * @p: destination string area
199 * @remain: size of destination string area in bytes
200 * @devix: the device index to be included in the output data, or -1 if no
201 * device index is to be included
203 * Reads @devInfo, and converts its contents to a printable string at @p,
204 * writing at most @remain bytes. Note there is NO '\0' termination
207 * Return: number of bytes written to @p
210 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
211 char *p, int remain, int devix)
217 psrc = &devinfo->devtype[0];
218 nsrc = sizeof(devinfo->devtype);
219 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
222 /* emit device index */
224 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
225 x = vbuschannel_itoa(p, remain, devix);
229 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
231 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
232 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
233 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
236 /* emit device type */
237 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
241 pad = 15 - x; /* pad device type to be exactly 15 chars */
242 for (i = 0; i < pad; i++)
243 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
244 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
246 /* emit driver name */
247 psrc = &devinfo->drvname[0];
248 nsrc = sizeof(devinfo->drvname);
249 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
253 pad = 15 - x; /* pad driver name to be exactly 15 chars */
254 for (i = 0; i < pad; i++)
255 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
256 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
259 psrc = &devinfo->infostrs[0];
260 nsrc = sizeof(devinfo->infostrs);
261 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
265 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
270 struct spar_vbus_headerinfo {
271 u32 struct_bytes; /* size of this struct in bytes */
272 u32 device_info_struct_bytes; /* sizeof(ULTRA_VBUS_DEVICEINFO) */
273 u32 dev_info_count; /* num of items in DevInfo member */
274 /* (this is the allocated size) */
275 u32 chp_info_offset; /* byte offset from beginning of this struct */
276 /* to the ChpInfo struct (below) */
277 u32 bus_info_offset; /* byte offset from beginning of this struct */
278 /* to the BusInfo struct (below) */
279 u32 dev_info_offset; /* byte offset from beginning of this struct */
280 /* to the DevInfo array (below) */
284 struct spar_vbus_channel_protocol {
285 struct channel_header channel_header; /* initialized by server */
286 struct spar_vbus_headerinfo hdr_info; /* initialized by server */
287 /* the remainder of this channel is filled in by the client */
288 struct ultra_vbus_deviceinfo chp_info;
289 /* describes client chipset device and driver */
290 struct ultra_vbus_deviceinfo bus_info;
291 /* describes client bus device and driver */
292 struct ultra_vbus_deviceinfo dev_info[0];
293 /* describes client device and driver for each device on the bus */
296 #define VBUS_CH_SIZE_EXACT(MAXDEVICES) \
297 (sizeof(ULTRA_VBUS_CHANNEL_PROTOCOL) + ((MAXDEVICES) * \
298 sizeof(ULTRA_VBUS_DEVICEINFO)))
299 #define VBUS_CH_SIZE(MAXDEVICES) COVER(VBUS_CH_SIZE_EXACT(MAXDEVICES), 4096)