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.
29 #include "qmi-errors.h"
30 #include "qmi-errors.c"
33 bool cancel_all_requests
= false;
35 #define __qmi_service(_n) [__##_n] = _n
36 static const uint8_t qmi_services
[__QMI_SERVICE_LAST
] = {
42 void dump_packet(const char *prefix
, void *ptr
, int len
)
44 unsigned char *data
= ptr
;
47 fprintf(stderr
, "%s:", prefix
);
48 for (i
= 0; i
< len
; i
++)
49 fprintf(stderr
, " %02x", data
[i
]);
50 fprintf(stderr
, "\n");
55 qmi_get_service_idx(QmiService svc
)
59 for (i
= 0; i
< ARRAY_SIZE(qmi_services
); i
++)
60 if (qmi_services
[i
] == svc
)
66 static bool qmi_message_is_response(struct qmi_msg
*msg
)
68 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
69 if (msg
->flags
& QMI_CTL_FLAG_RESPONSE
)
73 if (msg
->flags
& QMI_SERVICE_FLAG_RESPONSE
)
80 static bool qmi_message_is_indication(struct qmi_msg
*msg
)
82 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
83 if (msg
->flags
& QMI_CTL_FLAG_INDICATION
)
87 if (msg
->flags
& QMI_SERVICE_FLAG_INDICATION
)
94 static void __qmi_request_complete(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
102 req
->pending
= false;
103 list_del(&req
->list
);
106 tlv_buf
= qmi_msg_get_tlv_buf(msg
, &tlv_len
);
107 req
->ret
= qmi_check_message_status(tlv_buf
, tlv_len
);
111 req
->ret
= QMI_ERROR_CANCELLED
;
114 if (req
->cb
&& (msg
|| !req
->no_error_cb
))
115 req
->cb(qmi
, req
, msg
);
118 *req
->complete
= true;
119 uloop_cancelled
= true;
123 static void qmi_process_msg(struct qmi_dev
*qmi
, struct qmi_msg
*msg
)
125 struct qmi_request
*req
;
128 if (qmi_message_is_indication(msg
)) {
129 if (msg
->qmux
.service
== QMI_SERVICE_CTL
) {
130 struct qmi_msg sync_msg
= {0};
131 qmi_set_ctl_sync_request(&sync_msg
);
132 /* A SYNC indication might be sent on boot in order to indicate
133 * that all Client IDs have been deallocated by the modem:
134 * cancel all requests, as they will not be answered. */
135 if (msg
->ctl
.message
== sync_msg
.ctl
.message
) {
136 while (!list_empty(&qmi
->req
)) {
137 req
= list_first_entry(&qmi
->req
, struct qmi_request
, list
);
138 qmi_request_cancel(qmi
, req
);
146 if (!qmi_message_is_response(msg
))
149 if (msg
->qmux
.service
== QMI_SERVICE_CTL
)
150 tid
= msg
->ctl
.transaction
;
152 tid
= le16_to_cpu(msg
->svc
.transaction
);
154 list_for_each_entry(req
, &qmi
->req
, list
) {
155 if (req
->service
!= msg
->qmux
.service
)
161 __qmi_request_complete(qmi
, req
, msg
);
166 static void qmi_notify_read(struct ustream
*us
, int bytes
)
168 struct qmi_dev
*qmi
= container_of(us
, struct qmi_dev
, sf
.stream
);
175 buf
= ustream_get_read_buf(us
, &len
);
179 dump_packet("Received packet", buf
, len
);
181 struct mbim_command_message
*mbim
= (void *) buf
;
183 if (len
< sizeof(*mbim
))
185 msg
= (struct qmi_msg
*) (buf
+ sizeof(*mbim
));
186 msg_len
= le32_to_cpu(mbim
->header
.length
);
187 if (!is_mbim_qmi(mbim
)) {
188 /* must consume other MBIM packets */
189 ustream_consume(us
, msg_len
);
193 if (len
< offsetof(struct qmi_msg
, flags
))
195 msg
= (struct qmi_msg
*) buf
;
196 msg_len
= le16_to_cpu(msg
->qmux
.len
) + 1;
202 qmi_process_msg(qmi
, msg
);
203 ustream_consume(us
, msg_len
);
207 int qmi_request_start(struct qmi_dev
*qmi
, struct qmi_request
*req
, request_cb cb
)
209 struct qmi_msg
*msg
= qmi
->buf
;
210 int len
= qmi_complete_request_message(msg
);
212 void *buf
= (void *) qmi
->buf
;
214 memset(req
, 0, sizeof(*req
));
216 req
->service
= msg
->qmux
.service
;
217 if (req
->service
== QMI_SERVICE_CTL
) {
218 tid
= qmi
->ctl_tid
++;
219 msg
->ctl
.transaction
= tid
;
221 int idx
= qmi_get_service_idx(req
->service
);
226 tid
= qmi
->service_data
[idx
].tid
++;
227 msg
->svc
.transaction
= cpu_to_le16(tid
);
228 msg
->qmux
.client
= qmi
->service_data
[idx
].client_id
;
234 list_add(&req
->list
, &qmi
->req
);
237 buf
-= sizeof(struct mbim_command_message
);
238 mbim_qmi_cmd((struct mbim_command_message
*) buf
, len
, tid
);
239 len
+= sizeof(struct mbim_command_message
);
242 dump_packet("Send packet", buf
, len
);
243 ustream_write(&qmi
->sf
.stream
, buf
, len
, false);
247 void qmi_request_cancel(struct qmi_dev
*qmi
, struct qmi_request
*req
)
250 __qmi_request_complete(qmi
, req
, NULL
);
253 /*! Run uloop until the request has been completed
255 * \param qmi the qmi device
256 * \param req the request to wait for
257 * \return req->ret (0 on success)
259 int qmi_request_wait(struct qmi_dev
*qmi
, struct qmi_request
*req
)
261 bool complete
= false;
268 *req
->complete
= true;
270 req
->complete
= &complete
;
272 cancelled
= uloop_cancelled
;
273 uloop_cancelled
= false;
276 if (cancel_all_requests
)
277 qmi_request_cancel(qmi
, req
);
279 uloop_cancelled
= cancelled
;
282 if (req
->complete
== &complete
)
283 req
->complete
= NULL
;
288 struct qmi_connect_request
{
289 struct qmi_request req
;
293 static void qmi_connect_service_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
295 struct qmi_ctl_allocate_cid_response res
;
296 struct qmi_connect_request
*creq
= container_of(req
, struct qmi_connect_request
, req
);
301 qmi_parse_ctl_allocate_cid_response(msg
, &res
);
302 creq
->cid
= res
.data
.allocation_info
.cid
;
305 int qmi_service_connect(struct qmi_dev
*qmi
, QmiService svc
, int client_id
)
307 struct qmi_ctl_allocate_cid_request creq
= {
308 QMI_INIT(service
, svc
)
310 struct qmi_connect_request req
;
311 int idx
= qmi_get_service_idx(svc
);
312 struct qmi_msg
*msg
= qmi
->buf
;
317 if (qmi
->service_connected
& (1 << idx
))
321 qmi_set_ctl_allocate_cid_request(msg
, &creq
);
322 qmi_request_start(qmi
, &req
.req
, qmi_connect_service_cb
);
323 qmi_request_wait(qmi
, &req
.req
);
330 qmi
->service_keep_cid
|= (1 << idx
);
333 qmi
->service_data
[idx
].connected
= true;
334 qmi
->service_data
[idx
].client_id
= client_id
;
335 qmi
->service_data
[idx
].tid
= 1;
336 qmi
->service_connected
|= (1 << idx
);
341 static void __qmi_service_disconnect(struct qmi_dev
*qmi
, int idx
)
343 int client_id
= qmi
->service_data
[idx
].client_id
;
344 struct qmi_ctl_release_cid_request creq
= {
345 QMI_INIT_SEQUENCE(release_info
,
346 .service
= qmi_services
[idx
],
350 struct qmi_request req
;
351 struct qmi_msg
*msg
= qmi
->buf
;
353 qmi
->service_connected
&= ~(1 << idx
);
354 qmi
->service_data
[idx
].client_id
= -1;
355 qmi
->service_data
[idx
].tid
= 0;
357 qmi_set_ctl_release_cid_request(msg
, &creq
);
358 qmi_request_start(qmi
, &req
, NULL
);
359 qmi_request_wait(qmi
, &req
);
362 int qmi_service_release_client_id(struct qmi_dev
*qmi
, QmiService svc
)
364 int idx
= qmi_get_service_idx(svc
);
365 qmi
->service_release_cid
|= 1 << idx
;
369 static void qmi_close_all_services(struct qmi_dev
*qmi
)
371 uint32_t connected
= qmi
->service_connected
;
374 qmi
->service_keep_cid
&= ~qmi
->service_release_cid
;
375 for (idx
= 0; connected
; idx
++, connected
>>= 1) {
376 if (!(connected
& 1))
379 if (qmi
->service_keep_cid
& (1 << idx
))
382 __qmi_service_disconnect(qmi
, idx
);
386 int qmi_service_get_client_id(struct qmi_dev
*qmi
, QmiService svc
)
388 int idx
= qmi_get_service_idx(svc
);
393 qmi
->service_keep_cid
|= (1 << idx
);
394 return qmi
->service_data
[idx
].client_id
;
397 int qmi_device_open(struct qmi_dev
*qmi
, const char *path
)
400 struct mbim_command_message mbim
;
406 struct ustream
*us
= &qmi
->sf
.stream
;
411 fd
= open(path
, O_RDWR
| O_EXCL
| O_NONBLOCK
| O_NOCTTY
);
415 us
->notify_read
= qmi_notify_read
;
416 ustream_fd_init(&qmi
->sf
, fd
);
417 INIT_LIST_HEAD(&qmi
->req
);
419 qmi
->buf
= msgbuf
.u
.buf
;
424 void qmi_device_close(struct qmi_dev
*qmi
)
426 struct qmi_request
*req
;
428 qmi_close_all_services(qmi
);
429 ustream_free(&qmi
->sf
.stream
);
430 close(qmi
->sf
.fd
.fd
);
432 while (!list_empty(&qmi
->req
)) {
433 req
= list_first_entry(&qmi
->req
, struct qmi_request
, list
);
434 qmi_request_cancel(qmi
, req
);
438 QmiService
qmi_service_get_by_name(const char *str
)
440 static const struct {
444 { "dms", QMI_SERVICE_DMS
},
445 { "nas", QMI_SERVICE_NAS
},
446 { "pds", QMI_SERVICE_PDS
},
447 { "wds", QMI_SERVICE_WDS
},
448 { "wms", QMI_SERVICE_WMS
},
449 { "wda", QMI_SERVICE_WDA
},
450 { "uim", QMI_SERVICE_UIM
},
454 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
455 if (!strcasecmp(str
, services
[i
].name
))
456 return services
[i
].svc
;