From patchwork Tue Sep 2 18:02:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 4827591 Return-Path: X-Original-To: patchwork-linux-sh@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CE909C0338 for ; Tue, 2 Sep 2014 18:02:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A077720176 for ; Tue, 2 Sep 2014 18:02:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 253D520170 for ; Tue, 2 Sep 2014 18:02:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754692AbaIBSCq (ORCPT ); Tue, 2 Sep 2014 14:02:46 -0400 Received: from sauhun.de ([89.238.76.85]:56527 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754342AbaIBSCq (ORCPT ); Tue, 2 Sep 2014 14:02:46 -0400 Received: from p4fe244cd.dip0.t-ipconnect.de ([79.226.68.205]:56193 helo=localhost) by pokefinder.org with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1XOsPf-0004su-7S; Tue, 02 Sep 2014 20:02:39 +0200 From: Wolfram Sang To: linux-i2c@vger.kernel.org Cc: linux-sh@vger.kernel.org, Wolfram Sang Subject: [PATCH 2/3] WIP: sketch of an slave-eeprom simulator Date: Tue, 2 Sep 2014 20:02:40 +0200 Message-Id: <1409680961-12632-3-git-send-email-wsa@the-dreams.de> X-Mailer: git-send-email 2.0.0 In-Reply-To: <1409680961-12632-1-git-send-email-wsa@the-dreams.de> References: <1409680961-12632-1-git-send-email-wsa@the-dreams.de> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org X-Spam-Status: No, score=-8.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wolfram Sang Not for upstream yet. Signed-off-by: Wolfram Sang --- drivers/i2c/i2c-slave-eeprom.c | 140 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 drivers/i2c/i2c-slave-eeprom.c diff --git a/drivers/i2c/i2c-slave-eeprom.c b/drivers/i2c/i2c-slave-eeprom.c new file mode 100644 index 000000000000..f12edd316ad5 --- /dev/null +++ b/drivers/i2c/i2c-slave-eeprom.c @@ -0,0 +1,140 @@ +/* + * I2C Slave mode EEPROM simulator + * + * Copyright (C) 2014 by Wolfram Sang + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define EEPROM_SIZE 256 + +struct i2c_slave_eeprom_data { + struct mutex lock; + struct bin_attribute bin; + u8 buffer[EEPROM_SIZE]; + u8 eeprom_ptr; + bool first_write_done; +}; + +static int at24s_slave_cb(struct i2c_client *client, enum i2c_slave_event event, u8 *val) +{ + struct i2c_slave_eeprom_data *at24s = i2c_get_clientdata(client); + + switch (event) { + case I2C_SLAVE_REQ_WRITE: + if (!at24s->first_write_done) + at24s->eeprom_ptr = *val; + else + at24s->buffer[at24s->eeprom_ptr++] = *val; + + at24s->first_write_done = true; + break; + + case I2C_SLAVE_REQ_READ: + *val = at24s->buffer[at24s->eeprom_ptr++]; + break; + + case I2C_SLAVE_STOP: + at24s->first_write_done = false; + break; + } + + return 0; +} + +static ssize_t at24s_bin_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, char *buf, loff_t off, size_t count) +{ + struct i2c_slave_eeprom_data *at24s; + + at24s = dev_get_drvdata(container_of(kobj, struct device, kobj)); + + memcpy(buf, &at24s->buffer[off], count); + + return count; +} + +static ssize_t at24s_bin_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, char *buf, loff_t off, size_t count) +{ + struct i2c_slave_eeprom_data *at24s; + + if (unlikely(off >= attr->size)) + return -EFBIG; + + at24s = dev_get_drvdata(container_of(kobj, struct device, kobj)); + + memcpy(&at24s->buffer[off], buf, count); + + return count; +} + +static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_device_id *id) +{ + struct i2c_slave_eeprom_data *at24s; + int ret; + + at24s = devm_kzalloc(&client->dev, sizeof(struct i2c_slave_eeprom_data), GFP_KERNEL); + if (!at24s) + return -ENOMEM; + + i2c_set_clientdata(client, at24s); + + ret = i2c_slave_register(client, at24s_slave_cb); + if (ret) + return ret; + + sysfs_bin_attr_init(&at24s->bin); + at24s->bin.attr.name = "slave-eeprom"; + at24s->bin.attr.mode = S_IRUSR | S_IWUSR; + at24s->bin.read = at24s_bin_read; + at24s->bin.write = at24s_bin_write; + at24s->bin.size = EEPROM_SIZE; + + ret = sysfs_create_bin_file(&client->dev.kobj, &at24s->bin); + if (ret) + return ret; + + return 0; +}; + +static int i2c_slave_eeprom_remove(struct i2c_client *client) +{ + struct i2c_slave_eeprom_data *at24s; + + at24s = i2c_get_clientdata(client); + sysfs_remove_bin_file(&client->dev.kobj, &at24s->bin); + + return i2c_slave_unregister(client); +} + +static const struct i2c_device_id i2c_slave_eeprom_id[] = { + { "i2c-slave-eeprom", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id); + +static struct i2c_driver i2c_slave_eeprom_driver = { + .driver = { + .name = "i2c-slave-eeprom", + .owner = THIS_MODULE, + }, + .probe = i2c_slave_eeprom_probe, + .remove = i2c_slave_eeprom_remove, + .id_table = i2c_slave_eeprom_id, +}; +module_i2c_driver(i2c_slave_eeprom_driver); + +MODULE_AUTHOR("Wolfram Sang "); +MODULE_DESCRIPTION("I2C Slave mode EEPROM simulator"); +MODULE_LICENSE("GPL v2");