1 /* Copyright (C) 2010 - 2015 UNISYS CORPORATION
2 * All rights reserved.
3 *
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.
7 *
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
12 * details.
13 */
14
15 #ifndef __VBUSDEVICEINFO_H__
16 #define __VBUSDEVICEINFO_H__
17
18 #include <linux/types.h>
19
20 #pragma pack(push, 1) /* both GCC and VC now allow this pragma */
21
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.
26 */
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 */
33 };
34
35 #pragma pack(pop)
36
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
41 * buffer at <p>
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>.
46 *
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.
50 */
51 static inline int
vbuschannel_sanitize_buffer(char * p,int remain,char * src,int srcmax)52 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
53 {
54 int chars = 0;
55 int nonprintable_streak = 0;
56
57 while (srcmax > 0) {
58 if ((*src >= ' ') && (*src < 0x7f)) {
59 if (nonprintable_streak) {
60 if (remain > 0) {
61 *p = ' ';
62 p++;
63 remain--;
64 chars++;
65 } else if (p == NULL) {
66 chars++;
67 }
68 nonprintable_streak = 0;
69 }
70 if (remain > 0) {
71 *p = *src;
72 p++;
73 remain--;
74 chars++;
75 } else if (p == NULL) {
76 chars++;
77 }
78 } else {
79 nonprintable_streak = 1;
80 }
81 src++;
82 srcmax--;
83 }
84 return chars;
85 }
86
87 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
88 do { \
89 if (remain <= 0) \
90 break; \
91 *p = ch; \
92 p++; chars++; remain--; \
93 } while (0)
94
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
97 * written to <p>.
98 *
99 * Returns the number of bytes written to <p>.
100 *
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).
103 */
104 static inline int
vbuschannel_itoa(char * p,int remain,int num)105 vbuschannel_itoa(char *p, int remain, int num)
106 {
107 int digits = 0;
108 char s[32];
109 int i;
110
111 if (num == 0) {
112 /* '0' is a special case */
113 if (remain <= 0)
114 return 0;
115 *p = '0';
116 return 1;
117 }
118 /* form a backwards decimal ascii string in <s> */
119 while (num > 0) {
120 if (digits >= (int)sizeof(s))
121 return 0;
122 s[digits++] = (num % 10) + '0';
123 num = num / 10;
124 }
125 if (remain < digits) {
126 /* not enough room left at <p> to hold number, so fill with
127 * '?' */
128 for (i = 0; i < remain; i++, p++)
129 *p = '?';
130 return remain;
131 }
132 /* plug in the decimal ascii string representing the number, by */
133 /* reversing the string we just built in <s> */
134 i = digits;
135 while (i > 0) {
136 i--;
137 *p = s[i];
138 p++;
139 }
140 return digits;
141 }
142
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
145 * written to <p>.
146 *
147 * Pass <devix> >= 0 if you want a device index presented.
148 *
149 * Returns the number of bytes written to <p>.
150 */
151 static inline int
vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo * devinfo,char * p,int remain,int devix)152 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
153 char *p, int remain, int devix)
154 {
155 char *psrc;
156 int nsrc, x, i, pad;
157 int chars = 0;
158
159 psrc = &devinfo->devtype[0];
160 nsrc = sizeof(devinfo->devtype);
161 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
162 return 0;
163
164 /* emit device index */
165 if (devix >= 0) {
166 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
167 x = vbuschannel_itoa(p, remain, devix);
168 p += x;
169 remain -= x;
170 chars += x;
171 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
172 } else {
173 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
174 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
175 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
176 }
177
178 /* emit device type */
179 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
180 p += x;
181 remain -= x;
182 chars += x;
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);
187
188 /* emit driver name */
189 psrc = &devinfo->drvname[0];
190 nsrc = sizeof(devinfo->drvname);
191 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
192 p += x;
193 remain -= x;
194 chars += x;
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);
199
200 /* emit strings */
201 psrc = &devinfo->infostrs[0];
202 nsrc = sizeof(devinfo->infostrs);
203 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
204 p += x;
205 remain -= x;
206 chars += x;
207 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
208
209 return chars;
210 }
211
212 #endif
213