VirtualBox

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

Last change on this file since 75265 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: 26.3 KB
Line 
1/* $Id: DrvHostSerial.cpp 74454 2018-09-25 11:16:46Z 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/**
361 * @callback_method_impl{PDMISERIALCONNECTOR,pfnQueuesFlush}
362 */
363static DECLCALLBACK(int) drvHostSerialQueuesFlush(PPDMISERIALCONNECTOR pInterface, bool fQueueRecv, bool fQueueXmit)
364{
365 RT_NOREF(fQueueXmit);
366 LogFlowFunc(("pInterface=%#p fQueueRecv=%RTbool fQueueXmit=%RTbool\n", pInterface, fQueueRecv, fQueueXmit));
367 int rc = VINF_SUCCESS;
368 PDRVHOSTSERIAL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTSERIAL, ISerialConnector);
369
370 if (fQueueRecv)
371 {
372 size_t cbOld = ASMAtomicXchgZ(&pThis->cbReadBuf, 0);
373 if (cbOld) /* Kick the I/O thread to fetch new data. */
374 rc = RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
375 }
376
377 LogFlowFunc(("-> %Rrc\n", rc));
378 return VINF_SUCCESS;
379}
380
381
382/* -=-=-=-=- I/O thread -=-=-=-=- */
383
384/**
385 * I/O thread loop.
386 *
387 * @returns VINF_SUCCESS.
388 * @param pDrvIns PDM driver instance data.
389 * @param pThread The PDM thread data.
390 */
391static DECLCALLBACK(int) drvHostSerialIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
392{
393 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
394
395 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
396 return VINF_SUCCESS;
397
398 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
399 {
400 uint32_t fEvtFlags = RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED | RTSERIALPORT_EVT_F_BREAK_DETECTED;
401
402 if (!pThis->fAvailWrInt)
403 pThis->fAvailWrInt = ASMAtomicXchgBool(&pThis->fAvailWrExt, false);
404
405 /* Wait until there is room again if there is anyting to send. */
406 if ( pThis->fAvailWrInt
407 || pThis->cbTxUsed)
408 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_TX;
409
410 /* Try to receive more if there is still room. */
411 if (drvHostSerialReadBufGetWrite(pThis, NULL) > 0)
412 fEvtFlags |= RTSERIALPORT_EVT_F_DATA_RX;
413
414 uint32_t fEvtsRecv = 0;
415 int rc = RTSerialPortEvtPoll(pThis->hSerialPort, fEvtFlags, &fEvtsRecv, RT_INDEFINITE_WAIT);
416 if (RT_SUCCESS(rc))
417 {
418 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_TX)
419 {
420 if (pThis->fAvailWrInt)
421 {
422 /* Stuff as much data into the TX buffer as we can. */
423 size_t cbToFetch = RT_ELEMENTS(pThis->abTxBuf) - pThis->cbTxUsed;
424 size_t cbFetched = 0;
425 rc = pThis->pDrvSerialPort->pfnReadWr(pThis->pDrvSerialPort, &pThis->abTxBuf[pThis->cbTxUsed], cbToFetch,
426 &cbFetched);
427 AssertRC(rc);
428
429 if (cbFetched > 0)
430 pThis->cbTxUsed += cbFetched;
431 else
432 {
433 /* There is no data available anymore. */
434 pThis->fAvailWrInt = false;
435 }
436 }
437
438 if (pThis->cbTxUsed)
439 {
440 size_t cbProcessed = 0;
441 rc = RTSerialPortWriteNB(pThis->hSerialPort, &pThis->abTxBuf[0], pThis->cbTxUsed, &cbProcessed);
442 if (RT_SUCCESS(rc))
443 {
444 pThis->cbTxUsed -= cbProcessed;
445 if (pThis->cbTxUsed)
446 {
447 /* Move the data in the TX buffer to the front to fill the end again. */
448 memmove(&pThis->abTxBuf[0], &pThis->abTxBuf[cbProcessed], pThis->cbTxUsed);
449 }
450 else
451 pThis->pDrvSerialPort->pfnDataSentNotify(pThis->pDrvSerialPort);
452 STAM_COUNTER_ADD(&pThis->StatBytesWritten, cbProcessed);
453 }
454 else
455 {
456 LogRelMax(10, ("HostSerial#%d: Sending data failed even though the serial port is marked as writeable (rc=%Rrc)\n",
457 pThis->pDrvIns->iInstance, rc));
458 break;
459 }
460 }
461 }
462
463 if (fEvtsRecv & RTSERIALPORT_EVT_F_DATA_RX)
464 {
465 void *pvDst = NULL;
466 size_t cbToRead = drvHostSerialReadBufGetWrite(pThis, &pvDst);
467 size_t cbRead = 0;
468 rc = RTSerialPortReadNB(pThis->hSerialPort, pvDst, cbToRead, &cbRead);
469 if (RT_SUCCESS(rc))
470 {
471 drvHostSerialReadBufWriteAdv(pThis, cbRead);
472 /* Notify the device/driver above. */
473 rc = pThis->pDrvSerialPort->pfnDataAvailRdrNotify(pThis->pDrvSerialPort, cbRead);
474 AssertRC(rc);
475 }
476 else
477 LogRelMax(10, ("HostSerial#%d: Reading data failed even though the serial port is marked as readable (rc=%Rrc)\n",
478 pThis->pDrvIns->iInstance, rc));
479 }
480
481 if (fEvtsRecv & RTSERIALPORT_EVT_F_BREAK_DETECTED)
482 pThis->pDrvSerialPort->pfnNotifyBrk(pThis->pDrvSerialPort);
483
484 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_CHANGED)
485 {
486 /* The status lines have changed. Notify the device. */
487 uint32_t fStsLines = 0;
488 rc = RTSerialPortQueryStatusLines(pThis->hSerialPort, &fStsLines);
489 if (RT_SUCCESS(rc))
490 {
491 uint32_t fPdmStsLines = 0;
492
493 if (fStsLines & RTSERIALPORT_STS_LINE_DCD)
494 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DCD;
495 if (fStsLines & RTSERIALPORT_STS_LINE_RI)
496 fPdmStsLines |= PDMISERIALPORT_STS_LINE_RI;
497 if (fStsLines & RTSERIALPORT_STS_LINE_DSR)
498 fPdmStsLines |= PDMISERIALPORT_STS_LINE_DSR;
499 if (fStsLines & RTSERIALPORT_STS_LINE_CTS)
500 fPdmStsLines |= PDMISERIALPORT_STS_LINE_CTS;
501
502 rc = pThis->pDrvSerialPort->pfnNotifyStsLinesChanged(pThis->pDrvSerialPort, fPdmStsLines);
503 if (RT_FAILURE(rc))
504 {
505 /* Notifying device failed, continue but log it */
506 LogRelMax(10, ("HostSerial#%d: Notifying device about changed status lines failed with error %Rrc; continuing.\n",
507 pDrvIns->iInstance, rc));
508 }
509 }
510 else
511 LogRelMax(10, ("HostSerial#%d: Getting status lines state failed with error %Rrc; continuing.\n", pDrvIns->iInstance, rc));
512 }
513
514 if (fEvtsRecv & RTSERIALPORT_EVT_F_STATUS_LINE_MONITOR_FAILED)
515 LogRel(("HostSerial#%d: Status line monitoring failed at a lower level and is disabled\n", pDrvIns->iInstance));
516 }
517 else if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
518 {
519 /* Getting interrupted or running into a timeout are no error conditions. */
520 rc = VINF_SUCCESS;
521 }
522 }
523
524 return VINF_SUCCESS;
525}
526
527
528/**
529 * Unblock the send thread so it can respond to a state change.
530 *
531 * @returns a VBox status code.
532 * @param pDrvIns The driver instance.
533 * @param pThread The send thread.
534 */
535static DECLCALLBACK(int) drvHostSerialWakeupIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
536{
537 RT_NOREF(pThread);
538 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
539
540 return RTSerialPortEvtPollInterrupt(pThis->hSerialPort);
541}
542
543
544/* -=-=-=-=- driver interface -=-=-=-=- */
545
546/**
547 * Destruct a char driver instance.
548 *
549 * Most VM resources are freed by the VM. This callback is provided so that
550 * any non-VM resources can be freed correctly.
551 *
552 * @param pDrvIns The driver instance data.
553 */
554static DECLCALLBACK(void) drvHostSerialDestruct(PPDMDRVINS pDrvIns)
555{
556 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
557 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
558 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
559
560 if (pThis->hSerialPort != NIL_RTSERIALPORT)
561 {
562 RTSerialPortClose(pThis->hSerialPort);
563 pThis->hSerialPort = NIL_RTSERIALPORT;
564 }
565
566 if (pThis->pszDevicePath)
567 {
568 MMR3HeapFree(pThis->pszDevicePath);
569 pThis->pszDevicePath = NULL;
570 }
571}
572
573
574/**
575 * Construct a char driver instance.
576 *
577 * @copydoc FNPDMDRVCONSTRUCT
578 */
579static DECLCALLBACK(int) drvHostSerialConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
580{
581 RT_NOREF1(fFlags);
582 PDRVHOSTSERIAL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTSERIAL);
583 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
584 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
585
586 /*
587 * Init basic data members and interfaces.
588 */
589 pThis->pDrvIns = pDrvIns;
590 pThis->hSerialPort = NIL_RTSERIALPORT;
591 pThis->fAvailWrExt = false;
592 pThis->fAvailWrInt = false;
593 pThis->cbTxUsed = 0;
594 pThis->offWrite = 0;
595 pThis->offRead = 0;
596 pThis->cbReadBuf = 0;
597 /* IBase. */
598 pDrvIns->IBase.pfnQueryInterface = drvHostSerialQueryInterface;
599 /* ISerialConnector. */
600 pThis->ISerialConnector.pfnDataAvailWrNotify = drvHostSerialDataAvailWrNotify;
601 pThis->ISerialConnector.pfnReadRdr = drvHostSerialReadRdr;
602 pThis->ISerialConnector.pfnChgParams = drvHostSerialChgParams;
603 pThis->ISerialConnector.pfnChgModemLines = drvHostSerialChgModemLines;
604 pThis->ISerialConnector.pfnChgBrk = drvHostSerialChgBrk;
605 pThis->ISerialConnector.pfnQueryStsLines = drvHostSerialQueryStsLines;
606 pThis->ISerialConnector.pfnQueuesFlush = drvHostSerialQueuesFlush;
607
608 /*
609 * Query configuration.
610 */
611 /* Device */
612 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
613 if (RT_FAILURE(rc))
614 {
615 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
616 return rc;
617 }
618
619 /*
620 * Open the device
621 */
622 uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ
623 | RTSERIALPORT_OPEN_F_WRITE
624 | RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING
625 | RTSERIALPORT_OPEN_F_DETECT_BREAK_CONDITION;
626 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
627 if (rc == VERR_NOT_SUPPORTED)
628 {
629 /*
630 * For certain devices (or pseudo terminals) status line monitoring does not work
631 * so try again without it.
632 */
633 fOpenFlags &= ~RTSERIALPORT_OPEN_F_SUPPORT_STATUS_LINE_MONITORING;
634 rc = RTSerialPortOpen(&pThis->hSerialPort, pThis->pszDevicePath, fOpenFlags);
635 }
636
637 if (RT_FAILURE(rc))
638 {
639 AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pThis->pszDevicePath, rc));
640 switch (rc)
641 {
642 case VERR_ACCESS_DENIED:
643 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
644#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
645 N_("Cannot open host device '%s' for read/write access. Check the permissions "
646 "of that device ('/bin/ls -l %s'): Most probably you need to be member "
647 "of the device group. Make sure that you logout/login after changing "
648 "the group settings of the current user"),
649#else
650 N_("Cannot open host device '%s' for read/write access. Check the permissions "
651 "of that device"),
652#endif
653 pThis->pszDevicePath, pThis->pszDevicePath);
654 default:
655 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
656 N_("Failed to open host device '%s'"),
657 pThis->pszDevicePath);
658 }
659 }
660
661 /*
662 * Get the ISerialPort interface of the above driver/device.
663 */
664 pThis->pDrvSerialPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISERIALPORT);
665 if (!pThis->pDrvSerialPort)
666 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("HostSerial#%d has no serial port interface above"), pDrvIns->iInstance);
667
668 /*
669 * Create the I/O thread.
670 */
671 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pIoThrd, pThis, drvHostSerialIoThread, drvHostSerialWakeupIoThread, 0, RTTHREADTYPE_IO, "SerIo");
672 if (RT_FAILURE(rc))
673 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d cannot create I/O thread"), pDrvIns->iInstance);
674
675 /*
676 * Register release statistics.
677 */
678 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
679 "Nr of bytes written", "/Devices/HostSerial%d/Written", pDrvIns->iInstance);
680 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
681 "Nr of bytes read", "/Devices/HostSerial%d/Read", pDrvIns->iInstance);
682
683 return VINF_SUCCESS;
684}
685
686/**
687 * Char driver registration record.
688 */
689const PDMDRVREG g_DrvHostSerial =
690{
691 /* u32Version */
692 PDM_DRVREG_VERSION,
693 /* szName */
694 "Host Serial",
695 /* szRCMod */
696 "",
697 /* szR0Mod */
698 "",
699 /* pszDescription */
700 "Host serial driver.",
701 /* fFlags */
702 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
703 /* fClass. */
704 PDM_DRVREG_CLASS_CHAR,
705 /* cMaxInstances */
706 ~0U,
707 /* cbInstance */
708 sizeof(DRVHOSTSERIAL),
709 /* pfnConstruct */
710 drvHostSerialConstruct,
711 /* pfnDestruct */
712 drvHostSerialDestruct,
713 /* pfnRelocate */
714 NULL,
715 /* pfnIOCtl */
716 NULL,
717 /* pfnPowerOn */
718 NULL,
719 /* pfnReset */
720 NULL,
721 /* pfnSuspend */
722 NULL,
723 /* pfnResume */
724 NULL,
725 /* pfnAttach */
726 NULL,
727 /* pfnDetach */
728 NULL,
729 /* pfnPowerOff */
730 NULL,
731 /* pfnSoftReset */
732 NULL,
733 /* u32EndVersion */
734 PDM_DRVREG_VERSION
735};
736
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