root/branches/streaming-rework/src/libieee1394/csr1212.h

Revision 413, 28.3 kB (checked in by pieterpalmers, 14 years ago)

- moved all generic IEEE1394 classes into libieee1394

src/libieee1394/ieee1394service.h
src/libieee1394/csr1212.h
src/libieee1394/configrom.cpp
src/libieee1394/configrom.h
src/libieee1394/ieee1394service.cpp
src/libieee1394/csr1212.c

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