summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/notify_tesladriver/notify_tesla.c
blob: 041b307977256eb3917a7aaeac9821432be84094 (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
/*
 * notify_mbxDriver.c
 *
 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
 *
 * Copyright (C) 2008-2009 Texas Instruments, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */


#include <linux/spinlock.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/io.h>
#include <linux/module.h>
#include <mach/mailbox.h>

#include <syslink/notify_driver.h>
#include <syslink/notifydefs.h>
#include <syslink/notify_driverdefs.h>
#include <syslink/notify_tesladriver.h>
#include <syslink/gt.h>
#include <syslink/atomic_linux.h>



#define NOTIFYSHMDRV_MEM_ALIGN       0

#define NOTIFYSHMDRV_MAX_EVENTS      32

#define NOTIFYSHMDRV_INIT_STAMP      0xA9C8B7D6

#define NOTIFYNONSHMDRV_MAX_EVENTS      1

#define NOTIFYNONSHMDRV_RESERVED_EVENTS      1

#define NOTIFYDRV_TESLA_RECV_MBX     1

#define NOTIFYDRV_TESLA_SEND_MBX     0

#define SELF_ID    0

#define OTHER_ID   1

#define UP    1

#define DOWN  0

#define PROC_TESLA  0
#define PROC_DUCATI 1
#define PROC_GPP    2

/*FIXME MOVE THIS TO  A SUITABLE HEADER */
#define NOTIFYDRIVERSHM_MODULEID           (u32) 0xb9d4

/* Macro to make a correct module magic number with refCount */
#define NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(x) \
				((NOTIFYDRIVERSHM_MODULEID << 12u) | (x))

irqreturn_t(*irq_handler)(int, void *, struct pt_regs *);
EXPORT_SYMBOL(irq_handler);



/*
 *  brief  NotifyDriverShm instance object.
 */
struct notify_tesladrv_object {
	struct notify_tesladrv_params params;
	short int  proc_id;
	struct notify_drv_eventlist *event_list;
	struct notify_shmdrv_ctrl *ctrl_ptr;
	struct notify_shmdrv_eventreg *reg_chart;
	struct notify_driver_object *drv_handle;
	short int  self_id;
	short int other_id;
};


/*
 *  brief   Defines the NotifyDriverShm state object, which contains all the
 *           module specific information.
 */
struct notify_tesladrv_module {
	atomic_t ref_count;
	struct notify_tesladrv_config cfg;
	struct notify_tesladrv_config def_cfg;
	struct notify_tesladrv_params def_inst_params;
	struct mutex *gate_handle;
} ;


static struct notify_tesladrv_module notify_tesladriver_state = {
	.gate_handle = NULL,
	.def_inst_params.shared_addr = 0x0,
	.def_inst_params.shared_addr_size = 0x0,
	.def_inst_params.num_events = NOTIFYSHMDRV_MAX_EVENTS,
	.def_inst_params.num_reserved_events = 3,
	.def_inst_params.send_event_poll_count = (int) -1,
	.def_inst_params.remote_proc_id = -1,
	.def_inst_params.recv_int_id = (int) -1,
	.def_inst_params.send_int_id = (int) -1
};



struct omap_mbox *tesla_mbox;
static struct notify_tesladrv_object *tesla_isr_params[NOTIFY_MAX_DRIVERS];
static void notify_tesladrv_isr(void *ntfy_msg);
static void notify_tesladrv_isr_callback(void *ref_data, void* ntfy_msg);



/*
 *This function searchs for a element the List.
 */
static void notify_tesladrv_qsearch_elem(struct list_head *list,
		struct notify_drv_eventlistner *check_obj,
		struct notify_drv_eventlistner **listener);


/*!
 *  brief      Get the default configuration for the NotifyDriverShm module.
 *
 *              This function can be called by the application to get their
 *              configuration parameter to NotifyDriverShm_setup filled in by
 *              the NotifyDriverShm module with the default parameters. If the
 *              user does not wish to make any change in the default parameters,
 *              this API is not required to be called.
 */
void notify_tesladrv_getconfig(struct notify_tesladrv_config *cfg)
{
	BUG_ON(cfg == NULL);

	if (atomic_cmpmask_and_lt(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1))
								== true)
		memcpy(cfg,
			&(notify_tesladriver_state.def_cfg),
			sizeof(struct notify_tesladrv_config));
	else
		memcpy(cfg,
		&(notify_tesladriver_state.cfg),
		sizeof(struct notify_tesladrv_config));
}
EXPORT_SYMBOL(notify_tesladrv_getconfig);




