VirtualBox

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

Last change on this file since 46150 was 43816, checked in by vboxsync, 12 years ago

HostDrivers/solaris/USB: Passthrough short packets bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 144.5 KB
Line 
1/* $Id: VBoxUSB-solaris.c 43816 2012-11-06 17:44:17Z vboxsync $ */
2/** @file
3 * VirtualBox USB Client Driver, Solaris Hosts.
4 */
5
6/*
7 * Copyright (C) 2008-2012 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#define LOG_GROUP LOG_GROUP_USB_DRV
31#ifdef DEBUG_ramshankar
32# define LOG_ENABLED
33# define LOG_INSTANCE RTLogRelDefaultInstance()
34#endif
35#include <VBox/version.h>
36#include <VBox/log.h>
37#include <VBox/err.h>
38#include <VBox/cdefs.h>
39#include <VBox/sup.h>
40#include <VBox/usblib-solaris.h>
41
42#include <iprt/assert.h>
43#include <iprt/initterm.h>
44#include <iprt/semaphore.h>
45#include <iprt/mem.h>
46#include <iprt/process.h>
47#include <iprt/string.h>
48#include <iprt/path.h>
49#include <iprt/thread.h>
50
51#define USBDRV_MAJOR_VER 2
52#define USBDRV_MINOR_VER 0
53#include <sys/usb/usba.h>
54#include <sys/strsun.h>
55#include "usbai_private.h"
56#include <sys/archsystm.h>
57#include <sys/disp.h>
58
59/** @todo review the locking here, verify assumptions about code executed
60 * without the vboxusb_state_t::Mtx mutex */
61
62/*******************************************************************************
63* Defined Constants And Macros *
64*******************************************************************************/
65/** The module name. */
66#define DEVICE_NAME "vboxusb"
67/** The module description as seen in 'modinfo'. */
68#define DEVICE_DESC_DRV "VirtualBox USB"
69
70/** Endpoint states */
71#define VBOXUSB_EP_INITIALIZED 0xa1fa1fa
72#define VBOXUSB_EP_STATE_NONE RT_BIT(0)
73#define VBOXUSB_EP_STATE_CLOSED RT_BIT(1)
74#define VBOXUSB_EP_STATE_OPENED RT_BIT(2)
75/** Polling states */
76#define VBOXUSB_POLL_OFF RT_BIT(0)
77#define VBOXUSB_POLL_ON RT_BIT(1)
78#define VBOXUSB_POLL_REAP_PENDING RT_BIT(2)
79#define VBOXUSB_POLL_DEV_UNPLUGGED RT_BIT(3)
80
81/** -=-=-=-=-=-=- Standard Specifics -=-=-=-=-=-=- */
82/** Max. supported endpoints */
83#define VBOXUSB_MAX_ENDPOINTS 32
84/** Size of USB Ctrl Xfer Header */
85#define VBOXUSB_CTRL_XFER_SIZE 0x08
86/**
87 * USB2.0 (Sec. 9-13) Bits 10..0 is the max packet size; for high speed Isoc/Intr, bits 12..11 is
88 * number of additional transaction opportunities per microframe.
89 */
90#define VBOXUSB_PKT_SIZE(pkt) (pkt & 0x07FF) * (1 + ((pkt >> 11) & 3))
91/** Endpoint Xfer Type */
92#define VBOXUSB_XFER_TYPE(endp) ((endp)->EpDesc.bmAttributes & USB_EP_ATTR_MASK)
93/** Endpoint Xfer Direction */
94#define VBOXUSB_XFER_DIR(endp) ((endp)->EpDesc.bEndpointAddress & USB_EP_DIR_IN)
95
96/** -=-=-=-=-=-=- Tunable Parameters -=-=-=-=-=-=- */
97/** Time to wait while draining inflight UBRs on suspend, in seconds. */
98#define VBOXUSB_DRAIN_TIME 30
99/** Ctrl Xfer timeout in seconds. */
100#define VBOXUSB_CTRL_XFER_TIMEOUT 10
101/** Bulk Xfer timeout in seconds. */
102#define VBOXUSB_BULK_XFER_TIMEOUT 10
103/** Intr Xfer timeout in seconds. */
104#define VBOXUSB_INTR_XFER_TIMEOUT 10
105/** Maximum URB queue length. */
106#define VBOXUSB_URB_QUEUE_SIZE 64
107/** Maximum asynchronous requests per pipe */
108#define VBOXUSB_MAX_PIPE_ASYNC_REQS 2
109
110/** For enabling global symbols while debugging **/
111#if defined(DEBUG_ramshankar)
112# define LOCAL
113#else
114# define LOCAL static
115#endif
116
117
118/*******************************************************************************
119* Kernel Entry Hooks *
120*******************************************************************************/
121int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred);
122int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred);
123int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred);
124int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred);
125int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal);
126int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead);
127int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pArg, void **ppResult);
128int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd);
129int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd);
130int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level);
131
132
133/*******************************************************************************
134* Structures and Typedefs *
135*******************************************************************************/
136/**
137 * cb_ops: for drivers that support char/block entry points
138 */
139static struct cb_ops g_VBoxUSBSolarisCbOps =
140{
141 VBoxUSBSolarisOpen,
142 VBoxUSBSolarisClose,
143 nodev, /* b strategy */
144 nodev, /* b dump */
145 nodev, /* b print */
146 VBoxUSBSolarisRead,
147 VBoxUSBSolarisWrite,
148 VBoxUSBSolarisIOCtl,
149 nodev, /* c devmap */
150 nodev, /* c mmap */
151 nodev, /* c segmap */
152 VBoxUSBSolarisPoll,
153 ddi_prop_op, /* property ops */
154 NULL, /* streamtab */
155 D_NEW | D_MP, /* compat. flag */
156 CB_REV, /* revision */
157 nodev, /* c aread */
158 nodev /* c awrite */
159};
160
161/**
162 * dev_ops: for driver device operations
163 */
164static struct dev_ops g_VBoxUSBSolarisDevOps =
165{
166 DEVO_REV, /* driver build revision */
167 0, /* ref count */
168 VBoxUSBSolarisGetInfo,
169 nulldev, /* identify */
170 nulldev, /* probe */
171 VBoxUSBSolarisAttach,
172 VBoxUSBSolarisDetach,
173 nodev, /* reset */
174 &g_VBoxUSBSolarisCbOps,
175 NULL, /* bus ops */
176 VBoxUSBSolarisPower,
177 ddi_quiesce_not_needed
178};
179
180/**
181 * modldrv: export driver specifics to the kernel
182 */
183static struct modldrv g_VBoxUSBSolarisModule =
184{
185 &mod_driverops, /* extern from kernel */
186 DEVICE_DESC_DRV " " VBOX_VERSION_STRING "r" RT_XSTR(VBOX_SVN_REV),
187 &g_VBoxUSBSolarisDevOps
188};
189
190/**
191 * modlinkage: export install/remove/info to the kernel
192 */
193static struct modlinkage g_VBoxUSBSolarisModLinkage =
194{
195 MODREV_1,
196 &g_VBoxUSBSolarisModule,
197 NULL,
198};
199
200/**
201 * vboxusb_ep_t: Endpoint structure with info. for managing an endpoint.
202 */
203typedef struct vboxusb_ep_t
204{
205 uint_t fInitialized; /* Whether this Endpoint is initialized */
206 uint_t EpState; /* Endpoint state */
207 usb_ep_descr_t EpDesc; /* Endpoint descriptor */
208 uchar_t uCfgValue; /* Configuration value */
209 uchar_t uInterface; /* Interface number */
210 uchar_t uAlt; /* Alternate number */
211 usb_pipe_handle_t pPipe; /* Endpoint pipe handle */
212 usb_pipe_policy_t PipePolicy; /* Endpoint policy */
213 bool fIsocPolling; /* Whether Isoc. IN polling is enabled */
214 list_t hIsocInUrbs; /* Isoc. IN inflight URBs */
215 uint16_t cIsocInUrbs; /* Number of Isoc. IN inflight URBs */
216 list_t hIsocInLandedReqs; /* Isoc. IN landed requests */
217 uint16_t cbIsocInLandedReqs; /* Cumulative size of landed Isoc. IN requests */
218 size_t cbMaxIsocData; /* Maximum size of Isoc. IN landed buffer */
219} vboxusb_ep_t;
220
221/**
222 * vboxusb_isoc_req_t: Isoc IN. requests queued from device till they are reaped.
223 */
224typedef struct vboxusb_isoc_req_t
225{
226 mblk_t *pMsg; /* Pointer to the data buffer */
227 uint32_t cIsocPkts; /* Number of Isoc pkts */
228 VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
229 list_node_t hListLink;
230} vboxusb_isoc_req_t;
231
232/**
233 * VBOXUSB_URB_STATE: Internal USB URB state.
234 */
235typedef enum VBOXUSB_URB_STATE
236{
237 VBOXUSB_URB_STATE_FREE = 0x00,
238 VBOXUSB_URB_STATE_INFLIGHT = 0x04,
239 VBOXUSB_URB_STATE_LANDED = 0x08
240} VBOXUSB_URB_STATE;
241
242/**
243 * vboxusb_urb_t: kernel URB representation.
244 */
245typedef struct vboxusb_urb_t
246{
247 void *pvUrbR3; /* Userspace URB address (untouched, returned while reaping) */
248 uint8_t bEndpoint; /* Endpoint address */
249 VUSBXFERTYPE enmType; /* Xfer type */
250 VUSBDIRECTION enmDir; /* Xfer direction */
251 VUSBSTATUS enmStatus; /* URB status */
252 bool fShortOk; /* Whether receiving less data than requested is acceptable. */
253 RTR3PTR pvDataR3; /* Userspace address of the original data buffer */
254 size_t cbDataR3; /* Size of the data buffer */
255 mblk_t *pMsg; /* Pointer to the data buffer */
256 uint32_t cIsocPkts; /* Number of Isoc pkts */
257 VUSBISOC_PKT_DESC aIsocPkts[8]; /* Array of Isoc pkt descriptors */
258 VBOXUSB_URB_STATE enmState; /* Whether free/in-flight etc. */
259 struct vboxusb_state_t *pState; /* Pointer to the device instance */
260 list_node_t hListLink; /* List node link handle */
261} vboxusb_urb_t;
262
263/**
264 * vboxusb_power_t: Per Device Power Management info.
265 */
266typedef struct vboxusb_power_t
267{
268 uint_t PowerStates; /* Bit mask of the power states */
269 int PowerBusy; /* Busy counter */
270 bool fPowerWakeup; /* Whether remote power wakeup is enabled */
271 bool fPowerRaise; /* Whether to raise the power level */
272 uint8_t PowerLevel; /* Current power level */
273} vboxusb_power_t;
274
275/**
276 * vboxusb_state_t: Per Device instance state info.
277 */
278typedef struct vboxusb_state_t
279{
280 dev_info_t *pDip; /* Per instance device info. */
281 usb_client_dev_data_t *pDevDesc; /* Parsed & complete device descriptor */
282 uint8_t DevState; /* Current USB Device state */
283 bool fClosed; /* Whether the device (default control pipe) is closed */
284 bool fRestoreCfg; /* Whether we changed configs to restore while tearing down */
285 bool fGetCfgReqDone; /* First GET_CONFIG request has been circumvented */
286 kmutex_t Mtx; /* Mutex state protection */
287 usb_serialization_t StateMulti; /* State serialization */
288 size_t cbMaxBulkXfer; /* Maximum bulk xfer size */
289 vboxusb_ep_t aEps[VBOXUSB_MAX_ENDPOINTS]; /* All endpoints structures */
290 list_t hUrbs; /* Handle to list of free/inflight URBs */
291 list_t hLandedUrbs; /* Handle to list of landed URBs */
292 uint16_t cInflightUrbs; /* Number of inflight URBs. */
293 pollhead_t PollHead; /* Handle to pollhead for waking polling processes */
294 int fPoll; /* Polling status flag */
295 RTPROCESS Process; /* The process (id) of the session */
296 VBOXUSBREQ_CLIENT_INFO ClientInfo; /* Registration data */
297 vboxusb_power_t *pPower; /* Power Management */
298} vboxusb_state_t;
299
300
301/*******************************************************************************
302* Internal Functions *
303*******************************************************************************/
304LOCAL int vboxUSBSolarisInitEndPoint(vboxusb_state_t *pState, usb_ep_data_t *pEpData, uchar_t uCfgValue,
305 uchar_t uInterface, uchar_t uAlt);
306LOCAL int vboxUSBSolarisInitAllEndPoints(vboxusb_state_t *pState);
307LOCAL int vboxUSBSolarisInitEndPointsForConfig(vboxusb_state_t *pState, uint8_t uCfgIndex);
308LOCAL int vboxUSBSolarisInitEndPointsForInterfaceAlt(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt);
309LOCAL void vboxUSBSolarisDestroyAllEndPoints(vboxusb_state_t *pState);
310LOCAL void vboxUSBSolarisDestroyEndPoint(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
311LOCAL void vboxUSBSolarisCloseAllPipes(vboxusb_state_t *pState, bool fControlPipe);
312LOCAL int vboxUSBSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
313LOCAL void vboxUSBSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp);
314LOCAL int vboxUSBSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
315LOCAL void vboxUSBSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq);
316LOCAL int vboxUSBSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *purb);
317LOCAL void vboxUSBSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq);
318LOCAL int vboxUSBSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
319LOCAL void vboxUSBSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq);
320LOCAL int vboxUSBSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb);
321LOCAL void vboxUSBSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
322LOCAL void vboxUSBSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
323LOCAL void vboxUSBSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq);
324LOCAL vboxusb_urb_t *vboxUSBSolarisGetIsocInURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq);
325LOCAL vboxusb_urb_t *vboxUSBSolarisQueueURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg);
326LOCAL inline void vboxUSBSolarisConcatMsg(vboxusb_urb_t *pUrb);
327LOCAL inline void vboxUSBSolarisDeQueueURB(vboxusb_urb_t *pUrb, int URBStatus);
328LOCAL inline void vboxUSBSolarisNotifyComplete(vboxusb_state_t *pState);
329LOCAL int vboxUSBSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf,
330 size_t *pcbDataOut);
331LOCAL bool vboxUSBSolarisIsUSBDevice(dev_info_t *pDip);
332
333/** Device Operation Hooks */
334LOCAL int vboxUSBSolarisSendURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
335LOCAL int vboxUSBSolarisReapURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode);
336LOCAL int vboxUSBSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint);
337LOCAL int vboxUSBSolarisSetConfig(vboxusb_state_t *pState, uint8_t bCfgValue);
338LOCAL int vboxUSBSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pCfgValue);
339LOCAL int vboxUSBSolarisSetInterface(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt);
340LOCAL int vboxUSBSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset);
341LOCAL int vboxUSBSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint);
342LOCAL int vboxUSBSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t uCfgValue);
343
344/** Hotplug & Power Management Hooks */
345LOCAL inline void vboxUSBSolarisNotifyHotplug(vboxusb_state_t *pState);
346LOCAL int vboxUSBSolarisDeviceDisconnected(dev_info_t *pDip);
347LOCAL int vboxUSBSolarisDeviceReconnected(dev_info_t *pDip);
348
349LOCAL int vboxUSBSolarisInitPower(vboxusb_state_t *pState);
350LOCAL void vboxUSBSolarisDestroyPower(vboxusb_state_t *pState);
351LOCAL int vboxUSBSolarisDeviceSuspend(vboxusb_state_t *pState);
352LOCAL void vboxUSBSolarisDeviceResume(vboxusb_state_t *pState);
353LOCAL void vboxUSBSolarisDeviceRestore(vboxusb_state_t *pState);
354LOCAL void vboxUSBSolarisPowerBusy(vboxusb_state_t *pState);
355LOCAL void vboxUSBSolarisPowerIdle(vboxusb_state_t *pState);
356
357/** Monitor Hooks */
358int VBoxUSBMonSolarisRegisterClient(dev_info_t *pClientDip, PVBOXUSB_CLIENT_INFO pClientInfo);
359int VBoxUSBMonSolarisUnregisterClient(dev_info_t *pClientDip);
360
361/** Callbacks from Monitor */
362LOCAL int vboxUSBSolarisSetConsumerCredentials(RTPROCESS Process, int Instance, void *pvReserved);
363
364
365/*******************************************************************************
366* Global Variables *
367*******************************************************************************/
368/** Global list of all device instances. */
369static void *g_pVBoxUSBSolarisState;
370
371/** The default endpoint descriptor */
372static usb_ep_descr_t g_VBoxUSBSolarisDefaultEpDesc = {7, 5, 0, USB_EP_ATTR_CONTROL, 8, 0};
373
374/** Hotplug events */
375static usb_event_t g_VBoxUSBSolarisEvents =
376{
377 vboxUSBSolarisDeviceDisconnected,
378 vboxUSBSolarisDeviceReconnected,
379 NULL, /* presuspend */
380 NULL /* postresume */
381};
382
383
384/**
385 * Kernel entry points
386 */
387int _init(void)
388{
389 LogFunc((DEVICE_NAME ":_init\n"));
390
391 /*
392 * Prevent module autounloading.
393 */
394 modctl_t *pModCtl = mod_getctl(&g_VBoxUSBSolarisModLinkage);
395 if (pModCtl)
396 pModCtl->mod_loadflags |= MOD_NOAUTOUNLOAD;
397 else
398 LogRel((DEVICE_NAME ":failed to disable autounloading!\n"));
399
400 /*
401 * Initialize IPRT R0 driver, which internally calls OS-specific r0 init.
402 */
403 int rc = RTR0Init(0);
404 if (RT_SUCCESS(rc))
405 {
406 rc = ddi_soft_state_init(&g_pVBoxUSBSolarisState, sizeof(vboxusb_state_t), 4 /* pre-alloc */);
407 if (!rc)
408 {
409 rc = mod_install(&g_VBoxUSBSolarisModLinkage);
410 if (!rc)
411 return rc;
412
413 LogRel((DEVICE_NAME ":mod_install failed! rc=%d\n", rc));
414 ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
415 }
416 else
417 LogRel((DEVICE_NAME ":failed to initialize soft state.\n"));
418
419 RTR0Term();
420 }
421 else
422 LogRel((DEVICE_NAME ":RTR0Init failed! rc=%d\n", rc));
423 return RTErrConvertToErrno(rc);
424}
425
426
427int _fini(void)
428{
429 int rc;
430
431 LogFunc((DEVICE_NAME ":_fini\n"));
432
433 rc = mod_remove(&g_VBoxUSBSolarisModLinkage);
434 if (!rc)
435 {
436 ddi_soft_state_fini(&g_pVBoxUSBSolarisState);
437 RTR0Term();
438 }
439
440 return rc;
441}
442
443
444int _info(struct modinfo *pModInfo)
445{
446 LogFunc((DEVICE_NAME ":_info\n"));
447
448 return mod_info(&g_VBoxUSBSolarisModLinkage, pModInfo);
449}
450
451
452/**
453 * Attach entry point, to attach a device to the system or resume it.
454 *
455 * @param pDip The module structure instance.
456 * @param enmCmd Attach type (ddi_attach_cmd_t)
457 *
458 * @returns corresponding solaris error code.
459 */
460int VBoxUSBSolarisAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd)
461{
462 LogFunc((DEVICE_NAME ":VBoxUSBSolarisAttach pDip=%p enmCmd=%d\n", pDip, enmCmd));
463
464 int rc;
465 int instance = ddi_get_instance(pDip);
466 vboxusb_state_t *pState = NULL;
467
468 switch (enmCmd)
469 {
470 case DDI_ATTACH:
471 {
472 rc = ddi_soft_state_zalloc(g_pVBoxUSBSolarisState, instance);
473 if (rc == DDI_SUCCESS)
474 {
475 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
476 if (RT_LIKELY(pState))
477 {
478 pState->pDip = pDip;
479 pState->pDevDesc = NULL;
480 pState->fClosed = false;
481 pState->fRestoreCfg = false;
482 pState->fGetCfgReqDone = false;
483 bzero(pState->aEps, sizeof(pState->aEps));
484 list_create(&pState->hUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
485 list_create(&pState->hLandedUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
486 pState->cInflightUrbs = 0;
487 pState->fPoll = VBOXUSB_POLL_OFF;
488 pState->Process = NIL_RTPROCESS;
489 pState->pPower = NULL;
490
491 /*
492 * There is a bug in usb_client_attach() as of Nevada 120 which panics when we bind to
493 * a non-USB device. So check if we are really binding to a USB device or not.
494 */
495 if (vboxUSBSolarisIsUSBDevice(pState->pDip))
496 {
497 /*
498 * Here starts the USB specifics.
499 */
500 rc = usb_client_attach(pState->pDip, USBDRV_VERSION, 0);
501 if (rc == USB_SUCCESS)
502 {
503 /*
504 * Parse out the entire descriptor.
505 */
506 rc = usb_get_dev_data(pState->pDip, &pState->pDevDesc, USB_PARSE_LVL_ALL, 0 /* Unused */);
507 if (rc == USB_SUCCESS)
508 {
509#ifdef DEBUG_ramshankar
510 usb_print_descr_tree(pState->pDip, pState->pDevDesc);
511#endif
512
513 /*
514 * Initialize state locks.
515 */
516 mutex_init(&pState->Mtx, NULL, MUTEX_DRIVER, pState->pDevDesc->dev_iblock_cookie);
517 pState->StateMulti = usb_init_serialization(pState->pDip, USB_INIT_SER_CHECK_SAME_THREAD);
518
519 /*
520 * Get maximum bulk transfer size supported by the HCD.
521 */
522 rc = usb_pipe_get_max_bulk_transfer_size(pState->pDip, &pState->cbMaxBulkXfer);
523 if (rc == USB_SUCCESS)
524 {
525 Log((DEVICE_NAME ":VBoxUSBSolarisAttach cbMaxBulkXfer=%d\n", pState->cbMaxBulkXfer));
526
527 /*
528 * Initialize all endpoints.
529 */
530 rc = vboxUSBSolarisInitAllEndPoints(pState);
531 if (RT_SUCCESS(rc))
532 {
533 /*
534 * Set the device state.
535 */
536 pState->DevState = USB_DEV_ONLINE;
537
538 /*
539 * Initialize power management for the device.
540 */
541 rc = vboxUSBSolarisInitPower(pState);
542 if (RT_SUCCESS(rc))
543 {
544 /*
545 * Update endpoints (descriptors) for the current config.
546 */
547 vboxUSBSolarisInitEndPointsForConfig(pState, usb_get_current_cfgidx(pState->pDip));
548
549 /*
550 * Publish the minor node.
551 */
552 rc = ddi_create_priv_minor_node(pDip, DEVICE_NAME, S_IFCHR, instance, DDI_PSEUDO, 0,
553 "none", "none", 0666);
554 if (RT_LIKELY(rc == DDI_SUCCESS))
555 {
556 /*
557 * Register hotplug callbacks.
558 */
559 rc = usb_register_event_cbs(pState->pDip, &g_VBoxUSBSolarisEvents, 0 /* flags */);
560 if (RT_LIKELY(rc == USB_SUCCESS))
561 {
562 /*
563 * Register with our monitor driver.
564 */
565 bzero(&pState->ClientInfo, sizeof(pState->ClientInfo));
566 char szDevicePath[MAXPATHLEN];
567 ddi_pathname(pState->pDip, szDevicePath);
568 RTStrPrintf(pState->ClientInfo.szClientPath,
569 sizeof(pState->ClientInfo.szClientPath),
570 "/devices%s:%s", szDevicePath,DEVICE_NAME);
571 RTPathStripFilename(szDevicePath);
572 RTStrPrintf(pState->ClientInfo.szDeviceIdent,
573 sizeof(pState->ClientInfo.szDeviceIdent),
574 "%#x:%#x:%d:%s",
575 pState->pDevDesc->dev_descr->idVendor,
576 pState->pDevDesc->dev_descr->idProduct,
577 pState->pDevDesc->dev_descr->bcdDevice, szDevicePath);
578 pState->ClientInfo.Instance = instance;
579 pState->ClientInfo.pfnSetConsumerCredentials = &vboxUSBSolarisSetConsumerCredentials;
580 rc = VBoxUSBMonSolarisRegisterClient(pState->pDip, &pState->ClientInfo);
581 if (RT_SUCCESS(rc))
582 {
583 LogRel((DEVICE_NAME ": Captured %s %#x:%#x:%d:%s\n",
584 pState->pDevDesc->dev_product ? pState->pDevDesc->dev_product
585 : "<Unnamed USB device>",
586 pState->pDevDesc->dev_descr->idVendor,
587 pState->pDevDesc->dev_descr->idProduct,
588 pState->pDevDesc->dev_descr->bcdDevice,
589 pState->ClientInfo.szClientPath));
590
591 return DDI_SUCCESS;
592 }
593 else
594 {
595 LogRel((DEVICE_NAME ":VBoxUSBMonSolarisRegisterClient failed! rc=%d "
596 "path=%s instance=%d\n", rc, pState->ClientInfo.szClientPath,
597 instance));
598 }
599
600 usb_unregister_event_cbs(pState->pDip, &g_VBoxUSBSolarisEvents);
601 }
602 else
603 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to register hotplug "
604 "callbacks! rc=%d\n", rc));
605
606 ddi_remove_minor_node(pState->pDip, NULL);
607 }
608 else
609 {
610 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach ddi_create_minor_node failed! rc=%d\n",
611 rc));
612 }
613 }
614 else
615 {
616 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to initialize power management! "
617 "rc=%d\n", rc));
618 }
619 }
620 else
621 {
622 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach vboxUSBSolarisInitAllEndPoints failed! "
623 "rc=%d\n"));
624 }
625 }
626 else
627 {
628 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach usb_pipe_get_max_bulk_transfer_size failed! "
629 "rc=%d\n", rc));
630 }
631
632 usb_fini_serialization(pState->StateMulti);
633 mutex_destroy(&pState->Mtx);
634 usb_free_dev_data(pState->pDip, pState->pDevDesc);
635 }
636 else
637 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to get device descriptor. rc=%d\n", rc));
638
639 usb_client_detach(pState->pDip, NULL);
640 }
641 else
642 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach usb_client_attach failed! rc=%d\n", rc));
643 }
644 else
645 {
646 /* This would appear on every boot if it were LogRel() */
647 Log((DEVICE_NAME ":VBoxUSBSolarisAttach not a USB device.\n"));
648 }
649 }
650 else
651 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to get soft state\n", sizeof(*pState)));
652
653 ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
654 }
655 else
656 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach failed to alloc soft state. rc=%d\n", rc));
657
658 return DDI_FAILURE;
659 }
660
661 case DDI_RESUME:
662 {
663 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
664 if (RT_UNLIKELY(!pState))
665 {
666 LogRel((DEVICE_NAME ":VBoxUSBSolarisAttach DDI_RESUME: failed to get soft state on detach.\n"));
667 return DDI_FAILURE;
668 }
669
670 vboxUSBSolarisDeviceResume(pState);
671 return DDI_SUCCESS;
672 }
673
674 default:
675 return DDI_FAILURE;
676 }
677}
678
679
680/**
681 * Detach entry point, to detach a device to the system or suspend it.
682 *
683 * @param pDip The module structure instance.
684 * @param enmCmd Attach type (ddi_attach_cmd_t)
685 *
686 * @returns corresponding solaris error code.
687 */
688int VBoxUSBSolarisDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
689{
690 LogFunc((DEVICE_NAME ":VBoxUSBSolarisDetach pDip=%p enmCmd=%d\n", pDip, enmCmd));
691
692 int instance = ddi_get_instance(pDip);
693 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
694 if (RT_UNLIKELY(!pState))
695 {
696 LogRel((DEVICE_NAME ":VBoxUSBSolarisDetach failed to get soft state on detach.\n"));
697 return DDI_FAILURE;
698 }
699
700 switch (enmCmd)
701 {
702 case DDI_DETACH:
703 {
704 /*
705 * At this point it must be assumed that the default control pipe has
706 * already been closed by userland (via VBoxUSBSolarisClose() entry point).
707 * Once it's closed we can no longer open or reference the device here.
708 */
709
710 /*
711 * Notify userland if any that we're gone (while resetting device held by us).
712 */
713 vboxUSBSolarisNotifyHotplug(pState);
714
715 /*
716 * Unregister hotplug callback events first without holding the mutex as the callbacks
717 * would otherwise block on the mutex.
718 */
719 usb_unregister_event_cbs(pDip, &g_VBoxUSBSolarisEvents);
720
721
722 /*
723 * Serialize: paranoid; drain other driver activity.
724 */
725 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
726 usb_release_access(pState->StateMulti);
727 mutex_enter(&pState->Mtx);
728
729 /*
730 * Close all endpoints.
731 */
732 vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
733 pState->fClosed = true;
734
735 /*
736 * Deinitialize power, destroy endpoints.
737 */
738 vboxUSBSolarisDestroyPower(pState);
739 vboxUSBSolarisDestroyAllEndPoints(pState);
740
741 /*
742 * Free up all URBs.
743 */
744 vboxusb_urb_t *pUrb = NULL;
745 while ((pUrb = list_remove_head(&pState->hUrbs)) != NULL)
746 {
747 if (pUrb->pMsg)
748 freemsg(pUrb->pMsg);
749 RTMemFree(pUrb);
750 }
751
752 while ((pUrb = list_remove_head(&pState->hLandedUrbs)) != NULL)
753 {
754 if (pUrb->pMsg)
755 freemsg(pUrb->pMsg);
756 RTMemFree(pUrb);
757 }
758 pState->cInflightUrbs = 0;
759 list_destroy(&pState->hUrbs);
760 list_destroy(&pState->hLandedUrbs);
761
762 /*
763 * Destroy locks, free up descriptor and detach from USBA.
764 */
765 mutex_exit(&pState->Mtx);
766 usb_fini_serialization(pState->StateMulti);
767 mutex_destroy(&pState->Mtx);
768
769 usb_free_dev_data(pState->pDip, pState->pDevDesc);
770 usb_client_detach(pState->pDip, NULL);
771
772 /*
773 * Deregister with our Monitor driver.
774 */
775 VBoxUSBMonSolarisUnregisterClient(pState->pDip);
776
777 ddi_remove_minor_node(pState->pDip, NULL);
778
779 LogRel((DEVICE_NAME ": Released %s %s\n",
780 pState->pDevDesc->dev_product ? pState->pDevDesc->dev_product : "<Unnamed USB device>",
781 pState->ClientInfo.szDeviceIdent));
782
783 ddi_soft_state_free(g_pVBoxUSBSolarisState, instance);
784 pState = NULL;
785
786 return DDI_SUCCESS;
787 }
788
789 case DDI_SUSPEND:
790 {
791 int rc = vboxUSBSolarisDeviceSuspend(pState);
792 if (RT_SUCCESS(rc))
793 return DDI_SUCCESS;
794
795 return DDI_FAILURE;
796 }
797
798 default:
799 return DDI_FAILURE;
800 }
801}
802
803
804/**
805 * Info entry point, called by solaris kernel for obtaining driver info.
806 *
807 * @param pDip The module structure instance (do not use).
808 * @param enmCmd Information request type.
809 * @param pvArg Type specific argument.
810 * @param ppvResult Where to store the requested info.
811 *
812 * @returns corresponding solaris error code.
813 */
814int VBoxUSBSolarisGetInfo(dev_info_t *pDip, ddi_info_cmd_t enmCmd, void *pvArg, void **ppvResult)
815{
816 LogFunc((DEVICE_NAME ":VBoxUSBSolarisGetInfo\n"));
817
818 vboxusb_state_t *pState = NULL;
819 int instance = getminor((dev_t)pvArg);
820
821 switch (enmCmd)
822 {
823 case DDI_INFO_DEVT2DEVINFO:
824 {
825 /*
826 * One is to one mapping of instance & minor number as we publish only one minor node per device.
827 */
828 pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
829 if (pState)
830 {
831 *ppvResult = (void *)pState->pDip;
832 return DDI_SUCCESS;
833 }
834 else
835 LogRel((DEVICE_NAME ":VBoxUSBSolarisGetInfo failed to get device state.\n"));
836 return DDI_FAILURE;
837 }
838
839 case DDI_INFO_DEVT2INSTANCE:
840 {
841 *ppvResult = (void *)(uintptr_t)instance;
842 return DDI_SUCCESS;
843 }
844
845 default:
846 return DDI_FAILURE;
847 }
848}
849
850
851/**
852 * Callback invoked from the Monitor driver when a VM process tries to access
853 * this client instance. This determines which VM process will be allowed to
854 * open and access the USB device.
855 *
856 * @returns VBox status code.
857 *
858 * @param Process The VM process performing the client info. query.
859 * @param Instance This client instance (the one set while we register
860 * ourselves to the Monitor driver)
861 * @param pvReserved Reserved for future, unused.
862 */
863LOCAL int vboxUSBSolarisSetConsumerCredentials(RTPROCESS Process, int Instance, void *pvReserved)
864{
865 LogFunc((DEVICE_NAME ":vboxUSBSolarisSetConsumerCredentials Process=%u Instance=%d\n", Process, Instance));
866 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, Instance);
867 if (!pState)
868 {
869 LogRel((DEVICE_NAME ":vboxUSBSolarisSetConsumerCredentials failed to get device state for instance %d\n", Instance));
870 return VERR_INVALID_STATE;
871 }
872
873 int rc = VINF_SUCCESS;
874 mutex_enter(&pState->Mtx);
875
876 if (pState->Process == NIL_RTPROCESS)
877 pState->Process = Process;
878 else
879 {
880 LogRel((DEVICE_NAME ":vboxUSBSolarisSetConsumerCredentials failed! Process %u already has client open.\n",
881 pState->Process));
882 rc = VERR_RESOURCE_BUSY;
883 }
884
885 mutex_exit(&pState->Mtx);
886
887 return rc;
888}
889
890
891int VBoxUSBSolarisOpen(dev_t *pDev, int fFlag, int fType, cred_t *pCred)
892{
893 LogFunc((DEVICE_NAME ":VBoxUSBSolarisOpen pDev=%p fFlag=%d fType=%d pCred=%p\n", pDev, fFlag, fType, pCred));
894
895 /*
896 * Verify we are being opened as a character device
897 */
898 if (fType != OTYP_CHR)
899 return EINVAL;
900
901 /*
902 * One is to one mapping. (Minor<=>Instance).
903 */
904 int instance = getminor((dev_t)*pDev);
905 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
906 if (!pState)
907 {
908 LogRel((DEVICE_NAME ":VBoxUSBSolarisOpen failed to get device state for instance %d\n", instance));
909 return ENXIO;
910 }
911
912 mutex_enter(&pState->Mtx);
913
914 /*
915 * Only one user process can open a device instance at a time.
916 */
917 if (pState->Process != RTProcSelf())
918 {
919 if (pState->Process == NIL_RTPROCESS)
920 LogRel((DEVICE_NAME ":VBoxUSBSolarisOpen No prior information about authorized process.\n"));
921 else
922 LogRel((DEVICE_NAME ":VBoxUSBSolarisOpen Process %u is already using this device instance.\n", pState->Process));
923
924 mutex_exit(&pState->Mtx);
925 return EPERM;
926 }
927
928 pState->fPoll = VBOXUSB_POLL_ON;
929
930 mutex_exit(&pState->Mtx);
931
932 NOREF(fFlag);
933 NOREF(pCred);
934
935 return 0;
936}
937
938
939int VBoxUSBSolarisClose(dev_t Dev, int fFlag, int fType, cred_t *pCred)
940{
941 LogFunc((DEVICE_NAME ":VBoxUSBSolarisClose Dev=%d fFlag=%d fType=%d pCred=%p\n", Dev, fFlag, fType, pCred));
942
943 int instance = getminor((dev_t)Dev);
944 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
945 if (RT_UNLIKELY(!pState))
946 {
947 LogRel((DEVICE_NAME ":VBoxUSBSolarisClose failed to get device state for instance %d\n", instance));
948 return ENXIO;
949 }
950
951 mutex_enter(&pState->Mtx);
952 pState->fPoll = VBOXUSB_POLL_OFF;
953 pState->Process = NIL_RTPROCESS;
954 mutex_exit(&pState->Mtx);
955
956 return 0;
957}
958
959
960int VBoxUSBSolarisRead(dev_t Dev, struct uio *pUio, cred_t *pCred)
961{
962 LogFunc((DEVICE_NAME ":VBoxUSBSolarisRead\n"));
963 return ENOTSUP;
964}
965
966
967int VBoxUSBSolarisWrite(dev_t Dev, struct uio *pUio, cred_t *pCred)
968{
969 LogFunc((DEVICE_NAME ":VBoxUSBSolarisWrite\n"));
970 return ENOTSUP;
971}
972
973
974int VBoxUSBSolarisPoll(dev_t Dev, short fEvents, int fAnyYet, short *pReqEvents, struct pollhead **ppPollHead)
975{
976 LogFunc((DEVICE_NAME ":VBoxUSBSolarisPoll Dev=%d fEvents=%d fAnyYet=%d pReqEvents=%p\n", Dev, fEvents, fAnyYet, pReqEvents));
977
978 /*
979 * Get the device state (one to one mapping).
980 */
981 int instance = getminor((dev_t)Dev);
982 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
983 if (RT_UNLIKELY(!pState))
984 {
985 LogRel((DEVICE_NAME ":VBoxUSBSolarisPoll: no state data for %d\n", instance));
986 return ENXIO;
987 }
988
989 mutex_enter(&pState->Mtx);
990
991 /*
992 * "fEvents" HAS to be POLLIN. We won't bother to test it. The caller
993 * must always requests input events. Disconnect event (POLLHUP) is invalid in "fEvents".
994 */
995 fEvents = 0;
996 if (pState->fPoll & VBOXUSB_POLL_DEV_UNPLUGGED)
997 {
998 fEvents |= POLLHUP;
999 pState->fPoll &= ~VBOXUSB_POLL_DEV_UNPLUGGED;
1000 }
1001
1002 if (pState->fPoll & VBOXUSB_POLL_REAP_PENDING)
1003 {
1004 fEvents |= POLLIN;
1005 pState->fPoll &= ~VBOXUSB_POLL_REAP_PENDING;
1006 }
1007
1008 if ( !fEvents
1009 && !fAnyYet)
1010 {
1011 *ppPollHead = &pState->PollHead;
1012 }
1013
1014 *pReqEvents = fEvents;
1015
1016 mutex_exit(&pState->Mtx);
1017
1018 return 0;
1019}
1020
1021
1022int VBoxUSBSolarisPower(dev_info_t *pDip, int Component, int Level)
1023{
1024 LogFunc((DEVICE_NAME ":VBoxUSBSolarisPower pDip=%p Component=%d Level=%d\n", pDip, Component, Level));
1025
1026 int instance = ddi_get_instance(pDip);
1027 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1028 if (RT_UNLIKELY(!pState))
1029 {
1030 LogRel((DEVICE_NAME ":VBoxUSBSolarisPower Failed! missing state.\n"));
1031 return DDI_FAILURE;
1032 }
1033
1034 if (!pState->pPower)
1035 return DDI_SUCCESS;
1036
1037 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
1038 mutex_enter(&pState->Mtx);
1039
1040 int rc = USB_FAILURE;
1041 if (pState->DevState == USB_DEV_ONLINE)
1042 {
1043 /*
1044 * Check if we are transitioning to a valid power state.
1045 */
1046 if (!USB_DEV_PWRSTATE_OK(pState->pPower->PowerStates, Level))
1047 {
1048 switch (Level)
1049 {
1050 case USB_DEV_OS_PWR_OFF:
1051 {
1052 if (pState->pPower->PowerBusy)
1053 break;
1054
1055 /*
1056 * USB D3 command.
1057 */
1058 pState->pPower->PowerLevel = USB_DEV_OS_PWR_OFF;
1059 mutex_exit(&pState->Mtx);
1060 rc = usb_set_device_pwrlvl3(pDip);
1061 mutex_enter(&pState->Mtx);
1062 break;
1063 }
1064
1065 case USB_DEV_OS_FULL_PWR:
1066 {
1067 /*
1068 * Can happen during shutdown of the OS.
1069 */
1070 pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
1071 mutex_exit(&pState->Mtx);
1072 rc = usb_set_device_pwrlvl0(pDip);
1073 mutex_enter(&pState->Mtx);
1074 break;
1075 }
1076
1077 default: /* Power levels 1, 2 not implemented */
1078 break;
1079 }
1080 }
1081 else
1082 Log((DEVICE_NAME ":USB_DEV_PWRSTATE_OK failed.\n"));
1083 }
1084 else
1085 rc = USB_SUCCESS;
1086
1087 mutex_exit(&pState->Mtx);
1088 usb_release_access(pState->StateMulti);
1089 return rc == USB_SUCCESS ? DDI_SUCCESS : DDI_FAILURE;
1090}
1091
1092
1093/** @def IOCPARM_LEN
1094 * Gets the length from the ioctl number.
1095 * This is normally defined by sys/ioccom.h on BSD systems...
1096 */
1097#ifndef IOCPARM_LEN
1098# define IOCPARM_LEN(Code) (((Code) >> 16) & IOCPARM_MASK)
1099#endif
1100
1101int VBoxUSBSolarisIOCtl(dev_t Dev, int Cmd, intptr_t pArg, int Mode, cred_t *pCred, int *pVal)
1102{
1103/* LogFunc((DEVICE_NAME ":VBoxUSBSolarisIOCtl Dev=%d Cmd=%d pArg=%p Mode=%d\n", Dev, Cmd, pArg)); */
1104
1105 /*
1106 * Get the device state (one to one mapping).
1107 */
1108 int instance = getminor((dev_t)Dev);
1109 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
1110 if (RT_UNLIKELY(!pState))
1111 {
1112 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: no state data for %d\n", instance));
1113 return EINVAL;
1114 }
1115
1116 /*
1117 * Read the request wrapper.
1118 */
1119 VBOXUSBREQ ReqWrap;
1120 if (IOCPARM_LEN(Cmd) != sizeof(ReqWrap))
1121 {
1122 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad request %#x size=%d expected=%d\n", Cmd, IOCPARM_LEN(Cmd),
1123 sizeof(ReqWrap)));
1124 return ENOTTY;
1125 }
1126
1127 int rc = ddi_copyin((void *)pArg, &ReqWrap, sizeof(ReqWrap), Mode);
1128 if (RT_UNLIKELY(rc))
1129 {
1130 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: ddi_copyin failed to read header pArg=%p Cmd=%d. rc=%d.\n", pArg, Cmd, rc));
1131 return EINVAL;
1132 }
1133
1134 if (ReqWrap.u32Magic != VBOXUSB_MAGIC)
1135 {
1136 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad magic %#x; pArg=%p Cmd=%d.\n", ReqWrap.u32Magic, pArg, Cmd));
1137 return EINVAL;
1138 }
1139 if (RT_UNLIKELY( ReqWrap.cbData == 0
1140 || ReqWrap.cbData > _1M*16))
1141 {
1142 LogRel((DEVICE_NAME ": VBoxUSBSolarisIOCtl: bad size %#x; pArg=%p Cmd=%d.\n", ReqWrap.cbData, pArg, Cmd));
1143 return EINVAL;
1144 }
1145
1146 /*
1147 * Read the request.
1148 */
1149 void *pvBuf = RTMemTmpAlloc(ReqWrap.cbData);
1150 if (RT_UNLIKELY(!pvBuf))
1151 {
1152 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: RTMemTmpAlloc failed to alloc %d bytes.\n", ReqWrap.cbData));
1153 return ENOMEM;
1154 }
1155
1156 rc = ddi_copyin((void *)(uintptr_t)ReqWrap.pvDataR3, pvBuf, ReqWrap.cbData, Mode);
1157 if (RT_UNLIKELY(rc))
1158 {
1159 RTMemTmpFree(pvBuf);
1160 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyin failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg, Cmd, rc));
1161 return EFAULT;
1162 }
1163 if (RT_UNLIKELY( ReqWrap.cbData == 0
1164 || pvBuf == NULL))
1165 {
1166 RTMemTmpFree(pvBuf);
1167 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: invalid request pvBuf=%p cbData=%d\n", pvBuf, ReqWrap.cbData));
1168 return EINVAL;
1169 }
1170
1171 /*
1172 * Process the IOCtl.
1173 */
1174 size_t cbDataOut;
1175 rc = vboxUSBSolarisProcessIOCtl(Cmd, pState, Mode, &ReqWrap, pvBuf, &cbDataOut);
1176 ReqWrap.rc = rc;
1177 rc = 0;
1178
1179 if (RT_UNLIKELY(cbDataOut > ReqWrap.cbData))
1180 {
1181 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: too much output data %d expected %d Truncating!\n", cbDataOut,
1182 ReqWrap.cbData));
1183 cbDataOut = ReqWrap.cbData;
1184 }
1185
1186 ReqWrap.cbData = cbDataOut;
1187
1188 /*
1189 * Copy VBOXUSBREQ back to userspace (which contains rc for USB operation).
1190 */
1191 rc = ddi_copyout(&ReqWrap, (void *)pArg, sizeof(ReqWrap), Mode);
1192 if (RT_LIKELY(!rc))
1193 {
1194 /*
1195 * Copy payload (if any) back to userspace.
1196 */
1197 if (cbDataOut > 0)
1198 {
1199 rc = ddi_copyout(pvBuf, (void *)(uintptr_t)ReqWrap.pvDataR3, cbDataOut, Mode);
1200 if (RT_UNLIKELY(rc))
1201 {
1202 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyout failed; pvBuf=%p pArg=%p Cmd=%d. rc=%d\n", pvBuf, pArg,
1203 Cmd, rc));
1204 rc = EFAULT;
1205 }
1206 }
1207 }
1208 else
1209 {
1210 LogRel((DEVICE_NAME ":VBoxUSBSolarisIOCtl: ddi_copyout(1)failed; pReqWrap=%p pArg=%p Cmd=%d. rc=%d\n", &ReqWrap, pArg,
1211 Cmd, rc));
1212 rc = EFAULT;
1213 }
1214
1215 *pVal = rc;
1216 RTMemTmpFree(pvBuf);
1217 return rc;
1218}
1219
1220
1221/**
1222 * IOCtl processor for user to kernel and kernel to kernel communication.
1223 *
1224 * @returns VBox status code.
1225 *
1226 * @param iFunction The requested function.
1227 * @param pvState The USB device instance.
1228 * @param Mode The IOCtl mode.
1229 * @param pUSBReq Pointer to the VBOXUSB request.
1230 * @param pvBuf Pointer to the ring-3 URB.
1231 * @param pcbDataOut Where to store the IOCtl OUT data size.
1232 */
1233LOCAL int vboxUSBSolarisProcessIOCtl(int iFunction, void *pvState, int Mode, PVBOXUSBREQ pUSBReq, void *pvBuf, size_t *pcbDataOut)
1234{
1235// LogFunc((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl iFunction=%d pvState=%p pUSBReq=%p\n", iFunction, pvState, pUSBReq));
1236
1237 AssertPtrReturn(pvState, VERR_INVALID_PARAMETER);
1238 vboxusb_state_t *pState = (vboxusb_state_t *)pvState;
1239 size_t cbData = pUSBReq->cbData;
1240 int rc;
1241
1242#define CHECKRET_MIN_SIZE(mnemonic, cbMin) \
1243 do { \
1244 if (cbData < (cbMin)) \
1245 { \
1246 LogRel((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: " mnemonic ": cbData=%#zx (%zu) min is %#zx (%zu)\n", \
1247 cbData, cbData, (size_t)(cbMin), (size_t)(cbMin))); \
1248 return VERR_BUFFER_OVERFLOW; \
1249 } \
1250 if ((cbMin) != 0 && !VALID_PTR(pvBuf)) \
1251 { \
1252 LogRel((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: " mnemonic ": Invalid pointer %p\n", pvBuf)); \
1253 return VERR_INVALID_PARAMETER; \
1254 } \
1255 } while (0)
1256
1257 switch (iFunction)
1258 {
1259 case VBOXUSB_IOCTL_SEND_URB:
1260 {
1261 CHECKRET_MIN_SIZE("SEND_URB", sizeof(VBOXUSBREQ_URB));
1262
1263 PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1264 rc = vboxUSBSolarisSendURB(pState, pUrbReq, Mode);
1265 *pcbDataOut = 0;
1266 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SEND_URB returned %d\n", rc));
1267 break;
1268 }
1269
1270 case VBOXUSB_IOCTL_REAP_URB:
1271 {
1272 CHECKRET_MIN_SIZE("REAP_URB", sizeof(VBOXUSBREQ_URB));
1273
1274 PVBOXUSBREQ_URB pUrbReq = (PVBOXUSBREQ_URB)pvBuf;
1275 rc = vboxUSBSolarisReapURB(pState, pUrbReq, Mode);
1276 *pcbDataOut = sizeof(VBOXUSBREQ_URB);
1277 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: REAP_URB returned %d\n", rc));
1278 break;
1279 }
1280
1281 case VBOXUSB_IOCTL_CLEAR_EP:
1282 {
1283 CHECKRET_MIN_SIZE("CLEAR_EP", sizeof(VBOXUSBREQ_CLEAR_EP));
1284
1285 PVBOXUSBREQ_CLEAR_EP pClearEpReq = (PVBOXUSBREQ_CLEAR_EP)pvBuf;
1286 rc = vboxUSBSolarisClearEndPoint(pState, pClearEpReq->bEndpoint);
1287 *pcbDataOut = 0;
1288 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: CLEAR_EP returned %d\n", rc));
1289 break;
1290 }
1291
1292 case VBOXUSB_IOCTL_SET_CONFIG:
1293 {
1294 CHECKRET_MIN_SIZE("SET_CONFIG", sizeof(VBOXUSBREQ_SET_CONFIG));
1295
1296 PVBOXUSBREQ_SET_CONFIG pSetCfgReq = (PVBOXUSBREQ_SET_CONFIG)pvBuf;
1297 rc = vboxUSBSolarisSetConfig(pState, pSetCfgReq->bConfigValue);
1298 *pcbDataOut = 0;
1299 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SET_CONFIG returned %d\n", rc));
1300 break;
1301 }
1302
1303 case VBOXUSB_IOCTL_SET_INTERFACE:
1304 {
1305 CHECKRET_MIN_SIZE("SET_INTERFACE", sizeof(VBOXUSBREQ_SET_INTERFACE));
1306
1307 PVBOXUSBREQ_SET_INTERFACE pSetInterfaceReq = (PVBOXUSBREQ_SET_INTERFACE)pvBuf;
1308 rc = vboxUSBSolarisSetInterface(pState, pSetInterfaceReq->bInterface, pSetInterfaceReq->bAlternate);
1309 *pcbDataOut = 0;
1310 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: SET_INTERFACE returned %d\n", rc));
1311 break;
1312 }
1313
1314 case VBOXUSB_IOCTL_CLOSE_DEVICE:
1315 {
1316 CHECKRET_MIN_SIZE("CLOSE_DEVICE", sizeof(VBOXUSBREQ_CLOSE_DEVICE));
1317
1318 PVBOXUSBREQ_CLOSE_DEVICE pCloseDeviceReq = (PVBOXUSBREQ_CLOSE_DEVICE)pvBuf;
1319 rc = vboxUSBSolarisCloseDevice(pState, pCloseDeviceReq->ResetLevel);
1320 *pcbDataOut = 0;
1321 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: CLOSE_DEVICE returned %d\n", rc));
1322 break;
1323 }
1324
1325 case VBOXUSB_IOCTL_ABORT_PIPE:
1326 {
1327 CHECKRET_MIN_SIZE("ABORT_PIPE", sizeof(VBOXUSBREQ_ABORT_PIPE));
1328
1329 PVBOXUSBREQ_ABORT_PIPE pAbortPipeReq = (PVBOXUSBREQ_ABORT_PIPE)pvBuf;
1330 rc = vboxUSBSolarisAbortPipe(pState, pAbortPipeReq->bEndpoint);
1331 *pcbDataOut = 0;
1332 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: ABORT_PIPE returned %d\n", rc));
1333 break;
1334 }
1335
1336 case VBOXUSB_IOCTL_GET_CONFIG:
1337 {
1338 CHECKRET_MIN_SIZE("GET_CONFIG", sizeof(VBOXUSBREQ_GET_CONFIG));
1339
1340 PVBOXUSBREQ_GET_CONFIG pGetCfgReq = (PVBOXUSBREQ_GET_CONFIG)pvBuf;
1341 rc = vboxUSBSolarisGetConfig(pState, &pGetCfgReq->bConfigValue);
1342 *pcbDataOut = sizeof(VBOXUSBREQ_GET_CONFIG);
1343 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: GET_CONFIG returned %d\n", rc));
1344 break;
1345 }
1346
1347 case VBOXUSB_IOCTL_GET_VERSION:
1348 {
1349 CHECKRET_MIN_SIZE("GET_VERSION", sizeof(VBOXUSBREQ_GET_VERSION));
1350
1351 PVBOXUSBREQ_GET_VERSION pGetVersionReq = (PVBOXUSBREQ_GET_VERSION)pvBuf;
1352 pGetVersionReq->u32Major = VBOXUSB_VERSION_MAJOR;
1353 pGetVersionReq->u32Minor = VBOXUSB_VERSION_MINOR;
1354 *pcbDataOut = sizeof(VBOXUSBREQ_GET_VERSION);
1355 rc = VINF_SUCCESS;
1356 Log((DEVICE_NAME ":vboxUSBSolarisProcessIOCtl: GET_VERSION returned %d\n", rc));
1357 break;
1358 }
1359
1360 default:
1361 {
1362 LogRel((DEVICE_NAME ":solarisUSBProcessIOCtl: Unknown request %#x\n", iFunction));
1363 rc = VERR_NOT_SUPPORTED;
1364 *pcbDataOut = 0;
1365 break;
1366 }
1367 }
1368
1369 pUSBReq->cbData = *pcbDataOut;
1370 return rc;
1371}
1372
1373
1374/**
1375 * Initialize device power management functions.
1376 *
1377 * @param pState The USB device instance.
1378 *
1379 * @returns VBox status code.
1380 */
1381LOCAL int vboxUSBSolarisInitPower(vboxusb_state_t *pState)
1382{
1383 LogFunc((DEVICE_NAME ":vboxUSBSolarisInitPower pState=%p\n", pState));
1384
1385 int rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_ENABLE);
1386 if (rc == USB_SUCCESS)
1387 {
1388 vboxusb_power_t *pPower = RTMemAlloc(sizeof(vboxusb_power_t));
1389 if (RT_LIKELY(pPower))
1390 {
1391 mutex_enter(&pState->Mtx);
1392 pState->pPower = pPower;
1393 pState->pPower->fPowerWakeup = false;
1394 mutex_exit(&pState->Mtx);
1395
1396 uint_t PowerStates;
1397 rc = usb_create_pm_components(pState->pDip, &PowerStates);
1398 if (rc == USB_SUCCESS)
1399 {
1400 pState->pPower->fPowerWakeup = true;
1401 pState->pPower->PowerLevel = USB_DEV_OS_FULL_PWR;
1402 pState->pPower->PowerStates = PowerStates;
1403
1404 rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1405
1406 if (rc != DDI_SUCCESS)
1407 {
1408 LogRel((DEVICE_NAME ":vboxUSBSolarisInitPower failed to raise power level usb(%#x,%#x).\n",
1409 pState->pDevDesc->dev_descr->idVendor, pState->pDevDesc->dev_descr->idProduct));
1410 }
1411 }
1412 else
1413 Log((DEVICE_NAME ":vboxUSBSolarisInitPower failed to create power components.\n"));
1414
1415 return VINF_SUCCESS;
1416 }
1417 else
1418 rc = VERR_NO_MEMORY;
1419 }
1420 else
1421 {
1422 Log((DEVICE_NAME ":vboxUSBSolarisInitPower failed to enable remote wakeup. No PM.\n"));
1423 rc = VINF_SUCCESS;
1424 }
1425
1426 return rc;
1427}
1428
1429
1430/**
1431 * Destroy device power management functions.
1432 *
1433 * @param pState The USB device instance.
1434 * @remarks Requires the device state mutex to be held.
1435 *
1436 * @returns VBox status code.
1437 */
1438LOCAL void vboxUSBSolarisDestroyPower(vboxusb_state_t *pState)
1439{
1440 LogFunc((DEVICE_NAME ":vboxUSBSolarisDestroyPower pState=%p\n", pState));
1441
1442 if (pState->pPower)
1443 {
1444 mutex_exit(&pState->Mtx);
1445 vboxUSBSolarisPowerBusy(pState);
1446 mutex_enter(&pState->Mtx);
1447
1448 int rc = -1;
1449 if ( pState->pPower->fPowerWakeup
1450 && pState->DevState != USB_DEV_DISCONNECTED)
1451 {
1452 mutex_exit(&pState->Mtx);
1453 rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
1454 if (rc != DDI_SUCCESS)
1455 Log((DEVICE_NAME ":vboxUSBSolarisDestroyPower raising power failed! rc=%d\n", rc));
1456
1457 rc = usb_handle_remote_wakeup(pState->pDip, USB_REMOTE_WAKEUP_DISABLE);
1458 if (rc != DDI_SUCCESS)
1459 Log((DEVICE_NAME ":vboxUSBSolarisDestroyPower failed to disable remote wakeup.\n"));
1460 }
1461 else
1462 mutex_exit(&pState->Mtx);
1463
1464 rc = pm_lower_power(pState->pDip, 0 /* component */, USB_DEV_OS_PWR_OFF);
1465 if (rc != DDI_SUCCESS)
1466 Log((DEVICE_NAME ":vboxUSBSolarisDestroyPower lowering power failed! rc=%d\n", rc));
1467
1468 vboxUSBSolarisPowerIdle(pState);
1469 mutex_enter(&pState->Mtx);
1470 RTMemFree(pState->pPower);
1471 pState->pPower = NULL;
1472 }
1473}
1474
1475
1476/**
1477 * Convert Solaris' USBA URB status to VBox's USB URB status.
1478 *
1479 * @param Status Solaris USBA USB URB status.
1480 *
1481 * @returns VBox USB URB status.
1482 */
1483static inline VUSBSTATUS vboxUSBSolarisGetUrbStatus(usb_cr_t Status)
1484{
1485 switch (Status)
1486 {
1487 case USB_CR_OK: return VUSBSTATUS_OK;
1488 case USB_CR_CRC: return VUSBSTATUS_CRC;
1489 case USB_CR_DEV_NOT_RESP: return VUSBSTATUS_DNR;
1490 case USB_CR_DATA_UNDERRUN: return VUSBSTATUS_DATA_UNDERRUN;
1491 case USB_CR_DATA_OVERRUN: return VUSBSTATUS_DATA_OVERRUN;
1492 case USB_CR_STALL: return VUSBSTATUS_STALL;
1493 /*
1494 case USB_CR_BITSTUFFING:
1495 case USB_CR_DATA_TOGGLE_MM:
1496 case USB_CR_PID_CHECKFAILURE:
1497 case USB_CR_UNEXP_PID:
1498 case USB_CR_BUFFER_OVERRUN:
1499 case USB_CR_BUFFER_UNDERRUN:
1500 case USB_CR_TIMEOUT:
1501 case USB_CR_NOT_ACCESSED:
1502 case USB_CR_NO_RESOURCES:
1503 case USB_CR_UNSPECIFIED_ERR:
1504 case USB_CR_STOPPED_POLLING:
1505 case USB_CR_PIPE_CLOSING:
1506 case USB_CR_PIPE_RESET:
1507 case USB_CR_NOT_SUPPORTED:
1508 case USB_CR_FLUSHED:
1509 case USB_CR_HC_HARDWARE_ERR:
1510 */
1511 default: return VUSBSTATUS_INVALID;
1512 }
1513}
1514
1515
1516/**
1517 * Convert Solaris' USBA error code to VBox's error code.
1518 *
1519 * @param UsbRc Solaris USBA error code.
1520 *
1521 * @returns VBox error code.
1522 */
1523static inline int vboxUSBSolarisToVBoxRC(int UsbRc)
1524{
1525 switch (UsbRc)
1526 {
1527 case USB_SUCCESS: return VINF_SUCCESS;
1528 case USB_INVALID_ARGS: return VERR_INVALID_PARAMETER;
1529 case USB_INVALID_PIPE: return VERR_BAD_PIPE;
1530 case USB_INVALID_CONTEXT: return VERR_INVALID_CONTEXT;
1531 case USB_BUSY: return VERR_PIPE_BUSY;
1532 case USB_PIPE_ERROR: return VERR_PIPE_IO_ERROR;
1533 /*
1534 case USB_FAILURE:
1535 case USB_NO_RESOURCES:
1536 case USB_NO_BANDWIDTH:
1537 case USB_NOT_SUPPORTED:
1538 case USB_PIPE_ERROR:
1539 case USB_NO_FRAME_NUMBER:
1540 case USB_INVALID_START_FRAME:
1541 case USB_HC_HARDWARE_ERROR:
1542 case USB_INVALID_REQUEST:
1543 case USB_INVALID_VERSION:
1544 case USB_INVALID_PERM:
1545 */
1546 default: return VERR_GENERAL_FAILURE;
1547 }
1548}
1549
1550
1551/**
1552 * Convert Solaris' USBA device state to VBox's error code.
1553 *
1554 * @param UsbRc Solaris USBA error code.
1555 *
1556 * @returns VBox error code.
1557 */
1558static inline int vboxUSBSolarisDeviceState(uint8_t uDeviceState)
1559{
1560 switch (uDeviceState)
1561 {
1562 case USB_DEV_ONLINE: return VINF_SUCCESS;
1563 case USB_DEV_SUSPENDED: return VERR_VUSB_DEVICE_IS_SUSPENDED;
1564 case USB_DEV_DISCONNECTED:
1565 case USB_DEV_PWRED_DOWN: return VERR_VUSB_DEVICE_NOT_ATTACHED;
1566 default: return VERR_GENERAL_FAILURE;
1567 }
1568}
1569
1570
1571/**
1572 * Check if the device is a USB device.
1573 *
1574 * @param pDip Pointer to this device info. structure.
1575 *
1576 * @returns If this is really a USB device returns true, otherwise false.
1577 */
1578LOCAL bool vboxUSBSolarisIsUSBDevice(dev_info_t *pDip)
1579{
1580 int rc = DDI_FAILURE;
1581
1582 /*
1583 * Check device for "usb" compatible property, root hubs->device would likely mean parent has no "usb" property.
1584 */
1585 char **ppszCompatible = NULL;
1586 uint_t cCompatible;
1587 rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible, &cCompatible);
1588 if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1589 {
1590 while (cCompatible--)
1591 {
1592 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice compatible[%d]=%s\n", cCompatible, ppszCompatible[cCompatible]));
1593 if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1594 {
1595 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice verified device as USB. pszCompatible=%s\n",
1596 ppszCompatible[cCompatible]));
1597 ddi_prop_free(ppszCompatible);
1598 return true;
1599 }
1600 }
1601
1602 ddi_prop_free(ppszCompatible);
1603 ppszCompatible = NULL;
1604 }
1605 else
1606 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice USB property lookup failed. rc=%d\n", rc));
1607
1608 /*
1609 * Check parent for "usb" compatible property.
1610 */
1611 dev_info_t *pParentDip = ddi_get_parent(pDip);
1612 if (pParentDip)
1613 {
1614 rc = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, pParentDip, DDI_PROP_DONTPASS, "compatible", &ppszCompatible,
1615 &cCompatible);
1616 if (RT_LIKELY(rc == DDI_PROP_SUCCESS))
1617 {
1618 while (cCompatible--)
1619 {
1620 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice parent compatible[%d]=%s\n", cCompatible,
1621 ppszCompatible[cCompatible]));
1622 if (!strncmp(ppszCompatible[cCompatible], "usb", 3))
1623 {
1624 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice verified device as USB. parent pszCompatible=%s\n",
1625 ppszCompatible[cCompatible]));
1626 ddi_prop_free(ppszCompatible);
1627 return true;
1628 }
1629 }
1630
1631 ddi_prop_free(ppszCompatible);
1632 ppszCompatible = NULL;
1633 }
1634 else
1635 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice USB parent property lookup failed. rc=%d\n", rc));
1636 }
1637 else
1638 Log((DEVICE_NAME ":vboxUSBSolarisIsUSBDevice failed to obtain parent device for property lookup.\n"));
1639
1640 return false;
1641}
1642
1643
1644/**
1645 * Submit a URB.
1646 *
1647 * @param pState The USB device instance.
1648 * @param pUrbReq Pointer to the VBox USB URB.
1649 * @param Mode The IOCtl mode.
1650 *
1651 * @returns VBox error code.
1652 */
1653LOCAL int vboxUSBSolarisSendURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1654{
1655 uchar_t EndPtIndex = usb_get_ep_index(pUrbReq->bEndpoint);
1656 vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
1657 AssertPtrReturn(pEp, VERR_INVALID_POINTER);
1658
1659 /* LogFunc((DEVICE_NAME ":vboxUSBSolarisSendUrb pState=%p pUrbReq=%p bEndpoint=%#x[%d] enmDir=%#x enmType=%#x cbData=%d pvData=%p\n",
1660 pState, pUrbReq, pUrbReq->bEndpoint, EndPtIndex, pUrbReq->enmDir, pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData)); */
1661
1662 if (RT_UNLIKELY(!pUrbReq->pvData))
1663 {
1664 LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb Invalid request. No data.\n"));
1665 return VERR_INVALID_POINTER;
1666 }
1667
1668 /*
1669 * Allocate message block & copy userspace buffer for host to device Xfers and for
1670 * Control Xfers (since input has Setup header that needs copying).
1671 */
1672 mblk_t *pMsg = NULL;
1673 int rc = VINF_SUCCESS;
1674 if ( pUrbReq->enmDir == VUSBDIRECTION_OUT
1675 || pUrbReq->enmType == VUSBXFERTYPE_MSG)
1676 {
1677 pMsg = allocb(pUrbReq->cbData, BPRI_HI);
1678 if (RT_UNLIKELY(!pMsg))
1679 {
1680 LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb: failed to allocate %d bytes\n", pUrbReq->cbData));
1681 return VERR_NO_MEMORY;
1682 }
1683
1684 rc = ddi_copyin(pUrbReq->pvData, pMsg->b_wptr, pUrbReq->cbData, Mode);
1685 if (RT_UNLIKELY(rc))
1686 {
1687 LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb: ddi_copyin failed! rc=%d\n", rc));
1688 freemsg(pMsg);
1689 return VERR_NO_MEMORY;
1690 }
1691
1692 pMsg->b_wptr += pUrbReq->cbData;
1693 }
1694
1695 mutex_enter(&pState->Mtx);
1696 rc = vboxUSBSolarisDeviceState(pState->DevState);
1697
1698 if (pState->fClosed) /* Required for Isoc. IN Xfers which don't Xfer through the pipe after polling starts */
1699 rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1700
1701 if (RT_SUCCESS(rc))
1702 {
1703 /*
1704 * Open the pipe if needed.
1705 */
1706 rc = vboxUSBSolarisOpenPipe(pState, pEp);
1707 if (RT_UNLIKELY(RT_FAILURE(rc)))
1708 {
1709 mutex_exit(&pState->Mtx);
1710 freemsg(pMsg);
1711 LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb OpenPipe failed. pState=%p pUrbReq=%p bEndpoint=%#x enmDir=%#x "
1712 "enmType=%#x cbData=%d pvData=%p rc=%d\n", pState, pUrbReq, pUrbReq->bEndpoint, pUrbReq->enmDir,
1713 pUrbReq->enmType, pUrbReq->cbData, pUrbReq->pvData, rc));
1714 return VERR_BAD_PIPE;
1715 }
1716
1717 mutex_exit(&pState->Mtx);
1718
1719 vboxusb_urb_t *pUrb = NULL;
1720 if ( pUrbReq->enmType == VUSBXFERTYPE_ISOC
1721 && pUrbReq->enmDir == VUSBDIRECTION_IN)
1722 pUrb = vboxUSBSolarisGetIsocInURB(pState, pUrbReq);
1723 else
1724 pUrb = vboxUSBSolarisQueueURB(pState, pUrbReq, pMsg);
1725
1726 if (RT_LIKELY(pUrb))
1727 {
1728 switch (pUrb->enmType)
1729 {
1730 case VUSBXFERTYPE_MSG:
1731 {
1732 rc = vboxUSBSolarisCtrlXfer(pState, pEp, pUrb);
1733 break;
1734 }
1735
1736 case VUSBXFERTYPE_BULK:
1737 {
1738 rc = vboxUSBSolarisBulkXfer(pState, pEp, pUrb);
1739 break;
1740 }
1741
1742 case VUSBXFERTYPE_INTR:
1743 {
1744 rc = vboxUSBSolarisIntrXfer(pState, pEp, pUrb);
1745 break;
1746 }
1747
1748 case VUSBXFERTYPE_ISOC:
1749 {
1750 rc = vboxUSBSolarisIsocXfer(pState, pEp, pUrb);
1751 break;
1752 }
1753
1754 default:
1755 {
1756 rc = VERR_NOT_SUPPORTED;
1757 break;
1758 }
1759 }
1760
1761 if (RT_FAILURE(rc))
1762 {
1763 /** @todo We share the state mutex for protecting concurrent accesses to both
1764 * the inflight URB list as well as pUrb->pMsg (data). Probably make this
1765 * more fine grained later by having a different mutex for the URB if
1766 * it's really worth the trouble. */
1767 mutex_enter(&pState->Mtx);
1768 if (pUrb->pMsg)
1769 {
1770 freemsg(pUrb->pMsg);
1771 pUrb->pMsg = NULL;
1772 }
1773
1774 if ( pUrb->enmType == VUSBXFERTYPE_ISOC
1775 && pUrb->enmDir == VUSBDIRECTION_IN)
1776 {
1777 RTMemFree(pUrb);
1778 pUrb = NULL;
1779 }
1780 else
1781 {
1782 pUrb->pMsg = NULL;
1783 pUrb->enmState = VBOXUSB_URB_STATE_FREE;
1784 }
1785 mutex_exit(&pState->Mtx);
1786 }
1787 }
1788 else
1789 {
1790 LogRel((DEVICE_NAME ":vboxUSBSolarisSendUrb failed to queue URB.\n"));
1791 rc = VERR_NO_MEMORY;
1792 }
1793
1794 if ( RT_FAILURE(rc)
1795 && pUrb)
1796 {
1797 if ( pUrb->enmType != VUSBXFERTYPE_ISOC
1798 || pUrb->enmDir != VUSBDIRECTION_IN)
1799 {
1800 mutex_enter(&pState->Mtx);
1801 pState->cInflightUrbs--;
1802 mutex_exit(&pState->Mtx);
1803 }
1804 }
1805 }
1806 else
1807 {
1808 mutex_exit(&pState->Mtx);
1809 freemsg(pMsg);
1810 }
1811
1812 return rc;
1813}
1814
1815
1816/**
1817 * Reap a completed/error'd URB.
1818 *
1819 * @param pState The USB device instance.
1820 * @param pUrbReq Pointer to the VBox USB URB.
1821 * @param Mode The IOCtl mode.
1822 *
1823 * @returns VBox error code.
1824 */
1825LOCAL int vboxUSBSolarisReapURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, int Mode)
1826{
1827// LogFunc((DEVICE_NAME ":vboxUSBSolarisReapUrb pState=%p pUrbReq=%p\n", pState, pUrbReq));
1828
1829 AssertPtrReturn(pUrbReq, VERR_INVALID_POINTER);
1830
1831 int rc = VINF_SUCCESS;
1832 mutex_enter(&pState->Mtx);
1833 rc = vboxUSBSolarisDeviceState(pState->DevState);
1834 if (pState->fClosed)
1835 rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
1836 if (RT_SUCCESS(rc))
1837 {
1838 vboxusb_urb_t *pUrb = list_remove_head(&pState->hLandedUrbs);
1839
1840 /*
1841 * It is safe to access pUrb->pMsg outside the state mutex because this is from the landed URB list
1842 * and not the inflight URB list.
1843 */
1844 mutex_exit(&pState->Mtx);
1845 if (pUrb)
1846 {
1847 /*
1848 * Copy the URB which will then be copied to user-space.
1849 */
1850 pUrbReq->pvUrbR3 = pUrb->pvUrbR3;
1851 pUrbReq->bEndpoint = pUrb->bEndpoint;
1852 pUrbReq->enmType = pUrb->enmType;
1853 pUrbReq->enmDir = pUrb->enmDir;
1854 pUrbReq->enmStatus = pUrb->enmStatus;
1855
1856 if (RT_LIKELY(pUrb->pMsg))
1857 {
1858 /*
1859 * Chain copy the message back into the user buffer.
1860 */
1861 if (RT_LIKELY(pUrb->pvDataR3 != NIL_RTR3PTR))
1862 {
1863 size_t cbData = RT_MIN(msgdsize(pUrb->pMsg), pUrb->cbDataR3);
1864 pUrbReq->cbData = cbData;
1865 pUrbReq->pvData = (void *)pUrb->pvDataR3;
1866
1867 /*
1868 * Paranoia: we should have a single message block almost always.
1869 */
1870 if (RT_LIKELY(!pUrb->pMsg->b_cont && cbData > 0))
1871 {
1872 rc = ddi_copyout(pUrb->pMsg->b_rptr, (void *)pUrbReq->pvData, cbData, Mode);
1873 if (RT_UNLIKELY(rc != 0))
1874 {
1875 LogRel((DEVICE_NAME ":vboxUSBSolarisReapUrb ddi_copyout failed! rc=%d\n", rc));
1876 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1877 }
1878 }
1879 else
1880 {
1881 RTR3PTR pvDataR3 = pUrb->pvDataR3;
1882 mblk_t *pMsg = pUrb->pMsg;
1883 while (pMsg)
1884 {
1885 size_t cbMsg = MBLKL(pMsg);
1886 if (cbMsg > 0)
1887 {
1888 rc = ddi_copyout(pMsg->b_rptr, (void *)pvDataR3, cbMsg, Mode);
1889 if (RT_UNLIKELY(rc != 0))
1890 {
1891 LogRel((DEVICE_NAME ":vboxUSBSolarisReapUrb ddi_copyout (2) failed! rc=%d\n", rc));
1892 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1893 break;
1894 }
1895 }
1896
1897 pMsg = pMsg->b_cont;
1898 pvDataR3 += cbMsg;
1899 if ((pvDataR3 - pUrb->pvDataR3) >= cbData)
1900 break;
1901 }
1902 }
1903
1904 Log((DEVICE_NAME ":vboxUSBSolarisReapUrb pvUrbR3=%p pvDataR3=%p cbData=%d\n", pUrbReq->pvUrbR3,
1905 pUrbReq->pvData, pUrbReq->cbData));
1906 }
1907 else
1908 {
1909 pUrbReq->cbData = 0;
1910 rc = VERR_INVALID_POINTER;
1911 Log((DEVICE_NAME ":vboxUSBSolarisReapUrb missing pvDataR3!!\n"));
1912 }
1913
1914 /*
1915 * Free buffer allocated in VBOXUSB_IOCTL_SEND_URB.
1916 */
1917 freemsg(pUrb->pMsg);
1918 pUrb->pMsg = NULL;
1919 }
1920 else
1921 {
1922 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
1923 {
1924 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1925 pUrbReq->cbData = pUrb->cbDataR3;
1926 else
1927 {
1928 pUrbReq->enmStatus = VUSBSTATUS_INVALID;
1929 pUrbReq->cbData = 0;
1930 }
1931 }
1932 else
1933 {
1934 Log((DEVICE_NAME ":vboxUSBSolarisReapUrb missing message.\n"));
1935 pUrbReq->cbData = 0;
1936 }
1937 }
1938
1939 /*
1940 * Copy Isoc packet descriptors.
1941 */
1942 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
1943 {
1944 AssertCompile(sizeof(pUrbReq->aIsocPkts) == sizeof(pUrb->aIsocPkts));
1945 pUrbReq->cIsocPkts = pUrb->cIsocPkts;
1946#if 0
1947 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
1948 {
1949 pUrbReq->aIsocPkts[i].cbPkt = pUrb->aIsocPkts[i].cbPkt;
1950 pUrbReq->aIsocPkts[i].cbActPkt = pUrb->aIsocPkts[i].cbActPkt;
1951 pUrbReq->aIsocPkts[i].enmStatus = pUrb->aIsocPkts[i].enmStatus;
1952 }
1953#else
1954 bcopy(pUrb->aIsocPkts, pUrbReq->aIsocPkts, pUrb->cIsocPkts * sizeof(VUSBISOC_PKT_DESC));
1955#endif
1956
1957 if (pUrb->enmDir == VUSBDIRECTION_IN)
1958 {
1959 RTMemFree(pUrb);
1960 pUrb = NULL;
1961 }
1962 }
1963
1964 if (pUrb)
1965 {
1966 /*
1967 * Add URB back to the head of the free/inflight list.
1968 */
1969 pUrb->cbDataR3 = 0;
1970 pUrb->pvDataR3 = NIL_RTR3PTR;
1971 pUrb->enmState = VBOXUSB_URB_STATE_FREE;
1972 mutex_enter(&pState->Mtx);
1973 list_insert_head(&pState->hUrbs, pUrb);
1974 mutex_exit(&pState->Mtx);
1975 }
1976 }
1977 else
1978 pUrbReq->pvUrbR3 = NULL;
1979 }
1980 else
1981 mutex_exit(&pState->Mtx);
1982
1983 Log((DEVICE_NAME ":vboxUSBSolarisReapUrb returns %d\n", rc));
1984 return rc;
1985}
1986
1987
1988/**
1989 * Clear a pipe (CLEAR_FEATURE).
1990 *
1991 * @param pState The USB device instance.
1992 * @param bEndpoint The Endpoint address.
1993 *
1994 * @returns VBox error code.
1995 */
1996LOCAL int vboxUSBSolarisClearEndPoint(vboxusb_state_t *pState, uint8_t bEndpoint)
1997{
1998 LogFunc((DEVICE_NAME ":vboxUSBSolarisClearEndPoint pState=%p bEndpoint=%#x\n", pState, bEndpoint));
1999
2000 /*
2001 * Serialize access: single threaded per Endpoint, one request at a time.
2002 */
2003 mutex_enter(&pState->Mtx);
2004 int rc = vboxUSBSolarisDeviceState(pState->DevState);
2005 if (RT_SUCCESS(rc))
2006 {
2007 uchar_t EndPtIndex = usb_get_ep_index(bEndpoint);
2008 vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
2009 if (RT_LIKELY(pEp))
2010 {
2011 /*
2012 * Check if the endpoint is open to be cleared.
2013 */
2014 if (pEp->pPipe)
2015 {
2016 mutex_exit(&pState->Mtx);
2017#if 0
2018 /*
2019 * Asynchronous clear pipe.
2020 */
2021 rc = usb_clr_feature(pState->pDip, USB_DEV_REQ_RCPT_EP, USB_EP_HALT, bEndpoint,
2022 USB_FLAGS_NOSLEEP, /* Asynchronous */
2023 NULL, /* Completion callback */
2024 NULL); /* Exception callback */
2025#endif
2026 /*
2027 * Synchronous reset pipe.
2028 */
2029 usb_pipe_reset(pState->pDip, pEp->pPipe,
2030 USB_FLAGS_SLEEP, /* Synchronous */
2031 NULL, /* Completion callback */
2032 NULL); /* Exception callback */
2033
2034 mutex_enter(&pState->Mtx);
2035
2036 Log((DEVICE_NAME ":vboxUSBSolarisClearEndPoint bEndpoint=%#x[%d] returns %d\n", bEndpoint, EndPtIndex, rc));
2037
2038 rc = VINF_SUCCESS;
2039 }
2040 else
2041 {
2042 Log((DEVICE_NAME ":vboxUSBSolarisClearEndPoint not opened to be cleared. Faking success. bEndpoint=%#x.\n",
2043 bEndpoint));
2044 rc = VINF_SUCCESS;
2045 }
2046 }
2047 else
2048 {
2049 LogRel((DEVICE_NAME ":vboxUSBSolarisClearEndPoint Endpoint missing!! bEndpoint=%#x EndPtIndex=%d.\n", bEndpoint,
2050 EndPtIndex));
2051 rc = VERR_GENERAL_FAILURE;
2052 }
2053 }
2054 else
2055 Log((DEVICE_NAME ":vboxUSBSolarisClearEndPoint device state=%d not online.\n", pState->DevState));
2056
2057 mutex_exit(&pState->Mtx);
2058 return rc;
2059}
2060
2061
2062/**
2063 * Set configuration (SET_CONFIGURATION)
2064 *
2065 * @param pState The USB device instance.
2066 * @param uCfgValue The Configuration value.
2067 *
2068 * @returns VBox error code.
2069 */
2070LOCAL int vboxUSBSolarisSetConfig(vboxusb_state_t *pState, uint8_t bCfgValue)
2071{
2072 LogFunc((DEVICE_NAME ":vboxUSBSolarisSetConfig pState=%p bCfgValue=%#x\n", pState, bCfgValue));
2073
2074 /*
2075 * Serialize access: single threaded per Endpoint, one request at a time.
2076 */
2077 mutex_enter(&pState->Mtx);
2078 int rc = vboxUSBSolarisDeviceState(pState->DevState);
2079 if (RT_SUCCESS(rc))
2080 {
2081 vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
2082 int iCfgIndex = vboxUSBSolarisGetConfigIndex(pState, bCfgValue);
2083
2084 if (iCfgIndex >= 0)
2085 {
2086 /*
2087 * Switch Config synchronously.
2088 */
2089 mutex_exit(&pState->Mtx);
2090 rc = usb_set_cfg(pState->pDip, (uint_t)iCfgIndex, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
2091 mutex_enter(&pState->Mtx);
2092
2093 if (rc == USB_SUCCESS)
2094 {
2095 pState->fRestoreCfg = true;
2096 vboxUSBSolarisInitEndPointsForConfig(pState, iCfgIndex);
2097 rc = VINF_SUCCESS;
2098 }
2099 else
2100 {
2101 LogRel((DEVICE_NAME ":vboxUSBSolarisSetConfig usb_set_cfg failed for iCfgIndex=%#x bCfgValue=%#x rc=%d\n",
2102 iCfgIndex, bCfgValue, rc));
2103 rc = vboxUSBSolarisToVBoxRC(rc);
2104 }
2105 }
2106 else
2107 {
2108 LogRel((DEVICE_NAME ":vboxUSBSolarisSetConfig invalid iCfgIndex=%d bCfgValue=%#x\n", iCfgIndex, bCfgValue));
2109 rc = VERR_INVALID_HANDLE;
2110 }
2111 }
2112
2113 mutex_exit(&pState->Mtx);
2114
2115 return rc;
2116}
2117
2118
2119/**
2120 * Get configuration (GET_CONFIGURATION)
2121 *
2122 * @param pState The USB device instance.
2123 * @param pCfgValue Where to store the configuration value.
2124 *
2125 * @returns VBox error code.
2126 */
2127LOCAL int vboxUSBSolarisGetConfig(vboxusb_state_t *pState, uint8_t *pCfgValue)
2128{
2129 LogFunc((DEVICE_NAME ":vboxUSBSolarisGetConfig pState=%p pCfgValue=%p\n", pState, pCfgValue));
2130 AssertPtrReturn(pCfgValue, VERR_INVALID_POINTER);
2131
2132 /*
2133 * Solaris keeps the currently active configuration for the first time. Thus for the first request
2134 * we simply pass the cached configuration back to the user.
2135 */
2136 if (!pState->fGetCfgReqDone)
2137 {
2138 pState->fGetCfgReqDone = true;
2139 AssertPtrReturn(pState->pDevDesc, VERR_GENERAL_FAILURE);
2140 usb_cfg_data_t *pCurrCfg = pState->pDevDesc->dev_curr_cfg;
2141 if (pCurrCfg)
2142 {
2143 *pCfgValue = pCurrCfg->cfg_descr.bConfigurationValue;
2144 Log((DEVICE_NAME ":vboxUSBSolarisGetConfig cached config returned. CfgValue=%d\n", *pCfgValue));
2145 return VINF_SUCCESS;
2146 }
2147 }
2148
2149 /*
2150 * Get Config synchronously.
2151 */
2152 uint_t bCfgValue;
2153 int rc = usb_get_cfg(pState->pDip, &bCfgValue, USB_FLAGS_SLEEP);
2154 if (RT_LIKELY(rc == USB_SUCCESS))
2155 {
2156 *pCfgValue = bCfgValue;
2157 rc = VINF_SUCCESS;
2158 }
2159 else
2160 {
2161 LogRel((DEVICE_NAME ":vboxUSBSolarisGetConfig failed. rc=%d\n", rc));
2162 rc = vboxUSBSolarisToVBoxRC(rc);
2163 }
2164
2165 Log((DEVICE_NAME ":vboxUSBSolarisGetConfig returns %d CfgValue=%d\n", rc, *pCfgValue));
2166 return rc;
2167}
2168
2169
2170/**
2171 * Set interface (SET_INTERFACE)
2172 *
2173 * @param pState The USB device instance.
2174 * @param uInterface The Interface number.
2175 * @param uAlt The Alternate setting number.
2176 *
2177 * @returns VBox error code.
2178 */
2179LOCAL int vboxUSBSolarisSetInterface(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt)
2180{
2181 LogFunc((DEVICE_NAME ":vboxUSBSolarisSetInterface pState=%p uInterface=%#x uAlt=%#x\n", pState, uInterface, uAlt));
2182
2183 /*
2184 * Serialize access: single threaded per Endpoint, one request at a time.
2185 */
2186 mutex_enter(&pState->Mtx);
2187 int rc = vboxUSBSolarisDeviceState(pState->DevState);
2188 if (RT_SUCCESS(rc))
2189 {
2190 vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
2191
2192 /*
2193 * Set Interface & Alt setting synchronously.
2194 */
2195 mutex_exit(&pState->Mtx);
2196 rc = usb_set_alt_if(pState->pDip, uInterface, uAlt, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback data */);
2197 mutex_enter(&pState->Mtx);
2198
2199 if (rc == USB_SUCCESS)
2200 {
2201 vboxUSBSolarisInitEndPointsForInterfaceAlt(pState, uInterface, uAlt);
2202 rc = VINF_SUCCESS;
2203 }
2204 else
2205 {
2206 LogRel((DEVICE_NAME ":vboxUSBSolarisSetInterface usb_set_alt_if failed for uInterface=%#x bAlt=%#x rc=%d\n",
2207 uInterface, uAlt, rc));
2208 rc = vboxUSBSolarisToVBoxRC(rc);
2209 }
2210 }
2211
2212 mutex_exit(&pState->Mtx);
2213
2214 return rc;
2215}
2216
2217
2218/**
2219 * Close the USB device and reset it if required.
2220 *
2221 * @param pState The USB device instance.
2222 * @param ResetLevel The reset level.
2223 *
2224 * @returns VBox error code.
2225 */
2226LOCAL int vboxUSBSolarisCloseDevice(vboxusb_state_t *pState, VBOXUSB_RESET_LEVEL enmReset)
2227{
2228 Log((DEVICE_NAME ":vboxUSBSolarisCloseDevice pState=%p enmReset=%d\n", pState, enmReset));
2229
2230 /*
2231 * Serialize access: single threaded per Endpoint, one request at a time.
2232 */
2233 mutex_enter(&pState->Mtx);
2234 int rc = vboxUSBSolarisDeviceState(pState->DevState);
2235
2236 if (enmReset == VBOXUSB_RESET_LEVEL_NONE)
2237 {
2238 vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
2239 pState->fClosed = true;
2240 }
2241 else
2242 vboxUSBSolarisCloseAllPipes(pState, false /* ControlPipe */);
2243
2244
2245 mutex_exit(&pState->Mtx);
2246
2247 if (RT_SUCCESS(rc))
2248 {
2249 switch (enmReset)
2250 {
2251 case VBOXUSB_RESET_LEVEL_REATTACH:
2252 rc = usb_reset_device(pState->pDip, USB_RESET_LVL_REATTACH);
2253 break;
2254
2255 case VBOXUSB_RESET_LEVEL_SOFT:
2256 rc = usb_reset_device(pState->pDip, USB_RESET_LVL_DEFAULT);
2257 break;
2258
2259 default:
2260 rc = USB_SUCCESS;
2261 break;
2262 }
2263
2264 rc = vboxUSBSolarisToVBoxRC(rc);
2265 }
2266
2267 Log((DEVICE_NAME ":vboxUSBSolarisCloseDevice returns %d\n", rc));
2268 return rc;
2269}
2270
2271
2272/**
2273 * Abort pending requests and reset the pipe.
2274 *
2275 * @param pState The USB device instance.
2276 * @param bEndpoint The Endpoint address.
2277 *
2278 * @returns VBox error code.
2279 */
2280LOCAL int vboxUSBSolarisAbortPipe(vboxusb_state_t *pState, uint8_t bEndpoint)
2281{
2282 LogFunc((DEVICE_NAME ":vboxUSBSolarisAbortPipe pState=%p bEndpoint=%#x\n", pState, bEndpoint));
2283
2284 /*
2285 * Serialize access: single threaded per Endpoint, one request at a time.
2286 */
2287 mutex_enter(&pState->Mtx);
2288 int rc = vboxUSBSolarisDeviceState(pState->DevState);
2289 if (RT_SUCCESS(rc))
2290 {
2291 uchar_t EndPtIndex = usb_get_ep_index(bEndpoint);
2292 vboxusb_ep_t *pEp = &pState->aEps[EndPtIndex];
2293 if (RT_LIKELY(pEp))
2294 {
2295 if (pEp->pPipe)
2296 {
2297 /*
2298 * Default Endpoint; aborting requests not supported, fake success.
2299 */
2300 if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2301 {
2302 mutex_exit(&pState->Mtx);
2303 LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe Cannot reset control pipe.\n"));
2304 return VERR_NOT_SUPPORTED;
2305 }
2306
2307 /*
2308 * Serialize access: single threaded per Endpoint, one request at a time.
2309 */
2310 mutex_exit(&pState->Mtx);
2311 usb_pipe_reset(pState->pDip, pEp->pPipe,
2312 USB_FLAGS_SLEEP, /* Synchronous */
2313 NULL, /* Completion callback */
2314 NULL); /* Callback data */
2315
2316 /*
2317 * Allow pending async requests to complete.
2318 */
2319 rc = usb_pipe_drain_reqs(pState->pDip, pEp->pPipe,
2320 USB_FLAGS_SLEEP, /* Synchronous */
2321 5, /* Timeout (seconds) */
2322 NULL, /* Completion callback */
2323 NULL); /* Callback data*/
2324
2325 mutex_enter(&pState->Mtx);
2326
2327 Log((DEVICE_NAME ":usb_pipe_drain_reqs returns %d\n", rc));
2328 rc = vboxUSBSolarisToVBoxRC(rc);
2329 }
2330 else
2331 {
2332 LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe pipe not open. bEndpoint=%#x\n", bEndpoint));
2333 rc = VERR_PIPE_IO_ERROR;
2334 }
2335 }
2336 else
2337 {
2338 LogRel((DEVICE_NAME ":vboxUSBSolarisAbortPipe invalid pipe index %d bEndpoint=%#x\n", EndPtIndex, bEndpoint));
2339 rc = VERR_INVALID_HANDLE;
2340 }
2341 }
2342
2343 mutex_exit(&pState->Mtx);
2344
2345 LogFunc((DEVICE_NAME ":vboxUSBSolarisAbortPipe returns %d\n", rc));
2346 return rc;
2347}
2348
2349
2350/**
2351 * Initialize an endpoint.
2352 *
2353 * @param pState The USB device instance.
2354 * @param pEpData The Endpoint data.
2355 * @param uCfgValue The Configuration value.
2356 * @param uCfgIndex The Configuration index.
2357 * @param uInterface The Interface.
2358 * @param uAlt The Alternate setting.
2359 *
2360 * @returns VBox error code.
2361 */
2362LOCAL int vboxUSBSolarisInitEndPoint(vboxusb_state_t *pState, usb_ep_data_t *pEpData, uchar_t uCfgValue,
2363 uchar_t uInterface, uchar_t uAlt)
2364{
2365 LogFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPoint pState=%p pEpData=%p CfgVal=%d Iface=%d Alt=%d", pState,
2366 pEpData, uCfgValue, uInterface, uAlt));
2367
2368 /*
2369 * Is this the default endpoint?
2370 */
2371 usb_ep_descr_t *pEpDesc = NULL;
2372 vboxusb_ep_t *pEp = NULL;
2373 int EpIndex = 0;
2374 if (!pEpData)
2375 {
2376 EpIndex = 0;
2377 pEpDesc = &g_VBoxUSBSolarisDefaultEpDesc;
2378 }
2379 else
2380 {
2381 EpIndex = usb_get_ep_index(pEpData->ep_descr.bEndpointAddress);
2382 pEpDesc = &pEpData->ep_descr;
2383 }
2384
2385 pEp = &pState->aEps[EpIndex];
2386 AssertRelease(pEp);
2387
2388 /*
2389 * Initialize the endpoint data structure.
2390 */
2391 pEp->EpDesc = *pEpDesc;
2392 pEp->uCfgValue = uCfgValue;
2393 pEp->uInterface = uInterface;
2394 pEp->uAlt = uAlt;
2395 if (pEp->fInitialized != VBOXUSB_EP_INITIALIZED)
2396 {
2397 pEp->pPipe = NULL;
2398 pEp->EpState = VBOXUSB_EP_STATE_CLOSED;
2399 bzero(&pEp->PipePolicy, sizeof(pEp->PipePolicy));
2400 pEp->PipePolicy.pp_max_async_reqs = VBOXUSB_MAX_PIPE_ASYNC_REQS;
2401 pEp->fIsocPolling = false;
2402 list_create(&pEp->hIsocInUrbs, sizeof(vboxusb_urb_t), offsetof(vboxusb_urb_t, hListLink));
2403 pEp->cIsocInUrbs = 0;
2404 list_create(&pEp->hIsocInLandedReqs, sizeof(vboxusb_isoc_req_t), offsetof(vboxusb_isoc_req_t, hListLink));
2405 pEp->cbIsocInLandedReqs = 0;
2406 pEp->cbMaxIsocData = 0;
2407 pEp->fInitialized = VBOXUSB_EP_INITIALIZED;
2408 }
2409 Log((DEVICE_NAME ":vboxUSBSolarisInitEndPoint done. %s:[%d] bEndpoint=%#x\n", !pEpData ? "Default " : "Endpoint",
2410 EpIndex, pEp->EpDesc.bEndpointAddress));
2411 return VINF_SUCCESS;
2412}
2413
2414
2415/**
2416 * Initialize all Endpoint structures.
2417 *
2418 * @param pState The USB device instance.
2419 *
2420 * @returns VBox status code.
2421 */
2422LOCAL int vboxUSBSolarisInitAllEndPoints(vboxusb_state_t *pState)
2423{
2424 LogFunc((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints pState=%p\n", pState));
2425
2426 /*
2427 * Initialize all Endpoints for all Alternate settings of all Interfaces of all Configs.
2428 */
2429 int rc = vboxUSBSolarisInitEndPoint(pState, NULL /* pEp */, 0 /* uCfgValue */, 0 /* uInterface */, 0 /* uAlt */);
2430
2431 if (RT_SUCCESS(rc))
2432 {
2433 /*
2434 * Initialize all Endpoints for all Alternate settings of all Interfaces of all Configs.
2435 */
2436 for (uchar_t uCfgIndex = 0; uCfgIndex < pState->pDevDesc->dev_n_cfg; uCfgIndex++)
2437 {
2438 rc = vboxUSBSolarisInitEndPointsForConfig(pState, uCfgIndex);
2439 if (RT_FAILURE(rc))
2440 {
2441 LogRel((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints: vboxUSBSolarisInitEndPoints uCfgIndex=%d failed. rc=%d\n",
2442 uCfgIndex, rc));
2443 return rc;
2444 }
2445 }
2446 }
2447 else
2448 LogRel((DEVICE_NAME ":vboxUSBSolarisInitAllEndPoints default Endpoint initialization failed!\n"));
2449
2450 return rc;
2451}
2452
2453
2454/**
2455 * Initialize Endpoints structures for the given Config.
2456 *
2457 * @param pState The USB device instance.
2458 * @param uCfgIndex The current Config. index.
2459 *
2460 * @returns VBox status code.
2461 */
2462LOCAL int vboxUSBSolarisInitEndPointsForConfig(vboxusb_state_t *pState, uint8_t uCfgIndex)
2463{
2464 LogFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForConfig pState=%p uCfgIndex=%d\n", pState, uCfgIndex));
2465 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2466 uchar_t uCfgValue = pConfig->cfg_descr.bConfigurationValue;
2467
2468 for (uchar_t uInterface = 0; uInterface < pConfig->cfg_n_if; uInterface++)
2469 {
2470 usb_if_data_t *pInterface = &pConfig->cfg_if[uInterface];
2471
2472 for (uchar_t uAlt = 0; uAlt < pInterface->if_n_alt; uAlt++)
2473 {
2474 usb_alt_if_data_t *pAlt = &pInterface->if_alt[uAlt];
2475
2476 for (uchar_t uEp = 0; uEp < pAlt->altif_n_ep; uEp++)
2477 {
2478 usb_ep_data_t *pEpData = &pAlt->altif_ep[uEp];
2479
2480 int rc = vboxUSBSolarisInitEndPoint(pState, pEpData, uCfgValue, uInterface, uAlt);
2481 if (RT_FAILURE(rc))
2482 {
2483 LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForConfig: vboxUSBSolarisInitEndPoint failed! pEp=%p "
2484 "uCfgValue=%u uCfgIndex=%u uInterface=%u, uAlt=%u\n", uCfgValue, uCfgIndex, uInterface, uAlt));
2485 return rc;
2486 }
2487 }
2488 }
2489 }
2490 return VINF_SUCCESS;
2491}
2492
2493
2494/**
2495 * Initialize Endpoints structures for the given Interface & Alternate setting.
2496 *
2497 * @param pState The USB device instance.
2498 * @param uInterface The interface being switched to.
2499 * @param uAlt The alt being switched to.
2500 *
2501 * @returns VBox status code.
2502 */
2503LOCAL int vboxUSBSolarisInitEndPointsForInterfaceAlt(vboxusb_state_t *pState, uint8_t uInterface, uint8_t uAlt)
2504{
2505 LogFunc((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt pState=%p uInterface=%d uAlt=%d\n", pState, uInterface,
2506 uAlt));
2507
2508 /* Doesn't hurt to be paranoid */
2509 uint_t uCfgIndex = usb_get_current_cfgidx(pState->pDip);
2510 if (RT_UNLIKELY(uCfgIndex >= pState->pDevDesc->dev_n_cfg))
2511 {
2512 LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt invalid current config index %d\n", uCfgIndex));
2513 return VERR_GENERAL_FAILURE;
2514 }
2515
2516 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[uCfgIndex];
2517 uchar_t uCfgValue = pConfig->cfg_descr.bConfigurationValue;
2518 usb_if_data_t *pInterface = &pConfig->cfg_if[uInterface];
2519
2520 int rc = VINF_SUCCESS;
2521 if (RT_LIKELY(pInterface))
2522 {
2523 usb_alt_if_data_t *pAlt = &pInterface->if_alt[uAlt];
2524 if (RT_LIKELY(pAlt))
2525 {
2526 for (uchar_t uEp = 0; uEp < pAlt->altif_n_ep; uEp++)
2527 {
2528 usb_ep_data_t *pEpData = &pAlt->altif_ep[uEp];
2529 rc = vboxUSBSolarisInitEndPoint(pState, pEpData, uCfgValue, uInterface, uAlt);
2530 if (RT_FAILURE(rc))
2531 {
2532 LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt: vboxUSBSolarisInitEndPoint failed! pEp=%p "
2533 "uCfgValue=%u uCfgIndex=%u uInterface=%u, uAlt=%u\n", uCfgValue, uCfgIndex, uInterface, uAlt));
2534 return rc;
2535 }
2536 }
2537 }
2538 else
2539 {
2540 LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt missing alternate.\n"));
2541 rc = VERR_INVALID_POINTER;
2542 }
2543 }
2544 else
2545 {
2546 LogRel((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt missing interface.\n"));
2547 rc = VERR_INVALID_POINTER;
2548 }
2549
2550 Log((DEVICE_NAME ":vboxUSBSolarisInitEndPointsForInterfaceAlt returns %d\n", rc));
2551 return rc;
2552}
2553
2554
2555/**
2556 * Destroy all Endpoint Xfer structures.
2557 *
2558 * @param pState The USB device instance.
2559 * @remarks Requires the state mutex to be held.
2560 * Call only from Detach() or similar as callbacks
2561 */
2562LOCAL void vboxUSBSolarisDestroyAllEndPoints(vboxusb_state_t *pState)
2563{
2564 LogFunc((DEVICE_NAME ":vboxUSBSolarisDestroyAllEndPoints pState=%p\n", pState));
2565
2566 Assert(mutex_owned(&pState->Mtx));
2567 for (unsigned i = 0; i < VBOXUSB_MAX_ENDPOINTS; i++)
2568 {
2569 vboxusb_ep_t *pEp = &pState->aEps[i];
2570 if (pEp)
2571 {
2572 vboxUSBSolarisDestroyEndPoint(pState, pEp);
2573 pEp = NULL;
2574 }
2575 }
2576}
2577
2578
2579/**
2580 * Destroy an Endpoint.
2581 *
2582 * @param pState The USB device instance.
2583 * @param pEp The Endpoint.
2584 * @remarks Requires the state mutex to be held.
2585 */
2586LOCAL void vboxUSBSolarisDestroyEndPoint(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2587{
2588 LogFunc((DEVICE_NAME ":vboxUSBSolarisDestroyEndPoint pState=%p pEp=%p\n", pState, pEp));
2589
2590 Assert(mutex_owned(&pState->Mtx));
2591 if (pEp->fInitialized == VBOXUSB_EP_INITIALIZED)
2592 {
2593 vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
2594 while (pUrb)
2595 {
2596 if (pUrb->pMsg)
2597 freemsg(pUrb->pMsg);
2598 RTMemFree(pUrb);
2599 pUrb = list_remove_head(&pEp->hIsocInUrbs);
2600 }
2601 pEp->cIsocInUrbs = 0;
2602 list_destroy(&pEp->hIsocInUrbs);
2603
2604 vboxusb_isoc_req_t *pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2605 while (pIsocReq)
2606 {
2607 kmem_free(pIsocReq, sizeof(vboxusb_isoc_req_t));
2608 pIsocReq = list_remove_head(&pEp->hIsocInLandedReqs);
2609 }
2610 pEp->cbIsocInLandedReqs = 0;
2611 list_destroy(&pEp->hIsocInLandedReqs);
2612
2613 pEp->fInitialized = 0;
2614 }
2615}
2616
2617
2618/**
2619 * Close all non-default Endpoints and drains the default pipe.
2620 *
2621 * @param pState The USB device instance.
2622 * @param fDefault Whether to close the default control pipe.
2623 *
2624 * @remarks Requires the device state mutex to be held.
2625 */
2626LOCAL void vboxUSBSolarisCloseAllPipes(vboxusb_state_t *pState, bool fDefault)
2627{
2628 LogFunc((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes pState=%p\n", pState));
2629
2630 for (int i = 1; i < VBOXUSB_MAX_ENDPOINTS; i++)
2631 {
2632 vboxusb_ep_t *pEp = &pState->aEps[i];
2633 if ( pEp
2634 && pEp->pPipe)
2635 {
2636 Log((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes closing[%d]\n", i));
2637 vboxUSBSolarisClosePipe(pState, pEp);
2638 }
2639 }
2640
2641 if (fDefault)
2642 {
2643 vboxusb_ep_t *pEp = &pState->aEps[0];
2644 if ( pEp
2645 && pEp->pPipe)
2646 {
2647 vboxUSBSolarisClosePipe(pState, pEp);
2648 Log((DEVICE_NAME ":vboxUSBSolarisCloseAllPipes closed default pipe.\n"));
2649 }
2650 }
2651}
2652
2653
2654/**
2655 * Open the pipe for an Endpoint.
2656 *
2657 * @param pState The USB device instance.
2658 * @param pEp The Endpoint.
2659 * @remarks Requires the device state mutex to be held.
2660 *
2661 * @returns VBox status code.
2662 */
2663LOCAL int vboxUSBSolarisOpenPipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2664{
2665// LogFunc((DEVICE_NAME ":vboxUSBSolarisOpenPipe pState=%p pEp=%p\n", pState, pEp));
2666
2667 Assert(mutex_owned(&pState->Mtx));
2668
2669 /*
2670 * Make sure the Endpoint isn't open already.
2671 */
2672 if (pEp->pPipe)
2673 return VINF_SUCCESS;
2674
2675 /*
2676 * Default Endpoint; already opened just copy the pipe handle.
2677 */
2678 if ((pEp->EpDesc.bEndpointAddress & USB_EP_NUM_MASK) == 0)
2679 {
2680 pEp->pPipe = pState->pDevDesc->dev_default_ph;
2681 pEp->EpState |= VBOXUSB_EP_STATE_OPENED;
2682 Log((DEVICE_NAME ":vboxUSBSolarisOpenPipe default pipe opened.\n"));
2683 return VINF_SUCCESS;
2684 }
2685
2686 /*
2687 * Open the non-default pipe for the Endpoint.
2688 */
2689 mutex_exit(&pState->Mtx);
2690 int rc = usb_pipe_open(pState->pDip, &pEp->EpDesc, &pEp->PipePolicy, USB_FLAGS_NOSLEEP, &pEp->pPipe);
2691 mutex_enter(&pState->Mtx);
2692 if (rc == USB_SUCCESS)
2693 {
2694 usb_pipe_set_private(pEp->pPipe, (usb_opaque_t)pEp);
2695
2696 /*
2697 * Determine input buffer size for Isoc. IN transfers.
2698 */
2699 if ( VBOXUSB_XFER_TYPE(pEp) == VUSBXFERTYPE_ISOC
2700 && VBOXUSB_XFER_DIR(pEp) == VUSB_DIR_TO_HOST)
2701 {
2702 /*
2703 * wMaxPacketSize bits 10..0 specifies maximum packet size which can hold 1024 bytes.
2704 * If bits 12..11 is non-zero, cbMax will be more than 1024 and thus the Endpoint is a
2705 * high-bandwidth Endpoint.
2706 */
2707 uint16_t cbMax = VBOXUSB_PKT_SIZE(pEp->EpDesc.wMaxPacketSize);
2708 if (cbMax <= 1024)
2709 {
2710 /* Buffer 1 second for highspeed and 8 seconds for fullspeed Endpoints. */
2711 pEp->cbMaxIsocData = 1000 * cbMax * 8;
2712 }
2713 else
2714 {
2715 /* Buffer about 400 milliseconds of data for highspeed high-bandwidth endpoints. */
2716 pEp->cbMaxIsocData = 400 * cbMax * 8;
2717 }
2718 Log((DEVICE_NAME ":vboxUSBSolarisOpenPipe pEp=%p cbMaxIsocData=%u\n", pEp->cbMaxIsocData));
2719 }
2720
2721 pEp->EpState |= VBOXUSB_EP_STATE_OPENED;
2722 rc = VINF_SUCCESS;
2723 }
2724 else
2725 {
2726 LogRel((DEVICE_NAME ":vboxUSBSolarisOpenPipe failed! rc=%d pState=%p pEp=%p\n", rc, pState, pEp));
2727 rc = VERR_BAD_PIPE;
2728 }
2729
2730 return rc;
2731}
2732
2733
2734/**
2735 * Close the pipe of the Endpoint.
2736 *
2737 * @param pState The USB device instance.
2738 * @param pEp The Endpoint.
2739 *
2740 * @remarks Requires the device state mutex to be held.
2741 */
2742LOCAL void vboxUSBSolarisClosePipe(vboxusb_state_t *pState, vboxusb_ep_t *pEp)
2743{
2744 LogFunc((DEVICE_NAME ":vboxUSBSolarisClosePipe pState=%p pEp=%p\n", pState, pEp));
2745 AssertPtr(pEp);
2746
2747 if (pEp->pPipe)
2748 {
2749 pEp->EpState &= ~(VBOXUSB_EP_STATE_OPENED);
2750
2751 /*
2752 * Default pipe: allow completion of pending requests.
2753 */
2754 if (pEp->pPipe == pState->pDevDesc->dev_default_ph)
2755 {
2756 mutex_exit(&pState->Mtx);
2757 usb_pipe_drain_reqs(pState->pDip, pEp->pPipe, 0, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2758 mutex_enter(&pState->Mtx);
2759 Log((DEVICE_NAME ":vboxUSBSolarisClosePipe closed default pipe\n"));
2760 }
2761 else
2762 {
2763 /*
2764 * Stop Isoc. IN polling if required.
2765 */
2766 if (pEp->fIsocPolling)
2767 {
2768 pEp->fIsocPolling = false;
2769 mutex_exit(&pState->Mtx);
2770 usb_pipe_stop_isoc_polling(pEp->pPipe, USB_FLAGS_NOSLEEP);
2771 mutex_enter(&pState->Mtx);
2772 }
2773
2774 /*
2775 * Non-default pipe: close it.
2776 */
2777 Log((DEVICE_NAME ":vboxUSBSolarisClosePipe pipe bmAttributes=%#x bEndpointAddress=%#x\n", pEp->EpDesc.bmAttributes,
2778 pEp->EpDesc.bEndpointAddress));
2779 mutex_exit(&pState->Mtx);
2780 usb_pipe_close(pState->pDip, pEp->pPipe, USB_FLAGS_SLEEP, NULL /* callback */, NULL /* callback arg. */);
2781 mutex_enter(&pState->Mtx);
2782 }
2783
2784 /*
2785 * Free the Endpoint data message block and reset pipe handle.
2786 */
2787 pEp->pPipe = NULL;
2788
2789 Log((DEVICE_NAME ":vboxUSBSolarisClosePipe successful. pEp=%p\n", pEp));
2790 }
2791
2792 Assert(pEp->pPipe == NULL);
2793}
2794
2795
2796/**
2797 * Find the Configuration index for the passed in Configuration value.
2798 *
2799 * @param pState The USB device instance.
2800 * @param uCfgValue The Configuration value.
2801 *
2802 * @returns The configuration index if found, otherwise -1.
2803 */
2804LOCAL int vboxUSBSolarisGetConfigIndex(vboxusb_state_t *pState, uint_t uCfgValue)
2805{
2806 for (int CfgIndex = 0; CfgIndex < pState->pDevDesc->dev_n_cfg; CfgIndex++)
2807 {
2808 usb_cfg_data_t *pConfig = &pState->pDevDesc->dev_cfg[CfgIndex];
2809 if (pConfig->cfg_descr.bConfigurationValue == uCfgValue)
2810 return CfgIndex;
2811 }
2812
2813 return -1;
2814}
2815
2816
2817/**
2818 * Allocates and initializes an Isoc. In URB from the ring-3 equivalent.
2819 *
2820 * @param pState The USB device instance.
2821 * @param pUrb The URB to initialize.
2822 * @param pUrbReq Opaque pointer to the complete request.
2823 * @param pMsg Pointer to the allocated request data.
2824 *
2825 * @returns The allocated Isoc. In URB to be used.
2826 */
2827LOCAL vboxusb_urb_t *vboxUSBSolarisGetIsocInURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq)
2828{
2829 /*
2830 * Isoc. In URBs are not queued into the Inflight list like every other URBs.
2831 * For now we allocate each URB which gets queued into the respective Endpoint during Xfer.
2832 */
2833 vboxusb_urb_t *pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2834 if (RT_LIKELY(pUrb))
2835 {
2836 pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2837 pUrb->pState = pState;
2838
2839 if (RT_LIKELY(pUrbReq))
2840 {
2841 pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2842 pUrb->bEndpoint = pUrbReq->bEndpoint;
2843 pUrb->enmType = pUrbReq->enmType;
2844 pUrb->enmDir = pUrbReq->enmDir;
2845 pUrb->enmStatus = pUrbReq->enmStatus;
2846 pUrb->cbDataR3 = pUrbReq->cbData;
2847 pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2848 pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2849
2850 for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2851 pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2852
2853 pUrb->pMsg = NULL;
2854 }
2855 }
2856 else
2857 LogRel((DEVICE_NAME ":vboxUSBSolarisGetIsocInURB failed to alloc %d bytes.\n", sizeof(vboxusb_urb_t)));
2858 return pUrb;
2859}
2860
2861
2862/**
2863 * Queues a URB reusing previously allocated URBs as required.
2864 *
2865 * @param pState The USB device instance.
2866 * @param pUrbReq Opaque pointer to the complete request.
2867 * @param pMsg Pointer to the allocated request data.
2868 *
2869 * @returns The allocated URB to be used, or NULL upon failure.
2870 */
2871LOCAL vboxusb_urb_t *vboxUSBSolarisQueueURB(vboxusb_state_t *pState, PVBOXUSBREQ_URB pUrbReq, mblk_t *pMsg)
2872{
2873 LogFunc((DEVICE_NAME ":vboxUSBSolarisQueueURB pState=%p pUrbReq=%p\n", pState, pUrbReq));
2874
2875 mutex_enter(&pState->Mtx);
2876
2877 /*
2878 * Discard oldest queued URB if we've queued max URBs and none of them have completed.
2879 */
2880 if (pState->cInflightUrbs >= VBOXUSB_URB_QUEUE_SIZE)
2881 {
2882 vboxusb_urb_t *pUrb = list_head(&pState->hUrbs);
2883 if (RT_LIKELY(pUrb))
2884 {
2885 if (pUrb->pMsg)
2886 {
2887 freemsg(pUrb->pMsg);
2888 pUrb->pMsg = NULL;
2889 }
2890 pUrb->enmState = VBOXUSB_URB_STATE_FREE;
2891 }
2892 }
2893
2894 vboxusb_urb_t *pUrb = list_head(&pState->hUrbs);
2895 if ( !pUrb
2896 || ( pUrb
2897 && pUrb->enmState != VBOXUSB_URB_STATE_FREE))
2898 {
2899 mutex_exit(&pState->Mtx);
2900 pUrb = RTMemAllocZ(sizeof(vboxusb_urb_t));
2901 if (RT_UNLIKELY(!pUrb))
2902 {
2903 LogRel((DEVICE_NAME ":vboxUSBSolarisQueueURB failed to alloc %d bytes.\n", sizeof(vboxusb_urb_t)));
2904 return NULL;
2905 }
2906 mutex_enter(&pState->Mtx);
2907 }
2908 else
2909 {
2910 /*
2911 * Remove from head and move to tail so that when several URBs are reaped continuously we get to use
2912 * up each one free 'head'.
2913 */
2914 Assert(pUrb && pUrb->enmState == VBOXUSB_URB_STATE_FREE);
2915 list_remove_head(&pState->hUrbs);
2916 }
2917
2918 list_insert_tail(&pState->hUrbs, pUrb);
2919 ++pState->cInflightUrbs;
2920
2921 pUrb->enmState = VBOXUSB_URB_STATE_INFLIGHT;
2922
2923 Assert(pUrb->pMsg == NULL);
2924 pUrb->pState = pState;
2925 Log((DEVICE_NAME ":vboxUSBSolarisQueueURB cInflightUrbs=%d\n", pState->cInflightUrbs));
2926
2927 if (RT_LIKELY(pUrbReq))
2928 {
2929 pUrb->pvUrbR3 = pUrbReq->pvUrbR3;
2930 pUrb->bEndpoint = pUrbReq->bEndpoint;
2931 pUrb->enmType = pUrbReq->enmType;
2932 pUrb->enmDir = pUrbReq->enmDir;
2933 pUrb->enmStatus = pUrbReq->enmStatus;
2934 pUrb->cbDataR3 = pUrbReq->cbData;
2935 pUrb->pvDataR3 = (RTR3PTR)pUrbReq->pvData;
2936 pUrb->cIsocPkts = pUrbReq->cIsocPkts;
2937 if (pUrbReq->enmType == VUSBXFERTYPE_ISOC)
2938 {
2939 for (unsigned i = 0; i < pUrbReq->cIsocPkts; i++)
2940 pUrb->aIsocPkts[i].cbPkt = pUrbReq->aIsocPkts[i].cbPkt;
2941 }
2942
2943 pUrb->pMsg = pMsg;
2944 }
2945
2946 mutex_exit(&pState->Mtx);
2947
2948 return pUrb;
2949}
2950
2951
2952/**
2953 * Dequeues a completed URB into the landed list and informs user-land.
2954 *
2955 * @param pUrb The URB to move.
2956 * @param URBStatus The Solaris URB completion code.
2957 *
2958 * @remarks All pipes could be closed at this point (e.g. Device disconnected during inflight URBs)
2959 */
2960LOCAL inline void vboxUSBSolarisDeQueueURB(vboxusb_urb_t *pUrb, int URBStatus)
2961{
2962 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeQueue pUrb=%p\n", pUrb));
2963 AssertPtrReturnVoid(pUrb);
2964
2965 pUrb->enmStatus = vboxUSBSolarisGetUrbStatus(URBStatus);
2966
2967 vboxusb_state_t *pState = pUrb->pState;
2968 if (RT_LIKELY(pState))
2969 {
2970 mutex_enter(&pState->Mtx);
2971 pUrb->enmState = VBOXUSB_URB_STATE_LANDED;
2972
2973 /*
2974 * Remove it from the inflight list & move it to landed list.
2975 */
2976 list_remove(&pState->hUrbs, pUrb);
2977 --pState->cInflightUrbs;
2978 list_insert_tail(&pState->hLandedUrbs, pUrb);
2979
2980 vboxUSBSolarisNotifyComplete(pUrb->pState);
2981 mutex_exit(&pState->Mtx);
2982 }
2983 else
2984 {
2985 Log((DEVICE_NAME ":vboxUSBSolarisDeQueue State Gone.\n"));
2986 freemsg(pUrb->pMsg);
2987 pUrb->pMsg = NULL;
2988 pUrb->enmStatus = VUSBSTATUS_INVALID;
2989 }
2990}
2991
2992
2993/**
2994 * Concatenates a chain message block into a single message block if possible.
2995 *
2996 * @param pUrb The URB to move.
2997 */
2998LOCAL inline void vboxUSBSolarisConcatMsg(vboxusb_urb_t *pUrb)
2999{
3000 /*
3001 * Concatenate the whole message rather than doing a chained copy while reaping.
3002 */
3003 if ( pUrb->pMsg
3004 && pUrb->pMsg->b_cont)
3005 {
3006 mblk_t *pFullMsg = msgpullup(pUrb->pMsg, -1 /* all data */);
3007 if (RT_LIKELY(pFullMsg))
3008 {
3009 freemsg(pUrb->pMsg);
3010 pUrb->pMsg = pFullMsg;
3011 }
3012 }
3013}
3014
3015
3016/**
3017 * User process poll wake up wrapper for asynchronous URB completion.
3018 *
3019 * @param pState The USB device instance.
3020 * @remarks Requires the device state mutex to be held.
3021 */
3022LOCAL inline void vboxUSBSolarisNotifyComplete(vboxusb_state_t *pState)
3023{
3024 if (pState->fPoll & VBOXUSB_POLL_ON)
3025 {
3026 pollhead_t *pPollHead = &pState->PollHead;
3027 pState->fPoll |= VBOXUSB_POLL_REAP_PENDING;
3028 mutex_exit(&pState->Mtx);
3029 pollwakeup(pPollHead, POLLIN);
3030 mutex_enter(&pState->Mtx);
3031 }
3032}
3033
3034
3035/**
3036 * User process poll wake up wrapper for hotplug events.
3037 *
3038 * @param pState The USB device instance.
3039 * @remarks Requires the device state mutex to be held.
3040 */
3041LOCAL inline void vboxUSBSolarisNotifyHotplug(vboxusb_state_t *pState)
3042{
3043 if (pState->fPoll & VBOXUSB_POLL_ON)
3044 {
3045 pollhead_t *pPollHead = &pState->PollHead;
3046 pState->fPoll |= VBOXUSB_POLL_DEV_UNPLUGGED;
3047 mutex_exit(&pState->Mtx);
3048 pollwakeup(pPollHead, POLLHUP);
3049 mutex_enter(&pState->Mtx);
3050 }
3051}
3052
3053
3054/**
3055 * Perform a Control Xfer.
3056 *
3057 * @param pState The USB device instance.
3058 * @param pEp The Endpoint for the Xfer.
3059 * @param pUrb The VBox USB URB.
3060 *
3061 * @returns VBox status code.
3062 * @remarks Any errors, the caller should free pUrb->pMsg.
3063 */
3064LOCAL int vboxUSBSolarisCtrlXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3065{
3066 LogFunc((DEVICE_NAME ":vboxUSBSolarisCtrlXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3067 pUrb->enmDir, pUrb->cbDataR3));
3068
3069 AssertPtrReturn(pUrb->pMsg, VERR_INVALID_PARAMETER);
3070 uchar_t *pSetupData = pUrb->pMsg->b_rptr;
3071 size_t cbData = pUrb->cbDataR3 - VBOXUSB_CTRL_XFER_SIZE;
3072
3073 /*
3074 * Solaris USBA gives us garbage and incorrect message lengths making it impossible to use
3075 * pre-allocated control messages. The allocation of "ctrl_data" is not documented well.
3076 */
3077
3078 /*
3079 * Allocate a wrapper request.
3080 */
3081 int rc = VINF_SUCCESS;
3082 usb_ctrl_req_t *pReq = usb_alloc_ctrl_req(pState->pDip, cbData, USB_FLAGS_NOSLEEP);
3083 if (RT_LIKELY(pReq))
3084 {
3085 /*
3086 * Initialize the Ctrl Xfer Header.
3087 */
3088 pReq->ctrl_bmRequestType = pSetupData[0];
3089 pReq->ctrl_bRequest = pSetupData[1];
3090 pReq->ctrl_wValue = (pSetupData[3] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[2];
3091 pReq->ctrl_wIndex = (pSetupData[5] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[4];
3092 pReq->ctrl_wLength = (pSetupData[7] << VBOXUSB_CTRL_XFER_SIZE) | pSetupData[6];
3093
3094 if ( pUrb->enmDir == VUSBDIRECTION_OUT
3095 && cbData > 0)
3096 {
3097 pUrb->pMsg->b_rptr += VBOXUSB_CTRL_XFER_SIZE;
3098 bcopy(pUrb->pMsg->b_rptr, pReq->ctrl_data->b_wptr, cbData);
3099 pReq->ctrl_data->b_wptr += cbData;
3100 }
3101
3102 freemsg(pUrb->pMsg);
3103 pUrb->pMsg = NULL;
3104
3105 /*
3106 * Initialize callbacks and timeouts.
3107 */
3108 usb_req_attrs_t fAttributes = USB_ATTRS_AUTOCLEARING;
3109 if ( pUrb->enmDir == VUSBDIRECTION_IN
3110 && pUrb->fShortOk)
3111 {
3112 fAttributes |= USB_ATTRS_SHORT_XFER_OK;
3113 }
3114 pReq->ctrl_cb = vboxUSBSolarisCtrlXferCompleted;
3115 pReq->ctrl_exc_cb = vboxUSBSolarisCtrlXferCompleted;
3116 pReq->ctrl_timeout = VBOXUSB_CTRL_XFER_TIMEOUT;
3117 pReq->ctrl_attributes = fAttributes;
3118
3119 pReq->ctrl_client_private = (usb_opaque_t)pUrb;
3120
3121 Log((DEVICE_NAME ":vboxUSBSolarisCtrlXfer %.*Rhxd\n", VBOXUSB_CTRL_XFER_SIZE, pSetupData));
3122
3123 /*
3124 * Submit the request.
3125 */
3126 rc = usb_pipe_ctrl_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3127
3128 if (RT_LIKELY(rc == USB_SUCCESS))
3129 return VINF_SUCCESS;
3130 else
3131 {
3132 LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXfer usb_pipe_ctrl_xfer failed! rc=%d\n", rc));
3133 rc = VERR_PIPE_IO_ERROR;
3134 }
3135
3136 usb_free_ctrl_req(pReq);
3137 }
3138 else
3139 {
3140 LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXfer failed to alloc request.\n"));
3141 rc = VERR_NO_MEMORY;
3142 }
3143
3144 return rc;
3145}
3146
3147
3148/**
3149 * Completion/Exception callback for Control Xfers.
3150 *
3151 * @param pPipe The Ctrl pipe handle.
3152 * @param pReq The Ctrl request.
3153 */
3154LOCAL void vboxUSBSolarisCtrlXferCompleted(usb_pipe_handle_t pPipe, usb_ctrl_req_t *pReq)
3155{
3156 LogFunc((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3157
3158 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->ctrl_client_private;
3159 if (RT_LIKELY(pUrb))
3160 {
3161 /*
3162 * Funky stuff: We need to reconstruct the header for control transfers.
3163 * Let us chain along the data and while we dequeue the URB we attempt to
3164 * concatenate the entire message there.
3165 */
3166 mblk_t *pSetupMsg = allocb(sizeof(VUSBSETUP), BPRI_MED);
3167 if (RT_LIKELY(pSetupMsg))
3168 {
3169 VUSBSETUP SetupData;
3170 SetupData.bmRequestType = pReq->ctrl_bmRequestType;
3171 SetupData.bRequest = pReq->ctrl_bRequest;
3172 SetupData.wValue = pReq->ctrl_wValue;
3173 SetupData.wIndex = pReq->ctrl_wIndex;
3174 SetupData.wLength = pReq->ctrl_wLength;
3175 bcopy(&SetupData, pSetupMsg->b_wptr, sizeof(VUSBSETUP));
3176 pSetupMsg->b_wptr += sizeof(VUSBSETUP);
3177
3178 /*
3179 * Should be safe to update pMsg here without the state mutex, see vboxUSBSolarisSendURB()
3180 * and vboxUSBSolarisQueueURB() as the URB state is (still) not VBOXUSB_URB_STATE_FREE.
3181 */
3182 pUrb->pMsg = pSetupMsg;
3183 pUrb->pMsg->b_cont = pReq->ctrl_data;
3184 pReq->ctrl_data = NULL;
3185 vboxUSBSolarisConcatMsg(pUrb);
3186
3187#if defined(DEBUG_ramshankar)
3188 if ( pUrb->pMsg
3189 && pUrb->pMsg->b_cont == NULL) /* Concat succeeded */
3190 {
3191 Log((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted prepended header rc=%d cbData=%d.\n",
3192 pReq->ctrl_completion_reason, MBLKL(pUrb->pMsg)));
3193 Log((DEVICE_NAME ":%.*Rhxd\n", MBLKL(pUrb->pMsg), pUrb->pMsg->b_rptr));
3194 }
3195#endif
3196
3197 /*
3198 * Update the URB and move to landed list for reaping.
3199 */
3200 vboxUSBSolarisDeQueueURB(pUrb, pReq->ctrl_completion_reason);
3201 usb_free_ctrl_req(pReq);
3202 return;
3203 }
3204 else
3205 {
3206 LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted failed to alloc %d bytes for Setup Header.\n",
3207 sizeof(VUSBSETUP)));
3208 }
3209 }
3210 else
3211 LogRel((DEVICE_NAME ":vboxUSBSolarisCtrlXferCompleted Extreme error! missing private data.\n"));
3212
3213 usb_free_ctrl_req(pReq);
3214}
3215
3216
3217/**
3218 * Perform a Bulk Xfer.
3219 *
3220 * @param pState The USB device instance.
3221 * @param pEp The Endpoint for the Xfer.
3222 * @param pUrb The VBox USB URB.
3223 *
3224 * @returns VBox status code.
3225 * @remarks Any errors, the caller should free pUrb->pMsg.
3226 */
3227LOCAL int vboxUSBSolarisBulkXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3228{
3229 LogFunc((DEVICE_NAME ":vboxUSBSolarisBulkXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3230 pUrb->enmDir, pUrb->cbDataR3));
3231
3232 /*
3233 * Allocate a wrapper request.
3234 */
3235 int rc = VINF_SUCCESS;
3236 usb_bulk_req_t *pReq = usb_alloc_bulk_req(pState->pDip, pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cbDataR3 : 0,
3237 USB_FLAGS_NOSLEEP);
3238 if (RT_LIKELY(pReq))
3239 {
3240 /*
3241 * Initialize Bulk Xfer, callbacks and timeouts.
3242 */
3243 usb_req_attrs_t fAttributes = USB_ATTRS_AUTOCLEARING;
3244 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3245 pReq->bulk_data = pUrb->pMsg;
3246 else if ( pUrb->enmDir == VUSBDIRECTION_IN
3247 && pUrb->fShortOk)
3248 {
3249 fAttributes |= USB_ATTRS_SHORT_XFER_OK;
3250 }
3251
3252 pReq->bulk_len = pUrb->cbDataR3;
3253 pReq->bulk_cb = vboxUSBSolarisBulkXferCompleted;
3254 pReq->bulk_exc_cb = vboxUSBSolarisBulkXferCompleted;
3255 pReq->bulk_timeout = VBOXUSB_BULK_XFER_TIMEOUT;
3256 pReq->bulk_attributes = fAttributes;
3257 pReq->bulk_client_private = (usb_opaque_t)pUrb;
3258
3259 /* Don't obtain state lock here, we're just reading unchanging data... */
3260 if (RT_UNLIKELY(pUrb->cbDataR3 > pState->cbMaxBulkXfer))
3261 {
3262 LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer requesting %d bytes when only %d bytes supported by device\n",
3263 pUrb->cbDataR3, pState->cbMaxBulkXfer));
3264 }
3265
3266 /*
3267 * Submit the request.
3268 */
3269 rc = usb_pipe_bulk_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3270
3271 if (RT_LIKELY(rc == USB_SUCCESS))
3272 return VINF_SUCCESS;
3273 else
3274 {
3275 LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer usb_pipe_bulk_xfer enmDir=%#x Ep=%#x failed! rc=%d\n", pUrb->enmDir,
3276 pUrb->bEndpoint, rc));
3277 rc = VERR_PIPE_IO_ERROR;
3278 }
3279
3280 if (pUrb->enmDir == VUSBDIRECTION_OUT) /* pUrb->pMsg freed by caller */
3281 pReq->bulk_data = NULL;
3282
3283 usb_free_bulk_req(pReq);
3284 }
3285 else
3286 {
3287 LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXfer failed to alloc bulk request.\n"));
3288 rc = VERR_NO_MEMORY;
3289 }
3290
3291 return rc;
3292}
3293
3294
3295/**
3296 * Completion/Exception callback for Bulk Xfers.
3297 *
3298 * @param pPipe The Bulk pipe handle.
3299 * @param pReq The Bulk request.
3300 */
3301LOCAL void vboxUSBSolarisBulkXferCompleted(usb_pipe_handle_t pPipe, usb_bulk_req_t *pReq)
3302{
3303 LogFunc((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3304
3305 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3306 if (RT_LIKELY(pEp))
3307 {
3308 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->bulk_client_private;
3309 if (RT_LIKELY(pUrb))
3310 {
3311 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3312 pReq->bulk_data = NULL;
3313 else
3314 {
3315 if (pReq->bulk_completion_reason == USB_CR_OK)
3316 {
3317 pUrb->pMsg = pReq->bulk_data;
3318 pReq->bulk_data = NULL;
3319 vboxUSBSolarisConcatMsg(pUrb);
3320 }
3321 }
3322
3323 Log((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted %s. rc=%d cbData=%d\n",
3324 pReq->bulk_completion_reason != USB_CR_OK ? "failed URB" : "success",
3325 pReq->bulk_completion_reason, pUrb->pMsg ? MBLKL(pUrb->pMsg) : 0));
3326
3327 /*
3328 * Update the URB and move to tail for reaping.
3329 */
3330 vboxUSBSolarisDeQueueURB(pUrb, pReq->bulk_completion_reason);
3331 usb_free_bulk_req(pReq);
3332 return;
3333 }
3334 else
3335 LogRel((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted Extreme error! private request data missing.\n"));
3336 }
3337 else
3338 Log((DEVICE_NAME ":vboxUSBSolarisBulkXferCompleted Pipe Gone.\n"));
3339
3340 usb_free_bulk_req(pReq);
3341}
3342
3343
3344/**
3345 * Perform an Interrupt Xfer.
3346 *
3347 * @param pState The USB device instance.
3348 * @param pEp The Endpoint for the Xfer.
3349 * @param pUrb The VBox USB URB.
3350 *
3351 * @returns VBox status code.
3352 * @remarks Any errors, the caller should free pUrb->pMsg.
3353 */
3354LOCAL int vboxUSBSolarisIntrXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3355{
3356 LogFunc((DEVICE_NAME ":vboxUSBSolarisIntrXfer pState=%p pEp=%p pUrb=%p enmDir=%d cbData=%d\n", pState, pEp, pUrb,
3357 pUrb->enmDir, pUrb->cbDataR3));
3358
3359 int rc = VINF_SUCCESS;
3360 usb_intr_req_t *pReq = usb_alloc_intr_req(pState->pDip, 0 /* length */, USB_FLAGS_NOSLEEP);
3361 if (RT_LIKELY(pReq))
3362 {
3363 /*
3364 * Initialize Intr Xfer, callbacks & timeouts.
3365 */
3366 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3367 {
3368 pReq->intr_data = pUrb->pMsg;
3369 pReq->intr_attributes = USB_ATTRS_AUTOCLEARING;
3370 }
3371 else
3372 {
3373 Assert(pUrb->enmDir == VUSBDIRECTION_IN);
3374 pReq->intr_data = NULL;
3375 pReq->intr_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ONE_XFER | (pUrb->fShortOk ? USB_ATTRS_SHORT_XFER_OK : 0);
3376 }
3377
3378 pReq->intr_len = pUrb->cbDataR3; /* Not pEp->EpDesc.wMaxPacketSize */
3379 pReq->intr_cb = vboxUSBSolarisIntrXferCompleted;
3380 pReq->intr_exc_cb = vboxUSBSolarisIntrXferCompleted;
3381 pReq->intr_timeout = VBOXUSB_INTR_XFER_TIMEOUT;
3382 pReq->intr_client_private = (usb_opaque_t)pUrb;
3383
3384 /*
3385 * Submit the request.
3386 */
3387 rc = usb_pipe_intr_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3388
3389 if (RT_LIKELY(rc == USB_SUCCESS))
3390 return VINF_SUCCESS;
3391 else
3392 {
3393 LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXfer usb_pipe_intr_xfer failed! rc=%d\n", rc));
3394 rc = VERR_PIPE_IO_ERROR;
3395 }
3396
3397 pReq->intr_data = NULL;
3398 usb_free_intr_req(pReq);
3399 }
3400 else
3401 {
3402 LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXfer failed to alloc intr request.\n"));
3403 rc = VERR_NO_MEMORY;
3404 }
3405
3406 return rc;
3407}
3408
3409
3410/**
3411 * Completion/Exception callback for Intr Xfers.
3412 *
3413 * @param pPipe The Intr pipe handle.
3414 * @param pReq The Intr request.
3415 */
3416LOCAL void vboxUSBSolarisIntrXferCompleted(usb_pipe_handle_t pPipe, usb_intr_req_t *pReq)
3417{
3418 LogFunc((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3419
3420 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3421 if (RT_LIKELY(pEp))
3422 {
3423 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->intr_client_private;
3424 if (RT_LIKELY(pUrb))
3425 {
3426 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3427 pReq->intr_data = NULL;
3428 else
3429 {
3430 if (pReq->intr_completion_reason == USB_CR_OK)
3431 {
3432 pUrb->pMsg = pReq->intr_data;
3433 pReq->intr_data = NULL;
3434 }
3435 }
3436
3437 Log((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted rc=%d pMsg=%p enmDir=%#x\n", pReq->intr_completion_reason,
3438 pUrb->pMsg, pUrb->enmDir));
3439
3440 /*
3441 * Update the URB and move to landed list for reaping.
3442 */
3443 vboxUSBSolarisDeQueueURB(pUrb, pReq->intr_completion_reason);
3444 usb_free_intr_req(pReq);
3445 return;
3446 }
3447 else
3448 LogRel((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted Extreme error! private request data missing.\n"));
3449 }
3450 else
3451 Log((DEVICE_NAME ":vboxUSBSolarisIntrXferCompleted Pipe Gone.\n"));
3452
3453 usb_free_intr_req(pReq);
3454}
3455
3456
3457/**
3458 * Perform an Isochronous Xfer.
3459 *
3460 * @param pState The USB device instance.
3461 * @param pEp The Endpoint for the Xfer.
3462 * @param pUrb The VBox USB URB.
3463 *
3464 * @returns VBox status code.
3465 * @remarks Any errors, the caller should free pUrb->pMsg.
3466 */
3467LOCAL int vboxUSBSolarisIsocXfer(vboxusb_state_t *pState, vboxusb_ep_t *pEp, vboxusb_urb_t *pUrb)
3468{
3469// LogFunc((DEVICE_NAME ":vboxUSBSolarisIsocXfer pState=%p pEp=%p pUrb=%p\n", pState, pEp, pUrb));
3470
3471 /*
3472 * For Isoc. IN transfers we perform one request and USBA polls the device continuously
3473 * and supplies our Xfer callback with input data. We cannot perform one-shot Isoc. In transfers.
3474 */
3475 size_t cbData = (pUrb->enmDir == VUSBDIRECTION_IN ? pUrb->cIsocPkts * pUrb->aIsocPkts[0].cbPkt : 0);
3476 if (pUrb->enmDir == VUSBDIRECTION_IN)
3477 {
3478 Log((DEVICE_NAME ":vboxUSBSolarisIsocXfer Isoc. In queueing.\n"));
3479
3480 mutex_enter(&pState->Mtx);
3481 if (pEp->fIsocPolling)
3482 {
3483 /*
3484 * Queue a maximum of cbMaxIsocData bytes, else fail.
3485 */
3486 if (pEp->cbIsocInLandedReqs + cbData > pEp->cbMaxIsocData)
3487 {
3488 mutex_exit(&pState->Mtx);
3489 Log((DEVICE_NAME ":vboxUSBSolarisIsocXfer Max Isoc. data %d bytes queued\n", pEp->cbMaxIsocData));
3490 return VERR_TOO_MUCH_DATA;
3491 }
3492
3493 list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3494 ++pEp->cIsocInUrbs;
3495
3496 mutex_exit(&pState->Mtx);
3497 return VINF_SUCCESS;
3498 }
3499 mutex_exit(&pState->Mtx);
3500 }
3501
3502 int rc = VINF_SUCCESS;
3503 usb_isoc_req_t *pReq = usb_alloc_isoc_req(pState->pDip, pUrb->cIsocPkts, cbData, USB_FLAGS_NOSLEEP);
3504 Log((DEVICE_NAME ":vboxUSBSolarisIsocXfer enmDir=%#x cIsocPkts=%d aIsocPkts[0]=%d cbDataR3=%d\n", pUrb->enmDir,
3505 pUrb->cIsocPkts, pUrb->aIsocPkts[0].cbPkt, pUrb->cbDataR3));
3506 if (RT_LIKELY(pReq))
3507 {
3508 /*
3509 * Initialize Isoc Xfer, callbacks & timeouts.
3510 */
3511 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
3512 pReq->isoc_pkt_descr[i].isoc_pkt_length = pUrb->aIsocPkts[i].cbPkt;
3513
3514 if (pUrb->enmDir == VUSBDIRECTION_OUT)
3515 {
3516 pReq->isoc_data = pUrb->pMsg;
3517 pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP;
3518 pReq->isoc_cb = vboxUSBSolarisIsocOutXferCompleted;
3519 pReq->isoc_exc_cb = vboxUSBSolarisIsocOutXferCompleted;
3520 pReq->isoc_client_private = (usb_opaque_t)pUrb;
3521 }
3522 else
3523 {
3524 pReq->isoc_attributes = USB_ATTRS_AUTOCLEARING | USB_ATTRS_ISOC_XFER_ASAP | USB_ATTRS_SHORT_XFER_OK;
3525 pReq->isoc_cb = vboxUSBSolarisIsocInXferCompleted;
3526 pReq->isoc_exc_cb = vboxUSBSolarisIsocInXferError;
3527 pReq->isoc_client_private = (usb_opaque_t)pState;
3528 }
3529 pReq->isoc_pkts_count = pUrb->cIsocPkts;
3530 pReq->isoc_pkts_length = 0; /* auto compute */
3531
3532 /*
3533 * Submit the request.
3534 */
3535 rc = usb_pipe_isoc_xfer(pEp->pPipe, pReq, USB_FLAGS_NOSLEEP);
3536 if (RT_LIKELY(rc == USB_SUCCESS))
3537 {
3538 if (pUrb->enmDir == VUSBDIRECTION_IN)
3539 {
3540 /*
3541 * Add the first Isoc. IN URB to the queue as well.
3542 */
3543 mutex_enter(&pState->Mtx);
3544 list_insert_tail(&pEp->hIsocInUrbs, pUrb);
3545 ++pEp->cIsocInUrbs;
3546 pEp->fIsocPolling = true;
3547 mutex_exit(&pState->Mtx);
3548 }
3549
3550 return VINF_SUCCESS;
3551 }
3552 else
3553 {
3554 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocXfer usb_pipe_isoc_xfer failed! rc=%d\n", rc));
3555 rc = VERR_PIPE_IO_ERROR;
3556
3557 if (pUrb->enmDir == VUSBDIRECTION_IN)
3558 {
3559 mutex_enter(&pState->Mtx);
3560 vboxusb_urb_t *pIsocFailedUrb = list_remove_tail(&pEp->hIsocInUrbs);
3561 if (pIsocFailedUrb)
3562 {
3563 RTMemFree(pIsocFailedUrb);
3564 --pEp->cIsocInUrbs;
3565 }
3566 pEp->fIsocPolling = false;
3567 mutex_exit(&pState->Mtx);
3568 }
3569 }
3570
3571 if (pUrb->enmDir == VUSBDIRECTION_OUT) /* pUrb->pMsg freed by caller */
3572 pReq->isoc_data = NULL;
3573
3574 usb_free_isoc_req(pReq);
3575 }
3576 else
3577 {
3578 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocXfer failed to alloc isoc req for %d packets\n", pUrb->cIsocPkts));
3579 rc = VERR_NO_MEMORY;
3580 }
3581
3582 return rc;
3583}
3584
3585
3586/**
3587 * Completion/Exception callback for Isoc IN Xfers.
3588 *
3589 * @param pPipe The Intr pipe handle.
3590 * @param pReq The Intr request.
3591 *
3592 * @remarks Completion callback executes in interrupt context!
3593 */
3594LOCAL void vboxUSBSolarisIsocInXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3595{
3596// LogFunc((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3597
3598 vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3599 if (RT_LIKELY(pState))
3600 {
3601 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3602 if ( pEp
3603 && pEp->pPipe)
3604 {
3605#if 0
3606 /*
3607 * Stop polling if all packets failed.
3608 */
3609 if (pReq->isoc_error_count == pReq->isoc_pkts_count)
3610 {
3611 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted stopping polling! Too many errors.\n"));
3612 mutex_exit(&pState->Mtx);
3613 usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3614 mutex_enter(&pState->Mtx);
3615 pEp->fIsocPolling = false;
3616 }
3617#endif
3618
3619 AssertCompile(sizeof(VUSBISOC_PKT_DESC) == sizeof(usb_isoc_pkt_descr_t));
3620
3621 if (RT_LIKELY(pReq->isoc_data))
3622 {
3623 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted cIsocInUrbs=%d cbIsocInLandedReqs=%d\n", pEp->cIsocInUrbs,
3624 pEp->cbIsocInLandedReqs));
3625
3626 mutex_enter(&pState->Mtx);
3627
3628 /*
3629 * If there are waiting URBs, satisfy the oldest one.
3630 */
3631 if ( pEp->cIsocInUrbs > 0
3632 && pEp->cbIsocInLandedReqs == 0)
3633 {
3634 vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
3635 if (RT_LIKELY(pUrb))
3636 {
3637 --pEp->cIsocInUrbs;
3638 mutex_exit(&pState->Mtx);
3639
3640 for (unsigned i = 0; i < pReq->isoc_pkts_count; i++)
3641 {
3642 pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3643 pUrb->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3644 }
3645
3646 pUrb->pMsg = pReq->isoc_data;
3647 pReq->isoc_data = NULL;
3648
3649 /*
3650 * Move to landed list
3651 */
3652 mutex_enter(&pState->Mtx);
3653 list_insert_tail(&pState->hLandedUrbs, pUrb);
3654 vboxUSBSolarisNotifyComplete(pState);
3655 }
3656 else
3657 {
3658 /* Huh!? cIsocInUrbs is wrong then! Should never happen unless we decide to decrement cIsocInUrbs in
3659 Reap time */
3660 pEp->cIsocInUrbs = 0;
3661 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Extreme error! Isoc. counter b0rked!\n"));
3662 }
3663
3664 mutex_exit(&pState->Mtx);
3665 usb_free_isoc_req(pReq);
3666 return;
3667 }
3668
3669#if 0
3670 /*
3671 * If the maximum buffer size is reached, discard the oldest data.
3672 */
3673 if (pEp->cbIsocInLandedReqs + MBLKL(pReq->isoc_data) > pEp->cbMaxIsocData)
3674 {
3675 vboxusb_isoc_req_t *pOldReq = list_remove_head(&pEp->hIsocInLandedReqs);
3676 if (RT_LIKELY(pOldReq))
3677 {
3678 pEp->cbIsocInLandedReqs -= MBLKL(pOldReq->pMsg);
3679 kmem_free(pOldReq, sizeof(vboxusb_isoc_req_t));
3680 }
3681 }
3682
3683 mutex_exit(&pState->Mtx);
3684
3685 /*
3686 * Buffer incoming data if the guest has not yet queued any Input URBs.
3687 */
3688 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Buffering\n"));
3689 vboxusb_isoc_req_t *pIsocReq = kmem_alloc(sizeof(vboxusb_isoc_req_t), KM_NOSLEEP);
3690 if (RT_LIKELY(pIsocReq))
3691 {
3692 pIsocReq->pMsg = pReq->isoc_data;
3693 pReq->isoc_data = NULL;
3694 pIsocReq->cIsocPkts = pReq->isoc_pkts_count;
3695#if 0
3696 for (unsigned i = 0; i < pReq->isoc_pkts_count; i++)
3697 {
3698 pIsocReq->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3699 pIsocReq->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3700 }
3701#else
3702 bcopy(pReq->isoc_pkt_descr, pIsocReq->aIsocPkts, pReq->isoc_pkts_count * sizeof(VUSBISOC_PKT_DESC));
3703#endif
3704
3705 mutex_enter(&pState->Mtx);
3706 list_insert_tail(&pEp->hIsocInLandedReqs, pIsocReq);
3707 pEp->cbIsocInLandedReqs += MBLKL(pIsocReq->pMsg);
3708 mutex_exit(&pState->Mtx);
3709 }
3710 else
3711 {
3712 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted failed to alloc %d bytes for Isoc. queueing\n",
3713 sizeof(vboxusb_isoc_req_t)));
3714 }
3715
3716 /*
3717 * Drain the input URB buffer with the device buffer, queueing them with the landed URBs.
3718 */
3719 mutex_enter(&pState->Mtx);
3720 while (pEp->cIsocInUrbs)
3721 {
3722 vboxusb_urb_t *pUrb = list_remove_head(&pEp->hIsocInUrbs);
3723 if (RT_UNLIKELY(!pUrb))
3724 break;
3725
3726 vboxusb_isoc_req_t *pBuffReq = list_remove_head(&pEp->hIsocInLandedReqs);
3727 if (!pBuffReq)
3728 {
3729 list_insert_head(&pEp->hIsocInUrbs, pUrb);
3730 break;
3731 }
3732
3733 --pEp->cIsocInUrbs;
3734 pEp->cbIsocInLandedReqs -= MBLKL(pBuffReq->pMsg);
3735 mutex_exit(&pState->Mtx);
3736
3737#if 0
3738 for (unsigned i = 0; i < pBuffReq->cIsocPkts; i++)
3739 {
3740 pUrb->aIsocPkts[i].cbActPkt = pBuffReq->aIsocPkts[i].cbActPkt;
3741 pUrb->aIsocPkts[i].enmStatus = pBuffReq->aIsocPkts[i].enmStatus;
3742 }
3743#else
3744 bcopy(pBuffReq->aIsocPkts, pUrb->aIsocPkts, pBuffReq->cIsocPkts * sizeof(VUSBISOC_PKT_DESC));
3745#endif
3746 pUrb->pMsg = pBuffReq->pMsg;
3747 pBuffReq->pMsg = NULL;
3748 kmem_free(pBuffReq, sizeof(vboxusb_isoc_req_t));
3749
3750 /*
3751 * Move to landed list
3752 */
3753 mutex_enter(&pState->Mtx);
3754 list_insert_tail(&pState->hLandedUrbs, pUrb);
3755 vboxUSBSolarisNotifyComplete(pState);
3756 }
3757#endif
3758
3759 mutex_exit(&pState->Mtx);
3760 usb_free_isoc_req(pReq);
3761 return;
3762 }
3763 else
3764 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted data missing.\n"));
3765 }
3766 else
3767 LogRel((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted Pipe Gone.\n"));
3768 }
3769 else
3770 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferCompleted State Gone.\n"));
3771
3772 usb_free_isoc_req(pReq);
3773}
3774
3775
3776/**
3777 * Exception callback for Isoc IN Xfers.
3778 *
3779 * @param pPipe The Intr pipe handle.
3780 * @param pReq The Intr request.
3781 * @remarks Completion callback executes in interrupt context!
3782 */
3783LOCAL void vboxUSBSolarisIsocInXferError(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3784{
3785 LogFunc((DEVICE_NAME ":vboxUSBSolarisIsocInXferError pPipe=%p pReq=%p\n", pPipe, pReq));
3786
3787 vboxusb_state_t *pState = (vboxusb_state_t *)pReq->isoc_client_private;
3788 if (RT_UNLIKELY(!pState))
3789 {
3790 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferError State Gone.\n"));
3791 usb_free_isoc_req(pReq);
3792 return;
3793 }
3794
3795 mutex_enter(&pState->Mtx);
3796 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3797 if (RT_UNLIKELY(!pEp))
3798 {
3799 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferError Pipe Gone.\n"));
3800 mutex_exit(&pState->Mtx);
3801 usb_free_isoc_req(pReq);
3802 return;
3803 }
3804
3805 switch(pReq->isoc_completion_reason)
3806 {
3807 case USB_CR_NO_RESOURCES:
3808 {
3809 /*
3810 * Resubmit the request in case the original request did not complete due to
3811 * immediately unavailable requests
3812 */
3813 mutex_exit(&pState->Mtx);
3814 usb_pipe_isoc_xfer(pPipe, pReq, USB_FLAGS_NOSLEEP);
3815 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferError resubmitted Isoc. IN request due to immediately unavailable "
3816 "resources.\n"));
3817
3818 return;
3819 }
3820
3821 case USB_CR_PIPE_CLOSING:
3822 case USB_CR_STOPPED_POLLING:
3823 case USB_CR_PIPE_RESET:
3824 {
3825 pEp->fIsocPolling = false;
3826 usb_free_isoc_req(pReq);
3827 break;
3828 }
3829
3830 default:
3831 {
3832 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferError stopping Isoc. In. polling due to rc=%d\n",
3833 pReq->isoc_completion_reason));
3834 pEp->fIsocPolling = false;
3835 mutex_exit(&pState->Mtx);
3836 usb_pipe_stop_isoc_polling(pPipe, USB_FLAGS_NOSLEEP);
3837 usb_free_isoc_req(pReq);
3838 mutex_enter(&pState->Mtx);
3839 break;
3840 }
3841 }
3842
3843 /*
3844 * Dequeue i.e. delete the last queued Isoc In. URB. as failed.
3845 */
3846 vboxusb_urb_t *pUrb = list_remove_tail(&pEp->hIsocInUrbs);
3847 if (pUrb)
3848 {
3849 --pEp->cIsocInUrbs;
3850 Log((DEVICE_NAME ":vboxUSBSolarisIsocInXferError Deleting last queued URB as it failed.\n"));
3851 freemsg(pUrb->pMsg);
3852 RTMemFree(pUrb);
3853 vboxUSBSolarisNotifyComplete(pState);
3854 }
3855
3856 mutex_exit(&pState->Mtx);
3857}
3858
3859
3860/**
3861 * Completion/Exception callback for Isoc OUT Xfers.
3862 *
3863 * @param pPipe The Intr pipe handle.
3864 * @param pReq The Intr request.
3865 * @remarks Completion callback executes in interrupt context!
3866 */
3867LOCAL void vboxUSBSolarisIsocOutXferCompleted(usb_pipe_handle_t pPipe, usb_isoc_req_t *pReq)
3868{
3869 LogFunc((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted pPipe=%p pReq=%p\n", pPipe, pReq));
3870
3871 vboxusb_ep_t *pEp = (vboxusb_ep_t *)usb_pipe_get_private(pPipe);
3872 if (RT_LIKELY(pEp))
3873 {
3874 vboxusb_urb_t *pUrb = (vboxusb_urb_t *)pReq->isoc_client_private;
3875 if (RT_LIKELY(pUrb))
3876 {
3877 size_t cbActPkt = 0;
3878 for (int i = 0; i < pReq->isoc_pkts_count; i++)
3879 {
3880 cbActPkt += pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3881 pUrb->aIsocPkts[i].cbActPkt = pReq->isoc_pkt_descr[i].isoc_pkt_actual_length;
3882 pUrb->aIsocPkts[i].enmStatus = vboxUSBSolarisGetUrbStatus(pReq->isoc_pkt_descr[i].isoc_pkt_status);
3883 }
3884
3885 Log((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted cIsocPkts=%d cbData=%d cbActPkt=%d\n", pUrb->cIsocPkts,
3886 pUrb->cbDataR3, cbActPkt));
3887
3888 if (pReq->isoc_completion_reason == USB_CR_OK)
3889 {
3890 if (RT_UNLIKELY(pUrb->pMsg != pReq->isoc_data)) /* Paranoia */
3891 {
3892 freemsg(pUrb->pMsg);
3893 pUrb->pMsg = pReq->isoc_data;
3894 }
3895 }
3896 pReq->isoc_data = NULL;
3897
3898 pUrb->cIsocPkts = pReq->isoc_pkts_count;
3899 pUrb->cbDataR3 = cbActPkt;
3900
3901 /*
3902 * Update the URB and move to landed list for reaping.
3903 */
3904 vboxUSBSolarisDeQueueURB(pUrb, pReq->isoc_completion_reason);
3905 usb_free_isoc_req(pReq);
3906 return;
3907 }
3908 else
3909 Log((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted missing private data!?! Dropping OUT pUrb.\n"));
3910 }
3911 else
3912 Log((DEVICE_NAME ":vboxUSBSolarisIsocOutXferCompleted Pipe Gone.\n"));
3913
3914 usb_free_isoc_req(pReq);
3915}
3916
3917
3918/**
3919 * Callback when the device gets disconnected.
3920 *
3921 * @param pDip The module structure instance.
3922 *
3923 * @returns Solaris USB error code.
3924 */
3925LOCAL int vboxUSBSolarisDeviceDisconnected(dev_info_t *pDip)
3926{
3927 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeviceDisconnected pDip=%p\n", pDip));
3928
3929 int instance = ddi_get_instance(pDip);
3930 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3931
3932 if (RT_LIKELY(pState))
3933 {
3934 /*
3935 * Serialize access: exclusive access to the state.
3936 */
3937 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
3938 mutex_enter(&pState->Mtx);
3939
3940 pState->DevState = USB_DEV_DISCONNECTED;
3941
3942 vboxUSBSolarisCloseAllPipes(pState, true /* ControlPipe */);
3943 vboxUSBSolarisNotifyHotplug(pState);
3944
3945 mutex_exit(&pState->Mtx);
3946 usb_release_access(pState->StateMulti);
3947
3948 return USB_SUCCESS;
3949 }
3950
3951 LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceDisconnected failed to get device state!\n"));
3952 return USB_FAILURE;
3953}
3954
3955
3956/**
3957 * Callback when the device gets reconnected.
3958 *
3959 * @param pDip The module structure instance.
3960 *
3961 * @returns Solaris USB error code.
3962 */
3963LOCAL int vboxUSBSolarisDeviceReconnected(dev_info_t *pDip)
3964{
3965 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeviceReconnected pDip=%p\n", pDip));
3966
3967 int instance = ddi_get_instance(pDip);
3968 vboxusb_state_t *pState = ddi_get_soft_state(g_pVBoxUSBSolarisState, instance);
3969
3970 if (RT_LIKELY(pState))
3971 {
3972 vboxUSBSolarisDeviceRestore(pState);
3973 return USB_SUCCESS;
3974 }
3975
3976 LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceReconnected failed to get device state!\n"));
3977 return USB_FAILURE;
3978}
3979
3980
3981/**
3982 * Restore device state after a reconnect or resume.
3983 *
3984 * @param pState The USB device instance.
3985 */
3986LOCAL void vboxUSBSolarisDeviceRestore(vboxusb_state_t *pState)
3987{
3988 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeviceRestore pState=%p\n", pState));
3989 AssertPtrReturnVoid(pState);
3990
3991 /*
3992 * Raise device power.
3993 */
3994 vboxUSBSolarisPowerBusy(pState);
3995 int rc = pm_raise_power(pState->pDip, 0 /* component */, USB_DEV_OS_FULL_PWR);
3996
3997 /*
3998 * Check if the same device is resumed/reconnected.
3999 */
4000 rc = usb_check_same_device(pState->pDip,
4001 NULL, /* log handle */
4002 USB_LOG_L2, /* log level */
4003 -1, /* log mask */
4004 USB_CHK_ALL, /* check level */
4005 NULL); /* device string */
4006
4007 if (rc != USB_SUCCESS)
4008 {
4009 mutex_enter(&pState->Mtx);
4010 pState->DevState = USB_DEV_DISCONNECTED;
4011 mutex_exit(&pState->Mtx);
4012
4013 /* Do we need to inform userland here? */
4014 vboxUSBSolarisPowerIdle(pState);
4015 Log((DEVICE_NAME ":vboxUSBSolarisDeviceRestore not the same device.\n"));
4016 return;
4017 }
4018
4019 /*
4020 * Serialize access to not race with other PM functions.
4021 */
4022 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
4023
4024 mutex_enter(&pState->Mtx);
4025 if (pState->DevState == USB_DEV_DISCONNECTED)
4026 pState->DevState = USB_DEV_ONLINE;
4027 else if (pState->DevState == USB_DEV_SUSPENDED)
4028 pState->DevState = USB_DEV_ONLINE;
4029
4030 mutex_exit(&pState->Mtx);
4031 usb_release_access(pState->StateMulti);
4032
4033 vboxUSBSolarisPowerIdle(pState);
4034}
4035
4036
4037/**
4038 * Restore device state after a reconnect or resume.
4039 *
4040 * @param pState The USB device instance.
4041 *
4042 * @returns VBox status code.
4043 */
4044LOCAL int vboxUSBSolarisDeviceSuspend(vboxusb_state_t *pState)
4045{
4046 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend pState=%p\n", pState));
4047
4048 int rc = VERR_VUSB_DEVICE_IS_SUSPENDED;
4049 mutex_enter(&pState->Mtx);
4050
4051 switch (pState->DevState)
4052 {
4053 case USB_DEV_SUSPENDED:
4054 {
4055 LogRel((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend: Invalid device state %d\n", pState->DevState));
4056 break;
4057 }
4058
4059 case USB_DEV_ONLINE:
4060 case USB_DEV_DISCONNECTED:
4061 case USB_DEV_PWRED_DOWN:
4062 {
4063 int PreviousState = pState->DevState;
4064 pState->DevState = USB_DEV_DISCONNECTED;
4065
4066 /*
4067 * Drain pending URBs.
4068 */
4069 for (int i = 0; i < VBOXUSB_DRAIN_TIME; i++)
4070 {
4071 if (pState->cInflightUrbs < 1)
4072 break;
4073
4074 mutex_exit(&pState->Mtx);
4075 delay(drv_usectohz(100000));
4076 mutex_enter(&pState->Mtx);
4077 }
4078
4079 /*
4080 * Deny suspend if we still have pending URBs.
4081 */
4082 if (pState->cInflightUrbs > 0)
4083 {
4084 pState->DevState = PreviousState;
4085 LogRel((DEVICE_NAME ":Cannot suspend, still have %d inflight URBs.\n", pState->cInflightUrbs));
4086
4087 mutex_exit(&pState->Mtx);
4088 return VERR_RESOURCE_BUSY;
4089 }
4090
4091 pState->cInflightUrbs = 0;
4092
4093 /*
4094 * Serialize access to not race with Open/Detach/Close and
4095 * Close all pipes including the default pipe.
4096 */
4097 mutex_exit(&pState->Mtx);
4098 usb_serialize_access(pState->StateMulti, USB_WAIT, 0);
4099 mutex_enter(&pState->Mtx);
4100
4101 vboxUSBSolarisCloseAllPipes(pState, true /* default pipe */);
4102 vboxUSBSolarisNotifyHotplug(pState);
4103
4104 mutex_exit(&pState->Mtx);
4105 usb_release_access(pState->StateMulti);
4106 return VINF_SUCCESS;
4107 }
4108 }
4109
4110 mutex_exit(&pState->Mtx);
4111 Log((DEVICE_NAME ":vboxUSBSolarisDeviceSuspend returns %d\n", rc));
4112 return rc;
4113}
4114
4115
4116/**
4117 * Restore device state after a reconnect or resume.
4118 *
4119 * @param pState The USB device instance.
4120 */
4121LOCAL void vboxUSBSolarisDeviceResume(vboxusb_state_t *pState)
4122{
4123 LogFunc((DEVICE_NAME ":vboxUSBSolarisDeviceResume pState=%p\n", pState));
4124 return vboxUSBSolarisDeviceRestore(pState);
4125}
4126
4127
4128/**
4129 * Flag the PM component as busy so the system will not manage it's power.
4130 *
4131 * @param pState The USB device instance.
4132 */
4133LOCAL void vboxUSBSolarisPowerBusy(vboxusb_state_t *pState)
4134{
4135 LogFunc((DEVICE_NAME ":vboxUSBSolarisPowerBusy pState=%p\n", pState));
4136 AssertPtrReturnVoid(pState);
4137
4138 mutex_enter(&pState->Mtx);
4139 if (pState->pPower)
4140 {
4141 pState->pPower->PowerBusy++;
4142 mutex_exit(&pState->Mtx);
4143
4144 int rc = pm_busy_component(pState->pDip, 0 /* component */);
4145 if (rc != DDI_SUCCESS)
4146 {
4147 Log((DEVICE_NAME ":vboxUSBSolarisPowerBusy busy component failed! rc=%d\n", rc));
4148 mutex_enter(&pState->Mtx);
4149 pState->pPower->PowerBusy--;
4150 mutex_exit(&pState->Mtx);
4151 }
4152 }
4153 else
4154 mutex_exit(&pState->Mtx);
4155}
4156
4157
4158/**
4159 * Flag the PM component as idle so its power managed by the system.
4160 *
4161 * @param pState The USB device instance.
4162 */
4163LOCAL void vboxUSBSolarisPowerIdle(vboxusb_state_t *pState)
4164{
4165 LogFunc((DEVICE_NAME ":vboxUSBSolarisPowerIdle pState=%p\n", pState));
4166 AssertPtrReturnVoid(pState);
4167
4168 if (pState->pPower)
4169 {
4170 int rc = pm_idle_component(pState->pDip, 0 /* component */);
4171 if (rc == DDI_SUCCESS)
4172 {
4173 mutex_enter(&pState->Mtx);
4174 Assert(pState->pPower->PowerBusy > 0);
4175 pState->pPower->PowerBusy--;
4176 mutex_exit(&pState->Mtx);
4177 }
4178 else
4179 Log((DEVICE_NAME ":vboxUSBSolarisPowerIdle idle component failed! rc=%d\n", rc));
4180 }
4181}
4182
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