1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/kernel.h>
27#include <linux/device.h>
28
29#include "u_serial.h"
30#include "gadget_chips.h"
31
32
33
34
35
36
37
38
39
40
41struct obex_ep_descs {
42 struct usb_endpoint_descriptor *obex_in;
43 struct usb_endpoint_descriptor *obex_out;
44};
45
46struct f_obex {
47 struct gserial port;
48 u8 ctrl_id;
49 u8 data_id;
50 u8 port_num;
51 u8 can_activate;
52
53 struct obex_ep_descs fs;
54 struct obex_ep_descs hs;
55};
56
57static inline struct f_obex *func_to_obex(struct usb_function *f)
58{
59 return container_of(f, struct f_obex, port.func);
60}
61
62static inline struct f_obex *port_to_obex(struct gserial *p)
63{
64 return container_of(p, struct f_obex, port);
65}
66
67
68
69#define OBEX_CTRL_IDX 0
70#define OBEX_DATA_IDX 1
71
72static struct usb_string obex_string_defs[] = {
73 [OBEX_CTRL_IDX].s = "CDC Object Exchange (OBEX)",
74 [OBEX_DATA_IDX].s = "CDC OBEX Data",
75 { },
76};
77
78static struct usb_gadget_strings obex_string_table = {
79 .language = 0x0409,
80 .strings = obex_string_defs,
81};
82
83static struct usb_gadget_strings *obex_strings[] = {
84 &obex_string_table,
85 NULL,
86};
87
88
89
90static struct usb_interface_descriptor obex_control_intf __initdata = {
91 .bLength = sizeof(obex_control_intf),
92 .bDescriptorType = USB_DT_INTERFACE,
93 .bInterfaceNumber = 0,
94
95 .bAlternateSetting = 0,
96 .bNumEndpoints = 0,
97 .bInterfaceClass = USB_CLASS_COMM,
98 .bInterfaceSubClass = USB_CDC_SUBCLASS_OBEX,
99};
100
101static struct usb_interface_descriptor obex_data_nop_intf __initdata = {
102 .bLength = sizeof(obex_data_nop_intf),
103 .bDescriptorType = USB_DT_INTERFACE,
104 .bInterfaceNumber = 1,
105
106 .bAlternateSetting = 0,
107 .bNumEndpoints = 0,
108 .bInterfaceClass = USB_CLASS_CDC_DATA,
109};
110
111static struct usb_interface_descriptor obex_data_intf __initdata = {
112 .bLength = sizeof(obex_data_intf),
113 .bDescriptorType = USB_DT_INTERFACE,
114 .bInterfaceNumber = 2,
115
116 .bAlternateSetting = 1,
117 .bNumEndpoints = 2,
118 .bInterfaceClass = USB_CLASS_CDC_DATA,
119};
120
121static struct usb_cdc_header_desc obex_cdc_header_desc __initdata = {
122 .bLength = sizeof(obex_cdc_header_desc),
123 .bDescriptorType = USB_DT_CS_INTERFACE,
124 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
125 .bcdCDC = cpu_to_le16(0x0120),
126};
127
128static struct usb_cdc_union_desc obex_cdc_union_desc __initdata = {
129 .bLength = sizeof(obex_cdc_union_desc),
130 .bDescriptorType = USB_DT_CS_INTERFACE,
131 .bDescriptorSubType = USB_CDC_UNION_TYPE,
132 .bMasterInterface0 = 1,
133 .bSlaveInterface0 = 2,
134};
135
136static struct usb_cdc_obex_desc obex_desc __initdata = {
137 .bLength = sizeof(obex_desc),
138 .bDescriptorType = USB_DT_CS_INTERFACE,
139 .bDescriptorSubType = USB_CDC_OBEX_TYPE,
140 .bcdVersion = cpu_to_le16(0x0100),
141};
142
143
144
145static struct usb_endpoint_descriptor obex_hs_ep_out_desc __initdata = {
146 .bLength = USB_DT_ENDPOINT_SIZE,
147 .bDescriptorType = USB_DT_ENDPOINT,
148
149 .bEndpointAddress = USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151 .wMaxPacketSize = cpu_to_le16(512),
152};
153
154static struct usb_endpoint_descriptor obex_hs_ep_in_desc __initdata = {
155 .bLength = USB_DT_ENDPOINT_SIZE,
156 .bDescriptorType = USB_DT_ENDPOINT,
157
158 .bEndpointAddress = USB_DIR_IN,
159 .bmAttributes = USB_ENDPOINT_XFER_BULK,
160 .wMaxPacketSize = cpu_to_le16(512),
161};
162
163static struct usb_descriptor_header *hs_function[] __initdata = {
164 (struct usb_descriptor_header *) &obex_control_intf,
165 (struct usb_descriptor_header *) &obex_cdc_header_desc,
166 (struct usb_descriptor_header *) &obex_desc,
167 (struct usb_descriptor_header *) &obex_cdc_union_desc,
168
169 (struct usb_descriptor_header *) &obex_data_nop_intf,
170 (struct usb_descriptor_header *) &obex_data_intf,
171 (struct usb_descriptor_header *) &obex_hs_ep_in_desc,
172 (struct usb_descriptor_header *) &obex_hs_ep_out_desc,
173 NULL,
174};
175
176
177
178static struct usb_endpoint_descriptor obex_fs_ep_in_desc __initdata = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181
182 .bEndpointAddress = USB_DIR_IN,
183 .bmAttributes = USB_ENDPOINT_XFER_BULK,
184};
185
186static struct usb_endpoint_descriptor obex_fs_ep_out_desc __initdata = {
187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
189
190 .bEndpointAddress = USB_DIR_OUT,
191 .bmAttributes = USB_ENDPOINT_XFER_BULK,
192};
193
194static struct usb_descriptor_header *fs_function[] __initdata = {
195 (struct usb_descriptor_header *) &obex_control_intf,
196 (struct usb_descriptor_header *) &obex_cdc_header_desc,
197 (struct usb_descriptor_header *) &obex_desc,
198 (struct usb_descriptor_header *) &obex_cdc_union_desc,
199
200 (struct usb_descriptor_header *) &obex_data_nop_intf,
201 (struct usb_descriptor_header *) &obex_data_intf,
202 (struct usb_descriptor_header *) &obex_fs_ep_in_desc,
203 (struct usb_descriptor_header *) &obex_fs_ep_out_desc,
204 NULL,
205};
206
207
208
209static int obex_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
210{
211 struct f_obex *obex = func_to_obex(f);
212 struct usb_composite_dev *cdev = f->config->cdev;
213
214 if (intf == obex->ctrl_id) {
215 if (alt != 0)
216 goto fail;
217
218 DBG(cdev, "reset obex ttyGS%d control\n", obex->port_num);
219
220 } else if (intf == obex->data_id) {
221 if (alt > 1)
222 goto fail;
223
224 if (obex->port.in->driver_data) {
225 DBG(cdev, "reset obex ttyGS%d\n", obex->port_num);
226 gserial_disconnect(&obex->port);
227 }
228
229 if (!obex->port.in_desc) {
230 DBG(cdev, "init obex ttyGS%d\n", obex->port_num);
231 obex->port.in_desc = ep_choose(cdev->gadget,
232 obex->hs.obex_in, obex->fs.obex_in);
233 obex->port.out_desc = ep_choose(cdev->gadget,
234 obex->hs.obex_out, obex->fs.obex_out);
235 }
236
237 if (alt == 1) {
238 DBG(cdev, "activate obex ttyGS%d\n", obex->port_num);
239 gserial_connect(&obex->port, obex->port_num);
240 }
241
242 } else
243 goto fail;
244
245 return 0;
246
247fail:
248 return -EINVAL;
249}
250
251static int obex_get_alt(struct usb_function *f, unsigned intf)
252{
253 struct f_obex *obex = func_to_obex(f);
254
255 if (intf == obex->ctrl_id)
256 return 0;
257
258 return obex->port.in->driver_data ? 1 : 0;
259}
260
261static void obex_disable(struct usb_function *f)
262{
263 struct f_obex *obex = func_to_obex(f);
264 struct usb_composite_dev *cdev = f->config->cdev;
265
266 DBG(cdev, "obex ttyGS%d disable\n", obex->port_num);
267 gserial_disconnect(&obex->port);
268}
269
270
271
272static void obex_connect(struct gserial *g)
273{
274 struct f_obex *obex = port_to_obex(g);
275 struct usb_composite_dev *cdev = g->func.config->cdev;
276 int status;
277
278 if (!obex->can_activate)
279 return;
280
281 status = usb_function_activate(&g->func);
282 if (status)
283 DBG(cdev, "obex ttyGS%d function activate --> %d\n",
284 obex->port_num, status);
285}
286
287static void obex_disconnect(struct gserial *g)
288{
289 struct f_obex *obex = port_to_obex(g);
290 struct usb_composite_dev *cdev = g->func.config->cdev;
291 int status;
292
293 if (!obex->can_activate)
294 return;
295
296 status = usb_function_deactivate(&g->func);
297 if (status)
298 DBG(cdev, "obex ttyGS%d function deactivate --> %d\n",
299 obex->port_num, status);
300}
301
302
303
304static int __init
305obex_bind(struct usb_configuration *c, struct usb_function *f)
306{
307 struct usb_composite_dev *cdev = c->cdev;
308 struct f_obex *obex = func_to_obex(f);
309 int status;
310 struct usb_ep *ep;
311
312
313
314 status = usb_interface_id(c, f);
315 if (status < 0)
316 goto fail;
317 obex->ctrl_id = status;
318
319 obex_control_intf.bInterfaceNumber = status;
320 obex_cdc_union_desc.bMasterInterface0 = status;
321
322 status = usb_interface_id(c, f);
323 if (status < 0)
324 goto fail;
325 obex->data_id = status;
326
327 obex_data_nop_intf.bInterfaceNumber = status;
328 obex_data_intf.bInterfaceNumber = status;
329 obex_cdc_union_desc.bSlaveInterface0 = status;
330
331
332
333 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_in_desc);
334 if (!ep)
335 goto fail;
336 obex->port.in = ep;
337 ep->driver_data = cdev;
338
339 ep = usb_ep_autoconfig(cdev->gadget, &obex_fs_ep_out_desc);
340 if (!ep)
341 goto fail;
342 obex->port.out = ep;
343 ep->driver_data = cdev;
344
345
346 f->descriptors = usb_copy_descriptors(fs_function);
347
348 obex->fs.obex_in = usb_find_endpoint(fs_function,
349 f->descriptors, &obex_fs_ep_in_desc);
350 obex->fs.obex_out = usb_find_endpoint(fs_function,
351 f->descriptors, &obex_fs_ep_out_desc);
352
353
354
355
356
357 if (gadget_is_dualspeed(c->cdev->gadget)) {
358
359 obex_hs_ep_in_desc.bEndpointAddress =
360 obex_fs_ep_in_desc.bEndpointAddress;
361 obex_hs_ep_out_desc.bEndpointAddress =
362 obex_fs_ep_out_desc.bEndpointAddress;
363
364
365 f->hs_descriptors = usb_copy_descriptors(hs_function);
366
367 obex->hs.obex_in = usb_find_endpoint(hs_function,
368 f->hs_descriptors, &obex_hs_ep_in_desc);
369 obex->hs.obex_out = usb_find_endpoint(hs_function,
370 f->hs_descriptors, &obex_hs_ep_out_desc);
371 }
372
373
374
375
376 status = usb_function_deactivate(f);
377 if (status < 0)
378 WARNING(cdev, "obex ttyGS%d: can't prevent enumeration, %d\n",
379 obex->port_num, status);
380 else
381 obex->can_activate = true;
382
383
384 DBG(cdev, "obex ttyGS%d: %s speed IN/%s OUT/%s\n",
385 obex->port_num,
386 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
387 obex->port.in->name, obex->port.out->name);
388
389 return 0;
390
391fail:
392
393 if (obex->port.out)
394 obex->port.out->driver_data = NULL;
395 if (obex->port.in)
396 obex->port.in->driver_data = NULL;
397
398 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
399
400 return status;
401}
402
403static void
404obex_unbind(struct usb_configuration *c, struct usb_function *f)
405{
406 if (gadget_is_dualspeed(c->cdev->gadget))
407 usb_free_descriptors(f->hs_descriptors);
408 usb_free_descriptors(f->descriptors);
409 kfree(func_to_obex(f));
410}
411
412
413static inline bool can_support_obex(struct usb_configuration *c)
414{
415
416
417
418
419
420 if (!gadget_supports_altsettings(c->cdev->gadget))
421 return false;
422
423
424 return true;
425}
426
427
428
429
430
431
432
433
434
435
436
437
438
439int __init obex_bind_config(struct usb_configuration *c, u8 port_num)
440{
441 struct f_obex *obex;
442 int status;
443
444 if (!can_support_obex(c))
445 return -EINVAL;
446
447
448 if (obex_string_defs[OBEX_CTRL_IDX].id == 0) {
449 status = usb_string_id(c->cdev);
450 if (status < 0)
451 return status;
452 obex_string_defs[OBEX_CTRL_IDX].id = status;
453
454 obex_control_intf.iInterface = status;
455
456 status = usb_string_id(c->cdev);
457 if (status < 0)
458 return status;
459 obex_string_defs[OBEX_DATA_IDX].id = status;
460
461 obex_data_nop_intf.iInterface =
462 obex_data_intf.iInterface = status;
463 }
464
465
466 obex = kzalloc(sizeof *obex, GFP_KERNEL);
467 if (!obex)
468 return -ENOMEM;
469
470 obex->port_num = port_num;
471
472 obex->port.connect = obex_connect;
473 obex->port.disconnect = obex_disconnect;
474
475 obex->port.func.name = "obex";
476 obex->port.func.strings = obex_strings;
477
478 obex->port.func.bind = obex_bind;
479 obex->port.func.unbind = obex_unbind;
480 obex->port.func.set_alt = obex_set_alt;
481 obex->port.func.get_alt = obex_get_alt;
482 obex->port.func.disable = obex_disable;
483
484 status = usb_add_function(c, &obex->port.func);
485 if (status)
486 kfree(obex);
487
488 return status;
489}
490
491MODULE_AUTHOR("Felipe Balbi");
492MODULE_LICENSE("GPL");
493