3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2012 Intel Corporation. All rights reserved.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License version 2 as
9
* published by the Free Software Foundation.
11
* This program 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
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
#include <ofono/log.h>
29
#include <ofono/modem.h>
30
#include <ofono/sim.h>
38
#define EF_STATUS_INVALIDATED 0
39
#define EF_STATUS_VALID 1
42
struct qmi_service *uim;
47
int retries[OFONO_SIM_PASSWORD_INVALID];
50
static int create_fileid_data(uint8_t app_type, int fileid,
51
const unsigned char *path,
52
unsigned int path_len,
53
unsigned char *fileid_data)
55
unsigned char db_path[6];
59
memcpy(db_path, path, path_len);
63
case 0x01: /* SIM card */
64
len = sim_ef_db_get_path_2g(fileid, db_path);
66
case 0x02: /* USIM application */
67
len = sim_ef_db_get_path_3g(fileid, db_path);
75
/* Minimum length of path is 2 bytes */
79
fileid_data[0] = fileid & 0xff;
80
fileid_data[1] = (fileid & 0xff00) >> 8;
82
fileid_data[3] = db_path[1];
83
fileid_data[4] = db_path[0];
84
fileid_data[5] = db_path[3];
85
fileid_data[6] = db_path[2];
86
fileid_data[7] = db_path[5];
87
fileid_data[8] = db_path[4];
92
static void get_file_attributes_cb(struct qmi_result *result, void *user_data)
94
struct cb_data *cbd = user_data;
95
ofono_sim_file_info_cb_t cb = cbd->cb;
96
struct sim_data *data = ofono_sim_get_data(cbd->user);
97
const struct qmi_uim_file_attributes *attr;
98
uint16_t len, raw_len;
100
unsigned char access[3];
101
unsigned char file_status;
106
if (qmi_result_set_error(result, NULL))
109
attr = qmi_result_get(result, 0x11, &len);
113
raw_len = GUINT16_FROM_LE(attr->raw_len);
115
switch (data->app_type) {
116
case 0x01: /* SIM card */
117
ok = sim_parse_2g_get_response(attr->raw_value, raw_len,
118
&flen, &rlen, &str, access, &file_status);
120
case 0x02: /* USIM application */
121
ok = sim_parse_3g_get_response(attr->raw_value, raw_len,
122
&flen, &rlen, &str, access, NULL);
123
file_status = EF_STATUS_VALID;
131
CALLBACK_WITH_SUCCESS(cb, flen, str, rlen, access,
132
file_status, cbd->data);
137
CALLBACK_WITH_FAILURE(cb, -1, -1, -1, NULL,
138
EF_STATUS_INVALIDATED, cbd->data);
141
static void qmi_read_attributes(struct ofono_sim *sim, int fileid,
142
const unsigned char *path,
143
unsigned int path_len,
144
ofono_sim_file_info_cb_t cb, void *user_data)
146
struct sim_data *data = ofono_sim_get_data(sim);
147
struct cb_data *cbd = cb_data_new(cb, user_data);
148
unsigned char aid_data[2] = { 0x06, 0x00 };
149
unsigned char fileid_data[9];
151
struct qmi_param *param;
153
DBG("file id 0x%04x path len %d", fileid, path_len);
157
fileid_len = create_fileid_data(data->app_type, fileid,
158
path, path_len, fileid_data);
162
param = qmi_param_new();
166
qmi_param_append(param, 0x01, sizeof(aid_data), aid_data);
167
qmi_param_append(param, 0x02, fileid_len, fileid_data);
169
if (qmi_service_send(data->uim, QMI_UIM_GET_FILE_ATTRIBUTES, param,
170
get_file_attributes_cb, cbd, g_free) > 0)
173
qmi_param_free(param);
176
CALLBACK_WITH_FAILURE(cb, -1, -1, -1, NULL,
177
EF_STATUS_INVALIDATED, cbd->data);
182
static void read_generic_cb(struct qmi_result *result, void *user_data)
184
struct cb_data *cbd = user_data;
185
ofono_sim_read_cb_t cb = cbd->cb;
186
const unsigned char *content;
191
if (qmi_result_set_error(result, NULL)) {
192
CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
196
content = qmi_result_get(result, 0x11, &len);
198
CALLBACK_WITH_FAILURE(cb, NULL, 0, cbd->data);
202
CALLBACK_WITH_SUCCESS(cb, content + 2, len - 2, cbd->data);
205
static void qmi_read_transparent(struct ofono_sim *sim,
206
int fileid, int start, int length,
207
const unsigned char *path,
208
unsigned int path_len,
209
ofono_sim_read_cb_t cb, void *user_data)
211
struct sim_data *data = ofono_sim_get_data(sim);
212
struct cb_data *cbd = cb_data_new(cb, user_data);
213
unsigned char aid_data[2] = { 0x06, 0x00 };
214
unsigned char read_data[4];
215
unsigned char fileid_data[9];
217
struct qmi_param *param;
219
DBG("file id 0x%04x path len %d", fileid, path_len);
221
fileid_len = create_fileid_data(data->app_type, fileid,
222
path, path_len, fileid_data);
226
read_data[0] = start & 0xff;
227
read_data[1] = (start & 0xff00) >> 8;
228
read_data[2] = length & 0xff;
229
read_data[3] = (length & 0xff00) >> 8;
231
param = qmi_param_new();
235
qmi_param_append(param, 0x01, sizeof(aid_data), aid_data);
236
qmi_param_append(param, 0x02, fileid_len, fileid_data);
237
qmi_param_append(param, 0x03, sizeof(read_data), read_data);
239
if (qmi_service_send(data->uim, QMI_UIM_READ_TRANSPARENT, param,
240
read_generic_cb, cbd, g_free) > 0)
243
qmi_param_free(param);
246
CALLBACK_WITH_FAILURE(cb, NULL, 0, user_data);
251
static void qmi_read_record(struct ofono_sim *sim,
252
int fileid, int record, int length,
253
const unsigned char *path,
254
unsigned int path_len,
255
ofono_sim_read_cb_t cb, void *user_data)
257
struct sim_data *data = ofono_sim_get_data(sim);
258
struct cb_data *cbd = cb_data_new(cb, user_data);
259
unsigned char aid_data[2] = { 0x06, 0x00 };
260
unsigned char read_data[4];
261
unsigned char fileid_data[9];
263
struct qmi_param *param;
265
DBG("file id 0x%04x path len %d", fileid, path_len);
267
fileid_len = create_fileid_data(data->app_type, fileid,
268
path, path_len, fileid_data);
272
read_data[0] = record & 0xff;
273
read_data[1] = (record & 0xff00) >> 8;
274
read_data[2] = length & 0xff;
275
read_data[3] = (length & 0xff00) >> 8;
277
param = qmi_param_new();
281
qmi_param_append(param, 0x01, sizeof(aid_data), aid_data);
282
qmi_param_append(param, 0x02, fileid_len, fileid_data);
283
qmi_param_append(param, 0x03, sizeof(read_data), read_data);
285
if (qmi_service_send(data->uim, QMI_UIM_READ_RECORD, param,
286
read_generic_cb, cbd, g_free) > 0)
289
qmi_param_free(param);
292
CALLBACK_WITH_FAILURE(cb, NULL, 0, user_data);
297
static void qmi_query_passwd_state(struct ofono_sim *sim,
298
ofono_sim_passwd_cb_t cb, void *user_data)
300
struct sim_data *data = ofono_sim_get_data(sim);
302
DBG("passwd state %d", data->passwd_state);
304
if (data->passwd_state == OFONO_SIM_PASSWORD_INVALID) {
305
CALLBACK_WITH_FAILURE(cb, -1, user_data);
309
CALLBACK_WITH_SUCCESS(cb, data->passwd_state, user_data);
312
static void qmi_query_pin_retries(struct ofono_sim *sim,
313
ofono_sim_pin_retries_cb_t cb, void *user_data)
315
struct sim_data *data = ofono_sim_get_data(sim);
317
DBG("passwd state %d", data->passwd_state);
319
if (data->passwd_state == OFONO_SIM_PASSWORD_INVALID) {
320
CALLBACK_WITH_FAILURE(cb, NULL, user_data);
324
CALLBACK_WITH_SUCCESS(cb, data->retries, user_data);
327
static void card_setup(const struct qmi_uim_slot_info *slot,
328
const struct qmi_uim_app_info1 *info1,
329
const struct qmi_uim_app_info2 *info2,
330
struct sim_data *data)
332
data->card_state = slot->card_state;
333
data->app_type = info1->app_type;
335
switch (info1->app_state) {
336
case 0x02: /* PIN1 or UPIN is required */
337
data->passwd_state = OFONO_SIM_PASSWORD_SIM_PIN;
339
case 0x03: /* PUK1 or PUK for UPIN is required */
340
data->passwd_state = OFONO_SIM_PASSWORD_SIM_PUK;
342
case 0x07: /* Ready */
343
data->passwd_state = OFONO_SIM_PASSWORD_NONE;
346
data->passwd_state = OFONO_SIM_PASSWORD_INVALID;
350
data->retries[OFONO_SIM_PASSWORD_SIM_PIN] = info2->pin1_retries;
351
data->retries[OFONO_SIM_PASSWORD_SIM_PUK] = info2->puk1_retries;
353
data->retries[OFONO_SIM_PASSWORD_SIM_PIN2] = info2->pin2_retries;
354
data->retries[OFONO_SIM_PASSWORD_SIM_PUK2] = info2->puk2_retries;
357
static void get_card_status_cb(struct qmi_result *result, void *user_data)
359
struct ofono_sim *sim = user_data;
360
struct sim_data *data = ofono_sim_get_data(sim);
362
const struct qmi_uim_card_status *status;
363
uint16_t len, offset;
368
if (qmi_result_set_error(result, NULL))
371
ptr = qmi_result_get(result, QMI_UIM_RESULT_CARD_STATUS, &len);
376
offset = sizeof(struct qmi_uim_card_status);
378
for (i = 0; i < status->num_slot; i++) {
379
const struct qmi_uim_slot_info *slot;
383
offset += sizeof(struct qmi_uim_slot_info);
385
for (n = 0; n < slot->num_app; n++) {
386
const struct qmi_uim_app_info1 *info1;
387
const struct qmi_uim_app_info2 *info2;
390
info1 = ptr + offset;
391
offset += sizeof(struct qmi_uim_app_info1);
392
offset += info1->aid_len;
394
info2 = ptr + offset;
395
offset += sizeof(struct qmi_uim_app_info2);
397
index = GUINT16_FROM_LE(status->index_gw_pri);
399
if ((index & 0xff) == i && (index >> 8) == n)
400
card_setup(slot, info1, info2, data);
405
ofono_sim_register(sim);
407
switch (data->card_state) {
408
case 0x00: /* Absent */
409
case 0x02: /* Error */
411
case 0x01: /* Present */
412
ofono_sim_inserted_notify(sim, TRUE);
417
static void event_registration_cb(struct qmi_result *result, void *user_data)
419
struct ofono_sim *sim = user_data;
420
struct sim_data *data = ofono_sim_get_data(sim);
424
if (qmi_result_set_error(result, NULL))
427
if (!qmi_result_get_uint32(result, QMI_UIM_RESULT_EVENT_MASK,
431
DBG("event mask 0x%04x", data->event_mask);
433
if (qmi_service_send(data->uim, QMI_UIM_GET_CARD_STATUS, NULL,
434
get_card_status_cb, sim, NULL) > 0)
438
ofono_sim_remove(sim);
442
static void create_uim_cb(struct qmi_service *service, void *user_data)
444
struct ofono_sim *sim = user_data;
445
struct sim_data *data = ofono_sim_get_data(sim);
446
struct qmi_param *param;
447
uint32_t mask = 0x0003;
452
ofono_error("Failed to request UIM service");
456
data->uim = qmi_service_ref(service);
458
param = qmi_param_new_uint32(QMI_UIM_PARAM_EVENT_MASK, mask);
462
if (qmi_service_send(data->uim, QMI_UIM_EVENT_REGISTRATION, param,
463
event_registration_cb, sim, NULL) > 0)
467
qmi_service_unref(data->uim);
469
ofono_sim_remove(sim);
472
static int qmi_sim_probe(struct ofono_sim *sim,
473
unsigned int vendor, void *user_data)
475
struct qmi_device *device = user_data;
476
struct sim_data *data;
481
data = g_new0(struct sim_data, 1);
483
data->passwd_state = OFONO_SIM_PASSWORD_INVALID;
485
for (i = 0; i < OFONO_SIM_PASSWORD_INVALID; i++)
486
data->retries[i] = -1;
488
ofono_sim_set_data(sim, data);
490
qmi_service_create(device, QMI_SERVICE_UIM, create_uim_cb, sim, NULL);
495
static void qmi_sim_remove(struct ofono_sim *sim)
497
struct sim_data *data = ofono_sim_get_data(sim);
501
ofono_sim_set_data(sim, NULL);
503
qmi_service_unregister_all(data->uim);
505
qmi_service_unref(data->uim);
510
static struct ofono_sim_driver driver = {
512
.probe = qmi_sim_probe,
513
.remove = qmi_sim_remove,
514
.read_file_info = qmi_read_attributes,
515
.read_file_transparent = qmi_read_transparent,
516
.read_file_linear = qmi_read_record,
517
.read_file_cyclic = qmi_read_record,
518
.query_passwd_state = qmi_query_passwd_state,
519
.query_pin_retries = qmi_query_pin_retries,
522
void qmi_sim_init(void)
524
ofono_sim_driver_register(&driver);
527
void qmi_sim_exit(void)
529
ofono_sim_driver_unregister(&driver);