add UIM verify pin commands
[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 static struct {
41 struct mbim_command_message mbim;
42 union {
43 char buf[512];
44 struct qmi_msg msg;
45 } u;
46 } __packed msgbuf;
47
48 #ifdef DEBUG_PACKET
49 void dump_packet(const char *prefix, void *ptr, int len)
50 {
51 unsigned char *data = ptr;
52 int i;
53
54 fprintf(stderr, "%s:", prefix);
55 for (i = 0; i < len; i++)
56 fprintf(stderr, " %02x", data[i]);
57 fprintf(stderr, "\n");
58 }
59 #endif
60
61 static int
62 qmi_get_service_idx(QmiService svc)
63 {
64 int i;
65
66 for (i = 0; i < ARRAY_SIZE(qmi_services); i++)
67 if (qmi_services[i] == svc)
68 return i;
69
70 return -1;
71 }
72
73 static void __qmi_request_complete(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
74 {
75 void *tlv_buf;
76 int tlv_len;
77
78 if (!req->pending)
79 return;
80
81 req->pending = false;
82 list_del(&req->list);
83
84 if (msg) {
85 tlv_buf = qmi_msg_get_tlv_buf(msg, &tlv_len);
86 req->ret = qmi_check_message_status(tlv_buf, tlv_len);
87 if (req->ret)
88 msg = NULL;
89 } else {
90 req->ret = QMI_ERROR_CANCELLED;
91 }
92
93 if (req->cb && (msg || !req->no_error_cb))
94 req->cb(qmi, req, msg);
95
96 if (req->complete) {
97 *req->complete = true;
98 uloop_cancelled = true;
99 }
100 }
101
102 static void qmi_process_msg(struct qmi_dev *qmi, struct qmi_msg *msg)
103 {
104 struct qmi_request *req;
105 uint16_t tid;
106
107 if (msg->qmux.service == QMI_SERVICE_CTL)
108 tid = msg->ctl.transaction;
109 else
110 tid = le16_to_cpu(msg->svc.transaction);
111
112 list_for_each_entry(req, &qmi->req, list) {
113 if (req->service != msg->qmux.service)
114 continue;
115
116 if (req->tid != tid)
117 continue;
118
119 __qmi_request_complete(qmi, req, msg);
120 return;
121 }
122 }
123
124 static void qmi_notify_read(struct ustream *us, int bytes)
125 {
126 struct qmi_dev *qmi = container_of(us, struct qmi_dev, sf.stream);
127 struct qmi_msg *msg;
128 char *buf;
129 int len, msg_len;
130
131
132 while (1) {
133 buf = ustream_get_read_buf(us, &len);
134 if (!buf || !len)
135 return;
136
137 dump_packet("Received packet", buf, len);
138 if (qmi->is_mbim) {
139 struct mbim_command_message *mbim = (void *) buf;
140
141 if (len < sizeof(*mbim))
142 return;
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);
148 return;
149 }
150 } else {
151 if (len < offsetof(struct qmi_msg, flags))
152 return;
153 msg = (struct qmi_msg *) buf;
154 msg_len = le16_to_cpu(msg->qmux.len) + 1;
155 }
156
157 if (len < msg_len)
158 return;
159
160 qmi_process_msg(qmi, msg);
161 ustream_consume(us, msg_len);
162 }
163 }
164
165 int qmi_request_start(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg, request_cb cb)
166 {
167 int len = qmi_complete_request_message(msg);
168 uint16_t tid;
169 char *buf = (void *) msg;
170
171 memset(req, 0, sizeof(*req));
172 req->ret = -1;
173 req->service = msg->qmux.service;
174 if (req->service == QMI_SERVICE_CTL) {
175 tid = qmi->ctl_tid++;
176 msg->ctl.transaction = tid;
177 } else {
178 int idx = qmi_get_service_idx(req->service);
179
180 if (idx < 0)
181 return -1;
182
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;
186 }
187
188 req->tid = tid;
189 req->cb = cb;
190 req->pending = true;
191 list_add(&req->list, &qmi->req);
192
193 if (qmi->is_mbim) {
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);
197 }
198
199 dump_packet("Send packet", buf, len);
200 ustream_write(&qmi->sf.stream, buf, len, false);
201 return 0;
202 }
203
204 void qmi_request_cancel(struct qmi_dev *qmi, struct qmi_request *req)
205 {
206 req->cb = NULL;
207 __qmi_request_complete(qmi, req, NULL);
208 }
209
210 int qmi_request_wait(struct qmi_dev *qmi, struct qmi_request *req)
211 {
212 bool complete = false;
213 bool cancelled;
214
215 if (!req->pending)
216 return req->ret;
217
218 if (req->complete)
219 *req->complete = true;
220
221 req->complete = &complete;
222 while (!complete) {
223 cancelled = uloop_cancelled;
224 uloop_cancelled = false;
225 uloop_run();
226
227 if (cancel_all_requests)
228 qmi_request_cancel(qmi, req);
229
230 uloop_cancelled = cancelled;
231 }
232
233 if (req->complete == &complete)
234 req->complete = NULL;
235
236 return req->ret;
237 }
238
239 struct qmi_connect_request {
240 struct qmi_request req;
241 int cid;
242 };
243
244 static void qmi_connect_service_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
245 {
246 struct qmi_ctl_allocate_cid_response res;
247 struct qmi_connect_request *creq = container_of(req, struct qmi_connect_request, req);
248
249 if (!msg)
250 return;
251
252 qmi_parse_ctl_allocate_cid_response(msg, &res);
253 creq->cid = res.data.allocation_info.cid;
254 }
255
256 int qmi_service_connect(struct qmi_dev *qmi, QmiService svc, int client_id)
257 {
258 struct qmi_ctl_allocate_cid_request creq = {
259 QMI_INIT(service, svc)
260 };
261 struct qmi_connect_request req;
262 int idx = qmi_get_service_idx(svc);
263 struct qmi_msg *msg = &msgbuf.u.msg;
264
265 if (idx < 0)
266 return -1;
267
268 if (qmi->service_connected & (1 << idx))
269 return 0;
270
271 if (client_id < 0) {
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);
275
276 if (req.req.ret)
277 return req.req.ret;
278
279 client_id = req.cid;
280 } else {
281 qmi->service_keep_cid |= (1 << idx);
282 }
283
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);
288
289 return 0;
290 }
291
292 static void __qmi_service_disconnect(struct qmi_dev *qmi, int idx)
293 {
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],
298 .cid = client_id,
299 )
300 };
301 struct qmi_request req;
302 struct qmi_msg *msg = &msgbuf.u.msg;
303
304 qmi->service_connected &= ~(1 << idx);
305 qmi->service_data[idx].client_id = -1;
306 qmi->service_data[idx].tid = 0;
307
308 qmi_set_ctl_release_cid_request(msg, &creq);
309 qmi_request_start(qmi, &req, msg, NULL);
310 qmi_request_wait(qmi, &req);
311 }
312
313 int qmi_service_release_client_id(struct qmi_dev *qmi, QmiService svc)
314 {
315 int idx = qmi_get_service_idx(svc);
316 qmi->service_release_cid |= 1 << idx;
317 return 0;
318 }
319
320 static void qmi_close_all_services(struct qmi_dev *qmi)
321 {
322 uint32_t connected = qmi->service_connected;
323 int idx;
324
325 qmi->service_keep_cid &= ~qmi->service_release_cid;
326 for (idx = 0; connected; idx++, connected >>= 1) {
327 if (!(connected & 1))
328 continue;
329
330 if (qmi->service_keep_cid & (1 << idx))
331 continue;
332
333 __qmi_service_disconnect(qmi, idx);
334 }
335 }
336
337 int qmi_service_get_client_id(struct qmi_dev *qmi, QmiService svc)
338 {
339 int idx = qmi_get_service_idx(svc);
340
341 if (idx < 0)
342 return -1;
343
344 qmi->service_keep_cid |= (1 << idx);
345 return qmi->service_data[idx].client_id;
346 }
347
348 int qmi_device_open(struct qmi_dev *qmi, const char *path)
349 {
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
364 return 0;
365 }
366
367 void qmi_device_close(struct qmi_dev *qmi)
368 {
369 struct qmi_request *req;
370
371 qmi_close_all_services(qmi);
372 ustream_free(&qmi->sf.stream);
373 close(qmi->sf.fd.fd);
374
375 while (!list_empty(&qmi->req)) {
376 req = list_first_entry(&qmi->req, struct qmi_request, list);
377 qmi_request_cancel(qmi, req);
378 }
379 }
380
381 QmiService qmi_service_get_by_name(const char *str)
382 {
383 static const struct {
384 const char *name;
385 QmiService svc;
386 } services[] = {
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 },
393 { "uim", QMI_SERVICE_UIM },
394 };
395 int i;
396
397 for (i = 0; i < ARRAY_SIZE(services); i++) {
398 if (!strcasecmp(str, services[i].name))
399 return services[i].svc;
400 }
401
402 return -1;
403 }
404
405 const char *qmi_get_error_str(int code)
406 {
407 int i;
408
409 for (i = 0; i < ARRAY_SIZE(qmi_errors); i++) {
410 if (qmi_errors[i].code == code)
411 return qmi_errors[i].text;
412 }
413
414 return "Unknown error";
415 }