qmi_interface.c 21 KB

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