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)) {
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 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
491 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
496 uint8_t *u_p = (uint8_t *)p;
498 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
502 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
504 #ifdef __LITTLE_ENDIAN
507 uint8_t *u_p = (uint8_t *)p;
509 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
513 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
518 uint8_t *u_p = (uint8_t *)p;
520 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
524 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
526 #ifdef __LITTLE_ENDIAN
529 uint8_t *u_p = (uint8_t *)p;
530 return (u_p[1] | (u_p[0] << 8));
534 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
539 uint8_t *u_p = (uint8_t *)p;
540 return (u_p[1] | (u_p[0] << 8));
544 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
546 #ifdef __LITTLE_ENDIAN
549 uint8_t *u_p = (uint8_t *)p;
550 return (u_p[1] | (u_p[0] << 8));
554 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
559 uint8_t *u_p = (uint8_t *)p;
560 return (u_p[1] | (u_p[0] << 8));
567 uint32_t DWC_READ_REG32(uint32_t volatile *reg)
569 return readl_relaxed(reg);
573 uint64_t DWC_READ_REG64(uint64_t volatile *reg)
578 void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
580 writel_relaxed(value, reg);
585 void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
590 void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
592 writel_relaxed((readl_relaxed(reg) & ~clear_mask) | set_mask, reg);
597 void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
605 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
607 spinlock_t *sl = (spinlock_t *)1;
609 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
610 sl = DWC_ALLOC(sizeof(*sl));
612 DWC_ERROR("Cannot allocate memory for spinlock\n");
618 return (dwc_spinlock_t *)sl;
621 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
623 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
628 void DWC_SPINLOCK(dwc_spinlock_t *lock)
630 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
631 spin_lock((spinlock_t *)lock);
635 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
637 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
638 spin_unlock((spinlock_t *)lock);
642 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
646 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
647 spin_lock_irqsave((spinlock_t *)lock, f);
654 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
656 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
657 spin_unlock_irqrestore((spinlock_t *)lock, flags);
659 local_irq_restore(flags);
663 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
666 dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
669 DWC_ERROR("Cannot allocate memory for mutex\n");
673 m = (struct mutex *)mutex;
678 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
680 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
682 mutex_destroy((struct mutex *)mutex);
687 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
689 struct mutex *m = (struct mutex *)mutex;
693 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
695 struct mutex *m = (struct mutex *)mutex;
696 return mutex_trylock(m);
699 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
701 struct mutex *m = (struct mutex *)mutex;
708 void DWC_UDELAY(uint32_t usecs)
713 void DWC_MDELAY(uint32_t msecs)
718 void DWC_MSLEEP(uint32_t msecs)
723 uint32_t DWC_TIME(void)
725 return jiffies_to_msecs(jiffies);
732 struct timer_list *t;
734 dwc_timer_callback_t cb;
737 dwc_spinlock_t *lock;
740 static void timer_callback(unsigned long data)
742 dwc_timer_t *timer = (dwc_timer_t *)data;
743 dwc_irqflags_t flags;
745 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
746 timer->scheduled = 0;
747 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
748 //DWC_DEBUG("Timer %s callback", timer->name);
749 timer->cb(timer->data);
752 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
754 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
757 DWC_ERROR("Cannot allocate memory for timer");
761 t->t = DWC_ALLOC(sizeof(*t->t));
763 DWC_ERROR("Cannot allocate memory for timer->t");
767 t->name = DWC_STRDUP(name);
769 DWC_ERROR("Cannot allocate memory for timer->name");
773 t->lock = DWC_SPINLOCK_ALLOC();
775 DWC_ERROR("Cannot allocate memory for lock");
780 t->t->base = &boot_tvec_bases;
781 t->t->expires = jiffies;
782 setup_timer(t->t, timer_callback, (unsigned long)t);
798 void DWC_TIMER_FREE(dwc_timer_t *timer)
800 dwc_irqflags_t flags;
802 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
804 if (timer->scheduled) {
806 timer->scheduled = 0;
809 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
810 DWC_SPINLOCK_FREE(timer->lock);
812 DWC_FREE(timer->name);
816 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
818 dwc_irqflags_t flags;
820 DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
822 if (!timer->scheduled) {
823 timer->scheduled = 1;
824 //DWC_DEBUG("Scheduling timer %s to expire in +%d msec", timer->name, time);
825 timer->t->expires = jiffies + msecs_to_jiffies(time);
828 //DWC_DEBUG("Modifying timer %s to expire in +%d msec", timer->name, time);
829 mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
832 DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
835 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
844 wait_queue_head_t queue;
848 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
850 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
853 DWC_ERROR("Cannot allocate memory for waitqueue\n");
857 init_waitqueue_head(&wq->queue);
862 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
867 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
869 int result = wait_event_interruptible(wq->queue,
870 cond(data) || wq->abort);
871 if (result == -ERESTARTSYS) {
873 return -DWC_E_RESTART;
876 if (wq->abort == 1) {
887 return -DWC_E_UNKNOWN;
890 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
891 void *data, int32_t msecs)
894 int result = wait_event_interruptible_timeout(wq->queue,
895 cond(data) || wq->abort,
896 msecs_to_jiffies(msecs));
897 if (result == -ERESTARTSYS) {
899 return -DWC_E_RESTART;
902 if (wq->abort == 1) {
910 tmsecs = jiffies_to_msecs(result);
919 return -DWC_E_TIMEOUT;
922 return -DWC_E_UNKNOWN;
925 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
928 wake_up_interruptible(&wq->queue);
931 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
934 wake_up_interruptible(&wq->queue);
940 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
942 struct task_struct *thread = kthread_run(func, data, name);
944 if (thread == ERR_PTR(-ENOMEM)) {
948 return (dwc_thread_t *)thread;
951 int DWC_THREAD_STOP(dwc_thread_t *thread)
953 return kthread_stop((struct task_struct *)thread);
956 dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
958 return kthread_should_stop();
963 - run in interrupt context (cannot sleep)
964 - each tasklet runs on a single CPU
965 - different tasklets can be running simultaneously on different CPUs
968 struct tasklet_struct t;
969 dwc_tasklet_callback_t cb;
973 static void tasklet_callback(unsigned long data)
975 dwc_tasklet_t *t = (dwc_tasklet_t *)data;
979 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
981 dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
986 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
988 DWC_ERROR("Cannot allocate memory for tasklet\n");
994 void DWC_TASK_FREE(dwc_tasklet_t *task)
999 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
1001 tasklet_schedule(&task->t);
1006 - run in process context (can sleep)
1008 typedef struct work_container {
1009 dwc_work_callback_t cb;
1015 DWC_CIRCLEQ_ENTRY(work_container) entry;
1017 struct delayed_work work;
1021 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
1025 struct workqueue_struct *wq;
1026 dwc_spinlock_t *lock;
1031 struct work_container_queue entries;
1035 static void do_work(struct work_struct *work)
1037 dwc_irqflags_t flags;
1038 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1039 work_container_t *container = container_of(dw, struct work_container, work);
1040 dwc_workq_t *wq = container->wq;
1042 container->cb(container->data);
1045 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1047 //DWC_DEBUG("Work done: %s, container=%p", container->name, container);
1048 if (container->name) {
1049 DWC_FREE(container->name);
1051 DWC_FREE(container);
1053 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1055 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1056 DWC_WAITQ_TRIGGER(wq->waitq);
1059 static int work_done(void *data)
1061 dwc_workq_t *workq = (dwc_workq_t *)data;
1062 return workq->pending == 0;
1065 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1067 return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1070 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1072 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1078 wq->wq = create_singlethread_workqueue(name);
1085 wq->lock = DWC_SPINLOCK_ALLOC();
1090 wq->waitq = DWC_WAITQ_ALLOC();
1096 DWC_CIRCLEQ_INIT(&wq->entries);
1101 DWC_SPINLOCK_FREE(wq->lock);
1103 destroy_workqueue(wq->wq);
1110 void DWC_WORKQ_FREE(dwc_workq_t *wq)
1113 if (wq->pending != 0) {
1114 struct work_container *wc;
1115 DWC_ERROR("Destroying work queue with pending work");
1116 DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
1117 DWC_ERROR("Work %s still pending", wc->name);
1121 destroy_workqueue(wq->wq);
1122 DWC_SPINLOCK_FREE(wq->lock);
1123 DWC_WAITQ_FREE(wq->waitq);
1127 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
1130 dwc_irqflags_t flags;
1131 work_container_t *container;
1132 static char name[128];
1135 va_start(args, format);
1136 DWC_VSNPRINTF(name, 128, format, args);
1139 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1141 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1142 DWC_WAITQ_TRIGGER(wq->waitq);
1144 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1146 DWC_ERROR("Cannot allocate memory for container\n");
1150 container->name = DWC_STRDUP(name);
1151 if (!container->name) {
1152 DWC_ERROR("Cannot allocate memory for container->name\n");
1153 DWC_FREE(container);
1158 container->data = data;
1160 //DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
1161 INIT_WORK(&container->work.work, do_work);
1164 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1166 queue_work(wq->wq, &container->work.work);
1169 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1170 void *data, uint32_t time, char *format, ...)
1172 dwc_irqflags_t flags;
1173 work_container_t *container;
1174 static char name[128];
1177 va_start(args, format);
1178 DWC_VSNPRINTF(name, 128, format, args);
1181 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1183 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1184 DWC_WAITQ_TRIGGER(wq->waitq);
1186 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1188 DWC_ERROR("Cannot allocate memory for container\n");
1192 container->name = DWC_STRDUP(name);
1193 if (!container->name) {
1194 DWC_ERROR("Cannot allocate memory for container->name\n");
1195 DWC_FREE(container);
1200 container->data = data;
1202 //DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
1203 INIT_DELAYED_WORK(&container->work, do_work);
1206 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1208 queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
1211 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1217 #ifdef DWC_LIBMODULE
1221 EXPORT_SYMBOL(dwc_cc_if_alloc);
1222 EXPORT_SYMBOL(dwc_cc_if_free);
1223 EXPORT_SYMBOL(dwc_cc_clear);
1224 EXPORT_SYMBOL(dwc_cc_add);
1225 EXPORT_SYMBOL(dwc_cc_remove);
1226 EXPORT_SYMBOL(dwc_cc_change);
1227 EXPORT_SYMBOL(dwc_cc_data_for_save);
1228 EXPORT_SYMBOL(dwc_cc_restore_from_data);
1229 EXPORT_SYMBOL(dwc_cc_match_chid);
1230 EXPORT_SYMBOL(dwc_cc_match_cdid);
1231 EXPORT_SYMBOL(dwc_cc_ck);
1232 EXPORT_SYMBOL(dwc_cc_chid);
1233 EXPORT_SYMBOL(dwc_cc_cdid);
1234 EXPORT_SYMBOL(dwc_cc_name);
1235 #endif /* DWC_CCLIB */
1237 #ifdef DWC_CRYPTOLIB
1238 # ifndef CONFIG_MACH_IPMATE
1240 EXPORT_SYMBOL(dwc_modpow);
1243 EXPORT_SYMBOL(dwc_dh_modpow);
1244 EXPORT_SYMBOL(dwc_dh_derive_keys);
1245 EXPORT_SYMBOL(dwc_dh_pk);
1246 # endif /* CONFIG_MACH_IPMATE */
1249 EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
1250 EXPORT_SYMBOL(dwc_wusb_cmf);
1251 EXPORT_SYMBOL(dwc_wusb_prf);
1252 EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
1253 EXPORT_SYMBOL(dwc_wusb_gen_nonce);
1254 EXPORT_SYMBOL(dwc_wusb_gen_key);
1255 EXPORT_SYMBOL(dwc_wusb_gen_mic);
1256 #endif /* DWC_CRYPTOLIB */
1259 #ifdef DWC_NOTIFYLIB
1260 EXPORT_SYMBOL(dwc_alloc_notification_manager);
1261 EXPORT_SYMBOL(dwc_free_notification_manager);
1262 EXPORT_SYMBOL(dwc_register_notifier);
1263 EXPORT_SYMBOL(dwc_unregister_notifier);
1264 EXPORT_SYMBOL(dwc_add_observer);
1265 EXPORT_SYMBOL(dwc_remove_observer);
1266 EXPORT_SYMBOL(dwc_notify);
1269 /* Memory Debugging Routines */
1270 #ifdef DWC_DEBUG_MEMORY
1271 EXPORT_SYMBOL(dwc_alloc_debug);
1272 EXPORT_SYMBOL(dwc_alloc_atomic_debug);
1273 EXPORT_SYMBOL(dwc_free_debug);
1274 EXPORT_SYMBOL(dwc_dma_alloc_debug);
1275 EXPORT_SYMBOL(dwc_dma_free_debug);
1278 EXPORT_SYMBOL(DWC_MEMSET);
1279 EXPORT_SYMBOL(DWC_MEMCPY);
1280 EXPORT_SYMBOL(DWC_MEMMOVE);
1281 EXPORT_SYMBOL(DWC_MEMCMP);
1282 EXPORT_SYMBOL(DWC_STRNCMP);
1283 EXPORT_SYMBOL(DWC_STRCMP);
1284 EXPORT_SYMBOL(DWC_STRLEN);
1285 EXPORT_SYMBOL(DWC_STRCPY);
1286 EXPORT_SYMBOL(DWC_STRDUP);
1287 EXPORT_SYMBOL(DWC_ATOI);
1288 EXPORT_SYMBOL(DWC_ATOUI);
1291 EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
1292 #endif /* DWC_UTFLIB */
1294 EXPORT_SYMBOL(DWC_IN_IRQ);
1295 EXPORT_SYMBOL(DWC_IN_BH);
1296 EXPORT_SYMBOL(DWC_VPRINTF);
1297 EXPORT_SYMBOL(DWC_VSNPRINTF);
1298 EXPORT_SYMBOL(DWC_PRINTF);
1299 EXPORT_SYMBOL(DWC_SPRINTF);
1300 EXPORT_SYMBOL(DWC_SNPRINTF);
1301 EXPORT_SYMBOL(__DWC_WARN);
1302 EXPORT_SYMBOL(__DWC_ERROR);
1303 EXPORT_SYMBOL(DWC_EXCEPTION);
1306 EXPORT_SYMBOL(__DWC_DEBUG);
1309 EXPORT_SYMBOL(__DWC_DMA_ALLOC);
1310 EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
1311 EXPORT_SYMBOL(__DWC_DMA_FREE);
1312 EXPORT_SYMBOL(__DWC_ALLOC);
1313 EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
1314 EXPORT_SYMBOL(__DWC_FREE);
1316 #ifdef DWC_CRYPTOLIB
1317 EXPORT_SYMBOL(DWC_RANDOM_BYTES);
1318 EXPORT_SYMBOL(DWC_AES_CBC);
1319 EXPORT_SYMBOL(DWC_SHA256);
1320 EXPORT_SYMBOL(DWC_HMAC_SHA256);
1323 EXPORT_SYMBOL(DWC_CPU_TO_LE32);
1324 EXPORT_SYMBOL(DWC_CPU_TO_BE32);
1325 EXPORT_SYMBOL(DWC_LE32_TO_CPU);
1326 EXPORT_SYMBOL(DWC_BE32_TO_CPU);
1327 EXPORT_SYMBOL(DWC_CPU_TO_LE16);
1328 EXPORT_SYMBOL(DWC_CPU_TO_BE16);
1329 EXPORT_SYMBOL(DWC_LE16_TO_CPU);
1330 EXPORT_SYMBOL(DWC_BE16_TO_CPU);
1331 EXPORT_SYMBOL(DWC_READ_REG32);
1332 EXPORT_SYMBOL(DWC_WRITE_REG32);
1333 EXPORT_SYMBOL(DWC_MODIFY_REG32);
1336 EXPORT_SYMBOL(DWC_READ_REG64);
1337 EXPORT_SYMBOL(DWC_WRITE_REG64);
1338 EXPORT_SYMBOL(DWC_MODIFY_REG64);
1341 EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
1342 EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
1343 EXPORT_SYMBOL(DWC_SPINLOCK);
1344 EXPORT_SYMBOL(DWC_SPINUNLOCK);
1345 EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
1346 EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
1347 EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
1349 #if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
1350 EXPORT_SYMBOL(DWC_MUTEX_FREE);
1353 EXPORT_SYMBOL(DWC_MUTEX_LOCK);
1354 EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
1355 EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
1356 EXPORT_SYMBOL(DWC_UDELAY);
1357 EXPORT_SYMBOL(DWC_MDELAY);
1358 EXPORT_SYMBOL(DWC_MSLEEP);
1359 EXPORT_SYMBOL(DWC_TIME);
1360 EXPORT_SYMBOL(DWC_TIMER_ALLOC);
1361 EXPORT_SYMBOL(DWC_TIMER_FREE);
1362 EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
1363 EXPORT_SYMBOL(DWC_TIMER_CANCEL);
1364 EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
1365 EXPORT_SYMBOL(DWC_WAITQ_FREE);
1366 EXPORT_SYMBOL(DWC_WAITQ_WAIT);
1367 EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
1368 EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
1369 EXPORT_SYMBOL(DWC_WAITQ_ABORT);
1370 EXPORT_SYMBOL(DWC_THREAD_RUN);
1371 EXPORT_SYMBOL(DWC_THREAD_STOP);
1372 EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
1373 EXPORT_SYMBOL(DWC_TASK_ALLOC);
1374 EXPORT_SYMBOL(DWC_TASK_FREE);
1375 EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
1376 EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
1377 EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
1378 EXPORT_SYMBOL(DWC_WORKQ_FREE);
1379 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
1380 EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
1381 EXPORT_SYMBOL(DWC_WORKQ_PENDING);
1383 static int dwc_common_port_init_module(void)
1387 printk(KERN_DEBUG "Module dwc_common_port init\n" );
1389 #ifdef DWC_DEBUG_MEMORY
1390 result = dwc_memory_debug_start(NULL);
1393 "dwc_memory_debug_start() failed with error %d\n",
1399 #ifdef DWC_NOTIFYLIB
1400 result = dwc_alloc_notification_manager(NULL, NULL);
1403 "dwc_alloc_notification_manager() failed with error %d\n",
1411 static void dwc_common_port_exit_module(void)
1413 printk(KERN_DEBUG "Module dwc_common_port exit\n" );
1415 #ifdef DWC_NOTIFYLIB
1416 dwc_free_notification_manager();
1419 #ifdef DWC_DEBUG_MEMORY
1420 dwc_memory_debug_stop();
1424 module_init(dwc_common_port_init_module);
1425 module_exit(dwc_common_port_exit_module);
1427 MODULE_DESCRIPTION("DWC Common Library - Portable version");
1428 MODULE_AUTHOR("Synopsys Inc.");
1429 MODULE_LICENSE ("GPL");
1431 #endif /* DWC_LIBMODULE */