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,
23 #include <exec/types.h>
24 #include <exec/resident.h>
25 #include <exec/errors.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>
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 */
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
[] =
80 SMALLINITBYTEDEF(type
);
81 SMALLINITPINTDEF(name
);
82 SMALLINITBYTEDEF(flags
);
83 SMALLINITWORDDEF(version
);
84 SMALLINITWORDDEF(revision
);
85 SMALLINITPINTDEF(id_string
);
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
),
103 static const APTR init_table
[] =
105 (APTR
)sizeof(struct DevBase
),
112 const struct Resident rom_tag
=
115 (struct Resident
*)&rom_tag
,
116 (APTR
)(&rom_tag
+ 1),
122 (STRPTR
)version_string
,
127 static const ULONG rx_tags
[] =
134 static const ULONG tx_tags
[] =
143 /****i* atheros5000.device/DevInit *****************************************
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
))
162 /* Initialise base structure */
164 dev_base
->sys_base
= (APTR
)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
)
179 if(OpenDevice(timer_name
, UNIT_VBLANK
, (APTR
)&base
->timer_request
, 0)
194 /****i* atheros5000.device/DevOpen *****************************************
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
;
214 struct Opener
*opener
;
215 struct TagItem
*tag_list
;
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 */
234 request
->ios2_Req
.io_Unit
= (APTR
)(unit
= GetUnit(unit_num
, base
));
236 error
= IOERR_OPENFAIL
;
239 /* Handle device sharing */
243 if(unit
->open_count
!= 0 && ((unit
->flags
& UNITF_SHARED
) == 0
244 || (flags
& SANA2OPF_MINE
) != 0))
245 error
= IOERR_UNITBUSY
;
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
);
261 error
= IOERR_OPENFAIL
;
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
,
281 opener
->dma_tx_function
=
282 (APTR
)GetTagData(S2_DMACopyFromBuff32
, (UPINT
)NULL
, tag_list
);
285 AddTail((APTR
)&unit
->openers
, (APTR
)opener
);
289 /* Back out if anything went wrong */
292 CloseUnit(request
, base
);
296 request
->ios2_Req
.io_Error
= error
;
302 /****i* atheros5000.device/DevClose ****************************************
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
;
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
);
338 /****i* atheros5000.device/DevExpunge **************************************
344 * seg_list = DevExpunge()
346 * APTR DevExpunge(VOID);
348 ****************************************************************************
352 APTR
DevExpunge(REG(BASE_REG
, struct DevBase
*base
))
356 if(base
->device
.dd_Library
.lib_OpenCnt
== 0)
358 seg_list
= base
->seg_list
;
364 base
->device
.dd_Library
.lib_Flags
|= LIBF_DELEXP
;
373 /****i* atheros5000.device/DevReserved *************************************
379 * result = DevReserved()
381 * APTR DevReserved(VOID);
383 ****************************************************************************
394 /****i* atheros5000.device/DevBeginIO **************************************
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
);
419 PutRequest(unit
->request_ports
[GENERAL_QUEUE
], (APTR
)request
, base
);
426 /****i* atheros5000.device/DevAbortIO **************************************
429 * DevAbortIO -- Try to stop a request.
432 * DevAbortIO(request)
434 * VOID DevAbortIO(struct IOSana2Req *);
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
))
447 if(request
->ios2_Req
.io_Message
.mn_Node
.ln_Type
== NT_MESSAGE
&&
448 (request
->ios2_Req
.io_Flags
& IOF_QUICK
) == 0)
450 Remove((APTR
)request
);
451 request
->ios2_Req
.io_Error
= IOERR_ABORTED
;
452 request
->ios2_WireError
= S2WERR_GENERIC_ERROR
;
453 ReplyMsg((APTR
)request
);
462 /****i* atheros5000.device/DeleteDevice ************************************
470 * VOID DeleteDevice(VOID);
472 ****************************************************************************
476 VOID
DeleteDevice(struct DevBase
*base
)
478 UWORD neg_size
, pos_size
;
482 CloseDevice((APTR
)&base
->timer_request
);
484 /* Close libraries */
486 if(base
->dos_base
!= NULL
)
487 CloseLibrary((APTR
)base
->dos_base
);
488 if(base
->openpci_base
!= NULL
)
489 CloseLibrary(base
->openpci_base
);
490 if(base
->prometheus_base
!= NULL
)
491 CloseLibrary(base
->prometheus_base
);
492 if(base
->utility_base
!= NULL
)
493 CloseLibrary((APTR
)base
->utility_base
);
495 /* Free device's memory */
497 neg_size
= base
->device
.dd_Library
.lib_NegSize
;
498 pos_size
= base
->device
.dd_Library
.lib_PosSize
;
499 FreeMem((UBYTE
*)base
- neg_size
, pos_size
+ neg_size
);
506 /****i* atheros5000.device/CloseUnit ***************************************
514 * VOID CloseUnit(struct IOSana2Req *);
516 ****************************************************************************
520 VOID
CloseUnit(struct IOSana2Req
*request
, struct DevBase
*base
)
522 struct DevUnit
*unit
;
523 struct Opener
*opener
;
525 /* Decrement device usage count and free buffer-management resources */
527 base
->device
.dd_Library
.lib_OpenCnt
--;
528 opener
= (APTR
)request
->ios2_BufferManagement
;
532 Remove((APTR
)opener
);
537 /* Delete the unit if it's no longer in use */
539 unit
= (APTR
)request
->ios2_Req
.io_Unit
;
542 if((--unit
->open_count
) == 0)
548 DeletePCIUnit(unit
, base
);
559 /****i* atheros5000.device/GetUnit *****************************************
562 * GetUnit -- Get a unit by number.
565 * unit = GetUnit(unit_num)
567 * struct DevUnit *GetUnit(ULONG);
569 ****************************************************************************
573 struct DevUnit
*GetUnit(ULONG unit_num
, struct DevBase
*base
)
575 struct DevUnit
*unit
;
578 pci_limit
= GetPCICount(base
);
580 if(unit_num
< pci_limit
)
581 unit
= GetPCIUnit(unit_num
, base
);
590 /****i* atheros5000.device/WrapInt *****************************************
595 ****************************************************************************
599 BOOL
WrapInt(struct Interrupt
*interrupt
, struct DevBase
*base
)
604 if(base
->wrapper_int_code
!= NULL
)
606 int_data
= AllocMem(2 * sizeof(APTR
), MEMF_PUBLIC
| MEMF_CLEAR
);
609 int_data
[0] = interrupt
->is_Code
;
610 int_data
[1] = interrupt
->is_Data
;
611 interrupt
->is_Code
= base
->wrapper_int_code
;
612 interrupt
->is_Data
= int_data
;
623 /****i* atheros5000.device/UnwrapInt ***************************************
628 ****************************************************************************
632 VOID
UnwrapInt(struct Interrupt
*interrupt
, struct DevBase
*base
)
634 if(interrupt
->is_Code
== base
->wrapper_int_code
)
635 FreeMem(interrupt
->is_Data
, 2 * sizeof(APTR
));