2 * serial.c -- USB gadget serial driver
4 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
5 * Copyright (C) 2008 by David Brownell
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.
12 #include <linux/kernel.h>
13 #include <linux/utsname.h>
14 #include <linux/device.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
19 #include "gadget_chips.h"
24 #define GS_VERSION_STR "v2.3"
25 #define GS_VERSION_NUM 0x2300
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_SHORT_NAME "g_serial"
30 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
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.
37 #define GS_NUM_PORTS 1
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
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
49 #define GS_MAX_DESC_LEN 256
51 #define GS_DEFAULT_USE_ACM 0
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)
59 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
65 /* Thanks to NetChip Technologies for donating this product ID.
67 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
68 * Instead: allocate your own, using normal USB-IF procedures.
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 */
74 #define GS_LOG2_NOTIFY_INTERVAL 5 /* 1 << 5 == 32 msec */
75 #define GS_NOTIFY_MAXPACKET 8
77 /* the device structure holds info for the USB device */
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 */
82 struct usb_request *dev_ctrl_req; /* control request */
84 struct gserial gser; /* serial/tty port */
90 /* gadget driver internals */
91 static int gs_set_config(struct gs_dev *dev, unsigned config);
92 static void gs_reset_config(struct gs_dev *dev);
93 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
94 u8 type, unsigned int index, int is_otg);
96 static struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned int len,
98 static void gs_free_req(struct usb_ep *ep, struct usb_request *req);
100 /*-------------------------------------------------------------------------*/
102 /* USB descriptors */
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
112 /* static strings, in UTF-8 */
113 static char manufacturer[50];
114 static struct usb_string gs_strings[] = {
115 { GS_MANUFACTURER_STR_ID, manufacturer },
116 { GS_PRODUCT_STR_ID, GS_VERSION_NAME },
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 */
124 static struct usb_gadget_strings gs_string_table = {
125 .language = 0x0409, /* en-us */
126 .strings = gs_strings,
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,
139 .bNumConfigurations = GS_NUM_CONFIGS,
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,
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 */
153 .bConfigurationValue = GS_BULK_CONFIG_ID,
154 .iConfiguration = GS_BULK_CONFIG_STR_ID,
155 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
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 */
164 .bConfigurationValue = GS_ACM_CONFIG_ID,
165 .iConfiguration = GS_ACM_CONFIG_STR_ID,
166 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
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,
175 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
176 .bInterfaceSubClass = 0,
177 .bInterfaceProtocol = 0,
178 .iInterface = GS_DATA_STR_ID,
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,
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,
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,
197 .bInterfaceClass = USB_CLASS_CDC_DATA,
198 .bInterfaceSubClass = 0,
199 .bInterfaceProtocol = 0,
200 .iInterface = GS_DATA_STR_ID,
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),
210 static const struct usb_cdc_call_mgmt_descriptor gs_call_mgmt_descriptor = {
211 .bLength = sizeof(gs_call_mgmt_descriptor),
212 .bDescriptorType = USB_DT_CS_INTERFACE,
213 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
215 .bDataInterface = 1, /* index of data interface */
218 static struct usb_cdc_acm_descriptor gs_acm_descriptor = {
219 .bLength = sizeof(gs_acm_descriptor),
220 .bDescriptorType = USB_DT_CS_INTERFACE,
221 .bDescriptorSubType = USB_CDC_ACM_TYPE,
222 .bmCapabilities = (1 << 1),
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 */
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,
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,
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,
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,
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,
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,
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),
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),
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,
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,
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,
332 /*-------------------------------------------------------------------------*/
335 MODULE_DESCRIPTION(GS_VERSION_NAME);
336 MODULE_AUTHOR("Al Borchers");
337 MODULE_AUTHOR("David Brownell");
338 MODULE_LICENSE("GPL");
340 static unsigned int use_acm = GS_DEFAULT_USE_ACM;
341 module_param(use_acm, uint, S_IRUGO);
342 MODULE_PARM_DESC(use_acm, "Use CDC ACM, 0=no, 1=yes, default=no");
344 /*-------------------------------------------------------------------------*/
351 * Called on module unload. Frees the control request and device
354 static void __exit gs_unbind(struct usb_gadget *gadget)
356 struct gs_dev *dev = get_gadget_data(gadget);
358 /* read/write requests already freed, only control request remains */
360 if (dev->dev_ctrl_req != NULL) {
361 gs_free_req(gadget->ep0, dev->dev_ctrl_req);
362 dev->dev_ctrl_req = NULL;
364 gs_reset_config(dev);
366 set_gadget_data(gadget, NULL);
369 pr_info("gs_unbind: %s unbound\n", GS_VERSION_NAME);
377 * Called on module load. Allocates and initializes the device
378 * structure and a control request.
380 static int __init gs_bind(struct usb_gadget *gadget)
387 ret = gserial_setup(gadget, GS_NUM_PORTS);
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
395 if (gadget_is_sh(gadget) || gadget_is_sa1100(gadget))
398 gcnum = usb_gadget_controller_number(gadget);
400 gs_device_desc.bcdDevice =
401 cpu_to_le16(GS_VERSION_NUM | gcnum);
403 pr_warning("gs_bind: controller '%s' not recognized\n",
405 /* unrecognized, but safe unless bulk is REALLY quirky */
406 gs_device_desc.bcdDevice =
407 __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
410 dev = kzalloc(sizeof(struct gs_dev), GFP_KERNEL);
416 usb_ep_autoconfig_reset(gadget);
419 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
423 ep->driver_data = dev; /* claim the endpoint */
425 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
429 ep->driver_data = dev; /* claim the endpoint */
432 ep = usb_ep_autoconfig(gadget, &gs_fullspeed_notify_desc);
434 pr_err("gs_bind: cannot run ACM on %s\n", gadget->name);
437 gs_device_desc.idProduct = __constant_cpu_to_le16(
439 dev->gser.notify = ep;
440 ep->driver_data = dev; /* claim the endpoint */
443 gs_device_desc.bDeviceClass = use_acm
444 ? USB_CLASS_COMM : USB_CLASS_VENDOR_SPEC;
445 gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
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;
462 usb_gadget_set_selfpowered(gadget);
464 if (gadget_is_otg(gadget)) {
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;
470 snprintf(manufacturer, sizeof(manufacturer), "%s %s with %s",
471 init_utsname()->sysname, init_utsname()->release,
474 dev->dev_gadget = gadget;
475 spin_lock_init(&dev->dev_lock);
476 set_gadget_data(gadget, dev);
478 /* preallocate control response and buffer */
479 dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
481 if (dev->dev_ctrl_req == NULL) {
485 gadget->ep0->driver_data = dev;
487 pr_info("gs_bind: %s bound\n", GS_VERSION_NAME);
494 pr_err("gs_bind: to %s, err %d\n", gadget->name, ret);
498 static int gs_setup_standard(struct usb_gadget *gadget,
499 const struct usb_ctrlrequest *ctrl)
501 int ret = -EOPNOTSUPP;
502 struct gs_dev *dev = get_gadget_data(gadget);
503 struct usb_request *req = dev->dev_ctrl_req;
504 u16 wIndex = le16_to_cpu(ctrl->wIndex);
505 u16 wValue = le16_to_cpu(ctrl->wValue);
506 u16 wLength = le16_to_cpu(ctrl->wLength);
508 switch (ctrl->bRequest) {
509 case USB_REQ_GET_DESCRIPTOR:
510 if (ctrl->bRequestType != USB_DIR_IN)
513 switch (wValue >> 8) {
516 (u16)sizeof(struct usb_device_descriptor));
517 memcpy(req->buf, &gs_device_desc, ret);
520 case USB_DT_DEVICE_QUALIFIER:
521 if (!gadget_is_dualspeed(gadget))
524 (u16)sizeof(struct usb_qualifier_descriptor));
525 memcpy(req->buf, &gs_qualifier_desc, ret);
528 case USB_DT_OTHER_SPEED_CONFIG:
529 if (!gadget_is_dualspeed(gadget))
533 ret = gs_build_config_buf(req->buf, gadget,
534 wValue >> 8, wValue & 0xff,
535 gadget_is_otg(gadget));
537 ret = min(wLength, (u16)ret);
541 /* wIndex == language code. */
542 ret = usb_gadget_get_string(&gs_string_table,
543 wValue & 0xff, req->buf);
545 ret = min(wLength, (u16)ret);
550 case USB_REQ_SET_CONFIGURATION:
551 if (ctrl->bRequestType != 0)
553 spin_lock(&dev->dev_lock);
554 ret = gs_set_config(dev, wValue);
555 spin_unlock(&dev->dev_lock);
558 case USB_REQ_GET_CONFIGURATION:
559 if (ctrl->bRequestType != USB_DIR_IN)
561 *(u8 *)req->buf = dev->dev_config;
562 ret = min(wLength, (u16)1);
565 case USB_REQ_SET_INTERFACE:
566 if (ctrl->bRequestType != USB_RECIP_INTERFACE
568 || wIndex >= GS_MAX_NUM_INTERFACES)
570 if (dev->dev_config == GS_BULK_CONFIG_ID
571 && wIndex != GS_BULK_INTERFACE_ID)
573 /* no alternate interface settings */
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;
584 if (dev->dev_config != GS_BULK_CONFIG_ID
585 && wIndex == GS_CONTROL_INTERFACE_ID) {
586 if (dev->gser.notify) {
587 usb_ep_disable(dev->gser.notify);
588 usb_ep_enable(dev->gser.notify,
589 dev->gser.notify_desc);
592 gserial_connect(&dev->gser, 0);
593 gserial_disconnect(&dev->gser);
597 spin_unlock(&dev->dev_lock);
600 case USB_REQ_GET_INTERFACE:
601 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
602 || dev->dev_config == GS_NO_CONFIG_ID)
604 if (wIndex >= GS_MAX_NUM_INTERFACES
605 || (dev->dev_config == GS_BULK_CONFIG_ID
606 && wIndex != GS_BULK_INTERFACE_ID)) {
610 /* no alternate interface settings */
612 ret = min(wLength, (u16)1);
616 pr_err("gs_setup: unknown standard request, type=%02x, "
617 "request=%02x, value=%04x, index=%04x, length=%d\n",
618 ctrl->bRequestType, ctrl->bRequest,
619 wValue, wIndex, wLength);
626 static void gs_setup_complete_set_line_coding(struct usb_ep *ep,
627 struct usb_request *req)
629 struct gs_dev *dev = ep->driver_data;
631 switch (req->status) {
633 /* normal completion */
634 if (req->actual != sizeof(dev->gser.port_line_coding))
637 struct usb_cdc_line_coding *value = req->buf;
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.
643 spin_lock(&dev->dev_lock);
644 dev->gser.port_line_coding = *value;
645 spin_unlock(&dev->dev_lock);
651 gs_free_req(ep, req);
661 static int gs_setup_class(struct usb_gadget *gadget,
662 const struct usb_ctrlrequest *ctrl)
664 int ret = -EOPNOTSUPP;
665 struct gs_dev *dev = get_gadget_data(gadget);
666 struct usb_request *req = dev->dev_ctrl_req;
667 u16 wIndex = le16_to_cpu(ctrl->wIndex);
668 u16 wValue = le16_to_cpu(ctrl->wValue);
669 u16 wLength = le16_to_cpu(ctrl->wLength);
671 switch (ctrl->bRequest) {
672 case USB_CDC_REQ_SET_LINE_CODING:
673 if (wLength != sizeof(struct usb_cdc_line_coding))
676 req->complete = gs_setup_complete_set_line_coding;
679 case USB_CDC_REQ_GET_LINE_CODING:
680 ret = min_t(int, wLength, sizeof(struct usb_cdc_line_coding));
681 spin_lock(&dev->dev_lock);
682 memcpy(req->buf, &dev->gser.port_line_coding, ret);
683 spin_unlock(&dev->dev_lock);
686 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
690 /* REVISIT: we currently just remember this data.
691 * If we change that, update whatever hardware needs
694 spin_lock(&dev->dev_lock);
695 dev->gser.port_handshake_bits = wValue;
696 spin_unlock(&dev->dev_lock);
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).
706 pr_err("gs_setup: unknown class request, "
707 "type=%02x, request=%02x, value=%04x, "
708 "index=%04x, length=%d\n",
709 ctrl->bRequestType, ctrl->bRequest,
710 wValue, wIndex, wLength);
720 static void gs_setup_complete(struct usb_ep *ep, struct usb_request *req)
722 if (req->status || req->actual != req->length) {
723 pr_err("gs_setup_complete: status error, status=%d, "
724 "actual=%d, length=%d\n",
725 req->status, req->actual, req->length);
732 * Implements all the control endpoint functionality that's not
733 * handled in hardware or the hardware driver.
735 * Returns the size of the data sent to the host, or a negative
738 static int gs_setup(struct usb_gadget *gadget,
739 const struct usb_ctrlrequest *ctrl)
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);
748 req->complete = gs_setup_complete;
750 switch (ctrl->bRequestType & USB_TYPE_MASK) {
751 case USB_TYPE_STANDARD:
752 ret = gs_setup_standard(gadget, ctrl);
756 ret = gs_setup_class(gadget, ctrl);
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);
767 /* respond with data transfer before status phase? */
770 req->zero = ret < wLength
771 && (ret % gadget->ep0->maxpacket) == 0;
772 ret = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
774 pr_err("gs_setup: cannot queue response, ret=%d\n",
777 gs_setup_complete(gadget->ep0, req);
781 /* device either stalls (ret < 0) or reports success */
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.
792 static void gs_disconnect(struct usb_gadget *gadget)
795 struct gs_dev *dev = get_gadget_data(gadget);
797 spin_lock_irqsave(&dev->dev_lock, flags);
798 gs_reset_config(dev);
799 spin_unlock_irqrestore(&dev->dev_lock, flags);
801 pr_info("gs_disconnect: %s disconnected\n", GS_LONG_NAME);
804 static struct usb_gadget_driver gs_gadget_driver = {
805 #ifdef CONFIG_USB_GADGET_DUALSPEED
806 .speed = USB_SPEED_HIGH,
808 .speed = USB_SPEED_FULL,
809 #endif /* CONFIG_USB_GADGET_DUALSPEED */
810 .function = GS_LONG_NAME,
814 .disconnect = gs_disconnect,
816 .name = GS_SHORT_NAME,
817 .owner = THIS_MODULE,
824 * Configures the device by enabling device specific
825 * optimizations, setting up the endpoints, allocating
826 * read and write requests and queuing read requests.
828 * The device lock must be held when calling this function.
830 static int gs_set_config(struct gs_dev *dev, unsigned config)
833 struct usb_gadget *gadget = dev->dev_gadget;
835 if (config == dev->dev_config)
838 gs_reset_config(dev);
841 case GS_NO_CONFIG_ID:
843 case GS_BULK_CONFIG_ID:
847 case GS_ACM_CONFIG_ID:
855 dev->gser.in_desc = choose_ep_desc(gadget,
856 &gs_highspeed_in_desc,
857 &gs_fullspeed_in_desc);
858 dev->gser.out_desc = choose_ep_desc(gadget,
859 &gs_highspeed_out_desc,
860 &gs_fullspeed_out_desc);
861 dev->gser.notify_desc = dev->gser.notify
862 ? choose_ep_desc(gadget,
863 &gs_highspeed_notify_desc,
864 &gs_fullspeed_notify_desc)
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);
872 dev->gser.notify->driver_data = dev;
875 ret = gserial_connect(&dev->gser, 0);
877 if (dev->gser.notify) {
878 usb_ep_disable(dev->gser.notify);
879 dev->gser.notify->driver_data = NULL;
884 dev->dev_config = config;
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.
891 pr_info("gs_set_config: %s configured, %s speed %s config\n",
893 gadget->speed == USB_SPEED_HIGH ? "high" : "full",
894 config == GS_BULK_CONFIG_ID ? "BULK" : "CDC-ACM");
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
907 * The device lock must be held when calling this function.
909 static void gs_reset_config(struct gs_dev *dev)
911 if (dev->dev_config == GS_NO_CONFIG_ID)
914 dev->dev_config = GS_NO_CONFIG_ID;
916 gserial_disconnect(&dev->gser);
917 if (dev->gser.notify) {
918 usb_ep_disable(dev->gser.notify);
919 dev->gser.notify->driver_data = NULL;
924 * gs_build_config_buf
926 * Builds the config descriptors in the given buffer and returns the
927 * length, or a negative error number.
929 static int gs_build_config_buf(u8 *buf, struct usb_gadget *g,
930 u8 type, unsigned int index, int is_otg)
934 const struct usb_config_descriptor *config_desc;
935 const struct usb_descriptor_header **function;
937 if (index >= gs_device_desc.bNumConfigurations)
940 /* other speed switches high and full speed */
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;
948 config_desc = &gs_acm_config_desc;
949 function = high_speed
950 ? gs_acm_highspeed_function
951 : gs_acm_fullspeed_function;
953 config_desc = &gs_bulk_config_desc;
954 function = high_speed
955 ? gs_bulk_highspeed_function
956 : gs_bulk_fullspeed_function;
959 /* for now, don't advertise srp-only devices */
963 len = usb_gadget_config_buf(config_desc, buf, GS_MAX_DESC_LEN, function);
967 ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
975 * Allocate a usb_request and its buffer. Returns a pointer to the
976 * usb_request or NULL if there is an error.
978 static struct usb_request *
979 gs_alloc_req(struct usb_ep *ep, unsigned int len, gfp_t kmalloc_flags)
981 struct usb_request *req;
986 req = usb_ep_alloc_request(ep, kmalloc_flags);
990 req->buf = kmalloc(len, kmalloc_flags);
991 if (req->buf == NULL) {
992 usb_ep_free_request(ep, req);
1003 * Free a usb_request and its buffer.
1005 static void gs_free_req(struct usb_ep *ep, struct usb_request *req)
1007 if (ep != NULL && req != NULL) {
1009 usb_ep_free_request(ep, req);
1013 /*-------------------------------------------------------------------------*/
1018 * Register as a USB gadget driver and a tty driver.
1020 static int __init gs_module_init(void)
1022 return usb_gadget_register_driver(&gs_gadget_driver);
1024 module_init(gs_module_init);
1029 * Unregister as a tty driver and a USB gadget driver.
1031 static void __exit gs_module_exit(void)
1033 usb_gadget_unregister_driver(&gs_gadget_driver);
1035 module_exit(gs_module_exit);