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 __VBUSDEVICEINFO_H__
16 #define __VBUSDEVICEINFO_H__
18 #include <linux/types.h>
20 #pragma pack(push, 1) /* both GCC and VC now allow this pragma */
22 /* An array of this struct is present in the channel area for each vbus.
23 * (See vbuschannel.h.)
24 * It is filled in by the client side to provide info about the device
25 * and driver from the client's perspective.
27 struct ultra_vbus_deviceinfo {
28 u8 devtype[16]; /* short string identifying the device type */
29 u8 drvname[16]; /* driver .sys file name */
30 u8 infostrs[96]; /* sequence of tab-delimited id strings: */
31 /* <DRIVER_REV> <DRIVER_VERTAG> <DRIVER_COMPILETIME> */
32 u8 reserved[128]; /* pad size to 256 bytes */
37 /* Reads chars from the buffer at <src> for <srcmax> bytes, and writes to
38 * the buffer at <p>, which is <remain> bytes long, ensuring never to
39 * overflow the buffer at <p>, using the following rules:
40 * - printable characters are simply copied from the buffer at <src> to the
42 * - intervening streaks of non-printable characters in the buffer at <src>
43 * are replaced with a single space in the buffer at <p>
44 * Note that we pay no attention to '\0'-termination.
45 * Returns the number of bytes written to <p>.
47 * Pass <p> == NULL and <remain> == 0 for this special behavior. In this
48 * case, we simply return the number of bytes that WOULD HAVE been written
49 * to a buffer at <p>, had it been infinitely big.
52 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
55 int nonprintable_streak = 0;
58 if ((*src >= ' ') && (*src < 0x7f)) {
59 if (nonprintable_streak) {
65 } else if (p == NULL) {
68 nonprintable_streak = 0;
75 } else if (p == NULL) {
79 nonprintable_streak = 1;
87 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
92 p++; chars++; remain--; \
95 /* Converts the non-negative value at <num> to an ascii decimal string
96 * at <p>, writing at most <remain> bytes. Note there is NO '\0' termination
99 * Returns the number of bytes written to <p>.
101 * Note that we create this function because we need to do this operation in
102 * an environment-independent way (since we are in a common header file).
105 vbuschannel_itoa(char *p, int remain, int num)
112 /* '0' is a special case */
118 /* form a backwards decimal ascii string in <s> */
120 if (digits >= (int)sizeof(s))
122 s[digits++] = (num % 10) + '0';
125 if (remain < digits) {
126 /* not enough room left at <p> to hold number, so fill with
128 for (i = 0; i < remain; i++, p++)
132 /* plug in the decimal ascii string representing the number, by */
133 /* reversing the string we just built in <s> */
143 /* Reads <devInfo>, and converts its contents to a printable string at <p>,
144 * writing at most <remain> bytes. Note there is NO '\0' termination
147 * Pass <devix> >= 0 if you want a device index presented.
149 * Returns the number of bytes written to <p>.
152 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
153 char *p, int remain, int devix)
159 psrc = &devinfo->devtype[0];
160 nsrc = sizeof(devinfo->devtype);
161 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
164 /* emit device index */
166 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
167 x = vbuschannel_itoa(p, remain, devix);
171 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
173 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
174 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
175 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
178 /* emit device type */
179 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
183 pad = 15 - x; /* pad device type to be exactly 15 chars */
184 for (i = 0; i < pad; i++)
185 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
186 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
188 /* emit driver name */
189 psrc = &devinfo->drvname[0];
190 nsrc = sizeof(devinfo->drvname);
191 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
195 pad = 15 - x; /* pad driver name to be exactly 15 chars */
196 for (i = 0; i < pad; i++)
197 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
198 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
201 psrc = &devinfo->infostrs[0];
202 nsrc = sizeof(devinfo->infostrs);
203 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
207 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);