1/*
2 *	ASIX AX88172/AX88772/AX88178 USB 2.0 Ethernet Driver.
3 *	Copyright (c) 2008, 2011 S.Zharski <imker@gmx.li>
4 *	Distributed under the terms of the MIT license.
5 *
6 *	Heavily based on code of the
7 *	Driver for USB Ethernet Control Model devices
8 *	Copyright (C) 2008 Michael Lotz <mmlr@mlotz.ch>
9 *	Distributed under the terms of the MIT license.
10 *
11 */
12#ifndef _USB_ASIX_DEVICE_H_
13#define _USB_ASIX_DEVICE_H_
14
15
16#include <ether_driver.h>
17#include <util/Vector.h>
18
19#include "Driver.h"
20#include "MIIBus.h"
21
22
23struct DeviceInfo {
24	uint16	fIds[2];
25
26	enum Type {
27		AX88172 = 0,
28		AX88772 = 1,
29		AX88178 = 2,
30		AX88772A = 3,
31		AX88772B = 4
32	}			fType;
33
34	const char* fName;
35
36	inline uint16		VendorId()	{ return fIds[0]; }
37	inline uint16		ProductId()	{ return fIds[1]; }
38	inline uint32		Key()		{ return fIds[0] << 16 | fIds[1]; }
39};
40
41
42class ASIXDevice {
43public:
44							ASIXDevice(usb_device device, DeviceInfo& devInfo);
45		virtual				~ASIXDevice();
46
47		status_t			InitCheck() { return fStatus; };
48
49		status_t			Open(uint32 flags);
50		bool				IsOpen() { return fOpen; };
51
52		status_t			Close();
53		status_t			Free();
54
55		status_t			Read(uint8 *buffer, size_t *numBytes);
56		status_t			Write(const uint8 *buffer, size_t *numBytes);
57		status_t			Control(uint32 op, void *buffer, size_t length);
58
59		void				Removed();
60		bool				IsRemoved() { return fRemoved; };
61
62		status_t			CompareAndReattach(usb_device device);
63virtual	status_t			SetupDevice(bool deviceReplugged);
64
65private:
66static	void				_ReadCallback(void *cookie, int32 status,
67								void *data, size_t actualLength);
68static	void				_WriteCallback(void *cookie, int32 status,
69								void *data, size_t actualLength);
70static	void				_NotifyCallback(void *cookie, int32 status,
71								void *data, size_t actualLength);
72
73		status_t			_SetupEndpoints();
74
75protected:
76		// overrides
77virtual	status_t			StartDevice() = 0;
78virtual	status_t			StopDevice();
79virtual status_t			OnNotify(uint32 actualLength) = 0;
80virtual	status_t			GetLinkState(ether_link_state *state) = 0;
81virtual	status_t			SetPromiscuousMode(bool bOn);
82		uint32				EthernetCRC32(const uint8* buffer, size_t length);
83virtual	status_t			ModifyMulticastTable(bool add,
84								ether_address_t* group);
85virtual	status_t			ReadMACAddress(ether_address_t *address);
86		status_t			ReadRXControlRegister(uint16 *rxcontrol);
87		status_t			WriteRXControlRegister(uint16 rxcontrol);
88
89		// device info
90		usb_device			fDevice;
91		DeviceInfo			fDeviceInfo;
92		ether_address_t		fMACAddress;
93
94		// state tracking
95		status_t			fStatus;
96		bool				fOpen;
97		bool				fRemoved;
98		bool				fHasConnection;
99		bool				fNonBlocking;
100		int32				fInsideNotify;
101
102		// interface and device infos
103		uint16				fFrameSize;
104
105		// pipes for notifications and data io
106		usb_pipe			fNotifyEndpoint;
107		usb_pipe			fReadEndpoint;
108		usb_pipe			fWriteEndpoint;
109
110		// data stores for async usb transfers
111		uint32				fActualLengthRead;
112		uint32				fActualLengthWrite;
113		int32				fStatusRead;
114		int32				fStatusWrite;
115		sem_id				fNotifyReadSem;
116		sem_id				fNotifyWriteSem;
117
118		uint8 *				fNotifyBuffer;
119		uint32				fNotifyBufferLength;
120		sem_id				fLinkStateChangeSem;
121
122		// MII bus handler
123		MIIBus				fMII;
124
125		// connection data
126		bool				fUseTRXHeader;
127		uint8				fIPG[3];
128		uint8				fReadNodeIDRequest;
129		Vector<uint32>		fMulticastHashes;
130};
131
132#endif // _USB_ASIX_DEVICE_H_
133