VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/solaris/VBoxUSBMon-solaris.c@ 34891

Last change on this file since 34891 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.9 KB
Line 
1/* $Id: VBoxUSBMon-solaris.c 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 * VirtualBox USB Monitor Driver, Solaris Hosts.
4 */
5
6/*
7 * Copyright (C) 2008 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* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_USB_DRV
22#include "VBoxUSBFilterMgr.h"
23#include <VBox/usblib-solaris.h>
24#include <VBox/version.h>
25#include <VBox/log.h>
26#include <VBox/cdefs.h>
27#include <VBox/types.h>
28#include <VBox/version.h>
29#include <iprt/assert.h>
30#include <iprt/string.h>
31#include <iprt/initterm.h>
32#include <iprt/process.h>
33#include <iprt/mem.h>
34#include <iprt/semaphore.h>
35#include <iprt/path.h>
36
37#define USBDRV_MAJOR_VER 2
38#define USBDRV_MINOR_VER 0
39#include <sys/usb/usba.h>
40#include <sys/conf.h>
41#include <sys/modctl.h>
42#include <sys/mutex.h>
43#include <sys/stat.h>
44#include <sys/ddi.h>
45#include <sys/sunddi.h>
46#include <sys/sunndi.h>
47#include <sys/open.h>
48#include <sys/cmn_err.h>
49
50
51/*******************************************************************************
52* Defined Constants And Macros *
53*******************************************************************************/
54/** The module name. */
55#define DEVICE_NAME "vboxusbmon"
56/** The module description as seen in 'modinfo'. */
57#define DEVICE_DESC_DRV "VirtualBox USBMon"
58
59#if defined(DEBUG_ramshankar)
60# undef LogFlowFunc
61# define LogFlowFunc LogRel
62# undef Log
63# define Log LogRel
64# undef LogFlow
65# define LogFlow LogRel
66#endif
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71static int VBoxUSBMonSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
72static int VBoxUSBMonSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
73static int VBoxUSBMonSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
74static int VBoxUSBMonSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
75static int VBoxUSBMonSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
76static int VBoxUSBMonSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
77static int VBoxUSBMonSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
78static int VBoxUSBMonSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
79
80/*******************************************************************************
81* Structures and Typedefs *
82*******************************************************************************/
83/**
84 * cb_ops: for drivers that support char/block entry points
85 */
86static struct cb_ops g_VBoxUSBMonSolarisCbOps =
87{
88 VBoxUSBMonSolarisOpen,
89 VBoxUSBMonSolarisClose,
90 nodev, /* b strategy */
91 nodev, /* b dump */
92 nodev, /* b print */
93 VBoxUSBMonSolarisRead,
94 VBoxUSBMonSolarisWrite,
95 VBoxUSBMonSolarisIOCtl,
96 nodev, /* c devmap */
97 nodev, /* c mmap */
98 nodev, /* c segmap */
99 nochpoll, /* c poll */
100 ddi_prop_op, /* property ops */
101 NULL, /* streamtab */
102 D_NEW | D_MP, /* compat. flag */
103 CB_REV /* revision */
104};
105
106/**
107 * dev_ops: for driver device operations
108 */
109static struct dev_ops g_VBoxUSBMonSolarisDevOps =
110{
111 DEVO_REV, /* driver build revision */
112 0, /* ref count */
113 VBoxUSBMonSolarisGetInfo,
114 nulldev, /* identify */
115 nulldev, /* probe */
116 VBoxUSBMonSolarisAttach,
117 VBoxUSBMonSolarisDetach,
118 nodev, /* reset */
119 &g_VBoxUSBMonSolarisCbOps,
120 (struct bus_ops *)0,
121 nodev /* power */
122};
123
124/**
125 * modldrv: export driver specifics to the kernel
126 */
127static struct modldrv g_VBoxUSBMonSolarisModule =
128{
129 &mod_driverops, /* extern from kernel */
130 DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
131 &g_VBoxUSBMonSolarisDevOps
132};
133
134/**
135 * modlinkage: export install/remove/info to the kernel
136 */
137static struct modlinkage g_VBoxUSBMonSolarisModLinkage =
138{
139 MODREV_1,
140 &g_VBoxUSBMonSolarisModule,
141 NULL,
142};
143
144/**
145 * Client driver info.
146 */
147typedef struct vboxusbmon_client_t
148{
149 dev_info_t *pDip; /* Client device info. pointer */
150 VBOXUSB_CLIENT_INFO Info; /* Client registration data. */
151 struct vboxusbmon_client_t *pNext; /* Pointer to next client */
152} vboxusbmon_client_t;
153
154/**
155 * Device state info.
156 */
157typedef struct
158{
159 RTPROCESS Process; /* The process (id) of the session */
160} vboxusbmon_state_t;
161
162
163/*******************************************************************************
164* Global Variables *
165*******************************************************************************/
166/** Global Device handle we only support one instance. */
167static dev_info_t *g_pDip;
168/** Global Mutex. */
169static kmutex_t g_VBoxUSBMonSolarisMtx;
170/** Number of userland clients that have kept us open. */
171static uint64_t g_cVBoxUSBMonSolarisClient = 0;
172/** Global list of client drivers registered with us. */
173vboxusbmon_client_t *g_pVBoxUSBMonSolarisClients = 0;
174/** Opaque pointer to list of soft states. */
175static void *g_pVBoxUSBMonSolarisState;
176
177/*******************************************************************************
178* Internal Functions *
179*******************************************************************************/
180static int vboxUSBMonSolarisProcessIOCtl(int iFunction, void *pvState, void *pvData, size_t cbData, size_t *pcbReturnedData);
181static int vboxUSBMonSolarisResetDevice(char *pszDevicePath, bool fReattach);
182
183
184/*******************************************************************************
185* Monitor Global Hooks *
186*******************************************************************************/
187static int vboxUSBMonSolarisClientInfo(vboxusbmon_state_t *pState, PVBOXUSB_CLIENT_INFO pClientInfo);
188int VBoxUSBMonSolarisRegisterClient(dev_info_t *pClientDip, PVBOXUSB_CLIENT_INFO pClientInfo);
189int VBoxUSBMonSolarisUnregisterClient(dev_info_t *pClientDip);
190int VBoxUSBMonSolarisElectDriver(usb_dev_descr_t *pDevDesc, usb_dev_str_t *pDevStrings, char *pszDevicePath, int Bus, int Port,
191 char **ppszDrv, void *pvReserved);
192
193
194/**
195 * Kernel entry points
196 */
197int _init(void)
198{
199 int rc;
200
201 LogFlowFunc((DEVICE_NAME ":_init\n"));
202
203 g_pDip = NULL;
204
205 /*
206 * Prevent module autounloading.
207 */
208 modctl_t *pModCtl = mod_getctl(&g_VBoxUSBMonSolarisModLinkage);
209 if (pModCtl)
210 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
211 else
212 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
213
214 /*
215 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
216 */
217 rc = RTR0Init(0);
218 if (RT_SUCCESS(rc))
219 {
220 /*
221 * Initialize global mutex.
222 */
223 mutex_init(&g_VBoxUSBMonSolarisMtx, NULL, MUTEX_DRIVER, NULL);
224 rc = ddi_soft_state_init(&g_pVBoxUSBMonSolarisState, sizeof(vboxusbmon_state_t), 1);
225 if (!rc)
226 {
227 rc = mod_install(&g_VBoxUSBMonSolarisModLinkage);
228 if (!rc)
229 return rc;
230
231 LogRel((DEVICE_NAME ":mod_install failed! rc=%d\n", rc));
232 ddi_soft_state_fini(&g_pVBoxUSBMonSolarisState);
233 }
234 else
235 LogRel((DEVICE_NAME ":ddi_soft_state_init failed! rc=%d\n", rc));
236
237 mutex_destroy(&g_VBoxUSBMonSolarisMtx);
238 RTR0Term();
239 }
240 else
241 LogRel((DEVICE_NAME ":RTR0Init failed! rc=%d\n", rc));
242
243 return -1;
244}
245
246
247int _fini(void)
248{
249 int rc;
250
251 LogFlowFunc((DEVICE_NAME ":_fini\n"));
252
253 rc = mod_remove(&g_VBoxUSBMonSolarisModLinkage);
254 if (!rc)
255 {
256 ddi_soft_state_fini(&g_pVBoxUSBMonSolarisState);
257 mutex_destroy(&g_VBoxUSBMonSolarisMtx);
258
259 RTR0Term();
260 }
261 return rc;
262}
263
264
265int _info(struct modinfo *pModInfo)
266{
267 LogFlowFunc((DEVICE_NAME ":_info\n"));
268
269 return mod_info(&g_VBoxUSBMonSolarisModLinkage, pModInfo);
270}
271
272
273/**
274 * Attach entry point, to attach a device to the system or resume it.
275 *
276 * @param pDip The module structure instance.
277 * @param enmCmd Attach type (ddi_attach_cmd_t)
278 *
279 * @returns corresponding solaris error code.
280 */
281static int VBoxUSBMonSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
282{
283 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisAttach pDip=%p enmCmd=%d\n", pDip, enmCmd));
284 switch (enmCmd)
285 {
286 case DDI_ATTACH:
287 {
288 vboxusbmon_state_t *pState = NULL;
289 int instance = ddi_get_instance(pDip);
290 int rc;
291
292 pState = RTMemAllocZ(sizeof(*pState));
293 if (pState)
294 {
295 g_pDip = pDip;
296 rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0,
297 "none", "none", 0666);
298 if (rc == DDI_SUCCESS)
299 {
300 ddi_set_driver_private(pDip, pState);
301 ddi_report_dev(pDip);
302 return rc;
303 }
304 else
305 LogRel((DEVICE_NAME ":ddi_create_minor_node failed! rc=%d\n", rc));
306 RTMemFree(pState);
307 }
308 else
309 LogRel((DEVICE_NAME ":RTMemAllocZ failed to allocated %d bytes for pState\n", sizeof(*pState)));
310 return DDI_FAILURE;
311 }
312
313 case DDI_RESUME:
314 {
315 /* We don't have to bother about power management. */
316 return DDI_SUCCESS;
317 }
318
319 default:
320 return DDI_FAILURE;
321 }
322}
323
324
325/**
326 * Detach entry point, to detach a device to the system or suspend it.
327 *
328 * @param pDip The module structure instance.
329 * @param enmCmd Attach type (ddi_attach_cmd_t)
330 *
331 * @returns corresponding solaris error code.
332 */
333static int VBoxUSBMonSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
334{
335 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisDetach\n"));
336
337 switch (enmCmd)
338 {
339 case DDI_DETACH:
340 {
341 /*
342 * Free all registered clients' info.
343 */
344 mutex_enter(&g_VBoxUSBMonSolarisMtx);
345 vboxusbmon_client_t *pCur = g_pVBoxUSBMonSolarisClients;
346 while (pCur)
347 {
348 vboxusbmon_client_t *pNext = pCur->pNext;
349 RTMemFree(pCur);
350 pCur = pNext;
351 }
352 mutex_exit(&g_VBoxUSBMonSolarisMtx);
353
354 vboxusbmon_state_t *pState = ddi_get_driver_private(g_pDip);
355 if (pState)
356 {
357 ddi_remove_minor_node(pDip, NULL);
358 RTMemFree(pState);
359 return DDI_SUCCESS;
360 }
361 else
362 LogRel((DEVICE_NAME ":failed to get soft state on detach.\n"));
363 break;
364 }
365
366 case DDI_SUSPEND:
367 {
368 /* We don't have to bother about power management. */
369 return DDI_SUCCESS;
370 }
371
372 default:
373 return DDI_FAILURE;
374 }
375}
376
377
378/**
379 * Info entry point, called by solaris kernel for obtaining driver info.
380 *
381 * @param pDip The module structure instance (do not use).
382 * @param enmCmd Information request type.
383 * @param pvArg Type specific argument.
384 * @param ppvResult Where to store the requested info.
385 *
386 * @returns corresponding solaris error code.
387 */
388static int VBoxUSBMonSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
389{
390 int rc = DDI_SUCCESS;
391
392 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisGetInfo\n"));
393
394 switch (enmCmd)
395 {
396 case DDI_INFO_DEVT2DEVINFO:
397 *ppvResult = (void *)g_pDip;
398 break;
399
400 case DDI_INFO_DEVT2INSTANCE:
401 *ppvResult = (void *)(uintptr_t)ddi_get_instance(g_pDip);
402 break;
403
404 default:
405 rc = DDI_FAILURE;
406 break;
407 }
408 return rc;
409}
410
411
412static int VBoxUSBMonSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
413{
414 vboxusbmon_state_t *pState = NULL;
415 unsigned iOpenInstance;
416
417 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisOpen\n"));
418
419 /*
420 * Verify we are being opened as a character device
421 */
422 if (fType != OTYP_CHR)
423 return EINVAL;
424
425 /*
426 * Verify that we're called after attach
427 */
428 if (!g_pDip)
429 {
430 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisOpen invalid state for opening.\n"));
431 return ENXIO;
432 }
433
434 mutex_enter(&g_VBoxUSBMonSolarisMtx);
435 if (!g_cVBoxUSBMonSolarisClient)
436 {
437 mutex_exit(&g_VBoxUSBMonSolarisMtx);
438 int rc = usb_register_dev_driver(g_pDip, VBoxUSBMonSolarisElectDriver);
439 if (RT_UNLIKELY(rc != DDI_SUCCESS))
440 {
441 LogRel((DEVICE_NAME ":Failed to register driver election callback with USBA rc=%d\n", rc));
442 return EINVAL;
443 }
444 LogFlow((DEVICE_NAME ":Successfully registered election callback with USBA\n"));
445 mutex_enter(&g_VBoxUSBMonSolarisMtx);
446 }
447 g_cVBoxUSBMonSolarisClient++;
448 mutex_exit(&g_VBoxUSBMonSolarisMtx);
449
450 for (iOpenInstance = 0; iOpenInstance < 4096; iOpenInstance++)
451 {
452 if ( !ddi_get_soft_state(g_pVBoxUSBMonSolarisState, iOpenInstance) /* faster */
453 && ddi_soft_state_zalloc(g_pVBoxUSBMonSolarisState, iOpenInstance) == DDI_SUCCESS)
454 {
455 pState = ddi_get_soft_state(g_pVBoxUSBMonSolarisState, iOpenInstance);
456 break;
457 }
458 }
459 if (!pState)
460 {
461 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisOpen: too many open instances."));
462 mutex_enter(&g_VBoxUSBMonSolarisMtx);
463 g_cVBoxUSBMonSolarisClient--;
464 mutex_exit(&g_VBoxUSBMonSolarisMtx);
465 return ENXIO;
466 }
467
468 pState->Process = RTProcSelf();
469 *pDev = makedevice(getmajor(*pDev), iOpenInstance);
470
471 NOREF(fFlag);
472 NOREF(pCred);
473
474 return 0;
475}
476
477
478static int VBoxUSBMonSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred)
479{
480 vboxusbmon_state_t *pState = NULL;
481
482 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisClose\n"));
483
484 pState = ddi_get_soft_state(g_pVBoxUSBMonSolarisState, getminor(Dev));
485 if (!pState)
486 {
487 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisClose: failed to get pState.\n"));
488 return EFAULT;
489 }
490
491 mutex_enter(&g_VBoxUSBMonSolarisMtx);
492 g_cVBoxUSBMonSolarisClient--;
493 if (!g_cVBoxUSBMonSolarisClient)
494 {
495 if (RT_LIKELY(g_pDip))
496 {
497 mutex_exit(&g_VBoxUSBMonSolarisMtx);
498 usb_unregister_dev_driver(g_pDip);
499 LogFlow((DEVICE_NAME ":Successfully deregistered driver election callback\n"));
500 }
501 else
502 {
503 mutex_exit(&g_VBoxUSBMonSolarisMtx);
504 LogRel((DEVICE_NAME ":Extreme error! Missing device info during close.\n"));
505 }
506 }
507 else
508 mutex_exit(&g_VBoxUSBMonSolarisMtx);
509
510 /*
511 * Remove all filters for this client process.
512 */
513 VBoxUSBFilterRemoveOwner(pState->Process);
514
515 ddi_soft_state_free(g_pVBoxUSBMonSolarisState, getminor(Dev));
516 pState = NULL;
517
518 NOREF(fFlag);
519 NOREF(fType);
520 NOREF(pCred);
521
522 return 0;
523}
524
525
526static int VBoxUSBMonSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
527{
528 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisRead\n"));
529 return 0;
530}
531
532
533static int VBoxUSBMonSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
534{
535 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisWrite\n"));
536 return 0;
537}
538
539
540/** @def IOCPARM_LEN
541 * Gets the length from the ioctl number.
542 * This is normally defined by sys/ioccom.h on BSD systems...
543 */
544#ifndef IOCPARM_LEN
545# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
546#endif
547
548static int VBoxUSBMonSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
549{
550 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl Dev=%d Cmd=%d pArg=%p Mode=%d\n", Dev, Cmd, pArg));
551
552 /*
553 * Get the session from the soft state item.
554 */
555 vboxusbmon_state_t *pState = ddi_get_soft_state(g_pVBoxUSBMonSolarisState, getminor(Dev));
556 if (!pState)
557 {
558 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: no state data for %d\n", getminor(Dev)));
559 return EINVAL;
560 }
561
562 /*
563 * Read the request wrapper. Though We don't really need wrapper struct. now
564 * it's room for the future as Solaris isn't generous regarding the size.
565 */
566 VBOXUSBREQ ReqWrap;
567 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
568 {
569 LogRel((DEVICE_NAME ": VBoxUSBMonSolarisIOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd), sizeof(ReqWrap)));
570 return ENOTTY;
571 }
572
573 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
574 if (RT_UNLIKELY(rc))
575 {
576 LogRel((DEVICE_NAME ": VBoxUSBMonSolarisIOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%d.\n", pArg, Cmd, rc));
577 return EINVAL;
578 }
579
580 if (ReqWrap.u32Magic != VBOXUSBMON_MAGIC)
581 {
582 LogRel((DEVICE_NAME ": VBoxUSBMonSolarisIOCtl: bad magic %#x; pArg=%p Cmd=%d.\n", ReqWrap.u32Magic, pArg, Cmd));
583 return EINVAL;
584 }
585 if (RT_UNLIKELY( ReqWrap.cbData == 0
586 || ReqWrap.cbData > _1M*16))
587 {
588 LogRel((DEVICE_NAME ": VBoxUSBMonSolarisIOCtl: bad size %#x; pArg=%p Cmd=%d.\n", ReqWrap.cbData, pArg, Cmd));
589 return EINVAL;
590 }
591
592 /*
593 * Read the request.
594 */
595 void *pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
596 if (RT_UNLIKELY(!pvBuf))
597 {
598 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
599 return ENOMEM;
600 }
601
602 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
603 if (RT_UNLIKELY(rc))
604 {
605 RTMemTmpFree(pvBuf);
606 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
607 return EFAULT;
608 }
609 if (RT_UNLIKELY( ReqWrap.cbData != 0
610 && !VALID_PTR(pvBuf)))
611 {
612 RTMemTmpFree(pvBuf);
613 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: pvBuf invalid pointer %p\n", pvBuf));
614 return EINVAL;
615 }
616 LogFlow((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: pid=%d.\n", (int)RTProcSelf()));
617
618 /*
619 * Process the IOCtl.
620 */
621 size_t cbDataReturned;
622 rc = vboxUSBMonSolarisProcessIOCtl(Cmd, pState, pvBuf, ReqWrap.cbData, &cbDataReturned);
623 ReqWrap.rc = rc;
624 rc = 0;
625
626 if (RT_UNLIKELY(cbDataReturned > ReqWrap.cbData))
627 {
628 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: too much output data %d expected %d\n", cbDataReturned, ReqWrap.cbData));
629 cbDataReturned = ReqWrap.cbData;
630 }
631
632 ReqWrap.cbData = cbDataReturned;
633
634 /*
635 * Copy the request back to user space.
636 */
637 rc = ddi_copyout(&ReqWrap, (void *)pArg, sizeof(ReqWrap), Mode);
638 if (RT_LIKELY(!rc))
639 {
640 /*
641 * Copy the payload (if any) back to user space.
642 */
643 if (cbDataReturned > 0)
644 {
645 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataReturned, Mode);
646 if (RT_UNLIKELY(rc))
647 {
648 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: ddi_copyout failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
649 rc = EFAULT;
650 }
651 }
652 }
653 else
654 {
655 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisIOCtl: ddi_copyout(1) failed pArg=%p Cmd=%d\n", pArg, Cmd));
656 rc = EFAULT;
657 }
658
659 *pVal = rc;
660 RTMemTmpFree(pvBuf);
661 return rc;
662}
663
664
665/**
666 * IOCtl processor for user to kernel and kernel to kernel communication.
667 *
668 * @returns VBox status code.
669 *
670 * @param iFunction The requested function.
671 * @param pvState Opaque pointer to driver state used for getting ring-3 process (Id).
672 * @param pvData The input/output data buffer. Can be NULL depending on the function.
673 * @param cbData The max size of the data buffer.
674 * @param pcbDataReturned Where to store the amount of returned data. Can be NULL.
675 */
676static int vboxUSBMonSolarisProcessIOCtl(int iFunction, void *pvState, void *pvData, size_t cbData, size_t *pcbReturnedData)
677{
678 LogFlowFunc((DEVICE_NAME ":solarisUSBProcessIOCtl iFunction=%d pvBuf=%p cbBuf=%zu\n", iFunction, pvData, cbData));
679
680 AssertPtrReturn(pvState, VERR_INVALID_POINTER);
681 vboxusbmon_state_t *pState = (vboxusbmon_state_t *)pvState;
682 int rc;
683
684#define CHECKRET_MIN_SIZE(mnemonic, cbMin) \
685 do { \
686 if (cbData < (cbMin)) \
687 { \
688 LogRel(("vboxUSBSolarisProcessIOCtl: " mnemonic ": cbData=%#zx (%zu) min is %#zx (%zu)\n", \
689 cbData, cbData, (size_t)(cbMin), (size_t)(cbMin))); \
690 return VERR_BUFFER_OVERFLOW; \
691 } \
692 if ((cbMin) != 0 && !VALID_PTR(pvData)) \
693 { \
694 LogRel(("vboxUSBSolarisProcessIOCtl: " mnemonic ": Invalid pointer %p\n", pvData)); \
695 return VERR_INVALID_POINTER; \
696 } \
697 } while (0)
698
699 switch (iFunction)
700 {
701 case VBOXUSBMON_IOCTL_ADD_FILTER:
702 {
703 CHECKRET_MIN_SIZE("ADD_FILTER", sizeof(VBOXUSBREQ_ADD_FILTER));
704
705 VBOXUSBREQ_ADD_FILTER *pReq = (VBOXUSBREQ_ADD_FILTER *)pvData;
706 PUSBFILTER pFilter = (PUSBFILTER)&pReq->Filter;
707
708 LogFlow(("vboxUSBMonSolarisProcessIOCtl: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x\n",
709 USBFilterGetNum(pFilter, USBFILTERIDX_VENDOR_ID),
710 USBFilterGetNum(pFilter, USBFILTERIDX_PRODUCT_ID),
711 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_REV),
712 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_CLASS),
713 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS),
714 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_PROTOCOL),
715 USBFilterGetNum(pFilter, USBFILTERIDX_BUS),
716 USBFilterGetNum(pFilter, USBFILTERIDX_PORT)));
717 LogFlow(("vboxUSBMonSolarisProcessIOCtl: Manufacturer=%s Product=%s Serial=%s\n",
718 USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
719 USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) : "<null>",
720 USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
721
722 rc = USBFilterSetMustBePresent(pFilter, USBFILTERIDX_BUS, false /* fMustBePresent */); AssertRC(rc);
723
724 rc = VBoxUSBFilterAdd(pFilter, pState->Process, &pReq->uId);
725 *pcbReturnedData = cbData;
726 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: ADD_FILTER (Process:%d) returned %d\n", pState->Process, rc));
727 break;
728 }
729
730 case VBOXUSBMON_IOCTL_REMOVE_FILTER:
731 {
732 CHECKRET_MIN_SIZE("REMOVE_FILTER", sizeof(VBOXUSBREQ_REMOVE_FILTER));
733
734 VBOXUSBREQ_REMOVE_FILTER *pReq = (VBOXUSBREQ_REMOVE_FILTER *)pvData;
735 rc = VBoxUSBFilterRemove(pState->Process, (uintptr_t)pReq->uId);
736 *pcbReturnedData = 0;
737 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: REMOVE_FILTER (Process:%d) returned %d\n", pState->Process, rc));
738 break;
739 }
740
741 case VBOXUSBMON_IOCTL_RESET_DEVICE:
742 {
743 CHECKRET_MIN_SIZE("RESET_DEVICE", sizeof(VBOXUSBREQ_RESET_DEVICE));
744
745 VBOXUSBREQ_RESET_DEVICE *pReq = (VBOXUSBREQ_RESET_DEVICE *)pvData;
746 rc = vboxUSBMonSolarisResetDevice(pReq->szDevicePath, pReq->fReattach);
747 *pcbReturnedData = 0;
748 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: RESET_DEVICE (Process:%d) returned %d\n", pState->Process, rc));
749 break;
750 }
751
752 case VBOXUSBMON_IOCTL_CLIENT_INFO:
753 {
754 CHECKRET_MIN_SIZE("CLIENT_INFO", sizeof(VBOXUSBREQ_CLIENT_INFO));
755
756 VBOXUSBREQ_CLIENT_INFO *pReq = (VBOXUSBREQ_CLIENT_INFO *)pvData;
757 rc = vboxUSBMonSolarisClientInfo(pState, pReq);
758 *pcbReturnedData = cbData;
759 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: CLIENT_INFO (Process:%d) returned %d\n", pState->Process, rc));
760 break;
761 }
762
763 case VBOXUSBMON_IOCTL_GET_VERSION:
764 {
765 CHECKRET_MIN_SIZE("GET_VERSION", sizeof(VBOXUSBREQ_GET_VERSION));
766
767 PVBOXUSBREQ_GET_VERSION pGetVersionReq = (PVBOXUSBREQ_GET_VERSION)pvData;
768 pGetVersionReq->u32Major = VBOXUSBMON_VERSION_MAJOR;
769 pGetVersionReq->u32Minor = VBOXUSBMON_VERSION_MINOR;
770 *pcbReturnedData = sizeof(VBOXUSBREQ_GET_VERSION);
771 rc = VINF_SUCCESS;
772 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: GET_VERSION returned %d\n", rc));
773 break;
774 }
775
776 default:
777 {
778 LogRel((DEVICE_NAME ":vboxUSBMonSolarisProcessIOCtl: Unknown request (Process:%d) %#x\n", pState->Process, iFunction));
779 rc = VERR_NOT_SUPPORTED;
780 break;
781 }
782 }
783 return rc;
784}
785
786
787static int vboxUSBMonSolarisResetDevice(char *pszDevicePath, bool fReattach)
788{
789 int rc = VERR_GENERAL_FAILURE;
790
791 LogFlowFunc((DEVICE_NAME ":vboxUSBMonSolarisResetDevice pszDevicePath=%s fReattach=%d\n", pszDevicePath, fReattach));
792
793 /*
794 * Try grabbing the dev_info_t.
795 */
796 dev_info_t *pDeviceInfo = e_ddi_hold_devi_by_path(pszDevicePath, 0);
797 if (pDeviceInfo)
798 {
799 ddi_release_devi(pDeviceInfo);
800
801 /*
802 * Grab the root device node from the parent hub for resetting.
803 */
804 dev_info_t *pTmpDeviceInfo = NULL;
805 for (;;)
806 {
807 pTmpDeviceInfo = ddi_get_parent(pDeviceInfo);
808 if (!pTmpDeviceInfo)
809 {
810 LogRel((DEVICE_NAME ":vboxUSBMonSolarisResetDevice failed to get parent device info for %s\n", pszDevicePath));
811 return VERR_GENERAL_FAILURE;
812 }
813
814 if (ddi_prop_exists(DDI_DEV_T_ANY, pTmpDeviceInfo, DDI_PROP_DONTPASS, "usb-port-count")) /* parent hub */
815 break;
816
817 pDeviceInfo = pTmpDeviceInfo;
818 }
819
820 /*
821 * Try re-enumerating the device.
822 */
823 rc = usb_reset_device(pDeviceInfo, fReattach ? USB_RESET_LVL_REATTACH : USB_RESET_LVL_DEFAULT);
824 LogFlow((DEVICE_NAME ":usb_reset_device for %s level=%s returned %d\n", pszDevicePath, fReattach ? "ReAttach" : "Default", rc));
825 switch (rc)
826 {
827 case USB_SUCCESS: rc = VINF_SUCCESS; break;
828 case USB_INVALID_PERM: rc = VERR_PERMISSION_DENIED; break;
829 case USB_INVALID_ARGS: rc = VERR_INVALID_PARAMETER; break;
830
831 /* @todo find better codes for these (especially USB_BUSY) */
832 case USB_BUSY:
833 case USB_INVALID_CONTEXT:
834 case USB_FAILURE: rc = VERR_GENERAL_FAILURE; break;
835
836 default: rc = VERR_UNRESOLVED_ERROR; break;
837 }
838 }
839 else
840 {
841 rc = VERR_INVALID_HANDLE;
842 LogRel((DEVICE_NAME ":vboxUSBMonSolarisResetDevice Cannot obtain device info for %s\n", pszDevicePath));
843 }
844
845 return rc;
846}
847
848static int vboxUSBMonSolarisClientInfo(vboxusbmon_state_t *pState, PVBOXUSB_CLIENT_INFO pClientInfo)
849{
850 LogFlowFunc((DEVICE_NAME ":vboxUSBMonSolarisClientInfo pState=%p pClientInfo=%p\n", pState, pClientInfo));
851
852 AssertPtrReturn(pState, VERR_INVALID_POINTER);
853 AssertPtrReturn(pClientInfo, VERR_INVALID_POINTER);
854
855 mutex_enter(&g_VBoxUSBMonSolarisMtx);
856 vboxusbmon_client_t *pCur = g_pVBoxUSBMonSolarisClients;
857 vboxusbmon_client_t *pPrev = NULL;
858 while (pCur)
859 {
860 if (strncmp(pClientInfo->achDeviceIdent, pCur->Info.achDeviceIdent, sizeof(pCur->Info.achDeviceIdent) - 1) == 0)
861 {
862 pClientInfo->Instance = pCur->Info.Instance;
863 RTStrPrintf(pClientInfo->achClientPath, sizeof(pClientInfo->achClientPath), "%s", pCur->Info.achClientPath);
864
865 mutex_exit(&g_VBoxUSBMonSolarisMtx);
866
867 LogFlow((DEVICE_NAME ":vboxUSBMonSolarisClientInfo found. %s\n", pClientInfo->achDeviceIdent));
868 return VINF_SUCCESS;
869 }
870 pPrev = pCur;
871 pCur = pCur->pNext;
872 }
873
874 mutex_exit(&g_VBoxUSBMonSolarisMtx);
875
876 LogRel((DEVICE_NAME ":vboxUSBMonSolarisClientInfo Failed to find client %s\n", pClientInfo->achDeviceIdent));
877 return VERR_NOT_FOUND;
878}
879
880
881/**
882 * Registers client driver.
883 *
884 * @returns VBox status code.
885 * @param pszDevicePath The device path of the client driver.
886 * @param Instance The client driver instance.
887 */
888int VBoxUSBMonSolarisRegisterClient(dev_info_t *pClientDip, PVBOXUSB_CLIENT_INFO pClientInfo)
889{
890 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisRegisterClient pClientDip=%p pClientInfo=%p\n", pClientDip, pClientInfo));
891 AssertPtrReturn(pClientInfo, VERR_INVALID_PARAMETER);
892
893 if (RT_LIKELY(g_pDip))
894 {
895 vboxusbmon_state_t *pState = ddi_get_driver_private(g_pDip);
896
897 if (RT_LIKELY(pState))
898 {
899 vboxusbmon_client_t *pClient = RTMemAlloc(sizeof(vboxusbmon_client_t));
900 if (RT_LIKELY(pClient))
901 {
902 bcopy(pClientInfo, &pClient->Info, sizeof(pClient->Info));
903 pClient->pDip = pClientDip;
904
905 mutex_enter(&g_VBoxUSBMonSolarisMtx);
906 pClient->pNext = g_pVBoxUSBMonSolarisClients;
907 g_pVBoxUSBMonSolarisClients = pClient;
908 mutex_exit(&g_VBoxUSBMonSolarisMtx);
909
910 LogFlow((DEVICE_NAME ":VBoxUSBMonSolarisRegisterClient registered. %d %s %s\n",
911 pClient->Info.Instance, pClient->Info.achClientPath, pClient->Info.achDeviceIdent));
912
913 return VINF_SUCCESS;
914 }
915 else
916 return VERR_NO_MEMORY;
917 }
918 else
919 return VERR_INTERNAL_ERROR;
920 }
921 else
922 return VERR_INTERNAL_ERROR_2;
923}
924
925
926/**
927 * Deregisters client driver.
928 *
929 * @returns VBox status code.
930 * @param pszDevicePath The device path of the client driver.
931 * @param Instance The client driver instance.
932 */
933int VBoxUSBMonSolarisUnregisterClient(dev_info_t *pClientDip)
934{
935 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisUnregisterClient pClientDip=%p\n", pClientDip));
936
937 if (RT_LIKELY(g_pDip))
938 {
939 vboxusbmon_state_t *pState = ddi_get_driver_private(g_pDip);
940
941 if (RT_LIKELY(pState))
942 {
943 mutex_enter(&g_VBoxUSBMonSolarisMtx);
944
945 vboxusbmon_client_t *pCur = g_pVBoxUSBMonSolarisClients;
946 vboxusbmon_client_t *pPrev = NULL;
947 while (pCur)
948 {
949 if (pCur->pDip == pClientDip)
950 {
951 if (pPrev)
952 pPrev->pNext = pCur->pNext;
953 else
954 g_pVBoxUSBMonSolarisClients = pCur->pNext;
955
956 mutex_exit(&g_VBoxUSBMonSolarisMtx);
957
958 LogFlow((DEVICE_NAME ":VBoxUSBMonSolarisUnregisterClient unregistered. %d %s %s\n",
959 pCur->Info.Instance, pCur->Info.achClientPath, pCur->Info.achDeviceIdent));
960 RTMemFree(pCur);
961 pCur = NULL;
962 return VINF_SUCCESS;
963 }
964 pPrev = pCur;
965 pCur = pCur->pNext;
966 }
967
968 mutex_exit(&g_VBoxUSBMonSolarisMtx);
969
970 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisUnregisterClient Failed to find registered client %p\n", pClientDip));
971 return VERR_SEARCH_ERROR;
972 }
973 else
974 return VERR_INTERNAL_ERROR;
975 }
976 else
977 return VERR_INTERNAL_ERROR_2;
978}
979
980
981/**
982 * USBA driver election callback.
983 *
984 * @returns USB_SUCCESS if we want to capture the device, USB_FAILURE otherwise.
985 * @param pDevDesc The parsed device descriptor (does not include subconfigs).
986 * @param pDevStrings Device strings: Manufacturer, Product, Serial Number.
987 * @param pszDevicePath The physical path of the device being attached.
988 * @param Bus The Bus number on which the device is on.
989 * @param Port The Port number on the bus.
990 * @param ppszDrv The name of the driver we wish to capture the device with.
991 * @param pvReserved Reserved for future use.
992 */
993int VBoxUSBMonSolarisElectDriver(usb_dev_descr_t *pDevDesc, usb_dev_str_t *pDevStrings, char *pszDevicePath, int Bus, int Port,
994 char **ppszDrv, void *pvReserved)
995{
996 LogFlowFunc((DEVICE_NAME ":VBoxUSBMonSolarisElectDriver pDevDesc=%p pDevStrings=%p pszDevicePath=%s Bus=%d Port=%d\n", pDevDesc,
997 pDevStrings, pszDevicePath, Bus, Port));
998
999 AssertPtrReturn(pDevDesc, USB_FAILURE);
1000 AssertPtrReturn(pDevStrings, USB_FAILURE);
1001
1002 /*
1003 * Create a filter from the device being attached.
1004 */
1005 USBFILTER Filter;
1006 USBFilterInit(&Filter, USBFILTERTYPE_CAPTURE);
1007 USBFilterSetNumExact(&Filter, USBFILTERIDX_VENDOR_ID, pDevDesc->idVendor, true);
1008 USBFilterSetNumExact(&Filter, USBFILTERIDX_PRODUCT_ID, pDevDesc->idProduct, true);
1009 USBFilterSetNumExact(&Filter, USBFILTERIDX_DEVICE_REV, pDevDesc->bcdDevice, true);
1010 USBFilterSetNumExact(&Filter, USBFILTERIDX_DEVICE_CLASS, pDevDesc->bDeviceClass, true);
1011 USBFilterSetNumExact(&Filter, USBFILTERIDX_DEVICE_SUB_CLASS, pDevDesc->bDeviceSubClass, true);
1012 USBFilterSetNumExact(&Filter, USBFILTERIDX_DEVICE_PROTOCOL, pDevDesc->bDeviceProtocol, true);
1013 USBFilterSetNumExact(&Filter, USBFILTERIDX_BUS, 0x0 /* Bus */, true); /* Use 0x0 as userland initFilterFromDevice function in Main: see comment on "SetMustBePresent" below */
1014 USBFilterSetNumExact(&Filter, USBFILTERIDX_PORT, Port, true);
1015 USBFilterSetStringExact(&Filter, USBFILTERIDX_MANUFACTURER_STR, pDevStrings->usb_mfg, true);
1016 USBFilterSetStringExact(&Filter, USBFILTERIDX_PRODUCT_STR, pDevStrings->usb_product, true);
1017 USBFilterSetStringExact(&Filter, USBFILTERIDX_SERIAL_NUMBER_STR, pDevStrings->usb_serialno, true);
1018
1019 /* This doesn't work like it should (USBFilterMatch fails on matching field (6) i.e. Bus despite this. Investigate later. */
1020 USBFilterSetMustBePresent(&Filter, USBFILTERIDX_BUS, false /* fMustBePresent */);
1021
1022 LogFlow((DEVICE_NAME ":VBoxUSBMonSolarisElectDriver: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x\n",
1023 USBFilterGetNum(&Filter, USBFILTERIDX_VENDOR_ID),
1024 USBFilterGetNum(&Filter, USBFILTERIDX_PRODUCT_ID),
1025 USBFilterGetNum(&Filter, USBFILTERIDX_DEVICE_REV),
1026 USBFilterGetNum(&Filter, USBFILTERIDX_DEVICE_CLASS),
1027 USBFilterGetNum(&Filter, USBFILTERIDX_DEVICE_SUB_CLASS),
1028 USBFilterGetNum(&Filter, USBFILTERIDX_DEVICE_PROTOCOL),
1029 USBFilterGetNum(&Filter, USBFILTERIDX_BUS),
1030 USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
1031 LogFlow((DEVICE_NAME ":VBoxUSBMonSolarisElectDriver: Manufacturer=%s Product=%s Serial=%s\n",
1032 USBFilterGetString(&Filter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(&Filter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
1033 USBFilterGetString(&Filter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(&Filter, USBFILTERIDX_PRODUCT_STR) : "<null>",
1034 USBFilterGetString(&Filter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(&Filter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
1035
1036 /*
1037 * Run through user filters and try to see if it has a match.
1038 */
1039 uintptr_t uId = 0;
1040 RTPROCESS Owner = VBoxUSBFilterMatch(&Filter, &uId);
1041 USBFilterDelete(&Filter);
1042 if (Owner == NIL_RTPROCESS)
1043 {
1044 LogFlow((DEVICE_NAME ":No matching filters, device %#x:%#x uninteresting.\n", pDevDesc->idVendor, pDevDesc->idProduct));
1045 return USB_FAILURE;
1046 }
1047
1048 *ppszDrv = ddi_strdup(VBOXUSB_DRIVER_NAME, KM_SLEEP);
1049 LogRel((DEVICE_NAME ": Capturing %s %#x:%#x:%s\n", pDevStrings->usb_product ? pDevStrings->usb_product : "<Unnamed USB device>",
1050 pDevDesc->idVendor, pDevDesc->idProduct, pszDevicePath));
1051 return USB_SUCCESS;
1052}
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