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/module.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
56 #include <dev/usb/template/usb_template.h>
60 STRING_MSC_DATA_INDEX,
61 STRING_MSC_CONFIG_INDEX,
62 STRING_MSC_VENDOR_INDEX,
63 STRING_MSC_PRODUCT_INDEX,
64 STRING_MSC_SERIAL_INDEX,
69 0x09, 0x04, /* American English */
71 #define STRING_MSC_DATA \
72 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
73 'M', 0, 'a', 0, 's', 0, 's', 0, \
74 ' ', 0, 'S', 0, 't', 0, 'o', 0, \
75 'r', 0, 'a', 0, 'g', 0, 'e', 0, \
76 ' ', 0, 'I', 0, 'n', 0, 't', 0, \
77 'e', 0, 'r', 0, 'f', 0, 'a', 0, \
80 #define STRING_MSC_CONFIG \
81 'D', 0, 'e', 0, 'f', 0, 'a', 0, \
82 'u', 0, 'l', 0, 't', 0, ' ', 0, \
83 'c', 0, 'o', 0, 'n', 0, 'f', 0, \
86 #define STRING_MSC_VENDOR \
87 'F', 0, 'r', 0, 'e', 0, 'e', 0, \
88 'B', 0, 'S', 0, 'D', 0, ' ', 0, \
89 'f', 0, 'o', 0, 'u', 0, 'n', 0, \
90 'd', 0, 'a', 0, 't', 0, 'i', 0, \
93 #define STRING_MSC_PRODUCT \
94 'U', 0, 'S', 0, 'B', 0, ' ', 0, \
95 'M', 0, 'e', 0, 'm', 0, 'o', 0, \
96 'r', 0, 'y', 0, ' ', 0, 'S', 0, \
97 't', 0, 'i', 0, 'c', 0, 'k', 0
99 #define STRING_MSC_SERIAL \
100 'M', 0, 'a', 0, 'r', 0, 'c', 0, \
101 'h', 0, ' ', 0, '2', 0, '0', 0, \
104 /* make the real string descriptors */
106 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
107 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data);
108 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config);
109 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor);
110 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product);
111 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial);
115 static usb_temp_get_string_desc_t msc_get_string_desc;
117 static const struct usb_temp_packet_size bulk_mps = {
118 .mps[USB_SPEED_FULL] = 64,
119 .mps[USB_SPEED_HIGH] = 512,
122 static const struct usb_temp_endpoint_desc bulk_in_ep = {
123 .pPacketSize = &bulk_mps,
124 #ifdef USB_HIP_IN_EP_0
125 .bEndpointAddress = USB_HIP_IN_EP_0,
127 .bEndpointAddress = UE_DIR_IN,
129 .bmAttributes = UE_BULK,
132 static const struct usb_temp_endpoint_desc bulk_out_ep = {
133 .pPacketSize = &bulk_mps,
134 #ifdef USB_HIP_OUT_EP_0
135 .bEndpointAddress = USB_HIP_OUT_EP_0,
137 .bEndpointAddress = UE_DIR_OUT,
139 .bmAttributes = UE_BULK,
142 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
148 static const struct usb_temp_interface_desc msc_data_interface = {
149 .ppEndpoints = msc_data_endpoints,
150 .bInterfaceClass = UICLASS_MASS,
151 .bInterfaceSubClass = UISUBCLASS_SCSI,
152 .bInterfaceProtocol = UIPROTO_MASS_BBB,
153 .iInterface = STRING_MSC_DATA_INDEX,
156 static const struct usb_temp_interface_desc *msc_interfaces[] = {
161 static const struct usb_temp_config_desc msc_config_desc = {
162 .ppIfaceDesc = msc_interfaces,
163 .bmAttributes = UC_BUS_POWERED,
164 .bMaxPower = 25, /* 50 mA */
165 .iConfiguration = STRING_MSC_CONFIG_INDEX,
168 static const struct usb_temp_config_desc *msc_configs[] = {
173 const struct usb_temp_device_desc usb_template_msc = {
174 .getStringDesc = &msc_get_string_desc,
175 .ppConfigDesc = msc_configs,
176 .idVendor = USB_TEMPLATE_VENDOR,
179 .bDeviceClass = UDCLASS_COMM,
180 .bDeviceSubClass = 0,
181 .bDeviceProtocol = 0,
182 .iManufacturer = STRING_MSC_VENDOR_INDEX,
183 .iProduct = STRING_MSC_PRODUCT_INDEX,
184 .iSerialNumber = STRING_MSC_SERIAL_INDEX,
187 /*------------------------------------------------------------------------*
188 * msc_get_string_desc
191 * NULL: Failure. No such string.
192 * Else: Success. Pointer to string descriptor is returned.
193 *------------------------------------------------------------------------*/
195 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
197 static const void *ptr[STRING_MSC_MAX] = {
198 [STRING_LANG_INDEX] = &string_lang,
199 [STRING_MSC_DATA_INDEX] = &string_msc_data,
200 [STRING_MSC_CONFIG_INDEX] = &string_msc_config,
201 [STRING_MSC_VENDOR_INDEX] = &string_msc_vendor,
202 [STRING_MSC_PRODUCT_INDEX] = &string_msc_product,
203 [STRING_MSC_SERIAL_INDEX] = &string_msc_serial,
206 if (string_index == 0) {
207 return (&string_lang);
209 if (lang_id != 0x0409) {
212 if (string_index < STRING_MSC_MAX) {
213 return (ptr[string_index]);