Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
serial.c
Go to the documentation of this file.
1 /*
2  * serial.c -- USB gadget serial driver
3  *
4  * Copyright (C) 2003 Al Borchers ([email protected])
5  * Copyright (C) 2008 by David Brownell
6  * Copyright (C) 2008 by Nokia Corporation
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.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 
18 #include "u_serial.h"
19 #include "gadget_chips.h"
20 
21 
22 /* Defines */
23 
24 #define GS_VERSION_STR "v2.4"
25 #define GS_VERSION_NUM 0x2400
26 
27 #define GS_LONG_NAME "Gadget Serial"
28 #define GS_VERSION_NAME GS_LONG_NAME " " GS_VERSION_STR
29 
30 /*-------------------------------------------------------------------------*/
31 
32 /*
33  * Kbuild is not very cooperative with respect to linking separately
34  * compiled library objects into one module. So for now we won't use
35  * separate compilation ... ensuring init/exit sections work to shrink
36  * the runtime footprint, and giving us at least some parts of what
37  * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38  */
39 #include "f_acm.c"
40 #include "f_obex.c"
41 #include "f_serial.c"
42 #include "u_serial.c"
43 
44 /*-------------------------------------------------------------------------*/
46 
47 /* Thanks to NetChip Technologies for donating this product ID.
48 *
49 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
50 * Instead: allocate your own, using normal USB-IF procedures.
51 */
52 #define GS_VENDOR_ID 0x0525 /* NetChip */
53 #define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
54 #define GS_CDC_PRODUCT_ID 0xa4a7 /* ... as CDC-ACM */
55 #define GS_CDC_OBEX_PRODUCT_ID 0xa4a9 /* ... as CDC-OBEX */
56 
57 /* string IDs are assigned dynamically */
58 
59 #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
60 
61 static struct usb_string strings_dev[] = {
64  [USB_GADGET_SERIAL_IDX].s = "",
65  [STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
66  { } /* end of list */
67 };
68 
69 static struct usb_gadget_strings stringtab_dev = {
70  .language = 0x0409, /* en-us */
71  .strings = strings_dev,
72 };
73 
74 static struct usb_gadget_strings *dev_strings[] = {
75  &stringtab_dev,
76  NULL,
77 };
78 
79 static struct usb_device_descriptor device_desc = {
80  .bLength = USB_DT_DEVICE_SIZE,
81  .bDescriptorType = USB_DT_DEVICE,
82  .bcdUSB = cpu_to_le16(0x0200),
83  /* .bDeviceClass = f(use_acm) */
84  .bDeviceSubClass = 0,
85  .bDeviceProtocol = 0,
86  /* .bMaxPacketSize0 = f(hardware) */
87  .idVendor = cpu_to_le16(GS_VENDOR_ID),
88  /* .idProduct = f(use_acm) */
89  .bcdDevice = cpu_to_le16(GS_VERSION_NUM),
90  /* .iManufacturer = DYNAMIC */
91  /* .iProduct = DYNAMIC */
92  .bNumConfigurations = 1,
93 };
94 
95 static struct usb_otg_descriptor otg_descriptor = {
96  .bLength = sizeof otg_descriptor,
98 
99  /* REVISIT SRP-only hardware is possible, although
100  * it would not be called "OTG" ...
101  */
102  .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
103 };
104 
105 static const struct usb_descriptor_header *otg_desc[] = {
106  (struct usb_descriptor_header *) &otg_descriptor,
107  NULL,
108 };
109 
110 /*-------------------------------------------------------------------------*/
111 
112 /* Module */
114 MODULE_AUTHOR("Al Borchers");
115 MODULE_AUTHOR("David Brownell");
116 MODULE_LICENSE("GPL");
117 
118 static bool use_acm = true;
119 module_param(use_acm, bool, 0);
120 MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
121 
122 static bool use_obex = false;
123 module_param(use_obex, bool, 0);
124 MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
125 
126 static unsigned n_ports = 1;
127 module_param(n_ports, uint, 0);
128 MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
129 
130 /*-------------------------------------------------------------------------*/
131 
132 static int __init serial_bind_config(struct usb_configuration *c)
133 {
134  unsigned i;
135  int status = 0;
136 
137  for (i = 0; i < n_ports && status == 0; i++) {
138  if (use_acm)
139  status = acm_bind_config(c, i);
140  else if (use_obex)
141  status = obex_bind_config(c, i);
142  else
143  status = gser_bind_config(c, i);
144  }
145  return status;
146 }
147 
148 static struct usb_configuration serial_config_driver = {
149  /* .label = f(use_acm) */
150  /* .bConfigurationValue = f(use_acm) */
151  /* .iConfiguration = DYNAMIC */
152  .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
153 };
154 
155 static int __init gs_bind(struct usb_composite_dev *cdev)
156 {
157  int status;
158 
159  status = gserial_setup(cdev->gadget, n_ports);
160  if (status < 0)
161  return status;
162 
163  /* Allocate string descriptor numbers ... note that string
164  * contents can be overridden by the composite_dev glue.
165  */
166 
167  status = usb_string_ids_tab(cdev, strings_dev);
168  if (status < 0)
169  goto fail;
170  device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
171  device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
172  status = strings_dev[STRING_DESCRIPTION_IDX].id;
173  serial_config_driver.iConfiguration = status;
174 
175  if (gadget_is_otg(cdev->gadget)) {
176  serial_config_driver.descriptors = otg_desc;
177  serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
178  }
179 
180  /* register our configuration */
181  status = usb_add_config(cdev, &serial_config_driver,
182  serial_bind_config);
183  if (status < 0)
184  goto fail;
185 
186  usb_composite_overwrite_options(cdev, &coverwrite);
187  INFO(cdev, "%s\n", GS_VERSION_NAME);
188 
189  return 0;
190 
191 fail:
192  gserial_cleanup();
193  return status;
194 }
195 
196 static __refdata struct usb_composite_driver gserial_driver = {
197  .name = "g_serial",
198  .dev = &device_desc,
199  .strings = dev_strings,
200  .max_speed = USB_SPEED_SUPER,
201  .bind = gs_bind,
202 };
203 
204 static int __init init(void)
205 {
206  /* We *could* export two configs; that'd be much cleaner...
207  * but neither of these product IDs was defined that way.
208  */
209  if (use_acm) {
210  serial_config_driver.label = "CDC ACM config";
211  serial_config_driver.bConfigurationValue = 2;
212  device_desc.bDeviceClass = USB_CLASS_COMM;
213  device_desc.idProduct =
215  } else if (use_obex) {
216  serial_config_driver.label = "CDC OBEX config";
217  serial_config_driver.bConfigurationValue = 3;
218  device_desc.bDeviceClass = USB_CLASS_COMM;
219  device_desc.idProduct =
221  } else {
222  serial_config_driver.label = "Generic Serial config";
223  serial_config_driver.bConfigurationValue = 1;
224  device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
225  device_desc.idProduct =
227  }
228  strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
229 
230  return usb_composite_probe(&gserial_driver);
231 }
233 
234 static void __exit cleanup(void)
235 {
236  usb_composite_unregister(&gserial_driver);
237  gserial_cleanup();
238 }
239 module_exit(cleanup);