/*
 *  brief      Function to open a handle to an existing NotifyDriverShm object
 *              handling the procId.
 *
 *              This function returns a handle to an existing NotifyDriverShm
 *              instance created for this procId. It enables other entities to
 *              access and use this NotifyDriverShm instance.
 *
 */
int notify_tesladrv_open(char *driver_name,
			 struct notify_driver_object **handle_ptr)
{
	int status = 0;
	BUG_ON(driver_name == NULL);
	BUG_ON(handle_ptr == NULL);
	/* Enter critical section protection. */
	WARN_ON(mutex_lock_interruptible(notify_tesladriver_state.gate_handle)
									!= 0);
	/* Get the handle from Notify module. */
	status = notify_get_driver_handle(driver_name, handle_ptr);
	if (status < 0)
		status = -EINVAL;
	mutex_unlock(notify_tesladriver_state.gate_handle);
	return status;
}

/*
 *  brief      Function to close this handle to the NotifyDriverShm instance.
 *
 *              This function closes the handle to the NotifyDriverShm instance
 *              obtained through NotifyDriverShm_open call made earlier.
 */
int notify_tesladrv_close(struct notify_driver_object **handle_ptr)
{
	int status = 0;
	BUG_ON(handle_ptr == NULL);
	BUG_ON(*handle_ptr == NULL);
	*handle_ptr = NULL;
	return status;
}




/*
 *  brief      Function to initialize the parameters for this NotifyDriver
 *              instance.
 *
 */
void notify_tesladrv_params_init(struct notify_driver_object *handle,
				 struct notify_tesladrv_params *params)
{
	struct notify_tesladrv_object *driver_obj;
	BUG_ON(params == NULL);

	if (atomic_cmpmask_and_lt(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1))
								== true) {
		/*FIXME not intialized to be returned */
		BUG_ON(1);
	}
	if (handle == NULL) {
		memcpy(params,
			&(notify_tesladriver_state.def_inst_params),
			sizeof(struct notify_tesladrv_params));
	} else {
		/* Return updated NotifyDriverShm instance
		specific parameters. */
		driver_obj = (struct notify_tesladrv_object *)
				handle->driver_object;
		memcpy(params, &(driver_obj->params),
			sizeof(struct notify_tesladrv_params));
	}
}
EXPORT_SYMBOL(notify_tesladrv_params_init);



/*
 *  brief      Function to create an instance of this NotifyDriver.
 *
 */
