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>
59 /** Prefix string for DWC_DEBUG print macros. */
64 void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
66 return memset(dest, byte, size);
69 void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
71 return memcpy(dest, src, size);
74 void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
76 return memmove(dest, src, size);
79 int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
81 return memcmp(m1, m2, size);
84 int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
86 return strncmp(s1, s2, size);
89 int DWC_STRCMP(void *s1, void *s2)
91 return strcmp(s1, s2);
94 int DWC_STRLEN(char const *str)
99 char *DWC_STRCPY(char *to, char const *from)
101 return strcpy(to, from);
104 char *DWC_STRDUP(char const *str)
106 int len = DWC_STRLEN(str) + 1;
107 char *new = DWC_ALLOC_ATOMIC(len);
113 DWC_MEMCPY(new, str, len);
117 int DWC_ATOI(const char *str, int32_t *value)
121 *value = simple_strtol(str, &end, 0);
129 int DWC_ATOUI(const char *str, uint32_t *value)
133 *value = simple_strtoul(str, &end, 0);
143 /* From usbstring.c */
145 int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
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.)
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;
163 if ((c & 0xc0) != 0xc0)
168 /* 3-byte sequence (most CJKV characters): */
169 /* zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx */
170 } else if ((c & 0xf0) == 0xe0) {
171 uchar = (c & 0x0f) << 12;
174 if ((c & 0xc0) != 0xc0)
180 if ((c & 0xc0) != 0xc0)
185 /* no bogus surrogates */
186 if (0xd800 <= uchar && uchar <= 0xdfff)
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) */
198 put_unaligned (cpu_to_le16 (uchar), cp++);
206 #endif /* DWC_UTFLIB */
211 dwc_bool_t DWC_IN_IRQ(void)
216 dwc_bool_t DWC_IN_BH(void)
221 void DWC_VPRINTF(char *format, va_list args)
223 vprintk(format, args);
226 int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
228 return vsnprintf(str, size, format, args);
231 void DWC_PRINTF(char *format, ...)
235 va_start(args, format);
236 DWC_VPRINTF(format, args);
240 int DWC_SPRINTF(char *buffer, char *format, ...)
245 va_start(args, format);
246 retval = vsprintf(buffer, format, args);
251 int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
256 va_start(args, format);
257 retval = vsnprintf(buffer, size, format, args);
262 void __DWC_WARN(char *format, ...)
266 va_start(args, format);
267 DWC_PRINTF(KERN_WARNING);
268 DWC_VPRINTF(format, args);
272 void __DWC_ERROR(char *format, ...)
276 va_start(args, format);
277 DWC_PRINTF(KERN_ERR);
278 DWC_VPRINTF(format, args);
282 void DWC_EXCEPTION(char *format, ...)
286 va_start(args, format);
287 DWC_PRINTF(KERN_ERR);
288 DWC_VPRINTF(format, args);
294 void __DWC_DEBUG(char *format, ...)
298 va_start(args, format);
299 DWC_PRINTF(KERN_ERR);
300 DWC_VPRINTF(format, args);
304 void __DWC_DEBUG(char *format, ...)
315 dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
319 struct dma_pool *pool = dma_pool_create("Pool", NULL,
321 return (dwc_pool_t *)pool;
324 void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
326 dma_pool_destroy((struct dma_pool *)pool);
329 void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
331 return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
334 void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
336 void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
340 void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
342 dma_pool_free(pool, vaddr, daddr);
346 void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
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);
351 void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
357 memset(buf, 0, (size_t)size);
361 void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
363 void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
367 memset(buf, 0, (size_t)size);
371 void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
373 dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
376 void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
378 return kzalloc(size, GFP_KERNEL);
381 void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
383 return kzalloc(size, GFP_ATOMIC);
386 void __DWC_FREE(void *mem_ctx, void *addr)
395 void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
397 get_random_bytes(buffer, length);
400 int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
402 struct crypto_blkcipher *tfm;
403 struct blkcipher_desc desc;
404 struct scatterlist sgd;
405 struct scatterlist sgs;
407 tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
409 printk("failed to load transform for aes CBC\n");
413 crypto_blkcipher_setkey(tfm, key, keylen);
414 crypto_blkcipher_set_iv(tfm, iv, 16);
416 sg_init_one(&sgd, out, messagelen);
417 sg_init_one(&sgs, message, messagelen);
422 if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
423 crypto_free_blkcipher(tfm);
424 DWC_ERROR("AES CBC encryption failed");
428 crypto_free_blkcipher(tfm);
432 int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
434 struct crypto_hash *tfm;
435 struct hash_desc desc;
436 struct scatterlist sg;
438 tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
440 DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
446 sg_init_one(&sg, message, len);
447 crypto_hash_digest(&desc, &sg, len, out);
448 crypto_free_hash(tfm);
453 int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
454 uint8_t *key, uint32_t keylen, uint8_t *out)
456 struct crypto_hash *tfm;
457 struct hash_desc desc;
458 struct scatterlist sg;
460 tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
462 DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
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);
475 #endif /* DWC_CRYPTOLIB */
478 /* Byte Ordering Conversions */
480 uint32_t DWC_CPU_TO_LE32(uint32_t *p)
482 #ifdef __LITTLE_ENDIAN
485 uint8_t *u_p = (uint8_t *)p;
487 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
493 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
498 uint8_t *u_p = (uint8_t *)p;
500 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
506 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
508 #ifdef __LITTLE_ENDIAN
511 uint8_t *u_p = (uint8_t *)p;
513 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
519 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
524 uint8_t *u_p = (uint8_t *)p;
526 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
532 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
534 #ifdef __LITTLE_ENDIAN
537 uint8_t *u_p = (uint8_t *)p;
539 ret = u_p[1] | (u_p[0] << 8);
544 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
549 uint8_t *u_p = (uint8_t *)p;
551 ret = u_p[1] | (u_p[0] << 8);
556 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
558 #ifdef __LITTLE_ENDIAN
561 uint8_t *u_p = (uint8_t *)p;
563 ret = u_p[1] | (u_p[0] << 8);
568 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
573 uint8_t *u_p = (uint8_t *)p;
575 ret = u_p[1] | (u_p[0] << 8);
583 uint32_t DWC_READ_REG32(volatile uint32_t *reg)
585 return readl_relaxed(reg);
589 uint64_t DWC_READ_REG64(volatile uint64_t *reg)
594 void DWC_WRITE_REG32(volatile uint32_t *reg, uint32_t value)
596 writel_relaxed(value, reg);
601 void DWC_WRITE_REG64(volatile uint64_t *reg, uint64_t value)
606 void DWC_MODIFY_REG32(volatile uint32_t *reg, uint32_t clear_mask, uint32_t set_mask)
608 writel_relaxed((readl_relaxed(reg) & ~clear_mask) | set_mask, reg);
613 void DWC_MODIFY_REG64(volatile uint64_t *reg, uint64_t clear_mask, uint64_t set_mask)
621 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
623 spinlock_t *sl = (spinlock_t *)1;
625 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
626 sl = DWC_ALLOC(sizeof(*sl));
628 DWC_ERROR("Cannot allocate memory for spinlock\n");
634 return (dwc_spinlock_t *)sl;
637 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
639 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
644 void DWC_SPINLOCK(dwc_spinlock_t *lock)
646 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
647 spin_lock((spinlock_t *)lock);
651 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
653 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
654 spin_unlock((spinlock_t *)lock);
658 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
662 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
663 spin_lock_irqsave((spinlock_t *)lock, f);
670 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
672 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
673 spin_unlock_irqrestore((spinlock_t *)lock, flags);
675 local_irq_restore(flags);
679 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
682 dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
685 DWC_ERROR("Cannot allocate memory for mutex\n");
689 m = (struct mutex *)mutex;
694 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
696 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
698 mutex_destroy((struct mutex *)mutex);
703 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
705 struct mutex *m = (struct mutex *)mutex;
709 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
711 struct mutex *m = (struct mutex *)mutex;
712 return mutex_trylock(m);
715 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
717 struct mutex *m = (struct mutex *)mutex;
724 void DWC_UDELAY(uint32_t usecs)
729 void DWC_MDELAY(uint32_t msecs)
734 void DWC_MSLEEP(uint32_t msecs)
739 uint32_t DWC_TIME(void)
741 return jiffies_to_msecs(jiffies);
748 struct timer_list *t;
750 dwc_timer_callback_t cb;
753 dwc_spinlock_t *lock;
756 static void timer_callback(unsigned long data)
758 dwc_timer_t *timer = (dwc_timer_t *)data;
759 dwc_irqflags_t flags;
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);
768 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
770 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
773 DWC_ERROR("Cannot allocate memory for timer");
777 t->t = DWC_ALLOC(sizeof(*t->t));
779 DWC_ERROR("Cannot allocate memory for timer->t");
783 t->name = DWC_STRDUP(name);
785 DWC_ERROR("Cannot allocate memory for timer->name");
789 t->lock = DWC_SPINLOCK_ALLOC();
791 DWC_ERROR("Cannot allocate memory for lock");
796 t->t->base = &boot_tvec_bases;
797 t->t->expires = jiffies;
798 setup_timer(t->t, timer_callback, (unsigned long)t);
814 void DWC_TIMER_FREE(dwc_timer_t *timer)
816 dwc_irqflags_t flags;
818 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
820 if (timer->scheduled) {
822 timer->scheduled = 0;
825 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
826 DWC_SPINLOCK_FREE(timer->lock);
828 DWC_FREE(timer->name);
832 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
834 dwc_irqflags_t flags;
836 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
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);
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));
850 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
853 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
862 wait_queue_head_t queue;
866 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
868 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
871 DWC_ERROR("Cannot allocate memory for waitqueue\n");
875 init_waitqueue_head(&wq->queue);
880 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
885 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
887 int result = wait_event_interruptible(wq->queue,
888 cond(data) || wq->abort);
889 if (result == -ERESTARTSYS) {
891 return -DWC_E_RESTART;
894 if (wq->abort == 1) {
905 return -DWC_E_UNKNOWN;
908 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
909 void *data, int32_t msecs)
912 int result = wait_event_interruptible_timeout(wq->queue,
913 cond(data) || wq->abort,
914 msecs_to_jiffies(msecs));
915 if (result == -ERESTARTSYS) {
917 return -DWC_E_RESTART;
920 if (wq->abort == 1) {
928 tmsecs = jiffies_to_msecs(result);
937 return -DWC_E_TIMEOUT;
940 return -DWC_E_UNKNOWN;
943 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
946 wake_up_interruptible(&wq->queue);
949 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
952 wake_up_interruptible(&wq->queue);
958 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
960 struct task_struct *thread = kthread_run(func, data, name);
962 if (thread == ERR_PTR(-ENOMEM)) {
966 return (dwc_thread_t *)thread;
969 int DWC_THREAD_STOP(dwc_thread_t *thread)
971 return kthread_stop((struct task_struct *)thread);
974 dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
976 return kthread_should_stop();
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
986 struct tasklet_struct t;
987 dwc_tasklet_callback_t cb;
991 static void tasklet_callback(unsigned long data)
993 dwc_tasklet_t *t = (dwc_tasklet_t *)data;
997 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
999 dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
1004 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
1006 DWC_ERROR("Cannot allocate memory for tasklet\n");
1012 void DWC_TASK_FREE(dwc_tasklet_t *task)
1017 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
1019 tasklet_schedule(&task->t);
1024 - run in process context (can sleep)
1026 typedef struct work_container {
1027 dwc_work_callback_t cb;
1033 DWC_CIRCLEQ_ENTRY(work_container) entry;
1035 struct delayed_work work;
1039 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
1043 struct workqueue_struct *wq;
1044 dwc_spinlock_t *lock;
1049 struct work_container_queue entries;
1053 static void do_work(struct work_struct *work)
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;
1060 container->cb(container->data);
1063 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1065 /* DWC_DEBUG("Work done: %s, container=%p",
1066 * container->name, container); */
1067 if (container->name) {
1068 DWC_FREE(container->name);
1070 DWC_FREE(container);
1072 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1074 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1075 DWC_WAITQ_TRIGGER(wq->waitq);
1078 static int work_done(void *data)
1080 dwc_workq_t *workq = (dwc_workq_t *)data;
1081 return workq->pending == 0;
1084 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1086 return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1089 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1091 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1097 wq->wq = create_singlethread_workqueue(name);
1104 wq->lock = DWC_SPINLOCK_ALLOC();
1109 wq->waitq = DWC_WAITQ_ALLOC();
1115 DWC_CIRCLEQ_INIT(&wq->entries);
1120 DWC_SPINLOCK_FREE(wq->lock);
1122 destroy_workqueue(wq->wq);
1129 void DWC_WORKQ_FREE(dwc_workq_t *wq)
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);
1140 destroy_workqueue(wq->wq);
1141 DWC_SPINLOCK_FREE(wq->lock);
1142 DWC_WAITQ_FREE(wq->waitq);
1146 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
1149 dwc_irqflags_t flags;
1150 work_container_t *container;
1151 static char name[128];
1154 va_start(args, format);
1155 DWC_VSNPRINTF(name, 128, format, args);
1158 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1160 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1161 DWC_WAITQ_TRIGGER(wq->waitq);
1163 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1165 DWC_ERROR("Cannot allocate memory for container\n");
1169 container->name = DWC_STRDUP(name);
1170 if (!container->name) {
1171 DWC_ERROR("Cannot allocate memory for container->name\n");
1172 DWC_FREE(container);
1177 container->data = data;
1179 /* DWC_DEBUG("Queueing work: %s, container=%p",
1180 * container->name, container);*/
1181 INIT_WORK(&container->work.work, do_work);
1184 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1186 queue_work(wq->wq, &container->work.work);
1189 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1190 void *data, uint32_t time, char *format, ...)
1192 dwc_irqflags_t flags;
1193 work_container_t *container;
1194 static char name[128];
1197 va_start(args, format);
1198 DWC_VSNPRINTF(name, 128, format, args);
1201 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1203 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1204 DWC_WAITQ_TRIGGER(wq->waitq);
1206 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1208 DWC_ERROR("Cannot allocate memory for container\n");
1212 container->name = DWC_STRDUP(name);
1213 if (!container->name) {
1214 DWC_ERROR("Cannot allocate memory for container->name\n");
1215 DWC_FREE(container);
1220 container->data = data;
1222 /* DWC_DEBUG("Queueing work: %s, container=%p",
1223 * container->name, container);*/
1224 INIT_DELAYED_WORK(&container->work, do_work);
1227 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1229 queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
1232 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1238 #ifdef DWC_LIBMODULE
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 */
1258 #ifdef DWC_CRYPTOLIB
1259 # ifndef CONFIG_MACH_IPMATE
1261 EXPORT_SYMBOL(dwc_modpow);
1264 EXPORT_SYMBOL(dwc_dh_modpow);
1265 EXPORT_SYMBOL(dwc_dh_derive_keys);
1266 EXPORT_SYMBOL(dwc_dh_pk);
1267 # endif /* CONFIG_MACH_IPMATE */
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 */
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);
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);
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);
1312 EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
1313 #endif /* DWC_UTFLIB */
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);
1327 EXPORT_SYMBOL(__DWC_DEBUG);
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);
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);
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);
1357 EXPORT_SYMBOL(DWC_READ_REG64);
1358 EXPORT_SYMBOL(DWC_WRITE_REG64);
1359 EXPORT_SYMBOL(DWC_MODIFY_REG64);
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);
1370 #if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
1371 EXPORT_SYMBOL(DWC_MUTEX_FREE);
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);
1404 static int dwc_common_port_init_module(void)
1408 printk(KERN_DEBUG "Module dwc_common_port init\n");
1410 #ifdef DWC_DEBUG_MEMORY
1411 result = dwc_memory_debug_start(NULL);
1414 "dwc_memory_debug_start() failed with error %d\n",
1420 #ifdef DWC_NOTIFYLIB
1421 result = dwc_alloc_notification_manager(NULL, NULL);
1424 "dwc_alloc_notification_manager() failed with error %d\n",
1432 static void dwc_common_port_exit_module(void)
1434 printk(KERN_DEBUG "Module dwc_common_port exit\n");
1436 #ifdef DWC_NOTIFYLIB
1437 dwc_free_notification_manager();
1440 #ifdef DWC_DEBUG_MEMORY
1441 dwc_memory_debug_stop();
1445 module_init(dwc_common_port_init_module);
1446 module_exit(dwc_common_port_exit_module);
1448 MODULE_DESCRIPTION("DWC Common Library - Portable version");
1449 MODULE_AUTHOR("Synopsys Inc.");
1450 MODULE_LICENSE ("GPL");
1452 #endif /* DWC_LIBMODULE */