^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2017 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/qrtr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/soc/qcom/qmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct socket *qmi_sock_create(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct sockaddr_qrtr *sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * qmi_recv_new_server() - handler of NEW_SERVER control message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @service: service id of the new server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @instance: instance id of the new server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @node: node of the new server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @port: port of the new server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Calls the new_server callback to inform the client about a newly registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * server matching the currently registered service lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void qmi_recv_new_server(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int service, unsigned int instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int node, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct qmi_ops *ops = &qmi->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct qmi_service *svc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!ops->new_server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Ignore EOF marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (!node && !port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) svc = kzalloc(sizeof(*svc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!svc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) svc->service = service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) svc->version = instance & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) svc->instance = instance >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) svc->node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) svc->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) ret = ops->new_server(qmi, svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kfree(svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list_add(&svc->list_node, &qmi->lookup_results);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^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) * qmi_recv_del_server() - handler of DEL_SERVER control message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @node: node of the dying server, a value of -1 matches all nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @port: port of the dying server, a value of -1 matches all ports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Calls the del_server callback for each previously seen server, allowing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * client to react to the disappearing server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void qmi_recv_del_server(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int node, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct qmi_ops *ops = &qmi->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct qmi_service *svc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct qmi_service *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) list_for_each_entry_safe(svc, tmp, &qmi->lookup_results, list_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (node != -1 && svc->node != node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (port != -1 && svc->port != port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ops->del_server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ops->del_server(qmi, svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) list_del(&svc->list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) kfree(svc);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * qmi_recv_bye() - handler of BYE control message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @node: id of the dying node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Signals the client that all previously registered services on this node are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * now gone and then calls the bye callback to allow the client client further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * cleaning up resources associated with this remote.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void qmi_recv_bye(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unsigned int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct qmi_ops *ops = &qmi->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) qmi_recv_del_server(qmi, node, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (ops->bye)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ops->bye(qmi, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * qmi_recv_del_client() - handler of DEL_CLIENT control message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @node: node of the dying client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @port: port of the dying client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * Signals the client about a dying client, by calling the del_client callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void qmi_recv_del_client(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned int node, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct qmi_ops *ops = &qmi->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (ops->del_client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ops->del_client(qmi, node, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void qmi_recv_ctrl_pkt(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) const void *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const struct qrtr_ctrl_pkt *pkt = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (len < sizeof(struct qrtr_ctrl_pkt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pr_debug("ignoring short control packet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) switch (le32_to_cpu(pkt->cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) case QRTR_TYPE_BYE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) qmi_recv_bye(qmi, le32_to_cpu(pkt->client.node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) case QRTR_TYPE_NEW_SERVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) qmi_recv_new_server(qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) le32_to_cpu(pkt->server.service),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) le32_to_cpu(pkt->server.instance),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) le32_to_cpu(pkt->server.node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) le32_to_cpu(pkt->server.port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case QRTR_TYPE_DEL_SERVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) qmi_recv_del_server(qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) le32_to_cpu(pkt->server.node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) le32_to_cpu(pkt->server.port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) case QRTR_TYPE_DEL_CLIENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) qmi_recv_del_client(qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) le32_to_cpu(pkt->client.node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) le32_to_cpu(pkt->client.port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void qmi_send_new_lookup(struct qmi_handle *qmi, struct qmi_service *svc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct qrtr_ctrl_pkt pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct sockaddr_qrtr sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct msghdr msg = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct kvec iv = { &pkt, sizeof(pkt) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_LOOKUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) pkt.server.service = cpu_to_le32(svc->service);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) sq.sq_family = qmi->sq.sq_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) sq.sq_node = qmi->sq.sq_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) sq.sq_port = QRTR_PORT_CTRL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) msg.msg_name = &sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) msg.msg_namelen = sizeof(sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (qmi->sock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) pr_err("failed to send lookup registration: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * qmi_add_lookup() - register a new lookup with the name service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * @service: service id of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * @instance: instance id of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @version: version number of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Registering a lookup query with the name server will cause the name server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * to send NEW_SERVER and DEL_SERVER control messages to this socket as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * matching services are registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int qmi_add_lookup(struct qmi_handle *qmi, unsigned int service,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned int version, unsigned int instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct qmi_service *svc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) svc = kzalloc(sizeof(*svc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (!svc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) svc->service = service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) svc->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) svc->instance = instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) list_add(&svc->list_node, &qmi->lookups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) qmi_send_new_lookup(qmi, svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) EXPORT_SYMBOL(qmi_add_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void qmi_send_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct qrtr_ctrl_pkt pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct sockaddr_qrtr sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct msghdr msg = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct kvec iv = { &pkt, sizeof(pkt) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) pkt.server.service = cpu_to_le32(svc->service);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) pkt.server.node = cpu_to_le32(qmi->sq.sq_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pkt.server.port = cpu_to_le32(qmi->sq.sq_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) sq.sq_family = qmi->sq.sq_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) sq.sq_node = qmi->sq.sq_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) sq.sq_port = QRTR_PORT_CTRL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) msg.msg_name = &sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) msg.msg_namelen = sizeof(sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (qmi->sock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) pr_err("send service registration failed: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) mutex_unlock(&qmi->sock_lock);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * qmi_add_server() - register a service with the name service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * @service: type of the service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @instance: instance of the service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * @version: version of the service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * Register a new service with the name service. This allows clients to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * and start sending messages to the client associated with @qmi.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int qmi_add_server(struct qmi_handle *qmi, unsigned int service,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) unsigned int version, unsigned int instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct qmi_service *svc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) svc = kzalloc(sizeof(*svc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!svc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) svc->service = service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) svc->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) svc->instance = instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) list_add(&svc->list_node, &qmi->services);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) qmi_send_new_server(qmi, svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) EXPORT_SYMBOL(qmi_add_server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * qmi_txn_init() - allocate transaction id within the given QMI handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * @qmi: QMI handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * @txn: transaction context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @ei: description of how to decode a matching response (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * @c_struct: pointer to the object to decode the response into (optional)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * This allocates a transaction id within the QMI handle. If @ei and @c_struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * are specified any responses to this transaction will be decoded as described
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * by @ei into @c_struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * A client calling qmi_txn_init() must call either qmi_txn_wait() or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * qmi_txn_cancel() to free up the allocated resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * Return: Transaction id on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct qmi_elem_info *ei, void *c_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) memset(txn, 0, sizeof(*txn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mutex_init(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) init_completion(&txn->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) txn->qmi = qmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) txn->ei = ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) txn->dest = c_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) mutex_lock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) ret = idr_alloc_cyclic(&qmi->txns, txn, 0, U16_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) pr_err("failed to allocate transaction id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) txn->id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) mutex_unlock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) EXPORT_SYMBOL(qmi_txn_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * qmi_txn_wait() - wait for a response on a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * @txn: transaction handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * @timeout: timeout, in jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * If the transaction is decoded by the means of @ei and @c_struct the return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * value will be the returned value of qmi_decode_message(), otherwise it's up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * to the specified message handler to fill out the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Return: the transaction response on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) int qmi_txn_wait(struct qmi_txn *txn, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct qmi_handle *qmi = txn->qmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret = wait_for_completion_timeout(&txn->completion, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) mutex_lock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) mutex_lock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) idr_remove(&qmi->txns, txn->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) mutex_unlock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) mutex_unlock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return txn->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) EXPORT_SYMBOL(qmi_txn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * qmi_txn_cancel() - cancel an ongoing transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @txn: transaction id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void qmi_txn_cancel(struct qmi_txn *txn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct qmi_handle *qmi = txn->qmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) mutex_lock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) mutex_lock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) idr_remove(&qmi->txns, txn->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) mutex_unlock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) mutex_unlock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) EXPORT_SYMBOL(qmi_txn_cancel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * qmi_invoke_handler() - find and invoke a handler for a message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * @qmi: qmi handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * @sq: sockaddr of the sender
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * @txn: transaction object for the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * @buf: buffer containing the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * @len: length of @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Find handler and invoke handler for the incoming message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static void qmi_invoke_handler(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) struct qmi_txn *txn, const void *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) const struct qmi_msg_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) const struct qmi_header *hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) void *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!qmi->handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) for (handler = qmi->handlers; handler->fn; handler++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (handler->type == hdr->type &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) handler->msg_id == hdr->msg_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (!handler->fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) dest = kzalloc(handler->decoded_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (!dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) ret = qmi_decode_message(buf, len, handler->ei, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) pr_err("failed to decode incoming message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) handler->fn(qmi, sq, txn, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) kfree(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * qmi_handle_net_reset() - invoked to handle ENETRESET on a QMI handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @qmi: the QMI context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * As a result of registering a name service with the QRTR all open sockets are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * flagged with ENETRESET and this function will be called. The typical case is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * the initial boot, where this signals that the local node id has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * configured and as such any bound sockets needs to be rebound. So close the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * socket, inform the client and re-initialize the socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * For clients it's generally sufficient to react to the del_server callbacks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * but server code is expected to treat the net_reset callback as a "bye" from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * all nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * Finally the QMI handle will send out registration requests for any lookups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * and services.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static void qmi_handle_net_reset(struct qmi_handle *qmi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct sockaddr_qrtr sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct qmi_service *svc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) sock = qmi_sock_create(qmi, &sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (IS_ERR(sock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) sock_release(qmi->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) qmi->sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) qmi_recv_del_server(qmi, -1, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (qmi->ops.net_reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) qmi->ops.net_reset(qmi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) qmi->sock = sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) qmi->sq = sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) list_for_each_entry(svc, &qmi->lookups, list_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) qmi_send_new_lookup(qmi, svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) list_for_each_entry(svc, &qmi->services, list_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) qmi_send_new_server(qmi, svc);
^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 void qmi_handle_message(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct sockaddr_qrtr *sq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) const void *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) const struct qmi_header *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct qmi_txn tmp_txn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct qmi_txn *txn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (len < sizeof(*hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_err("ignoring short QMI packet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) hdr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* If this is a response, find the matching transaction handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (hdr->type == QMI_RESPONSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) mutex_lock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) txn = idr_find(&qmi->txns, hdr->txn_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /* Ignore unexpected responses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (!txn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) mutex_unlock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) mutex_lock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) mutex_unlock(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (txn->dest && txn->ei) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) ret = qmi_decode_message(buf, len, txn->ei, txn->dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pr_err("failed to decode incoming message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) txn->result = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) complete(&txn->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) qmi_invoke_handler(qmi, sq, txn, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) mutex_unlock(&txn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* Create a txn based on the txn_id of the incoming message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) memset(&tmp_txn, 0, sizeof(tmp_txn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) tmp_txn.id = hdr->txn_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) qmi_invoke_handler(qmi, sq, &tmp_txn, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static void qmi_data_ready_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct qmi_handle *qmi = container_of(work, struct qmi_handle, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct qmi_ops *ops = &qmi->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct sockaddr_qrtr sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct msghdr msg = { .msg_name = &sq, .msg_namelen = sizeof(sq) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) struct kvec iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ssize_t msglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) iv.iov_base = qmi->recv_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) iv.iov_len = qmi->recv_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (qmi->sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) msglen = kernel_recvmsg(qmi->sock, &msg, &iv, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) iv.iov_len, MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) msglen = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (msglen == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (msglen == -ENETRESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) qmi_handle_net_reset(qmi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) /* The old qmi->sock is gone, our work is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (msglen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) pr_err("qmi recvmsg failed: %zd\n", msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (sq.sq_node == qmi->sq.sq_node &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) sq.sq_port == QRTR_PORT_CTRL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) qmi_recv_ctrl_pkt(qmi, qmi->recv_buf, msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) } else if (ops->msg_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ops->msg_handler(qmi, &sq, qmi->recv_buf, msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) qmi_handle_message(qmi, &sq, qmi->recv_buf, msglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static void qmi_data_ready(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) struct qmi_handle *qmi = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * This will be NULL if we receive data while being in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * qmi_handle_release()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (!qmi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) queue_work(qmi->wq, &qmi->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) static struct socket *qmi_sock_create(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct sockaddr_qrtr *sq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) PF_QIPCRTR, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) ret = kernel_getsockname(sock, (struct sockaddr *)sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) sock->sk->sk_user_data = qmi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) sock->sk->sk_data_ready = qmi_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) sock->sk->sk_error_report = qmi_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) return sock;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * qmi_handle_init() - initialize a QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * @qmi: QMI handle to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * @recv_buf_size: maximum size of incoming message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * @ops: reference to callbacks for QRTR notifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * @handlers: NULL-terminated list of QMI message handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * This initializes the QMI client handle to allow sending and receiving QMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * messages. As messages are received the appropriate handler will be invoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int qmi_handle_init(struct qmi_handle *qmi, size_t recv_buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) const struct qmi_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) const struct qmi_msg_handler *handlers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) mutex_init(&qmi->txn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) mutex_init(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) idr_init(&qmi->txns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) INIT_LIST_HEAD(&qmi->lookups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) INIT_LIST_HEAD(&qmi->lookup_results);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) INIT_LIST_HEAD(&qmi->services);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) INIT_WORK(&qmi->work, qmi_data_ready_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) qmi->handlers = handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) qmi->ops = *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* Make room for the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) recv_buf_size += sizeof(struct qmi_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) /* Must also be sufficient to hold a control packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (recv_buf_size < sizeof(struct qrtr_ctrl_pkt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) recv_buf_size = sizeof(struct qrtr_ctrl_pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) qmi->recv_buf_size = recv_buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) qmi->recv_buf = kzalloc(recv_buf_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) if (!qmi->recv_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) qmi->wq = alloc_workqueue("qmi_msg_handler", WQ_UNBOUND, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!qmi->wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) goto err_free_recv_buf;
^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) qmi->sock = qmi_sock_create(qmi, &qmi->sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (IS_ERR(qmi->sock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (PTR_ERR(qmi->sock) == -EAFNOSUPPORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ret = -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) pr_err("failed to create QMI socket\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) ret = PTR_ERR(qmi->sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) goto err_destroy_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) err_destroy_wq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) destroy_workqueue(qmi->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) err_free_recv_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) kfree(qmi->recv_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) EXPORT_SYMBOL(qmi_handle_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * qmi_handle_release() - release the QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * @qmi: QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * This closes the underlying socket and stops any handling of QMI messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) void qmi_handle_release(struct qmi_handle *qmi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct socket *sock = qmi->sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct qmi_service *svc, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) sock->sk->sk_user_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) cancel_work_sync(&qmi->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) qmi_recv_del_server(qmi, -1, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) qmi->sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) destroy_workqueue(qmi->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) idr_destroy(&qmi->txns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) kfree(qmi->recv_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /* Free registered lookup requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) list_for_each_entry_safe(svc, tmp, &qmi->lookups, list_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) list_del(&svc->list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) kfree(svc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* Free registered service information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) list_for_each_entry_safe(svc, tmp, &qmi->services, list_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) list_del(&svc->list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) kfree(svc);
^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) EXPORT_SYMBOL(qmi_handle_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * qmi_send_message() - send a QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * @qmi: QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * @sq: destination sockaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * @txn: transaction object to use for the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * @type: type of message to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) * @msg_id: message id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * @len: max length of the QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * @ei: QMI message description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * @c_struct: object to be encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * This function encodes @c_struct using @ei into a message of type @type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * with @msg_id and @txn into a buffer of maximum size @len, and sends this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * @sq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) static ssize_t qmi_send_message(struct qmi_handle *qmi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) struct sockaddr_qrtr *sq, struct qmi_txn *txn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) int type, int msg_id, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct qmi_elem_info *ei, const void *c_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct msghdr msghdr = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct kvec iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) void *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) msg = qmi_encode_message(type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) msg_id, &len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) txn->id, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) c_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (IS_ERR(msg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return PTR_ERR(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) iv.iov_base = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) iv.iov_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) if (sq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) msghdr.msg_name = sq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) msghdr.msg_namelen = sizeof(*sq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) mutex_lock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (qmi->sock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) ret = kernel_sendmsg(qmi->sock, &msghdr, &iv, 1, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) pr_err("failed to send QMI message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ret = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) mutex_unlock(&qmi->sock_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) kfree(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * qmi_send_request() - send a request QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * @qmi: QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * @sq: destination sockaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * @txn: transaction object to use for the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * @msg_id: message id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * @len: max length of the QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * @ei: QMI message description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * @c_struct: object to be encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct qmi_txn *txn, int msg_id, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct qmi_elem_info *ei, const void *c_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return qmi_send_message(qmi, sq, txn, QMI_REQUEST, msg_id, len, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) c_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) EXPORT_SYMBOL(qmi_send_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * qmi_send_response() - send a response QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * @qmi: QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * @sq: destination sockaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * @txn: transaction object to use for the message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * @msg_id: message id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * @len: max length of the QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) * @ei: QMI message description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * @c_struct: object to be encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) struct qmi_txn *txn, int msg_id, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct qmi_elem_info *ei, const void *c_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return qmi_send_message(qmi, sq, txn, QMI_RESPONSE, msg_id, len, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) c_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) EXPORT_SYMBOL(qmi_send_response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * qmi_send_indication() - send an indication QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * @qmi: QMI client handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * @sq: destination sockaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * @msg_id: message id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * @len: max length of the QMI message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * @ei: QMI message description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * @c_struct: object to be encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int msg_id, size_t len, struct qmi_elem_info *ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) const void *c_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct qmi_txn txn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ssize_t rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) ret = qmi_txn_init(qmi, &txn, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) rval = qmi_send_message(qmi, sq, &txn, QMI_INDICATION, msg_id, len, ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) c_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /* We don't care about future messages on this txn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) qmi_txn_cancel(&txn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) EXPORT_SYMBOL(qmi_send_indication);