VirtualBox

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

Last change on this file since 74882 was 74454, checked in by vboxsync, 6 years ago

DrvHostSerial: Ditto.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/* $Id: DrvChar.cpp 74454 2018-09-25 11:16:46Z vboxsync $ */
2/** @file
3 * Driver that adapts PDMISTREAM into PDMISERIALCONNECTOR / PDMISERIALPORT.
4 */
5
6/*
7 * Copyright (C) 2006-2018 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 /* Nothing to do here. */
195 *pfStsLines = 0;
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 VINF_SUCCESS; /** @todo r=bird: return rc? */
221}
222
223
224/* -=-=-=-=- I/O thread -=-=-=-=- */
225
226/**
227 * Send thread loop - pushes data down thru the driver chain.
228 *
229 * @returns VBox status code.
230 * @param pDrvIns The char driver instance.
231 * @param pThread The worker thread.
232 */
233static DECLCALLBACK(int) drvCharIoLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
234{
235 RT_NOREF(pDrvIns);
236 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
237
238 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
239 return VINF_SUCCESS;
240
241 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
242 {
243 uint32_t fEvts = 0;
244
245 if (!pThis->fAvailWrInt)
246 pThis->fAvailWrInt = ASMAtomicXchgBool(&pThis->fAvailWrExt, false);
247
248 if ( !pThis->cbRemaining
249 && pThis->pDrvStream->pfnRead)
250 fEvts |= RTPOLL_EVT_READ;
251 if ( pThis->fAvailWrInt
252 || pThis->cbTxUsed)
253 fEvts |= RTPOLL_EVT_WRITE;
254
255 uint32_t fEvtsRecv = 0;
256 int rc = pThis->pDrvStream->pfnPoll(pThis->pDrvStream, fEvts, &fEvtsRecv, RT_INDEFINITE_WAIT);
257 if (RT_SUCCESS(rc))
258 {
259 if (fEvtsRecv & RTPOLL_EVT_WRITE)
260 {
261 if (pThis->fAvailWrInt)
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 {
288 /* Move the data in the TX buffer to the front to fill the end again. */
289 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
290 }
291 else
292 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
293 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
294 }
295 else if (rc != VERR_TIMEOUT)
296 {
297 LogRel(("Char#%d: Write failed with %Rrc; skipping\n", pDrvIns->iInstance, rc));
298 break;
299 }
300 }
301 }
302
303 if (fEvtsRecv & RTPOLL_EVT_READ)
304 {
305 AssertPtr(pThis->pDrvStream->pfnRead);
306 Assert(!pThis->cbRemaining);
307
308 size_t cbRead = sizeof(pThis->abBuffer);
309 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, &pThis->abBuffer[0], &cbRead);
310 if (RT_FAILURE(rc))
311 {
312 LogFlow(("Read failed with %Rrc\n", rc));
313 break;
314 }
315
316 if (cbRead)
317 {
318 pThis->pbBuf = &pThis->abBuffer[0];
319 ASMAtomicWriteZ(&pThis->cbRemaining, cbRead);
320 /* Notify the upper device/driver. */
321 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
322 }
323 }
324 }
325 else if (rc != VERR_INTERRUPTED)
326 LogRelMax(10, ("Char#%d: Polling failed with %Rrc\n", pDrvIns->iInstance, rc));
327 }
328
329 return VINF_SUCCESS;
330}
331
332
333/**
334 * Unblock the send worker thread so it can respond to a state change.
335 *
336 * @returns VBox status code.
337 * @param pDrvIns The char driver instance.
338 * @param pThread The worker thread.
339 */
340static DECLCALLBACK(int) drvCharIoLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
341{
342 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
343
344 RT_NOREF(pDrvIns);
345 return pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
346}
347
348
349/* -=-=-=-=- driver interface -=-=-=-=- */
350
351/**
352 * @interface_method_impl{PDMDEVREG,pfnReset}
353 */
354static DECLCALLBACK(void) drvCharReset(PPDMDRVINS pDrvIns)
355{
356 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
357 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
358
359 /* Reset TX and RX buffers. */
360 pThis->fAvailWrExt = false;
361 pThis->fAvailWrInt = false;
362 pThis->cbTxUsed = 0;
363 pThis->cbRemaining = 0;
364}
365
366
367/**
368 * Construct a char driver instance.
369 *
370 * @copydoc FNPDMDRVCONSTRUCT
371 */
372static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
373{
374 RT_NOREF(pCfg);
375 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
376 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
377 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
378
379 /*
380 * Init basic data members and interfaces.
381 */
382 pThis->pDrvIns = pDrvIns;
383 pThis->pThrdIo = NIL_RTTHREAD;
384 /* IBase. */
385 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
386 /* ISerialConnector. */
387 pThis->ISerialConnector.pfnDataAvailWrNotify = drvCharDataAvailWrNotify;
388 pThis->ISerialConnector.pfnReadRdr = drvCharReadRdr;
389 pThis->ISerialConnector.pfnChgParams = drvCharChgParams;
390 pThis->ISerialConnector.pfnChgModemLines = drvCharChgModemLines;
391 pThis->ISerialConnector.pfnChgBrk = drvCharChgBrk;
392 pThis->ISerialConnector.pfnQueryStsLines = drvCharQueryStsLines;
393 pThis->ISerialConnector.pfnQueuesFlush = drvCharQueuesFlush;
394
395 /*
396 * Get the ISerialPort interface of the above driver/device.
397 */
398 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
399 if (!pThis->pDrvSerialPort)
400 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS,
401 N_("Char#%d has no serial port interface above"), pDrvIns->iInstance);
402
403 /*
404 * Attach driver below and query its stream interface.
405 */
406 PPDMIBASE pBase;
407 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
408 if (RT_FAILURE(rc))
409 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
410 pThis->pDrvStream = PDMIBASE_QUERY_INTERFACE(pBase, PDMISTREAM);
411 if (!pThis->pDrvStream)
412 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS,
413 N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
414
415 rc = PDMDrvHlpThreadCreate(pThis->pDrvIns, &pThis->pThrdIo, pThis, drvCharIoLoop,
416 drvCharIoLoopWakeup, 0, RTTHREADTYPE_IO, "CharIo");
417 if (RT_FAILURE(rc))
418 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create I/O thread"), pDrvIns->iInstance);
419
420
421 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
422 "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
423 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
424 "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
425
426 return VINF_SUCCESS;
427}
428
429
430/**
431 * Char driver registration record.
432 */
433const PDMDRVREG g_DrvChar =
434{
435 /* u32Version */
436 PDM_DRVREG_VERSION,
437 /* szName */
438 "Char",
439 /* szRCMod */
440 "",
441 /* szR0Mod */
442 "",
443 /* pszDescription */
444 "Generic char driver.",
445 /* fFlags */
446 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
447 /* fClass. */
448 PDM_DRVREG_CLASS_CHAR,
449 /* cMaxInstances */
450 ~0U,
451 /* cbInstance */
452 sizeof(DRVCHAR),
453 /* pfnConstruct */
454 drvCharConstruct,
455 /* pfnDestruct */
456 NULL,
457 /* pfnRelocate */
458 NULL,
459 /* pfnIOCtl */
460 NULL,
461 /* pfnPowerOn */
462 NULL,
463 /* pfnReset */
464 drvCharReset,
465 /* pfnSuspend */
466 NULL,
467 /* pfnResume */
468 NULL,
469 /* pfnAttach */
470 NULL,
471 /* pfnDetach */
472 NULL,
473 /* pfnPowerOff */
474 NULL,
475 /* pfnSoftReset */
476 NULL,
477 /* u32EndVersion */
478 PDM_DRVREG_VERSION
479};
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