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 #ifndef _USB_DEVICE_H_ 9 #define _USB_DEVICE_H_ 10 11 #include "Driver.h" 12 13 class SerialDevice { 14 public: 15 SerialDevice(usb_device device, 16 uint16 vendorID, uint16 productID, 17 const char *description); 18 virtual ~SerialDevice(); 19 20 static SerialDevice * MakeDevice(usb_device device, uint16 vendorID, 21 uint16 productID); 22 23 status_t Init(); 24 25 usb_device Device() { return fDevice; }; 26 uint16 ProductID() { return fProductID; }; 27 uint16 VendorID() { return fVendorID; }; 28 const char * Description() { return fDescription; }; 29 30 void SetControlPipe(usb_pipe handle); 31 usb_pipe ControlPipe() { return fControlPipe; }; 32 33 void SetReadPipe(usb_pipe handle); 34 usb_pipe ReadPipe() { return fReadPipe; }; 35 36 void SetWritePipe(usb_pipe handle); 37 usb_pipe WritePipe() { return fWritePipe; } 38 39 char * ReadBuffer() { return fReadBuffer; }; 40 size_t ReadBufferSize() { return fReadBufferSize; }; 41 42 char * WriteBuffer() { return fWriteBuffer; }; 43 size_t WriteBufferSize() { return fWriteBufferSize; }; 44 45 void SetModes(); 46 bool Service(struct tty *ptty, struct ddrover *ddr, 47 uint flags); 48 49 status_t Open(uint32 flags); 50 status_t Read(char *buffer, size_t *numBytes); 51 status_t Write(const char *buffer, size_t *numBytes); 52 status_t Control(uint32 op, void *arg, size_t length); 53 status_t Select(uint8 event, uint32 ref, selectsync *sync); 54 status_t DeSelect(uint8 event, selectsync *sync); 55 status_t Close(); 56 status_t Free(); 57 58 bool IsOpen() { return fDeviceOpen; }; 59 void Removed(); 60 bool IsRemoved() { return fDeviceRemoved; }; 61 62 /* virtual interface to be overriden as necessary */ 63 virtual status_t AddDevice(const usb_configuration_info *config); 64 65 virtual status_t ResetDevice(); 66 67 virtual status_t SetLineCoding(usb_serial_line_coding *coding); 68 virtual status_t SetControlLineState(uint16 state); 69 70 virtual void OnRead(char **buffer, size_t *numBytes); 71 virtual void OnWrite(const char *buffer, size_t *numBytes); 72 virtual void OnClose(); 73 74 private: 75 static int32 DeviceThread(void *data); 76 77 static void ReadCallbackFunction(void *cookie, 78 int32 status, void *data, 79 uint32 actualLength); 80 static void WriteCallbackFunction(void *cookie, 81 int32 status, void *data, 82 uint32 actualLength); 83 static void InterruptCallbackFunction(void *cookie, 84 int32 status, void *data, 85 uint32 actualLength); 86 87 usb_device fDevice; // USB device handle 88 uint16 fVendorID; 89 uint16 fProductID; 90 const char * fDescription; // informational description 91 bool fDeviceOpen; 92 bool fDeviceRemoved; 93 94 /* communication pipes */ 95 usb_pipe fControlPipe; 96 usb_pipe fReadPipe; 97 usb_pipe fWritePipe; 98 99 /* line coding */ 100 usb_serial_line_coding fLineCoding; 101 102 /* data buffers */ 103 area_id fBufferArea; 104 char * fReadBuffer; 105 size_t fReadBufferSize; 106 char * fWriteBuffer; 107 size_t fWriteBufferSize; 108 char * fInterruptBuffer; 109 size_t fInterruptBufferSize; 110 111 /* variables used in callback functionality */ 112 size_t fActualLengthRead; 113 uint32 fStatusRead; 114 size_t fActualLengthWrite; 115 uint32 fStatusWrite; 116 size_t fActualLengthInterrupt; 117 uint32 fStatusInterrupt; 118 119 /* semaphores used in callbacks */ 120 sem_id fDoneRead; 121 sem_id fDoneWrite; 122 123 uint16 fControlOut; 124 bool fInputStopped; 125 struct ttyfile fTTYFile; 126 struct tty fTTY; 127 128 /* device thread management */ 129 thread_id fDeviceThread; 130 bool fStopDeviceThread; 131 132 /* device locks to ensure no concurent reads/writes */ 133 benaphore fReadLock; 134 benaphore fWriteLock; 135 }; 136 137 #endif // _USB_DEVICE_H_ 138