Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * HSI character device driver, implements the character device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2010 Nokia Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Contact: Andras Domokos <andras.domokos@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/kmemleak.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/hsi/hsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/hsi/hsi_char.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define HSC_DEVS		16 /* Num of channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define HSC_MSGS		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define HSC_RXBREAK		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define HSC_ID_BITS		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define HSC_PORT_ID_BITS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define HSC_ID_MASK		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define HSC_PORT_ID_MASK	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define HSC_CH_MASK		0xf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * We support up to 4 controllers that can have up to 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * ports, which should currently be more than enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define HSC_BASEMINOR(id, port_id) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		((((id) & HSC_ID_MASK) << HSC_ID_BITS) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		(((port_id) & HSC_PORT_ID_MASK) << HSC_PORT_ID_BITS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	HSC_CH_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	HSC_CH_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	HSC_CH_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	HSC_CH_WLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	HSC_RX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	HSC_TX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) struct hsc_client_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * struct hsc_channel - hsi_char internal channel data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * @ch: channel number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * @flags: Keeps state of the channel (open/close, reading, writing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * @free_msgs_list: List of free HSI messages/requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * @rx_msgs_queue: List of pending RX requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * @tx_msgs_queue: List of pending TX requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @lock: Serialize access to the lists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * @cl: reference to the associated hsi_client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * @cl_data: reference to the client data that this channels belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * @rx_wait: RX requests wait queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * @tx_wait: TX requests wait queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) struct hsc_channel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	unsigned int		ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	unsigned long		flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct list_head	free_msgs_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct list_head	rx_msgs_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct list_head	tx_msgs_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	spinlock_t		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct hsi_client	*cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct hsc_client_data *cl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	wait_queue_head_t	rx_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	wait_queue_head_t	tx_wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * struct hsc_client_data - hsi_char internal client data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * @cdev: Characther device associated to the hsi_client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * @lock: Lock to serialize open/close access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * @flags: Keeps track of port state (rx hwbreak armed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * @usecnt: Use count for claiming the HSI port (mutex protected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * @cl: Referece to the HSI client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @channels: Array of channels accessible by the client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct hsc_client_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct cdev		cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct mutex		lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	unsigned long		flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	unsigned int		usecnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct hsi_client	*cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct hsc_channel	channels[HSC_DEVS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Stores the major number dynamically allocated for hsi_char */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static unsigned int hsc_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* Maximum buffer size that hsi_char will accept from userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static unsigned int max_data_size = 0x1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) module_param(max_data_size, uint, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) MODULE_PARM_DESC(max_data_size, "max read/write data size [4,8..65536] (^2)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void hsc_add_tail(struct hsc_channel *channel, struct hsi_msg *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 							struct list_head *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	spin_lock_irqsave(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	list_add_tail(&msg->link, queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	spin_unlock_irqrestore(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct hsi_msg *hsc_get_first_msg(struct hsc_channel *channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 							struct list_head *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct hsi_msg *msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	spin_lock_irqsave(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (list_empty(queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	msg = list_first_entry(queue, struct hsi_msg, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	list_del(&msg->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	spin_unlock_irqrestore(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline void hsc_msg_free(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	kfree(sg_virt(msg->sgt.sgl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	hsi_free_msg(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void hsc_free_list(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct hsi_msg *msg, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	list_for_each_entry_safe(msg, tmp, list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		list_del(&msg->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		hsc_msg_free(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void hsc_reset_list(struct hsc_channel *channel, struct list_head *l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	spin_lock_irqsave(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	list_splice_init(l, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	spin_unlock_irqrestore(&channel->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	hsc_free_list(&list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline struct hsi_msg *hsc_msg_alloc(unsigned int alloc_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	msg = hsi_alloc_msg(1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	buf = kmalloc(alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		hsi_free_msg(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	sg_init_one(msg->sgt.sgl, buf, alloc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/* Ignore false positive, due to sg pointer handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	kmemleak_ignore(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static inline int hsc_msgs_alloc(struct hsc_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	for (i = 0; i < HSC_MSGS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		msg = hsc_msg_alloc(max_data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		msg->channel = channel->ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		list_add_tail(&msg->link, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	hsc_free_list(&channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline unsigned int hsc_msg_len_get(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return msg->sgt.sgl->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static inline void hsc_msg_len_set(struct hsi_msg *msg, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	msg->sgt.sgl->length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static void hsc_rx_completed(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct hsc_channel *channel = cl_data->channels + msg->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (test_bit(HSC_CH_READ, &channel->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		hsc_add_tail(channel, msg, &channel->rx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		wake_up(&channel->rx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		hsc_add_tail(channel, msg, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static void hsc_rx_msg_destructor(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	msg->status = HSI_STATUS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	hsc_msg_len_set(msg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	hsc_rx_completed(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void hsc_tx_completed(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct hsc_channel *channel = cl_data->channels + msg->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (test_bit(HSC_CH_WRITE, &channel->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		hsc_add_tail(channel, msg, &channel->tx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		wake_up(&channel->tx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		hsc_add_tail(channel, msg, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void hsc_tx_msg_destructor(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	msg->status = HSI_STATUS_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	hsc_msg_len_set(msg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	hsc_tx_completed(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static void hsc_break_req_destructor(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	hsi_free_msg(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	clear_bit(HSC_RXBREAK, &cl_data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void hsc_break_received(struct hsi_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct hsc_channel *channel = cl_data->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/* Broadcast HWBREAK on all channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	for (i = 0; i < HSC_DEVS; i++, channel++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		struct hsi_msg *msg2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (!test_bit(HSC_CH_READ, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		msg2 = hsc_get_first_msg(channel, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (!msg2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		clear_bit(HSC_CH_READ, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		hsc_msg_len_set(msg2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		msg2->status = HSI_STATUS_COMPLETED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		hsc_add_tail(channel, msg2, &channel->rx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		wake_up(&channel->rx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	hsi_flush(msg->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	ret = hsi_async_read(msg->cl, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		hsc_break_req_destructor(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static int hsc_break_request(struct hsi_client *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (test_and_set_bit(HSC_RXBREAK, &cl_data->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	msg = hsi_alloc_msg(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (!msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		clear_bit(HSC_RXBREAK, &cl_data->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	msg->break_frame = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	msg->complete = hsc_break_received;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	msg->destructor = hsc_break_req_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ret = hsi_async_read(cl, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		hsc_break_req_destructor(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int hsc_break_send(struct hsi_client *cl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	msg = hsi_alloc_msg(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	msg->break_frame = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	msg->complete = hsi_free_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	msg->destructor = hsi_free_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	ret = hsi_async_write(cl, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		hsi_free_msg(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static int hsc_rx_set(struct hsi_client *cl, struct hsc_rx_config *rxc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	struct hsi_config tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if ((rxc->mode != HSI_MODE_STREAM) && (rxc->mode != HSI_MODE_FRAME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	if ((rxc->channels == 0) || (rxc->channels > HSC_DEVS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (rxc->channels & (rxc->channels - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if ((rxc->flow != HSI_FLOW_SYNC) && (rxc->flow != HSI_FLOW_PIPE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	tmp = cl->rx_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	cl->rx_cfg.mode = rxc->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	cl->rx_cfg.num_hw_channels = rxc->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	cl->rx_cfg.flow = rxc->flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	ret = hsi_setup(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		cl->rx_cfg = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (rxc->mode == HSI_MODE_FRAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		hsc_break_request(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static inline void hsc_rx_get(struct hsi_client *cl, struct hsc_rx_config *rxc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	rxc->mode = cl->rx_cfg.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	rxc->channels = cl->rx_cfg.num_hw_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	rxc->flow = cl->rx_cfg.flow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int hsc_tx_set(struct hsi_client *cl, struct hsc_tx_config *txc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct hsi_config tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if ((txc->mode != HSI_MODE_STREAM) && (txc->mode != HSI_MODE_FRAME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if ((txc->channels == 0) || (txc->channels > HSC_DEVS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (txc->channels & (txc->channels - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if ((txc->arb_mode != HSI_ARB_RR) && (txc->arb_mode != HSI_ARB_PRIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	tmp = cl->tx_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	cl->tx_cfg.mode = txc->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	cl->tx_cfg.num_hw_channels = txc->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	cl->tx_cfg.speed = txc->speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	cl->tx_cfg.arb_mode = txc->arb_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	ret = hsi_setup(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		cl->tx_cfg = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static inline void hsc_tx_get(struct hsi_client *cl, struct hsc_tx_config *txc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	txc->mode = cl->tx_cfg.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	txc->channels = cl->tx_cfg.num_hw_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	txc->speed = cl->tx_cfg.speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	txc->arb_mode = cl->tx_cfg.arb_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static ssize_t hsc_read(struct file *file, char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 						loff_t *ppos __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	struct hsc_channel *channel = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	if (!IS_ALIGNED(len, sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (len > max_data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		len = max_data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (channel->ch >= channel->cl->rx_cfg.num_hw_channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		return -ECHRNG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (test_and_set_bit(HSC_CH_READ, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (!msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	hsc_msg_len_set(msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	msg->complete = hsc_rx_completed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	msg->destructor = hsc_rx_msg_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	ret = hsi_async_read(channel->cl, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		hsc_add_tail(channel, msg, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	ret = wait_event_interruptible(channel->rx_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 					!list_empty(&channel->rx_msgs_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		clear_bit(HSC_CH_READ, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		hsi_flush(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	msg = hsc_get_first_msg(channel, &channel->rx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (msg->status != HSI_STATUS_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			ret = copy_to_user((void __user *)buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			sg_virt(msg->sgt.sgl), hsc_msg_len_get(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 				ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 				ret = hsc_msg_len_get(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		hsc_add_tail(channel, msg, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	clear_bit(HSC_CH_READ, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static ssize_t hsc_write(struct file *file, const char __user *buf, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 						loff_t *ppos __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	struct hsc_channel *channel = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	struct hsi_msg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if ((len == 0) || !IS_ALIGNED(len, sizeof(u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (len > max_data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		len = max_data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (channel->ch >= channel->cl->tx_cfg.num_hw_channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		return -ECHRNG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (test_and_set_bit(HSC_CH_WRITE, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (!msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		clear_bit(HSC_CH_WRITE, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (copy_from_user(sg_virt(msg->sgt.sgl), (void __user *)buf, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	hsc_msg_len_set(msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	msg->complete = hsc_tx_completed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	msg->destructor = hsc_tx_msg_destructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	ret = hsi_async_write(channel->cl, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	ret = wait_event_interruptible(channel->tx_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 					!list_empty(&channel->tx_msgs_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		clear_bit(HSC_CH_WRITE, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		hsi_flush(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	msg = hsc_get_first_msg(channel, &channel->tx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	if (msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		if (msg->status == HSI_STATUS_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			ret = hsc_msg_len_get(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		hsc_add_tail(channel, msg, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	clear_bit(HSC_CH_WRITE, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) static long hsc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct hsc_channel *channel = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	unsigned int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	struct hsc_rx_config rxc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	struct hsc_tx_config txc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	long ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	case HSC_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		hsi_flush(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	case HSC_SET_PM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		if (copy_from_user(&state, (void __user *)arg, sizeof(state)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		if (state == HSC_PM_DISABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			if (test_and_set_bit(HSC_CH_WLINE, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			ret = hsi_start_tx(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		} else if (state == HSC_PM_ENABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			if (!test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			ret = hsi_stop_tx(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	case HSC_SEND_BREAK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return hsc_break_send(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	case HSC_SET_RX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		if (copy_from_user(&rxc, (void __user *)arg, sizeof(rxc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		return hsc_rx_set(channel->cl, &rxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	case HSC_GET_RX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		hsc_rx_get(channel->cl, &rxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		if (copy_to_user((void __user *)arg, &rxc, sizeof(rxc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	case HSC_SET_TX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		if (copy_from_user(&txc, (void __user *)arg, sizeof(txc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		return hsc_tx_set(channel->cl, &txc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	case HSC_GET_TX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		hsc_tx_get(channel->cl, &txc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		if (copy_to_user((void __user *)arg, &txc, sizeof(txc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static inline void __hsc_port_release(struct hsc_client_data *cl_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	BUG_ON(cl_data->usecnt == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (--cl_data->usecnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		hsi_flush(cl_data->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		hsi_release_port(cl_data->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) static int hsc_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	struct hsc_client_data *cl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	struct hsc_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	pr_debug("open, minor = %d\n", iminor(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	cl_data = container_of(inode->i_cdev, struct hsc_client_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	mutex_lock(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	channel = cl_data->channels + (iminor(inode) & HSC_CH_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	if (test_and_set_bit(HSC_CH_OPEN, &channel->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	 * Check if we have already claimed the port associated to the HSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	 * client. If not then try to claim it, else increase its refcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (cl_data->usecnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		ret = hsi_claim_port(cl_data->cl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		hsi_setup(cl_data->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	cl_data->usecnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	ret = hsc_msgs_alloc(channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		__hsc_port_release(cl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	file->private_data = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	mutex_unlock(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	mutex_unlock(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static int hsc_release(struct inode *inode __maybe_unused, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	struct hsc_channel *channel = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	struct hsc_client_data *cl_data = channel->cl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	mutex_lock(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	if (test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		hsi_stop_tx(channel->cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	__hsc_port_release(cl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	hsc_reset_list(channel, &channel->rx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	hsc_reset_list(channel, &channel->tx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	hsc_reset_list(channel, &channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	clear_bit(HSC_CH_READ, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	clear_bit(HSC_CH_WRITE, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	clear_bit(HSC_CH_OPEN, &channel->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	wake_up(&channel->rx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	wake_up(&channel->tx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	mutex_unlock(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) static const struct file_operations hsc_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	.read		= hsc_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	.write		= hsc_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	.unlocked_ioctl	= hsc_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	.open		= hsc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	.release	= hsc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) static void hsc_channel_init(struct hsc_channel *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	init_waitqueue_head(&channel->rx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	init_waitqueue_head(&channel->tx_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	spin_lock_init(&channel->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	INIT_LIST_HEAD(&channel->free_msgs_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	INIT_LIST_HEAD(&channel->rx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	INIT_LIST_HEAD(&channel->tx_msgs_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static int hsc_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	const char devname[] = "hsi_char";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	struct hsc_client_data *cl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	struct hsc_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	struct hsi_client *cl = to_hsi_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	unsigned int hsc_baseminor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	dev_t hsc_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	cl_data = kzalloc(sizeof(*cl_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	if (!cl_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	hsc_baseminor = HSC_BASEMINOR(hsi_id(cl), hsi_port_id(cl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (!hsc_major) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		ret = alloc_chrdev_region(&hsc_dev, hsc_baseminor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 						HSC_DEVS, devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 			hsc_major = MAJOR(hsc_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		hsc_dev = MKDEV(hsc_major, hsc_baseminor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		ret = register_chrdev_region(hsc_dev, HSC_DEVS, devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		dev_err(dev, "Device %s allocation failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 					hsc_major ? "minor" : "major", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	mutex_init(&cl_data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	hsi_client_set_drvdata(cl, cl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	cdev_init(&cl_data->cdev, &hsc_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	cl_data->cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	cl_data->cl = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	for (i = 0, channel = cl_data->channels; i < HSC_DEVS; i++, channel++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		hsc_channel_init(channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		channel->ch = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		channel->cl = cl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		channel->cl_data = cl_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	/* 1 hsi client -> N char devices (one for each channel) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	ret = cdev_add(&cl_data->cdev, hsc_dev, HSC_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		dev_err(dev, "Could not add char device %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	unregister_chrdev_region(hsc_dev, HSC_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	kfree(cl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) static int hsc_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	struct hsi_client *cl = to_hsi_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	dev_t hsc_dev = cl_data->cdev.dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	cdev_del(&cl_data->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	unregister_chrdev_region(hsc_dev, HSC_DEVS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	hsi_client_set_drvdata(cl, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	kfree(cl_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static struct hsi_client_driver hsc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		.name	= "hsi_char",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		.probe	= hsc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		.remove	= hsc_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static int __init hsc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	if ((max_data_size < 4) || (max_data_size > 0x10000) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		(max_data_size & (max_data_size - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		pr_err("Invalid max read/write data size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	ret = hsi_register_client_driver(&hsc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		pr_err("Error while registering HSI/SSI driver %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	pr_info("HSI/SSI char device loaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) module_init(hsc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static void __exit hsc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	hsi_unregister_client_driver(&hsc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	pr_info("HSI char device removed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) module_exit(hsc_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) MODULE_AUTHOR("Andras Domokos <andras.domokos@nokia.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) MODULE_ALIAS("hsi:hsi_char");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) MODULE_DESCRIPTION("HSI character device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) MODULE_LICENSE("GPL v2");