Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 2 | * serial.c -- USB gadget serial driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 4 | * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) |
| 5 | * Copyright (C) 2008 by David Brownell |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 6 | * Copyright (C) 2008 by Nokia Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This software is distributed under the terms of the GNU General |
| 9 | * Public License ("GPL") as published by the Free Software Foundation, |
| 10 | * either version 2 of that License or (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/utsname.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/tty.h> |
| 17 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 19 | #include "u_serial.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "gadget_chips.h" |
| 21 | |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* Defines */ |
| 24 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 25 | #define GS_VERSION_STR "v2.4" |
| 26 | #define GS_VERSION_NUM 0x2400 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #define GS_LONG_NAME "Gadget Serial" |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 29 | #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR |
| 30 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 31 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* Thanks to NetChip Technologies for donating this product ID. |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 34 | * |
| 35 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 36 | * Instead: allocate your own, using normal USB-IF procedures. |
| 37 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define GS_VENDOR_ID 0x0525 /* NetChip */ |
| 39 | #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ |
| 40 | #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ |
| 41 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 42 | /* string IDs are assigned dynamically */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 44 | #define STRING_MANUFACTURER_IDX 0 |
| 45 | #define STRING_PRODUCT_IDX 1 |
| 46 | #define STRING_DESCRIPTION_IDX 2 |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static char manufacturer[50]; |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 49 | |
| 50 | static struct usb_string strings_dev[] = { |
| 51 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
| 52 | [STRING_PRODUCT_IDX].s = GS_VERSION_NAME, |
| 53 | [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { } /* end of list */ |
| 55 | }; |
| 56 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 57 | static struct usb_gadget_strings stringtab_dev = { |
| 58 | .language = 0x0409, /* en-us */ |
| 59 | .strings = strings_dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 62 | static struct usb_gadget_strings *dev_strings[] = { |
| 63 | &stringtab_dev, |
| 64 | NULL, |
| 65 | }; |
| 66 | |
| 67 | static struct usb_device_descriptor device_desc = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .bLength = USB_DT_DEVICE_SIZE, |
| 69 | .bDescriptorType = USB_DT_DEVICE, |
| 70 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 71 | /* .bDeviceClass = f(use_acm) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | .bDeviceSubClass = 0, |
| 73 | .bDeviceProtocol = 0, |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 74 | /* .bMaxPacketSize0 = f(hardware) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 76 | /* .idProduct = f(use_acm) */ |
| 77 | /* .bcdDevice = f(hardware) */ |
| 78 | /* .iManufacturer = DYNAMIC */ |
| 79 | /* .iProduct = DYNAMIC */ |
| 80 | .bNumConfigurations = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 83 | static struct usb_otg_descriptor otg_descriptor = { |
| 84 | .bLength = sizeof otg_descriptor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | .bDescriptorType = USB_DT_OTG, |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 86 | |
| 87 | /* REVISIT SRP-only hardware is possible, although |
| 88 | * it would not be called "OTG" ... |
| 89 | */ |
| 90 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 93 | static const struct usb_descriptor_header *otg_desc[] = { |
| 94 | (struct usb_descriptor_header *) &otg_descriptor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | NULL, |
| 96 | }; |
| 97 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 98 | /*-------------------------------------------------------------------------*/ |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* Module */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 101 | MODULE_DESCRIPTION(GS_VERSION_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | MODULE_AUTHOR("Al Borchers"); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame] | 103 | MODULE_AUTHOR("David Brownell"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | MODULE_LICENSE("GPL"); |
| 105 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 106 | static int use_acm = true; |
| 107 | module_param(use_acm, bool, 0); |
| 108 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes"); |
| 109 | |
| 110 | static unsigned n_ports = 1; |
| 111 | module_param(n_ports, uint, 0); |
| 112 | MODULE_PARM_DESC(n_ports, "number of ports to create, default=1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 114 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 116 | static int __init serial_bind_config(struct usb_configuration *c) |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 117 | { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 118 | unsigned i; |
| 119 | int status = 0; |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 120 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 121 | for (i = 0; i < n_ports && status == 0; i++) { |
| 122 | if (use_acm) |
| 123 | status = acm_bind_config(c, i); |
| 124 | else |
| 125 | status = gser_bind_config(c, i); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 126 | } |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 127 | return status; |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 130 | static struct usb_configuration serial_config_driver = { |
| 131 | /* .label = f(use_acm) */ |
| 132 | .bind = serial_bind_config, |
| 133 | /* .bConfigurationValue = f(use_acm) */ |
| 134 | /* .iConfiguration = DYNAMIC */ |
| 135 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 136 | .bMaxPower = 1, /* 2 mA, minimal */ |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 139 | static int __init gs_bind(struct usb_composite_dev *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 141 | int gcnum; |
| 142 | struct usb_gadget *gadget = cdev->gadget; |
| 143 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 145 | status = gserial_setup(cdev->gadget, n_ports); |
| 146 | if (status < 0) |
| 147 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 149 | /* Allocate string descriptor numbers ... note that string |
| 150 | * contents can be overridden by the composite_dev glue. |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 151 | */ |
| 152 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 153 | /* device description: manufacturer, product */ |
| 154 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
| 155 | init_utsname()->sysname, init_utsname()->release, |
| 156 | gadget->name); |
| 157 | status = usb_string_id(cdev); |
| 158 | if (status < 0) |
| 159 | goto fail; |
| 160 | strings_dev[STRING_MANUFACTURER_IDX].id = status; |
| 161 | |
| 162 | device_desc.iManufacturer = status; |
| 163 | |
| 164 | status = usb_string_id(cdev); |
| 165 | if (status < 0) |
| 166 | goto fail; |
| 167 | strings_dev[STRING_PRODUCT_IDX].id = status; |
| 168 | |
| 169 | device_desc.iProduct = status; |
| 170 | |
| 171 | /* config description */ |
| 172 | status = usb_string_id(cdev); |
| 173 | if (status < 0) |
| 174 | goto fail; |
| 175 | strings_dev[STRING_DESCRIPTION_IDX].id = status; |
| 176 | |
| 177 | serial_config_driver.iConfiguration = status; |
| 178 | |
| 179 | /* set up other descriptors */ |
| 180 | gcnum = usb_gadget_controller_number(gadget); |
| 181 | if (gcnum >= 0) |
| 182 | device_desc.bcdDevice = cpu_to_le16(GS_VERSION_NUM | gcnum); |
| 183 | else { |
| 184 | /* this is so simple (for now, no altsettings) that it |
| 185 | * SHOULD NOT have problems with bulk-capable hardware. |
| 186 | * so warn about unrcognized controllers -- don't panic. |
| 187 | * |
| 188 | * things like configuration and altsetting numbering |
| 189 | * can need hardware-specific attention though. |
| 190 | */ |
| 191 | pr_warning("gs_bind: controller '%s' not recognized\n", |
| 192 | gadget->name); |
| 193 | device_desc.bcdDevice = |
| 194 | __constant_cpu_to_le16(GS_VERSION_NUM | 0x0099); |
| 195 | } |
| 196 | |
| 197 | if (gadget_is_otg(cdev->gadget)) { |
| 198 | serial_config_driver.descriptors = otg_desc; |
| 199 | serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 200 | } |
| 201 | |
| 202 | /* register our configuration */ |
| 203 | status = usb_add_config(cdev, &serial_config_driver); |
| 204 | if (status < 0) |
| 205 | goto fail; |
| 206 | |
| 207 | INFO(cdev, "%s\n", GS_VERSION_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | return 0; |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 210 | |
| 211 | fail: |
| 212 | gserial_cleanup(); |
| 213 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 216 | static struct usb_composite_driver gserial_driver = { |
| 217 | .name = "g_serial", |
| 218 | .dev = &device_desc, |
| 219 | .strings = dev_strings, |
| 220 | .bind = gs_bind, |
| 221 | }; |
| 222 | |
| 223 | static int __init init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 225 | /* We *could* export two configs; that'd be much cleaner... |
| 226 | * but neither of these product IDs was defined that way. |
| 227 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (use_acm) { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 229 | serial_config_driver.label = "CDC ACM config"; |
| 230 | serial_config_driver.bConfigurationValue = 2; |
| 231 | device_desc.bDeviceClass = USB_CLASS_COMM; |
| 232 | device_desc.idProduct = |
| 233 | __constant_cpu_to_le16(GS_CDC_PRODUCT_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } else { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 235 | serial_config_driver.label = "Generic Serial config"; |
| 236 | serial_config_driver.bConfigurationValue = 1; |
| 237 | device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC; |
| 238 | device_desc.idProduct = |
| 239 | __constant_cpu_to_le16(GS_PRODUCT_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 241 | strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 243 | return usb_composite_register(&gserial_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 245 | module_init(init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 247 | static void __exit cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 249 | usb_composite_unregister(&gserial_driver); |
| 250 | gserial_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
David Brownell | 7bb5ea5 | 2008-06-19 18:19:03 -0700 | [diff] [blame^] | 252 | module_exit(cleanup); |