1/*
2	Driver for USB Ethernet Control Model devices
3	Copyright (C) 2008 Michael Lotz <mmlr@mlotz.ch>
4	Distributed under the terms of the MIT license.
5*/
6#ifndef _USB_ECM_DEVICE_H_
7#define _USB_ECM_DEVICE_H_
8
9#include "Driver.h"
10
11class ECMDevice {
12public:
13							ECMDevice(usb_device device);
14							~ECMDevice();
15
16		status_t			InitCheck() { return fStatus; };
17
18		status_t			Open();
19		bool				IsOpen() { return fOpen; };
20
21		status_t			Close();
22		status_t			Free();
23
24		status_t			Read(uint8 *buffer, size_t *numBytes);
25		status_t			Write(const uint8 *buffer, size_t *numBytes);
26		status_t			Control(uint32 op, void *buffer, size_t length);
27
28		void				Removed();
29		bool				IsRemoved() { return fRemoved; };
30
31		status_t			CompareAndReattach(usb_device device);
32
33private:
34static	void				_ReadCallback(void *cookie, int32 status,
35								void *data, size_t actualLength);
36static	void				_WriteCallback(void *cookie, int32 status,
37								void *data, size_t actualLength);
38static	void				_NotifyCallback(void *cookie, int32 status,
39								void *data, size_t actualLength);
40
41		status_t			_SetupDevice();
42		status_t			_ReadMACAddress(usb_device device, uint8 *buffer);
43
44		// state tracking
45		status_t			fStatus;
46		bool				fOpen;
47		bool				fRemoved;
48		int32				fInsideNotify;
49		usb_device			fDevice;
50		uint16				fVendorID;
51		uint16				fProductID;
52
53		// interface and device infos
54		uint8				fControlInterfaceIndex;
55		uint8				fDataInterfaceIndex;
56		uint8				fMACAddressIndex;
57		uint16				fMaxSegmentSize;
58
59		// pipes for notifications and data io
60		usb_pipe			fNotifyEndpoint;
61		usb_pipe			fReadEndpoint;
62		usb_pipe			fWriteEndpoint;
63
64		// data stores for async usb transfers
65		uint32				fActualLengthRead;
66		uint32				fActualLengthWrite;
67		int32				fStatusRead;
68		int32				fStatusWrite;
69		sem_id				fNotifyReadSem;
70		sem_id				fNotifyWriteSem;
71
72		uint8 *				fNotifyBuffer;
73		uint32				fNotifyBufferLength;
74
75		// connection data
76		sem_id				fLinkStateChangeSem;
77		uint8				fMACAddress[6];
78		bool				fHasConnection;
79		uint32				fDownstreamSpeed;
80		uint32				fUpstreamSpeed;
81};
82
83#endif //_USB_ECM_DEVICE_H_
84