5 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This file contains the USB templates for an USB Mass Storage Device.
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/linker_set.h>
43 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
57 #include <dev/usb/template/usb_template.h>
61 STRING_MSC_DATA_INDEX,
62 STRING_MSC_CONFIG_INDEX,
63 STRING_MSC_VENDOR_INDEX,
64 STRING_MSC_PRODUCT_INDEX,
65 STRING_MSC_SERIAL_INDEX,
70 0x09, 0x04, /* American English */
72 #define STRING_MSC_DATA \
73 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
74 'M', 0, 'a', 0, 's', 0, 's', 0, \
75 ' ', 0, 'S', 0, 't', 0, 'o', 0, \
76 'r', 0, 'a', 0, 'g', 0, 'e', 0, \
77 ' ', 0, 'I', 0, 'n', 0, 't', 0, \
78 'e', 0, 'r', 0, 'f', 0, 'a', 0, \
81 #define STRING_MSC_CONFIG \
82 'D', 0, 'e', 0, 'f', 0, 'a', 0, \
83 'u', 0, 'l', 0, 't', 0, ' ', 0, \
84 'c', 0, 'o', 0, 'n', 0, 'f', 0, \
87 #define STRING_MSC_VENDOR \
88 'F', 0, 'r', 0, 'e', 0, 'e', 0, \
89 'B', 0, 'S', 0, 'D', 0, ' ', 0, \
90 'f', 0, 'o', 0, 'u', 0, 'n', 0, \
91 'd', 0, 'a', 0, 't', 0, 'i', 0, \
94 #define STRING_MSC_PRODUCT \
95 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
96 'M', 0, 'e', 0, 'm', 0, 'o', 0, \
97 'r', 0, 'y', 0, ' ', 0, 'S', 0, \
98 't', 0, 'i', 0, 'c', 0, 'k', 0
100 #define STRING_MSC_SERIAL \
101 'M', 0, 'a', 0, 'r', 0, 'c', 0, \
102 'h', 0, ' ', 0, '2', 0, '0', 0, \
105 /* make the real string descriptors */
107 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
108 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data);
109 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config);
110 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor);
111 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product);
112 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial);
116 static usb_temp_get_string_desc_t msc_get_string_desc;
118 static const struct usb_temp_packet_size bulk_mps = {
119 .mps[USB_SPEED_FULL] = 64,
120 .mps[USB_SPEED_HIGH] = 512,
123 static const struct usb_temp_endpoint_desc bulk_in_ep = {
124 .pPacketSize = &bulk_mps,
125 #ifdef USB_HIP_IN_EP_0
126 .bEndpointAddress = USB_HIP_IN_EP_0,
128 .bEndpointAddress = UE_DIR_IN,
130 .bmAttributes = UE_BULK,
133 static const struct usb_temp_endpoint_desc bulk_out_ep = {
134 .pPacketSize = &bulk_mps,
135 #ifdef USB_HIP_OUT_EP_0
136 .bEndpointAddress = USB_HIP_OUT_EP_0,
138 .bEndpointAddress = UE_DIR_OUT,
140 .bmAttributes = UE_BULK,
143 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
149 static const struct usb_temp_interface_desc msc_data_interface = {
150 .ppEndpoints = msc_data_endpoints,
151 .bInterfaceClass = UICLASS_MASS,
152 .bInterfaceSubClass = UISUBCLASS_SCSI,
153 .bInterfaceProtocol = UIPROTO_MASS_BBB,
154 .iInterface = STRING_MSC_DATA_INDEX,
157 static const struct usb_temp_interface_desc *msc_interfaces[] = {
162 static const struct usb_temp_config_desc msc_config_desc = {
163 .ppIfaceDesc = msc_interfaces,
164 .bmAttributes = UC_BUS_POWERED,
165 .bMaxPower = 25, /* 50 mA */
166 .iConfiguration = STRING_MSC_CONFIG_INDEX,
169 static const struct usb_temp_config_desc *msc_configs[] = {
174 const struct usb_temp_device_desc usb_template_msc = {
175 .getStringDesc = &msc_get_string_desc,
176 .ppConfigDesc = msc_configs,
180 .bDeviceClass = UDCLASS_COMM,
181 .bDeviceSubClass = 0,
182 .bDeviceProtocol = 0,
183 .iManufacturer = STRING_MSC_VENDOR_INDEX,
184 .iProduct = STRING_MSC_PRODUCT_INDEX,
185 .iSerialNumber = STRING_MSC_SERIAL_INDEX,
188 /*------------------------------------------------------------------------*
189 * msc_get_string_desc
192 * NULL: Failure. No such string.
193 * Else: Success. Pointer to string descriptor is returned.
194 *------------------------------------------------------------------------*/
196 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
198 static const void *ptr[STRING_MSC_MAX] = {
199 [STRING_LANG_INDEX] = &string_lang,
200 [STRING_MSC_DATA_INDEX] = &string_msc_data,
201 [STRING_MSC_CONFIG_INDEX] = &string_msc_config,
202 [STRING_MSC_VENDOR_INDEX] = &string_msc_vendor,
203 [STRING_MSC_PRODUCT_INDEX] = &string_msc_product,
204 [STRING_MSC_SERIAL_INDEX] = &string_msc_serial,
207 if (string_index == 0) {
208 return (&string_lang);
210 if (lang_id != 0x0409) {
213 if (string_index < STRING_MSC_MAX) {
214 return (ptr[string_index]);