struct notify_driver_object *notify_tesladrv_create(char *driver_name,
		const struct notify_tesladrv_params *params) {

	int status = 0;
	struct notify_tesladrv_object *driver_obj = NULL;
	struct notify_driver_object *drv_handle = NULL;
	struct notify_drv_eventlist *event_list = NULL;
	volatile struct notify_shmdrv_proc_ctrl *ctrl_ptr = NULL;
	struct notify_driver_attrs drv_attrs;
	struct notify_interface fxn_table;
	int i;
	unsigned long int num_events = NOTIFYNONSHMDRV_MAX_EVENTS;
	int slot = false;

	BUG_ON(driver_name == NULL);
	BUG_ON(params == NULL);

	if (atomic_cmpmask_and_lt(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1))
								== true) {
		/*FIXME not intialized to be returned */
		goto func_end;
	}

	if (params->num_events > NOTIFYSHMDRV_MAX_EVENTS) {
		status = -EINVAL;
		goto func_end;
	} else {

		if (mutex_lock_interruptible(notify_tesladriver_state.
					gate_handle) != 0)
			WARN_ON(1);

		drv_attrs.numProc = 1;
		drv_attrs.proc_info[PROC_TESLA].max_events = num_events;

		drv_attrs.proc_info[PROC_TESLA].reserved_events =
			params->num_reserved_events;
		/* 32-bit payload supported. */
		drv_attrs.proc_info[PROC_TESLA].payload_size = sizeof(int);
		drv_attrs.proc_info[PROC_TESLA].proc_id =
			params->remote_proc_id;

		/* Function table information */
		fxn_table.register_event = (void *)
					&notify_tesladrv_register_event;
		fxn_table.unregister_event = (void *)
					&notify_tesladrv_unregister_event;
		fxn_table.send_event = (void *)&notify_tesladrv_sendevent;
		fxn_table.disable = (void *)&notify_tesladrv_disable;
		fxn_table.restore = (void *)&notify_tesladrv_restore;
		fxn_table.disable_event = (void *)
					&notify_tesladrv_disable_event;
		fxn_table.enable_event = (void *)&notify_tesladrv_enable_event;

		status = notify_register_driver(driver_name,
						&fxn_table,
						&drv_attrs,
						&drv_handle);

		if (status != NOTIFY_SUCCESS) {
			/*retval NULL Failed to register
			driver with Notify module*/
			printk(KERN_ERR "notify_register_driver"
					" failed and status = %d\n",
					status);
			status = -EINVAL;
			goto func_end;
		} else {

			/* Allocate memory for the
			NotifyDriverShm_Object object. */
			drv_handle->driver_object = driver_obj =
				kmalloc(sizeof(struct notify_tesladrv_object),
						GFP_ATOMIC);

			if (driver_obj == NULL) {
				status = -ENOMEM;
				goto func_end;
			} else {
				memcpy(&(driver_obj->params), (void *) params,
					sizeof(struct notify_tesladrv_params));

			}
		}

	}
	/*FIXME: Check with MultiProc_getId */
	driver_obj->self_id  = 1;
	driver_obj->other_id = 0;
	driver_obj->ctrl_ptr = (struct notify_shmdrv_ctrl *)
					params->shared_addr;


	ctrl_ptr = &(driver_obj->ctrl_ptr->proc_ctrl[driver_obj->self_id]);


	ctrl_ptr->self_event_chart = (struct notify_shmdrv_event_entry *)
					((int)(driver_obj->ctrl_ptr)
					+ sizeof(struct notify_shmdrv_ctrl)+
					(sizeof(
					struct notify_shmdrv_event_entry)
					* params->num_events
					* driver_obj->other_id));


	ctrl_ptr->other_event_chart = (struct notify_shmdrv_event_entry *)
					((int)(driver_obj->ctrl_ptr)
					+ sizeof(struct notify_shmdrv_ctrl) +
					(sizeof(
					struct notify_shmdrv_event_entry)
					* params->num_events
					* driver_obj->self_id));

	driver_obj->proc_id = params->remote_proc_id;


	driver_obj->event_list = kmalloc((sizeof(struct notify_drv_eventlist)
					 * params->num_events), GFP_ATOMIC);

	if (driver_obj->event_list == NULL) {

		status = -ENOMEM;
		goto func_end;
	} else {
		memset(driver_obj->event_list, 0,
			sizeof(struct notify_drv_eventlist)*params->num_events);
	}


	driver_obj->reg_chart = kmalloc(sizeof(struct notify_shmdrv_eventreg)
				       *params->num_events, GFP_ATOMIC);


	if (driver_obj->reg_chart == NULL) {

		status = -ENOMEM;
		goto func_end;
	} else {
		memset(driver_obj->reg_chart, 0,
			sizeof(struct notify_shmdrv_eventreg)
			*params->num_events);
	}


	event_list = driver_obj->event_list;
	for (i = 0 ; (i < params->num_events) ; i++) {

		ctrl_ptr->self_event_chart[i].flag = 0;

		driver_obj->reg_chart[i].reg_event_no = (int) -1;

		event_list[i].event_handler_count = 0;
		INIT_LIST_HEAD(&event_list[i].listeners);
	}


	ctrl_ptr->reg_mask.mask = 0x0;

	/* Enable all events initially.*/
	ctrl_ptr->reg_mask.enable_mask = 0xFFFFFFFF;

	tesla_mbox = omap_mbox_get("mailbox-1");
	if (tesla_mbox == NULL) {
		status = -ENODEV;
		goto func_end;
	}
	tesla_mbox->rxq->callback = (int (*)(void *))notify_tesladrv_isr;

	/*Set up the ISR on the Modena-Tesla FIFO */

	for (i = 0; i < NOTIFY_MAX_DRIVERS; i++) {
		if (tesla_isr_params[i] == NULL) {
			slot = true;
			break;
		}
	}

	if ((!slot) || (i == NOTIFY_MAX_DRIVERS)) {
		/*FIXME: set a proper error value */
		printk(KERN_ERR "Error: no free slots\n");
		status = -ENODEV;
		goto func_end;
	}
	tesla_isr_params[i] = (void *)driver_obj;
	omap_mbox_enable_irq(tesla_mbox, IRQ_RX);


	/*Set up the ISR on the Modena-Ducati FIFO */
	status = 0;


	if (status == 0) {
		driver_obj = drv_handle->driver_object;
		ctrl_ptr->reg_mask.mask = 0x0;
		ctrl_ptr->reg_mask.enable_mask = 0xFFFFFFFF;
		ctrl_ptr->recv_init_status = NOTIFYSHMDRV_INIT_STAMP;
		ctrl_ptr->send_init_status = NOTIFYSHMDRV_INIT_STAMP;
		drv_handle->is_init = NOTIFY_DRIVERINITSTATUS_DONE;
	}



