2 * usb.c - USB driver stack project for Windows NT 4.0
4 * Copyright (c) 2002-2004 Zhiming mypublic99@yahoo.com
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program (in the main directory of the distribution, the file
18 * COPYING); if not, write to the Free Software Foundation,Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "usbdriver.h"
25 ULONG cpu_clock_freq
= 0;
27 NTSTATUS
usb_get_descriptor(PUSB_DEV pdev
, PURB purb
);
28 VOID
usb_set_interface_completion(PURB purb
, PVOID context
);
29 NTSTATUS
usb_set_interface(PURB purb
);
32 usb_alloc_mem(POOL_TYPE pool_type
, LONG size
)
36 ret
= ExAllocatePool(pool_type
, size
);
37 usb_dbg_print(DBGLVL_ULTRA
, ("usb_alloc_mem(): alloced=0x%x\n", g_alloc_cnt
));
42 usb_free_mem(PVOID pbuf
)
45 usb_dbg_print(DBGLVL_ULTRA
, ("usb_free_mem(): alloced=0x%x\n", g_alloc_cnt
));
49 VOID
usb_config_dev_completion(PURB purb
, PVOID context
);
51 //shamelessly pasted from linux's usb.c
53 usb_calc_bus_time(LONG speed
, LONG input_dir
, LONG is_iso
, LONG byte_count
)
57 switch (speed
& 0x3) /* no isoc. here */
63 tmp
= (67667L * (31L + 10L * bit_time(byte_count
))) / 1000L;
64 return (64060L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
68 tmp
= (66700L * (31L + 10L * bit_time(byte_count
))) / 1000L;
69 return (64107L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
76 if (!is_iso
) /* Input or Output */
78 tmp
= (8354L * (31L + 10L * bit_time(byte_count
))) / 1000L;
79 return (9107L + BW_HOST_DELAY
+ tmp
);
84 tmp
= (8354L * (31L + 10L * bit_time(byte_count
))) / 1000L;
85 return (((input_dir
) ? 7268L : 6265L) + BW_HOST_DELAY
+ tmp
);
91 tmp
= (999 + 926520 + 2083 * ((LONG
) ((19 + 7 * 8 * byte_count
) / 6))) / 1000;
95 tmp
= (999 + 633232 + 2083 * ((LONG
) ((19 + 7 * 8 * byte_count
) / 6))) / 1000;
97 return tmp
+ USB2_HOST_DELAY
;
108 // if the dev is not in the list, return value is not success and the pointer is nulled
109 // if the dev is in the list but zomb, return value is error code and the pointer is the dev( no ref_count guarded )
110 // if the dev is alive and in the list, return is success and the pointer is the dev.
111 // one must be aware of what his doing before he uses the ppdev
114 usb_query_and_lock_dev(PUSB_DEV_MANAGER dev_mgr
, DEV_HANDLE dev_handle
, PUSB_DEV
* ppdev
)
117 PLIST_ENTRY pthis
, pnext
;
118 PUSB_DEV pdev
= NULL
;
121 USE_NON_PENDING_IRQL
;
125 if (dev_mgr
== NULL
|| dev_handle
== 0)
126 return STATUS_INVALID_PARAMETER
;
128 i
= dev_id_from_handle(dev_handle
);
130 KeAcquireSpinLock(&dev_mgr
->dev_list_lock
, &old_irql
);
131 ListFirst(&dev_mgr
->dev_list
, pthis
);
135 pdev
= (PUSB_DEV
) pthis
;
136 if (pdev
->dev_id
!= (ULONG
) i
)
138 ListNext(&dev_mgr
->dev_list
, pthis
, pnext
);
148 KeReleaseSpinLock(&dev_mgr
->dev_list_lock
, old_irql
);
149 return STATUS_INVALID_PARAMETER
;
154 lock_dev(pdev
, TRUE
);
156 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
161 pdev
->ref_count
++; //guard the dev by increasing the ref count
163 unlock_dev(pdev
, TRUE
);
165 KeReleaseSpinLock(&dev_mgr
->dev_list_lock
, old_irql
);
170 return STATUS_DEVICE_DOES_NOT_EXIST
;
172 return STATUS_SUCCESS
;
177 usb_unlock_dev(PUSB_DEV dev
)
179 USE_BASIC_NON_PENDING_IRQL
;
182 return STATUS_INVALID_PARAMETER
;
184 lock_dev(dev
, FALSE
);
186 if (dev
->ref_count
< 0)
188 unlock_dev(dev
, FALSE
);
189 return STATUS_SUCCESS
;
193 usb_reset_pipe_ex(PUSB_DEV_MANAGER dev_mgr
,
194 DEV_HANDLE endp_handle
, //endp handle to reset
195 PURBCOMPLETION reset_completion
, //note: this reset completion has no right to delete the urb, that is only for reference
200 LONG if_idx
, endp_idx
;
202 USE_BASIC_NON_PENDING_IRQL
;
205 return STATUS_INVALID_PARAMETER
;
207 status
= usb_query_and_lock_dev(dev_mgr
, (endp_handle
& 0xffff0000), &pdev
);
208 if (status
!= STATUS_SUCCESS
)
209 return STATUS_UNSUCCESSFUL
;
211 lock_dev(pdev
, FALSE
);
212 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
214 status
= STATUS_UNSUCCESSFUL
;
218 if_idx
= if_idx_from_handle(endp_handle
);
219 endp_idx
= endp_idx_from_handle(endp_handle
);
221 if (default_endp_handle(endp_handle
))
223 status
= STATUS_UNSUCCESSFUL
;
227 if (dev_state(pdev
) < USB_DEV_STATE_CONFIGURED
)
229 status
= STATUS_DEVICE_NOT_READY
;
233 pendp
= &pdev
->usb_config
->interf
[if_idx
].endp
[endp_idx
];
234 unlock_dev(pdev
, FALSE
) status
= usb_reset_pipe(pdev
, pendp
, reset_completion
, param
);
235 usb_unlock_dev(pdev
);
239 unlock_dev(pdev
, FALSE
);
240 usb_unlock_dev(pdev
);
245 // caller must guarantee the pdev exist before the routine exit
247 usb_reset_pipe(PUSB_DEV pdev
, PUSB_ENDPOINT pendp
, PURBCOMPLETION client_reset_pipe_completion
, PVOID param
)
254 DEV_HANDLE dev_handle
;
256 USE_BASIC_NON_PENDING_IRQL
;
258 if (pdev
== NULL
|| pendp
== NULL
)
259 return STATUS_INVALID_PARAMETER
;
261 lock_dev(pdev
, FALSE
);
262 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
264 unlock_dev(pdev
, FALSE
);
265 return STATUS_DEVICE_DOES_NOT_EXIST
;
269 endp_addr
= pendp
->pusb_endp_desc
->bEndpointAddress
;
270 dev_handle
= usb_make_handle(pdev
->dev_id
, 0, 0);
271 unlock_dev(pdev
, FALSE
);
273 purb
= (PURB
) usb_alloc_mem(NonPagedPool
, sizeof(URB
) + sizeof(PIRP
));
276 return STATUS_NO_MEMORY
;
278 UsbBuildResetPipeRequest(purb
,
281 usb_reset_pipe_completion
,
283 (LONG
)client_reset_pipe_completion
);
285 *((PULONG
)&purb
[1]) = (ULONG
)param
;
287 if ((status
= hcd
->hcd_submit_urb(hcd
, pdev
, &pdev
->default_endp
, purb
)) != STATUS_PENDING
)
296 usb_reset_pipe_completion(PURB purb
, PVOID context
)
301 USE_BASIC_NON_PENDING_IRQL
;
303 if (purb
== NULL
|| context
== NULL
)
307 pendp
= (PUSB_ENDPOINT
) context
;
309 lock_dev(pdev
, TRUE
);
310 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
315 if (usb_error(purb
->status
))
320 pendp
->flags
&= ~USB_ENDP_FLAG_STAT_MASK
;
322 //reset toggle endp_type
323 if ((pendp
->pusb_endp_desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_BULK
||
324 (pendp
->pusb_endp_desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
326 pendp
->flags
&= ~USB_ENDP_FLAG_DATATOGGLE
;
330 unlock_dev(pdev
, TRUE
);
333 ((PURBCOMPLETION
) purb
->reference
) (purb
, (PVOID
) (*((PULONG
) & purb
[1])));
342 usb_reset_pipe_from_dispatch_completion(PURB purb
, PVOID param
)
345 if (purb
== NULL
|| param
== NULL
)
347 pclient_urb
= (PURB
) param
;
348 pclient_urb
->status
= purb
->status
;
350 if (pclient_urb
->completion
)
352 pclient_urb
->completion(pclient_urb
, pclient_urb
->context
);
354 // the urb can not be freed here because it is owned by the reset
359 //used to check descriptor validity
361 is_header_match(PUCHAR pbuf
, ULONG type
)
364 PUSB_DESC_HEADER phdr
;
365 phdr
= (PUSB_DESC_HEADER
) pbuf
;
371 ret
= (phdr
->bLength
== sizeof(USB_DEVICE_DESC
) && phdr
->bDescriptorType
== USB_DT_DEVICE
);
376 ret
= (phdr
->bLength
== sizeof(USB_CONFIGURATION_DESC
) && phdr
->bDescriptorType
== USB_DT_CONFIG
);
379 case USB_DT_INTERFACE
:
381 ret
= (phdr
->bLength
== sizeof(USB_INTERFACE_DESC
) && phdr
->bDescriptorType
== USB_DT_INTERFACE
);
384 case USB_DT_ENDPOINT
:
386 ret
= (phdr
->bLength
== sizeof(USB_ENDPOINT_DESC
) && phdr
->bDescriptorType
== USB_DT_ENDPOINT
);
396 usb_skip_endp_desc(PBYTE
* pbUF
, LONG n
)
398 if (is_header_match(*pbUF
, USB_DT_ENDPOINT
))
400 (*pbUF
) += sizeof(USB_ENDPOINT_DESC
) * n
;
407 usb_skip_if_desc(PBYTE
* pBUF
)
410 PUSB_INTERFACE_DESC pif_desc
= (PUSB_INTERFACE_DESC
) * pBUF
;
412 ret
= is_header_match((PBYTE
) * pBUF
, USB_DT_INTERFACE
);
415 endp_count
= pif_desc
->bNumEndpoints
;
416 if (endp_count
< MAX_ENDPS_PER_IF
)
419 ret
= usb_skip_endp_desc((PBYTE
*) & pif_desc
, endp_count
);
421 *(pBUF
) = (PBYTE
) pif_desc
;
430 usb_skip_if_and_altif(PUCHAR
* pdesc_BUF
)
433 PUSB_INTERFACE_DESC pif_desc1
= (PUSB_INTERFACE_DESC
) * pdesc_BUF
;
434 ret
= is_header_match(*pdesc_BUF
, USB_DT_INTERFACE
);
437 if (pif_desc1
->bAlternateSetting
== 0)
438 ret
= usb_skip_if_desc((PUCHAR
*) & pif_desc1
);
440 //no default interface
443 while (ret
&& pif_desc1
->bAlternateSetting
!= 0)
444 ret
= usb_skip_if_desc((PUCHAR
*) & pif_desc1
);
447 *pdesc_BUF
= (PUCHAR
) pif_desc1
;
453 usb_skip_one_config(PUCHAR
*pconfig_desc_BUF
)
457 PUSB_CONFIGURATION_DESC pcfg_DESC
= (PUSB_CONFIGURATION_DESC
) * pconfig_desc_BUF
;
458 PUSB_INTERFACE_DESC pif_desc2
= (PUSB_INTERFACE_DESC
) & pcfg_DESC
[1];
460 ret
= is_header_match((PUCHAR
) pcfg_DESC
, USB_DT_CONFIG
);
462 *pconfig_desc_BUF
= &((BYTE
*) pcfg_DESC
)[pcfg_DESC
->wTotalLength
];
465 ret
= is_header_match((PUCHAR
) pcfg_DESC
, USB_DT_CONFIG
)
466 && is_header_match((PUCHAR
) pif_desc2
, USB_DT_INTERFACE
);
470 if_count
= pcfg_DESC
->bNumInterfaces
;
471 if (if_count
< MAX_INTERFACES_PER_CONFIG
)
473 for(i
= 0; i
< if_count
; i
++)
475 ret
= usb_skip_if_and_altif((PUCHAR
*) & pif_desc2
);
480 *pconfig_desc_BUF
= (PUCHAR
) pif_desc2
;
486 PUSB_CONFIGURATION_DESC
487 usb_find_config_desc_by_idx(PUCHAR pbuf
, LONG idx
, LONG cfg_count
)
491 PUSB_CONFIGURATION_DESC pcfg_desc
= (PUSB_CONFIGURATION_DESC
) pbuf
;
492 if (pcfg_desc
== NULL
)
495 if (cfg_count
> MAX_CONFIGS_PER_DEV
)
504 for(i
= 0; i
< idx
- 1; i
++)
506 ret
= usb_skip_one_config((PBYTE
*) & pcfg_desc
);
513 PUSB_CONFIGURATION_DESC
514 usb_find_config_desc_by_val(PBYTE pbuf
, LONG val
, LONG cfg_count
)
518 PUSB_CONFIGURATION_DESC pcfg_desc
= (PUSB_CONFIGURATION_DESC
) pbuf
;
519 if (pcfg_desc
== NULL
)
522 if (cfg_count
> MAX_CONFIGS_PER_DEV
)
525 for(i
= 0; i
< cfg_count
; i
++)
527 if (pcfg_desc
->bConfigurationValue
== val
)
530 ret
= usb_skip_one_config((PBYTE
*) & pcfg_desc
);
538 #define if_from_handle( handle ) ( ( handle & 0xff00 ) >> 8 )
541 usb_submit_config_urb(PURB purb
)
544 PUSB_DEV_MANAGER dev_mgr
;
547 PUSB_CTRL_SETUP_PACKET psetup
;
551 USE_BASIC_NON_PENDING_IRQL
;
554 return STATUS_INVALID_PARAMETER
;
559 lock_dev(pdev
, FALSE
);
561 dev_mgr
= dev_mgr_from_dev(pdev
);
564 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
566 status
= STATUS_DEVICE_DOES_NOT_EXIST
;
570 if (dev_state(pdev
) == USB_DEV_STATE_FIRST_CONFIG
|| dev_state(pdev
) == USB_DEV_STATE_RECONFIG
)
572 //outstanding request of set configuration exists in process
573 status
= STATUS_UNSUCCESSFUL
;
577 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
579 if (dev_state(pdev
) == USB_DEV_STATE_CONFIGURED
580 && pdev
->usb_config
->pusb_config_desc
->bConfigurationValue
== (BYTE
) psetup
->wValue
)
582 //already the current config
583 status
= STATUS_SUCCESS
;
588 if (dev_state(pdev
) == USB_DEV_STATE_CONFIGURED
)
590 // not support re-configuration yet
591 status
= STATUS_NOT_SUPPORTED
;
595 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
596 purb1
= usb_alloc_mem(NonPagedPool
, sizeof(URB
));
599 status
= STATUS_NO_MEMORY
;
603 UsbBuildSelectConfigurationRequest(purb1
,
604 usb_make_handle(pdev
->dev_id
, 0, 0) | 0xffff,
605 psetup
->wValue
, usb_config_dev_completion
, 0, ((ULONG
) purb
));
607 purb1
->pendp
= pendp
;
609 //change the dev state
610 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
611 pdev
->flags
|= USB_DEV_STATE_FIRST_CONFIG
;
613 unlock_dev(pdev
, FALSE
);
615 status
= hcd
->hcd_submit_urb(hcd
, pdev
, pendp
, purb1
);
616 if (status
!= STATUS_PENDING
)
624 unlock_dev(pdev
, FALSE
);
630 usb_submit_urb(PUSB_DEV_MANAGER dev_mgr
, PURB purb
)
634 LONG if_idx
, endp_idx
;
635 DEV_HANDLE endp_handle
;
636 PUSB_CTRL_SETUP_PACKET psetup
;
640 USE_BASIC_NON_PENDING_IRQL
;
642 if (purb
== NULL
|| dev_mgr
== NULL
)
643 return STATUS_INVALID_PARAMETER
;
645 endp_handle
= purb
->endp_handle
;
647 if (endp_handle
== 0)
648 return STATUS_INVALID_PARAMETER
;
650 status
= usb_query_and_lock_dev(dev_mgr
, endp_handle
, &pdev
);
651 if (status
!= STATUS_SUCCESS
)
656 if_idx
= if_idx_from_handle(endp_handle
);
657 endp_idx
= endp_idx_from_handle(endp_handle
);
659 lock_dev(pdev
, FALSE
);
660 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
662 unlock_dev(pdev
, FALSE
);
663 status
= STATUS_DEVICE_DOES_NOT_EXIST
;
667 if (dev_state(pdev
) < USB_DEV_STATE_ADDRESSED
)
669 unlock_dev(pdev
, FALSE
);
670 status
= STATUS_DEVICE_NOT_READY
;
674 dev_mgr
= dev_mgr_from_dev(pdev
);
677 if (default_endp_handle(endp_handle
))
680 pendp
= &pdev
->default_endp
;
682 else if (if_idx
>= MAX_INTERFACES_PER_CONFIG
|| endp_idx
>= MAX_ENDPS_PER_IF
)
684 status
= STATUS_INVALID_PARAMETER
;
685 unlock_dev(pdev
, FALSE
);
690 if (dev_state(pdev
) < USB_DEV_STATE_CONFIGURED
)
692 status
= STATUS_DEVICE_NOT_READY
;
693 unlock_dev(pdev
, FALSE
);
696 pendp
= &pdev
->usb_config
->interf
[if_idx
].endp
[endp_idx
];
703 //for default endpoint we have some special process
704 if (default_endp_handle(endp_handle
))
706 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
707 if (psetup
->bmRequestType
== 0 && psetup
->bRequest
== USB_REQ_SET_CONFIGURATION
)
709 unlock_dev(pdev
, FALSE
);
710 status
= usb_submit_config_urb(purb
);
713 else if (psetup
->bmRequestType
== 1 && psetup
->bRequest
== USB_REQ_SET_INTERFACE
)
715 unlock_dev(pdev
, FALSE
);
716 // status = STATUS_NOT_SUPPORTED;
717 status
= usb_set_interface(purb
);
720 else if (psetup
->bmRequestType
== 0x80 && psetup
->bRequest
== USB_REQ_GET_DESCRIPTOR
)
722 if ((psetup
->wValue
>> 8) == USB_DT_CONFIG
|| (psetup
->wValue
>> 8) == USB_DT_DEVICE
)
724 unlock_dev(pdev
, FALSE
);
725 status
= usb_get_descriptor(pdev
, purb
);
728 //get the descriptor directly
729 //status = hcd->hcd_submit_urb( hcd, pdev, purb->pendp, purb );
733 else if (psetup
->bmRequestType
== 0x02 && psetup
->bRequest
== USB_REQ_CLEAR_FEATURE
&& psetup
->wValue
== 0) //reset pipe
737 endp_addr
= psetup
->wIndex
;
738 if ((endp_addr
& 0xf) == 0)
740 unlock_dev(pdev
, FALSE
);
741 status
= STATUS_INVALID_PARAMETER
;
745 // search for the endp by the endp addr in the wIndex
747 for(if_idx
= 0; if_idx
< pdev
->usb_config
->if_count
; if_idx
++)
749 for(endp_idx
= 0; endp_idx
< pdev
->usb_config
->interf
[if_idx
].endp_count
; endp_idx
++)
751 pendp
= &pdev
->usb_config
->interf
[if_idx
].endp
[endp_idx
];
752 if (pendp
->pusb_endp_desc
->bEndpointAddress
== endp_addr
)
762 endp_handle
= usb_make_handle(pdev
->dev_id
, if_idx
, endp_idx
);
765 unlock_dev(pdev
, FALSE
);
766 status
= STATUS_INVALID_PARAMETER
;
769 unlock_dev(pdev
, FALSE
);
770 status
= usb_reset_pipe_ex(dev_mgr
, endp_handle
, usb_reset_pipe_from_dispatch_completion
, purb
);
776 unlock_dev(pdev
, FALSE
);
777 status
= hcd
->hcd_submit_urb(hcd
, pdev
, purb
->pendp
, purb
);
780 usb_unlock_dev(pdev
);
785 usb_config_dev_completion(PURB purb
, PVOID context
)
790 PUSB_CTRL_SETUP_PACKET psetup
;
794 USE_BASIC_NON_PENDING_IRQL
;
796 UNREFERENCED_PARAMETER(context
);
808 if (purb
->reference
!= 0)
809 puser_urb
= (PURB
) purb
->reference
;
813 lock_dev(pdev
, TRUE
);
816 puser_urb
->status
= purb
->status
;
818 if (purb
->status
!= STATUS_SUCCESS
)
820 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
825 if (dev_state(pdev
) == USB_DEV_STATE_FIRST_CONFIG
)
827 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
828 pdev
->flags
|= USB_DEV_STATE_ADDRESSED
;
830 else if (dev_state(pdev
) == USB_DEV_STATE_RECONFIG
)
832 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
833 pdev
->flags
|= USB_DEV_STATE_CONFIGURED
;
838 // now let's construct usb_config
839 if (!pdev
->usb_config
)
841 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
842 config_val
= psetup
->wValue
;
843 status
= dev_mgr_build_usb_config(pdev
,
844 &pdev
->desc_buf
[sizeof(USB_DEVICE_DESC
)],
845 config_val
, pdev
->pusb_dev_desc
->bNumConfigurations
);
846 if (status
!= STATUS_SUCCESS
)
848 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
849 pdev
->flags
|= USB_DEV_STATE_ADDRESSED
;
852 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
853 pdev
->flags
|= USB_DEV_STATE_CONFIGURED
;
854 //this usb dev represents physical dev
855 if (pdev
->pusb_dev_desc
->bDeviceClass
== USB_CLASS_HUB
&& pdev
->pusb_dev_desc
->bDeviceSubClass
== 0)
857 pdev
->flags
&= ~USB_DEV_CLASS_MASK
;
858 pdev
->flags
|= USB_DEV_CLASS_HUB
;
860 else if (pdev
->pusb_dev_desc
->bDeviceClass
== USB_CLASS_MASS_STORAGE
861 && pdev
->pusb_dev_desc
->bDeviceSubClass
== 0)
863 pdev
->flags
&= ~USB_DEV_CLASS_MASK
;
864 pdev
->flags
|= USB_DEV_CLASS_MASSSTOR
;
868 pdev
->flags
&= ~USB_DEV_CLASS_MASK
;
869 pdev
->flags
|= USB_DEV_CLASS_SCANNER
;
875 puser_urb
->status
= STATUS_NOT_SUPPORTED
;
876 pdev
->flags
&= ~USB_DEV_STATE_MASK
;
877 pdev
->flags
|= USB_DEV_STATE_CONFIGURED
;
881 unlock_dev(pdev
, TRUE
);
883 if (puser_urb
&& puser_urb
->completion
)
884 puser_urb
->completion(puser_urb
, puser_urb
->context
);
890 usb_get_descriptor(PUSB_DEV pdev
, PURB purb
)
892 PUSB_CTRL_SETUP_PACKET psetup
;
893 LONG idx
, size
, count
, i
;
895 PUSB_CONFIGURATION_DESC pcfg_desc1
;
897 USE_BASIC_NON_PENDING_IRQL
;
899 if (pdev
== NULL
|| purb
== NULL
)
900 return STATUS_INVALID_PARAMETER
;
902 if (purb
->data_buffer
== NULL
|| purb
->data_length
== 0)
904 return purb
->status
= STATUS_INVALID_PARAMETER
;
907 lock_dev(pdev
, FALSE
);
908 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
909 if (pdev
->desc_buf
== NULL
)
911 purb
->status
= STATUS_DEVICE_NOT_READY
;
915 if ((psetup
->wValue
>> 8) == USB_DT_CONFIG
)
917 idx
= (psetup
->wValue
& 0xff);
919 count
= pdev
->pusb_dev_desc
->bNumConfigurations
;
923 purb
->status
= STATUS_INVALID_PARAMETER
;
926 buf
= &pdev
->desc_buf
[sizeof(USB_DEVICE_DESC
)];
927 pcfg_desc1
= usb_find_config_desc_by_idx(buf
, idx
, count
);
928 if (pcfg_desc1
== NULL
)
930 purb
->status
= STATUS_UNSUCCESSFUL
;
934 size
= pcfg_desc1
->wTotalLength
;
935 size
= size
> purb
->data_length
? purb
->data_length
: size
;
936 for(i
= 0; i
< size
; i
++)
938 purb
->data_buffer
[i
] = ((PBYTE
) pcfg_desc1
)[i
];
940 purb
->status
= STATUS_SUCCESS
;
944 else if ((psetup
->wValue
>> 8) == USB_DT_DEVICE
)
946 size
= purb
->data_length
> sizeof(USB_DEVICE_DESC
) ? sizeof(USB_DEVICE_DESC
) : purb
->data_length
;
948 for(i
= 0; i
< size
; i
++)
950 purb
->data_buffer
[i
] = ((PBYTE
) pdev
->pusb_dev_desc
)[i
];
952 purb
->status
= STATUS_SUCCESS
;
956 unlock_dev(pdev
, FALSE
);
961 usb_count_list(PLIST_HEAD list_head
)
964 PLIST_ENTRY pthis
, pnext
;
966 if (list_head
== NULL
)
970 ListFirst(list_head
, pthis
);
974 ListNext(list_head
, pthis
, pnext
);
981 // checks if processor supports Time Stamp Counter
983 usb_query_clicks(PLARGE_INTEGER clicks
)
986 //so we have to use intel's cpu???
994 mov eax
, 1; //read version
996 test edx
, 0x10; //timer stamp
998 // cpuid //serialization
1000 mov ebx
, dword ptr
[clicks
];
1001 mov dword ptr
[ebx
], eax
;
1002 mov dword ptr
[ebx
+ 4], edx
;
1003 mov dword ptr
[ret_val
], TRUE
;
1015 usb_wait_ms_dpc(ULONG ms
)
1017 LARGE_INTEGER Interval
;
1021 Interval
.QuadPart
= -ms
* 10000;
1022 KeDelayExecutionThread(KernelMode
, FALSE
, &Interval
);
1027 usb_wait_us_dpc(ULONG us
)
1029 LARGE_INTEGER Interval
;
1033 Interval
.QuadPart
= -us
;
1034 KeDelayExecutionThread(KernelMode
, FALSE
, &Interval
);
1040 LARGE_INTEGER tick1
, tick2
;
1042 // interval.QuadPart = -40 * 1000 * 1000;
1044 if (cpu_clock_freq
>= 100 * 1000 * 1000) // assume it is valid
1047 if (usb_query_clicks(&tick1
))
1049 for(i
= 0; i
< 25; i
++)
1051 usb_query_clicks(&tick1
);
1052 KeStallExecutionProcessor(40 * 1000);
1053 usb_query_clicks(&tick2
);
1054 cpu_clock_freq
+= (ULONG
) (tick2
.QuadPart
- tick1
.QuadPart
);
1056 // cpu_clock_freq *= 1000;
1057 usb_dbg_print(DBGLVL_MAXIMUM
, ("usb_cal_cpu_freq(): cpu frequency = %d Hz\n", cpu_clock_freq
));
1062 usb_set_interface(PURB purb
)
1066 PCTRL_REQ_STACK pstack
;
1068 PUSB_CTRL_SETUP_PACKET psetup
;
1069 PUSB_ENDPOINT pendp
;
1073 USE_BASIC_NON_PENDING_IRQL
;
1077 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
1079 lock_dev(pdev
, FALSE
);
1080 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
1082 unlock_dev(pdev
, FALSE
);
1083 return STATUS_DEVICE_NOT_CONNECTED
;
1085 if (dev_state(pdev
) < USB_DEV_STATE_CONFIGURED
)
1087 unlock_dev(pdev
, FALSE
);
1088 return STATUS_DEVICE_NOT_READY
;
1093 if (psetup
->wIndex
>= pdev
->usb_config
->if_count
)
1095 unlock_dev(pdev
, FALSE
);
1096 return STATUS_INVALID_PARAMETER
;
1098 if (psetup
->wValue
>= pdev
->usb_config
->interf
[psetup
->wIndex
].altif_count
+ 1)
1100 unlock_dev(pdev
, FALSE
);
1101 return STATUS_INVALID_PARAMETER
;
1103 if (pdev
->usb_config
->interf
[psetup
->wIndex
].pusb_if_desc
->bAlternateSetting
== psetup
->wValue
)
1105 // already the current interface
1106 unlock_dev(pdev
, FALSE
);
1107 return STATUS_SUCCESS
;
1109 // check to see if the endp is busy
1110 for(u
= 0; u
< pdev
->usb_config
->interf
[psetup
->wIndex
].endp_count
; u
++)
1112 // This check is not adquate. Since we do not have mechanism to block the new coming
1113 // request during this request. the caller must guarantee no active or pending
1114 // usb request on these endpoint.
1115 pendp
= &pdev
->usb_config
->interf
[psetup
->wIndex
].endp
[u
];
1116 if (usb_endp_busy_count(pendp
))
1118 // active urb on that endp
1119 unlock_dev(pdev
, FALSE
);
1120 return STATUS_DEVICE_NOT_READY
;
1122 if (IsListEmpty(&pendp
->urb_list
))
1124 // pending urb on that endp
1125 unlock_dev(pdev
, FALSE
);
1126 return STATUS_DEVICE_NOT_READY
;
1129 unlock_dev(pdev
, FALSE
);
1131 if (purb1
->ctrl_req_context
.ctrl_stack_count
== 0)
1133 // ok, we have one stack cell for our use
1134 if (purb1
->completion
!= NULL
)
1136 purb1
->ctrl_req_context
.ctrl_stack_count
= 1;
1137 purb1
->ctrl_req_context
.ctrl_cur_stack
= 0;
1141 // use urb's completion and context
1142 purb1
->completion
= usb_set_interface_completion
;
1143 purb1
->context
= pdev
;
1148 if (purb
->ctrl_req_context
.ctrl_cur_stack
+ 1 >= purb
->ctrl_req_context
.ctrl_stack_count
)
1150 // stack full, let's allocate one new urb, we need stack size one
1151 purb1
= usb_alloc_mem(NonPagedPool
, sizeof(URB
));
1153 return STATUS_NO_MEMORY
;
1155 RtlCopyMemory(purb1
, purb
, sizeof(URB
));
1157 // we do not use stack
1158 RtlZeroMemory(purb1
->ctrl_req_stack
, sizeof(CTRL_REQ_STACK
));
1159 purb1
->context
= pdev
;
1160 purb1
->completion
= usb_set_interface_completion
;
1161 purb1
->ctrl_parent_urb
= purb
;
1162 purb1
->ctrl_req_context
.ctrl_req_flags
= CTRL_PARENT_URB_VALID
;
1167 purb
->ctrl_req_context
.ctrl_cur_stack
++;
1170 u
= purb1
->ctrl_req_context
.ctrl_cur_stack
;
1171 RtlZeroMemory(&purb1
->ctrl_req_stack
[u
], sizeof(CTRL_REQ_STACK
));
1172 pstack
= &purb1
->ctrl_req_stack
[u
];
1173 pstack
->context
= pdev
;
1174 pstack
->urb_completion
= usb_set_interface_completion
;
1178 return STATUS_INVALID_PARAMETER
;
1180 status
= hcd
->hcd_submit_urb(hcd
, purb
->pdev
, purb
->pendp
, purb
);
1184 #define usb_complete_and_free_ctrl_urb( pURB ) \
1187 i = pURB->ctrl_req_context.ctrl_cur_stack;\
1188 j = pURB->ctrl_req_context.ctrl_stack_count;\
1189 usb_call_ctrl_completion( pURB );\
1190 if( i == 0xff || j == 0 )\
1191 usb_free_mem( pURB );\
1195 usb_set_interface_completion(PURB purb
, PVOID context
)
1197 PUSB_CTRL_SETUP_PACKET psetup
;
1198 PUSB_INTERFACE pif
, palt_if
;
1199 USB_INTERFACE temp_if
;
1200 UCHAR if_idx
, if_alt_idx
;
1202 PUSB_ENDPOINT pendp
;
1204 PLIST_ENTRY pthis
, pnext
;
1206 USE_BASIC_NON_PENDING_IRQL
;
1208 UNREFERENCED_PARAMETER(context
);
1213 if (purb
->status
== STATUS_SUCCESS
)
1215 psetup
= (PUSB_CTRL_SETUP_PACKET
) purb
->setup_packet
;
1216 if_idx
= (UCHAR
) psetup
->wIndex
;
1217 if_alt_idx
= (UCHAR
) psetup
->wValue
;
1219 RtlZeroMemory(&temp_if
, sizeof(USB_INTERFACE
));
1221 lock_dev(pdev
, TRUE
);
1222 if (dev_state(pdev
) == USB_DEV_STATE_ZOMB
)
1224 unlock_dev(pdev
, TRUE
);
1225 purb
->status
= STATUS_DEVICE_NOT_CONNECTED
;
1226 purb
->data_length
= 0;
1230 // let's swap the interface
1231 pif
= &pdev
->usb_config
->interf
[if_idx
];
1232 ListFirst(&pif
->altif_list
, pthis
);
1236 palt_if
= struct_ptr(pthis
, USB_INTERFACE
, altif_list
);
1237 if (palt_if
->pusb_if_desc
->bAlternateSetting
== if_alt_idx
)
1242 ListNext(&pif
->altif_list
, pthis
, pnext
);
1247 if (palt_if
!= NULL
)
1249 RtlCopyMemory(&temp_if
, palt_if
, sizeof(USB_INTERFACE
));
1251 palt_if
->endp_count
= pif
->endp_count
;
1252 RtlCopyMemory(palt_if
->endp
, pif
->endp
, sizeof(pif
->endp
));
1253 palt_if
->pif_drv
= pif
->pif_drv
;
1254 palt_if
->pusb_if_desc
= pif
->pusb_if_desc
;
1255 for(i
= 0; i
< palt_if
->endp_count
; i
++)
1257 pendp
= &palt_if
->endp
[i
];
1258 InitializeListHead(&pendp
->urb_list
);
1262 RtlCopyMemory(pif
->endp
, temp_if
.endp
, sizeof(temp_if
.endp
));
1263 pif
->endp_count
= temp_if
.endp_count
;
1264 pif
->pusb_if_desc
= temp_if
.pusb_if_desc
;
1265 for(i
= 0; i
< pif
->endp_count
; i
++)
1267 pendp
= &pif
->endp
[i
];
1268 InitializeListHead(&pendp
->urb_list
);
1275 purb
->status
= STATUS_UNSUCCESSFUL
;
1278 unlock_dev(pdev
, TRUE
);
1281 // for recursive reason, we have to store the parameter ahead
1282 usb_complete_and_free_ctrl_urb(purb
);
1285 // can only be called when current completion finished and called only in
1286 // urb completion. And this func may be called recursively, if this routine
1287 // is called, the urb must be treated as released.
1289 usb_call_ctrl_completion(PURB purb
)
1292 PCTRL_REQ_STACK pstack
;
1299 if (purb
->ctrl_req_context
.ctrl_stack_count
!= 0)
1301 i
= purb
->ctrl_req_context
.ctrl_cur_stack
;
1302 if (i
> 0 && i
< 0x10)
1305 purb
->ctrl_req_context
.ctrl_cur_stack
= (UCHAR
) i
;
1306 pstack
= &purb
->ctrl_req_stack
[i
];
1307 if (pstack
->urb_completion
)
1309 pstack
->urb_completion(purb
, pstack
->context
);
1316 i
= purb
->ctrl_req_context
.ctrl_cur_stack
= 0xff;
1317 if (purb
->completion
)
1319 purb
->completion(purb
, purb
->context
);
1326 // only parent urb's completion, if parent urb exists, can be called
1327 if (purb
->ctrl_req_context
.ctrl_req_flags
& CTRL_PARENT_URB_VALID
)
1329 parent_urb
= purb
->ctrl_parent_urb
;
1332 pstack
= &parent_urb
->ctrl_req_stack
[parent_urb
->ctrl_req_context
.ctrl_cur_stack
];
1333 pstack
->urb_completion(parent_urb
, pstack
->context
);
1342 else if (purb
->ctrl_req_context
.ctrl_req_flags
& CTRL_PARENT_URB_VALID
)
1344 // this is the case when the child urb won't use the stack
1345 parent_urb
= purb
->ctrl_parent_urb
;
1348 // pstack = &parent_urb->ctrl_req_stack[ parent_urb->ctrl_req_context.ctrl_cur_stack ];
1349 // pstack->urb_completion( parent_urb, pstack->context );
1350 usb_call_ctrl_completion(parent_urb
);