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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This software is distributed under the terms of the GNU General |
| 8 | * Public License ("GPL") as published by the Free Software Foundation, |
| 9 | * 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] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/utsname.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/tty.h> |
| 16 | #include <linux/tty_flip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 18 | #include "u_serial.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "gadget_chips.h" |
| 20 | |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | /* Defines */ |
| 23 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 24 | #define GS_VERSION_STR "v2.3" |
| 25 | #define GS_VERSION_NUM 0x2300 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define GS_LONG_NAME "Gadget Serial" |
| 28 | #define GS_SHORT_NAME "g_serial" |
| 29 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 30 | #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 33 | /* REVISIT only one port is supported for now; |
| 34 | * see gs_{send,recv}_packet() ... no multiplexing, |
| 35 | * and no support for multiple ACM devices. |
| 36 | */ |
| 37 | #define GS_NUM_PORTS 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #define GS_NUM_CONFIGS 1 |
| 40 | #define GS_NO_CONFIG_ID 0 |
| 41 | #define GS_BULK_CONFIG_ID 1 |
| 42 | #define GS_ACM_CONFIG_ID 2 |
| 43 | |
| 44 | #define GS_MAX_NUM_INTERFACES 2 |
| 45 | #define GS_BULK_INTERFACE_ID 0 |
| 46 | #define GS_CONTROL_INTERFACE_ID 0 |
| 47 | #define GS_DATA_INTERFACE_ID 1 |
| 48 | |
| 49 | #define GS_MAX_DESC_LEN 256 |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #define GS_DEFAULT_USE_ACM 0 |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 54 | /* maxpacket and other transfer characteristics vary by speed. */ |
| 55 | static inline struct usb_endpoint_descriptor * |
| 56 | choose_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, |
| 57 | struct usb_endpoint_descriptor *fs) |
| 58 | { |
| 59 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) |
| 60 | return hs; |
| 61 | return fs; |
| 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* Thanks to NetChip Technologies for donating this product ID. |
| 66 | * |
| 67 | * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! |
| 68 | * Instead: allocate your own, using normal USB-IF procedures. |
| 69 | */ |
| 70 | #define GS_VENDOR_ID 0x0525 /* NetChip */ |
| 71 | #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */ |
| 72 | #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */ |
| 73 | |
| 74 | #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */ |
| 75 | #define GS_NOTIFY_MAXPACKET 8 |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* the device structure holds info for the USB device */ |
| 78 | struct gs_dev { |
| 79 | struct usb_gadget *dev_gadget; /* gadget device pointer */ |
| 80 | spinlock_t dev_lock; /* lock for set/reset config */ |
| 81 | int dev_config; /* configuration number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct usb_request *dev_ctrl_req; /* control request */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 83 | |
| 84 | struct gserial gser; /* serial/tty port */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | |
| 88 | /* Functions */ |
| 89 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 90 | /* gadget driver internals */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static int gs_set_config(struct gs_dev *dev, unsigned config); |
| 92 | static void gs_reset_config(struct gs_dev *dev); |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 93 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | u8 type, unsigned int index, int is_otg); |
| 95 | |
| 96 | static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 97 | gfp_t kmalloc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req); |
| 99 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 100 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* USB descriptors */ |
| 103 | |
| 104 | #define GS_MANUFACTURER_STR_ID 1 |
| 105 | #define GS_PRODUCT_STR_ID 2 |
| 106 | #define GS_SERIAL_STR_ID 3 |
| 107 | #define GS_BULK_CONFIG_STR_ID 4 |
| 108 | #define GS_ACM_CONFIG_STR_ID 5 |
| 109 | #define GS_CONTROL_STR_ID 6 |
| 110 | #define GS_DATA_STR_ID 7 |
| 111 | |
| 112 | /* static strings, in UTF-8 */ |
| 113 | static char manufacturer[50]; |
| 114 | static struct usb_string gs_strings[] = { |
| 115 | { GS_MANUFACTURER_STR_ID, manufacturer }, |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 116 | { GS_PRODUCT_STR_ID, GS_VERSION_NAME }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { GS_BULK_CONFIG_STR_ID, "Gadget Serial Bulk" }, |
| 118 | { GS_ACM_CONFIG_STR_ID, "Gadget Serial CDC ACM" }, |
| 119 | { GS_CONTROL_STR_ID, "Gadget Serial Control" }, |
| 120 | { GS_DATA_STR_ID, "Gadget Serial Data" }, |
| 121 | { } /* end of list */ |
| 122 | }; |
| 123 | |
| 124 | static struct usb_gadget_strings gs_string_table = { |
| 125 | .language = 0x0409, /* en-us */ |
| 126 | .strings = gs_strings, |
| 127 | }; |
| 128 | |
| 129 | static struct usb_device_descriptor gs_device_desc = { |
| 130 | .bLength = USB_DT_DEVICE_SIZE, |
| 131 | .bDescriptorType = USB_DT_DEVICE, |
| 132 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 133 | .bDeviceSubClass = 0, |
| 134 | .bDeviceProtocol = 0, |
| 135 | .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID), |
| 136 | .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID), |
| 137 | .iManufacturer = GS_MANUFACTURER_STR_ID, |
| 138 | .iProduct = GS_PRODUCT_STR_ID, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .bNumConfigurations = GS_NUM_CONFIGS, |
| 140 | }; |
| 141 | |
| 142 | static struct usb_otg_descriptor gs_otg_descriptor = { |
| 143 | .bLength = sizeof(gs_otg_descriptor), |
| 144 | .bDescriptorType = USB_DT_OTG, |
| 145 | .bmAttributes = USB_OTG_SRP, |
| 146 | }; |
| 147 | |
| 148 | static struct usb_config_descriptor gs_bulk_config_desc = { |
| 149 | .bLength = USB_DT_CONFIG_SIZE, |
| 150 | .bDescriptorType = USB_DT_CONFIG, |
| 151 | /* .wTotalLength computed dynamically */ |
| 152 | .bNumInterfaces = 1, |
| 153 | .bConfigurationValue = GS_BULK_CONFIG_ID, |
| 154 | .iConfiguration = GS_BULK_CONFIG_STR_ID, |
| 155 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 156 | .bMaxPower = 1, |
| 157 | }; |
| 158 | |
| 159 | static struct usb_config_descriptor gs_acm_config_desc = { |
| 160 | .bLength = USB_DT_CONFIG_SIZE, |
| 161 | .bDescriptorType = USB_DT_CONFIG, |
| 162 | /* .wTotalLength computed dynamically */ |
| 163 | .bNumInterfaces = 2, |
| 164 | .bConfigurationValue = GS_ACM_CONFIG_ID, |
| 165 | .iConfiguration = GS_ACM_CONFIG_STR_ID, |
| 166 | .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER, |
| 167 | .bMaxPower = 1, |
| 168 | }; |
| 169 | |
| 170 | static const struct usb_interface_descriptor gs_bulk_interface_desc = { |
| 171 | .bLength = USB_DT_INTERFACE_SIZE, |
| 172 | .bDescriptorType = USB_DT_INTERFACE, |
| 173 | .bInterfaceNumber = GS_BULK_INTERFACE_ID, |
| 174 | .bNumEndpoints = 2, |
David Brownell | b937033 | 2008-05-07 14:27:37 -0700 | [diff] [blame] | 175 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | .bInterfaceSubClass = 0, |
| 177 | .bInterfaceProtocol = 0, |
| 178 | .iInterface = GS_DATA_STR_ID, |
| 179 | }; |
| 180 | |
| 181 | static const struct usb_interface_descriptor gs_control_interface_desc = { |
| 182 | .bLength = USB_DT_INTERFACE_SIZE, |
| 183 | .bDescriptorType = USB_DT_INTERFACE, |
| 184 | .bInterfaceNumber = GS_CONTROL_INTERFACE_ID, |
| 185 | .bNumEndpoints = 1, |
| 186 | .bInterfaceClass = USB_CLASS_COMM, |
| 187 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, |
| 188 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER, |
| 189 | .iInterface = GS_CONTROL_STR_ID, |
| 190 | }; |
| 191 | |
| 192 | static const struct usb_interface_descriptor gs_data_interface_desc = { |
| 193 | .bLength = USB_DT_INTERFACE_SIZE, |
| 194 | .bDescriptorType = USB_DT_INTERFACE, |
| 195 | .bInterfaceNumber = GS_DATA_INTERFACE_ID, |
| 196 | .bNumEndpoints = 2, |
| 197 | .bInterfaceClass = USB_CLASS_CDC_DATA, |
| 198 | .bInterfaceSubClass = 0, |
| 199 | .bInterfaceProtocol = 0, |
| 200 | .iInterface = GS_DATA_STR_ID, |
| 201 | }; |
| 202 | |
| 203 | static const struct usb_cdc_header_desc gs_header_desc = { |
| 204 | .bLength = sizeof(gs_header_desc), |
| 205 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 206 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, |
| 207 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 208 | }; |
| 209 | |
| 210 | static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 211 | .bLength = sizeof(gs_call_mgmt_descriptor), |
| 212 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 213 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, |
| 214 | .bmCapabilities = 0, |
| 215 | .bDataInterface = 1, /* index of data interface */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | static struct usb_cdc_acm_descriptor gs_acm_descriptor = { |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 219 | .bLength = sizeof(gs_acm_descriptor), |
| 220 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 221 | .bDescriptorSubType = USB_CDC_ACM_TYPE, |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 222 | .bmCapabilities = (1 << 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static const struct usb_cdc_union_desc gs_union_desc = { |
| 226 | .bLength = sizeof(gs_union_desc), |
| 227 | .bDescriptorType = USB_DT_CS_INTERFACE, |
| 228 | .bDescriptorSubType = USB_CDC_UNION_TYPE, |
| 229 | .bMasterInterface0 = 0, /* index of control interface */ |
| 230 | .bSlaveInterface0 = 1, /* index of data interface */ |
| 231 | }; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | static struct usb_endpoint_descriptor gs_fullspeed_notify_desc = { |
| 234 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 235 | .bDescriptorType = USB_DT_ENDPOINT, |
| 236 | .bEndpointAddress = USB_DIR_IN, |
| 237 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 238 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 239 | .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, |
| 240 | }; |
| 241 | |
| 242 | static struct usb_endpoint_descriptor gs_fullspeed_in_desc = { |
| 243 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 244 | .bDescriptorType = USB_DT_ENDPOINT, |
| 245 | .bEndpointAddress = USB_DIR_IN, |
| 246 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 247 | }; |
| 248 | |
| 249 | static struct usb_endpoint_descriptor gs_fullspeed_out_desc = { |
| 250 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 251 | .bDescriptorType = USB_DT_ENDPOINT, |
| 252 | .bEndpointAddress = USB_DIR_OUT, |
| 253 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 254 | }; |
| 255 | |
| 256 | static const struct usb_descriptor_header *gs_bulk_fullspeed_function[] = { |
| 257 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 258 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, |
| 259 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, |
| 260 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, |
| 261 | NULL, |
| 262 | }; |
| 263 | |
| 264 | static const struct usb_descriptor_header *gs_acm_fullspeed_function[] = { |
| 265 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 266 | (struct usb_descriptor_header *) &gs_control_interface_desc, |
| 267 | (struct usb_descriptor_header *) &gs_header_desc, |
| 268 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, |
| 269 | (struct usb_descriptor_header *) &gs_acm_descriptor, |
| 270 | (struct usb_descriptor_header *) &gs_union_desc, |
| 271 | (struct usb_descriptor_header *) &gs_fullspeed_notify_desc, |
| 272 | (struct usb_descriptor_header *) &gs_data_interface_desc, |
| 273 | (struct usb_descriptor_header *) &gs_fullspeed_in_desc, |
| 274 | (struct usb_descriptor_header *) &gs_fullspeed_out_desc, |
| 275 | NULL, |
| 276 | }; |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | static struct usb_endpoint_descriptor gs_highspeed_notify_desc = { |
| 279 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 280 | .bDescriptorType = USB_DT_ENDPOINT, |
| 281 | .bEndpointAddress = USB_DIR_IN, |
| 282 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 283 | .wMaxPacketSize = __constant_cpu_to_le16(GS_NOTIFY_MAXPACKET), |
| 284 | .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, |
| 285 | }; |
| 286 | |
| 287 | static struct usb_endpoint_descriptor gs_highspeed_in_desc = { |
| 288 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 289 | .bDescriptorType = USB_DT_ENDPOINT, |
| 290 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 291 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 292 | }; |
| 293 | |
| 294 | static struct usb_endpoint_descriptor gs_highspeed_out_desc = { |
| 295 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 296 | .bDescriptorType = USB_DT_ENDPOINT, |
| 297 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 298 | .wMaxPacketSize = __constant_cpu_to_le16(512), |
| 299 | }; |
| 300 | |
| 301 | static struct usb_qualifier_descriptor gs_qualifier_desc = { |
| 302 | .bLength = sizeof(struct usb_qualifier_descriptor), |
| 303 | .bDescriptorType = USB_DT_DEVICE_QUALIFIER, |
| 304 | .bcdUSB = __constant_cpu_to_le16 (0x0200), |
| 305 | /* assumes ep0 uses the same value for both speeds ... */ |
| 306 | .bNumConfigurations = GS_NUM_CONFIGS, |
| 307 | }; |
| 308 | |
| 309 | static const struct usb_descriptor_header *gs_bulk_highspeed_function[] = { |
| 310 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 311 | (struct usb_descriptor_header *) &gs_bulk_interface_desc, |
| 312 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, |
| 313 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, |
| 314 | NULL, |
| 315 | }; |
| 316 | |
| 317 | static const struct usb_descriptor_header *gs_acm_highspeed_function[] = { |
| 318 | (struct usb_descriptor_header *) &gs_otg_descriptor, |
| 319 | (struct usb_descriptor_header *) &gs_control_interface_desc, |
| 320 | (struct usb_descriptor_header *) &gs_header_desc, |
| 321 | (struct usb_descriptor_header *) &gs_call_mgmt_descriptor, |
| 322 | (struct usb_descriptor_header *) &gs_acm_descriptor, |
| 323 | (struct usb_descriptor_header *) &gs_union_desc, |
| 324 | (struct usb_descriptor_header *) &gs_highspeed_notify_desc, |
| 325 | (struct usb_descriptor_header *) &gs_data_interface_desc, |
| 326 | (struct usb_descriptor_header *) &gs_highspeed_in_desc, |
| 327 | (struct usb_descriptor_header *) &gs_highspeed_out_desc, |
| 328 | NULL, |
| 329 | }; |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 332 | /*-------------------------------------------------------------------------*/ |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* Module */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 335 | MODULE_DESCRIPTION(GS_VERSION_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | MODULE_AUTHOR("Al Borchers"); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 337 | MODULE_AUTHOR("David Brownell"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | MODULE_LICENSE("GPL"); |
| 339 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 340 | static unsigned int use_acm = GS_DEFAULT_USE_ACM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | module_param(use_acm, uint, S_IRUGO); |
| 342 | MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no"); |
| 343 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 344 | /*-------------------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* Gadget Driver */ |
| 347 | |
| 348 | /* |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 349 | * gs_unbind |
| 350 | * |
| 351 | * Called on module unload. Frees the control request and device |
| 352 | * structure. |
| 353 | */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 354 | static void __exit gs_unbind(struct usb_gadget *gadget) |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 355 | { |
| 356 | struct gs_dev *dev = get_gadget_data(gadget); |
| 357 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 358 | /* read/write requests already freed, only control request remains */ |
| 359 | if (dev != NULL) { |
| 360 | if (dev->dev_ctrl_req != NULL) { |
| 361 | gs_free_req(gadget->ep0, dev->dev_ctrl_req); |
| 362 | dev->dev_ctrl_req = NULL; |
| 363 | } |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 364 | gs_reset_config(dev); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 365 | kfree(dev); |
| 366 | set_gadget_data(gadget, NULL); |
| 367 | } |
| 368 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 369 | pr_info("gs_unbind: %s unbound\n", GS_VERSION_NAME); |
| 370 | |
| 371 | gserial_cleanup(); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * gs_bind |
| 376 | * |
| 377 | * Called on module load. Allocates and initializes the device |
| 378 | * structure and a control request. |
| 379 | */ |
David Brownell | 329af28 | 2006-02-18 12:31:05 -0800 | [diff] [blame] | 380 | static int __init gs_bind(struct usb_gadget *gadget) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | int ret; |
| 383 | struct usb_ep *ep; |
| 384 | struct gs_dev *dev; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 385 | int gcnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 387 | ret = gserial_setup(gadget, GS_NUM_PORTS); |
| 388 | if (ret < 0) |
| 389 | return ret; |
| 390 | |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 391 | /* Some controllers can't support CDC ACM: |
| 392 | * - sh doesn't support multiple interfaces or configs; |
| 393 | * - sa1100 doesn't have a third interrupt endpoint |
| 394 | */ |
| 395 | if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | use_acm = 0; |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 397 | |
| 398 | gcnum = usb_gadget_controller_number(gadget); |
| 399 | if (gcnum >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | gs_device_desc.bcdDevice = |
David Brownell | 91e79c9 | 2005-07-13 15:18:30 -0700 | [diff] [blame] | 401 | cpu_to_le16(GS_VERSION_NUM | gcnum); |
| 402 | else { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 403 | pr_warning("gs_bind: controller '%s' not recognized\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | gadget->name); |
| 405 | /* unrecognized, but safe unless bulk is REALLY quirky */ |
| 406 | gs_device_desc.bcdDevice = |
| 407 | __constant_cpu_to_le16(GS_VERSION_NUM|0x0099); |
| 408 | } |
| 409 | |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 410 | dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 411 | if (dev == NULL) { |
| 412 | ret = -ENOMEM; |
| 413 | goto autoconf_fail; |
| 414 | } |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | usb_ep_autoconfig_reset(gadget); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 417 | ret = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc); |
| 420 | if (!ep) |
| 421 | goto autoconf_fail; |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 422 | dev->gser.in = ep; |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 423 | ep->driver_data = dev; /* claim the endpoint */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc); |
| 426 | if (!ep) |
| 427 | goto autoconf_fail; |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 428 | dev->gser.out = ep; |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 429 | ep->driver_data = dev; /* claim the endpoint */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | if (use_acm) { |
| 432 | ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc); |
| 433 | if (!ep) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 434 | pr_err("gs_bind: cannot run ACM on %s\n", gadget->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | goto autoconf_fail; |
| 436 | } |
| 437 | gs_device_desc.idProduct = __constant_cpu_to_le16( |
| 438 | GS_CDC_PRODUCT_ID), |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 439 | dev->gser.notify = ep; |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 440 | ep->driver_data = dev; /* claim the endpoint */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | gs_device_desc.bDeviceClass = use_acm |
| 444 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; |
| 445 | gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 446 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 447 | if (gadget_is_dualspeed(gadget)) { |
| 448 | gs_qualifier_desc.bDeviceClass = use_acm |
| 449 | ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC; |
| 450 | /* assume ep0 uses the same packet size for both speeds */ |
| 451 | gs_qualifier_desc.bMaxPacketSize0 = |
| 452 | gs_device_desc.bMaxPacketSize0; |
| 453 | /* assume endpoints are dual-speed */ |
| 454 | gs_highspeed_notify_desc.bEndpointAddress = |
| 455 | gs_fullspeed_notify_desc.bEndpointAddress; |
| 456 | gs_highspeed_in_desc.bEndpointAddress = |
| 457 | gs_fullspeed_in_desc.bEndpointAddress; |
| 458 | gs_highspeed_out_desc.bEndpointAddress = |
| 459 | gs_fullspeed_out_desc.bEndpointAddress; |
| 460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | usb_gadget_set_selfpowered(gadget); |
| 463 | |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 464 | if (gadget_is_otg(gadget)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | gs_otg_descriptor.bmAttributes |= USB_OTG_HNP, |
| 466 | gs_bulk_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 467 | gs_acm_config_desc.bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 468 | } |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s", |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 471 | init_utsname()->sysname, init_utsname()->release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | gadget->name); |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | dev->dev_gadget = gadget; |
| 475 | spin_lock_init(&dev->dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | set_gadget_data(gadget, dev); |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /* preallocate control response and buffer */ |
| 479 | dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN, |
| 480 | GFP_KERNEL); |
| 481 | if (dev->dev_ctrl_req == NULL) { |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 482 | ret = -ENOMEM; |
| 483 | goto autoconf_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | gadget->ep0->driver_data = dev; |
| 486 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 487 | pr_info("gs_bind: %s bound\n", GS_VERSION_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | |
| 491 | autoconf_fail: |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 492 | kfree(dev); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 493 | gserial_cleanup(); |
| 494 | pr_err("gs_bind: to %s, err %d\n", gadget->name, ret); |
| 495 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | static int gs_setup_standard(struct usb_gadget *gadget, |
| 499 | const struct usb_ctrlrequest *ctrl) |
| 500 | { |
| 501 | int ret = -EOPNOTSUPP; |
| 502 | struct gs_dev *dev = get_gadget_data(gadget); |
| 503 | struct usb_request *req = dev->dev_ctrl_req; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 504 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 505 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 506 | u16 wLength = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | switch (ctrl->bRequest) { |
| 509 | case USB_REQ_GET_DESCRIPTOR: |
| 510 | if (ctrl->bRequestType != USB_DIR_IN) |
| 511 | break; |
| 512 | |
| 513 | switch (wValue >> 8) { |
| 514 | case USB_DT_DEVICE: |
| 515 | ret = min(wLength, |
| 516 | (u16)sizeof(struct usb_device_descriptor)); |
| 517 | memcpy(req->buf, &gs_device_desc, ret); |
| 518 | break; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | case USB_DT_DEVICE_QUALIFIER: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 521 | if (!gadget_is_dualspeed(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | break; |
| 523 | ret = min(wLength, |
| 524 | (u16)sizeof(struct usb_qualifier_descriptor)); |
| 525 | memcpy(req->buf, &gs_qualifier_desc, ret); |
| 526 | break; |
| 527 | |
| 528 | case USB_DT_OTHER_SPEED_CONFIG: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 529 | if (!gadget_is_dualspeed(gadget)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | break; |
| 531 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | case USB_DT_CONFIG: |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 533 | ret = gs_build_config_buf(req->buf, gadget, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | wValue >> 8, wValue & 0xff, |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 535 | gadget_is_otg(gadget)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | if (ret >= 0) |
| 537 | ret = min(wLength, (u16)ret); |
| 538 | break; |
| 539 | |
| 540 | case USB_DT_STRING: |
| 541 | /* wIndex == language code. */ |
| 542 | ret = usb_gadget_get_string(&gs_string_table, |
| 543 | wValue & 0xff, req->buf); |
| 544 | if (ret >= 0) |
| 545 | ret = min(wLength, (u16)ret); |
| 546 | break; |
| 547 | } |
| 548 | break; |
| 549 | |
| 550 | case USB_REQ_SET_CONFIGURATION: |
| 551 | if (ctrl->bRequestType != 0) |
| 552 | break; |
| 553 | spin_lock(&dev->dev_lock); |
| 554 | ret = gs_set_config(dev, wValue); |
| 555 | spin_unlock(&dev->dev_lock); |
| 556 | break; |
| 557 | |
| 558 | case USB_REQ_GET_CONFIGURATION: |
| 559 | if (ctrl->bRequestType != USB_DIR_IN) |
| 560 | break; |
| 561 | *(u8 *)req->buf = dev->dev_config; |
| 562 | ret = min(wLength, (u16)1); |
| 563 | break; |
| 564 | |
| 565 | case USB_REQ_SET_INTERFACE: |
| 566 | if (ctrl->bRequestType != USB_RECIP_INTERFACE |
| 567 | || !dev->dev_config |
| 568 | || wIndex >= GS_MAX_NUM_INTERFACES) |
| 569 | break; |
| 570 | if (dev->dev_config == GS_BULK_CONFIG_ID |
| 571 | && wIndex != GS_BULK_INTERFACE_ID) |
| 572 | break; |
| 573 | /* no alternate interface settings */ |
| 574 | if (wValue != 0) |
| 575 | break; |
| 576 | spin_lock(&dev->dev_lock); |
| 577 | /* PXA hardware partially handles SET_INTERFACE; |
| 578 | * we need to kluge around that interference. */ |
| 579 | if (gadget_is_pxa(gadget)) { |
| 580 | ret = gs_set_config(dev, use_acm ? |
| 581 | GS_ACM_CONFIG_ID : GS_BULK_CONFIG_ID); |
| 582 | goto set_interface_done; |
| 583 | } |
| 584 | if (dev->dev_config != GS_BULK_CONFIG_ID |
| 585 | && wIndex == GS_CONTROL_INTERFACE_ID) { |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 586 | if (dev->gser.notify) { |
| 587 | usb_ep_disable(dev->gser.notify); |
| 588 | usb_ep_enable(dev->gser.notify, |
| 589 | dev->gser.notify_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | } else { |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 592 | gserial_connect(&dev->gser, 0); |
| 593 | gserial_disconnect(&dev->gser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | ret = 0; |
| 596 | set_interface_done: |
| 597 | spin_unlock(&dev->dev_lock); |
| 598 | break; |
| 599 | |
| 600 | case USB_REQ_GET_INTERFACE: |
| 601 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) |
| 602 | || dev->dev_config == GS_NO_CONFIG_ID) |
| 603 | break; |
| 604 | if (wIndex >= GS_MAX_NUM_INTERFACES |
| 605 | || (dev->dev_config == GS_BULK_CONFIG_ID |
| 606 | && wIndex != GS_BULK_INTERFACE_ID)) { |
| 607 | ret = -EDOM; |
| 608 | break; |
| 609 | } |
| 610 | /* no alternate interface settings */ |
| 611 | *(u8 *)req->buf = 0; |
| 612 | ret = min(wLength, (u16)1); |
| 613 | break; |
| 614 | |
| 615 | default: |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 616 | pr_err("gs_setup: unknown standard request, type=%02x, " |
| 617 | "request=%02x, value=%04x, index=%04x, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | ctrl->bRequestType, ctrl->bRequest, |
| 619 | wValue, wIndex, wLength); |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 626 | static void gs_setup_complete_set_line_coding(struct usb_ep *ep, |
| 627 | struct usb_request *req) |
| 628 | { |
| 629 | struct gs_dev *dev = ep->driver_data; |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 630 | |
| 631 | switch (req->status) { |
| 632 | case 0: |
| 633 | /* normal completion */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 634 | if (req->actual != sizeof(dev->gser.port_line_coding)) |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 635 | usb_ep_set_halt(ep); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 636 | else { |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 637 | struct usb_cdc_line_coding *value = req->buf; |
| 638 | |
| 639 | /* REVISIT: we currently just remember this data. |
| 640 | * If we change that, (a) validate it first, then |
| 641 | * (b) update whatever hardware needs updating. |
| 642 | */ |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 643 | spin_lock(&dev->dev_lock); |
| 644 | dev->gser.port_line_coding = *value; |
| 645 | spin_unlock(&dev->dev_lock); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 646 | } |
| 647 | break; |
| 648 | |
| 649 | case -ESHUTDOWN: |
| 650 | /* disconnect */ |
| 651 | gs_free_req(ep, req); |
| 652 | break; |
| 653 | |
| 654 | default: |
| 655 | /* unexpected */ |
| 656 | break; |
| 657 | } |
| 658 | return; |
| 659 | } |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | static int gs_setup_class(struct usb_gadget *gadget, |
| 662 | const struct usb_ctrlrequest *ctrl) |
| 663 | { |
| 664 | int ret = -EOPNOTSUPP; |
| 665 | struct gs_dev *dev = get_gadget_data(gadget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | struct usb_request *req = dev->dev_ctrl_req; |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 667 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 668 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 669 | u16 wLength = le16_to_cpu(ctrl->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
| 671 | switch (ctrl->bRequest) { |
| 672 | case USB_CDC_REQ_SET_LINE_CODING: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 673 | if (wLength != sizeof(struct usb_cdc_line_coding)) |
| 674 | break; |
| 675 | ret = wLength; |
| 676 | req->complete = gs_setup_complete_set_line_coding; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | break; |
| 678 | |
| 679 | case USB_CDC_REQ_GET_LINE_CODING: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 680 | ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding)); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 681 | spin_lock(&dev->dev_lock); |
| 682 | memcpy(req->buf, &dev->gser.port_line_coding, ret); |
| 683 | spin_unlock(&dev->dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case USB_CDC_REQ_SET_CONTROL_LINE_STATE: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 687 | if (wLength != 0) |
| 688 | break; |
| 689 | ret = 0; |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 690 | /* REVISIT: we currently just remember this data. |
| 691 | * If we change that, update whatever hardware needs |
| 692 | * updating. |
| 693 | */ |
| 694 | spin_lock(&dev->dev_lock); |
| 695 | dev->gser.port_handshake_bits = wValue; |
| 696 | spin_unlock(&dev->dev_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | break; |
| 698 | |
| 699 | default: |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 700 | /* NOTE: strictly speaking, we should accept AT-commands |
| 701 | * using SEND_ENCPSULATED_COMMAND/GET_ENCAPSULATED_RESPONSE. |
| 702 | * But our call management descriptor says we don't handle |
| 703 | * call management, so we should be able to get by without |
| 704 | * handling those "required" commands (except by stalling). |
| 705 | */ |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 706 | pr_err("gs_setup: unknown class request, " |
David Brownell | 49b4f90 | 2007-08-26 12:44:24 -0700 | [diff] [blame] | 707 | "type=%02x, request=%02x, value=%04x, " |
| 708 | "index=%04x, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | ctrl->bRequestType, ctrl->bRequest, |
| 710 | wValue, wIndex, wLength); |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | return ret; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * gs_setup_complete |
| 719 | */ |
| 720 | static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 721 | { |
| 722 | if (req->status || req->actual != req->length) { |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 723 | pr_err("gs_setup_complete: status error, status=%d, " |
| 724 | "actual=%d, length=%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | req->status, req->actual, req->length); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 730 | * gs_setup |
| 731 | * |
| 732 | * Implements all the control endpoint functionality that's not |
| 733 | * handled in hardware or the hardware driver. |
| 734 | * |
| 735 | * Returns the size of the data sent to the host, or a negative |
| 736 | * error number. |
| 737 | */ |
| 738 | static int gs_setup(struct usb_gadget *gadget, |
| 739 | const struct usb_ctrlrequest *ctrl) |
| 740 | { |
| 741 | int ret = -EOPNOTSUPP; |
| 742 | struct gs_dev *dev = get_gadget_data(gadget); |
| 743 | struct usb_request *req = dev->dev_ctrl_req; |
| 744 | u16 wIndex = le16_to_cpu(ctrl->wIndex); |
| 745 | u16 wValue = le16_to_cpu(ctrl->wValue); |
| 746 | u16 wLength = le16_to_cpu(ctrl->wLength); |
| 747 | |
| 748 | req->complete = gs_setup_complete; |
| 749 | |
| 750 | switch (ctrl->bRequestType & USB_TYPE_MASK) { |
| 751 | case USB_TYPE_STANDARD: |
| 752 | ret = gs_setup_standard(gadget, ctrl); |
| 753 | break; |
| 754 | |
| 755 | case USB_TYPE_CLASS: |
| 756 | ret = gs_setup_class(gadget, ctrl); |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | pr_err("gs_setup: unknown request, type=%02x, request=%02x, " |
| 761 | "value=%04x, index=%04x, length=%d\n", |
| 762 | ctrl->bRequestType, ctrl->bRequest, |
| 763 | wValue, wIndex, wLength); |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | /* respond with data transfer before status phase? */ |
| 768 | if (ret >= 0) { |
| 769 | req->length = ret; |
| 770 | req->zero = ret < wLength |
| 771 | && (ret % gadget->ep0->maxpacket) == 0; |
| 772 | ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
| 773 | if (ret < 0) { |
| 774 | pr_err("gs_setup: cannot queue response, ret=%d\n", |
| 775 | ret); |
| 776 | req->status = 0; |
| 777 | gs_setup_complete(gadget->ep0, req); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | /* device either stalls (ret < 0) or reports success */ |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | * gs_disconnect |
| 787 | * |
| 788 | * Called when the device is disconnected. Frees the closed |
| 789 | * ports and disconnects open ports. Open ports will be freed |
| 790 | * on close. Then reallocates the ports for the next connection. |
| 791 | */ |
| 792 | static void gs_disconnect(struct usb_gadget *gadget) |
| 793 | { |
| 794 | unsigned long flags; |
| 795 | struct gs_dev *dev = get_gadget_data(gadget); |
| 796 | |
| 797 | spin_lock_irqsave(&dev->dev_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | gs_reset_config(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | spin_unlock_irqrestore(&dev->dev_lock, flags); |
| 800 | |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 801 | pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 804 | static struct usb_gadget_driver gs_gadget_driver = { |
| 805 | #ifdef CONFIG_USB_GADGET_DUALSPEED |
| 806 | .speed = USB_SPEED_HIGH, |
| 807 | #else |
| 808 | .speed = USB_SPEED_FULL, |
| 809 | #endif /* CONFIG_USB_GADGET_DUALSPEED */ |
| 810 | .function = GS_LONG_NAME, |
| 811 | .bind = gs_bind, |
| 812 | .unbind = gs_unbind, |
| 813 | .setup = gs_setup, |
| 814 | .disconnect = gs_disconnect, |
| 815 | .driver = { |
| 816 | .name = GS_SHORT_NAME, |
| 817 | .owner = THIS_MODULE, |
| 818 | }, |
| 819 | }; |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | /* |
| 822 | * gs_set_config |
| 823 | * |
| 824 | * Configures the device by enabling device specific |
| 825 | * optimizations, setting up the endpoints, allocating |
| 826 | * read and write requests and queuing read requests. |
| 827 | * |
| 828 | * The device lock must be held when calling this function. |
| 829 | */ |
| 830 | static int gs_set_config(struct gs_dev *dev, unsigned config) |
| 831 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | int ret = 0; |
| 833 | struct usb_gadget *gadget = dev->dev_gadget; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
| 835 | if (config == dev->dev_config) |
| 836 | return 0; |
| 837 | |
| 838 | gs_reset_config(dev); |
| 839 | |
| 840 | switch (config) { |
| 841 | case GS_NO_CONFIG_ID: |
| 842 | return 0; |
| 843 | case GS_BULK_CONFIG_ID: |
| 844 | if (use_acm) |
| 845 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | break; |
| 847 | case GS_ACM_CONFIG_ID: |
| 848 | if (!use_acm) |
| 849 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | break; |
| 851 | default: |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 855 | dev->gser.in_desc = choose_ep_desc(gadget, |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 856 | &gs_highspeed_in_desc, |
| 857 | &gs_fullspeed_in_desc); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 858 | dev->gser.out_desc = choose_ep_desc(gadget, |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 859 | &gs_highspeed_out_desc, |
| 860 | &gs_fullspeed_out_desc); |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 861 | dev->gser.notify_desc = dev->gser.notify |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 862 | ? choose_ep_desc(gadget, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | &gs_highspeed_notify_desc, |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 864 | &gs_fullspeed_notify_desc) |
| 865 | : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 867 | /* only support one "serial" port for now */ |
| 868 | if (dev->gser.notify) { |
| 869 | ret = usb_ep_enable(dev->gser.notify, dev->gser.notify_desc); |
| 870 | if (ret < 0) |
| 871 | return ret; |
| 872 | dev->gser.notify->driver_data = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 875 | ret = gserial_connect(&dev->gser, 0); |
| 876 | if (ret < 0) { |
| 877 | if (dev->gser.notify) { |
| 878 | usb_ep_disable(dev->gser.notify); |
| 879 | dev->gser.notify->driver_data = NULL; |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 880 | } |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 881 | return ret; |
David Brownell | 734d37c | 2008-05-07 14:25:24 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | dev->dev_config = config; |
| 885 | |
David Brownell | f371e75 | 2008-04-18 17:37:49 -0700 | [diff] [blame] | 886 | /* REVISIT the ACM mode should be able to actually *issue* some |
| 887 | * notifications, for at least serial state change events if |
| 888 | * not also for network connection; say so in bmCapabilities. |
| 889 | */ |
| 890 | |
David Brownell | 0027492 | 2007-11-19 12:58:36 -0800 | [diff] [blame] | 891 | pr_info("gs_set_config: %s configured, %s speed %s config\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | GS_LONG_NAME, |
| 893 | gadget->speed == USB_SPEED_HIGH ? "high" : "full", |
| 894 | config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM"); |
| 895 | |
| 896 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* |
| 900 | * gs_reset_config |
| 901 | * |
| 902 | * Mark the device as not configured, disable all endpoints, |
| 903 | * which forces completion of pending I/O and frees queued |
| 904 | * requests, and free the remaining write requests on the |
| 905 | * free list. |
| 906 | * |
| 907 | * The device lock must be held when calling this function. |
| 908 | */ |
| 909 | static void gs_reset_config(struct gs_dev *dev) |
| 910 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (dev->dev_config == GS_NO_CONFIG_ID) |
| 912 | return; |
| 913 | |
| 914 | dev->dev_config = GS_NO_CONFIG_ID; |
| 915 | |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 916 | gserial_disconnect(&dev->gser); |
| 917 | if (dev->gser.notify) { |
| 918 | usb_ep_disable(dev->gser.notify); |
| 919 | dev->gser.notify->driver_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
| 924 | * gs_build_config_buf |
| 925 | * |
| 926 | * Builds the config descriptors in the given buffer and returns the |
| 927 | * length, or a negative error number. |
| 928 | */ |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 929 | static int gs_build_config_buf(u8 *buf, struct usb_gadget *g, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | u8 type, unsigned int index, int is_otg) |
| 931 | { |
| 932 | int len; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 933 | int high_speed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | const struct usb_config_descriptor *config_desc; |
| 935 | const struct usb_descriptor_header **function; |
| 936 | |
| 937 | if (index >= gs_device_desc.bNumConfigurations) |
| 938 | return -EINVAL; |
| 939 | |
| 940 | /* other speed switches high and full speed */ |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 941 | if (gadget_is_dualspeed(g)) { |
| 942 | high_speed = (g->speed == USB_SPEED_HIGH); |
| 943 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 944 | high_speed = !high_speed; |
| 945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | if (use_acm) { |
| 948 | config_desc = &gs_acm_config_desc; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 949 | function = high_speed |
| 950 | ? gs_acm_highspeed_function |
| 951 | : gs_acm_fullspeed_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } else { |
| 953 | config_desc = &gs_bulk_config_desc; |
David Brownell | 51a0e85 | 2007-08-02 00:02:47 -0700 | [diff] [blame] | 954 | function = high_speed |
| 955 | ? gs_bulk_highspeed_function |
| 956 | : gs_bulk_fullspeed_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* for now, don't advertise srp-only devices */ |
| 960 | if (!is_otg) |
| 961 | function++; |
| 962 | |
| 963 | len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function); |
| 964 | if (len < 0) |
| 965 | return len; |
| 966 | |
| 967 | ((struct usb_config_descriptor *)buf)->bDescriptorType = type; |
| 968 | |
| 969 | return len; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * gs_alloc_req |
| 974 | * |
| 975 | * Allocate a usb_request and its buffer. Returns a pointer to the |
| 976 | * usb_request or NULL if there is an error. |
| 977 | */ |
David Brownell | 1bbc169 | 2005-05-07 13:05:13 -0700 | [diff] [blame] | 978 | static struct usb_request * |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 979 | gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | { |
| 981 | struct usb_request *req; |
| 982 | |
| 983 | if (ep == NULL) |
| 984 | return NULL; |
| 985 | |
| 986 | req = usb_ep_alloc_request(ep, kmalloc_flags); |
| 987 | |
| 988 | if (req != NULL) { |
| 989 | req->length = len; |
| 990 | req->buf = kmalloc(len, kmalloc_flags); |
| 991 | if (req->buf == NULL) { |
| 992 | usb_ep_free_request(ep, req); |
| 993 | return NULL; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | return req; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * gs_free_req |
| 1002 | * |
| 1003 | * Free a usb_request and its buffer. |
| 1004 | */ |
| 1005 | static void gs_free_req(struct usb_ep *ep, struct usb_request *req) |
| 1006 | { |
| 1007 | if (ep != NULL && req != NULL) { |
| 1008 | kfree(req->buf); |
| 1009 | usb_ep_free_request(ep, req); |
| 1010 | } |
| 1011 | } |
| 1012 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1013 | /*-------------------------------------------------------------------------*/ |
| 1014 | |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1015 | /* |
| 1016 | * gs_module_init |
| 1017 | * |
| 1018 | * Register as a USB gadget driver and a tty driver. |
| 1019 | */ |
| 1020 | static int __init gs_module_init(void) |
| 1021 | { |
David Brownell | a7707ad | 2008-06-19 17:52:07 -0700 | [diff] [blame^] | 1022 | return usb_gadget_register_driver(&gs_gadget_driver); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | module_init(gs_module_init); |
| 1025 | |
| 1026 | /* |
| 1027 | * gs_module_exit |
| 1028 | * |
| 1029 | * Unregister as a tty driver and a USB gadget driver. |
| 1030 | */ |
| 1031 | static void __exit gs_module_exit(void) |
| 1032 | { |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1033 | usb_gadget_unregister_driver(&gs_gadget_driver); |
David Brownell | 9079e91 | 2008-05-07 16:00:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | module_exit(gs_module_exit); |