vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / kernel / drivers / network / usb_asix / ASIXDevice.h
blob5aa13faf9383e7e333bc4139811e389a571f9719
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.
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.
12 #ifndef _USB_ASIX_DEVICE_H_
13 #define _USB_ASIX_DEVICE_H_
16 #include <ether_driver.h>
17 #include <util/Vector.h>
19 #include "Driver.h"
20 #include "MIIBus.h"
23 struct DeviceInfo {
24 uint16 fIds[2];
26 enum Type {
27 AX88172 = 0,
28 AX88772 = 1,
29 AX88178 = 2,
30 AX88772A = 3,
31 AX88772B = 4
32 } fType;
34 const char* fName;
36 inline uint16 VendorId() { return fIds[0]; }
37 inline uint16 ProductId() { return fIds[1]; }
38 inline uint32 Key() { return fIds[0] << 16 | fIds[1]; }
42 class ASIXDevice {
43 public:
44 ASIXDevice(usb_device device, DeviceInfo& devInfo);
45 virtual ~ASIXDevice();
47 status_t InitCheck() { return fStatus; };
49 status_t Open(uint32 flags);
50 bool IsOpen() { return fOpen; };
52 status_t Close();
53 status_t Free();
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);
59 void Removed();
60 bool IsRemoved() { return fRemoved; };
62 status_t CompareAndReattach(usb_device device);
63 virtual status_t SetupDevice(bool deviceReplugged);
65 private:
66 static void _ReadCallback(void *cookie, int32 status,
67 void *data, size_t actualLength);
68 static void _WriteCallback(void *cookie, int32 status,
69 void *data, size_t actualLength);
70 static void _NotifyCallback(void *cookie, int32 status,
71 void *data, size_t actualLength);
73 status_t _SetupEndpoints();
75 protected:
76 // overrides
77 virtual status_t StartDevice() = 0;
78 virtual status_t StopDevice();
79 virtual status_t OnNotify(uint32 actualLength) = 0;
80 virtual status_t GetLinkState(ether_link_state *state) = 0;
81 virtual status_t SetPromiscuousMode(bool bOn);
82 uint32 EthernetCRC32(const uint8* buffer, size_t length);
83 virtual status_t ModifyMulticastTable(bool add,
84 ether_address_t* group);
85 virtual status_t ReadMACAddress(ether_address_t *address);
86 status_t ReadRXControlRegister(uint16 *rxcontrol);
87 status_t WriteRXControlRegister(uint16 rxcontrol);
89 // device info
90 usb_device fDevice;
91 DeviceInfo fDeviceInfo;
92 ether_address_t fMACAddress;
94 // state tracking
95 status_t fStatus;
96 bool fOpen;
97 bool fRemoved;
98 bool fHasConnection;
99 bool fNonBlocking;
100 int32 fInsideNotify;
102 // interface and device infos
103 uint16 fFrameSize;
105 // pipes for notifications and data io
106 usb_pipe fNotifyEndpoint;
107 usb_pipe fReadEndpoint;
108 usb_pipe fWriteEndpoint;
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;
118 uint8 * fNotifyBuffer;
119 uint32 fNotifyBufferLength;
120 sem_id fLinkStateChangeSem;
122 // MII bus handler
123 MIIBus fMII;
125 // connection data
126 bool fUseTRXHeader;
127 uint8 fIPG[3];
128 uint8 fReadNodeIDRequest;
129 Vector<uint32> fMulticastHashes;
132 #endif // _USB_ASIX_DEVICE_H_