1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * serial.c -- USB gadget serial driver 4 * 5 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) 6 * Copyright (C) 2008 by David Brownell 7 * Copyright (C) 2008 by Nokia Corporation 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/tty.h> 14 #include <linux/tty_flip.h> 15 16 #include "u_serial.h" 17 18 19 /* Defines */ 20 21 #define GS_VERSION_STR "v2.4" 22 #define GS_VERSION_NUM 0x2400 23 24 #define GS_LONG_NAME "Gadget Serial" 25 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR 26 27 /*-------------------------------------------------------------------------*/ 28 USB_GADGET_COMPOSITE_OPTIONS(); 29 30 /* Thanks to NetChip Technologies for donating this product ID. 31 * 32 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 33 * Instead: allocate your own, using normal USB-IF procedures. 34 */ 35 #define GS_VENDOR_ID 0x0525 /* NetChip */ 36 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ 37 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ 38 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */ 39 40 /* string IDs are assigned dynamically */ 41 42 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX 43 44 static struct usb_string strings_dev[] = { 45 [USB_GADGET_MANUFACTURER_IDX].s = "", 46 [USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME, 47 [USB_GADGET_SERIAL_IDX].s = "", 48 [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, 49 { } /* end of list */ 50 }; 51 52 static struct usb_gadget_strings stringtab_dev = { 53 .language = 0x0409, /* en-us */ 54 .strings = strings_dev, 55 }; 56 57 static struct usb_gadget_strings *dev_strings[] = { 58 &stringtab_dev, 59 NULL, 60 }; 61 62 static struct usb_device_descriptor device_desc = { 63 .bLength = USB_DT_DEVICE_SIZE, 64 .bDescriptorType = USB_DT_DEVICE, 65 /* .bcdUSB = DYNAMIC */ 66 /* .bDeviceClass = f(use_acm) */ 67 .bDeviceSubClass = 0, 68 .bDeviceProtocol = 0, 69 /* .bMaxPacketSize0 = f(hardware) */ 70 .idVendor = cpu_to_le16(GS_VENDOR_ID), 71 /* .idProduct = f(use_acm) */ 72 .bcdDevice = cpu_to_le16(GS_VERSION_NUM), 73 /* .iManufacturer = DYNAMIC */ 74 /* .iProduct = DYNAMIC */ 75 .bNumConfigurations = 1, 76 }; 77 78 static const struct usb_descriptor_header *otg_desc[2]; 79 80 /*-------------------------------------------------------------------------*/ 81 82 /* Module */ 83 MODULE_DESCRIPTION(GS_VERSION_NAME); 84 MODULE_AUTHOR("Al Borchers"); 85 MODULE_AUTHOR("David Brownell"); 86 MODULE_LICENSE("GPL"); 87 88 static bool use_acm = true; 89 module_param(use_acm, bool, 0); 90 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); 91 92 static bool use_obex = false; 93 module_param(use_obex, bool, 0); 94 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no"); 95 96 static unsigned n_ports = 1; 97 module_param(n_ports, uint, 0); 98 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1"); 99 100 /*-------------------------------------------------------------------------*/ 101 102 static struct usb_configuration serial_config_driver = { 103 /* .label = f(use_acm) */ 104 /* .bConfigurationValue = f(use_acm) */ 105 /* .iConfiguration = DYNAMIC */ 106 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 107 }; 108 109 static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS]; 110 static struct usb_function *f_serial[MAX_U_SERIAL_PORTS]; 111 112 static int serial_register_ports(struct usb_composite_dev *cdev, 113 struct usb_configuration *c, const char *f_name) 114 { 115 int i; 116 int ret; 117 118 ret = usb_add_config_only(cdev, c); 119 if (ret) 120 goto out; 121 122 for (i = 0; i < n_ports; i++) { 123 124 fi_serial[i] = usb_get_function_instance(f_name); 125 if (IS_ERR(fi_serial[i])) { 126 ret = PTR_ERR(fi_serial[i]); 127 goto fail; 128 } 129 130 f_serial[i] = usb_get_function(fi_serial[i]); 131 if (IS_ERR(f_serial[i])) { 132 ret = PTR_ERR(f_serial[i]); 133 goto err_get_func; 134 } 135 136 ret = usb_add_function(c, f_serial[i]); 137 if (ret) 138 goto err_add_func; 139 } 140 141 return 0; 142 143 err_add_func: 144 usb_put_function(f_serial[i]); 145 err_get_func: 146 usb_put_function_instance(fi_serial[i]); 147 148 fail: 149 i--; 150 while (i >= 0) { 151 usb_remove_function(c, f_serial[i]); 152 usb_put_function(f_serial[i]); 153 usb_put_function_instance(fi_serial[i]); 154 i--; 155 } 156 out: 157 return ret; 158 } 159 160 static int gs_bind(struct usb_composite_dev *cdev) 161 { 162 int status; 163 164 /* Allocate string descriptor numbers ... note that string 165 * contents can be overridden by the composite_dev glue. 166 */ 167 168 status = usb_string_ids_tab(cdev, strings_dev); 169 if (status < 0) 170 goto fail; 171 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; 172 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; 173 status = strings_dev[STRING_DESCRIPTION_IDX].id; 174 serial_config_driver.iConfiguration = status; 175 176 if (gadget_is_otg(cdev->gadget)) { 177 if (!otg_desc[0]) { 178 struct usb_descriptor_header *usb_desc; 179 180 usb_desc = usb_otg_descriptor_alloc(cdev->gadget); 181 if (!usb_desc) { 182 status = -ENOMEM; 183 goto fail; 184 } 185 usb_otg_descriptor_init(cdev->gadget, usb_desc); 186 otg_desc[0] = usb_desc; 187 otg_desc[1] = NULL; 188 } 189 serial_config_driver.descriptors = otg_desc; 190 serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 191 } 192 193 /* register our configuration */ 194 if (use_acm) { 195 status = serial_register_ports(cdev, &serial_config_driver, 196 "acm"); 197 usb_ep_autoconfig_reset(cdev->gadget); 198 } else if (use_obex) 199 status = serial_register_ports(cdev, &serial_config_driver, 200 "obex"); 201 else { 202 status = serial_register_ports(cdev, &serial_config_driver, 203 "gser"); 204 } 205 if (status < 0) 206 goto fail1; 207 208 usb_composite_overwrite_options(cdev, &coverwrite); 209 INFO(cdev, "%s\n", GS_VERSION_NAME); 210 211 return 0; 212 fail1: 213 kfree(otg_desc[0]); 214 otg_desc[0] = NULL; 215 fail: 216 return status; 217 } 218 219 static int gs_unbind(struct usb_composite_dev *cdev) 220 { 221 int i; 222 223 for (i = 0; i < n_ports; i++) { 224 usb_put_function(f_serial[i]); 225 usb_put_function_instance(fi_serial[i]); 226 } 227 228 kfree(otg_desc[0]); 229 otg_desc[0] = NULL; 230 231 return 0; 232 } 233 234 static struct usb_composite_driver gserial_driver = { 235 .name = "g_serial", 236 .dev = &device_desc, 237 .strings = dev_strings, 238 .max_speed = USB_SPEED_SUPER, 239 .bind = gs_bind, 240 .unbind = gs_unbind, 241 }; 242 243 static int __init init(void) 244 { 245 /* We *could* export two configs; that'd be much cleaner... 246 * but neither of these product IDs was defined that way. 247 */ 248 if (use_acm) { 249 serial_config_driver.label = "CDC ACM config"; 250 serial_config_driver.bConfigurationValue = 2; 251 device_desc.bDeviceClass = USB_CLASS_COMM; 252 device_desc.idProduct = 253 cpu_to_le16(GS_CDC_PRODUCT_ID); 254 } else if (use_obex) { 255 serial_config_driver.label = "CDC OBEX config"; 256 serial_config_driver.bConfigurationValue = 3; 257 device_desc.bDeviceClass = USB_CLASS_COMM; 258 device_desc.idProduct = 259 cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID); 260 } else { 261 serial_config_driver.label = "Generic Serial config"; 262 serial_config_driver.bConfigurationValue = 1; 263 device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; 264 device_desc.idProduct = 265 cpu_to_le16(GS_PRODUCT_ID); 266 } 267 strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; 268 269 return usb_composite_probe(&gserial_driver); 270 } 271 module_init(init); 272 273 static void __exit cleanup(void) 274 { 275 usb_composite_unregister(&gserial_driver); 276 } 277 module_exit(cleanup); 278