Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / unisys / common-spar / include / vbusdeviceinfo.h
1 /* Copyright (C) 2010 - 2013 UNISYS CORPORATION
2  * All rights reserved.
3  *
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.
8  *
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
13  * details.
14  */
15
16 #ifndef __VBUSDEVICEINFO_H__
17 #define __VBUSDEVICEINFO_H__
18
19 #include <linux/types.h>
20
21 #pragma pack(push, 1)           /* both GCC and VC now allow this pragma */
22
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.
27  */
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 */
34 };
35
36 #pragma pack(pop)
37
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
42  *   buffer at <p>
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>.
47  *
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.
51  */
52 static inline int
53 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
54 {
55         int chars = 0;
56         int nonprintable_streak = 0;
57
58         while (srcmax > 0) {
59                 if ((*src >= ' ') && (*src < 0x7f)) {
60                         if (nonprintable_streak) {
61                                 if (remain > 0) {
62                                         *p = ' ';
63                                         p++;
64                                         remain--;
65                                         chars++;
66                                 } else if (p == NULL) {
67                                         chars++;
68                                 }
69                                 nonprintable_streak = 0;
70                         }
71                         if (remain > 0) {
72                                 *p = *src;
73                                 p++;
74                                 remain--;
75                                 chars++;
76                         } else if (p == NULL) {
77                                 chars++;
78                         }
79                 } else {
80                         nonprintable_streak = 1;
81                 }
82                 src++;
83                 srcmax--;
84         }
85         return chars;
86 }
87
88 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
89         do {                                       \
90                 if (remain <= 0)                   \
91                         break;                     \
92                 *p = ch;                           \
93                 p++;  chars++;  remain--;          \
94         } while (0)
95
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
98  * written to <p>.
99  *
100  * Returns the number of bytes written to <p>.
101  *
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).
104  */
105 static inline int
106 vbuschannel_itoa(char *p, int remain, int num)
107 {
108         int digits = 0;
109         char s[32];
110         int i;
111
112         if (num == 0) {
113                 /* '0' is a special case */
114                 if (remain <= 0)
115                         return 0;
116                 *p = '0';
117                 return 1;
118         }
119         /* form a backwards decimal ascii string in <s> */
120         while (num > 0) {
121                 if (digits >= (int)sizeof(s))
122                         return 0;
123                 s[digits++] = (num % 10) + '0';
124                 num = num / 10;
125         }
126         if (remain < digits) {
127                 /* not enough room left at <p> to hold number, so fill with
128                  * '?' */
129                 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  */
152 static inline int
153 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
154                               char *p, int remain, int devix)
155 {
156         char *psrc;
157         int nsrc, x, i, pad;
158         int chars = 0;
159
160         psrc = &devinfo->devtype[0];
161         nsrc = sizeof(devinfo->devtype);
162         if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
163                 return 0;
164
165         /* emit device index */
166         if (devix >= 0) {
167                 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
168                 x = vbuschannel_itoa(p, remain, devix);
169                 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 */
180         x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
181         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 */
190         psrc = &devinfo->drvname[0];
191         nsrc = sizeof(devinfo->drvname);
192         x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
193         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 */
202         psrc = &devinfo->infostrs[0];
203         nsrc = sizeof(devinfo->infostrs);
204         x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
205         p += x;
206         remain -= x;
207         chars += x;
208         VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
209
210         return chars;
211 }
212
213 #endif