1 /* 2 * Copyright (c) 2007-2008 by Michael Lotz 3 * Heavily based on the original usb_serial driver which is: 4 * 5 * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li> 6 * Distributed under the terms of the MIT License. 7 * 8 * Authors: 9 * Alexander von Gluck IV, kallisti5@unixzen.com 10 */ 11 #ifndef _USB_DEVICE_H_ 12 #define _USB_DEVICE_H_ 13 14 15 #include "Driver.h" 16 17 18 struct usb_serial_device { 19 uint32 vendorID; 20 uint32 productID; 21 const char* deviceName; 22 }; 23 24 25 class SerialDevice { 26 public: 27 SerialDevice(usb_device device, 28 uint16 vendorID, uint16 productID, 29 const char *description); 30 virtual ~SerialDevice(); 31 32 static SerialDevice * MakeDevice(usb_device device, uint16 vendorID, 33 uint16 productID); 34 35 status_t Init(); 36 37 usb_device Device() { return fDevice; }; 38 uint16 ProductID() { return fProductID; }; 39 uint16 VendorID() { return fVendorID; }; 40 const char * Description() { return fDescription; }; 41 42 void SetControlPipe(usb_pipe handle); 43 usb_pipe ControlPipe() { return fControlPipe; }; 44 45 void SetReadPipe(usb_pipe handle); 46 usb_pipe ReadPipe() { return fReadPipe; }; 47 48 void SetWritePipe(usb_pipe handle); 49 usb_pipe WritePipe() { return fWritePipe; } 50 51 char * ReadBuffer() { return fReadBuffer; }; 52 size_t ReadBufferSize() { return fReadBufferSize; }; 53 54 char * WriteBuffer() { return fWriteBuffer; }; 55 size_t WriteBufferSize() { return fWriteBufferSize; }; 56 57 void SetModes(struct termios *tios); 58 bool Service(struct tty *tty, uint32 op, 59 void *buffer, size_t length); 60 61 status_t Open(uint32 flags); 62 status_t Read(char *buffer, size_t *numBytes); 63 status_t Write(const char *buffer, size_t *numBytes); 64 status_t Control(uint32 op, void *arg, size_t length); 65 status_t Select(uint8 event, uint32 ref, selectsync *sync); 66 status_t DeSelect(uint8 event, selectsync *sync); 67 status_t Close(); 68 status_t Free(); 69 70 bool IsOpen() { return fDeviceOpen; }; 71 void Removed(); 72 bool IsRemoved() { return fDeviceRemoved; }; 73 74 /* virtual interface to be overriden as necessary */ 75 virtual status_t AddDevice(const usb_configuration_info *config); 76 77 virtual status_t ResetDevice(); 78 79 virtual status_t SetLineCoding(usb_cdc_line_coding *coding); 80 virtual status_t SetControlLineState(uint16 state); 81 virtual status_t SetHardwareFlowControl(bool enable); 82 83 virtual void OnRead(char **buffer, size_t *numBytes); 84 virtual void OnWrite(const char *buffer, size_t *numBytes, 85 size_t *packetBytes); 86 virtual void OnClose(); 87 88 protected: 89 void SetReadBufferSize(size_t size) { fReadBufferSize = size; }; 90 void SetWriteBufferSize(size_t size) { fWriteBufferSize = size; }; 91 void SetInterruptBufferSize(size_t size) { fInterruptBufferSize = size; }; 92 93 private: 94 static int32 _InputThread(void *data); 95 status_t _WriteToDevice(); 96 97 static void _ReadCallbackFunction(void *cookie, 98 int32 status, void *data, 99 size_t actualLength); 100 static void _WriteCallbackFunction(void *cookie, 101 int32 status, void *data, 102 size_t actualLength); 103 static void _InterruptCallbackFunction(void *cookie, 104 int32 status, void *data, 105 size_t actualLength); 106 107 usb_device fDevice; // USB device handle 108 uint16 fVendorID; 109 uint16 fProductID; 110 const char * fDescription; // informational description 111 bool fDeviceOpen; 112 bool fDeviceRemoved; 113 114 /* communication pipes */ 115 usb_pipe fControlPipe; 116 usb_pipe fReadPipe; 117 usb_pipe fWritePipe; 118 119 /* line coding */ 120 usb_cdc_line_coding fLineCoding; 121 122 /* data buffers */ 123 area_id fBufferArea; 124 char * fReadBuffer; 125 size_t fReadBufferSize; 126 char * fOutputBuffer; 127 size_t fOutputBufferSize; 128 char * fWriteBuffer; 129 size_t fWriteBufferSize; 130 char * fInterruptBuffer; 131 size_t fInterruptBufferSize; 132 133 /* variables used in callback functionality */ 134 size_t fActualLengthRead; 135 status_t fStatusRead; 136 size_t fActualLengthWrite; 137 status_t fStatusWrite; 138 size_t fActualLengthInterrupt; 139 status_t fStatusInterrupt; 140 141 /* semaphores used in callbacks */ 142 sem_id fDoneRead; 143 sem_id fDoneWrite; 144 145 uint16 fControlOut; 146 bool fInputStopped; 147 struct tty * fMasterTTY; 148 struct tty * fSlaveTTY; 149 struct tty_cookie * fSystemTTYCookie; 150 struct tty_cookie * fDeviceTTYCookie; 151 struct termios fTTYConfig; 152 153 /* input thread management */ 154 thread_id fInputThread; 155 bool fStopThreads; 156 }; 157 158 #endif // _USB_DEVICE_H_ 159