blob: abdab4ad0b3614fe17aea2d9f0d4f28c2d6e536f [file] [log] [blame]
Benjamin Romer6f14cc12015-07-16 12:40:48 -04001/* Copyright (C) 2010 - 2015 UNISYS CORPORATION
Ken Cox12e364b2014-03-04 07:58:07 -06002 * All rights reserved.
3 *
Benjamin Romer6f14cc12015-07-16 12:40:48 -04004 * 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.
Ken Cox12e364b2014-03-04 07:58:07 -06007 *
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
Benjamin Romer1d2def92014-08-02 22:03:43 -040018#include <linux/types.h>
Ken Cox12e364b2014-03-04 07:58:07 -060019
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 */
Benjamin Romer32b6b292014-10-03 14:09:18 -040027struct 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: */
Ken Cox12e364b2014-03-04 07:58:07 -060031 /* <DRIVER_REV> <DRIVER_VERTAG> <DRIVER_COMPILETIME> */
Benjamin Romerc2422332014-07-29 15:09:40 -040032 u8 reserved[128]; /* pad size to 256 bytes */
Benjamin Romer32b6b292014-10-03 14:09:18 -040033};
Ken Cox12e364b2014-03-04 07:58:07 -060034
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 */
51static inline int
Ken Coxe4c293c2014-07-03 10:02:18 -050052vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
Ken Cox12e364b2014-03-04 07:58:07 -060053{
54 int chars = 0;
55 int nonprintable_streak = 0;
Benjamin Romer11c4eba2014-08-05 14:57:54 -040056
Ken Cox12e364b2014-03-04 07:58:07 -060057 while (srcmax > 0) {
Ken Coxe4c293c2014-07-03 10:02:18 -050058 if ((*src >= ' ') && (*src < 0x7f)) {
Ken Cox12e364b2014-03-04 07:58:07 -060059 if (nonprintable_streak) {
60 if (remain > 0) {
61 *p = ' ';
62 p++;
63 remain--;
64 chars++;
Erik Arfvidson277f3f72016-02-08 10:41:42 -050065 } else if (!p) {
Ken Cox12e364b2014-03-04 07:58:07 -060066 chars++;
Benjamin Romerdf55a1f2014-10-23 14:29:40 -040067 }
Ken Cox12e364b2014-03-04 07:58:07 -060068 nonprintable_streak = 0;
69 }
70 if (remain > 0) {
Ken Coxe4c293c2014-07-03 10:02:18 -050071 *p = *src;
Ken Cox12e364b2014-03-04 07:58:07 -060072 p++;
73 remain--;
74 chars++;
Erik Arfvidson277f3f72016-02-08 10:41:42 -050075 } else if (!p) {
Ken Cox12e364b2014-03-04 07:58:07 -060076 chars++;
Benjamin Romerdf55a1f2014-10-23 14:29:40 -040077 }
78 } else {
Ken Cox12e364b2014-03-04 07:58:07 -060079 nonprintable_streak = 1;
Benjamin Romerdf55a1f2014-10-23 14:29:40 -040080 }
Ken Cox12e364b2014-03-04 07:58:07 -060081 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 */
104static inline int
Ken Cox960e0a52014-07-03 10:02:15 -0500105vbuschannel_itoa(char *p, int remain, int num)
Ken Cox12e364b2014-03-04 07:58:07 -0600106{
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) {
Benjamin Romer0d2400c2014-10-23 14:29:43 -0400120 if (digits >= (int)sizeof(s))
Ken Cox12e364b2014-03-04 07:58:07 -0600121 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
Erik Arfvidsoncbc5d962016-02-08 10:41:43 -0500127 * '?'
128 */
Ken Cox12e364b2014-03-04 07:58:07 -0600129 for (i = 0; i < remain; i++, p++)
130 *p = '?';
131 return remain;
132 }
133 /* plug in the decimal ascii string representing the number, by */
134 /* reversing the string we just built in <s> */
135 i = digits;
136 while (i > 0) {
137 i--;
138 *p = s[i];
139 p++;
140 }
141 return digits;
142}
143
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
146 * written to <p>.
147 *
148 * Pass <devix> >= 0 if you want a device index presented.
149 *
150 * Returns the number of bytes written to <p>.
151 */
152static inline int
Benjamin Romer32b6b292014-10-03 14:09:18 -0400153vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
Benjamin Romera6208122014-10-23 14:29:41 -0400154 char *p, int remain, int devix)
Ken Cox12e364b2014-03-04 07:58:07 -0600155{
Ken Coxe4c293c2014-07-03 10:02:18 -0500156 char *psrc;
Ken Cox12e364b2014-03-04 07:58:07 -0600157 int nsrc, x, i, pad;
158 int chars = 0;
159
Benjamin Romer8d507c62014-10-23 14:29:42 -0400160 psrc = &devinfo->devtype[0];
Benjamin Romer32b6b292014-10-03 14:09:18 -0400161 nsrc = sizeof(devinfo->devtype);
Ken Cox960e0a52014-07-03 10:02:15 -0500162 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
Ken Cox12e364b2014-03-04 07:58:07 -0600163 return 0;
164
165 /* emit device index */
166 if (devix >= 0) {
167 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
Ken Cox960e0a52014-07-03 10:02:15 -0500168 x = vbuschannel_itoa(p, remain, devix);
Ken Cox12e364b2014-03-04 07:58:07 -0600169 p += x;
170 remain -= x;
171 chars += x;
172 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
173 } else {
174 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
175 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
176 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
177 }
178
179 /* emit device type */
Ken Cox960e0a52014-07-03 10:02:15 -0500180 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
Ken Cox12e364b2014-03-04 07:58:07 -0600181 p += x;
182 remain -= x;
183 chars += x;
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);
188
189 /* emit driver name */
Benjamin Romer8d507c62014-10-23 14:29:42 -0400190 psrc = &devinfo->drvname[0];
Benjamin Romer32b6b292014-10-03 14:09:18 -0400191 nsrc = sizeof(devinfo->drvname);
Ken Cox960e0a52014-07-03 10:02:15 -0500192 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
Ken Cox12e364b2014-03-04 07:58:07 -0600193 p += x;
194 remain -= x;
195 chars += x;
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);
200
201 /* emit strings */
Benjamin Romer8d507c62014-10-23 14:29:42 -0400202 psrc = &devinfo->infostrs[0];
Benjamin Romer32b6b292014-10-03 14:09:18 -0400203 nsrc = sizeof(devinfo->infostrs);
Ken Cox960e0a52014-07-03 10:02:15 -0500204 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
Ken Cox12e364b2014-03-04 07:58:07 -0600205 p += x;
206 remain -= x;
207 chars += x;
208 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
209
210 return chars;
211}
212
213#endif