VirtualBox

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

Last change on this file since 74098 was 73766, checked in by vboxsync, 6 years ago

Serial: Remove the old serial code and switch over to the new one by default now that it is mostly stable (Any remaining bugs will get fixed)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.5 KB
Line 
1/* $Id: DrvHostSerial.cpp 73766 2018-08-19 16:55:11Z vboxsync $ */
2/** @file
3 * VBox serial devices: Host serial driver
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/*********************************************************************************************************************************
21* Header Files *
22*********************************************************************************************************************************/
23#define LOG_GROUP LOG_GROUP_DRV_HOST_SERIAL
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/pdmserialifs.h>
26#include <VBox/err.h>
27
28#include <VBox/log.h>
29#include <iprt/asm.h>
30#include <iprt/assert.h>
31#include <iprt/file.h>
32#include <iprt/mem.h>
33#include <iprt/pipe.h>
34#include <iprt/semaphore.h>
35#include <iprt/uuid.h>
36#include <iprt/serialport.h>
37
38#include "VBoxDD.h"
39
40
41/*********************************************************************************************************************************
42* Structures and Typedefs *
43*********************************************************************************************************************************/
44
45/**
46 * Char driver instance data.
47 *
48 * @implements PDMISERIALCONNECTOR
49 */
50typedef struct DRVHOSTSERIAL
51{
52 /** Pointer to the driver instance structure. */
53 PPDMDRVINS pDrvIns;
54 /** Pointer to the serial port interface of the driver/device above us. */
55 PPDMISERIALPORT pDrvSerialPort;
56 /** Our serial interface. */
57 PDMISERIALCONNECTOR ISerialConnector;
58 /** I/O thread. */
59 PPDMTHREAD pIoThrd;
60 /** The serial port handle. */
61 RTSERIALPORT hSerialPort;
62 /** the device path */
63 char *pszDevicePath;
64
65 /** Flag whether data is available from the device/driver above as notified by the driver. */
66 volatile bool fAvailWrExt;
67 /** Internal copy of the flag which gets reset when there is no data anymore. */
68 bool fAvailWrInt;
69 /** Small send buffer. */
70 uint8_t abTxBuf[16];
71 /** Amount of data in the buffer. */
72 size_t cbTxUsed;
73
74 /** The read queue. */
75 uint8_t abReadBuf[256];
76 /** Current offset to write to next. */
77 volatile uint32_t offWrite;
78 /** Current offset into the read buffer. */
79 volatile uint32_t offRead;
80 /** Current amount of data in the buffer. */
81 volatile size_t cbReadBuf;
82
83 /** Read/write statistics */
84 STAMCOUNTER StatBytesRead;
85 STAMCOUNTER StatBytesWritten;
86} DRVHOSTSERIAL, *PDRVHOSTSERIAL;
87
88
89/*********************************************************************************************************************************
90* Global Variables *
91*********************************************************************************************************************************/
92
93
94/*********************************************************************************************************************************
95* Internal Functions *
96*********************************************************************************************************************************/
97
98
99/**
100 * Returns number of bytes free in the read buffer and pointer to the start of the free space
101 * in the read buffer.
102 *
103 * @returns Number of bytes free in the buffer.
104 * @param pThis The host serial driver instance.
105 * @param ppv Where to return the pointer if there is still free space.
106 */
107DECLINLINE(size_t) drvHostSerialReadBufGetWrite(PDRVHOSTSERIAL pThis, void **ppv)
108{
109 if (ppv)
110 *ppv = &pThis->abReadBuf[pThis->offWrite];
111
112 size_t cbFree = sizeof(pThis->abReadBuf) - ASMAtomicReadZ(&pThis->cbReadBuf);
113 if (cbFree)
114 cbFree = RT_MIN(cbFree, sizeof(pThis->abReadBuf) - pThis->offWrite);
115
116 return cbFree;
117}
118
119
120/**
121 * Returns number of bytes used in the read buffer and pointer to the next byte to read.
122 *
123 * @returns Number of bytes free in the buffer.
124 * @param pThis The host serial driver instance.
125 * @param ppv Where to return the pointer to the next data to read.
126 */
127DECLINLINE(size_t) drvHostSerialReadBufGetRead(PDRVHOSTSERIAL pThis, void **ppv)
128{
129 if (ppv)
130 *ppv = &pThis->abReadBuf[pThis->offRead];
131
132 size_t cbUsed = ASMAtomicReadZ(&pThis->cbReadBuf);
133 if (cbUsed)
134 cbUsed = RT_MIN(cbUsed, sizeof(pThis->abReadBuf) - pThis->offRead);
135
136 return cbUsed;
137}
138
139
140/**
141 * Advances the write position of the read buffer by the given amount of bytes.
142 *
143 * @returns nothing.
144 * @param pThis The host serial driver instance.
145 * @param cbAdv Number of bytes to advance.
146 */
147DECLINLINE(void) drvHostSerialReadBufWriteAdv(PDRVHOSTSERIAL pThis, size_t cbAdv)
148{
149 uint32_t offWrite = ASMAtomicReadU32(&pThis->offWrite);
150 offWrite = (offWrite + cbAdv) % sizeof(pThis->abReadBuf);
151 ASMAtomicWriteU32(&pThis->offWrite, offWrite);
152 ASMAtomicAddZ(&pThis->cbReadBuf, cbAdv);
153}
154
155
156/**
157 * Advances the read position of the read buffer by the given amount of bytes.
158 *
159 * @returns nothing.
160 * @param pThis The host serial driver instance.
161 * @param cbAdv Number of bytes to advance.
162 */
163DECLINLINE(void) drvHostSerialReadBufReadAdv(PDRVHOSTSERIAL pThis, size_t cbAdv)
164{
165 uint32_t offRead = ASMAtomicReadU32(&pThis->offRead);
166 offRead = (offRead + cbAdv) % sizeof(pThis->abReadBuf);
167 ASMAtomicWriteU32(&pThis->offRead, offRead);
168 ASMAtomicSubZ(&pThis->cbReadBuf, cbAdv);
169}
170
171
172/* -=-=-=-=- IBase -=-=-=-=- */
173
174/**
175 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
176 */
177static DECLCALLBACK(void *) drvHostSerialQueryInterface(PPDMIBASE pInterface, const char *pszIID)
178{
179 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
180 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
181
182 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
183 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISERIALCONNECTOR, &pThis->ISerialConnector);
184 return NULL;
185}
186
187
188/* -=-=-=-=- ISerialConnector -=-=-=-=- */
189
190/** @interface_method_impl{PDMISERIALCONNECTOR,pfnDataAvailWrNotify} */
191static DECLCALLBACK(int) drvHostSerialDataAvailWrNotify(PPDMISERIALCONNECTOR pInterface)
192{
193 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
194
195 int rc = VINF_SUCCESS;
196 bool fAvailOld = ASMAtomicXchgBool(&pThis->fAvailWrExt, true);
197 if (!fAvailOld)
198 rc = RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
199
200 return rc;
201}
202
203
204/**
205 * @interface_method_impl{PDMISERIALCONNECTOR,pfnReadRdr}
206 */
207static DECLCALLBACK(int) drvHostSerialReadRdr(PPDMISERIALCONNECTOR pInterface, void *pvBuf,
208 size_t cbRead, size_t *pcbRead)
209{
210 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
211 int rc = VINF_SUCCESS;
212 uint8_t *pbDst = (uint8_t *)pvBuf;
213 size_t cbReadAll = 0;
214
215 do
216 {
217 void *pvSrc = NULL;
218 size_t cbThisRead = RT_MIN(drvHostSerialReadBufGetRead(pThis, &pvSrc), cbRead);
219 if (cbThisRead)
220 {
221 memcpy(pbDst, pvSrc, cbThisRead);
222 cbRead -= cbThisRead;
223 pbDst += cbThisRead;
224 cbReadAll += cbThisRead;
225 drvHostSerialReadBufReadAdv(pThis, cbThisRead);
226 }
227 else
228 break;
229 } while (cbRead > 0);
230
231 *pcbRead = cbReadAll;
232 /* Kick the I/O thread if there is nothing to read to recalculate the poll flags. */
233 if (!drvHostSerialReadBufGetRead(pThis, NULL))
234 rc = RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
235
236 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbReadAll);
237 return rc;
238}
239
240
241/**
242 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgParams}
243 */
244static DECLCALLBACK(int) drvHostSerialChgParams(PPDMISERIALCONNECTOR pInterface, uint32_t uBps,
245 PDMSERIALPARITY enmParity, unsigned cDataBits,
246 PDMSERIALSTOPBITS enmStopBits)
247{
248 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
249 RTSERIALPORTCFG Cfg;
250
251 Cfg.uBaudRate = uBps;
252
253 switch (enmParity)
254 {
255 case PDMSERIALPARITY_EVEN:
256 Cfg.enmParity = RTSERIALPORTPARITY_EVEN;
257 break;
258 case PDMSERIALPARITY_ODD:
259 Cfg.enmParity = RTSERIALPORTPARITY_ODD;
260 break;
261 case PDMSERIALPARITY_NONE:
262 Cfg.enmParity = RTSERIALPORTPARITY_NONE;
263 break;
264 case PDMSERIALPARITY_MARK:
265 Cfg.enmParity = RTSERIALPORTPARITY_MARK;
266 break;
267 case PDMSERIALPARITY_SPACE:
268 Cfg.enmParity = RTSERIALPORTPARITY_SPACE;
269 break;
270 default:
271 AssertMsgFailed(("Unsupported parity setting %d\n", enmParity)); /* Should not happen. */
272 Cfg.enmParity = RTSERIALPORTPARITY_NONE;
273 }
274
275 switch (cDataBits)
276 {
277 case 5:
278 Cfg.enmDataBitCount = RTSERIALPORTDATABITS_5BITS;
279 break;
280 case 6:
281 Cfg.enmDataBitCount = RTSERIALPORTDATABITS_6BITS;
282 break;
283 case 7:
284 Cfg.enmDataBitCount = RTSERIALPORTDATABITS_7BITS;
285 break;
286 case 8:
287 Cfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
288 break;
289 default:
290 AssertMsgFailed(("Unsupported data bit count %u\n", cDataBits)); /* Should not happen. */
291 Cfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS;
292 }
293
294 switch (enmStopBits)
295 {
296 case PDMSERIALSTOPBITS_ONE:
297 Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
298 break;
299 case PDMSERIALSTOPBITS_ONEPOINTFIVE:
300 Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONEPOINTFIVE;
301 break;
302 case PDMSERIALSTOPBITS_TWO:
303 Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_TWO;
304 break;
305 default:
306 AssertMsgFailed(("Unsupported stop bit count %d\n", enmStopBits)); /* Should not happen. */
307 Cfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE;
308 }
309
310 return RTSerialPortCfgSet(pThis->hSerialPort, &Cfg, NULL);
311}
312
313
314/**
315 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgModemLines}
316 */
317static DECLCALLBACK(int) drvHostSerialChgModemLines(PPDMISERIALCONNECTOR pInterface, bool fRts, bool fDtr)
318{
319 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
320
321 uint32_t fClear = 0;
322 uint32_t fSet = 0;
323
324 if (fRts)
325 fSet |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
326 else
327 fClear |= RTSERIALPORT_CHG_STS_LINES_F_RTS;
328
329 if (fDtr)
330 fSet |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
331 else
332 fClear |= RTSERIALPORT_CHG_STS_LINES_F_DTR;
333
334 return RTSerialPortChgStatusLines(pThis->hSerialPort, fClear, fSet);
335}
336
337
338/**
339 * @interface_method_impl{PDMISERIALCONNECTOR,pfnChgBrk}
340 */
341static DECLCALLBACK(int) drvHostSerialChgBrk(PPDMISERIALCONNECTOR pInterface, bool fBrk)
342{
343 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
344
345 return RTSerialPortChgBreakCondition(pThis->hSerialPort, fBrk);
346}
347
348
349/**
350 * @interface_method_impl{PDMISERIALCONNECTOR,pfnQueryStsLines}
351 */
352static DECLCALLBACK(int) drvHostSerialQueryStsLines(PPDMISERIALCONNECTOR pInterface, uint32_t *pfStsLines)
353{
354 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
355
356 return RTSerialPortQueryStatusLines(pThis->hSerialPort, pfStsLines);
357}
358
359
360/* -=-=-=-=- I/O thread -=-=-=-=- */
361
362/**
363 * I/O thread loop.
364 *
365 * @returns VINF_SUCCESS.
366 * @param pDrvIns PDM driver instance data.
367 * @param pThread The PDM thread data.
368 */
369static DECLCALLBACK(int) drvHostSerialIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
370{
371 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
372
373 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
374 return VINF_SUCCESS;
375
376 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
377 {
378 uint32_t fEvtFlags = RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED | RTSERIALPORT_EVT_F_BREAK_DETECTED;
379
380 if (!pThis->fAvailWrInt)
381 pThis->fAvailWrInt = ASMAtomicXchgBool(&pThis->fAvailWrExt, false);
382
383 /* Wait until there is room again if there is anyting to send. */
384 if ( pThis->fAvailWrInt
385 || pThis->cbTxUsed)
386 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_TX;
387
388 /* Try to receive more if there is still room. */
389 if (drvHostSerialReadBufGetWrite(pThis, NULL) > 0)
390 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_RX;
391
392 uint32_t fEvtsRecv = 0;
393 int rc = RTSerialPortEvtPoll(pThis->hSerialPort, fEvtFlags, &fEvtsRecv, RT_INDEFINITE_WAIT);
394 if (RT_SUCCESS(rc))
395 {
396 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_TX)
397 {
398 if (pThis->fAvailWrInt)
399 {
400 /* Stuff as much data into the TX buffer as we can. */
401 size_t cbToFetch = RT_ELEMENTS(pThis->abTxBuf) - pThis->cbTxUsed;
402 size_t cbFetched = 0;
403 rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &pThis->abTxBuf[pThis->cbTxUsed], cbToFetch,
404 &cbFetched);
405 AssertRC(rc);
406
407 if (cbFetched > 0)
408 pThis->cbTxUsed += cbFetched;
409 else
410 {
411 /* There is no data available anymore. */
412 pThis->fAvailWrInt = false;
413 }
414 }
415
416 if (pThis->cbTxUsed)
417 {
418 size_t cbProcessed = 0;
419 rc = RTSerialPortWriteNB(pThis->hSerialPort, &pThis->abTxBuf[0], pThis->cbTxUsed, &cbProcessed);
420 if (RT_SUCCESS(rc))
421 {
422 pThis->cbTxUsed -= cbProcessed;
423 if (pThis->cbTxUsed)
424 {
425 /* Move the data in the TX buffer to the front to fill the end again. */
426 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
427 }
428 else
429 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
430 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
431 }
432 else
433 {
434 LogRelMax(10, ("HostSerial#%d: Sending data failed even though the serial port is marked as writeable (rc=%Rrc)\n",
435 pThis->pDrvIns->iInstance, rc));
436 break;
437 }
438 }
439 }
440
441 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_RX)
442 {
443 void *pvDst = NULL;
444 size_t cbToRead = drvHostSerialReadBufGetWrite(pThis, &pvDst);
445 size_t cbRead = 0;
446 rc = RTSerialPortReadNB(pThis->hSerialPort, pvDst, cbToRead, &cbRead);
447 if (RT_SUCCESS(rc))
448 {
449 drvHostSerialReadBufWriteAdv(pThis, cbRead);
450 /* Notify the device/driver above. */
451 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
452 AssertRC(rc);
453 }
454 else
455 LogRelMax(10, ("HostSerial#%d: Reading data failed even though the serial port is marked as readable (rc=%Rrc)\n",
456 pThis->pDrvIns->iInstance, rc));
457 }
458
459 if (fEvtsRecv & RTSERIALPORT_EVT_F_BREAK_DETECTED)
460 pThis->pDrvSerialPort->pfnNotifyBrk(pThis->pDrvSerialPort);
461
462 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED)
463 {
464 /* The status lines have changed. Notify the device. */
465 uint32_t fStsLines = 0;
466 rc = RTSerialPortQueryStatusLines(pThis->hSerialPort, &fStsLines);
467 if (RT_SUCCESS(rc))
468 {
469 uint32_t fPdmStsLines = 0;
470
471 if (fStsLines & RTSERIALPORT_STS_LINE_DCD)
472 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DCD;
473 if (fStsLines & RTSERIALPORT_STS_LINE_RI)
474 fPdmStsLines |= PDMISERIALPORT_STS_LINE_RI;
475 if (fStsLines & RTSERIALPORT_STS_LINE_DSR)
476 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DSR;
477 if (fStsLines & RTSERIALPORT_STS_LINE_CTS)
478 fPdmStsLines |= PDMISERIALPORT_STS_LINE_CTS;
479
480 rc = pThis->pDrvSerialPort->pfnNotifyStsLinesChanged(pThis->pDrvSerialPort, fPdmStsLines);
481 if (RT_FAILURE(rc))
482 {
483 /* Notifying device failed, continue but log it */
484 LogRelMax(10, ("HostSerial#%d: Notifying device about changed status lines failed with error %Rrc; continuing.\n",
485 pDrvIns->iInstance, rc));
486 }
487 }
488 else
489 LogRelMax(10, ("HostSerial#%d: Getting status lines state failed with error %Rrc; continuing.\n", pDrvIns->iInstance, rc));
490 }
491
492 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_MONITOR_FAILED)
493 LogRel(("HostSerial#%d: Status line monitoring failed at a lower level and is disabled\n", pDrvIns->iInstance));
494 }
495 else if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
496 {
497 /* Getting interrupted or running into a timeout are no error conditions. */
498 rc = VINF_SUCCESS;
499 }
500 }
501
502 return VINF_SUCCESS;
503}
504
505
506/**
507 * Unblock the send thread so it can respond to a state change.
508 *
509 * @returns a VBox status code.
510 * @param pDrvIns The driver instance.
511 * @param pThread The send thread.
512 */
513static DECLCALLBACK(int) drvHostSerialWakeupIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
514{
515 RT_NOREF(pThread);
516 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
517
518 return RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
519}
520
521
522/* -=-=-=-=- driver interface -=-=-=-=- */
523
524/**
525 * Destruct a char driver instance.
526 *
527 * Most VM resources are freed by the VM. This callback is provided so that
528 * any non-VM resources can be freed correctly.
529 *
530 * @param pDrvIns The driver instance data.
531 */
532static DECLCALLBACK(void) drvHostSerialDestruct(PPDMDRVINS pDrvIns)
533{
534 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
535 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
536 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
537
538 if (pThis->hSerialPort != NIL_RTSERIALPORT)
539 {
540 RTSerialPortClose(pThis->hSerialPort);
541 pThis->hSerialPort = NIL_RTSERIALPORT;
542 }
543
544 if (pThis->pszDevicePath)
545 {
546 MMR3HeapFree(pThis->pszDevicePath);
547 pThis->pszDevicePath = NULL;
548 }
549}
550
551
552/**
553 * Construct a char driver instance.
554 *
555 * @copydoc FNPDMDRVCONSTRUCT
556 */
557static DECLCALLBACK(int) drvHostSerialConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
558{
559 RT_NOREF1(fFlags);
560 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
561 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
562 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
563
564 /*
565 * Init basic data members and interfaces.
566 */
567 pThis->pDrvIns = pDrvIns;
568 pThis->hSerialPort = NIL_RTSERIALPORT;
569 pThis->fAvailWrExt = false;
570 pThis->fAvailWrInt = false;
571 pThis->cbTxUsed = 0;
572 pThis->offWrite = 0;
573 pThis->offRead = 0;
574 pThis->cbReadBuf = 0;
575 /* IBase. */
576 pDrvIns->IBase.pfnQueryInterface = drvHostSerialQueryInterface;
577 /* ISerialConnector. */
578 pThis->ISerialConnector.pfnDataAvailWrNotify = drvHostSerialDataAvailWrNotify;
579 pThis->ISerialConnector.pfnReadRdr = drvHostSerialReadRdr;
580 pThis->ISerialConnector.pfnChgParams = drvHostSerialChgParams;
581 pThis->ISerialConnector.pfnChgModemLines = drvHostSerialChgModemLines;
582 pThis->ISerialConnector.pfnChgBrk = drvHostSerialChgBrk;
583 pThis->ISerialConnector.pfnQueryStsLines = drvHostSerialQueryStsLines;
584
585 /*
586 * Query configuration.
587 */
588 /* Device */
589 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
590 if (RT_FAILURE(rc))
591 {
592 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
593 return rc;
594 }
595
596 /*
597 * Open the device
598 */
599 uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ
600 | RTSERIALPORT_OPEN_F_WRITE
601 | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING
602 | RTSERIALPORT_OPEN_F_DETECT_BREAK_CONDITION;
603 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
604 if (rc == VERR_NOT_SUPPORTED)
605 {
606 /*
607 * For certain devices (or pseudo terminals) status line monitoring does not work
608 * so try again without it.
609 */
610 fOpenFlags &= ~RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
611 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
612 }
613
614 if (RT_FAILURE(rc))
615 {
616 AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pThis->pszDevicePath, rc));
617 switch (rc)
618 {
619 case VERR_ACCESS_DENIED:
620 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
621#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
622 N_("Cannot open host device '%s' for read/write access. Check the permissions "
623 "of that device ('/bin/ls -l %s'): Most probably you need to be member "
624 "of the device group. Make sure that you logout/login after changing "
625 "the group settings of the current user"),
626#else
627 N_("Cannot open host device '%s' for read/write access. Check the permissions "
628 "of that device"),
629#endif
630 pThis->pszDevicePath, pThis->pszDevicePath);
631 default:
632 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
633 N_("Failed to open host device '%s'"),
634 pThis->pszDevicePath);
635 }
636 }
637
638 /*
639 * Get the ISerialPort interface of the above driver/device.
640 */
641 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
642 if (!pThis->pDrvSerialPort)
643 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("HostSerial#%d has no serial port interface above"), pDrvIns->iInstance);
644
645 /*
646 * Create the I/O thread.
647 */
648 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pIoThrd, pThis, drvHostSerialIoThread, drvHostSerialWakeupIoThread, 0, RTTHREADTYPE_IO, "SerIo");
649 if (RT_FAILURE(rc))
650 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d cannot create I/O thread"), pDrvIns->iInstance);
651
652 /*
653 * Register release statistics.
654 */
655 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
656 "Nr of bytes written", "/Devices/HostSerial%d/Written", pDrvIns->iInstance);
657 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
658 "Nr of bytes read", "/Devices/HostSerial%d/Read", pDrvIns->iInstance);
659
660 return VINF_SUCCESS;
661}
662
663/**
664 * Char driver registration record.
665 */
666const PDMDRVREG g_DrvHostSerial =
667{
668 /* u32Version */
669 PDM_DRVREG_VERSION,
670 /* szName */
671 "Host Serial",
672 /* szRCMod */
673 "",
674 /* szR0Mod */
675 "",
676 /* pszDescription */
677 "Host serial driver.",
678 /* fFlags */
679 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
680 /* fClass. */
681 PDM_DRVREG_CLASS_CHAR,
682 /* cMaxInstances */
683 ~0U,
684 /* cbInstance */
685 sizeof(DRVHOSTSERIAL),
686 /* pfnConstruct */
687 drvHostSerialConstruct,
688 /* pfnDestruct */
689 drvHostSerialDestruct,
690 /* pfnRelocate */
691 NULL,
692 /* pfnIOCtl */
693 NULL,
694 /* pfnPowerOn */
695 NULL,
696 /* pfnReset */
697 NULL,
698 /* pfnSuspend */
699 NULL,
700 /* pfnResume */
701 NULL,
702 /* pfnAttach */
703 NULL,
704 /* pfnDetach */
705 NULL,
706 /* pfnPowerOff */
707 NULL,
708 /* pfnSoftReset */
709 NULL,
710 /* u32EndVersion */
711 PDM_DRVREG_VERSION
712};
713
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