func_end:
	mutex_unlock(notify_tesladriver_state.gate_handle);
	return drv_handle;
}
EXPORT_SYMBOL(notify_tesladrv_create);



/*
 *  brief      Function to delete the instance of tesla driver
 *
 */
int notify_tesladrv_delete(struct notify_driver_object **handlePtr)
{
	int status    = 0;
	struct notify_driver_object  *drv_handle = NULL;
	struct notify_tesladrv_object *driver_obj = NULL;
	struct notify_drv_eventlist *event_list;
	short int i;
	int proc_id;

	if (atomic_cmpmask_and_lt(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1))
								== true) {
		/*FIXME not intialized to be returned */
		return -1;
	}

	WARN_ON(handlePtr == NULL);
	if (handlePtr == NULL)
		return -1;
	driver_obj = (struct notify_tesladrv_object *)
			(*handlePtr)->driver_object;
	drv_handle = (*handlePtr);
	WARN_ON((*handlePtr)->driver_object == NULL);

	/*Uninstall the ISRs & Disable the Mailbox interrupt.*/


	if (drv_handle != NULL) {
		status = notify_unregister_driver(drv_handle);
		driver_obj = drv_handle->driver_object;
	}
	if (status != NOTIFY_SUCCESS)
		printk(KERN_WARNING "driver is not registerd\n");

	if (driver_obj != NULL) {
		if (driver_obj->ctrl_ptr != NULL) {
			/* Clear initialization status in shared memory. */
			driver_obj->ctrl_ptr->
				proc_ctrl[driver_obj->self_id].
				recv_init_status = 0x0;
			driver_obj->ctrl_ptr->
				proc_ctrl[driver_obj->self_id].
				send_init_status = 0x0;
			driver_obj->ctrl_ptr = NULL;
		}

		event_list = driver_obj->event_list;
		if (event_list != NULL) {
			/* Check if lists were created. */
			for (i = 0 ; i < driver_obj->params.num_events ; i++) {
				WARN_ON(event_list[i].event_handler_count != 0);
				event_list[i].event_handler_count = 0;
				list_del((struct list_head *)
					&event_list[i].listeners);
			}

			kfree(event_list);
			driver_obj->event_list = NULL;
		}

		/* Check if reg_chart was allocated. */
		if (driver_obj->reg_chart != NULL) {
			kfree(driver_obj->reg_chart);
			driver_obj->reg_chart = NULL;
		}

		/* Disable the interrupt, Uninstall the ISR and delete it. */
		/* Check if ISR was created. */
		/*Remove the ISR on the Modena-Tesla FIFO */
		proc_id = PROC_TESLA;

		omap_mbox_disable_irq(tesla_mbox, IRQ_RX);

		if (mutex_lock_interruptible(
			notify_tesladriver_state.gate_handle) != 0)
			WARN_ON(1);

		for (i = 0; i < NOTIFY_MAX_DRIVERS; i++) {
			if (tesla_isr_params[i] == (void *)driver_obj) {
				tesla_isr_params[i] = NULL;
				break;
			}
		}
		mutex_unlock(notify_tesladriver_state.gate_handle);

		if (i == NOTIFY_MAX_DRIVERS) {
			printk(KERN_ERR "Error: No handle to delete\n");
			/*FIXME: Exit gracefully */
			WARN_ON(1);
		}

		/*Remove the generic ISR */
		omap_mbox_put(tesla_mbox);
		tesla_mbox = NULL;

		kfree(driver_obj);
		driver_obj = NULL;
	}
	return status;
}
EXPORT_SYMBOL(notify_tesladrv_delete);


