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