2 * uqmi -- tiny QMI support implementation
4 * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
28 #include "qmi-errors.h"
29 #include "qmi-errors.c"
32 bool cancel_all_requests
= false;
34 #define __qmi_service(_n) [__##_n] = _n
35 static const uint8_t qmi_services
[__QMI_SERVICE_LAST
] = {
41 void dump_packet(const char *prefix
, void *ptr
, int len
)
43 unsigned char *data
= ptr
;
46 fprintf(stderr
, "%s:", prefix
);
47 for (i
= 0; i
< len
; i
++)
48 fprintf(stderr
, " %02x", data
[i
]);
49 fprintf(stderr
, "\n");
54 qmi_get_service_idx(QmiService svc
)
58 for (i
= 0; i
< ARRAY_SIZE(qmi_services
); i
++)
59 if (qmi_services
[i
] == svc
)
65 static bool qmi_message_is_response(struct qmi_msg
*msg
)
67 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
68 if (msg
->flags
& QMI_CTL_FLAG_RESPONSE
)
72 if (msg
->flags
& QMI_SERVICE_FLAG_RESPONSE
)
79 static bool qmi_message_is_indication(struct qmi_msg
*msg
)
81 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
82 if (msg
->flags
& QMI_CTL_FLAG_INDICATION
)
86 if (msg
->flags
& QMI_SERVICE_FLAG_INDICATION
)
93 static void __qmi_request_complete(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
101 req
->pending
= false;
102 list_del(&req
->list
);
105 tlv_buf
= qmi_msg_get_tlv_buf(msg
, &tlv_len
);
106 req
->ret
= qmi_check_message_status(tlv_buf
, tlv_len
);
110 req
->ret
= QMI_ERROR_CANCELLED
;
113 if (req
->cb
&& (msg
|| !req
->no_error_cb
))
114 req
->cb(qmi
, req
, msg
);
117 *req
->complete
= true;
118 uloop_cancelled
= true;
122 static void qmi_process_msg(struct qmi_dev
*qmi
, struct qmi_msg
*msg
)
124 struct qmi_request
*req
;
127 if (qmi_message_is_indication(msg
)) {
128 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
129 struct qmi_msg sync_msg
= {0};
130 qmi_set_ctl_sync_request(&sync_msg
);
131 /* A SYNC indication might be sent on boot in order to indicate
132 * that all Client IDs have been deallocated by the modem:
133 * cancel all requests, as they will not be answered. */
134 if (msg
->ctl
.message
== sync_msg
.ctl
.message
) {
135 while (!list_empty(&qmi
->req
)) {
136 req
= list_first_entry(&qmi
->req
, struct qmi_request
, list
);
137 qmi_request_cancel(qmi
, req
);
145 if (!qmi_message_is_response(msg
))
148 if (msg
->qmux
.service
== QMI_SERVICE_CTL
)
149 tid
= msg
->ctl
.transaction
;
151 tid
= le16_to_cpu(msg
->svc
.transaction
);
153 list_for_each_entry(req
, &qmi
->req
, list
) {
154 if (req
->service
!= msg
->qmux
.service
)
160 __qmi_request_complete(qmi
, req
, msg
);
165 static void qmi_notify_read(struct ustream
*us
, int bytes
)
167 struct qmi_dev
*qmi
= container_of(us
, struct qmi_dev
, sf
.stream
);
174 buf
= ustream_get_read_buf(us
, &len
);
178 dump_packet("Received packet", buf
, len
);
180 struct mbim_command_message
*mbim
= (void *) buf
;
182 if (len
< sizeof(*mbim
))
184 msg
= (struct qmi_msg
*) (buf
+ sizeof(*mbim
));
185 msg_len
= le32_to_cpu(mbim
->header
.length
);
186 if (!is_mbim_qmi(mbim
)) {
187 /* must consume other MBIM packets */
188 ustream_consume(us
, msg_len
);
192 if (len
< offsetof(struct qmi_msg
, flags
))
194 msg
= (struct qmi_msg
*) buf
;
195 msg_len
= le16_to_cpu(msg
->qmux
.len
) + 1;
201 qmi_process_msg(qmi
, msg
);
202 ustream_consume(us
, msg_len
);
206 int qmi_request_start(struct qmi_dev
*qmi
, struct qmi_request
*req
, request_cb cb
)
208 struct qmi_msg
*msg
= qmi
->buf
;
209 int len
= qmi_complete_request_message(msg
);
211 void *buf
= (void *) qmi
->buf
;
213 memset(req
, 0, sizeof(*req
));
215 req
->service
= msg
->qmux
.service
;
216 if (req
->service
== QMI_SERVICE_CTL
) {
217 tid
= qmi
->ctl_tid
++;
218 msg
->ctl
.transaction
= tid
;
220 int idx
= qmi_get_service_idx(req
->service
);
225 tid
= qmi
->service_data
[idx
].tid
++;
226 msg
->svc
.transaction
= cpu_to_le16(tid
);
227 msg
->qmux
.client
= qmi
->service_data
[idx
].client_id
;
233 list_add(&req
->list
, &qmi
->req
);
236 buf
-= sizeof(struct mbim_command_message
);
237 mbim_qmi_cmd((struct mbim_command_message
*) buf
, len
, tid
);
238 len
+= sizeof(struct mbim_command_message
);
241 dump_packet("Send packet", buf
, len
);
242 ustream_write(&qmi
->sf
.stream
, buf
, len
, false);
246 void qmi_request_cancel(struct qmi_dev
*qmi
, struct qmi_request
*req
)
249 __qmi_request_complete(qmi
, req
, NULL
);
252 int qmi_request_wait(struct qmi_dev
*qmi
, struct qmi_request
*req
)
254 bool complete
= false;
261 *req
->complete
= true;
263 req
->complete
= &complete
;
265 cancelled
= uloop_cancelled
;
266 uloop_cancelled
= false;
269 if (cancel_all_requests
)
270 qmi_request_cancel(qmi
, req
);
272 uloop_cancelled
= cancelled
;
275 if (req
->complete
== &complete
)
276 req
->complete
= NULL
;
281 struct qmi_connect_request
{
282 struct qmi_request req
;
286 static void qmi_connect_service_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
288 struct qmi_ctl_allocate_cid_response res
;
289 struct qmi_connect_request
*creq
= container_of(req
, struct qmi_connect_request
, req
);
294 qmi_parse_ctl_allocate_cid_response(msg
, &res
);
295 creq
->cid
= res
.data
.allocation_info
.cid
;
298 int qmi_service_connect(struct qmi_dev
*qmi
, QmiService svc
, int client_id
)
300 struct qmi_ctl_allocate_cid_request creq
= {
301 QMI_INIT(service
, svc
)
303 struct qmi_connect_request req
;
304 int idx
= qmi_get_service_idx(svc
);
305 struct qmi_msg
*msg
= qmi
->buf
;
310 if (qmi
->service_connected
& (1 << idx
))
314 qmi_set_ctl_allocate_cid_request(msg
, &creq
);
315 qmi_request_start(qmi
, &req
.req
, qmi_connect_service_cb
);
316 qmi_request_wait(qmi
, &req
.req
);
323 qmi
->service_keep_cid
|= (1 << idx
);
326 qmi
->service_data
[idx
].connected
= true;
327 qmi
->service_data
[idx
].client_id
= client_id
;
328 qmi
->service_data
[idx
].tid
= 1;
329 qmi
->service_connected
|= (1 << idx
);
334 static void __qmi_service_disconnect(struct qmi_dev
*qmi
, int idx
)
336 int client_id
= qmi
->service_data
[idx
].client_id
;
337 struct qmi_ctl_release_cid_request creq
= {
338 QMI_INIT_SEQUENCE(release_info
,
339 .service
= qmi_services
[idx
],
343 struct qmi_request req
;
344 struct qmi_msg
*msg
= qmi
->buf
;
346 qmi
->service_connected
&= ~(1 << idx
);
347 qmi
->service_data
[idx
].client_id
= -1;
348 qmi
->service_data
[idx
].tid
= 0;
350 qmi_set_ctl_release_cid_request(msg
, &creq
);
351 qmi_request_start(qmi
, &req
, NULL
);
352 qmi_request_wait(qmi
, &req
);
355 int qmi_service_release_client_id(struct qmi_dev
*qmi
, QmiService svc
)
357 int idx
= qmi_get_service_idx(svc
);
358 qmi
->service_release_cid
|= 1 << idx
;
362 static void qmi_close_all_services(struct qmi_dev
*qmi
)
364 uint32_t connected
= qmi
->service_connected
;
367 qmi
->service_keep_cid
&= ~qmi
->service_release_cid
;
368 for (idx
= 0; connected
; idx
++, connected
>>= 1) {
369 if (!(connected
& 1))
372 if (qmi
->service_keep_cid
& (1 << idx
))
375 __qmi_service_disconnect(qmi
, idx
);
379 int qmi_service_get_client_id(struct qmi_dev
*qmi
, QmiService svc
)
381 int idx
= qmi_get_service_idx(svc
);
386 qmi
->service_keep_cid
|= (1 << idx
);
387 return qmi
->service_data
[idx
].client_id
;
390 int qmi_device_open(struct qmi_dev
*qmi
, const char *path
)
393 struct mbim_command_message mbim
;
399 struct ustream
*us
= &qmi
->sf
.stream
;
404 fd
= open(path
, O_RDWR
| O_EXCL
| O_NONBLOCK
| O_NOCTTY
);
408 us
->notify_read
= qmi_notify_read
;
409 ustream_fd_init(&qmi
->sf
, fd
);
410 INIT_LIST_HEAD(&qmi
->req
);
412 qmi
->buf
= msgbuf
.u
.buf
;
417 void qmi_device_close(struct qmi_dev
*qmi
)
419 struct qmi_request
*req
;
421 qmi_close_all_services(qmi
);
422 ustream_free(&qmi
->sf
.stream
);
423 close(qmi
->sf
.fd
.fd
);
425 while (!list_empty(&qmi
->req
)) {
426 req
= list_first_entry(&qmi
->req
, struct qmi_request
, list
);
427 qmi_request_cancel(qmi
, req
);
431 QmiService
qmi_service_get_by_name(const char *str
)
433 static const struct {
437 { "dms", QMI_SERVICE_DMS
},
438 { "nas", QMI_SERVICE_NAS
},
439 { "pds", QMI_SERVICE_PDS
},
440 { "wds", QMI_SERVICE_WDS
},
441 { "wms", QMI_SERVICE_WMS
},
442 { "wda", QMI_SERVICE_WDA
},
443 { "uim", QMI_SERVICE_UIM
},
447 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
448 if (!strcasecmp(str
, services
[i
].name
))
449 return services
[i
].svc
;
455 const char *qmi_get_error_str(int code
)
459 for (i
= 0; i
< ARRAY_SIZE(qmi_errors
); i
++) {
460 if (qmi_errors
[i
].code
== code
)
461 return qmi_errors
[i
].text
;
464 return "Unknown error";