xref: /haiku/src/add-ons/kernel/drivers/ports/pc_serial/SerialDevice.h (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2009-2010, François Revol, <revol@free.fr>.
3  * Sponsored by TuneTracker Systems.
4  * Based on the Haiku usb_serial driver which is:
5  *
6  * Copyright (c) 2007-2008 by Michael Lotz
7  * Heavily based on the original usb_serial driver which is:
8  *
9  * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li>
10  * Distributed under the terms of the MIT License.
11  */
12 #ifndef _SERIAL_DEVICE_H_
13 #define _SERIAL_DEVICE_H_
14 
15 #include "Driver.h"
16 
17 class SerialDevice {
18 public:
19 /*								SerialDevice(struct serial_config_descriptor
20 									*device, uint32 ioBase, uint32 irq, SerialDevice *master=NULL);*/
21 								SerialDevice(const struct serial_support_descriptor
22 									*device, uint32 ioBase, uint32 irq, const SerialDevice *master=NULL);
23 virtual							~SerialDevice();
24 
25 static	SerialDevice *			MakeDevice(struct serial_config_descriptor
26 									*device);
27 
28 		status_t				Init();
29 
30 		const struct serial_support_descriptor	*SupportDescriptor() const
31 									{ return fSupportDescriptor; };
32 		struct serial_config_descriptor	*ConfigDescriptor() const
33 									{ return fDevice; };
34 		//uint16					ProductID() const { return fProductID; };
35 		//uint16					VendorID() const { return fVendorID; };
36 		const char *			Description() const { return fDescription; };
37 
38 		const SerialDevice *	Master() const { return fMaster ? fMaster : this; };
39 
40 		char *					ReadBuffer() { return fReadBuffer; };
41 		size_t					ReadBufferSize() { return fReadBufferSize; };
42 
43 		char *					WriteBuffer() { return fWriteBuffer; };
44 		size_t					WriteBufferSize() { return fWriteBufferSize; };
45 
46 #ifdef __BEOS__
47 		void					SetModes();
48 #endif
49 		void					SetModes(struct termios *tios);
50 #ifdef __HAIKU__
51 		bool					Service(struct tty *tty, uint32 op,
52 									void *buffer, size_t length);
53 #else
54 		bool					Service(struct tty *ptty, struct ddrover *ddr,
55 									uint flags);
56 #endif
57 
58 		int32					InterruptHandler();
59 
60 		status_t				Open(uint32 flags);
61 		status_t				Read(char *buffer, size_t *numBytes);
62 		status_t				Write(const char *buffer, size_t *numBytes);
63 		status_t				Control(uint32 op, void *arg, size_t length);
64 		status_t				Select(uint8 event, uint32 ref, selectsync *sync);
65 		status_t				DeSelect(uint8 event, selectsync *sync);
66 		status_t				Close();
67 		status_t				Free();
68 
69 		bool					IsOpen() { return fDeviceOpen; };
70 		void					Removed();
71 		bool					IsRemoved() { return fDeviceRemoved; };
72 
73 		/* virtual interface to be overriden as necessary */
74 virtual	status_t				AddDevice(const struct serial_config_descriptor *device);
75 
76 virtual	status_t				ResetDevice();
77 
78 //virtual	status_t				SetLineCoding(usb_serial_line_coding *coding);
79 //virtual	status_t				SetControlLineState(uint16 state);
80 virtual	status_t				SignalControlLineState(int line, bool enable);
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 		uint32					IOBase() const { return fIOBase; };
88 		uint32					IRQ() const { return fIRQ; };
89 
90 protected:
91 		void					SetReadBufferSize(size_t size) { fReadBufferSize = size; };
92 		void					SetWriteBufferSize(size_t size) { fWriteBufferSize = size; };
93 		void					SetInterruptBufferSize(size_t size) { fInterruptBufferSize = size; };
94 private:
95 static	int32					DeviceThread(void *data);
96 
97 static	void					ReadCallbackFunction(void *cookie,
98 									int32 status, void *data,
99 									uint32 actualLength);
100 static	void					WriteCallbackFunction(void *cookie,
101 									int32 status, void *data,
102 									uint32 actualLength);
103 static	void					InterruptCallbackFunction(void *cookie,
104 									int32 status, void *data,
105 									uint32 actualLength);
106 
107 		uint8					ReadReg8(int reg);
108 		void					WriteReg8(int reg, uint8 value);
109 		void					OrReg8(int reg, uint8 value);
110 		void					AndReg8(int reg, uint8 value);
111 		void					MaskReg8(int reg, uint8 value);
112 
113 		const struct serial_support_descriptor	*fSupportDescriptor;
114 		struct serial_config_descriptor		*fDevice;		// USB device handle
115 		const char *			fDescription;	// informational description
116 		bool					fDeviceOpen;
117 		bool					fDeviceRemoved;
118 
119 		bus_type				fBus;
120 		uint32					fIOBase;
121 		uint32					fIRQ;
122 		const SerialDevice *	fMaster;
123 
124 		/* line coding */
125 		//usb_serial_line_coding	fLineCoding;
126 
127 		/* data buffers */
128 		area_id					fBufferArea;
129 		char *					fReadBuffer;
130 		size_t					fReadBufferSize;
131 		char *					fWriteBuffer;
132 		size_t					fWriteBufferSize;
133 		char *					fInterruptBuffer;
134 		size_t					fInterruptBufferSize;
135 
136 		/* variables used in callback functionality */
137 		size_t					fActualLengthRead;
138 		uint32					fStatusRead;
139 		size_t					fActualLengthWrite;
140 		uint32					fStatusWrite;
141 		size_t					fActualLengthInterrupt;
142 		uint32					fStatusInterrupt;
143 
144 		/* semaphores used in callbacks */
145 		sem_id					fDoneRead;
146 		sem_id					fDoneWrite;
147 
148 		uint16					fControlOut;
149 		bool					fInputStopped;
150 #ifdef __HAIKU__
151 		struct tty *			fMasterTTY;
152 		struct tty *			fSlaveTTY;
153 		struct tty_cookie *		fTTYCookie;
154 #else
155 		struct ttyfile			fTTYFile;
156 		struct tty				fTTY;
157 		struct ddrover			fRover;
158 #endif
159 
160 		/* device thread management */
161 		thread_id				fDeviceThread;
162 		bool					fStopDeviceThread;
163 
164 		/* device locks to ensure no concurent reads/writes */
165 		mutex					fReadLock;
166 		mutex					fWriteLock;
167 };
168 
169 #endif // _SERIAL_DEVICE_H_
170