]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
Add dwc_otg driver
[mirror_ubuntu-artful-kernel.git] / drivers / usb / host / dwc_common_port / dwc_common_fbsd.c
1 #include "dwc_os.h"
2 #include "dwc_list.h"
3
4 #ifdef DWC_CCLIB
5 # include "dwc_cc.h"
6 #endif
7
8 #ifdef DWC_CRYPTOLIB
9 # include "dwc_modpow.h"
10 # include "dwc_dh.h"
11 # include "dwc_crypto.h"
12 #endif
13
14 #ifdef DWC_NOTIFYLIB
15 # include "dwc_notifier.h"
16 #endif
17
18 /* OS-Level Implementations */
19
20 /* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
21
22
23 /* MISC */
24
25 void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
26 {
27 return memset(dest, byte, size);
28 }
29
30 void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
31 {
32 return memcpy(dest, src, size);
33 }
34
35 void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
36 {
37 bcopy(src, dest, size);
38 return dest;
39 }
40
41 int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
42 {
43 return memcmp(m1, m2, size);
44 }
45
46 int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
47 {
48 return strncmp(s1, s2, size);
49 }
50
51 int DWC_STRCMP(void *s1, void *s2)
52 {
53 return strcmp(s1, s2);
54 }
55
56 int DWC_STRLEN(char const *str)
57 {
58 return strlen(str);
59 }
60
61 char *DWC_STRCPY(char *to, char const *from)
62 {
63 return strcpy(to, from);
64 }
65
66 char *DWC_STRDUP(char const *str)
67 {
68 int len = DWC_STRLEN(str) + 1;
69 char *new = DWC_ALLOC_ATOMIC(len);
70
71 if (!new) {
72 return NULL;
73 }
74
75 DWC_MEMCPY(new, str, len);
76 return new;
77 }
78
79 int DWC_ATOI(char *str, int32_t *value)
80 {
81 char *end = NULL;
82
83 *value = strtol(str, &end, 0);
84 if (*end == '\0') {
85 return 0;
86 }
87
88 return -1;
89 }
90
91 int DWC_ATOUI(char *str, uint32_t *value)
92 {
93 char *end = NULL;
94
95 *value = strtoul(str, &end, 0);
96 if (*end == '\0') {
97 return 0;
98 }
99
100 return -1;
101 }
102
103
104 #ifdef DWC_UTFLIB
105 /* From usbstring.c */
106
107 int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
108 {
109 int count = 0;
110 u8 c;
111 u16 uchar;
112
113 /* this insists on correct encodings, though not minimal ones.
114 * BUT it currently rejects legit 4-byte UTF-8 code points,
115 * which need surrogate pairs. (Unicode 3.1 can use them.)
116 */
117 while (len != 0 && (c = (u8) *s++) != 0) {
118 if (unlikely(c & 0x80)) {
119 // 2-byte sequence:
120 // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
121 if ((c & 0xe0) == 0xc0) {
122 uchar = (c & 0x1f) << 6;
123
124 c = (u8) *s++;
125 if ((c & 0xc0) != 0xc0)
126 goto fail;
127 c &= 0x3f;
128 uchar |= c;
129
130 // 3-byte sequence (most CJKV characters):
131 // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
132 } else if ((c & 0xf0) == 0xe0) {
133 uchar = (c & 0x0f) << 12;
134
135 c = (u8) *s++;
136 if ((c & 0xc0) != 0xc0)
137 goto fail;
138 c &= 0x3f;
139 uchar |= c << 6;
140
141 c = (u8) *s++;
142 if ((c & 0xc0) != 0xc0)
143 goto fail;
144 c &= 0x3f;
145 uchar |= c;
146
147 /* no bogus surrogates */
148 if (0xd800 <= uchar && uchar <= 0xdfff)
149 goto fail;
150
151 // 4-byte sequence (surrogate pairs, currently rare):
152 // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
153 // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
154 // (uuuuu = wwww + 1)
155 // FIXME accept the surrogate code points (only)
156 } else
157 goto fail;
158 } else
159 uchar = c;
160 put_unaligned (cpu_to_le16 (uchar), cp++);
161 count++;
162 len--;
163 }
164 return count;
165 fail:
166 return -1;
167 }
168
169 #endif /* DWC_UTFLIB */
170
171
172 /* dwc_debug.h */
173
174 dwc_bool_t DWC_IN_IRQ(void)
175 {
176 // return in_irq();
177 return 0;
178 }
179
180 dwc_bool_t DWC_IN_BH(void)
181 {
182 // return in_softirq();
183 return 0;
184 }
185
186 void DWC_VPRINTF(char *format, va_list args)
187 {
188 vprintf(format, args);
189 }
190
191 int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
192 {
193 return vsnprintf(str, size, format, args);
194 }
195
196 void DWC_PRINTF(char *format, ...)
197 {
198 va_list args;
199
200 va_start(args, format);
201 DWC_VPRINTF(format, args);
202 va_end(args);
203 }
204
205 int DWC_SPRINTF(char *buffer, char *format, ...)
206 {
207 int retval;
208 va_list args;
209
210 va_start(args, format);
211 retval = vsprintf(buffer, format, args);
212 va_end(args);
213 return retval;
214 }
215
216 int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
217 {
218 int retval;
219 va_list args;
220
221 va_start(args, format);
222 retval = vsnprintf(buffer, size, format, args);
223 va_end(args);
224 return retval;
225 }
226
227 void __DWC_WARN(char *format, ...)
228 {
229 va_list args;
230
231 va_start(args, format);
232 DWC_VPRINTF(format, args);
233 va_end(args);
234 }
235
236 void __DWC_ERROR(char *format, ...)
237 {
238 va_list args;
239
240 va_start(args, format);
241 DWC_VPRINTF(format, args);
242 va_end(args);
243 }
244
245 void DWC_EXCEPTION(char *format, ...)
246 {
247 va_list args;
248
249 va_start(args, format);
250 DWC_VPRINTF(format, args);
251 va_end(args);
252 // BUG_ON(1); ???
253 }
254
255 #ifdef DEBUG
256 void __DWC_DEBUG(char *format, ...)
257 {
258 va_list args;
259
260 va_start(args, format);
261 DWC_VPRINTF(format, args);
262 va_end(args);
263 }
264 #endif
265
266
267 /* dwc_mem.h */
268
269 #if 0
270 dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
271 uint32_t align,
272 uint32_t alloc)
273 {
274 struct dma_pool *pool = dma_pool_create("Pool", NULL,
275 size, align, alloc);
276 return (dwc_pool_t *)pool;
277 }
278
279 void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
280 {
281 dma_pool_destroy((struct dma_pool *)pool);
282 }
283
284 void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
285 {
286 // return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
287 return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
288 }
289
290 void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
291 {
292 void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
293 memset(..);
294 }
295
296 void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
297 {
298 dma_pool_free(pool, vaddr, daddr);
299 }
300 #endif
301
302 static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
303 {
304 if (error)
305 return;
306 *(bus_addr_t *)arg = segs[0].ds_addr;
307 }
308
309 void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
310 {
311 dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
312 int error;
313
314 error = bus_dma_tag_create(
315 #if __FreeBSD_version >= 700000
316 bus_get_dma_tag(dma->dev), /* parent */
317 #else
318 NULL, /* parent */
319 #endif
320 4, 0, /* alignment, bounds */
321 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
322 BUS_SPACE_MAXADDR, /* highaddr */
323 NULL, NULL, /* filter, filterarg */
324 size, /* maxsize */
325 1, /* nsegments */
326 size, /* maxsegsize */
327 0, /* flags */
328 NULL, /* lockfunc */
329 NULL, /* lockarg */
330 &dma->dma_tag);
331 if (error) {
332 device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
333 __func__, error);
334 goto fail_0;
335 }
336
337 error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
338 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
339 if (error) {
340 device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
341 __func__, (uintmax_t)size, error);
342 goto fail_1;
343 }
344
345 dma->dma_paddr = 0;
346 error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
347 dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
348 if (error || dma->dma_paddr == 0) {
349 device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
350 __func__, error);
351 goto fail_2;
352 }
353
354 *dma_addr = dma->dma_paddr;
355 return dma->dma_vaddr;
356
357 fail_2:
358 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
359 fail_1:
360 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
361 bus_dma_tag_destroy(dma->dma_tag);
362 fail_0:
363 dma->dma_map = NULL;
364 dma->dma_tag = NULL;
365
366 return NULL;
367 }
368
369 void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
370 {
371 dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
372
373 if (dma->dma_tag == NULL)
374 return;
375 if (dma->dma_map != NULL) {
376 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
377 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
378 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
379 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
380 dma->dma_map = NULL;
381 }
382
383 bus_dma_tag_destroy(dma->dma_tag);
384 dma->dma_tag = NULL;
385 }
386
387 void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
388 {
389 return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
390 }
391
392 void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
393 {
394 return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
395 }
396
397 void __DWC_FREE(void *mem_ctx, void *addr)
398 {
399 free(addr, M_DEVBUF);
400 }
401
402
403 #ifdef DWC_CRYPTOLIB
404 /* dwc_crypto.h */
405
406 void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
407 {
408 get_random_bytes(buffer, length);
409 }
410
411 int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
412 {
413 struct crypto_blkcipher *tfm;
414 struct blkcipher_desc desc;
415 struct scatterlist sgd;
416 struct scatterlist sgs;
417
418 tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
419 if (tfm == NULL) {
420 printk("failed to load transform for aes CBC\n");
421 return -1;
422 }
423
424 crypto_blkcipher_setkey(tfm, key, keylen);
425 crypto_blkcipher_set_iv(tfm, iv, 16);
426
427 sg_init_one(&sgd, out, messagelen);
428 sg_init_one(&sgs, message, messagelen);
429
430 desc.tfm = tfm;
431 desc.flags = 0;
432
433 if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
434 crypto_free_blkcipher(tfm);
435 DWC_ERROR("AES CBC encryption failed");
436 return -1;
437 }
438
439 crypto_free_blkcipher(tfm);
440 return 0;
441 }
442
443 int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
444 {
445 struct crypto_hash *tfm;
446 struct hash_desc desc;
447 struct scatterlist sg;
448
449 tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
450 if (IS_ERR(tfm)) {
451 DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
452 return 0;
453 }
454 desc.tfm = tfm;
455 desc.flags = 0;
456
457 sg_init_one(&sg, message, len);
458 crypto_hash_digest(&desc, &sg, len, out);
459 crypto_free_hash(tfm);
460
461 return 1;
462 }
463
464 int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
465 uint8_t *key, uint32_t keylen, uint8_t *out)
466 {
467 struct crypto_hash *tfm;
468 struct hash_desc desc;
469 struct scatterlist sg;
470
471 tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
472 if (IS_ERR(tfm)) {
473 DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
474 return 0;
475 }
476 desc.tfm = tfm;
477 desc.flags = 0;
478
479 sg_init_one(&sg, message, messagelen);
480 crypto_hash_setkey(tfm, key, keylen);
481 crypto_hash_digest(&desc, &sg, messagelen, out);
482 crypto_free_hash(tfm);
483
484 return 1;
485 }
486
487 #endif /* DWC_CRYPTOLIB */
488
489
490 /* Byte Ordering Conversions */
491
492 uint32_t DWC_CPU_TO_LE32(uint32_t *p)
493 {
494 #ifdef __LITTLE_ENDIAN
495 return *p;
496 #else
497 uint8_t *u_p = (uint8_t *)p;
498
499 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
500 #endif
501 }
502
503 uint32_t DWC_CPU_TO_BE32(uint32_t *p)
504 {
505 #ifdef __BIG_ENDIAN
506 return *p;
507 #else
508 uint8_t *u_p = (uint8_t *)p;
509
510 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
511 #endif
512 }
513
514 uint32_t DWC_LE32_TO_CPU(uint32_t *p)
515 {
516 #ifdef __LITTLE_ENDIAN
517 return *p;
518 #else
519 uint8_t *u_p = (uint8_t *)p;
520
521 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
522 #endif
523 }
524
525 uint32_t DWC_BE32_TO_CPU(uint32_t *p)
526 {
527 #ifdef __BIG_ENDIAN
528 return *p;
529 #else
530 uint8_t *u_p = (uint8_t *)p;
531
532 return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
533 #endif
534 }
535
536 uint16_t DWC_CPU_TO_LE16(uint16_t *p)
537 {
538 #ifdef __LITTLE_ENDIAN
539 return *p;
540 #else
541 uint8_t *u_p = (uint8_t *)p;
542 return (u_p[1] | (u_p[0] << 8));
543 #endif
544 }
545
546 uint16_t DWC_CPU_TO_BE16(uint16_t *p)
547 {
548 #ifdef __BIG_ENDIAN
549 return *p;
550 #else
551 uint8_t *u_p = (uint8_t *)p;
552 return (u_p[1] | (u_p[0] << 8));
553 #endif
554 }
555
556 uint16_t DWC_LE16_TO_CPU(uint16_t *p)
557 {
558 #ifdef __LITTLE_ENDIAN
559 return *p;
560 #else
561 uint8_t *u_p = (uint8_t *)p;
562 return (u_p[1] | (u_p[0] << 8));
563 #endif
564 }
565
566 uint16_t DWC_BE16_TO_CPU(uint16_t *p)
567 {
568 #ifdef __BIG_ENDIAN
569 return *p;
570 #else
571 uint8_t *u_p = (uint8_t *)p;
572 return (u_p[1] | (u_p[0] << 8));
573 #endif
574 }
575
576
577 /* Registers */
578
579 uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
580 {
581 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
582 bus_size_t ior = (bus_size_t)reg;
583
584 return bus_space_read_4(io->iot, io->ioh, ior);
585 }
586
587 #if 0
588 uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
589 {
590 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
591 bus_size_t ior = (bus_size_t)reg;
592
593 return bus_space_read_8(io->iot, io->ioh, ior);
594 }
595 #endif
596
597 void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
598 {
599 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
600 bus_size_t ior = (bus_size_t)reg;
601
602 bus_space_write_4(io->iot, io->ioh, ior, value);
603 }
604
605 #if 0
606 void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
607 {
608 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
609 bus_size_t ior = (bus_size_t)reg;
610
611 bus_space_write_8(io->iot, io->ioh, ior, value);
612 }
613 #endif
614
615 void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
616 uint32_t set_mask)
617 {
618 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
619 bus_size_t ior = (bus_size_t)reg;
620
621 bus_space_write_4(io->iot, io->ioh, ior,
622 (bus_space_read_4(io->iot, io->ioh, ior) &
623 ~clear_mask) | set_mask);
624 }
625
626 #if 0
627 void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
628 uint64_t set_mask)
629 {
630 dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
631 bus_size_t ior = (bus_size_t)reg;
632
633 bus_space_write_8(io->iot, io->ioh, ior,
634 (bus_space_read_8(io->iot, io->ioh, ior) &
635 ~clear_mask) | set_mask);
636 }
637 #endif
638
639
640 /* Locking */
641
642 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
643 {
644 struct mtx *sl = DWC_ALLOC(sizeof(*sl));
645
646 if (!sl) {
647 DWC_ERROR("Cannot allocate memory for spinlock");
648 return NULL;
649 }
650
651 mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
652 return (dwc_spinlock_t *)sl;
653 }
654
655 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
656 {
657 struct mtx *sl = (struct mtx *)lock;
658
659 mtx_destroy(sl);
660 DWC_FREE(sl);
661 }
662
663 void DWC_SPINLOCK(dwc_spinlock_t *lock)
664 {
665 mtx_lock_spin((struct mtx *)lock); // ???
666 }
667
668 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
669 {
670 mtx_unlock_spin((struct mtx *)lock); // ???
671 }
672
673 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
674 {
675 mtx_lock_spin((struct mtx *)lock);
676 }
677
678 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
679 {
680 mtx_unlock_spin((struct mtx *)lock);
681 }
682
683 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
684 {
685 struct mtx *m;
686 dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
687
688 if (!mutex) {
689 DWC_ERROR("Cannot allocate memory for mutex");
690 return NULL;
691 }
692
693 m = (struct mtx *)mutex;
694 mtx_init(m, "dw3mtx", NULL, MTX_DEF);
695 return mutex;
696 }
697
698 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
699 #else
700 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
701 {
702 mtx_destroy((struct mtx *)mutex);
703 DWC_FREE(mutex);
704 }
705 #endif
706
707 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
708 {
709 struct mtx *m = (struct mtx *)mutex;
710
711 mtx_lock(m);
712 }
713
714 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
715 {
716 struct mtx *m = (struct mtx *)mutex;
717
718 return mtx_trylock(m);
719 }
720
721 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
722 {
723 struct mtx *m = (struct mtx *)mutex;
724
725 mtx_unlock(m);
726 }
727
728
729 /* Timing */
730
731 void DWC_UDELAY(uint32_t usecs)
732 {
733 DELAY(usecs);
734 }
735
736 void DWC_MDELAY(uint32_t msecs)
737 {
738 do {
739 DELAY(1000);
740 } while (--msecs);
741 }
742
743 void DWC_MSLEEP(uint32_t msecs)
744 {
745 struct timeval tv;
746
747 tv.tv_sec = msecs / 1000;
748 tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
749 pause("dw3slp", tvtohz(&tv));
750 }
751
752 uint32_t DWC_TIME(void)
753 {
754 struct timeval tv;
755
756 microuptime(&tv); // or getmicrouptime? (less precise, but faster)
757 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
758 }
759
760
761 /* Timers */
762
763 struct dwc_timer {
764 struct callout t;
765 char *name;
766 dwc_spinlock_t *lock;
767 dwc_timer_callback_t cb;
768 void *data;
769 };
770
771 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
772 {
773 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
774
775 if (!t) {
776 DWC_ERROR("Cannot allocate memory for timer");
777 return NULL;
778 }
779
780 callout_init(&t->t, 1);
781
782 t->name = DWC_STRDUP(name);
783 if (!t->name) {
784 DWC_ERROR("Cannot allocate memory for timer->name");
785 goto no_name;
786 }
787
788 t->lock = DWC_SPINLOCK_ALLOC();
789 if (!t->lock) {
790 DWC_ERROR("Cannot allocate memory for lock");
791 goto no_lock;
792 }
793
794 t->cb = cb;
795 t->data = data;
796
797 return t;
798
799 no_lock:
800 DWC_FREE(t->name);
801 no_name:
802 DWC_FREE(t);
803
804 return NULL;
805 }
806
807 void DWC_TIMER_FREE(dwc_timer_t *timer)
808 {
809 callout_stop(&timer->t);
810 DWC_SPINLOCK_FREE(timer->lock);
811 DWC_FREE(timer->name);
812 DWC_FREE(timer);
813 }
814
815 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
816 {
817 struct timeval tv;
818
819 tv.tv_sec = time / 1000;
820 tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
821 callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
822 }
823
824 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
825 {
826 callout_stop(&timer->t);
827 }
828
829
830 /* Wait Queues */
831
832 struct dwc_waitq {
833 struct mtx lock;
834 int abort;
835 };
836
837 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
838 {
839 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
840
841 if (!wq) {
842 DWC_ERROR("Cannot allocate memory for waitqueue");
843 return NULL;
844 }
845
846 mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
847 wq->abort = 0;
848
849 return wq;
850 }
851
852 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
853 {
854 mtx_destroy(&wq->lock);
855 DWC_FREE(wq);
856 }
857
858 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
859 {
860 // intrmask_t ipl;
861 int result = 0;
862
863 mtx_lock(&wq->lock);
864 // ipl = splbio();
865
866 /* Skip the sleep if already aborted or triggered */
867 if (!wq->abort && !cond(data)) {
868 // splx(ipl);
869 result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
870 // ipl = splbio();
871 }
872
873 if (result == ERESTART) { // signaled - restart
874 result = -DWC_E_RESTART;
875
876 } else if (result == EINTR) { // signaled - interrupt
877 result = -DWC_E_ABORT;
878
879 } else if (wq->abort) {
880 result = -DWC_E_ABORT;
881
882 } else {
883 result = 0;
884 }
885
886 wq->abort = 0;
887 // splx(ipl);
888 mtx_unlock(&wq->lock);
889 return result;
890 }
891
892 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
893 void *data, int32_t msecs)
894 {
895 struct timeval tv, tv1, tv2;
896 // intrmask_t ipl;
897 int result = 0;
898
899 tv.tv_sec = msecs / 1000;
900 tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
901
902 mtx_lock(&wq->lock);
903 // ipl = splbio();
904
905 /* Skip the sleep if already aborted or triggered */
906 if (!wq->abort && !cond(data)) {
907 // splx(ipl);
908 getmicrouptime(&tv1);
909 result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
910 getmicrouptime(&tv2);
911 // ipl = splbio();
912 }
913
914 if (result == 0) { // awoken
915 if (wq->abort) {
916 result = -DWC_E_ABORT;
917 } else {
918 tv2.tv_usec -= tv1.tv_usec;
919 if (tv2.tv_usec < 0) {
920 tv2.tv_usec += 1000000;
921 tv2.tv_sec--;
922 }
923
924 tv2.tv_sec -= tv1.tv_sec;
925 result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
926 result = msecs - result;
927 if (result <= 0)
928 result = 1;
929 }
930 } else if (result == ERESTART) { // signaled - restart
931 result = -DWC_E_RESTART;
932
933 } else if (result == EINTR) { // signaled - interrupt
934 result = -DWC_E_ABORT;
935
936 } else { // timed out
937 result = -DWC_E_TIMEOUT;
938 }
939
940 wq->abort = 0;
941 // splx(ipl);
942 mtx_unlock(&wq->lock);
943 return result;
944 }
945
946 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
947 {
948 wakeup(wq);
949 }
950
951 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
952 {
953 // intrmask_t ipl;
954
955 mtx_lock(&wq->lock);
956 // ipl = splbio();
957 wq->abort = 1;
958 wakeup(wq);
959 // splx(ipl);
960 mtx_unlock(&wq->lock);
961 }
962
963
964 /* Threading */
965
966 struct dwc_thread {
967 struct proc *proc;
968 int abort;
969 };
970
971 dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
972 {
973 int retval;
974 dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
975
976 if (!thread) {
977 return NULL;
978 }
979
980 thread->abort = 0;
981 retval = kthread_create((void (*)(void *))func, data, &thread->proc,
982 RFPROC | RFNOWAIT, 0, "%s", name);
983 if (retval) {
984 DWC_FREE(thread);
985 return NULL;
986 }
987
988 return thread;
989 }
990
991 int DWC_THREAD_STOP(dwc_thread_t *thread)
992 {
993 int retval;
994
995 thread->abort = 1;
996 retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
997
998 if (retval == 0) {
999 /* DWC_THREAD_EXIT() will free the thread struct */
1000 return 0;
1001 }
1002
1003 /* NOTE: We leak the thread struct if thread doesn't die */
1004
1005 if (retval == EWOULDBLOCK) {
1006 return -DWC_E_TIMEOUT;
1007 }
1008
1009 return -DWC_E_UNKNOWN;
1010 }
1011
1012 dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
1013 {
1014 return thread->abort;
1015 }
1016
1017 void DWC_THREAD_EXIT(dwc_thread_t *thread)
1018 {
1019 wakeup(&thread->abort);
1020 DWC_FREE(thread);
1021 kthread_exit(0);
1022 }
1023
1024
1025 /* tasklets
1026 - Runs in interrupt context (cannot sleep)
1027 - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
1028 - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
1029 */
1030 struct dwc_tasklet {
1031 struct task t;
1032 dwc_tasklet_callback_t cb;
1033 void *data;
1034 };
1035
1036 static void tasklet_callback(void *data, int pending) // what to do with pending ???
1037 {
1038 dwc_tasklet_t *task = (dwc_tasklet_t *)data;
1039
1040 task->cb(task->data);
1041 }
1042
1043 dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
1044 {
1045 dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
1046
1047 if (task) {
1048 task->cb = cb;
1049 task->data = data;
1050 TASK_INIT(&task->t, 0, tasklet_callback, task);
1051 } else {
1052 DWC_ERROR("Cannot allocate memory for tasklet");
1053 }
1054
1055 return task;
1056 }
1057
1058 void DWC_TASK_FREE(dwc_tasklet_t *task)
1059 {
1060 taskqueue_drain(taskqueue_fast, &task->t); // ???
1061 DWC_FREE(task);
1062 }
1063
1064 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
1065 {
1066 /* Uses predefined system queue */
1067 taskqueue_enqueue_fast(taskqueue_fast, &task->t);
1068 }
1069
1070
1071 /* workqueues
1072 - Runs in process context (can sleep)
1073 */
1074 typedef struct work_container {
1075 dwc_work_callback_t cb;
1076 void *data;
1077 dwc_workq_t *wq;
1078 char *name;
1079 int hz;
1080
1081 #ifdef DEBUG
1082 DWC_CIRCLEQ_ENTRY(work_container) entry;
1083 #endif
1084 struct task task;
1085 } work_container_t;
1086
1087 #ifdef DEBUG
1088 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
1089 #endif
1090
1091 struct dwc_workq {
1092 struct taskqueue *taskq;
1093 dwc_spinlock_t *lock;
1094 dwc_waitq_t *waitq;
1095 int pending;
1096
1097 #ifdef DEBUG
1098 struct work_container_queue entries;
1099 #endif
1100 };
1101
1102 static void do_work(void *data, int pending) // what to do with pending ???
1103 {
1104 work_container_t *container = (work_container_t *)data;
1105 dwc_workq_t *wq = container->wq;
1106 dwc_irqflags_t flags;
1107
1108 if (container->hz) {
1109 pause("dw3wrk", container->hz);
1110 }
1111
1112 container->cb(container->data);
1113 DWC_DEBUG("Work done: %s, container=%p", container->name, container);
1114
1115 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1116
1117 #ifdef DEBUG
1118 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
1119 #endif
1120 if (container->name)
1121 DWC_FREE(container->name);
1122 DWC_FREE(container);
1123 wq->pending--;
1124 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1125 DWC_WAITQ_TRIGGER(wq->waitq);
1126 }
1127
1128 static int work_done(void *data)
1129 {
1130 dwc_workq_t *workq = (dwc_workq_t *)data;
1131
1132 return workq->pending == 0;
1133 }
1134
1135 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
1136 {
1137 return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
1138 }
1139
1140 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
1141 {
1142 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
1143
1144 if (!wq) {
1145 DWC_ERROR("Cannot allocate memory for workqueue");
1146 return NULL;
1147 }
1148
1149 wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
1150 if (!wq->taskq) {
1151 DWC_ERROR("Cannot allocate memory for taskqueue");
1152 goto no_taskq;
1153 }
1154
1155 wq->pending = 0;
1156
1157 wq->lock = DWC_SPINLOCK_ALLOC();
1158 if (!wq->lock) {
1159 DWC_ERROR("Cannot allocate memory for spinlock");
1160 goto no_lock;
1161 }
1162
1163 wq->waitq = DWC_WAITQ_ALLOC();
1164 if (!wq->waitq) {
1165 DWC_ERROR("Cannot allocate memory for waitqueue");
1166 goto no_waitq;
1167 }
1168
1169 taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
1170
1171 #ifdef DEBUG
1172 DWC_CIRCLEQ_INIT(&wq->entries);
1173 #endif
1174 return wq;
1175
1176 no_waitq:
1177 DWC_SPINLOCK_FREE(wq->lock);
1178 no_lock:
1179 taskqueue_free(wq->taskq);
1180 no_taskq:
1181 DWC_FREE(wq);
1182
1183 return NULL;
1184 }
1185
1186 void DWC_WORKQ_FREE(dwc_workq_t *wq)
1187 {
1188 #ifdef DEBUG
1189 dwc_irqflags_t flags;
1190
1191 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1192
1193 if (wq->pending != 0) {
1194 struct work_container *container;
1195
1196 DWC_ERROR("Destroying work queue with pending work");
1197
1198 DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
1199 DWC_ERROR("Work %s still pending", container->name);
1200 }
1201 }
1202
1203 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1204 #endif
1205 DWC_WAITQ_FREE(wq->waitq);
1206 DWC_SPINLOCK_FREE(wq->lock);
1207 taskqueue_free(wq->taskq);
1208 DWC_FREE(wq);
1209 }
1210
1211 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
1212 char *format, ...)
1213 {
1214 dwc_irqflags_t flags;
1215 work_container_t *container;
1216 static char name[128];
1217 va_list args;
1218
1219 va_start(args, format);
1220 DWC_VSNPRINTF(name, 128, format, args);
1221 va_end(args);
1222
1223 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1224 wq->pending++;
1225 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1226 DWC_WAITQ_TRIGGER(wq->waitq);
1227
1228 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1229 if (!container) {
1230 DWC_ERROR("Cannot allocate memory for container");
1231 return;
1232 }
1233
1234 container->name = DWC_STRDUP(name);
1235 if (!container->name) {
1236 DWC_ERROR("Cannot allocate memory for container->name");
1237 DWC_FREE(container);
1238 return;
1239 }
1240
1241 container->cb = cb;
1242 container->data = data;
1243 container->wq = wq;
1244 container->hz = 0;
1245
1246 DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
1247
1248 TASK_INIT(&container->task, 0, do_work, container);
1249
1250 #ifdef DEBUG
1251 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1252 #endif
1253 taskqueue_enqueue_fast(wq->taskq, &container->task);
1254 }
1255
1256 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
1257 void *data, uint32_t time, char *format, ...)
1258 {
1259 dwc_irqflags_t flags;
1260 work_container_t *container;
1261 static char name[128];
1262 struct timeval tv;
1263 va_list args;
1264
1265 va_start(args, format);
1266 DWC_VSNPRINTF(name, 128, format, args);
1267 va_end(args);
1268
1269 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
1270 wq->pending++;
1271 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
1272 DWC_WAITQ_TRIGGER(wq->waitq);
1273
1274 container = DWC_ALLOC_ATOMIC(sizeof(*container));
1275 if (!container) {
1276 DWC_ERROR("Cannot allocate memory for container");
1277 return;
1278 }
1279
1280 container->name = DWC_STRDUP(name);
1281 if (!container->name) {
1282 DWC_ERROR("Cannot allocate memory for container->name");
1283 DWC_FREE(container);
1284 return;
1285 }
1286
1287 container->cb = cb;
1288 container->data = data;
1289 container->wq = wq;
1290
1291 tv.tv_sec = time / 1000;
1292 tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
1293 container->hz = tvtohz(&tv);
1294
1295 DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
1296
1297 TASK_INIT(&container->task, 0, do_work, container);
1298
1299 #ifdef DEBUG
1300 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
1301 #endif
1302 taskqueue_enqueue_fast(wq->taskq, &container->task);
1303 }
1304
1305 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
1306 {
1307 return wq->pending;
1308 }