71 lines
2.1 KiB
C

#include "lab.h"
void backend_init(struct backend *backend)
{
size_t bank;
size_t index;
memset(backend, 0, sizeof(*backend));
for (bank = 0; bank < BANKS; bank++) {
for (index = 0; index < 256; index++) {
backend->storage[bank][index] = (unsigned char)((index * 17 + bank * 29) & 255);
}
}
}
void backend_control(struct backend *backend, const struct command *command,
struct reply *reply)
{
int bank = backend->selected;
if (command->count != 3 || command->argument[0] < 0 ||
command->argument[0] >= 8) {
reply->result = -EINVAL;
return;
}
backend->value[bank][command->argument[0]] = command->argument[1];
if (command->argument[2] >= 0) {
size_t index;
for (index = 0; index < 256; index++) {
backend->storage[bank][index] = (unsigned char)
((index * 17 + command->argument[2]) & 255);
}
}
}
int device_call(struct backend *backend, int operation, int a, int b,
unsigned char *bytes, size_t length)
{
int bank = backend->selected;
int *state = backend->value[bank];
int result = fault_take(backend, operation - 1);
(void)b;
if (!result) {
if (operation == 1) {
if (state[0]) {
result = -EBUSY;
} else {
state[0] = 1;
state[1] = a;
}
} else if (operation == 5) {
state[0] = 0;
} else if (!state[0]) {
result = -ENODEV;
} else if (operation == 2) {
state[2] = a;
} else if (operation == 3) {
if (state[2] != 1 || length != 1 ||
(bytes[0] != '0' && bytes[0] != '1')) {
result = -EINVAL;
} else {
state[3] = bytes[0] - '0';
}
} else if (operation == 4) {
bytes[0] = (unsigned char)('0' + state[3]);
} else {
result = -ENOTTY;
}
}
trace_add(&backend->trace, "SYSFS", operation, a, result);
return result;
}