1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/**
* This file is part of libsamsung-ipc.
*
* Copyright (C) 2011 Paul Kocialkowski <contact@paulk.fr>
* Joerie de Gram <j.de.gram@gmail.com>
* Simon Busch <morphis@gravedo.de>
*
* libsamsung-ipc is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsamsung-ipc 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <asm/types.h>
#include <mtd/mtd-abi.h>
#include <assert.h>
#include <radio.h>
#include "crespo_modem_ctl.h"
#include "crespo_ipc.h"
#include "ipc_private.h"
int wake_lock_fd = -1;
int wake_unlock_fd = -1;
int wake_lock(char *lock_name, int len)
{
int rc = 0;
if(wake_lock_fd < 0)
wake_lock_fd = open("/sys/power/wake_lock", O_RDWR);
rc = write(wake_lock_fd, lock_name, len);
return rc;
}
int wake_unlock(char *lock_name, int len)
{
int rc = 0;
if(wake_unlock_fd < 0)
wake_unlock_fd = open("/sys/power/wake_unlock", O_RDWR);
rc = write(wake_unlock_fd, lock_name, len);
return rc;
}
int crespo_modem_bootstrap(struct ipc_client *client)
{
int s3c2410_serial3_fd = -1;
int modem_ctl_fd = -1;
/* Control variables. */
int rc = 0;
/* Boot variables */
uint8_t *radio_img_p = NULL;
uint8_t bootcore_version = 0;
uint8_t info_size = 0;
uint8_t crc_byte = 0;
int block_size = 0;
/* s3c2410 serial setup variables. */
struct termios termios;
int serial;
/* fds maniplation variables */
struct timeval timeout;
fd_set fds;
/* nv_data variables */
void *nv_data_p;
/* General purpose variables. */
uint8_t data;
uint16_t data_16;
uint8_t *data_p;
int i;
ipc_client_log(client, "crespo_ipc_bootstrap: enter");
/* Read the radio.img image. */
ipc_client_log(client, "crespo_ipc_bootstrap: reading radio image");
radio_img_p = ipc_client_mtd_read(client, "/dev/mtd/mtd5ro", RADIO_IMG_SIZE, 0x1000);
if (radio_img_p == NULL) {
radio_img_p = ipc_client_mtd_read(client, "/dev/mtd5ro", RADIO_IMG_SIZE, 0x1000);
if (radio_img_p == NULL)
goto error;
}
ipc_client_log(client, "crespo_ipc_bootstrap: radio image read");
ipc_client_log(client, "crespo_ipc_bootstrap: open modem_ctl");
modem_ctl_fd=open("/dev/modem_ctl", O_RDWR | O_NDELAY);
if(modem_ctl_fd < 0)
goto error;
/* Reset the modem before init to send the first part of modem.img. */
ioctl(modem_ctl_fd, IOCTL_MODEM_RESET);
usleep(400000);
ipc_client_log(client, "crespo_ipc_bootstrap: open s3c2410_serial3");
s3c2410_serial3_fd=open("/dev/s3c2410_serial3", O_RDWR | O_NDELAY);
if(s3c2410_serial3_fd < 0)
goto error;
/* Setup the s3c2410 serial. */
ipc_client_log(client, "crespo_ipc_bootstrap: setup s3c2410_serial3");
tcgetattr(s3c2410_serial3_fd, &termios);
cfmakeraw(&termios);
cfsetispeed(&termios, B115200);
cfsetospeed(&termios, B115200);
tcsetattr(s3c2410_serial3_fd, TCSANOW, &termios);
/* Send 'AT' in ASCII. */
ipc_client_log(client, "crespo_ipc_bootstrap: sending AT in ASCII");
for(i=0 ; i < 20 ; i++)
{
rc = write(s3c2410_serial3_fd, "AT", 2);
usleep(50000);
}
ipc_client_log(client, "crespo_ipc_bootstrap: sending AT in ASCII done");
usleep(50000); //FIXME
/* Get and check bootcore version. */
read(s3c2410_serial3_fd, &bootcore_version, sizeof(bootcore_version));
ipc_client_log(client, "crespo_ipc_bootstrap: got bootcore version: 0x%x", bootcore_version);
if(bootcore_version != BOOTCORE_VERSION)
goto error;
/* Get info_size. */
read(s3c2410_serial3_fd, &info_size, sizeof(info_size));
ipc_client_log(client, "crespo_ipc_bootstrap: got info_size: 0x%x", info_size);
/* Send PSI magic. */
data=PSI_MAGIC;
write(s3c2410_serial3_fd, &data, sizeof(data));
ipc_client_log(client, "crespo_ipc_bootstrap: sent PSI_MAGIC (0x%x)", PSI_MAGIC);
/* Send PSI data len. */
data_16=PSI_DATA_LEN;
data_p=(uint8_t *)&data_16;
for(i=0 ; i < 2 ; i++)
{
write(s3c2410_serial3_fd, data_p, 1);
data_p++;
}
ipc_client_log(client, "crespo_ipc_bootstrap: sent PSI_DATA_LEN (0x%x)", PSI_DATA_LEN);
/* Write the first part of modem.img. */
FD_ZERO(&fds);
FD_SET(s3c2410_serial3_fd, &fds);
timeout.tv_sec=4;
timeout.tv_usec=0;
data_p=radio_img_p;
ipc_client_log(client, "crespo_ipc_bootstrap: sending the first part of radio.img");
for(i=0 ; i < PSI_DATA_LEN ; i++)
{
if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
{
ipc_client_log(client, "crespo_ipc_bootstrap: select timeout passed");
goto error;
}
write(s3c2410_serial3_fd, data_p, 1);
crc_byte=crc_byte ^ *data_p;
data_p++;
}
ipc_client_log(client, "crespo_ipc_bootstrap: first part of radio.img sent; crc_byte is 0x%x", crc_byte);
if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
{
ipc_client_log(client, "crespo_ipc_bootstrap: select timeout passed");
goto error;
}
write(s3c2410_serial3_fd, &crc_byte, sizeof(crc_byte));
ipc_client_log(client, "crespo_ipc_bootstrap: crc_byte sent");
data = 0;
for(i = 0 ; data != 0x01 ; i++)
{
if(select(FD_SETSIZE, &fds, NULL, NULL, &timeout) == 0)
{
ipc_client_log(client, "crespo_ipc_bootstrap: select timeout passed");
goto error;
}
read(s3c2410_serial3_fd, &data, sizeof(data));
if(i > 50)
{
ipc_client_log(client, "crespo_ipc_bootstrap: fairly too much attempts to get ACK");
goto error;
}
}
ipc_client_log(client, "crespo_ipc_bootstrap: close s3c2410_serial3");
close(s3c2410_serial3_fd);
ipc_client_log(client, "crespo_ipc_bootstrap: writing the rest of radio.img to modem_ctl.");
/* Seek to the begining of modem_ctl_fd (should already be so). */
lseek(modem_ctl_fd, 0, SEEK_SET);
/* Pointer to the remaining part of radio.img. */
data_p=radio_img_p + PSI_DATA_LEN;
FD_ZERO(&fds);
FD_SET(modem_ctl_fd, &fds);
block_size = 0x100000;
for(i=0 ; i < (RADIO_IMG_SIZE - PSI_DATA_LEN) / block_size ; i++)
{
if(select(FD_SETSIZE, NULL, &fds, NULL, &timeout) == 0)
{
ipc_client_log(client, "crespo_ipc_bootstrap: select timeout passed");
goto error;
}
write(modem_ctl_fd, data_p, block_size);
data_p += block_size;
}
free(radio_img_p);
/* nv_data part. */
/* Check if all the nv_data files are ok. */
nv_data_check(client);
/* Check if the MD5 is ok. */
nv_data_md5_check(client);
/* Write nv_data.bin to modem_ctl. */
ipc_client_log(client, "crespo_ipc_bootstrap: write nv_data to modem_ctl");
nv_data_p = ipc_client_file_read(client, nv_data_path(client), nv_data_size(client), nv_data_chunk_size(client));
if (nv_data_p == NULL)
goto error;
data_p = nv_data_p;
lseek(modem_ctl_fd, RADIO_IMG_SIZE, SEEK_SET);
for(i=0 ; i < 2 ; i++)
{
write(modem_ctl_fd, data_p, nv_data_size(client) / 2);
data_p += nv_data_size(client) / 2;
}
free(nv_data_p);
close(modem_ctl_fd);
rc = 0;
goto exit;
error:
ipc_client_log(client, "%s: something went wrong", __func__);
rc = -1;
exit:
ipc_client_log(client, "crespo_ipc_bootstrap: exit");
return rc;
}
int crespo_ipc_fmt_client_send(struct ipc_client *client, struct ipc_message_info *request)
{
struct modem_io modem_data;
struct ipc_header reqhdr;
int rc = 0;
memset(&modem_data, 0, sizeof(struct modem_io));
modem_data.size = request->length + sizeof(struct ipc_header);
reqhdr.mseq = request->mseq;
reqhdr.aseq = request->aseq;
reqhdr.group = request->group;
reqhdr.index = request->index;
reqhdr.type = request->type;
reqhdr.length = (uint16_t) (request->length + sizeof(struct ipc_header));
modem_data.data = malloc(reqhdr.length);
memcpy(modem_data.data, &reqhdr, sizeof(struct ipc_header));
memcpy((unsigned char *) (modem_data.data + sizeof(struct ipc_header)), request->data, request->length);
assert(client->handlers->write != NULL);
ipc_client_log_send(client, request, __func__);
rc = client->handlers->write((uint8_t*) &modem_data, sizeof(struct modem_io), client->handlers->write_data);
return rc;
}
int crespo_ipc_rfs_client_send(struct ipc_client *client, struct ipc_message_info *request)
{
struct modem_io modem_data;
int rc = 0;
memset(&modem_data, 0, sizeof(struct modem_io));
modem_data.id = request->mseq;
modem_data.cmd = request->index;
modem_data.size = request->length;
modem_data.data = malloc(request->length);
memcpy(modem_data.data, request->data, request->length);
assert(client->handlers->write != NULL);
ipc_client_log_send(client, request, __func__);
rc = client->handlers->write((uint8_t*) &modem_data, sizeof(struct modem_io), client->handlers->write_data);
return rc;
}
int crespo_ipc_fmt_client_recv(struct ipc_client *client, struct ipc_message_info *response)
{
struct modem_io modem_data;
struct ipc_header *resphdr;
int bread = 0;
memset(&modem_data, 0, sizeof(struct modem_io));
modem_data.data = malloc(MAX_MODEM_DATA_SIZE);
modem_data.size = MAX_MODEM_DATA_SIZE;
memset(response, 0, sizeof(struct ipc_message_info));
wake_lock("secril_fmt-interface", 20);
assert(client->handlers->read != NULL);
bread = client->handlers->read((uint8_t*) &modem_data, sizeof(struct modem_io) + MAX_MODEM_DATA_SIZE, client->handlers->read_data);
if (bread < 0)
{
ipc_client_log(client, "crespo_ipc_fmt_client_recv: can't receive enough bytes from modem to process incoming response!");
return -1;
}
if(modem_data.size <= 0 || modem_data.size >= MAX_MODEM_DATA_SIZE || modem_data.data == NULL)
{
ipc_client_log(client, "crespo_ipc_fmt_client_recv: we retrieve less (or fairly too much) bytes from the modem than we exepected!");
return -1;
}
resphdr = (struct ipc_header *) modem_data.data;
response->mseq = resphdr->mseq;
response->aseq = resphdr->aseq;
response->group = resphdr->group;
response->index = resphdr->index;
response->type = resphdr->type;
response->length = modem_data.size - sizeof(struct ipc_header);
response->data = NULL;
if(response->length > 0)
{
response->data = malloc(response->length);
memcpy(response->data, (uint8_t *) modem_data.data + sizeof(struct ipc_header), response->length);
}
free(modem_data.data);
ipc_client_log_recv(client, response, __func__);
wake_unlock("secril_fmt-interface", 20);
return 0;
}
int crespo_ipc_rfs_client_recv(struct ipc_client *client, struct ipc_message_info *response)
{
struct modem_io modem_data;
int bread = 0;
memset(&modem_data, 0, sizeof(struct modem_io));
modem_data.data = malloc(MAX_MODEM_DATA_SIZE);
modem_data.size = MAX_MODEM_DATA_SIZE;
memset(response, 0, sizeof(struct ipc_message_info));
wake_lock("secril_rfs-interface", 20);
assert(client->handlers->read != NULL);
bread = client->handlers->read((uint8_t*) &modem_data, sizeof(struct modem_io) + MAX_MODEM_DATA_SIZE, client->handlers->read_data);
if (bread < 0)
{
ipc_client_log(client, "crespo_ipc_rfs_client_recv: can't receive enough bytes from modem to process incoming response!");
return -1;
}
if(modem_data.size <= 0 || modem_data.size >= MAX_MODEM_DATA_SIZE || modem_data.data == NULL)
{
ipc_client_log(client, "crespo_ipc_rfs_client_recv: we retrieve less (or fairly too much) bytes from the modem than we exepected!");
return -1;
}
response->mseq = 0;
response->aseq = modem_data.id;
response->group = IPC_GROUP_RFS;
response->index = modem_data.cmd;
response->type = 0;
response->length = modem_data.size;
response->data = NULL;
if(response->length > 0)
{
response->data = malloc(response->length);
memcpy(response->data, (uint8_t *) modem_data.data, response->length);
}
free(modem_data.data);
ipc_client_log_recv(client, response, __func__);
wake_unlock("secril_rfs-interface", 20);
return 0;
}
int crespo_ipc_open(int type, void *io_data)
{
int fd = -1;
switch(type)
{
case IPC_CLIENT_TYPE_FMT:
fd = open("/dev/modem_fmt", O_RDWR | O_NOCTTY | O_NONBLOCK);
break;
case IPC_CLIENT_TYPE_RFS:
fd = open("/dev/modem_rfs", O_RDWR | O_NOCTTY | O_NONBLOCK);
break;
default:
break;
}
if(fd < 0)
return -1;
if(io_data == NULL)
return -1;
memcpy(io_data, &fd, sizeof(int));
return 0;
}
int crespo_ipc_close(void *io_data)
{
int fd = -1;
if(io_data == NULL)
return -1;
fd = *((int *) io_data);
if(fd < 0)
return -1;
close(fd);
return 0;
}
int crespo_ipc_read(void *data, unsigned int size, void *io_data)
{
int fd = -1;
int rc;
if(io_data == NULL)
return -1;
if(data == NULL)
return -1;
fd = *((int *) io_data);
if(fd < 0)
return -1;
rc = ioctl(fd, IOCTL_MODEM_RECV, data);
if(rc < 0)
return -1;
return 0;
}
int crespo_ipc_write(void *data, unsigned int size, void *io_data)
{
int fd = -1;
int rc;
if(io_data == NULL)
return -1;
fd = *((int *) io_data);
if(fd < 0)
return -1;
rc = ioctl(fd, IOCTL_MODEM_SEND, data);
if(rc < 0)
return -1;
return 0;
}
int crespo_ipc_power_on(void *io_data)
{
int fd=open("/dev/modem_ctl", O_RDWR);
int rc;
/*
fd = open("/sys/devices/platform/modemctl/power_mode", O_RDWR);
rc = write(fd, "1", 1);
*/
if(fd < 0)
return -1;
rc = ioctl(fd, IOCTL_MODEM_START);
close(fd);
if(rc < 0)
return -1;
return 0;
}
int crespo_ipc_power_off(void *io_data)
{
int fd=open("/dev/modem_ctl", O_RDWR);
int rc;
/*
fd = open("/sys/devices/platform/modemctl/power_mode", O_RDWR);
rc = write(fd, "0", 1);
*/
if(fd < 0)
return -1;
rc = ioctl(fd, IOCTL_MODEM_OFF);
close(fd);
if(rc < 0)
return -1;
return 0;
}
int crespo_ipc_gprs_get_iface(char **iface, int cid)
{
asprintf(iface, "%s%d", GPRS_IFACE_PREFIX, 0);
return 0;
}
int crespo_ipc_gprs_get_capabilities(struct ipc_client_gprs_capabilities *cap)
{
if (cap == NULL)
return -1;
cap->port_list = 0;
cap->cid_max = 1;
return 0;
}
void *crespo_ipc_common_data_create(void)
{
void *io_data;
int io_data_len;
io_data_len = sizeof(int);
io_data = malloc(io_data_len);
if(io_data == NULL)
return NULL;
memset(io_data, 0, io_data_len);
return io_data;
}
int crespo_ipc_common_data_destroy(void *io_data)
{
// This was already done, not an error but we need to return
if(io_data == NULL)
return 0;
free(io_data);
return 0;
}
int crespo_ipc_common_data_set_fd(void *io_data, int fd)
{
int *common_data;
if(io_data == NULL)
return -1;
common_data = (int *) io_data;
*common_data = fd;
return 0;
}
int crespo_ipc_common_data_get_fd(void *io_data)
{
int *common_data;
if(io_data == NULL)
return -1;
common_data = (int *) io_data;
return (int) *(common_data);
}
struct ipc_ops crespo_fmt_ops = {
.send = crespo_ipc_fmt_client_send,
.recv = crespo_ipc_fmt_client_recv,
.bootstrap = crespo_modem_bootstrap,
};
struct ipc_ops crespo_rfs_ops = {
.send = crespo_ipc_rfs_client_send,
.recv = crespo_ipc_rfs_client_recv,
.bootstrap = NULL,
};
struct ipc_handlers crespo_default_handlers = {
.read = crespo_ipc_read,
.write = crespo_ipc_write,
.open = crespo_ipc_open,
.close = crespo_ipc_close,
.power_on = crespo_ipc_power_on,
.power_off = crespo_ipc_power_off,
.common_data = NULL,
.common_data_create = crespo_ipc_common_data_create,
.common_data_destroy = crespo_ipc_common_data_destroy,
.common_data_set_fd = crespo_ipc_common_data_set_fd,
.common_data_get_fd = crespo_ipc_common_data_get_fd,
};
struct ipc_gprs_specs crespo_gprs_specs = {
.gprs_get_iface = crespo_ipc_gprs_get_iface,
.gprs_get_capabilities = crespo_ipc_gprs_get_capabilities,
};
// vim:ts=4:sw=4:expandtab
|