/*
 *  brief      Destroy the notify tesla module.
 *
 */
int notify_tesladrv_destroy(void)
{

	int status = 0;

	if (atomic_cmpmask_and_lt(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1))
								== true) {
		/* FIXME Invalid state to be reuurned. */
		return -1;
	}

	if (atomic_dec_return(&(notify_tesladriver_state.ref_count)) ==
				NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0)) {

		/* Check if the gate_handle was created internally. */

		if (notify_tesladriver_state.gate_handle != NULL)
			kfree(notify_tesladriver_state.gate_handle);
	}
	/* FIXME- Why do we need to reset this if we already
	 *	decremented it to 0 */
	atomic_set(&(notify_tesladriver_state.ref_count),
		NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0));

	return status;
}
EXPORT_SYMBOL(notify_tesladrv_destroy);



/*
 *  brief      Setup the notify tesla module.
 *
 *              This function sets up the notify tesla module. This function
 *              must be called before any other instance-level APIs can be
 *              invoked.
 *              Module-level configuration needs to be provided to this
 *              function. If the user wishes to change some specific config
 *              parameters, then notify_tesla_get_config can be called to get
 *              the configuration filled with the default values. After this,
 *              only the required configuration values can be changed. If the
 *              user does not wish to make any change in the default parameters,
 *              the application can simply call notify_tesla_setup with NULL
 *              parameters. The default parameters would get automatically used.
 *
 */
int notify_tesladrv_setup(struct notify_tesladrv_config *cfg)
{
	int status = 0;
	int i = 0;
	struct notify_tesladrv_config tmpCfg;

	if (cfg == NULL) {
		notify_tesladrv_getconfig(&tmpCfg);
		cfg = &tmpCfg;
	}

	/* Init the ref_count to 0 */
	atomic_cmpmask_and_set(&(notify_tesladriver_state.ref_count),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0),
					NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0));

	if (atomic_inc_return(&(notify_tesladriver_state.ref_count)) !=
		NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(1u)) {
		/* FIXME Already exists status to be returned. */
		return -1;
	}

	/* Create a default gate handle here */
	notify_tesladriver_state.gate_handle =
			kmalloc(sizeof(struct mutex), GFP_KERNEL);
			mutex_init(notify_tesladriver_state.gate_handle);


	if (notify_tesladriver_state.gate_handle == NULL) {
		atomic_set(&(notify_tesladriver_state.ref_count),
				NOTIFYDRIVERSHM_MAKE_MAGICSTAMP(0));
		status = -ENOMEM;
	} else {
		memcpy(&notify_tesladriver_state.cfg,
		       cfg, sizeof(struct notify_tesladrv_config));
	}

	for (i = 0; i < NOTIFY_MAX_DRIVERS; i++)
		tesla_isr_params[i] = NULL;
	return status;
}
EXPORT_SYMBOL(notify_tesladrv_setup);


