aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/shrm/shrm_driver.c
blob: c25bf46bb166a51f92c7b27a4c295dabfe363680 (plain)
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
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
/*----------------------------------------------------------------------------*/
/* Copyright (C) ST Ericsson, 2009.                                           */
/*                                                                            */
/* This program 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 2.1 of the License, or (at your option */
/* any later version.                                                         */
/*                                                                            */
/* 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.   							      */
/*                                                                            */
/* You should have received a copy of the GNU General Public License          */
/* along with this program. If not, see <http://www.gnu.org/licenses/>.       */
/*----------------------------------------------------------------------------*/

#include <linux/err.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/smp_lock.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
#include <asm/atomic.h>
#include <linux/io.h>

#include <mach/isa_ioctl.h>
#include "shrm_driver.h"
#include "shrm_private.h"
#include "shrm_config.h"
#include <mach/shrm.h>


#ifdef CONFIG_HIGH_RES_TIMERS
#include <linux/hrtimer.h>
static struct hrtimer timer;
#endif


#define NAME "IPC_ISA"
#define ISA_DEVICES 4
/**debug functionality*/
#define ISA_DEBUG 0
#define dbg_printk(format, arg...) (ISA_DEBUG & 1) ? \
		(printk(KERN_ALERT NAME ": " format , ## arg)) : \
		({do {} while (0); })

#define ISI_MESSAGING (0)
#define RPC_MESSAGING (1)
#define AUDIO_MESSAGING (2)
#define SECURITY_MESSAGING (3)

#define SIZE_OF_FIFO (512*1024)

static unsigned char message_fifo[4][SIZE_OF_FIFO];

static unsigned char wr_isi_msg[10*1024];
static unsigned char wr_rpc_msg[10*1024];
static unsigned char wr_sec_msg[10*1024];
static unsigned char wr_audio_msg[10*1024];

/**global data*/
/*
int major:This variable is exported to user as module_param to specify
major number at load time
*/
static int major;
module_param(major, int, 0);
MODULE_PARM_DESC(major, "Major device number");
/**global fops mutex*/
static DEFINE_MUTEX(isa_lock);
/**global driver context pointer */
struct t_isa_driver_context *p_isa_context;
rx_cb common_rx;
rx_cb audio_rx;


struct shrm_dev *pshm_dev;

static void isi_receive(void *p_data, u32 n_bytes);
static void rpc_receive(void *p_data, u32 n_bytes);
static void audio_receive(void *p_data, u32 n_bytes);
static void security_receive(void *p_data, u32 n_bytes);

static void rx_common_l2msg_handler(unsigned char l2_header,
				 void *msg, unsigned int length)
{
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
	unsigned char *pdata;
#endif
    dbgprintk("++ \n");

	switch (l2_header) {

	case 0:
		isi_receive(msg, length);
	break;
	case 1:
		rpc_receive(msg, length);
	break;
	case 3:
		security_receive(msg, length);
	break;
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
	case 0xC0:
		pdata = (unsigned char *)msg;
		if ((*pdata == 0x50) || (*pdata == 0xAF))
			isi_receive(msg, length);
		else if ((*pdata == 0x0A) || (*pdata == 0xF5))
			rpc_receive(msg, length);
		else if ((*pdata == 0xFF) || (*pdata == 0x00))
			security_receive(msg, length);
	break;
#endif
	default:
	break;
	}

	dbgprintk("-- \n");
}

static void rx_audio_l2msg_handler(unsigned char l2_header,
				void *msg, unsigned int length)
{
	dbgprintk("++ \n");

	 audio_receive(msg, length);

	dbgprintk("-- \n");
}


static int __init shm_initialise_irq(struct shrm_dev *shm_dev_data)
{
	int err = 0;

	shm_protocol_init(rx_common_l2msg_handler, rx_audio_l2msg_handler);

	err = request_irq(shm_dev_data->ca_wake_irq,
			ca_wake_irq_handler, IRQF_TRIGGER_RISING,
				 "ca_wake-up", shm_dev_data);
		if (err < 0) {
			printk("Unable to allocate shm tx interrupt line\n");
			err = -EBUSY;
		free_irq(shm_dev_data->ca_wake_irq, shm_dev_data);
	}

	err = request_irq(shm_dev_data->ac_read_notif_0_irq, \
		ac_read_notif_0_irq_handler, 0, \
		"ac_read_notif_0", shm_dev_data);
	if (err < 0) {
		printk(KERN_ALERT "error ac_read_notif_0_irq interrupt line\n");
		err = -EBUSY;
		free_irq(shm_dev_data->ac_read_notif_0_irq, shm_dev_data);
	}

	err = request_irq(shm_dev_data->ac_read_notif_1_irq, \
		ac_read_notif_1_irq_handler, 0, \
		"ac_read_notif_1", shm_dev_data);
	if (err < 0) {
		printk(KERN_ALERT "error ac_read_notif_1_irq interrupt line\n");
		err = -EBUSY;
		free_irq(shm_dev_data->ac_read_notif_1_irq, shm_dev_data);
	}

	err = request_irq(shm_dev_data->ca_msg_pending_notif_0_irq, \
		 ca_msg_pending_notif_0_irq_handler, 0, \
		"ca_msg_pending_notif_0", shm_dev_data);
	if (err < 0) {
		printk(KERN_ALERT "error  ca_msg_pending_notif_0_irq line\n");
		err = -EBUSY;
		free_irq(shm_dev_data->ca_msg_pending_notif_0_irq, \
							shm_dev_data);
	}

	err = request_irq(shm_dev_data->ca_msg_pending_notif_1_irq, \
		 ca_msg_pending_notif_1_irq_handler, 0, \
		"ca_msg_pending_notif_1", shm_dev_data);
	if (err < 0) {
		printk(KERN_ALERT "error ca_msg_pending_notif_1_irq interrupt line\n");
		err = -EBUSY;
		free_irq(shm_dev_data->ca_msg_pending_notif_1_irq, \
							shm_dev_data);
	}


	return err;
}




static void free_shm_irq(struct shrm_dev *shm_dev_data)
{
	free_irq(shm_dev_data->ca_wake_irq, shm_dev_data);
	free_irq(shm_dev_data->ac_read_notif_0_irq, shm_dev_data);
	free_irq(shm_dev_data->ac_read_notif_1_irq, shm_dev_data);
	free_irq(shm_dev_data->ca_msg_pending_notif_0_irq, shm_dev_data);
	free_irq(shm_dev_data->ca_msg_pending_notif_1_irq, shm_dev_data);
}

/**
 * create_queue() - To create FIFO for Tx and Rx message buffering.
 * @q: message queue.
 * @devicetype: device type 0-isi,1-rpc,2-audio,3-security.
 *
 * This function creates a FIFO buffer of n_bytes size using
 * dma_alloc_coherent(). It also initializes all queue handling
 * locks, queue management pointers. It also initializes message list
 * which occupies this queue.
 *
 * It return -ENOMEM in case of no memory.
 */
static int create_queue(struct t_message_queue *q, u32 devicetype)
{
	q->p_fifo_base = (unsigned char *)&message_fifo[devicetype];
	q->size = SIZE_OF_FIFO;
	q->readptr = 0;
	q->writeptr = 0;
	q->no = 0;
	spin_lock_init(&q->update_lock);
	sema_init(&q->tx_mutex, 1);
	INIT_LIST_HEAD(&q->msg_list);
	init_waitqueue_head(&q->wq_readable);
	atomic_set(&q->q_rp, 0);

	return 0;
}
/**
 * delete_queue() - To delete FIFO and assiciated memory.
 * @q: message queue
 *
 * This function deletes FIFO created using create_queue() function.
 * It resets queue management pointers.
 */
static void delete_queue(struct t_message_queue *q)
{
	q->size = 0;
	q->readptr = 0;
	q->writeptr = 0;

}

/**
 * add_msg_to_queue() - Add a message inside inside queue
 *
 * @q: message queue
 * @size: size in bytes
 *
 * This function tries to allocate n_bytes of size in FIFO q.
 * It returns negative number when no memory can be allocated
 * currently.
 */
void add_msg_to_queue(struct t_message_queue *q, unsigned int size)
{
	struct t_queue_element *new_msg = NULL;

	dbgprintk("++ q->writeptr=%d \n", q->writeptr);
	new_msg = kmalloc(sizeof(struct t_queue_element),
						 GFP_KERNEL|GFP_ATOMIC);

	if (new_msg == NULL) {
		printk(KERN_ALERT ":memory overflow inside while(1) \n");
		BUG_ON(new_msg == NULL);
	}
	new_msg->offset = q->writeptr;
	new_msg->size = size;
	new_msg->no = q->no++;

	/*check for overflow condition*/
	if (q->readptr <= q->writeptr) {
		if (((q->writeptr-q->readptr) + size) >= q->size) {
			printk(KERN_ALERT "Buffer overflow**************************\n");
			BUG_ON(((q->writeptr-q->readptr) + size) >= q->size);
		}
	} else {
		if ((q->writeptr + size) >= q->readptr) {
			printk(KERN_ALERT "Buffer overflow**************************\n");
			BUG_ON((q->writeptr + size) >= q->readptr);
		}
	}


	q->writeptr = (q->writeptr + size) % q->size;

	if (list_empty(&q->msg_list)) {
		list_add_tail(&new_msg->entry, &q->msg_list);
		/*There can be 2 blocking calls read  and another select */

		atomic_set(&q->q_rp, 1);
		wake_up_interruptible(&q->wq_readable);
	} else
		list_add_tail(&new_msg->entry, &q->msg_list);


	dbgprintk("-- \n");
}

/**
 * remove_msg_from_queue() - To remove a message from the msg queue.
 *
 * @q: message queue
 *
 * This function delets a message from the message list associated with message
 * queue q and also updates read ptr.
 * If the message list is empty, then, event is set to block the select and
 * read calls of the paricular queue.
 *
 * The message list is FIFO style and message is always added to tail and
 * removed from head.
 */

void remove_msg_from_queue(struct t_message_queue *q)
{
	struct t_queue_element *old_msg = NULL;
	struct list_head *msg;

	dbgprintk("++ q->readptr %d\n", q->readptr);

	list_for_each(msg, &q->msg_list) {
		old_msg = list_entry(msg, struct t_queue_element, entry);
		if (old_msg == NULL) {
			printk(KERN_ALERT ":no message found\n");
			BUG_ON(old_msg == NULL);
		}
		break;
	}

	list_del(msg);

	q->readptr = (q->readptr + old_msg->size) % q->size;

	if (list_empty(&q->msg_list)) {
		dbgprintk("List is empty setting RP= 0\n");
		atomic_set(&q->q_rp, 0);
	}

	kfree(old_msg);

	dbgprintk("-- \n");
}

/**
 * get_size_of_new_msg() - retrieve new message from message list
 *
 * @q: message queue
 *
 * This function will retrieve most recent message from the corresponding
 * queue list. New message is always retrieved from head side.
 * It returns new message no, offset if FIFO and size.
 */
static int get_size_of_new_msg(struct t_message_queue *q)
{
	struct t_queue_element *new_msg = NULL;
	struct list_head *msg_list;

	dbgprintk("++\n");

	spin_lock_bh(&q->update_lock);
	list_for_each(msg_list, &q->msg_list) {
		new_msg = list_entry(msg_list, struct t_queue_element, entry);
		if (new_msg == NULL) {
			spin_unlock_bh(&q->update_lock);
			printk(KERN_ALERT NAME ":no message found\n");
			return -1;
		}
		break;
	}
	spin_unlock_bh(&q->update_lock);

	dbgprintk("--\n");
	return new_msg->size;
}

/**
 * isi_receive() - Rx Completion callback
 *
 * @p_data:message pointer
 * @n_bytes:message size
 *
 * This function is a callback to indicate ISI message reception is complete.
 * It updates Writeptr of the Fifo
 */
static void isi_receive(void *p_data, u32 n_bytes)
{
	u32 size = 0;
	unsigned char *psrc;
	struct t_message_queue *q;
	struct t_isadev_context *isidev = p_isa_context->p_isadev[0];

	dbgprintk("++\n");
	q = &isidev->dl_queue;
	spin_lock(&q->update_lock);
	/*Memcopy RX data first*/
	if ((q->writeptr+n_bytes) >= q->size) {
		dbgprintk("Inside Loop Back\n");
		psrc = (unsigned char *)p_data;
		size = (q->size-q->writeptr);
		/*Copy First Part of msg*/
		memcpy((q->p_fifo_base+q->writeptr), psrc, size);
		psrc += size;
		/*Copy Second Part of msg at the top of fifo*/
		memcpy(q->p_fifo_base, psrc, (n_bytes-size));
	} else {
		memcpy((q->p_fifo_base+q->writeptr), p_data, n_bytes);
	}
	add_msg_to_queue(q, n_bytes);
	spin_unlock(&q->update_lock);
	dbgprintk("--\n");
}

/**
 * rpc_receive() - Rx Completion callback
 *
 * @p_data:message pointer
 * @n_bytes:message size
 *
 * This function is a callback to indicate RPC message reception is complete.
 * It updates Writeptr of the Fifo
 */
static void rpc_receive(void *p_data, u32 n_bytes)
{
	u32 size = 0;
	unsigned char *psrc;
	struct t_message_queue *q;
	struct t_isadev_context *rpcdev = p_isa_context->p_isadev[1];

	dbgprintk("++\n");
	q = &rpcdev->dl_queue;
	spin_lock(&q->update_lock);
	/*Memcopy RX data first*/
	if ((q->writeptr+n_bytes) >= q->size) {
		psrc = (unsigned char *)p_data;
		size = (q->size-q->writeptr);
		/*Copy First Part of msg*/
		memcpy((q->p_fifo_base+q->writeptr), psrc, size);
		psrc += size;
		/*Copy Second Part of msg at the top of fifo*/
		memcpy(q->p_fifo_base, psrc, (n_bytes-size));
	} else {
		memcpy((q->p_fifo_base+q->writeptr), p_data, n_bytes);
	}

	add_msg_to_queue(q, n_bytes);
	spin_unlock(&q->update_lock);
	dbgprintk("--\n");
}

/**
 * audio_receive() - Rx Completion callback
 *
 * @p_data:message pointer
 * @n_bytes:message size
 *
 * This function is a callback to indicate audio message reception is complete.
 * It updates Writeptr of the Fifo
 */
static void audio_receive(void *p_data, u32 n_bytes)
{
	u32 size = 0;
	unsigned char *psrc;
	struct t_message_queue *q;
	struct t_isadev_context *audiodev = p_isa_context->p_isadev[2];

	dbgprintk("++\n");
	q = &audiodev->dl_queue;
	spin_lock(&q->update_lock);
	/*Memcopy RX data first*/
	if ((q->writeptr+n_bytes) >= q->size) {
		psrc = (unsigned char *)p_data;
		size = (q->size-q->writeptr);
		/*Copy First Part of msg*/
		memcpy((q->p_fifo_base+q->writeptr), psrc, size);
		psrc += size;
		/*Copy Second Part of msg at the top of fifo*/
		memcpy(q->p_fifo_base, psrc, (n_bytes-size));
	} else {
		memcpy((q->p_fifo_base+q->writeptr), p_data, n_bytes);
	}
	add_msg_to_queue(q, n_bytes);
	spin_unlock(&q->update_lock);
	dbgprintk("--\n");
}


/**
 * security_receive() - Rx Completion callback
 *
 * @p_data:message pointer
 * @n_bytes: message size
 *
 * This function is a callback to indicate security message reception
 * is complete.It updates Writeptr of the Fifo
 */
static void security_receive(void *p_data, u32 n_bytes)
{
	u32 size = 0;
	unsigned char *psrc;
	struct t_message_queue *q;
	struct t_isadev_context *secdev = p_isa_context->p_isadev[3];

	dbgprintk("++\n");
	q = &secdev->dl_queue;
	spin_lock(&q->update_lock);
	/*Memcopy RX data first*/
	if ((q->writeptr+n_bytes) >= q->size) {
		psrc = (unsigned char *)p_data;
		size = (q->size-q->writeptr);
		/*Copy First Part of msg*/
		memcpy((q->p_fifo_base+q->writeptr), psrc, size);
		psrc += size;
		/*Copy Second Part of msg at the top of fifo*/
		memcpy(q->p_fifo_base, psrc, (n_bytes-size));
	} else {
		memcpy((q->p_fifo_base+q->writeptr), p_data, n_bytes);
	}
	add_msg_to_queue(q, n_bytes);
	spin_unlock(&q->update_lock);
	dbgprintk("--\n");
}


/**
 * isa_select() - Select Interface
 *
 * @filp:file descriptor pointer
 * @wait:poll_table_struct pointer
 *
 * This function is used to perform non-blocking read operations. It allows
 * a process to determine whether it can read from one or more open files
 * without blocking. These calls can also block a process until any of a
 * given set of file descriptors becomes available for reading.
 * If a file is ready to read, POLLIN | POLLRDNORM bitmask is returned.
 * The driver method is called whenever the user-space program performs a select
 * system call involving a file descriptor associated with the driver.
 */
static unsigned int isa_select(struct file *filp, \
				struct poll_table_struct *wait)
{
	struct t_isadev_context *p_isadev;
	struct t_message_queue *q;
	unsigned int mask = 0;
	u32	m = iminor(filp->f_path.dentry->d_inode);
	dbg_printk("isa_select++\n");
	p_isadev = (struct t_isadev_context *)filp->private_data;

	if (p_isadev->device_id != m)
			return -1;

	q = &p_isadev->dl_queue;

	poll_wait(filp, &q->wq_readable, wait);

	if (atomic_read(&q->q_rp) == 1)
		mask = POLLIN | POLLRDNORM;

	dbg_printk("isa_select--\n");
	return mask;
}

/**
 * isa_read() - Read from device
 *
 * @filp:file descriptor
 * @buf:user buffer pointer
 * @len:size of requested data transfer
 * @ppos:not used
 *
 * This function is called whenever user calls read() system call.
 * It reads a oldest message from queue and copies it into user buffer and
 * returns its size.
 * If there is no message present in queue, then it blocks until new data is
 * available.
 */
static ssize_t isa_read(struct file *filp, char __user *buf,
				size_t len, loff_t *ppos)
{
	u32 size = 0;
	char *psrc;
	struct t_isadev_context *p_isadev;
	struct t_message_queue *q;
	u32 msgsize;

	dbgprintk("++\n");

	if (len <= 0)
		return -EFAULT;

	p_isadev = (struct t_isadev_context *)filp->private_data;
	q = &p_isadev->dl_queue;

	spin_lock_bh(&q->update_lock);
	if (list_empty(&q->msg_list)) {
		spin_unlock_bh(&q->update_lock);

		if (wait_event_interruptible(q->wq_readable, \
				atomic_read(&q->q_rp) == 1)) {
			return -ERESTARTSYS;
		}

	} else
		spin_unlock_bh(&q->update_lock);

	msgsize = get_size_of_new_msg(q);

	if ((q->readptr+msgsize) >= q->size) {
		dbgprintk("Inside Loop Back\n");

		psrc = (char *)buf;
		size = (q->size-q->readptr);
		/*Copy First Part of msg*/
		if (copy_to_user(psrc, \
			(unsigned char *)(q->p_fifo_base+q->readptr), size)) {
			printk(KERN_ALERT NAME ":copy_to_user failed\n");
			return -EFAULT;
		}
		psrc += size;
		/*Copy Second Part of msg at the top of fifo*/
		if (copy_to_user(psrc, \
			(unsigned char *)(q->p_fifo_base), (msgsize-size))) {
			printk(KERN_ALERT NAME ":copy_to_user failed\n");
			return -EFAULT;
		}
	} else {
		if (copy_to_user(buf, \
		(unsigned char *)(q->p_fifo_base+q->readptr), msgsize)) {
			printk(KERN_ALERT NAME ":copy_to_user failed\n");
			return -EFAULT;
		}
	}


	spin_lock_bh(&q->update_lock);
	remove_msg_from_queue(q);
	spin_unlock_bh(&q->update_lock);
	dbgprintk("--\n");
	return msgsize;
}
/**
 * isa_write() - Write to device
 *
 * @filp:file descriptor
 * @buf:user buffer pointer
 * @len:size of requested data transfer
 * @ppos:not used
 *
 * This function is called whenever user calls write() system call.
 * It checks if there is space available in queue, and copies the message
 * inside queue. If there is no space, it blocks until space becomes available.
 * It also schedules transfer thread to transmit the newly added message.
 */
static ssize_t isa_write(struct file *filp, const char __user *buf,
				 size_t len, loff_t *ppos)
{
	struct t_isadev_context *p_isadev;
	struct t_message_queue *q;
	void *addr = 0;
	unsigned char l2_header = 0;

	dbgprintk("++\n");
	if (len <= 0)
		return -EFAULT;

	p_isadev = (struct t_isadev_context *)filp->private_data;
	q = &p_isadev->dl_queue;
	down(&q->tx_mutex);

	switch (p_isadev->device_id) {
	case 0:
		dbg_printk("ISI \n");
		addr = (void *)wr_isi_msg;
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
		printk(KERN_INFO "Loopback \n");
		l2_header = 0xC0;
#else
		l2_header = p_isadev->device_id;
#endif
		break;
	case 1:
		dbg_printk("RPC \n");
		addr = (void *)wr_rpc_msg;
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
		l2_header = 0xC0;
#else
		l2_header = p_isadev->device_id;
#endif
		break;
	case 2:
		dbg_printk("Audio \n");
		addr = (void *)wr_audio_msg;
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
		l2_header = 0x80;
#else
		l2_header = p_isadev->device_id;
#endif

		break;
	case 3:
		dbg_printk("Security \n");
		addr = (void *)wr_sec_msg;
#ifdef CONFIG_U8500_SHRM_LOOP_BACK
		l2_header = 0xC0;
#else
		l2_header = p_isadev->device_id;
#endif
		break;
	default:
		dbgprintk("Wrong device \n");
		break;
	}

	if (copy_from_user(addr, buf, len)) {
		printk(KERN_ALERT NAME ":copy_from_user failed\n");
		return -EFAULT;
	}

	/*Write msg to Fifo*/
	 shm_write_msg(l2_header, addr, len);

	 /*hrtimer_start(&timer, ktime_set(0, 2*NSEC_PER_MSEC),
					HRTIMER_MODE_REL);*/

	up(&q->tx_mutex);
	dbgprintk("--\n");
	return len;
}

/**
 * isa_ioctl() - To handle different ioctl commands supported by driver.
 *
 * @inode: structure is used by the kernel internally to represent files
 * @filp:file descriptor pointer
 * @cmd:ioctl command
 * @arg:input param
 *
 * Following ioctls are supported by this driver.
 * DLP_IOCTL_ALLOCATE_BUFFER - To allocate buffer for new uplink message.
 * This ioctl is called with required message size. It returns offset for
 * the allocates space in the queue. DLP_IOCTL_PUT_MESSAGE -  To indicate
 * new uplink message available in queuq for  transmission. Message is copied
 * from offset location returned by previous ioctl before calling this ioctl.
 * DLP_IOCTL_GET_MESSAGE - To check if any downlink message is available in
 * queue. It returns  offset for new message inside queue.
 * DLP_IOCTL_DEALLOCATE_BUFFER - To deallocate any buffer allocate for
 * downlink message once the message is copied. Message is copied from offset
 * location returned by previous ioctl before calling this ioctl.
 */
static int isa_ioctl(struct inode *inode, struct file *filp,
				unsigned cmd, unsigned long arg)
{
	int err = 0;
	struct t_isadev_context *p_isadev;
	u32 m = iminor(inode);
	p_isadev = (struct t_isadev_context *)filp->private_data;

	if (p_isadev->device_id != m)
		return -1;

	switch (cmd) {
	case DLP_IOC_ALLOCATE_BUFFER:
		dbg_printk(" DLP_IOC_ALLOCATE_BUFFER++\n");
	break;
	case DLP_IOC_PUT_MESSAGE:
		dbg_printk(" DLP_IOC_PUT_MESSAGE++\n");
	break;

	case DLP_IOC_GET_MESSAGE:
		dbg_printk(" DLP_IOC_GET_MESSAGE++\n");
	break;
	case DLP_IOC_DEALLOCATE_BUFFER:
		dbg_printk(" DLP_IOC_DEALLOCATE_BUFFER++\n");
	break;
	default:
		dbg_printk("Unknown IOCTL\n");
		err = -1;
	break;
	}
	return err;
}
/**
 * isa_mmap() - Maps kernel queue memory to user space.
 *
 * @filp:file descriptor pointer
 * @vma:virtual area memory structure.
 *
 * This function maps kernel FIFO into user space. This function
 * shall be called twice to map both uplink and downlink buffers.
 */
static int isa_mmap(struct file *filp, struct vm_area_struct *vma)
{
	struct t_isadev_context *p_isadev;
	int err = 0;
	u32	m = iminor(filp->f_path.dentry->d_inode);
	dbg_printk(" isa_mmap %d++\n", m);

	p_isadev = (struct t_isadev_context *)filp->private_data;
	dbg_printk(" isa_mmap--\n");
	return err;
}

/**
 * isa_close() - Close device file
 *
 * @inode:structure is used by the kernel internally to represent files
 * @filp:device file descriptor
 *
 * This function deletes structues associated with this file, deletes
 * queues, flushes and destroys workqueus and closes this file.
 * It also unregisters itself from l2mux driver.
 */
static int isa_close(struct inode *inode, struct file *filp)
{
	struct t_isadev_context *p_isadev;
	u8 m;
	mutex_lock(&isa_lock);
	m = iminor(filp->f_path.dentry->d_inode);
	dbg_printk("isa_close %d", m);

	if (atomic_dec_and_test(&p_isa_context->is_open[m])) {
		atomic_inc(&p_isa_context->is_open[m]);
		printk(KERN_ALERT NAME ":Device not opened yet\n");
		mutex_unlock(&isa_lock);
		return -ENODEV;
	}
	atomic_set(&p_isa_context->is_open[m], 1);

	p_isadev = (struct t_isadev_context *)filp->private_data;
	dbg_printk("p_isadev->device_id %d", p_isadev->device_id);

	dbg_printk(" Closed %d device\n", m);

	if (m == ISI_MESSAGING)
		printk(KERN_ALERT "Closed ISI_MESSAGING Device\n");
	else if (m == RPC_MESSAGING)
		printk(KERN_ALERT "Closed RPC_MESSAGING Device\n");
	else if (m == AUDIO_MESSAGING)
		printk(KERN_ALERT "Closed AUDIO_MESSAGING Device\n");
	else if (m == SECURITY_MESSAGING)
		printk(KERN_ALERT "Closed SECURITY_MESSAGING Device\n");
	else
		printk(KERN_ALERT NAME ":No such device present\n");

	mutex_unlock(&isa_lock);
	return 0;
}
/**
 * isa_open() -  Open device file
 *
 * @inode: structure is used by the kernel internally to represent files
 * @filp: device file descriptor
 *
 * This function performs initialization tasks needed to open SHM channel.
 * Following tasks are performed.
 * -return if device is already opened
 * -create uplink FIFO
 * -create downlink FIFO
 * -init delayed workqueue thread
 * -register to l2mux driver
 */
static int isa_open(struct inode *inode, struct file *filp)
{
	int err = 0;
	u8 m;
	struct t_isadev_context *p_isadev;
	dbgprintk("++\n");

	if (get_boot_state() != BOOT_DONE) {
		printk(KERN_ALERT "Boot is not done \n");
		return -EBUSY;
	}
	mutex_lock(&isa_lock);
	m = iminor(inode);

	if ((m != ISI_MESSAGING) && (m != RPC_MESSAGING) && \
		(m != AUDIO_MESSAGING) && (m != SECURITY_MESSAGING)) {
		printk(KERN_ALERT NAME ":No such device present\n");
		mutex_unlock(&isa_lock);
		return -ENODEV;
	}
	if (!atomic_dec_and_test(&p_isa_context->is_open[m])) {
		atomic_inc(&p_isa_context->is_open[m]);
		printk(KERN_ALERT NAME ":Device already opened\n");
		mutex_unlock(&isa_lock);
		return -EBUSY;
	}

	if (m == ISI_MESSAGING)
		printk(KERN_ALERT "Open ISI_MESSAGING Device\n");
	else if (m == RPC_MESSAGING)
		printk(KERN_ALERT "Open RPC_MESSAGING Device\n");
	else if (m == AUDIO_MESSAGING)
		printk(KERN_ALERT "Open AUDIO_MESSAGING Device\n");
	else if (m == SECURITY_MESSAGING)
		printk(KERN_ALERT "Open SECURITY_MESSAGING Device\n");
	else
		printk(KERN_ALERT NAME ":No such device present\n");

	p_isadev = p_isa_context->p_isadev[m];
	filp->private_data = p_isadev;

	mutex_unlock(&isa_lock);
	dbgprintk("--\n");
	return err;
}


/*
 * struct shm_driver: SHRM platform structure
 * @owner:	The probe funtion to be called
 * @open:	The remove funtion to be called
 * @release:	The driver data
 * @ioctl:	The probe funtion to be called
 * @mmap:	The remove funtion to be called
 * @read:	The driver data
 * @write:	The remove funtion to be called
 * @poll:	The driver data
 */
const struct file_operations isa_fops = {
	.owner	= THIS_MODULE,
	.open 	= isa_open,
	.release = isa_close,
	.ioctl 	= isa_ioctl,
	.mmap 	= isa_mmap,
	.read 	= isa_read,
	.write 	= isa_write,
	.poll	= isa_select,
};

/**
 * isa_init() - module insertion function
 *
 * This function registers module as a character driver using
 * register_chrdev_region() or alloc_chrdev_region. It adds this
 * driver to system using cdev_add() call. Major number is dynamically
 * allocated using alloc_chrdev_region() by default or left to user to specify
 * it during load time. For this variable major is used as module_param
 * Nodes to be created using
 * mknod /dev/isi c $major 0
 * mknod /dev/rpc c $major 1
 * mknod /dev/audio c $major 2
 * mknod /dev/sec c $major 3
 */
static int isa_init(void)
{
	dev_t	dev_id;
	int	retval, no_dev;
	struct t_isadev_context *p_isadev;

	p_isa_context = kzalloc(sizeof(struct t_isa_driver_context), \
							GFP_KERNEL);
	if (p_isa_context == NULL) {
		printk(KERN_ALERT NAME ":Failed to alloc memory\n");
		return -ENOMEM;
	}

	if (major) {
		dev_id = MKDEV(major, 0);
		retval = register_chrdev_region(dev_id, ISA_DEVICES, NAME);
	} else {
		retval = alloc_chrdev_region(&dev_id, 0, ISA_DEVICES, NAME);
		major = MAJOR(dev_id);
	}

	printk(KERN_ALERT NAME ": major %d\n", major);

	cdev_init(&p_isa_context->cdev, &isa_fops);
	p_isa_context->cdev.owner = THIS_MODULE;
	retval = cdev_add(&p_isa_context->cdev, dev_id, ISA_DEVICES);
	if (retval) {
		printk(KERN_ALERT NAME ":Failed to add char device\n");
		return retval;
	}

	for (no_dev = 0; no_dev < ISA_DEVICES; no_dev++)
		atomic_set(&p_isa_context->is_open[no_dev], 1);

	for (no_dev = 0; no_dev < ISA_DEVICES; no_dev++) {

		p_isadev = kzalloc(sizeof(struct t_isadev_context), GFP_KERNEL);
		if (p_isadev == NULL) {
			printk(KERN_ALERT NAME ":Failed to alloc memory\n");
			return -ENOMEM;
		}

		p_isadev->device_id = no_dev;

		retval = create_queue(&p_isadev->dl_queue, p_isadev->device_id);
		if (retval < 0) {
			printk(KERN_ALERT NAME ":create dl_queue failed\n");
			delete_queue(&p_isadev->dl_queue);
			kfree(p_isadev);
			return retval;
		}
		p_isa_context->p_isadev[p_isadev->device_id] = p_isadev;
		sema_init(&p_isadev->tx_mutex, 1);

	}

	printk(KERN_ALERT NAME ": Driver added\n");

	return retval;
}
/**
 * isa_exit() - module removal function
 *
 * This function removes this driver from system.
 */
static void isa_exit(void)
{
	int	no_dev;
	struct t_isadev_context *p_isadev;
	dev_t dev_id = MKDEV(major, 0);

	for (no_dev = 0; no_dev < ISA_DEVICES; no_dev++) {
		p_isadev = p_isa_context->p_isadev[no_dev];
		delete_queue(&p_isadev->dl_queue);
		kfree(p_isadev);
	}

	cdev_del(&p_isa_context->cdev);
	unregister_chrdev_region(dev_id, ISA_DEVICES);
	kfree(p_isa_context);

	printk(KERN_ALERT NAME ": Driver removed\n");
}

#ifdef CONFIG_HIGH_RES_TIMERS
static enum hrtimer_restart callback(struct hrtimer *timer)
{
	return HRTIMER_NORESTART;
}
#endif


static int __init shrm_probe(struct platform_device *pdev)
{
	int err = 0;
	struct resource *res;
	struct shrm_dev *shm_dev_data = NULL;
	unsigned int *p_edge;

	if (pdev == NULL)  {
		printk(KERN_ALERT "No device/platform_data found on shm device\n");
		return -ENODEV;
	}


	shm_dev_data = kzalloc(sizeof(struct shrm_dev), GFP_KERNEL);

	pshm_dev = shm_dev_data;
	if (shm_dev_data == NULL) {
		printk(KERN_ALERT "Could not allocate memory for struct shm_dev\n");
		return -ENOMEM;
	}

	/** initialise the SHM */

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		printk(KERN_ALERT "Unable to map Ca Wake up interrupt \n");
		err = -EBUSY;
		goto rollback_intr;
	}
	shm_dev_data->ca_wake_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
	if (!res) {
		printk(KERN_ALERT "Unable to map APE_Read_notif_common IRQ base \n");
		err = -EBUSY;
		goto rollback_intr;
	}
	shm_dev_data->ac_read_notif_0_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
	if (!res) {
		printk(KERN_ALERT "Unable to map APE_Read_notif_audio IRQ base \n");
		err = -EBUSY;
		goto rollback_intr;
	}
	shm_dev_data->ac_read_notif_1_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 3);
	if (!res) {
		printk(KERN_ALERT "Unable to map Cmt_msg_pending_notif_common IRQ base \n");
		err = -EBUSY;
		goto rollback_intr;
	}
	shm_dev_data->ca_msg_pending_notif_0_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 4);
	if (!res) {
		printk(KERN_ALERT "Unable to map Cmt_msg_pending_notif_audio IRQ base \n");
		err = -EBUSY;
		goto rollback_intr;
	}
	shm_dev_data->ca_msg_pending_notif_1_irq = res->start;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		printk(KERN_ALERT "Could not get SHM IO memory information\n");
		err = -ENODEV;
		goto rollback_intr;
	}

	shm_dev_data->intr_base = (void __iomem *)ioremap_nocache(res->start, \
					res->end - res->start + 1);

	if (!(shm_dev_data->intr_base)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_intr;
	}

	shm_dev_data->ape_common_fifo_base_phy = \
			(unsigned int *)U8500_SHM_FIFO_APE_COMMON_BASE;
	shm_dev_data->ape_common_fifo_base = \
		(void __iomem *)ioremap_nocache( \
					U8500_SHM_FIFO_APE_COMMON_BASE, \
					SHM_FIFO_0_SIZE);
	shm_dev_data->ape_common_fifo_size = (SHM_FIFO_0_SIZE)/4;

	if (!(shm_dev_data->ape_common_fifo_base)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_ape_common_fifo_base;
	}

	shm_dev_data->cmt_common_fifo_base_phy = \
		(unsigned int *)U8500_SHM_FIFO_CMT_COMMON_BASE;

	shm_dev_data->cmt_common_fifo_base = \
		(void __iomem *)ioremap_nocache( \
			U8500_SHM_FIFO_CMT_COMMON_BASE, SHM_FIFO_0_SIZE);
	shm_dev_data->cmt_common_fifo_size = (SHM_FIFO_0_SIZE)/4;

	if (!(shm_dev_data->cmt_common_fifo_base)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_cmt_common_fifo_base;
	}

	shm_dev_data->ape_audio_fifo_base_phy = \
			(unsigned int *)U8500_SHM_FIFO_APE_AUDIO_BASE;
	shm_dev_data->ape_audio_fifo_base = \
		(void __iomem *)ioremap_nocache(U8500_SHM_FIFO_APE_AUDIO_BASE, \
							SHM_FIFO_1_SIZE);
	shm_dev_data->ape_audio_fifo_size = (SHM_FIFO_1_SIZE)/4;

	if (!(shm_dev_data->ape_audio_fifo_base)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_ape_audio_fifo_base;
	}

	shm_dev_data->cmt_audio_fifo_base_phy = \
			(unsigned int *)U8500_SHM_FIFO_CMT_AUDIO_BASE;
	shm_dev_data->cmt_audio_fifo_base = \
		(void __iomem *)ioremap_nocache(U8500_SHM_FIFO_CMT_AUDIO_BASE, \
							SHM_FIFO_1_SIZE);
	shm_dev_data->cmt_audio_fifo_size = (SHM_FIFO_1_SIZE)/4;

	if (!(shm_dev_data->cmt_audio_fifo_base)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_cmt_audio_fifo_base;
	}

	shm_dev_data->ac_common_shared_wptr = \
		(void __iomem *)ioremap(SHM_ACFIFO_0_WRITE_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ac_common_shared_wptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_ac_common_shared_wptr;
	}

	shm_dev_data->ac_common_shared_rptr = \
		(void __iomem *)ioremap(SHM_ACFIFO_0_READ_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ac_common_shared_rptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	shm_dev_data->ca_common_shared_wptr = \
		(void __iomem *)ioremap(SHM_CAFIFO_0_WRITE_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ca_common_shared_wptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}

	shm_dev_data->ca_common_shared_rptr = \
		(void __iomem *)ioremap(SHM_CAFIFO_0_READ_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ca_common_shared_rptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	shm_dev_data->ac_audio_shared_wptr = \
		(void __iomem *)ioremap(SHM_ACFIFO_1_WRITE_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ac_audio_shared_wptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	shm_dev_data->ac_audio_shared_rptr = \
		(void __iomem *)ioremap(SHM_ACFIFO_1_READ_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ac_audio_shared_rptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	shm_dev_data->ca_audio_shared_wptr = \
		(void __iomem *)ioremap(SHM_CAFIFO_1_WRITE_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ca_audio_shared_wptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	shm_dev_data->ca_audio_shared_rptr = \
		(void __iomem *)ioremap(SHM_CAFIFO_1_READ_AMCU, SHM_PTR_SIZE);

	if (!(shm_dev_data->ca_audio_shared_rptr)) {
		printk(KERN_ALERT "Unable to map register base \n");
		err = -EBUSY;
		goto rollback_map;
	}


	if (isa_init() != 0) {
		printk(KERN_ALERT "Driver Initialization Error \n");
		err = -EBUSY;
	}
	/* install handlers and tasklets */
	if (shm_initialise_irq(shm_dev_data)) {
		printk(KERN_ALERT "shm error in interrupt registration \n");
		goto rollback_irq;
	}

#ifdef CONFIG_HIGH_RES_TIMERS
	hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	timer.function = callback;

	hrtimer_start(&timer, ktime_set(0, 2*NSEC_PER_MSEC), HRTIMER_MODE_REL);
#endif

	return err;

rollback_irq:
	free_shm_irq(shm_dev_data);
rollback_map:
	iounmap(pshm_dev->ac_common_shared_wptr);
	iounmap(pshm_dev->ac_common_shared_rptr);
	iounmap(pshm_dev->ca_common_shared_wptr);
	iounmap(pshm_dev->ca_common_shared_rptr);
	iounmap(pshm_dev->ac_audio_shared_wptr);
	iounmap(pshm_dev->ac_audio_shared_rptr);
	iounmap(pshm_dev->ca_audio_shared_wptr);
	iounmap(pshm_dev->ca_audio_shared_rptr);
rollback_ac_common_shared_wptr:
	iounmap(pshm_dev->cmt_audio_fifo_base);
rollback_cmt_audio_fifo_base:
	iounmap(pshm_dev->ape_audio_fifo_base);
rollback_ape_audio_fifo_base:
	iounmap(pshm_dev->cmt_common_fifo_base);
rollback_cmt_common_fifo_base:
	iounmap(pshm_dev->ape_common_fifo_base);
rollback_ape_common_fifo_base:
	iounmap(pshm_dev->intr_base);
rollback_intr:
	kfree(shm_dev_data);
	return err;
}

static int __exit shrm_remove(struct platform_device *pdev)
{

	free_shm_irq(pshm_dev);
	iounmap(pshm_dev->intr_base);
	iounmap(pshm_dev->ape_common_fifo_base);
	iounmap(pshm_dev->cmt_common_fifo_base);
	iounmap(pshm_dev->ape_audio_fifo_base);
	iounmap(pshm_dev->cmt_audio_fifo_base);
	iounmap(pshm_dev->ac_common_shared_wptr);
	iounmap(pshm_dev->ac_common_shared_rptr);
	iounmap(pshm_dev->ca_common_shared_wptr);
	iounmap(pshm_dev->ca_common_shared_rptr);
	iounmap(pshm_dev->ac_audio_shared_wptr);
	iounmap(pshm_dev->ac_audio_shared_rptr);
	iounmap(pshm_dev->ca_audio_shared_wptr);
	iounmap(pshm_dev->ca_audio_shared_rptr);
	kfree(pshm_dev);
	isa_exit();

	return 0;
}

#ifdef CONFIG_PM
/**
 * u8500_shrm_suspend() - This routine puts the SHRM in to sustend state.
 * @pdev: platform device.
 *
 * This routine checks the current ongoing communication with Modem by
 * examining the ca_wake state and prevents suspend if modem communication
 * is on-going.
 * If ca_wake = 1 (high), modem comm. is on-going; don't suspend
 * If ca_wake = 0 (low), no comm. with modem on-going.Allow suspend
 */
int u8500_shrm_suspend(struct platform_device *pdev, pm_message_t state)
{
	dbgprintk("\n u8500_shrm_suspend: called...\n");
	dbgprintk("\n ca_wake_req_state = %x\n", get_ca_wake_req_state());

	/* if ca_wake_req is high, prevent system suspend */
	if (get_ca_wake_req_state())
		return -EBUSY;
	else
		return 0;
}

/**
 * u8500_shrm_resume() - This routine resumes the SHRM from sustend state.
 * @pdev: platform device.
 *
 * This routine restore back the current state of the SHRM
 */
int u8500_shrm_resume(struct platform_device *pdev)
{
	dbgprintk("\n u8500_shrm_resume: called...\n");

	/* TODO:
	 * As of now, no state save takes place in suspend.
	 * So, nothing to restore in resume.
	 * Simply return as of now.
	 * State saved in suspend should be restored here.
	 */

	return 0;
}

#else

#define u8500_shrm_suspend NULL
#define u8500_shrm_resume NULL

#endif



/*
 * struct shrm_driver: SHRM platform structure
 * @probe:	The probe funtion to be called
 * @remove:	The remove funtion to be called
 * @driver:	The driver data
 */

static struct platform_driver shrm_driver = {
	.probe = shrm_probe,
	.remove = __exit_p(shrm_remove),
	.driver = {
		.name = "u8500_shrm",
		.owner = THIS_MODULE,
	},
#ifdef CONFIG_PM
	.suspend = u8500_shrm_suspend,
	.resume = u8500_shrm_resume,
#endif
};

static int __init shrm_driver_init(void)
{
	int err = 0;

	err = platform_driver_probe(&shrm_driver, shrm_probe);
	if (err < 0) {
		printk(KERN_ALERT "SHM Platform  register FAILED: %d\n", err);
		return err;
	}
	return 0;
}

static void __exit shrm_driver_exit(void)
{
	platform_driver_unregister(&shrm_driver);

	printk(KERN_ALERT "SHM Platform DRIVER removed\n");
}

module_init(shrm_driver_init);
module_exit(shrm_driver_exit);

MODULE_AUTHOR("Biju Das");
MODULE_DESCRIPTION("Shared Memory Modem Driver Interface");
MODULE_LICENSE("GPL");