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