1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/device.h>
17
18#include "u_serial.h"
19#include "gadget_chips.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39struct acm_ep_descs {
40 struct usb_endpoint_descriptor *in;
41 struct usb_endpoint_descriptor *out;
42 struct usb_endpoint_descriptor *notify;
43};
44
45struct f_acm {
46 struct gserial port;
47 u8 ctrl_id, data_id;
48 u8 port_num;
49
50 u8 pending;
51
52
53
54
55
56 spinlock_t lock;
57
58 struct acm_ep_descs fs;
59 struct acm_ep_descs hs;
60
61 struct usb_ep *notify;
62 struct usb_endpoint_descriptor *notify_desc;
63 struct usb_request *notify_req;
64
65 struct usb_cdc_line_coding port_line_coding;
66
67
68 u16 port_handshake_bits;
69#define ACM_CTRL_RTS (1 << 1)
70#define ACM_CTRL_DTR (1 << 0)
71
72
73 u16 serial_state;
74#define ACM_CTRL_OVERRUN (1 << 6)
75#define ACM_CTRL_PARITY (1 << 5)
76#define ACM_CTRL_FRAMING (1 << 4)
77#define ACM_CTRL_RI (1 << 3)
78#define ACM_CTRL_BRK (1 << 2)
79#define ACM_CTRL_DSR (1 << 1)
80#define ACM_CTRL_DCD (1 << 0)
81};
82
83static inline struct f_acm *func_to_acm(struct usb_function *f)
84{
85 return container_of(f, struct f_acm, port.func);
86}
87
88static inline struct f_acm *port_to_acm(struct gserial *p)
89{
90 return container_of(p, struct f_acm, port);
91}
92
93
94
95
96
97#define GS_LOG2_NOTIFY_INTERVAL 5
98#define GS_NOTIFY_MAXPACKET 10
99
100
101
102static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
103 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE,
105
106 .bNumEndpoints = 1,
107 .bInterfaceClass = USB_CLASS_COMM,
108 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
109 .bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
110
111};
112
113static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
114 .bLength = USB_DT_INTERFACE_SIZE,
115 .bDescriptorType = USB_DT_INTERFACE,
116
117 .bNumEndpoints = 2,
118 .bInterfaceClass = USB_CLASS_CDC_DATA,
119 .bInterfaceSubClass = 0,
120 .bInterfaceProtocol = 0,
121
122};
123
124static struct usb_cdc_header_desc acm_header_desc __initdata = {
125 .bLength = sizeof(acm_header_desc),
126 .bDescriptorType = USB_DT_CS_INTERFACE,
127 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
128 .bcdCDC = cpu_to_le16(0x0110),
129};
130
131static struct usb_cdc_call_mgmt_descriptor
132acm_call_mgmt_descriptor __initdata = {
133 .bLength = sizeof(acm_call_mgmt_descriptor),
134 .bDescriptorType = USB_DT_CS_INTERFACE,
135 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
136 .bmCapabilities = 0,
137
138};
139
140static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
141 .bLength = sizeof(acm_descriptor),
142 .bDescriptorType = USB_DT_CS_INTERFACE,
143 .bDescriptorSubType = USB_CDC_ACM_TYPE,
144 .bmCapabilities = USB_CDC_CAP_LINE,
145};
146
147static struct usb_cdc_union_desc acm_union_desc __initdata = {
148 .bLength = sizeof(acm_union_desc),
149 .bDescriptorType = USB_DT_CS_INTERFACE,
150 .bDescriptorSubType = USB_CDC_UNION_TYPE,
151
152
153};
154
155
156
157static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
158 .bLength = USB_DT_ENDPOINT_SIZE,
159 .bDescriptorType = USB_DT_ENDPOINT,
160 .bEndpointAddress = USB_DIR_IN,
161 .bmAttributes = USB_ENDPOINT_XFER_INT,
162 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
163 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
164};
165
166static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = {
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169 .bEndpointAddress = USB_DIR_IN,
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171};
172
173static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = {
174 .bLength = USB_DT_ENDPOINT_SIZE,
175 .bDescriptorType = USB_DT_ENDPOINT,
176 .bEndpointAddress = USB_DIR_OUT,
177 .bmAttributes = USB_ENDPOINT_XFER_BULK,
178};
179
180static struct usb_descriptor_header *acm_fs_function[] __initdata = {
181 (struct usb_descriptor_header *) &acm_control_interface_desc,
182 (struct usb_descriptor_header *) &acm_header_desc,
183 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
184 (struct usb_descriptor_header *) &acm_descriptor,
185 (struct usb_descriptor_header *) &acm_union_desc,
186 (struct usb_descriptor_header *) &acm_fs_notify_desc,
187 (struct usb_descriptor_header *) &acm_data_interface_desc,
188 (struct usb_descriptor_header *) &acm_fs_in_desc,
189 (struct usb_descriptor_header *) &acm_fs_out_desc,
190 NULL,
191};
192
193
194
195static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198 .bEndpointAddress = USB_DIR_IN,
199 .bmAttributes = USB_ENDPOINT_XFER_INT,
200 .wMaxPacketSize = cpu_to_le16(GS_NOTIFY_MAXPACKET),
201 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
202};
203
204static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = {
205 .bLength = USB_DT_ENDPOINT_SIZE,
206 .bDescriptorType = USB_DT_ENDPOINT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208 .wMaxPacketSize = cpu_to_le16(512),
209};
210
211static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = {
212 .bLength = USB_DT_ENDPOINT_SIZE,
213 .bDescriptorType = USB_DT_ENDPOINT,
214 .bmAttributes = USB_ENDPOINT_XFER_BULK,
215 .wMaxPacketSize = cpu_to_le16(512),
216};
217
218static struct usb_descriptor_header *acm_hs_function[] __initdata = {
219 (struct usb_descriptor_header *) &acm_control_interface_desc,
220 (struct usb_descriptor_header *) &acm_header_desc,
221 (struct usb_descriptor_header *) &acm_call_mgmt_descriptor,
222 (struct usb_descriptor_header *) &acm_descriptor,
223 (struct usb_descriptor_header *) &acm_union_desc,
224 (struct usb_descriptor_header *) &acm_hs_notify_desc,
225 (struct usb_descriptor_header *) &acm_data_interface_desc,
226 (struct usb_descriptor_header *) &acm_hs_in_desc,
227 (struct usb_descriptor_header *) &acm_hs_out_desc,
228 NULL,
229};
230
231
232
233#define ACM_CTRL_IDX 0
234#define ACM_DATA_IDX 1
235
236
237static struct usb_string acm_string_defs[] = {
238 [ACM_CTRL_IDX].s = "CDC Abstract Control Model (ACM)",
239 [ACM_DATA_IDX].s = "CDC ACM Data",
240 { },
241};
242
243static struct usb_gadget_strings acm_string_table = {
244 .language = 0x0409,
245 .strings = acm_string_defs,
246};
247
248static struct usb_gadget_strings *acm_strings[] = {
249 &acm_string_table,
250 NULL,
251};
252
253
254
255
256
257
258
259
260
261static void acm_complete_set_line_coding(struct usb_ep *ep,
262 struct usb_request *req)
263{
264 struct f_acm *acm = ep->driver_data;
265 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
266
267 if (req->status != 0) {
268 DBG(cdev, "acm ttyGS%d completion, err %d\n",
269 acm->port_num, req->status);
270 return;
271 }
272
273
274 if (req->actual != sizeof(acm->port_line_coding)) {
275 DBG(cdev, "acm ttyGS%d short resp, len %d\n",
276 acm->port_num, req->actual);
277 usb_ep_set_halt(ep);
278 } else {
279 struct usb_cdc_line_coding *value = req->buf;
280
281
282
283
284
285
286
287
288 acm->port_line_coding = *value;
289 }
290}
291
292static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
293{
294 struct f_acm *acm = func_to_acm(f);
295 struct usb_composite_dev *cdev = f->config->cdev;
296 struct usb_request *req = cdev->req;
297 int value = -EOPNOTSUPP;
298 u16 w_index = le16_to_cpu(ctrl->wIndex);
299 u16 w_value = le16_to_cpu(ctrl->wValue);
300 u16 w_length = le16_to_cpu(ctrl->wLength);
301
302
303
304
305
306
307
308
309
310 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
311
312
313 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
314 | USB_CDC_REQ_SET_LINE_CODING:
315 if (w_length != sizeof(struct usb_cdc_line_coding)
316 || w_index != acm->ctrl_id)
317 goto invalid;
318
319 value = w_length;
320 cdev->gadget->ep0->driver_data = acm;
321 req->complete = acm_complete_set_line_coding;
322 break;
323
324
325 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
326 | USB_CDC_REQ_GET_LINE_CODING:
327 if (w_index != acm->ctrl_id)
328 goto invalid;
329
330 value = min_t(unsigned, w_length,
331 sizeof(struct usb_cdc_line_coding));
332 memcpy(req->buf, &acm->port_line_coding, value);
333 break;
334
335
336 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
337 | USB_CDC_REQ_SET_CONTROL_LINE_STATE:
338 if (w_index != acm->ctrl_id)
339 goto invalid;
340
341 value = 0;
342
343
344
345
346
347 acm->port_handshake_bits = w_value;
348 break;
349
350 default:
351invalid:
352 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
353 ctrl->bRequestType, ctrl->bRequest,
354 w_value, w_index, w_length);
355 }
356
357
358 if (value >= 0) {
359 DBG(cdev, "acm ttyGS%d req%02x.%02x v%04x i%04x l%d\n",
360 acm->port_num, ctrl->bRequestType, ctrl->bRequest,
361 w_value, w_index, w_length);
362 req->zero = 0;
363 req->length = value;
364 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
365 if (value < 0)
366 ERROR(cdev, "acm response on ttyGS%d, err %d\n",
367 acm->port_num, value);
368 }
369
370
371 return value;
372}
373
374static int acm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
375{
376 struct f_acm *acm = func_to_acm(f);
377 struct usb_composite_dev *cdev = f->config->cdev;
378
379
380
381 if (intf == acm->ctrl_id) {
382 if (acm->notify->driver_data) {
383 VDBG(cdev, "reset acm control interface %d\n", intf);
384 usb_ep_disable(acm->notify);
385 } else {
386 VDBG(cdev, "init acm ctrl interface %d\n", intf);
387 acm->notify_desc = ep_choose(cdev->gadget,
388 acm->hs.notify,
389 acm->fs.notify);
390 }
391 usb_ep_enable(acm->notify, acm->notify_desc);
392 acm->notify->driver_data = acm;
393
394 } else if (intf == acm->data_id) {
395 if (acm->port.in->driver_data) {
396 DBG(cdev, "reset acm ttyGS%d\n", acm->port_num);
397 gserial_disconnect(&acm->port);
398 } else {
399 DBG(cdev, "activate acm ttyGS%d\n", acm->port_num);
400 acm->port.in_desc = ep_choose(cdev->gadget,
401 acm->hs.in, acm->fs.in);
402 acm->port.out_desc = ep_choose(cdev->gadget,
403 acm->hs.out, acm->fs.out);
404 }
405 gserial_connect(&acm->port, acm->port_num);
406
407 } else
408 return -EINVAL;
409
410 return 0;
411}
412
413static void acm_disable(struct usb_function *f)
414{
415 struct f_acm *acm = func_to_acm(f);
416 struct usb_composite_dev *cdev = f->config->cdev;
417
418 DBG(cdev, "acm ttyGS%d deactivated\n", acm->port_num);
419 gserial_disconnect(&acm->port);
420 usb_ep_disable(acm->notify);
421 acm->notify->driver_data = NULL;
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440static int acm_cdc_notify(struct f_acm *acm, u8 type, u16 value,
441 void *data, unsigned length)
442{
443 struct usb_ep *ep = acm->notify;
444 struct usb_request *req;
445 struct usb_cdc_notification *notify;
446 const unsigned len = sizeof(*notify) + length;
447 void *buf;
448 int status;
449
450 req = acm->notify_req;
451 acm->notify_req = NULL;
452 acm->pending = false;
453
454 req->length = len;
455 notify = req->buf;
456 buf = notify + 1;
457
458 notify->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
459 | USB_RECIP_INTERFACE;
460 notify->bNotificationType = type;
461 notify->wValue = cpu_to_le16(value);
462 notify->wIndex = cpu_to_le16(acm->ctrl_id);
463 notify->wLength = cpu_to_le16(length);
464 memcpy(buf, data, length);
465
466
467 spin_unlock(&acm->lock);
468 status = usb_ep_queue(ep, req, GFP_ATOMIC);
469 spin_lock(&acm->lock);
470
471 if (status < 0) {
472 ERROR(acm->port.func.config->cdev,
473 "acm ttyGS%d can't notify serial state, %d\n",
474 acm->port_num, status);
475 acm->notify_req = req;
476 }
477
478 return status;
479}
480
481static int acm_notify_serial_state(struct f_acm *acm)
482{
483 struct usb_composite_dev *cdev = acm->port.func.config->cdev;
484 int status;
485
486 spin_lock(&acm->lock);
487 if (acm->notify_req) {
488 DBG(cdev, "acm ttyGS%d serial state %04x\n",
489 acm->port_num, acm->serial_state);
490 status = acm_cdc_notify(acm, USB_CDC_NOTIFY_SERIAL_STATE,
491 0, &acm->serial_state, sizeof(acm->serial_state));
492 } else {
493 acm->pending = true;
494 status = 0;
495 }
496 spin_unlock(&acm->lock);
497 return status;
498}
499
500static void acm_cdc_notify_complete(struct usb_ep *ep, struct usb_request *req)
501{
502 struct f_acm *acm = req->context;
503 u8 doit = false;
504
505
506
507
508 spin_lock(&acm->lock);
509 if (req->status != -ESHUTDOWN)
510 doit = acm->pending;
511 acm->notify_req = req;
512 spin_unlock(&acm->lock);
513
514 if (doit)
515 acm_notify_serial_state(acm);
516}
517
518
519
520static void acm_connect(struct gserial *port)
521{
522 struct f_acm *acm = port_to_acm(port);
523
524 acm->serial_state |= ACM_CTRL_DSR | ACM_CTRL_DCD;
525 acm_notify_serial_state(acm);
526}
527
528static void acm_disconnect(struct gserial *port)
529{
530 struct f_acm *acm = port_to_acm(port);
531
532 acm->serial_state &= ~(ACM_CTRL_DSR | ACM_CTRL_DCD);
533 acm_notify_serial_state(acm);
534}
535
536static int acm_send_break(struct gserial *port, int duration)
537{
538 struct f_acm *acm = port_to_acm(port);
539 u16 state;
540
541 state = acm->serial_state;
542 state &= ~ACM_CTRL_BRK;
543 if (duration)
544 state |= ACM_CTRL_BRK;
545
546 acm->serial_state = state;
547 return acm_notify_serial_state(acm);
548}
549
550
551
552
553static int __init
554acm_bind(struct usb_configuration *c, struct usb_function *f)
555{
556 struct usb_composite_dev *cdev = c->cdev;
557 struct f_acm *acm = func_to_acm(f);
558 int status;
559 struct usb_ep *ep;
560
561
562 status = usb_interface_id(c, f);
563 if (status < 0)
564 goto fail;
565 acm->ctrl_id = status;
566
567 acm_control_interface_desc.bInterfaceNumber = status;
568 acm_union_desc .bMasterInterface0 = status;
569
570 status = usb_interface_id(c, f);
571 if (status < 0)
572 goto fail;
573 acm->data_id = status;
574
575 acm_data_interface_desc.bInterfaceNumber = status;
576 acm_union_desc.bSlaveInterface0 = status;
577 acm_call_mgmt_descriptor.bDataInterface = status;
578
579 status = -ENODEV;
580
581
582 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
583 if (!ep)
584 goto fail;
585 acm->port.in = ep;
586 ep->driver_data = cdev;
587
588 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
589 if (!ep)
590 goto fail;
591 acm->port.out = ep;
592 ep->driver_data = cdev;
593
594 ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
595 if (!ep)
596 goto fail;
597 acm->notify = ep;
598 ep->driver_data = cdev;
599
600
601 acm->notify_req = gs_alloc_req(ep,
602 sizeof(struct usb_cdc_notification) + 2,
603 GFP_KERNEL);
604 if (!acm->notify_req)
605 goto fail;
606
607 acm->notify_req->complete = acm_cdc_notify_complete;
608 acm->notify_req->context = acm;
609
610
611 f->descriptors = usb_copy_descriptors(acm_fs_function);
612 if (!f->descriptors)
613 goto fail;
614
615 acm->fs.in = usb_find_endpoint(acm_fs_function,
616 f->descriptors, &acm_fs_in_desc);
617 acm->fs.out = usb_find_endpoint(acm_fs_function,
618 f->descriptors, &acm_fs_out_desc);
619 acm->fs.notify = usb_find_endpoint(acm_fs_function,
620 f->descriptors, &acm_fs_notify_desc);
621
622
623
624
625
626 if (gadget_is_dualspeed(c->cdev->gadget)) {
627 acm_hs_in_desc.bEndpointAddress =
628 acm_fs_in_desc.bEndpointAddress;
629 acm_hs_out_desc.bEndpointAddress =
630 acm_fs_out_desc.bEndpointAddress;
631 acm_hs_notify_desc.bEndpointAddress =
632 acm_fs_notify_desc.bEndpointAddress;
633
634
635 f->hs_descriptors = usb_copy_descriptors(acm_hs_function);
636
637 acm->hs.in = usb_find_endpoint(acm_hs_function,
638 f->hs_descriptors, &acm_hs_in_desc);
639 acm->hs.out = usb_find_endpoint(acm_hs_function,
640 f->hs_descriptors, &acm_hs_out_desc);
641 acm->hs.notify = usb_find_endpoint(acm_hs_function,
642 f->hs_descriptors, &acm_hs_notify_desc);
643 }
644
645 DBG(cdev, "acm ttyGS%d: %s speed IN/%s OUT/%s NOTIFY/%s\n",
646 acm->port_num,
647 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
648 acm->port.in->name, acm->port.out->name,
649 acm->notify->name);
650 return 0;
651
652fail:
653 if (acm->notify_req)
654 gs_free_req(acm->notify, acm->notify_req);
655
656
657 if (acm->notify)
658 acm->notify->driver_data = NULL;
659 if (acm->port.out)
660 acm->port.out->driver_data = NULL;
661 if (acm->port.in)
662 acm->port.in->driver_data = NULL;
663
664 ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
665
666 return status;
667}
668
669static void
670acm_unbind(struct usb_configuration *c, struct usb_function *f)
671{
672 struct f_acm *acm = func_to_acm(f);
673
674 if (gadget_is_dualspeed(c->cdev->gadget))
675 usb_free_descriptors(f->hs_descriptors);
676 usb_free_descriptors(f->descriptors);
677 gs_free_req(acm->notify, acm->notify_req);
678 kfree(acm);
679}
680
681
682static inline bool can_support_cdc(struct usb_configuration *c)
683{
684
685 if (gadget_is_sh(c->cdev->gadget))
686 return false;
687
688
689 if (gadget_is_sa1100(c->cdev->gadget))
690 return false;
691
692
693 return true;
694}
695
696
697
698
699
700
701
702
703
704
705
706
707
708int __init acm_bind_config(struct usb_configuration *c, u8 port_num)
709{
710 struct f_acm *acm;
711 int status;
712
713 if (!can_support_cdc(c))
714 return -EINVAL;
715
716
717
718
719
720
721 if (acm_string_defs[ACM_CTRL_IDX].id == 0) {
722 status = usb_string_id(c->cdev);
723 if (status < 0)
724 return status;
725 acm_string_defs[ACM_CTRL_IDX].id = status;
726
727 acm_control_interface_desc.iInterface = status;
728
729 status = usb_string_id(c->cdev);
730 if (status < 0)
731 return status;
732 acm_string_defs[ACM_DATA_IDX].id = status;
733
734 acm_data_interface_desc.iInterface = status;
735 }
736
737
738 acm = kzalloc(sizeof *acm, GFP_KERNEL);
739 if (!acm)
740 return -ENOMEM;
741
742 spin_lock_init(&acm->lock);
743
744 acm->port_num = port_num;
745
746 acm->port.connect = acm_connect;
747 acm->port.disconnect = acm_disconnect;
748 acm->port.send_break = acm_send_break;
749
750 acm->port.func.name = "acm";
751 acm->port.func.strings = acm_strings;
752
753 acm->port.func.bind = acm_bind;
754 acm->port.func.unbind = acm_unbind;
755 acm->port.func.set_alt = acm_set_alt;
756 acm->port.func.setup = acm_setup;
757 acm->port.func.disable = acm_disable;
758
759 status = usb_add_function(c, &acm->port.func);
760 if (status)
761 kfree(acm);
762 return status;
763}
764