VirtualBox

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

Last change on this file since 21205 was 19624, checked in by vboxsync, 15 years ago

added raw file serial driver

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/** @file
2 *
3 * VBox stream I/O devices:
4 * Generic char driver
5 */
6
7/*
8 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
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
35#include "Builtins.h"
36
37
38/** Size of the send fifo queue (in bytes) */
39#define CHAR_MAX_SEND_QUEUE 0x80
40#define CHAR_MAX_SEND_QUEUE_MASK 0x7f
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45
46/**
47 * Char driver instance data.
48 */
49typedef struct DRVCHAR
50{
51 /** Pointer to the driver instance structure. */
52 PPDMDRVINS pDrvIns;
53 /** Pointer to the char port interface of the driver/device above us. */
54 PPDMICHARPORT pDrvCharPort;
55 /** Pointer to the stream interface of the driver below us. */
56 PPDMISTREAM pDrvStream;
57 /** Our char interface. */
58 PDMICHAR IChar;
59 /** Flag to notify the receive thread it should terminate. */
60 volatile bool fShutdown;
61 /** Receive thread ID. */
62 RTTHREAD ReceiveThread;
63 /** Send thread ID. */
64 RTTHREAD SendThread;
65 /** Send event semephore */
66 RTSEMEVENT SendSem;
67
68 /** Internal send FIFO queue */
69 uint8_t aSendQueue[CHAR_MAX_SEND_QUEUE];
70 uint32_t iSendQueueHead;
71 uint32_t iSendQueueTail;
72
73 /** Read/write statistics */
74 STAMCOUNTER StatBytesRead;
75 STAMCOUNTER StatBytesWritten;
76} DRVCHAR, *PDRVCHAR;
77
78
79/** Converts a pointer to DRVCHAR::IChar to a PDRVCHAR. */
80#define PDMICHAR_2_DRVCHAR(pInterface) ( (PDRVCHAR)((uintptr_t)pInterface - RT_OFFSETOF(DRVCHAR, IChar)) )
81
82
83/* -=-=-=-=- IBase -=-=-=-=- */
84
85/**
86 * Queries an interface to the driver.
87 *
88 * @returns Pointer to interface.
89 * @returns NULL if the interface was not supported by the driver.
90 * @param pInterface Pointer to this interface structure.
91 * @param enmInterface The requested interface identification.
92 */
93static DECLCALLBACK(void *) drvCharQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
94{
95 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
96 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
97 switch (enmInterface)
98 {
99 case PDMINTERFACE_BASE:
100 return &pDrvIns->IBase;
101 case PDMINTERFACE_CHAR:
102 return &pThis->IChar;
103 default:
104 return NULL;
105 }
106}
107
108
109/* -=-=-=-=- IChar -=-=-=-=- */
110
111/** @copydoc PDMICHAR::pfnWrite */
112static DECLCALLBACK(int) drvCharWrite(PPDMICHAR pInterface, const void *pvBuf, size_t cbWrite)
113{
114 PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface);
115 const char *pBuffer = (const char *)pvBuf;
116
117 LogFlow(("%s: pvBuf=%#p cbWrite=%d\n", __FUNCTION__, pvBuf, cbWrite));
118
119 for (uint32_t i=0;i<cbWrite;i++)
120 {
121 uint32_t idx = pThis->iSendQueueHead;
122
123 pThis->aSendQueue[idx] = pBuffer[i];
124 idx = (idx + 1) & CHAR_MAX_SEND_QUEUE_MASK;
125
126 STAM_COUNTER_INC(&pThis->StatBytesWritten);
127 ASMAtomicXchgU32(&pThis->iSendQueueHead, idx);
128 }
129 RTSemEventSignal(pThis->SendSem);
130 return VINF_SUCCESS;
131}
132
133/** @copydoc PDMICHAR::pfnSetParameters */
134static DECLCALLBACK(int) drvCharSetParameters(PPDMICHAR pInterface, unsigned Bps, char chParity, unsigned cDataBits, unsigned cStopBits)
135{
136 /*PDRVCHAR pThis = PDMICHAR_2_DRVCHAR(pInterface); - unused*/
137
138 LogFlow(("%s: Bps=%u chParity=%c cDataBits=%u cStopBits=%u\n", __FUNCTION__, Bps, chParity, cDataBits, cStopBits));
139 return VINF_SUCCESS;
140}
141
142
143/* -=-=-=-=- receive thread -=-=-=-=- */
144
145/**
146 * Send thread loop.
147 *
148 * @returns 0 on success.
149 * @param ThreadSelf Thread handle to this thread.
150 * @param pvUser User argument.
151 */
152static DECLCALLBACK(int) drvCharSendLoop(RTTHREAD ThreadSelf, void *pvUser)
153{
154 PDRVCHAR pThis = (PDRVCHAR)pvUser;
155
156 for(;;)
157 {
158 int rc = RTSemEventWait(pThis->SendSem, RT_INDEFINITE_WAIT);
159 if (RT_FAILURE(rc))
160 break;
161
162 /*
163 * Write the character to the attached stream (if present).
164 */
165 if ( !pThis->fShutdown
166 && pThis->pDrvStream)
167 {
168 while (pThis->iSendQueueTail != pThis->iSendQueueHead)
169 {
170 size_t cbProcessed = 1;
171
172 rc = pThis->pDrvStream->pfnWrite(pThis->pDrvStream, &pThis->aSendQueue[pThis->iSendQueueTail], &cbProcessed);
173 if (RT_SUCCESS(rc))
174 {
175 Assert(cbProcessed);
176 pThis->iSendQueueTail++;
177 pThis->iSendQueueTail &= CHAR_MAX_SEND_QUEUE_MASK;
178 }
179 else if (rc == VERR_TIMEOUT)
180 {
181 /* Normal case, just means that the stream didn't accept a new
182 * character before the timeout elapsed. Just retry. */
183 rc = VINF_SUCCESS;
184 }
185 else
186 {
187 LogFlow(("Write failed with %Rrc; skipping\n", rc));
188 break;
189 }
190 }
191 }
192 else
193 break;
194 }
195
196 pThis->SendThread = NIL_RTTHREAD;
197
198 return VINF_SUCCESS;
199}
200
201/* -=-=-=-=- receive thread -=-=-=-=- */
202
203/**
204 * Receive thread loop.
205 *
206 * @returns 0 on success.
207 * @param ThreadSelf Thread handle to this thread.
208 * @param pvUser User argument.
209 */
210static DECLCALLBACK(int) drvCharReceiveLoop(RTTHREAD ThreadSelf, void *pvUser)
211{
212 PDRVCHAR pThis = (PDRVCHAR)pvUser;
213 char aBuffer[256], *pBuffer;
214 size_t cbRemaining, cbProcessed;
215 int rc;
216
217 cbRemaining = 0;
218 pBuffer = aBuffer;
219 while (!pThis->fShutdown)
220 {
221 if (!cbRemaining)
222 {
223 /* Get block of data from stream driver. */
224 if (pThis->pDrvStream)
225 {
226 cbRemaining = sizeof(aBuffer);
227 rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, aBuffer, &cbRemaining);
228 if (RT_FAILURE(rc))
229 {
230 LogFlow(("Read failed with %Rrc\n", rc));
231 break;
232 }
233 }
234 else
235 {
236 cbRemaining = 0;
237 RTThreadSleep(100);
238 }
239 pBuffer = aBuffer;
240 }
241 else
242 {
243 /* Send data to guest. */
244 cbProcessed = cbRemaining;
245 rc = pThis->pDrvCharPort->pfnNotifyRead(pThis->pDrvCharPort, pBuffer, &cbProcessed);
246 if (RT_SUCCESS(rc))
247 {
248 Assert(cbProcessed);
249 pBuffer += cbProcessed;
250 cbRemaining -= cbProcessed;
251 STAM_COUNTER_ADD(&pThis->StatBytesRead, cbProcessed);
252 }
253 else if (rc == VERR_TIMEOUT)
254 {
255 /* Normal case, just means that the guest didn't accept a new
256 * character before the timeout elapsed. Just retry. */
257 rc = VINF_SUCCESS;
258 }
259 else
260 {
261 LogFlow(("NotifyRead failed with %Rrc\n", rc));
262 break;
263 }
264 }
265 }
266
267 pThis->ReceiveThread = NIL_RTTHREAD;
268
269 return VINF_SUCCESS;
270}
271
272/**
273 * Set the modem lines.
274 *
275 * @returns VBox status code
276 * @param pInterface Pointer to the interface structure.
277 * @param RequestToSend Set to true if this control line should be made active.
278 * @param DataTerminalReady Set to true if this control line should be made active.
279 */
280static DECLCALLBACK(int) drvCharSetModemLines(PPDMICHAR pInterface, bool RequestToSend, bool DataTerminalReady)
281{
282 /* Nothing to do here. */
283 return VINF_SUCCESS;
284}
285
286/* -=-=-=-=- driver interface -=-=-=-=- */
287
288/**
289 * Construct a char driver instance.
290 *
291 * @returns VBox status.
292 * @param pDrvIns The driver instance data.
293 * If the registration structure is needed,
294 * pDrvIns->pDrvReg points to it.
295 * @param pCfgHandle Configuration node handle for the driver. Use this to
296 * obtain the configuration of the driver instance. It's
297 * also found in pDrvIns->pCfgHandle as it's expected to
298 * be used frequently in this function.
299 */
300static DECLCALLBACK(int) drvCharConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
301{
302 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
303 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
304
305 /*
306 * Init basic data members and interfaces.
307 */
308 pThis->ReceiveThread = NIL_RTTHREAD;
309 pThis->fShutdown = false;
310 /* IBase. */
311 pDrvIns->IBase.pfnQueryInterface = drvCharQueryInterface;
312 /* IChar. */
313 pThis->IChar.pfnWrite = drvCharWrite;
314 pThis->IChar.pfnSetParameters = drvCharSetParameters;
315 pThis->IChar.pfnSetModemLines = drvCharSetModemLines;
316
317 /*
318 * Get the ICharPort interface of the above driver/device.
319 */
320 pThis->pDrvCharPort = (PPDMICHARPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_CHAR_PORT);
321 if (!pThis->pDrvCharPort)
322 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Char#%d has no char port interface above"), pDrvIns->iInstance);
323
324 /*
325 * Attach driver below and query its stream interface.
326 */
327 PPDMIBASE pBase;
328 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBase);
329 if (RT_FAILURE(rc))
330 return rc; /* Don't call PDMDrvHlpVMSetError here as we assume that the driver already set an appropriate error */
331 pThis->pDrvStream = (PPDMISTREAM)pBase->pfnQueryInterface(pBase, PDMINTERFACE_STREAM);
332 if (!pThis->pDrvStream)
333 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS, N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
334
335 /*
336 * Don't start the receive thread if the driver doesn't support reading
337 */
338 if (pThis->pDrvStream->pfnRead)
339 {
340 rc = RTThreadCreate(&pThis->ReceiveThread, drvCharReceiveLoop, (void *)pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharRecv");
341 if (RT_FAILURE(rc))
342 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create receive thread"), pDrvIns->iInstance);
343 }
344
345 rc = RTSemEventCreate(&pThis->SendSem);
346 AssertRC(rc);
347
348 rc = RTThreadCreate(&pThis->SendThread, drvCharSendLoop, (void *)pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "CharSend");
349 if (RT_FAILURE(rc))
350 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create send thread"), pDrvIns->iInstance);
351
352
353 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
354 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
355
356 return VINF_SUCCESS;
357}
358
359
360/**
361 * Destruct a char driver instance.
362 *
363 * Most VM resources are freed by the VM. This callback is provided so that
364 * any non-VM resources can be freed correctly.
365 *
366 * @param pDrvIns The driver instance data.
367 */
368static DECLCALLBACK(void) drvCharDestruct(PPDMDRVINS pDrvIns)
369{
370 PDRVCHAR pThis = PDMINS_2_DATA(pDrvIns, PDRVCHAR);
371
372 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
373
374 pThis->fShutdown = true;
375 if (pThis->ReceiveThread)
376 {
377 RTThreadWait(pThis->ReceiveThread, 1000, NULL);
378 if (pThis->ReceiveThread != NIL_RTTHREAD)
379 LogRel(("Char%d: receive thread did not terminate\n", pDrvIns->iInstance));
380 }
381
382 /* Empty the send queue */
383 pThis->iSendQueueTail = pThis->iSendQueueHead = 0;
384
385 RTSemEventSignal(pThis->SendSem);
386 RTSemEventDestroy(pThis->SendSem);
387 pThis->SendSem = NIL_RTSEMEVENT;
388
389 if (pThis->SendThread)
390 {
391 RTThreadWait(pThis->SendThread, 1000, NULL);
392 if (pThis->SendThread != NIL_RTTHREAD)
393 LogRel(("Char%d: send thread did not terminate\n", pDrvIns->iInstance));
394 }
395}
396
397/**
398 * Char driver registration record.
399 */
400const PDMDRVREG g_DrvChar =
401{
402 /* u32Version */
403 PDM_DRVREG_VERSION,
404 /* szDriverName */
405 "Char",
406 /* pszDescription */
407 "Generic char driver.",
408 /* fFlags */
409 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
410 /* fClass. */
411 PDM_DRVREG_CLASS_CHAR,
412 /* cMaxInstances */
413 ~0,
414 /* cbInstance */
415 sizeof(DRVCHAR),
416 /* pfnConstruct */
417 drvCharConstruct,
418 /* pfnDestruct */
419 drvCharDestruct,
420 /* pfnIOCtl */
421 NULL,
422 /* pfnPowerOn */
423 NULL,
424 /* pfnReset */
425 NULL,
426 /* pfnSuspend */
427 NULL,
428 /* pfnResume */
429 NULL,
430 /* pfnDetach */
431 NULL,
432 /** pfnPowerOff */
433 NULL
434};
435
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use