From patchwork Sat Aug 12 11:54:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vikram N X-Patchwork-Id: 9897045 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EABB6603B4 for ; Sat, 12 Aug 2017 11:55:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BD59B28B11 for ; Sat, 12 Aug 2017 11:55:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE5B028B3D; Sat, 12 Aug 2017 11:55:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4425C28B11 for ; Sat, 12 Aug 2017 11:55:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751205AbdHLLzE (ORCPT ); Sat, 12 Aug 2017 07:55:04 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:38322 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750844AbdHLLzE (ORCPT ); Sat, 12 Aug 2017 07:55:04 -0400 Received: by mail-pf0-f195.google.com with SMTP id h75so5693187pfh.5; Sat, 12 Aug 2017 04:55:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=qZDC32YsiuvvjqJCQn+OmYKMcRBRcaJ0GU9u7+XNlo8=; b=ETPSqk0rgWT0c0WS5w4OOSqalFpXfpvQ/Om1TFvGftmoCSQ6KX1B09DXyUpTu+Kfmd 7l9QT/izNnWS943xrLj71pDs1rfJW7yphxOh8YfDw2SMe33h24d1kvd/6zjJlv4ULJlD v1sc+qCoFS6OTIimC1ovpOQS+YWh6xDDoW17QD/thRv91stlpDghJx0z/uvFHLhX5VCD OYWnUx/zEme88Dbp8WgPjwPHCJvDosuyMZCfQY1jzLVS64LXQs4/sesH/edqL9FL6Pif 6yWBYaz+nqW+V/8oTMHy55ZVbO5oQz8JKo+YVuRWDJHuRv6WqqCF/ySnW6P/a+Sqxh/t TBeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=qZDC32YsiuvvjqJCQn+OmYKMcRBRcaJ0GU9u7+XNlo8=; b=LxcYABUeGk8WpMV7aSpaKUOWi6tA988IGmybbQW1ddoCs5uUcb+G9vkQpk2XDrZ1vH d42012IBvnAVvOBkX48NDf4b10pk0sQpaQBC4UfxEKQyQ3HbDys9YiKj5YdJIfrALXEc OxSxDUYZ9fYbYmG4aJNZ/s2NuZkn20JR3qoXJxUmKw89scX0KHw3V5hQVp+l6kyT1sC2 ZByctS2wj/xhc8b0RWfrGJkzyJ3n39WjOfUR6OX0obWhWY3QXpuOSsXnJ4Xfed1O7fAA IzjRAQ9rpalIHCh80DszpUn1GCHVHYumInlDhJW0v+TjTFFVlkOwgBzsY1CAWs0XfuHr Xveg== X-Gm-Message-State: AHYfb5i53QAuegnAMBqt/zCIjVYUAgU35pSqwqqKGXHPPsa2XiUhZxm7 2lWZDtzcI5x6aA== X-Received: by 10.99.16.65 with SMTP id 1mr18169346pgq.208.1502538903361; Sat, 12 Aug 2017 04:55:03 -0700 (PDT) Received: from localhost.localdomain ([106.51.242.255]) by smtp.googlemail.com with ESMTPSA id b68sm5800752pfd.33.2017.08.12.04.55.00 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 12 Aug 2017 04:55:02 -0700 (PDT) From: Vikram N To: broonie@kernel.org Cc: Vikram N , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] spi: spidev: add exclusive bus access lock via ioctls Date: Sat, 12 Aug 2017 17:24:03 +0530 Message-Id: <1502538843-3283-1-git-send-email-vicky773@gmail.com> X-Mailer: git-send-email 1.7.9.5 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There are use-cases wherein one or more applications wants to communicate with the same spi slave chip and they wish to have exclusive bus access. May to serialize their command,response patterns. This can be realized in two ways, - Identify different applications using same spi channel and implement synchronization mechanisms to get exclusive bus access - Provide some mechanism through spidev driver to get exclusive spi bus access like via ioctls This patch implements the second approach which eliminates the need for implementing complex IPC mechanisms in the applications which shares the spi bus. This becomes even harder if we don't have access to the source code of the application which is using the spi channel. Signed-off-by: Vikram N --- drivers/spi/spidev.c | 14 +++++++++++++- include/uapi/linux/spi/spidev.h | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c index cda1071..d14a8c1 100644 --- a/drivers/spi/spidev.c +++ b/drivers/spi/spidev.c @@ -85,6 +85,7 @@ struct spidev_data { u8 *tx_buffer; u8 *rx_buffer; u32 speed_hz; + bool bus_locked; }; static LIST_HEAD(device_list); @@ -109,7 +110,8 @@ struct spidev_data { if (spi == NULL) status = -ESHUTDOWN; else - status = spi_sync(spi, message); + status = spidev->bus_locked ? spi_sync_locked(spi, message) : + spi_sync(spi, message); if (status == 0) status = message->actual_length; @@ -462,6 +464,16 @@ static int spidev_message(struct spidev_data *spidev, } break; + case SPI_IOC_BUS_LOCK: + spi_bus_lock(spi->master); + spidev->bus_locked = true; + break; + + case SPI_IOC_BUS_UNLOCK: + spi_bus_unlock(spi->master); + spidev->bus_locked = false; + break; + default: /* segmented and/or full-duplex I/O request */ /* Check message and copy into scratch area */ diff --git a/include/uapi/linux/spi/spidev.h b/include/uapi/linux/spi/spidev.h index dd5f21e..1f776fe 100644 --- a/include/uapi/linux/spi/spidev.h +++ b/include/uapi/linux/spi/spidev.h @@ -136,6 +136,8 @@ struct spi_ioc_transfer { #define SPI_IOC_RD_MODE32 _IOR(SPI_IOC_MAGIC, 5, __u32) #define SPI_IOC_WR_MODE32 _IOW(SPI_IOC_MAGIC, 5, __u32) - +/* Exclusive bus access lock */ +#define SPI_IOC_BUS_LOCK _IO(SPI_IOC_MAGIC, 6) +#define SPI_IOC_BUS_UNLOCK _IO(SPI_IOC_MAGIC, 7) #endif /* SPIDEV_H */