Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / usb / host / dwc_common_port / dwc_common_linux.c
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/kthread.h>
5
6 #ifdef DWC_CCLIB
7 # include "dwc_cc.h"
8 #endif
9
10 #ifdef DWC_CRYPTOLIB
11 # include "dwc_modpow.h"
12 # include "dwc_dh.h"
13 # include "dwc_crypto.h"
14 #endif
15
16 #ifdef DWC_NOTIFYLIB
17 # include "dwc_notifier.h"
18 #endif
19
20 /* OS-Level Implementations */
21
22 /* This is the Linux kernel implementation of the DWC platform library. */
23 #include <linux/moduleparam.h>
24 #include <linux/ctype.h>
25 #include <linux/crypto.h>
26 #include <linux/delay.h>
27 #include <linux/device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/cdev.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/jiffies.h>
33 #include <linux/list.h>
34 #include <linux/pci.h>
35 #include <linux/random.h>
36 #include <linux/scatterlist.h>
37 #include <linux/slab.h>
38 #include <linux/stat.h>
39 #include <linux/string.h>
40 #include <linux/timer.h>
41 #include <linux/usb.h>
42
43 #include <linux/version.h>
44
45 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
46 # include <linux/usb/gadget.h>
47 #else
48 # include <linux/usb_gadget.h>
49 #endif
50
51 #include <asm/io.h>
52 #include <asm/page.h>
53 #include <asm/uaccess.h>
54 #include <asm/unaligned.h>
55
56 #include "dwc_os.h"
57 #include "dwc_list.h"
58
59
60 /* MISC */
61
62 void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
63 {
64         return memset(dest, byte, size);
65 }
66
67 void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
68 {
69         return memcpy(dest, src, size);
70 }
71
72 void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
73 {
74         return memmove(dest, src, size);
75 }
76
77 int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
78 {
79         return memcmp(m1, m2, size);
80 }
81
82 int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
83 {
84         return strncmp(s1, s2, size);
85 }
86
87 int DWC_STRCMP(void *s1, void *s2)
88 {
89         return strcmp(s1, s2);
90 }
91
92 int DWC_STRLEN(char const *str)
93 {
94         return strlen(str);
95 }
96
97 char *DWC_STRCPY(char *to, char const *from)
98 {
99         return strcpy(to, from);
100 }
101
102 char *DWC_STRDUP(char const *str)
103 {
104         int len = DWC_STRLEN(str) + 1;
105         char *new = DWC_ALLOC_ATOMIC(len);
106
107         if (!new) {
108                 return NULL;
109         }
110
111         DWC_MEMCPY(new, str, len);
112         return new;
113 }
114
115 int DWC_ATOI(const char *str, int32_t *value)
116 {
117         char *end = NULL;
118
119         *value = simple_strtol(str, &end, 0);
120         if (*end == '\0') {
121                 return 0;
122         }
123
124         return -1;
125 }
126
127 int DWC_ATOUI(const char *str, uint32_t *value)
128 {
129         char *end = NULL;
130
131         *value = simple_strtoul(str, &end, 0);
132         if (*end == '\0') {
133                 return 0;
134         }
135
136         return -1;
137 }
138
139
140 #ifdef DWC_UTFLIB
141 /* From usbstring.c */
142
143 int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
144 {
145         int     count = 0;
146         u8      c;
147         u16     uchar;
148
149         /* this insists on correct encodings, though not minimal ones.
150          * BUT it currently rejects legit 4-byte UTF-8 code points,
151          * which need surrogate pairs.  (Unicode 3.1 can use them.)
152          */
153         while (len != 0 && (c = (u8) *s++) != 0) {
154                 if (unlikely(c & 0x80)) {
155                         // 2-byte sequence:
156                         // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
157                         if ((c & 0xe0) == 0xc0) {
158                                 uchar = (c & 0x1f) << 6;
159
160                                 c = (u8) *s++;
161                                 if ((c & 0xc0) != 0xc0)
162                                         goto fail;
163                                 c &= 0x3f;
164                                 uchar |= c;
165
166                         // 3-byte sequence (most CJKV characters):
167                         // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
168                         } else if ((c & 0xf0) == 0xe0) {
169                                 uchar = (c & 0x0f) << 12;
170
171                                 c = (u8) *s++;
172                                 if ((c & 0xc0) != 0xc0)
173                                         goto fail;
174                                 c &= 0x3f;
175                                 uchar |= c << 6;
176
177                                 c = (u8) *s++;
178                                 if ((c & 0xc0) != 0xc0)
179                                         goto fail;
180                                 c &= 0x3f;
181                                 uchar |= c;
182
183                                 /* no bogus surrogates */
184                                 if (0xd800 <= uchar && uchar <= 0xdfff)
185                                         goto fail;
186
187                         // 4-byte sequence (surrogate pairs, currently rare):
188                         // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
189                         //     = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
190                         // (uuuuu = wwww + 1)
191                         // FIXME accept the surrogate code points (only)
192                         } else
193                                 goto fail;
194                 } else
195                         uchar = c;
196                 put_unaligned (cpu_to_le16 (uchar), cp++);
197                 count++;
198                 len--;
199         }
200         return count;
201 fail:
202         return -1;
203 }
204 #endif  /* DWC_UTFLIB */
205
206
207 /* dwc_debug.h */
208
209 dwc_bool_t DWC_IN_IRQ(void)
210 {
211         return in_irq();
212 }
213
214 dwc_bool_t DWC_IN_BH(void)
215 {
216         return in_softirq();
217 }
218
219 void DWC_VPRINTF(char *format, va_list args)
220 {
221         vprintk(format, args);
222 }
223
224 int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
225 {
226         return vsnprintf(str, size, format, args);
227 }
228
229 void DWC_PRINTF(char *format, ...)
230 {
231         va_list args;
232
233         va_start(args, format);
234         DWC_VPRINTF(format, args);
235         va_end(args);
236 }
237
238 int DWC_SPRINTF(char *buffer, char *format, ...)
239 {
240         int retval;
241         va_list args;
242
243         va_start(args, format);
244         retval = vsprintf(buffer, format, args);
245         va_end(args);
246         return retval;
247 }
248
249 int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
250 {
251         int retval;
252         va_list args;
253
254         va_start(args, format);
255         retval = vsnprintf(buffer, size, format, args);
256         va_end(args);
257         return retval;
258 }
259
260 void __DWC_WARN(char *format, ...)
261 {
262         va_list args;
263
264         va_start(args, format);
265         DWC_PRINTF(KERN_WARNING);
266         DWC_VPRINTF(format, args);
267         va_end(args);
268 }
269
270 void __DWC_ERROR(char *format, ...)
271 {
272         va_list args;
273
274         va_start(args, format);
275         DWC_PRINTF(KERN_ERR);
276         DWC_VPRINTF(format, args);
277         va_end(args);
278 }
279
280 void DWC_EXCEPTION(char *format, ...)
281 {
282         va_list args;
283
284         va_start(args, format);
285         DWC_PRINTF(KERN_ERR);
286         DWC_VPRINTF(format, args);
287         va_end(args);
288         BUG_ON(1);
289 }
290
291 #ifdef DEBUG
292 void __DWC_DEBUG(char *format, ...)
293 {
294         va_list args;
295
296         va_start(args, format);
297         DWC_PRINTF(KERN_DEBUG);
298         DWC_VPRINTF(format, args);
299         va_end(args);
300 }
301 #endif
302
303
304 /* dwc_mem.h */
305
306 #if 0
307 dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
308                                 uint32_t align,
309                                 uint32_t alloc)
310 {
311         struct dma_pool *pool = dma_pool_create("Pool", NULL,
312                                                 size, align, alloc);
313         return (dwc_pool_t *)pool;
314 }
315
316 void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
317 {
318         dma_pool_destroy((struct dma_pool *)pool);
319 }
320
321 void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
322 {
323         return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
324 }
325
326 void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
327 {
328         void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
329         memset(..);
330 }
331
332 void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
333 {
334         dma_pool_free(pool, vaddr, daddr);
335 }
336 #endif
337
338 void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
339 {
340         return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
341 }
342
343 void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
344 {
345         return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
346 }
347
348 void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
349 {
350         dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
351 }
352
353 void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
354 {
355         return kzalloc(size, GFP_KERNEL);
356 }
357
358 void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
359 {
360         return kzalloc(size, GFP_ATOMIC);
361 }
362
363 void __DWC_FREE(void *mem_ctx, void *addr)
364 {
365         kfree(addr);
366 }
367
368
369 #ifdef DWC_CRYPTOLIB
370 /* dwc_crypto.h */
371
372 void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
373 {
374         get_random_bytes(buffer, length);
375 }
376
377 int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
378 {
379         struct crypto_blkcipher *tfm;
380         struct blkcipher_desc desc;
381         struct scatterlist sgd;
382         struct scatterlist sgs;
383
384         tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
385         if (tfm == NULL) {
386                 printk("failed to load transform for aes CBC\n");
387                 return -1;
388         }
389
390         crypto_blkcipher_setkey(tfm, key, keylen);
391         crypto_blkcipher_set_iv(tfm, iv, 16);
392
393         sg_init_one(&sgd, out, messagelen);
394         sg_init_one(&sgs, message, messagelen);
395
396         desc.tfm = tfm;
397         desc.flags = 0;
398
399         if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
400                 crypto_free_blkcipher(tfm);
401                 DWC_ERROR("AES CBC encryption failed");
402                 return -1;
403         }
404
405         crypto_free_blkcipher(tfm);
406         return 0;
407 }
408
409 int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
410 {
411         struct crypto_hash *tfm;
412         struct hash_desc desc;
413         struct scatterlist sg;
414
415         tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
416         if (IS_ERR(tfm)) {
417                 DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
418                 return 0;
419         }
420         desc.tfm = tfm;
421         desc.flags = 0;
422
423         sg_init_one(&sg, message, len);
424         crypto_hash_digest(&desc, &sg, len, out);
425         crypto_free_hash(tfm);
426
427         return 1;
428 }
429
430 int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
431                     uint8_t *key, uint32_t keylen, uint8_t *out)
432 {
433         struct crypto_hash *tfm;
434         struct hash_desc desc;
435         struct scatterlist sg;
436
437         tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
438         if (IS_ERR(tfm)) {
439                 DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
440                 return 0;
441         }
442         desc.tfm = tfm;
443         desc.flags = 0;
444
445         sg_init_one(&sg, message, messagelen);
446         crypto_hash_setkey(tfm, key, keylen);
447         crypto_hash_digest(&desc, &sg, messagelen, out);
448         crypto_free_hash(tfm);
449
450         return 1;
451 }
452 #endif  /* DWC_CRYPTOLIB */
453
454
455 /* Byte Ordering Conversions */
456
457 uint32_t DWC_CPU_TO_LE32(uint32_t *p)
458 {
459 #ifdef __LITTLE_ENDIAN
460         return *p;
461 #else
462         uint8_t *u_p = (uint8_t *)p;
463
464         return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
465 #endif
466 }
467
468 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
469 {
470 #ifdef __BIG_ENDIAN
471         return *p;
472 #else
473         uint8_t *u_p = (uint8_t *)p;
474
475         return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
476 #endif
477 }
478
479 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
480 {
481 #ifdef __LITTLE_ENDIAN
482         return *p;
483 #else
484         uint8_t *u_p = (uint8_t *)p;
485
486         return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
487 #endif
488 }
489
490 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
491 {
492 #ifdef __BIG_ENDIAN
493         return *p;
494 #else
495         uint8_t *u_p = (uint8_t *)p;
496
497         return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
498 #endif
499 }
500
501 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
502 {
503 #ifdef __LITTLE_ENDIAN
504         return *p;
505 #else
506         uint8_t *u_p = (uint8_t *)p;
507         return (u_p[1] | (u_p[0] << 8));
508 #endif
509 }
510
511 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
512 {
513 #ifdef __BIG_ENDIAN
514         return *p;
515 #else
516         uint8_t *u_p = (uint8_t *)p;
517         return (u_p[1] | (u_p[0] << 8));
518 #endif
519 }
520
521 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
522 {
523 #ifdef __LITTLE_ENDIAN
524         return *p;
525 #else
526         uint8_t *u_p = (uint8_t *)p;
527         return (u_p[1] | (u_p[0] << 8));
528 #endif
529 }
530
531 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
532 {
533 #ifdef __BIG_ENDIAN
534         return *p;
535 #else
536         uint8_t *u_p = (uint8_t *)p;
537         return (u_p[1] | (u_p[0] << 8));
538 #endif
539 }
540
541
542 /* Registers */
543
544 uint32_t DWC_READ_REG32(uint32_t volatile *reg)
545 {
546         return readl(reg);
547 }
548
549 #if 0
550 uint64_t DWC_READ_REG64(uint64_t volatile *reg)
551 {
552 }
553 #endif
554
555 void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
556 {
557         writel(value, reg);
558 }
559
560 #if 0
561 void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
562 {
563 }
564 #endif
565
566 void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
567 {
568         writel((readl(reg) & ~clear_mask) | set_mask, reg);
569 }
570
571 #if 0
572 void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
573 {
574 }
575 #endif
576
577
578 /* Locking */
579
580 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
581 {
582         spinlock_t *sl = (spinlock_t *)1;
583
584 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
585         sl = DWC_ALLOC(sizeof(*sl));
586         if (!sl) {
587                 DWC_ERROR("Cannot allocate memory for spinlock\n");
588                 return NULL;
589         }
590
591         spin_lock_init(sl);
592 #endif
593         return (dwc_spinlock_t *)sl;
594 }
595
596 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
597 {
598 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
599         DWC_FREE(lock);
600 #endif
601 }
602
603 void DWC_SPINLOCK(dwc_spinlock_t *lock)
604 {
605 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
606         spin_lock((spinlock_t *)lock);
607 #endif
608 }
609
610 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
611 {
612 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
613         spin_unlock((spinlock_t *)lock);
614 #endif
615 }
616
617 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
618 {
619         dwc_irqflags_t f;
620
621 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
622         spin_lock_irqsave((spinlock_t *)lock, f);
623 #else
624         local_irq_save(f);
625 #endif
626         *flags = f;
627 }
628
629 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
630 {
631 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
632         spin_unlock_irqrestore((spinlock_t *)lock, flags);
633 #else
634         local_irq_restore(flags);
635 #endif
636 }
637
638 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
639 {
640         struct mutex *m;
641         dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
642
643         if (!mutex) {
644                 DWC_ERROR("Cannot allocate memory for mutex\n");
645                 return NULL;
646         }
647
648         m = (struct mutex *)mutex;
649         mutex_init(m);
650         return mutex;
651 }
652
653 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
654 #else
655 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
656 {
657         mutex_destroy((struct mutex *)mutex);
658         DWC_FREE(mutex);
659 }
660 #endif
661
662 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
663 {
664         struct mutex *m = (struct mutex *)mutex;
665         mutex_lock(m);
666 }
667
668 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
669 {
670         struct mutex *m = (struct mutex *)mutex;
671         return mutex_trylock(m);
672 }
673
674 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
675 {
676         struct mutex *m = (struct mutex *)mutex;
677         mutex_unlock(m);
678 }
679
680
681 /* Timing */
682
683 void DWC_UDELAY(uint32_t usecs)
684 {
685         udelay(usecs);
686 }
687
688 void DWC_MDELAY(uint32_t msecs)
689 {
690         mdelay(msecs);
691 }
692
693 void DWC_MSLEEP(uint32_t msecs)
694 {
695         msleep(msecs);
696 }
697
698 uint32_t DWC_TIME(void)
699 {
700         return jiffies_to_msecs(jiffies);
701 }
702
703
704 /* Timers */
705
706 struct dwc_timer {
707         struct timer_list t;
708         char *name;
709         dwc_timer_callback_t cb;
710         void *data;
711         uint8_t scheduled;
712         dwc_spinlock_t *lock;
713 };
714
715 static void timer_callback(struct timer_list *tt)
716 {
717         dwc_timer_t *timer = from_timer(timer, tt, t);
718         dwc_irqflags_t flags;
719
720         DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
721         timer->scheduled = 0;
722         DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
723         DWC_DEBUGC("Timer %s callback", timer->name);
724         timer->cb(timer->data);
725 }
726
727 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
728 {
729         dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
730
731         if (!t) {
732                 DWC_ERROR("Cannot allocate memory for timer");
733                 return NULL;
734         }
735
736         t->name = DWC_STRDUP(name);
737         if (!t->name) {
738                 DWC_ERROR("Cannot allocate memory for timer->name");
739                 goto no_name;
740         }
741
742 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
743         DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
744 #else
745         t->lock = DWC_SPINLOCK_ALLOC();
746 #endif
747         if (!t->lock) {
748                 DWC_ERROR("Cannot allocate memory for lock");
749                 goto no_lock;
750         }
751
752         t->scheduled = 0;
753         t->t.expires = jiffies;
754         timer_setup(&t->t, timer_callback, 0);
755
756         t->cb = cb;
757         t->data = data;
758
759         return t;
760
761  no_lock:
762         DWC_FREE(t->name);
763  no_name:
764         DWC_FREE(t);
765         return NULL;
766 }
767
768 void DWC_TIMER_FREE(dwc_timer_t *timer)
769 {
770         dwc_irqflags_t flags;
771
772         DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
773
774         if (timer->scheduled) {
775                 del_timer(&timer->t);
776                 timer->scheduled = 0;
777         }
778
779         DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
780         DWC_SPINLOCK_FREE(timer->lock);
781         DWC_FREE(timer->name);
782         DWC_FREE(timer);
783 }
784
785 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
786 {
787         dwc_irqflags_t flags;
788
789         DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
790
791         if (!timer->scheduled) {
792                 timer->scheduled = 1;
793                 DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
794                 timer->t.expires = jiffies + msecs_to_jiffies(time);
795                 add_timer(&timer->t);
796         } else {
797                 DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
798                 mod_timer(&timer->t, jiffies + msecs_to_jiffies(time));
799         }
800
801         DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
802 }
803
804 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
805 {
806         del_timer(&timer->t);
807 }
808
809
810 /* Wait Queues */
811
812 struct dwc_waitq {
813         wait_queue_head_t queue;
814         int abort;
815 };
816
817 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
818 {
819         dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
820
821         if (!wq) {
822                 DWC_ERROR("Cannot allocate memory for waitqueue\n");
823                 return NULL;
824         }
825
826         init_waitqueue_head(&wq->queue);
827         wq->abort = 0;
828         return wq;
829 }
830
831 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
832 {
833         DWC_FREE(wq);
834 }
835
836 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
837 {
838         int result = wait_event_interruptible(wq->queue,
839                                               cond(data) || wq->abort);
840         if (result == -ERESTARTSYS) {
841                 wq->abort = 0;
842                 return -DWC_E_RESTART;
843         }
844
845         if (wq->abort == 1) {
846                 wq->abort = 0;
847                 return -DWC_E_ABORT;
848         }
849
850         wq->abort = 0;
851
852         if (result == 0) {
853                 return 0;
854         }
855
856         return -DWC_E_UNKNOWN;
857 }
858
859 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
860                                void *data, int32_t msecs)
861 {
862         int32_t tmsecs;
863         int result = wait_event_interruptible_timeout(wq->queue,
864                                                       cond(data) || wq->abort,
865                                                       msecs_to_jiffies(msecs));
866         if (result == -ERESTARTSYS) {
867                 wq->abort = 0;
868                 return -DWC_E_RESTART;
869         }
870
871         if (wq->abort == 1) {
872                 wq->abort = 0;
873                 return -DWC_E_ABORT;
874         }
875
876         wq->abort = 0;
877
878         if (result > 0) {
879                 tmsecs = jiffies_to_msecs(result);
880                 if (!tmsecs) {
881                         return 1;
882                 }
883
884                 return tmsecs;
885         }
886
887         if (result == 0) {
888                 return -DWC_E_TIMEOUT;
889         }
890
891         return -DWC_E_UNKNOWN;
892 }
893
894 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
895 {
896         wq->abort = 0;
897         wake_up_interruptible(&wq->queue);
898 }
899
900 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
901 {
902         wq->abort = 1;
903         wake_up_interruptible(&wq->queue);
904 }
905
906
907 /* Threading */
908
909 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
910 {
911         struct task_struct *thread = kthread_run(func, data, name);
912
913         if (thread == ERR_PTR(-ENOMEM)) {
914                 return NULL;
915         }
916
917         return (dwc_thread_t *)thread;
918 }
919
920 int DWC_THREAD_STOP(dwc_thread_t *thread)
921 {
922         return kthread_stop((struct task_struct *)thread);
923 }
924
925 dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
926 {
927         return kthread_should_stop();
928 }
929
930
931 /* tasklets
932  - run in interrupt context (cannot sleep)
933  - each tasklet runs on a single CPU
934  - different tasklets can be running simultaneously on different CPUs
935  */
936 struct dwc_tasklet {
937         struct tasklet_struct t;
938         dwc_tasklet_callback_t cb;
939         void *data;
940 };
941
942 static void tasklet_callback(unsigned long data)
943 {
944         dwc_tasklet_t *t = (dwc_tasklet_t *)data;
945         t->cb(t->data);
946 }
947
948 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
949 {
950         dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
951
952         if (t) {
953                 t->cb = cb;
954                 t->data = data;
955                 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
956         } else {
957                 DWC_ERROR("Cannot allocate memory for tasklet\n");
958         }
959
960         return t;
961 }
962
963 void DWC_TASK_FREE(dwc_tasklet_t *task)
964 {
965         DWC_FREE(task);
966 }
967
968 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
969 {
970         tasklet_schedule(&task->t);
971 }
972
973 void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
974 {
975         tasklet_hi_schedule(&task->t);
976 }
977
978
979 /* workqueues
980  - run in process context (can sleep)
981  */
982 typedef struct work_container {
983         dwc_work_callback_t cb;
984         void *data;
985         dwc_workq_t *wq;
986         char *name;
987
988 #ifdef DEBUG
989         DWC_CIRCLEQ_ENTRY(work_container) entry;
990 #endif
991         struct delayed_work work;
992 } work_container_t;
993
994 #ifdef DEBUG
995 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
996 #endif
997
998 struct dwc_workq {
999         struct workqueue_struct *wq;
1000         dwc_spinlock_t *lock;
1001         dwc_waitq_t *waitq;
1002         int pending;
1003
1004 #ifdef DEBUG
1005         struct work_container_queue entries;
1006 #endif
1007 };
1008
1009 static void do_work(struct work_struct *work)
1010 {
1011         dwc_irqflags_t flags;
1012         struct delayed_work *dw = container_of(work, struct delayed_work, work);
1013         work_container_t *container = container_of(dw, struct work_container, work);
1014         dwc_workq_t *wq = container->wq;
1015
1016         container->cb(container->data);
1017
1018 #ifdef DEBUG
1019         DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1020 #endif
1021         DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
1022         if (container->name) {
1023                 DWC_FREE(container->name);
1024         }
1025         DWC_FREE(container);
1026
1027         DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1028         wq->pending--;
1029         DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1030         DWC_WAITQ_TRIGGER(wq->waitq);
1031 }
1032
1033 static int work_done(void *data)
1034 {
1035         dwc_workq_t *workq = (dwc_workq_t *)data;
1036         return workq->pending == 0;
1037 }
1038
1039 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1040 {
1041         return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1042 }
1043
1044 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1045 {
1046         dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1047
1048         if (!wq) {
1049                 return NULL;
1050         }
1051
1052         wq->wq = create_singlethread_workqueue(name);
1053         if (!wq->wq) {
1054                 goto no_wq;
1055         }
1056
1057         wq->pending = 0;
1058
1059 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
1060         DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
1061 #else
1062         wq->lock = DWC_SPINLOCK_ALLOC();
1063 #endif
1064         if (!wq->lock) {
1065                 goto no_lock;
1066         }
1067
1068         wq->waitq = DWC_WAITQ_ALLOC();
1069         if (!wq->waitq) {
1070                 goto no_waitq;
1071         }
1072
1073 #ifdef DEBUG
1074         DWC_CIRCLEQ_INIT(&wq->entries);
1075 #endif
1076         return wq;
1077
1078  no_waitq:
1079         DWC_SPINLOCK_FREE(wq->lock);
1080  no_lock:
1081         destroy_workqueue(wq->wq);
1082  no_wq:
1083         DWC_FREE(wq);
1084
1085         return NULL;
1086 }
1087
1088 void DWC_WORKQ_FREE(dwc_workq_t *wq)
1089 {
1090 #ifdef DEBUG
1091         if (wq->pending != 0) {
1092                 struct work_container *wc;
1093                 DWC_ERROR("Destroying work queue with pending work");
1094                 DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
1095                         DWC_ERROR("Work %s still pending", wc->name);
1096                 }
1097         }
1098 #endif
1099         destroy_workqueue(wq->wq);
1100         DWC_SPINLOCK_FREE(wq->lock);
1101         DWC_WAITQ_FREE(wq->waitq);
1102         DWC_FREE(wq);
1103 }
1104
1105 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
1106                         char *format, ...)
1107 {
1108         dwc_irqflags_t flags;
1109         work_container_t *container;
1110         static char name[128];
1111         va_list args;
1112
1113         va_start(args, format);
1114         DWC_VSNPRINTF(name, 128, format, args);
1115         va_end(args);
1116
1117         DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1118         wq->pending++;
1119         DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1120         DWC_WAITQ_TRIGGER(wq->waitq);
1121
1122         container = DWC_ALLOC_ATOMIC(sizeof(*container));
1123         if (!container) {
1124                 DWC_ERROR("Cannot allocate memory for container\n");
1125                 return;
1126         }
1127
1128         container->name = DWC_STRDUP(name);
1129         if (!container->name) {
1130                 DWC_ERROR("Cannot allocate memory for container->name\n");
1131                 DWC_FREE(container);
1132                 return;
1133         }
1134
1135         container->cb = cb;
1136         container->data = data;
1137         container->wq = wq;
1138         DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
1139         INIT_WORK(&container->work.work, do_work);
1140
1141 #ifdef DEBUG
1142         DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1143 #endif
1144         queue_work(wq->wq, &container->work.work);
1145 }
1146
1147 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1148                                 void *data, uint32_t time, char *format, ...)
1149 {
1150         dwc_irqflags_t flags;
1151         work_container_t *container;
1152         static char name[128];
1153         va_list args;
1154
1155         va_start(args, format);
1156         DWC_VSNPRINTF(name, 128, format, args);
1157         va_end(args);
1158
1159         DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1160         wq->pending++;
1161         DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1162         DWC_WAITQ_TRIGGER(wq->waitq);
1163
1164         container = DWC_ALLOC_ATOMIC(sizeof(*container));
1165         if (!container) {
1166                 DWC_ERROR("Cannot allocate memory for container\n");
1167                 return;
1168         }
1169
1170         container->name = DWC_STRDUP(name);
1171         if (!container->name) {
1172                 DWC_ERROR("Cannot allocate memory for container->name\n");
1173                 DWC_FREE(container);
1174                 return;
1175         }
1176
1177         container->cb = cb;
1178         container->data = data;
1179         container->wq = wq;
1180         DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
1181         INIT_DELAYED_WORK(&container->work, do_work);
1182
1183 #ifdef DEBUG
1184         DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1185 #endif
1186         queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
1187 }
1188
1189 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1190 {
1191         return wq->pending;
1192 }
1193
1194
1195 #ifdef DWC_LIBMODULE
1196
1197 #ifdef DWC_CCLIB
1198 /* CC */
1199 EXPORT_SYMBOL(dwc_cc_if_alloc);
1200 EXPORT_SYMBOL(dwc_cc_if_free);
1201 EXPORT_SYMBOL(dwc_cc_clear);
1202 EXPORT_SYMBOL(dwc_cc_add);
1203 EXPORT_SYMBOL(dwc_cc_remove);
1204 EXPORT_SYMBOL(dwc_cc_change);
1205 EXPORT_SYMBOL(dwc_cc_data_for_save);
1206 EXPORT_SYMBOL(dwc_cc_restore_from_data);
1207 EXPORT_SYMBOL(dwc_cc_match_chid);
1208 EXPORT_SYMBOL(dwc_cc_match_cdid);
1209 EXPORT_SYMBOL(dwc_cc_ck);
1210 EXPORT_SYMBOL(dwc_cc_chid);
1211 EXPORT_SYMBOL(dwc_cc_cdid);
1212 EXPORT_SYMBOL(dwc_cc_name);
1213 #endif  /* DWC_CCLIB */
1214
1215 #ifdef DWC_CRYPTOLIB
1216 # ifndef CONFIG_MACH_IPMATE
1217 /* Modpow */
1218 EXPORT_SYMBOL(dwc_modpow);
1219
1220 /* DH */
1221 EXPORT_SYMBOL(dwc_dh_modpow);
1222 EXPORT_SYMBOL(dwc_dh_derive_keys);
1223 EXPORT_SYMBOL(dwc_dh_pk);
1224 # endif /* CONFIG_MACH_IPMATE */
1225
1226 /* Crypto */
1227 EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
1228 EXPORT_SYMBOL(dwc_wusb_cmf);
1229 EXPORT_SYMBOL(dwc_wusb_prf);
1230 EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
1231 EXPORT_SYMBOL(dwc_wusb_gen_nonce);
1232 EXPORT_SYMBOL(dwc_wusb_gen_key);
1233 EXPORT_SYMBOL(dwc_wusb_gen_mic);
1234 #endif  /* DWC_CRYPTOLIB */
1235
1236 /* Notification */
1237 #ifdef DWC_NOTIFYLIB
1238 EXPORT_SYMBOL(dwc_alloc_notification_manager);
1239 EXPORT_SYMBOL(dwc_free_notification_manager);
1240 EXPORT_SYMBOL(dwc_register_notifier);
1241 EXPORT_SYMBOL(dwc_unregister_notifier);
1242 EXPORT_SYMBOL(dwc_add_observer);
1243 EXPORT_SYMBOL(dwc_remove_observer);
1244 EXPORT_SYMBOL(dwc_notify);
1245 #endif
1246
1247 /* Memory Debugging Routines */
1248 #ifdef DWC_DEBUG_MEMORY
1249 EXPORT_SYMBOL(dwc_alloc_debug);
1250 EXPORT_SYMBOL(dwc_alloc_atomic_debug);
1251 EXPORT_SYMBOL(dwc_free_debug);
1252 EXPORT_SYMBOL(dwc_dma_alloc_debug);
1253 EXPORT_SYMBOL(dwc_dma_free_debug);
1254 #endif
1255
1256 EXPORT_SYMBOL(DWC_MEMSET);
1257 EXPORT_SYMBOL(DWC_MEMCPY);
1258 EXPORT_SYMBOL(DWC_MEMMOVE);
1259 EXPORT_SYMBOL(DWC_MEMCMP);
1260 EXPORT_SYMBOL(DWC_STRNCMP);
1261 EXPORT_SYMBOL(DWC_STRCMP);
1262 EXPORT_SYMBOL(DWC_STRLEN);
1263 EXPORT_SYMBOL(DWC_STRCPY);
1264 EXPORT_SYMBOL(DWC_STRDUP);
1265 EXPORT_SYMBOL(DWC_ATOI);
1266 EXPORT_SYMBOL(DWC_ATOUI);
1267
1268 #ifdef DWC_UTFLIB
1269 EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
1270 #endif  /* DWC_UTFLIB */
1271
1272 EXPORT_SYMBOL(DWC_IN_IRQ);
1273 EXPORT_SYMBOL(DWC_IN_BH);
1274 EXPORT_SYMBOL(DWC_VPRINTF);
1275 EXPORT_SYMBOL(DWC_VSNPRINTF);
1276 EXPORT_SYMBOL(DWC_PRINTF);
1277 EXPORT_SYMBOL(DWC_SPRINTF);
1278 EXPORT_SYMBOL(DWC_SNPRINTF);
1279 EXPORT_SYMBOL(__DWC_WARN);
1280 EXPORT_SYMBOL(__DWC_ERROR);
1281 EXPORT_SYMBOL(DWC_EXCEPTION);
1282
1283 #ifdef DEBUG
1284 EXPORT_SYMBOL(__DWC_DEBUG);
1285 #endif
1286
1287 EXPORT_SYMBOL(__DWC_DMA_ALLOC);
1288 EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
1289 EXPORT_SYMBOL(__DWC_DMA_FREE);
1290 EXPORT_SYMBOL(__DWC_ALLOC);
1291 EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
1292 EXPORT_SYMBOL(__DWC_FREE);
1293
1294 #ifdef DWC_CRYPTOLIB
1295 EXPORT_SYMBOL(DWC_RANDOM_BYTES);
1296 EXPORT_SYMBOL(DWC_AES_CBC);
1297 EXPORT_SYMBOL(DWC_SHA256);
1298 EXPORT_SYMBOL(DWC_HMAC_SHA256);
1299 #endif
1300
1301 EXPORT_SYMBOL(DWC_CPU_TO_LE32);
1302 EXPORT_SYMBOL(DWC_CPU_TO_BE32);
1303 EXPORT_SYMBOL(DWC_LE32_TO_CPU);
1304 EXPORT_SYMBOL(DWC_BE32_TO_CPU);
1305 EXPORT_SYMBOL(DWC_CPU_TO_LE16);
1306 EXPORT_SYMBOL(DWC_CPU_TO_BE16);
1307 EXPORT_SYMBOL(DWC_LE16_TO_CPU);
1308 EXPORT_SYMBOL(DWC_BE16_TO_CPU);
1309 EXPORT_SYMBOL(DWC_READ_REG32);
1310 EXPORT_SYMBOL(DWC_WRITE_REG32);
1311 EXPORT_SYMBOL(DWC_MODIFY_REG32);
1312
1313 #if 0
1314 EXPORT_SYMBOL(DWC_READ_REG64);
1315 EXPORT_SYMBOL(DWC_WRITE_REG64);
1316 EXPORT_SYMBOL(DWC_MODIFY_REG64);
1317 #endif
1318
1319 EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
1320 EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
1321 EXPORT_SYMBOL(DWC_SPINLOCK);
1322 EXPORT_SYMBOL(DWC_SPINUNLOCK);
1323 EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
1324 EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
1325 EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
1326
1327 #if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
1328 EXPORT_SYMBOL(DWC_MUTEX_FREE);
1329 #endif
1330
1331 EXPORT_SYMBOL(DWC_MUTEX_LOCK);
1332 EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
1333 EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
1334 EXPORT_SYMBOL(DWC_UDELAY);
1335 EXPORT_SYMBOL(DWC_MDELAY);
1336 EXPORT_SYMBOL(DWC_MSLEEP);
1337 EXPORT_SYMBOL(DWC_TIME);
1338 EXPORT_SYMBOL(DWC_TIMER_ALLOC);
1339 EXPORT_SYMBOL(DWC_TIMER_FREE);
1340 EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
1341 EXPORT_SYMBOL(DWC_TIMER_CANCEL);
1342 EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
1343 EXPORT_SYMBOL(DWC_WAITQ_FREE);
1344 EXPORT_SYMBOL(DWC_WAITQ_WAIT);
1345 EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
1346 EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
1347 EXPORT_SYMBOL(DWC_WAITQ_ABORT);
1348 EXPORT_SYMBOL(DWC_THREAD_RUN);
1349 EXPORT_SYMBOL(DWC_THREAD_STOP);
1350 EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
1351 EXPORT_SYMBOL(DWC_TASK_ALLOC);
1352 EXPORT_SYMBOL(DWC_TASK_FREE);
1353 EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
1354 EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
1355 EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
1356 EXPORT_SYMBOL(DWC_WORKQ_FREE);
1357 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
1358 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
1359 EXPORT_SYMBOL(DWC_WORKQ_PENDING);
1360
1361 static int dwc_common_port_init_module(void)
1362 {
1363         int result = 0;
1364
1365         printk(KERN_DEBUG "Module dwc_common_port init\n" );
1366
1367 #ifdef DWC_DEBUG_MEMORY
1368         result = dwc_memory_debug_start(NULL);
1369         if (result) {
1370                 printk(KERN_ERR
1371                        "dwc_memory_debug_start() failed with error %d\n",
1372                        result);
1373                 return result;
1374         }
1375 #endif
1376
1377 #ifdef DWC_NOTIFYLIB
1378         result = dwc_alloc_notification_manager(NULL, NULL);
1379         if (result) {
1380                 printk(KERN_ERR
1381                        "dwc_alloc_notification_manager() failed with error %d\n",
1382                        result);
1383                 return result;
1384         }
1385 #endif
1386         return result;
1387 }
1388
1389 static void dwc_common_port_exit_module(void)
1390 {
1391         printk(KERN_DEBUG "Module dwc_common_port exit\n" );
1392
1393 #ifdef DWC_NOTIFYLIB
1394         dwc_free_notification_manager();
1395 #endif
1396
1397 #ifdef DWC_DEBUG_MEMORY
1398         dwc_memory_debug_stop();
1399 #endif
1400 }
1401
1402 module_init(dwc_common_port_init_module);
1403 module_exit(dwc_common_port_exit_module);
1404
1405 MODULE_DESCRIPTION("DWC Common Library - Portable version");
1406 MODULE_AUTHOR("Synopsys Inc.");
1407 MODULE_LICENSE ("GPL");
1408
1409 #endif  /* DWC_LIBMODULE */