1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/kthread.h>
11 # include "dwc_modpow.h"
13 # include "dwc_crypto.h"
17 # include "dwc_notifier.h"
20 /* OS-Level Implementations */
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>
43 #include <linux/version.h>
45 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
46 # include <linux/usb/gadget.h>
48 # include <linux/usb_gadget.h>
53 #include <asm/uaccess.h>
54 #include <asm/unaligned.h>
62 void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
64 return memset(dest, byte, size);
67 void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
69 return memcpy(dest, src, size);
72 void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
74 return memmove(dest, src, size);
77 int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
79 return memcmp(m1, m2, size);
82 int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
84 return strncmp(s1, s2, size);
87 int DWC_STRCMP(void *s1, void *s2)
89 return strcmp(s1, s2);
92 int DWC_STRLEN(char const *str)
97 char *DWC_STRCPY(char *to, char const *from)
99 return strcpy(to, from);
102 char *DWC_STRDUP(char const *str)
104 int len = DWC_STRLEN(str) + 1;
105 char *new = DWC_ALLOC_ATOMIC(len);
111 DWC_MEMCPY(new, str, len);
115 int DWC_ATOI(const char *str, int32_t *value)
119 *value = simple_strtol(str, &end, 0);
127 int DWC_ATOUI(const char *str, uint32_t *value)
131 *value = simple_strtoul(str, &end, 0);
141 /* From usbstring.c */
143 int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
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.)
153 while (len != 0 && (c = (u8) *s++) != 0) {
154 if (unlikely(c & 0x80)) {
156 // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
157 if ((c & 0xe0) == 0xc0) {
158 uchar = (c & 0x1f) << 6;
161 if ((c & 0xc0) != 0xc0)
166 // 3-byte sequence (most CJKV characters):
167 // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
168 } else if ((c & 0xf0) == 0xe0) {
169 uchar = (c & 0x0f) << 12;
172 if ((c & 0xc0) != 0xc0)
178 if ((c & 0xc0) != 0xc0)
183 /* no bogus surrogates */
184 if (0xd800 <= uchar && uchar <= 0xdfff)
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)
196 put_unaligned (cpu_to_le16 (uchar), cp++);
204 #endif /* DWC_UTFLIB */
209 dwc_bool_t DWC_IN_IRQ(void)
214 dwc_bool_t DWC_IN_BH(void)
219 void DWC_VPRINTF(char *format, va_list args)
221 vprintk(format, args);
224 int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
226 return vsnprintf(str, size, format, args);
229 void DWC_PRINTF(char *format, ...)
233 va_start(args, format);
234 DWC_VPRINTF(format, args);
238 int DWC_SPRINTF(char *buffer, char *format, ...)
243 va_start(args, format);
244 retval = vsprintf(buffer, format, args);
249 int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
254 va_start(args, format);
255 retval = vsnprintf(buffer, size, format, args);
260 void __DWC_WARN(char *format, ...)
264 va_start(args, format);
265 DWC_PRINTF(KERN_WARNING);
266 DWC_VPRINTF(format, args);
270 void __DWC_ERROR(char *format, ...)
274 va_start(args, format);
275 DWC_PRINTF(KERN_ERR);
276 DWC_VPRINTF(format, args);
280 void DWC_EXCEPTION(char *format, ...)
284 va_start(args, format);
285 DWC_PRINTF(KERN_ERR);
286 DWC_VPRINTF(format, args);
292 void __DWC_DEBUG(char *format, ...)
296 va_start(args, format);
297 DWC_PRINTF(KERN_DEBUG);
298 DWC_VPRINTF(format, args);
307 dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
311 struct dma_pool *pool = dma_pool_create("Pool", NULL,
313 return (dwc_pool_t *)pool;
316 void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
318 dma_pool_destroy((struct dma_pool *)pool);
321 void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
323 return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
326 void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
328 void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
332 void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
334 dma_pool_free(pool, vaddr, daddr);
338 void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
340 return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
343 void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
345 return dma_alloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
348 void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
350 dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
353 void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
355 return kzalloc(size, GFP_KERNEL);
358 void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
360 return kzalloc(size, GFP_ATOMIC);
363 void __DWC_FREE(void *mem_ctx, void *addr)
372 void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
374 get_random_bytes(buffer, length);
377 int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
379 struct crypto_blkcipher *tfm;
380 struct blkcipher_desc desc;
381 struct scatterlist sgd;
382 struct scatterlist sgs;
384 tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
386 printk("failed to load transform for aes CBC\n");
390 crypto_blkcipher_setkey(tfm, key, keylen);
391 crypto_blkcipher_set_iv(tfm, iv, 16);
393 sg_init_one(&sgd, out, messagelen);
394 sg_init_one(&sgs, message, messagelen);
399 if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
400 crypto_free_blkcipher(tfm);
401 DWC_ERROR("AES CBC encryption failed");
405 crypto_free_blkcipher(tfm);
409 int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
411 struct crypto_hash *tfm;
412 struct hash_desc desc;
413 struct scatterlist sg;
415 tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
417 DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
423 sg_init_one(&sg, message, len);
424 crypto_hash_digest(&desc, &sg, len, out);
425 crypto_free_hash(tfm);
430 int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
431 uint8_t *key, uint32_t keylen, uint8_t *out)
433 struct crypto_hash *tfm;
434 struct hash_desc desc;
435 struct scatterlist sg;
437 tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
439 DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
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);
452 #endif /* DWC_CRYPTOLIB */
455 /* Byte Ordering Conversions */
457 uint32_t DWC_CPU_TO_LE32(uint32_t *p)
459 #ifdef __LITTLE_ENDIAN
462 uint8_t *u_p = (uint8_t *)p;
464 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
468 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
473 uint8_t *u_p = (uint8_t *)p;
475 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
479 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
481 #ifdef __LITTLE_ENDIAN
484 uint8_t *u_p = (uint8_t *)p;
486 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
490 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
495 uint8_t *u_p = (uint8_t *)p;
497 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
501 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
503 #ifdef __LITTLE_ENDIAN
506 uint8_t *u_p = (uint8_t *)p;
507 return (u_p[1] | (u_p[0] << 8));
511 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
516 uint8_t *u_p = (uint8_t *)p;
517 return (u_p[1] | (u_p[0] << 8));
521 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
523 #ifdef __LITTLE_ENDIAN
526 uint8_t *u_p = (uint8_t *)p;
527 return (u_p[1] | (u_p[0] << 8));
531 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
536 uint8_t *u_p = (uint8_t *)p;
537 return (u_p[1] | (u_p[0] << 8));
544 uint32_t DWC_READ_REG32(uint32_t volatile *reg)
550 uint64_t DWC_READ_REG64(uint64_t volatile *reg)
555 void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
561 void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
566 void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
568 writel((readl(reg) & ~clear_mask) | set_mask, reg);
572 void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
580 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
582 spinlock_t *sl = (spinlock_t *)1;
584 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
585 sl = DWC_ALLOC(sizeof(*sl));
587 DWC_ERROR("Cannot allocate memory for spinlock\n");
593 return (dwc_spinlock_t *)sl;
596 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
598 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
603 void DWC_SPINLOCK(dwc_spinlock_t *lock)
605 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
606 spin_lock((spinlock_t *)lock);
610 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
612 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
613 spin_unlock((spinlock_t *)lock);
617 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
621 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
622 spin_lock_irqsave((spinlock_t *)lock, f);
629 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
631 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
632 spin_unlock_irqrestore((spinlock_t *)lock, flags);
634 local_irq_restore(flags);
638 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
641 dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
644 DWC_ERROR("Cannot allocate memory for mutex\n");
648 m = (struct mutex *)mutex;
653 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
655 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
657 mutex_destroy((struct mutex *)mutex);
662 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
664 struct mutex *m = (struct mutex *)mutex;
668 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
670 struct mutex *m = (struct mutex *)mutex;
671 return mutex_trylock(m);
674 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
676 struct mutex *m = (struct mutex *)mutex;
683 void DWC_UDELAY(uint32_t usecs)
688 void DWC_MDELAY(uint32_t msecs)
693 void DWC_MSLEEP(uint32_t msecs)
698 uint32_t DWC_TIME(void)
700 return jiffies_to_msecs(jiffies);
709 dwc_timer_callback_t cb;
712 dwc_spinlock_t *lock;
715 static void timer_callback(struct timer_list *tt)
717 dwc_timer_t *timer = from_timer(timer, tt, t);
718 dwc_irqflags_t flags;
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);
727 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
729 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
732 DWC_ERROR("Cannot allocate memory for timer");
736 t->name = DWC_STRDUP(name);
738 DWC_ERROR("Cannot allocate memory for timer->name");
742 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
743 DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
745 t->lock = DWC_SPINLOCK_ALLOC();
748 DWC_ERROR("Cannot allocate memory for lock");
753 t->t.expires = jiffies;
754 timer_setup(&t->t, timer_callback, 0);
768 void DWC_TIMER_FREE(dwc_timer_t *timer)
770 dwc_irqflags_t flags;
772 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
774 if (timer->scheduled) {
775 del_timer(&timer->t);
776 timer->scheduled = 0;
779 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
780 DWC_SPINLOCK_FREE(timer->lock);
781 DWC_FREE(timer->name);
785 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
787 dwc_irqflags_t flags;
789 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
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);
797 DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
798 mod_timer(&timer->t, jiffies + msecs_to_jiffies(time));
801 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
804 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
806 del_timer(&timer->t);
813 wait_queue_head_t queue;
817 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
819 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
822 DWC_ERROR("Cannot allocate memory for waitqueue\n");
826 init_waitqueue_head(&wq->queue);
831 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
836 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
838 int result = wait_event_interruptible(wq->queue,
839 cond(data) || wq->abort);
840 if (result == -ERESTARTSYS) {
842 return -DWC_E_RESTART;
845 if (wq->abort == 1) {
856 return -DWC_E_UNKNOWN;
859 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
860 void *data, int32_t msecs)
863 int result = wait_event_interruptible_timeout(wq->queue,
864 cond(data) || wq->abort,
865 msecs_to_jiffies(msecs));
866 if (result == -ERESTARTSYS) {
868 return -DWC_E_RESTART;
871 if (wq->abort == 1) {
879 tmsecs = jiffies_to_msecs(result);
888 return -DWC_E_TIMEOUT;
891 return -DWC_E_UNKNOWN;
894 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
897 wake_up_interruptible(&wq->queue);
900 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
903 wake_up_interruptible(&wq->queue);
909 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
911 struct task_struct *thread = kthread_run(func, data, name);
913 if (thread == ERR_PTR(-ENOMEM)) {
917 return (dwc_thread_t *)thread;
920 int DWC_THREAD_STOP(dwc_thread_t *thread)
922 return kthread_stop((struct task_struct *)thread);
925 dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
927 return kthread_should_stop();
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
937 struct tasklet_struct t;
938 dwc_tasklet_callback_t cb;
942 static void tasklet_callback(unsigned long data)
944 dwc_tasklet_t *t = (dwc_tasklet_t *)data;
948 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
950 dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
955 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
957 DWC_ERROR("Cannot allocate memory for tasklet\n");
963 void DWC_TASK_FREE(dwc_tasklet_t *task)
968 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
970 tasklet_schedule(&task->t);
973 void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
975 tasklet_hi_schedule(&task->t);
980 - run in process context (can sleep)
982 typedef struct work_container {
983 dwc_work_callback_t cb;
989 DWC_CIRCLEQ_ENTRY(work_container) entry;
991 struct delayed_work work;
995 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
999 struct workqueue_struct *wq;
1000 dwc_spinlock_t *lock;
1005 struct work_container_queue entries;
1009 static void do_work(struct work_struct *work)
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;
1016 container->cb(container->data);
1019 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1021 DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
1022 if (container->name) {
1023 DWC_FREE(container->name);
1025 DWC_FREE(container);
1027 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1029 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1030 DWC_WAITQ_TRIGGER(wq->waitq);
1033 static int work_done(void *data)
1035 dwc_workq_t *workq = (dwc_workq_t *)data;
1036 return workq->pending == 0;
1039 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1041 return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1044 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1046 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1052 wq->wq = create_singlethread_workqueue(name);
1059 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
1060 DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
1062 wq->lock = DWC_SPINLOCK_ALLOC();
1068 wq->waitq = DWC_WAITQ_ALLOC();
1074 DWC_CIRCLEQ_INIT(&wq->entries);
1079 DWC_SPINLOCK_FREE(wq->lock);
1081 destroy_workqueue(wq->wq);
1088 void DWC_WORKQ_FREE(dwc_workq_t *wq)
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);
1099 destroy_workqueue(wq->wq);
1100 DWC_SPINLOCK_FREE(wq->lock);
1101 DWC_WAITQ_FREE(wq->waitq);
1105 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
1108 dwc_irqflags_t flags;
1109 work_container_t *container;
1110 static char name[128];
1113 va_start(args, format);
1114 DWC_VSNPRINTF(name, 128, format, args);
1117 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1119 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1120 DWC_WAITQ_TRIGGER(wq->waitq);
1122 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1124 DWC_ERROR("Cannot allocate memory for container\n");
1128 container->name = DWC_STRDUP(name);
1129 if (!container->name) {
1130 DWC_ERROR("Cannot allocate memory for container->name\n");
1131 DWC_FREE(container);
1136 container->data = data;
1138 DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
1139 INIT_WORK(&container->work.work, do_work);
1142 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1144 queue_work(wq->wq, &container->work.work);
1147 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1148 void *data, uint32_t time, char *format, ...)
1150 dwc_irqflags_t flags;
1151 work_container_t *container;
1152 static char name[128];
1155 va_start(args, format);
1156 DWC_VSNPRINTF(name, 128, format, args);
1159 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1161 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1162 DWC_WAITQ_TRIGGER(wq->waitq);
1164 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1166 DWC_ERROR("Cannot allocate memory for container\n");
1170 container->name = DWC_STRDUP(name);
1171 if (!container->name) {
1172 DWC_ERROR("Cannot allocate memory for container->name\n");
1173 DWC_FREE(container);
1178 container->data = data;
1180 DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
1181 INIT_DELAYED_WORK(&container->work, do_work);
1184 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1186 queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
1189 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1195 #ifdef DWC_LIBMODULE
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 */
1215 #ifdef DWC_CRYPTOLIB
1216 # ifndef CONFIG_MACH_IPMATE
1218 EXPORT_SYMBOL(dwc_modpow);
1221 EXPORT_SYMBOL(dwc_dh_modpow);
1222 EXPORT_SYMBOL(dwc_dh_derive_keys);
1223 EXPORT_SYMBOL(dwc_dh_pk);
1224 # endif /* CONFIG_MACH_IPMATE */
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 */
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);
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);
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);
1269 EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
1270 #endif /* DWC_UTFLIB */
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);
1284 EXPORT_SYMBOL(__DWC_DEBUG);
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);
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);
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);
1314 EXPORT_SYMBOL(DWC_READ_REG64);
1315 EXPORT_SYMBOL(DWC_WRITE_REG64);
1316 EXPORT_SYMBOL(DWC_MODIFY_REG64);
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);
1327 #if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
1328 EXPORT_SYMBOL(DWC_MUTEX_FREE);
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);
1361 static int dwc_common_port_init_module(void)
1365 printk(KERN_DEBUG "Module dwc_common_port init\n" );
1367 #ifdef DWC_DEBUG_MEMORY
1368 result = dwc_memory_debug_start(NULL);
1371 "dwc_memory_debug_start() failed with error %d\n",
1377 #ifdef DWC_NOTIFYLIB
1378 result = dwc_alloc_notification_manager(NULL, NULL);
1381 "dwc_alloc_notification_manager() failed with error %d\n",
1389 static void dwc_common_port_exit_module(void)
1391 printk(KERN_DEBUG "Module dwc_common_port exit\n" );
1393 #ifdef DWC_NOTIFYLIB
1394 dwc_free_notification_manager();
1397 #ifdef DWC_DEBUG_MEMORY
1398 dwc_memory_debug_stop();
1402 module_init(dwc_common_port_init_module);
1403 module_exit(dwc_common_port_exit_module);
1405 MODULE_DESCRIPTION("DWC Common Library - Portable version");
1406 MODULE_AUTHOR("Synopsys Inc.");
1407 MODULE_LICENSE ("GPL");
1409 #endif /* DWC_LIBMODULE */