qmi_interface.c 21 KB

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