/*
* brief     Register a callback for an event with the Notify driver.
*
*/
int  notify_tesladrv_register_event(
	struct notify_driver_object *handle,
	short int  proc_id,
	int  event_no,
	fn_notify_cbck     fn_notify_cbck,
	void *cbck_arg)
{
	int status = 0;
	int firstReg = false;
	bool done = true;
	struct notify_drv_eventlistner *event_listener;
	struct notify_drv_eventlist *event_list;
	struct notify_tesladrv_object *driver_object;
	struct notify_shmdrv_eventreg *reg_chart;
	volatile struct notify_shmdrv_ctrl *ctrl_ptr;
	volatile struct notify_shmdrv_event_entry *self_event_chart;
	int i;
	int j;
	BUG_ON(handle == NULL);
	BUG_ON(handle->is_init != NOTIFY_DRIVERINITSTATUS_DONE);
	BUG_ON(handle->driver_object == NULL);
	BUG_ON(fn_notify_cbck == NULL);


	driver_object = (struct notify_tesladrv_object *) handle->driver_object;

	ctrl_ptr = driver_object->ctrl_ptr;

	/* Allocate memory for event listener. */
	event_listener = kmalloc(sizeof(struct notify_drv_eventlistner),
						GFP_ATOMIC);

	if (event_listener == NULL) {
		status = -ENOMEM;
		goto func_end;
	} else {
		memset(event_listener, 0,
			sizeof(struct notify_drv_eventlistner));
	}

	if (mutex_lock_interruptible(notify_tesladriver_state.gate_handle) != 0)
		WARN_ON(1);

	event_list = driver_object->event_list;
	WARN_ON(event_list == NULL);
	event_listener->fn_notify_cbck = fn_notify_cbck;
	event_listener->cbck_arg = cbck_arg;
	/* Check if this is the first registration for this event. */

	if (list_empty((struct list_head *)
			&event_list[event_no].listeners)) {
		firstReg = true;
		self_event_chart = ctrl_ptr->proc_ctrl[driver_object->self_id].
				 self_event_chart;
		/* Clear any pending unserviced event as there are no listeners
		* for the pending event
		*/
		self_event_chart[event_no].flag = DOWN;
	}
	list_add_tail((struct list_head *)
		&(event_listener->element),
		(struct list_head *)
		&event_list[event_no].listeners);
	event_list[event_no].event_handler_count++;


	if (firstReg == true) {
		reg_chart = driver_object->reg_chart;
		for (i = 0 ; i < driver_object->params.num_events ; i++) {
			/* Find the correct slot in the registration array. */
			if (reg_chart[i].reg_event_no == (int) -1) {
				for (j = (i - 1); j >= 0; j--) {
					if (event_no < reg_chart[j].
							reg_event_no) {
						reg_chart[j + 1].reg_event_no =
							reg_chart[j].
							reg_event_no;
						reg_chart[j + 1].reserved =
							reg_chart[j].reserved;
						i = j;
					} else {
						/* End the loop, slot found. */
						j = -1;
					}
				}

				reg_chart[i].reg_event_no = event_no;
				done = true;
				break;
			}
		}

		if (done) {
			set_bit(event_no, (unsigned long *)
				&ctrl_ptr->proc_ctrl[driver_object->self_id].
				reg_mask.mask);
		} else {
			/*! @retval NOTIFY_E_MAXEVENTS Maximum number of
			supported events have already been registered. */
			status = -EINVAL;
			list_del((struct list_head *)&
					(event_listener->element));
			kfree(event_listener);

		}
	}

func_end:
	mutex_unlock(notify_tesladriver_state.gate_handle);
	return status;
}


/*
*
* brief   unregister a callback for an event with the Notify driver.
*
*/

