00001 /* 00002 * QEMU SMBus API 00003 * 00004 * Copyright (c) 2007 Arastra, Inc. 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining a copy 00007 * of this software and associated documentation files (the "Software"), to deal 00008 * in the Software without restriction, including without limitation the rights 00009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00010 * copies of the Software, and to permit persons to whom the Software is 00011 * furnished to do so, subject to the following conditions: 00012 * 00013 * The above copyright notice and this permission notice shall be included in 00014 * all copies or substantial portions of the Software. 00015 * 00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00019 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00022 * THE SOFTWARE. 00023 */ 00024 00025 #include "i2c.h" 00026 00027 struct SMBusDevice { 00028 /* The SMBus protocol is implemented on top of I2C. */ 00029 i2c_slave i2c; 00030 00031 /* Callbacks set by the device. */ 00032 void (*quick_cmd)(SMBusDevice *dev, uint8_t read); 00033 void (*send_byte)(SMBusDevice *dev, uint8_t val); 00034 uint8_t (*receive_byte)(SMBusDevice *dev); 00035 /* We can't distinguish between a word write and a block write with 00036 length 1, so pass the whole data block including the length byte 00037 (if present). The device is responsible figuring out what type of 00038 command this is. */ 00039 void (*write_data)(SMBusDevice *dev, uint8_t cmd, uint8_t *buf, int len); 00040 /* Likewise we can't distinguish between different reads, or even know 00041 the length of the read until the read is complete, so read data a 00042 byte at a time. The device is responsible for adding the length 00043 byte on block reads. */ 00044 uint8_t (*read_data)(SMBusDevice *dev, uint8_t cmd, int n); 00045 00046 /* Remaining fields for internal use only. */ 00047 int mode; 00048 int data_len; 00049 uint8_t data_buf[34]; /* command + len + 32 bytes of data. */ 00050 uint8_t command; 00051 }; 00052 00053 /* Create a slave device. */ 00054 SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size); 00055 00056 /* Master device commands. */ 00057 void smbus_quick_command(i2c_bus *bus, int addr, int read); 00058 uint8_t smbus_receive_byte(i2c_bus *bus, int addr); 00059 void smbus_send_byte(i2c_bus *bus, int addr, uint8_t data); 00060 uint8_t smbus_read_byte(i2c_bus *bus, int addr, uint8_t command); 00061 void smbus_write_byte(i2c_bus *bus, int addr, uint8_t command, uint8_t data); 00062 uint16_t smbus_read_word(i2c_bus *bus, int addr, uint8_t command); 00063 void smbus_write_word(i2c_bus *bus, int addr, uint8_t command, uint16_t data); 00064 int smbus_read_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data); 00065 void smbus_write_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data, 00066 int len); 00067 00068 /* smbus_eeprom.c */ 00069 void smbus_eeprom_device_init(i2c_bus *bus, uint8_t addr, uint8_t *buf);