1 // SPDX-License-Identifier: GPL-2.0
2 /* Target based USB-Gadget
3  *
4  * UAS protocol handling, target callbacks, configfs handling,
5  * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
6  *
7  * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
8  * License: GPLv2 as published by FSF.
9  */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/configfs.h>
15 #include <linux/ctype.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/composite.h>
18 #include <linux/usb/gadget.h>
19 #include <linux/usb/storage.h>
20 #include <scsi/scsi_tcq.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23 #include <asm/unaligned.h>
24 
25 #include "u_tcm.h"
26 
27 USB_GADGET_COMPOSITE_OPTIONS();
28 
29 #define UAS_VENDOR_ID	0x0525	/* NetChip */
30 #define UAS_PRODUCT_ID	0xa4a5	/* Linux-USB File-backed Storage Gadget */
31 
32 static struct usb_device_descriptor usbg_device_desc = {
33 	.bLength =		sizeof(usbg_device_desc),
34 	.bDescriptorType =	USB_DT_DEVICE,
35 	/* .bcdUSB = DYNAMIC */
36 	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
37 	.idVendor =		cpu_to_le16(UAS_VENDOR_ID),
38 	.idProduct =		cpu_to_le16(UAS_PRODUCT_ID),
39 	.bNumConfigurations =   1,
40 };
41 
42 #define USB_G_STR_CONFIG USB_GADGET_FIRST_AVAIL_IDX
43 
44 static struct usb_string	usbg_us_strings[] = {
45 	[USB_GADGET_MANUFACTURER_IDX].s	= "Target Manufactor",
46 	[USB_GADGET_PRODUCT_IDX].s	= "Target Product",
47 	[USB_GADGET_SERIAL_IDX].s	= "000000000001",
48 	[USB_G_STR_CONFIG].s		= "default config",
49 	{ },
50 };
51 
52 static struct usb_gadget_strings usbg_stringtab = {
53 	.language = 0x0409,
54 	.strings = usbg_us_strings,
55 };
56 
57 static struct usb_gadget_strings *usbg_strings[] = {
58 	&usbg_stringtab,
59 	NULL,
60 };
61 
62 static struct usb_function_instance *fi_tcm;
63 static struct usb_function *f_tcm;
64 
65 static int guas_unbind(struct usb_composite_dev *cdev)
66 {
67 	if (!IS_ERR_OR_NULL(f_tcm))
68 		usb_put_function(f_tcm);
69 
70 	return 0;
71 }
72 
73 static int tcm_do_config(struct usb_configuration *c)
74 {
75 	int status;
76 
77 	f_tcm = usb_get_function(fi_tcm);
78 	if (IS_ERR(f_tcm))
79 		return PTR_ERR(f_tcm);
80 
81 	status = usb_add_function(c, f_tcm);
82 	if (status < 0) {
83 		usb_put_function(f_tcm);
84 		return status;
85 	}
86 
87 	return 0;
88 }
89 
90 static struct usb_configuration usbg_config_driver = {
91 	.label                  = "Linux Target",
92 	.bConfigurationValue    = 1,
93 	.bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
94 };
95 
96 static int usbg_attach(struct usb_function_instance *f);
97 static void usbg_detach(struct usb_function_instance *f);
98 
99 static int usb_target_bind(struct usb_composite_dev *cdev)
100 {
101 	int ret;
102 
103 	ret = usb_string_ids_tab(cdev, usbg_us_strings);
104 	if (ret)
105 		return ret;
106 
107 	usbg_device_desc.iManufacturer =
108 		usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
109 	usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
110 	usbg_device_desc.iSerialNumber =
111 		usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
112 	usbg_config_driver.iConfiguration =
113 		usbg_us_strings[USB_G_STR_CONFIG].id;
114 
115 	ret = usb_add_config(cdev, &usbg_config_driver, tcm_do_config);
116 	if (ret)
117 		return ret;
118 	usb_composite_overwrite_options(cdev, &coverwrite);
119 	return 0;
120 }
121 
122 static struct usb_composite_driver usbg_driver = {
123 	.name           = "g_target",
124 	.dev            = &usbg_device_desc,
125 	.strings        = usbg_strings,
126 	.max_speed      = USB_SPEED_SUPER,
127 	.bind		= usb_target_bind,
128 	.unbind         = guas_unbind,
129 };
130 
131 static int usbg_attach(struct usb_function_instance *f)
132 {
133 	return usb_composite_probe(&usbg_driver);
134 }
135 
136 static void usbg_detach(struct usb_function_instance *f)
137 {
138 	usb_composite_unregister(&usbg_driver);
139 }
140 
141 static int __init usb_target_gadget_init(void)
142 {
143 	struct f_tcm_opts *tcm_opts;
144 
145 	fi_tcm = usb_get_function_instance("tcm");
146 	if (IS_ERR(fi_tcm))
147 		return PTR_ERR(fi_tcm);
148 
149 	tcm_opts = container_of(fi_tcm, struct f_tcm_opts, func_inst);
150 	mutex_lock(&tcm_opts->dep_lock);
151 	tcm_opts->tcm_register_callback = usbg_attach;
152 	tcm_opts->tcm_unregister_callback = usbg_detach;
153 	tcm_opts->dependent = THIS_MODULE;
154 	tcm_opts->can_attach = true;
155 	tcm_opts->has_dep = true;
156 	mutex_unlock(&tcm_opts->dep_lock);
157 
158 	fi_tcm->set_inst_name(fi_tcm, "tcm-legacy");
159 
160 	return 0;
161 }
162 module_init(usb_target_gadget_init);
163 
164 static void __exit usb_target_gadget_exit(void)
165 {
166 	if (!IS_ERR_OR_NULL(fi_tcm))
167 		usb_put_function_instance(fi_tcm);
168 
169 }
170 module_exit(usb_target_gadget_exit);
171 
172 MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
173 MODULE_DESCRIPTION("usb-gadget fabric");
174 MODULE_LICENSE("GPL v2");
175