int notify_tesladrv_unregister_event(
		struct notify_driver_object *handle,
		short int  proc_id,
		int  event_no,
		fn_notify_cbck     fn_notify_cbck,
		void *cbck_arg)
{
	int status = 0;
	struct notify_drv_eventlistner *listener  = NULL;
	int num_events;
	struct notify_tesladrv_object *driver_object;
	struct notify_drv_eventlist *event_list;
	struct notify_shmdrv_eventreg *reg_chart;
	volatile struct notify_shmdrv_ctrl *ctrl_ptr = NULL;
	struct notify_drv_eventlistner   unreg_info;
	volatile struct notify_shmdrv_event_entry *self_event_chart;
	int i;
	int j;

	BUG_ON(fn_notify_cbck ==  NULL);
	BUG_ON(handle == NULL);
	BUG_ON(handle->driver_object == NULL);

	driver_object = (struct notify_tesladrv_object *) handle->driver_object;
	num_events = driver_object->params.num_events;

	ctrl_ptr = driver_object->ctrl_ptr;

	/* Enter critical section protection. */
	if (mutex_lock_interruptible(notify_tesladriver_state.gate_handle) != 0)
		WARN_ON(1);

	event_list = driver_object->event_list;


	unreg_info.fn_notify_cbck = fn_notify_cbck;
	unreg_info.cbck_arg = cbck_arg;
	notify_tesladrv_qsearch_elem(&event_list[event_no].listeners,
				&unreg_info,
				&listener);
	if (listener == NULL) {
		status = -EFAULT;
		goto func_end;
	}
	list_del((struct list_head *)&(listener->element));
	event_list[event_no].event_handler_count--;
	kfree(listener);


	if (list_empty((struct list_head *)
			&event_list[event_no].listeners) == true) {
		clear_bit(event_no, (unsigned long *)
				&ctrl_ptr->proc_ctrl[driver_object->self_id].
				reg_mask.mask);
		self_event_chart = ctrl_ptr->proc_ctrl[driver_object->self_id].
				   self_event_chart;
		/* Clear any pending unserviced event as there are no
		* listeners for the pending event
		*/
		self_event_chart[event_no].flag = DOWN;
		reg_chart = driver_object->reg_chart;
		for (i = 0; i < num_events; i++) {
			/* Find the correct slot in the registration array. */
			if (event_no == reg_chart[i].reg_event_no) {
				reg_chart[i].reg_event_no = (int) -1;
				for (j = (i + 1);
					(reg_chart[j].reg_event_no != (int) -1)
					&& (j != num_events); j++) {
					reg_chart[j - 1].reg_event_no =
						reg_chart[j].reg_event_no;
					reg_chart[j - 1].reserved =
						reg_chart[j].reserved;
				}
				if (j == num_events) {
					reg_chart[j - 1].reg_event_no =
								(int) -1;
				}

				break;
			}
		}
	}

func_end:
	mutex_unlock(notify_tesladriver_state.gate_handle);
	return status;
}

/*
*brief      Send a notification event to the registered users for this
*            notification on the specified processor.
*
*/
int notify_tesladrv_sendevent(struct notify_driver_object *handle,
		short int proc_id, int event_no,
		int payload, short int wait_clear)
{
	int status = 0;
	struct notify_tesladrv_object *driver_object;
	volatile struct notify_shmdrv_ctrl *ctrl_ptr;
	int max_poll_count;

	BUG_ON(handle ==  NULL);
	BUG_ON(handle->driver_object == NULL);

	driver_object = (struct notify_tesladrv_object *) handle->driver_object;
	BUG_ON(event_no > driver_object->params.num_events);

	ctrl_ptr = driver_object->ctrl_ptr;
	max_poll_count = driver_object->params.send_event_poll_count;

	omap_mbox_msg_send(tesla_mbox, payload);
	return status;
}

/*
* brief    Disable all events for this Notify driver.
*
*/
void *notify_tesladrv_disable(struct notify_driver_object *handle, u16 proc_id)
{

	omap_mbox_disable_irq(tesla_mbox, IRQ_RX);

	return NULL; /*No flags to be returned. */
}

/*
* brief    Restore the Notify driver to the state before the last disable was
*           called.
*
*/
int notify_tesladrv_restore(struct notify_driver_object *handle,
					u32 key, u16 proc_id)
{

	(void) handle;
	(void) key;
	(void) proc_id;
	/*Enable the receive interrupt for Tesla */
	omap_mbox_enable_irq(tesla_mbox, IRQ_RX);
	return 0;
}

/*
* brief    Disable a specific event for this Notify driver.
*
*/
int notify_tesladrv_disable_event(
		struct notify_driver_object *handle,
		short int  proc_id, int  event_no)
{
	static int access_count ;
	signed long int status = 0;
	struct notify_tesladrv_object *driver_object;
	BUG_ON(handle == NULL);
	BUG_ON(handle->driver_object == NULL);
	access_count++;
	driver_object = (struct notify_tesladrv_object *) handle->driver_object;
	/* Enter critical section protection. */

	WARN_ON(mutex_lock_interruptible(notify_tesladriver_state.gate_handle)
									!= 0);
	clear_bit(event_no, (unsigned long *)
		&driver_object->ctrl_ptr->proc_ctrl[driver_object->self_id].
		  reg_mask.enable_mask);
	/* Leave critical section protection. */
	mutex_unlock(notify_tesladriver_state.gate_handle);
	return status;
}

