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((struct device *)dma_ctx, (size_t)size,
350 dma_addr, GFP_KERNEL);
352 void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
358 memset(buf, 0, (size_t)size);
362 void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
364 void *buf = dma_alloc_coherent((struct device *)dma_ctx, (size_t)size,
365 dma_addr, GFP_ATOMIC);
369 memset(buf, 0, (size_t)size);
373 void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
375 dma_free_coherent((struct device *)dma_ctx, size, virt_addr, dma_addr);
378 void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
380 return kzalloc(size, GFP_KERNEL);
383 void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
385 return kzalloc(size, GFP_ATOMIC);
388 void __DWC_FREE(void *mem_ctx, void *addr)
397 void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
399 get_random_bytes(buffer, length);
402 int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
404 struct crypto_blkcipher *tfm;
405 struct blkcipher_desc desc;
406 struct scatterlist sgd;
407 struct scatterlist sgs;
409 tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
411 printk("failed to load transform for aes CBC\n");
415 crypto_blkcipher_setkey(tfm, key, keylen);
416 crypto_blkcipher_set_iv(tfm, iv, 16);
418 sg_init_one(&sgd, out, messagelen);
419 sg_init_one(&sgs, message, messagelen);
424 if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
425 crypto_free_blkcipher(tfm);
426 DWC_ERROR("AES CBC encryption failed");
430 crypto_free_blkcipher(tfm);
434 int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
436 struct crypto_hash *tfm;
437 struct hash_desc desc;
438 struct scatterlist sg;
440 tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
442 DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
448 sg_init_one(&sg, message, len);
449 crypto_hash_digest(&desc, &sg, len, out);
450 crypto_free_hash(tfm);
455 int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
456 uint8_t *key, uint32_t keylen, uint8_t *out)
458 struct crypto_hash *tfm;
459 struct hash_desc desc;
460 struct scatterlist sg;
462 tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
464 DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
470 sg_init_one(&sg, message, messagelen);
471 crypto_hash_setkey(tfm, key, keylen);
472 crypto_hash_digest(&desc, &sg, messagelen, out);
473 crypto_free_hash(tfm);
477 #endif /* DWC_CRYPTOLIB */
480 /* Byte Ordering Conversions */
482 uint32_t DWC_CPU_TO_LE32(uint32_t *p)
484 #ifdef __LITTLE_ENDIAN
487 uint8_t *u_p = (uint8_t *)p;
489 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
495 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
500 uint8_t *u_p = (uint8_t *)p;
502 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
508 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
510 #ifdef __LITTLE_ENDIAN
513 uint8_t *u_p = (uint8_t *)p;
515 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
521 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
526 uint8_t *u_p = (uint8_t *)p;
528 ret = u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24);
534 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
536 #ifdef __LITTLE_ENDIAN
539 uint8_t *u_p = (uint8_t *)p;
541 ret = u_p[1] | (u_p[0] << 8);
546 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
551 uint8_t *u_p = (uint8_t *)p;
553 ret = u_p[1] | (u_p[0] << 8);
558 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
560 #ifdef __LITTLE_ENDIAN
563 uint8_t *u_p = (uint8_t *)p;
565 ret = u_p[1] | (u_p[0] << 8);
570 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
575 uint8_t *u_p = (uint8_t *)p;
577 ret = u_p[1] | (u_p[0] << 8);
585 uint32_t DWC_READ_REG32(volatile uint32_t *reg)
587 return readl_relaxed(reg);
591 uint64_t DWC_READ_REG64(volatile uint64_t *reg)
596 void DWC_WRITE_REG32(volatile uint32_t *reg, uint32_t value)
598 writel_relaxed(value, reg);
603 void DWC_WRITE_REG64(volatile uint64_t *reg, uint64_t value)
608 void DWC_MODIFY_REG32(volatile uint32_t *reg, uint32_t clear_mask, uint32_t set_mask)
610 writel_relaxed((readl_relaxed(reg) & ~clear_mask) | set_mask, reg);
615 void DWC_MODIFY_REG64(volatile uint64_t *reg, uint64_t clear_mask, uint64_t set_mask)
623 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
625 spinlock_t *sl = (spinlock_t *)1;
627 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
628 sl = DWC_ALLOC(sizeof(*sl));
630 DWC_ERROR("Cannot allocate memory for spinlock\n");
636 return (dwc_spinlock_t *)sl;
639 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
641 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
646 void DWC_SPINLOCK(dwc_spinlock_t *lock)
648 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
649 spin_lock((spinlock_t *)lock);
653 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
655 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
656 spin_unlock((spinlock_t *)lock);
660 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
664 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
665 spin_lock_irqsave((spinlock_t *)lock, f);
672 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
674 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
675 spin_unlock_irqrestore((spinlock_t *)lock, flags);
677 local_irq_restore(flags);
681 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
684 dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
687 DWC_ERROR("Cannot allocate memory for mutex\n");
691 m = (struct mutex *)mutex;
696 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
698 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
700 mutex_destroy((struct mutex *)mutex);
705 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
707 struct mutex *m = (struct mutex *)mutex;
711 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
713 struct mutex *m = (struct mutex *)mutex;
714 return mutex_trylock(m);
717 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
719 struct mutex *m = (struct mutex *)mutex;
726 void DWC_UDELAY(uint32_t usecs)
731 void DWC_MDELAY(uint32_t msecs)
736 void DWC_MSLEEP(uint32_t msecs)
741 uint32_t DWC_TIME(void)
743 return jiffies_to_msecs(jiffies);
750 struct timer_list *t;
752 dwc_timer_callback_t cb;
755 dwc_spinlock_t *lock;
758 static void timer_callback(unsigned long data)
760 dwc_timer_t *timer = (dwc_timer_t *)data;
761 dwc_irqflags_t flags;
763 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
764 timer->scheduled = 0;
765 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
766 /* DWC_DEBUG("Timer %s callback", timer->name); */
767 timer->cb(timer->data);
770 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
772 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
775 DWC_ERROR("Cannot allocate memory for timer");
779 t->t = DWC_ALLOC(sizeof(*t->t));
781 DWC_ERROR("Cannot allocate memory for timer->t");
785 t->name = DWC_STRDUP(name);
787 DWC_ERROR("Cannot allocate memory for timer->name");
791 t->lock = DWC_SPINLOCK_ALLOC();
793 DWC_ERROR("Cannot allocate memory for lock");
798 t->t->expires = jiffies;
799 setup_timer(t->t, timer_callback, (unsigned long)t);
815 void DWC_TIMER_FREE(dwc_timer_t *timer)
817 dwc_irqflags_t flags;
819 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
821 if (timer->scheduled) {
823 timer->scheduled = 0;
826 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
827 DWC_SPINLOCK_FREE(timer->lock);
829 DWC_FREE(timer->name);
833 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
835 dwc_irqflags_t flags;
837 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
839 if (!timer->scheduled) {
840 timer->scheduled = 1;
841 /* DWC_DEBUG("Scheduling timer %s to expire in +%d msec",
842 * timer->name, time);*/
843 timer->t->expires = jiffies + msecs_to_jiffies(time);
846 /* DWC_DEBUG("Modifying timer %s to expire in +%d msec",
847 * timer->name, time);*/
848 mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
851 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
854 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
863 wait_queue_head_t queue;
867 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
869 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
872 DWC_ERROR("Cannot allocate memory for waitqueue\n");
876 init_waitqueue_head(&wq->queue);
881 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
886 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
888 int result = wait_event_interruptible(wq->queue,
889 cond(data) || wq->abort);
890 if (result == -ERESTARTSYS) {
892 return -DWC_E_RESTART;
895 if (wq->abort == 1) {
906 return -DWC_E_UNKNOWN;
909 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
910 void *data, int32_t msecs)
913 int result = wait_event_interruptible_timeout(wq->queue,
914 cond(data) || wq->abort,
915 msecs_to_jiffies(msecs));
916 if (result == -ERESTARTSYS) {
918 return -DWC_E_RESTART;
921 if (wq->abort == 1) {
929 tmsecs = jiffies_to_msecs(result);
938 return -DWC_E_TIMEOUT;
941 return -DWC_E_UNKNOWN;
944 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
947 wake_up_interruptible(&wq->queue);
950 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
953 wake_up_interruptible(&wq->queue);
959 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
961 struct task_struct *thread = kthread_run(func, data, name);
963 if (thread == ERR_PTR(-ENOMEM)) {
967 return (dwc_thread_t *)thread;
970 int DWC_THREAD_STOP(dwc_thread_t *thread)
972 return kthread_stop((struct task_struct *)thread);
975 dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
977 return kthread_should_stop();
982 - run in interrupt context (cannot sleep)
983 - each tasklet runs on a single CPU
984 - different tasklets can be running simultaneously on different CPUs
987 struct tasklet_struct t;
988 dwc_tasklet_callback_t cb;
992 static void tasklet_callback(unsigned long data)
994 dwc_tasklet_t *t = (dwc_tasklet_t *)data;
998 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
1000 dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
1005 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
1007 DWC_ERROR("Cannot allocate memory for tasklet\n");
1013 void DWC_TASK_FREE(dwc_tasklet_t *task)
1018 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
1020 tasklet_schedule(&task->t);
1025 - run in process context (can sleep)
1027 typedef struct work_container {
1028 dwc_work_callback_t cb;
1034 DWC_CIRCLEQ_ENTRY(work_container) entry;
1036 struct delayed_work work;
1040 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
1044 struct workqueue_struct *wq;
1045 dwc_spinlock_t *lock;
1050 struct work_container_queue entries;
1054 static void do_work(struct work_struct *work)
1056 dwc_irqflags_t flags;
1057 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1058 work_container_t *container = container_of(dw, struct work_container, work);
1059 dwc_workq_t *wq = container->wq;
1061 container->cb(container->data);
1064 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1066 /* DWC_DEBUG("Work done: %s, container=%p",
1067 * container->name, container); */
1068 if (container->name) {
1069 DWC_FREE(container->name);
1071 DWC_FREE(container);
1073 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1075 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1076 DWC_WAITQ_TRIGGER(wq->waitq);
1079 static int work_done(void *data)
1081 dwc_workq_t *workq = (dwc_workq_t *)data;
1082 return workq->pending == 0;
1085 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1087 return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1090 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1092 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1098 wq->wq = create_singlethread_workqueue(name);
1105 wq->lock = DWC_SPINLOCK_ALLOC();
1110 wq->waitq = DWC_WAITQ_ALLOC();
1116 DWC_CIRCLEQ_INIT(&wq->entries);
1121 DWC_SPINLOCK_FREE(wq->lock);
1123 destroy_workqueue(wq->wq);
1130 void DWC_WORKQ_FREE(dwc_workq_t *wq)
1133 if (wq->pending != 0) {
1134 struct work_container *wc;
1135 DWC_ERROR("Destroying work queue with pending work");
1136 DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
1137 DWC_ERROR("Work %s still pending", wc->name);
1141 destroy_workqueue(wq->wq);
1142 DWC_SPINLOCK_FREE(wq->lock);
1143 DWC_WAITQ_FREE(wq->waitq);
1147 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
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_DEBUG("Queueing work: %s, container=%p",
1181 * container->name, container);*/
1182 INIT_WORK(&container->work.work, do_work);
1185 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1187 queue_work(wq->wq, &container->work.work);
1190 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1191 void *data, uint32_t time, char *format, ...)
1193 dwc_irqflags_t flags;
1194 work_container_t *container;
1195 static char name[128];
1198 va_start(args, format);
1199 DWC_VSNPRINTF(name, 128, format, args);
1202 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1204 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1205 DWC_WAITQ_TRIGGER(wq->waitq);
1207 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1209 DWC_ERROR("Cannot allocate memory for container\n");
1213 container->name = DWC_STRDUP(name);
1214 if (!container->name) {
1215 DWC_ERROR("Cannot allocate memory for container->name\n");
1216 DWC_FREE(container);
1221 container->data = data;
1223 /* DWC_DEBUG("Queueing work: %s, container=%p",
1224 * container->name, container);*/
1225 INIT_DELAYED_WORK(&container->work, do_work);
1228 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1230 queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
1233 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1239 #ifdef DWC_LIBMODULE
1243 EXPORT_SYMBOL(dwc_cc_if_alloc);
1244 EXPORT_SYMBOL(dwc_cc_if_free);
1245 EXPORT_SYMBOL(dwc_cc_clear);
1246 EXPORT_SYMBOL(dwc_cc_add);
1247 EXPORT_SYMBOL(dwc_cc_remove);
1248 EXPORT_SYMBOL(dwc_cc_change);
1249 EXPORT_SYMBOL(dwc_cc_data_for_save);
1250 EXPORT_SYMBOL(dwc_cc_restore_from_data);
1251 EXPORT_SYMBOL(dwc_cc_match_chid);
1252 EXPORT_SYMBOL(dwc_cc_match_cdid);
1253 EXPORT_SYMBOL(dwc_cc_ck);
1254 EXPORT_SYMBOL(dwc_cc_chid);
1255 EXPORT_SYMBOL(dwc_cc_cdid);
1256 EXPORT_SYMBOL(dwc_cc_name);
1257 #endif /* DWC_CCLIB */
1259 #ifdef DWC_CRYPTOLIB
1260 # ifndef CONFIG_MACH_IPMATE
1262 EXPORT_SYMBOL(dwc_modpow);
1265 EXPORT_SYMBOL(dwc_dh_modpow);
1266 EXPORT_SYMBOL(dwc_dh_derive_keys);
1267 EXPORT_SYMBOL(dwc_dh_pk);
1268 # endif /* CONFIG_MACH_IPMATE */
1271 EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
1272 EXPORT_SYMBOL(dwc_wusb_cmf);
1273 EXPORT_SYMBOL(dwc_wusb_prf);
1274 EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
1275 EXPORT_SYMBOL(dwc_wusb_gen_nonce);
1276 EXPORT_SYMBOL(dwc_wusb_gen_key);
1277 EXPORT_SYMBOL(dwc_wusb_gen_mic);
1278 #endif /* DWC_CRYPTOLIB */
1281 #ifdef DWC_NOTIFYLIB
1282 EXPORT_SYMBOL(dwc_alloc_notification_manager);
1283 EXPORT_SYMBOL(dwc_free_notification_manager);
1284 EXPORT_SYMBOL(dwc_register_notifier);
1285 EXPORT_SYMBOL(dwc_unregister_notifier);
1286 EXPORT_SYMBOL(dwc_add_observer);
1287 EXPORT_SYMBOL(dwc_remove_observer);
1288 EXPORT_SYMBOL(dwc_notify);
1291 /* Memory Debugging Routines */
1292 #ifdef DWC_DEBUG_MEMORY
1293 EXPORT_SYMBOL(dwc_alloc_debug);
1294 EXPORT_SYMBOL(dwc_alloc_atomic_debug);
1295 EXPORT_SYMBOL(dwc_free_debug);
1296 EXPORT_SYMBOL(dwc_dma_alloc_debug);
1297 EXPORT_SYMBOL(dwc_dma_free_debug);
1300 EXPORT_SYMBOL(DWC_MEMSET);
1301 EXPORT_SYMBOL(DWC_MEMCPY);
1302 EXPORT_SYMBOL(DWC_MEMMOVE);
1303 EXPORT_SYMBOL(DWC_MEMCMP);
1304 EXPORT_SYMBOL(DWC_STRNCMP);
1305 EXPORT_SYMBOL(DWC_STRCMP);
1306 EXPORT_SYMBOL(DWC_STRLEN);
1307 EXPORT_SYMBOL(DWC_STRCPY);
1308 EXPORT_SYMBOL(DWC_STRDUP);
1309 EXPORT_SYMBOL(DWC_ATOI);
1310 EXPORT_SYMBOL(DWC_ATOUI);
1313 EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
1314 #endif /* DWC_UTFLIB */
1316 EXPORT_SYMBOL(DWC_IN_IRQ);
1317 EXPORT_SYMBOL(DWC_IN_BH);
1318 EXPORT_SYMBOL(DWC_VPRINTF);
1319 EXPORT_SYMBOL(DWC_VSNPRINTF);
1320 EXPORT_SYMBOL(DWC_PRINTF);
1321 EXPORT_SYMBOL(DWC_SPRINTF);
1322 EXPORT_SYMBOL(DWC_SNPRINTF);
1323 EXPORT_SYMBOL(__DWC_WARN);
1324 EXPORT_SYMBOL(__DWC_ERROR);
1325 EXPORT_SYMBOL(DWC_EXCEPTION);
1328 EXPORT_SYMBOL(__DWC_DEBUG);
1331 EXPORT_SYMBOL(__DWC_DMA_ALLOC);
1332 EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
1333 EXPORT_SYMBOL(__DWC_DMA_FREE);
1334 EXPORT_SYMBOL(__DWC_ALLOC);
1335 EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
1336 EXPORT_SYMBOL(__DWC_FREE);
1338 #ifdef DWC_CRYPTOLIB
1339 EXPORT_SYMBOL(DWC_RANDOM_BYTES);
1340 EXPORT_SYMBOL(DWC_AES_CBC);
1341 EXPORT_SYMBOL(DWC_SHA256);
1342 EXPORT_SYMBOL(DWC_HMAC_SHA256);
1345 EXPORT_SYMBOL(DWC_CPU_TO_LE32);
1346 EXPORT_SYMBOL(DWC_CPU_TO_BE32);
1347 EXPORT_SYMBOL(DWC_LE32_TO_CPU);
1348 EXPORT_SYMBOL(DWC_BE32_TO_CPU);
1349 EXPORT_SYMBOL(DWC_CPU_TO_LE16);
1350 EXPORT_SYMBOL(DWC_CPU_TO_BE16);
1351 EXPORT_SYMBOL(DWC_LE16_TO_CPU);
1352 EXPORT_SYMBOL(DWC_BE16_TO_CPU);
1353 EXPORT_SYMBOL(DWC_READ_REG32);
1354 EXPORT_SYMBOL(DWC_WRITE_REG32);
1355 EXPORT_SYMBOL(DWC_MODIFY_REG32);
1358 EXPORT_SYMBOL(DWC_READ_REG64);
1359 EXPORT_SYMBOL(DWC_WRITE_REG64);
1360 EXPORT_SYMBOL(DWC_MODIFY_REG64);
1363 EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
1364 EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
1365 EXPORT_SYMBOL(DWC_SPINLOCK);
1366 EXPORT_SYMBOL(DWC_SPINUNLOCK);
1367 EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
1368 EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
1369 EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
1371 #if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
1372 EXPORT_SYMBOL(DWC_MUTEX_FREE);
1375 EXPORT_SYMBOL(DWC_MUTEX_LOCK);
1376 EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
1377 EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
1378 EXPORT_SYMBOL(DWC_UDELAY);
1379 EXPORT_SYMBOL(DWC_MDELAY);
1380 EXPORT_SYMBOL(DWC_MSLEEP);
1381 EXPORT_SYMBOL(DWC_TIME);
1382 EXPORT_SYMBOL(DWC_TIMER_ALLOC);
1383 EXPORT_SYMBOL(DWC_TIMER_FREE);
1384 EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
1385 EXPORT_SYMBOL(DWC_TIMER_CANCEL);
1386 EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
1387 EXPORT_SYMBOL(DWC_WAITQ_FREE);
1388 EXPORT_SYMBOL(DWC_WAITQ_WAIT);
1389 EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
1390 EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
1391 EXPORT_SYMBOL(DWC_WAITQ_ABORT);
1392 EXPORT_SYMBOL(DWC_THREAD_RUN);
1393 EXPORT_SYMBOL(DWC_THREAD_STOP);
1394 EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
1395 EXPORT_SYMBOL(DWC_TASK_ALLOC);
1396 EXPORT_SYMBOL(DWC_TASK_FREE);
1397 EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
1398 EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
1399 EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
1400 EXPORT_SYMBOL(DWC_WORKQ_FREE);
1401 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
1402 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
1403 EXPORT_SYMBOL(DWC_WORKQ_PENDING);
1405 static int dwc_common_port_init_module(void)
1409 printk(KERN_DEBUG "Module dwc_common_port init\n");
1411 #ifdef DWC_DEBUG_MEMORY
1412 result = dwc_memory_debug_start(NULL);
1415 "dwc_memory_debug_start() failed with error %d\n",
1421 #ifdef DWC_NOTIFYLIB
1422 result = dwc_alloc_notification_manager(NULL, NULL);
1425 "dwc_alloc_notification_manager() failed with error %d\n",
1433 static void dwc_common_port_exit_module(void)
1435 printk(KERN_DEBUG "Module dwc_common_port exit\n");
1437 #ifdef DWC_NOTIFYLIB
1438 dwc_free_notification_manager();
1441 #ifdef DWC_DEBUG_MEMORY
1442 dwc_memory_debug_stop();
1446 module_init(dwc_common_port_init_module);
1447 module_exit(dwc_common_port_exit_module);
1449 MODULE_DESCRIPTION("DWC Common Library - Portable version");
1450 MODULE_AUTHOR("Synopsys Inc.");
1451 MODULE_LICENSE ("GPL");
1453 #endif /* DWC_LIBMODULE */