1 /* $FreeBSD: head/sys/dev/usb/template/usb_template_msc.c 246125 2013-01-30 16:05:54Z hselasky $ */
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/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/caps.h>
50 
51 #include <bus/u4b/usb.h>
52 #include <bus/u4b/usbdi.h>
53 #include <bus/u4b/usb_core.h>
54 
55 #include <bus/u4b/template/usb_template.h>
56 #endif			/* USB_GLOBAL_INCLUDE_FILE */
57 
58 enum {
59 	STRING_LANG_INDEX,
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,
65 	STRING_MSC_MAX,
66 };
67 
68 #define	STRING_MSC_DATA	\
69   "U\0S\0B\0 \0M\0a\0s\0s\0 \0S\0t\0o\0r\0a\0g\0e\0 " \
70   "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
71 
72 #define	STRING_MSC_CONFIG \
73   "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
74 
75 #define	STRING_MSC_VENDOR \
76   "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
77 
78 #define	STRING_MSC_PRODUCT \
79   "U\0S\0B\0 \0M\0e\0m\0o\0r\0y\0 \0S\0t\0i\0c\0k"
80 
81 #define	STRING_MSC_SERIAL \
82   "M\0a\0r\0c\0h\0 \0002\0000\0000\08"
83 
84 /* make the real string descriptors */
85 
86 USB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data);
87 USB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config);
88 USB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor);
89 USB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product);
90 USB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial);
91 
92 /* prototypes */
93 
94 static usb_temp_get_string_desc_t msc_get_string_desc;
95 
96 static const struct usb_temp_packet_size bulk_mps = {
97 	.mps[USB_SPEED_FULL] = 64,
98 	.mps[USB_SPEED_HIGH] = 512,
99 };
100 
101 static const struct usb_temp_endpoint_desc bulk_in_ep = {
102 	.pPacketSize = &bulk_mps,
103 #ifdef USB_HIP_IN_EP_0
104 	.bEndpointAddress = USB_HIP_IN_EP_0,
105 #else
106 	.bEndpointAddress = UE_DIR_IN,
107 #endif
108 	.bmAttributes = UE_BULK,
109 };
110 
111 static const struct usb_temp_endpoint_desc bulk_out_ep = {
112 	.pPacketSize = &bulk_mps,
113 #ifdef USB_HIP_OUT_EP_0
114 	.bEndpointAddress = USB_HIP_OUT_EP_0,
115 #else
116 	.bEndpointAddress = UE_DIR_OUT,
117 #endif
118 	.bmAttributes = UE_BULK,
119 };
120 
121 static const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
122 	&bulk_in_ep,
123 	&bulk_out_ep,
124 	NULL,
125 };
126 
127 static const struct usb_temp_interface_desc msc_data_interface = {
128 	.ppEndpoints = msc_data_endpoints,
129 	.bInterfaceClass = UICLASS_MASS,
130 	.bInterfaceSubClass = UISUBCLASS_SCSI,
131 	.bInterfaceProtocol = UIPROTO_MASS_BBB,
132 	.iInterface = STRING_MSC_DATA_INDEX,
133 };
134 
135 static const struct usb_temp_interface_desc *msc_interfaces[] = {
136 	&msc_data_interface,
137 	NULL,
138 };
139 
140 static const struct usb_temp_config_desc msc_config_desc = {
141 	.ppIfaceDesc = msc_interfaces,
142 	.bmAttributes = UC_BUS_POWERED,
143 	.bMaxPower = 25,		/* 50 mA */
144 	.iConfiguration = STRING_MSC_CONFIG_INDEX,
145 };
146 
147 static const struct usb_temp_config_desc *msc_configs[] = {
148 	&msc_config_desc,
149 	NULL,
150 };
151 
152 const struct usb_temp_device_desc usb_template_msc = {
153 	.getStringDesc = &msc_get_string_desc,
154 	.ppConfigDesc = msc_configs,
155 	.idVendor = USB_TEMPLATE_VENDOR,
156 	.idProduct = 0x0012,
157 	.bcdDevice = 0x0100,
158 	.bDeviceClass = UDCLASS_COMM,
159 	.bDeviceSubClass = 0,
160 	.bDeviceProtocol = 0,
161 	.iManufacturer = STRING_MSC_VENDOR_INDEX,
162 	.iProduct = STRING_MSC_PRODUCT_INDEX,
163 	.iSerialNumber = STRING_MSC_SERIAL_INDEX,
164 };
165 
166 /*------------------------------------------------------------------------*
167  *	msc_get_string_desc
168  *
169  * Return values:
170  * NULL: Failure. No such string.
171  * Else: Success. Pointer to string descriptor is returned.
172  *------------------------------------------------------------------------*/
173 static const void *
msc_get_string_desc(uint16_t lang_id,uint8_t string_index)174 msc_get_string_desc(uint16_t lang_id, uint8_t string_index)
175 {
176 	static const void *ptr[STRING_MSC_MAX] = {
177 		[STRING_LANG_INDEX] = &usb_string_lang_en,
178 		[STRING_MSC_DATA_INDEX] = &string_msc_data,
179 		[STRING_MSC_CONFIG_INDEX] = &string_msc_config,
180 		[STRING_MSC_VENDOR_INDEX] = &string_msc_vendor,
181 		[STRING_MSC_PRODUCT_INDEX] = &string_msc_product,
182 		[STRING_MSC_SERIAL_INDEX] = &string_msc_serial,
183 	};
184 
185 	if (string_index == 0) {
186 		return (&usb_string_lang_en);
187 	}
188 	if (lang_id != 0x0409) {
189 		return (NULL);
190 	}
191 	if (string_index < STRING_MSC_MAX) {
192 		return (ptr[string_index]);
193 	}
194 	return (NULL);
195 }
196