xref: /haiku/src/add-ons/kernel/drivers/ports/usb_serial/SerialDevice.h (revision 746cac055adc6ac3308c7bc2d29040fb95689cc9)
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 									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 private:
80 static	int32					DeviceThread(void *data);
81 
82 static	void					ReadCallbackFunction(void *cookie,
83 									int32 status, void *data,
84 									uint32 actualLength);
85 static	void					WriteCallbackFunction(void *cookie,
86 									int32 status, void *data,
87 									uint32 actualLength);
88 static	void					InterruptCallbackFunction(void *cookie,
89 									int32 status, void *data,
90 									uint32 actualLength);
91 
92 		usb_device				fDevice;		// USB device handle
93 		uint16					fVendorID;
94 		uint16					fProductID;
95 		const char *			fDescription;	// informational description
96 		bool					fDeviceOpen;
97 		bool					fDeviceRemoved;
98 
99 		/* communication pipes */
100 		usb_pipe				fControlPipe;
101 		usb_pipe				fReadPipe;
102 		usb_pipe				fWritePipe;
103 
104 		/* line coding */
105 		usb_serial_line_coding	fLineCoding;
106 
107 		/* data buffers */
108 		area_id					fBufferArea;
109 		char *					fReadBuffer;
110 		size_t					fReadBufferSize;
111 		char *					fWriteBuffer;
112 		size_t					fWriteBufferSize;
113 		char *					fInterruptBuffer;
114 		size_t					fInterruptBufferSize;
115 
116 		/* variables used in callback functionality */
117 		size_t					fActualLengthRead;
118 		uint32					fStatusRead;
119 		size_t					fActualLengthWrite;
120 		uint32					fStatusWrite;
121 		size_t					fActualLengthInterrupt;
122 		uint32					fStatusInterrupt;
123 
124 		/* semaphores used in callbacks */
125 		sem_id					fDoneRead;
126 		sem_id					fDoneWrite;
127 
128 		uint16					fControlOut;
129 		bool					fInputStopped;
130 		struct ttyfile			fTTYFile;
131 		struct tty				fTTY;
132 
133 		/* device thread management */
134 		thread_id				fDeviceThread;
135 		bool					fStopDeviceThread;
136 
137 		/* device locks to ensure no concurent reads/writes */
138 		mutex					fReadLock;
139 		mutex					fWriteLock;
140 };
141 
142 #endif // _USB_DEVICE_H_
143