Annotation of qemu/hw/smbus_eeprom.c, revision 1.1.1.4

1.1       root        1: /*
                      2:  * QEMU SMBus EEPROM device
1.1.1.2   root        3:  *
1.1       root        4:  * Copyright (c) 2007 Arastra, Inc.
1.1.1.2   root        5:  *
1.1       root        6:  * Permission is hereby granted, free of charge, to any person obtaining a copy
                      7:  * of this software and associated documentation files (the "Software"), to deal
                      8:  * in the Software without restriction, including without limitation the rights
                      9:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                     10:  * copies of the Software, and to permit persons to whom the Software is
                     11:  * furnished to do so, subject to the following conditions:
                     12:  *
                     13:  * The above copyright notice and this permission notice shall be included in
                     14:  * all copies or substantial portions of the Software.
                     15:  *
                     16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                     17:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
                     19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                     21:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
                     22:  * THE SOFTWARE.
                     23:  */
                     24: 
1.1.1.2   root       25: #include "hw.h"
                     26: #include "i2c.h"
                     27: #include "smbus.h"
1.1       root       28: 
                     29: //#define DEBUG
                     30: 
                     31: typedef struct SMBusEEPROMDevice {
1.1.1.4 ! root       32:     SMBusDevice smbusdev;
1.1       root       33:     uint8_t *data;
                     34:     uint8_t offset;
                     35: } SMBusEEPROMDevice;
                     36: 
                     37: static void eeprom_quick_cmd(SMBusDevice *dev, uint8_t read)
                     38: {
                     39: #ifdef DEBUG
1.1.1.3   root       40:     printf("eeprom_quick_cmd: addr=0x%02x read=%d\n", dev->i2c.address, read);
1.1       root       41: #endif
                     42: }
                     43: 
                     44: static void eeprom_send_byte(SMBusDevice *dev, uint8_t val)
                     45: {
                     46:     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
                     47: #ifdef DEBUG
1.1.1.3   root       48:     printf("eeprom_send_byte: addr=0x%02x val=0x%02x\n",
                     49:            dev->i2c.address, val);
1.1       root       50: #endif
                     51:     eeprom->offset = val;
                     52: }
                     53: 
                     54: static uint8_t eeprom_receive_byte(SMBusDevice *dev)
                     55: {
                     56:     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
                     57:     uint8_t val = eeprom->data[eeprom->offset++];
                     58: #ifdef DEBUG
1.1.1.3   root       59:     printf("eeprom_receive_byte: addr=0x%02x val=0x%02x\n",
                     60:            dev->i2c.address, val);
1.1       root       61: #endif
                     62:     return val;
                     63: }
                     64: 
1.1.1.2   root       65: static void eeprom_write_data(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len)
1.1       root       66: {
                     67:     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
1.1.1.2   root       68:     int n;
1.1       root       69: #ifdef DEBUG
1.1.1.3   root       70:     printf("eeprom_write_byte: addr=0x%02x cmd=0x%02x val=0x%02x\n",
                     71:            dev->i2c.address, cmd, buf[0]);
1.1       root       72: #endif
1.1.1.2   root       73:     /* An page write operation is not a valid SMBus command.
                     74:        It is a block write without a length byte.  Fortunately we
                     75:        get the full block anyway.  */
                     76:     /* TODO: Should this set the current location?  */
                     77:     if (cmd + len > 256)
                     78:         n = 256 - cmd;
                     79:     else
                     80:         n = len;
                     81:     memcpy(eeprom->data + cmd, buf, n);
                     82:     len -= n;
                     83:     if (len)
                     84:         memcpy(eeprom->data, buf + n, len);
1.1       root       85: }
                     86: 
1.1.1.2   root       87: static uint8_t eeprom_read_data(SMBusDevice *dev, uint8_t cmd, int n)
1.1       root       88: {
                     89:     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *) dev;
1.1.1.2   root       90:     /* If this is the first byte then set the current position.  */
                     91:     if (n == 0)
                     92:         eeprom->offset = cmd;
                     93:     /* As with writes, we implement block reads without the
                     94:        SMBus length byte.  */
                     95:     return eeprom_receive_byte(dev);
1.1       root       96: }
                     97: 
1.1.1.4 ! root       98: static void smbus_eeprom_init(SMBusDevice *dev)
1.1       root       99: {
1.1.1.4 ! root      100:     SMBusEEPROMDevice *eeprom = (SMBusEEPROMDevice *)dev;
1.1.1.2   root      101: 
1.1       root      102:     eeprom->offset = 0;
                    103: }
1.1.1.4 ! root      104: 
        !           105: static SMBusDeviceInfo smbus_eeprom_info = {
        !           106:     .i2c.qdev.name = "smbus-eeprom",
        !           107:     .i2c.qdev.size = sizeof(SMBusEEPROMDevice),
        !           108:     .i2c.qdev.props = (Property[]) {
        !           109:         {
        !           110:             .name   = "data",
        !           111:             .info   = &qdev_prop_ptr,
        !           112:             .offset = offsetof(SMBusEEPROMDevice, data),
        !           113:         },
        !           114:         {/* end of list */}
        !           115:     },
        !           116:     .init = smbus_eeprom_init,
        !           117:     .quick_cmd = eeprom_quick_cmd,
        !           118:     .send_byte = eeprom_send_byte,
        !           119:     .receive_byte = eeprom_receive_byte,
        !           120:     .write_data = eeprom_write_data,
        !           121:     .read_data = eeprom_read_data
        !           122: };
        !           123: 
        !           124: static void smbus_eeprom_register_devices(void)
        !           125: {
        !           126:     smbus_register_device(&smbus_eeprom_info);
        !           127: }
        !           128: 
        !           129: device_init(smbus_eeprom_register_devices)

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.