/
dwc_common_linux.c
1138 lines (967 loc) · 22.9 KB
/
dwc_common_linux.c
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
#include "dwc_cc.h"
#include "dwc_notifier.h"
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
MODULE_DESCRIPTION("DWC Common Library - Portable version");
MODULE_AUTHOR("Synopsys Inc.");
MODULE_LICENSE ("GPL");
static int dwc_common_port_init_module(void)
{
printk( KERN_DEBUG "Module dwc_common_port init\n" );
#ifdef DEBUG_MEMORY
dwc_memory_debug_start();
#endif
dwc_alloc_notification_manager();
return 0;
}
static void dwc_common_port_exit_module(void)
{
printk( KERN_DEBUG "Module dwc_common_port exit\n" );
dwc_free_notification_manager();
#ifdef DEBUG_MEMORY
dwc_memory_debug_stop();
#endif
}
module_init(dwc_common_port_init_module);
module_exit(dwc_common_port_exit_module);
/* CC */
EXPORT_SYMBOL(dwc_cc_if_alloc);
EXPORT_SYMBOL(dwc_cc_if_free);
EXPORT_SYMBOL(dwc_cc_clear);
EXPORT_SYMBOL(dwc_cc_add);
EXPORT_SYMBOL(dwc_cc_remove);
EXPORT_SYMBOL(dwc_cc_change);
EXPORT_SYMBOL(dwc_cc_data_for_save);
EXPORT_SYMBOL(dwc_cc_restore_from_data);
EXPORT_SYMBOL(dwc_cc_match_chid);
EXPORT_SYMBOL(dwc_cc_match_cdid);
EXPORT_SYMBOL(dwc_cc_ck);
EXPORT_SYMBOL(dwc_cc_chid);
EXPORT_SYMBOL(dwc_cc_cdid);
EXPORT_SYMBOL(dwc_cc_name);
/* Notification */
EXPORT_SYMBOL(dwc_alloc_notification_manager);
EXPORT_SYMBOL(dwc_free_notification_manager);
EXPORT_SYMBOL(dwc_register_notifier);
EXPORT_SYMBOL(dwc_unregister_notifier);
EXPORT_SYMBOL(dwc_add_observer);
EXPORT_SYMBOL(dwc_remove_observer);
EXPORT_SYMBOL(dwc_notify);
/* Memory Debugging Routines */
#ifdef DEBUG_MEMORY
EXPORT_SYMBOL(dwc_alloc_debug);
EXPORT_SYMBOL(dwc_alloc_atomic_debug);
EXPORT_SYMBOL(dwc_free_debug);
EXPORT_SYMBOL(dwc_dma_alloc_debug);
EXPORT_SYMBOL(dwc_dma_alloc_atomic_debug);
EXPORT_SYMBOL(dwc_dma_free_debug);
#endif
/* OS-Level Implementations */
/* This is the Linux kernel implementation of the DWC platform library. */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/jiffies.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/version.h>
#include <linux/usb.h>
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
#include <linux/usb_gadget.h>
#else
#include <linux/usb/gadget.h>
#endif
#include <linux/random.h>
#include <asm/io.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include <asm/unaligned.h>
#include <asm/page.h>
#include <linux/scatterlist.h>
/* MISC */
void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
{
return memset(dest, byte, size);
}
EXPORT_SYMBOL(DWC_MEMSET);
void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
{
return memcpy(dest, src, size);
}
EXPORT_SYMBOL(DWC_MEMCPY);
void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
{
return memmove(dest, src, size);
}
EXPORT_SYMBOL(DWC_MEMMOVE);
int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
{
return memcmp(m1, m2, size);
}
EXPORT_SYMBOL(DWC_MEMCMP);
int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
{
return strncmp(s1, s2, size);
}
EXPORT_SYMBOL(DWC_STRNCMP);
int DWC_STRCMP(void *s1, void *s2)
{
return strcmp(s1, s2);
}
EXPORT_SYMBOL(DWC_STRCMP);
int DWC_STRLEN(char const *str)
{
return strlen(str);
}
EXPORT_SYMBOL(DWC_STRLEN);
char *DWC_STRCPY(char *to, const char *from)
{
return strcpy(to, from);
}
EXPORT_SYMBOL(DWC_STRCPY);
char *DWC_STRDUP(char const *str)
{
int len = DWC_STRLEN(str) + 1;
char *new = DWC_ALLOC_ATOMIC(len);
if (!new) {
return NULL;
}
DWC_MEMCPY(new, str, len);
return new;
}
EXPORT_SYMBOL(DWC_STRDUP);
int DWC_ATOI(char *str, int32_t *value)
{
char *end = NULL;
*value = simple_strtol(str, &end, 0);
if (*end == '\0') {
return 0;
}
return -1;
}
EXPORT_SYMBOL(DWC_ATOI);
int DWC_ATOUI(char *str, uint32_t *value)
{
char *end = NULL;
*value = simple_strtoul(str, &end, 0);
if (*end == '\0') {
return 0;
}
return -1;
}
EXPORT_SYMBOL(DWC_ATOUI);
/* From usbstring.c */
int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
{
int count = 0;
u8 c;
u16 uchar;
/* this insists on correct encodings, though not minimal ones.
* BUT it currently rejects legit 4-byte UTF-8 code points,
* which need surrogate pairs. (Unicode 3.1 can use them.)
*/
while (len != 0 && (c = (u8) *s++) != 0) {
if (unlikely(c & 0x80)) {
// 2-byte sequence:
// 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
if ((c & 0xe0) == 0xc0) {
uchar = (c & 0x1f) << 6;
c = (u8) *s++;
if ((c & 0xc0) != 0xc0)
goto fail;
c &= 0x3f;
uchar |= c;
// 3-byte sequence (most CJKV characters):
// zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
} else if ((c & 0xf0) == 0xe0) {
uchar = (c & 0x0f) << 12;
c = (u8) *s++;
if ((c & 0xc0) != 0xc0)
goto fail;
c &= 0x3f;
uchar |= c << 6;
c = (u8) *s++;
if ((c & 0xc0) != 0xc0)
goto fail;
c &= 0x3f;
uchar |= c;
/* no bogus surrogates */
if (0xd800 <= uchar && uchar <= 0xdfff)
goto fail;
// 4-byte sequence (surrogate pairs, currently rare):
// 11101110wwwwzzzzyy + 110111yyyyxxxxxx
// = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
// (uuuuu = wwww + 1)
// FIXME accept the surrogate code points (only)
} else
goto fail;
} else
uchar = c;
put_unaligned (cpu_to_le16 (uchar), cp++);
count++;
len--;
}
return count;
fail:
return -1;
}
EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
/* dwc_debug.h */
dwc_bool_t DWC_IN_IRQ(void)
{
return in_irq();
}
EXPORT_SYMBOL(DWC_IN_IRQ);
int DWC_IN_BH(void)
{
return in_softirq();
}
EXPORT_SYMBOL(DWC_IN_BH);
void DWC_VPRINTF(char *format, va_list args)
{
vprintk(format, args);
}
EXPORT_SYMBOL(DWC_VPRINTF);
int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
{
return vsnprintf(str, size, format, args);
}
void DWC_PRINTF(char *format, ...)
{
va_list args;
va_start(args, format);
DWC_VPRINTF(format, args);
va_end(args);
}
EXPORT_SYMBOL(DWC_PRINTF);
int DWC_SPRINTF(char *buffer, char *format, ...)
{
int retval;
va_list args;
va_start(args, format);
retval = vsprintf(buffer, format, args);
va_end(args);
return retval;
}
EXPORT_SYMBOL(DWC_SPRINTF);
int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
{
int retval;
va_list args;
va_start(args, format);
retval = vsnprintf(buffer, size, format, args);
va_end(args);
return retval;
}
EXPORT_SYMBOL(DWC_SNPRINTF);
void __DWC_WARN(char *format, ...)
{
va_list args;
va_start(args, format);
DWC_PRINTF(KERN_WARNING);
DWC_VPRINTF(format, args);
va_end(args);
}
EXPORT_SYMBOL(__DWC_WARN);
void __DWC_ERROR(char *format, ...)
{
va_list args;
va_start(args, format);
DWC_PRINTF(KERN_ERR);
DWC_VPRINTF(format, args);
va_end(args);
}
EXPORT_SYMBOL(__DWC_ERROR);
void DWC_EXCEPTION(char *format, ...)
{
va_list args;
va_start(args, format);
DWC_PRINTF(KERN_ERR);
DWC_VPRINTF(format, args);
va_end(args);
BUG_ON(1);
}
EXPORT_SYMBOL(DWC_EXCEPTION);
#ifdef DEBUG
void __DWC_DEBUG(char *format, ...)
{
va_list args;
va_start(args, format);
DWC_PRINTF(KERN_DEBUG);
DWC_VPRINTF(format, args);
va_end(args);
}
EXPORT_SYMBOL(__DWC_DEBUG);
#endif
/* dwc_mem.h */
#if 0
dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
uint32_t align,
uint32_t alloc)
{
struct dma_pool *pool = dma_pool_create("Pool", NULL,
size, align, alloc);
return (dwc_pool_t *)pool;
}
void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
{
dma_pool_destroy((struct dma_pool *)pool);
}
void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, U64 *dma_addr)
{
return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
}
void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, U64 *dma_addr)
{
void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
memset();
}
void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
{
dma_pool_free(pool, vaddr, daddr);
}
#endif
void *__DWC_DMA_ALLOC(uint32_t size, dwc_dma_t *dma_addr)
{
void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_KERNEL);
if (!buf) {
return NULL;
}
memset(buf, 0, (size_t)size);
return buf;
}
EXPORT_SYMBOL(__DWC_DMA_ALLOC);
void __DWC_DMA_FREE(uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
{
dma_free_coherent(NULL, size, virt_addr, dma_addr);
}
EXPORT_SYMBOL(__DWC_DMA_FREE);
void *__DWC_DMA_ALLOC_ATOMIC(uint32_t size, dwc_dma_t *dma_addr)
{
void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
if (!buf) {
return NULL;
}
memset(buf, 0, (size_t)size);
return buf;
}
EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
void *__DWC_ALLOC(uint32_t size)
{
return kzalloc(size, GFP_KERNEL);
}
EXPORT_SYMBOL(__DWC_ALLOC);
void *__DWC_ALLOC_ATOMIC(uint32_t size)
{
return kzalloc(size, GFP_ATOMIC);
}
EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
void __DWC_FREE(void *addr)
{
kfree(addr);
}
EXPORT_SYMBOL(__DWC_FREE);
/* Byte Ordering Conversions. */
uint32_t DWC_CPU_TO_LE32(void *p)
{
#ifdef __LITTLE_ENDIAN
return *((uint32_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
#endif
}
EXPORT_SYMBOL(DWC_CPU_TO_LE32);
uint32_t DWC_CPU_TO_BE32(void *p)
{
#ifdef __BIG_ENDIAN
return *((uint32_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
#endif
}
EXPORT_SYMBOL(DWC_CPU_TO_BE32);
uint32_t DWC_LE32_TO_CPU(void *p)
{
#ifdef __LITTLE_ENDIAN
return *((uint32_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
#endif
}
EXPORT_SYMBOL(DWC_LE32_TO_CPU);
uint32_t DWC_BE32_TO_CPU(void *p)
{
#ifdef __BIG_ENDIAN
return *((uint32_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
#endif
}
EXPORT_SYMBOL(DWC_BE32_TO_CPU);
uint16_t DWC_CPU_TO_LE16(void *p)
{
#ifdef __LITTLE_ENDIAN
return *((uint16_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[1] | (u_p[0] << 8));
#endif
}
EXPORT_SYMBOL(DWC_CPU_TO_LE16);
uint16_t DWC_CPU_TO_BE16(void *p)
{
#ifdef __BIG_ENDIAN
return *((uint16_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[1] | (u_p[0] << 8));
#endif
}
EXPORT_SYMBOL(DWC_CPU_TO_BE16);
uint16_t DWC_LE16_TO_CPU(void *p)
{
#ifdef __LITTLE_ENDIAN
return *((uint16_t *)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[1] | (u_p[0] << 8));
#endif
}
EXPORT_SYMBOL(DWC_LE16_TO_CPU);
uint16_t DWC_BE16_TO_CPU(void *p)
{
#ifdef __BIG_ENDIAN
return *((uint16_t *p)p);
#else
uint8_t *u_p = (uint8_t *)p;
return (u_p[1] | (u_p[0] << 8));
#endif
}
EXPORT_SYMBOL(DWC_BE16_TO_CPU);
/* Registers */
uint32_t DWC_READ_REG32(uint32_t volatile *reg)
{
return readl(reg);
}
EXPORT_SYMBOL(DWC_READ_REG32);
#if 0
uint64_t DWC_READ_REG64(uint64_t volatile *reg)
{
}
#endif
void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
{
writel(value, reg);
}
EXPORT_SYMBOL(DWC_WRITE_REG32);
#if 0
void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
{
}
#endif
void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
{
writel( (readl(reg) & ~clear_mask) | set_mask, reg );
}
EXPORT_SYMBOL(DWC_MODIFY_REG32);
#if 0
void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t value)
{
}
#endif
/* Threading */
typedef struct work_container
{
dwc_work_callback_t cb;
void *data;
dwc_workq_t *wq;
char *name;
#ifdef DEBUG
DWC_CIRCLEQ_ENTRY(work_container) entry;
#endif
struct delayed_work work;
} work_container_t;
#ifdef DEBUG
DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
#endif
struct dwc_workq
{
struct workqueue_struct *wq;
int pending;
dwc_spinlock_t *lock;
dwc_waitq_t *waitq;
#ifdef DEBUG
struct work_container_queue entries;
#endif
};
static void do_work(struct work_struct *work)
{
int64_t flags;
struct delayed_work *dw = container_of(work, struct delayed_work, work);
work_container_t *container = container_of(dw, struct work_container, work);
dwc_workq_t *wq = container->wq;
container->cb(container->data);
#ifdef DEBUG
DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
#endif
if (container->name) {
DWC_DEBUG("Work done: %s, container=%p",
container->name, container); //GRAYG
DWC_FREE(container->name);
}
DWC_FREE(container);
DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
wq->pending --;
DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
DWC_WAITQ_TRIGGER(wq->waitq);
}
static int work_done(void *data)
{
dwc_workq_t *workq = (dwc_workq_t *)data;
return workq->pending == 0;
}
int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
{
return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
}
EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
{
dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
wq->wq = create_singlethread_workqueue(name);
wq->pending = 0;
wq->lock = DWC_SPINLOCK_ALLOC();
wq->waitq = DWC_WAITQ_ALLOC();
#ifdef DEBUG
DWC_CIRCLEQ_INIT(&wq->entries);
#endif
return wq;
}
EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
void DWC_WORKQ_FREE(dwc_workq_t *wq)
{
#ifdef DEBUG
if (wq->pending != 0) {
struct work_container *wc;
DWC_ERROR("Destroying work queue with pending work");
DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
DWC_ERROR("Work %s still pending", wc->name);
}
}
#endif
destroy_workqueue((struct workqueue_struct *)wq->wq);
DWC_SPINLOCK_FREE(wq->lock);
DWC_WAITQ_FREE(wq->waitq);
DWC_FREE(wq);
}
EXPORT_SYMBOL(DWC_WORKQ_FREE);
void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t work_cb, void *data, char *format, ...)
{
int64_t flags;
work_container_t *container;
static char name[128];
va_list args;
va_start(args, format);
if (format)
DWC_VSNPRINTF(name, 128, format, args);
va_end(args);
DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
wq->pending ++;
DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
DWC_WAITQ_TRIGGER(wq->waitq);
container = DWC_ALLOC_ATOMIC(sizeof(*container));
container->data = data;
container->cb = work_cb;
container->wq = wq;
if (format) {
container->name = DWC_STRDUP(name);
DWC_DEBUG("Queueing work: %s, contianer=%p",
container->name, container);
} else
container->name = NULL;
INIT_WORK(&container->work.work, do_work);
#ifdef DEBUG
DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
#endif
queue_work(wq->wq, &container->work.work);
}
EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t work_cb, void *data, uint32_t time, char *format, ...)
{
int64_t flags;
work_container_t *container;
static char name[128];
va_list args;
va_start(args, format);
if (format)
DWC_VSNPRINTF(name, 128, format, args);
va_end(args);
DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
wq->pending ++;
DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
DWC_WAITQ_TRIGGER(wq->waitq);
container = DWC_ALLOC_ATOMIC(sizeof(*container));
container->data = data;
container->cb = work_cb;
container->wq = wq;
if (format) { //GRAYG
container->name = DWC_STRDUP(name);
DWC_DEBUG("Queueing work: %s, contianer=%p",
container->name, container);
} else
container->name = NULL;
INIT_DELAYED_WORK(&container->work, do_work);
#ifdef DEBUG
DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
#endif
queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
}
EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
int DWC_WORKQ_PENDING(dwc_workq_t *wq)
{
return wq->pending;
}
EXPORT_SYMBOL(DWC_WORKQ_PENDING);
dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
{
spinlock_t *sl = (spinlock_t *)1;
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
sl = DWC_ALLOC(sizeof(*sl));
spin_lock_init(sl);
#endif
return (dwc_spinlock_t *)sl;
}
EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
{
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
DWC_FREE(lock);
#endif
}
EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
void DWC_SPINLOCK(dwc_spinlock_t *lock)
{
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
spin_lock((spinlock_t *)lock);
#endif
}
EXPORT_SYMBOL(DWC_SPINLOCK);
void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
{
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
spin_unlock((spinlock_t *)lock);
#endif
}
EXPORT_SYMBOL(DWC_SPINUNLOCK);
void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, uint64_t *flags)
{
unsigned long f;
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
spin_lock_irqsave((spinlock_t *)lock, f);
#else
local_irq_save(f);
#endif
*flags = f;
}
EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, uint64_t flags)
{
#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
spin_unlock_irqrestore((spinlock_t *)lock, flags);
#else
// in kernel 2.6.31, at least, we check for unsigned long
local_irq_restore((unsigned long)flags);
#endif
}
EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
dwc_mutex_t *DWC_MUTEX_ALLOC(void)
{
dwc_mutex_t *mutex = (dwc_mutex_t*)DWC_ALLOC(sizeof(struct mutex));
struct mutex *m = (struct mutex *)mutex;
mutex_init(m);
return mutex;
}
EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
#else
void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
{
mutex_destroy((struct mutex *)mutex);
DWC_FREE(mutex);
}
EXPORT_SYMBOL(DWC_MUTEX_FREE);
#endif
void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
{
struct mutex *m = (struct mutex *)mutex;
mutex_lock(m);
}
EXPORT_SYMBOL(DWC_MUTEX_LOCK);
int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
{
struct mutex *m = (struct mutex *)mutex;
return mutex_trylock(m);
}
EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
{
struct mutex *m = (struct mutex *)mutex;
mutex_unlock(m);
}
EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t thread_function, char *name, void *data)
{
struct task_struct *thread = kthread_run(thread_function, data, name);
if (thread == ERR_PTR(-ENOMEM)) {
return NULL;
}
return (dwc_thread_t *)thread;
}
EXPORT_SYMBOL(DWC_THREAD_RUN);
int DWC_THREAD_STOP(dwc_thread_t *thread)
{
return kthread_stop((struct task_struct *)thread);
}
EXPORT_SYMBOL(DWC_THREAD_STOP);
dwc_bool_t DWC_THREAD_SHOULD_STOP()
{
return kthread_should_stop();
}
EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
/* Timers */
struct dwc_timer
{
struct timer_list *t;
char *name;
dwc_timer_callback_t cb;
void *data;
uint8_t scheduled;
dwc_spinlock_t *lock;
};
static void set_scheduled(dwc_timer_t *t, int s)
{
uint64_t flags;
DWC_SPINLOCK_IRQSAVE(t->lock, &flags);
t->scheduled = s;
DWC_SPINUNLOCK_IRQRESTORE(t->lock, flags);
}
static int get_scheduled(dwc_timer_t *t)
{
int s;
uint64_t flags;
DWC_SPINLOCK_IRQSAVE(t->lock, &flags);
s = t->scheduled;
DWC_SPINUNLOCK_IRQRESTORE(t->lock, flags);
return s;
}
static void timer_callback(unsigned long data)
{
dwc_timer_t *timer = (dwc_timer_t *)data;
set_scheduled(timer, 0);
DWC_DEBUG("Timer %s callback", timer->name);
timer->cb(timer->data);
}
dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
{
dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
if (!t) {
DWC_ERROR("Cannot allocate memory for timer");
return NULL;
}
t->t = DWC_ALLOC(sizeof(*t->t));
if (!t->t) {
DWC_ERROR("Cannot allocate memory for timer->t");
goto no_timer;
}
t->name = DWC_STRDUP(name);
if (!t->name) {
DWC_ERROR("Cannot allocate memory for timer->name");
goto no_name;
}
t->lock = DWC_SPINLOCK_ALLOC();
if (!t->lock) {
DWC_ERROR("Cannot allocate memory for lock");
goto no_lock;
}
t->scheduled = 0;
t->t->base = &boot_tvec_bases;
t->t->expires = jiffies;
setup_timer(t->t, timer_callback, (unsigned long)t);
t->cb = cb;
t->data = data;
return t;
no_lock:
DWC_FREE(t->name);
no_name:
DWC_FREE(t->t);
no_timer:
DWC_FREE(t);
return NULL;
}
EXPORT_SYMBOL(DWC_TIMER_ALLOC);
void DWC_TIMER_FREE(dwc_timer_t *timer)
{
if (get_scheduled(timer)) {
del_timer(timer->t);
}
DWC_SPINLOCK_FREE(timer->lock);
DWC_FREE(timer->t);
DWC_FREE(timer->name);
DWC_FREE(timer);
}
EXPORT_SYMBOL(DWC_TIMER_FREE);
void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
{
if (!get_scheduled(timer)) {
set_scheduled(timer, 1);
//cgg: DWC_DEBUG("Scheduling timer %s to expire in +%d msec", timer->name, time);
timer->t->expires = jiffies + msecs_to_jiffies(time);
add_timer(timer->t);
}
else {
//cgg: DWC_DEBUG("Modifying timer %s to expire in +%d msec", timer->name, time);
mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
}
}
EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
void DWC_TIMER_CANCEL(dwc_timer_t *timer)
{
del_timer(timer->t);
}
EXPORT_SYMBOL(DWC_TIMER_CANCEL);
struct dwc_tasklet
{
struct tasklet_struct t;
dwc_tasklet_callback_t cb;