xref: /haiku/src/add-ons/kernel/drivers/ports/pc_serial/SerialDevice.h (revision 21258e2674226d6aa732321b6f8494841895af5f)
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 		bool					Probe();
26 
27 static	SerialDevice *			MakeDevice(struct serial_config_descriptor
28 									*device);
29 
30 		status_t				Init();
31 
32 		const struct serial_support_descriptor	*SupportDescriptor() const
33 									{ return fSupportDescriptor; };
34 		struct serial_config_descriptor	*ConfigDescriptor() const
35 									{ return fDevice; };
36 		//uint16					ProductID() const { return fProductID; };
37 		//uint16					VendorID() const { return fVendorID; };
38 		const char *			Description() const { return fDescription; };
39 
40 		const SerialDevice *	Master() const { return fMaster ? fMaster : this; };
41 
42 		char *					ReadBuffer() { return fReadBuffer; };
43 
44 		char *					WriteBuffer() { return fWriteBuffer; };
45 
46 		void					SetModes(struct termios *tios);
47 
48 		bool					Service(struct tty *tty, uint32 op,
49 									void *buffer, size_t length);
50 
51 		bool					IsInterruptPending();
52 		int32					InterruptHandler();
53 
54 		status_t				Open(uint32 flags);
55 		status_t				Read(char *buffer, size_t *numBytes);
56 		status_t				Write(const char *buffer, size_t *numBytes);
57 		status_t				Control(uint32 op, void *arg, size_t length);
58 		status_t				Select(uint8 event, uint32 ref, selectsync *sync);
59 		status_t				DeSelect(uint8 event, selectsync *sync);
60 		status_t				Close();
61 		status_t				Free();
62 
63 		bool					IsOpen() { return fDeviceOpen; };
64 		void					Removed();
65 		bool					IsRemoved() { return fDeviceRemoved; };
66 
67 		/* virtual interface to be overriden as necessary */
68 virtual	status_t				AddDevice(const struct serial_config_descriptor *device);
69 
70 virtual	status_t				ResetDevice();
71 
72 //virtual	status_t				SetLineCoding(usb_serial_line_coding *coding);
73 //virtual	status_t				SetControlLineState(uint16 state);
74 virtual	status_t				SignalControlLineState(int line, bool enable);
75 
76 virtual	void					OnRead(char **buffer, size_t *numBytes);
77 virtual	void					OnWrite(const char *buffer, size_t *numBytes,
78 									size_t *packetBytes);
79 virtual	void					OnClose();
80 
81 		uint32					IOBase() const { return fIOBase; };
82 		uint32					IRQ() const { return fIRQ; };
83 
84 private:
85 static	int32					_DeviceThread(void *data);
86 		status_t				_WriteToDevice();
87 
88 static	void					ReadCallbackFunction(void *cookie,
89 									int32 status, void *data,
90 									uint32 actualLength);
91 static	void					WriteCallbackFunction(void *cookie,
92 									int32 status, void *data,
93 									uint32 actualLength);
94 static	void					InterruptCallbackFunction(void *cookie,
95 									int32 status, void *data,
96 									uint32 actualLength);
97 
98 		uint8					ReadReg8(int reg);
99 		void					WriteReg8(int reg, uint8 value);
100 		void					OrReg8(int reg, uint8 value);
101 		void					AndReg8(int reg, uint8 value);
102 		void					MaskReg8(int reg, uint8 value);
103 
104 		const struct serial_support_descriptor	*fSupportDescriptor;
105 		struct serial_config_descriptor		*fDevice;		// USB device handle
106 		const char *			fDescription;	// informational description
107 		bool					fDeviceOpen;
108 		bool					fDeviceRemoved;
109 
110 		bus_type				fBus;
111 		uint32					fIOBase;
112 		uint32					fIRQ;
113 		const SerialDevice *	fMaster;
114 
115 		/* line coding */
116 		//usb_serial_line_coding	fLineCoding;
117 
118 		/* deferred interrupt */
119 		uint8					fCachedIER;	// last value written to IER
120 		uint8					fCachedIIR;	// cached IRQ condition
121 		int32					fPendingDPC; // some IRQ still
122 
123 		/* data buffers */
124 		char					fReadBuffer[DEF_BUFFER_SIZE];
125 		int32					fReadBufferAvail;
126 		uint32					fReadBufferIn;
127 		uint32					fReadBufferOut;
128 		sem_id					fReadBufferSem;
129 		char					fWriteBuffer[DEF_BUFFER_SIZE];
130 		int32					fWriteBufferAvail;
131 		uint32					fWriteBufferIn;
132 		uint32					fWriteBufferOut;
133 		sem_id					fWriteBufferSem;
134 
135 		/* variables used in callback functionality */
136 		size_t					fActualLengthRead;
137 		uint32					fStatusRead;
138 		size_t					fActualLengthWrite;
139 		uint32					fStatusWrite;
140 		size_t					fActualLengthInterrupt;
141 		uint32					fStatusInterrupt;
142 
143 		/* semaphores used in callbacks */
144 		sem_id					fDoneRead;
145 		sem_id					fDoneWrite;
146 
147 		uint16					fControlOut;
148 		bool					fInputStopped;
149 
150 		struct tty *			fMasterTTY;
151 		struct tty *			fSlaveTTY;
152 		struct tty_cookie *		fSystemTTYCookie;
153 		struct tty_cookie *		fDeviceTTYCookie;
154 		struct termios			fTTYConfig;
155 
156 		/* device thread management */
157 		thread_id				fDeviceThread;
158 		bool					fStopDeviceThread;
159 };
160 
161 #endif // _SERIAL_DEVICE_H_
162