1 /* Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more
16 #ifndef __VBUSDEVICEINFO_H__
17 #define __VBUSDEVICEINFO_H__
19 #include <linux/types.h>
21 #pragma pack(push, 1) /* both GCC and VC now allow this pragma */
23 /* An array of this struct is present in the channel area for each vbus.
24 * (See vbuschannel.h.)
25 * It is filled in by the client side to provide info about the device
26 * and driver from the client's perspective.
28 struct ultra_vbus_deviceinfo {
29 u8 devtype[16]; /* short string identifying the device type */
30 u8 drvname[16]; /* driver .sys file name */
31 u8 infostrs[96]; /* sequence of tab-delimited id strings: */
32 /* <DRIVER_REV> <DRIVER_VERTAG> <DRIVER_COMPILETIME> */
33 u8 reserved[128]; /* pad size to 256 bytes */
38 /* Reads chars from the buffer at <src> for <srcmax> bytes, and writes to
39 * the buffer at <p>, which is <remain> bytes long, ensuring never to
40 * overflow the buffer at <p>, using the following rules:
41 * - printable characters are simply copied from the buffer at <src> to the
43 * - intervening streaks of non-printable characters in the buffer at <src>
44 * are replaced with a single space in the buffer at <p>
45 * Note that we pay no attention to '\0'-termination.
46 * Returns the number of bytes written to <p>.
48 * Pass <p> == NULL and <remain> == 0 for this special behavior. In this
49 * case, we simply return the number of bytes that WOULD HAVE been written
50 * to a buffer at <p>, had it been infinitely big.
53 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
56 int nonprintable_streak = 0;
59 if ((*src >= ' ') && (*src < 0x7f)) {
60 if (nonprintable_streak) {
66 } else if (p == NULL) {
69 nonprintable_streak = 0;
76 } else if (p == NULL) {
80 nonprintable_streak = 1;
88 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
93 p++; chars++; remain--; \
96 /* Converts the non-negative value at <num> to an ascii decimal string
97 * at <p>, writing at most <remain> bytes. Note there is NO '\0' termination
100 * Returns the number of bytes written to <p>.
102 * Note that we create this function because we need to do this operation in
103 * an environment-independent way (since we are in a common header file).
106 vbuschannel_itoa(char *p, int remain, int num)
113 /* '0' is a special case */
119 /* form a backwards decimal ascii string in <s> */
121 if (digits >= (int)sizeof(s))
123 s[digits++] = (num % 10) + '0';
126 if (remain < digits) {
127 /* not enough room left at <p> to hold number, so fill with
129 for (i = 0; i < remain; i++, p++)
133 /* plug in the decimal ascii string representing the number, by */
134 /* reversing the string we just built in <s> */
144 /* Reads <devInfo>, and converts its contents to a printable string at <p>,
145 * writing at most <remain> bytes. Note there is NO '\0' termination
148 * Pass <devix> >= 0 if you want a device index presented.
150 * Returns the number of bytes written to <p>.
153 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
154 char *p, int remain, int devix)
160 psrc = &devinfo->devtype[0];
161 nsrc = sizeof(devinfo->devtype);
162 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
165 /* emit device index */
167 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
168 x = vbuschannel_itoa(p, remain, devix);
172 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
174 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
175 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
176 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
179 /* emit device type */
180 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
184 pad = 15 - x; /* pad device type to be exactly 15 chars */
185 for (i = 0; i < pad; i++)
186 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
187 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
189 /* emit driver name */
190 psrc = &devinfo->drvname[0];
191 nsrc = sizeof(devinfo->drvname);
192 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
196 pad = 15 - x; /* pad driver name to be exactly 15 chars */
197 for (i = 0; i < pad; i++)
198 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
199 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
202 psrc = &devinfo->infostrs[0];
203 nsrc = sizeof(devinfo->infostrs);
204 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
208 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);