Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753581AbcDRLxX (ORCPT ); Mon, 18 Apr 2016 07:53:23 -0400 Received: from mail-db3on0126.outbound.protection.outlook.com ([157.55.234.126]:62571 "EHLO emea01-db3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751490AbcDRLxV (ORCPT ); Mon, 18 Apr 2016 07:53:21 -0400 Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=toradex.com; From: Bhuvanchandra DV To: CC: , , , , Bhuvanchandra DV , Stefan Agner Subject: [RFC 1/2] spi: spidev: Use 'new_id' sysfs file for enabling spidev Date: Mon, 18 Apr 2016 16:48:27 +0530 Message-ID: <1460978308-8062-2-git-send-email-bhuvanchandra.dv@toradex.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1460978308-8062-1-git-send-email-bhuvanchandra.dv@toradex.com> References: <1460978308-8062-1-git-send-email-bhuvanchandra.dv@toradex.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [115.115.225.206] X-ClientProxiedBy: HKXPR03CA0066.apcprd03.prod.outlook.com (10.163.104.24) To AMSPR05MB262.eurprd05.prod.outlook.com (10.242.95.19) X-MS-Office365-Filtering-Correlation-Id: 1a3aa3e6-f92a-4f1b-0b46-08d3677b3df6 X-Microsoft-Exchange-Diagnostics: 1;AMSPR05MB262;2:oiFfr01Hq2wM49jKOzvCU7AWRcZxlLQjph/NAwu8+CKLEcKtsOnDbv/zDSROHLaIsdOJv3EslnwWdjbsqt3RDcCiRfRJaEDWkAuydNGkQugKt4qCrBJ8z1YFFsRBbhYnj/JO7nJlj2tcAZ3T0Wjtns0xWLA9H9e5z616Cvfg73T9U8WKSPKuXEz/tz4Yswr4;3:Tj424q2gXzca6HlIGS6YnveMILH8LePNWzPY9NEFbQgGz6bU6B6W8rVG3ZqwcMzVQjMR+4TEE0iK/aCL8qRuSFYwasujOMnoE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s8/F+Obr1lVvl4aAuYgDiXRaNRfXLUkD3xDjFioOIMhBXvSPila6xyJmwXsrJKBoh7iMrZ8fnd6j2eVtM0uQ2YT/CXkNzeasBHMaIyxEaqazQc8fjTAcUEskhq9lLOOyNg== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(9101521026)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046);SRVR:AMSPR05MB262;BCL:0;PCL:0;RULEID:;SRVR:AMSPR05MB262; X-Forefront-PRVS: 0916FC3A18 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4630300001)(6009001)(50944005)(77096005)(586003)(92566002)(189998001)(36756003)(15975445007)(47776003)(229853001)(4326007)(2351001)(66066001)(3846002)(6116002)(1096002)(2950100001)(76176999)(50986999)(107886002)(110136002)(86362001)(33646002)(2906002)(19580395003)(19580405001)(53416004)(50466002)(81166005)(48376002)(42186005)(50226001)(5008740100001)(4001430100002);DIR:OUT;SFP:1102;SCL:1;SRVR:AMSPR05MB262;H:linuxdev-in.toradex.int;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: 1;AMSPR05MB262;23:ANxo88tX67bVi2VPZ9U3G6HrpMubTdwSogxigNWnCe1QVREE6KVYblpbbAbS65XhbAUEbFuJdQDmba7lvYuSWu5knTYmlq1jZPfAcnn0CSum5H90chAH0NdBguyUP5WvuT9NXPonXFCYP03PqemoTQQ7cCpRFEQViyaXI5KWei/1NatOuD5utPduiuIX04TQ2z9H8E4JVUDinuxLYQkJLPw7Lshc+rfyPqwEmN/9P/vkG6FOSriHoANCt6KJMQCxM2dqHliJCNFJVnjAI+JsmsahIHRNqrAmYL2scKdFZBfNt13E9YAsVpNKNrlsNEC/pENh0okmzyjSytFFSylQ2o0htmkGyNaqIKkjgTZ2Q2s3a5gR3VrcDosWa0azxVXBEoXgkapdzVyOww1KdgCB31uLqw1rxreT2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uqUVCJCdTzwIdsyILc0kxgOj9WWE9lwlRl6o= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: toradex.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Apr 2016 11:18:57.5669 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AMSPR05MB262 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4023 Lines: 136 Use sysfs new_id file to enable spidev at runtime. new_id file accepts bus number and chip select in 'B.C' format. With reference to the last ML discussion here[1] regarding the solution for spidev with device trees. This patch adds support to load spidevB.C via sysfs new_id file as suggested by Greg. [1] http://thread.gmane.org/gmane.linux.kernel.spi.devel/20331/focus=20369 Suggested-by: Greg Kroah-Hartman Signed-off-by: Bhuvanchandra DV Signed-off-by: Stefan Agner --- Documentation/ABI/testing/sysfs-bus-spi-spidev | 8 +++ drivers/spi/spidev.c | 75 +++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-spi-spidev diff --git a/Documentation/ABI/testing/sysfs-bus-spi-spidev b/Documentation/ABI/testing/sysfs-bus-spi-spidev new file mode 100644 index 0000000..3f6e092 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-bus-spi-spidev @@ -0,0 +1,8 @@ +What: /sys/bus/spi/drivers/spidev/new_id +Date: March 2016 +Description: + This allows to load spidev at runtime. new_id file accepts bus + number and chip select in 'B.C' format. + e.g. + To load spidev1.1 at runtime: + $ echo 1.1 > /sys/bus/spi/drivers/spidev/new_id diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index e3c19f3..99e9842 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -667,6 +667,57 @@ static int spidev_release(struct inode *inode, struct file *filp) return 0; } +/** + * new_id_store - add a new spidevB.C instance + * @driver: target device driver + * @buf: buffer for scanning bus number and chip select + * @count: input size + * + * Adds a new dynamic spidev instance based on the requested bus number + * and chip select. + */ +static ssize_t new_id_store(struct device_driver *drv, const char *buf, + size_t count) +{ + int ret; + u32 bus, cs; + struct spi_device *spi; + struct spi_master *master; + + ret = sscanf(buf, "%u.%u", &bus, &cs); + + if (ret < 2) + return -EINVAL; + + master = spi_busnum_to_master(bus); + if (!master) + return -ENODEV; + + if (cs >= master->num_chipselect) + return -ENODEV; + + spi = spi_alloc_device(master); + if (!spi) { + dev_err(&master->dev, "Couldn't allocate spidev device\n"); + return -ENOMEM;; + } + + spi->chip_select = cs; + master->bus_num = bus; + + strlcpy(spi->modalias, "spidev", sizeof(spi->modalias)); + + ret = spi_add_device(spi); + if (ret) { + dev_err(&master->dev, "Couldn't add spidev device\n"); + spi_dev_put(spi); + return ret; + } + + return count; +} +static DRIVER_ATTR_WO(new_id); + static const struct file_operations spidev_fops = { .owner = THIS_MODULE, /* REVISIT switch to aio primitives, so that userspace @@ -817,21 +868,33 @@ static int __init spidev_init(void) spidev_class = class_create(THIS_MODULE, "spidev"); if (IS_ERR(spidev_class)) { - unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); - return PTR_ERR(spidev_class); + status = PTR_ERR(spidev_class); + goto err_unregister_chrdev; } status = spi_register_driver(&spidev_spi_driver); - if (status < 0) { - class_destroy(spidev_class); - unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); - } + if (status < 0) + goto err_destroy_class; + + status = driver_create_file(&spidev_spi_driver.driver, &driver_attr_new_id); + if (status < 0) + goto err_unregister_driver; + + return status; + +err_unregister_driver: + spi_unregister_driver(&spidev_spi_driver); +err_destroy_class: + class_destroy(spidev_class); +err_unregister_chrdev: + unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); return status; } module_init(spidev_init); static void __exit spidev_exit(void) { + driver_remove_file(&spidev_spi_driver.driver, &driver_attr_new_id); spi_unregister_driver(&spidev_spi_driver); class_destroy(spidev_class); unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); -- 2.7.3