X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=dev.c;h=4bca4299602a107efaa4ab9a6b51514369c89cb0;hb=d682e966615fc652da6e4e26b57aa3adfebeb2fe;hp=21e30eae56bedbb7a5f5af40e2c91a5a56e42def;hpb=f38d620f4c172feb435d332ad7aba353fe7326d9;p=project%2Fuqmi.git diff --git a/dev.c b/dev.c index 21e30ea..4bca429 100644 --- a/dev.c +++ b/dev.c @@ -1,3 +1,24 @@ +/* + * uqmi -- tiny QMI support implementation + * + * Copyright (C) 2014-2015 Felix Fietkau + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + */ + #include #include #include @@ -6,6 +27,9 @@ #include "uqmi.h" #include "qmi-errors.h" #include "qmi-errors.c" +#include "mbim.h" + +bool cancel_all_requests = false; #define __qmi_service(_n) [__##_n] = _n static const uint8_t qmi_services[__QMI_SERVICE_LAST] = { @@ -13,11 +37,6 @@ static const uint8_t qmi_services[__QMI_SERVICE_LAST] = { }; #undef __qmi_service -static union { - char buf[512]; - struct qmi_msg msg; -} msgbuf; - #ifdef DEBUG_PACKET void dump_packet(const char *prefix, void *ptr, int len) { @@ -47,7 +66,6 @@ static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, { void *tlv_buf; int tlv_len; - int ret; if (!req->pending) return; @@ -55,10 +73,14 @@ static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, req->pending = false; list_del(&req->list); - tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len); - req->ret = qmi_check_message_status(tlv_buf, tlv_len); - if (req->ret) - msg = NULL; + if (msg) { + tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len); + req->ret = qmi_check_message_status(tlv_buf, tlv_len); + if (req->ret) + msg = NULL; + } else { + req->ret = QMI_ERROR_CANCELLED; + } if (req->cb && (msg || !req->no_error_cb)) req->cb(qmi, req, msg); @@ -97,33 +119,47 @@ static void qmi_notify_read(struct ustream *us, int bytes) struct qmi_msg *msg; char *buf; int len, msg_len; - int i; + while (1) { buf = ustream_get_read_buf(us, &len); if (!buf || !len) return; - if (len < offsetof(struct qmi_msg, flags)) - return; + dump_packet("Received packet", buf, len); + if (qmi->is_mbim) { + struct mbim_command_message *mbim = (void *) buf; + + if (len < sizeof(*mbim)) + return; + msg = (struct qmi_msg *) (buf + sizeof(*mbim)); + msg_len = le32_to_cpu(mbim->header.length); + if (!is_mbim_qmi(mbim)) { + /* must consume other MBIM packets */ + ustream_consume(us, msg_len); + return; + } + } else { + if (len < offsetof(struct qmi_msg, flags)) + return; + msg = (struct qmi_msg *) buf; + msg_len = le16_to_cpu(msg->qmux.len) + 1; + } - msg = (struct qmi_msg *) buf; - msg_len = le16_to_cpu(msg->qmux.len) + 1; if (len < msg_len) return; - dump_packet("Received packet", msg, msg_len); qmi_process_msg(qmi, msg); ustream_consume(us, msg_len); } } -int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, request_cb cb) +int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, request_cb cb) { + struct qmi_msg *msg = qmi->buf; int len = qmi_complete_request_message(msg); uint16_t tid; - int ret; - int i; + void *buf = (void *) qmi->buf; memset(req, 0, sizeof(*req)); req->ret = -1; @@ -147,8 +183,14 @@ int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_m req->pending = true; list_add(&req->list, &qmi->req); - dump_packet("Send packet", msg, len); - ustream_write(&qmi->sf.stream, (void *) msg, len, false); + if (qmi->is_mbim) { + buf -= sizeof(struct mbim_command_message); + mbim_qmi_cmd((struct mbim_command_message *) buf, len, tid); + len += sizeof(struct mbim_command_message); + } + + dump_packet("Send packet", buf, len); + ustream_write(&qmi->sf.stream, buf, len, false); return 0; } @@ -174,6 +216,10 @@ int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req) cancelled = uloop_cancelled; uloop_cancelled = false; uloop_run(); + + if (cancel_all_requests) + qmi_request_cancel(qmi, req); + uloop_cancelled = cancelled; } @@ -207,7 +253,7 @@ int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id) }; struct qmi_connect_request req; int idx = qmi_get_service_idx(svc); - struct qmi_msg *msg = &msgbuf.msg; + struct qmi_msg *msg = qmi->buf; if (idx < 0) return -1; @@ -217,13 +263,15 @@ int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id) if (client_id < 0) { qmi_set_ctl_allocate_cid_request(msg, &creq); - qmi_request_start(qmi, &req.req, msg, qmi_connect_service_cb); + qmi_request_start(qmi, &req.req, qmi_connect_service_cb); qmi_request_wait(qmi, &req.req); if (req.req.ret) return req.req.ret; client_id = req.cid; + } else { + qmi->service_keep_cid |= (1 << idx); } qmi->service_data[idx].connected = true; @@ -244,22 +292,30 @@ static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx) ) }; struct qmi_request req; - struct qmi_msg *msg = &msgbuf.msg; + struct qmi_msg *msg = qmi->buf; qmi->service_connected &= ~(1 << idx); qmi->service_data[idx].client_id = -1; qmi->service_data[idx].tid = 0; qmi_set_ctl_release_cid_request(msg, &creq); - qmi_request_start(qmi, &req, msg, NULL); + qmi_request_start(qmi, &req, NULL); qmi_request_wait(qmi, &req); } +int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc) +{ + int idx = qmi_get_service_idx(svc); + qmi->service_release_cid |= 1 << idx; + return 0; +} + static void qmi_close_all_services(struct qmi_dev *qmi) { uint32_t connected = qmi->service_connected; int idx; + qmi->service_keep_cid &= ~qmi->service_release_cid; for (idx = 0; connected; idx++, connected >>= 1) { if (!(connected & 1)) continue; @@ -284,6 +340,13 @@ int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc) int qmi_device_open(struct qmi_dev *qmi, const char *path) { + static struct { + struct mbim_command_message mbim; + union { + char buf[2048]; + struct qmi_msg msg; + } u; + } __packed msgbuf; struct ustream *us = &qmi->sf.stream; int fd; @@ -297,6 +360,7 @@ int qmi_device_open(struct qmi_dev *qmi, const char *path) ustream_fd_init(&qmi->sf, fd); INIT_LIST_HEAD(&qmi->req); qmi->ctl_tid = 1; + qmi->buf = msgbuf.u.buf; return 0; } @@ -304,7 +368,6 @@ int qmi_device_open(struct qmi_dev *qmi, const char *path) void qmi_device_close(struct qmi_dev *qmi) { struct qmi_request *req; - int idx; qmi_close_all_services(qmi); ustream_free(&qmi->sf.stream); @@ -327,6 +390,8 @@ QmiService qmi_service_get_by_name(const char *str) { "pds", QMI_SERVICE_PDS }, { "wds", QMI_SERVICE_WDS }, { "wms", QMI_SERVICE_WMS }, + { "wda", QMI_SERVICE_WDA }, + { "uim", QMI_SERVICE_UIM }, }; int i;