qmi_interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 <trace/events/sock.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_GPL(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_GPL(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. mutex_init(&txn->lock);
  272. init_completion(&txn->completion);
  273. txn->qmi = qmi;
  274. txn->ei = ei;
  275. txn->dest = c_struct;
  276. mutex_lock(&qmi->txn_lock);
  277. ret = idr_alloc_cyclic(&qmi->txns, txn, 0, U16_MAX, GFP_KERNEL);
  278. if (ret < 0)
  279. pr_err("failed to allocate transaction id\n");
  280. txn->id = ret;
  281. mutex_unlock(&qmi->txn_lock);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL_GPL(qmi_txn_init);
  285. /**
  286. * qmi_txn_wait() - wait for a response on a transaction
  287. * @txn: transaction handle
  288. * @timeout: timeout, in jiffies
  289. *
  290. * If the transaction is decoded by the means of @ei and @c_struct the return
  291. * value will be the returned value of qmi_decode_message(), otherwise it's up
  292. * to the specified message handler to fill out the result.
  293. *
  294. * Return: the transaction response on success, negative errno on failure.
  295. */
  296. int qmi_txn_wait(struct qmi_txn *txn, unsigned long timeout)
  297. {
  298. struct qmi_handle *qmi = txn->qmi;
  299. int ret;
  300. ret = wait_for_completion_timeout(&txn->completion, timeout);
  301. mutex_lock(&qmi->txn_lock);
  302. mutex_lock(&txn->lock);
  303. idr_remove(&qmi->txns, txn->id);
  304. mutex_unlock(&txn->lock);
  305. mutex_unlock(&qmi->txn_lock);
  306. if (ret == 0)
  307. return -ETIMEDOUT;
  308. else
  309. return txn->result;
  310. }
  311. EXPORT_SYMBOL_GPL(qmi_txn_wait);
  312. /**
  313. * qmi_txn_cancel() - cancel an ongoing transaction
  314. * @txn: transaction id
  315. */
  316. void qmi_txn_cancel(struct qmi_txn *txn)
  317. {
  318. struct qmi_handle *qmi = txn->qmi;
  319. mutex_lock(&qmi->txn_lock);
  320. mutex_lock(&txn->lock);
  321. idr_remove(&qmi->txns, txn->id);
  322. mutex_unlock(&txn->lock);
  323. mutex_unlock(&qmi->txn_lock);
  324. }
  325. EXPORT_SYMBOL_GPL(qmi_txn_cancel);
  326. /**
  327. * qmi_invoke_handler() - find and invoke a handler for a message
  328. * @qmi: qmi handle
  329. * @sq: sockaddr of the sender
  330. * @txn: transaction object for the message
  331. * @buf: buffer containing the message
  332. * @len: length of @buf
  333. *
  334. * Find handler and invoke handler for the incoming message.
  335. */
  336. static void qmi_invoke_handler(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  337. struct qmi_txn *txn, const void *buf, size_t len)
  338. {
  339. const struct qmi_msg_handler *handler;
  340. const struct qmi_header *hdr = buf;
  341. void *dest;
  342. int ret;
  343. if (!qmi->handlers)
  344. return;
  345. for (handler = qmi->handlers; handler->fn; handler++) {
  346. if (handler->type == hdr->type &&
  347. handler->msg_id == hdr->msg_id)
  348. break;
  349. }
  350. if (!handler->fn)
  351. return;
  352. dest = kzalloc(handler->decoded_size, GFP_KERNEL);
  353. if (!dest)
  354. return;
  355. ret = qmi_decode_message(buf, len, handler->ei, dest);
  356. if (ret < 0)
  357. pr_err("failed to decode incoming message\n");
  358. else
  359. handler->fn(qmi, sq, txn, dest);
  360. kfree(dest);
  361. }
  362. /**
  363. * qmi_handle_net_reset() - invoked to handle ENETRESET on a QMI handle
  364. * @qmi: the QMI context
  365. *
  366. * As a result of registering a name service with the QRTR all open sockets are
  367. * flagged with ENETRESET and this function will be called. The typical case is
  368. * the initial boot, where this signals that the local node id has been
  369. * configured and as such any bound sockets needs to be rebound. So close the
  370. * socket, inform the client and re-initialize the socket.
  371. *
  372. * For clients it's generally sufficient to react to the del_server callbacks,
  373. * but server code is expected to treat the net_reset callback as a "bye" from
  374. * all nodes.
  375. *
  376. * Finally the QMI handle will send out registration requests for any lookups
  377. * and services.
  378. */
  379. static void qmi_handle_net_reset(struct qmi_handle *qmi)
  380. {
  381. struct sockaddr_qrtr sq;
  382. struct qmi_service *svc;
  383. struct socket *sock;
  384. sock = qmi_sock_create(qmi, &sq);
  385. if (IS_ERR(sock))
  386. return;
  387. mutex_lock(&qmi->sock_lock);
  388. sock_release(qmi->sock);
  389. qmi->sock = NULL;
  390. mutex_unlock(&qmi->sock_lock);
  391. qmi_recv_del_server(qmi, -1, -1);
  392. if (qmi->ops.net_reset)
  393. qmi->ops.net_reset(qmi);
  394. mutex_lock(&qmi->sock_lock);
  395. qmi->sock = sock;
  396. qmi->sq = sq;
  397. mutex_unlock(&qmi->sock_lock);
  398. list_for_each_entry(svc, &qmi->lookups, list_node)
  399. qmi_send_new_lookup(qmi, svc);
  400. list_for_each_entry(svc, &qmi->services, list_node)
  401. qmi_send_new_server(qmi, svc);
  402. }
  403. static void qmi_handle_message(struct qmi_handle *qmi,
  404. struct sockaddr_qrtr *sq,
  405. const void *buf, size_t len)
  406. {
  407. const struct qmi_header *hdr;
  408. struct qmi_txn tmp_txn;
  409. struct qmi_txn *txn = NULL;
  410. int ret;
  411. if (len < sizeof(*hdr)) {
  412. pr_err("ignoring short QMI packet\n");
  413. return;
  414. }
  415. hdr = buf;
  416. /* If this is a response, find the matching transaction handle */
  417. if (hdr->type == QMI_RESPONSE) {
  418. mutex_lock(&qmi->txn_lock);
  419. txn = idr_find(&qmi->txns, hdr->txn_id);
  420. /* Ignore unexpected responses */
  421. if (!txn) {
  422. mutex_unlock(&qmi->txn_lock);
  423. return;
  424. }
  425. mutex_lock(&txn->lock);
  426. mutex_unlock(&qmi->txn_lock);
  427. if (txn->dest && txn->ei) {
  428. ret = qmi_decode_message(buf, len, txn->ei, txn->dest);
  429. if (ret < 0)
  430. pr_err("failed to decode incoming message\n");
  431. txn->result = ret;
  432. complete(&txn->completion);
  433. } else {
  434. qmi_invoke_handler(qmi, sq, txn, buf, len);
  435. }
  436. mutex_unlock(&txn->lock);
  437. } else {
  438. /* Create a txn based on the txn_id of the incoming message */
  439. memset(&tmp_txn, 0, sizeof(tmp_txn));
  440. tmp_txn.id = hdr->txn_id;
  441. qmi_invoke_handler(qmi, sq, &tmp_txn, buf, len);
  442. }
  443. }
  444. static void qmi_data_ready_work(struct work_struct *work)
  445. {
  446. struct qmi_handle *qmi = container_of(work, struct qmi_handle, work);
  447. struct qmi_ops *ops = &qmi->ops;
  448. struct sockaddr_qrtr sq;
  449. struct msghdr msg = { .msg_name = &sq, .msg_namelen = sizeof(sq) };
  450. struct kvec iv;
  451. ssize_t msglen;
  452. for (;;) {
  453. iv.iov_base = qmi->recv_buf;
  454. iv.iov_len = qmi->recv_buf_size;
  455. mutex_lock(&qmi->sock_lock);
  456. if (qmi->sock)
  457. msglen = kernel_recvmsg(qmi->sock, &msg, &iv, 1,
  458. iv.iov_len, MSG_DONTWAIT);
  459. else
  460. msglen = -EPIPE;
  461. mutex_unlock(&qmi->sock_lock);
  462. if (msglen == -EAGAIN)
  463. break;
  464. if (msglen == -ENETRESET) {
  465. qmi_handle_net_reset(qmi);
  466. /* The old qmi->sock is gone, our work is done */
  467. break;
  468. }
  469. if (msglen < 0) {
  470. pr_err("qmi recvmsg failed: %zd\n", msglen);
  471. break;
  472. }
  473. if (sq.sq_node == qmi->sq.sq_node &&
  474. sq.sq_port == QRTR_PORT_CTRL) {
  475. qmi_recv_ctrl_pkt(qmi, qmi->recv_buf, msglen);
  476. } else if (ops->msg_handler) {
  477. ops->msg_handler(qmi, &sq, qmi->recv_buf, msglen);
  478. } else {
  479. qmi_handle_message(qmi, &sq, qmi->recv_buf, msglen);
  480. }
  481. }
  482. }
  483. static void qmi_data_ready(struct sock *sk)
  484. {
  485. struct qmi_handle *qmi = sk->sk_user_data;
  486. trace_sk_data_ready(sk);
  487. /*
  488. * This will be NULL if we receive data while being in
  489. * qmi_handle_release()
  490. */
  491. if (!qmi)
  492. return;
  493. queue_work(qmi->wq, &qmi->work);
  494. }
  495. static struct socket *qmi_sock_create(struct qmi_handle *qmi,
  496. struct sockaddr_qrtr *sq)
  497. {
  498. struct socket *sock;
  499. int ret;
  500. ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM,
  501. PF_QIPCRTR, &sock);
  502. if (ret < 0)
  503. return ERR_PTR(ret);
  504. ret = kernel_getsockname(sock, (struct sockaddr *)sq);
  505. if (ret < 0) {
  506. sock_release(sock);
  507. return ERR_PTR(ret);
  508. }
  509. sock->sk->sk_user_data = qmi;
  510. sock->sk->sk_data_ready = qmi_data_ready;
  511. sock->sk->sk_error_report = qmi_data_ready;
  512. return sock;
  513. }
  514. /**
  515. * qmi_handle_init() - initialize a QMI client handle
  516. * @qmi: QMI handle to initialize
  517. * @recv_buf_size: maximum size of incoming message
  518. * @ops: reference to callbacks for QRTR notifications
  519. * @handlers: NULL-terminated list of QMI message handlers
  520. *
  521. * This initializes the QMI client handle to allow sending and receiving QMI
  522. * messages. As messages are received the appropriate handler will be invoked.
  523. *
  524. * Return: 0 on success, negative errno on failure.
  525. */
  526. int qmi_handle_init(struct qmi_handle *qmi, size_t recv_buf_size,
  527. const struct qmi_ops *ops,
  528. const struct qmi_msg_handler *handlers)
  529. {
  530. int ret;
  531. mutex_init(&qmi->txn_lock);
  532. mutex_init(&qmi->sock_lock);
  533. idr_init(&qmi->txns);
  534. INIT_LIST_HEAD(&qmi->lookups);
  535. INIT_LIST_HEAD(&qmi->lookup_results);
  536. INIT_LIST_HEAD(&qmi->services);
  537. INIT_WORK(&qmi->work, qmi_data_ready_work);
  538. qmi->handlers = handlers;
  539. if (ops)
  540. qmi->ops = *ops;
  541. /* Make room for the header */
  542. recv_buf_size += sizeof(struct qmi_header);
  543. /* Must also be sufficient to hold a control packet */
  544. if (recv_buf_size < sizeof(struct qrtr_ctrl_pkt))
  545. recv_buf_size = sizeof(struct qrtr_ctrl_pkt);
  546. qmi->recv_buf_size = recv_buf_size;
  547. qmi->recv_buf = kzalloc(recv_buf_size, GFP_KERNEL);
  548. if (!qmi->recv_buf)
  549. return -ENOMEM;
  550. qmi->wq = alloc_ordered_workqueue("qmi_msg_handler", 0);
  551. if (!qmi->wq) {
  552. ret = -ENOMEM;
  553. goto err_free_recv_buf;
  554. }
  555. qmi->sock = qmi_sock_create(qmi, &qmi->sq);
  556. if (IS_ERR(qmi->sock)) {
  557. if (PTR_ERR(qmi->sock) == -EAFNOSUPPORT) {
  558. ret = -EPROBE_DEFER;
  559. } else {
  560. pr_err("failed to create QMI socket\n");
  561. ret = PTR_ERR(qmi->sock);
  562. }
  563. goto err_destroy_wq;
  564. }
  565. return 0;
  566. err_destroy_wq:
  567. destroy_workqueue(qmi->wq);
  568. err_free_recv_buf:
  569. kfree(qmi->recv_buf);
  570. return ret;
  571. }
  572. EXPORT_SYMBOL_GPL(qmi_handle_init);
  573. /**
  574. * qmi_handle_release() - release the QMI client handle
  575. * @qmi: QMI client handle
  576. *
  577. * This closes the underlying socket and stops any handling of QMI messages.
  578. */
  579. void qmi_handle_release(struct qmi_handle *qmi)
  580. {
  581. struct socket *sock = qmi->sock;
  582. struct qmi_service *svc, *tmp;
  583. sock->sk->sk_user_data = NULL;
  584. cancel_work_sync(&qmi->work);
  585. qmi_recv_del_server(qmi, -1, -1);
  586. mutex_lock(&qmi->sock_lock);
  587. sock_release(sock);
  588. qmi->sock = NULL;
  589. mutex_unlock(&qmi->sock_lock);
  590. destroy_workqueue(qmi->wq);
  591. idr_destroy(&qmi->txns);
  592. kfree(qmi->recv_buf);
  593. /* Free registered lookup requests */
  594. list_for_each_entry_safe(svc, tmp, &qmi->lookups, list_node) {
  595. list_del(&svc->list_node);
  596. kfree(svc);
  597. }
  598. /* Free registered service information */
  599. list_for_each_entry_safe(svc, tmp, &qmi->services, list_node) {
  600. list_del(&svc->list_node);
  601. kfree(svc);
  602. }
  603. }
  604. EXPORT_SYMBOL_GPL(qmi_handle_release);
  605. /**
  606. * qmi_send_message() - send a QMI message
  607. * @qmi: QMI client handle
  608. * @sq: destination sockaddr
  609. * @txn: transaction object to use for the message
  610. * @type: type of message to send
  611. * @msg_id: message id
  612. * @len: max length of the QMI message
  613. * @ei: QMI message description
  614. * @c_struct: object to be encoded
  615. *
  616. * This function encodes @c_struct using @ei into a message of type @type,
  617. * with @msg_id and @txn into a buffer of maximum size @len, and sends this to
  618. * @sq.
  619. *
  620. * Return: 0 on success, negative errno on failure.
  621. */
  622. static ssize_t qmi_send_message(struct qmi_handle *qmi,
  623. struct sockaddr_qrtr *sq, struct qmi_txn *txn,
  624. int type, int msg_id, size_t len,
  625. const struct qmi_elem_info *ei,
  626. const void *c_struct)
  627. {
  628. struct msghdr msghdr = {};
  629. struct kvec iv;
  630. void *msg;
  631. int ret;
  632. msg = qmi_encode_message(type,
  633. msg_id, &len,
  634. txn->id, ei,
  635. c_struct);
  636. if (IS_ERR(msg))
  637. return PTR_ERR(msg);
  638. iv.iov_base = msg;
  639. iv.iov_len = len;
  640. if (sq) {
  641. msghdr.msg_name = sq;
  642. msghdr.msg_namelen = sizeof(*sq);
  643. }
  644. mutex_lock(&qmi->sock_lock);
  645. if (qmi->sock) {
  646. ret = kernel_sendmsg(qmi->sock, &msghdr, &iv, 1, len);
  647. if (ret < 0)
  648. pr_err("failed to send QMI message\n");
  649. } else {
  650. ret = -EPIPE;
  651. }
  652. mutex_unlock(&qmi->sock_lock);
  653. kfree(msg);
  654. return ret < 0 ? ret : 0;
  655. }
  656. /**
  657. * qmi_send_request() - send a request QMI message
  658. * @qmi: QMI client handle
  659. * @sq: destination sockaddr
  660. * @txn: transaction object to use for the message
  661. * @msg_id: message id
  662. * @len: max length of the QMI message
  663. * @ei: QMI message description
  664. * @c_struct: object to be encoded
  665. *
  666. * Return: 0 on success, negative errno on failure.
  667. */
  668. ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  669. struct qmi_txn *txn, int msg_id, size_t len,
  670. const struct qmi_elem_info *ei, const void *c_struct)
  671. {
  672. return qmi_send_message(qmi, sq, txn, QMI_REQUEST, msg_id, len, ei,
  673. c_struct);
  674. }
  675. EXPORT_SYMBOL_GPL(qmi_send_request);
  676. /**
  677. * qmi_send_response() - send a response QMI message
  678. * @qmi: QMI client handle
  679. * @sq: destination sockaddr
  680. * @txn: transaction object to use for the message
  681. * @msg_id: message id
  682. * @len: max length of the QMI message
  683. * @ei: QMI message description
  684. * @c_struct: object to be encoded
  685. *
  686. * Return: 0 on success, negative errno on failure.
  687. */
  688. ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  689. struct qmi_txn *txn, int msg_id, size_t len,
  690. const struct qmi_elem_info *ei, const void *c_struct)
  691. {
  692. return qmi_send_message(qmi, sq, txn, QMI_RESPONSE, msg_id, len, ei,
  693. c_struct);
  694. }
  695. EXPORT_SYMBOL_GPL(qmi_send_response);
  696. /**
  697. * qmi_send_indication() - send an indication QMI message
  698. * @qmi: QMI client handle
  699. * @sq: destination sockaddr
  700. * @msg_id: message id
  701. * @len: max length of the QMI message
  702. * @ei: QMI message description
  703. * @c_struct: object to be encoded
  704. *
  705. * Return: 0 on success, negative errno on failure.
  706. */
  707. ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  708. int msg_id, size_t len,
  709. const struct qmi_elem_info *ei,
  710. const void *c_struct)
  711. {
  712. struct qmi_txn txn;
  713. ssize_t rval;
  714. int ret;
  715. ret = qmi_txn_init(qmi, &txn, NULL, NULL);
  716. if (ret < 0)
  717. return ret;
  718. rval = qmi_send_message(qmi, sq, &txn, QMI_INDICATION, msg_id, len, ei,
  719. c_struct);
  720. /* We don't care about future messages on this txn */
  721. qmi_txn_cancel(&txn);
  722. return rval;
  723. }
  724. EXPORT_SYMBOL_GPL(qmi_send_indication);