usb_os_adapter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #ifndef _USB_OS_ADAPTER_H
  2. #define _USB_OS_ADAPTER_H
  3. #include "os_adapt.h"
  4. #include "board.h"
  5. #include <unistd.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define NO_GNU
  10. #define pr_err(...) TRACE_ERROR(__VA_ARGS__)
  11. #define WARN_ON_ONCE(condition) WARN_ON(condition)
  12. #define USB_UNUSED(x) ((void) x)
  13. typedef INT8 __s8;
  14. typedef UINT8 __u8;
  15. typedef INT16 __s16;
  16. typedef UINT16 __u16;
  17. typedef INT32 __s32;
  18. typedef UINT32 __u32;
  19. typedef long long __s64;
  20. typedef unsigned long long __u64;
  21. typedef __u16 __le16;
  22. typedef __u16 __be16;
  23. //typedef __u32 __le32;
  24. typedef __u32 __be32;
  25. typedef __u64 __le64;
  26. typedef __u64 __be64;
  27. typedef unsigned long ulong;
  28. typedef __u32 dev_t;
  29. #define __cpu_to_le16(x) (x)
  30. #define cpu_to_le16(x) (x)
  31. #define __le16_to_cpu le16_to_cpu
  32. #define get_unaligned(x) (*x)
  33. void iowrite32_rep(u32 addr, const void *buffer, unsigned int count);
  34. void ioread32_rep(u32 addr, void *buffer, unsigned int count);
  35. unsigned long bitmap_find_next_zero_area(unsigned long *map,
  36. unsigned long size,
  37. unsigned long start,
  38. unsigned int nr,
  39. unsigned long align_mask);
  40. void bitmap_set(unsigned long *map, unsigned int start, int len);
  41. void bitmap_clear(unsigned long *map, unsigned int start, int len);
  42. /**
  43. * list_move_tail - delete from one list and add as another's tail
  44. * @list: the entry to move
  45. * @head: the head that will follow our entry
  46. */
  47. static inline void list_move_tail(ListItem_t *item, List_t *list)
  48. {
  49. if (!listIS_CONTAINED_WITHIN(NULL, item))
  50. uxListRemove(item);
  51. vListInsertEnd(list, item);
  52. }
  53. static inline void list_del_init(ListItem_t *item)
  54. {
  55. if (!listIS_CONTAINED_WITHIN(NULL, item)) {//maybe item has removed from list
  56. uxListRemove(item);
  57. vListInitialiseItem(item);
  58. }
  59. }
  60. /**
  61. * list_add_tail - add a new entry
  62. * @new: new entry to be added
  63. * @head: list head to add it before
  64. *
  65. * Insert a new entry before the specified head.
  66. * This is useful for implementing queues.
  67. */
  68. static inline void list_add_tail(ListItem_t *item, List_t *list)
  69. {
  70. vListInsertEnd(list, item);
  71. }
  72. static inline void INIT_LIST_HEAD(List_t *list)
  73. {
  74. vListInitialise(list);
  75. }
  76. static inline void INIT_LIST_ITEM(ListItem_t *item)
  77. {
  78. vListInitialiseItem(item);
  79. }
  80. #define list_for_each_entry_safe(pxListItem, nListItem, pvOwner, list) \
  81. for (pxListItem = listGET_HEAD_ENTRY(list), \
  82. nListItem = listGET_NEXT(pxListItem), \
  83. pvOwner = listGET_LIST_ITEM_OWNER(pxListItem); \
  84. pxListItem != listGET_END_MARKER(list); \
  85. pxListItem = nListItem, \
  86. nListItem = listGET_NEXT(pxListItem), \
  87. pvOwner = listGET_LIST_ITEM_OWNER(pxListItem))
  88. #define list_for_each_safe(pxListItem, nListItem, list) \
  89. for (pxListItem = listGET_HEAD_ENTRY(list), \
  90. nListItem = listGET_NEXT(pxListItem); \
  91. pxListItem != listGET_END_MARKER(list); \
  92. pxListItem = nListItem, \
  93. nListItem = listGET_NEXT(pxListItem))
  94. #define list_del(pxListItem) uxListRemove(pxListItem)
  95. #define list_empty(pxList) listLIST_IS_EMPTY(pxList)
  96. #define list_item_empty(pxListItem) ((pxListItem)->pxContainer == NULL)
  97. #define __ARG_PLACEHOLDER_1 0,
  98. #define config_enabled(cfg) _config_enabled(cfg)
  99. #define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
  100. #define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0)
  101. #define ___config_enabled(__ignored, val, ...) val
  102. /*
  103. * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or 'm',
  104. * 0 otherwise.
  105. *
  106. */
  107. #define IS_ENABLED(option) \
  108. (config_enabled(option) || config_enabled(option##_MODULE))
  109. struct timer_list
  110. {
  111. int a;
  112. };
  113. struct unused {
  114. int a;
  115. };
  116. typedef struct unused unused_t;
  117. #define task_pid_nr(x) 0
  118. #define set_freezable(...) do { } while (0)
  119. #define try_to_freeze(...) 0
  120. #define set_current_state(...) do { } while (0)
  121. #define kthread_should_stop(...) 0
  122. #define schedule() do { } while (0)
  123. #define setup_timer(timer, func, data) do {} while (0)
  124. #define del_timer_sync(timer) do {} while (0)
  125. #define schedule_work(work) do {} while (0)
  126. #define INIT_WORK(work, fun) do {} while (0)
  127. //#define local_irq_save(flag) do {} while (0)
  128. //#define local_irq_restore(flag) do {} while (0)
  129. #define local_irq_save(flag) do {portENTER_CRITICAL(); (void)flag;} while(0)
  130. #define local_irq_restore(flag) do {portEXIT_CRITICAL(); (void)flag;} while(0)
  131. struct work_struct {
  132. int a;
  133. };
  134. struct kmem_cache {
  135. int sz;
  136. };
  137. typedef int wait_queue_head_t;
  138. typedef struct {
  139. volatile unsigned int slock;
  140. } spinlock_t;
  141. static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size)
  142. {
  143. //TODO
  144. unsigned volatile long y;
  145. switch(size){
  146. case 1:
  147. y = (*(char *)ptr) & 0x000000ff;
  148. *((char *)ptr) = (char)x;
  149. break;
  150. case 2:
  151. y = (*(short *)ptr) & 0x0000ffff;
  152. *((short *)ptr) = (short)x;
  153. break;
  154. default: // 4
  155. y = (*(unsigned long *)ptr) & 0xffffffff;
  156. *((unsigned long *)ptr) = x;
  157. break;
  158. }
  159. return y;
  160. }
  161. #define ARCH_SPIN_LOCK_UNLOCKED 1
  162. #define arch_spin_is_locked(x) (*(volatile signed char *)(&(x)->slock) <= 0)
  163. #define arch_spin_unlock_wait(x) do { barrier(); } while(spin_is_locked(x))
  164. #define xchg(ptr,v) ((unsigned int)__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
  165. #define __xg(x) ((volatile long *)(x))
  166. static inline void _raw_spin_unlock(spinlock_t *lock)
  167. {
  168. xchg(&lock->slock, 1);
  169. }
  170. static inline int _raw_spin_trylock(spinlock_t *lock)
  171. {
  172. return xchg(&lock->slock, 0) != 0 ? 1 : 0;
  173. }
  174. static inline void _raw_spin_lock(spinlock_t *lock)
  175. {
  176. volatile int was_locked;
  177. do {
  178. was_locked = xchg(&lock->slock, 0) == 0 ? 1 : 0;
  179. } while(was_locked);
  180. }
  181. #define SPINLOCK_MAGIC 0xdead4ead
  182. #define SPIN_LOCK_UNLOCKED ARCH_SPIN_LOCK_UNLOCKED
  183. #define spin_lock_init(x) do { (x)->slock = SPIN_LOCK_UNLOCKED; } while(0)
  184. #define spin_is_locked(x) arch_spin_is_locked(x)
  185. #define spin_unlock_wait(x) arch_spin_unlock_wait(x)
  186. #define _spin_trylock(lock) ({_raw_spin_trylock(lock) ? \
  187. 1 : ({ 0;});})
  188. #define _spin_lock(lock) \
  189. do { \
  190. if (!xPortIsInInterrupt()) \
  191. _raw_spin_lock(lock); \
  192. } while(0)
  193. #define _spin_unlock(lock) \
  194. do { \
  195. if (!xPortIsInInterrupt()) \
  196. _raw_spin_unlock(lock); \
  197. } while (0)
  198. #define spin_lock(lock) _spin_lock(lock)
  199. #define spin_unlock(lock) _spin_unlock(lock)
  200. #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
  201. #if USB_MODE_ID && USB_MODE_ID != -1 //usb device
  202. #define spin_lock_irqsave(lock, flags) \
  203. do { \
  204. (void)lock; flags = __get_interrupt_state(); __disable_irq(); \
  205. } while (0)
  206. #define spin_unlock_irqrestore(lock, flags) \
  207. do { \
  208. (void)lock; __set_interrupt_state(flags); \
  209. } while (0)
  210. #else
  211. #define spin_lock_irqsave(lock, flags) \
  212. do { \
  213. (void)lock; flags = __get_interrupt_state(); portENTER_CRITICAL(); \
  214. } while (0)
  215. #define spin_unlock_irqrestore(lock, flags) \
  216. do { \
  217. (void)lock; portEXIT_CRITICAL();__set_interrupt_state(flags); \
  218. } while (0)
  219. #endif
  220. #define assert_spin_locked(lock) do {} while (0)
  221. #define irqreturn_t int
  222. #define IRQ_NONE 0
  223. #define IRQ_HANDLED 1
  224. #define IRQ_WAKE_THREAD 2
  225. #define GFP_ATOMIC ((gfp_t) 0)
  226. #define GFP_KERNEL ((gfp_t) 0)
  227. #define GFP_NOFS ((gfp_t) 0)
  228. #define GFP_USER ((gfp_t) 0)
  229. #define __GFP_NOWARN ((gfp_t) 0)
  230. #define __GFP_ZERO ((gfp_t)0x8000u)
  231. #define UINT_MAX (~0U)
  232. void *kmem_cache_alloc(struct kmem_cache *obj, int flag);
  233. void kmem_cache_free(struct kmem_cache *cachep, void *obj);
  234. void kmem_cache_destroy(struct kmem_cache *cachep);
  235. void *kcalloc(size_t n, size_t size, gfp_t flags);
  236. void *kmalloc(size_t size, int flags);
  237. void *kzalloc(size_t size, gfp_t flags);
  238. void kfree(void* addr);
  239. struct device;
  240. void *devm_kzalloc(struct device *dev, size_t size, gfp_t flags);
  241. struct kmem_cache *get_mem(int element_sz);
  242. #define kmem_cache_create(a, sz, c, d, e) get_mem(sz)
  243. #define min_t(type, x, y) (x < y ? x: y)
  244. #define max_t(type, x, y) (x > y ? x: y)
  245. //#define msleep mdelay
  246. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  247. #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
  248. #define __round_mask(x, y) ((unsigned long)((y)-1))
  249. #define round_down(x, y) ((x) & ~(__round_mask((x), (y))))
  250. #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  251. //#define min(x,y) ((x) < (y) ? x : y)
  252. #define max(x,y) ((x) > (y) ? x : y)
  253. #define min3(x, y, z) min(min(x, y), z)
  254. #define max3(x, y, z) max(max(x, y), z)
  255. #define ROUND(a,b) (((a) + (b) - 1) & ~((b) - 1))
  256. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  257. #define ALIGN(x,a) __ALIGN_MASK((x),(uintptr_t)(a)-1)
  258. //#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  259. //typedef unsigned long uintptr_t;
  260. #define PAD_COUNT(s, pad) (((s) - 1) / (pad) + 1)
  261. #define PAD_SIZE(s, pad) (PAD_COUNT(s, pad) * pad)
  262. #define ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, pad) \
  263. char __##name[ROUND(PAD_SIZE((size) * sizeof(type), pad), align) \
  264. + (align - 1)]; \
  265. \
  266. type *name = (type *)ALIGN((uintptr_t)__##name, align)
  267. #define ALLOC_ALIGN_BUFFER(type, name, size, align) \
  268. ALLOC_ALIGN_BUFFER_PAD(type, name, size, align, 1)
  269. #define ALLOC_CACHE_ALIGN_BUFFER_PAD(type, name, size, pad) \
  270. ALLOC_ALIGN_BUFFER_PAD(type, name, size, ARCH_DMA_MINALIGN, pad)
  271. #define ALLOC_CACHE_ALIGN_BUFFER(type, name, size) \
  272. ALLOC_ALIGN_BUFFER(type, name, size, ARCH_DMA_MINALIGN)
  273. #define be32_to_cpu(x) ((uint32_t)( \
  274. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  275. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  276. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  277. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
  278. #define LOG2(x) (((x & 0xaaaaaaaa) ? 1 : 0) + ((x & 0xcccccccc) ? 2 : 0) + \
  279. ((x & 0xf0f0f0f0) ? 4 : 0) + ((x & 0xff00ff00) ? 8 : 0) + \
  280. ((x & 0xffff0000) ? 16 : 0))
  281. void put_unaligned_le16(u16 val, void *p);
  282. void put_unaligned_le32(u32 val, void *p);
  283. int get_unaligned_le16(void *p);
  284. int get_unaligned_le32(void *p);
  285. void generic_set_bit(int nr, volatile unsigned long *addr);
  286. int is_zero_ether_addr(const u8 *addr);
  287. int is_multicast_ether_addr(const u8 *addr);
  288. int is_local_ether_addr(const u8 *addr);
  289. int is_broadcast_ether_addr(const u8 *addr);
  290. int is_unicast_ether_addr(const u8 *addr);
  291. int is_valid_ether_addr(const u8 *addr);
  292. int get_usb_mode(void);
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif