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->flags != QMI_CTL_FLAG_RESPONSE && msg->flags != QMI_SERVICE_FLAG_RESPONSE)
102 if (msg->qmux.service == QMI_SERVICE_CTL)
103 tid = msg->ctl.transaction;
105 tid = le16_to_cpu(msg->svc.transaction);
107 list_for_each_entry(req, &qmi->req, list) {
108 if (req->service != msg->qmux.service)
114 __qmi_request_complete(qmi, req, msg);
119 static void qmi_notify_read(struct ustream *us, int bytes)
121 struct qmi_dev *qmi = container_of(us, struct qmi_dev, sf.stream);
128 buf = ustream_get_read_buf(us, &len);
132 dump_packet("Received packet", buf, len);
134 struct mbim_command_message *mbim = (void *) buf;
136 if (len < sizeof(*mbim))
138 msg = (struct qmi_msg *) (buf + sizeof(*mbim));
139 msg_len = le32_to_cpu(mbim->header.length);
140 if (!is_mbim_qmi(mbim)) {
141 /* must consume other MBIM packets */
142 ustream_consume(us, msg_len);
146 if (len < offsetof(struct qmi_msg, flags))
148 msg = (struct qmi_msg *) buf;
149 msg_len = le16_to_cpu(msg->qmux.len) + 1;
155 qmi_process_msg(qmi, msg);
156 ustream_consume(us, msg_len);
160 int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, request_cb cb)
162 struct qmi_msg *msg = qmi->buf;
163 int len = qmi_complete_request_message(msg);
165 void *buf = (void *) qmi->buf;
167 memset(req, 0, sizeof(*req));
169 req->service = msg->qmux.service;
170 if (req->service == QMI_SERVICE_CTL) {
171 tid = qmi->ctl_tid++;
172 msg->ctl.transaction = tid;
174 int idx = qmi_get_service_idx(req->service);
179 tid = qmi->service_data[idx].tid++;
180 msg->svc.transaction = cpu_to_le16(tid);
181 msg->qmux.client = qmi->service_data[idx].client_id;
187 list_add(&req->list, &qmi->req);
190 buf -= sizeof(struct mbim_command_message);
191 mbim_qmi_cmd((struct mbim_command_message *) buf, len, tid);
192 len += sizeof(struct mbim_command_message);
195 dump_packet("Send packet", buf, len);
196 ustream_write(&qmi->sf.stream, buf, len, false);
200 void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
203 __qmi_request_complete(qmi, req, NULL);
206 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
208 bool complete = false;
215 *req->complete = true;
217 req->complete = &complete;
219 cancelled = uloop_cancelled;
220 uloop_cancelled = false;
223 if (cancel_all_requests)
224 qmi_request_cancel(qmi, req);
226 uloop_cancelled = cancelled;
229 if (req->complete == &complete)
230 req->complete = NULL;
235 struct qmi_connect_request {
236 struct qmi_request req;
240 static void qmi_connect_service_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
242 struct qmi_ctl_allocate_cid_response res;
243 struct qmi_connect_request *creq = container_of(req, struct qmi_connect_request, req);
248 qmi_parse_ctl_allocate_cid_response(msg, &res);
249 creq->cid = res.data.allocation_info.cid;
252 int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
254 struct qmi_ctl_allocate_cid_request creq = {
255 QMI_INIT(service, svc)
257 struct qmi_connect_request req;
258 int idx = qmi_get_service_idx(svc);
259 struct qmi_msg *msg = qmi->buf;
264 if (qmi->service_connected & (1 << idx))
268 qmi_set_ctl_allocate_cid_request(msg, &creq);
269 qmi_request_start(qmi, &req.req, qmi_connect_service_cb);
270 qmi_request_wait(qmi, &req.req);
277 qmi->service_keep_cid |= (1 << idx);
280 qmi->service_data[idx].connected = true;
281 qmi->service_data[idx].client_id = client_id;
282 qmi->service_data[idx].tid = 1;
283 qmi->service_connected |= (1 << idx);
288 static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
290 int client_id = qmi->service_data[idx].client_id;
291 struct qmi_ctl_release_cid_request creq = {
292 QMI_INIT_SEQUENCE(release_info,
293 .service = qmi_services[idx],
297 struct qmi_request req;
298 struct qmi_msg *msg = qmi->buf;
300 qmi->service_connected &= ~(1 << idx);
301 qmi->service_data[idx].client_id = -1;
302 qmi->service_data[idx].tid = 0;
304 qmi_set_ctl_release_cid_request(msg, &creq);
305 qmi_request_start(qmi, &req, NULL);
306 qmi_request_wait(qmi, &req);
309 int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc)
311 int idx = qmi_get_service_idx(svc);
312 qmi->service_release_cid |= 1 << idx;
316 static void qmi_close_all_services(struct qmi_dev *qmi)
318 uint32_t connected = qmi->service_connected;
321 qmi->service_keep_cid &= ~qmi->service_release_cid;
322 for (idx = 0; connected; idx++, connected >>= 1) {
323 if (!(connected & 1))
326 if (qmi->service_keep_cid & (1 << idx))
329 __qmi_service_disconnect(qmi, idx);
333 int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
335 int idx = qmi_get_service_idx(svc);
340 qmi->service_keep_cid |= (1 << idx);
341 return qmi->service_data[idx].client_id;
344 int qmi_device_open(struct qmi_dev *qmi, const char *path)
347 struct mbim_command_message mbim;
353 struct ustream *us = &qmi->sf.stream;
358 fd = open(path, O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
362 us->notify_read = qmi_notify_read;
363 ustream_fd_init(&qmi->sf, fd);
364 INIT_LIST_HEAD(&qmi->req);
366 qmi->buf = msgbuf.u.buf;
371 void qmi_device_close(struct qmi_dev *qmi)
373 struct qmi_request *req;
375 qmi_close_all_services(qmi);
376 ustream_free(&qmi->sf.stream);
377 close(qmi->sf.fd.fd);
379 while (!list_empty(&qmi->req)) {
380 req = list_first_entry(&qmi->req, struct qmi_request, list);
381 qmi_request_cancel(qmi, req);
385 QmiService qmi_service_get_by_name(const char *str)
387 static const struct {
391 { "dms", QMI_SERVICE_DMS },
392 { "nas", QMI_SERVICE_NAS },
393 { "pds", QMI_SERVICE_PDS },
394 { "wds", QMI_SERVICE_WDS },
395 { "wms", QMI_SERVICE_WMS },
396 { "wda", QMI_SERVICE_WDA },
397 { "uim", QMI_SERVICE_UIM },
401 for (i = 0; i < ARRAY_SIZE(services); i++) {
402 if (!strcasecmp(str, services[i].name))
403 return services[i].svc;
409 const char *qmi_get_error_str(int code)
413 for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
414 if (qmi_errors[i].code == code)
415 return qmi_errors[i].text;
418 return "Unknown error";