Merge branch 'x86-platform-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / staging / ath6kl / os / linux / netbuf.c
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2004-2010 Atheros Communications Inc.
3 // All rights reserved.
4 //
5 // 
6 //
7 // Permission to use, copy, modify, and/or distribute this software for any
8 // purpose with or without fee is hereby granted, provided that the above
9 // copyright notice and this permission notice appear in all copies.
10 //
11 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 //
19 //
20 //
21 // Author(s): ="Atheros"
22 //------------------------------------------------------------------------------
23 #include <a_config.h>
24 #include "athdefs.h"
25 #include "a_types.h"
26 #include "a_osapi.h"
27 #include "htc_packet.h"
28
29 #define AR6000_DATA_OFFSET    64
30
31 void a_netbuf_enqueue(A_NETBUF_QUEUE_T *q, void *pkt)
32 {
33     skb_queue_tail((struct sk_buff_head *) q, (struct sk_buff *) pkt);
34 }
35
36 void a_netbuf_prequeue(A_NETBUF_QUEUE_T *q, void *pkt)
37 {
38     skb_queue_head((struct sk_buff_head *) q, (struct sk_buff *) pkt);
39 }
40
41 void *a_netbuf_dequeue(A_NETBUF_QUEUE_T *q)
42 {
43     return((void *) skb_dequeue((struct sk_buff_head *) q));
44 }
45
46 int a_netbuf_queue_size(A_NETBUF_QUEUE_T *q)
47 {
48     return(skb_queue_len((struct sk_buff_head *) q));
49 }
50
51 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q)
52 {
53     return(skb_queue_empty((struct sk_buff_head *) q));
54 }
55
56 void a_netbuf_queue_init(A_NETBUF_QUEUE_T *q)
57 {
58     skb_queue_head_init((struct sk_buff_head *) q);
59 }
60
61 void *
62 a_netbuf_alloc(int size)
63 {
64     struct sk_buff *skb;
65     size += 2 * (A_GET_CACHE_LINE_BYTES()); /* add some cacheline space at front and back of buffer */
66     skb = dev_alloc_skb(AR6000_DATA_OFFSET + sizeof(HTC_PACKET) + size);
67     skb_reserve(skb, AR6000_DATA_OFFSET + sizeof(HTC_PACKET) + A_GET_CACHE_LINE_BYTES());    
68     return ((void *)skb);
69 }
70
71 /*
72  * Allocate an SKB w.o. any encapsulation requirement.
73  */
74 void *
75 a_netbuf_alloc_raw(int size)
76 {
77     struct sk_buff *skb;
78
79     skb = dev_alloc_skb(size);
80
81     return ((void *)skb);
82 }
83
84 void
85 a_netbuf_free(void *bufPtr)
86 {
87     struct sk_buff *skb = (struct sk_buff *)bufPtr;
88
89     dev_kfree_skb(skb);
90 }
91
92 A_UINT32
93 a_netbuf_to_len(void *bufPtr)
94 {
95     return (((struct sk_buff *)bufPtr)->len);
96 }
97
98 void *
99 a_netbuf_to_data(void *bufPtr)
100 {
101     return (((struct sk_buff *)bufPtr)->data);
102 }
103
104 /*
105  * Add len # of bytes to the beginning of the network buffer
106  * pointed to by bufPtr
107  */
108 A_STATUS
109 a_netbuf_push(void *bufPtr, A_INT32 len)
110 {
111     skb_push((struct sk_buff *)bufPtr, len);
112
113     return A_OK;
114 }
115
116 /*
117  * Add len # of bytes to the beginning of the network buffer
118  * pointed to by bufPtr and also fill with data
119  */
120 A_STATUS
121 a_netbuf_push_data(void *bufPtr, char *srcPtr, A_INT32 len)
122 {
123     skb_push((struct sk_buff *) bufPtr, len);
124     A_MEMCPY(((struct sk_buff *)bufPtr)->data, srcPtr, len);
125
126     return A_OK;
127 }
128
129 /*
130  * Add len # of bytes to the end of the network buffer
131  * pointed to by bufPtr
132  */
133 A_STATUS
134 a_netbuf_put(void *bufPtr, A_INT32 len)
135 {
136     skb_put((struct sk_buff *)bufPtr, len);
137
138     return A_OK;
139 }
140
141 /*
142  * Add len # of bytes to the end of the network buffer
143  * pointed to by bufPtr and also fill with data
144  */
145 A_STATUS
146 a_netbuf_put_data(void *bufPtr, char *srcPtr, A_INT32 len)
147 {
148     char *start = (char*)(((struct sk_buff *)bufPtr)->data +
149         ((struct sk_buff *)bufPtr)->len);
150     skb_put((struct sk_buff *)bufPtr, len);
151     A_MEMCPY(start, srcPtr, len);
152
153     return A_OK;
154 }
155
156
157 /*
158  * Trim the network buffer pointed to by bufPtr to len # of bytes 
159  */
160 A_STATUS
161 a_netbuf_setlen(void *bufPtr, A_INT32 len)
162 {
163     skb_trim((struct sk_buff *)bufPtr, len);
164
165     return A_OK;
166 }
167
168 /*
169  * Chop of len # of bytes from the end of the buffer.
170  */
171 A_STATUS
172 a_netbuf_trim(void *bufPtr, A_INT32 len)
173 {
174     skb_trim((struct sk_buff *)bufPtr, ((struct sk_buff *)bufPtr)->len - len);
175
176     return A_OK;
177 }
178
179 /*
180  * Chop of len # of bytes from the end of the buffer and return the data.
181  */
182 A_STATUS
183 a_netbuf_trim_data(void *bufPtr, char *dstPtr, A_INT32 len)
184 {
185     char *start = (char*)(((struct sk_buff *)bufPtr)->data +
186         (((struct sk_buff *)bufPtr)->len - len));
187     
188     A_MEMCPY(dstPtr, start, len);
189     skb_trim((struct sk_buff *)bufPtr, ((struct sk_buff *)bufPtr)->len - len);
190
191     return A_OK;
192 }
193
194
195 /*
196  * Returns the number of bytes available to a a_netbuf_push()
197  */
198 A_INT32
199 a_netbuf_headroom(void *bufPtr)
200 {
201     return (skb_headroom((struct sk_buff *)bufPtr));
202 }
203
204 /*
205  * Removes specified number of bytes from the beginning of the buffer
206  */
207 A_STATUS
208 a_netbuf_pull(void *bufPtr, A_INT32 len)
209 {
210     skb_pull((struct sk_buff *)bufPtr, len);
211
212     return A_OK;
213 }
214
215 /*
216  * Removes specified number of bytes from the beginning of the buffer
217  * and return the data
218  */
219 A_STATUS
220 a_netbuf_pull_data(void *bufPtr, char *dstPtr, A_INT32 len)
221 {
222     A_MEMCPY(dstPtr, ((struct sk_buff *)bufPtr)->data, len);
223     skb_pull((struct sk_buff *)bufPtr, len);
224
225     return A_OK;
226 }
227
228 #ifdef EXPORT_HCI_BRIDGE_INTERFACE
229 EXPORT_SYMBOL(a_netbuf_to_data);
230 EXPORT_SYMBOL(a_netbuf_put);
231 EXPORT_SYMBOL(a_netbuf_pull);
232 EXPORT_SYMBOL(a_netbuf_alloc);
233 EXPORT_SYMBOL(a_netbuf_free);
234 #endif