VirtualBox

source: vbox/trunk/src/VBox/Devices/Serial/DrvCharNew.cpp@ 73529

Last change on this file since 73529 was 73331, checked in by vboxsync, 6 years ago

Serial: Fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1/* $Id: DrvCharNew.cpp 73331 2018-07-23 15:23:48Z 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 /** I/O thread. */
62 PPDMTHREAD pThrdIo;
63
64 /** Amount of data available for sending from the device/driver above. */
65 volatile size_t cbAvailWr;
66 /** Small send buffer. */
67 uint8_t abTxBuf[16];
68 /** Amount of data in the buffer. */
69 size_t cbTxUsed;
70
71 /** Receive buffer. */
72 uint8_t abBuffer[256];
73 /** Number of bytes remaining in the receive buffer. */
74 volatile size_t cbRemaining;
75 /** Current position into the read buffer. */
76 uint8_t *pbBuf;
77
78#if HC_ARCH_BITS == 32
79 /** Alignment. */
80 uint32_t u32Alignment0;
81#endif
82
83 /** Read/write statistics */
84 STAMCOUNTER StatBytesRead;
85 STAMCOUNTER StatBytesWritten;
86} DRVCHAR, *PDRVCHAR;
87AssertCompileMemberAlignment(DRVCHAR, StatBytesRead, 8);
88
89
90
91
92/* -=-=-=-=- IBase -=-=-=-=- */
93
94/**
95 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
96 */
97static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, const char *pszIID)
98{
99 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
100 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
101
102 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
103 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISERIALCONNECTOR, &pThis->ISerialConnector);
104 return NULL;
105}
106
107
108/* -=-=-=-=- ISerialConnector -=-=-=-=- */
109
110
111/**
112 * @interface_method_impl{PDMISERIALCONNECTOR,pfnDataAvailWrNotify}
113 */
114static DECLCALLBACK(int) drvCharDataAvailWrNotify(PPDMISERIALCONNECTOR pInterface, size_t cbAvail)
115{
116 LogFlowFunc(("pInterface=%#p cbAvail=%zu\n", pInterface, cbAvail));
117 PDRVCHAR pThis = RT_FROM_MEMBER(pInterface, DRVCHAR, ISerialConnector);
118
119 int rc = VINF_SUCCESS;
120 size_t cbAvailOld = ASMAtomicAddZ(&pThis->cbAvailWr, cbAvail);
121 if (!cbAvailOld)
122 rc = pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
123
124 return rc;
125}
126
127
128/**
129 * @interface_method_impl{PDMISERIALCONNECTOR,pfnReadRdr}
130 */
131static DECLCALLBACK(int) drvCharReadRdr(PPDMISERIALCONNECTOR pInterface, void *pvBuf, size_t cbRead, size_t *pcbRead)
132{
133 LogFlowFunc(("pInterface=%#p pvBuf=%#p cbRead=%zu pcbRead=%#p\n", pInterface, pvBuf, cbRead, pcbRead));
134 PDRVCHAR pThis = RT_FROM_MEMBER(pInterface, DRVCHAR, ISerialConnector);
135 int rc = VINF_SUCCESS;
136
137 AssertReturn(pThis->cbRemaining, VERR_INVALID_STATE);
138 size_t cbToRead = RT_MIN(cbRead, pThis->cbRemaining);
139 memcpy(pvBuf, pThis->pbBuf, cbToRead);
140
141 pThis->pbBuf += cbToRead;
142 *pcbRead = cbToRead;
143 size_t cbOld = ASMAtomicSubZ(&pThis->cbRemaining, cbToRead);
144 if (!(cbOld - cbToRead)) /* Kick the I/O thread to fetch new data. */
145 rc = pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
146 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbToRead);
147
148 LogFlowFunc(("-> %Rrc\n", rc));
149 return rc;
150}
151
152
153/**
154 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgParams}
155 */
156static DECLCALLBACK(int) drvCharChgParams(PPDMISERIALCONNECTOR pInterface, uint32_t uBps,
157 PDMSERIALPARITY enmParity, unsigned cDataBits,
158 PDMSERIALSTOPBITS enmStopBits)
159{
160 /* Nothing to do here. */
161 RT_NOREF(pInterface, uBps, enmParity, cDataBits, enmStopBits);
162 return VINF_SUCCESS;
163}
164
165
166/**
167 * @callback_method_impl{PDMISERIALCONNECTOR,pfnChgModemLines}
168 */
169static DECLCALLBACK(int) drvCharChgModemLines(PPDMISERIALCONNECTOR pInterface, bool fRts, bool fDtr)
170{
171 /* Nothing to do here. */
172 RT_NOREF(pInterface, fRts, fDtr);
173 return VINF_SUCCESS;
174}
175
176
177/**
178 * @callback_method_impl{PDMISERIALCONNECTOR,pfnChgBrk}
179 */
180static DECLCALLBACK(int) drvCharChgBrk(PPDMISERIALCONNECTOR pInterface, bool fBrk)
181{
182 /* Nothing to do here. */
183 RT_NOREF(pInterface, fBrk);
184 return VINF_SUCCESS;
185}
186
187
188/**
189 * @callback_method_impl{PDMISERIALCONNECTOR,pfnQueryStsLines}
190 */
191static DECLCALLBACK(int) drvCharQueryStsLines(PPDMISERIALCONNECTOR pInterface, uint32_t *pfStsLines)
192{
193 /* Nothing to do here. */
194 *pfStsLines = 0;
195 RT_NOREF(pInterface);
196 return VINF_SUCCESS;
197}
198
199
200/* -=-=-=-=- I/O thread -=-=-=-=- */
201
202/**
203 * Send thread loop - pushes data down thru the driver chain.
204 *
205 * @returns VBox status code.
206 * @param pDrvIns The char driver instance.
207 * @param pThread The worker thread.
208 */
209static DECLCALLBACK(int) drvCharIoLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
210{
211 RT_NOREF(pDrvIns);
212 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
213
214 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
215 return VINF_SUCCESS;
216
217 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
218 {
219 uint32_t fEvts = 0;
220
221 if ( !pThis->cbRemaining
222 && pThis->pDrvStream->pfnRead)
223 fEvts |= RTPOLL_EVT_READ;
224 if ( pThis->cbAvailWr
225 || pThis->cbTxUsed)
226 fEvts |= RTPOLL_EVT_WRITE;
227
228 uint32_t fEvtsRecv = 0;
229 int rc = pThis->pDrvStream->pfnPoll(pThis->pDrvStream, fEvts, &fEvtsRecv, RT_INDEFINITE_WAIT);
230 if (RT_SUCCESS(rc))
231 {
232 if (fEvtsRecv & RTPOLL_EVT_WRITE)
233 {
234 if (pThis->cbAvailWr)
235 {
236 /* Stuff as much data into the TX buffer as we can. */
237 size_t cbToFetch = RT_MIN(RT_ELEMENTS(pThis->abTxBuf) - pThis->cbTxUsed, pThis->cbAvailWr);
238 size_t cbFetched = 0;
239 rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &pThis->abTxBuf[pThis->cbTxUsed], cbToFetch,
240 &cbFetched);
241 AssertRC(rc);
242
243 if (cbFetched > 0)
244 {
245 ASMAtomicSubZ(&pThis->cbAvailWr, cbFetched);
246 pThis->cbTxUsed += cbFetched;
247 }
248 else
249 {
250 /* The guest reset the send queue and there is no data available anymore. */
251 pThis->cbAvailWr = 0;
252 }
253 }
254
255 size_t cbProcessed = pThis->cbTxUsed;
256 rc = pThis->pDrvStream->pfnWrite(pThis->pDrvStream, &pThis->abTxBuf[0], &cbProcessed);
257 if (RT_SUCCESS(rc))
258 {
259 pThis->cbTxUsed -= cbProcessed;
260 if (pThis->cbTxUsed)
261 {
262 /* Move the data in the TX buffer to the front to fill the end again. */
263 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
264 }
265 else
266 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
267 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
268 }
269 else if (rc != VERR_TIMEOUT)
270 {
271 LogRel(("Char#%d: Write failed with %Rrc; skipping\n", pDrvIns->iInstance, rc));
272 break;
273 }
274 }
275
276 if (fEvtsRecv & RTPOLL_EVT_READ)
277 {
278 AssertPtr(pThis->pDrvStream->pfnRead);
279 Assert(!pThis->cbRemaining);
280
281 size_t cbRead = sizeof(pThis->abBuffer);
282 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, &pThis->abBuffer[0], &cbRead);
283 if (RT_FAILURE(rc))
284 {
285 LogFlow(("Read failed with %Rrc\n", rc));
286 break;
287 }
288
289 if (cbRead)
290 {
291 pThis->pbBuf = &pThis->abBuffer[0];
292 ASMAtomicWriteZ(&pThis->cbRemaining, cbRead);
293 /* Notify the upper device/driver. */
294 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
295 }
296 }
297 }
298 else if (rc != VERR_INTERRUPTED)
299 LogRelMax(10, ("Char#%d: Polling failed with %Rrc\n", pDrvIns->iInstance, rc));
300 }
301
302 return VINF_SUCCESS;
303}
304
305
306/**
307 * Unblock the send worker thread so it can respond to a state change.
308 *
309 * @returns VBox status code.
310 * @param pDrvIns The char driver instance.
311 * @param pThread The worker thread.
312 */
313static DECLCALLBACK(int) drvCharIoLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
314{
315 PDRVCHAR pThis = (PDRVCHAR)pThread->pvUser;
316
317 RT_NOREF(pDrvIns);
318 return pThis->pDrvStream->pfnPollInterrupt(pThis->pDrvStream);
319}
320
321
322/* -=-=-=-=- driver interface -=-=-=-=- */
323
324/**
325 * @interface_method_impl{PDMDEVREG,pfnReset}
326 */
327static DECLCALLBACK(void) drvCharReset(PPDMDRVINS pDrvIns)
328{
329 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
330 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
331
332 /* Reset TX and RX buffers. */
333 pThis->cbAvailWr = 0;
334 pThis->cbTxUsed = 0;
335 pThis->cbRemaining = 0;
336}
337
338
339/**
340 * Construct a char driver instance.
341 *
342 * @copydoc FNPDMDRVCONSTRUCT
343 */
344static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
345{
346 RT_NOREF(pCfg);
347 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
348 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
349 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
350
351 /*
352 * Init basic data members and interfaces.
353 */
354 pThis->pDrvIns = pDrvIns;
355 pThis->pThrdIo = NIL_RTTHREAD;
356 /* IBase. */
357 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
358 /* ISerialConnector. */
359 pThis->ISerialConnector.pfnDataAvailWrNotify = drvCharDataAvailWrNotify;
360 pThis->ISerialConnector.pfnReadRdr = drvCharReadRdr;
361 pThis->ISerialConnector.pfnChgParams = drvCharChgParams;
362 pThis->ISerialConnector.pfnChgModemLines = drvCharChgModemLines;
363 pThis->ISerialConnector.pfnChgBrk = drvCharChgBrk;
364 pThis->ISerialConnector.pfnQueryStsLines = drvCharQueryStsLines;
365
366 /*
367 * Get the ISerialPort interface of the above driver/device.
368 */
369 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
370 if (!pThis->pDrvSerialPort)
371 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS,
372 N_("Char#%d has no serial port interface above"), pDrvIns->iInstance);
373
374 /*
375 * Attach driver below and query its stream interface.
376 */
377 PPDMIBASE pBase;
378 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
379 if (RT_FAILURE(rc))
380 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
381 pThis->pDrvStream = PDMIBASE_QUERY_INTERFACE(pBase, PDMISTREAM);
382 if (!pThis->pDrvStream)
383 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS,
384 N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
385
386 rc = PDMDrvHlpThreadCreate(pThis->pDrvIns, &pThis->pThrdIo, pThis, drvCharIoLoop,
387 drvCharIoLoopWakeup, 0, RTTHREADTYPE_IO, "CharIo");
388 if (RT_FAILURE(rc))
389 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create I/O thread"), pDrvIns->iInstance);
390
391
392 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
393 "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
394 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
395 "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
396
397 return VINF_SUCCESS;
398}
399
400
401/**
402 * Char driver registration record.
403 */
404const PDMDRVREG g_DrvChar =
405{
406 /* u32Version */
407 PDM_DRVREG_VERSION,
408 /* szName */
409 "Char",
410 /* szRCMod */
411 "",
412 /* szR0Mod */
413 "",
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 ~0U,
422 /* cbInstance */
423 sizeof(DRVCHAR),
424 /* pfnConstruct */
425 drvCharConstruct,
426 /* pfnDestruct */
427 NULL,
428 /* pfnRelocate */
429 NULL,
430 /* pfnIOCtl */
431 NULL,
432 /* pfnPowerOn */
433 NULL,
434 /* pfnReset */
435 drvCharReset,
436 /* pfnSuspend */
437 NULL,
438 /* pfnResume */
439 NULL,
440 /* pfnAttach */
441 NULL,
442 /* pfnDetach */
443 NULL,
444 /* pfnPowerOff */
445 NULL,
446 /* pfnSoftReset */
447 NULL,
448 /* u32EndVersion */
449 PDM_DRVREG_VERSION
450};
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