1246122Shselasky/* $FreeBSD$ */
2184610Salfred/*-
3189002Sed * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
4184610Salfred * All rights reserved.
5184610Salfred *
6184610Salfred * Redistribution and use in source and binary forms, with or without
7184610Salfred * modification, are permitted provided that the following conditions
8184610Salfred * are met:
9184610Salfred * 1. Redistributions of source code must retain the above copyright
10184610Salfred *    notice, this list of conditions and the following disclaimer.
11184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer in the
13184610Salfred *    documentation and/or other materials provided with the distribution.
14184610Salfred *
15184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25184610Salfred * SUCH DAMAGE.
26184610Salfred */
27184610Salfred
28184610Salfred/*
29184610Salfred * This file contains the USB templates for an USB Mass Storage Device.
30184610Salfred */
31184610Salfred
32246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE
33246122Shselasky#include USB_GLOBAL_INCLUDE_FILE
34246122Shselasky#else
35194677Sthompsa#include <sys/stdint.h>
36194677Sthompsa#include <sys/stddef.h>
37194677Sthompsa#include <sys/param.h>
38194677Sthompsa#include <sys/queue.h>
39194677Sthompsa#include <sys/types.h>
40194677Sthompsa#include <sys/systm.h>
41194677Sthompsa#include <sys/kernel.h>
42194677Sthompsa#include <sys/bus.h>
43194677Sthompsa#include <sys/module.h>
44194677Sthompsa#include <sys/lock.h>
45194677Sthompsa#include <sys/mutex.h>
46194677Sthompsa#include <sys/condvar.h>
47194677Sthompsa#include <sys/sysctl.h>
48194677Sthompsa#include <sys/sx.h>
49194677Sthompsa#include <sys/unistd.h>
50194677Sthompsa#include <sys/callout.h>
51194677Sthompsa#include <sys/malloc.h>
52194677Sthompsa#include <sys/priv.h>
53194677Sthompsa
54188942Sthompsa#include <dev/usb/usb.h>
55194677Sthompsa#include <dev/usb/usbdi.h>
56246123Shselasky#include <dev/usb/usb_core.h>
57184610Salfred
58188942Sthompsa#include <dev/usb/template/usb_template.h>
59246122Shselasky#endif			/* USB_GLOBAL_INCLUDE_FILE */
60184610Salfred
61184610Salfredenum {
62184610Salfred	STRING_LANG_INDEX,
63184610Salfred	STRING_MSC_DATA_INDEX,
64184610Salfred	STRING_MSC_CONFIG_INDEX,
65184610Salfred	STRING_MSC_VENDOR_INDEX,
66184610Salfred	STRING_MSC_PRODUCT_INDEX,
67184610Salfred	STRING_MSC_SERIAL_INDEX,
68184610Salfred	STRING_MSC_MAX,
69184610Salfred};
70184610Salfred
71184610Salfred#define	STRING_MSC_DATA	\
72246125Shselasky  "U\0S\0B\0 \0M\0a\0s\0s\0 \0S\0t\0o\0r\0a\0g\0e\0 " \
73246125Shselasky  "\0I\0n\0t\0e\0r\0f\0a\0c\0e"
74184610Salfred
75184610Salfred#define	STRING_MSC_CONFIG \
76246125Shselasky  "D\0e\0f\0a\0u\0l\0t\0 \0c\0o\0n\0f\0i\0g"
77184610Salfred
78184610Salfred#define	STRING_MSC_VENDOR \
79246125Shselasky  "F\0r\0e\0e\0B\0S\0D\0 \0f\0o\0u\0n\0d\0a\0t\0i\0o\0n"
80184610Salfred
81184610Salfred#define	STRING_MSC_PRODUCT \
82246125Shselasky  "U\0S\0B\0 \0M\0e\0m\0o\0r\0y\0 \0S\0t\0i\0c\0k"
83184610Salfred
84184610Salfred#define	STRING_MSC_SERIAL \
85246125Shselasky  "M\0a\0r\0c\0h\0 \0002\0000\0000\08"
86184610Salfred
87184610Salfred/* make the real string descriptors */
88184610Salfred
89184610SalfredUSB_MAKE_STRING_DESC(STRING_MSC_DATA, string_msc_data);
90184610SalfredUSB_MAKE_STRING_DESC(STRING_MSC_CONFIG, string_msc_config);
91184610SalfredUSB_MAKE_STRING_DESC(STRING_MSC_VENDOR, string_msc_vendor);
92184610SalfredUSB_MAKE_STRING_DESC(STRING_MSC_PRODUCT, string_msc_product);
93184610SalfredUSB_MAKE_STRING_DESC(STRING_MSC_SERIAL, string_msc_serial);
94184610Salfred
95184610Salfred/* prototypes */
96184610Salfred
97194228Sthompsastatic usb_temp_get_string_desc_t msc_get_string_desc;
98184610Salfred
99192984Sthompsastatic const struct usb_temp_packet_size bulk_mps = {
100184610Salfred	.mps[USB_SPEED_FULL] = 64,
101184610Salfred	.mps[USB_SPEED_HIGH] = 512,
102184610Salfred};
103184610Salfred
104192984Sthompsastatic const struct usb_temp_endpoint_desc bulk_in_ep = {
105184610Salfred	.pPacketSize = &bulk_mps,
106184610Salfred#ifdef USB_HIP_IN_EP_0
107184610Salfred	.bEndpointAddress = USB_HIP_IN_EP_0,
108184610Salfred#else
109184610Salfred	.bEndpointAddress = UE_DIR_IN,
110184610Salfred#endif
111184610Salfred	.bmAttributes = UE_BULK,
112184610Salfred};
113184610Salfred
114192984Sthompsastatic const struct usb_temp_endpoint_desc bulk_out_ep = {
115184610Salfred	.pPacketSize = &bulk_mps,
116184610Salfred#ifdef USB_HIP_OUT_EP_0
117184610Salfred	.bEndpointAddress = USB_HIP_OUT_EP_0,
118184610Salfred#else
119184610Salfred	.bEndpointAddress = UE_DIR_OUT,
120184610Salfred#endif
121184610Salfred	.bmAttributes = UE_BULK,
122184610Salfred};
123184610Salfred
124192984Sthompsastatic const struct usb_temp_endpoint_desc *msc_data_endpoints[] = {
125184610Salfred	&bulk_in_ep,
126184610Salfred	&bulk_out_ep,
127184610Salfred	NULL,
128184610Salfred};
129184610Salfred
130192984Sthompsastatic const struct usb_temp_interface_desc msc_data_interface = {
131184610Salfred	.ppEndpoints = msc_data_endpoints,
132184610Salfred	.bInterfaceClass = UICLASS_MASS,
133184610Salfred	.bInterfaceSubClass = UISUBCLASS_SCSI,
134184610Salfred	.bInterfaceProtocol = UIPROTO_MASS_BBB,
135184610Salfred	.iInterface = STRING_MSC_DATA_INDEX,
136184610Salfred};
137184610Salfred
138192984Sthompsastatic const struct usb_temp_interface_desc *msc_interfaces[] = {
139184610Salfred	&msc_data_interface,
140184610Salfred	NULL,
141184610Salfred};
142184610Salfred
143192984Sthompsastatic const struct usb_temp_config_desc msc_config_desc = {
144184610Salfred	.ppIfaceDesc = msc_interfaces,
145184610Salfred	.bmAttributes = UC_BUS_POWERED,
146184610Salfred	.bMaxPower = 25,		/* 50 mA */
147184610Salfred	.iConfiguration = STRING_MSC_CONFIG_INDEX,
148184610Salfred};
149184610Salfred
150192984Sthompsastatic const struct usb_temp_config_desc *msc_configs[] = {
151184610Salfred	&msc_config_desc,
152184610Salfred	NULL,
153184610Salfred};
154184610Salfred
155194228Sthompsaconst struct usb_temp_device_desc usb_template_msc = {
156184610Salfred	.getStringDesc = &msc_get_string_desc,
157184610Salfred	.ppConfigDesc = msc_configs,
158223467Shselasky	.idVendor = USB_TEMPLATE_VENDOR,
159223467Shselasky	.idProduct = 0x0012,
160184610Salfred	.bcdDevice = 0x0100,
161184610Salfred	.bDeviceClass = UDCLASS_COMM,
162184610Salfred	.bDeviceSubClass = 0,
163184610Salfred	.bDeviceProtocol = 0,
164184610Salfred	.iManufacturer = STRING_MSC_VENDOR_INDEX,
165184610Salfred	.iProduct = STRING_MSC_PRODUCT_INDEX,
166184610Salfred	.iSerialNumber = STRING_MSC_SERIAL_INDEX,
167184610Salfred};
168184610Salfred
169184610Salfred/*------------------------------------------------------------------------*
170184610Salfred *	msc_get_string_desc
171184610Salfred *
172184610Salfred * Return values:
173184610Salfred * NULL: Failure. No such string.
174184610Salfred * Else: Success. Pointer to string descriptor is returned.
175184610Salfred *------------------------------------------------------------------------*/
176184610Salfredstatic const void *
177184610Salfredmsc_get_string_desc(uint16_t lang_id, uint8_t string_index)
178184610Salfred{
179184610Salfred	static const void *ptr[STRING_MSC_MAX] = {
180246123Shselasky		[STRING_LANG_INDEX] = &usb_string_lang_en,
181184610Salfred		[STRING_MSC_DATA_INDEX] = &string_msc_data,
182184610Salfred		[STRING_MSC_CONFIG_INDEX] = &string_msc_config,
183184610Salfred		[STRING_MSC_VENDOR_INDEX] = &string_msc_vendor,
184184610Salfred		[STRING_MSC_PRODUCT_INDEX] = &string_msc_product,
185184610Salfred		[STRING_MSC_SERIAL_INDEX] = &string_msc_serial,
186184610Salfred	};
187184610Salfred
188184610Salfred	if (string_index == 0) {
189246123Shselasky		return (&usb_string_lang_en);
190184610Salfred	}
191184610Salfred	if (lang_id != 0x0409) {
192184610Salfred		return (NULL);
193184610Salfred	}
194184610Salfred	if (string_index < STRING_MSC_MAX) {
195184610Salfred		return (ptr[string_index]);
196184610Salfred	}
197184610Salfred	return (NULL);
198184610Salfred}
199