VirtualBox

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

Last change on this file since 30714 was 29846, checked in by vboxsync, 15 years ago

Devices/Serial: update the implementation to 16550A

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* $Id: DrvChar.cpp 29846 2010-05-27 16:25:28Z vboxsync $ */
2/** @file
3 * Driver that adapts PDMISTREAM into PDMICHARCONNECTOR / PDMICHARPORT.
4 *
5 * Converts synchronous calls (PDMICHARCONNECTOR::pfnWrite, PDMISTREAM::pfnRead)
6 * into asynchronous ones.
7 *
8 * Note that we don't use a send buffer here to be able to handle
9 * dropping of bytes for xmit at device level.
10 */
11
12/*
13 * Copyright (C) 2006-2010 Oracle Corporation
14 *
15 * This file is part of VirtualBox Open Source Edition (OSE), as
16 * available from http://www.virtualbox.org. This file is free software;
17 * you can redistribute it and/or modify it under the terms of the GNU
18 * General Public License (GPL) as published by the Free Software
19 * Foundation, in version 2 as it comes in the "COPYING" file of the
20 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22 */
23
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#define LOG_GROUP LOG_GROUP_DRV_CHAR
29#include <VBox/pdmdrv.h>
30#include <iprt/asm.h>
31#include <iprt/assert.h>
32#include <iprt/stream.h>
33#include <iprt/semaphore.h>
34#include <iprt/uuid.h>
35
36#include "Builtins.h"
37
38
39/*******************************************************************************
40* Defined Constants And Macros *
41*******************************************************************************/
42/** Converts a pointer to DRVCHAR::ICharConnector to a PDRVCHAR. */
43#define PDMICHAR_2_DRVCHAR(pInterface) RT_FROM_MEMBER(pInterface, DRVCHAR, ICharConnector)
44
45
46/*******************************************************************************
47* Structures and Typedefs *
48*******************************************************************************/
49/**
50 * Char driver instance data.
51 *
52 * @implements PDMICHARCONNECTOR
53 */
54typedef struct DRVCHAR
55{
56 /** Pointer to the driver instance structure. */
57 PPDMDRVINS pDrvIns;
58 /** Pointer to the char port interface of the driver/device above us. */
59 PPDMICHARPORT pDrvCharPort;
60 /** Pointer to the stream interface of the driver below us. */
61 PPDMISTREAM pDrvStream;
62 /** Our char interface. */
63 PDMICHARCONNECTOR ICharConnector;
64 /** Flag to notify the receive thread it should terminate. */
65 volatile bool fShutdown;
66 /** Receive thread ID. */
67 RTTHREAD ReceiveThread;
68 /** Send thread ID. */
69 RTTHREAD SendThread;
70 /** Send event semephore */
71 RTSEMEVENT SendSem;
72
73 /** Internal send FIFO queue */
74 uint8_t volatile u8SendByte;
75 bool volatile fSending;
76 uint8_t Alignment[2];
77
78 /** Read/write statistics */
79 STAMCOUNTER StatBytesRead;
80 STAMCOUNTER StatBytesWritten;
81} DRVCHAR, *PDRVCHAR;
82AssertCompileMemberAlignment(DRVCHAR, StatBytesRead, 8);
83
84
85
86
87/* -=-=-=-=- IBase -=-=-=-=- */
88
89/**
90 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
91 */
92static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, const char *pszIID)
93{
94 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
95 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
96
97 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
98 PDMIBASE_RETURN_INTERFACE(pszIID, PDMICHARCONNECTOR, &pThis->ICharConnector);
99 return NULL;
100}
101
102
103/* -=-=-=-=- ICharConnector -=-=-=-=- */
104
105/** @copydoc PDMICHARCONNECTOR::pfnWrite */
106static DECLCALLBACK(int) drvCharWrite(PPDMICHARCONNECTOR pInterface, const void *pvBuf, size_t cbWrite)
107{
108 PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface);
109 const char *pbBuffer = (const char *)pvBuf;
110
111 LogFlow(("%s: pvBuf=%#p cbWrite=%d\n", __FUNCTION__, pvBuf, cbWrite));
112
113 for (uint32_t i = 0; i < cbWrite; i++)
114 {
115 if (ASMAtomicXchgBool(&pThis->fSending, true))
116 return VERR_BUFFER_OVERFLOW;
117
118 pThis->u8SendByte = pbBuffer[i];
119 RTSemEventSignal(pThis->SendSem);
120 STAM_COUNTER_INC(&pThis->StatBytesWritten);
121 }
122 return VINF_SUCCESS;
123}
124
125/** @copydoc PDMICHARCONNECTOR::pfnSetParameters */
126static DECLCALLBACK(int) drvCharSetParameters(PPDMICHARCONNECTOR pInterface, unsigned Bps, char chParity, unsigned cDataBits, unsigned cStopBits)
127{
128 /*PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface); - unused*/
129
130 LogFlow(("%s: Bps=%u chParity=%c cDataBits=%u cStopBits=%u\n", __FUNCTION__, Bps, chParity, cDataBits, cStopBits));
131 return VINF_SUCCESS;
132}
133
134
135/* -=-=-=-=- receive thread -=-=-=-=- */
136
137/**
138 * Send thread loop - pushes data down thru the driver chain.
139 *
140 * @returns 0 on success.
141 * @param ThreadSelf Thread handle to this thread.
142 * @param pvUser User argument.
143 */
144static DECLCALLBACK(int) drvCharSendLoop(RTTHREAD ThreadSelf, void *pvUser)
145{
146 PDRVCHAR pThis = (PDRVCHAR)pvUser;
147
148 int rc = VINF_SUCCESS;
149 while (!pThis->fShutdown)
150 {
151 RTMSINTERVAL cMillies = (rc == VERR_TIMEOUT) ? 50 : RT_INDEFINITE_WAIT;
152 rc = RTSemEventWait(pThis->SendSem, cMillies);
153 if ( RT_FAILURE(rc)
154 && rc != VERR_TIMEOUT)
155 break;
156
157 /*
158 * Write the character to the attached stream (if present).
159 */
160 if ( pThis->fShutdown
161 || !pThis->pDrvStream)
162 break;
163
164 size_t cbProcessed = 1;
165 uint8_t ch = pThis->u8SendByte;
166 rc = pThis->pDrvStream->pfnWrite(pThis->pDrvStream, &ch, &cbProcessed);
167 if (RT_SUCCESS(rc))
168 {
169 ASMAtomicXchgBool(&pThis->fSending, false);
170 Assert(cbProcessed == 1);
171 }
172 else if (rc == VERR_TIMEOUT)
173 {
174 /* Normal case, just means that the stream didn't accept a new
175 * character before the timeout elapsed. Just retry. */
176 rc = VINF_SUCCESS;
177 }
178 else
179 {
180 LogRel(("Write failed with %Rrc; skipping\n", rc));
181 break;
182 }
183 }
184
185 return VINF_SUCCESS;
186}
187
188/* -=-=-=-=- receive thread -=-=-=-=- */
189
190/**
191 * Receive thread loop.
192 *
193 * @returns 0 on success.
194 * @param ThreadSelf Thread handle to this thread.
195 * @param pvUser User argument.
196 */
197static DECLCALLBACK(int) drvCharReceiveLoop(RTTHREAD ThreadSelf, void *pvUser)
198{
199 PDRVCHAR pThis = (PDRVCHAR)pvUser;
200 char abBuffer[256];
201 char *pbRemaining = abBuffer;
202 size_t cbRemaining = 0;
203 int rc;
204
205 while (!pThis->fShutdown)
206 {
207 if (!cbRemaining)
208 {
209 /* Get block of data from stream driver. */
210 if (pThis->pDrvStream)
211 {
212 pbRemaining = abBuffer;
213 cbRemaining = sizeof(abBuffer);
214 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, abBuffer, &cbRemaining);
215 if (RT_FAILURE(rc))
216 {
217 LogFlow(("Read failed with %Rrc\n", rc));
218 break;
219 }
220 }
221 else
222 RTThreadSleep(100);
223 }
224 else
225 {
226 /* Send data to guest. */
227 size_t cbProcessed = cbRemaining;
228 rc = pThis->pDrvCharPort->pfnNotifyRead(pThis->pDrvCharPort, pbRemaining, &cbProcessed);
229 if (RT_SUCCESS(rc))
230 {
231 Assert(cbProcessed);
232 pbRemaining += cbProcessed;
233 cbRemaining -= cbProcessed;
234 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbProcessed);
235 }
236 else if (rc == VERR_TIMEOUT)
237 {
238 /* Normal case, just means that the guest didn't accept a new
239 * character before the timeout elapsed. Just retry. */
240 rc = VINF_SUCCESS;
241 }
242 else
243 {
244 LogFlow(("NotifyRead failed with %Rrc\n", rc));
245 break;
246 }
247 }
248 }
249
250 return VINF_SUCCESS;
251}
252
253/**
254 * Set the modem lines.
255 *
256 * @returns VBox status code
257 * @param pInterface Pointer to the interface structure.
258 * @param RequestToSend Set to true if this control line should be made active.
259 * @param DataTerminalReady Set to true if this control line should be made active.
260 */
261static DECLCALLBACK(int) drvCharSetModemLines(PPDMICHARCONNECTOR pInterface, bool RequestToSend, bool DataTerminalReady)
262{
263 /* Nothing to do here. */
264 return VINF_SUCCESS;
265}
266
267/**
268 * Sets the TD line into break condition.
269 *
270 * @returns VBox status code.
271 * @param pInterface Pointer to the interface structure containing the called function pointer.
272 * @param fBreak Set to true to let the device send a break false to put into normal operation.
273 * @thread Any thread.
274 */
275static DECLCALLBACK(int) drvCharSetBreak(PPDMICHARCONNECTOR pInterface, bool fBreak)
276{
277 /* Nothing to do here. */
278 return VINF_SUCCESS;
279}
280
281/* -=-=-=-=- driver interface -=-=-=-=- */
282
283/**
284 * Destruct a char driver instance.
285 *
286 * Most VM resources are freed by the VM. This callback is provided so that
287 * any non-VM resources can be freed correctly.
288 *
289 * @param pDrvIns The driver instance data.
290 */
291static DECLCALLBACK(void) drvCharDestruct(PPDMDRVINS pDrvIns)
292{
293 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
294 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
295 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
296
297 /*
298 * Tell the threads to shut down.
299 */
300 pThis->fShutdown = true;
301 if (pThis->SendSem != NIL_RTSEMEVENT)
302 {
303 RTSemEventSignal(pThis->SendSem);
304 RTSemEventDestroy(pThis->SendSem);
305 pThis->SendSem = NIL_RTSEMEVENT;
306 }
307
308 /*
309 * Wait for the threads.
310 * ASSUMES that PDM destroys the driver chain from the the bottom and up.
311 */
312 if (pThis->ReceiveThread != NIL_RTTHREAD)
313 {
314 int rc = RTThreadWait(pThis->ReceiveThread, 30000, NULL);
315 if (RT_SUCCESS(rc))
316 pThis->ReceiveThread = NIL_RTTHREAD;
317 else
318 LogRel(("Char%d: receive thread did not terminate (%Rrc)\n", pDrvIns->iInstance, rc));
319 }
320
321 if (pThis->SendThread != NIL_RTTHREAD)
322 {
323 int rc = RTThreadWait(pThis->SendThread, 30000, NULL);
324 if (RT_SUCCESS(rc))
325 pThis->SendThread = NIL_RTTHREAD;
326 else
327 LogRel(("Char%d: send thread did not terminate (%Rrc)\n", pDrvIns->iInstance, rc));
328 }
329}
330
331
332/**
333 * Construct a char driver instance.
334 *
335 * @copydoc FNPDMDRVCONSTRUCT
336 */
337static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
338{
339 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
340 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
341 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
342
343 /*
344 * Init basic data members and interfaces.
345 */
346 pThis->fShutdown = false;
347 pThis->ReceiveThread = NIL_RTTHREAD;
348 pThis->SendThread = NIL_RTTHREAD;
349 pThis->SendSem = NIL_RTSEMEVENT;
350 /* IBase. */
351 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
352 /* ICharConnector. */
353 pThis->ICharConnector.pfnWrite = drvCharWrite;
354 pThis->ICharConnector.pfnSetParameters = drvCharSetParameters;
355 pThis->ICharConnector.pfnSetModemLines = drvCharSetModemLines;
356 pThis->ICharConnector.pfnSetBreak = drvCharSetBreak;
357
358 /*
359 * Get the ICharPort interface of the above driver/device.
360 */
361 pThis->pDrvCharPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMICHARPORT);
362 if (!pThis->pDrvCharPort)
363 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Char#%d has no char port interface above"), pDrvIns->iInstance);
364
365 /*
366 * Attach driver below and query its stream interface.
367 */
368 PPDMIBASE pBase;
369 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pBase);
370 if (RT_FAILURE(rc))
371 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
372 pThis->pDrvStream = PDMIBASE_QUERY_INTERFACE(pBase, PDMISTREAM);
373 if (!pThis->pDrvStream)
374 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS, N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
375
376 /*
377 * Don't start the receive thread if the driver doesn't support reading
378 */
379 if (pThis->pDrvStream->pfnRead)
380 {
381 rc = RTThreadCreate(&pThis->ReceiveThread, drvCharReceiveLoop, (void *)pThis, 0,
382 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharRecv");
383 if (RT_FAILURE(rc))
384 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create receive thread"), pDrvIns->iInstance);
385 }
386
387 rc = RTSemEventCreate(&pThis->SendSem);
388 AssertRCReturn(rc, rc);
389
390 rc = RTThreadCreate(&pThis->SendThread, drvCharSendLoop, (void *)pThis, 0,
391 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharSend");
392 if (RT_FAILURE(rc))
393 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create send thread"), pDrvIns->iInstance);
394
395
396 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
397 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
398
399 return VINF_SUCCESS;
400}
401
402
403/**
404 * Char driver registration record.
405 */
406const PDMDRVREG g_DrvChar =
407{
408 /* u32Version */
409 PDM_DRVREG_VERSION,
410 /* szName */
411 "Char",
412 /* szRCMod */
413 "",
414 /* szR0Mod */
415 "",
416 /* pszDescription */
417 "Generic char driver.",
418 /* fFlags */
419 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
420 /* fClass. */
421 PDM_DRVREG_CLASS_CHAR,
422 /* cMaxInstances */
423 ~0,
424 /* cbInstance */
425 sizeof(DRVCHAR),
426 /* pfnConstruct */
427 drvCharConstruct,
428 /* pfnDestruct */
429 drvCharDestruct,
430 /* pfnRelocate */
431 NULL,
432 /* pfnIOCtl */
433 NULL,
434 /* pfnPowerOn */
435 NULL,
436 /* pfnReset */
437 NULL,
438 /* pfnSuspend */
439 NULL,
440 /* pfnResume */
441 NULL,
442 /* pfnAttach */
443 NULL,
444 /* pfnDetach */
445 NULL,
446 /* pfnPowerOff */
447 NULL,
448 /* pfnSoftReset */
449 NULL,
450 /* u32EndVersion */
451 PDM_DRVREG_VERSION
452};
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