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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
/* driver/char/modem_interface/modem_interface_drv.c
*
* modem interface API for dloader driver and API for application layer
*
* Copyright (C) 2012 Spreadtrum
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program 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.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/semaphore.h>
#include <mach/modem_interface.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/wait.h>
#include <linux/wakelock.h>
#include <linux/sched.h>
#include<linux/spinlock.h>
#include "modem_buffer.h"
#include "modem_interface_driver.h"
#include <mach/globalregs.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/timer.h>
#define MAX_MSG_NODE_NUM 32
#define BUSY_BIT 0x8000
#define MAX_WAIT_WATCHDOG_TIME 5
extern int get_alive_status(void);
extern int get_assert_status(void);
extern int modem_gpio_uninit(void *para);
extern int modem_gpio_init(void *para);
extern int modem_share_gpio_uninit(void *para);
extern int modem_share_gpio_init(void *para);
extern void mux_ipc_enable(u8 is_enable);
extern void modem_gpio_irq_init(void* para);
extern void modem_poweron_async_step1(void);
extern void modem_poweron_async_step2(void);
extern struct modem_device_operation *modem_sdio_drv_init(void);
extern int modem_gpio_init(void *para);
extern void modem_poweron(void);
extern void modem_poweroff(void);
extern void Set_modem_status(unsigned char modem_status);
extern void modem_soft_reset(void);
extern void modem_hard_reset(void);
extern void sdio_ipc_enable(u8 is_enable);
extern void shutdown_protocol(struct modem_message_node *msg);
extern void bootcomp_protocol(struct modem_message_node *msg);
extern void reset_protocol(struct modem_message_node *msg);
extern int modem_intf_get_watchdog_gpio_value();
int modem_intf_send_boot_message(void);
void modem_intf_reboot_routine(void);
void modem_intf_alive_routine(void);
void modem_intf_assert_routine(void);
void modem_intf_wdg_reset_routine(void);
static struct modem_intf_device *modem_intf_device = NULL;
static struct modem_message_node *msg_nodes=NULL;
static atomic_t nodes_count;
static int lost_msg_count;
static LIST_HEAD(msg_list);
static struct semaphore msg_list_sem;
static struct semaphore modem_event_sem;
static int modem_gpio_status[CTRL_GPIO_MAX];
static int modem_event = MODEM_INTF_EVENT_SHUTDOWN;
static struct wake_lock s_modem_intf_proc_wake_lock;
static struct wake_lock s_modem_intf_msg_wake_lock;
static struct wake_lock s_modem_intf_event_wake_lock;
static struct timer_list s_modem_watch_timer;
//wait_queue_head_t mdoem_mode_normal;
spinlock_t int_lock;
struct modem_device_operation *devices_op[8] = {NULL};
/************ modemsts notifier *****************/
static LIST_HEAD(modemsts_chg_handlers);
static DEFINE_MUTEX(modemsts_chg_lock);
/* register a callback function when modem status changed
* @handler: callback function
*/
int modemsts_notifier_register(struct modemsts_chg *handler)
{
struct list_head *pos;
struct modemsts_chg *e;
mutex_lock(&modemsts_chg_lock);
list_for_each(pos, &modemsts_chg_handlers) {
e = list_entry(pos, struct modemsts_chg, link);
if(e == handler){
printk("***** %s, %pf already exsited ****\n",
__func__, e->modemsts_notifier);
return -1;
}
}
list_for_each(pos, &modemsts_chg_handlers) {
struct modemsts_chg *e;
e = list_entry(pos, struct modemsts_chg, link);
if (e->level > handler->level)
break;
}
list_add_tail(&handler->link, pos);
mutex_unlock(&modemsts_chg_lock);
return 0;
}
EXPORT_SYMBOL(modemsts_notifier_register);
/* unregister a callback function for detecting modem status change
* @handler: callback function
*/
int modemsts_notifier_unregister(struct modemsts_chg *handler)
{
mutex_lock(&modemsts_chg_lock);
list_del(&handler->link);
mutex_unlock(&modemsts_chg_lock);
return 0;
}
EXPORT_SYMBOL(modemsts_notifier_unregister);
void modemsts_change_notification(unsigned int state)
{
struct modemsts_chg *pos;
mutex_lock(&modemsts_chg_lock);
if(state == MODEM_STATUS_REBOOT) {
list_for_each_entry(pos, &modemsts_chg_handlers, link) {
if (pos->modemsts_notifier != NULL) {
pos->modemsts_notifier(pos, state);
}
}
} else {
list_for_each_entry_reverse(pos, &modemsts_chg_handlers, link) {
if (pos->modemsts_notifier != NULL) {
pos->modemsts_notifier(pos, state);
}
}
}
mutex_unlock(&modemsts_chg_lock);
}
/************ modem notifier *****************/
char *modem_intf_msg_string(enum MODEM_MSG_type msg)
{
switch(msg) {
case MODEM_TRANSFER_REQ:
return "MODEM_TRANSFER_REQ";
case MODEM_TRANSFER_END:
return "MODEM_TRANSFER_END";
case MODEM_SLAVE_RTS:
return "MODEM_SLAVE_RTS";
case MODEM_SLAVE_RDY:
return "MODEM_SLAVE_RDY";
case MODEM_SET_MODE:
return "MODEM_SET_MODE";
case MODEM_CTRL_GPIO_CHG:
return "MODEM_CTRL_GPIO_CHG";
case MODEM_USER_REQ:
return "MODEM_USER_REQ";
case MODEM_BOOT_CMP:
return "MODEM_BOOT_CMP";
case MODEM_OPEN_DEVICE:
return "MODEM_OPEN_DEVICE";
default:
break;
}
return "UNKNOWN MSG";
}
static void modem_intf_register_device_operation(struct modem_device_operation *op)
{
int i;
for(i=0; i<8; i++) {
if(devices_op[i] == NULL)
devices_op[i] = op;
}
}
static struct modem_device_operation *modem_intf_get_device_operation(enum MODEM_device_type dev) {
int i;
for(i=0; i<8; i++) {
if((devices_op[i] != NULL)&&(devices_op[i]->dev == dev))
return devices_op[i];
}
return NULL;
}
static int init_msg_nodes(void)
{
int i;
if(msg_nodes)
return 0;
msg_nodes = kzalloc(sizeof(struct modem_message_node)*MAX_MSG_NODE_NUM, GFP_KERNEL);
if (msg_nodes == NULL)
return -ENOMEM;
atomic_set(&nodes_count,0);
for (i=0; i<MAX_MSG_NODE_NUM; i++) {
msg_nodes[i].buno = i;
}
lost_msg_count = 0;
atomic_set(&nodes_count,i);
return 0;
}
static struct modem_message_node * find_msg_node(void) {
unsigned long flags;
int i;
if(atomic_read(&nodes_count) == 0)
return NULL;
//local_irq_save(flags);
spin_lock_irqsave(&int_lock, flags);
for (i=0; i<MAX_MSG_NODE_NUM; i++) {
if ((msg_nodes[i].buno & BUSY_BIT )==0) {
atomic_dec(&nodes_count);
msg_nodes[i].buno |= BUSY_BIT;
break;
}
}
//local_irq_restore(flags);
spin_unlock_irqrestore(&int_lock, flags);
if(i==MAX_MSG_NODE_NUM)
return NULL;
return &msg_nodes[i];
}
static void free_msg_node(struct modem_message_node *msg)
{
if (msg) {
msg->buno &= ~BUSY_BIT;
atomic_inc(&nodes_count);
}
}
static struct modem_message_node * modem_intf_get_message( void ) {
struct list_head *entry;
struct modem_message_node *msg;
unsigned long flags;
do {
if(!list_empty(&msg_list))
break;
down(&msg_list_sem);
} while(1);
if(!list_empty(&msg_list)) {
entry = msg_list.next;
//local_irq_save(flags);
spin_lock_irqsave(&int_lock, flags);
list_del_init(entry);
//local_irq_restore(flags);
spin_unlock_irqrestore(&int_lock, flags);
msg = (struct modem_message_node *)list_entry(entry,struct modem_message_node,link);
return (struct modem_message_node *)msg;
}
return NULL;
}
static void modem_send_message( struct modem_message_node *msg )
{
unsigned long flags;
wake_lock_timeout(&s_modem_intf_msg_wake_lock, HZ / 2);
//local_irq_save(flags);
spin_lock_irqsave(&int_lock, flags);
list_add_tail(&msg->link,&msg_list);
//local_irq_restore(flags);
spin_unlock_irqrestore(&int_lock, flags);
up(&msg_list_sem);
}
static int modem_protocol_thread(void *data)
{
struct modem_message_node *msg;
int boot_cpu = smp_processor_id();
long rc;
struct sched_param param = {.sched_priority = 98};
current_thread_info()->cpu = boot_cpu;
rc = sched_setaffinity(current->pid, cpumask_of(boot_cpu));
sched_setscheduler(current, SCHED_FIFO, ¶m);
while(1) {
msg = modem_intf_get_message();
if(msg == NULL)
continue;
wake_lock(&s_modem_intf_proc_wake_lock);
switch(modem_intf_device->mode) {
case MODEM_MODE_SHUTDOWN:
shutdown_protocol(msg);
break;
case MODEM_MODE_BOOT:
boot_protocol(msg);
break;
case MODEM_MODE_BOOTCOMP:
bootcomp_protocol(msg);
break;
case MODEM_MODE_NORMAL:
normal_protocol(msg);
break;
case MODEM_MODE_DUMP:
dump_memory_protocol(msg);
break;
case MODEM_MODE_RESET:
reset_protocol(msg);
break;
default:
break;
}
free_msg_node(msg);
wake_unlock(&s_modem_intf_proc_wake_lock);
}
return 0;
}
int modem_intf_open(enum MODEM_Mode_type mode,int index)
{
int retval=0;
if(modem_intf_device == NULL)
return -1;
printk("boot gpio = %d\n",modem_intf_device->modem_config.modem_boot_gpio);
if(modem_intf_device->op == NULL) {
modem_intf_device->op = modem_intf_get_device_operation(modem_intf_device->modem_config.dev_type);
printk("modem_intf_open: op = %x\n",modem_intf_device->op);
if(modem_intf_device->op == NULL)
return -1;
}
if((modem_intf_device->op)&&(mode == MODEM_MODE_BOOT)) {
if(0 != modem_intf_device->op->open(NULL)) {
return -1;
}
}
return retval;
}
int modem_intf_send_bootcomp_message(enum MODEM_Mode_type mode,int index)
{
struct modem_message_node *msg;
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_bootcomp_message MSG LOST!!!!!!\n");
return -1;
}
msg->src = SRC_DLOADER;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = 0;
msg->type = MODEM_BOOT_CMP;
modem_send_message(msg);
return 0;
}
int modem_intf_read(char *buffer, int size,int index)
{
int read_len;
if(modem_intf_device->mode == MODEM_MODE_UNKNOWN)
return 0;
read_len = pingpang_buffer_read(&modem_intf_device->recv_buffer,buffer,size);
return read_len;
}
int modem_intf_write(char *buffer, int size,int index)
{
struct modem_message_node *msg;
if(modem_intf_device->mode == MODEM_MODE_UNKNOWN)
return 0;
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
return -1;
}
if(buffer && size )
pingpang_buffer_write(&modem_intf_device->send_buffer,buffer,size);
msg->type = MODEM_TRANSFER_REQ;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = 0;
modem_send_message(msg);
return size;
}
void modem_intf_set_mode(enum MODEM_Mode_type mode,int force)
{
printk(KERN_INFO "modem_intf_set_mode enter old:%d,new:%d\n",
modem_intf_device->mode, mode);
if(modem_intf_device->mode == mode) {
goto fin;
}
if(force) {
modem_intf_device->mode = mode;
goto fin;
}
switch(modem_intf_device->mode) {
case MODEM_MODE_BOOT:
if(MODEM_MODE_NORMAL == mode || MODEM_MODE_BOOTCOMP == mode) {
modem_intf_device->mode = mode;
} else {
printk(KERN_ERR "modem_intf: try to enter mode:%d, in MODEM_MODE_BOOT", mode);
}
break;
case MODEM_MODE_BOOTCOMP:
if(MODEM_MODE_NORMAL == mode) {
modem_intf_device->mode = mode;
} else {
printk(KERN_ERR "modem_intf: try to enter mode:%d, in MODEM_MODE_BOOTCOMP", mode);
}
break;
case MODEM_MODE_NORMAL:
if(MODEM_MODE_DUMP == mode || MODEM_MODE_RESET == mode) {
modem_intf_device->mode = mode;
} else {
printk(KERN_ERR "modem_intf: try to enter mode:%d, in MODEM_MODE_NORMAL", mode);
}
break;
case MODEM_MODE_DUMP:
if(MODEM_MODE_RESET == mode || MODEM_MODE_BOOT == mode) {
modem_intf_device->mode = mode;
} else {
printk(KERN_ERR "modem_intf: try to enter mode:%d, in MODEM_MODE_DUMP", mode);
}
break;
default: //MODEM_MODE_RESET
if(MODEM_MODE_BOOT == mode) {
modem_intf_device->mode = mode;
} else {
printk(KERN_ERR "modem_intf: try to enter mode:%d, in MODEM_MODE_RESET", mode);
}
break;
}
fin:
printk(KERN_INFO "modem_intf_set_mode leave new:%d\n", modem_intf_device->mode);
}
void modem_intf_send_channel_message(int dir,int para,int index)
{
struct modem_message_node *msg;
if(modem_intf_device->mode == MODEM_MODE_UNKNOWN)
return ;
if(modem_intf_device->mode != MODEM_MODE_NORMAL)
return;
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
return ;
}
msg->src = SRC_SPI;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = dir;
msg->type = MODEM_TRANSFER_END;
modem_send_message(msg);
}
void modem_intf_send_ctrl_gpio_message(enum MSG_CTRL_GPIO_TYPE gpio_type, int gpio_value)
{
struct modem_message_node *msg;
printk("ctrl_gpio: gpio_type = %d value = %d\n", gpio_type, gpio_value);
if(modem_gpio_status[gpio_type] == gpio_value) {
return;
}
modem_gpio_status[gpio_type] = gpio_value;
//ctrl gpio only effective in these modem mode
if(modem_intf_device->mode != MODEM_MODE_BOOT &&
modem_intf_device->mode != MODEM_MODE_BOOTCOMP &&
modem_intf_device->mode != MODEM_MODE_NORMAL &&
modem_intf_device->mode != MODEM_MODE_DUMP) {
return;
}
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_ctrl_gpio_message MSG LOST!!!!!!\n");
return ;
}
msg->src = SRC_GPIO;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = ((gpio_type << 16) | gpio_value);
msg->type = MODEM_CTRL_GPIO_CHG;
modem_send_message(msg);
}
void modem_intf_send_user_req_message(int req)
{
struct modem_message_node *msg;
retry:
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_user_reboot_req_message MSG LOST!!!!!!\n");
msleep(100);
printk("modem_intf_send_user_reboot_req_message retry!!!!!!\n");
goto retry;
}
msg->src = SRC_USER;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = req;
msg->type = MODEM_USER_REQ;
modem_send_message(msg);
}
int modem_intf_send_boot_message()
{
struct modem_message_node *msg;
retry:
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_boot_message MSG LOST retry!!!!!!\n");
msleep(100);
goto retry;
}
msg->src = SRC_DLOADER;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = MODEM_MODE_BOOT;
msg->type = MODEM_SET_MODE;
modem_send_message(msg);
return 0;
}
int modem_intf_send_shutdown_message()
{
struct modem_message_node *msg;
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_shutdown_message MSG LOST !!!!!!\n");
return 0;
}
msg->src = SRC_DLOADER;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = MODEM_MODE_SHUTDOWN;
msg->type = MODEM_SET_MODE;
modem_send_message(msg);
return 0;
}
void modem_intf_send_GPIO_message(int gpio_no,int status,int index)
{
struct modem_message_node *msg;
printk("GPIO_MSG: mode = %d gpio=%d status = %d\n",modem_intf_device->mode,gpio_no,status);
if(modem_intf_device->mode == MODEM_MODE_UNKNOWN)
return ;
msg = find_msg_node();
if (msg == NULL) {
lost_msg_count++;
printk("modem_intf_send_GPIO_message MSG LOST!!!!!!\n");
return ;
}
msg->src = SRC_GPIO;
msg->parameter1 = (int)modem_intf_device;
msg->parameter2 = gpio_no|(status << 16);
if(modem_intf_device->mode == MODEM_MODE_NORMAL) {
if (gpio_no == modem_intf_device->modem_config.modem_boot_gpio) {
msg->type = MODEM_SLAVE_RDY;
} else if (gpio_no == modem_intf_device->modem_config.modem_crash_gpio) {
msg->type = MODEM_SLAVE_RTS;
} else if (gpio_no == modem_intf_device->modem_config.modem_alive_gpio) {
msg->type = MODEM_SLAVE_RTS;
}
} else if(modem_intf_device->mode == MODEM_MODE_BOOT) {
if ((gpio_no == modem_intf_device->modem_config.modem_boot_gpio)) {
if ( status != 0)
msg->type = MODEM_SLAVE_RTS;
else
msg->type = MODEM_TRANSFER_END;
}
} else if(modem_intf_device->mode == MODEM_MODE_DUMP) {
if (gpio_no == modem_intf_device->modem_config.modem_crash_gpio) {
msg->type = MODEM_SLAVE_RTS;
}
} else {
free_msg_node(msg);
return;
}
modem_send_message(msg);
}
static int modem_intf_driver_remove( struct platform_device *_dev)
{
return 0;
}
/**
* This function shows the modem state
*/
static ssize_t modem_intf_state_show(struct device *dev,struct device_attribute *attr, char *buf)
{
int ret;
printk("call modem_intf_state_show\n");
ret = down_interruptible(&modem_event_sem);
if(ret == -EINTR)
return ret;
printk("event available:%d ...\n",modem_event);
snprintf(buf,4,"%d\n",modem_event);
printk("event = %s\n",buf);
return strlen(buf);
}
extern void mux_ipc_enable(u8 is_enable);
void modem_intf_send_event(int event)
{
//user space process should wait for modem_event_sem
wake_lock_timeout(&s_modem_intf_event_wake_lock, HZ / 2);
modem_event = event;
up(&modem_event_sem);
}
void modem_intf_cp_reset_req_routine()
{
modem_intf_send_event(MODEM_INTF_EVENT_CP_REQ_RESET);
}
void modem_intf_reboot_routine()
{
//disable mux sdio spi
mux_ipc_enable(0);
sdio_ipc_enable(0);
modemsts_change_notification(MODEM_STATUS_REBOOT);
//modem_share_gpio_init(&(modem_intf_device->modem_config));
modem_intf_set_mode(MODEM_MODE_RESET, 0);
modem_intf_send_event(MODEM_INTF_EVENT_FORCE_RESET);
}
void modem_intf_alive_routine()
{
//msleep(10*1000);
modem_intf_set_mode(MODEM_MODE_NORMAL, 0);
modem_share_gpio_uninit(&(modem_intf_device->modem_config));
sdio_ipc_enable(1);
modemsts_change_notification(MODEM_STATUS_ALVIE);
mux_ipc_enable(1);
modem_intf_send_event(MODEM_INTF_EVENT_ALIVE);
}
void modem_intf_assert_routine()
{
modemsts_change_notification(MODEM_STATUS_ASSERT);
modem_intf_send_event(MODEM_INTF_EVENT_ASSERT);
}
void modem_intf_wdg_reset_routine(void)
{
modem_intf_send_event(MODEM_INTF_EVENT_WDG_RESET);
}
int chk_watchdog_reset()
{
unsigned long now = jiffies;
do {
udelay(10);
if(1 == modem_intf_get_watchdog_gpio_value())
{
return 1;
}
}while((msecs_to_jiffies(jiffies - now) < MAX_WAIT_WATCHDOG_TIME));
return 0;
}
void modem_intf_ctrl_gpio_handle_normal(int status)
{
int type, value;
type = status >> 16;
value = status & 0xFFFF;
switch(type) {
case GPIO_RESET:
//reset -> 1, ,means need reboot
if(value) {
//modem_intf_assert_routine();
modem_intf_set_mode(MODEM_MODE_DUMP, 0);
modem_intf_cp_reset_req_routine();
}
break;
case GPIO_ALIVE:
//alive -> 0,means assert
if(!value) {
//first check if watch dog reset changes cp_alive
if(chk_watchdog_reset()) {
printk("modem_intf: found watchdog reset in GPIO_ALIVE handle\n");
modem_intf_wdg_reset_routine();
modem_intf_set_mode(MODEM_MODE_DUMP, 0);
break;
}
modem_intf_assert_routine();
modem_intf_set_mode(MODEM_MODE_DUMP, 0);
}
break;
case GPIO_WATCHDOG:
//watchdog -> 0,means watch dog reset
if(value) {
modem_intf_wdg_reset_routine();
modem_intf_set_mode(MODEM_MODE_DUMP, 0);
}
break;
default:
break;
}
}
void modem_intf_ctrl_gpio_handle_dump(int status)
{
int type, value;
type = status >> 16;
value = status & 0xFFFF;
switch(type) {
case GPIO_RESET:
//reset -> 1,means need reboot
if(value) {
modem_intf_cp_reset_req_routine();
}
break;
case GPIO_ALIVE:
break;
case GPIO_WATCHDOG:
break;
default:
break;
}
}
void modem_intf_ctrl_gpio_handle_boot(int status)
{
int type, value;
type = status >> 16;
value = status & 0xFFFF;
switch(type) {
case GPIO_RESET:
break;
case GPIO_ALIVE:
//alive -> 1, means alive
if(value) {
modem_intf_alive_routine();
}
break;
case GPIO_WATCHDOG:
break;
default:
break;
}
}
void modem_intf_ctrl_gpio_handle_bootcomp(int status)
{
int type, value;
type = status >> 16;
value = status & 0xFFFF;
switch(type) {
case GPIO_RESET:
break;
case GPIO_ALIVE:
//alive -> 1, means alive
if(value) {
modem_intf_alive_routine();
}
break;
case GPIO_WATCHDOG:
break;
default:
break;
}
}
static ssize_t modem_intf_state_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
{
int status;
printk("modem_intf_state_store :%d : %s\n",modem_gpio_status,buf);
status = simple_strtoul(buf, NULL, 16);
//status 0, mean enter boot mode
if(0 == status) {
//status 0 means start to boot cp
modem_share_gpio_init(&(modem_intf_device->modem_config));
printk("modem_intf_open: modem_intf_send_boot_message mode = %d\n", MODEM_MODE_BOOT);
modem_intf_send_boot_message();
} else {
modem_intf_send_user_req_message(status);
}
return 0;
}
DEVICE_ATTR(state, S_IRUGO | S_IWUSR, modem_intf_state_show,modem_intf_state_store);
static int s_modem_poweron = 0;
void on_modem_poweron()
{
modem_gpio_irq_init(&(modem_intf_device->modem_config));
modemsts_change_notification(MODEM_STATUS_POWERON);
}
static ssize_t modem_intf_modempower_show(struct device *dev,struct device_attribute *attr, char *buf)
{
int count=0;
if(buf == NULL)
return 0;
printk("modempower_show: %d\n",s_modem_poweron);
if(s_modem_poweron == 0) {
snprintf(buf,4,"off");
count = 4;
} else {
snprintf(buf,3,"on");
count = 3;
}
return count;
}
static ssize_t modem_intf_modempower_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
{
int power;
if(buf == NULL)
return 0;
power = simple_strtoul(buf, NULL, 16);
if (power == 1) {
if(s_modem_poweron)
return size;
modem_poweron();
s_modem_poweron = 1;
on_modem_poweron();
} else if(power == 0) {
if(!s_modem_poweron)
return size;
modem_poweroff();
s_modem_poweron = 0;
}
return size;
}
DEVICE_ATTR(modempower, S_IRUGO | S_IWUSR, modem_intf_modempower_show,modem_intf_modempower_store);
static void modem_intf_init_poweron_timeout(unsigned long priv)
{
modem_poweron_async_step2();
s_modem_poweron = 1;
on_modem_poweron();
}
static void modem_intf_init_poweron_modem()
{
init_timer(&s_modem_watch_timer);
s_modem_watch_timer.expires = jiffies + (2 * HZ);
s_modem_watch_timer.data = (unsigned long)NULL;
s_modem_watch_timer.function = modem_intf_init_poweron_timeout;
modem_poweron_async_step1();
add_timer(&s_modem_watch_timer);
}
static ssize_t modem_intf_modemreset_store(struct device *dev,struct device_attribute *attr, const char *buf, size_t size)
{
int power;
if(buf == NULL)
return 0;
if(!s_modem_poweron)
return -1;
power = simple_strtoul(buf, NULL, 16);
if (power == 1) {
modem_soft_reset();
} else if(power == 2) {
modem_hard_reset();
}
return size;
}
DEVICE_ATTR(modemreset, S_IWUSR, NULL,modem_intf_modemreset_store);
static int modem_intf_driver_probe(struct platform_device *_dev)
{
int retval = 0;
int i;
struct modem_intf_platform_data *modem_config = _dev->dev.platform_data;
struct modem_intf_device *device;
dev_dbg(&_dev->dev, "MODEM_INTF_driver_probe(%p)\n", _dev);
device = kzalloc(sizeof(*device), GFP_KERNEL);
if (!device) {
printk("MODEM device alloc memory failed!!!\n");
retval = -ENOMEM;
goto fail;
}
memcpy(&device->modem_config,modem_config,sizeof(*modem_config));
device->send_buffer.type = BUF_SEND;
device->recv_buffer.type = BUF_RECV;
if (pingpang_buffer_init(&device->send_buffer)) {
dev_dbg(&_dev->dev, "send_buffer init failed\n");
retval = -ENOMEM;
goto fail;
}
if (pingpang_buffer_init(&device->recv_buffer)) {
dev_dbg(&_dev->dev, "receive_buffer init failed\n");
pingpang_buffer_free(&device->send_buffer);
retval = -ENOMEM;
goto fail;
}
// sprd_greg_set_bits(REG_TYPE_GLOBAL, 0x00000040, GR_PIN_CTL);
device->mode = MODEM_MODE_SHUTDOWN;
modem_intf_device = device;
modem_intf_device->op = NULL;
for(i = 0; i < CTRL_GPIO_MAX; i++) {
modem_gpio_status[i] = 0xFF;
}
retval = device_create_file(&_dev->dev, &dev_attr_state);
retval = device_create_file(&_dev->dev, &dev_attr_modempower);
retval = device_create_file(&_dev->dev, &dev_attr_modemreset);
modem_gpio_init(&modem_intf_device->modem_config);
modem_intf_register_device_operation(modem_sdio_drv_init());
modem_event = MODEM_INTF_EVENT_SHUTDOWN;
sema_init(&modem_event_sem,1);
spin_lock_init(&int_lock);
modem_intf_init_poweron_modem();
return 0;
fail:
kfree(device);
modem_intf_device = NULL;
return retval;
}
static void modem_intf_driver_shutdown(struct platform_device *_dev)
{
unsigned long timeout = jiffies + 30 * HZ;
//wait downloading cp finished
printk(KERN_ERR "modem_intf: modem_intf_driver_shutdown enter!\n" );
while(time_after(timeout, jiffies)) {
if(modem_intf_device->mode == MODEM_MODE_BOOT)
{
printk(KERN_ERR "modem_intf: shutdown found booting cp ...!\n" );
msleep(1000);
} else {
break;
}
}
//send message
printk(KERN_ERR "modem_intf: shutdown modem_intf_send_shutdown_message!\n" );
modem_intf_send_shutdown_message();
//power off modem
printk(KERN_ERR "modem_intf: shutdown modem_poweroff!\n" );
modem_poweroff();
}
static struct platform_driver modem_intf_driver = {
.driver = {
.name = "modem_interface",
.owner = THIS_MODULE,
},
.probe = modem_intf_driver_probe,
.remove = modem_intf_driver_remove,
.shutdown = modem_intf_driver_shutdown,
};
static int __init init(void)
{
int retval = 0;
struct task_struct * task;
printk(KERN_INFO "MODEM_INF_DRIVER: version 0.1\n" );
//init_waitqueue_head(&mdoem_mode_normal);
sema_init(&msg_list_sem,0);
retval = init_msg_nodes();
retval = platform_driver_probe(&modem_intf_driver, modem_intf_driver_probe);
if (retval < 0) {
printk(KERN_ERR "%s retval=%d\n", __func__, retval);
}
if(retval < 0)
return retval;
wake_lock_init(&s_modem_intf_proc_wake_lock, WAKE_LOCK_SUSPEND, "modem_if_proc_lock");
wake_lock_init(&s_modem_intf_msg_wake_lock, WAKE_LOCK_SUSPEND, "modem_if_msg_lock");
wake_lock_init(&s_modem_intf_event_wake_lock, WAKE_LOCK_SUSPEND, "modem_if_evt_lock");
task = kthread_create(modem_protocol_thread, NULL, "ModemIntf");
if(0 != task)
wake_up_process(task);
return retval;
}
module_init(init);
static void __exit cleanup(void)
{
printk(KERN_DEBUG "MODEM_INTF_driver_cleanup()\n");
if (modem_intf_device) {
pingpang_buffer_free(&modem_intf_device->send_buffer);
pingpang_buffer_free(&modem_intf_device->recv_buffer);
kfree(modem_intf_device);
modem_intf_device = NULL;
}
platform_driver_unregister(&modem_intf_driver);
printk(KERN_INFO "MODEM_INTF module removed\n" );
}
module_exit(cleanup);
|