/*
* brief         Enable a specific event for this Notify driver.
*
*/
int notify_tesladrv_enable_event(struct notify_driver_object *handle,
	short int proc_id, int event_no)
{
	int status = 0;
	struct notify_tesladrv_object *driver_object;
	BUG_ON(handle == NULL);
	BUG_ON(handle->driver_object == NULL);

	driver_object = (struct notify_tesladrv_object *) handle->driver_object;
	/* Enter critical section protection. */

	WARN_ON(mutex_lock_interruptible(notify_tesladriver_state.gate_handle)
									!= 0);
	set_bit(event_no, (unsigned long *)
		&driver_object->ctrl_ptr->proc_ctrl[driver_object->self_id].
		reg_mask.enable_mask);

	mutex_unlock(notify_tesladriver_state.gate_handle);
	return status;
}

/*
* brief     Print debug information for the Notify driver.
*
*/
int notify_tesladrv_debug(struct notify_driver_object *handle)
{
	int status = 0;
	printk(KERN_WARNING "Tesla Debug: Nothing being printed currently.\n");
	return status;
}



static void notify_tesladrv_isr(void *ntfy_msg)
{
	int i = 0;
	for (i = 0; i < NOTIFY_MAX_DRIVERS; i++) {
		if (tesla_isr_params[i] != NULL) {
			notify_tesladrv_isr_callback(tesla_isr_params[i],
							ntfy_msg);
		}
	}
}
EXPORT_SYMBOL(notify_tesladrv_isr);

/*
* brief     This function implements the interrupt service routine for the
*            interrupt received from the DSP.
*
*/
static void notify_tesladrv_isr_callback(void *ref_data, void *ntfy_msg)
{
	int payload = 0;
	int i = 0;
	struct list_head *temp;
	int j = 0;
	struct notify_shmdrv_eventreg *reg_chart;
	int event_no;
	struct notify_tesladrv_object *drv_object;
	volatile struct notify_shmdrv_proc_ctrl *ctrl_ptr;
	int num_events = 0;

	drv_object = (struct notify_tesladrv_object *) ref_data;
	ctrl_ptr = &(drv_object->ctrl_ptr->proc_ctrl[drv_object->self_id]);
	reg_chart = drv_object->reg_chart;
	num_events = drv_object->params.num_events;


	/* Commented out, since this function will be called in an ISR */

	BUG_ON(drv_object == NULL);


	do {
		event_no = reg_chart[i].reg_event_no;
		if (event_no != (unsigned long int) -1) {
			if (test_bit(event_no, (unsigned long *)
				&ctrl_ptr->reg_mask.enable_mask) == 1) {
				payload = (int)ntfy_msg;

				temp  = drv_object->event_list[event_no].
					listeners.next;

				for (j = 0; j <
						drv_object->
						event_list[event_no].
						event_handler_count; j++) {
					if (temp > (struct list_head *)0) {
						((struct
						notify_drv_eventlistner
						*)temp)->fn_notify_cbck(
							drv_object->
							proc_id,
							event_no,
							((struct
							notify_drv_eventlistner
							*)
							temp)->cbck_arg,
							payload);
						temp = temp->next;
					}
				}
			}
			i++;
		}
	} while ((event_no != (unsigned long int) -1) && (i < num_events));
}

/*
* brief     This function searchs for a element the List.
*
*/
static void notify_tesladrv_qsearch_elem(struct list_head *list,
		struct notify_drv_eventlistner *check_obj,
		struct notify_drv_eventlistner **listener)
{
	struct list_head    *temp = NULL ;
	struct notify_drv_eventlistner *l_temp = NULL ;
	short int found = false;


	BUG_ON(list ==  NULL);
	BUG_ON(check_obj == NULL);
	WARN_ON(listener == NULL);

	if (listener != NULL)
		return;
	*listener = NULL;
	if ((check_obj != NULL)) {
		if (list_empty((struct list_head *)list) == false) {
			temp = list->next;
			while ((found == false) && (temp != NULL)) {
				l_temp =
				(struct notify_drv_eventlistner *)(temp);
				if ((l_temp->fn_notify_cbck ==
					check_obj->fn_notify_cbck) &&
					(l_temp->cbck_arg ==
							check_obj->cbck_arg)) {
					found = true;
				} else
					temp = temp->next;
			}
			if (found == true)
				*listener = l_temp;
		}
	}
	return;
}