Kill redundant declarion of perror()
[qemu-kvm/fedora.git] / hw / smbus.c
blob6eb84124017965becfe4c7b0edc65150320f4cdc
1 /*
2 * QEMU SMBus device emulation.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
8 */
10 /* TODO: Implement PEC. */
12 #include "hw.h"
13 #include "i2c.h"
14 #include "smbus.h"
16 //#define DEBUG_SMBUS 1
18 #ifdef DEBUG_SMBUS
19 #define DPRINTF(fmt, args...) \
20 do { printf("smbus(%02x): " fmt , dev->i2c.address, ##args); } while (0)
21 #define BADF(fmt, args...) \
22 do { fprintf(stderr, "smbus: error: " fmt , ##args); exit(1);} while (0)
23 #else
24 #define DPRINTF(fmt, args...) do {} while(0)
25 #define BADF(fmt, args...) \
26 do { fprintf(stderr, "smbus: error: " fmt , ##args);} while (0)
27 #endif
29 enum {
30 SMBUS_IDLE,
31 SMBUS_WRITE_DATA,
32 SMBUS_RECV_BYTE,
33 SMBUS_READ_DATA,
34 SMBUS_DONE,
35 SMBUS_CONFUSED = -1
38 static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
40 DPRINTF("Quick Command %d\n", recv);
41 if (dev->quick_cmd)
42 dev->quick_cmd(dev, recv);
45 static void smbus_do_write(SMBusDevice *dev)
47 if (dev->data_len == 0) {
48 smbus_do_quick_cmd(dev, 0);
49 } else if (dev->data_len == 1) {
50 DPRINTF("Send Byte\n");
51 if (dev->send_byte) {
52 dev->send_byte(dev, dev->data_buf[0]);
54 } else {
55 dev->command = dev->data_buf[0];
56 DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
57 if (dev->write_data) {
58 dev->write_data(dev, dev->command, dev->data_buf + 1,
59 dev->data_len - 1);
64 static void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
66 SMBusDevice *dev = (SMBusDevice *)s;
67 switch (event) {
68 case I2C_START_SEND:
69 switch (dev->mode) {
70 case SMBUS_IDLE:
71 DPRINTF("Incoming data\n");
72 dev->mode = SMBUS_WRITE_DATA;
73 break;
74 default:
75 BADF("Unexpected send start condition in state %d\n", dev->mode);
76 dev->mode = SMBUS_CONFUSED;
77 break;
79 break;
81 case I2C_START_RECV:
82 switch (dev->mode) {
83 case SMBUS_IDLE:
84 DPRINTF("Read mode\n");
85 dev->mode = SMBUS_RECV_BYTE;
86 break;
87 case SMBUS_WRITE_DATA:
88 if (dev->data_len == 0) {
89 BADF("Read after write with no data\n");
90 dev->mode = SMBUS_CONFUSED;
91 } else {
92 if (dev->data_len > 1) {
93 smbus_do_write(dev);
94 } else {
95 dev->command = dev->data_buf[0];
96 DPRINTF("%02x: Command %d\n", dev->i2c.address,
97 dev->command);
99 DPRINTF("Read mode\n");
100 dev->data_len = 0;
101 dev->mode = SMBUS_READ_DATA;
103 break;
104 default:
105 BADF("Unexpected recv start condition in state %d\n", dev->mode);
106 dev->mode = SMBUS_CONFUSED;
107 break;
109 break;
111 case I2C_FINISH:
112 switch (dev->mode) {
113 case SMBUS_WRITE_DATA:
114 smbus_do_write(dev);
115 break;
116 case SMBUS_RECV_BYTE:
117 smbus_do_quick_cmd(dev, 1);
118 break;
119 case SMBUS_READ_DATA:
120 BADF("Unexpected stop during receive\n");
121 break;
122 default:
123 /* Nothing to do. */
124 break;
126 dev->mode = SMBUS_IDLE;
127 dev->data_len = 0;
128 break;
130 case I2C_NACK:
131 switch (dev->mode) {
132 case SMBUS_DONE:
133 /* Nothing to do. */
134 break;
135 case SMBUS_READ_DATA:
136 dev->mode = SMBUS_DONE;
137 break;
138 default:
139 BADF("Unexpected NACK in state %d\n", dev->mode);
140 dev->mode = SMBUS_CONFUSED;
141 break;
146 static int smbus_i2c_recv(i2c_slave *s)
148 SMBusDevice *dev = (SMBusDevice *)s;
149 int ret;
151 switch (dev->mode) {
152 case SMBUS_RECV_BYTE:
153 if (dev->receive_byte) {
154 ret = dev->receive_byte(dev);
155 } else {
156 ret = 0;
158 DPRINTF("Receive Byte %02x\n", ret);
159 dev->mode = SMBUS_DONE;
160 break;
161 case SMBUS_READ_DATA:
162 if (dev->read_data) {
163 ret = dev->read_data(dev, dev->command, dev->data_len);
164 dev->data_len++;
165 } else {
166 ret = 0;
168 DPRINTF("Read data %02x\n", ret);
169 break;
170 default:
171 BADF("Unexpected read in state %d\n", dev->mode);
172 dev->mode = SMBUS_CONFUSED;
173 ret = 0;
174 break;
176 return ret;
179 static int smbus_i2c_send(i2c_slave *s, uint8_t data)
181 SMBusDevice *dev = (SMBusDevice *)s;
182 switch (dev->mode) {
183 case SMBUS_WRITE_DATA:
184 DPRINTF("Write data %02x\n", data);
185 dev->data_buf[dev->data_len++] = data;
186 break;
187 default:
188 BADF("Unexpected write in state %d\n", dev->mode);
189 break;
191 return 0;
194 SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size)
196 SMBusDevice *dev;
198 if (size < sizeof(SMBusDevice))
199 hw_error("SMBus struct too small");
201 dev = (SMBusDevice *)i2c_slave_init(bus, address, size);
202 dev->i2c.event = smbus_i2c_event;
203 dev->i2c.recv = smbus_i2c_recv;
204 dev->i2c.send = smbus_i2c_send;
206 return dev;
209 /* Master device commands. */
210 void smbus_quick_command(i2c_bus *bus, int addr, int read)
212 i2c_start_transfer(bus, addr, read);
213 i2c_end_transfer(bus);
216 uint8_t smbus_receive_byte(i2c_bus *bus, int addr)
218 uint8_t data;
220 i2c_start_transfer(bus, addr, 1);
221 data = i2c_recv(bus);
222 i2c_nack(bus);
223 i2c_end_transfer(bus);
224 return data;
227 void smbus_send_byte(i2c_bus *bus, int addr, uint8_t data)
229 i2c_start_transfer(bus, addr, 0);
230 i2c_send(bus, data);
231 i2c_end_transfer(bus);
234 uint8_t smbus_read_byte(i2c_bus *bus, int addr, uint8_t command)
236 uint8_t data;
237 i2c_start_transfer(bus, addr, 0);
238 i2c_send(bus, command);
239 i2c_start_transfer(bus, addr, 1);
240 data = i2c_recv(bus);
241 i2c_nack(bus);
242 i2c_end_transfer(bus);
243 return data;
246 void smbus_write_byte(i2c_bus *bus, int addr, uint8_t command, uint8_t data)
248 i2c_start_transfer(bus, addr, 0);
249 i2c_send(bus, command);
250 i2c_send(bus, data);
251 i2c_end_transfer(bus);
254 uint16_t smbus_read_word(i2c_bus *bus, int addr, uint8_t command)
256 uint16_t data;
257 i2c_start_transfer(bus, addr, 0);
258 i2c_send(bus, command);
259 i2c_start_transfer(bus, addr, 1);
260 data = i2c_recv(bus);
261 data |= i2c_recv(bus) << 8;
262 i2c_nack(bus);
263 i2c_end_transfer(bus);
264 return data;
267 void smbus_write_word(i2c_bus *bus, int addr, uint8_t command, uint16_t data)
269 i2c_start_transfer(bus, addr, 0);
270 i2c_send(bus, command);
271 i2c_send(bus, data & 0xff);
272 i2c_send(bus, data >> 8);
273 i2c_end_transfer(bus);
276 int smbus_read_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data)
278 int len;
279 int i;
281 i2c_start_transfer(bus, addr, 0);
282 i2c_send(bus, command);
283 i2c_start_transfer(bus, addr, 1);
284 len = i2c_recv(bus);
285 if (len > 32)
286 len = 0;
287 for (i = 0; i < len; i++)
288 data[i] = i2c_recv(bus);
289 i2c_nack(bus);
290 i2c_end_transfer(bus);
291 return len;
294 void smbus_write_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data,
295 int len)
297 int i;
299 if (len > 32)
300 len = 32;
302 i2c_start_transfer(bus, addr, 0);
303 i2c_send(bus, command);
304 i2c_send(bus, len);
305 for (i = 0; i < len; i++)
306 i2c_send(bus, data[i]);
307 i2c_end_transfer(bus);