This source file includes following definitions.
- qmi_recv_new_server
- qmi_recv_del_server
- qmi_recv_bye
- qmi_recv_del_client
- qmi_recv_ctrl_pkt
- qmi_send_new_lookup
- qmi_add_lookup
- qmi_send_new_server
- qmi_add_server
- qmi_txn_init
- qmi_txn_wait
- qmi_txn_cancel
- qmi_invoke_handler
- qmi_handle_net_reset
- qmi_handle_message
- qmi_data_ready_work
- qmi_data_ready
- qmi_sock_create
- qmi_handle_init
- qmi_handle_release
- qmi_send_message
- qmi_send_request
- qmi_send_response
- qmi_send_indication
1
2
3
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
17 static struct socket *qmi_sock_create(struct qmi_handle *qmi,
18 struct sockaddr_qrtr *sq);
19
20
21
22
23
24
25
26
27
28
29
30
31 static void qmi_recv_new_server(struct qmi_handle *qmi,
32 unsigned int service, unsigned int instance,
33 unsigned int node, unsigned int port)
34 {
35 struct qmi_ops *ops = &qmi->ops;
36 struct qmi_service *svc;
37 int ret;
38
39 if (!ops->new_server)
40 return;
41
42
43 if (!node && !port)
44 return;
45
46 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
47 if (!svc)
48 return;
49
50 svc->service = service;
51 svc->version = instance & 0xff;
52 svc->instance = instance >> 8;
53 svc->node = node;
54 svc->port = port;
55
56 ret = ops->new_server(qmi, svc);
57 if (ret < 0)
58 kfree(svc);
59 else
60 list_add(&svc->list_node, &qmi->lookup_results);
61 }
62
63
64
65
66
67
68
69
70
71
72 static void qmi_recv_del_server(struct qmi_handle *qmi,
73 unsigned int node, unsigned int port)
74 {
75 struct qmi_ops *ops = &qmi->ops;
76 struct qmi_service *svc;
77 struct qmi_service *tmp;
78
79 list_for_each_entry_safe(svc, tmp, &qmi->lookup_results, list_node) {
80 if (node != -1 && svc->node != node)
81 continue;
82 if (port != -1 && svc->port != port)
83 continue;
84
85 if (ops->del_server)
86 ops->del_server(qmi, svc);
87
88 list_del(&svc->list_node);
89 kfree(svc);
90 }
91 }
92
93
94
95
96
97
98
99
100
101
102 static void qmi_recv_bye(struct qmi_handle *qmi,
103 unsigned int node)
104 {
105 struct qmi_ops *ops = &qmi->ops;
106
107 qmi_recv_del_server(qmi, node, -1);
108
109 if (ops->bye)
110 ops->bye(qmi, node);
111 }
112
113
114
115
116
117
118
119
120
121 static void qmi_recv_del_client(struct qmi_handle *qmi,
122 unsigned int node, unsigned int port)
123 {
124 struct qmi_ops *ops = &qmi->ops;
125
126 if (ops->del_client)
127 ops->del_client(qmi, node, port);
128 }
129
130 static void qmi_recv_ctrl_pkt(struct qmi_handle *qmi,
131 const void *buf, size_t len)
132 {
133 const struct qrtr_ctrl_pkt *pkt = buf;
134
135 if (len < sizeof(struct qrtr_ctrl_pkt)) {
136 pr_debug("ignoring short control packet\n");
137 return;
138 }
139
140 switch (le32_to_cpu(pkt->cmd)) {
141 case QRTR_TYPE_BYE:
142 qmi_recv_bye(qmi, le32_to_cpu(pkt->client.node));
143 break;
144 case QRTR_TYPE_NEW_SERVER:
145 qmi_recv_new_server(qmi,
146 le32_to_cpu(pkt->server.service),
147 le32_to_cpu(pkt->server.instance),
148 le32_to_cpu(pkt->server.node),
149 le32_to_cpu(pkt->server.port));
150 break;
151 case QRTR_TYPE_DEL_SERVER:
152 qmi_recv_del_server(qmi,
153 le32_to_cpu(pkt->server.node),
154 le32_to_cpu(pkt->server.port));
155 break;
156 case QRTR_TYPE_DEL_CLIENT:
157 qmi_recv_del_client(qmi,
158 le32_to_cpu(pkt->client.node),
159 le32_to_cpu(pkt->client.port));
160 break;
161 }
162 }
163
164 static void qmi_send_new_lookup(struct qmi_handle *qmi, struct qmi_service *svc)
165 {
166 struct qrtr_ctrl_pkt pkt;
167 struct sockaddr_qrtr sq;
168 struct msghdr msg = { };
169 struct kvec iv = { &pkt, sizeof(pkt) };
170 int ret;
171
172 memset(&pkt, 0, sizeof(pkt));
173 pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_LOOKUP);
174 pkt.server.service = cpu_to_le32(svc->service);
175 pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
176
177 sq.sq_family = qmi->sq.sq_family;
178 sq.sq_node = qmi->sq.sq_node;
179 sq.sq_port = QRTR_PORT_CTRL;
180
181 msg.msg_name = &sq;
182 msg.msg_namelen = sizeof(sq);
183
184 mutex_lock(&qmi->sock_lock);
185 if (qmi->sock) {
186 ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
187 if (ret < 0)
188 pr_err("failed to send lookup registration: %d\n", ret);
189 }
190 mutex_unlock(&qmi->sock_lock);
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206 int qmi_add_lookup(struct qmi_handle *qmi, unsigned int service,
207 unsigned int version, unsigned int instance)
208 {
209 struct qmi_service *svc;
210
211 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
212 if (!svc)
213 return -ENOMEM;
214
215 svc->service = service;
216 svc->version = version;
217 svc->instance = instance;
218
219 list_add(&svc->list_node, &qmi->lookups);
220
221 qmi_send_new_lookup(qmi, svc);
222
223 return 0;
224 }
225 EXPORT_SYMBOL(qmi_add_lookup);
226
227 static void qmi_send_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
228 {
229 struct qrtr_ctrl_pkt pkt;
230 struct sockaddr_qrtr sq;
231 struct msghdr msg = { };
232 struct kvec iv = { &pkt, sizeof(pkt) };
233 int ret;
234
235 memset(&pkt, 0, sizeof(pkt));
236 pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
237 pkt.server.service = cpu_to_le32(svc->service);
238 pkt.server.instance = cpu_to_le32(svc->version | svc->instance << 8);
239 pkt.server.node = cpu_to_le32(qmi->sq.sq_node);
240 pkt.server.port = cpu_to_le32(qmi->sq.sq_port);
241
242 sq.sq_family = qmi->sq.sq_family;
243 sq.sq_node = qmi->sq.sq_node;
244 sq.sq_port = QRTR_PORT_CTRL;
245
246 msg.msg_name = &sq;
247 msg.msg_namelen = sizeof(sq);
248
249 mutex_lock(&qmi->sock_lock);
250 if (qmi->sock) {
251 ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt));
252 if (ret < 0)
253 pr_err("send service registration failed: %d\n", ret);
254 }
255 mutex_unlock(&qmi->sock_lock);
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270 int qmi_add_server(struct qmi_handle *qmi, unsigned int service,
271 unsigned int version, unsigned int instance)
272 {
273 struct qmi_service *svc;
274
275 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
276 if (!svc)
277 return -ENOMEM;
278
279 svc->service = service;
280 svc->version = version;
281 svc->instance = instance;
282
283 list_add(&svc->list_node, &qmi->services);
284
285 qmi_send_new_server(qmi, svc);
286
287 return 0;
288 }
289 EXPORT_SYMBOL(qmi_add_server);
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn,
308 struct qmi_elem_info *ei, void *c_struct)
309 {
310 int ret;
311
312 memset(txn, 0, sizeof(*txn));
313
314 mutex_init(&txn->lock);
315 init_completion(&txn->completion);
316 txn->qmi = qmi;
317 txn->ei = ei;
318 txn->dest = c_struct;
319
320 mutex_lock(&qmi->txn_lock);
321 ret = idr_alloc_cyclic(&qmi->txns, txn, 0, U16_MAX, GFP_KERNEL);
322 if (ret < 0)
323 pr_err("failed to allocate transaction id\n");
324
325 txn->id = ret;
326 mutex_unlock(&qmi->txn_lock);
327
328 return ret;
329 }
330 EXPORT_SYMBOL(qmi_txn_init);
331
332
333
334
335
336
337
338
339
340
341
342
343 int qmi_txn_wait(struct qmi_txn *txn, unsigned long timeout)
344 {
345 struct qmi_handle *qmi = txn->qmi;
346 int ret;
347
348 ret = wait_for_completion_timeout(&txn->completion, timeout);
349
350 mutex_lock(&qmi->txn_lock);
351 mutex_lock(&txn->lock);
352 idr_remove(&qmi->txns, txn->id);
353 mutex_unlock(&txn->lock);
354 mutex_unlock(&qmi->txn_lock);
355
356 if (ret == 0)
357 return -ETIMEDOUT;
358 else
359 return txn->result;
360 }
361 EXPORT_SYMBOL(qmi_txn_wait);
362
363
364
365
366
367 void qmi_txn_cancel(struct qmi_txn *txn)
368 {
369 struct qmi_handle *qmi = txn->qmi;
370
371 mutex_lock(&qmi->txn_lock);
372 mutex_lock(&txn->lock);
373 idr_remove(&qmi->txns, txn->id);
374 mutex_unlock(&txn->lock);
375 mutex_unlock(&qmi->txn_lock);
376 }
377 EXPORT_SYMBOL(qmi_txn_cancel);
378
379
380
381
382
383
384
385
386
387
388
389 static void qmi_invoke_handler(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
390 struct qmi_txn *txn, const void *buf, size_t len)
391 {
392 const struct qmi_msg_handler *handler;
393 const struct qmi_header *hdr = buf;
394 void *dest;
395 int ret;
396
397 if (!qmi->handlers)
398 return;
399
400 for (handler = qmi->handlers; handler->fn; handler++) {
401 if (handler->type == hdr->type &&
402 handler->msg_id == hdr->msg_id)
403 break;
404 }
405
406 if (!handler->fn)
407 return;
408
409 dest = kzalloc(handler->decoded_size, GFP_KERNEL);
410 if (!dest)
411 return;
412
413 ret = qmi_decode_message(buf, len, handler->ei, dest);
414 if (ret < 0)
415 pr_err("failed to decode incoming message\n");
416 else
417 handler->fn(qmi, sq, txn, dest);
418
419 kfree(dest);
420 }
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 static void qmi_handle_net_reset(struct qmi_handle *qmi)
440 {
441 struct sockaddr_qrtr sq;
442 struct qmi_service *svc;
443 struct socket *sock;
444
445 sock = qmi_sock_create(qmi, &sq);
446 if (IS_ERR(sock))
447 return;
448
449 mutex_lock(&qmi->sock_lock);
450 sock_release(qmi->sock);
451 qmi->sock = NULL;
452 mutex_unlock(&qmi->sock_lock);
453
454 qmi_recv_del_server(qmi, -1, -1);
455
456 if (qmi->ops.net_reset)
457 qmi->ops.net_reset(qmi);
458
459 mutex_lock(&qmi->sock_lock);
460 qmi->sock = sock;
461 qmi->sq = sq;
462 mutex_unlock(&qmi->sock_lock);
463
464 list_for_each_entry(svc, &qmi->lookups, list_node)
465 qmi_send_new_lookup(qmi, svc);
466
467 list_for_each_entry(svc, &qmi->services, list_node)
468 qmi_send_new_server(qmi, svc);
469 }
470
471 static void qmi_handle_message(struct qmi_handle *qmi,
472 struct sockaddr_qrtr *sq,
473 const void *buf, size_t len)
474 {
475 const struct qmi_header *hdr;
476 struct qmi_txn tmp_txn;
477 struct qmi_txn *txn = NULL;
478 int ret;
479
480 if (len < sizeof(*hdr)) {
481 pr_err("ignoring short QMI packet\n");
482 return;
483 }
484
485 hdr = buf;
486
487
488 if (hdr->type == QMI_RESPONSE) {
489 mutex_lock(&qmi->txn_lock);
490 txn = idr_find(&qmi->txns, hdr->txn_id);
491
492
493 if (!txn) {
494 mutex_unlock(&qmi->txn_lock);
495 return;
496 }
497
498 mutex_lock(&txn->lock);
499 mutex_unlock(&qmi->txn_lock);
500
501 if (txn->dest && txn->ei) {
502 ret = qmi_decode_message(buf, len, txn->ei, txn->dest);
503 if (ret < 0)
504 pr_err("failed to decode incoming message\n");
505
506 txn->result = ret;
507 complete(&txn->completion);
508 } else {
509 qmi_invoke_handler(qmi, sq, txn, buf, len);
510 }
511
512 mutex_unlock(&txn->lock);
513 } else {
514
515 memset(&tmp_txn, 0, sizeof(tmp_txn));
516 tmp_txn.id = hdr->txn_id;
517
518 qmi_invoke_handler(qmi, sq, &tmp_txn, buf, len);
519 }
520 }
521
522 static void qmi_data_ready_work(struct work_struct *work)
523 {
524 struct qmi_handle *qmi = container_of(work, struct qmi_handle, work);
525 struct qmi_ops *ops = &qmi->ops;
526 struct sockaddr_qrtr sq;
527 struct msghdr msg = { .msg_name = &sq, .msg_namelen = sizeof(sq) };
528 struct kvec iv;
529 ssize_t msglen;
530
531 for (;;) {
532 iv.iov_base = qmi->recv_buf;
533 iv.iov_len = qmi->recv_buf_size;
534
535 mutex_lock(&qmi->sock_lock);
536 if (qmi->sock)
537 msglen = kernel_recvmsg(qmi->sock, &msg, &iv, 1,
538 iv.iov_len, MSG_DONTWAIT);
539 else
540 msglen = -EPIPE;
541 mutex_unlock(&qmi->sock_lock);
542 if (msglen == -EAGAIN)
543 break;
544
545 if (msglen == -ENETRESET) {
546 qmi_handle_net_reset(qmi);
547
548
549 break;
550 }
551
552 if (msglen < 0) {
553 pr_err("qmi recvmsg failed: %zd\n", msglen);
554 break;
555 }
556
557 if (sq.sq_node == qmi->sq.sq_node &&
558 sq.sq_port == QRTR_PORT_CTRL) {
559 qmi_recv_ctrl_pkt(qmi, qmi->recv_buf, msglen);
560 } else if (ops->msg_handler) {
561 ops->msg_handler(qmi, &sq, qmi->recv_buf, msglen);
562 } else {
563 qmi_handle_message(qmi, &sq, qmi->recv_buf, msglen);
564 }
565 }
566 }
567
568 static void qmi_data_ready(struct sock *sk)
569 {
570 struct qmi_handle *qmi = sk->sk_user_data;
571
572
573
574
575
576 if (!qmi)
577 return;
578
579 queue_work(qmi->wq, &qmi->work);
580 }
581
582 static struct socket *qmi_sock_create(struct qmi_handle *qmi,
583 struct sockaddr_qrtr *sq)
584 {
585 struct socket *sock;
586 int ret;
587
588 ret = sock_create_kern(&init_net, AF_QIPCRTR, SOCK_DGRAM,
589 PF_QIPCRTR, &sock);
590 if (ret < 0)
591 return ERR_PTR(ret);
592
593 ret = kernel_getsockname(sock, (struct sockaddr *)sq);
594 if (ret < 0) {
595 sock_release(sock);
596 return ERR_PTR(ret);
597 }
598
599 sock->sk->sk_user_data = qmi;
600 sock->sk->sk_data_ready = qmi_data_ready;
601 sock->sk->sk_error_report = qmi_data_ready;
602
603 return sock;
604 }
605
606
607
608
609
610
611
612
613
614
615
616
617
618 int qmi_handle_init(struct qmi_handle *qmi, size_t recv_buf_size,
619 const struct qmi_ops *ops,
620 const struct qmi_msg_handler *handlers)
621 {
622 int ret;
623
624 mutex_init(&qmi->txn_lock);
625 mutex_init(&qmi->sock_lock);
626
627 idr_init(&qmi->txns);
628
629 INIT_LIST_HEAD(&qmi->lookups);
630 INIT_LIST_HEAD(&qmi->lookup_results);
631 INIT_LIST_HEAD(&qmi->services);
632
633 INIT_WORK(&qmi->work, qmi_data_ready_work);
634
635 qmi->handlers = handlers;
636 if (ops)
637 qmi->ops = *ops;
638
639
640 recv_buf_size += sizeof(struct qmi_header);
641
642 if (recv_buf_size < sizeof(struct qrtr_ctrl_pkt))
643 recv_buf_size = sizeof(struct qrtr_ctrl_pkt);
644
645 qmi->recv_buf_size = recv_buf_size;
646 qmi->recv_buf = kzalloc(recv_buf_size, GFP_KERNEL);
647 if (!qmi->recv_buf)
648 return -ENOMEM;
649
650 qmi->wq = alloc_workqueue("qmi_msg_handler", WQ_UNBOUND, 1);
651 if (!qmi->wq) {
652 ret = -ENOMEM;
653 goto err_free_recv_buf;
654 }
655
656 qmi->sock = qmi_sock_create(qmi, &qmi->sq);
657 if (IS_ERR(qmi->sock)) {
658 pr_err("failed to create QMI socket\n");
659 ret = PTR_ERR(qmi->sock);
660 goto err_destroy_wq;
661 }
662
663 return 0;
664
665 err_destroy_wq:
666 destroy_workqueue(qmi->wq);
667 err_free_recv_buf:
668 kfree(qmi->recv_buf);
669
670 return ret;
671 }
672 EXPORT_SYMBOL(qmi_handle_init);
673
674
675
676
677
678
679
680 void qmi_handle_release(struct qmi_handle *qmi)
681 {
682 struct socket *sock = qmi->sock;
683 struct qmi_service *svc, *tmp;
684
685 sock->sk->sk_user_data = NULL;
686 cancel_work_sync(&qmi->work);
687
688 qmi_recv_del_server(qmi, -1, -1);
689
690 mutex_lock(&qmi->sock_lock);
691 sock_release(sock);
692 qmi->sock = NULL;
693 mutex_unlock(&qmi->sock_lock);
694
695 destroy_workqueue(qmi->wq);
696
697 idr_destroy(&qmi->txns);
698
699 kfree(qmi->recv_buf);
700
701
702 list_for_each_entry_safe(svc, tmp, &qmi->lookups, list_node) {
703 list_del(&svc->list_node);
704 kfree(svc);
705 }
706
707
708 list_for_each_entry_safe(svc, tmp, &qmi->services, list_node) {
709 list_del(&svc->list_node);
710 kfree(svc);
711 }
712 }
713 EXPORT_SYMBOL(qmi_handle_release);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 static ssize_t qmi_send_message(struct qmi_handle *qmi,
733 struct sockaddr_qrtr *sq, struct qmi_txn *txn,
734 int type, int msg_id, size_t len,
735 struct qmi_elem_info *ei, const void *c_struct)
736 {
737 struct msghdr msghdr = {};
738 struct kvec iv;
739 void *msg;
740 int ret;
741
742 msg = qmi_encode_message(type,
743 msg_id, &len,
744 txn->id, ei,
745 c_struct);
746 if (IS_ERR(msg))
747 return PTR_ERR(msg);
748
749 iv.iov_base = msg;
750 iv.iov_len = len;
751
752 if (sq) {
753 msghdr.msg_name = sq;
754 msghdr.msg_namelen = sizeof(*sq);
755 }
756
757 mutex_lock(&qmi->sock_lock);
758 if (qmi->sock) {
759 ret = kernel_sendmsg(qmi->sock, &msghdr, &iv, 1, len);
760 if (ret < 0)
761 pr_err("failed to send QMI message\n");
762 } else {
763 ret = -EPIPE;
764 }
765 mutex_unlock(&qmi->sock_lock);
766
767 kfree(msg);
768
769 return ret < 0 ? ret : 0;
770 }
771
772
773
774
775
776
777
778
779
780
781
782
783
784 ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
785 struct qmi_txn *txn, int msg_id, size_t len,
786 struct qmi_elem_info *ei, const void *c_struct)
787 {
788 return qmi_send_message(qmi, sq, txn, QMI_REQUEST, msg_id, len, ei,
789 c_struct);
790 }
791 EXPORT_SYMBOL(qmi_send_request);
792
793
794
795
796
797
798
799
800
801
802
803
804
805 ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
806 struct qmi_txn *txn, int msg_id, size_t len,
807 struct qmi_elem_info *ei, const void *c_struct)
808 {
809 return qmi_send_message(qmi, sq, txn, QMI_RESPONSE, msg_id, len, ei,
810 c_struct);
811 }
812 EXPORT_SYMBOL(qmi_send_response);
813
814
815
816
817
818
819
820
821
822
823
824
825 ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
826 int msg_id, size_t len, struct qmi_elem_info *ei,
827 const void *c_struct)
828 {
829 struct qmi_txn txn;
830 ssize_t rval;
831 int ret;
832
833 ret = qmi_txn_init(qmi, &txn, NULL, NULL);
834 if (ret < 0)
835 return ret;
836
837 rval = qmi_send_message(qmi, sq, &txn, QMI_INDICATION, msg_id, len, ei,
838 c_struct);
839
840
841 qmi_txn_cancel(&txn);
842
843 return rval;
844 }
845 EXPORT_SYMBOL(qmi_send_indication);