3 Copyright (C) 2001-2012 Neil Cafferkey
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <exec/memory.h>
24 #include <exec/execbase.h>
25 #include <exec/errors.h>
27 #include <proto/exec.h>
29 #include <proto/alib.h>
31 #include <clib/alib_protos.h>
33 #include <proto/utility.h>
34 #include <proto/timer.h>
38 #include "unit_protos.h"
39 #include "request_protos.h"
40 #include "radio_protos.h"
41 #include "eeprom_protos.h"
42 #include "encryption_protos.h"
43 #include "timer_protos.h"
44 #include "realtek8187.h"
47 #define TASK_PRIORITY 0
48 #define STACK_SIZE 4096
49 #define INT_MASK 0xffff
54 #define AbsExecBase sys_base
57 VOID
DeinitialiseAdapter(struct DevUnit
*unit
, struct DevBase
*base
);
58 static struct AddressRange
*FindMulticastRange(struct DevUnit
*unit
,
59 ULONG lower_bound_left
, UWORD lower_bound_right
, ULONG upper_bound_left
,
60 UWORD upper_bound_right
, struct DevBase
*base
);
61 static VOID
SetMulticast(struct DevUnit
*unit
, struct DevBase
*base
);
62 static UBYTE
*GetRXBuffer(struct DevUnit
*unit
, const UBYTE
*address
,
63 UWORD frag_no
, UWORD
*buffer_no
, struct DevBase
*base
);
64 static VOID
DistributeRXPacket(struct DevUnit
*unit
, const UBYTE
*frame
,
65 struct DevBase
*base
);
66 static VOID
CopyPacket(struct DevUnit
*unit
, struct IOSana2Req
*request
,
67 UWORD packet_size
, UWORD packet_type
, UBYTE
*buffer
,
68 struct DevBase
*base
);
69 static BOOL
AddressFilter(struct DevUnit
*unit
, UBYTE
*address
,
70 struct DevBase
*base
);
71 static VOID
DistributeMgmtFrame(struct DevUnit
*unit
, UBYTE
*frame
,
72 UWORD frame_size
, struct DevBase
*base
);
73 static VOID
TXInt(REG(a1
, struct DevUnit
*unit
), REG(a6
, APTR int_code
));
74 static VOID
MgmtTXInt(REG(a1
, struct DevUnit
*unit
),
75 REG(a6
, APTR int_code
));
76 static VOID
ReportEvents(struct DevUnit
*unit
, ULONG events
,
77 struct DevBase
*base
);
78 static UWORD
GetDuration(struct DevUnit
*unit
, UWORD length
, UWORD rate
,
79 BOOL short_preamble
, struct DevBase
*base
);
80 static UWORD
AckRate(struct DevUnit
*unit
, UWORD data_rate
,
81 struct DevBase
*base
);
82 static VOID
UnitTask(struct ExecBase
*sys_base
);
85 static const UBYTE snap_template
[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
86 static const UBYTE broadcast_address
[] =
87 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
92 #define AddTask(task, initial_pc, final_pc) \
93 IExec->AddTask(task, initial_pc, final_pc, NULL)
96 static const struct EmulLibEntry mos_task_trap
=
102 #define UnitTask &mos_task_trap
106 #define AddTask(task, initial_pc, final_pc) \
108 struct TagItem _task_tags[] = \
109 {{TASKTAG_ARG1, (IPTR)SysBase}, {TAG_END, 0}}; \
110 NewAddTask(task, initial_pc, final_pc, _task_tags); \
116 /****i* realtek8180.device/CreateUnit **************************************
119 * CreateUnit -- Create a unit.
122 * unit = CreateUnit(index, io_base, id, card,
125 * struct DevUnit *CreateUnit(ULONG, APTR, UWORD, APTR,
126 * struct TagItem *, UWORD);
129 * Creates a new unit.
131 ****************************************************************************
135 struct DevUnit
*CreateUnit(ULONG index
, APTR card
,
136 const struct TagItem
*io_tags
, UWORD bus
, struct DevBase
*base
)
139 struct DevUnit
*unit
;
141 struct MsgPort
*port
;
145 unit
= AllocMem(sizeof(struct DevUnit
), MEMF_CLEAR
| MEMF_PUBLIC
);
151 /* Initialise lists etc. */
153 NewList((APTR
)&unit
->openers
);
154 NewList((APTR
)&unit
->type_trackers
);
155 NewList((APTR
)&unit
->multicast_ranges
);
161 unit
->generation
= RTL8187B1_GEN
;
163 unit
->tx_rate_code
= 3; /* 11 Mbps */
165 unit
->mgmt_rate_code
= 0; /* 1 Mbps */
168 /* Store I/O hooks */
171 (APTR
)GetTagData(IOTAG_ByteOut
, (UPINT
)NULL
, io_tags
);
173 (APTR
)GetTagData(IOTAG_ByteIn
, (UPINT
)NULL
, io_tags
);
175 (APTR
)GetTagData(IOTAG_LEWordIn
, (UPINT
)NULL
, io_tags
);
177 (APTR
)GetTagData(IOTAG_LELongIn
, (UPINT
)NULL
, io_tags
);
179 (APTR
)GetTagData(IOTAG_LEWordOut
, (UPINT
)NULL
, io_tags
);
181 (APTR
)GetTagData(IOTAG_LELongOut
, (UPINT
)NULL
, io_tags
);
183 (APTR
)GetTagData(IOTAG_AllocDMAMem
, (UPINT
)NULL
, io_tags
);
185 (APTR
)GetTagData(IOTAG_FreeDMAMem
, (UPINT
)NULL
, io_tags
);
187 (APTR
)GetTagData(IOTAG_SendFrame
, (UPINT
)NULL
, io_tags
);
189 (APTR
)GetTagData(IOTAG_ReceiveFrame
, (UPINT
)NULL
, io_tags
);
190 if(unit
->ByteIn
== NULL
191 || unit
->ByteOut
== NULL
192 || unit
->LEWordIn
== NULL
193 || unit
->LELongIn
== NULL
194 || unit
->LEWordOut
== NULL
195 || unit
->LELongOut
== NULL
196 || unit
->AllocDMAMem
== NULL
197 || unit
->FreeDMAMem
== NULL
198 || unit
->SendFrame
== NULL
199 || unit
->ReceiveFrame
== NULL
)
205 InitSemaphore(&unit
->access_lock
);
207 /* Create the message ports for queuing requests */
209 for(i
= 0; i
< REQUEST_QUEUE_COUNT
; i
++)
211 unit
->request_ports
[i
] = port
= AllocMem(sizeof(struct MsgPort
),
212 MEMF_PUBLIC
| MEMF_CLEAR
);
218 NewList(&port
->mp_MsgList
);
219 port
->mp_Flags
= PA_IGNORE
;
225 unit
->request_ports
[WRITE_QUEUE
]->mp_SigTask
= &unit
->tx_int
;
226 unit
->request_ports
[MGMT_QUEUE
]->mp_SigTask
= &unit
->mgmt_int
;
229 /* Allocate buffers and descriptors */
231 unit
->tx_buffer
= AllocVec(ETH_MAXPACKETSIZE
, MEMF_PUBLIC
);
232 for(i
= 0; i
< TX_SLOT_COUNT
; i
++)
234 unit
->tx_descs
[i
] = unit
->AllocDMAMem(unit
->card
,
235 R8180_MAXDESCSIZE
+ FRAME_BUFFER_SIZE
, 4);
236 if(unit
->tx_descs
[i
] != NULL
)
237 unit
->tx_buffers
[i
] = unit
->tx_descs
[i
] + R8180_MAXDESCSIZE
;
241 unit
->rx_buffer
= AllocVec(FRAME_BUFFER_SIZE
, MEMF_PUBLIC
);
242 for(i
= 0; i
< RX_SLOT_COUNT
; i
++)
244 if(unit
->bus
!= USB_BUS
)
246 unit
->rx_descs
[i
] = unit
->AllocDMAMem(unit
->card
,
247 R8180_MAXDESCSIZE
, 4);
248 if(unit
->rx_descs
[i
] == NULL
)
251 unit
->rx_buffers
[i
] = unit
->AllocDMAMem(unit
->card
,
252 FRAME_BUFFER_SIZE
+ R8180_MAXDESCSIZE
, 4);
253 if(unit
->rx_buffers
[i
] == NULL
)
257 AllocVec(FRAME_BUFFER_SIZE
* FRAME_BUFFER_COUNT
, MEMF_PUBLIC
);
258 for(i
= 0; i
< FRAME_BUFFER_COUNT
; i
++)
259 unit
->rx_fragment_nos
[i
] = -1;
260 unit
->tx_requests
= AllocVec(sizeof(APTR
) * TX_SLOT_COUNT
,
262 if(unit
->tx_buffer
== NULL
263 || unit
->rx_buffer
== NULL
264 || unit
->rx_frames
== NULL
265 || unit
->tx_requests
== NULL
)
271 /* Initialise network adapter hardware */
273 success
= InitialiseAdapter(unit
, FALSE
, base
);
274 unit
->flags
|= UNITF_HAVEADAPTER
;
279 /* Record maximum speed in BPS */
281 unit
->speed
= 54000000;
283 /* Initialise interrupts */
285 unit
->rx_int
.is_Node
.ln_Name
=
286 base
->device
.dd_Library
.lib_Node
.ln_Name
;
287 unit
->rx_int
.is_Code
= (APTR
)RXInt
;
288 unit
->rx_int
.is_Data
= unit
;
290 unit
->tx_int
.is_Node
.ln_Name
=
291 base
->device
.dd_Library
.lib_Node
.ln_Name
;
292 unit
->tx_int
.is_Code
= (APTR
)TXInt
;
293 unit
->tx_int
.is_Data
= unit
;
295 unit
->mgmt_int
.is_Node
.ln_Name
=
296 base
->device
.dd_Library
.lib_Node
.ln_Name
;
297 unit
->mgmt_int
.is_Code
= (APTR
)MgmtTXInt
;
298 unit
->mgmt_int
.is_Data
= unit
;
300 unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
= PA_SOFTINT
;
301 unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
= PA_SOFTINT
;
303 /* Create a new task */
306 AllocMem(sizeof(struct Task
), MEMF_PUBLIC
| MEMF_CLEAR
);
313 stack
= AllocMem(STACK_SIZE
, MEMF_PUBLIC
);
320 /* Initialise and start task */
322 task
->tc_Node
.ln_Type
= NT_TASK
;
323 task
->tc_Node
.ln_Pri
= TASK_PRIORITY
;
324 task
->tc_Node
.ln_Name
= base
->device
.dd_Library
.lib_Node
.ln_Name
;
325 task
->tc_SPUpper
= stack
+ STACK_SIZE
;
326 task
->tc_SPLower
= stack
;
327 task
->tc_SPReg
= stack
+ STACK_SIZE
;
328 NewList(&task
->tc_MemEntry
);
330 if(AddTask(task
, UnitTask
, NULL
) == NULL
)
336 /* Send the unit to the new task */
338 task
->tc_UserData
= unit
;
340 /* Set default wireless options */
342 unit
->mode
= S2PORT_MANAGED
;
347 DeleteUnit(unit
, base
);
356 /****i* realtek8180.device/DeleteUnit **************************************
359 * DeleteUnit -- Delete a unit.
364 * VOID DeleteUnit(struct DevUnit *);
370 * unit - Device unit (may be NULL).
375 ****************************************************************************
379 VOID
DeleteUnit(struct DevUnit
*unit
, struct DevBase
*base
)
389 if(task
->tc_UserData
!= NULL
)
392 FreeMem(task
->tc_SPLower
, STACK_SIZE
);
394 FreeMem(task
, sizeof(struct Task
));
397 for(i
= 0; i
< REQUEST_QUEUE_COUNT
; i
++)
399 if(unit
->request_ports
[i
] != NULL
)
400 FreeMem(unit
->request_ports
[i
], sizeof(struct MsgPort
));
403 if((unit
->flags
& UNITF_ONLINE
) != 0) /* Needed! */
404 GoOffline(unit
, base
);
406 if((unit
->flags
& UNITF_HAVEADAPTER
) != 0)
407 DeinitialiseAdapter(unit
, base
);
409 for(i
= 0; i
< TX_SLOT_COUNT
; i
++)
410 unit
->FreeDMAMem(unit
->card
, unit
->tx_descs
[i
]);
411 for(i
= 0; i
< RX_SLOT_COUNT
; i
++)
413 unit
->FreeDMAMem(unit
->card
, unit
->rx_buffers
[i
]);
414 if(unit
->bus
!= USB_BUS
)
415 unit
->FreeDMAMem(unit
->card
, unit
->rx_descs
[i
]);
418 FreeVec(unit
->tx_buffer
);
419 FreeVec(unit
->rx_frames
);
420 FreeVec(unit
->tx_requests
);
421 FreeVec(unit
->rx_buffer
);
423 FreeMem(unit
, sizeof(struct DevUnit
));
431 /****i* realtek8180.device/InitialiseAdapter *******************************
437 * success = InitialiseAdapter(unit, reinsertion)
439 * BOOL InitialiseAdapter(struct DevUnit *, BOOL);
448 * success - Success indicator.
450 ****************************************************************************
454 BOOL
InitialiseAdapter(struct DevUnit
*unit
, BOOL reinsertion
,
455 struct DevBase
*base
)
457 BOOL success
= FALSE
;
458 UBYTE reg_62
, revision
;
462 /* Initialise EEPROM */
464 rx_conf
= unit
->LELongIn(unit
->card
, 0x100 + R8180REG_RXCONF
);
465 unit
->eeprom_addr_size
= ((rx_conf
& (1 << 6)) != 0) ? 8 : 6;
467 BusyMicroDelay(10, base
);
468 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, R8180ECMD_CONFIG
);
470 /* Get default MAC address */
472 p
= (UWORD
*)unit
->default_address
;
473 for(i
= 0; i
< ETH_ADDRESSSIZE
/ sizeof(UWORD
); i
++)
474 *p
++ = LEWord(ReadEEPROM(unit
, R8180ROM_ADDRESS0
+ i
, base
));
476 /* Refine main chip revision */
478 if(unit
->generation
== RTL8187B1_GEN
)
480 revision
= unit
->ByteIn(unit
->card
, 0x100 + 0xe1);
481 if(revision
== 1 || revision
== 2)
482 unit
->generation
= RTL8187B2_GEN
;
486 if((unit
->LELongIn(unit
->card
, 0x100 + R8180REG_TXCONF
)
487 & R8180REG_TXCONFF_HWVER
) == 6 << R8180REG_TXCONFB_HWVER
)
488 unit
->generation
= RTL8187B0_GEN
;
491 /* Set up power tables */
493 GetPower(unit
, base
);
497 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_CONFIG3
,
498 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_CONFIG3
)
499 | R8180REG_CONFIG3F_ANAPARAMWRITE
| R8180REG_CONFIG3F_GNTSELECT
);
500 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_ANAPARAM2
, 0x727f3f52);
501 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_ANAPARAM1
, 0x45090658);
502 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_ANAPARAM3
, 0);
504 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_CONFIG3
,
505 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_CONFIG3
)
506 & ~R8180REG_CONFIG3F_ANAPARAMWRITE
);
508 /* Reset PLL sequence */
510 unit
->ByteOut(unit
->card
, 0x161, 0x10);
511 reg_62
= unit
->ByteIn(unit
->card
, 0x162);
512 unit
->ByteOut(unit
->card
, 0x162, reg_62
& ~(1 << 5));
513 unit
->ByteOut(unit
->card
, 0x162, reg_62
| (1 << 5));
515 /* Send reset command */
517 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_COMMAND
,
518 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_COMMAND
)
519 & 0x2 | R8180REG_COMMANDF_RESET
);
521 for(i
= 0; i
< 10 && !success
; i
++)
523 BusyMilliDelay(2, base
);
524 if((unit
->ByteIn(unit
->card
, 0x100 + R8180REG_COMMAND
)
525 & R8180REG_COMMANDF_RESET
) == 0)
529 if(success
&& unit
->generation
== RTL8187L_GEN
)
531 /* Reload registers from EEPROM */
533 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, R8180ECMD_LOAD
);
535 for(i
= 0, success
= FALSE
; i
< 10 && !success
; i
++)
537 BusyMilliDelay(4, base
);
538 if((unit
->ByteIn(unit
->card
, 0x100 + R8180REG_EEPROM
)
539 & R8180REG_EEPROMF_COMMAND
) == 0)
548 if(unit
->generation
== RTL8187L_GEN
)
549 unit
->LEWordOut(unit
->card
, 0x12d, 0xfff);
551 unit
->LEWordOut(unit
->card
, 0x134, 0xfff);
552 unit
->LEWordOut(unit
->card
, 0x12c, 0x1ff);
553 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_CWCONF
,
554 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_CWCONF
)
555 | R8180REG_CWCONFF_PPRETRYSHIFT
);
556 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_TXAGCCTL
,
557 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_TXAGCCTL
)
558 | R8180REG_TXAGCCTLF_PPGAINSHIFT
);
560 unit
->LEWordOut(unit
->card
, 0x1e0 | 1 << 16, 0xfff);
561 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_RATEFALLBACK
,
562 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_RATEFALLBACK
)
563 | R8180REG_RATEFALLBACKF_ENABLE
);
565 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_MSR
,
566 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_MSR
) & 0xf3);
567 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_MSR
,
568 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_MSR
)
569 | R8180REG_MSRF_ENEDCA
);
570 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_ACMCONTROL
, 0);
572 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_ATIMWINDOW
, 2);
573 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_BEACONINTERVAL
, 100);
574 unit
->LEWordOut(unit
->card
, 0x1d4 | 1 << 16, 0xffff);
576 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, R8180ECMD_CONFIG
);
577 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_CONFIG1
,
578 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_CONFIG1
) & 0x3f | 0x80);
579 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, 0);
581 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_WPACONF
, 0);
583 unit
->ByteOut(unit
->card
, 0x1f0, 0x32);
584 unit
->ByteOut(unit
->card
, 0x1f1, 0x32);
585 unit
->ByteOut(unit
->card
, 0x1f2, 0x0);
586 unit
->ByteOut(unit
->card
, 0x1f3, 0x0);
587 unit
->ByteOut(unit
->card
, 0x1f4, 0x32);
588 unit
->ByteOut(unit
->card
, 0x1f5, 0x43);
589 unit
->ByteOut(unit
->card
, 0x1f6, 0x0);
590 unit
->ByteOut(unit
->card
, 0x1f7, 0x0);
591 unit
->ByteOut(unit
->card
, 0x1f8, 0x46);
592 unit
->ByteOut(unit
->card
, 0x1f9, 0xa4);
593 unit
->ByteOut(unit
->card
, 0x1fa, 0x0);
594 unit
->ByteOut(unit
->card
, 0x1fb, 0x0);
595 unit
->ByteOut(unit
->card
, 0x1fc, 0x96);
596 unit
->ByteOut(unit
->card
, 0x1fd, 0xa4);
597 unit
->ByteOut(unit
->card
, 0x1fe, 0x0);
598 unit
->ByteOut(unit
->card
, 0x1ff, 0x0);
600 unit
->ByteOut(unit
->card
, 0x158 | 1 << 16, 0x4b);
601 unit
->ByteOut(unit
->card
, 0x159 | 1 << 16, 0x0);
602 unit
->ByteOut(unit
->card
, 0x15a | 1 << 16, 0x4b);
603 unit
->ByteOut(unit
->card
, 0x15b | 1 << 16, 0x0);
604 unit
->ByteOut(unit
->card
, 0x160 | 1 << 16, 0x4b);
605 unit
->ByteOut(unit
->card
, 0x161 | 1 << 16, 0x9);
606 unit
->ByteOut(unit
->card
, 0x162 | 1 << 16, 0x4b);
607 unit
->ByteOut(unit
->card
, 0x163 | 1 << 16, 0x9);
608 unit
->ByteOut(unit
->card
, 0x1ce | 1 << 16, 0xf);
609 unit
->ByteOut(unit
->card
, 0x1cf | 1 << 16, 0x0);
610 unit
->ByteOut(unit
->card
, 0x1e0 | 1 << 16, 0xff);
611 unit
->ByteOut(unit
->card
, 0x1e1 | 1 << 16, 0xf);
612 unit
->ByteOut(unit
->card
, 0x1e2 | 1 << 16, 0x0);
613 unit
->ByteOut(unit
->card
, 0x1f0 | 1 << 16, 0x4e);
614 unit
->ByteOut(unit
->card
, 0x1f1 | 1 << 16, 0x1);
615 unit
->ByteOut(unit
->card
, 0x1f2 | 1 << 16, 0x2);
616 unit
->ByteOut(unit
->card
, 0x1f3 | 1 << 16, 0x3);
617 unit
->ByteOut(unit
->card
, 0x1f4 | 1 << 16, 0x4);
618 unit
->ByteOut(unit
->card
, 0x1f5 | 1 << 16, 0x5);
619 unit
->ByteOut(unit
->card
, 0x1f6 | 1 << 16, 0x6);
620 unit
->ByteOut(unit
->card
, 0x1f7 | 1 << 16, 0x7);
621 unit
->ByteOut(unit
->card
, 0x1f8 | 1 << 16, 0x8);
623 unit
->ByteOut(unit
->card
, 0x14e | 2 << 16, 0x0);
624 unit
->ByteOut(unit
->card
, 0x10c | 2 << 16, 0x4);
625 unit
->ByteOut(unit
->card
, 0x121 | 2 << 16, 0x61);
626 unit
->ByteOut(unit
->card
, 0x122 | 2 << 16, 0x68);
627 unit
->ByteOut(unit
->card
, 0x123 | 2 << 16, 0x6f);
628 unit
->ByteOut(unit
->card
, 0x124 | 2 << 16, 0x76);
629 unit
->ByteOut(unit
->card
, 0x125 | 2 << 16, 0x7d);
630 unit
->ByteOut(unit
->card
, 0x126 | 2 << 16, 0x84);
631 unit
->ByteOut(unit
->card
, 0x127 | 2 << 16, 0x8d);
632 unit
->ByteOut(unit
->card
, 0x14d | 2 << 16, 0x8);
633 unit
->ByteOut(unit
->card
, 0x150 | 2 << 16, 0x5);
634 unit
->ByteOut(unit
->card
, 0x151 | 2 << 16, 0xf5);
635 unit
->ByteOut(unit
->card
, 0x152 | 2 << 16, 0x4);
636 unit
->ByteOut(unit
->card
, 0x153 | 2 << 16, 0xa0);
637 unit
->ByteOut(unit
->card
, 0x154 | 2 << 16, 0x1f);
638 unit
->ByteOut(unit
->card
, 0x155 | 2 << 16, 0x23);
639 unit
->ByteOut(unit
->card
, 0x156 | 2 << 16, 0x45);
640 unit
->ByteOut(unit
->card
, 0x157 | 2 << 16, 0x67);
641 unit
->ByteOut(unit
->card
, 0x158 | 2 << 16, 0x8);
642 unit
->ByteOut(unit
->card
, 0x159 | 2 << 16, 0x8);
643 unit
->ByteOut(unit
->card
, 0x15a | 2 << 16, 0x8);
644 unit
->ByteOut(unit
->card
, 0x15b | 2 << 16, 0x8);
645 unit
->ByteOut(unit
->card
, 0x160 | 2 << 16, 0x8);
646 unit
->ByteOut(unit
->card
, 0x161 | 2 << 16, 0x8);
647 unit
->ByteOut(unit
->card
, 0x162 | 2 << 16, 0x8);
648 unit
->ByteOut(unit
->card
, 0x163 | 2 << 16, 0x8);
649 unit
->ByteOut(unit
->card
, 0x164 | 2 << 16, 0xcf);
650 unit
->ByteOut(unit
->card
, 0x172 | 2 << 16, 0x56);
651 unit
->ByteOut(unit
->card
, 0x173 | 2 << 16, 0x9a);
653 unit
->ByteOut(unit
->card
, 0x134, 0xf0);
654 unit
->ByteOut(unit
->card
, 0x135, 0xf);
655 unit
->ByteOut(unit
->card
, 0x15b, 0x40);
656 unit
->ByteOut(unit
->card
, 0x184, 0x88);
657 unit
->ByteOut(unit
->card
, 0x185, 0x24);
658 unit
->ByteOut(unit
->card
, 0x188, 0x54);
659 unit
->ByteOut(unit
->card
, 0x18b, 0xb8);
660 unit
->ByteOut(unit
->card
, 0x18c, 0x7);
661 unit
->ByteOut(unit
->card
, 0x18d, 0x0);
662 unit
->ByteOut(unit
->card
, 0x194, 0x1b);
663 unit
->ByteOut(unit
->card
, 0x195, 0x12);
664 unit
->ByteOut(unit
->card
, 0x196, 0x0);
665 unit
->ByteOut(unit
->card
, 0x197, 0x6);
666 unit
->ByteOut(unit
->card
, 0x19d, 0x1a);
667 unit
->ByteOut(unit
->card
, 0x19f, 0x10);
668 unit
->ByteOut(unit
->card
, 0x1b4, 0x22);
669 unit
->ByteOut(unit
->card
, 0x1be, 0x80);
670 unit
->ByteOut(unit
->card
, 0x1db, 0x0);
671 unit
->ByteOut(unit
->card
, 0x1ee, 0x0);
672 unit
->ByteOut(unit
->card
, 0x191, 0x3);
673 unit
->ByteOut(unit
->card
, 0x14c | 2 << 16, 0x0);
675 unit
->ByteOut(unit
->card
, 0x19f | 3 << 16, 0x0);
676 unit
->ByteOut(unit
->card
, 0x18c, 0x1);
677 unit
->ByteOut(unit
->card
, 0x18d, 0x10);
678 unit
->ByteOut(unit
->card
, 0x18e, 0x8);
679 unit
->ByteOut(unit
->card
, 0x18f, 0x0);
681 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_TIDACMAP
, 0xfa50);
682 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_INTMIG
, 0);
684 unit
->LELongOut(unit
->card
, 0x1f0 | 1 << 16, 0);
685 unit
->LELongOut(unit
->card
, 0x1f4 | 1 << 16, 0);
686 unit
->ByteOut(unit
->card
, 0x1f8 | 1 << 16, 0);
688 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_RFTIMING
, 0x4001);
690 unit
->LEWordOut(unit
->card
, 0x172 | 2 << 16, 0x569a);
692 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, R8180ECMD_CONFIG
);
694 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_CONFIG3
,
695 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_CONFIG3
)
696 | R8180REG_CONFIG3F_ANAPARAMWRITE
);
697 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EEPROM
, 0);
701 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_GPIO0
, 1);
702 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_GPENABLE
, 0);
704 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_RFPINSOUTPUT
, 0x480);
705 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_RFPINSSELECT
, 0x2488);
706 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_RFPINSENABLE
, 0x1fff);
707 BusyMilliDelay(100, base
);
709 /* Initialise radio */
711 success
= InitialiseRadio(unit
, base
);
713 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_COMMAND
,
714 R8180REG_COMMANDF_TXENABLE
| R8180REG_COMMANDF_RXENABLE
);
715 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_INTMASK
, INT_MASK
);
717 unit
->ByteOut(unit
->card
, 0x41, 0xf4);
718 unit
->ByteOut(unit
->card
, 0x40, 0);
719 unit
->ByteOut(unit
->card
, 0x42, 0);
720 unit
->ByteOut(unit
->card
, 0x42, 1);
721 unit
->ByteOut(unit
->card
, 0x40, 0xf);
722 unit
->ByteOut(unit
->card
, 0x42, 0);
723 unit
->ByteOut(unit
->card
, 0x42, 1);
725 unit
->ByteOut(unit
->card
, 0x1db,
726 unit
->ByteIn(unit
->card
, 0x1db) | 1 << 2);
727 unit
->LEWordOut(unit
->card
, 0x172 | 3 << 16, 0x59fa);
728 unit
->LEWordOut(unit
->card
, 0x174 | 3 << 16, 0x59d2);
729 unit
->LEWordOut(unit
->card
, 0x176 | 3 << 16, 0x59d2);
730 unit
->LEWordOut(unit
->card
, 0x178 | 3 << 16, 0x19fa);
731 unit
->LEWordOut(unit
->card
, 0x17a | 3 << 16, 0x19fa);
732 unit
->LEWordOut(unit
->card
, 0x17c | 3 << 16, 0xd0);
733 unit
->ByteOut(unit
->card
, 0x161, 0);
734 unit
->ByteOut(unit
->card
, 0x180 | 1 << 16, 0xf);
735 unit
->ByteOut(unit
->card
, 0x183 | 1 << 16, 3);
736 unit
->ByteOut(unit
->card
, 0x1da, 0x10);
737 unit
->ByteOut(unit
->card
, 0x14d | 2 << 16, 8);
739 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_HSSIPARA
, 0x600321b);
741 /* Set maximum RX frame size */
743 unit
->LEWordOut(unit
->card
, 0x1ec | 1 << 16, 0x800);
745 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_ACMCONTROL
, 0);
746 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_MSR
, R8180REG_MSRF_ENEDCA
);
751 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_RXCONF
,
752 R8180REG_RXCONFF_EARLYTHRESH
753 | R8180REG_RXCONFF_AUTORESETPHY
754 | R8180REG_RXCONFF_CHECKBSSID
755 | R8180REG_RXCONFF_MGMT
756 | R8180REG_RXCONFF_DATA
757 | 7 << R8180REG_RXCONFB_FIFOTHRESH
758 | 7 << R8180REG_RXCONFB_MAXDMA
759 | R8180REG_RXCONFF_BCAST
760 | R8180REG_RXCONFF_MCAST
761 | R8180REG_RXCONFF_UCAST
);
762 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_TXAGCCTL
,
763 unit
->ByteIn(unit
->card
, 0x100 + R8180REG_TXAGCCTL
)
764 & ~(R8180REG_TXAGCCTLF_PPGAINSHIFT
765 | R8180REG_TXAGCCTLF_PPANTSELSHIFT
766 | R8180REG_TXAGCCTLF_FEEDBACKANT
));
767 unit
->LELongOut(unit
->card
, 0x100 + R8180REG_TXCONF
,
768 R8180REG_TXCONFF_DISREQQSIZE
769 | 7 << R8180REG_TXCONFB_MAXDMA
770 | TX_TRIES
<< R8180REG_TXCONFB_SHORTTRIES
771 | TX_TRIES
<< R8180REG_TXCONFB_LONGTRIES
);
774 /* Determine features, and get offsets of certain fields within frame
777 unit
->retries_offset
= R8180FRM_RETRY
;
778 unit
->tx_desc_size
= 32;
779 unit
->rx_desc_size
= 20;
783 unit
->iv_sizes
[S2ENC_WEP
] = IV_SIZE
;
784 unit
->iv_sizes
[S2ENC_TKIP
] = EIV_SIZE
;
785 unit
->iv_sizes
[S2ENC_CCMP
] = EIV_SIZE
;
787 /* Set encryption functions */
789 unit
->fragment_encrypt_functions
[S2ENC_NONE
] = WriteClearFragment
;
791 if((unit
->flags
& UNITF_HARDWEP
) != 0)
792 unit
->fragment_encrypt_functions
[S2ENC_WEP
] = WriteWEPFragment
;
794 unit
->fragment_encrypt_functions
[S2ENC_WEP
] = EncryptWEPFragment
;
796 if((unit
->flags
& UNITF_HARDTKIP
) != 0)
797 unit
->fragment_encrypt_functions
[S2ENC_TKIP
] = WriteTKIPFragment
;
799 unit
->fragment_encrypt_functions
[S2ENC_TKIP
] = EncryptTKIPFragment
;
801 if((unit
->flags
& UNITF_HARDCCMP
) != 0)
802 unit
->fragment_encrypt_functions
[S2ENC_CCMP
] = WriteCCMPFragment
;
804 unit
->fragment_encrypt_functions
[S2ENC_CCMP
] = EncryptCCMPFragment
;
806 /* Set decryption functions */
808 unit
->fragment_decrypt_functions
[S2ENC_NONE
] = ReadClearFragment
;
810 if((unit
->flags
& UNITF_HARDWEP
) != 0)
811 unit
->fragment_decrypt_functions
[S2ENC_WEP
] = ReadWEPFragment
;
813 unit
->fragment_decrypt_functions
[S2ENC_WEP
] = DecryptWEPFragment
;
815 if((unit
->flags
& UNITF_HARDTKIP
) != 0)
816 unit
->fragment_decrypt_functions
[S2ENC_TKIP
] = ReadTKIPFragment
;
818 unit
->fragment_decrypt_functions
[S2ENC_TKIP
] = DecryptTKIPFragment
;
820 if((unit
->flags
& UNITF_HARDCCMP
) != 0)
821 unit
->fragment_decrypt_functions
[S2ENC_CCMP
] = ReadCCMPFragment
;
823 unit
->fragment_decrypt_functions
[S2ENC_CCMP
] = DecryptCCMPFragment
;
832 /****i* realtek8180.device/DeinitialiseAdapter *****************************
835 * DeinitialiseAdapter
838 * DeinitialiseAdapter(unit)
840 * VOID DeinitialiseAdapter(struct DevUnit *);
850 ****************************************************************************
854 VOID
DeinitialiseAdapter(struct DevUnit
*unit
, struct DevBase
*base
)
858 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_GPIO0
, 1);
859 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_GPENABLE
, 1);
866 /****i* realtek8180.device/ConfigureAdapter ********************************
869 * ConfigureAdapter -- Set up card for transmission/reception.
872 * ConfigureAdapter(unit)
874 * VOID ConfigureAdapter(struct DevUnit *);
876 ****************************************************************************
880 VOID
ConfigureAdapter(struct DevUnit
*unit
, struct DevBase
*base
)
887 for(i
= 0; i
< ETH_ADDRESSSIZE
; i
++)
888 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_BSSID
+ i
, unit
->bssid
[i
]);
890 /* Set channel and power */
892 SetPower(unit
, base
);
894 msr
= unit
->ByteIn(unit
->card
, 0x100 + R8180REG_MSR
)
895 & ~R8180REG_MSRF_LINK
;
896 if(unit
->assoc_id
!= 0)
898 msr
|= 2 << R8180REG_MSRB_LINK
;
899 if(unit
->generation
>= RTL8187B0_GEN
)
900 msr
|= R8180REG_MSRF_ENEDCA
;
902 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_MSR
, msr
);
906 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_ATIMWINDOW
, 2);
907 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_ATIMTRINTERVAL
, 100);
908 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_BEACONINTERVAL
, 100);
909 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_BEACONINTERVAL2
, 100);
911 if(unit
->generation
>= RTL8187B0_GEN
)
913 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_SIFS
, 0x22);
914 if(unit
->band
== S2BAND_G
)
915 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_SLOT
, 0x9);
917 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_SLOT
, 0x14);
918 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_EIFS
, 0x5b);
919 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_SENSECOUNT
, 0x5b);
929 /****i* realtek8180.device/GoOnline ****************************************
932 * GoOnline -- Enable transmission/reception.
937 * VOID GoOnline(struct DevUnit *);
939 ****************************************************************************
943 VOID
GoOnline(struct DevUnit
*unit
, struct DevBase
*base
)
945 /* Enable interrupts */
947 unit
->flags
|= UNITF_ONLINE
;
948 unit
->LEWordOut(unit
->card
, 0x100 + R8180REG_INTMASK
, INT_MASK
);
950 /* Enable frame transmission and reception */
952 unit
->ByteOut(unit
->card
, 0x100 + R8180REG_COMMAND
,
953 R8180REG_COMMANDF_TXENABLE
| R8180REG_COMMANDF_RXENABLE
);
955 /* Record start time and report Online event */
957 GetSysTime(&unit
->stats
.LastStart
);
958 ReportEvents(unit
, S2EVENT_ONLINE
, base
);
965 /****i* realtek8180.device/GoOffline ***************************************
968 * GoOffline -- Disable transmission/reception.
973 * VOID GoOffline(struct DevUnit *);
983 ****************************************************************************
987 VOID
GoOffline(struct DevUnit
*unit
, struct DevBase
*base
)
989 unit
->flags
&= ~UNITF_ONLINE
;
991 /* Flush pending read and write requests */
993 FlushUnit(unit
, MGMT_QUEUE
, S2ERR_OUTOFSERVICE
, base
);
995 /* Report Offline event and return */
997 ReportEvents(unit
, S2EVENT_OFFLINE
, base
);
1003 /****i* realtek8180.device/SetOptions **************************************
1006 * SetOptions -- Set and use interface options.
1009 * reconfigure = SetOptions(unit, tag_list)
1011 * BOOL SetOptions(struct DevUnit *, struct TagItem *);
1013 ****************************************************************************
1017 BOOL
SetOptions(struct DevUnit
*unit
, const struct TagItem
*tag_list
,
1018 struct DevBase
*base
)
1020 struct TagItem
*tag_item
, *tlist
= (struct TagItem
*)tag_list
;
1021 BOOL reconfigure
= TRUE
;
1023 while((tag_item
= NextTagItem(&tlist
)) != NULL
)
1025 switch(tag_item
->ti_Tag
)
1028 CopyMem((APTR
)tag_item
->ti_Data
, unit
->bssid
, ETH_ADDRESSSIZE
);
1031 case S2INFO_AssocID
:
1032 unit
->assoc_id
= tag_item
->ti_Data
;
1035 case S2INFO_Capabilities
:
1036 unit
->capabilities
= tag_item
->ti_Data
;
1037 if((unit
->capabilities
& (1 << 5)) != 0)
1038 unit
->flags
|= UNITF_SHORTPREAMBLE
;
1040 unit
->flags
&= ~UNITF_SHORTPREAMBLE
;
1043 case S2INFO_DefaultKeyNo
:
1044 unit
->tx_key_no
= tag_item
->ti_Data
;
1047 case S2INFO_PortType
:
1048 unit
->mode
= tag_item
->ti_Data
;
1051 case S2INFO_Channel
:
1052 if(tag_item
->ti_Data
!= unit
->channel
)
1054 unit
->channel
= tag_item
->ti_Data
;
1060 unit
->band
= tag_item
->ti_Data
;
1061 if(unit
->band
== S2BAND_G
)
1064 unit
->tx_rate_code
= 11; /* 54 Mbps */
1065 unit
->mgmt_rate
= 2;
1066 unit
->mgmt_rate_code
= 1; /* 2 Mbps */
1068 else if(unit
->band
== S2BAND_B
)
1071 unit
->tx_rate_code
= 3; /* 11 Mbps */
1072 unit
->mgmt_rate
= 1;
1073 unit
->mgmt_rate_code
= 0; /* 1 Mbps */
1085 /****i* realtek8180.device/SetKey ******************************************
1088 * SetKey -- Set an encryption key.
1091 * SetKey(unit, index, type, key, key_length,
1094 * VOID SetKey(struct DevUnit *, ULONG, ULONG, UBYTE *, ULONG,
1097 ****************************************************************************
1101 VOID
SetKey(struct DevUnit
*unit
, ULONG index
, ULONG type
, const UBYTE
*key
,
1102 ULONG key_length
, const UBYTE
*rx_counter
, struct DevBase
*base
)
1104 struct KeyUnion
*slot
;
1105 struct EClockVal eclock
;
1109 slot
= &unit
->keys
[index
];
1113 CopyMem(key
, slot
->u
.wep
.key
, key_length
);
1114 slot
->u
.wep
.length
= key_length
;
1116 if((unit
->flags
& UNITF_HARDWEP
) == 0)
1118 /* Create a reasonably random IV */
1120 ReadEClock(&eclock
);
1121 slot
->u
.wep
.tx_iv
= FastRand(eclock
.ev_lo
^ eclock
.ev_hi
);
1127 CopyMem(key
, slot
->u
.tkip
.key
, 16);
1128 CopyMem(key
+ 16, slot
->u
.tkip
.tx_mic_key
, MIC_SIZE
);
1129 CopyMem(key
+ 24, slot
->u
.tkip
.rx_mic_key
, MIC_SIZE
);
1130 slot
->u
.tkip
.tx_iv_low
= 0;
1131 slot
->u
.tkip
.tx_iv_high
= 0;
1132 slot
->u
.tkip
.rx_iv_low
= LEWord(*(UWORD
*)rx_counter
);
1133 slot
->u
.tkip
.rx_iv_high
= LELong(*(ULONG
*)(rx_counter
+ 2));
1134 slot
->u
.tkip
.tx_ttak_set
= FALSE
;
1135 slot
->u
.tkip
.rx_ttak_set
= FALSE
;
1137 if((unit
->flags
& UNITF_HARDTKIP
) != 0)
1142 /* Convert key to native endianness */
1144 for(i
= 0; i
< 8; i
++)
1145 slot
->u
.tkip
.key
[i
] = LEWord(slot
->u
.tkip
.key
[i
]);
1151 CopyMem(key
, slot
->u
.ccmp
.key
, 16);
1152 slot
->u
.ccmp
.tx_iv_low
= 0;
1153 slot
->u
.ccmp
.tx_iv_high
= 0;
1154 slot
->u
.ccmp
.rx_iv_low
= LEWord(*(UWORD
*)rx_counter
);
1155 slot
->u
.ccmp
.rx_iv_high
= LELong(*(ULONG
*)(rx_counter
+ 2));
1156 slot
->u
.ccmp
.stream_set
= FALSE
;
1159 /* Update type of key in selected slot */
1169 /****i* realtek8180.device/AddMulticastRange *******************************
1175 * success = AddMulticastRange(unit, lower_bound, upper_bound)
1177 * BOOL AddMulticastRange(struct DevUnit *, UBYTE *, UBYTE *);
1179 ****************************************************************************
1183 BOOL
AddMulticastRange(struct DevUnit
*unit
, const UBYTE
*lower_bound
,
1184 const UBYTE
*upper_bound
, struct DevBase
*base
)
1186 struct AddressRange
*range
;
1187 ULONG lower_bound_left
, upper_bound_left
;
1188 UWORD lower_bound_right
, upper_bound_right
;
1190 lower_bound_left
= BELong(*((ULONG
*)lower_bound
));
1191 lower_bound_right
= BEWord(*((UWORD
*)(lower_bound
+ 4)));
1192 upper_bound_left
= BELong(*((ULONG
*)upper_bound
));
1193 upper_bound_right
= BEWord(*((UWORD
*)(upper_bound
+ 4)));
1195 range
= FindMulticastRange(unit
, lower_bound_left
, lower_bound_right
,
1196 upper_bound_left
, upper_bound_right
, base
);
1202 range
= AllocMem(sizeof(struct AddressRange
), MEMF_PUBLIC
);
1205 range
->lower_bound_left
= lower_bound_left
;
1206 range
->lower_bound_right
= lower_bound_right
;
1207 range
->upper_bound_left
= upper_bound_left
;
1208 range
->upper_bound_right
= upper_bound_right
;
1209 range
->add_count
= 1;
1212 AddTail((APTR
)&unit
->multicast_ranges
, (APTR
)range
);
1213 unit
->range_count
++;
1214 SetMulticast(unit
, base
);
1219 return range
!= NULL
;
1224 /****i* realtek8180.device/RemMulticastRange *******************************
1230 * found = RemMulticastRange(unit, lower_bound, upper_bound)
1232 * BOOL RemMulticastRange(struct DevUnit *, UBYTE *, UBYTE *);
1234 ****************************************************************************
1238 BOOL
RemMulticastRange(struct DevUnit
*unit
, const UBYTE
*lower_bound
,
1239 const UBYTE
*upper_bound
, struct DevBase
*base
)
1241 struct AddressRange
*range
;
1242 ULONG lower_bound_left
, upper_bound_left
;
1243 UWORD lower_bound_right
, upper_bound_right
;
1245 lower_bound_left
= BELong(*((ULONG
*)lower_bound
));
1246 lower_bound_right
= BEWord(*((UWORD
*)(lower_bound
+ 4)));
1247 upper_bound_left
= BELong(*((ULONG
*)upper_bound
));
1248 upper_bound_right
= BEWord(*((UWORD
*)(upper_bound
+ 4)));
1250 range
= FindMulticastRange(unit
, lower_bound_left
, lower_bound_right
,
1251 upper_bound_left
, upper_bound_right
, base
);
1255 if(--range
->add_count
== 0)
1258 Remove((APTR
)range
);
1259 unit
->range_count
--;
1260 SetMulticast(unit
, base
);
1262 FreeMem(range
, sizeof(struct AddressRange
));
1266 return range
!= NULL
;
1271 /****i* realtek8180.device/FindMulticastRange ******************************
1274 * FindMulticastRange
1277 * range = FindMulticastRange(unit, lower_bound_left,
1278 * lower_bound_right, upper_bound_left, upper_bound_right)
1280 * struct AddressRange *FindMulticastRange(struct DevUnit *, ULONG,
1281 * UWORD, ULONG, UWORD);
1283 ****************************************************************************
1287 static struct AddressRange
*FindMulticastRange(struct DevUnit
*unit
,
1288 ULONG lower_bound_left
, UWORD lower_bound_right
, ULONG upper_bound_left
,
1289 UWORD upper_bound_right
, struct DevBase
*base
)
1291 struct AddressRange
*range
, *tail
;
1294 range
= (APTR
)unit
->multicast_ranges
.mlh_Head
;
1295 tail
= (APTR
)&unit
->multicast_ranges
.mlh_Tail
;
1297 while(range
!= tail
&& !found
)
1299 if(lower_bound_left
== range
->lower_bound_left
&&
1300 lower_bound_right
== range
->lower_bound_right
&&
1301 upper_bound_left
== range
->upper_bound_left
&&
1302 upper_bound_right
== range
->upper_bound_right
)
1305 range
= (APTR
)range
->node
.mln_Succ
;
1316 /****i* realtek8180.device/SetMulticast ************************************
1322 * SetMulticast(unit)
1324 * VOID SetMulticast(struct DevUnit *);
1326 ****************************************************************************
1330 static VOID
SetMulticast(struct DevUnit
*unit
, struct DevBase
*base
)
1337 /****i* realtek8180.device/FindTypeStats ***********************************
1343 * stats = FindTypeStats(unit, list,
1346 * struct TypeStats *FindTypeStats(struct DevUnit *, struct MinList *,
1349 ****************************************************************************
1353 struct TypeStats
*FindTypeStats(struct DevUnit
*unit
, struct MinList
*list
,
1354 ULONG packet_type
, struct DevBase
*base
)
1356 struct TypeStats
*stats
, *tail
;
1359 stats
= (APTR
)list
->mlh_Head
;
1360 tail
= (APTR
)&list
->mlh_Tail
;
1362 while(stats
!= tail
&& !found
)
1364 if(stats
->packet_type
== packet_type
)
1367 stats
= (APTR
)stats
->node
.mln_Succ
;
1378 /****i* realtek8180.device/FlushUnit ***************************************
1384 * FlushUnit(unit, last_queue, error)
1386 * VOID FlushUnit(struct DevUnit *, UBYTE, BYTE);
1388 ****************************************************************************
1392 VOID
FlushUnit(struct DevUnit
*unit
, UBYTE last_queue
, BYTE error
,
1393 struct DevBase
*base
)
1395 struct IORequest
*request
;
1397 struct Opener
*opener
, *tail
;
1399 /* Abort queued requests */
1401 for(i
= 0; i
<= last_queue
; i
++)
1403 while((request
= (APTR
)GetMsg(unit
->request_ports
[i
])) != NULL
)
1405 request
->io_Error
= IOERR_ABORTED
;
1406 ReplyMsg((APTR
)request
);
1411 opener
= (APTR
)unit
->openers
.mlh_Head
;
1412 tail
= (APTR
)&unit
->openers
.mlh_Tail
;
1414 /* Flush every opener's read queues */
1416 while(opener
!= tail
)
1418 while((request
= (APTR
)GetMsg(&opener
->read_port
)) != NULL
)
1420 request
->io_Error
= error
;
1421 ReplyMsg((APTR
)request
);
1423 while((request
= (APTR
)GetMsg(&opener
->mgmt_port
)) != NULL
)
1425 request
->io_Error
= error
;
1426 ReplyMsg((APTR
)request
);
1428 opener
= (APTR
)opener
->node
.mln_Succ
;
1432 opener
= request
->ios2_BufferManagement
;
1433 while((request
= (APTR
)GetMsg(&opener
->read_port
)) != NULL
)
1435 request
->io_Error
= IOERR_ABORTED
;
1436 ReplyMsg((APTR
)request
);
1438 while((request
= (APTR
)GetMsg(&opener
->mgmt_port
)) != NULL
)
1440 request
->io_Error
= IOERR_ABORTED
;
1441 ReplyMsg((APTR
)request
);
1452 /****i* realtek8180.device/RXInt *******************************************
1455 * RXInt -- Soft interrupt for packet reception.
1460 * VOID RXInt(struct DevUnit *);
1465 * unit - A unit of this device.
1470 ****************************************************************************
1474 VOID
RXInt(REG(a1
, struct DevUnit
*unit
), REG(a6
, APTR int_code
))
1476 UWORD ieee_length
, frame_control
, frame_type
, slot
, next_slot
,
1477 frame_size
, frame_subtype
, encryption
, key_no
, buffer_no
, old_length
;
1478 struct DevBase
*base
;
1479 BOOL is_good
, proceed
= TRUE
;
1482 UBYTE
*rx_desc
, *buffer
, *p
, *frame
, *data
, *snap_frame
, *source
;
1484 base
= unit
->device
;
1485 slot
= unit
->rx_slot
;
1486 rx_desc
= unit
->rx_descs
[slot
];
1487 next_slot
= (slot
+ 1) % RX_SLOT_COUNT
;
1492 buffer
= unit
->rx_buffers
[slot
];
1494 status
= LELong(*(ULONG
*)(rx_desc
+ R8180FRM_RXSTATUS
));
1495 frame_size
= status
& R8180FRM_RXSTATUSF_LENGTH
;
1497 if((status
& (R8180FRM_RXSTATUSF_DMAERR
| R8180FRM_RXSTATUSF_OVERFLOW
1498 | R8180FRM_RXSTATUSF_RXERR
| R8180FRM_RXSTATUSF_BADCRC
)) == 0
1499 && frame_size
>= WIFI_FRM_DATA
+ 4)
1501 /* Get fragment info */
1504 ieee_length
= frame_size
- 4 - WIFI_FRM_DATA
;
1505 data
= frame
+ WIFI_FRM_DATA
;
1507 LEWord(*(UWORD
*)(frame
+ WIFI_FRM_CONTROL
));
1509 /* Get buffer to store fragment in */
1511 frag_no
= LEWord(*(UWORD
*)(frame
+ WIFI_FRM_SEQCONTROL
));
1512 if(unit
->mode
== S2PORT_ADHOC
)
1513 source
= frame
+ WIFI_FRM_ADDRESS2
;
1515 source
= frame
+ WIFI_FRM_ADDRESS3
;
1516 snap_frame
= GetRXBuffer(unit
, source
, frag_no
, &buffer_no
, base
);
1518 /* Get location to put new data */
1520 if(snap_frame
!= NULL
)
1522 if((frag_no
& 0xf ) > 0)
1524 BEWord(*(UWORD
*)(snap_frame
+ ETH_PACKET_IEEELEN
));
1527 /* Create new 802.3 header */
1529 CopyMem(frame
+ WIFI_FRM_ADDRESS1
, snap_frame
,
1531 CopyMem(source
, snap_frame
+ ETH_PACKET_SOURCE
,
1535 p
= snap_frame
+ ETH_HEADERSIZE
+ old_length
;
1537 /* Append fragment to frame, decrypting fragment if necessary */
1539 if((frame_control
& WIFI_FRM_CONTROLF_WEP
) != 0)
1541 key_no
= data
[3] >> 6 & 0x3;
1542 encryption
= unit
->keys
[key_no
].type
;
1545 encryption
= S2ENC_NONE
;
1547 /* Decrypt, check and/or copy fragment */
1549 is_good
= unit
->fragment_decrypt_functions
[encryption
](unit
,
1550 frame
, data
, &ieee_length
, p
, base
);
1552 /* Update length in frame being built with current fragment, or
1553 increment bad frame counter if fragment is bad */
1557 ieee_length
+= old_length
;
1558 *(UWORD
*)(snap_frame
+ ETH_PACKET_IEEELEN
) =
1559 MakeBEWord(ieee_length
);
1562 unit
->stats
.BadData
++;
1564 /* If all fragments have arrived, process the complete frame */
1566 if((frame_control
& WIFI_FRM_CONTROLF_MOREFRAGS
) == 0)
1570 /* Decrypt complete frame if necessary */
1572 data
= snap_frame
+ ETH_HEADERSIZE
;
1573 if(encryption
== S2ENC_TKIP
1574 && (unit
->flags
& UNITF_HARDTKIP
) == 0)
1576 is_good
= TKIPDecryptFrame(unit
, snap_frame
, data
,
1577 ieee_length
, data
, key_no
, base
);
1578 ieee_length
-= MIC_SIZE
;
1579 *(UWORD
*)(snap_frame
+ ETH_PACKET_IEEELEN
) =
1580 MakeBEWord(ieee_length
);
1582 unit
->stats
.BadData
++;
1588 /* Get frame's 802.11 type and subtype */
1590 frame_type
= (frame_control
& WIFI_FRM_CONTROLF_TYPE
)
1591 >> WIFI_FRM_CONTROLB_TYPE
;
1593 (frame_control
& WIFI_FRM_CONTROLF_SUBTYPE
)
1594 >> WIFI_FRM_CONTROLB_SUBTYPE
;
1596 /* If it's a management frame, process it separately;
1597 otherwise distribute it to clients after filtering */
1599 if(frame_type
== WIFI_FRMTYPE_MGMT
)
1601 if(frame_subtype
!= 8)
1602 DistributeMgmtFrame(unit
, frame
, frame_size
- 4,
1605 else if(AddressFilter(unit
, snap_frame
+ ETH_PACKET_DEST
,
1608 unit
->stats
.PacketsReceived
++;
1609 DistributeRXPacket(unit
, snap_frame
, base
);
1614 /* Mark fragment buffer as unused for next time */
1616 unit
->rx_fragment_nos
[buffer_no
] = -1;
1619 ReportEvents(unit
, S2EVENT_ERROR
| S2EVENT_RX
, base
);
1628 ReportEvents(unit
, S2EVENT_ERROR
| S2EVENT_HARDWARE
| S2EVENT_RX
,
1632 /* Prepare descriptor for next time */
1634 unit
->ReceiveFrame(unit
->card
, buffer
,
1635 FRAME_BUFFER_SIZE
+ R8180_MAXDESCSIZE
);
1637 /* Get next descriptor */
1640 if(unit
->bus
== USB_BUS
)
1644 unit
->rx_slot
= slot
;
1651 /****i* realtek8180.device/GetRXBuffer *************************************
1654 * GetRXBuffer -- Find an appropriate RX frame buffer to use.
1657 * buffer = GetRXBuffer(unit, address, frag_no)
1659 * UBYTE *GetRXBuffer(struct DevUnit *, UBYTE *, UWORD);
1661 ****************************************************************************
1665 static UBYTE
*GetRXBuffer(struct DevUnit
*unit
, const UBYTE
*address
,
1666 UWORD frag_no
, UWORD
*buffer_no
, struct DevBase
*base
)
1673 buffer
= unit
->rx_frames
;
1674 for(i
= 0, found
= FALSE
; i
< FRAME_BUFFER_COUNT
* 2 && !found
; i
++)
1676 /* Throw away old buffer contents if we didn't find a free slot the
1677 first time around */
1679 if(i
>= FRAME_BUFFER_COUNT
)
1680 unit
->rx_fragment_nos
[i
% FRAME_BUFFER_COUNT
] = -1;
1682 /* For a frame's first fragment, find an empty slot; for subsequent
1683 fragments, find a slot with matching source address */
1685 n
= unit
->rx_fragment_nos
[i
% FRAME_BUFFER_COUNT
];
1686 if(n
== -1 && (frag_no
& 0xf) == 0
1687 || *((ULONG
*)(buffer
+ ETH_PACKET_SOURCE
))
1688 == *((ULONG
*)(address
))
1689 && *((UWORD
*)(buffer
+ ETH_PACKET_SOURCE
+ 4))
1690 == *((UWORD
*)(address
+ 4)))
1694 unit
->rx_fragment_nos
[i
% FRAME_BUFFER_COUNT
] = frag_no
;
1698 buffer
+= FRAME_BUFFER_SIZE
;
1709 /****i* realtek8180.device/DistributeRXPacket ******************************
1712 * DistributeRXPacket -- Send a packet to all appropriate destinations.
1715 * DistributeRXPacket(unit, frame)
1717 * VOID DistributeRXPacket(struct DevUnit *, UBYTE *);
1719 ****************************************************************************
1723 static VOID
DistributeRXPacket(struct DevUnit
*unit
, const UBYTE
*frame
,
1724 struct DevBase
*base
)
1726 UWORD packet_size
, ieee_length
;
1727 BOOL is_orphan
= TRUE
, accepted
, is_snap
= FALSE
;
1730 const UBYTE
*template = snap_template
;
1731 struct IOSana2Req
*request
, *request_tail
;
1732 struct Opener
*opener
, *opener_tail
;
1733 struct TypeStats
*tracker
;
1735 buffer
= unit
->rx_buffer
;
1736 ieee_length
= BEWord(*(UWORD
*)(frame
+ ETH_PACKET_IEEELEN
));
1737 packet_size
= ETH_HEADERSIZE
+ ieee_length
;
1738 if(ieee_length
>= SNAP_HEADERSIZE
)
1739 is_snap
= *(const ULONG
*)(frame
+ ETH_PACKET_DATA
)
1740 == *(const ULONG
*)template;
1742 /* De-encapsulate SNAP packets and get packet type */
1746 packet_size
-= SNAP_HEADERSIZE
;
1747 CopyMem(frame
, buffer
, ETH_PACKET_TYPE
);
1748 CopyMem(frame
+ ETH_HEADERSIZE
+ SNAP_FRM_TYPE
,
1749 buffer
+ ETH_PACKET_TYPE
, packet_size
- ETH_PACKET_TYPE
);
1751 packet_type
= BEWord(*((UWORD
*)(buffer
+ ETH_PACKET_TYPE
)));
1753 /* Offer packet to every opener */
1755 opener
= (APTR
)unit
->openers
.mlh_Head
;
1756 opener_tail
= (APTR
)&unit
->openers
.mlh_Tail
;
1758 while(opener
!= opener_tail
)
1760 request
= (APTR
)opener
->read_port
.mp_MsgList
.lh_Head
;
1761 request_tail
= (APTR
)&opener
->read_port
.mp_MsgList
.lh_Tail
;
1764 /* Offer packet to each request until it's accepted */
1766 while(request
!= request_tail
&& !accepted
)
1768 if(request
->ios2_PacketType
== packet_type
)
1770 CopyPacket(unit
, request
, packet_size
, packet_type
,
1775 (APTR
)request
->ios2_Req
.io_Message
.mn_Node
.ln_Succ
;
1780 opener
= (APTR
)opener
->node
.mln_Succ
;
1783 /* If packet was unwanted, give it to S2_READORPHAN request */
1787 unit
->stats
.UnknownTypesReceived
++;
1788 if(!IsMsgPortEmpty(unit
->request_ports
[ADOPT_QUEUE
]))
1791 (APTR
)unit
->request_ports
[ADOPT_QUEUE
]->
1792 mp_MsgList
.lh_Head
, packet_size
, packet_type
, buffer
,
1797 /* Update remaining statistics */
1799 if(packet_type
<= ETH_MTU
)
1800 packet_type
= ETH_MTU
;
1802 FindTypeStats(unit
, &unit
->type_trackers
, packet_type
, base
);
1805 tracker
->stats
.PacketsReceived
++;
1806 tracker
->stats
.BytesReceived
+= packet_size
;
1814 /****i* realtek8180.device/CopyPacket **************************************
1817 * CopyPacket -- Copy packet to client's buffer.
1820 * CopyPacket(unit, request, packet_size, packet_type,
1823 * VOID CopyPacket(struct DevUnit *, struct IOSana2Req *, UWORD, UWORD,
1826 ****************************************************************************
1830 static VOID
CopyPacket(struct DevUnit
*unit
, struct IOSana2Req
*request
,
1831 UWORD packet_size
, UWORD packet_type
, UBYTE
*buffer
,
1832 struct DevBase
*base
)
1834 struct Opener
*opener
;
1835 BOOL filtered
= FALSE
;
1837 /* Set multicast and broadcast flags */
1839 request
->ios2_Req
.io_Flags
&= ~(SANA2IOF_BCAST
| SANA2IOF_MCAST
);
1840 if((*((ULONG
*)(buffer
+ ETH_PACKET_DEST
)) == 0xffffffff) &&
1841 (*((UWORD
*)(buffer
+ ETH_PACKET_DEST
+ 4)) == 0xffff))
1842 request
->ios2_Req
.io_Flags
|= SANA2IOF_BCAST
;
1843 else if((buffer
[ETH_PACKET_DEST
] & 0x1) != 0)
1844 request
->ios2_Req
.io_Flags
|= SANA2IOF_MCAST
;
1846 /* Set source and destination addresses and packet type */
1848 CopyMem(buffer
+ ETH_PACKET_SOURCE
, request
->ios2_SrcAddr
,
1850 CopyMem(buffer
+ ETH_PACKET_DEST
, request
->ios2_DstAddr
,
1852 request
->ios2_PacketType
= packet_type
;
1854 /* Adjust for cooked packet request */
1856 if((request
->ios2_Req
.io_Flags
& SANA2IOF_RAW
) == 0)
1858 packet_size
-= ETH_PACKET_DATA
;
1859 buffer
+= ETH_PACKET_DATA
;
1863 packet_size
+= 4; /* Needed for Shapeshifter & Fusion? */
1865 request
->ios2_DataLength
= packet_size
;
1869 opener
= request
->ios2_BufferManagement
;
1870 if(request
->ios2_Req
.io_Command
== CMD_READ
&&
1871 opener
->filter_hook
!= NULL
)
1872 if(!CallHookPkt(opener
->filter_hook
, request
, buffer
))
1877 /* Copy packet into opener's buffer and reply packet */
1879 if(!opener
->rx_function(request
->ios2_Data
, buffer
, packet_size
))
1881 request
->ios2_Req
.io_Error
= S2ERR_NO_RESOURCES
;
1882 request
->ios2_WireError
= S2WERR_BUFF_ERROR
;
1884 S2EVENT_ERROR
| S2EVENT_SOFTWARE
| S2EVENT_BUFF
| S2EVENT_RX
,
1887 Remove((APTR
)request
);
1888 ReplyMsg((APTR
)request
);
1896 /****i* realtek8180.device/AddressFilter ***********************************
1899 * AddressFilter -- Determine if an RX packet should be accepted.
1902 * accept = AddressFilter(unit, address)
1904 * BOOL AddressFilter(struct DevUnit *, UBYTE *);
1906 ****************************************************************************
1910 static BOOL
AddressFilter(struct DevUnit
*unit
, UBYTE
*address
,
1911 struct DevBase
*base
)
1913 struct AddressRange
*range
, *tail
;
1916 UWORD address_right
;
1918 /* Check whether address is unicast/broadcast or multicast */
1920 address_left
= BELong(*((ULONG
*)address
));
1921 address_right
= BEWord(*((UWORD
*)(address
+ 4)));
1923 if(((address_left
& 0x01000000) != 0) &&
1924 !((address_left
== 0xffffffff) && (address_right
== 0xffff)))
1926 /* Check if this multicast address is wanted */
1928 range
= (APTR
)unit
->multicast_ranges
.mlh_Head
;
1929 tail
= (APTR
)&unit
->multicast_ranges
.mlh_Tail
;
1932 while((range
!= tail
) && !accept
)
1934 if((address_left
> range
->lower_bound_left
||
1935 address_left
== range
->lower_bound_left
&&
1936 address_right
>= range
->lower_bound_right
) &&
1937 (address_left
< range
->upper_bound_left
||
1938 address_left
== range
->upper_bound_left
&&
1939 address_right
<= range
->upper_bound_right
))
1941 range
= (APTR
)range
->node
.mln_Succ
;
1945 unit
->special_stats
[S2SS_ETHERNET_BADMULTICAST
& 0xffff]++;
1953 /****i* realtek8180.device/DistributeMgmtFrame *****************************
1956 * DistributeMgmtFrame -- Send a management frame to clients.
1959 * DistributeMgmtFrame(unit, frame, frame_size)
1961 * VOID DistributeMgmtFrame(struct DevUnit *, UBYTE *, UWORD);
1963 ****************************************************************************
1967 static VOID
DistributeMgmtFrame(struct DevUnit
*unit
, UBYTE
*frame
,
1968 UWORD frame_size
, struct DevBase
*base
)
1970 struct IOSana2Req
*request
;
1971 struct Opener
*opener
, *opener_tail
;
1973 /* Send packet to every opener */
1975 opener
= (APTR
)unit
->openers
.mlh_Head
;
1976 opener_tail
= (APTR
)&unit
->openers
.mlh_Tail
;
1978 while(opener
!= opener_tail
)
1980 request
= (APTR
)RemHead(&opener
->mgmt_port
.mp_MsgList
);
1984 /* Copy packet into opener's buffer and reply packet */
1986 if(frame_size
<= request
->ios2_DataLength
)
1988 CopyMem(frame
, request
->ios2_Data
, frame_size
);
1989 request
->ios2_DataLength
= frame_size
;
1993 request
->ios2_Req
.io_Error
= S2ERR_NO_RESOURCES
;
1994 request
->ios2_WireError
= S2WERR_BUFF_ERROR
;
1996 S2EVENT_ERROR
| S2EVENT_SOFTWARE
| S2EVENT_BUFF
| S2EVENT_RX
,
1999 ReplyMsg((APTR
)request
);
2001 (APTR
)request
->ios2_Req
.io_Message
.mn_Node
.ln_Succ
;
2004 opener
= (APTR
)opener
->node
.mln_Succ
;
2012 /****i* realtek8180.device/TXInt *******************************************
2015 * TXInt -- Soft interrupt for packet transmission.
2020 * VOID TXInt(struct DevUnit *);
2025 * unit - A unit of this device.
2030 ****************************************************************************
2034 static VOID
TXInt(REG(a1
, struct DevUnit
*unit
), REG(a6
, APTR int_code
))
2036 struct DevBase
*base
;
2037 UWORD i
, frame_size
, data_size
, packet_type
, body_size
, slot
, new_slot
,
2038 encryption
, subtype
, duration
;
2039 UBYTE
*buffer
, *q
, *plaintext
, *ciphertext
, *frame
,
2040 mic_header
[ETH_ADDRESSSIZE
* 2];
2041 const UBYTE
*p
, *dest
, *source
;
2042 struct IOSana2Req
*request
;
2043 BOOL proceed
= TRUE
, is_ieee
, has_bssid
;
2044 struct Opener
*opener
;
2045 ULONG wire_error
, control_value
;
2047 UBYTE
*(*dma_tx_function
)(REG(a0
, APTR
));
2049 struct MsgPort
*port
;
2050 struct TypeStats
*tracker
;
2052 base
= unit
->device
;
2053 port
= unit
->request_ports
[WRITE_QUEUE
];
2055 while(proceed
&& (!IsMsgPortEmpty(port
)))
2057 slot
= unit
->tx_in_slot
;
2058 new_slot
= (slot
+ 1) % TX_SLOT_COUNT
;
2060 if(new_slot
!= unit
->tx_out_slot
)
2065 /* Get request and DMA frame descriptor */
2067 request
= (APTR
)port
->mp_MsgList
.lh_Head
;
2069 Remove((APTR
)request
);
2070 unit
->tx_requests
[slot
] = request
;
2071 tx_desc
= unit
->tx_descs
[slot
];
2072 frame
= unit
->tx_buffers
[slot
];
2074 /* Get packet data */
2076 opener
= request
->ios2_BufferManagement
;
2077 dma_tx_function
= opener
->dma_tx_function
;
2078 if(dma_tx_function
!= NULL
)
2079 buffer
= dma_tx_function(request
->ios2_Data
);
2085 buffer
= unit
->tx_buffer
;
2086 if(!opener
->tx_function(buffer
, request
->ios2_Data
,
2087 request
->ios2_DataLength
))
2089 error
= S2ERR_NO_RESOURCES
;
2090 wire_error
= S2WERR_BUFF_ERROR
;
2092 S2EVENT_ERROR
| S2EVENT_SOFTWARE
| S2EVENT_BUFF
2093 | S2EVENT_TX
, base
);
2099 /* Get packet type and/or length */
2101 data_size
= request
->ios2_DataLength
;
2102 if((request
->ios2_Req
.io_Flags
& SANA2IOF_RAW
) != 0)
2104 data_size
-= ETH_PACKET_DATA
;
2105 packet_type
= BEWord(*(UWORD
*)(buffer
+ ETH_PACKET_TYPE
));
2108 packet_type
= request
->ios2_PacketType
;
2109 is_ieee
= packet_type
<= ETH_MTU
;
2111 /* Determine encryption type and frame subtype */
2115 encryption
= unit
->keys
[unit
->tx_key_no
].type
;
2120 encryption
= S2ENC_NONE
;
2124 /* Get source and destination addresses */
2126 if((request
->ios2_Req
.io_Flags
& SANA2IOF_RAW
) != 0)
2129 source
= buffer
+ ETH_ADDRESSSIZE
;
2130 buffer
+= ETH_ADDRESSSIZE
* 2 + 2;
2134 dest
= request
->ios2_DstAddr
;
2135 source
= unit
->address
;
2138 /* Write 802.11 header */
2141 *(UWORD
*)q
= MakeLEWord(
2142 (encryption
== S2ENC_NONE
? 0 : WIFI_FRM_CONTROLF_WEP
)
2143 | (unit
->mode
== S2PORT_ADHOC
? 0 : WIFI_FRM_CONTROLF_TODS
)
2144 | subtype
<< WIFI_FRM_CONTROLB_SUBTYPE
2145 | WIFI_FRMTYPE_DATA
<< WIFI_FRM_CONTROLB_TYPE
);
2148 if(unit
->mode
== S2PORT_ADHOC
)
2152 for(i
= 0; i
< ETH_ADDRESSSIZE
; i
++)
2155 for(i
= 0, p
= source
; i
< ETH_ADDRESSSIZE
; i
++)
2158 if(unit
->mode
== S2PORT_ADHOC
)
2162 for(i
= 0; i
< ETH_ADDRESSSIZE
; i
++)
2164 *(UWORD
*)q
= MakeLEWord(unit
->tx_sequence
);
2165 unit
->tx_sequence
+= 0x10;
2168 /* Leave room for encryption overhead */
2171 q
+= unit
->iv_sizes
[encryption
];
2174 /* Write SNAP header */
2178 for(i
= 0, p
= snap_template
;
2179 i
< SNAP_FRM_TYPE
; i
++)
2181 *(UWORD
*)q
= MakeBEWord(packet_type
);
2183 body_size
+= SNAP_HEADERSIZE
;
2186 /* Copy data into frame */
2188 CopyMem(buffer
, q
, data_size
);
2189 body_size
+= data_size
;
2191 /* Append MIC to frame for TKIP */
2193 if(encryption
== S2ENC_TKIP
)
2196 for(i
= 0, p
= dest
; i
< ETH_ADDRESSSIZE
; i
++)
2198 for(i
= 0, p
= source
; i
< ETH_ADDRESSSIZE
; i
++)
2200 TKIPEncryptFrame(unit
, mic_header
, plaintext
, body_size
,
2202 body_size
+= MIC_SIZE
;
2205 /* Encrypt fragment if applicable */
2207 unit
->fragment_encrypt_functions
[encryption
](unit
, frame
,
2208 plaintext
, &body_size
, ciphertext
, base
);
2210 /* Clear frame descriptor as far as start of 802.11 header */
2213 for(i
= 0; i
< unit
->tx_desc_size
; i
++)
2216 /* Set TX control field */
2218 frame_size
= WIFI_FRM_DATA
+ body_size
;
2219 control_value
= R8180FRM_TXCONTROLF_NOENC
2220 | R8180FRM_TXCONTROLF_FIRSTFRAG
2221 | R8180FRM_TXCONTROLF_LASTFRAG
2222 | unit
->tx_rate_code
<< R8180FRM_TXCONTROLB_RATE
2224 *(ULONG
*)(tx_desc
+ R8180FRM_TXCONTROL
) =
2225 MakeLELong(control_value
);
2229 has_bssid
= ((frame
+ WIFI_FRM_ADDRESS1
)[0] & 0x1) == 0;
2231 duration
= SIFS_TIME
+ GetDuration(unit
, 14,
2232 AckRate(unit
, unit
->tx_rate
, base
), FALSE
, base
);
2235 *(UWORD
*)(frame
+ WIFI_FRM_DURATION
) = MakeLEWord(duration
);
2237 if(unit
->generation
>= RTL8187B0_GEN
)
2239 duration
+= GetDuration(unit
, frame_size
+ 4,
2241 (unit
->flags
& UNITF_SHORTPREAMBLE
) != 0 && has_bssid
,
2243 *(UWORD
*)(tx_desc
+ R8180FRM_TXDUR
) = MakeLEWord(duration
);
2246 /* Set max number of retries */
2248 *(ULONG
*)(tx_desc
+ unit
->retries_offset
) =
2249 MakeLELong((TX_TRIES
- 1) << 8);
2251 /* Pass packet to adapter */
2253 unit
->SendFrame(unit
->card
, tx_desc
,
2254 R8180_MAXDESCSIZE
+ frame_size
);
2255 unit
->tx_in_slot
= new_slot
;
2259 /* Reply failed request */
2261 request
->ios2_Req
.io_Error
= error
;
2262 request
->ios2_WireError
= wire_error
;
2263 ReplyMsg((APTR
)request
);
2266 /* Update statistics */
2270 unit
->stats
.PacketsSent
++;
2272 tracker
= FindTypeStats(unit
, &unit
->type_trackers
,
2273 request
->ios2_PacketType
, base
);
2276 tracker
->stats
.PacketsSent
++;
2277 tracker
->stats
.BytesSent
+= ETH_HEADERSIZE
+ data_size
;
2285 /* Don't try to keep sending packets if there's no space left */
2289 unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
= PA_SOFTINT
;
2290 unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
= PA_SOFTINT
;
2294 unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
= PA_IGNORE
;
2295 unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
= PA_IGNORE
;
2303 /****i* realtek8180.device/MgmtTXInt ***************************************
2306 * MgmtTXInt -- Soft interrupt for management frame transmission.
2311 * VOID MgmtTXInt(struct DevUnit *);
2316 * unit - A unit of this device.
2321 ****************************************************************************
2325 static VOID
MgmtTXInt(REG(a1
, struct DevUnit
*unit
), REG(a6
, APTR int_code
))
2327 struct DevBase
*base
;
2328 UWORD frame_size
, slot
, new_slot
, i
, duration
;
2329 UBYTE
*desc
, *frame
, *q
;
2330 struct IOSana2Req
*request
;
2331 BOOL proceed
= TRUE
, has_bssid
;
2332 ULONG control_value
;
2333 struct MsgPort
*port
;
2335 base
= unit
->device
;
2336 port
= unit
->request_ports
[MGMT_QUEUE
];
2338 while(proceed
&& (!IsMsgPortEmpty(port
)))
2340 slot
= unit
->tx_in_slot
;
2341 new_slot
= (slot
+ 1) % TX_SLOT_COUNT
;
2343 if(new_slot
!= unit
->tx_out_slot
)
2345 /* Get request and frame descriptor */
2347 request
= (APTR
)port
->mp_MsgList
.lh_Head
;
2349 Remove((APTR
)request
);
2350 unit
->tx_requests
[slot
] = request
;
2351 desc
= unit
->tx_descs
[slot
];
2352 frame
= unit
->tx_buffers
[slot
];
2354 /* Get packet length */
2356 frame_size
= request
->ios2_DataLength
;
2358 /* Copy frame into transmit buffer */
2360 CopyMem(request
->ios2_Data
, frame
, frame_size
);
2362 /* Clear frame descriptor as far as start of 802.11 header */
2365 for(i
= 0; i
< unit
->tx_desc_size
; i
++)
2368 /* Set TX control field */
2370 control_value
= R8180FRM_TXCONTROLF_NOENC
2371 | R8180FRM_TXCONTROLF_FIRSTFRAG
2372 | R8180FRM_TXCONTROLF_LASTFRAG
2373 | unit
->mgmt_rate_code
<< R8180FRM_TXCONTROLB_RATE
2375 *(ULONG
*)(desc
+ R8180FRM_TXCONTROL
) = MakeLELong(control_value
);
2379 has_bssid
= ((frame
+ WIFI_FRM_ADDRESS1
)[0] & 0x1) == 0;
2381 duration
= SIFS_TIME
+ GetDuration(unit
, 14,
2382 AckRate(unit
, unit
->mgmt_rate
, base
), FALSE
, base
);
2385 *(UWORD
*)(frame
+ WIFI_FRM_DURATION
) = MakeLEWord(duration
);
2387 if(unit
->generation
>= RTL8187B0_GEN
)
2389 duration
+= GetDuration(unit
, frame_size
+ 4,
2390 unit
->mgmt_rate
, FALSE
, base
);
2391 *(UWORD
*)(desc
+ R8180FRM_TXDUR
) = MakeLEWord(duration
);
2394 /* Set max number of retries */
2396 *(ULONG
*)(desc
+ unit
->retries_offset
) =
2397 MakeLELong((TX_TRIES
- 1) << 8);
2399 /* Set sequence number */
2401 *(UWORD
*)(frame
+ WIFI_FRM_SEQCONTROL
) =
2402 MakeLEWord(unit
->tx_sequence
);
2403 unit
->tx_sequence
+= 0x10;
2405 /* Pass packet to adapter */
2407 unit
->SendFrame(unit
->card
, desc
, R8180_MAXDESCSIZE
+ frame_size
);
2408 unit
->tx_in_slot
= new_slot
;
2414 /* Don't try to keep sending packets if there's no space left */
2418 unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
= PA_SOFTINT
;
2419 unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
= PA_SOFTINT
;
2423 unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
= PA_IGNORE
;
2424 unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
= PA_IGNORE
;
2432 /****i* realtek8180.device/RetireTXSlot ************************************
2435 * RetireTXSlot -- Clean up after a frame has been sent.
2438 * RetireTXSlot(unit)
2440 * VOID RetireTXSlot(struct DevUnit *);
2442 ****************************************************************************
2446 VOID
RetireTXSlot(struct DevUnit
*unit
, struct DevBase
*base
)
2448 UWORD frame_size
, slot
;
2449 struct IOSana2Req
*request
;
2450 struct TypeStats
*tracker
;
2452 /* Update statistics */
2454 slot
= unit
->tx_out_slot
;
2455 request
= unit
->tx_requests
[slot
];
2456 if(request
->ios2_Req
.io_Command
!= S2_WRITEMGMT
)
2458 frame_size
= request
->ios2_DataLength
;
2459 if((request
->ios2_Req
.io_Flags
& SANA2IOF_RAW
) == 0)
2460 frame_size
+= ETH_HEADERSIZE
;
2462 tracker
= FindTypeStats(unit
, &unit
->type_trackers
,
2463 request
->ios2_PacketType
, base
);
2466 tracker
->stats
.PacketsSent
++;
2467 tracker
->stats
.BytesSent
+= frame_size
;
2473 request
->ios2_Req
.io_Error
= 0;
2474 ReplyMsg((APTR
)request
);
2476 unit
->tx_out_slot
= (slot
+ 1) % TX_SLOT_COUNT
;
2478 /* Restart downloads if they had stopped */
2480 if(unit
->request_ports
[WRITE_QUEUE
]->mp_Flags
== PA_IGNORE
)
2481 Cause(&unit
->tx_int
);
2482 if(unit
->request_ports
[MGMT_QUEUE
]->mp_Flags
== PA_IGNORE
)
2483 Cause(&unit
->mgmt_int
);
2490 /****i* realtek8180.device/ReportEvents ************************************
2496 * ReportEvents(unit, events)
2498 * VOID ReportEvents(struct DevUnit *, ULONG);
2503 * unit - A unit of this device.
2504 * events - A mask of events to report.
2509 ****************************************************************************
2513 static VOID
ReportEvents(struct DevUnit
*unit
, ULONG events
,
2514 struct DevBase
*base
)
2516 struct IOSana2Req
*request
, *tail
, *next_request
;
2519 list
= &unit
->request_ports
[EVENT_QUEUE
]->mp_MsgList
;
2520 next_request
= (APTR
)list
->lh_Head
;
2521 tail
= (APTR
)&list
->lh_Tail
;
2524 while(next_request
!= tail
)
2526 request
= next_request
;
2527 next_request
= (APTR
)request
->ios2_Req
.io_Message
.mn_Node
.ln_Succ
;
2529 if((request
->ios2_WireError
& events
) != 0)
2531 request
->ios2_WireError
= events
;
2532 Remove((APTR
)request
);
2533 ReplyMsg((APTR
)request
);
2543 /****i* realtek8180.device/GetDuration *************************************
2546 * GetDuration -- Calculate a duration value.
2549 * GetDuration(unit, length, rate, is_mgmt)
2551 * VOID GetDuration(struct DevUnit *);
2554 * Calculates a duration for a frame of given length when transmitted
2555 * at a given rate. If this is a transmiss
2558 * unit - A unit of this device.
2559 * length - Length of frame whose duration is to be calculated.
2560 * rate - Rate frame will be transmitted at (in Mbps, rounded down).
2561 * is_short - Indicates if frame has a short preamble.
2566 ****************************************************************************
2570 static UWORD
GetDuration(struct DevUnit
*unit
, UWORD length
, UWORD rate
,
2571 BOOL short_preamble
, struct DevBase
*base
)
2573 UWORD duration
, cycles
;
2577 duration
= 97 + (length
* 8 - 1) / rate
;
2578 if (!short_preamble
|| rate
== 1)
2584 duration
= ((length
+ 29) / cycles
+ 1) * 4 + 26;
2592 /****i* realtek8180.device/AckRate *****************************************
2595 * AckRate -- Get the ACK rate corresponding to a data rate.
2598 * ack_rate = AckRate(unit, data_rate)
2600 * UWORD AckRate(struct DevUnit *, UWORD);
2603 * Calculates the rate at which the ACK frame for a data frame with the
2604 * given rate should be transmitted.
2607 * unit - A unit of this device.
2608 * rate - Rate data frame is transmitted at (in Mbps, rounded down).
2611 * ack_rate - The rate for the ACK frame (Mbps, rounded down).
2613 ****************************************************************************
2617 static UWORD
AckRate(struct DevUnit
*unit
, UWORD data_rate
,
2618 struct DevBase
*base
)
2653 /****i* realtek8180.device/UnitTask ****************************************
2664 * Completes deferred requests, and handles card insertion and removal
2665 * in conjunction with the relevant interrupts.
2667 ****************************************************************************
2675 static VOID
UnitTask(struct ExecBase
*sys_base
)
2678 struct IORequest
*request
;
2679 struct DevUnit
*unit
;
2680 struct DevBase
*base
;
2681 struct MsgPort
*general_port
;
2682 ULONG signals
= 0, wait_signals
, card_removed_signal
,
2683 card_inserted_signal
, general_port_signal
;
2685 /* Get parameters */
2687 task
= AbsExecBase
->ThisTask
;
2688 unit
= task
->tc_UserData
;
2689 base
= unit
->device
;
2691 /* Activate general request port */
2693 general_port
= unit
->request_ports
[GENERAL_QUEUE
];
2694 general_port
->mp_SigTask
= task
;
2695 general_port
->mp_SigBit
= AllocSignal(-1);
2696 general_port_signal
= 1 << general_port
->mp_SigBit
;
2697 general_port
->mp_Flags
= PA_SIGNAL
;
2699 /* Allocate signals for notification of card removal and insertion */
2701 card_removed_signal
= unit
->card_removed_signal
= 1 << AllocSignal(-1);
2702 card_inserted_signal
= unit
->card_inserted_signal
= 1 << AllocSignal(-1);
2703 wait_signals
= (1 << general_port
->mp_SigBit
) | card_removed_signal
2704 | card_inserted_signal
| SIGBREAKF_CTRL_C
;
2706 /* Tell ourselves to check port for old messages */
2708 Signal(task
, general_port_signal
);
2710 /* Infinite loop to service requests and signals */
2714 signals
= Wait(wait_signals
);
2716 if((signals
& card_inserted_signal
) != 0)
2718 if(unit
->insertion_function(unit
->card
, base
))
2720 unit
->flags
|= UNITF_HAVEADAPTER
;
2721 if((unit
->flags
& UNITF_CONFIGURED
) != 0)
2722 ConfigureAdapter(unit
, base
);
2723 if((unit
->flags
& UNITF_WASONLINE
) != 0)
2725 GoOnline(unit
, base
);
2726 unit
->flags
&= ~UNITF_WASONLINE
;
2731 if((signals
& card_removed_signal
) != 0)
2733 unit
->removal_function(unit
->card
, base
);
2734 if((unit
->flags
& UNITF_WASONLINE
) != 0)
2735 GoOffline(unit
, base
);
2738 if((signals
& general_port_signal
) != 0)
2740 while((request
= (APTR
)GetMsg(general_port
)) != NULL
)
2742 /* Service the request as soon as the unit is free */
2744 ObtainSemaphore(&unit
->access_lock
);
2745 ServiceRequest((APTR
)request
, base
);