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 void __qmi_request_complete(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
77 tlv_buf
= qmi_msg_get_tlv_buf(msg
, &tlv_len
);
78 req
->ret
= qmi_check_message_status(tlv_buf
, tlv_len
);
82 req
->ret
= QMI_ERROR_CANCELLED
;
85 if (req
->cb
&& (msg
|| !req
->no_error_cb
))
86 req
->cb(qmi
, req
, msg
);
89 *req
->complete
= true;
90 uloop_cancelled
= true;
94 static void qmi_process_msg(struct qmi_dev
*qmi
, struct qmi_msg
*msg
)
96 struct qmi_request
*req
;
99 if (msg
->qmux
.service
== QMI_SERVICE_CTL
)
100 tid
= msg
->ctl
.transaction
;
102 tid
= le16_to_cpu(msg
->svc
.transaction
);
104 list_for_each_entry(req
, &qmi
->req
, list
) {
105 if (req
->service
!= msg
->qmux
.service
)
111 __qmi_request_complete(qmi
, req
, msg
);
116 static void qmi_notify_read(struct ustream
*us
, int bytes
)
118 struct qmi_dev
*qmi
= container_of(us
, struct qmi_dev
, sf
.stream
);
125 buf
= ustream_get_read_buf(us
, &len
);
129 dump_packet("Received packet", buf
, len
);
131 struct mbim_command_message
*mbim
= (void *) buf
;
133 if (len
< sizeof(*mbim
))
135 msg
= (struct qmi_msg
*) (buf
+ sizeof(*mbim
));
136 msg_len
= le32_to_cpu(mbim
->header
.length
);
137 if (!is_mbim_qmi(mbim
)) {
138 /* must consume other MBIM packets */
139 ustream_consume(us
, msg_len
);
143 if (len
< offsetof(struct qmi_msg
, flags
))
145 msg
= (struct qmi_msg
*) buf
;
146 msg_len
= le16_to_cpu(msg
->qmux
.len
) + 1;
152 qmi_process_msg(qmi
, msg
);
153 ustream_consume(us
, msg_len
);
157 int qmi_request_start(struct qmi_dev
*qmi
, struct qmi_request
*req
, request_cb cb
)
159 struct qmi_msg
*msg
= qmi
->buf
;
160 int len
= qmi_complete_request_message(msg
);
162 void *buf
= (void *) qmi
->buf
;
164 memset(req
, 0, sizeof(*req
));
166 req
->service
= msg
->qmux
.service
;
167 if (req
->service
== QMI_SERVICE_CTL
) {
168 tid
= qmi
->ctl_tid
++;
169 msg
->ctl
.transaction
= tid
;
171 int idx
= qmi_get_service_idx(req
->service
);
176 tid
= qmi
->service_data
[idx
].tid
++;
177 msg
->svc
.transaction
= cpu_to_le16(tid
);
178 msg
->qmux
.client
= qmi
->service_data
[idx
].client_id
;
184 list_add(&req
->list
, &qmi
->req
);
187 buf
-= sizeof(struct mbim_command_message
);
188 mbim_qmi_cmd((struct mbim_command_message
*) buf
, len
, tid
);
189 len
+= sizeof(struct mbim_command_message
);
192 dump_packet("Send packet", buf
, len
);
193 ustream_write(&qmi
->sf
.stream
, buf
, len
, false);
197 void qmi_request_cancel(struct qmi_dev
*qmi
, struct qmi_request
*req
)
200 __qmi_request_complete(qmi
, req
, NULL
);
203 int qmi_request_wait(struct qmi_dev
*qmi
, struct qmi_request
*req
)
205 bool complete
= false;
212 *req
->complete
= true;
214 req
->complete
= &complete
;
216 cancelled
= uloop_cancelled
;
217 uloop_cancelled
= false;
220 if (cancel_all_requests
)
221 qmi_request_cancel(qmi
, req
);
223 uloop_cancelled
= cancelled
;
226 if (req
->complete
== &complete
)
227 req
->complete
= NULL
;
232 struct qmi_connect_request
{
233 struct qmi_request req
;
237 static void qmi_connect_service_cb(struct qmi_dev
*qmi
, struct qmi_request
*req
, struct qmi_msg
*msg
)
239 struct qmi_ctl_allocate_cid_response res
;
240 struct qmi_connect_request
*creq
= container_of(req
, struct qmi_connect_request
, req
);
245 qmi_parse_ctl_allocate_cid_response(msg
, &res
);
246 creq
->cid
= res
.data
.allocation_info
.cid
;
249 int qmi_service_connect(struct qmi_dev
*qmi
, QmiService svc
, int client_id
)
251 struct qmi_ctl_allocate_cid_request creq
= {
252 QMI_INIT(service
, svc
)
254 struct qmi_connect_request req
;
255 int idx
= qmi_get_service_idx(svc
);
256 struct qmi_msg
*msg
= qmi
->buf
;
261 if (qmi
->service_connected
& (1 << idx
))
265 qmi_set_ctl_allocate_cid_request(msg
, &creq
);
266 qmi_request_start(qmi
, &req
.req
, qmi_connect_service_cb
);
267 qmi_request_wait(qmi
, &req
.req
);
274 qmi
->service_keep_cid
|= (1 << idx
);
277 qmi
->service_data
[idx
].connected
= true;
278 qmi
->service_data
[idx
].client_id
= client_id
;
279 qmi
->service_data
[idx
].tid
= 1;
280 qmi
->service_connected
|= (1 << idx
);
285 static void __qmi_service_disconnect(struct qmi_dev
*qmi
, int idx
)
287 int client_id
= qmi
->service_data
[idx
].client_id
;
288 struct qmi_ctl_release_cid_request creq
= {
289 QMI_INIT_SEQUENCE(release_info
,
290 .service
= qmi_services
[idx
],
294 struct qmi_request req
;
295 struct qmi_msg
*msg
= qmi
->buf
;
297 qmi
->service_connected
&= ~(1 << idx
);
298 qmi
->service_data
[idx
].client_id
= -1;
299 qmi
->service_data
[idx
].tid
= 0;
301 qmi_set_ctl_release_cid_request(msg
, &creq
);
302 qmi_request_start(qmi
, &req
, NULL
);
303 qmi_request_wait(qmi
, &req
);
306 int qmi_service_release_client_id(struct qmi_dev
*qmi
, QmiService svc
)
308 int idx
= qmi_get_service_idx(svc
);
309 qmi
->service_release_cid
|= 1 << idx
;
313 static void qmi_close_all_services(struct qmi_dev
*qmi
)
315 uint32_t connected
= qmi
->service_connected
;
318 qmi
->service_keep_cid
&= ~qmi
->service_release_cid
;
319 for (idx
= 0; connected
; idx
++, connected
>>= 1) {
320 if (!(connected
& 1))
323 if (qmi
->service_keep_cid
& (1 << idx
))
326 __qmi_service_disconnect(qmi
, idx
);
330 int qmi_service_get_client_id(struct qmi_dev
*qmi
, QmiService svc
)
332 int idx
= qmi_get_service_idx(svc
);
337 qmi
->service_keep_cid
|= (1 << idx
);
338 return qmi
->service_data
[idx
].client_id
;
341 int qmi_device_open(struct qmi_dev
*qmi
, const char *path
)
344 struct mbim_command_message mbim
;
350 struct ustream
*us
= &qmi
->sf
.stream
;
355 fd
= open(path
, O_RDWR
| O_EXCL
| O_NONBLOCK
| O_NOCTTY
);
359 us
->notify_read
= qmi_notify_read
;
360 ustream_fd_init(&qmi
->sf
, fd
);
361 INIT_LIST_HEAD(&qmi
->req
);
363 qmi
->buf
= msgbuf
.u
.buf
;
368 void qmi_device_close(struct qmi_dev
*qmi
)
370 struct qmi_request
*req
;
372 qmi_close_all_services(qmi
);
373 ustream_free(&qmi
->sf
.stream
);
374 close(qmi
->sf
.fd
.fd
);
376 while (!list_empty(&qmi
->req
)) {
377 req
= list_first_entry(&qmi
->req
, struct qmi_request
, list
);
378 qmi_request_cancel(qmi
, req
);
382 QmiService
qmi_service_get_by_name(const char *str
)
384 static const struct {
388 { "dms", QMI_SERVICE_DMS
},
389 { "nas", QMI_SERVICE_NAS
},
390 { "pds", QMI_SERVICE_PDS
},
391 { "wds", QMI_SERVICE_WDS
},
392 { "wms", QMI_SERVICE_WMS
},
393 { "wda", QMI_SERVICE_WDA
},
394 { "uim", QMI_SERVICE_UIM
},
398 for (i
= 0; i
< ARRAY_SIZE(services
); i
++) {
399 if (!strcasecmp(str
, services
[i
].name
))
400 return services
[i
].svc
;
406 const char *qmi_get_error_str(int code
)
410 for (i
= 0; i
< ARRAY_SIZE(qmi_errors
); i
++) {
411 if (qmi_errors
[i
].code
== code
)
412 return qmi_errors
[i
].text
;
415 return "Unknown error";