1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/device.h>
15
16#include "u_serial.h"
17#include "gadget_chips.h"
18
19
20
21
22
23
24
25
26
27
28
29struct gser_descs {
30 struct usb_endpoint_descriptor *in;
31 struct usb_endpoint_descriptor *out;
32};
33
34struct f_gser {
35 struct gserial port;
36 u8 data_id;
37 u8 port_num;
38
39 struct gser_descs fs;
40 struct gser_descs hs;
41};
42
43static inline struct f_gser *func_to_gser(struct usb_function *f)
44{
45 return container_of(f, struct f_gser, port.func);
46}
47
48
49
50
51
52static struct usb_interface_descriptor gser_interface_desc __initdata = {
53 .bLength = USB_DT_INTERFACE_SIZE,
54 .bDescriptorType = USB_DT_INTERFACE,
55
56 .bNumEndpoints = 2,
57 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
58 .bInterfaceSubClass = 0,
59 .bInterfaceProtocol = 0,
60
61};
62
63
64
65static struct usb_endpoint_descriptor gser_fs_in_desc __initdata = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
68 .bEndpointAddress = USB_DIR_IN,
69 .bmAttributes = USB_ENDPOINT_XFER_BULK,
70};
71
72static struct usb_endpoint_descriptor gser_fs_out_desc __initdata = {
73 .bLength = USB_DT_ENDPOINT_SIZE,
74 .bDescriptorType = USB_DT_ENDPOINT,
75 .bEndpointAddress = USB_DIR_OUT,
76 .bmAttributes = USB_ENDPOINT_XFER_BULK,
77};
78
79static struct usb_descriptor_header *gser_fs_function[] __initdata = {
80 (struct usb_descriptor_header *) &gser_interface_desc,
81 (struct usb_descriptor_header *) &gser_fs_in_desc,
82 (struct usb_descriptor_header *) &gser_fs_out_desc,
83 NULL,
84};
85
86
87
88static struct usb_endpoint_descriptor gser_hs_in_desc __initdata = {
89 .bLength = USB_DT_ENDPOINT_SIZE,
90 .bDescriptorType = USB_DT_ENDPOINT,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = cpu_to_le16(512),
93};
94
95static struct usb_endpoint_descriptor gser_hs_out_desc __initdata = {
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bmAttributes = USB_ENDPOINT_XFER_BULK,
99 .wMaxPacketSize = cpu_to_le16(512),
100};
101
102static struct usb_descriptor_header *gser_hs_function[] __initdata = {
103 (struct usb_descriptor_header *) &gser_interface_desc,
104 (struct usb_descriptor_header *) &gser_hs_in_desc,
105 (struct usb_descriptor_header *) &gser_hs_out_desc,
106 NULL,
107};
108
109
110
111static struct usb_string gser_string_defs[] = {
112 [0].s = "Generic Serial",
113 { }
114};
115
116static struct usb_gadget_strings gser_string_table = {
117 .language = 0x0409,
118 .strings = gser_string_defs,
119};
120
121static struct usb_gadget_strings *gser_strings[] = {
122 &gser_string_table,
123 NULL,
124};
125
126
127
128static int gser_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
129{
130 struct f_gser *gser = func_to_gser(f);
131 struct usb_composite_dev *cdev = f->config->cdev;
132
133
134
135 if (gser->port.in->driver_data) {
136 DBG(cdev, "reset generic ttyGS%d\n", gser->port_num);
137 gserial_disconnect(&gser->port);
138 } else {
139 DBG(cdev, "activate generic ttyGS%d\n", gser->port_num);
140 gser->port.in_desc = ep_choose(cdev->gadget,
141 gser->hs.in, gser->fs.in);
142 gser->port.out_desc = ep_choose(cdev->gadget,
143 gser->hs.out, gser->fs.out);
144 }
145 gserial_connect(&gser->port, gser->port_num);
146 return 0;
147}
148
149static void gser_disable(struct usb_function *f)
150{
151 struct f_gser *gser = func_to_gser(f);
152 struct usb_composite_dev *cdev = f->config->cdev;
153
154 DBG(cdev, "generic ttyGS%d deactivated\n", gser->port_num);
155 gserial_disconnect(&gser->port);
156}
157
158
159
160
161
162static int __init
163gser_bind(struct usb_configuration *c, struct usb_function *f)
164{
165 struct usb_composite_dev *cdev = c->cdev;
166 struct f_gser *gser = func_to_gser(f);
167 int status;
168 struct usb_ep *ep;
169
170
171 status = usb_interface_id(c, f);
172 if (status < 0)
173 goto fail;
174 gser->data_id = status;
175 gser_interface_desc.bInterfaceNumber = status;
176
177 status = -ENODEV;
178
179
180 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_in_desc);
181 if (!ep)
182 goto fail;
183 gser->port.in = ep;
184 ep->driver_data = cdev;
185
186 ep = usb_ep_autoconfig(cdev->gadget, &gser_fs_out_desc);
187 if (!ep)
188 goto fail;
189 gser->port.out = ep;
190 ep->driver_data = cdev;
191
192
193 f->descriptors = usb_copy_descriptors(gser_fs_function);
194
195 gser->fs.in = usb_find_endpoint(gser_fs_function,
196 f->descriptors, &gser_fs_in_desc);
197 gser->fs.out = usb_find_endpoint(gser_fs_function,
198 f->descriptors, &gser_fs_out_desc);
199
200
201
202
203
204
205 if (gadget_is_dualspeed(c->cdev->gadget)) {
206 gser_hs_in_desc.bEndpointAddress =
207 gser_fs_in_desc.bEndpointAddress;
208 gser_hs_out_desc.bEndpointAddress =
209 gser_fs_out_desc.bEndpointAddress;
210
211
212 f->hs_descriptors = usb_copy_descriptors(gser_hs_function);
213
214 gser->hs.in = usb_find_endpoint(gser_hs_function,
215 f->hs_descriptors, &gser_hs_in_desc);
216 gser->hs.out = usb_find_endpoint(gser_hs_function,
217 f->hs_descriptors, &gser_hs_out_desc);
218 }
219
220 DBG(cdev, "generic ttyGS%d: %s speed IN/%s OUT/%s\n",
221 gser->port_num,
222 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
223 gser->port.in->name, gser->port.out->name);
224 return 0;
225
226fail:
227
228 if (gser->port.out)
229 gser->port.out->driver_data = NULL;
230 if (gser->port.in)
231 gser->port.in->driver_data = NULL;
232
233 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
234
235 return status;
236}
237
238static void
239gser_unbind(struct usb_configuration *c, struct usb_function *f)
240{
241 if (gadget_is_dualspeed(c->cdev->gadget))
242 usb_free_descriptors(f->hs_descriptors);
243 usb_free_descriptors(f->descriptors);
244 kfree(func_to_gser(f));
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259int __init gser_bind_config(struct usb_configuration *c, u8 port_num)
260{
261 struct f_gser *gser;
262 int status;
263
264
265
266
267
268
269 if (gser_string_defs[0].id == 0) {
270 status = usb_string_id(c->cdev);
271 if (status < 0)
272 return status;
273 gser_string_defs[0].id = status;
274 }
275
276
277 gser = kzalloc(sizeof *gser, GFP_KERNEL);
278 if (!gser)
279 return -ENOMEM;
280
281 gser->port_num = port_num;
282
283 gser->port.func.name = "gser";
284 gser->port.func.strings = gser_strings;
285 gser->port.func.bind = gser_bind;
286 gser->port.func.unbind = gser_unbind;
287 gser->port.func.set_alt = gser_set_alt;
288 gser->port.func.disable = gser_disable;
289
290 status = usb_add_function(c, &gser->port.func);
291 if (status)
292 kfree(gser);
293 return status;
294}
295