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 /* virtual interface to be overriden as necessary */ 59 virtual status_t AddDevice(const usb_configuration_info *config); 60 61 virtual status_t ResetDevice(); 62 63 virtual status_t SetLineCoding(usb_serial_line_coding *coding); 64 virtual status_t SetControlLineState(uint16 state); 65 66 virtual void OnRead(char **buffer, size_t *numBytes); 67 virtual void OnWrite(const char *buffer, size_t *numBytes); 68 virtual void OnClose(); 69 70 private: 71 static int32 DeviceThread(void *data); 72 73 static void ReadCallbackFunction(void *cookie, 74 int32 status, void *data, 75 uint32 actualLength); 76 static void WriteCallbackFunction(void *cookie, 77 int32 status, void *data, 78 uint32 actualLength); 79 static void InterruptCallbackFunction(void *cookie, 80 int32 status, void *data, 81 uint32 actualLength); 82 83 usb_device fDevice; // USB device handle 84 uint16 fVendorID; 85 uint16 fProductID; 86 const char * fDescription; // informational description 87 88 /* communication pipes */ 89 usb_pipe fControlPipe; 90 usb_pipe fReadPipe; 91 usb_pipe fWritePipe; 92 93 /* line coding */ 94 usb_serial_line_coding fLineCoding; 95 96 /* data buffers */ 97 area_id fBufferArea; 98 char * fReadBuffer; 99 size_t fReadBufferSize; 100 char * fWriteBuffer; 101 size_t fWriteBufferSize; 102 char * fInterruptBuffer; 103 size_t fInterruptBufferSize; 104 105 /* variables used in callback functionality */ 106 size_t fActualLengthRead; 107 uint32 fStatusRead; 108 size_t fActualLengthWrite; 109 uint32 fStatusWrite; 110 size_t fActualLengthInterrupt; 111 uint32 fStatusInterrupt; 112 113 /* semaphores used in callbacks */ 114 sem_id fDoneRead; 115 sem_id fDoneWrite; 116 117 uint16 fControlOut; 118 bool fInputStopped; 119 struct ttyfile fTTYFile; 120 struct tty fTTY; 121 122 /* device thread management */ 123 thread_id fDeviceThread; 124 bool fStopDeviceThread; 125 126 /* device locks to ensure no concurent reads/writes */ 127 benaphore fReadLock; 128 benaphore fWriteLock; 129 }; 130 131 #endif // _USB_DEVICE_H_ 132