xref: /haiku/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.h (revision 362efe0c9f36d3dd38b22d2c24ac02e54b189d7c)
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(struct termios *tios);
46 		bool					Service(struct tty *tty, uint32 op,
47 									void *buffer, size_t length);
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_cdc_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 									size_t *packetBytes);
73 virtual	void					OnClose();
74 
75 protected:
76 		void					SetReadBufferSize(size_t size) { fReadBufferSize = size; };
77 		void					SetWriteBufferSize(size_t size) { fWriteBufferSize = size; };
78 		void					SetInterruptBufferSize(size_t size) { fInterruptBufferSize = size; };
79 
80 private:
81 static	int32					_InputThread(void *data);
82 		status_t				_WriteToDevice();
83 
84 static	void					_ReadCallbackFunction(void *cookie,
85 									int32 status, void *data,
86 									uint32 actualLength);
87 static	void					_WriteCallbackFunction(void *cookie,
88 									int32 status, void *data,
89 									uint32 actualLength);
90 static	void					_InterruptCallbackFunction(void *cookie,
91 									int32 status, void *data,
92 									uint32 actualLength);
93 
94 		usb_device				fDevice;		// USB device handle
95 		uint16					fVendorID;
96 		uint16					fProductID;
97 		const char *			fDescription;	// informational description
98 		bool					fDeviceOpen;
99 		bool					fDeviceRemoved;
100 
101 		/* communication pipes */
102 		usb_pipe				fControlPipe;
103 		usb_pipe				fReadPipe;
104 		usb_pipe				fWritePipe;
105 
106 		/* line coding */
107 		usb_cdc_line_coding		fLineCoding;
108 
109 		/* data buffers */
110 		area_id					fBufferArea;
111 		char *					fReadBuffer;
112 		size_t					fReadBufferSize;
113 		char *					fOutputBuffer;
114 		size_t					fOutputBufferSize;
115 		char *					fWriteBuffer;
116 		size_t					fWriteBufferSize;
117 		char *					fInterruptBuffer;
118 		size_t					fInterruptBufferSize;
119 
120 		/* variables used in callback functionality */
121 		size_t					fActualLengthRead;
122 		status_t				fStatusRead;
123 		size_t					fActualLengthWrite;
124 		status_t				fStatusWrite;
125 		size_t					fActualLengthInterrupt;
126 		status_t				fStatusInterrupt;
127 
128 		/* semaphores used in callbacks */
129 		sem_id					fDoneRead;
130 		sem_id					fDoneWrite;
131 
132 		uint16					fControlOut;
133 		bool					fInputStopped;
134 		struct tty *			fMasterTTY;
135 		struct tty *			fSlaveTTY;
136 		struct tty_cookie *		fSystemTTYCookie;
137 		struct tty_cookie *		fDeviceTTYCookie;
138 		struct termios			fTTYConfig;
139 
140 		/* input thread management */
141 		thread_id				fInputThread;
142 		bool					fStopThreads;
143 };
144 
145 #endif // _USB_DEVICE_H_
146