VirtualBox

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

Last change on this file since 1765 was 1765, checked in by vboxsync, 18 years ago

Doesn't work.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette