summaryrefslogtreecommitdiff
path: root/drivers/mxc/security/sahara2/include/linux_port.h
blob: 1fb58366a6959bc35c618afe4547917abcd5ed74 (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
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
/*
 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file linux_port.h
 *
 * OS_PORT ported to Linux (2.6.9+ for now)
 *
 */

 /*!
  * @if USE_MAINPAGE
  * @mainpage ==Linux version of== Generic OS API for STC Drivers
  * @endif
  *
  * @section intro_sec Introduction
  *
  * This API / kernel programming environment blah blah.
  *
  * See @ref dkops "Driver-to-Kernel Operations" as a good place to start.
  */

#ifndef LINUX_PORT_H
#define LINUX_PORT_H

#define PORTABLE_OS_VERSION 101

/* Linux Kernel Includes */
#include <linux/version.h>	/* Current version Linux kernel */

#if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
#include <linux/modversions.h>
#endif
#define MODVERSIONS
#endif
/*!
 * __NO_VERSION__ defined due to Kernel module possibly spanning multiple
 * files.
 */
#define __NO_VERSION__

#include <linux/module.h>	/* Basic support for loadable modules,
				   printk */
#include <linux/init.h>		/* module_init, module_exit */
#include <linux/kernel.h>	/* General kernel system calls */
#include <linux/sched.h>	/* for interrupt.h */
#include <linux/fs.h>		/* for inode */
#include <linux/random.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>		/* kmalloc */

#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
#include <linux/device.h>	/* used in dynamic power management */
#else
#include <linux/platform_device.h>	/* used in dynamic power management */
#endif

#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
#include <asm/arch/clock.h>	/* clock en/disable for DPM */
#else
#include <linux/clk.h>		/* clock en/disable for DPM */
#endif

#include <linux/dmapool.h>
#include <linux/dma-mapping.h>

#include <asm/io.h>		/* ioremap() */
#include <asm/irq.h>
#include <asm/uaccess.h>	/* copy_to_user(), copy_from_user() */
#include <asm/cacheflush.h>

#ifndef TRUE
/*! Useful symbol for unsigned values used as flags. */
#define TRUE 1
#endif

#ifndef FALSE
/*! Useful symbol for unsigned values used as flags. */
#define FALSE 0
#endif

/* These symbols are defined in Linux 2.6 and later.  Include here for minimal
 * support of 2.4 kernel.
 **/
#if !defined(LINUX_VERSION_CODE) || LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
/*!
 * Symbol defined somewhere in 2.5/2.6.  It is the return signature of an ISR.
 */
#define irqreturn_t void
/*! Possible return value of 'modern' ISR routine. */
#define IRQ_HANDLED
/*! Method of generating value of 'modern' ISR routine. */
#define IRQ_RETVAL(x)
#endif

/*!
 * Type used for registering and deregistering interrupts.
 */
typedef int os_interrupt_id_t;

/*!
 * Type used as handle for a process
 *
 * See #os_get_process_handle() and #os_send_signal().
 */
/*
 * The following should be defined this way, but it gets compiler errors
 * on the current tool chain.
 *
 * typedef task_t *os_process_handle_t;
 */

#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
typedef task_t *os_process_handle_t;
#else
typedef struct task_struct *os_process_handle_t;
#endif

/*!
 * Generic return code for functions which need such a thing.
 *
 * No knowledge should be assumed of the value of any of these symbols except
 * that @c OS_ERROR_OK_S is guaranteed to be zero.
 */
typedef enum {
	OS_ERROR_OK_S = 0,	/*!< Success  */
	OS_ERROR_FAIL_S = -EIO,	/*!< Generic driver failure */
	OS_ERROR_NO_MEMORY_S = -ENOMEM,	/*!< Failure to acquire/use memory  */
	OS_ERROR_BAD_ADDRESS = -EFAULT	/*!< Bad address  */
} os_error_code;

/*!
 * Handle to a lock.
 */
#ifdef CONFIG_PREEMPT_RT
typedef raw_spinlock_t *os_lock_t;
#else
typedef spinlock_t *os_lock_t;
#endif

/*!
 * Context while locking.
 */
typedef unsigned long os_lock_context_t;

/*!
 * Declare a wait object for sleeping/waking processes.
 */
#define OS_WAIT_OBJECT(name)                                            \
    DECLARE_WAIT_QUEUE_HEAD(name##_qh)

/*!
 * Driver registration handle
 *
 * Used with #os_driver_init_registration(), #os_driver_add_registration(),
 * and #os_driver_complete_registration().
 */
typedef struct {
	unsigned reg_complete;	/*!< TRUE if next inits succeeded. */
	dev_t dev;		/*!< dev_t for register_chrdev() */
	struct file_operations fops;	/*!< struct for register_chrdev() */
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
	struct class_simple *cs;	/*!< results of class_simple_create() */
#else
	struct class *cs;	/*!< results of class_create() */
#endif
	struct class_device *cd;	/*!< Result of class_simple_device_add() */
	unsigned power_complete;	/*!< TRUE if next inits succeeded */
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
	struct device_driver dd;	/*!< struct for register_driver() */
#else
	struct platform_driver dd;	/*!< struct for register_driver() */
#endif
	struct platform_device pd;	/*!< struct for platform_register_device() */
} os_driver_reg_t;

/*
 *  Function types which can be associated with driver entry points.
 *
 *  Note that init and shutdown are absent.
 */
/*! @{ */
/*! Keyword for registering open() operation handler. */
#define OS_FN_OPEN open
/*! Keyword for registering close() operation handler. */
#define OS_FN_CLOSE release
/*! Keyword for registering read() operation handler. */
#define OS_FN_READ read
/*! Keyword for registering write() operation handler. */
#define OS_FN_WRITE write
/*! Keyword for registering ioctl() operation handler. */
#define OS_FN_IOCTL ioctl
/*! Keyword for registering mmap() operation handler. */
#define OS_FN_MMAP mmap
/*! @} */

/*!
 * Function signature for the portable interrupt handler
 *
 * While it would be nice to know which interrupt is being serviced, the
 * Least Common Denominator rule says that no arguments get passed in.
 *
 * @return  Zero if not handled, non-zero if handled.
 */
typedef int (*os_interrupt_handler_t) (int, void *);

/*!
 * @defgroup dkops Driver-to-Kernel Operations
 *
 * These are the operations which drivers should call to get the OS to perform
 * services.
 */

/*! @addtogroup dkops */
/*! @{ */

/*!
 * Register an interrupt handler.
 *
 * @param driver_name    The name of the driver
 * @param interrupt_id   The interrupt line to monitor (type
 *                       #os_interrupt_id_t)
 * @param function       The function to be called to handle an interrupt
 *
 * @return #os_error_code
 */
#define os_register_interrupt(driver_name, interrupt_id, function)            \
     request_irq(interrupt_id, function, 0, driver_name, NULL)

/*!
 * Deregister an interrupt handler.
 *
 * @param interrupt_id   The interrupt line to stop monitoring
 *
 * @return #os_error_code
 */
#define os_deregister_interrupt(interrupt_id)                                 \
     free_irq(interrupt_id, NULL)

/*!
 * INTERNAL implementation of os_driver_init_registration()
 *
 * @return An os error code.
 */
inline static int os_drv_do_init_reg(os_driver_reg_t * handle)
{
	memset(handle, sizeof(*handle), 0);
	handle->fops.owner = THIS_MODULE;
	handle->power_complete = FALSE;
	handle->reg_complete = FALSE;
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
	handle->dd.name = NULL;
#else
	handle->dd.driver.name = NULL;
#endif

	return OS_ERROR_OK_S;
}

/*!
 * Initialize driver registration.
 *
 * If the driver handles open(), close(), ioctl(), read(), write(), or mmap()
 * calls, then it needs to register their location with the kernel so that they
 * get associated with the device.
 *
 * @param handle  The handle object to be used with this registration.  The
 *                object must live (be in memory somewhere) at least until
 *                os_driver_remove_registration() is called.
 *
 * @return A handle for further driver registration, or NULL if failed.
 */
#define os_driver_init_registration(handle)                                   \
    os_drv_do_init_reg(&handle)

/*!
 * Add a function registration to driver registration.
 *
 * @param handle    A handle initialized by #os_driver_init_registration().
 * @param name      Which function is being supported.
 * @param function  The result of a call to a @c _REF version of one of the
 *                  driver function signature macros
 * @return void
 */
#define os_driver_add_registration(handle, name, function)                    \
    do {handle.fops.name = (void*)(function); } while (0)

/*!
 * Record 'power suspend' function for the device.
 *
 * @param handle    A handle initialized by #os_driver_init_registration().
 * @param function  Name of function to call on power suspend request
 *
 * Status: Provisional
 *
 * @return void
 */
#define os_driver_register_power_suspend(handle, function)                    \
   handle.dd.suspend = function

/*!
 * Record 'power resume' function for the device.
 *
 * @param handle    A handle initialized by #os_driver_init_registration().
 * @param function  Name of function to call on power resume request
 *
 * Status: Provisional
 *
 * @return void
 */
#define os_driver_register_resume(handle, function)                          \
   handle.dd.resume = function

/*!
 * INTERNAL function of the Linux port of the OS API.   Implements the
 * os_driver_complete_registration() function.
 *
 * @param handle    The handle used with #os_driver_init_registration().
 * @param major      The major device number to be associated with the driver.
 *                   If this value is zero, a major number may be assigned.
 *                   See #os_driver_get_major() to determine final value.
 *                   #os_driver_remove_registration().
 * @param driver_name The driver name.  Can be used as part of 'device node'
 *                   name on platforms which support such a feature.
 *
 * @return  An error code
 */
inline static int os_drv_do_reg(os_driver_reg_t * handle,
				unsigned major, char *driver_name)
{
	os_error_code code = OS_ERROR_NO_MEMORY_S;
	char *name = kmalloc(strlen(driver_name) + 1, 0);

	if (name != NULL) {
		memcpy(name, driver_name, strlen(driver_name) + 1);
		code = OS_ERROR_OK_S;	/* OK so far */
		/* If any chardev/POSIX routines were added, then do chrdev part */
		if (handle->fops.open || handle->fops.release
		    || handle->fops.read || handle->fops.write
		    || handle->fops.ioctl || handle->fops.mmap) {
			code =
			    register_chrdev(major, driver_name, &handle->fops);
			if (code < 0) {
				code = OS_ERROR_FAIL_S;
			} else {
				if (code != 0) {
					/* Zero was passed in for major; code is actual value */
					handle->dev = MKDEV(code, 0);
				} else {
					handle->dev = MKDEV(major, 0);
				}
				code = OS_ERROR_OK_S;
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
				handle->cs =
				    class_simple_create(THIS_MODULE,
							driver_name);
				if (IS_ERR(handle->cs)) {
					code = (os_error_code) handle->cs;
					handle->cs = NULL;
				} else {
					handle->cd =
					    class_simple_device_add(handle->cs,
								    handle->dev,
								    NULL,
								    driver_name);
					if (IS_ERR(handle->cd)) {
						class_simple_device_remove
						    (handle->dev);
						unregister_chrdev(MAJOR
								  (handle->dev),
								  driver_name);
						code =
						    (os_error_code) handle->cs;
						handle->cs = NULL;
					} else {
						handle->reg_complete = TRUE;
					}
				}
#else
				handle->cs =
				    class_create(THIS_MODULE, driver_name);
				if (IS_ERR(handle->cs)) {
					code = (os_error_code) handle->cs;
					handle->cs = NULL;
				} else {
					handle->cd =
					    class_device_create(handle->cs,
								NULL,
								handle->dev,
								NULL,
								driver_name);
					if (IS_ERR(handle->cd)) {
						class_device_destroy(handle->cs,
								     handle->
								     dev);
						class_destroy(handle->cs);
						unregister_chrdev(MAJOR
								  (handle->dev),
								  driver_name);
						code =
						    (os_error_code) handle->cs;
						handle->cs = NULL;
					} else {
						handle->reg_complete = TRUE;
					}
				}
#endif
			}
		}
		/* ... fops routine registered */
		/* Handle power management fns through separate interface */
		if ((code == OS_ERROR_OK_S) &&
		    (handle->dd.suspend || handle->dd.resume)) {
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
			handle->dd.name = name;
			handle->dd.bus = &platform_bus_type;
			code = driver_register(&handle->dd);
#else
			handle->dd.driver.name = name;
			handle->dd.driver.bus = &platform_bus_type;
			code = driver_register(&handle->dd.driver);
#endif
			if (code == OS_ERROR_OK_S) {
				handle->pd.name = name;
				handle->pd.id = 0;
				code = platform_device_register(&handle->pd);
				if (code != OS_ERROR_OK_S) {
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
					driver_unregister(&handle->dd);
#else
					driver_unregister(&handle->dd.driver);
#endif
				} else {
					handle->power_complete = TRUE;
				}
			}
		}		/* ... suspend or resume */
	}			/* name != NULL */
	return code;
}

/*!
 * Finalize the driver registration with the kernel.
 *
 * Upon return from this call, the driver may begin receiving calls at the
 * defined entry points.
 *
 * @param handle    The handle used with #os_driver_init_registration().
 * @param major      The major device number to be associated with the driver.
 *                   If this value is zero, a major number may be assigned.
 *                   See #os_driver_get_major() to determine final value.
 *                   #os_driver_remove_registration().
 * @param driver_name The driver name.  Can be used as part of 'device node'
 *                   name on platforms which support such a feature.
 *
 * @return  An error code
 */
#define os_driver_complete_registration(handle, major, driver_name)           \
   os_drv_do_reg(&handle, major, driver_name)

/*!
 * Get driver Major Number from handle after a successful registration.
 *
 * @param   handle  A handle which has completed registration.
 *
 * @return The major number (if any) associated with the handle.
 */
#define os_driver_get_major(handle)                                           \
    (handle.reg_complete ? MAJOR(handle.dev) : -1)

/*!
 * INTERNAL implemention of os_driver_remove_registration.
 *
 * @param handle    A handle initialized by #os_driver_init_registration().
 *
 * @return  An error code.
 */
inline static int os_drv_rmv_reg(os_driver_reg_t * handle)
{
	if (handle->reg_complete) {
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
		if (handle->cd != NULL) {
			class_simple_device_remove(handle->dev);
			handle->cd = NULL;
		}
		if (handle->cs != NULL) {
			class_simple_destroy(handle->cs);
			handle->cs = NULL;
		}
		unregister_chrdev(MAJOR(handle->dev), handle->dd.name);
#else
		if (handle->cd != NULL) {
			class_device_destroy(handle->cs, handle->dev);
			handle->cd = NULL;
		}
		if (handle->cs != NULL) {
			class_destroy(handle->cs);
			handle->cs = NULL;
		}
		unregister_chrdev(MAJOR(handle->dev), handle->dd.driver.name);
#endif
		handle->reg_complete = FALSE;
	}
	if (handle->power_complete) {
		platform_device_unregister(&handle->pd);
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
		driver_unregister(&handle->dd);
#else
		driver_unregister(&handle->dd.driver);
#endif
		handle->power_complete = FALSE;
	}
#if  LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
	if (handle->dd.name != NULL) {
		kfree(handle->dd.name);
		handle->dd.name = NULL;
	}
#else
	if (handle->dd.driver.name != NULL) {
		kfree(handle->dd.driver.name);
		handle->dd.driver.name = NULL;
	}
#endif
	return OS_ERROR_OK_S;
}

/*!
 * Remove the driver's registration with the kernel.
 *
 * Upon return from this call, the driver not receive any more calls at the
 * defined entry points (other than ISR and shutdown).
 *
 * @param handle    A handle initialized by #os_driver_init_registration().
 *
 * @return  An error code.
 */
#define os_driver_remove_registration(handle)                                 \
   os_drv_rmv_reg(&handle)

/*!
 * Register a driver with the Linux Device Model.
 *
 * @param   driver_information  The device_driver structure information
 *
 * @return  An error code.
 *
 * Status: denigrated in favor of #os_driver_complete_registration()
 */
#define os_register_to_driver(driver_information)                             \
              driver_register(driver_information)

/*!
 * Unregister a driver from the Linux Device Model
 *
 * this routine unregisters from the Linux Device Model
 *
 * @param   driver_information  The device_driver structure information
 *
 * @return  An error code.
 *
 * Status: Denigrated. See #os_register_to_driver().
 */
#define os_unregister_from_driver(driver_information)                         \
                driver_unregister(driver_information)

/*!
 * register a device to a driver
 *
 * this routine registers a drivers devices to the Linux Device Model
 *
 * @param   device_information  The platform_device structure information
 *
 * @return  An error code.
 *
 * Status: denigrated in favor of #os_driver_complete_registration()
 */
#define os_register_a_device(device_information)                              \
    platform_device_register(device_information)

/*!
 * unregister a device from a driver
 *
 * this routine unregisters a drivers devices from the Linux Device Model
 *
 * @param   device_information  The platform_device structure information
 *
 * @return  An error code.
 *
 * Status: Denigrated.  See #os_register_a_device().
 */
#define os_unregister_a_device(device_information)                            \
    platform_device_unregister(device_information)

/*!
 * Print a message to console / into log file.  After the @c msg argument a
 * number of printf-style arguments may be added.  Types should be limited to
 * printf string, char, octal, decimal, and hexadecimal types.  (This excludes
 * pointers, and floating point).
 *
 * @param  msg    The main text of the message to be logged
 * @param  s      The printf-style arguments which go with msg, if any
 *
 * @return (void)
 */
/* This may be a GCC-ism which needs to be ported to ANSI */
#define os_printk(msg, s...)                                                  \
    (void) printk(msg, ## s)

/*!
 * Prepare a task to execute the given function.  This should only be done once
 * per function,, during the driver's initialization routine.
 *
 * @param task_fn   Name of the OS_DEV_TASK() function to be created.
 *
 * @return an OS ERROR code.
 */
#define os_create_task(function_name)                                         \
    OS_ERROR_OK_S

/*!
 * Schedule execution of a task.
 *
 * @param function_name   The function associated with the task.
 *
 * @return (void)
 */
#define os_dev_schedule_task(function_name)                                   \
    tasklet_schedule(&(function_name ## let))

/*!
 * Make sure that task is no longer running and will no longer run.
 *
 * This function will not return until both are true.  This is useful when
 * shutting down a driver.
 */
#define os_dev_stop_task(function_name)                                       \
do {                                                                          \
    tasklet_disable(&(function_name ## let));                                 \
    tasklet_kill(&(function_name ## let));                                    \
} while (0)

/*!
 * Allocate some kernel memory
 *
 * @param amount   Number of 8-bit bytes to allocate
 * @param flags    Some indication of purpose of memory (needs definition)
 *
 * @return  Pointer to allocated memory, or NULL if failed.
 */
#define os_alloc_memory(amount, flags)                                        \
    (void*)kmalloc(amount, flags)

/*!
 * Free some kernel memory
 *
 * @param location  The beginning of the region to be freed.
 *
 * Do some OSes have separate free() functions which should be
 * distinguished by passing in @c flags here, too? Don't some also require the
 * size of the buffer being freed?
 */
#define os_free_memory(location)                                              \
    kfree(location)

/*!
 * Allocate cache-coherent memory
 *
 * @param       amount     Number of bytes to allocate
 * @param[out]  dma_addrp  Location to store physical address of allocated
 *                         memory.
 * @param       flags      Some indication of purpose of memory (needs
 *                         definition).
 *
 * @return (virtual space) pointer to allocated memory, or NULL if failed.
 *
 */
#define os_alloc_coherent(amount, dma_addrp, flags)                           \
    (void*)dma_alloc_coherent(NULL, amount, dma_addrp, flags)

/*!
 * Free cache-coherent memory
 *
 * @param       size       Number of bytes which were allocated.
 * @param       virt_addr  Virtual(kernel) address of memory.to be freed, as
 *                         returned by #os_alloc_coherent().
 * @param       dma_addr   Physical address of memory.to be freed, as returned
 *                         by #os_alloc_coherent().
 *
 * @return void
 *
 */
#define os_free_coherent(size, virt_addr, dma_addr)                           \
    dma_free_coherent(NULL, size, virt_addr, dma_addr

/*!
 * Map an I/O space into kernel memory space
 *
 * @param start       The starting address of the (physical / io space) region
 * @param range_bytes The number of bytes to map
 *
 * @return A pointer to the mapped area, or NULL on failure
 */
#define os_map_device(start, range_bytes)                                     \
    (void*)ioremap_nocache((start), range_bytes)

/*!
 * Unmap an I/O space from kernel memory space
 *
 * @param start       The starting address of the (virtual) region
 * @param range_bytes The number of bytes to unmap
 *
 * @return None
 */
#define os_unmap_device(start, range_bytes)                                   \
    iounmap((void*)(start))

/*!
 * Copy data from Kernel space to User space
 *
 * @param to   The target location in user memory
 * @param from The source location in kernel memory
 * @param size The number of bytes to be copied
 *
 * @return #os_error_code
 */
#define os_copy_to_user(to, from, size)                                       \
    ((copy_to_user(to, from, size) == 0) ? 0 : OS_ERROR_BAD_ADDRESS)

/*!
 * Copy data from User space to Kernel space
 *
 * @param to   The target location in kernel memory
 * @param from The source location in user memory
 * @param size The number of bytes to be copied
 *
 * @return #os_error_code
 */
#define os_copy_from_user(to, from, size)                                     \
    ((copy_from_user(to, from, size) == 0) ? 0 : OS_ERROR_BAD_ADDRESS)

/*!
 * Read a 8-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @return                  The value in the register
 */
#define os_read8(register_address)                                            \
    __raw_readb(register_address)

/*!
 * Write a 8-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @param value             The value to write into the register
 */
#define os_write8(register_address, value)                                    \
    __raw_writeb(value, register_address)

/*!
 * Read a 16-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @return                  The value in the register
 */
#define os_read16(register_address)                                           \
    __raw_readw(register_address)

/*!
 * Write a 16-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @param value             The value to write into the register
 */
#define os_write16(register_address, value)                                   \
    __raw_writew(value, (uint32_t*)(register_address))

/*!
 * Read a 32-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @return                  The value in the register
 */
#define os_read32(register_address)                                           \
    __raw_readl((uint32_t*)(register_address))

/*!
 * Write a 32-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @param value             The value to write into the register
 */
#define os_write32(register_address, value)                                   \
    __raw_writel(value, register_address)

/*!
 * Read a 64-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @return                  The value in the register
 */
#define os_read64(register_address)                                           \
     ERROR_UNIMPLEMENTED

/*!
 * Write a 64-bit device register
 *
 * @param register_address  The (bus) address of the register to write to
 * @param value             The value to write into the register
 */
#define os_write64(register_address, value)                                   \
    ERROR_UNIMPLEMENTED

/*!
 * Delay some number of microseconds
 *
 * Note that this is a busy-loop, not a suspension of the task/process.
 *
 * @param  msecs   The number of microseconds to delay
 *
 * @return void
 */
#define os_mdelay mdelay

/*!
 * Calculate virtual address from physical address
 *
 * @param pa    Physical address
 *
 * @return virtual address
 *
 * @note this assumes that addresses are 32 bits wide
 */
#define os_va __va

/*!
 * Calculate physical address from virtual address
 *
 *
 * @param va    Virtual address
 *
 * @return physical address
 *
 * @note this assumes that addresses are 32 bits wide
 */
#define os_pa __pa

#ifdef CONFIG_PREEMPT_RT
/*!
 * Allocate and initialize a lock, returning a lock handle.
 *
 * The lock state will be initialized to 'unlocked'.
 *
 * @return A lock handle, or NULL if an error occurred.
 */
inline static os_lock_t os_lock_alloc_init(void)
{
	raw_spinlock_t *lockp;
	lockp = (raw_spinlock_t *) kmalloc(sizeof(raw_spinlock_t), 0);
	if (lockp) {
		_raw_spin_lock_init(lockp);
	} else {
		printk("OS: lock init failed\n");
	}

	return lockp;
}
#else
/*!
 * Allocate and initialize a lock, returning a lock handle.
 *
 * The lock state will be initialized to 'unlocked'.
 *
 * @return A lock handle, or NULL if an error occurred.
 */
inline static os_lock_t os_lock_alloc_init(void)
{
	spinlock_t *lockp;
	lockp = (spinlock_t *) kmalloc(sizeof(spinlock_t), 0);
	if (lockp) {
		spin_lock_init(lockp);
	} else {
		printk("OS: lock init failed\n");
	}

	return lockp;
}
#endif				/* CONFIG_PREEMPT_RT */

/*!
 * Acquire a lock.
 *
 * This function should only be called from an interrupt service routine.
 *
 * @param   lock_handle  A handle to the lock to acquire.
 *
 * @return void
 */
#define os_lock(lock_handle)                                              \
   spin_lock(lock_handle)

/*!
 * Unlock a lock.  Lock must have been acquired by #os_lock().
 *
 * @param   lock_handle  A handle to the lock to unlock.
 *
 * @return void
 */
#define os_unlock(lock_handle)                                            \
   spin_unlock(lock_handle)

/*!
 * Acquire a lock in non-ISR context
 *
 * This function will spin until the lock is available.
 *
 * @param lock_handle  A handle of the lock to acquire.
 * @param context      Place to save the before-lock context
 *
 * @return void
 */
#define os_lock_save_context(lock_handle, context)                        \
    spin_lock_irqsave(lock_handle, context)

/*!
 * Release a lock in non-ISR context
 *
 * @param lock_handle  A handle of the lock to release.
 * @param context      Place where before-lock context was saved.
 *
 * @return void
 */
#define os_unlock_restore_context(lock_handle, context)                  \
    spin_unlock_irqrestore(lock_handle, context)

/*!
 * Deallocate a lock handle.
 *
 * @param lock_handle   An #os_lock_t that has been allocated.
 *
 * @return void
 */
#define os_lock_deallocate(lock_handle)                                   \
    kfree(lock_handle)

/*!
 * Determine process handle
 *
 * The process handle of the current user is returned.
 *
 * @return   A handle on the current process.
 */
#define os_get_process_handle()                                           \
    current

/*!
 * Send a signal to a process
 *
 * @param  proc   A handle to the target process.
 * @param  sig    The POSIX signal to send to that process.
 */
#define os_send_signal(proc, sig)                                         \
    send_sig(sig, proc, 0);

/*!
 * Get some random bytes
 *
 * @param buf    The location to store the random data.
 * @param count  The number of bytes to store.
 *
 * @return  void
 */
#define os_get_random_bytes(buf, count)                                   \
    get_random_bytes(buf, count)

/*!
 * Go to sleep on an object.
 *
 * @param object    The object on which to sleep
 * @param condition An expression to check for sleep completion.  Must be
 *                  coded so that it can be referenced more than once inside
 *                  macro, i.e., no ++ or other modifying expressions.
 * @param atomic    Non-zero if sleep must not return until condition.
 *
 * @return error code -- OK or sleep interrupted??
 */
#define os_sleep(object, condition, atomic)                             \
({                                                                      \
    DEFINE_WAIT(_waitentry_);                                           \
    os_error_code code = OS_ERROR_OK_S;                                 \
                                                                        \
    while (!(condition)) {                                              \
        prepare_to_wait(&(object##_qh), &_waitentry_,                   \
                        atomic ? 0 : TASK_INTERRUPTIBLE);               \
        if (!(condition)) {                                             \
            schedule();                                                 \
        }                                                               \
                                                                        \
        finish_wait(&(object##_qh), &_waitentry_);                      \
                                                                        \
        if (!atomic && signal_pending(current)) {                       \
            code = OS_ERROR_FAIL_S; /* NEED SOMETHING BETTER */         \
            break;                                                      \
        }                                                               \
    };                                                                  \
                                                                        \
    code;                                                               \
})

/*!
 * Wake up whatever is sleeping on sleep object
 *
 * @param object  The object on which things might be sleeping
 *
 * @return none
 */
#define os_wake_sleepers(object)                                        \
    wake_up_interruptible(&(object##_qh));

	  /*! @} *//* dkops */

/******************************************************************************
 *  Function signature-generating macros
 *****************************************************************************/

/*!
 * @defgroup drsigs Driver Signatures
 *
 * These macros will define the entry point signatures for interrupt handlers;
 * driver initialization and shutdown; device open/close; etc.
 *
 * There are two versions of each macro for a given Driver Entry Point.  The
 * first version is used to define a function and its implementation in the
 * driver.c file, e.g. #OS_DEV_INIT().
 *
 * The second form is used whenever a forward declaration (prototype) is
 * needed.  It has the letters @c _DCL appended to the name of the defintion
 * function, and takes only the first two arguments (driver_name and
 * function_name).  These are not otherwise mentioned in this documenation.
 *
 * There is a third form used when a reference to a function is required, for
 * instance when passing the routine as a pointer to a function.  It has the
 * letters @c _REF appended to it, and takes only the first two arguments
 * (driver_name and function_name).  These functions are not otherwise
 * mentioned in this documentation.
 *
 * (Note that these two extra forms are required because of the
 * possibility/likelihood of having a 'wrapper function' which invokes the
 * generic function with expected arguments.  An alternative would be to have a
 * generic function which isn't able to get at any arguments directly, but
 * would be equipped with macros which could get at information passed in.
 *
 * Example:
 *
 * (in a header file)
 * @code
 * OS_DEV_INIT_DCL(widget, widget_init);
 * @endcode
 *
 * (in an implementation file)
 * @code
 * OS_DEV_INIT(widget, widget_init)
 * {
 *     os_dev_init_return(TRUE);
 * }
 * @endcode
 *
 */

/*! @addtogroup drsigs */
/*! @{ */

/*!
 * Define a function which will handle device initialization
 *
 * This is tne driver initialization routine.  This is normally where the
 * part would be initialized; queues, locks, interrupts handlers defined;
 * long-term dynamic memory allocated for driver use; etc.
 *
 * @param function_name   The name of the portable initialization function.
 *
 * @return  A call to #os_dev_init_return()
 *
 */
#define OS_DEV_INIT(function_name)                                            \
module_init(function_name);                                                   \
static int __init function_name (void)

/*! Make declaration for driver init function.
 * @param function_name foo
 */
#define OS_DEV_INIT_DCL(function_name)                                        \
static int __init function_name (void);

/*!
 * Generate a function reference to the driver's init function.
 * @param function_name   Name of the OS_DEV_INIT() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_INIT_REF(function_name)                                        \
function_name

/*!
 * Define a function which will handle device shutdown
 *
 * This is the inverse of the #OS_DEV_INIT() routine.
 *
 * @param function_name   The name of the portable driver shutdown routine.
 *
 * @return  A call to #os_dev_shutdown_return()
 *
 */
#define OS_DEV_SHUTDOWN(function_name)                                        \
module_exit(function_name);                                                   \
static void function_name(void)

/*!
 * Generate a function reference to the driver's shutdown function.
 * @param function_name   Name of the OS_DEV_HUSTDOWN() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_SHUTDOWN_DCL(function_name)                                    \
static void function_name(void);

/*!
 * Generate a reference to driver's shutdown function
 * @param function_name   Name of the OS_DEV_HUSTDOWN() function.
*/

#define OS_DEV_SHUTDOWN_REF(function_name)                                    \
function_name

/*!
 * Define a function which will open the device for a user.
 *
 * @param function_name The name of the driver open() function
 *
 * @return  A call to #os_dev_open_return()
 */
#define OS_DEV_OPEN(function_name)                                            \
static int function_name(struct inode* inode_p_, struct file* file_p_)

/*!
 * Declare prototype for an open() function.
 *
 * @param function_name The name of the OS_DEV_OPEN() function.
 */
#define OS_DEV_OPEN_DCL(function_name)                                        \
OS_DEV_OPEN(function_name);

/*!
 * Generate a function reference to the driver's open() function.
 * @param function_name   Name of the OS_DEV_OPEN() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_OPEN_REF(function_name)                                        \
function_name

/*!
 * Define a function which will handle a user's ioctl() request
 *
 * @param function_name The name of the driver ioctl() function
 *
 * @return A call to #os_dev_ioctl_return()
 */
#define OS_DEV_IOCTL(function_name)                                           \
static int function_name(struct inode* inode_p_, struct file* file_p_,        \
                     unsigned int cmd_, unsigned long data_)

/*! Boo. */
#define OS_DEV_IOCTL_DCL(function_name)                                       \
OS_DEV_IOCTL(function_name);

/*!
 * Generate a function reference to the driver's ioctl() function.
 * @param function_name   Name of the OS_DEV_IOCTL() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_IOCTL_REF(function_name)                                       \
function_name

/*!
 * Define a function which will handle a user's read() request
 *
 * @param function_name The name of the driver read() function
 *
 * @return A call to #os_dev_read_return()
 */
#define OS_DEV_READ(function_name)                                            \
static ssize_t function_name(struct file* file_p_, char* user_buffer_,        \
                     size_t count_bytes_, loff_t* file_position_)

/*!
 * Declare prototype for an read() function.
 *
 * @param function_name The name of the driver read function.
 */
#define OS_DEV_READ_DCL(function_name)                                        \
OS_DEV_READ(function_name);

/*!
 * Generate a function reference to the driver's read() routine
 * @param function_name   Name of the OS_DEV_READ() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_READ_REF(function_name)                                        \
function_name

/*!
 * Define a function which will handle a user's write() request
 *
 * @param function_name The name of the driver write() function
 *
 * @return A call to #os_dev_write_return()
 */
#define OS_DEV_WRITE(function_name)                                           \
static ssize_t function_name(struct file* file_p_, char* user_buffer_,        \
                     size_t count_bytes_, loff_t* file_position_)

/*!
 * Declare prototype for an write() function.
 *
 * @param function_name The name of the driver write function.
 */
#define OS_DEV_WRITE_DCL(function_name)                                       \
OS_DEV_WRITE(function_name);

/*!
 * Generate a function reference to the driver's write() routine
 * @param function_name   Name of the OS_DEV_WRITE() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_WRITE_REF(function_name)                                       \
function_name

/*!
 * Define a function which will close the device - opposite of OS_DEV_OPEN()
 *
 * @param function_name The name of the driver close() function
 *
 * @return A call to #os_dev_close_return()
 */
#define OS_DEV_CLOSE(function_name)                                           \
static int function_name(struct inode* inode_p_, struct file* file_p_)

/*!
 * Declare prototype for an close() function
 *
 * @param function_name The name of the driver close() function.
 */
#define OS_DEV_CLOSE_DCL(function_name)                                       \
OS_DEV_CLOSE(function_name);

/*!
 * Generate a function reference to the driver's close function.
 * @param function_name   Name of the OS_DEV_CLOSE() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_CLOSE_REF(function_name)                                       \
function_name

/*!
 * Define a function which will handle an interrupt
 *
 * No arguments are available to the generic function.  It must not invoke any
 * OS functions which are illegal in a ISR.  It gets no parameters, and must
 * have a call to #os_dev_isr_return() instead of any/all return statements.
 *
 * Example:
 * @code
 * OS_DEV_ISR(widget, widget_isr, WIDGET_IRQ_NUMBER)
 * {
 *     os_dev_isr_return(1);
 * }
 * @endcode
 *
 * @param function_name The name of the driver ISR function
 *
 * @return   A call to #os_dev_isr_return()
 */
#define OS_DEV_ISR(function_name)                                             \
static irqreturn_t function_name(int N1_, void* N2_)

/*!
 * Declare prototype for an ISR function.
 *
 * @param function_name The name of the driver ISR function.
 */
#define OS_DEV_ISR_DCL(function_name)                                         \
OS_DEV_ISR(function_name);

/*!
 * Generate a function reference to the driver's interrupt service routine
 * @param function_name   Name of the OS_DEV_ISR() function.
 *
 * @return A function pointer.
 */
#define OS_DEV_ISR_REF(function_name)                                         \
function_name

/*!
 * Define a function which will operate as a background task / bottom half.
 *
 * Tasklet stuff isn't strictly limited to 'Device drivers', but leave it
 * this namespace anyway.
 *
 * @param function_name The name of this background task function
 *
 * @return A call to #os_dev_task_return()
 */
#define OS_DEV_TASK(function_name)                                            \
static void function_name(unsigned long data_)

/*!
 * Declare prototype for a background task / bottom half function
 *
 * @param function_name The name of this background task function
 */
#define OS_DEV_TASK_DCL(function_name)                                        \
OS_DEV_TASK(function_name);                                                   \
DECLARE_TASKLET(function_name ## let, function_name, 0);

/*!
 * Generate a reference to an #OS_DEV_TASK() function
 *
 * @param function_name   The name of the task being referenced.
 */
#define OS_DEV_TASK_REF(function_name)                                        \
    (function_name ## let)

	  /*! @} *//* drsigs */

/*****************************************************************************
 *  Functions/Macros for returning values from Driver Signature routines
 *****************************************************************************/

/*!
 * Return from the #OS_DEV_INIT() function
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_init_return(code)                                             \
    return code

/*!
 * Return from the #OS_DEV_SHUTDOWN() function
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_shutdown_return(code)                                         \
    return

/*!
 * Return from the #OS_DEV_ISR() function
 *
 * The function should verify that it really was supposed to be called,
 * and that its device needed attention, in order to properly set the
 * return code.
 *
 * @param code    non-zero if interrupt handled, zero otherwise.
 *
 */
#define os_dev_isr_return(code)                                              \
do {                                                                         \
    /* Unused warnings */                                                    \
    (void)N1_;                                                               \
    (void)N2_;                                                               \
                                                                             \
    return IRQ_RETVAL(code);                                                 \
} while (0)

/*!
 * Return from the #OS_DEV_OPEN() function
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_open_return(code)                                             \
do {                                                                         \
    int retcode = code;                                                      \
                                                                             \
    /* get rid of 'unused parameter' warnings */                             \
    (void)inode_p_;                                                          \
    (void)file_p_;                                                           \
                                                                             \
    return retcode;                                                          \
} while (0)

/*!
 * Return from the #OS_DEV_IOCTL() function
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_ioctl_return(code)                                            \
do {                                                                         \
    int retcode = code;                                                      \
                                                                             \
    /* get rid of 'unused parameter' warnings */                             \
    (void)inode_p_;                                                          \
    (void)file_p_;                                                           \
    (void)cmd_;                                                              \
    (void)data_;                                                             \
                                                                             \
    return retcode;                                                          \
} while (0)

/*!
 * Return from the #OS_DEV_READ() function
 *
 * @param code    Number of bytes read, or an error code to report failure.
 *
 */
#define os_dev_read_return(code)                                             \
do {                                                                         \
    ssize_t retcode = code;                                                  \
                                                                             \
    /* get rid of 'unused parameter' warnings */                             \
    (void)file_p_;                                                           \
    (void)user_buffer_;                                                      \
    (void)count_bytes_;                                                      \
    (void)file_position_;                                                    \
                                                                             \
    return retcode;                                                          \
} while (0)

/*!
 * Return from the #OS_DEV_WRITE() function
 *
 * @param code    Number of bytes written, or an error code to report failure.
 *
 */
#define os_dev_write_return(code)                                            \
do {                                                                         \
    ssize_t retcode = code;                                                  \
                                                                             \
    /* get rid of 'unused parameter' warnings */                             \
    (void)file_p_;                                                           \
    (void)user_buffer_;                                                      \
    (void)count_bytes_;                                                      \
    (void)file_position_;                                                    \
                                                                             \
    return retcode;                                                          \
} while (0)

/*!
 * Return from the #OS_DEV_CLOSE() function
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_close_return(code)                                            \
do {                                                                         \
    ssize_t retcode = code;                                                  \
                                                                             \
    /* get rid of 'unused parameter' warnings */                             \
    (void)inode_p_;                                                          \
    (void)file_p_;                                                           \
                                                                             \
    return retcode;                                                          \
} while (0)

/*!
 * Start the #OS_DEV_TASK() function
 *
 * In some implementations, this could be turned into a label for
 * the os_dev_task_return() call.
 *
 * @return none
 */
#define os_dev_task_begin()

/*!
 * Return from the #OS_DEV_TASK() function
 *
 * In some implementations, this could be turned into a sleep followed
 * by a jump back to the os_dev_task_begin() call.
 *
 * @param code    An error code to report success or failure.
 *
 */
#define os_dev_task_return(code)                                             \
do {                                                                         \
    /* Unused warnings */                                                    \
    (void)data_;                                                             \
                                                                             \
    return;                                                                  \
} while (0)

/*****************************************************************************
 *  Functions/Macros for accessing arguments from Driver Signature routines
 *****************************************************************************/

/*! @defgroup drsigargs Functions for Getting Arguments in Signature functions
 *
 */
/* @addtogroup @drsigargs */
/*! @{ */
/*!
 * Used in #OS_DEV_OPEN(), #OS_DEV_CLOSE(), #OS_DEV_IOCTL(), #OS_DEV_READ() and
 * #OS_DEV_WRITE() routines to check whether user is requesting read
 * (permission)
 */
#define os_dev_is_flag_read()                                                 \
   (file_p_->f_mode & FMODE_READ)

/*!
 * Used in #OS_DEV_OPEN(), #OS_DEV_CLOSE(), #OS_DEV_IOCTL(), #OS_DEV_READ() and
 * #OS_DEV_WRITE() routines to check whether user is requesting write
 * (permission)
 */
#define os_dev_is_flag_write()                                                \
   (file_p_->f_mode & FMODE_WRITE)

/*!
 * Used in #OS_DEV_OPEN(), #OS_DEV_CLOSE(), #OS_DEV_IOCTL(), #OS_DEV_READ() and
 * #OS_DEV_WRITE() routines to check whether user is requesting non-blocking
 * I/O.
 */
#define os_dev_is_flag_nonblock()                                             \
   (file_p_->f_flags & (O_NONBLOCK | O_NDELAY))

/*!
 * Used in #OS_DEV_OPEN() and #OS_DEV_CLOSE() to determine major device being
 * accessed.
 */
#define os_dev_get_major()                                                    \
    (imajor(inode_p_))

/*!
 * Used in #OS_DEV_OPEN() and #OS_DEV_CLOSE() to determine minor device being
 * accessed.
 */
#define os_dev_get_minor()                                                    \
    (iminor(inode_p_))

/*!
 * Used in #OS_DEV_IOCTL() to determine which operation the user wants
 * performed.
 *
 * @return Value of the operation.
 */
#define os_dev_get_ioctl_op()                                                 \
    (cmd_)

/*!
 * Used in #OS_DEV_IOCTL() to return the associated argument for the desired
 * operation.
 *
 * @return    A value which can be cast to a struct pointer or used as
 *            int/long.
 */
#define os_dev_get_ioctl_arg()                                                \
    (data_)

/*!
 * Used in OS_DEV_READ() and OS_DEV_WRITE() routines to access the requested
 * byte count.
 *
 * @return  (unsigned) a count of bytes
 */
#define os_dev_get_count()                                                    \
    ((unsigned)count_bytes_)

/*!
 * Used in OS_DEV_READ() and OS_DEV_WRITE() routines to return the pointer
 * byte count.
 *
 * @return   char* pointer to user buffer
 */
#define os_dev_get_user_buffer()                                              \
    ((void*)user_buffer_)

/*!
 * Used in OS_DEV_READ(), OS_DEV_WRITE(), and OS_DEV_IOCTL() routines to
 * get the POSIX flags field for the associated open file).
 *
 * @return The flags associated with the file.
 */
#define os_dev_get_file_flags()                                               \
    (file_p_->f_flags)

/*!
 * Set the driver's private structure associated with this file/open.
 *
 * Generally used during #OS_DEV_OPEN().  See #os_dev_get_user_private().
 *
 * @param  struct_p   The driver data structure to associate with this user.
 */
#define os_dev_set_user_private(struct_p)                                     \
    file_p_->private_data = (void*)(struct_p)

/*!
 * Get the driver's private structure associated with this file.
 *
 * May be used during #OS_DEV_OPEN(), #OS_DEV_READ(), #OS_DEV_WRITE(),
 * #OS_DEV_IOCTL(), and #OS_DEV_CLOSE().  See #os_dev_set_user_private().
 *
 * @return   The driver data structure to associate with this user.
 */
#define os_dev_get_user_private()                                             \
    ((void*)file_p_->private_data)
	   /*! @} *//* drsigargs */

/*!
 * @defgroup cacheops Cache Operations
 *
 * These functions are for synchronizing processor cache with RAM.
 */
/*! @addtogroup cacheops */
/*! @{ */

/*!
 * Flush and invalidate all cache lines.
 */
#if 0
#define os_flush_cache_all()                                              \
    flush_cache_all()
#else
/* Call ARM fn directly, in case L2cache=on3 not set */
#define os_flush_cache_all()                                              \
    v6_flush_kern_cache_all_L2()

/*!
 * ARM-routine to flush all cache.  Defined here, because it exists in no
 * easy-access header file.  ARM-11 with L210 cache only!
 */
extern void v6_flush_kern_cache_all_L2(void);
#endif

/*
 *  These macros are using part of the Linux DMA API.  They rely on the
 *  map function to do nothing more than the equivalent clean/inv/flush
 *  operation at the time of the mapping, and do nothing at an unmapping
 *  call, which the Sahara driver code will never invoke.
 */

/*!
 * Clean a range of addresses from the cache.  That is, write updates back
 * to (RAM, next layer).
 *
 * @param  start    Starting virtual address
 * @param  len      Number of bytes to flush
 *
 * @return void
 */
#define os_cache_clean_range(start,len)                                   \
    dma_map_single(NULL, (void*)start, len, DMA_TO_DEVICE)

/*!
 * Invalidate a range of addresses in the cache
 *
 * @param  start    Starting virtual address
 * @param  len      Number of bytes to flush
 *
 * @return void
 */
#define os_cache_inv_range(start,len)                                     \
    dma_map_single(NULL, (void*)start, len, DMA_FROM_DEVICE)

/*!
 * Flush a range of addresses from the cache.  That is, perform clean
 * and invalidate
 *
 * @param  start    Starting virtual address
 * @param  len      Number of bytes to flush
 *
 * @return void
 */
#define os_cache_flush_range(start,len)                                   \
    dma_map_single(NULL, (void*)start, len, DMA_BIDIRECTIONAL)

	  /*! @} *//* cacheops */

#endif				/* LINUX_PORT_H */