qmi_interface.c 21 KB

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