VirtualBox

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

Last change on this file since 82867 was 82867, checked in by vboxsync, 5 years ago

Devices/Serial/DrvChar: Return the actual value of rc instead of VINF_SUCCESS

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: DrvChar.cpp 82867 2020-01-27 10:23:13Z vboxsync $ */
2/** @file
3 * Driver that adapts PDMISTREAM into PDMISERIALCONNECTOR / PDMISERIALPORT.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_CHAR
23#include <VBox/vmm/pdmdrv.h>
24#include <VBox/vmm/pdmserialifs.h>
25#include <iprt/asm.h>
26#include <iprt/assert.h>
27#include <iprt/poll.h>
28#include <iprt/stream.h>
29#include <iprt/critsect.h>
30#include <iprt/semaphore.h>
31#include <iprt/uuid.h>
32
33#include "VBoxDD.h"
34
35
36/*********************************************************************************************************************************
37* Defined Constants And Macros *
38*********************************************************************************************************************************/
39
40
41/*********************************************************************************************************************************
42* Structures and Typedefs *
43*********************************************************************************************************************************/
44/**
45 * Char driver instance data.
46 *
47 * @implements PDMISERIALCONNECTOR
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 PPDMISERIALPORT pDrvSerialPort;
55 /** Pointer to the stream interface of the driver below us. */
56 PPDMISTREAM pDrvStream;
57 /** Our serial interface. */
58 PDMISERIALCONNECTOR ISerialConnector;
59 /** Flag to notify the receive thread it should terminate. */
60 volatile bool fShutdown;
61 /** Flag whether data is available from the device/driver above as notified by the driver. */
62 volatile bool fAvailWrExt;
63 /** Internal copy of the flag which gets reset when there is no data anymore. */
64 bool fAvailWrInt;
65 /** I/O thread. */
66 PPDMTHREAD pThrdIo;
67
68 /** Small send buffer. */
69 uint8_t abTxBuf[16];
70 /** Amount of data in the buffer. */
71 size_t cbTxUsed;
72
73 /** Receive buffer. */
74 uint8_t abBuffer[256];
75 /** Number of bytes remaining in the receive buffer. */
76 volatile size_t cbRemaining;
77 /** Current position into the read buffer. */
78 uint8_t *pbBuf;
79
80#if HC_ARCH_BITS == 32
81 uint32_t uAlignment0;
82#endif
83
84 /** Read/write statistics */
85 STAMCOUNTER StatBytesRead;
86 STAMCOUNTER StatBytesWritten;
87} DRVCHAR, *PDRVCHAR;
88AssertCompileMemberAlignment(DRVCHAR, StatBytesRead, 8);
89
90
91
92
93/* -=-=-=-=- IBase -=-=-=-=- */
94
95/**
96 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
97 */
98static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, const char *pszIID)
99{
100 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
101 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
102
103 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
104 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISERIALCONNECTOR, &pThis->ISerialConnector);
105 return NULL;
106}
107
108
109/* -=-=-=-=- ISerialConnector -=-=-=-=- */
110
111
112/**
113 * @interface_method_impl{PDMISERIALCONNECTOR,pfnDataAvailWrNotify}
114 */
115static DECLCALLBACK(int) drvCharDataAvailWrNotify(PPDMISERIALCONNECTOR pInterface)
116{
117 LogFlowFunc(("pInterface=%#p\n", pInterface));
118 PDRVCHAR pThis = RT_FROM_MEMBER(pInterface, DRVCHAR, ISerialConnector);
119
120 int rc = VINF_SUCCESS;
121 bool fAvailOld = ASMAtomicXchgBool(&pThis->fAvailWrExt, true);
122 if (!fAvailOld)
123 rc = pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
124
125 return rc;
126}
127
128
129/**
130 * @interface_method_impl{PDMISERIALCONNECTOR,pfnReadRdr}
131 */
132static DECLCALLBACK(int) drvCharReadRdr(PPDMISERIALCONNECTOR pInterface, void *pvBuf, size_t cbRead, size_t *pcbRead)
133{
134 LogFlowFunc(("pInterface=%#p pvBuf=%#p cbRead=%zu pcbRead=%#p\n", pInterface, pvBuf, cbRead, pcbRead));
135 PDRVCHAR pThis = RT_FROM_MEMBER(pInterface, DRVCHAR, ISerialConnector);
136 int rc = VINF_SUCCESS;
137
138 AssertReturn(pThis->cbRemaining, VERR_INVALID_STATE);
139 size_t cbToRead = RT_MIN(cbRead, pThis->cbRemaining);
140 memcpy(pvBuf, pThis->pbBuf, cbToRead);
141
142 pThis->pbBuf += cbToRead;
143 *pcbRead = cbToRead;
144 size_t cbOld = ASMAtomicSubZ(&pThis->cbRemaining, cbToRead);
145 if (!(cbOld - cbToRead)) /* Kick the I/O thread to fetch new data. */
146 rc = pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
147 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbToRead);
148
149 LogFlowFunc(("-> %Rrc\n", rc));
150 return rc;
151}
152
153
154/**
155 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgParams}
156 */
157static DECLCALLBACK(int) drvCharChgParams(PPDMISERIALCONNECTOR pInterface, uint32_t uBps,
158 PDMSERIALPARITY enmParity, unsigned cDataBits,
159 PDMSERIALSTOPBITS enmStopBits)
160{
161 /* Nothing to do here. */
162 RT_NOREF(pInterface, uBps, enmParity, cDataBits, enmStopBits);
163 return VINF_SUCCESS;
164}
165
166
167/**
168 * @callback_method_impl{PDMISERIALCONNECTOR,pfnChgModemLines}
169 */
170static DECLCALLBACK(int) drvCharChgModemLines(PPDMISERIALCONNECTOR pInterface, bool fRts, bool fDtr)
171{
172 /* Nothing to do here. */
173 RT_NOREF(pInterface, fRts, fDtr);
174 return VINF_SUCCESS;
175}
176
177
178/**
179 * @callback_method_impl{PDMISERIALCONNECTOR,pfnChgBrk}
180 */
181static DECLCALLBACK(int) drvCharChgBrk(PPDMISERIALCONNECTOR pInterface, bool fBrk)
182{
183 /* Nothing to do here. */
184 RT_NOREF(pInterface, fBrk);
185 return VINF_SUCCESS;
186}
187
188
189/**
190 * @callback_method_impl{PDMISERIALCONNECTOR,pfnQueryStsLines}
191 */
192static DECLCALLBACK(int) drvCharQueryStsLines(PPDMISERIALCONNECTOR pInterface, uint32_t *pfStsLines)
193{
194 /* Always carrier detect, data set read and clear to send. */
195 *pfStsLines = PDMISERIALPORT_STS_LINE_DCD | PDMISERIALPORT_STS_LINE_DSR | PDMISERIALPORT_STS_LINE_CTS;
196 RT_NOREF(pInterface);
197 return VINF_SUCCESS;
198}
199
200
201/**
202 * @callback_method_impl{PDMISERIALCONNECTOR,pfnQueuesFlush}
203 */
204static DECLCALLBACK(int) drvCharQueuesFlush(PPDMISERIALCONNECTOR pInterface, bool fQueueRecv, bool fQueueXmit)
205{
206 RT_NOREF(fQueueXmit);
207 LogFlowFunc(("pInterface=%#p fQueueRecv=%RTbool fQueueXmit=%RTbool\n", pInterface, fQueueRecv, fQueueXmit));
208 int rc = VINF_SUCCESS;
209 PDRVCHAR pThis = RT_FROM_MEMBER(pInterface, DRVCHAR, ISerialConnector);
210
211 if (fQueueRecv)
212 {
213 size_t cbOld = 0;
214 cbOld = ASMAtomicXchgZ(&pThis->cbRemaining, 0);
215 if (cbOld) /* Kick the I/O thread to fetch new data. */
216 rc = pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
217 }
218
219 LogFlowFunc(("-> %Rrc\n", rc));
220 return rc;
221
222
223/* -=-=-=-=- I/O thread -=-=-=-=- */
224
225/**
226 * Send thread loop - pushes data down thru the driver chain.
227 *
228 * @returns VBox status code.
229 * @param pDrvIns The char driver instance.
230 * @param pThread The worker thread.
231 */
232static DECLCALLBACK(int) drvCharIoLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
233{
234 RT_NOREF(pDrvIns);
235 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
236
237 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
238 return VINF_SUCCESS;
239
240 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
241 {
242 uint32_t fEvts = 0;
243
244 if (!pThis->fAvailWrInt)
245 pThis->fAvailWrInt = ASMAtomicXchgBool(&pThis->fAvailWrExt, false);
246
247 if ( !pThis->cbRemaining
248 && pThis->pDrvStream->pfnRead)
249 fEvts |= RTPOLL_EVT_READ;
250 if ( pThis->fAvailWrInt
251 || pThis->cbTxUsed)
252 fEvts |= RTPOLL_EVT_WRITE;
253
254 uint32_t fEvtsRecv = 0;
255 int rc = pThis->pDrvStream->pfnPoll(pThis->pDrvStream, fEvts, &fEvtsRecv, RT_INDEFINITE_WAIT);
256 if (RT_SUCCESS(rc))
257 {
258 if (fEvtsRecv & RTPOLL_EVT_WRITE)
259 {
260 if ( pThis->fAvailWrInt
261 && pThis->cbTxUsed < RT_ELEMENTS(pThis->abTxBuf))
262 {
263 /* Stuff as much data into the TX buffer as we can. */
264 size_t cbToFetch = RT_ELEMENTS(pThis->abTxBuf) - pThis->cbTxUsed;
265 size_t cbFetched = 0;
266 rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &pThis->abTxBuf[pThis->cbTxUsed], cbToFetch,
267 &cbFetched);
268 AssertRC(rc);
269
270 if (cbFetched > 0)
271 pThis->cbTxUsed += cbFetched;
272 else
273 {
274 /* There is no data available anymore. */
275 pThis->fAvailWrInt = false;
276 }
277 }
278
279 if (pThis->cbTxUsed)
280 {
281 size_t cbProcessed = pThis->cbTxUsed;
282 rc = pThis->pDrvStream->pfnWrite(pThis->pDrvStream, &pThis->abTxBuf[0], &cbProcessed);
283 if (RT_SUCCESS(rc))
284 {
285 pThis->cbTxUsed -= cbProcessed;
286 if ( pThis->cbTxUsed
287 && cbProcessed)
288 {
289 /* Move the data in the TX buffer to the front to fill the end again. */
290 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
291 }
292 else
293 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
294 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
295 }
296 else if (rc != VERR_TIMEOUT)
297 {
298 LogRel(("Char#%d: Write failed with %Rrc; skipping\n", pDrvIns->iInstance, rc));
299 break;
300 }
301 }
302 }
303
304 if (fEvtsRecv & RTPOLL_EVT_READ)
305 {
306 AssertPtr(pThis->pDrvStream->pfnRead);
307 Assert(!pThis->cbRemaining);
308
309 size_t cbRead = sizeof(pThis->abBuffer);
310 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, &pThis->abBuffer[0], &cbRead);
311 if (RT_FAILURE(rc))
312 {
313 LogFlow(("Read failed with %Rrc\n", rc));
314 break;
315 }
316
317 if (cbRead)
318 {
319 pThis->pbBuf = &pThis->abBuffer[0];
320 ASMAtomicWriteZ(&pThis->cbRemaining, cbRead);
321 /* Notify the upper device/driver. */
322 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
323 }
324 }
325 }
326 else if (rc != VERR_INTERRUPTED)
327 LogRelMax(10, ("Char#%d: Polling failed with %Rrc\n", pDrvIns->iInstance, rc));
328 }
329
330 return VINF_SUCCESS;
331}
332
333
334/**
335 * Unblock the send worker thread so it can respond to a state change.
336 *
337 * @returns VBox status code.
338 * @param pDrvIns The char driver instance.
339 * @param pThread The worker thread.
340 */
341static DECLCALLBACK(int) drvCharIoLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
342{
343 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
344
345 RT_NOREF(pDrvIns);
346 return pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
347}
348
349
350/* -=-=-=-=- driver interface -=-=-=-=- */
351
352/**
353 * @interface_method_impl{PDMDRVREG,pfnReset}
354 */
355static DECLCALLBACK(void) drvCharReset(PPDMDRVINS pDrvIns)
356{
357 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
358 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
359
360 /* Reset TX and RX buffers. */
361 pThis->fAvailWrExt = false;
362 pThis->fAvailWrInt = false;
363 pThis->cbTxUsed = 0;
364 pThis->cbRemaining = 0;
365}
366
367
368/**
369 * Construct a char driver instance.
370 *
371 * @copydoc FNPDMDRVCONSTRUCT
372 */
373static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
374{
375 RT_NOREF(pCfg);
376 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
377 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
378 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
379
380 /*
381 * Init basic data members and interfaces.
382 */
383 pThis->pDrvIns = pDrvIns;
384 pThis->pThrdIo = NIL_RTTHREAD;
385 /* IBase. */
386 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
387 /* ISerialConnector. */
388 pThis->ISerialConnector.pfnDataAvailWrNotify = drvCharDataAvailWrNotify;
389 pThis->ISerialConnector.pfnReadRdr = drvCharReadRdr;
390 pThis->ISerialConnector.pfnChgParams = drvCharChgParams;
391 pThis->ISerialConnector.pfnChgModemLines = drvCharChgModemLines;
392 pThis->ISerialConnector.pfnChgBrk = drvCharChgBrk;
393 pThis->ISerialConnector.pfnQueryStsLines = drvCharQueryStsLines;
394 pThis->ISerialConnector.pfnQueuesFlush = drvCharQueuesFlush;
395
396 /*
397 * Get the ISerialPort interface of the above driver/device.
398 */
399 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
400 if (!pThis->pDrvSerialPort)
401 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS,
402 N_("Char#%d has no serial port interface above"), pDrvIns->iInstance);
403
404 /*
405 * Attach driver below and query its stream interface.
406 */
407 PPDMIBASE pBase;
408 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
409 if (RT_FAILURE(rc))
410 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
411 pThis->pDrvStream = PDMIBASE_QUERY_INTERFACE(pBase, PDMISTREAM);
412 if (!pThis->pDrvStream)
413 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS,
414 N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
415
416 rc = PDMDrvHlpThreadCreate(pThis->pDrvIns, &pThis->pThrdIo, pThis, drvCharIoLoop,
417 drvCharIoLoopWakeup, 0, RTTHREADTYPE_IO, "CharIo");
418 if (RT_FAILURE(rc))
419 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create I/O thread"), pDrvIns->iInstance);
420
421
422 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
423 "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
424 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
425 "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
426
427 return VINF_SUCCESS;
428}
429
430
431/**
432 * Char driver registration record.
433 */
434const PDMDRVREG g_DrvChar =
435{
436 /* u32Version */
437 PDM_DRVREG_VERSION,
438 /* szName */
439 "Char",
440 /* szRCMod */
441 "",
442 /* szR0Mod */
443 "",
444 /* pszDescription */
445 "Generic char driver.",
446 /* fFlags */
447 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
448 /* fClass. */
449 PDM_DRVREG_CLASS_CHAR,
450 /* cMaxInstances */
451 ~0U,
452 /* cbInstance */
453 sizeof(DRVCHAR),
454 /* pfnConstruct */
455 drvCharConstruct,
456 /* pfnDestruct */
457 NULL,
458 /* pfnRelocate */
459 NULL,
460 /* pfnIOCtl */
461 NULL,
462 /* pfnPowerOn */
463 NULL,
464 /* pfnReset */
465 drvCharReset,
466 /* pfnSuspend */
467 NULL,
468 /* pfnResume */
469 NULL,
470 /* pfnAttach */
471 NULL,
472 /* pfnDetach */
473 NULL,
474 /* pfnPowerOff */
475 NULL,
476 /* pfnSoftReset */
477 NULL,
478 /* u32EndVersion */
479 PDM_DRVREG_VERSION
480};
Note: See TracBrowser for help on using the repository browser.

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