revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / networks / atheros5000 / hal / amigaos / ah_osdep.c
blob615147e16de160cf67c09f4c6590e94d48cc5a13
1 /*
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
4 * Copyright (c) 2010 Neil Cafferkey
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * $Id$
20 #include "opt_ah.h"
22 #ifndef EXPORT_SYMTAB
23 #define EXPORT_SYMTAB
24 #endif
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <exec/memory.h>
32 #include <proto/exec.h>
33 #include <proto/timer.h>
35 #include "ah.h"
37 #ifdef AH_DEBUG
38 static int ath_hal_debug = 0;
39 #endif
41 int ath_hal_dma_beacon_response_time = 2; /* in TU's */
42 int ath_hal_sw_beacon_response_time = 10; /* in TU's */
43 int ath_hal_additional_swba_backoff = 0; /* in TU's */
45 #if 0
46 struct ath_hal *
47 _ath_hal_attach(uint16_t devid, HAL_SOFTC sc,
48 HAL_BUS_TAG t, HAL_BUS_HANDLE h, void* s)
50 HAL_STATUS status;
51 struct ath_hal *ah = ath_hal_attach(devid, sc, t, h, &status);
53 *(HAL_STATUS *)s = status;
54 return ah;
57 void
58 ath_hal_detach(struct ath_hal *ah)
60 (*ah->ah_detach)(ah);
62 #endif
65 * Print/log message support.
68 void __ahdecl
69 ath_hal_vprintf(struct ath_hal *ah, const char* fmt, va_list ap)
71 char buf[1024]; /* XXX */
72 vsnprintf(buf, sizeof(buf), fmt, ap);
73 // printk("%s", buf);
76 void __ahdecl
77 ath_hal_printf(struct ath_hal *ah, const char* fmt, ...)
79 va_list ap;
80 va_start(ap, fmt);
81 ath_hal_vprintf(ah, fmt, ap);
82 va_end(ap);
84 //EXPORT_SYMBOL(ath_hal_printf);
87 * Format an Ethernet MAC for printing.
89 const char* __ahdecl
90 ath_hal_ether_sprintf(const uint8_t *mac)
92 static char etherbuf[18];
93 snprintf(etherbuf, sizeof(etherbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
94 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
95 return etherbuf;
98 #ifdef AH_ASSERT
99 void __ahdecl
100 ath_hal_assert_failed(const char* filename, int lineno, const char *msg)
102 printk("Atheros HAL assertion failure: %s: line %u: %s\n",
103 filename, lineno, msg);
104 panic("ath_hal_assert");
106 #endif /* AH_ASSERT */
108 #ifdef AH_DEBUG_ALQ
110 * ALQ register tracing support.
112 * Setting hw.ath.hal.alq=1 enables tracing of all register reads and
113 * writes to the file /tmp/ath_hal.log. The file format is a simple
114 * fixed-size array of records. When done logging set hw.ath.hal.alq=0
115 * and then decode the file with the ardecode program (that is part of the
116 * HAL). If you start+stop tracing the data will be appended to an
117 * existing file.
119 * NB: doesn't handle multiple devices properly; only one DEVICE record
120 * is emitted and the different devices are not identified.
122 #include "alq/alq.h"
123 #include "ah_decode.h"
125 static struct alq *ath_hal_alq;
126 static int ath_hal_alq_emitdev; /* need to emit DEVICE record */
127 static u_int ath_hal_alq_lost; /* count of lost records */
128 static const char *ath_hal_logfile = "/tmp/ath_hal.log";
129 static u_int ath_hal_alq_qsize = 8*1024;
131 static int
132 ath_hal_setlogging(int enable)
134 int error;
136 if (enable) {
137 if (!capable(CAP_NET_ADMIN))
138 return -EPERM;
139 error = alq_open(&ath_hal_alq, ath_hal_logfile,
140 sizeof (struct athregrec), ath_hal_alq_qsize);
141 ath_hal_alq_lost = 0;
142 ath_hal_alq_emitdev = 1;
143 printk("ath_hal: logging to %s %s\n", ath_hal_logfile,
144 error == 0 ? "enabled" : "could not be setup");
145 } else {
146 if (ath_hal_alq)
147 alq_close(ath_hal_alq);
148 ath_hal_alq = NULL;
149 printk("ath_hal: logging disabled\n");
150 error = 0;
152 return error;
156 * Deal with the sysctl handler api changing.
158 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
159 #define AH_SYSCTL_ARGS_DECL \
160 ctl_table *ctl, int write, struct file *filp, void *buffer, \
161 size_t *lenp
162 #define AH_SYSCTL_ARGS ctl, write, filp, buffer, lenp
163 #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8) */
164 #define AH_SYSCTL_ARGS_DECL \
165 ctl_table *ctl, int write, struct file *filp, void *buffer,\
166 size_t *lenp, loff_t *ppos
167 #define AH_SYSCTL_ARGS ctl, write, filp, buffer, lenp, ppos
168 #endif
170 static int
171 sysctl_hw_ath_hal_log(AH_SYSCTL_ARGS_DECL)
173 int error, enable;
175 ctl->data = &enable;
176 ctl->maxlen = sizeof(enable);
177 enable = (ath_hal_alq != NULL);
178 error = proc_dointvec(AH_SYSCTL_ARGS);
179 if (error || !write)
180 return error;
181 else
182 return ath_hal_setlogging(enable);
185 static struct ale *
186 ath_hal_alq_get(struct ath_hal *ah)
188 struct ale *ale;
190 if (ath_hal_alq_emitdev) {
191 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
192 if (ale) {
193 struct athregrec *r =
194 (struct athregrec *) ale->ae_data;
195 r->op = OP_DEVICE;
196 r->reg = 0;
197 r->val = ah->ah_devid;
198 alq_post(ath_hal_alq, ale);
199 ath_hal_alq_emitdev = 0;
200 } else
201 ath_hal_alq_lost++;
203 ale = alq_get(ath_hal_alq, ALQ_NOWAIT);
204 if (!ale)
205 ath_hal_alq_lost++;
206 return ale;
209 void __ahdecl
210 ath_hal_reg_write(struct ath_hal *ah, uint32_t reg, uint32_t val)
212 if (ath_hal_alq) {
213 unsigned long flags;
214 struct ale *ale;
216 local_irq_save(flags);
217 ale = ath_hal_alq_get(ah);
218 if (ale) {
219 struct athregrec *r = (struct athregrec *) ale->ae_data;
220 r->op = OP_WRITE;
221 r->reg = reg;
222 r->val = val;
223 alq_post(ath_hal_alq, ale);
225 local_irq_restore(flags);
227 _OS_REG_WRITE(ah, reg, val);
229 EXPORT_SYMBOL(ath_hal_reg_write);
231 uint32_t __ahdecl
232 ath_hal_reg_read(struct ath_hal *ah, uint32_t reg)
234 uint32_t val;
236 val = _OS_REG_READ(ah, reg);
237 if (ath_hal_alq) {
238 unsigned long flags;
239 struct ale *ale;
241 local_irq_save(flags);
242 ale = ath_hal_alq_get(ah);
243 if (ale) {
244 struct athregrec *r = (struct athregrec *) ale->ae_data;
245 r->op = OP_READ;
246 r->reg = reg;
247 r->val = val;
248 alq_post(ath_hal_alq, ale);
250 local_irq_restore(flags);
252 return val;
254 EXPORT_SYMBOL(ath_hal_reg_read);
256 void __ahdecl
257 OS_MARK(struct ath_hal *ah, u_int id, uint32_t v)
259 if (ath_hal_alq) {
260 unsigned long flags;
261 struct ale *ale;
263 local_irq_save(flags);
264 ale = ath_hal_alq_get(ah);
265 if (ale) {
266 struct athregrec *r = (struct athregrec *) ale->ae_data;
267 r->op = OP_MARK;
268 r->reg = id;
269 r->val = v;
270 alq_post(ath_hal_alq, ale);
272 local_irq_restore(flags);
275 EXPORT_SYMBOL(OS_MARK);
276 #elif defined(AH_DEBUG) || defined(AH_REGOPS_FUNC)
278 * Memory-mapped device register read/write. These are here
279 * as routines when debugging support is enabled and/or when
280 * explicitly configured to use function calls. The latter is
281 * for architectures that might need to do something before
282 * referencing memory (e.g. remap an i/o window).
284 * NB: see the comments in ah_osdep.h about byte-swapping register
285 * reads and writes to understand what's going on below.
287 void __ahdecl
288 ath_hal_reg_write(struct ath_hal *ah, u_int reg, uint32_t val)
290 #ifdef AH_DEBUG
291 if (ath_hal_debug > 1)
292 ath_hal_printf(ah, "WRITE 0x%x <= 0x%x\n", reg, val);
293 #endif
294 _OS_REG_WRITE(ah, reg, val);
296 EXPORT_SYMBOL(ath_hal_reg_write);
298 uint32_t __ahdecl
299 ath_hal_reg_read(struct ath_hal *ah, u_int reg)
301 uint32_t val;
303 val = _OS_REG_READ(ah, reg);
304 #ifdef AH_DEBUG
305 if (ath_hal_debug > 1)
306 ath_hal_printf(ah, "READ 0x%x => 0x%x\n", reg, val);
307 #endif
308 return val;
310 EXPORT_SYMBOL(ath_hal_reg_read);
311 #endif /* AH_DEBUG || AH_REGOPS_FUNC */
313 #ifdef AH_DEBUG
314 void __ahdecl
315 HALDEBUG(struct ath_hal *ah, const char* fmt, ...)
317 if (ath_hal_debug) {
318 __va_list ap;
319 va_start(ap, fmt);
320 ath_hal_vprintf(ah, fmt, ap);
321 va_end(ap);
326 void __ahdecl
327 HALDEBUGn(struct ath_hal *ah, u_int level, const char* fmt, ...)
329 if (ath_hal_debug >= level) {
330 __va_list ap;
331 va_start(ap, fmt);
332 ath_hal_vprintf(ah, fmt, ap);
333 va_end(ap);
336 #endif /* AH_DEBUG */
339 * Delay n microseconds.
341 void __ahdecl
342 ath_hal_delay(int n)
344 BusyMicroDelay(n);
347 uint32_t __ahdecl
348 ath_hal_getuptime(struct ath_hal *ah)
350 // return ((jiffies / HZ) * 1000) + (jiffies % HZ) * (1000 / HZ);
351 return 0;
353 //EXPORT_SYMBOL(ath_hal_getuptime);
356 * Allocate/free memory.
359 void * __ahdecl
360 ath_hal_malloc(size_t size)
362 return AllocVec(size, MEMF_PUBLIC | MEMF_CLEAR);
365 void __ahdecl
366 ath_hal_free(void* p)
368 FreeVec(p);
371 void __ahdecl
372 ath_hal_memzero(void *dst, size_t n)
374 memset(dst, 0, n);
376 //EXPORT_SYMBOL(ath_hal_memzero);
378 void * __ahdecl
379 ath_hal_memcpy(void *dst, const void *src, size_t n)
381 return memcpy(dst, src, n);
383 //EXPORT_SYMBOL(ath_hal_memcpy);
385 int __ahdecl
386 ath_hal_memcmp(const void *a, const void *b, size_t n)
388 return memcmp(a, b, n);
390 //EXPORT_SYMBOL(ath_hal_memcmp);