VirtualBox

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

Last change on this file since 38736 was 38736, checked in by vboxsync, 13 years ago

*: Please don NOT redefine logger macros.

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