Added two wireless drivers: atheros5000.device and realtek8180.device.
[AROS.git] / workbench / devs / networks / atheros5000 / device.c
blobcbfcf298bc6fe38fe009d9a28cf4a26716344a81
1 /*
3 Copyright (C) 2000-2011 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,
18 MA 02111-1307, USA.
23 #include <exec/types.h>
24 #include <exec/resident.h>
25 #include <exec/errors.h>
26 #include <dos/dos.h>
27 #include <utility/utility.h>
28 #include "initializers.h"
30 #include <proto/exec.h>
31 #include <clib/alib_protos.h>
32 #include <proto/utility.h>
34 #include "device.h"
36 #include "device_protos.h"
37 #include "pci_protos.h"
38 #include "request_protos.h"
41 /* Private prototypes */
43 static VOID DeleteDevice(struct DevBase *base);
46 /* Return an error immediately if someone tries to run the device */
48 LONG Main()
50 return -1;
54 const TEXT device_name[] = DEVICE_NAME;
55 const TEXT version_string[] =
56 DEVICE_NAME " " STR(VERSION) "." STR(REVISION) " (" DATE ")\n";
57 const TEXT utility_name[] = UTILITYNAME;
58 static const TEXT prometheus_name[] = "prometheus.library";
59 const TEXT dos_name[] = DOSNAME;
60 const TEXT timer_name[] = TIMERNAME;
63 static const APTR vectors[] =
65 (APTR)DevOpen,
66 (APTR)DevClose,
67 (APTR)DevExpunge,
68 (APTR)DevReserved,
69 (APTR)DevBeginIO,
70 (APTR)DevAbortIO,
71 (APTR)-1
75 #ifdef __MORPHOS__
76 #pragma pack(2)
77 #endif
78 const struct
80 SMALLINITBYTEDEF(type);
81 SMALLINITPINTDEF(name);
82 SMALLINITBYTEDEF(flags);
83 SMALLINITWORDDEF(version);
84 SMALLINITWORDDEF(revision);
85 SMALLINITPINTDEF(id_string);
86 INITENDDEF;
88 init_data =
90 SMALLINITBYTE(OFFSET(Node, ln_Type), NT_DEVICE),
91 SMALLINITPINT(OFFSET(Node, ln_Name), device_name),
92 SMALLINITBYTE(OFFSET(Library, lib_Flags), LIBF_SUMUSED | LIBF_CHANGED),
93 SMALLINITWORD(OFFSET(Library, lib_Version), VERSION),
94 SMALLINITWORD(OFFSET(Library, lib_Revision), REVISION),
95 SMALLINITPINT(OFFSET(Library, lib_IdString), version_string),
96 INITEND
98 #ifdef __MORPHOS__
99 #pragma pack()
100 #endif
103 static const APTR init_table[] =
105 (APTR)sizeof(struct DevBase),
106 (APTR)vectors,
107 (APTR)&init_data,
108 (APTR)DevInit
112 const struct Resident rom_tag =
114 RTC_MATCHWORD,
115 (struct Resident *)&rom_tag,
116 (APTR)(&rom_tag + 1),
117 RTF_AUTOINIT,
118 VERSION,
119 NT_DEVICE,
121 (STRPTR)device_name,
122 (STRPTR)version_string,
123 (APTR)init_table
127 static const ULONG rx_tags[] =
129 S2_CopyToBuff,
130 S2_CopyToBuff16,
134 static const ULONG tx_tags[] =
136 S2_CopyFromBuff,
137 S2_CopyFromBuff16,
138 S2_CopyFromBuff32
143 /****i* atheros5000.device/DevInit *****************************************
145 * NAME
146 * DevInit
148 * SYNOPSIS
149 * dev_base = DevInit(dev_base, seg_list)
151 * struct DevBase *DevInit(struct DevBase *, APTR);
153 ****************************************************************************
157 struct DevBase *DevInit(REG(d0, struct DevBase *dev_base),
158 REG(a0, APTR seg_list), REG(BASE_REG, struct DevBase *base))
160 BOOL success = TRUE;
162 /* Initialise base structure */
164 dev_base->sys_base = (APTR)base;
165 base = dev_base;
166 hal_dev_base = dev_base;
167 base->seg_list = seg_list;
168 NewList((APTR)(&base->pci_units));
170 /* Open libraries, resources and devices */
172 base->utility_base = (APTR)OpenLibrary(utility_name, UTILITY_VERSION);
173 base->prometheus_base = OpenLibrary(prometheus_name, PROMETHEUS_VERSION);
174 base->dos_base = (APTR)OpenLibrary(dos_name, DOS_VERSION);
176 if(base->utility_base == NULL || base->dos_base == NULL)
177 success = FALSE;
179 if(OpenDevice(timer_name, UNIT_VBLANK, (APTR)&base->timer_request, 0)
180 != 0)
181 success = FALSE;
183 if(!success)
185 DeleteDevice(base);
186 base = NULL;
189 return base;
194 /****i* atheros5000.device/DevOpen *****************************************
196 * NAME
197 * DevOpen
199 * SYNOPSIS
200 * error = DevOpen(request, unit_num, flags)
202 * BYTE DevOpen(struct IOSana2Req *, ULONG, ULONG);
204 ****************************************************************************
208 BYTE DevOpen(REG(a1, struct IOSana2Req *request),
209 REG(d0, ULONG unit_num), REG(d1, ULONG flags),
210 REG(BASE_REG, struct DevBase *base))
212 struct DevUnit *unit;
213 BYTE error = 0;
214 struct Opener *opener;
215 struct TagItem *tag_list;
216 UWORD i;
218 base->device.dd_Library.lib_OpenCnt++;
219 base->device.dd_Library.lib_Flags &= ~LIBF_DELEXP;
221 request->ios2_Req.io_Unit = NULL;
222 tag_list = request->ios2_BufferManagement;
223 request->ios2_BufferManagement = NULL;
225 /* Check request size */
227 if(request->ios2_Req.io_Message.mn_Length < sizeof(struct IOSana2Req))
228 error = IOERR_OPENFAIL;
230 /* Get the requested unit */
232 if(error == 0)
234 request->ios2_Req.io_Unit = (APTR)(unit = GetUnit(unit_num, base));
235 if(unit == NULL)
236 error = IOERR_OPENFAIL;
239 /* Handle device sharing */
241 if(error == 0)
243 if(unit->open_count != 0 && ((unit->flags & UNITF_SHARED) == 0
244 || (flags & SANA2OPF_MINE) != 0))
245 error = IOERR_UNITBUSY;
246 unit->open_count++;
249 if(error == 0)
251 if((flags & SANA2OPF_MINE) == 0)
252 unit->flags |= UNITF_SHARED;
253 else if((flags & SANA2OPF_PROM) != 0)
254 unit->flags |= UNITF_PROM;
256 /* Set up buffer-management structure and get hooks */
258 request->ios2_BufferManagement = opener =
259 AllocVec(sizeof(struct Opener), MEMF_PUBLIC | MEMF_CLEAR);
260 if(opener == NULL)
261 error = IOERR_OPENFAIL;
264 if(error == 0)
266 NewList(&opener->read_port.mp_MsgList);
267 opener->read_port.mp_Flags = PA_IGNORE;
268 NewList(&opener->mgmt_port.mp_MsgList);
269 opener->mgmt_port.mp_Flags = PA_IGNORE;
270 NewList((APTR)&opener->initial_stats);
272 for(i = 0; i < 2; i++)
273 opener->rx_function = (APTR)GetTagData(rx_tags[i],
274 (UPINT)opener->rx_function, tag_list);
275 for(i = 0; i < 3; i++)
276 opener->tx_function = (APTR)GetTagData(tx_tags[i],
277 (UPINT)opener->tx_function, tag_list);
279 opener->filter_hook = (APTR)GetTagData(S2_PacketFilter, (UPINT)NULL,
280 tag_list);
281 opener->dma_tx_function =
282 (APTR)GetTagData(S2_DMACopyFromBuff32, (UPINT)NULL, tag_list);
284 Disable();
285 AddTail((APTR)&unit->openers, (APTR)opener);
286 Enable();
289 /* Back out if anything went wrong */
291 if(error != 0)
292 CloseUnit(request, base);
294 /* Return */
296 request->ios2_Req.io_Error = error;
297 return error;
302 /****i* atheros5000.device/DevClose ****************************************
304 * NAME
305 * DevClose
307 * SYNOPSIS
308 * seg_list = DevClose(request)
310 * APTR DevClose(struct IOSana2Req *);
312 ****************************************************************************
316 APTR DevClose(REG(a1, struct IOSana2Req *request),
317 REG(BASE_REG, struct DevBase *base))
319 APTR seg_list = NULL;
321 /* Close the unit */
323 CloseUnit(request, base);
325 /* Expunge the device if a delayed expunge is pending */
327 if(base->device.dd_Library.lib_OpenCnt == 0)
329 if((base->device.dd_Library.lib_Flags & LIBF_DELEXP) != 0)
330 seg_list = DevExpunge(base);
333 return seg_list;
338 /****i* atheros5000.device/DevExpunge **************************************
340 * NAME
341 * DevExpunge
343 * SYNOPSIS
344 * seg_list = DevExpunge()
346 * APTR DevExpunge(VOID);
348 ****************************************************************************
352 APTR DevExpunge(REG(BASE_REG, struct DevBase *base))
354 APTR seg_list;
356 if(base->device.dd_Library.lib_OpenCnt == 0)
358 seg_list = base->seg_list;
359 Remove((APTR)base);
360 DeleteDevice(base);
362 else
364 base->device.dd_Library.lib_Flags |= LIBF_DELEXP;
365 seg_list = NULL;
368 return seg_list;
373 /****i* atheros5000.device/DevReserved *************************************
375 * NAME
376 * DevReserved
378 * SYNOPSIS
379 * result = DevReserved()
381 * APTR DevReserved(VOID);
383 ****************************************************************************
387 APTR DevReserved()
389 return NULL;
394 /****i* atheros5000.device/DevBeginIO **************************************
396 * NAME
397 * DevBeginIO
399 * SYNOPSIS
400 * DevBeginIO(request)
402 * VOID DevBeginIO(struct IORequest *);
404 ****************************************************************************
408 VOID DevBeginIO(REG(a1, struct IOSana2Req *request),
409 REG(BASE_REG, struct DevBase *base))
411 struct DevUnit *unit;
413 request->ios2_Req.io_Error = 0;
414 unit = (APTR)request->ios2_Req.io_Unit;
416 if(AttemptSemaphore(&unit->access_lock))
417 ServiceRequest(request, base);
418 else
419 PutRequest(unit->request_ports[GENERAL_QUEUE], (APTR)request, base);
421 return;
426 /****i* atheros5000.device/DevAbortIO **************************************
428 * NAME
429 * DevAbortIO -- Try to stop a request.
431 * SYNOPSIS
432 * DevAbortIO(request)
434 * VOID DevAbortIO(struct IOSana2Req *);
436 * FUNCTION
437 * Do our best to halt the progress of a request.
439 ****************************************************************************
443 VOID DevAbortIO(REG(a1, struct IOSana2Req *request),
444 REG(BASE_REG, struct DevBase *base))
446 struct DevUnit *unit;
448 unit = (APTR)request->ios2_Req.io_Unit;
450 Disable();
451 if(request->ios2_Req.io_Message.mn_Node.ln_Type == NT_MESSAGE &&
452 (request->ios2_Req.io_Flags & IOF_QUICK) == 0)
454 Remove((APTR)request);
455 request->ios2_Req.io_Error = IOERR_ABORTED;
456 request->ios2_WireError = S2WERR_GENERIC_ERROR;
457 ReplyMsg((APTR)request);
459 Enable();
461 return;
466 /****i* atheros5000.device/DeleteDevice ************************************
468 * NAME
469 * DeleteDevice
471 * SYNOPSIS
472 * DeleteDevice()
474 * VOID DeleteDevice(VOID);
476 ****************************************************************************
480 VOID DeleteDevice(struct DevBase *base)
482 UWORD neg_size, pos_size;
484 /* Close devices */
486 CloseDevice((APTR)&base->timer_request);
488 /* Close libraries */
490 if(base->dos_base != NULL)
491 CloseLibrary((APTR)base->dos_base);
492 if(base->openpci_base != NULL)
493 CloseLibrary(base->openpci_base);
494 if(base->prometheus_base != NULL)
495 CloseLibrary(base->prometheus_base);
496 if(base->utility_base != NULL)
497 CloseLibrary((APTR)base->utility_base);
499 /* Free device's memory */
501 neg_size = base->device.dd_Library.lib_NegSize;
502 pos_size = base->device.dd_Library.lib_PosSize;
503 FreeMem((UBYTE *)base - neg_size, pos_size + neg_size);
505 return;
510 /****i* atheros5000.device/CloseUnit ***************************************
512 * NAME
513 * CloseUnit
515 * SYNOPSIS
516 * CloseUnit(request)
518 * VOID CloseUnit(struct IOSana2Req *);
520 ****************************************************************************
524 VOID CloseUnit(struct IOSana2Req *request, struct DevBase *base)
526 struct DevUnit *unit;
527 struct Opener *opener;
529 /* Decrement device usage count and free buffer-management resources */
531 base->device.dd_Library.lib_OpenCnt--;
532 opener = (APTR)request->ios2_BufferManagement;
533 if(opener != NULL)
535 Disable();
536 Remove((APTR)opener);
537 Enable();
538 FreeVec(opener);
541 /* Delete the unit if it's no longer in use */
543 unit = (APTR)request->ios2_Req.io_Unit;
544 if(unit != NULL)
546 if((--unit->open_count) == 0)
548 Remove((APTR)unit);
549 switch(unit->bus)
551 case PCI_BUS:
552 DeletePCIUnit(unit, base);
553 break;
558 return;
563 /****i* atheros5000.device/GetUnit *****************************************
565 * NAME
566 * GetUnit -- Get a unit by number.
568 * SYNOPSIS
569 * unit = GetUnit(unit_num)
571 * struct DevUnit *GetUnit(ULONG);
573 ****************************************************************************
577 struct DevUnit *GetUnit(ULONG unit_num, struct DevBase *base)
579 struct DevUnit *unit;
580 ULONG pci_limit;
582 pci_limit = GetPCICount(base);
584 if(unit_num < pci_limit)
585 unit = GetPCIUnit(unit_num, base);
586 else
587 unit = NULL;
589 return unit;
594 /****i* atheros5000.device/WrapInt *****************************************
596 * NAME
597 * WrapInt
599 ****************************************************************************
603 BOOL WrapInt(struct Interrupt *interrupt, struct DevBase *base)
605 BOOL success = TRUE;
606 APTR *int_data;
608 if(base->wrapper_int_code != NULL)
610 int_data = AllocMem(2 * sizeof(APTR), MEMF_PUBLIC | MEMF_CLEAR);
611 if(int_data != NULL)
613 int_data[0] = interrupt->is_Code;
614 int_data[1] = interrupt->is_Data;
615 interrupt->is_Code = base->wrapper_int_code;
616 interrupt->is_Data = int_data;
618 else
619 success = FALSE;
622 return success;
627 /****i* atheros5000.device/UnwrapInt ***************************************
629 * NAME
630 * UnwrapInt
632 ****************************************************************************
636 VOID UnwrapInt(struct Interrupt *interrupt, struct DevBase *base)
638 if(interrupt->is_Code == base->wrapper_int_code)
639 FreeMem(interrupt->is_Data, 2 * sizeof(APTR));
641 return;