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 82 virtual void OnRead(char **buffer, size_t *numBytes); 83 virtual void OnWrite(const char *buffer, size_t *numBytes, 84 size_t *packetBytes); 85 virtual void OnClose(); 86 87 protected: 88 void SetReadBufferSize(size_t size) { fReadBufferSize = size; }; 89 void SetWriteBufferSize(size_t size) { fWriteBufferSize = size; }; 90 void SetInterruptBufferSize(size_t size) { fInterruptBufferSize = size; }; 91 92 private: 93 static int32 _InputThread(void *data); 94 status_t _WriteToDevice(); 95 96 static void _ReadCallbackFunction(void *cookie, 97 int32 status, void *data, 98 size_t actualLength); 99 static void _WriteCallbackFunction(void *cookie, 100 int32 status, void *data, 101 size_t actualLength); 102 static void _InterruptCallbackFunction(void *cookie, 103 int32 status, void *data, 104 size_t actualLength); 105 106 usb_device fDevice; // USB device handle 107 uint16 fVendorID; 108 uint16 fProductID; 109 const char * fDescription; // informational description 110 bool fDeviceOpen; 111 bool fDeviceRemoved; 112 113 /* communication pipes */ 114 usb_pipe fControlPipe; 115 usb_pipe fReadPipe; 116 usb_pipe fWritePipe; 117 118 /* line coding */ 119 usb_cdc_line_coding fLineCoding; 120 121 /* data buffers */ 122 area_id fBufferArea; 123 char * fReadBuffer; 124 size_t fReadBufferSize; 125 char * fOutputBuffer; 126 size_t fOutputBufferSize; 127 char * fWriteBuffer; 128 size_t fWriteBufferSize; 129 char * fInterruptBuffer; 130 size_t fInterruptBufferSize; 131 132 /* variables used in callback functionality */ 133 size_t fActualLengthRead; 134 status_t fStatusRead; 135 size_t fActualLengthWrite; 136 status_t fStatusWrite; 137 size_t fActualLengthInterrupt; 138 status_t fStatusInterrupt; 139 140 /* semaphores used in callbacks */ 141 sem_id fDoneRead; 142 sem_id fDoneWrite; 143 144 uint16 fControlOut; 145 bool fInputStopped; 146 struct tty * fMasterTTY; 147 struct tty * fSlaveTTY; 148 struct tty_cookie * fSystemTTYCookie; 149 struct tty_cookie * fDeviceTTYCookie; 150 struct termios fTTYConfig; 151 152 /* input thread management */ 153 thread_id fInputThread; 154 bool fStopThreads; 155 }; 156 157 #endif // _USB_DEVICE_H_ 158