root/trunk/libffado/src/libieee1394/csr1212.h

Revision 742, 29.1 kB (checked in by ppalmers, 16 years ago)

- Remove some obsolete support files and dirs

- Clean up the license statements in the source files. Everything is

GPL version 3 now.

- Add license and copyright notices to scons scripts

- Clean up some other text files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * This file is part of FFADO
3  * FFADO = Free Firewire (pro-)audio drivers for linux
4  *
5  * FFADO is based upon FreeBoB.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 /*
22  * csr1212.h -- IEEE 1212 Control and Status Register support for Linux
23  *
24  * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za>
25  *                    Steve Kinneberg <kinnebergsteve@acmsystems.com>
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions are met:
29  *
30  *    1. Redistributions of source code must retain the above copyright notice,
31  *       this list of conditions and the following disclaimer.
32  *    2. Redistributions in binary form must reproduce the above copyright
33  *       notice, this list of conditions and the following disclaimer in the
34  *       documentation and/or other materials provided with the distribution.
35  *    3. The name of the author may not be used to endorse or promote products
36  *       derived from this software without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
41  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
44  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 #ifndef __CSR1212_H__
51 #define __CSR1212_H__
52
53 #ifdef __cplusplus
54   extern "C" {
55 #endif
56
57 /* Compatibility layer */
58 #ifdef __KERNEL__
59
60 #include <linux/types.h>
61 #include <linux/slab.h>
62 #include <linux/interrupt.h>
63 #include <linux/sched.h>
64 #include <linux/vmalloc.h>
65 #include <asm/pgalloc.h>
66
67 #define CSR1212_MALLOC(size)            vmalloc((size))
68 #define CSR1212_FREE(ptr)               vfree(ptr)
69 #define CSR1212_BE16_TO_CPU(quad)       be16_to_cpu(quad)
70 #define CSR1212_CPU_TO_BE16(quad)       cpu_to_be16(quad)
71 #define CSR1212_BE32_TO_CPU(quad)       be32_to_cpu(quad)
72 #define CSR1212_CPU_TO_BE32(quad)       cpu_to_be32(quad)
73 #define CSR1212_BE64_TO_CPU(quad)       be64_to_cpu(quad)
74 #define CSR1212_CPU_TO_BE64(quad)       cpu_to_be64(quad)
75
76 #define CSR1212_LE16_TO_CPU(quad)       le16_to_cpu(quad)
77 #define CSR1212_CPU_TO_LE16(quad)       cpu_to_le16(quad)
78 #define CSR1212_LE32_TO_CPU(quad)       le32_to_cpu(quad)
79 #define CSR1212_CPU_TO_LE32(quad)       cpu_to_le32(quad)
80 #define CSR1212_LE64_TO_CPU(quad)       le64_to_cpu(quad)
81 #define CSR1212_CPU_TO_LE64(quad)       cpu_to_le64(quad)
82
83 #include <linux/errno.h>
84 #define CSR1212_SUCCESS (0)
85 #define CSR1212_EINVAL  (-EINVAL)
86 #define CSR1212_ENOMEM  (-ENOMEM)
87 #define CSR1212_ENOENT  (-ENOENT)
88 #define CSR1212_EIO     (-EIO)
89 #define CSR1212_EBUSY   (-EBUSY)
90
91 #else   /* Userspace */
92
93 #include <sys/types.h>
94 #include <malloc.h>
95 #define CSR1212_MALLOC(size)            calloc(1,size)
96 #define CSR1212_FREE(ptr)               free(ptr)
97 #include <endian.h>
98 #if __BYTE_ORDER == __LITTLE_ENDIAN
99 #include <byteswap.h>
100 #define CSR1212_BE16_TO_CPU(quad)       bswap_16(quad)
101 #define CSR1212_CPU_TO_BE16(quad)       bswap_16(quad)
102 #define CSR1212_BE32_TO_CPU(quad)       bswap_32(quad)
103 #define CSR1212_CPU_TO_BE32(quad)       bswap_32(quad)
104 #define CSR1212_BE64_TO_CPU(quad)       bswap_64(quad)
105 #define CSR1212_CPU_TO_BE64(quad)       bswap_64(quad)
106
107 #define CSR1212_LE16_TO_CPU(quad)       (quad)
108 #define CSR1212_CPU_TO_LE16(quad)       (quad)
109 #define CSR1212_LE32_TO_CPU(quad)       (quad)
110 #define CSR1212_CPU_TO_LE32(quad)       (quad)
111 #define CSR1212_LE64_TO_CPU(quad)       (quad)
112 #define CSR1212_CPU_TO_LE64(quad)       (quad)
113 #else
114 #define CSR1212_BE16_TO_CPU(quad)       (quad)
115 #define CSR1212_CPU_TO_BE16(quad)       (quad)
116 #define CSR1212_BE32_TO_CPU(quad)       (quad)
117 #define CSR1212_CPU_TO_BE32(quad)       (quad)
118 #define CSR1212_BE64_TO_CPU(quad)       (quad)
119 #define CSR1212_CPU_TO_BE64(quad)       (quad)
120
121 #define CSR1212_LE16_TO_CPU(quad)       bswap_16(quad)
122 #define CSR1212_CPU_TO_LE16(quad)       bswap_16(quad)
123 #define CSR1212_LE32_TO_CPU(quad)       bswap_32(quad)
124 #define CSR1212_CPU_TO_LE32(quad)       bswap_32(quad)
125 #define CSR1212_LE64_TO_CPU(quad)       bswap_64(quad)
126 #define CSR1212_CPU_TO_LE64(quad)       bswap_64(quad)
127 #endif
128
129 #include <errno.h>
130 #define CSR1212_SUCCESS (0)
131 #define CSR1212_EINVAL  (EINVAL)
132 #define CSR1212_ENOMEM  (ENOMEM)
133 #define CSR1212_ENOENT  (ENOENT)
134 #define CSR1212_EIO     (EIO)
135 #define CSR1212_EBUSY   (EBUSY)
136
137 #endif
138
139
140 #define CSR1212_KV_VAL_MASK                     0xffffff
141 #define CSR1212_KV_KEY_SHIFT                    24
142 #define CSR1212_KV_KEY_TYPE_SHIFT               6
143 #define CSR1212_KV_KEY_ID_MASK                  0x3f
144 #define CSR1212_KV_KEY_TYPE_MASK                0x3             /* After shift */
145
146
147 /* CSR 1212 key types */
148 #define CSR1212_KV_TYPE_IMMEDIATE               0
149 #define CSR1212_KV_TYPE_CSR_OFFSET              1
150 #define CSR1212_KV_TYPE_LEAF                    2
151 #define CSR1212_KV_TYPE_DIRECTORY               3
152
153
154 /* CSR 1212 key ids */
155 #define CSR1212_KV_ID_DESCRIPTOR                0x01
156 #define CSR1212_KV_ID_BUS_DEPENDENT_INFO        0x02
157 #define CSR1212_KV_ID_VENDOR                    0x03
158 #define CSR1212_KV_ID_HARDWARE_VERSION          0x04
159 #define CSR1212_KV_ID_MODULE                    0x07
160 #define CSR1212_KV_ID_NODE_CAPABILITIES         0x0C
161 #define CSR1212_KV_ID_EUI_64                    0x0D
162 #define CSR1212_KV_ID_UNIT                      0x11
163 #define CSR1212_KV_ID_SPECIFIER_ID              0x12
164 #define CSR1212_KV_ID_VERSION                   0x13
165 #define CSR1212_KV_ID_DEPENDENT_INFO            0x14
166 #define CSR1212_KV_ID_UNIT_LOCATION             0x15
167 #define CSR1212_KV_ID_MODEL                     0x17
168 #define CSR1212_KV_ID_INSTANCE                  0x18
169 #define CSR1212_KV_ID_KEYWORD                   0x19
170 #define CSR1212_KV_ID_FEATURE                   0x1A
171 #define CSR1212_KV_ID_EXTENDED_ROM              0x1B
172 #define CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID 0x1C
173 #define CSR1212_KV_ID_EXTENDED_KEY              0x1D
174 #define CSR1212_KV_ID_EXTENDED_DATA             0x1E
175 #define CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR     0x1F
176 #define CSR1212_KV_ID_DIRECTORY_ID              0x20
177 #define CSR1212_KV_ID_REVISION                  0x21
178
179
180 /* IEEE 1212 Address space map */
181 #define CSR1212_ALL_SPACE_BASE                  (0x000000000000ULL)
182 #define CSR1212_ALL_SPACE_SIZE                  (1ULL << 48)
183 #define CSR1212_ALL_SPACE_END                   (CSR1212_ALL_SPACE_BASE + CSR1212_ALL_SPACE_SIZE)
184
185 #define  CSR1212_MEMORY_SPACE_BASE              (0x000000000000ULL)
186 #define  CSR1212_MEMORY_SPACE_SIZE              ((256ULL * (1ULL << 40)) - (512ULL * (1ULL << 20)))
187 #define  CSR1212_MEMORY_SPACE_END               (CSR1212_MEMORY_SPACE_BASE + CSR1212_MEMORY_SPACE_SIZE)
188
189 #define  CSR1212_PRIVATE_SPACE_BASE             (0xffffe0000000ULL)
190 #define  CSR1212_PRIVATE_SPACE_SIZE             (256ULL * (1ULL << 20))
191 #define  CSR1212_PRIVATE_SPACE_END              (CSR1212_PRIVATE_SPACE_BASE + CSR1212_PRIVATE_SPACE_SIZE)
192
193 #define  CSR1212_REGISTER_SPACE_BASE            (0xfffff0000000ULL)
194 #define  CSR1212_REGISTER_SPACE_SIZE            (256ULL * (1ULL << 20))
195 #define  CSR1212_REGISTER_SPACE_END             (CSR1212_REGISTER_SPACE_BASE + CSR1212_REGISTER_SPACE_SIZE)
196
197 #define  CSR1212_CSR_ARCH_REG_SPACE_BASE        (0xfffff0000000ULL)
198 #define  CSR1212_CSR_ARCH_REG_SPACE_SIZE        (512)
199 #define  CSR1212_CSR_ARCH_REG_SPACE_END         (CSR1212_CSR_ARCH_REG_SPACE_BASE + CSR1212_CSR_ARCH_REG_SPACE_SIZE)
200 #define  CSR1212_CSR_ARCH_REG_SPACE_OFFSET      (CSR1212_CSR_ARCH_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
201
202 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_BASE     (0xfffff0000200ULL)
203 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE     (512)
204 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_END      (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE + CSR1212_CSR_BUS_DEP_REG_SPACE_SIZE)
205 #define  CSR1212_CSR_BUS_DEP_REG_SPACE_OFFSET   (CSR1212_CSR_BUS_DEP_REG_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
206
207 #define  CSR1212_CONFIG_ROM_SPACE_BASE          (0xfffff0000400ULL)
208 #define  CSR1212_CONFIG_ROM_SPACE_SIZE          (1024)
209 #define  CSR1212_CONFIG_ROM_SPACE_END           (CSR1212_CONFIG_ROM_SPACE_BASE + CSR1212_CONFIG_ROM_SPACE_SIZE)
210 #define  CSR1212_CONFIG_ROM_SPACE_OFFSET        (CSR1212_CONFIG_ROM_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
211
212 #define  CSR1212_UNITS_SPACE_BASE               (0xfffff0000800ULL)
213 #define  CSR1212_UNITS_SPACE_SIZE               ((256ULL * (1ULL << 20)) - 2048)
214 #define  CSR1212_UNITS_SPACE_END                (CSR1212_UNITS_SPACE_BASE + CSR1212_UNITS_SPACE_SIZE)
215 #define  CSR1212_UNITS_SPACE_OFFSET             (CSR1212_UNITS_SPACE_BASE - CSR1212_REGISTER_SPACE_BASE)
216
217 #define  CSR1212_EXTENDED_ROM_SIZE              (0x10000 * sizeof(u_int32_t))
218
219
220 /* Config ROM image structures */
221 struct csr1212_bus_info_block_img {
222         u_int8_t length;
223         u_int8_t crc_length;
224         u_int16_t crc;
225
226         /* Must be last */
227         u_int32_t data[0];      /* older gcc can't handle [] which is standard */
228 };
229
230 #define CSR1212_KV_KEY(quad)            (CSR1212_BE32_TO_CPU(quad) >> CSR1212_KV_KEY_SHIFT)
231 #define CSR1212_KV_KEY_TYPE(quad)       (CSR1212_KV_KEY(quad) >> CSR1212_KV_KEY_TYPE_SHIFT)
232 #define CSR1212_KV_KEY_ID(quad)         (CSR1212_KV_KEY(quad) & CSR1212_KV_KEY_ID_MASK)
233 #define CSR1212_KV_VAL(quad)            (CSR1212_BE32_TO_CPU(quad) & CSR1212_KV_VAL_MASK)
234
235 #define CSR1212_SET_KV_KEY(quad, key)   ((quad) = \
236         CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | ((key) << CSR1212_KV_KEY_SHIFT)))
237 #define CSR1212_SET_KV_VAL(quad, val)   ((quad) = \
238         CSR1212_CPU_TO_BE32((CSR1212_KV_KEY(quad) << CSR1212_KV_KEY_SHIFT) | (val)))
239 #define CSR1212_SET_KV_TYPEID(quad, type, id)   ((quad) = \
240         CSR1212_CPU_TO_BE32(CSR1212_KV_VAL(quad) | \
241         (((((type) & CSR1212_KV_KEY_TYPE_MASK) << CSR1212_KV_KEY_TYPE_SHIFT) | \
242           ((id) & CSR1212_KV_KEY_ID_MASK)) << CSR1212_KV_KEY_SHIFT)))
243
244 typedef u_int32_t csr1212_quad_t;
245
246
247 struct csr1212_keyval_img {
248         u_int16_t length;
249         u_int16_t crc;
250
251         /* Must be last */
252         csr1212_quad_t data[0]; /* older gcc can't handle [] which is standard */
253 };
254
255 struct csr1212_leaf {
256         int len;
257         u_int32_t *data;
258 };
259
260 struct csr1212_dentry {
261         struct csr1212_dentry *next, *prev;
262         struct csr1212_keyval *kv;
263 };
264
265 struct csr1212_directory {
266         int len;
267         struct csr1212_dentry *dentries_head, *dentries_tail;
268 };
269
270 struct csr1212_keyval {
271         struct {
272                 u_int8_t type;
273                 u_int8_t id;
274         } key;
275         union {
276                 u_int32_t immediate;
277                 u_int32_t csr_offset;
278                 struct csr1212_leaf leaf;
279                 struct csr1212_directory directory;
280         } value;
281         struct csr1212_keyval *associate;
282         int refcnt;
283
284         /* used in generating and/or parsing CSR image */
285         struct csr1212_keyval *next, *prev;     /* flat list of CSR elements */
286         u_int32_t offset;       /* position in CSR from 0xffff f000 0000 */
287         u_int8_t valid;         /* flag indicating keyval has valid data*/
288 };
289
290
291 struct csr1212_cache_region {
292         struct csr1212_cache_region *next, *prev;
293         u_int32_t offset_start;         /* inclusive */
294         u_int32_t offset_end;           /* exclusive */
295 };
296
297 struct csr1212_csr_rom_cache {
298         struct csr1212_csr_rom_cache *next, *prev;
299         struct csr1212_cache_region *filled_head, *filled_tail;
300         struct csr1212_keyval *layout_head, *layout_tail;
301         size_t size;
302         u_int32_t offset;
303         struct csr1212_keyval *ext_rom;
304         size_t len;
305
306         /* Must be last */
307         u_int32_t data[0];      /* older gcc can't handle [] which is standard */
308 };
309
310 struct csr1212_csr {
311         size_t bus_info_len;    /* bus info block length in bytes */
312         size_t crc_len;         /* crc length in bytes */
313         u_int32_t *bus_info_data;       /* bus info data incl bus name and EUI */
314
315         void *private_data;             /* private_data, bus specific data */
316         struct csr1212_bus_ops *ops;
317
318         struct csr1212_keyval *root_kv;
319
320         int max_rom;            /* max bytes readable in Config ROM region */
321
322         /* Items below used for image parsing and generation */
323         struct csr1212_csr_rom_cache *cache_head, *cache_tail;
324 };
325
326 struct csr1212_bus_ops {
327         /* This function is used by csr1212 to read additional information
328          * from remote nodes when parsing a Config ROM (i.e., read Config ROM
329          * entries located in the Units Space.  Must return 0 on success
330          * anything else indicates an error. */
331         int (*bus_read) (struct csr1212_csr *csr, u_int64_t addr,
332                          u_int16_t length, void *buffer, void *private_data);
333
334         /* This function is used by csr1212 to allocate a region in units space
335          * in the event that Config ROM entries don't all fit in the predefined
336          * 1K region.  The void *private_data parameter is private_data member of struct
337          * csr1212_csr. */
338         u_int64_t (*allocate_addr_range) (u_int64_t size, u_int32_t alignment,
339                                           void *private_data);
340
341
342         /* This function is used by csr1212 to release a region in units space
343          * that is no longer needed. */
344         void (*release_addr) (u_int64_t addr, void *private_data);
345
346         /* This function is used by csr1212 to determine the max read request
347          * supported by a remote node when reading the ConfigROM space.  Must
348          * return 0, 1, or 2 per IEEE 1212.  */
349         int (*get_max_rom) (u_int32_t *bus_info, void *private_data);
350 };
351
352
353
354
355 /* Descriptor Leaf manipulation macros */
356 #define CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT 24
357 #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK 0xffffff
358 #define CSR1212_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
359
360 #define CSR1212_DESCRIPTOR_LEAF_TYPE(kv) \
361         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) >> CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)
362 #define CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) \
363         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[0]) & \
364          CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)
365 #define CSR1212_DESCRIPTOR_LEAF_DATA(kv) \
366         (&((kv)->value.leaf.data[1]))
367
368 #define CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, type) \
369         ((kv)->value.leaf.data[0] = \
370          CSR1212_CPU_TO_BE32(CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID(kv) | \
371                              ((type) << CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT)))
372 #define CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, spec_id) \
373         ((kv)->value.leaf.data[0] = \
374          CSR1212_CPU_TO_BE32((CSR1212_DESCRIPTOR_LEAF_TYPE(kv) << \
375                               CSR1212_DESCRIPTOR_LEAF_TYPE_SHIFT) | \
376                              ((spec_id) & CSR1212_DESCRIPTOR_LEAF_SPECIFIER_ID_MASK)))
377
378 /* Text Descriptor Leaf manipulation macros */
379 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT 28
380 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK 0xf /* after shift */
381 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT 16
382 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK 0xfff  /* after shift */
383 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
384 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD (1 * sizeof(u_int32_t))
385
386 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH(kv) \
387         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
388          CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT)
389 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET(kv) \
390         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) >> \
391                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT) & \
392                             CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK)
393 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE(kv) \
394         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]) & \
395          CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)
396 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv) \
397         (&((kv)->value.leaf.data[2]))
398
399 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, width) \
400         ((kv)->value.leaf.data[1] = \
401          ((kv)->value.leaf.data[1] & \
402           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK << \
403                                 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))) | \
404          CSR1212_CPU_TO_BE32(((width) & \
405                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_MASK) << \
406                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_WIDTH_SHIFT))
407 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, char_set) \
408         ((kv)->value.leaf.data[1] = \
409          ((kv)->value.leaf.data[1] & \
410           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK << \
411                                 CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))) | \
412          CSR1212_CPU_TO_BE32(((char_set) & \
413                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_MASK) << \
414                              CSR1212_TEXTUAL_DESCRIPTOR_LEAF_CHAR_SET_SHIFT))
415 #define CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
416         ((kv)->value.leaf.data[1] = \
417          ((kv)->value.leaf.data[1] & \
418           CSR1212_CPU_TO_BE32(~(CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
419          CSR1212_CPU_TO_BE32(((language) & \
420                               CSR1212_TEXTUAL_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
421
422
423 /* Icon Descriptor Leaf manipulation macros */
424 #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK 0xffffff
425 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT 30
426 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK 0x3 /* after shift */
427 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT 16
428 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK 0xf /* after shift */
429 #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK 0xffff
430 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT 16
431 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK 0xffff /* after shift */
432 #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK 0xffff
433 #define CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD (3 * sizeof(u_int32_t))
434
435 #define CSR1212_ICON_DESCRIPTOR_LEAF_VERSION(kv) \
436         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[2]) & \
437          CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)
438
439 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv) \
440         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
441          CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT)
442
443 #define CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv) \
444         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) >> \
445           CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT) & \
446          CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK)
447
448 #define CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE(kv) \
449         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[3]) & \
450          CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)
451
452 #define CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN(kv) \
453         ((CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) >> \
454           CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_SHIFT) & \
455          CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_HSCAN_MASK)
456
457 #define CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN(kv) \
458         (CSR1212_BE32_TO_CPU((kv)->value.leaf.data[4]) & \
459          CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)
460
461 #define CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv) \
462         (&((kv)->value.leaf.data[5]))
463
464 static inline u_int32_t *CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(struct csr1212_keyval *kv)
465 {
466         static const int pd[4] = { 0, 4, 16, 256 };
467         static const int cs[16] = { 4, 2 };
468         int ps = pd[CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH(kv)];
469
470         return &kv->value.leaf.data[5 +
471                                     (ps * cs[CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE(kv)]) /
472                            sizeof(u_int32_t)];
473 }
474
475 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version) \
476         ((kv)->value.leaf.data[2] = \
477          ((kv)->value.leaf.data[2] & \
478           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK))) | \
479          CSR1212_CPU_TO_BE32(((version) & \
480                               CSR1212_ICON_DESCRIPTOR_LEAF_VERSION_MASK)))
481
482 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth) \
483         ((kv)->value.leaf.data[3] = \
484          ((kv)->value.leaf.data[3] & \
485           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK << \
486                                 CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))) | \
487          CSR1212_CPU_TO_BE32(((palette_depth) & \
488                               CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_MASK) << \
489                              CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE_DEPTH_SHIFT))
490
491 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space) \
492         ((kv)->value.leaf.data[3] = \
493          ((kv)->value.leaf.data[3] & \
494           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK << \
495                                 CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))) | \
496          CSR1212_CPU_TO_BE32(((color_space) & \
497                               CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_MASK) << \
498                              CSR1212_ICON_DESCRIPTOR_LEAF_COLOR_SPACE_SHIFT))
499
500 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language) \
501         ((kv)->value.leaf.data[3] = \
502          ((kv)->value.leaf.data[3] & \
503           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK))) | \
504          CSR1212_CPU_TO_BE32(((language) & \
505                               CSR1212_ICON_DESCRIPTOR_LEAF_LANGUAGE_MASK)))
506
507 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan) \
508         ((kv)->value.leaf.data[4] = \
509          ((kv)->value.leaf.data[4] & \
510           CSR1212_CPU_TO_BE32(~(CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK << \
511                                 CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))) | \
512          CSR1212_CPU_TO_BE32(((hscan) & \
513                               CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_MASK) << \
514                              CSR1212_ICON_DESCRIPTOR_LEAF_HSCAN_SHIFT))
515
516 #define CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan) \
517         ((kv)->value.leaf.data[4] = \
518          (((kv)->value.leaf.data[4] & \
519           CSR1212_CPU_TO_BE32(~CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK))) | \
520          CSR1212_CPU_TO_BE32(((vscan) & \
521                               CSR1212_ICON_DESCRIPTOR_LEAF_VSCAN_MASK)))
522
523
524 /* Modifiable Descriptor Leaf manipulation macros */
525 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT 16
526 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK 0xffff
527 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_SHIFT 32
528 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK 0xffff
529 #define CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK 0xffffffffULL
530
531 #define CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE(kv) \
532         CSR1212_BE16_TO_CPU((kv)->value.leaf.data[0] >> CSR1212_MODIFIABLE_DESCRIPTOR_MAX_SIZE_SHIFT)
533
534 #define CSR1212_MODIFIABLE_DESCRIPTOR_ADDRESS(kv) \
535         (CSR1212_BE16_TO_CPU(((u_int64_t)((kv)->value.leaf.data[0])) << \
536                              CSR1212_MODIFIABLE_DESCRIPTOR_ADDR_HI_SHIFT) | \
537          CSR1212_BE32_TO_CPU((kv)->value.leaf.data[1]))
538
539 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, size) \
540         ((kv)->value.leaf.data[0] = \
541          ((kv)->value.leaf.data[0] & \
542           CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK << \
543                                 CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))) | \
544          CSR1212_CPU_TO_BE32(((size) & \
545                               CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_MASK) << \
546                              CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_MAX_SIZE_SHIFT))
547
548 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, addr) \
549         ((kv)->value.leaf.data[0] = \
550          ((kv)->value.leaf.data[0] & \
551           CSR1212_CPU_TO_BE32(~(CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK))) | \
552           CSR1212_CPU_TO_BE32(((addr) & \
553                                CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_HI_MASK)))
554
555 #define CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, addr) \
556         ((kv)->value.leaf.data[1] = \
557          CSR1212_CPU_TO_BE32(addr & CSR1212_MODIFIABLE_DESCRIPTOR_LEAF_ADDR_LO_MASK))
558
559
560
561 /* The following 2 function are for creating new Configuration ROM trees.  The
562  * first function is used for both creating local trees and parsing remote
563  * trees.  The second function adds pertinent information to local Configuration
564  * ROM trees - namely data for the bus information block. */
565 extern struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
566                                               size_t bus_info_size,
567                                               void *private_data);
568 extern void csr1212_init_local_csr(struct csr1212_csr *csr,
569                                    const u_int32_t *bus_info_data, int max_rom);
570
571
572 /* The following function destroys a Configuration ROM tree and release all
573  * memory taken by the tree. */
574 extern void csr1212_destroy_csr(struct csr1212_csr *csr);
575
576
577 /* The following set of functions are fore creating new keyvals for placement in
578  * a Configuration ROM tree.  Code that creates new keyvals with these functions
579  * must release those keyvals with csr1212_release_keyval() when they are no
580  * longer needed. */
581 extern struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value);
582 extern struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data,
583                                                size_t data_len);
584 extern struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key,
585                                                      u_int32_t csr_offset);
586 extern struct csr1212_keyval *csr1212_new_directory(u_int8_t key);
587 extern struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec,
588                                                              u_int32_t key,
589                                                              u_int32_t value);
590 extern struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec,
591                                                         u_int32_t key,
592                                                         const void *data,
593                                                         size_t data_len);
594 extern struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype,
595                                                           u_int32_t specifier_id,
596                                                           const void *data,
597                                                           size_t data_len);
598 extern struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
599                                                                   u_int16_t cset,
600                                                                   u_int16_t language,
601                                                                   const void *data,
602                                                                   size_t data_len);
603 extern struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s);
604 extern struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
605                                                                u_int8_t palette_depth,
606                                                                u_int8_t color_space,
607                                                                u_int16_t language,
608                                                                u_int16_t hscan,
609                                                                u_int16_t vscan,
610                                                                u_int32_t *palette,
611                                                                u_int32_t *pixels);
612 extern struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
613                                                                      u_int64_t address);
614 extern struct csr1212_keyval *csr1212_new_keyword_leaf(int strc,
615                                                        const char *strv[]);
616
617
618 /* The following functions manage association between keyvals.  Typically,
619  * Descriptor Leaves and Directories will be associated with another keyval and
620  * it is desirable for the Descriptor keyval to be place immediately after the
621  * keyval that it is associated with.*/
622 extern int csr1212_associate_keyval(struct csr1212_keyval *kv,
623                                     struct csr1212_keyval *associate);
624 extern void csr1212_disassociate_keyval(struct csr1212_keyval *kv);
625
626
627 /* The following functions manage the association of a keyval and directories.
628  * A keyval may be attached to more than one directory. */
629 extern int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
630                                               struct csr1212_keyval *kv);
631 extern void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
632                                                  struct csr1212_keyval *kv);
633
634
635 /* The following functions create a Configuration ROM image from the tree of
636  * keyvals provided.  csr1212_generate_csr_image() creates a complete image in
637  * the list of caches available via csr->cache_head.  The other functions are
638  * provided should there be a need to create a flat image without restrictions
639  * placed by IEEE 1212. */
640 extern struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
641                                                          struct csr1212_keyval *start_kv,
642                                                          int start_pos);
643 extern size_t csr1212_generate_layout_order(struct csr1212_keyval *kv);
644 extern void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache);
645 extern int csr1212_generate_csr_image(struct csr1212_csr *csr);
646
647
648 /* This is a convience function for reading a block of data out of one of the
649  * caches in the csr->cache_head list. */
650 extern int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer,
651                         u_int32_t len);
652
653
654 /* The following functions are in place for parsing Configuration ROM images.
655  * csr1212_parse_keyval() is used should there be a need to directly parse a
656  * Configuration ROM directly. */
657 extern int csr1212_parse_keyval(struct csr1212_keyval *kv,
658                                 struct csr1212_csr_rom_cache *cache);
659 extern int csr1212_parse_csr(struct csr1212_csr *csr);
660
661 /* These are internal functions referenced by inline functions below. */
662 extern int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv);
663 extern void _csr1212_destroy_keyval(struct csr1212_keyval *kv);
664
665
666 /* This function allocates a new cache which may be used for either parsing or
667  * generating sub-sets of Configuration ROM images. */
668 static inline struct csr1212_csr_rom_cache *csr1212_rom_cache_malloc(u_int32_t offset,
669                                                                      size_t size)
670 {
671         struct csr1212_csr_rom_cache *cache;
672
673         cache = (struct csr1212_csr_rom_cache*)CSR1212_MALLOC(sizeof(struct csr1212_csr_rom_cache) + size);
674         if (!cache)
675                 return NULL;
676
677         cache->next = NULL;
678         cache->prev = NULL;
679         cache->filled_head = NULL;
680         cache->filled_tail = NULL;
681         cache->layout_head = NULL;
682         cache->layout_tail = NULL;
683         cache->offset = offset;
684         cache->size = size;
685         cache->ext_rom = NULL;
686
687         return cache;
688 }
689
690
691 /* This function ensures that a keyval contains data when referencing a keyval
692  * created by parsing a Configuration ROM. */
693 static inline struct csr1212_keyval *csr1212_get_keyval(struct csr1212_csr *csr,
694                                                         struct csr1212_keyval *kv)
695 {
696         if (!kv)
697                 return NULL;
698         if (!kv->valid)
699                 if (_csr1212_read_keyval(csr, kv) != CSR1212_SUCCESS)
700                         return NULL;
701         return kv;
702 }
703
704
705 /* This function increments the reference count for a keyval should there be a
706  * need for code to retain a keyval that has been parsed. */
707 static inline void csr1212_keep_keyval(struct csr1212_keyval *kv)
708 {
709         kv->refcnt++;
710 }
711
712
713 /* This function decrements a keyval's reference count and will destroy the
714  * keyval when there are no more users of the keyval.  This should be called by
715  * any code that calls csr1212_keep_keyval() or any of the keyval creation
716  * routines csr1212_new_*(). */
717 static inline void csr1212_release_keyval(struct csr1212_keyval *kv)
718 {
719         if (kv->refcnt > 1)
720                 kv->refcnt--;
721         else
722                 _csr1212_destroy_keyval(kv);
723 }
724
725
726 /*
727  * This macro allows for looping over the keyval entries in a directory and it
728  * ensures that keyvals from remote ConfigROMs are parsed properly.
729  *
730  * _csr is a struct csr1212_csr * that points to CSR associated with dir.
731  * _kv is a struct csr1212_keyval * that'll point to the current keyval (loop index).
732  * _dir is a struct csr1212_keyval * that points to the directory to be looped.
733  * _pos is a struct csr1212_dentry * that is used internally for indexing.
734  *
735  * kv will be NULL upon exit of the loop.
736  */
737 #define csr1212_for_each_dir_entry(_csr, _kv, _dir, _pos)                       \
738         for (csr1212_get_keyval((_csr), (_dir)),                                \
739              _pos = (_dir)->value.directory.dentries_head,                      \
740              _kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) : NULL;        \
741              (_kv) && (_pos);                                                   \
742              (_kv->associate == NULL) ?                                         \
743                      ((_pos = _pos->next),                                      \
744                       (_kv = (_pos) ? csr1212_get_keyval((_csr), _pos->kv) :    \
745                           NULL)) :                                              \
746                      (_kv = csr1212_get_keyval((_csr), _kv->associate)))
747
748
749
750 #ifdef __cplusplus
751    }
752 #endif
753
754 #endif /* __CSR1212_H__ */
Note: See TracBrowser for help on using the browser.