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"
31 bool cancel_all_requests = false;
33 #define __qmi_service(_n) [__##_n] = _n
34 static const uint8_t qmi_services[__QMI_SERVICE_LAST] = {
45 void dump_packet(const char *prefix, void *ptr, int len)
47 unsigned char *data = ptr;
50 fprintf(stderr, "%s:", prefix);
51 for (i = 0; i < len; i++)
52 fprintf(stderr, " %02x", data[i]);
53 fprintf(stderr, "\n");
58 qmi_get_service_idx(QmiService svc)
62 for (i = 0; i < ARRAY_SIZE(qmi_services); i++)
63 if (qmi_services[i] == svc)
69 static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
81 tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len);
82 req->ret = qmi_check_message_status(tlv_buf, tlv_len);
86 req->ret = QMI_ERROR_CANCELLED;
89 if (req->cb && (msg || !req->no_error_cb))
90 req->cb(qmi, req, msg);
93 *req->complete = true;
94 uloop_cancelled = true;
98 static void qmi_process_msg(struct qmi_dev *qmi, struct qmi_msg *msg)
100 struct qmi_request *req;
103 if (msg->qmux.service == QMI_SERVICE_CTL)
104 tid = msg->ctl.transaction;
106 tid = le16_to_cpu(msg->svc.transaction);
108 list_for_each_entry(req, &qmi->req, list) {
109 if (req->service != msg->qmux.service)
115 __qmi_request_complete(qmi, req, msg);
120 static void qmi_notify_read(struct ustream *us, int bytes)
122 struct qmi_dev *qmi = container_of(us, struct qmi_dev, sf.stream);
128 buf = ustream_get_read_buf(us, &len);
132 if (len < offsetof(struct qmi_msg, flags))
135 msg = (struct qmi_msg *) buf;
136 msg_len = le16_to_cpu(msg->qmux.len) + 1;
140 dump_packet("Received packet", msg, msg_len);
141 qmi_process_msg(qmi, msg);
142 ustream_consume(us, msg_len);
146 int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, request_cb cb)
148 int len = qmi_complete_request_message(msg);
151 memset(req, 0, sizeof(*req));
153 req->service = msg->qmux.service;
154 if (req->service == QMI_SERVICE_CTL) {
155 tid = qmi->ctl_tid++;
156 msg->ctl.transaction = tid;
158 int idx = qmi_get_service_idx(req->service);
163 tid = qmi->service_data[idx].tid++;
164 msg->svc.transaction = cpu_to_le16(tid);
165 msg->qmux.client = qmi->service_data[idx].client_id;
171 list_add(&req->list, &qmi->req);
173 dump_packet("Send packet", msg, len);
174 ustream_write(&qmi->sf.stream, (void *) msg, len, false);
178 void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
181 __qmi_request_complete(qmi, req, NULL);
184 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
186 bool complete = false;
193 *req->complete = true;
195 req->complete = &complete;
197 cancelled = uloop_cancelled;
198 uloop_cancelled = false;
201 if (cancel_all_requests)
202 qmi_request_cancel(qmi, req);
204 uloop_cancelled = cancelled;
207 if (req->complete == &complete)
208 req->complete = NULL;
213 struct qmi_connect_request {
214 struct qmi_request req;
218 static void qmi_connect_service_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
220 struct qmi_ctl_allocate_cid_response res;
221 struct qmi_connect_request *creq = container_of(req, struct qmi_connect_request, req);
226 qmi_parse_ctl_allocate_cid_response(msg, &res);
227 creq->cid = res.data.allocation_info.cid;
230 int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
232 struct qmi_ctl_allocate_cid_request creq = {
233 QMI_INIT(service, svc)
235 struct qmi_connect_request req;
236 int idx = qmi_get_service_idx(svc);
237 struct qmi_msg *msg = &msgbuf.msg;
242 if (qmi->service_connected & (1 << idx))
246 qmi_set_ctl_allocate_cid_request(msg, &creq);
247 qmi_request_start(qmi, &req.req, msg, qmi_connect_service_cb);
248 qmi_request_wait(qmi, &req.req);
255 qmi->service_keep_cid |= (1 << idx);
258 qmi->service_data[idx].connected = true;
259 qmi->service_data[idx].client_id = client_id;
260 qmi->service_data[idx].tid = 1;
261 qmi->service_connected |= (1 << idx);
266 static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
268 int client_id = qmi->service_data[idx].client_id;
269 struct qmi_ctl_release_cid_request creq = {
270 QMI_INIT_SEQUENCE(release_info,
271 .service = qmi_services[idx],
275 struct qmi_request req;
276 struct qmi_msg *msg = &msgbuf.msg;
278 qmi->service_connected &= ~(1 << idx);
279 qmi->service_data[idx].client_id = -1;
280 qmi->service_data[idx].tid = 0;
282 qmi_set_ctl_release_cid_request(msg, &creq);
283 qmi_request_start(qmi, &req, msg, NULL);
284 qmi_request_wait(qmi, &req);
287 int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc)
289 int idx = qmi_get_service_idx(svc);
290 qmi->service_release_cid |= 1 << idx;
294 static void qmi_close_all_services(struct qmi_dev *qmi)
296 uint32_t connected = qmi->service_connected;
299 qmi->service_keep_cid &= ~qmi->service_release_cid;
300 for (idx = 0; connected; idx++, connected >>= 1) {
301 if (!(connected & 1))
304 if (qmi->service_keep_cid & (1 << idx))
307 __qmi_service_disconnect(qmi, idx);
311 int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
313 int idx = qmi_get_service_idx(svc);
318 qmi->service_keep_cid |= (1 << idx);
319 return qmi->service_data[idx].client_id;
322 int qmi_device_open(struct qmi_dev *qmi, const char *path)
324 struct ustream *us = &qmi->sf.stream;
329 fd = open(path, O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
333 us->notify_read = qmi_notify_read;
334 ustream_fd_init(&qmi->sf, fd);
335 INIT_LIST_HEAD(&qmi->req);
341 void qmi_device_close(struct qmi_dev *qmi)
343 struct qmi_request *req;
345 qmi_close_all_services(qmi);
346 ustream_free(&qmi->sf.stream);
347 close(qmi->sf.fd.fd);
349 while (!list_empty(&qmi->req)) {
350 req = list_first_entry(&qmi->req, struct qmi_request, list);
351 qmi_request_cancel(qmi, req);
355 QmiService qmi_service_get_by_name(const char *str)
357 static const struct {
361 { "dms", QMI_SERVICE_DMS },
362 { "nas", QMI_SERVICE_NAS },
363 { "pds", QMI_SERVICE_PDS },
364 { "wds", QMI_SERVICE_WDS },
365 { "wms", QMI_SERVICE_WMS },
366 { "wda", QMI_SERVICE_WDA },
370 for (i = 0; i < ARRAY_SIZE(services); i++) {
371 if (!strcasecmp(str, services[i].name))
372 return services[i].svc;
378 const char *qmi_get_error_str(int code)
382 for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
383 if (qmi_errors[i].code == code)
384 return qmi_errors[i].text;
387 return "Unknown error";