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);
231 init_completion(&channel->open_req);
232 init_completion(&channel->open_ack);
233 init_waitqueue_head(&channel->intent_req_wq);
235 INIT_LIST_HEAD(&channel->done_intents);
236 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
238 idr_init(&channel->liids);
239 idr_init(&channel->riids);
240 kref_init(&channel->refcount);
245 static void qcom_glink_channel_release(struct kref *ref)
247 struct glink_channel *channel = container_of(ref, struct glink_channel,
249 struct glink_core_rx_intent *intent;
250 struct glink_core_rx_intent *tmp;
254 /* cancel pending rx_done work */
255 cancel_work_sync(&channel->intent_work);
257 spin_lock_irqsave(&channel->intent_lock, flags);
258 /* Free all non-reuse intents pending rx_done work */
259 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
260 if (!intent->reuse) {
266 idr_for_each_entry(&channel->liids, tmp, iid) {
270 idr_destroy(&channel->liids);
272 idr_for_each_entry(&channel->riids, tmp, iid)
274 idr_destroy(&channel->riids);
275 spin_unlock_irqrestore(&channel->intent_lock, flags);
277 kfree(channel->name);
281 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
283 return glink->rx_pipe->avail(glink->rx_pipe);
286 static void qcom_glink_rx_peek(struct qcom_glink *glink,
287 void *data, unsigned int offset, size_t count)
289 glink->rx_pipe->peek(glink->rx_pipe, data, offset, count);
292 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
294 glink->rx_pipe->advance(glink->rx_pipe, count);
297 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
299 return glink->tx_pipe->avail(glink->tx_pipe);
302 static void qcom_glink_tx_write(struct qcom_glink *glink,
303 const void *hdr, size_t hlen,
304 const void *data, size_t dlen)
306 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
309 static void qcom_glink_tx_kick(struct qcom_glink *glink)
311 glink->tx_pipe->kick(glink->tx_pipe);
314 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
316 struct glink_msg msg;
318 msg.cmd = cpu_to_le16(GLINK_CMD_READ_NOTIF);
322 qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
324 qcom_glink_tx_kick(glink);
327 static int qcom_glink_tx(struct qcom_glink *glink,
328 const void *hdr, size_t hlen,
329 const void *data, size_t dlen, bool wait)
331 unsigned int tlen = hlen + dlen;
335 /* Reject packets that are too big */
336 if (tlen >= glink->tx_pipe->length)
339 spin_lock_irqsave(&glink->tx_lock, flags);
341 if (glink->abort_tx) {
346 while (qcom_glink_tx_avail(glink) < tlen) {
352 if (glink->abort_tx) {
357 if (!glink->sent_read_notify) {
358 glink->sent_read_notify = true;
359 qcom_glink_send_read_notify(glink);
362 /* Wait without holding the tx_lock */
363 spin_unlock_irqrestore(&glink->tx_lock, flags);
365 wait_event_timeout(glink->tx_avail_notify,
366 qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
368 spin_lock_irqsave(&glink->tx_lock, flags);
370 if (qcom_glink_tx_avail(glink) >= tlen)
371 glink->sent_read_notify = false;
374 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
375 qcom_glink_tx_kick(glink);
378 spin_unlock_irqrestore(&glink->tx_lock, flags);
383 static int qcom_glink_send_version(struct qcom_glink *glink)
385 struct glink_msg msg;
387 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION);
388 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
389 msg.param2 = cpu_to_le32(glink->features);
391 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
394 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
396 struct glink_msg msg;
398 msg.cmd = cpu_to_le16(GLINK_CMD_VERSION_ACK);
399 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
400 msg.param2 = cpu_to_le32(glink->features);
402 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
405 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
406 struct glink_channel *channel)
408 struct glink_msg msg;
410 msg.cmd = cpu_to_le16(GLINK_CMD_OPEN_ACK);
411 msg.param1 = cpu_to_le16(channel->rcid);
412 msg.param2 = cpu_to_le32(0);
414 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
417 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
418 unsigned int cid, bool granted)
420 struct glink_channel *channel;
423 spin_lock_irqsave(&glink->idr_lock, flags);
424 channel = idr_find(&glink->rcids, cid);
425 spin_unlock_irqrestore(&glink->idr_lock, flags);
427 dev_err(glink->dev, "unable to find channel\n");
431 WRITE_ONCE(channel->intent_req_result, granted);
432 wake_up_all(&channel->intent_req_wq);
435 static void qcom_glink_intent_req_abort(struct glink_channel *channel)
437 WRITE_ONCE(channel->intent_req_result, 0);
438 wake_up_all(&channel->intent_req_wq);
442 * qcom_glink_send_open_req() - send a GLINK_CMD_OPEN request to the remote
443 * @glink: Ptr to the glink edge
444 * @channel: Ptr to the channel that the open req is sent
446 * Allocates a local channel id and sends a GLINK_CMD_OPEN message to the remote.
447 * Will return with refcount held, regardless of outcome.
449 * Return: 0 on success, negative errno otherwise.
451 static int qcom_glink_send_open_req(struct qcom_glink *glink,
452 struct glink_channel *channel)
455 struct glink_msg msg;
456 u8 name[GLINK_NAME_SIZE];
458 int name_len = strlen(channel->name) + 1;
459 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
463 kref_get(&channel->refcount);
465 spin_lock_irqsave(&glink->idr_lock, flags);
466 ret = idr_alloc_cyclic(&glink->lcids, channel,
467 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
469 spin_unlock_irqrestore(&glink->idr_lock, flags);
475 req.msg.cmd = cpu_to_le16(GLINK_CMD_OPEN);
476 req.msg.param1 = cpu_to_le16(channel->lcid);
477 req.msg.param2 = cpu_to_le32(name_len);
478 strcpy(req.name, channel->name);
480 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
487 spin_lock_irqsave(&glink->idr_lock, flags);
488 idr_remove(&glink->lcids, channel->lcid);
490 spin_unlock_irqrestore(&glink->idr_lock, flags);
495 static void qcom_glink_send_close_req(struct qcom_glink *glink,
496 struct glink_channel *channel)
498 struct glink_msg req;
500 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE);
501 req.param1 = cpu_to_le16(channel->lcid);
504 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
507 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
510 struct glink_msg req;
512 req.cmd = cpu_to_le16(GLINK_CMD_CLOSE_ACK);
513 req.param1 = cpu_to_le16(rcid);
516 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
519 static void qcom_glink_rx_done_work(struct work_struct *work)
521 struct glink_channel *channel = container_of(work, struct glink_channel,
523 struct qcom_glink *glink = channel->glink;
524 struct glink_core_rx_intent *intent, *tmp;
531 unsigned int cid = channel->lcid;
536 spin_lock_irqsave(&channel->intent_lock, flags);
537 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
538 list_del(&intent->node);
539 spin_unlock_irqrestore(&channel->intent_lock, flags);
541 reuse = intent->reuse;
543 cmd.id = reuse ? GLINK_CMD_RX_DONE_W_REUSE : GLINK_CMD_RX_DONE;
547 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
552 spin_lock_irqsave(&channel->intent_lock, flags);
554 spin_unlock_irqrestore(&channel->intent_lock, flags);
557 static void qcom_glink_rx_done(struct qcom_glink *glink,
558 struct glink_channel *channel,
559 struct glink_core_rx_intent *intent)
561 /* We don't send RX_DONE to intentless systems */
562 if (glink->intentless) {
568 /* Take it off the tree of receive intents */
569 if (!intent->reuse) {
570 spin_lock(&channel->intent_lock);
571 idr_remove(&channel->liids, intent->id);
572 spin_unlock(&channel->intent_lock);
575 /* Schedule the sending of a rx_done indication */
576 spin_lock(&channel->intent_lock);
577 list_add_tail(&intent->node, &channel->done_intents);
578 spin_unlock(&channel->intent_lock);
580 schedule_work(&channel->intent_work);
584 * qcom_glink_receive_version() - receive version/features from remote system
586 * @glink: pointer to transport interface
587 * @version: remote version
588 * @features: remote features
590 * This function is called in response to a remote-initiated version/feature
591 * negotiation sequence.
593 static void qcom_glink_receive_version(struct qcom_glink *glink,
600 case GLINK_VERSION_1:
601 glink->features &= features;
604 qcom_glink_send_version_ack(glink);
610 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
612 * @glink: pointer to transport interface
613 * @version: remote version response
614 * @features: remote features response
616 * This function is called in response to a local-initiated version/feature
617 * negotiation sequence and is the counter-offer from the remote side based
618 * upon the initial version and feature set requested.
620 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
626 /* Version negotiation failed */
628 case GLINK_VERSION_1:
629 if (features == glink->features)
632 glink->features &= features;
635 qcom_glink_send_version(glink);
641 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
642 * wire format and transmit
643 * @glink: The transport to transmit on.
644 * @channel: The glink channel
645 * @granted: The request response to encode.
647 * Return: 0 on success or standard Linux error code.
649 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
650 struct glink_channel *channel,
653 struct glink_msg msg;
655 msg.cmd = cpu_to_le16(GLINK_CMD_RX_INTENT_REQ_ACK);
656 msg.param1 = cpu_to_le16(channel->lcid);
657 msg.param2 = cpu_to_le32(granted);
659 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
665 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
667 * @glink: The transport to transmit on.
668 * @channel: The local channel
669 * @intent: The intent to pass on to remote.
671 * Return: 0 on success or standard Linux error code.
673 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
674 struct glink_channel *channel,
675 struct glink_core_rx_intent *intent)
686 cmd.id = cpu_to_le16(GLINK_CMD_INTENT);
687 cmd.lcid = cpu_to_le16(channel->lcid);
688 cmd.count = cpu_to_le32(1);
689 cmd.size = cpu_to_le32(intent->size);
690 cmd.liid = cpu_to_le32(intent->id);
692 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
697 static struct glink_core_rx_intent *
698 qcom_glink_alloc_intent(struct qcom_glink *glink,
699 struct glink_channel *channel,
703 struct glink_core_rx_intent *intent;
707 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
711 intent->data = kzalloc(size, GFP_KERNEL);
715 spin_lock_irqsave(&channel->intent_lock, flags);
716 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
718 spin_unlock_irqrestore(&channel->intent_lock, flags);
721 spin_unlock_irqrestore(&channel->intent_lock, flags);
725 intent->reuse = reuseable;
736 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
737 u32 cid, uint32_t iid,
740 struct glink_core_rx_intent *intent;
741 struct glink_channel *channel;
744 spin_lock_irqsave(&glink->idr_lock, flags);
745 channel = idr_find(&glink->rcids, cid);
746 spin_unlock_irqrestore(&glink->idr_lock, flags);
748 dev_err(glink->dev, "invalid channel id received\n");
752 spin_lock_irqsave(&channel->intent_lock, flags);
753 intent = idr_find(&channel->riids, iid);
756 spin_unlock_irqrestore(&channel->intent_lock, flags);
757 dev_err(glink->dev, "invalid intent id received\n");
761 intent->in_use = false;
764 idr_remove(&channel->riids, intent->id);
767 spin_unlock_irqrestore(&channel->intent_lock, flags);
770 WRITE_ONCE(channel->intent_received, true);
771 wake_up_all(&channel->intent_req_wq);
776 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
778 * @glink: Pointer to the transport interface
779 * @cid: Remote channel ID
780 * @size: size of the intent
782 * The function searches for the local channel to which the request for
783 * rx_intent has arrived and allocates and notifies the remote back
785 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
786 u32 cid, size_t size)
788 struct glink_core_rx_intent *intent;
789 struct glink_channel *channel;
792 spin_lock_irqsave(&glink->idr_lock, flags);
793 channel = idr_find(&glink->rcids, cid);
794 spin_unlock_irqrestore(&glink->idr_lock, flags);
797 pr_err("%s channel not found for cid %d\n", __func__, cid);
801 intent = qcom_glink_alloc_intent(glink, channel, size, false);
803 qcom_glink_advertise_intent(glink, channel, intent);
805 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
808 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
810 struct glink_defer_cmd *dcmd;
812 extra = ALIGN(extra, 8);
814 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
815 dev_dbg(glink->dev, "Insufficient data in rx fifo");
819 dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
823 INIT_LIST_HEAD(&dcmd->node);
825 qcom_glink_rx_peek(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
827 spin_lock(&glink->rx_lock);
828 list_add_tail(&dcmd->node, &glink->rx_queue);
829 spin_unlock(&glink->rx_lock);
831 schedule_work(&glink->rx_work);
832 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
837 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
839 struct glink_core_rx_intent *intent;
840 struct glink_channel *channel;
842 struct glink_msg msg;
846 unsigned int chunk_size;
847 unsigned int left_size;
853 if (avail < sizeof(hdr)) {
854 dev_dbg(glink->dev, "Not enough data in fifo\n");
858 qcom_glink_rx_peek(glink, &hdr, 0, sizeof(hdr));
859 chunk_size = le32_to_cpu(hdr.chunk_size);
860 left_size = le32_to_cpu(hdr.left_size);
862 if (avail < sizeof(hdr) + chunk_size) {
863 dev_dbg(glink->dev, "Payload not yet in fifo\n");
867 rcid = le16_to_cpu(hdr.msg.param1);
868 spin_lock_irqsave(&glink->idr_lock, flags);
869 channel = idr_find(&glink->rcids, rcid);
870 spin_unlock_irqrestore(&glink->idr_lock, flags);
872 dev_dbg(glink->dev, "Data on non-existing channel\n");
874 /* Drop the message */
878 if (glink->intentless) {
879 /* Might have an ongoing, fragmented, message to append */
881 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
885 intent->data = kmalloc(chunk_size + left_size,
892 intent->id = 0xbabababa;
893 intent->size = chunk_size + left_size;
896 channel->buf = intent;
898 intent = channel->buf;
901 liid = le32_to_cpu(hdr.msg.param2);
903 spin_lock_irqsave(&channel->intent_lock, flags);
904 intent = idr_find(&channel->liids, liid);
905 spin_unlock_irqrestore(&channel->intent_lock, flags);
909 "no intent found for channel %s intent %d",
910 channel->name, liid);
916 if (intent->size - intent->offset < chunk_size) {
917 dev_err(glink->dev, "Insufficient space in intent\n");
919 /* The packet header lied, drop payload */
923 qcom_glink_rx_peek(glink, intent->data + intent->offset,
924 sizeof(hdr), chunk_size);
925 intent->offset += chunk_size;
927 /* Handle message when no fragments remain to be received */
929 spin_lock(&channel->recv_lock);
930 if (channel->ept.cb) {
931 channel->ept.cb(channel->ept.rpdev,
937 spin_unlock(&channel->recv_lock);
942 qcom_glink_rx_done(glink, channel, intent);
946 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
951 static void qcom_glink_handle_intent(struct qcom_glink *glink,
956 struct glink_core_rx_intent *intent;
957 struct glink_channel *channel;
964 struct glink_msg msg;
965 struct intent_pair intents[];
968 const size_t msglen = struct_size(msg, intents, count);
973 if (avail < msglen) {
974 dev_dbg(glink->dev, "Not enough data in fifo\n");
978 spin_lock_irqsave(&glink->idr_lock, flags);
979 channel = idr_find(&glink->rcids, cid);
980 spin_unlock_irqrestore(&glink->idr_lock, flags);
982 dev_err(glink->dev, "intents for non-existing channel\n");
983 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
987 msg = kmalloc(msglen, GFP_ATOMIC);
991 qcom_glink_rx_peek(glink, msg, 0, msglen);
993 for (i = 0; i < count; ++i) {
994 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
998 intent->id = le32_to_cpu(msg->intents[i].iid);
999 intent->size = le32_to_cpu(msg->intents[i].size);
1001 spin_lock_irqsave(&channel->intent_lock, flags);
1002 ret = idr_alloc(&channel->riids, intent,
1003 intent->id, intent->id + 1, GFP_ATOMIC);
1004 spin_unlock_irqrestore(&channel->intent_lock, flags);
1007 dev_err(glink->dev, "failed to store remote intent\n");
1010 WRITE_ONCE(channel->intent_received, true);
1011 wake_up_all(&channel->intent_req_wq);
1014 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
1017 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
1019 struct glink_channel *channel;
1021 spin_lock(&glink->idr_lock);
1022 channel = idr_find(&glink->lcids, lcid);
1023 spin_unlock(&glink->idr_lock);
1025 dev_err(glink->dev, "Invalid open ack packet\n");
1029 complete_all(&channel->open_ack);
1035 * qcom_glink_set_flow_control() - convert a signal cmd to wire format and transmit
1036 * @ept: Rpmsg endpoint for channel.
1037 * @pause: Pause transmission
1038 * @dst: destination address of the endpoint
1040 * Return: 0 on success or standard Linux error code.
1042 static int qcom_glink_set_flow_control(struct rpmsg_endpoint *ept, bool pause, u32 dst)
1044 struct glink_channel *channel = to_glink_channel(ept);
1045 struct qcom_glink *glink = channel->glink;
1046 struct glink_msg msg;
1050 sigs |= NATIVE_DTR_SIG | NATIVE_RTS_SIG;
1052 msg.cmd = cpu_to_le16(GLINK_CMD_SIGNALS);
1053 msg.param1 = cpu_to_le16(channel->lcid);
1054 msg.param2 = cpu_to_le32(sigs);
1056 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
1059 static void qcom_glink_handle_signals(struct qcom_glink *glink,
1060 unsigned int rcid, unsigned int sigs)
1062 struct glink_channel *channel;
1063 unsigned long flags;
1066 spin_lock_irqsave(&glink->idr_lock, flags);
1067 channel = idr_find(&glink->rcids, rcid);
1068 spin_unlock_irqrestore(&glink->idr_lock, flags);
1070 dev_err(glink->dev, "signal for non-existing channel\n");
1072 enable = sigs & NATIVE_DSR_SIG || sigs & NATIVE_CTS_SIG;
1074 if (channel->ept.flow_cb)
1075 channel->ept.flow_cb(channel->ept.rpdev, channel->ept.priv, enable);
1078 void qcom_glink_native_rx(struct qcom_glink *glink)
1080 struct glink_msg msg;
1081 unsigned int param1;
1082 unsigned int param2;
1087 /* To wakeup any blocking writers */
1088 wake_up_all(&glink->tx_avail_notify);
1091 avail = qcom_glink_rx_avail(glink);
1092 if (avail < sizeof(msg))
1095 qcom_glink_rx_peek(glink, &msg, 0, sizeof(msg));
1097 cmd = le16_to_cpu(msg.cmd);
1098 param1 = le16_to_cpu(msg.param1);
1099 param2 = le32_to_cpu(msg.param2);
1102 case GLINK_CMD_VERSION:
1103 case GLINK_CMD_VERSION_ACK:
1104 case GLINK_CMD_CLOSE:
1105 case GLINK_CMD_CLOSE_ACK:
1106 case GLINK_CMD_RX_INTENT_REQ:
1107 ret = qcom_glink_rx_defer(glink, 0);
1109 case GLINK_CMD_OPEN_ACK:
1110 ret = qcom_glink_rx_open_ack(glink, param1);
1111 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1113 case GLINK_CMD_OPEN:
1114 ret = qcom_glink_rx_defer(glink, param2);
1116 case GLINK_CMD_TX_DATA:
1117 case GLINK_CMD_TX_DATA_CONT:
1118 ret = qcom_glink_rx_data(glink, avail);
1120 case GLINK_CMD_READ_NOTIF:
1121 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1122 qcom_glink_tx_kick(glink);
1124 case GLINK_CMD_INTENT:
1125 qcom_glink_handle_intent(glink, param1, param2, avail);
1127 case GLINK_CMD_RX_DONE:
1128 qcom_glink_handle_rx_done(glink, param1, param2, false);
1129 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1131 case GLINK_CMD_RX_DONE_W_REUSE:
1132 qcom_glink_handle_rx_done(glink, param1, param2, true);
1133 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1135 case GLINK_CMD_RX_INTENT_REQ_ACK:
1136 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1137 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1139 case GLINK_CMD_SIGNALS:
1140 qcom_glink_handle_signals(glink, param1, param2);
1141 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1144 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1153 EXPORT_SYMBOL(qcom_glink_native_rx);
1155 /* Locally initiated rpmsg_create_ept */
1156 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1159 struct glink_channel *channel;
1161 unsigned long flags;
1163 channel = qcom_glink_alloc_channel(glink, name);
1164 if (IS_ERR(channel))
1165 return ERR_CAST(channel);
1167 ret = qcom_glink_send_open_req(glink, channel);
1169 goto release_channel;
1171 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1175 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1179 qcom_glink_send_open_ack(glink, channel);
1184 /* qcom_glink_send_open_req() did register the channel in lcids*/
1185 spin_lock_irqsave(&glink->idr_lock, flags);
1186 idr_remove(&glink->lcids, channel->lcid);
1187 spin_unlock_irqrestore(&glink->idr_lock, flags);
1190 /* Release qcom_glink_send_open_req() reference */
1191 kref_put(&channel->refcount, qcom_glink_channel_release);
1192 /* Release qcom_glink_alloc_channel() reference */
1193 kref_put(&channel->refcount, qcom_glink_channel_release);
1195 return ERR_PTR(-ETIMEDOUT);
1198 /* Remote initiated rpmsg_create_ept */
1199 static int qcom_glink_create_remote(struct qcom_glink *glink,
1200 struct glink_channel *channel)
1204 qcom_glink_send_open_ack(glink, channel);
1206 ret = qcom_glink_send_open_req(glink, channel);
1210 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1220 * Send a close request to "undo" our open-ack. The close-ack will
1221 * release qcom_glink_send_open_req() reference and the last reference
1222 * will be relesed after receiving remote_close or transport unregister
1223 * by calling qcom_glink_native_remove().
1225 qcom_glink_send_close_req(glink, channel);
1230 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1233 struct rpmsg_channel_info
1236 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1237 struct glink_channel *channel;
1238 struct qcom_glink *glink = parent->glink;
1239 struct rpmsg_endpoint *ept;
1240 const char *name = chinfo.name;
1243 unsigned long flags;
1245 spin_lock_irqsave(&glink->idr_lock, flags);
1246 idr_for_each_entry(&glink->rcids, channel, cid) {
1247 if (!strcmp(channel->name, name))
1250 spin_unlock_irqrestore(&glink->idr_lock, flags);
1253 channel = qcom_glink_create_local(glink, name);
1254 if (IS_ERR(channel))
1257 ret = qcom_glink_create_remote(glink, channel);
1262 ept = &channel->ept;
1266 ept->ops = &glink_endpoint_ops;
1271 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1273 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1274 struct device_node *np = rpdev->dev.of_node;
1275 struct qcom_glink *glink = channel->glink;
1276 struct glink_core_rx_intent *intent;
1277 const struct property *prop = NULL;
1278 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1281 __be32 *val = defaults;
1284 if (glink->intentless || !completion_done(&channel->open_ack))
1287 prop = of_find_property(np, "qcom,intents", NULL);
1290 num_groups = prop->length / sizeof(u32) / 2;
1293 /* Channel is now open, advertise base set of intents */
1294 while (num_groups--) {
1295 size = be32_to_cpup(val++);
1296 num_intents = be32_to_cpup(val++);
1297 while (num_intents--) {
1298 intent = qcom_glink_alloc_intent(glink, channel, size,
1303 qcom_glink_advertise_intent(glink, channel, intent);
1309 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1311 struct glink_channel *channel = to_glink_channel(ept);
1312 struct qcom_glink *glink = channel->glink;
1313 unsigned long flags;
1315 spin_lock_irqsave(&channel->recv_lock, flags);
1316 channel->ept.cb = NULL;
1317 spin_unlock_irqrestore(&channel->recv_lock, flags);
1319 /* Decouple the potential rpdev from the channel */
1320 channel->rpdev = NULL;
1322 qcom_glink_send_close_req(glink, channel);
1325 static int qcom_glink_request_intent(struct qcom_glink *glink,
1326 struct glink_channel *channel,
1337 mutex_lock(&channel->intent_req_lock);
1339 WRITE_ONCE(channel->intent_req_result, -1);
1340 WRITE_ONCE(channel->intent_received, false);
1342 cmd.id = GLINK_CMD_RX_INTENT_REQ;
1343 cmd.cid = channel->lcid;
1346 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1350 ret = wait_event_timeout(channel->intent_req_wq,
1351 READ_ONCE(channel->intent_req_result) >= 0 &&
1352 READ_ONCE(channel->intent_received),
1355 dev_err(glink->dev, "intent request timed out\n");
1358 ret = READ_ONCE(channel->intent_req_result) ? 0 : -ECANCELED;
1362 mutex_unlock(&channel->intent_req_lock);
1366 static int __qcom_glink_send(struct glink_channel *channel,
1367 void *data, int len, bool wait)
1369 struct qcom_glink *glink = channel->glink;
1370 struct glink_core_rx_intent *intent = NULL;
1371 struct glink_core_rx_intent *tmp;
1374 struct glink_msg msg;
1379 unsigned long flags;
1380 int chunk_size = len;
1383 if (!glink->intentless) {
1385 spin_lock_irqsave(&channel->intent_lock, flags);
1386 idr_for_each_entry(&channel->riids, tmp, iid) {
1387 if (tmp->size >= len && !tmp->in_use) {
1390 else if (intent->size > tmp->size)
1392 if (intent->size == len)
1397 intent->in_use = true;
1398 spin_unlock_irqrestore(&channel->intent_lock, flags);
1400 /* We found an available intent */
1407 ret = qcom_glink_request_intent(glink, channel, len);
1415 while (offset < len) {
1416 chunk_size = len - offset;
1417 if (chunk_size > SZ_8K && wait)
1420 req.msg.cmd = cpu_to_le16(offset == 0 ? GLINK_CMD_TX_DATA : GLINK_CMD_TX_DATA_CONT);
1421 req.msg.param1 = cpu_to_le16(channel->lcid);
1422 req.msg.param2 = cpu_to_le32(iid);
1423 req.chunk_size = cpu_to_le32(chunk_size);
1424 req.left_size = cpu_to_le32(len - offset - chunk_size);
1426 ret = qcom_glink_tx(glink, &req, sizeof(req), data + offset, chunk_size, wait);
1428 /* Mark intent available if we failed */
1430 intent->in_use = false;
1434 offset += chunk_size;
1440 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1442 struct glink_channel *channel = to_glink_channel(ept);
1444 return __qcom_glink_send(channel, data, len, true);
1447 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1449 struct glink_channel *channel = to_glink_channel(ept);
1451 return __qcom_glink_send(channel, data, len, false);
1454 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1456 struct glink_channel *channel = to_glink_channel(ept);
1458 return __qcom_glink_send(channel, data, len, true);
1461 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1463 struct glink_channel *channel = to_glink_channel(ept);
1465 return __qcom_glink_send(channel, data, len, false);
1469 * Finds the device_node for the glink child interested in this channel.
1471 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1472 const char *channel)
1474 struct device_node *child;
1479 for_each_available_child_of_node(node, child) {
1480 key = "qcom,glink-channels";
1481 ret = of_property_read_string(child, key, &name);
1485 if (strcmp(name, channel) == 0)
1492 static const struct rpmsg_device_ops glink_device_ops = {
1493 .create_ept = qcom_glink_create_ept,
1494 .announce_create = qcom_glink_announce_create,
1497 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1498 .destroy_ept = qcom_glink_destroy_ept,
1499 .send = qcom_glink_send,
1500 .sendto = qcom_glink_sendto,
1501 .trysend = qcom_glink_trysend,
1502 .trysendto = qcom_glink_trysendto,
1503 .set_flow_control = qcom_glink_set_flow_control,
1506 static void qcom_glink_rpdev_release(struct device *dev)
1508 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1510 kfree(rpdev->driver_override);
1514 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1517 struct glink_channel *channel;
1518 struct rpmsg_device *rpdev;
1519 bool create_device = false;
1520 struct device_node *node;
1523 unsigned long flags;
1525 spin_lock_irqsave(&glink->idr_lock, flags);
1526 idr_for_each_entry(&glink->lcids, channel, lcid) {
1527 if (!strcmp(channel->name, name))
1530 spin_unlock_irqrestore(&glink->idr_lock, flags);
1533 channel = qcom_glink_alloc_channel(glink, name);
1534 if (IS_ERR(channel))
1535 return PTR_ERR(channel);
1537 /* The opening dance was initiated by the remote */
1538 create_device = true;
1541 spin_lock_irqsave(&glink->idr_lock, flags);
1542 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1544 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1545 spin_unlock_irqrestore(&glink->idr_lock, flags);
1548 channel->rcid = ret;
1549 spin_unlock_irqrestore(&glink->idr_lock, flags);
1551 complete_all(&channel->open_req);
1553 if (create_device) {
1554 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1560 rpdev->ept = &channel->ept;
1561 strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1562 rpdev->src = RPMSG_ADDR_ANY;
1563 rpdev->dst = RPMSG_ADDR_ANY;
1564 rpdev->ops = &glink_device_ops;
1566 node = qcom_glink_match_channel(glink->dev->of_node, name);
1567 rpdev->dev.of_node = node;
1568 rpdev->dev.parent = glink->dev;
1569 rpdev->dev.release = qcom_glink_rpdev_release;
1571 ret = rpmsg_register_device(rpdev);
1575 channel->rpdev = rpdev;
1581 spin_lock_irqsave(&glink->idr_lock, flags);
1582 idr_remove(&glink->rcids, channel->rcid);
1584 spin_unlock_irqrestore(&glink->idr_lock, flags);
1586 /* Release the reference, iff we took it */
1588 kref_put(&channel->refcount, qcom_glink_channel_release);
1593 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1595 struct rpmsg_channel_info chinfo;
1596 struct glink_channel *channel;
1597 unsigned long flags;
1599 spin_lock_irqsave(&glink->idr_lock, flags);
1600 channel = idr_find(&glink->rcids, rcid);
1601 spin_unlock_irqrestore(&glink->idr_lock, flags);
1602 if (WARN(!channel, "close request on unknown channel\n"))
1605 /* cancel pending rx_done work */
1606 cancel_work_sync(&channel->intent_work);
1608 if (channel->rpdev) {
1609 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1610 chinfo.src = RPMSG_ADDR_ANY;
1611 chinfo.dst = RPMSG_ADDR_ANY;
1613 rpmsg_unregister_device(glink->dev, &chinfo);
1615 channel->rpdev = NULL;
1617 qcom_glink_send_close_ack(glink, channel->rcid);
1619 spin_lock_irqsave(&glink->idr_lock, flags);
1620 idr_remove(&glink->rcids, channel->rcid);
1622 spin_unlock_irqrestore(&glink->idr_lock, flags);
1624 kref_put(&channel->refcount, qcom_glink_channel_release);
1627 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1629 struct rpmsg_channel_info chinfo;
1630 struct glink_channel *channel;
1631 unsigned long flags;
1633 /* To wakeup any blocking writers */
1634 wake_up_all(&glink->tx_avail_notify);
1636 spin_lock_irqsave(&glink->idr_lock, flags);
1637 channel = idr_find(&glink->lcids, lcid);
1638 if (WARN(!channel, "close ack on unknown channel\n")) {
1639 spin_unlock_irqrestore(&glink->idr_lock, flags);
1643 idr_remove(&glink->lcids, channel->lcid);
1645 spin_unlock_irqrestore(&glink->idr_lock, flags);
1647 /* Decouple the potential rpdev from the channel */
1648 if (channel->rpdev) {
1649 strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1650 chinfo.src = RPMSG_ADDR_ANY;
1651 chinfo.dst = RPMSG_ADDR_ANY;
1653 rpmsg_unregister_device(glink->dev, &chinfo);
1655 channel->rpdev = NULL;
1657 kref_put(&channel->refcount, qcom_glink_channel_release);
1660 static void qcom_glink_work(struct work_struct *work)
1662 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1664 struct glink_defer_cmd *dcmd;
1665 struct glink_msg *msg;
1666 unsigned long flags;
1667 unsigned int param1;
1668 unsigned int param2;
1672 spin_lock_irqsave(&glink->rx_lock, flags);
1673 if (list_empty(&glink->rx_queue)) {
1674 spin_unlock_irqrestore(&glink->rx_lock, flags);
1677 dcmd = list_first_entry(&glink->rx_queue,
1678 struct glink_defer_cmd, node);
1679 list_del(&dcmd->node);
1680 spin_unlock_irqrestore(&glink->rx_lock, flags);
1683 cmd = le16_to_cpu(msg->cmd);
1684 param1 = le16_to_cpu(msg->param1);
1685 param2 = le32_to_cpu(msg->param2);
1688 case GLINK_CMD_VERSION:
1689 qcom_glink_receive_version(glink, param1, param2);
1691 case GLINK_CMD_VERSION_ACK:
1692 qcom_glink_receive_version_ack(glink, param1, param2);
1694 case GLINK_CMD_OPEN:
1695 qcom_glink_rx_open(glink, param1, msg->data);
1697 case GLINK_CMD_CLOSE:
1698 qcom_glink_rx_close(glink, param1);
1700 case GLINK_CMD_CLOSE_ACK:
1701 qcom_glink_rx_close_ack(glink, param1);
1703 case GLINK_CMD_RX_INTENT_REQ:
1704 qcom_glink_handle_intent_req(glink, param1, param2);
1707 WARN(1, "Unknown defer object %d\n", cmd);
1715 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1717 struct glink_defer_cmd *dcmd;
1718 struct glink_defer_cmd *tmp;
1720 /* cancel any pending deferred rx_work */
1721 cancel_work_sync(&glink->rx_work);
1723 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1727 static ssize_t rpmsg_name_show(struct device *dev,
1728 struct device_attribute *attr, char *buf)
1733 ret = of_property_read_string(dev->of_node, "label", &name);
1735 name = dev->of_node->name;
1737 return sysfs_emit(buf, "%s\n", name);
1739 static DEVICE_ATTR_RO(rpmsg_name);
1741 static struct attribute *qcom_glink_attrs[] = {
1742 &dev_attr_rpmsg_name.attr,
1745 ATTRIBUTE_GROUPS(qcom_glink);
1747 static void qcom_glink_device_release(struct device *dev)
1749 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1750 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1752 /* Release qcom_glink_alloc_channel() reference */
1753 kref_put(&channel->refcount, qcom_glink_channel_release);
1754 kfree(rpdev->driver_override);
1758 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1760 struct rpmsg_device *rpdev;
1761 struct glink_channel *channel;
1763 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1767 channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1768 if (IS_ERR(channel)) {
1770 return PTR_ERR(channel);
1772 channel->rpdev = rpdev;
1774 rpdev->ept = &channel->ept;
1775 rpdev->ops = &glink_device_ops;
1776 rpdev->dev.parent = glink->dev;
1777 rpdev->dev.release = qcom_glink_device_release;
1779 return rpmsg_ctrldev_register_device(rpdev);
1782 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1783 unsigned long features,
1784 struct qcom_glink_pipe *rx,
1785 struct qcom_glink_pipe *tx,
1789 struct qcom_glink *glink;
1791 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1793 return ERR_PTR(-ENOMEM);
1796 glink->tx_pipe = tx;
1797 glink->rx_pipe = rx;
1799 glink->features = features;
1800 glink->intentless = intentless;
1802 spin_lock_init(&glink->tx_lock);
1803 spin_lock_init(&glink->rx_lock);
1804 INIT_LIST_HEAD(&glink->rx_queue);
1805 INIT_WORK(&glink->rx_work, qcom_glink_work);
1806 init_waitqueue_head(&glink->tx_avail_notify);
1808 spin_lock_init(&glink->idr_lock);
1809 idr_init(&glink->lcids);
1810 idr_init(&glink->rcids);
1812 glink->dev->groups = qcom_glink_groups;
1814 ret = device_add_groups(dev, qcom_glink_groups);
1816 dev_err(dev, "failed to add groups\n");
1818 ret = qcom_glink_send_version(glink);
1820 return ERR_PTR(ret);
1822 ret = qcom_glink_create_chrdev(glink);
1824 dev_err(glink->dev, "failed to register chrdev\n");
1828 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1830 static int qcom_glink_remove_device(struct device *dev, void *data)
1832 device_unregister(dev);
1837 void qcom_glink_native_remove(struct qcom_glink *glink)
1839 struct glink_channel *channel;
1840 unsigned long flags;
1844 qcom_glink_cancel_rx_work(glink);
1846 /* Fail all attempts at sending messages */
1847 spin_lock_irqsave(&glink->tx_lock, flags);
1848 glink->abort_tx = true;
1849 wake_up_all(&glink->tx_avail_notify);
1850 spin_unlock_irqrestore(&glink->tx_lock, flags);
1852 /* Abort any senders waiting for intent requests */
1853 spin_lock_irqsave(&glink->idr_lock, flags);
1854 idr_for_each_entry(&glink->lcids, channel, cid)
1855 qcom_glink_intent_req_abort(channel);
1856 spin_unlock_irqrestore(&glink->idr_lock, flags);
1858 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1860 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1862 /* Release any defunct local channels, waiting for close-ack */
1863 idr_for_each_entry(&glink->lcids, channel, cid)
1864 kref_put(&channel->refcount, qcom_glink_channel_release);
1866 /* Release any defunct local channels, waiting for close-req */
1867 idr_for_each_entry(&glink->rcids, channel, cid)
1868 kref_put(&channel->refcount, qcom_glink_channel_release);
1870 idr_destroy(&glink->lcids);
1871 idr_destroy(&glink->rcids);
1873 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1875 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1876 MODULE_LICENSE("GPL v2");