uqmi: Change returned value to QMI_CMD_REQUEST for 'sync' command.
[project/uqmi.git] / dev.c
1 /*
2  * uqmi -- tiny QMI support implementation
3  *
4  * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include "uqmi.h"
28 #include "qmi-errors.h"
29 #include "qmi-errors.c"
30 #include "mbim.h"
31
32 bool cancel_all_requests = false;
33
34 #define __qmi_service(_n) [__##_n] = _n
35 static const uint8_t qmi_services[__QMI_SERVICE_LAST] = {
36         __qmi_services
37 };
38 #undef __qmi_service
39
40 #ifdef DEBUG_PACKET
41 void dump_packet(const char *prefix, void *ptr, int len)
42 {
43         unsigned char *data = ptr;
44         int i;
45
46         fprintf(stderr, "%s:", prefix);
47         for (i = 0; i < len; i++)
48                 fprintf(stderr, " %02x", data[i]);
49         fprintf(stderr, "\n");
50 }
51 #endif
52
53 static int
54 qmi_get_service_idx(QmiService svc)
55 {
56         int i;
57
58         for (i = 0; i < ARRAY_SIZE(qmi_services); i++)
59                 if (qmi_services[i] == svc)
60                         return i;
61
62         return -1;
63 }
64
65 static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
66 {
67         void *tlv_buf;
68         int tlv_len;
69
70         if (!req->pending)
71                 return;
72
73         req->pending = false;
74         list_del(&req->list);
75
76         if (msg) {
77                 tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len);
78                 req->ret = qmi_check_message_status(tlv_buf, tlv_len);
79                 if (req->ret)
80                         msg = NULL;
81         } else {
82                 req->ret = QMI_ERROR_CANCELLED;
83         }
84
85         if (req->cb && (msg || !req->no_error_cb))
86                 req->cb(qmi, req, msg);
87
88         if (req->complete) {
89                 *req->complete = true;
90                 uloop_cancelled = true;
91         }
92 }
93
94 static void qmi_process_msg(struct qmi_dev *qmi, struct qmi_msg *msg)
95 {
96         struct qmi_request *req;
97         uint16_t tid;
98
99         if (msg->qmux.service == QMI_SERVICE_CTL)
100                 tid = msg->ctl.transaction;
101         else
102                 tid = le16_to_cpu(msg->svc.transaction);
103
104         list_for_each_entry(req, &qmi->req, list) {
105                 if (req->service != msg->qmux.service)
106                         continue;
107
108                 if (req->tid != tid)
109                         continue;
110
111                 __qmi_request_complete(qmi, req, msg);
112                 return;
113         }
114 }
115
116 static void qmi_notify_read(struct ustream *us, int bytes)
117 {
118         struct qmi_dev *qmi = container_of(us, struct qmi_dev, sf.stream);
119         struct qmi_msg *msg;
120         char *buf;
121         int len, msg_len;
122
123
124         while (1) {
125                 buf = ustream_get_read_buf(us, &len);
126                 if (!buf || !len)
127                         return;
128
129                 dump_packet("Received packet", buf, len);
130                 if (qmi->is_mbim) {
131                         struct mbim_command_message *mbim = (void *) buf;
132
133                         if (len < sizeof(*mbim))
134                                 return;
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);
140                                 return;
141                         }
142                 } else {
143                         if (len < offsetof(struct qmi_msg, flags))
144                                 return;
145                         msg = (struct qmi_msg *) buf;
146                         msg_len = le16_to_cpu(msg->qmux.len) + 1;
147                 }
148
149                 if (len < msg_len)
150                         return;
151
152                 qmi_process_msg(qmi, msg);
153                 ustream_consume(us, msg_len);
154         }
155 }
156
157 int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, request_cb cb)
158 {
159         struct qmi_msg *msg = qmi->buf;
160         int len = qmi_complete_request_message(msg);
161         uint16_t tid;
162         void *buf = (void *) qmi->buf;
163
164         memset(req, 0, sizeof(*req));
165         req->ret = -1;
166         req->service = msg->qmux.service;
167         if (req->service == QMI_SERVICE_CTL) {
168                 tid = qmi->ctl_tid++;
169                 msg->ctl.transaction = tid;
170         } else {
171                 int idx = qmi_get_service_idx(req->service);
172
173                 if (idx < 0)
174                         return -1;
175
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;
179         }
180
181         req->tid = tid;
182         req->cb = cb;
183         req->pending = true;
184         list_add(&req->list, &qmi->req);
185
186         if (qmi->is_mbim) {
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);
190         }
191
192         dump_packet("Send packet", buf, len);
193         ustream_write(&qmi->sf.stream, buf, len, false);
194         return 0;
195 }
196
197 void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
198 {
199         req->cb = NULL;
200         __qmi_request_complete(qmi, req, NULL);
201 }
202
203 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
204 {
205         bool complete = false;
206         bool cancelled;
207
208         if (!req->pending)
209                 return req->ret;
210
211         if (req->complete)
212                 *req->complete = true;
213
214         req->complete = &complete;
215         while (!complete) {
216                 cancelled = uloop_cancelled;
217                 uloop_cancelled = false;
218                 uloop_run();
219
220                 if (cancel_all_requests)
221                         qmi_request_cancel(qmi, req);
222
223                 uloop_cancelled = cancelled;
224         }
225
226         if (req->complete == &complete)
227                 req->complete = NULL;
228
229         return req->ret;
230 }
231
232 struct qmi_connect_request {
233         struct qmi_request req;
234         int cid;
235 };
236
237 static void qmi_connect_service_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
238 {
239         struct qmi_ctl_allocate_cid_response res;
240         struct qmi_connect_request *creq = container_of(req, struct qmi_connect_request, req);
241
242         if (!msg)
243                 return;
244
245         qmi_parse_ctl_allocate_cid_response(msg, &res);
246         creq->cid = res.data.allocation_info.cid;
247 }
248
249 int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
250 {
251         struct qmi_ctl_allocate_cid_request creq = {
252                 QMI_INIT(service, svc)
253         };
254         struct qmi_connect_request req;
255         int idx = qmi_get_service_idx(svc);
256         struct qmi_msg *msg = qmi->buf;
257
258         if (idx < 0)
259                 return -1;
260
261         if (qmi->service_connected & (1 << idx))
262                 return 0;
263
264         if (client_id < 0) {
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);
268
269                 if (req.req.ret)
270                         return req.req.ret;
271
272                 client_id = req.cid;
273         } else {
274                 qmi->service_keep_cid |= (1 << idx);
275         }
276
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);
281
282         return 0;
283 }
284
285 static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
286 {
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],
291                         .cid = client_id,
292                 )
293         };
294         struct qmi_request req;
295         struct qmi_msg *msg = qmi->buf;
296
297         qmi->service_connected &= ~(1 << idx);
298         qmi->service_data[idx].client_id = -1;
299         qmi->service_data[idx].tid = 0;
300
301         qmi_set_ctl_release_cid_request(msg, &creq);
302         qmi_request_start(qmi, &req, NULL);
303         qmi_request_wait(qmi, &req);
304 }
305
306 int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc)
307 {
308         int idx = qmi_get_service_idx(svc);
309         qmi->service_release_cid |= 1 << idx;
310         return 0;
311 }
312
313 static void qmi_close_all_services(struct qmi_dev *qmi)
314 {
315         uint32_t connected = qmi->service_connected;
316         int idx;
317
318         qmi->service_keep_cid &= ~qmi->service_release_cid;
319         for (idx = 0; connected; idx++, connected >>= 1) {
320                 if (!(connected & 1))
321                         continue;
322
323                 if (qmi->service_keep_cid & (1 << idx))
324                         continue;
325
326                 __qmi_service_disconnect(qmi, idx);
327         }
328 }
329
330 int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
331 {
332         int idx = qmi_get_service_idx(svc);
333
334         if (idx < 0)
335                 return -1;
336
337         qmi->service_keep_cid |= (1 << idx);
338         return qmi->service_data[idx].client_id;
339 }
340
341 int qmi_device_open(struct qmi_dev *qmi, const char *path)
342 {
343         static struct {
344                 struct mbim_command_message mbim;
345                 union {
346                         char buf[2048];
347                         struct qmi_msg msg;
348                 } u;
349         } __packed msgbuf;
350         struct ustream *us = &qmi->sf.stream;
351         int fd;
352
353         uloop_init();
354
355         fd = open(path, O_RDWR | O_EXCL | O_NONBLOCK | O_NOCTTY);
356         if (fd < 0)
357                 return -1;
358
359         us->notify_read = qmi_notify_read;
360         ustream_fd_init(&qmi->sf, fd);
361         INIT_LIST_HEAD(&qmi->req);
362         qmi->ctl_tid = 1;
363         qmi->buf = msgbuf.u.buf;
364
365         return 0;
366 }
367
368 void qmi_device_close(struct qmi_dev *qmi)
369 {
370         struct qmi_request *req;
371
372         qmi_close_all_services(qmi);
373         ustream_free(&qmi->sf.stream);
374         close(qmi->sf.fd.fd);
375
376         while (!list_empty(&qmi->req)) {
377                 req = list_first_entry(&qmi->req, struct qmi_request, list);
378                 qmi_request_cancel(qmi, req);
379         }
380 }
381
382 QmiService qmi_service_get_by_name(const char *str)
383 {
384         static const struct {
385                 const char *name;
386                 QmiService svc;
387         } services[] = {
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 },
395         };
396         int i;
397
398         for (i = 0; i < ARRAY_SIZE(services); i++) {
399                 if (!strcasecmp(str, services[i].name))
400                         return services[i].svc;
401         }
402
403         return -1;
404 }
405
406 const char *qmi_get_error_str(int code)
407 {
408         int i;
409
410         for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
411                 if (qmi_errors[i].code == code)
412                         return qmi_errors[i].text;
413         }
414
415         return "Unknown error";
416 }