1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016-2017, Linaro Ltd
7 #include <linux/interrupt.h>
9 #include <linux/list.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/rpmsg.h>
17 #include <linux/sizes.h>
18 #include <linux/slab.h>
19 #include <linux/wait.h>
20 #include <linux/workqueue.h>
21 #include <linux/mailbox_client.h>
23 #include "rpmsg_internal.h"
24 #include "qcom_glink_native.h"
26 #define GLINK_NAME_SIZE 32
27 #define GLINK_VERSION_1 1
29 #define RPM_GLINK_CID_MIN 1
30 #define RPM_GLINK_CID_MAX 65536
40 * struct glink_defer_cmd - deferred incoming control message
42 * @msg: message header
43 * @data: payload of the message
45 * Copy of a received control message, to be added to @rx_queue and processed
46 * by @rx_work of @qcom_glink.
48 struct glink_defer_cmd {
49 struct list_head node;
56 * struct glink_core_rx_intent - RX intent
59 * @data: pointer to the data (may be NULL for zero-copy)
60 * @id: remote or local intent ID
61 * @size: size of the original intent (do not modify)
62 * @reuse: To mark if the intent can be reused after first use
63 * @in_use: To mark if intent is already in use for the channel
64 * @offset: next write offset (initially 0)
67 struct glink_core_rx_intent {
75 struct list_head node;
79 * struct qcom_glink - driver context, relates to one remote subsystem
80 * @dev: reference to the associated struct device
81 * @rx_pipe: pipe object for receive FIFO
82 * @tx_pipe: pipe object for transmit FIFO
83 * @rx_work: worker for handling received control messages
84 * @rx_lock: protects the @rx_queue
85 * @rx_queue: queue of received control messages to be processed in @rx_work
86 * @tx_lock: synchronizes operations on the tx fifo
87 * @idr_lock: synchronizes @lcids and @rcids modifications
88 * @lcids: idr of all channels with a known local channel id
89 * @rcids: idr of all channels with a known remote channel id
90 * @features: remote features
91 * @intentless: flag to indicate that there is no intent
92 * @tx_avail_notify: Waitqueue for pending tx tasks
93 * @sent_read_notify: flag to check cmd sent or not
94 * @abort_tx: flag indicating that all tx attempts should fail
99 struct qcom_glink_pipe *rx_pipe;
100 struct qcom_glink_pipe *tx_pipe;
102 struct work_struct rx_work;
104 struct list_head rx_queue;
111 unsigned long features;
114 wait_queue_head_t tx_avail_notify;
115 bool sent_read_notify;
128 * struct glink_channel - internal representation of a channel
129 * @rpdev: rpdev reference, only used for primary endpoints
130 * @ept: rpmsg endpoint this channel is associated with
131 * @glink: qcom_glink context handle
132 * @refcount: refcount for the channel object
133 * @recv_lock: guard for @ept.cb
134 * @name: unique channel name/identifier
135 * @lcid: channel id, in local space
136 * @rcid: channel id, in remote space
137 * @intent_lock: lock for protection of @liids, @riids
138 * @liids: idr of all local intents
139 * @riids: idr of all remote intents
140 * @intent_work: worker responsible for transmitting rx_done packets
141 * @done_intents: list of intents that needs to be announced rx_done
142 * @buf: receive buffer, for gathering fragments
143 * @buf_offset: write offset in @buf
144 * @buf_size: size of current @buf
145 * @open_ack: completed once remote has acked the open-request
146 * @open_req: completed once open-request has been received
147 * @intent_req_lock: Synchronises multiple intent requests
148 * @intent_req_result: Result of intent request
149 * @intent_received: flag indicating that an intent has been received
150 * @intent_req_wq: wait queue for intent_req signalling
152 struct glink_channel {
153 struct rpmsg_endpoint ept;
155 struct rpmsg_device *rpdev;
156 struct qcom_glink *glink;
158 struct kref refcount;
160 spinlock_t recv_lock;
166 spinlock_t intent_lock;
169 struct work_struct intent_work;
170 struct list_head done_intents;
172 struct glink_core_rx_intent *buf;
176 struct completion open_ack;
177 struct completion open_req;
179 struct mutex intent_req_lock;
180 int intent_req_result;
181 bool intent_received;
182 wait_queue_head_t intent_req_wq;
185 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
187 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
189 #define GLINK_CMD_VERSION 0
190 #define GLINK_CMD_VERSION_ACK 1
191 #define GLINK_CMD_OPEN 2
192 #define GLINK_CMD_CLOSE 3
193 #define GLINK_CMD_OPEN_ACK 4
194 #define GLINK_CMD_INTENT 5
195 #define GLINK_CMD_RX_DONE 6
196 #define GLINK_CMD_RX_INTENT_REQ 7
197 #define GLINK_CMD_RX_INTENT_REQ_ACK 8
198 #define GLINK_CMD_TX_DATA 9
199 #define GLINK_CMD_CLOSE_ACK 11
200 #define GLINK_CMD_TX_DATA_CONT 12
201 #define GLINK_CMD_READ_NOTIF 13
202 #define GLINK_CMD_RX_DONE_W_REUSE 14
203 #define GLINK_CMD_SIGNALS 15
205 #define GLINK_FEATURE_INTENTLESS BIT(1)
207 #define NATIVE_DTR_SIG NATIVE_DSR_SIG
208 #define NATIVE_DSR_SIG BIT(31)
209 #define NATIVE_RTS_SIG NATIVE_CTS_SIG
210 #define NATIVE_CTS_SIG BIT(30)
212 static void qcom_glink_rx_done_work(struct work_struct *work);
214 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
217 struct glink_channel *channel;
219 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
223 /* Setup glink internal glink_channel data */
224 spin_lock_init(&channel->recv_lock);
225 spin_lock_init(&channel->intent_lock);
226 mutex_init(&channel->intent_req_lock);
228 channel->glink = glink;
229 channel->name = kstrdup(name, GFP_KERNEL);
230 if (!channel->name) {
232 return ERR_PTR(-ENOMEM);
235 init_completion(&channel->open_req);
236 init_completion(&channel->open_ack);
237 init_waitqueue_head(&channel->intent_req_wq);
239 INIT_LIST_HEAD(&channel->done_intents);
240 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
242 idr_init(&channel->liids);
243 idr_init(&channel->riids);
244 kref_init(&channel->refcount);
249 static void qcom_glink_channel_release(struct kref *ref)
251 struct glink_channel *channel = container_of(ref, struct glink_channel,
253 struct glink_core_rx_intent *intent;
254 struct glink_core_rx_intent *tmp;
258 /* cancel pending rx_done work */
259 cancel_work_sync(&channel->intent_work);
261 spin_lock_irqsave(&channel->intent_lock, flags);
262 /* Free all non-reuse intents pending rx_done work */
263 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
264 if (!intent->reuse) {
270 idr_for_each_entry(&channel->liids, tmp, iid) {
274 idr_destroy(&channel->liids);
276 idr_for_each_entry(&channel->riids, tmp, iid)
278 idr_destroy(&channel->riids);
279 spin_unlock_irqrestore(&channel->intent_lock, flags);
281 kfree(channel->name);
285 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
287 return glink->rx_pipe->avail(glink->rx_pipe);
290 static void qcom_glink_rx_peek(struct qcom_glink *glink,
291 void *data, unsigned int offset, size_t count)
293 glink->rx_pipe->peek(glink->rx_pipe, data, offset, count);
296 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
298 glink->rx_pipe->advance(glink->rx_pipe, count);
301 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
303 return glink->tx_pipe->avail(glink->tx_pipe);
306 static void qcom_glink_tx_write(struct qcom_glink *glink,
307 const void *hdr, size_t hlen,
308 const void *data, size_t dlen)
310 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
313 static void qcom_glink_tx_kick(struct qcom_glink *glink)
315 glink->tx_pipe->kick(glink->tx_pipe);
318 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
320 struct glink_msg msg;
322 msg.cmd = cpu_to_le16(GLINK_CMD_READ_NOTIF);
326 qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
328 qcom_glink_tx_kick(glink);
331 static int qcom_glink_tx(struct qcom_glink *glink,
332 const void *hdr, size_t hlen,
333 const void *data, size_t dlen, bool wait)
335 unsigned int tlen = hlen + dlen;
339 /* Reject packets that are too big */
340 if (tlen >= glink->tx_pipe->length)
343 spin_lock_irqsave(&glink->tx_lock, flags);
345 if (glink->abort_tx) {
350 while (qcom_glink_tx_avail(glink) < tlen) {
356 if (glink->abort_tx) {
361 if (!glink->sent_read_notify) {
362 glink->sent_read_notify = true;
363 qcom_glink_send_read_notify(glink);
366 /* Wait without holding the tx_lock */
367 spin_unlock_irqrestore(&glink->tx_lock, flags);
369 wait_event_timeout(glink->tx_avail_notify,
370 qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
372 spin_lock_irqsave(&glink->tx_lock, flags);
374 if (qcom_glink_tx_avail(glink) >= tlen)
375 glink->sent_read_notify = false;
378 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
379 qcom_glink_tx_kick(glink);
382 spin_unlock_irqrestore(&glink->tx_lock, flags);
387 static int qcom_glink_send_version(struct qcom_glink *glink)
389 struct glink_msg msg;
391 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION);
392 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
393 msg.param2 = cpu_to_le32(glink->features);
395 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
398 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
400 struct glink_msg msg;
402 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION_ACK);
403 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
404 msg.param2 = cpu_to_le32(glink->features);
406 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
409 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
410 struct glink_channel *channel)
412 struct glink_msg msg;
414 msg.cmd = cpu_to_le16(GLINK_CMD_OPEN_ACK);
415 msg.param1 = cpu_to_le16(channel->rcid);
416 msg.param2 = cpu_to_le32(0);
418 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
421 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
422 unsigned int cid, bool granted)
424 struct glink_channel *channel;
427 spin_lock_irqsave(&glink->idr_lock, flags);
428 channel = idr_find(&glink->rcids, cid);
429 spin_unlock_irqrestore(&glink->idr_lock, flags);
431 dev_err(glink->dev, "unable to find channel\n");
435 WRITE_ONCE(channel->intent_req_result, granted);
436 wake_up_all(&channel->intent_req_wq);
439 static void qcom_glink_intent_req_abort(struct glink_channel *channel)
441 WRITE_ONCE(channel->intent_req_result, 0);
442 wake_up_all(&channel->intent_req_wq);
446 * qcom_glink_send_open_req() - send a GLINK_CMD_OPEN request to the remote
447 * @glink: Ptr to the glink edge
448 * @channel: Ptr to the channel that the open req is sent
450 * Allocates a local channel id and sends a GLINK_CMD_OPEN message to the remote.
451 * Will return with refcount held, regardless of outcome.
453 * Return: 0 on success, negative errno otherwise.
455 static int qcom_glink_send_open_req(struct qcom_glink *glink,
456 struct glink_channel *channel)
459 struct glink_msg msg;
460 u8 name[GLINK_NAME_SIZE];
462 int name_len = strlen(channel->name) + 1;
463 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
467 kref_get(&channel->refcount);
469 spin_lock_irqsave(&glink->idr_lock, flags);
470 ret = idr_alloc_cyclic(&glink->lcids, channel,
471 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
473 spin_unlock_irqrestore(&glink->idr_lock, flags);
479 req.msg.cmd = cpu_to_le16(GLINK_CMD_OPEN);
480 req.msg.param1 = cpu_to_le16(channel->lcid);
481 req.msg.param2 = cpu_to_le32(name_len);
482 strcpy(req.name, channel->name);
484 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
491 spin_lock_irqsave(&glink->idr_lock, flags);
492 idr_remove(&glink->lcids, channel->lcid);
494 spin_unlock_irqrestore(&glink->idr_lock, flags);
499 static void qcom_glink_send_close_req(struct qcom_glink *glink,
500 struct glink_channel *channel)
502 struct glink_msg req;
504 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE);
505 req.param1 = cpu_to_le16(channel->lcid);
508 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
511 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
514 struct glink_msg req;
516 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE_ACK);
517 req.param1 = cpu_to_le16(rcid);
520 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
523 static void qcom_glink_rx_done_work(struct work_struct *work)
525 struct glink_channel *channel = container_of(work, struct glink_channel,
527 struct qcom_glink *glink = channel->glink;
528 struct glink_core_rx_intent *intent, *tmp;
535 unsigned int cid = channel->lcid;
540 spin_lock_irqsave(&channel->intent_lock, flags);
541 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
542 list_del(&intent->node);
543 spin_unlock_irqrestore(&channel->intent_lock, flags);
545 reuse = intent->reuse;
547 cmd.id = reuse ? GLINK_CMD_RX_DONE_W_REUSE : GLINK_CMD_RX_DONE;
551 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
556 spin_lock_irqsave(&channel->intent_lock, flags);
558 spin_unlock_irqrestore(&channel->intent_lock, flags);
561 static void qcom_glink_rx_done(struct qcom_glink *glink,
562 struct glink_channel *channel,
563 struct glink_core_rx_intent *intent)
565 /* We don't send RX_DONE to intentless systems */
566 if (glink->intentless) {
572 /* Take it off the tree of receive intents */
573 if (!intent->reuse) {
574 spin_lock(&channel->intent_lock);
575 idr_remove(&channel->liids, intent->id);
576 spin_unlock(&channel->intent_lock);
579 /* Schedule the sending of a rx_done indication */
580 spin_lock(&channel->intent_lock);
581 list_add_tail(&intent->node, &channel->done_intents);
582 spin_unlock(&channel->intent_lock);
584 schedule_work(&channel->intent_work);
588 * qcom_glink_receive_version() - receive version/features from remote system
590 * @glink: pointer to transport interface
591 * @version: remote version
592 * @features: remote features
594 * This function is called in response to a remote-initiated version/feature
595 * negotiation sequence.
597 static void qcom_glink_receive_version(struct qcom_glink *glink,
604 case GLINK_VERSION_1:
605 glink->features &= features;
608 qcom_glink_send_version_ack(glink);
614 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
616 * @glink: pointer to transport interface
617 * @version: remote version response
618 * @features: remote features response
620 * This function is called in response to a local-initiated version/feature
621 * negotiation sequence and is the counter-offer from the remote side based
622 * upon the initial version and feature set requested.
624 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
630 /* Version negotiation failed */
632 case GLINK_VERSION_1:
633 if (features == glink->features)
636 glink->features &= features;
639 qcom_glink_send_version(glink);
645 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
646 * wire format and transmit
647 * @glink: The transport to transmit on.
648 * @channel: The glink channel
649 * @granted: The request response to encode.
651 * Return: 0 on success or standard Linux error code.
653 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
654 struct glink_channel *channel,
657 struct glink_msg msg;
659 msg.cmd = cpu_to_le16(GLINK_CMD_RX_INTENT_REQ_ACK);
660 msg.param1 = cpu_to_le16(channel->lcid);
661 msg.param2 = cpu_to_le32(granted);
663 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
669 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
671 * @glink: The transport to transmit on.
672 * @channel: The local channel
673 * @intent: The intent to pass on to remote.
675 * Return: 0 on success or standard Linux error code.
677 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
678 struct glink_channel *channel,
679 struct glink_core_rx_intent *intent)
690 cmd.id = cpu_to_le16(GLINK_CMD_INTENT);
691 cmd.lcid = cpu_to_le16(channel->lcid);
692 cmd.count = cpu_to_le32(1);
693 cmd.size = cpu_to_le32(intent->size);
694 cmd.liid = cpu_to_le32(intent->id);
696 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
701 static struct glink_core_rx_intent *
702 qcom_glink_alloc_intent(struct qcom_glink *glink,
703 struct glink_channel *channel,
707 struct glink_core_rx_intent *intent;
711 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
715 intent->data = kzalloc(size, GFP_KERNEL);
719 spin_lock_irqsave(&channel->intent_lock, flags);
720 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
722 spin_unlock_irqrestore(&channel->intent_lock, flags);
725 spin_unlock_irqrestore(&channel->intent_lock, flags);
729 intent->reuse = reuseable;
740 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
741 u32 cid, uint32_t iid,
744 struct glink_core_rx_intent *intent;
745 struct glink_channel *channel;
748 spin_lock_irqsave(&glink->idr_lock, flags);
749 channel = idr_find(&glink->rcids, cid);
750 spin_unlock_irqrestore(&glink->idr_lock, flags);
752 dev_err(glink->dev, "invalid channel id received\n");
756 spin_lock_irqsave(&channel->intent_lock, flags);
757 intent = idr_find(&channel->riids, iid);
760 spin_unlock_irqrestore(&channel->intent_lock, flags);
761 dev_err(glink->dev, "invalid intent id received\n");
765 intent->in_use = false;
768 idr_remove(&channel->riids, intent->id);
771 spin_unlock_irqrestore(&channel->intent_lock, flags);
774 WRITE_ONCE(channel->intent_received, true);
775 wake_up_all(&channel->intent_req_wq);
780 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
782 * @glink: Pointer to the transport interface
783 * @cid: Remote channel ID
784 * @size: size of the intent
786 * The function searches for the local channel to which the request for
787 * rx_intent has arrived and allocates and notifies the remote back
789 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
790 u32 cid, size_t size)
792 struct glink_core_rx_intent *intent;
793 struct glink_channel *channel;
796 spin_lock_irqsave(&glink->idr_lock, flags);
797 channel = idr_find(&glink->rcids, cid);
798 spin_unlock_irqrestore(&glink->idr_lock, flags);
801 pr_err("%s channel not found for cid %d\n", __func__, cid);
805 intent = qcom_glink_alloc_intent(glink, channel, size, false);
807 qcom_glink_advertise_intent(glink, channel, intent);
809 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
812 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
814 struct glink_defer_cmd *dcmd;
816 extra = ALIGN(extra, 8);
818 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
819 dev_dbg(glink->dev, "Insufficient data in rx fifo");
823 dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
827 INIT_LIST_HEAD(&dcmd->node);
829 qcom_glink_rx_peek(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
831 spin_lock(&glink->rx_lock);
832 list_add_tail(&dcmd->node, &glink->rx_queue);
833 spin_unlock(&glink->rx_lock);
835 schedule_work(&glink->rx_work);
836 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
841 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
843 struct glink_core_rx_intent *intent;
844 struct glink_channel *channel;
846 struct glink_msg msg;
850 unsigned int chunk_size;
851 unsigned int left_size;
857 if (avail < sizeof(hdr)) {
858 dev_dbg(glink->dev, "Not enough data in fifo\n");
862 qcom_glink_rx_peek(glink, &hdr, 0, sizeof(hdr));
863 chunk_size = le32_to_cpu(hdr.chunk_size);
864 left_size = le32_to_cpu(hdr.left_size);
866 if (avail < sizeof(hdr) + chunk_size) {
867 dev_dbg(glink->dev, "Payload not yet in fifo\n");
871 rcid = le16_to_cpu(hdr.msg.param1);
872 spin_lock_irqsave(&glink->idr_lock, flags);
873 channel = idr_find(&glink->rcids, rcid);
874 spin_unlock_irqrestore(&glink->idr_lock, flags);
876 dev_dbg(glink->dev, "Data on non-existing channel\n");
878 /* Drop the message */
882 if (glink->intentless) {
883 /* Might have an ongoing, fragmented, message to append */
885 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
889 intent->data = kmalloc(chunk_size + left_size,
896 intent->id = 0xbabababa;
897 intent->size = chunk_size + left_size;
900 channel->buf = intent;
902 intent = channel->buf;
905 liid = le32_to_cpu(hdr.msg.param2);
907 spin_lock_irqsave(&channel->intent_lock, flags);
908 intent = idr_find(&channel->liids, liid);
909 spin_unlock_irqrestore(&channel->intent_lock, flags);
913 "no intent found for channel %s intent %d",
914 channel->name, liid);
920 if (intent->size - intent->offset < chunk_size) {
921 dev_err(glink->dev, "Insufficient space in intent\n");
923 /* The packet header lied, drop payload */
927 qcom_glink_rx_peek(glink, intent->data + intent->offset,
928 sizeof(hdr), chunk_size);
929 intent->offset += chunk_size;
931 /* Handle message when no fragments remain to be received */
933 spin_lock(&channel->recv_lock);
934 if (channel->ept.cb) {
935 channel->ept.cb(channel->ept.rpdev,
941 spin_unlock(&channel->recv_lock);
946 qcom_glink_rx_done(glink, channel, intent);
950 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
955 static void qcom_glink_handle_intent(struct qcom_glink *glink,
960 struct glink_core_rx_intent *intent;
961 struct glink_channel *channel;
968 struct glink_msg msg;
969 struct intent_pair intents[];
972 const size_t msglen = struct_size(msg, intents, count);
977 if (avail < msglen) {
978 dev_dbg(glink->dev, "Not enough data in fifo\n");
982 spin_lock_irqsave(&glink->idr_lock, flags);
983 channel = idr_find(&glink->rcids, cid);
984 spin_unlock_irqrestore(&glink->idr_lock, flags);
986 dev_err(glink->dev, "intents for non-existing channel\n");
987 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
991 msg = kmalloc(msglen, GFP_ATOMIC);
995 qcom_glink_rx_peek(glink, msg, 0, msglen);
997 for (i = 0; i < count; ++i) {
998 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
1002 intent->id = le32_to_cpu(msg->intents[i].iid);
1003 intent->size = le32_to_cpu(msg->intents[i].size);
1005 spin_lock_irqsave(&channel->intent_lock, flags);
1006 ret = idr_alloc(&channel->riids, intent,
1007 intent->id, intent->id + 1, GFP_ATOMIC);
1008 spin_unlock_irqrestore(&channel->intent_lock, flags);
1011 dev_err(glink->dev, "failed to store remote intent\n");
1014 WRITE_ONCE(channel->intent_received, true);
1015 wake_up_all(&channel->intent_req_wq);
1018 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
1021 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
1023 struct glink_channel *channel;
1025 spin_lock(&glink->idr_lock);
1026 channel = idr_find(&glink->lcids, lcid);
1027 spin_unlock(&glink->idr_lock);
1029 dev_err(glink->dev, "Invalid open ack packet\n");
1033 complete_all(&channel->open_ack);
1039 * qcom_glink_set_flow_control() - convert a signal cmd to wire format and transmit
1040 * @ept: Rpmsg endpoint for channel.
1041 * @pause: Pause transmission
1042 * @dst: destination address of the endpoint
1044 * Return: 0 on success or standard Linux error code.
1046 static int qcom_glink_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
1048 struct glink_channel *channel = to_glink_channel(ept);
1049 struct qcom_glink *glink = channel->glink;
1050 struct glink_msg msg;
1054 sigs |= NATIVE_DTR_SIG | NATIVE_RTS_SIG;
1056 msg.cmd = cpu_to_le16(GLINK_CMD_SIGNALS);
1057 msg.param1 = cpu_to_le16(channel->lcid);
1058 msg.param2 = cpu_to_le32(sigs);
1060 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
1063 static void qcom_glink_handle_signals(struct qcom_glink *glink,
1064 unsigned int rcid, unsigned int sigs)
1066 struct glink_channel *channel;
1067 unsigned long flags;
1070 spin_lock_irqsave(&glink->idr_lock, flags);
1071 channel = idr_find(&glink->rcids, rcid);
1072 spin_unlock_irqrestore(&glink->idr_lock, flags);
1074 dev_err(glink->dev, "signal for non-existing channel\n");
1078 enable = sigs & NATIVE_DSR_SIG || sigs & NATIVE_CTS_SIG;
1080 if (channel->ept.flow_cb)
1081 channel->ept.flow_cb(channel->ept.rpdev, channel->ept.priv, enable);
1084 void qcom_glink_native_rx(struct qcom_glink *glink)
1086 struct glink_msg msg;
1087 unsigned int param1;
1088 unsigned int param2;
1093 /* To wakeup any blocking writers */
1094 wake_up_all(&glink->tx_avail_notify);
1097 avail = qcom_glink_rx_avail(glink);
1098 if (avail < sizeof(msg))
1101 qcom_glink_rx_peek(glink, &msg, 0, sizeof(msg));
1103 cmd = le16_to_cpu(msg.cmd);
1104 param1 = le16_to_cpu(msg.param1);
1105 param2 = le32_to_cpu(msg.param2);
1108 case GLINK_CMD_VERSION:
1109 case GLINK_CMD_VERSION_ACK:
1110 case GLINK_CMD_CLOSE:
1111 case GLINK_CMD_CLOSE_ACK:
1112 case GLINK_CMD_RX_INTENT_REQ:
1113 ret = qcom_glink_rx_defer(glink, 0);
1115 case GLINK_CMD_OPEN_ACK:
1116 ret = qcom_glink_rx_open_ack(glink, param1);
1117 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1119 case GLINK_CMD_OPEN:
1120 ret = qcom_glink_rx_defer(glink, param2);
1122 case GLINK_CMD_TX_DATA:
1123 case GLINK_CMD_TX_DATA_CONT:
1124 ret = qcom_glink_rx_data(glink, avail);
1126 case GLINK_CMD_READ_NOTIF:
1127 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1128 qcom_glink_tx_kick(glink);
1130 case GLINK_CMD_INTENT:
1131 qcom_glink_handle_intent(glink, param1, param2, avail);
1133 case GLINK_CMD_RX_DONE:
1134 qcom_glink_handle_rx_done(glink, param1, param2, false);
1135 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1137 case GLINK_CMD_RX_DONE_W_REUSE:
1138 qcom_glink_handle_rx_done(glink, param1, param2, true);
1139 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1141 case GLINK_CMD_RX_INTENT_REQ_ACK:
1142 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1143 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1145 case GLINK_CMD_SIGNALS:
1146 qcom_glink_handle_signals(glink, param1, param2);
1147 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1150 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1159 EXPORT_SYMBOL(qcom_glink_native_rx);
1161 /* Locally initiated rpmsg_create_ept */
1162 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1165 struct glink_channel *channel;
1167 unsigned long flags;
1169 channel = qcom_glink_alloc_channel(glink, name);
1170 if (IS_ERR(channel))
1171 return ERR_CAST(channel);
1173 ret = qcom_glink_send_open_req(glink, channel);
1175 goto release_channel;
1177 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1181 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1185 qcom_glink_send_open_ack(glink, channel);
1190 /* qcom_glink_send_open_req() did register the channel in lcids*/
1191 spin_lock_irqsave(&glink->idr_lock, flags);
1192 idr_remove(&glink->lcids, channel->lcid);
1193 spin_unlock_irqrestore(&glink->idr_lock, flags);
1196 /* Release qcom_glink_send_open_req() reference */
1197 kref_put(&channel->refcount, qcom_glink_channel_release);
1198 /* Release qcom_glink_alloc_channel() reference */
1199 kref_put(&channel->refcount, qcom_glink_channel_release);
1201 return ERR_PTR(-ETIMEDOUT);
1204 /* Remote initiated rpmsg_create_ept */
1205 static int qcom_glink_create_remote(struct qcom_glink *glink,
1206 struct glink_channel *channel)
1210 qcom_glink_send_open_ack(glink, channel);
1212 ret = qcom_glink_send_open_req(glink, channel);
1216 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1226 * Send a close request to "undo" our open-ack. The close-ack will
1227 * release qcom_glink_send_open_req() reference and the last reference
1228 * will be relesed after receiving remote_close or transport unregister
1229 * by calling qcom_glink_native_remove().
1231 qcom_glink_send_close_req(glink, channel);
1236 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1239 struct rpmsg_channel_info
1242 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1243 struct glink_channel *channel;
1244 struct qcom_glink *glink = parent->glink;
1245 struct rpmsg_endpoint *ept;
1246 const char *name = chinfo.name;
1249 unsigned long flags;
1251 spin_lock_irqsave(&glink->idr_lock, flags);
1252 idr_for_each_entry(&glink->rcids, channel, cid) {
1253 if (!strcmp(channel->name, name))
1256 spin_unlock_irqrestore(&glink->idr_lock, flags);
1259 channel = qcom_glink_create_local(glink, name);
1260 if (IS_ERR(channel))
1263 ret = qcom_glink_create_remote(glink, channel);
1268 ept = &channel->ept;
1272 ept->ops = &glink_endpoint_ops;
1277 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1279 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1280 struct device_node *np = rpdev->dev.of_node;
1281 struct qcom_glink *glink = channel->glink;
1282 struct glink_core_rx_intent *intent;
1283 const struct property *prop = NULL;
1284 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1287 __be32 *val = defaults;
1290 if (glink->intentless || !completion_done(&channel->open_ack))
1293 prop = of_find_property(np, "qcom,intents", NULL);
1296 num_groups = prop->length / sizeof(u32) / 2;
1299 /* Channel is now open, advertise base set of intents */
1300 while (num_groups--) {
1301 size = be32_to_cpup(val++);
1302 num_intents = be32_to_cpup(val++);
1303 while (num_intents--) {
1304 intent = qcom_glink_alloc_intent(glink, channel, size,
1309 qcom_glink_advertise_intent(glink, channel, intent);
1315 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1317 struct glink_channel *channel = to_glink_channel(ept);
1318 struct qcom_glink *glink = channel->glink;
1319 unsigned long flags;
1321 spin_lock_irqsave(&channel->recv_lock, flags);
1322 channel->ept.cb = NULL;
1323 spin_unlock_irqrestore(&channel->recv_lock, flags);
1325 /* Decouple the potential rpdev from the channel */
1326 channel->rpdev = NULL;
1328 qcom_glink_send_close_req(glink, channel);
1331 static int qcom_glink_request_intent(struct qcom_glink *glink,
1332 struct glink_channel *channel,
1343 mutex_lock(&channel->intent_req_lock);
1345 WRITE_ONCE(channel->intent_req_result, -1);
1346 WRITE_ONCE(channel->intent_received, false);
1348 cmd.id = GLINK_CMD_RX_INTENT_REQ;
1349 cmd.cid = channel->lcid;
1352 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1356 ret = wait_event_timeout(channel->intent_req_wq,
1357 READ_ONCE(channel->intent_req_result) >= 0 &&
1358 READ_ONCE(channel->intent_received),
1361 dev_err(glink->dev, "intent request timed out\n");
1364 ret = READ_ONCE(channel->intent_req_result) ? 0 : -ECANCELED;
1368 mutex_unlock(&channel->intent_req_lock);
1372 static int __qcom_glink_send(struct glink_channel *channel,
1373 void *data, int len, bool wait)
1375 struct qcom_glink *glink = channel->glink;
1376 struct glink_core_rx_intent *intent = NULL;
1377 struct glink_core_rx_intent *tmp;
1380 struct glink_msg msg;
1385 unsigned long flags;
1386 int chunk_size = len;
1389 if (!glink->intentless) {
1391 spin_lock_irqsave(&channel->intent_lock, flags);
1392 idr_for_each_entry(&channel->riids, tmp, iid) {
1393 if (tmp->size >= len && !tmp->in_use) {
1396 else if (intent->size > tmp->size)
1398 if (intent->size == len)
1403 intent->in_use = true;
1404 spin_unlock_irqrestore(&channel->intent_lock, flags);
1406 /* We found an available intent */
1413 ret = qcom_glink_request_intent(glink, channel, len);
1421 while (offset < len) {
1422 chunk_size = len - offset;
1423 if (chunk_size > SZ_8K && wait)
1426 req.msg.cmd = cpu_to_le16(offset == 0 ? GLINK_CMD_TX_DATA : GLINK_CMD_TX_DATA_CONT);
1427 req.msg.param1 = cpu_to_le16(channel->lcid);
1428 req.msg.param2 = cpu_to_le32(iid);
1429 req.chunk_size = cpu_to_le32(chunk_size);
1430 req.left_size = cpu_to_le32(len - offset - chunk_size);
1432 ret = qcom_glink_tx(glink, &req, sizeof(req), data + offset, chunk_size, wait);
1434 /* Mark intent available if we failed */
1436 intent->in_use = false;
1440 offset += chunk_size;
1446 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1448 struct glink_channel *channel = to_glink_channel(ept);
1450 return __qcom_glink_send(channel, data, len, true);
1453 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1455 struct glink_channel *channel = to_glink_channel(ept);
1457 return __qcom_glink_send(channel, data, len, false);
1460 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1462 struct glink_channel *channel = to_glink_channel(ept);
1464 return __qcom_glink_send(channel, data, len, true);
1467 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1469 struct glink_channel *channel = to_glink_channel(ept);
1471 return __qcom_glink_send(channel, data, len, false);
1475 * Finds the device_node for the glink child interested in this channel.
1477 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1478 const char *channel)
1480 struct device_node *child;
1485 for_each_available_child_of_node(node, child) {
1486 key = "qcom,glink-channels";
1487 ret = of_property_read_string(child, key, &name);
1491 if (strcmp(name, channel) == 0)
1498 static const struct rpmsg_device_ops glink_device_ops = {
1499 .create_ept = qcom_glink_create_ept,
1500 .announce_create = qcom_glink_announce_create,
1503 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1504 .destroy_ept = qcom_glink_destroy_ept,
1505 .send = qcom_glink_send,
1506 .sendto = qcom_glink_sendto,
1507 .trysend = qcom_glink_trysend,
1508 .trysendto = qcom_glink_trysendto,
1509 .set_flow_control = qcom_glink_set_flow_control,
1512 static void qcom_glink_rpdev_release(struct device *dev)
1514 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1516 kfree(rpdev->driver_override);
1520 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1523 struct glink_channel *channel;
1524 struct rpmsg_device *rpdev;
1525 bool create_device = false;
1526 struct device_node *node;
1529 unsigned long flags;
1531 spin_lock_irqsave(&glink->idr_lock, flags);
1532 idr_for_each_entry(&glink->lcids, channel, lcid) {
1533 if (!strcmp(channel->name, name))
1536 spin_unlock_irqrestore(&glink->idr_lock, flags);
1539 channel = qcom_glink_alloc_channel(glink, name);
1540 if (IS_ERR(channel))
1541 return PTR_ERR(channel);
1543 /* The opening dance was initiated by the remote */
1544 create_device = true;
1547 spin_lock_irqsave(&glink->idr_lock, flags);
1548 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1550 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1551 spin_unlock_irqrestore(&glink->idr_lock, flags);
1554 channel->rcid = ret;
1555 spin_unlock_irqrestore(&glink->idr_lock, flags);
1557 complete_all(&channel->open_req);
1559 if (create_device) {
1560 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1566 rpdev->ept = &channel->ept;
1567 strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1568 rpdev->src = RPMSG_ADDR_ANY;
1569 rpdev->dst = RPMSG_ADDR_ANY;
1570 rpdev->ops = &glink_device_ops;
1572 node = qcom_glink_match_channel(glink->dev->of_node, name);
1573 rpdev->dev.of_node = node;
1574 rpdev->dev.parent = glink->dev;
1575 rpdev->dev.release = qcom_glink_rpdev_release;
1577 ret = rpmsg_register_device(rpdev);
1581 channel->rpdev = rpdev;
1587 spin_lock_irqsave(&glink->idr_lock, flags);
1588 idr_remove(&glink->rcids, channel->rcid);
1590 spin_unlock_irqrestore(&glink->idr_lock, flags);
1592 /* Release the reference, iff we took it */
1594 kref_put(&channel->refcount, qcom_glink_channel_release);
1599 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1601 struct rpmsg_channel_info chinfo;
1602 struct glink_channel *channel;
1603 unsigned long flags;
1605 spin_lock_irqsave(&glink->idr_lock, flags);
1606 channel = idr_find(&glink->rcids, rcid);
1607 spin_unlock_irqrestore(&glink->idr_lock, flags);
1608 if (WARN(!channel, "close request on unknown channel\n"))
1611 /* cancel pending rx_done work */
1612 cancel_work_sync(&channel->intent_work);
1614 if (channel->rpdev) {
1615 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1616 chinfo.src = RPMSG_ADDR_ANY;
1617 chinfo.dst = RPMSG_ADDR_ANY;
1619 rpmsg_unregister_device(glink->dev, &chinfo);
1621 channel->rpdev = NULL;
1623 qcom_glink_send_close_ack(glink, channel->rcid);
1625 spin_lock_irqsave(&glink->idr_lock, flags);
1626 idr_remove(&glink->rcids, channel->rcid);
1628 spin_unlock_irqrestore(&glink->idr_lock, flags);
1630 kref_put(&channel->refcount, qcom_glink_channel_release);
1633 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1635 struct rpmsg_channel_info chinfo;
1636 struct glink_channel *channel;
1637 unsigned long flags;
1639 /* To wakeup any blocking writers */
1640 wake_up_all(&glink->tx_avail_notify);
1642 spin_lock_irqsave(&glink->idr_lock, flags);
1643 channel = idr_find(&glink->lcids, lcid);
1644 if (WARN(!channel, "close ack on unknown channel\n")) {
1645 spin_unlock_irqrestore(&glink->idr_lock, flags);
1649 idr_remove(&glink->lcids, channel->lcid);
1651 spin_unlock_irqrestore(&glink->idr_lock, flags);
1653 /* Decouple the potential rpdev from the channel */
1654 if (channel->rpdev) {
1655 strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1656 chinfo.src = RPMSG_ADDR_ANY;
1657 chinfo.dst = RPMSG_ADDR_ANY;
1659 rpmsg_unregister_device(glink->dev, &chinfo);
1661 channel->rpdev = NULL;
1663 kref_put(&channel->refcount, qcom_glink_channel_release);
1666 static void qcom_glink_work(struct work_struct *work)
1668 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1670 struct glink_defer_cmd *dcmd;
1671 struct glink_msg *msg;
1672 unsigned long flags;
1673 unsigned int param1;
1674 unsigned int param2;
1678 spin_lock_irqsave(&glink->rx_lock, flags);
1679 if (list_empty(&glink->rx_queue)) {
1680 spin_unlock_irqrestore(&glink->rx_lock, flags);
1683 dcmd = list_first_entry(&glink->rx_queue,
1684 struct glink_defer_cmd, node);
1685 list_del(&dcmd->node);
1686 spin_unlock_irqrestore(&glink->rx_lock, flags);
1689 cmd = le16_to_cpu(msg->cmd);
1690 param1 = le16_to_cpu(msg->param1);
1691 param2 = le32_to_cpu(msg->param2);
1694 case GLINK_CMD_VERSION:
1695 qcom_glink_receive_version(glink, param1, param2);
1697 case GLINK_CMD_VERSION_ACK:
1698 qcom_glink_receive_version_ack(glink, param1, param2);
1700 case GLINK_CMD_OPEN:
1701 qcom_glink_rx_open(glink, param1, msg->data);
1703 case GLINK_CMD_CLOSE:
1704 qcom_glink_rx_close(glink, param1);
1706 case GLINK_CMD_CLOSE_ACK:
1707 qcom_glink_rx_close_ack(glink, param1);
1709 case GLINK_CMD_RX_INTENT_REQ:
1710 qcom_glink_handle_intent_req(glink, param1, param2);
1713 WARN(1, "Unknown defer object %d\n", cmd);
1721 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1723 struct glink_defer_cmd *dcmd;
1724 struct glink_defer_cmd *tmp;
1726 /* cancel any pending deferred rx_work */
1727 cancel_work_sync(&glink->rx_work);
1729 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1733 static ssize_t rpmsg_name_show(struct device *dev,
1734 struct device_attribute *attr, char *buf)
1739 ret = of_property_read_string(dev->of_node, "label", &name);
1741 name = dev->of_node->name;
1743 return sysfs_emit(buf, "%s\n", name);
1745 static DEVICE_ATTR_RO(rpmsg_name);
1747 static struct attribute *qcom_glink_attrs[] = {
1748 &dev_attr_rpmsg_name.attr,
1751 ATTRIBUTE_GROUPS(qcom_glink);
1753 static void qcom_glink_device_release(struct device *dev)
1755 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1756 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1758 /* Release qcom_glink_alloc_channel() reference */
1759 kref_put(&channel->refcount, qcom_glink_channel_release);
1760 kfree(rpdev->driver_override);
1764 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1766 struct rpmsg_device *rpdev;
1767 struct glink_channel *channel;
1769 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1773 channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1774 if (IS_ERR(channel)) {
1776 return PTR_ERR(channel);
1778 channel->rpdev = rpdev;
1780 rpdev->ept = &channel->ept;
1781 rpdev->ops = &glink_device_ops;
1782 rpdev->dev.parent = glink->dev;
1783 rpdev->dev.release = qcom_glink_device_release;
1785 return rpmsg_ctrldev_register_device(rpdev);
1788 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1789 unsigned long features,
1790 struct qcom_glink_pipe *rx,
1791 struct qcom_glink_pipe *tx,
1795 struct qcom_glink *glink;
1797 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1799 return ERR_PTR(-ENOMEM);
1802 glink->tx_pipe = tx;
1803 glink->rx_pipe = rx;
1805 glink->features = features;
1806 glink->intentless = intentless;
1808 spin_lock_init(&glink->tx_lock);
1809 spin_lock_init(&glink->rx_lock);
1810 INIT_LIST_HEAD(&glink->rx_queue);
1811 INIT_WORK(&glink->rx_work, qcom_glink_work);
1812 init_waitqueue_head(&glink->tx_avail_notify);
1814 spin_lock_init(&glink->idr_lock);
1815 idr_init(&glink->lcids);
1816 idr_init(&glink->rcids);
1818 glink->dev->groups = qcom_glink_groups;
1820 ret = device_add_groups(dev, qcom_glink_groups);
1822 dev_err(dev, "failed to add groups\n");
1824 ret = qcom_glink_send_version(glink);
1826 return ERR_PTR(ret);
1828 ret = qcom_glink_create_chrdev(glink);
1830 dev_err(glink->dev, "failed to register chrdev\n");
1834 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1836 static int qcom_glink_remove_device(struct device *dev, void *data)
1838 device_unregister(dev);
1843 void qcom_glink_native_remove(struct qcom_glink *glink)
1845 struct glink_channel *channel;
1846 unsigned long flags;
1850 qcom_glink_cancel_rx_work(glink);
1852 /* Fail all attempts at sending messages */
1853 spin_lock_irqsave(&glink->tx_lock, flags);
1854 glink->abort_tx = true;
1855 wake_up_all(&glink->tx_avail_notify);
1856 spin_unlock_irqrestore(&glink->tx_lock, flags);
1858 /* Abort any senders waiting for intent requests */
1859 spin_lock_irqsave(&glink->idr_lock, flags);
1860 idr_for_each_entry(&glink->lcids, channel, cid)
1861 qcom_glink_intent_req_abort(channel);
1862 spin_unlock_irqrestore(&glink->idr_lock, flags);
1864 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1866 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1868 /* Release any defunct local channels, waiting for close-ack */
1869 idr_for_each_entry(&glink->lcids, channel, cid)
1870 kref_put(&channel->refcount, qcom_glink_channel_release);
1872 /* Release any defunct local channels, waiting for close-req */
1873 idr_for_each_entry(&glink->rcids, channel, cid)
1874 kref_put(&channel->refcount, qcom_glink_channel_release);
1876 idr_destroy(&glink->lcids);
1877 idr_destroy(&glink->rcids);
1879 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1881 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1882 MODULE_LICENSE("GPL v2");