xref: /freebsd/sys/dev/usb/template/usb_template_msc.c (revision d2b99310b17979e99c03251de3f9bc08dd9219d1)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * This file contains the USB templates for an USB Mass Storage Device.
30  */
31 
32 #ifdef USB_GLOBAL_INCLUDE_FILE
33 #include USB_GLOBAL_INCLUDE_FILE
34 #else
35 #include <sys/stdint.h>
36 #include <sys/stddef.h>
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/bus.h>
43 #include <sys/module.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/condvar.h>
47 #include <sys/sysctl.h>
48 #include <sys/sx.h>
49 #include <sys/unistd.h>
50 #include <sys/callout.h>
51 #include <sys/malloc.h>
52 #include <sys/priv.h>
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 
57 #include <dev/usb/template/usb_template.h>
58 #endif			/* USB_GLOBAL_INCLUDE_FILE */
59 
60 enum {
61 	STRING_LANG_INDEX,
62 	STRING_MSC_DATA_INDEX,
63 	STRING_MSC_CONFIG_INDEX,
64 	STRING_MSC_VENDOR_INDEX,
65 	STRING_MSC_PRODUCT_INDEX,
66 	STRING_MSC_SERIAL_INDEX,
67 	STRING_MSC_MAX,
68 };
69 
70 #define	STRING_LANG \
71   0x09, 0x04,				/* American English */
72 
73 #define	STRING_MSC_DATA	\
74   'U', 0, 'S', 0, 'B', 0, ' ', 0, \
75   'M', 0, 'a', 0, 's', 0, 's', 0, \
76   ' ', 0, 'S', 0, 't', 0, 'o', 0, \
77   'r', 0, 'a', 0, 'g', 0, 'e', 0, \
78   ' ', 0, 'I', 0, 'n', 0, 't', 0, \
79   'e', 0, 'r', 0, 'f', 0, 'a', 0, \
80   'c', 0, 'e', 0,
81 
82 #define	STRING_MSC_CONFIG \
83   'D', 0, 'e', 0, 'f', 0, 'a', 0, \
84   'u', 0, 'l', 0, 't', 0, ' ', 0, \
85   'c', 0, 'o', 0, 'n', 0, 'f', 0, \
86   'i', 0, 'g', 0,
87 
88 #define	STRING_MSC_VENDOR \
89   'F', 0, 'r', 0, 'e', 0, 'e', 0, \
90   'B', 0, 'S', 0, 'D', 0, ' ', 0, \
91   'f', 0, 'o', 0, 'u', 0, 'n', 0, \
92   'd', 0, 'a', 0, 't', 0, 'i', 0, \
93   'o', 0, 'n', 0,
94 
95 #define	STRING_MSC_PRODUCT \
96   'U', 0, 'S', 0, 'B', 0, ' ', 0, \
97   'M', 0, 'e', 0, 'm', 0, 'o', 0, \
98   'r', 0, 'y', 0, ' ', 0, 'S', 0, \
99   't', 0, 'i', 0, 'c', 0, 'k', 0
100 
101 #define	STRING_MSC_SERIAL \
102   'M', 0, 'a', 0, 'r', 0, 'c', 0, \
103   'h', 0, ' ', 0, '2', 0, '0', 0, \
104   '0', 0, '8', 0,
105 
106 /* make the real string descriptors */
107 
108 USB_MAKE_STRING_DESC(STRING_LANG, string_lang);
109 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data);
110 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config);
111 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor);
112 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product);
113 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial);
114 
115 /* prototypes */
116 
117 static usb_temp_get_string_desc_t msc_get_string_desc;
118 
119 static const struct usb_temp_packet_size bulk_mps = {
120 	.mps[USB_SPEED_FULL] = 64,
121 	.mps[USB_SPEED_HIGH] = 512,
122 };
123 
124 static const struct usb_temp_endpoint_desc bulk_in_ep = {
125 	.pPacketSize = &bulk_mps,
126 #ifdef USB_HIP_IN_EP_0
127 	.bEndpointAddress = USB_HIP_IN_EP_0,
128 #else
129 	.bEndpointAddress = UE_DIR_IN,
130 #endif
131 	.bmAttributes = UE_BULK,
132 };
133 
134 static const struct usb_temp_endpoint_desc bulk_out_ep = {
135 	.pPacketSize = &bulk_mps,
136 #ifdef USB_HIP_OUT_EP_0
137 	.bEndpointAddress = USB_HIP_OUT_EP_0,
138 #else
139 	.bEndpointAddress = UE_DIR_OUT,
140 #endif
141 	.bmAttributes = UE_BULK,
142 };
143 
144 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
145 	&bulk_in_ep,
146 	&bulk_out_ep,
147 	NULL,
148 };
149 
150 static const struct usb_temp_interface_desc msc_data_interface = {
151 	.ppEndpoints = msc_data_endpoints,
152 	.bInterfaceClass = UICLASS_MASS,
153 	.bInterfaceSubClass = UISUBCLASS_SCSI,
154 	.bInterfaceProtocol = UIPROTO_MASS_BBB,
155 	.iInterface = STRING_MSC_DATA_INDEX,
156 };
157 
158 static const struct usb_temp_interface_desc *msc_interfaces[] = {
159 	&msc_data_interface,
160 	NULL,
161 };
162 
163 static const struct usb_temp_config_desc msc_config_desc = {
164 	.ppIfaceDesc = msc_interfaces,
165 	.bmAttributes = UC_BUS_POWERED,
166 	.bMaxPower = 25,		/* 50 mA */
167 	.iConfiguration = STRING_MSC_CONFIG_INDEX,
168 };
169 
170 static const struct usb_temp_config_desc *msc_configs[] = {
171 	&msc_config_desc,
172 	NULL,
173 };
174 
175 const struct usb_temp_device_desc usb_template_msc = {
176 	.getStringDesc = &msc_get_string_desc,
177 	.ppConfigDesc = msc_configs,
178 	.idVendor = USB_TEMPLATE_VENDOR,
179 	.idProduct = 0x0012,
180 	.bcdDevice = 0x0100,
181 	.bDeviceClass = UDCLASS_COMM,
182 	.bDeviceSubClass = 0,
183 	.bDeviceProtocol = 0,
184 	.iManufacturer = STRING_MSC_VENDOR_INDEX,
185 	.iProduct = STRING_MSC_PRODUCT_INDEX,
186 	.iSerialNumber = STRING_MSC_SERIAL_INDEX,
187 };
188 
189 /*------------------------------------------------------------------------*
190  *	msc_get_string_desc
191  *
192  * Return values:
193  * NULL: Failure. No such string.
194  * Else: Success. Pointer to string descriptor is returned.
195  *------------------------------------------------------------------------*/
196 static const void *
197 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
198 {
199 	static const void *ptr[STRING_MSC_MAX] = {
200 		[STRING_LANG_INDEX] = &string_lang,
201 		[STRING_MSC_DATA_INDEX] = &string_msc_data,
202 		[STRING_MSC_CONFIG_INDEX] = &string_msc_config,
203 		[STRING_MSC_VENDOR_INDEX] = &string_msc_vendor,
204 		[STRING_MSC_PRODUCT_INDEX] = &string_msc_product,
205 		[STRING_MSC_SERIAL_INDEX] = &string_msc_serial,
206 	};
207 
208 	if (string_index == 0) {
209 		return (&string_lang);
210 	}
211 	if (lang_id != 0x0409) {
212 		return (NULL);
213 	}
214 	if (string_index < STRING_MSC_MAX) {
215 		return (ptr[string_index]);
216 	}
217 	return (NULL);
218 }
219