VirtualBox

source: vbox/trunk/src/VBox/Devices/Serial/DrvHostSerial.cpp@ 106438

Last change on this file since 106438 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.0 KB
Line 
1/* $Id: DrvHostSerial.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBox serial devices: Host serial driver
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29
30/*********************************************************************************************************************************
31* Header Files *
32*********************************************************************************************************************************/
33#define LOG_GROUP LOG_GROUP_DRV_HOST_SERIAL
34#include <VBox/vmm/pdm.h>
35#include <VBox/vmm/pdmserialifs.h>
36#include <VBox/err.h>
37
38#include <VBox/log.h>
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/file.h>
42#include <iprt/mem.h>
43#include <iprt/pipe.h>
44#include <iprt/semaphore.h>
45#include <iprt/uuid.h>
46#include <iprt/serialport.h>
47
48#include "VBoxDD.h"
49
50
51/*********************************************************************************************************************************
52* Structures and Typedefs *
53*********************************************************************************************************************************/
54
55/**
56 * Char driver instance data.
57 *
58 * @implements PDMISERIALCONNECTOR
59 */
60typedef struct DRVHOSTSERIAL
61{
62 /** Pointer to the driver instance structure. */
63 PPDMDRVINS pDrvIns;
64 /** Pointer to the serial port interface of the driver/device above us. */
65 PPDMISERIALPORT pDrvSerialPort;
66 /** Our serial interface. */
67 PDMISERIALCONNECTOR ISerialConnector;
68 /** I/O thread. */
69 PPDMTHREAD pIoThrd;
70 /** The serial port handle. */
71 RTSERIALPORT hSerialPort;
72 /** the device path */
73 char *pszDevicePath;
74 /** The active config of the serial port. */
75 RTSERIALPORTCFG Cfg;
76
77 /** Flag whether data is available from the device/driver above as notified by the driver. */
78 volatile bool fAvailWrExt;
79 /** Internal copy of the flag which gets reset when there is no data anymore. */
80 bool fAvailWrInt;
81 /** Small send buffer. */
82 uint8_t abTxBuf[16];
83 /** Amount of data in the buffer. */
84 size_t cbTxUsed;
85
86 /** The read queue. */
87 uint8_t abReadBuf[256];
88 /** Current offset to write to next. */
89 volatile uint32_t offWrite;
90 /** Current offset into the read buffer. */
91 volatile uint32_t offRead;
92 /** Current amount of data in the buffer. */
93 volatile size_t cbReadBuf;
94
95 /* Flag whether the host device ran into a fatal error condition and I/O is suspended
96 * until the nuext VM suspend/resume cycle where we will try again. */
97 volatile bool fIoFatalErr;
98 /** Event semaphore the I/O thread is waiting on */
99 RTSEMEVENT hSemEvtIoFatalErr;
100
101 /** Read/write statistics */
102 STAMCOUNTER StatBytesRead;
103 STAMCOUNTER StatBytesWritten;
104} DRVHOSTSERIAL, *PDRVHOSTSERIAL;
105
106
107/*********************************************************************************************************************************
108* Global Variables *
109*********************************************************************************************************************************/
110
111
112/*********************************************************************************************************************************
113* Internal Functions *
114*********************************************************************************************************************************/
115
116
117/**
118 * Resets the read buffer.
119 *
120 * @returns Number of bytes which were queued in the read buffer before reset.
121 * @param pThis The host serial driver instance.
122 */
123DECLINLINE(size_t) drvHostSerialReadBufReset(PDRVHOSTSERIAL pThis)
124{
125 size_t cbOld = ASMAtomicXchgZ(&pThis->cbReadBuf, 0);
126 ASMAtomicWriteU32(&pThis->offWrite, 0);
127 ASMAtomicWriteU32(&pThis->offRead, 0);
128
129 return cbOld;
130}
131
132
133/**
134 * Returns number of bytes free in the read buffer and pointer to the start of the free space
135 * in the read buffer.
136 *
137 * @returns Number of bytes free in the buffer.
138 * @param pThis The host serial driver instance.
139 * @param ppv Where to return the pointer if there is still free space.
140 */
141DECLINLINE(size_t) drvHostSerialReadBufGetWrite(PDRVHOSTSERIAL pThis, void **ppv)
142{
143 if (ppv)
144 *ppv = &pThis->abReadBuf[pThis->offWrite];
145
146 size_t cbFree = sizeof(pThis->abReadBuf) - ASMAtomicReadZ(&pThis->cbReadBuf);
147 if (cbFree)
148 cbFree = RT_MIN(cbFree, sizeof(pThis->abReadBuf) - pThis->offWrite);
149
150 return cbFree;
151}
152
153
154/**
155 * Returns number of bytes used in the read buffer and pointer to the next byte to read.
156 *
157 * @returns Number of bytes free in the buffer.
158 * @param pThis The host serial driver instance.
159 * @param ppv Where to return the pointer to the next data to read.
160 */
161DECLINLINE(size_t) drvHostSerialReadBufGetRead(PDRVHOSTSERIAL pThis, void **ppv)
162{
163 if (ppv)
164 *ppv = &pThis->abReadBuf[pThis->offRead];
165
166 size_t cbUsed = ASMAtomicReadZ(&pThis->cbReadBuf);
167 if (cbUsed)
168 cbUsed = RT_MIN(cbUsed, sizeof(pThis->abReadBuf) - pThis->offRead);
169
170 return cbUsed;
171}
172
173
174/**
175 * Advances the write position of the read buffer by the given amount of bytes.
176 *
177 * @param pThis The host serial driver instance.
178 * @param cbAdv Number of bytes to advance.
179 */
180DECLINLINE(void) drvHostSerialReadBufWriteAdv(PDRVHOSTSERIAL pThis, size_t cbAdv)
181{
182 uint32_t offWrite = ASMAtomicReadU32(&pThis->offWrite);
183 offWrite = (offWrite + cbAdv) % sizeof(pThis->abReadBuf);
184 ASMAtomicWriteU32(&pThis->offWrite, offWrite);
185 ASMAtomicAddZ(&pThis->cbReadBuf, cbAdv);
186}
187
188
189/**
190 * Advances the read position of the read buffer by the given amount of bytes.
191 *
192 * @param pThis The host serial driver instance.
193 * @param cbAdv Number of bytes to advance.
194 */
195DECLINLINE(void) drvHostSerialReadBufReadAdv(PDRVHOSTSERIAL pThis, size_t cbAdv)
196{
197 uint32_t offRead = ASMAtomicReadU32(&pThis->offRead);
198 offRead = (offRead + cbAdv) % sizeof(pThis->abReadBuf);
199 ASMAtomicWriteU32(&pThis->offRead, offRead);
200 ASMAtomicSubZ(&pThis->cbReadBuf, cbAdv);
201}
202
203
204/**
205 * Wakes up the serial port I/O thread.
206 *
207 * @returns VBox status code.
208 * @param pThis The host serial driver instance.
209 */
210static int drvHostSerialWakeupIoThread(PDRVHOSTSERIAL pThis)
211{
212
213 if (RT_UNLIKELY(pThis->fIoFatalErr))
214 return RTSemEventSignal(pThis->hSemEvtIoFatalErr);
215
216 return RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
217}
218
219
220/* -=-=-=-=- IBase -=-=-=-=- */
221
222/**
223 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
224 */
225static DECLCALLBACK(void *) drvHostSerialQueryInterface(PPDMIBASE pInterface, const char *pszIID)
226{
227 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
228 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
229
230 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
231 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISERIALCONNECTOR, &pThis->ISerialConnector);
232 return NULL;
233}
234
235
236/* -=-=-=-=- ISerialConnector -=-=-=-=- */
237
238/** @interface_method_impl{PDMISERIALCONNECTOR,pfnDataAvailWrNotify} */
239static DECLCALLBACK(int) drvHostSerialDataAvailWrNotify(PPDMISERIALCONNECTOR pInterface)
240{
241 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
242
243 int rc = VINF_SUCCESS;
244 bool fAvailOld = ASMAtomicXchgBool(&pThis->fAvailWrExt, true);
245 if (!fAvailOld)
246 rc = drvHostSerialWakeupIoThread(pThis);
247
248 return rc;
249}
250
251
252/**
253 * @interface_method_impl{PDMISERIALCONNECTOR,pfnReadRdr}
254 */
255static DECLCALLBACK(int) drvHostSerialReadRdr(PPDMISERIALCONNECTOR pInterface, void *pvBuf,
256 size_t cbRead, size_t *pcbRead)
257{
258 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
259 int rc = VINF_SUCCESS;
260 uint8_t *pbDst = (uint8_t *)pvBuf;
261 size_t cbReadAll = 0;
262
263 do
264 {
265 void *pvSrc = NULL;
266 size_t cbReadMax = drvHostSerialReadBufGetRead(pThis, &pvSrc);
267 size_t cbThisRead = RT_MIN(cbReadMax, cbRead);
268 if (cbThisRead)
269 {
270 memcpy(pbDst, pvSrc, cbThisRead);
271 cbRead -= cbThisRead;
272 pbDst += cbThisRead;
273 cbReadAll += cbThisRead;
274 drvHostSerialReadBufReadAdv(pThis, cbThisRead);
275 }
276 else
277 break;
278 } while (cbRead > 0);
279
280 *pcbRead = cbReadAll;
281 /* Kick the I/O thread if there is nothing to read to recalculate the poll flags. */
282 if (!drvHostSerialReadBufGetRead(pThis, NULL))
283 rc = drvHostSerialWakeupIoThread(pThis);
284
285 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbReadAll);
286 return rc;
287}
288
289
290/**
291 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgParams}
292 */
293static DECLCALLBACK(int) drvHostSerialChgParams(PPDMISERIALCONNECTOR pInterface, uint32_t uBps,
294 PDMSERIALPARITY enmParity, unsigned cDataBits,
295 PDMSERIALSTOPBITS enmStopBits)
296{
297 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
298
299 pThis->Cfg.uBaudRate = uBps;
300
301 switch (enmParity)
302 {
303 case PDMSERIALPARITY_EVEN:
304 pThis->Cfg.enmParity = RTSERIALPORTPARITY_EVEN;
305 break;
306 case PDMSERIALPARITY_ODD:
307 pThis->Cfg.enmParity = RTSERIALPORTPARITY_ODD;
308 break;
309 case PDMSERIALPARITY_NONE:
310 pThis->Cfg.enmParity = RTSERIALPORTPARITY_NONE;
311 break;
312 case PDMSERIALPARITY_MARK:
313 pThis->Cfg.enmParity = RTSERIALPORTPARITY_MARK;
314 break;
315 case PDMSERIALPARITY_SPACE:
316 pThis->Cfg.enmParity = RTSERIALPORTPARITY_SPACE;
317 break;
318 default:
319 AssertMsgFailed(("Unsupported parity setting %d\n", enmParity)); /* Should not happen. */
320 pThis->Cfg.enmParity = RTSERIALPORTPARITY_NONE;
321 }
322
323 switch (cDataBits)
324 {
325 case 5:
326 pThis->Cfg.enmDataBitCount = RTSERIALPORTDATABITS_5BITS;
327 break;
328 case 6:
329 pThis->Cfg.enmDataBitCount = RTSERIALPORTDATABITS_6BITS;
330 break;
331 case 7:
332 pThis->Cfg.enmDataBitCount = RTSERIALPORTDATABITS_7BITS;
333 break;
334 case 8:
335 pThis->Cfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
336 break;
337 default:
338 AssertMsgFailed(("Unsupported data bit count %u\n", cDataBits)); /* Should not happen. */
339 pThis->Cfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
340 }
341
342 switch (enmStopBits)
343 {
344 case PDMSERIALSTOPBITS_ONE:
345 pThis->Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
346 break;
347 case PDMSERIALSTOPBITS_ONEPOINTFIVE:
348 pThis->Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONEPOINTFIVE;
349 break;
350 case PDMSERIALSTOPBITS_TWO:
351 pThis->Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_TWO;
352 break;
353 default:
354 AssertMsgFailed(("Unsupported stop bit count %d\n", enmStopBits)); /* Should not happen. */
355 pThis->Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
356 }
357
358 return RTSerialPortCfgSet(pThis->hSerialPort, &pThis->Cfg, NULL);
359}
360
361
362/**
363 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgModemLines}
364 */
365static DECLCALLBACK(int) drvHostSerialChgModemLines(PPDMISERIALCONNECTOR pInterface, bool fRts, bool fDtr)
366{
367 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
368
369 uint32_t fClear = 0;
370 uint32_t fSet = 0;
371
372 if (fRts)
373 fSet |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
374 else
375 fClear |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
376
377 if (fDtr)
378 fSet |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
379 else
380 fClear |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
381
382 return RTSerialPortChgStatusLines(pThis->hSerialPort, fClear, fSet);
383}
384
385
386/**
387 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgBrk}
388 */
389static DECLCALLBACK(int) drvHostSerialChgBrk(PPDMISERIALCONNECTOR pInterface, bool fBrk)
390{
391 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
392
393 return RTSerialPortChgBreakCondition(pThis->hSerialPort, fBrk);
394}
395
396
397/**
398 * @interface_method_impl{PDMISERIALCONNECTOR,pfnQueryStsLines}
399 */
400static DECLCALLBACK(int) drvHostSerialQueryStsLines(PPDMISERIALCONNECTOR pInterface, uint32_t *pfStsLines)
401{
402 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
403
404 return RTSerialPortQueryStatusLines(pThis->hSerialPort, pfStsLines);
405}
406
407
408/**
409 * @callback_method_impl{PDMISERIALCONNECTOR,pfnQueuesFlush}
410 */
411static DECLCALLBACK(int) drvHostSerialQueuesFlush(PPDMISERIALCONNECTOR pInterface, bool fQueueRecv, bool fQueueXmit)
412{
413 RT_NOREF(fQueueXmit);
414 LogFlowFunc(("pInterface=%#p fQueueRecv=%RTbool fQueueXmit=%RTbool\n", pInterface, fQueueRecv, fQueueXmit));
415 int rc = VINF_SUCCESS;
416 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
417
418 if (fQueueRecv)
419 {
420 size_t cbOld = drvHostSerialReadBufReset(pThis);
421 if (cbOld) /* Kick the I/O thread to fetch new data. */
422 rc = drvHostSerialWakeupIoThread(pThis);
423 }
424
425 LogFlowFunc(("-> %Rrc\n", rc));
426 return rc;
427}
428
429
430/* -=-=-=-=- I/O thread -=-=-=-=- */
431
432/**
433 * The normal I/O loop.
434 *
435 * @returns VBox status code.
436 * @param pDrvIns Pointer to the driver instance data.
437 * @param pThis Host serial driver instance data.
438 * @param pThread Thread instance data.
439 */
440static int drvHostSerialIoLoopNormal(PPDMDRVINS pDrvIns, PDRVHOSTSERIAL pThis, PPDMTHREAD pThread)
441{
442 int rc = VINF_SUCCESS;
443 while ( pThread->enmState == PDMTHREADSTATE_RUNNING
444 && RT_SUCCESS(rc))
445 {
446 uint32_t fEvtFlags = RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED | RTSERIALPORT_EVT_F_BREAK_DETECTED;
447
448 if (!pThis->fAvailWrInt)
449 pThis->fAvailWrInt = ASMAtomicXchgBool(&pThis->fAvailWrExt, false);
450
451 /* Wait until there is room again if there is anyting to send. */
452 if ( pThis->fAvailWrInt
453 || pThis->cbTxUsed)
454 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_TX;
455
456 /* Try to receive more if there is still room. */
457 if (drvHostSerialReadBufGetWrite(pThis, NULL) > 0)
458 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_RX;
459
460 uint32_t fEvtsRecv = 0;
461 rc = RTSerialPortEvtPoll(pThis->hSerialPort, fEvtFlags, &fEvtsRecv, RT_INDEFINITE_WAIT);
462 if (RT_SUCCESS(rc))
463 {
464 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_TX)
465 {
466 if ( pThis->fAvailWrInt
467 && pThis->cbTxUsed < RT_ELEMENTS(pThis->abTxBuf))
468 {
469 /* Stuff as much data into the TX buffer as we can. */
470 size_t cbToFetch = RT_ELEMENTS(pThis->abTxBuf) - pThis->cbTxUsed;
471 size_t cbFetched = 0;
472 rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &pThis->abTxBuf[pThis->cbTxUsed], cbToFetch,
473 &cbFetched);
474 AssertRC(rc);
475
476 if (cbFetched > 0)
477 pThis->cbTxUsed += cbFetched;
478 else
479 {
480 /* There is no data available anymore. */
481 pThis->fAvailWrInt = false;
482 }
483 }
484
485 if (pThis->cbTxUsed)
486 {
487 size_t cbProcessed = 0;
488 rc = RTSerialPortWriteNB(pThis->hSerialPort, &pThis->abTxBuf[0], pThis->cbTxUsed, &cbProcessed);
489 if (RT_SUCCESS(rc))
490 {
491 pThis->cbTxUsed -= cbProcessed;
492 if ( pThis->cbTxUsed
493 && cbProcessed)
494 {
495 /* Move the data in the TX buffer to the front to fill the end again. */
496 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
497 }
498 else
499 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
500 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
501 }
502 else
503 {
504 LogRelMax(10, ("HostSerial#%d: Sending data failed even though the serial port is marked as writeable (rc=%Rrc)\n",
505 pThis->pDrvIns->iInstance, rc));
506 break;
507 }
508 }
509 }
510
511 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_RX)
512 {
513 void *pvDst = NULL;
514 size_t cbToRead = drvHostSerialReadBufGetWrite(pThis, &pvDst);
515 size_t cbRead = 0;
516 rc = RTSerialPortReadNB(pThis->hSerialPort, pvDst, cbToRead, &cbRead);
517 /*
518 * No data being available while the port is marked as readable can happen
519 * if another thread changed the settings of the port inbetween the poll and
520 * the read call because it can flush all the buffered data (seen on Windows).
521 */
522 if (rc != VINF_TRY_AGAIN)
523 {
524 if (RT_SUCCESS(rc))
525 {
526 drvHostSerialReadBufWriteAdv(pThis, cbRead);
527 /* Notify the device/driver above. */
528 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
529 AssertRC(rc);
530 }
531 else
532 LogRelMax(10, ("HostSerial#%d: Reading data failed even though the serial port is marked as readable (rc=%Rrc)\n",
533 pThis->pDrvIns->iInstance, rc));
534 }
535 }
536
537 if (fEvtsRecv & RTSERIALPORT_EVT_F_BREAK_DETECTED)
538 pThis->pDrvSerialPort->pfnNotifyBrk(pThis->pDrvSerialPort);
539
540 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED)
541 {
542 /* The status lines have changed. Notify the device. */
543 uint32_t fStsLines = 0;
544 rc = RTSerialPortQueryStatusLines(pThis->hSerialPort, &fStsLines);
545 if (RT_SUCCESS(rc))
546 {
547 uint32_t fPdmStsLines = 0;
548
549 if (fStsLines & RTSERIALPORT_STS_LINE_DCD)
550 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DCD;
551 if (fStsLines & RTSERIALPORT_STS_LINE_RI)
552 fPdmStsLines |= PDMISERIALPORT_STS_LINE_RI;
553 if (fStsLines & RTSERIALPORT_STS_LINE_DSR)
554 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DSR;
555 if (fStsLines & RTSERIALPORT_STS_LINE_CTS)
556 fPdmStsLines |= PDMISERIALPORT_STS_LINE_CTS;
557
558 rc = pThis->pDrvSerialPort->pfnNotifyStsLinesChanged(pThis->pDrvSerialPort, fPdmStsLines);
559 if (RT_FAILURE(rc))
560 {
561 /* Notifying device failed, continue but log it */
562 LogRelMax(10, ("HostSerial#%d: Notifying device about changed status lines failed with error %Rrc; continuing.\n",
563 pDrvIns->iInstance, rc));
564 rc = VINF_SUCCESS;
565 }
566 }
567 else
568 {
569 LogRelMax(10, ("HostSerial#%d: Getting status lines state failed with error %Rrc; continuing.\n", pDrvIns->iInstance, rc));
570 rc = VINF_SUCCESS;
571 }
572 }
573
574 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_MONITOR_FAILED)
575 {
576 LogRel(("HostSerial#%d: Status line monitoring failed at a lower level with rc=%Rrc and is disabled\n", pDrvIns->iInstance, rc));
577 rc = VINF_SUCCESS;
578 }
579 }
580 else if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
581 {
582 /* Getting interrupted or running into a timeout are no error conditions. */
583 rc = VINF_SUCCESS;
584 }
585 }
586
587 LogRel(("HostSerial#%d: The underlying host device run into a fatal error condition %Rrc, any data transfer is disabled\n",
588 pDrvIns->iInstance, rc));
589
590 return rc;
591}
592
593
594/**
595 * The error I/O loop.
596 *
597 * @param pThis Host serial driver instance data.
598 * @param pThread Thread instance data.
599 */
600static void drvHostSerialIoLoopError(PDRVHOSTSERIAL pThis, PPDMTHREAD pThread)
601{
602 ASMAtomicXchgBool(&pThis->fIoFatalErr, true);
603
604 PDMDrvHlpVMSetRuntimeError(pThis->pDrvIns, 0 /*fFlags*/, "SerialPortIoError",
605 N_("The host serial port \"%s\" encountered a fatal error and stopped functioning. "
606 "This can be caused by bad cabling or USB to serial converters being unplugged by accident. "
607 "To restart I/O transfers suspend and resume the VM after fixing the underlying issue."),
608 pThis->pszDevicePath);
609
610 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
611 {
612 /*
613 * We have to discard any data which is going to be send (the error
614 * mode resembles the "someone just pulled the plug on the serial port" situation)
615 */
616 RTSemEventWait(pThis->hSemEvtIoFatalErr, RT_INDEFINITE_WAIT);
617
618 if (ASMAtomicXchgBool(&pThis->fAvailWrExt, false))
619 {
620 size_t cbFetched = 0;
621
622 do
623 {
624 /* Stuff as much data into the TX buffer as we can. */
625 uint8_t abDiscard[64];
626 int rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &abDiscard, sizeof(abDiscard),
627 &cbFetched);
628 AssertRC(rc);
629 } while (cbFetched > 0);
630
631 /* Acknowledge the sent data. */
632 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
633
634 /*
635 * Sleep a bit to avoid excessive I/O loop CPU usage, timing is not important in
636 * this mode.
637 */
638 PDMDrvHlpThreadSleep(pThis->pDrvIns, pThread, 100);
639 }
640 }
641}
642
643
644/**
645 * I/O thread loop.
646 *
647 * @returns VINF_SUCCESS.
648 * @param pDrvIns PDM driver instance data.
649 * @param pThread The PDM thread data.
650 */
651static DECLCALLBACK(int) drvHostSerialIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
652{
653 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
654
655 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
656 return VINF_SUCCESS;
657
658 int rc = VINF_SUCCESS;
659 if (!pThis->fIoFatalErr)
660 rc = drvHostSerialIoLoopNormal(pDrvIns, pThis, pThread);
661
662 if ( RT_FAILURE(rc)
663 || pThis->fIoFatalErr)
664 drvHostSerialIoLoopError(pThis, pThread);
665
666 return VINF_SUCCESS;
667}
668
669
670/**
671 * Unblock the send thread so it can respond to a state change.
672 *
673 * @returns a VBox status code.
674 * @param pDrvIns The driver instance.
675 * @param pThread The send thread.
676 */
677static DECLCALLBACK(int) drvHostSerialWakeupIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
678{
679 RT_NOREF(pThread);
680 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
681
682 return drvHostSerialWakeupIoThread(pThis);
683}
684
685
686/* -=-=-=-=- driver interface -=-=-=-=- */
687
688/**
689 * @callback_method_impl{FNPDMDRVRESUME}
690 */
691static DECLCALLBACK(void) drvHostSerialResume(PPDMDRVINS pDrvIns)
692{
693 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
694
695 if (RT_UNLIKELY(pThis->fIoFatalErr))
696 {
697 /* Try to reopen the device and set the old config. */
698 uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ
699 | RTSERIALPORT_OPEN_F_WRITE
700 | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING
701 | RTSERIALPORT_OPEN_F_DETECT_BREAK_CONDITION;
702 int rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
703 if (rc == VERR_NOT_SUPPORTED)
704 {
705 /*
706 * For certain devices (or pseudo terminals) status line monitoring does not work
707 * so try again without it.
708 */
709 fOpenFlags &= ~RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
710 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
711 }
712
713 if (RT_SUCCESS(rc))
714 {
715 /* Set the config which is currently active. */
716 rc = RTSerialPortCfgSet(pThis->hSerialPort, &pThis->Cfg, NULL);
717 if (RT_FAILURE(rc))
718 LogRelMax(10, ("HostSerial#%d: Setting the active serial port config failed with error %Rrc during VM resume; continuing.\n", pDrvIns->iInstance, rc));
719 /* Reset the I/O error flag on success to resume the normal I/O thread loop. */
720 ASMAtomicXchgBool(&pThis->fIoFatalErr, false);
721 }
722 }
723}
724
725
726/**
727 * @callback_method_impl{FNPDMDRVSUSPEND}
728 */
729static DECLCALLBACK(void) drvHostSerialSuspend(PPDMDRVINS pDrvIns)
730{
731 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
732
733 if (RT_UNLIKELY(pThis->fIoFatalErr))
734 {
735 /* Close the device and try reopening it on resume. */
736 if (pThis->hSerialPort != NIL_RTSERIALPORT)
737 {
738 RTSerialPortClose(pThis->hSerialPort);
739 pThis->hSerialPort = NIL_RTSERIALPORT;
740 }
741 }
742}
743
744
745/**
746 * Destruct a char driver instance.
747 *
748 * Most VM resources are freed by the VM. This callback is provided so that
749 * any non-VM resources can be freed correctly.
750 *
751 * @param pDrvIns The driver instance data.
752 */
753static DECLCALLBACK(void) drvHostSerialDestruct(PPDMDRVINS pDrvIns)
754{
755 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
756 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
757 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
758
759 if (pThis->hSerialPort != NIL_RTSERIALPORT)
760 {
761 RTSerialPortClose(pThis->hSerialPort);
762 pThis->hSerialPort = NIL_RTSERIALPORT;
763 }
764
765 if (pThis->hSemEvtIoFatalErr != NIL_RTSEMEVENT)
766 {
767 RTSemEventDestroy(pThis->hSemEvtIoFatalErr);
768 pThis->hSemEvtIoFatalErr = NIL_RTSEMEVENT;
769 }
770
771 if (pThis->pszDevicePath)
772 {
773 PDMDrvHlpMMHeapFree(pDrvIns, pThis->pszDevicePath);
774 pThis->pszDevicePath = NULL;
775 }
776}
777
778
779/**
780 * Construct a char driver instance.
781 *
782 * @copydoc FNPDMDRVCONSTRUCT
783 */
784static DECLCALLBACK(int) drvHostSerialConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
785{
786 RT_NOREF1(fFlags);
787 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
788 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
789 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
790
791 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
792
793 /*
794 * Init basic data members and interfaces.
795 */
796 pThis->pDrvIns = pDrvIns;
797 pThis->hSerialPort = NIL_RTSERIALPORT;
798 pThis->fAvailWrExt = false;
799 pThis->fAvailWrInt = false;
800 pThis->cbTxUsed = 0;
801 pThis->offWrite = 0;
802 pThis->offRead = 0;
803 pThis->cbReadBuf = 0;
804 pThis->fIoFatalErr = false;
805 pThis->hSemEvtIoFatalErr = NIL_RTSEMEVENT;
806 /* IBase. */
807 pDrvIns->IBase.pfnQueryInterface = drvHostSerialQueryInterface;
808 /* ISerialConnector. */
809 pThis->ISerialConnector.pfnDataAvailWrNotify = drvHostSerialDataAvailWrNotify;
810 pThis->ISerialConnector.pfnReadRdr = drvHostSerialReadRdr;
811 pThis->ISerialConnector.pfnChgParams = drvHostSerialChgParams;
812 pThis->ISerialConnector.pfnChgModemLines = drvHostSerialChgModemLines;
813 pThis->ISerialConnector.pfnChgBrk = drvHostSerialChgBrk;
814 pThis->ISerialConnector.pfnQueryStsLines = drvHostSerialQueryStsLines;
815 pThis->ISerialConnector.pfnQueuesFlush = drvHostSerialQueuesFlush;
816
817 /*
818 * Validate the config.
819 */
820 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "DevicePath", "");
821
822 /*
823 * Query configuration.
824 */
825 /* Device */
826 int rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
827 if (RT_FAILURE(rc))
828 {
829 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
830 return rc;
831 }
832
833 /*
834 * Open the device
835 */
836 uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ
837 | RTSERIALPORT_OPEN_F_WRITE
838 | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING
839 | RTSERIALPORT_OPEN_F_DETECT_BREAK_CONDITION;
840 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
841 if (rc == VERR_NOT_SUPPORTED)
842 {
843 /*
844 * For certain devices (or pseudo terminals) status line monitoring does not work
845 * so try again without it.
846 */
847 fOpenFlags &= ~RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
848 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
849 }
850
851 if (RT_FAILURE(rc))
852 {
853 AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pThis->pszDevicePath, rc));
854 switch (rc)
855 {
856 case VERR_ACCESS_DENIED:
857 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
858#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
859 N_("Cannot open host device '%s' for read/write access. Check the permissions "
860 "of that device ('/bin/ls -l %s'): Most probably you need to be member "
861 "of the device group. Make sure that you logout/login after changing "
862 "the group settings of the current user"),
863#else
864 N_("Cannot open host device '%s' for read/write access. Check the permissions "
865 "of that device"),
866#endif
867 pThis->pszDevicePath, pThis->pszDevicePath);
868 default:
869 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
870 N_("Failed to open host device '%s'"),
871 pThis->pszDevicePath);
872 }
873 }
874
875 rc = RTSemEventCreate(&pThis->hSemEvtIoFatalErr);
876 if (RT_FAILURE(rc))
877 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d failed to create event semaphore"), pDrvIns->iInstance);
878
879 /*
880 * Get the ISerialPort interface of the above driver/device.
881 */
882 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
883 if (!pThis->pDrvSerialPort)
884 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("HostSerial#%d has no serial port interface above"), pDrvIns->iInstance);
885
886 /*
887 * Create the I/O thread.
888 */
889 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pIoThrd, pThis, drvHostSerialIoThread, drvHostSerialWakeupIoThread, 0, RTTHREADTYPE_IO, "SerIo");
890 if (RT_FAILURE(rc))
891 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d cannot create I/O thread"), pDrvIns->iInstance);
892
893 /*
894 * Register release statistics.
895 */
896 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
897 "Nr of bytes written", "/Devices/HostSerial%d/Written", pDrvIns->iInstance);
898 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
899 "Nr of bytes read", "/Devices/HostSerial%d/Read", pDrvIns->iInstance);
900
901 return VINF_SUCCESS;
902}
903
904/**
905 * Char driver registration record.
906 */
907const PDMDRVREG g_DrvHostSerial =
908{
909 /* u32Version */
910 PDM_DRVREG_VERSION,
911 /* szName */
912 "Host Serial",
913 /* szRCMod */
914 "",
915 /* szR0Mod */
916 "",
917 /* pszDescription */
918 "Host serial driver.",
919 /* fFlags */
920 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
921 /* fClass. */
922 PDM_DRVREG_CLASS_CHAR,
923 /* cMaxInstances */
924 ~0U,
925 /* cbInstance */
926 sizeof(DRVHOSTSERIAL),
927 /* pfnConstruct */
928 drvHostSerialConstruct,
929 /* pfnDestruct */
930 drvHostSerialDestruct,
931 /* pfnRelocate */
932 NULL,
933 /* pfnIOCtl */
934 NULL,
935 /* pfnPowerOn */
936 NULL,
937 /* pfnReset */
938 NULL,
939 /* pfnSuspend */
940 drvHostSerialSuspend,
941 /* pfnResume */
942 drvHostSerialResume,
943 /* pfnAttach */
944 NULL,
945 /* pfnDetach */
946 NULL,
947 /* pfnPowerOff */
948 NULL,
949 /* pfnSoftReset */
950 NULL,
951 /* u32EndVersion */
952 PDM_DRVREG_VERSION
953};
954
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