Original 20060124 tarball
[acx-mac80211.git] / acx_func.h
bloba8e2482cd326650499689415fb5fd7050226309d
1 /***********************************************************************
2 ** Copyright (C) 2003 ACX100 Open Source Project
3 **
4 ** The contents of this file are subject to the Mozilla Public
5 ** License Version 1.1 (the "License"); you may not use this file
6 ** except in compliance with the License. You may obtain a copy of
7 ** the License at http://www.mozilla.org/MPL/
8 **
9 ** Software distributed under the License is distributed on an "AS
10 ** IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 ** implied. See the License for the specific language governing
12 ** rights and limitations under the License.
14 ** Alternatively, the contents of this file may be used under the
15 ** terms of the GNU Public License version 2 (the "GPL"), in which
16 ** case the provisions of the GPL are applicable instead of the
17 ** above. If you wish to allow the use of your version of this file
18 ** only under the terms of the GPL and not to allow others to use
19 ** your version of this file under the MPL, indicate your decision
20 ** by deleting the provisions above and replace them with the notice
21 ** and other provisions required by the GPL. If you do not delete
22 ** the provisions above, a recipient may use your version of this
23 ** file under either the MPL or the GPL.
24 ** ---------------------------------------------------------------------
25 ** Inquiries regarding the ACX100 Open Source Project can be
26 ** made directly to:
28 ** acx100-users@lists.sf.net
29 ** http://acx100.sf.net
30 ** ---------------------------------------------------------------------
34 /***********************************************************************
35 ** LOGGING
37 ** - Avoid SHOUTING needlessly. Avoid excessive verbosity.
38 ** Gradually remove messages which are old debugging aids.
40 ** - Use printk() for messages which are to be always logged.
41 ** Supply either 'acx:' or '<devname>:' prefix so that user
42 ** can figure out who's speaking among other kernel chatter.
43 ** acx: is for general issues (e.g. "acx: no firmware image!")
44 ** while <devname>: is related to a particular device
45 ** (think about multi-card setup). Double check that message
46 ** is not confusing to the average user.
48 ** - use printk KERN_xxx level only if message is not a WARNING
49 ** but is INFO, ERR etc.
51 ** - Use printk_ratelimited() for messages which may flood
52 ** (e.g. "rx DUP pkt!").
54 ** - Use log() for messages which may be omitted (and they
55 ** _will_ be omitted in non-debug builds). Note that
56 ** message levels may be disabled at compile-time selectively,
57 ** thus select them wisely. Example: L_DEBUG is the lowest
58 ** (most likely to be compiled out) -> use for less important stuff.
60 ** - Do not print important stuff with log(), or else people
61 ** will never build non-debug driver.
63 ** Style:
64 ** hex: capital letters, zero filled (e.g. 0x02AC)
65 ** str: dont start from capitals, no trailing periods ("tx: queue is stopped")
67 #if ACX_DEBUG > 1
69 void log_fn_enter(const char *funcname);
70 void log_fn_exit(const char *funcname);
71 void log_fn_exit_v(const char *funcname, int v);
73 #define FN_ENTER \
74 do { \
75 if (unlikely(acx_debug & L_FUNC)) { \
76 log_fn_enter(__func__); \
77 } \
78 } while (0)
80 #define FN_EXIT1(v) \
81 do { \
82 if (unlikely(acx_debug & L_FUNC)) { \
83 log_fn_exit_v(__func__, v); \
84 } \
85 } while (0)
86 #define FN_EXIT0 \
87 do { \
88 if (unlikely(acx_debug & L_FUNC)) { \
89 log_fn_exit(__func__); \
90 } \
91 } while (0)
93 #else
95 #define FN_ENTER
96 #define FN_EXIT1(v)
97 #define FN_EXIT0
99 #endif /* ACX_DEBUG > 1 */
102 #if ACX_DEBUG
104 #define log(chan, args...) \
105 do { \
106 if (acx_debug & (chan)) \
107 printk(args); \
108 } while (0)
109 #define printk_ratelimited(args...) printk(args)
111 #else /* Non-debug build: */
113 #define log(chan, args...)
114 /* Standard way of log flood prevention */
115 #define printk_ratelimited(args...) \
116 do { \
117 if (printk_ratelimit()) \
118 printk(args); \
119 } while (0)
121 #endif /* ACX_DEBUG */
123 void acx_print_mac(const char *head, const u8 *mac, const char *tail);
125 /* Optimized out to nothing in non-debug build */
126 static inline void
127 acxlog_mac(int level, const char *head, const u8 *mac, const char *tail)
129 if (acx_debug & level) {
130 acx_print_mac(head, mac, tail);
135 /***********************************************************************
136 ** MAC address helpers
138 static inline void
139 MAC_COPY(u8 *mac, const u8 *src)
141 *(u32*)mac = *(u32*)src;
142 ((u16*)mac)[2] = ((u16*)src)[2];
143 /* kernel's memcpy will do the same: memcpy(dst, src, ETH_ALEN); */
146 static inline void
147 MAC_FILL(u8 *mac, u8 val)
149 memset(mac, val, ETH_ALEN);
152 static inline void
153 MAC_BCAST(u8 *mac)
155 ((u16*)mac)[2] = *(u32*)mac = -1;
158 static inline void
159 MAC_ZERO(u8 *mac)
161 ((u16*)mac)[2] = *(u32*)mac = 0;
164 static inline int
165 mac_is_equal(const u8 *a, const u8 *b)
167 /* can't beat this */
168 return memcmp(a, b, ETH_ALEN) == 0;
171 static inline int
172 mac_is_bcast(const u8 *mac)
174 /* AND together 4 first bytes with sign-extended 2 last bytes
175 ** Only bcast address gives 0xffffffff. +1 gives 0 */
176 return ( *(s32*)mac & ((s16*)mac)[2] ) + 1 == 0;
179 static inline int
180 mac_is_zero(const u8 *mac)
182 return ( *(u32*)mac | ((u16*)mac)[2] ) == 0;
185 static inline int
186 mac_is_directed(const u8 *mac)
188 return (mac[0] & 1)==0;
191 static inline int
192 mac_is_mcast(const u8 *mac)
194 return (mac[0] & 1) && !mac_is_bcast(mac);
197 #define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"
198 #define MAC(bytevector) \
199 ((unsigned char *)bytevector)[0], \
200 ((unsigned char *)bytevector)[1], \
201 ((unsigned char *)bytevector)[2], \
202 ((unsigned char *)bytevector)[3], \
203 ((unsigned char *)bytevector)[4], \
204 ((unsigned char *)bytevector)[5]
207 /***********************************************************************
208 ** Random helpers
210 #define TO_STRING(x) #x
211 #define STRING(x) TO_STRING(x)
213 #define CLEAR_BIT(val, mask) ((val) &= ~(mask))
214 #define SET_BIT(val, mask) ((val) |= (mask))
216 /* undefined if v==0 */
217 static inline unsigned int
218 lowest_bit(u16 v)
220 unsigned int n = 0;
221 while (!(v & 0xf)) { v>>=4; n+=4; }
222 while (!(v & 1)) { v>>=1; n++; }
223 return n;
226 /* undefined if v==0 */
227 static inline unsigned int
228 highest_bit(u16 v)
230 unsigned int n = 0;
231 while (v>0xf) { v>>=4; n+=4; }
232 while (v>1) { v>>=1; n++; }
233 return n;
236 /* undefined if v==0 */
237 static inline int
238 has_only_one_bit(u16 v)
240 return ((v-1) ^ v) >= v;
244 /***********************************************************************
245 ** LOCKING
246 ** We have adev->sem and adev->lock.
248 ** We employ following naming convention in order to get locking right:
250 ** acx_e_xxxx - external entry points called from process context.
251 ** It is okay to sleep. adev->sem is to be taken on entry.
252 ** acx_i_xxxx - external entry points possibly called from atomic context.
253 ** Sleeping is not allowed (and thus down(sem) is not legal!)
254 ** acx_s_xxxx - potentially sleeping functions. Do not ever call under lock!
255 ** acx_l_xxxx - functions which expect lock to be already taken.
256 ** rest - non-sleeping functions which do not require locking
257 ** but may be run under lock
259 ** A small number of local helpers do not have acx_[eisl]_ prefix.
260 ** They are always close to caller and are to be revieved locally.
262 ** Theory of operation:
264 ** All process-context entry points (_e_ functions) take sem
265 ** immediately. IRQ handler and other 'atomic-context' entry points
266 ** (_i_ functions) take lock immediately on entry, but dont take sem
267 ** because that might sleep.
269 ** Thus *all* code is either protected by sem or lock, or both.
271 ** Code which must not run concurrently with IRQ takes lock.
272 ** Such code is marked with _l_.
274 ** This results in the following rules of thumb useful in code review:
276 ** + If a function calls _s_ fn, it must be an _s_ itself.
277 ** + You can call _l_ fn only (a) from another _l_ fn
278 ** or (b) from _s_, _e_ or _i_ fn by taking lock, calling _l_,
279 ** and dropping lock.
280 ** + All IRQ code runs under lock.
281 ** + Any _s_ fn is running under sem.
282 ** + Code under sem can race only with IRQ code.
283 ** + Code under sem+lock cannot race with anything.
286 /* These functions *must* be inline or they will break horribly on SPARC, due
287 * to its weird semantics for save/restore flags */
289 #if defined(PARANOID_LOCKING) /* Lock debugging */
291 void acx_lock_debug(acx_device_t *adev, const char* where);
292 void acx_unlock_debug(acx_device_t *adev, const char* where);
293 void acx_down_debug(acx_device_t *adev, const char* where);
294 void acx_up_debug(acx_device_t *adev, const char* where);
295 void acx_lock_unhold(void);
296 void acx_sem_unhold(void);
298 static inline void
299 acx_lock_helper(acx_device_t *adev, unsigned long *fp, const char* where)
301 acx_lock_debug(adev, where);
302 spin_lock_irqsave(&adev->lock, *fp);
304 static inline void
305 acx_unlock_helper(acx_device_t *adev, unsigned long *fp, const char* where)
307 acx_unlock_debug(adev, where);
308 spin_unlock_irqrestore(&adev->lock, *fp);
310 static inline void
311 acx_down_helper(acx_device_t *adev, const char* where)
313 acx_down_debug(adev, where);
315 static inline void
316 acx_up_helper(acx_device_t *adev, const char* where)
318 acx_up_debug(adev, where);
320 #define acx_lock(adev, flags) acx_lock_helper(adev, &(flags), __FILE__ ":" STRING(__LINE__))
321 #define acx_unlock(adev, flags) acx_unlock_helper(adev, &(flags), __FILE__ ":" STRING(__LINE__))
322 #define acx_sem_lock(adev) acx_down_helper(adev, __FILE__ ":" STRING(__LINE__))
323 #define acx_sem_unlock(adev) acx_up_helper(adev, __FILE__ ":" STRING(__LINE__))
325 #elif defined(DO_LOCKING)
327 #define acx_lock(adev, flags) spin_lock_irqsave(&adev->lock, flags)
328 #define acx_unlock(adev, flags) spin_unlock_irqrestore(&adev->lock, flags)
329 #define acx_sem_lock(adev) down(&adev->sem)
330 #define acx_sem_unlock(adev) up(&adev->sem)
331 #define acx_lock_unhold() ((void)0)
332 #define acx_sem_unhold() ((void)0)
334 #else /* no locking! :( */
336 #define acx_lock(adev, flags) ((void)0)
337 #define acx_unlock(adev, flags) ((void)0)
338 #define acx_sem_lock(adev) ((void)0)
339 #define acx_sem_unlock(adev) ((void)0)
340 #define acx_lock_unhold() ((void)0)
341 #define acx_sem_unhold() ((void)0)
343 #endif
346 /***********************************************************************
349 /* Can race with rx path (which is not protected by sem):
350 ** rx -> process_[re]assocresp() -> set_status(ASSOCIATED) -> wake_queue()
351 ** Can race with tx_complete IRQ:
352 ** IRQ -> acxpci_l_clean_txdesc -> acx_wake_queue
353 ** Review carefully all callsites */
354 static inline void
355 acx_stop_queue(struct net_device *ndev, const char *msg)
357 if (netif_queue_stopped(ndev))
358 return;
360 netif_stop_queue(ndev);
361 if (msg)
362 log(L_BUFT, "tx: stop queue %s\n", msg);
365 static inline int
366 acx_queue_stopped(struct net_device *ndev)
368 return netif_queue_stopped(ndev);
372 static inline void
373 acx_start_queue(struct net_device *ndev, const char *msg)
375 netif_start_queue(ndev);
376 if (msg)
377 log(L_BUFT, "tx: start queue %s\n", msg);
381 static inline void
382 acx_wake_queue(struct net_device *ndev, const char *msg)
384 netif_wake_queue(ndev);
385 if (msg)
386 log(L_BUFT, "tx: wake queue %s\n", msg);
389 static inline void
390 acx_carrier_off(struct net_device *ndev, const char *msg)
392 netif_carrier_off(ndev);
393 if (msg)
394 log(L_BUFT, "tx: carrier off %s\n", msg);
397 static inline void
398 acx_carrier_on(struct net_device *ndev, const char *msg)
400 netif_carrier_on(ndev);
401 if (msg)
402 log(L_BUFT, "tx: carrier on %s\n", msg);
405 /* This function does not need locking UNLESS you call it
406 ** as acx_set_status(ACX_STATUS_4_ASSOCIATED), bacause this can
407 ** wake queue. This can race with stop_queue elsewhere. */
408 void acx_set_status(acx_device_t *adev, u16 status);
411 /***********************************************************************
412 ** Communication with firmware
414 #define CMD_TIMEOUT_MS(n) (n)
415 #define ACX_CMD_TIMEOUT_DEFAULT CMD_TIMEOUT_MS(50)
417 #if ACX_DEBUG
419 /* We want to log cmd names */
420 int acxpci_s_issue_cmd_timeo_debug(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout, const char* cmdstr);
421 int acxusb_s_issue_cmd_timeo_debug(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout, const char* cmdstr);
422 static inline int
423 acx_s_issue_cmd_timeo_debug(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout, const char* cmdstr)
425 if (IS_PCI(adev))
426 return acxpci_s_issue_cmd_timeo_debug(adev, cmd, param, len, timeout, cmdstr);
427 return acxusb_s_issue_cmd_timeo_debug(adev, cmd, param, len, timeout, cmdstr);
429 #define acx_s_issue_cmd(adev,cmd,param,len) \
430 acx_s_issue_cmd_timeo_debug(adev,cmd,param,len,ACX_CMD_TIMEOUT_DEFAULT,#cmd)
431 #define acx_s_issue_cmd_timeo(adev,cmd,param,len,timeo) \
432 acx_s_issue_cmd_timeo_debug(adev,cmd,param,len,timeo,#cmd)
433 int acx_s_configure_debug(acx_device_t *adev, void *pdr, int type, const char* str);
434 #define acx_s_configure(adev,pdr,type) \
435 acx_s_configure_debug(adev,pdr,type,#type)
436 int acx_s_interrogate_debug(acx_device_t *adev, void *pdr, int type, const char* str);
437 #define acx_s_interrogate(adev,pdr,type) \
438 acx_s_interrogate_debug(adev,pdr,type,#type)
440 #else
442 int acxpci_s_issue_cmd_timeo(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout);
443 int acxusb_s_issue_cmd_timeo(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout);
444 static inline int
445 acx_s_issue_cmd_timeo(acx_device_t *adev, unsigned cmd, void *param, unsigned len, unsigned timeout)
447 if (IS_PCI(adev))
448 return acxpci_s_issue_cmd_timeo(adev, cmd, param, len, timeout);
449 return acxusb_s_issue_cmd_timeo(adev, cmd, param, len, timeout);
451 static inline int
452 acx_s_issue_cmd(acx_device_t *adev, unsigned cmd, void *param, unsigned len)
454 if (IS_PCI(adev))
455 return acxpci_s_issue_cmd_timeo(adev, cmd, param, len, ACX_CMD_TIMEOUT_DEFAULT);
456 return acxusb_s_issue_cmd_timeo(adev, cmd, param, len, ACX_CMD_TIMEOUT_DEFAULT);
458 int acx_s_configure(acx_device_t *adev, void *pdr, int type);
459 int acx_s_interrogate(acx_device_t *adev, void *pdr, int type);
461 #endif
463 void acx_s_cmd_start_scan(acx_device_t *adev);
466 /***********************************************************************
467 ** Ioctls
470 acx111pci_ioctl_info(
471 struct net_device *ndev,
472 struct iw_request_info *info,
473 struct iw_param *vwrq,
474 char *extra);
476 acx100pci_ioctl_set_phy_amp_bias(
477 struct net_device *ndev,
478 struct iw_request_info *info,
479 struct iw_param *vwrq,
480 char *extra);
483 /***********************************************************************
484 ** /proc
486 #ifdef CONFIG_PROC_FS
487 int acx_proc_register_entries(const struct net_device *ndev);
488 int acx_proc_unregister_entries(const struct net_device *ndev);
489 #else
490 static inline int
491 acx_proc_register_entries(const struct net_device *ndev) { return OK; }
492 static inline int
493 acx_proc_unregister_entries(const struct net_device *ndev) { return OK; }
494 #endif
497 /***********************************************************************
499 firmware_image_t *acx_s_read_fw(struct device *dev, const char *file, u32 *size);
500 int acxpci_s_upload_radio(acx_device_t *adev);
503 /***********************************************************************
504 ** Unsorted yet :)
506 int acxpci_s_read_phy_reg(acx_device_t *adev, u32 reg, u8 *charbuf);
507 int acxusb_s_read_phy_reg(acx_device_t *adev, u32 reg, u8 *charbuf);
508 static inline int
509 acx_s_read_phy_reg(acx_device_t *adev, u32 reg, u8 *charbuf)
511 if (IS_PCI(adev))
512 return acxpci_s_read_phy_reg(adev, reg, charbuf);
513 return acxusb_s_read_phy_reg(adev, reg, charbuf);
516 int acxpci_s_write_phy_reg(acx_device_t *adev, u32 reg, u8 value);
517 int acxusb_s_write_phy_reg(acx_device_t *adev, u32 reg, u8 value);
518 static inline int
519 acx_s_write_phy_reg(acx_device_t *adev, u32 reg, u8 value)
521 if (IS_PCI(adev))
522 return acxpci_s_write_phy_reg(adev, reg, value);
523 return acxusb_s_write_phy_reg(adev, reg, value);
526 tx_t* acxpci_l_alloc_tx(acx_device_t *adev);
527 tx_t* acxusb_l_alloc_tx(acx_device_t *adev);
528 static inline tx_t*
529 acx_l_alloc_tx(acx_device_t *adev)
531 if (IS_PCI(adev))
532 return acxpci_l_alloc_tx(adev);
533 return acxusb_l_alloc_tx(adev);
536 void acxusb_l_dealloc_tx(tx_t *tx_opaque);
537 static inline void
538 acx_l_dealloc_tx(acx_device_t *adev, tx_t *tx_opaque)
540 if (IS_USB(adev))
541 acxusb_l_dealloc_tx(tx_opaque);
544 void* acxpci_l_get_txbuf(acx_device_t *adev, tx_t *tx_opaque);
545 void* acxusb_l_get_txbuf(acx_device_t *adev, tx_t *tx_opaque);
546 static inline void*
547 acx_l_get_txbuf(acx_device_t *adev, tx_t *tx_opaque)
549 if (IS_PCI(adev))
550 return acxpci_l_get_txbuf(adev, tx_opaque);
551 return acxusb_l_get_txbuf(adev, tx_opaque);
554 void acxpci_l_tx_data(acx_device_t *adev, tx_t *tx_opaque, int len);
555 void acxusb_l_tx_data(acx_device_t *adev, tx_t *tx_opaque, int len);
556 static inline void
557 acx_l_tx_data(acx_device_t *adev, tx_t *tx_opaque, int len)
559 if (IS_PCI(adev))
560 acxpci_l_tx_data(adev, tx_opaque, len);
561 else
562 acxusb_l_tx_data(adev, tx_opaque, len);
565 static inline wlan_hdr_t*
566 acx_get_wlan_hdr(acx_device_t *adev, const rxbuffer_t *rxbuf)
568 return (wlan_hdr_t*)((u8*)&rxbuf->hdr_a3 + adev->phy_header_len);
571 void acxpci_l_power_led(acx_device_t *adev, int enable);
572 int acxpci_read_eeprom_byte(acx_device_t *adev, u32 addr, u8 *charbuf);
573 unsigned int acxpci_l_clean_txdesc(acx_device_t *adev);
574 void acxpci_l_clean_txdesc_emergency(acx_device_t *adev);
575 int acxpci_s_create_hostdesc_queues(acx_device_t *adev);
576 void acxpci_create_desc_queues(acx_device_t *adev, u32 tx_queue_start, u32 rx_queue_start);
577 void acxpci_free_desc_queues(acx_device_t *adev);
578 char* acxpci_s_proc_diag_output(char *p, acx_device_t *adev);
579 int acxpci_proc_eeprom_output(char *p, acx_device_t *adev);
580 void acxpci_set_interrupt_mask(acx_device_t *adev);
581 int acx100pci_s_set_tx_level(acx_device_t *adev, u8 level_dbm);
583 void acx_s_msleep(int ms);
584 int acx_s_init_mac(acx_device_t *adev);
585 void acx_set_reg_domain(acx_device_t *adev, unsigned char reg_dom_id);
586 void acx_set_timer(acx_device_t *adev, int timeout_us);
587 void acx_update_capabilities(acx_device_t *adev);
588 void acx_s_start(acx_device_t *adev);
590 void acx_s_update_card_settings(acx_device_t *adev);
591 void acx_s_parse_configoption(acx_device_t *adev, const acx111_ie_configoption_t *pcfg);
592 void acx_l_update_ratevector(acx_device_t *adev);
594 void acx_init_task_scheduler(acx_device_t *adev);
595 void acx_schedule_task(acx_device_t *adev, unsigned int set_flag);
597 int acx_e_ioctl_old(struct net_device *ndev, struct ifreq *ifr, int cmd);
599 client_t *acx_l_sta_list_get(acx_device_t *adev, const u8 *address);
600 void acx_l_sta_list_del(acx_device_t *adev, client_t *clt);
602 int acx_l_transmit_disassoc(acx_device_t *adev, client_t *clt);
603 void acx_i_timer(unsigned long a);
604 int acx_s_complete_scan(acx_device_t *adev);
606 struct sk_buff *acx_rxbuf_to_ether(acx_device_t *adev, rxbuffer_t *rxbuf);
607 int acx_ether_to_txbuf(acx_device_t *adev, void *txbuf, const struct sk_buff *skb);
609 u8 acx_signal_determine_quality(u8 signal, u8 noise);
611 void acx_l_process_rxbuf(acx_device_t *adev, rxbuffer_t *rxbuf);
612 void acx_l_handle_txrate_auto(acx_device_t *adev, struct client *txc,
613 u16 intended_rate, u8 rate100, u16 rate111, u8 error,
614 int pkts_to_ignore);
616 void acx_dump_bytes(const void *, int);
617 void acx_log_bad_eid(wlan_hdr_t* hdr, int len, wlan_ie_t* ie_ptr);
619 u8 acx_rate111to100(u16);
621 void acx_s_set_defaults(acx_device_t *adev);
623 #if !ACX_DEBUG
624 static inline const char* acx_get_packet_type_string(u16 fc) { return ""; }
625 #else
626 const char* acx_get_packet_type_string(u16 fc);
627 #endif
628 const char* acx_cmd_status_str(unsigned int state);
630 int acx_i_start_xmit(struct sk_buff *skb, struct net_device *ndev);
632 void great_inquisitor(acx_device_t *adev);
634 void acx_s_get_firmware_version(acx_device_t *adev);
635 void acx_display_hardware_details(acx_device_t *adev);
637 int acx_e_change_mtu(struct net_device *ndev, int mtu);
638 struct net_device_stats* acx_e_get_stats(struct net_device *ndev);
639 struct iw_statistics* acx_e_get_wireless_stats(struct net_device *ndev);
641 int __init acxpci_e_init_module(void);
642 int __init acxusb_e_init_module(void);
643 void __exit acxpci_e_cleanup_module(void);
644 void __exit acxusb_e_cleanup_module(void);