VirtualBox

source: vbox/trunk/src/VBox/Devices/Serial/DrvChar.cpp@ 23613

Last change on this file since 23613 was 23160, checked in by vboxsync, 15 years ago

Serial: Add support for break conditions. On Windows it also possible to detect break conditions on the serial device

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/** @file
2 *
3 * VBox stream I/O devices:
4 * Generic char driver
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#define LOG_GROUP LOG_GROUP_DRV_CHAR
29#include <VBox/pdmdrv.h>
30#include <iprt/asm.h>
31#include <iprt/assert.h>
32#include <iprt/stream.h>
33#include <iprt/semaphore.h>
34
35#include "Builtins.h"
36
37
38/** Size of the send fifo queue (in bytes) */
39#define CHAR_MAX_SEND_QUEUE 0x80
40#define CHAR_MAX_SEND_QUEUE_MASK 0x7f
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45
46/**
47 * Char driver instance data.
48 */
49typedef struct DRVCHAR
50{
51 /** Pointer to the driver instance structure. */
52 PPDMDRVINS pDrvIns;
53 /** Pointer to the char port interface of the driver/device above us. */
54 PPDMICHARPORT pDrvCharPort;
55 /** Pointer to the stream interface of the driver below us. */
56 PPDMISTREAM pDrvStream;
57 /** Our char interface. */
58 PDMICHAR IChar;
59 /** Flag to notify the receive thread it should terminate. */
60 volatile bool fShutdown;
61 /** Receive thread ID. */
62 RTTHREAD ReceiveThread;
63 /** Send thread ID. */
64 RTTHREAD SendThread;
65 /** Send event semephore */
66 RTSEMEVENT SendSem;
67
68 /** Internal send FIFO queue */
69 uint8_t aSendQueue[CHAR_MAX_SEND_QUEUE];
70 uint32_t iSendQueueHead;
71 uint32_t iSendQueueTail;
72
73 /** Read/write statistics */
74 STAMCOUNTER StatBytesRead;
75 STAMCOUNTER StatBytesWritten;
76} DRVCHAR, *PDRVCHAR;
77
78
79/** Converts a pointer to DRVCHAR::IChar to a PDRVCHAR. */
80#define PDMICHAR_2_DRVCHAR(pInterface) ( (PDRVCHAR)((uintptr_t)pInterface - RT_OFFSETOF(DRVCHAR, IChar)) )
81
82
83/* -=-=-=-=- IBase -=-=-=-=- */
84
85/**
86 * Queries an interface to the driver.
87 *
88 * @returns Pointer to interface.
89 * @returns NULL if the interface was not supported by the driver.
90 * @param pInterface Pointer to this interface structure.
91 * @param enmInterface The requested interface identification.
92 */
93static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
94{
95 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
96 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
97 switch (enmInterface)
98 {
99 case PDMINTERFACE_BASE:
100 return &pDrvIns->IBase;
101 case PDMINTERFACE_CHAR:
102 return &pThis->IChar;
103 default:
104 return NULL;
105 }
106}
107
108
109/* -=-=-=-=- IChar -=-=-=-=- */
110
111/** @copydoc PDMICHAR::pfnWrite */
112static DECLCALLBACK(int) drvCharWrite(PPDMICHAR pInterface, const void *pvBuf, size_t cbWrite)
113{
114 PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface);
115 const char *pBuffer = (const char *)pvBuf;
116
117 LogFlow(("%s: pvBuf=%#p cbWrite=%d\n", __FUNCTION__, pvBuf, cbWrite));
118
119 for (uint32_t i=0;i<cbWrite;i++)
120 {
121 uint32_t idx = pThis->iSendQueueHead;
122
123 pThis->aSendQueue[idx] = pBuffer[i];
124 idx = (idx + 1) & CHAR_MAX_SEND_QUEUE_MASK;
125
126 STAM_COUNTER_INC(&pThis->StatBytesWritten);
127 ASMAtomicXchgU32(&pThis->iSendQueueHead, idx);
128 }
129 RTSemEventSignal(pThis->SendSem);
130 return VINF_SUCCESS;
131}
132
133/** @copydoc PDMICHAR::pfnSetParameters */
134static DECLCALLBACK(int) drvCharSetParameters(PPDMICHAR pInterface, unsigned Bps, char chParity, unsigned cDataBits, unsigned cStopBits)
135{
136 /*PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface); - unused*/
137
138 LogFlow(("%s: Bps=%u chParity=%c cDataBits=%u cStopBits=%u\n", __FUNCTION__, Bps, chParity, cDataBits, cStopBits));
139 return VINF_SUCCESS;
140}
141
142
143/* -=-=-=-=- receive thread -=-=-=-=- */
144
145/**
146 * Send thread loop.
147 *
148 * @returns 0 on success.
149 * @param ThreadSelf Thread handle to this thread.
150 * @param pvUser User argument.
151 */
152static DECLCALLBACK(int) drvCharSendLoop(RTTHREAD ThreadSelf, void *pvUser)
153{
154 PDRVCHAR pThis = (PDRVCHAR)pvUser;
155
156 for(;;)
157 {
158 int rc = RTSemEventWait(pThis->SendSem, RT_INDEFINITE_WAIT);
159 if (RT_FAILURE(rc))
160 break;
161
162 /*
163 * Write the character to the attached stream (if present).
164 */
165 if ( !pThis->fShutdown
166 && pThis->pDrvStream)
167 {
168 while (pThis->iSendQueueTail != pThis->iSendQueueHead)
169 {
170 size_t cbProcessed = 1;
171
172 rc = pThis->pDrvStream->pfnWrite(pThis->pDrvStream, &pThis->aSendQueue[pThis->iSendQueueTail], &cbProcessed);
173 if (RT_SUCCESS(rc))
174 {
175 Assert(cbProcessed);
176 pThis->iSendQueueTail++;
177 pThis->iSendQueueTail &= CHAR_MAX_SEND_QUEUE_MASK;
178 }
179 else if (rc == VERR_TIMEOUT)
180 {
181 /* Normal case, just means that the stream didn't accept a new
182 * character before the timeout elapsed. Just retry. */
183 rc = VINF_SUCCESS;
184 }
185 else
186 {
187 LogFlow(("Write failed with %Rrc; skipping\n", rc));
188 break;
189 }
190 }
191 }
192 else
193 break;
194 }
195
196 pThis->SendThread = NIL_RTTHREAD;
197
198 return VINF_SUCCESS;
199}
200
201/* -=-=-=-=- receive thread -=-=-=-=- */
202
203/**
204 * Receive thread loop.
205 *
206 * @returns 0 on success.
207 * @param ThreadSelf Thread handle to this thread.
208 * @param pvUser User argument.
209 */
210static DECLCALLBACK(int) drvCharReceiveLoop(RTTHREAD ThreadSelf, void *pvUser)
211{
212 PDRVCHAR pThis = (PDRVCHAR)pvUser;
213 char aBuffer[256], *pBuffer;
214 size_t cbRemaining, cbProcessed;
215 int rc;
216
217 cbRemaining = 0;
218 pBuffer = aBuffer;
219 while (!pThis->fShutdown)
220 {
221 if (!cbRemaining)
222 {
223 /* Get block of data from stream driver. */
224 if (pThis->pDrvStream)
225 {
226 cbRemaining = sizeof(aBuffer);
227 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, aBuffer, &cbRemaining);
228 if (RT_FAILURE(rc))
229 {
230 LogFlow(("Read failed with %Rrc\n", rc));
231 break;
232 }
233 }
234 else
235 {
236 cbRemaining = 0;
237 RTThreadSleep(100);
238 }
239 pBuffer = aBuffer;
240 }
241 else
242 {
243 /* Send data to guest. */
244 cbProcessed = cbRemaining;
245 rc = pThis->pDrvCharPort->pfnNotifyRead(pThis->pDrvCharPort, pBuffer, &cbProcessed);
246 if (RT_SUCCESS(rc))
247 {
248 Assert(cbProcessed);
249 pBuffer += cbProcessed;
250 cbRemaining -= cbProcessed;
251 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbProcessed);
252 }
253 else if (rc == VERR_TIMEOUT)
254 {
255 /* Normal case, just means that the guest didn't accept a new
256 * character before the timeout elapsed. Just retry. */
257 rc = VINF_SUCCESS;
258 }
259 else
260 {
261 LogFlow(("NotifyRead failed with %Rrc\n", rc));
262 break;
263 }
264 }
265 }
266
267 pThis->ReceiveThread = NIL_RTTHREAD;
268
269 return VINF_SUCCESS;
270}
271
272/**
273 * Set the modem lines.
274 *
275 * @returns VBox status code
276 * @param pInterface Pointer to the interface structure.
277 * @param RequestToSend Set to true if this control line should be made active.
278 * @param DataTerminalReady Set to true if this control line should be made active.
279 */
280static DECLCALLBACK(int) drvCharSetModemLines(PPDMICHAR pInterface, bool RequestToSend, bool DataTerminalReady)
281{
282 /* Nothing to do here. */
283 return VINF_SUCCESS;
284}
285
286/**
287 * Sets the TD line into break condition.
288 *
289 * @returns VBox status code.
290 * @param pInterface Pointer to the interface structure containing the called function pointer.
291 * @param fBreak Set to true to let the device send a break false to put into normal operation.
292 * @thread Any thread.
293 */
294static DECLCALLBACK(int) drvCharSetBreak(PPDMICHAR pInterface, bool fBreak)
295{
296 /* Nothing to do here. */
297 return VINF_SUCCESS;
298}
299
300/* -=-=-=-=- driver interface -=-=-=-=- */
301
302/**
303 * Construct a char driver instance.
304 *
305 * @copydoc FNPDMDRVCONSTRUCT
306 */
307static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
308{
309 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
310 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
311
312 /*
313 * Init basic data members and interfaces.
314 */
315 pThis->ReceiveThread = NIL_RTTHREAD;
316 pThis->fShutdown = false;
317 /* IBase. */
318 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
319 /* IChar. */
320 pThis->IChar.pfnWrite = drvCharWrite;
321 pThis->IChar.pfnSetParameters = drvCharSetParameters;
322 pThis->IChar.pfnSetModemLines = drvCharSetModemLines;
323 pThis->IChar.pfnSetBreak = drvCharSetBreak;
324
325 /*
326 * Get the ICharPort interface of the above driver/device.
327 */
328 pThis->pDrvCharPort = (PPDMICHARPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_CHAR_PORT);
329 if (!pThis->pDrvCharPort)
330 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Char#%d has no char port interface above"), pDrvIns->iInstance);
331
332 /*
333 * Attach driver below and query its stream interface.
334 */
335 PPDMIBASE pBase;
336 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
337 if (RT_FAILURE(rc))
338 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
339 pThis->pDrvStream = (PPDMISTREAM)pBase->pfnQueryInterface(pBase, PDMINTERFACE_STREAM);
340 if (!pThis->pDrvStream)
341 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS, N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
342
343 /*
344 * Don't start the receive thread if the driver doesn't support reading
345 */
346 if (pThis->pDrvStream->pfnRead)
347 {
348 rc = RTThreadCreate(&pThis->ReceiveThread, drvCharReceiveLoop, (void *)pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharRecv");
349 if (RT_FAILURE(rc))
350 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create receive thread"), pDrvIns->iInstance);
351 }
352
353 rc = RTSemEventCreate(&pThis->SendSem);
354 AssertRC(rc);
355
356 rc = RTThreadCreate(&pThis->SendThread, drvCharSendLoop, (void *)pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharSend");
357 if (RT_FAILURE(rc))
358 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create send thread"), pDrvIns->iInstance);
359
360
361 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
362 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
363
364 return VINF_SUCCESS;
365}
366
367
368/**
369 * Destruct a char driver instance.
370 *
371 * Most VM resources are freed by the VM. This callback is provided so that
372 * any non-VM resources can be freed correctly.
373 *
374 * @param pDrvIns The driver instance data.
375 */
376static DECLCALLBACK(void) drvCharDestruct(PPDMDRVINS pDrvIns)
377{
378 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
379
380 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
381
382 pThis->fShutdown = true;
383 if (pThis->ReceiveThread)
384 {
385 RTThreadWait(pThis->ReceiveThread, 1000, NULL);
386 if (pThis->ReceiveThread != NIL_RTTHREAD)
387 LogRel(("Char%d: receive thread did not terminate\n", pDrvIns->iInstance));
388 }
389
390 /* Empty the send queue */
391 pThis->iSendQueueTail = pThis->iSendQueueHead = 0;
392
393 RTSemEventSignal(pThis->SendSem);
394 RTSemEventDestroy(pThis->SendSem);
395 pThis->SendSem = NIL_RTSEMEVENT;
396
397 if (pThis->SendThread)
398 {
399 RTThreadWait(pThis->SendThread, 1000, NULL);
400 if (pThis->SendThread != NIL_RTTHREAD)
401 LogRel(("Char%d: send thread did not terminate\n", pDrvIns->iInstance));
402 }
403}
404
405/**
406 * Char driver registration record.
407 */
408const PDMDRVREG g_DrvChar =
409{
410 /* u32Version */
411 PDM_DRVREG_VERSION,
412 /* szDriverName */
413 "Char",
414 /* pszDescription */
415 "Generic char driver.",
416 /* fFlags */
417 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
418 /* fClass. */
419 PDM_DRVREG_CLASS_CHAR,
420 /* cMaxInstances */
421 ~0,
422 /* cbInstance */
423 sizeof(DRVCHAR),
424 /* pfnConstruct */
425 drvCharConstruct,
426 /* pfnDestruct */
427 drvCharDestruct,
428 /* pfnIOCtl */
429 NULL,
430 /* pfnPowerOn */
431 NULL,
432 /* pfnReset */
433 NULL,
434 /* pfnSuspend */
435 NULL,
436 /* pfnResume */
437 NULL,
438 /* pfnAttach */
439 NULL,
440 /* pfnDetach */
441 NULL,
442 /* pfnPowerOff */
443 NULL,
444 /* pfnSoftReset */
445 NULL,
446 /* u32EndVersion */
447 PDM_DRVREG_VERSION
448};
449
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette