📄 csr1212.c
字号:
/* * csr1212.c -- IEEE 1212 Control and Status Register support for Linux * * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za> * Steve Kinneberg <kinnebergsteve@acmsystems.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* TODO List: * - Verify interface consistency: i.e., public functions that take a size * parameter expect size to be in bytes. * - Convenience functions for reading a block of data from a given offset. */#ifndef __KERNEL__#include <string.h>#endif#include "csr1212.h"/* Permitted key type for each key id */#define __I (1 << CSR1212_KV_TYPE_IMMEDIATE)#define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)#define __D (1 << CSR1212_KV_TYPE_DIRECTORY)#define __L (1 << CSR1212_KV_TYPE_LEAF)static const u_int8_t csr1212_key_id_type_map[0x30] = { __C, /* used by Apple iSight */ __D | __L, /* Descriptor */ __I | __D | __L, /* Bus_Dependent_Info */ __I | __D | __L, /* Vendor */ __I, /* Hardware_Version */ 0, 0, /* Reserved */ __D | __L | __I, /* Module */ __I, 0, 0, 0, /* used by Apple iSight, Reserved */ __I, /* Node_Capabilities */ __L, /* EUI_64 */ 0, 0, 0, /* Reserved */ __D, /* Unit */ __I, /* Specifier_ID */ __I, /* Version */ __I | __C | __D | __L, /* Dependent_Info */ __L, /* Unit_Location */ 0, /* Reserved */ __I, /* Model */ __D, /* Instance */ __L, /* Keyword */ __D, /* Feature */ __L, /* Extended_ROM */ __I, /* Extended_Key_Specifier_ID */ __I, /* Extended_Key */ __I | __C | __D | __L, /* Extended_Data */ __L, /* Modifiable_Descriptor */ __I, /* Directory_ID */ __I, /* Revision */};#undef __I#undef __C#undef __D#undef __L#define quads_to_bytes(_q) ((_q) * sizeof(u_int32_t))#define bytes_to_quads(_b) (((_b) + sizeof(u_int32_t) - 1) / sizeof(u_int32_t))static inline void free_keyval(struct csr1212_keyval *kv){ if ((kv->key.type == CSR1212_KV_TYPE_LEAF) && (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)) CSR1212_FREE(kv->value.leaf.data); CSR1212_FREE(kv);}static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length){ int shift; u_int32_t data; u_int16_t sum, crc = 0; for (; length; length--) { data = CSR1212_BE32_TO_CPU(*buffer); buffer++; for (shift = 28; shift >= 0; shift -= 4 ) { sum = ((crc >> 12) ^ (data >> shift)) & 0xf; crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum); } crc &= 0xffff; } return CSR1212_CPU_TO_BE16(crc);}#if 0/* Microsoft computes the CRC with the bytes in reverse order. Therefore we * have a special version of the CRC algorithm to account for their buggy * software. */static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length){ int shift; u_int32_t data; u_int16_t sum, crc = 0; for (; length; length--) { data = CSR1212_LE32_TO_CPU(*buffer); buffer++; for (shift = 28; shift >= 0; shift -= 4 ) { sum = ((crc >> 12) ^ (data >> shift)) & 0xf; crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum); } crc &= 0xffff; } return CSR1212_CPU_TO_BE16(crc);}#endifstatic inline struct csr1212_dentry *csr1212_find_keyval(struct csr1212_keyval *dir, struct csr1212_keyval *kv){ struct csr1212_dentry *pos; for (pos = dir->value.directory.dentries_head; pos != NULL; pos = pos->next) { if (pos->kv == kv) return pos; } return NULL;}static inline struct csr1212_keyval *csr1212_find_keyval_offset(struct csr1212_keyval *kv_list, u_int32_t offset){ struct csr1212_keyval *kv; for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next) { if (kv->offset == offset) return kv; } return NULL;}/* Creation Routines */struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops, size_t bus_info_size, void *private){ struct csr1212_csr *csr; csr = CSR1212_MALLOC(sizeof(*csr)); if (!csr) return NULL; csr->cache_head = csr1212_rom_cache_malloc(CSR1212_CONFIG_ROM_SPACE_OFFSET, CSR1212_CONFIG_ROM_SPACE_SIZE); if (!csr->cache_head) { CSR1212_FREE(csr); return NULL; } /* The keyval key id is not used for the root node, but a valid key id * that can be used for a directory needs to be passed to * csr1212_new_directory(). */ csr->root_kv = csr1212_new_directory(CSR1212_KV_ID_VENDOR); if (!csr->root_kv) { CSR1212_FREE(csr->cache_head); CSR1212_FREE(csr); return NULL; } csr->bus_info_data = csr->cache_head->data; csr->bus_info_len = bus_info_size; csr->crc_len = bus_info_size; csr->ops = ops; csr->private = private; csr->cache_tail = csr->cache_head; return csr;}void csr1212_init_local_csr(struct csr1212_csr *csr, const u_int32_t *bus_info_data, int max_rom){ static const int mr_map[] = { 4, 64, 1024, 0 };#ifdef __KERNEL__ BUG_ON(max_rom & ~0x3); csr->max_rom = mr_map[max_rom];#else if (max_rom & ~0x3) /* caller supplied invalid argument */ csr->max_rom = 0; else csr->max_rom = mr_map[max_rom];#endif memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);}static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key){ struct csr1212_keyval *kv; if (key < 0x30 && ((csr1212_key_id_type_map[key] & (1 << type)) == 0)) return NULL; kv = CSR1212_MALLOC(sizeof(*kv)); if (!kv) return NULL; kv->key.type = type; kv->key.id = key; kv->associate = NULL; kv->refcnt = 1; kv->next = NULL; kv->prev = NULL; kv->offset = 0; kv->valid = 0; return kv;}struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value){ struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key); if (!kv) return NULL; kv->value.immediate = value; kv->valid = 1; return kv;}struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t data_len){ struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key); if (!kv) return NULL; if (data_len > 0) { kv->value.leaf.data = CSR1212_MALLOC(data_len); if (!kv->value.leaf.data) { CSR1212_FREE(kv); return NULL; } if (data) memcpy(kv->value.leaf.data, data, data_len); } else { kv->value.leaf.data = NULL; } kv->value.leaf.len = bytes_to_quads(data_len); kv->offset = 0; kv->valid = 1; return kv;}struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset){ struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key); if (!kv) return NULL; kv->value.csr_offset = csr_offset; kv->offset = 0; kv->valid = 1; return kv;}struct csr1212_keyval *csr1212_new_directory(u_int8_t key){ struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key); if (!kv) return NULL; kv->value.directory.len = 0; kv->offset = 0; kv->value.directory.dentries_head = NULL; kv->value.directory.dentries_tail = NULL; kv->valid = 1; return kv;}int csr1212_associate_keyval(struct csr1212_keyval *kv, struct csr1212_keyval *associate){ if (!kv || !associate) return CSR1212_EINVAL; if (kv->key.id == CSR1212_KV_ID_DESCRIPTOR || (associate->key.id != CSR1212_KV_ID_DESCRIPTOR && associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO && associate->key.id != CSR1212_KV_ID_EXTENDED_KEY && associate->key.id != CSR1212_KV_ID_EXTENDED_DATA && associate->key.id < 0x30)) return CSR1212_EINVAL; if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID && associate->key.id != CSR1212_KV_ID_EXTENDED_KEY) return CSR1212_EINVAL; if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY && associate->key.id != CSR1212_KV_ID_EXTENDED_DATA) return CSR1212_EINVAL; if (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY && kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) return CSR1212_EINVAL; if (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA && kv->key.id != CSR1212_KV_ID_EXTENDED_KEY) return CSR1212_EINVAL; if (kv->associate) csr1212_release_keyval(kv->associate); associate->refcnt++; kv->associate = associate; return CSR1212_SUCCESS;}int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir, struct csr1212_keyval *kv){ struct csr1212_dentry *dentry; if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY) return CSR1212_EINVAL; dentry = CSR1212_MALLOC(sizeof(*dentry)); if (!dentry) return CSR1212_ENOMEM; dentry->kv = kv; kv->refcnt++; dentry->next = NULL; dentry->prev = dir->value.directory.dentries_tail; if (!dir->value.directory.dentries_head) dir->value.directory.dentries_head = dentry; if (dir->value.directory.dentries_tail) dir->value.directory.dentries_tail->next = dentry; dir->value.directory.dentries_tail = dentry; return CSR1212_SUCCESS;}struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec, u_int32_t key, u_int32_t value){ struct csr1212_keyval *kvs, *kvk, *kvv; kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec); kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key); kvv = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_DATA, value); if (!kvs || !kvk || !kvv) { if (kvs) free_keyval(kvs); if (kvk) free_keyval(kvk); if (kvv) free_keyval(kvv); return NULL; } /* Don't keep a local reference to the extended key or value. */ kvk->refcnt = 0; kvv->refcnt = 0; csr1212_associate_keyval(kvk, kvv); csr1212_associate_keyval(kvs, kvk); return kvs;}struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec, u_int32_t key, const void *data, size_t data_len){ struct csr1212_keyval *kvs, *kvk, *kvv; kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec); kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key); kvv = csr1212_new_leaf(CSR1212_KV_ID_EXTENDED_DATA, data, data_len); if (!kvs || !kvk || !kvv) { if (kvs) free_keyval(kvs); if (kvk) free_keyval(kvk); if (kvv) free_keyval(kvv); return NULL; } /* Don't keep a local reference to the extended key or value. */ kvk->refcnt = 0; kvv->refcnt = 0; csr1212_associate_keyval(kvk, kvv); csr1212_associate_keyval(kvs, kvk); return kvs;}struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t specifier_id, const void *data, size_t data_len){ struct csr1212_keyval *kv; kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL, data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD); if (!kv) return NULL; CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype); CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id); if (data) { memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len); } return kv;}struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth, u_int16_t cset, u_int16_t language, const void *data, size_t data_len){ struct csr1212_keyval *kv; char *lstr; kv = csr1212_new_descriptor_leaf(0, 0, NULL, data_len + CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD); if (!kv) return NULL; CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, cwidth); CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, cset); CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language); lstr = (char*)CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv); /* make sure last quadlet is zeroed out */ *((u_int32_t*)&(lstr[(data_len - 1) & ~0x3])) = 0; /* don't copy the NUL terminator */ memcpy(lstr, data, data_len); return kv;}static int csr1212_check_minimal_ascii(const char *s){ static const char minimal_ascii_table[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x0a, 0x00, 0x0C, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x21, 0x22, 0x00, 0x00, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, }; for (; *s; s++) { if (minimal_ascii_table[*s & 0x7F] != *s) return -1; /* failed */ } /* String conforms to minimal-ascii, as specified by IEEE 1212, * par. 7.4 */ return 0;}struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s){ /* Check if string conform to minimal_ascii format */ if (csr1212_check_minimal_ascii(s)) return NULL; /* IEEE 1212, par. 7.5.4.1 Textual descriptors (minimal ASCII) */ return csr1212_new_textual_descriptor_leaf(0, 0, 0, s, strlen(s));}struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version, u_int8_t palette_depth, u_int8_t color_space, u_int16_t language, u_int16_t hscan, u_int16_t vscan, u_int32_t *palette, u_int32_t *pixels){ static const int pd[4] = { 0, 4, 16, 256 }; static const int cs[16] = { 4, 2 }; struct csr1212_keyval *kv; int palette_size; int pixel_size = (hscan * vscan + 3) & ~0x3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -