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 struct mbim_command_message mbim;
49 void dump_packet(const char *prefix, void *ptr, int len)
51 unsigned char *data = ptr;
54 fprintf(stderr, "%s:", prefix);
55 for (i = 0; i < len; i++)
56 fprintf(stderr, " %02x", data[i]);
57 fprintf(stderr, "\n");
62 qmi_get_service_idx(QmiService svc)
66 for (i = 0; i < ARRAY_SIZE(qmi_services); i++)
67 if (qmi_services[i] == svc)
73 static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
85 tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len);
86 req->ret = qmi_check_message_status(tlv_buf, tlv_len);
90 req->ret = QMI_ERROR_CANCELLED;
93 if (req->cb && (msg || !req->no_error_cb))
94 req->cb(qmi, req, msg);
97 *req->complete = true;
98 uloop_cancelled = true;
102 static void qmi_process_msg(struct qmi_dev *qmi, struct qmi_msg *msg)
104 struct qmi_request *req;
107 if (msg->qmux.service == QMI_SERVICE_CTL)
108 tid = msg->ctl.transaction;
110 tid = le16_to_cpu(msg->svc.transaction);
112 list_for_each_entry(req, &qmi->req, list) {
113 if (req->service != msg->qmux.service)
119 __qmi_request_complete(qmi, req, msg);
124 static void qmi_notify_read(struct ustream *us, int bytes)
126 struct qmi_dev *qmi = container_of(us, struct qmi_dev, sf.stream);
133 buf = ustream_get_read_buf(us, &len);
137 dump_packet("Received packet", buf, len);
139 struct mbim_command_message *mbim = (void *) buf;
141 if (len < sizeof(*mbim))
143 msg = (struct qmi_msg *) (buf + sizeof(*mbim));
144 msg_len = le32_to_cpu(mbim->header.length);
145 if (!is_mbim_qmi(mbim)) {
146 /* must consume other MBIM packets */
147 ustream_consume(us, msg_len);
151 if (len < offsetof(struct qmi_msg, flags))
153 msg = (struct qmi_msg *) buf;
154 msg_len = le16_to_cpu(msg->qmux.len) + 1;
160 qmi_process_msg(qmi, msg);
161 ustream_consume(us, msg_len);
165 int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, request_cb cb)
167 int len = qmi_complete_request_message(msg);
169 char *buf = (void *) msg;
171 memset(req, 0, sizeof(*req));
173 req->service = msg->qmux.service;
174 if (req->service == QMI_SERVICE_CTL) {
175 tid = qmi->ctl_tid++;
176 msg->ctl.transaction = tid;
178 int idx = qmi_get_service_idx(req->service);
183 tid = qmi->service_data[idx].tid++;
184 msg->svc.transaction = cpu_to_le16(tid);
185 msg->qmux.client = qmi->service_data[idx].client_id;
191 list_add(&req->list, &qmi->req);
194 buf -= sizeof(struct mbim_command_message);
195 mbim_qmi_cmd((struct mbim_command_message *) buf, len, tid);
196 len += sizeof(struct mbim_command_message);
199 dump_packet("Send packet", buf, len);
200 ustream_write(&qmi->sf.stream, buf, len, false);
204 void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
207 __qmi_request_complete(qmi, req, NULL);
210 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
212 bool complete = false;
219 *req->complete = true;
221 req->complete = &complete;
223 cancelled = uloop_cancelled;
224 uloop_cancelled = false;
227 if (cancel_all_requests)
228 qmi_request_cancel(qmi, req);
230 uloop_cancelled = cancelled;
233 if (req->complete == &complete)
234 req->complete = NULL;
239 struct qmi_connect_request {
240 struct qmi_request req;
244 static void qmi_connect_service_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
246 struct qmi_ctl_allocate_cid_response res;
247 struct qmi_connect_request *creq = container_of(req, struct qmi_connect_request, req);
252 qmi_parse_ctl_allocate_cid_response(msg, &res);
253 creq->cid = res.data.allocation_info.cid;
256 int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
258 struct qmi_ctl_allocate_cid_request creq = {
259 QMI_INIT(service, svc)
261 struct qmi_connect_request req;
262 int idx = qmi_get_service_idx(svc);
263 struct qmi_msg *msg = &msgbuf.u.msg;
268 if (qmi->service_connected & (1 << idx))
272 qmi_set_ctl_allocate_cid_request(msg, &creq);
273 qmi_request_start(qmi, &req.req, msg, qmi_connect_service_cb);
274 qmi_request_wait(qmi, &req.req);
281 qmi->service_keep_cid |= (1 << idx);
284 qmi->service_data[idx].connected = true;
285 qmi->service_data[idx].client_id = client_id;
286 qmi->service_data[idx].tid = 1;
287 qmi->service_connected |= (1 << idx);
292 static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
294 int client_id = qmi->service_data[idx].client_id;
295 struct qmi_ctl_release_cid_request creq = {
296 QMI_INIT_SEQUENCE(release_info,
297 .service = qmi_services[idx],
301 struct qmi_request req;
302 struct qmi_msg *msg = &msgbuf.u.msg;
304 qmi->service_connected &= ~(1 << idx);
305 qmi->service_data[idx].client_id = -1;
306 qmi->service_data[idx].tid = 0;
308 qmi_set_ctl_release_cid_request(msg, &creq);
309 qmi_request_start(qmi, &req, msg, NULL);
310 qmi_request_wait(qmi, &req);
313 int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc)
315 int idx = qmi_get_service_idx(svc);
316 qmi->service_release_cid |= 1 << idx;
320 static void qmi_close_all_services(struct qmi_dev *qmi)
322 uint32_t connected = qmi->service_connected;
325 qmi->service_keep_cid &= ~qmi->service_release_cid;
326 for (idx = 0; connected; idx++, connected >>= 1) {
327 if (!(connected & 1))
330 if (qmi->service_keep_cid & (1 << idx))
333 __qmi_service_disconnect(qmi, idx);
337 int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
339 int idx = qmi_get_service_idx(svc);
344 qmi->service_keep_cid |= (1 << idx);
345 return qmi->service_data[idx].client_id;
348 int qmi_device_open(struct qmi_dev *qmi, const char *path)
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);
367 void qmi_device_close(struct qmi_dev *qmi)
369 struct qmi_request *req;
371 qmi_close_all_services(qmi);
372 ustream_free(&qmi->sf.stream);
373 close(qmi->sf.fd.fd);
375 while (!list_empty(&qmi->req)) {
376 req = list_first_entry(&qmi->req, struct qmi_request, list);
377 qmi_request_cancel(qmi, req);
381 QmiService qmi_service_get_by_name(const char *str)
383 static const struct {
387 { "dms", QMI_SERVICE_DMS },
388 { "nas", QMI_SERVICE_NAS },
389 { "pds", QMI_SERVICE_PDS },
390 { "wds", QMI_SERVICE_WDS },
391 { "wms", QMI_SERVICE_WMS },
392 { "wda", QMI_SERVICE_WDA },
396 for (i = 0; i < ARRAY_SIZE(services); i++) {
397 if (!strcasecmp(str, services[i].name))
398 return services[i].svc;
404 const char *qmi_get_error_str(int code)
408 for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
409 if (qmi_errors[i].code == code)
410 return qmi_errors[i].text;
413 return "Unknown error";