1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/kernel.h>
21#include <linux/interrupt.h>
22#include <linux/device.h>
23#include <linux/delay.h>
24#include <linux/tty.h>
25#include <linux/tty_flip.h>
26
27#include "u_serial.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55#define PREFIX "ttyGS"
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#define QUEUE_SIZE 16
79#define WRITE_BUF_SIZE 8192
80
81
82struct gs_buf {
83 unsigned buf_size;
84 char *buf_buf;
85 char *buf_get;
86 char *buf_put;
87};
88
89
90
91
92
93struct gs_port {
94 spinlock_t port_lock;
95
96 struct gserial *port_usb;
97 struct tty_struct *port_tty;
98
99 unsigned open_count;
100 bool openclose;
101 u8 port_num;
102
103 wait_queue_head_t close_wait;
104
105 struct list_head read_pool;
106 struct list_head read_queue;
107 unsigned n_read;
108 struct tasklet_struct push;
109
110 struct list_head write_pool;
111 struct gs_buf port_write_buf;
112 wait_queue_head_t drain_wait;
113
114
115 struct usb_cdc_line_coding port_line_coding;
116};
117
118
119#define N_PORTS 4
120static struct portmaster {
121 struct mutex lock;
122 struct gs_port *port;
123} ports[N_PORTS];
124static unsigned n_ports;
125
126#define GS_CLOSE_TIMEOUT 15
127
128
129
130#ifdef VERBOSE_DEBUG
131#define pr_vdebug(fmt, arg...) \
132 pr_debug(fmt, ##arg)
133#else
134#define pr_vdebug(fmt, arg...) \
135 ({ if (0) pr_debug(fmt, ##arg); })
136#endif
137
138
139
140
141
142
143
144
145
146
147static int gs_buf_alloc(struct gs_buf *gb, unsigned size)
148{
149 gb->buf_buf = kmalloc(size, GFP_KERNEL);
150 if (gb->buf_buf == NULL)
151 return -ENOMEM;
152
153 gb->buf_size = size;
154 gb->buf_put = gb->buf_buf;
155 gb->buf_get = gb->buf_buf;
156
157 return 0;
158}
159
160
161
162
163
164
165static void gs_buf_free(struct gs_buf *gb)
166{
167 kfree(gb->buf_buf);
168 gb->buf_buf = NULL;
169}
170
171
172
173
174
175
176static void gs_buf_clear(struct gs_buf *gb)
177{
178 gb->buf_get = gb->buf_put;
179
180}
181
182
183
184
185
186
187
188static unsigned gs_buf_data_avail(struct gs_buf *gb)
189{
190 return (gb->buf_size + gb->buf_put - gb->buf_get) % gb->buf_size;
191}
192
193
194
195
196
197
198
199static unsigned gs_buf_space_avail(struct gs_buf *gb)
200{
201 return (gb->buf_size + gb->buf_get - gb->buf_put - 1) % gb->buf_size;
202}
203
204
205
206
207
208
209
210
211
212static unsigned
213gs_buf_put(struct gs_buf *gb, const char *buf, unsigned count)
214{
215 unsigned len;
216
217 len = gs_buf_space_avail(gb);
218 if (count > len)
219 count = len;
220
221 if (count == 0)
222 return 0;
223
224 len = gb->buf_buf + gb->buf_size - gb->buf_put;
225 if (count > len) {
226 memcpy(gb->buf_put, buf, len);
227 memcpy(gb->buf_buf, buf+len, count - len);
228 gb->buf_put = gb->buf_buf + count - len;
229 } else {
230 memcpy(gb->buf_put, buf, count);
231 if (count < len)
232 gb->buf_put += count;
233 else
234 gb->buf_put = gb->buf_buf;
235 }
236
237 return count;
238}
239
240
241
242
243
244
245
246
247
248static unsigned
249gs_buf_get(struct gs_buf *gb, char *buf, unsigned count)
250{
251 unsigned len;
252
253 len = gs_buf_data_avail(gb);
254 if (count > len)
255 count = len;
256
257 if (count == 0)
258 return 0;
259
260 len = gb->buf_buf + gb->buf_size - gb->buf_get;
261 if (count > len) {
262 memcpy(buf, gb->buf_get, len);
263 memcpy(buf+len, gb->buf_buf, count - len);
264 gb->buf_get = gb->buf_buf + count - len;
265 } else {
266 memcpy(buf, gb->buf_get, count);
267 if (count < len)
268 gb->buf_get += count;
269 else
270 gb->buf_get = gb->buf_buf;
271 }
272
273 return count;
274}
275
276
277
278
279
280
281
282
283
284
285
286struct usb_request *
287gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
288{
289 struct usb_request *req;
290
291 req = usb_ep_alloc_request(ep, kmalloc_flags);
292
293 if (req != NULL) {
294 req->length = len;
295 req->buf = kmalloc(len, kmalloc_flags);
296 if (req->buf == NULL) {
297 usb_ep_free_request(ep, req);
298 return NULL;
299 }
300 }
301
302 return req;
303}
304
305
306
307
308
309
310void gs_free_req(struct usb_ep *ep, struct usb_request *req)
311{
312 kfree(req->buf);
313 usb_ep_free_request(ep, req);
314}
315
316
317
318
319
320
321
322
323
324
325static unsigned
326gs_send_packet(struct gs_port *port, char *packet, unsigned size)
327{
328 unsigned len;
329
330 len = gs_buf_data_avail(&port->port_write_buf);
331 if (len < size)
332 size = len;
333 if (size != 0)
334 size = gs_buf_get(&port->port_write_buf, packet, size);
335 return size;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349static int gs_start_tx(struct gs_port *port)
350
351
352
353
354{
355 struct list_head *pool = &port->write_pool;
356 struct usb_ep *in = port->port_usb->in;
357 int status = 0;
358 bool do_tty_wake = false;
359
360 while (!list_empty(pool)) {
361 struct usb_request *req;
362 int len;
363
364 req = list_entry(pool->next, struct usb_request, list);
365 len = gs_send_packet(port, req->buf, in->maxpacket);
366 if (len == 0) {
367 wake_up_interruptible(&port->drain_wait);
368 break;
369 }
370 do_tty_wake = true;
371
372 req->length = len;
373 list_del(&req->list);
374 req->zero = (gs_buf_data_avail(&port->port_write_buf) == 0);
375
376 pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n",
377 port->port_num, len, *((u8 *)req->buf),
378 *((u8 *)req->buf+1), *((u8 *)req->buf+2));
379
380
381
382
383
384
385
386
387 spin_unlock(&port->port_lock);
388 status = usb_ep_queue(in, req, GFP_ATOMIC);
389 spin_lock(&port->port_lock);
390
391 if (status) {
392 pr_debug("%s: %s %s err %d\n",
393 __func__, "queue", in->name, status);
394 list_add(&req->list, pool);
395 break;
396 }
397
398
399 if (!port->port_usb)
400 break;
401 }
402
403 if (do_tty_wake && port->port_tty)
404 tty_wakeup(port->port_tty);
405 return status;
406}
407
408
409
410
411static unsigned gs_start_rx(struct gs_port *port)
412
413
414
415
416{
417 struct list_head *pool = &port->read_pool;
418 struct usb_ep *out = port->port_usb->out;
419 unsigned started = 0;
420
421 while (!list_empty(pool)) {
422 struct usb_request *req;
423 int status;
424 struct tty_struct *tty;
425
426
427 tty = port->port_tty;
428 if (!tty)
429 break;
430
431 req = list_entry(pool->next, struct usb_request, list);
432 list_del(&req->list);
433 req->length = out->maxpacket;
434
435
436
437
438 spin_unlock(&port->port_lock);
439 status = usb_ep_queue(out, req, GFP_ATOMIC);
440 spin_lock(&port->port_lock);
441
442 if (status) {
443 pr_debug("%s: %s %s err %d\n",
444 __func__, "queue", out->name, status);
445 list_add(&req->list, pool);
446 break;
447 }
448 started++;
449
450
451 if (!port->port_usb)
452 break;
453 }
454 return started;
455}
456
457
458
459
460
461
462
463
464
465
466
467static void gs_rx_push(unsigned long _port)
468{
469 struct gs_port *port = (void *)_port;
470 struct tty_struct *tty;
471 struct list_head *queue = &port->read_queue;
472 bool disconnect = false;
473 bool do_push = false;
474
475
476 spin_lock_irq(&port->port_lock);
477 tty = port->port_tty;
478 while (!list_empty(queue)) {
479 struct usb_request *req;
480
481 req = list_first_entry(queue, struct usb_request, list);
482
483
484 if (!tty)
485 goto recycle;
486
487
488 if (test_bit(TTY_THROTTLED, &tty->flags))
489 break;
490
491 switch (req->status) {
492 case -ESHUTDOWN:
493 disconnect = true;
494 pr_vdebug(PREFIX "%d: shutdown\n", port->port_num);
495 break;
496
497 default:
498
499 pr_warning(PREFIX "%d: unexpected RX status %d\n",
500 port->port_num, req->status);
501
502 case 0:
503
504 break;
505 }
506
507
508 if (req->actual) {
509 char *packet = req->buf;
510 unsigned size = req->actual;
511 unsigned n;
512 int count;
513
514
515 n = port->n_read;
516 if (n) {
517 packet += n;
518 size -= n;
519 }
520
521 count = tty_insert_flip_string(tty, packet, size);
522 if (count)
523 do_push = true;
524 if (count != size) {
525
526 port->n_read += count;
527 pr_vdebug(PREFIX "%d: rx block %d/%d\n",
528 port->port_num,
529 count, req->actual);
530 break;
531 }
532 port->n_read = 0;
533 }
534recycle:
535 list_move(&req->list, &port->read_pool);
536 }
537
538
539
540
541 if (tty && do_push) {
542 spin_unlock_irq(&port->port_lock);
543 tty_flip_buffer_push(tty);
544 wake_up_interruptible(&tty->read_wait);
545 spin_lock_irq(&port->port_lock);
546
547
548 tty = port->port_tty;
549 }
550
551
552
553
554
555
556
557
558
559
560 if (!list_empty(queue) && tty) {
561 if (!test_bit(TTY_THROTTLED, &tty->flags)) {
562 if (do_push)
563 tasklet_schedule(&port->push);
564 else
565 pr_warning(PREFIX "%d: RX not scheduled?\n",
566 port->port_num);
567 }
568 }
569
570
571 if (!disconnect && port->port_usb)
572 gs_start_rx(port);
573
574 spin_unlock_irq(&port->port_lock);
575}
576
577static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
578{
579 struct gs_port *port = ep->driver_data;
580
581
582 spin_lock(&port->port_lock);
583 list_add_tail(&req->list, &port->read_queue);
584 tasklet_schedule(&port->push);
585 spin_unlock(&port->port_lock);
586}
587
588static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
589{
590 struct gs_port *port = ep->driver_data;
591
592 spin_lock(&port->port_lock);
593 list_add(&req->list, &port->write_pool);
594
595 switch (req->status) {
596 default:
597
598 pr_warning("%s: unexpected %s status %d\n",
599 __func__, ep->name, req->status);
600
601 case 0:
602
603 gs_start_tx(port);
604 break;
605
606 case -ESHUTDOWN:
607
608 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
609 break;
610 }
611
612 spin_unlock(&port->port_lock);
613}
614
615static void gs_free_requests(struct usb_ep *ep, struct list_head *head)
616{
617 struct usb_request *req;
618
619 while (!list_empty(head)) {
620 req = list_entry(head->next, struct usb_request, list);
621 list_del(&req->list);
622 gs_free_req(ep, req);
623 }
624}
625
626static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
627 void (*fn)(struct usb_ep *, struct usb_request *))
628{
629 int i;
630 struct usb_request *req;
631
632
633
634
635
636 for (i = 0; i < QUEUE_SIZE; i++) {
637 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
638 if (!req)
639 return list_empty(head) ? -ENOMEM : 0;
640 req->complete = fn;
641 list_add_tail(&req->list, head);
642 }
643 return 0;
644}
645
646
647
648
649
650
651
652
653
654
655static int gs_start_io(struct gs_port *port)
656{
657 struct list_head *head = &port->read_pool;
658 struct usb_ep *ep = port->port_usb->out;
659 int status;
660 unsigned started;
661
662
663
664
665
666
667
668 status = gs_alloc_requests(ep, head, gs_read_complete);
669 if (status)
670 return status;
671
672 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
673 gs_write_complete);
674 if (status) {
675 gs_free_requests(ep, head);
676 return status;
677 }
678
679
680 port->n_read = 0;
681 started = gs_start_rx(port);
682
683
684 if (started) {
685 tty_wakeup(port->port_tty);
686 } else {
687 gs_free_requests(ep, head);
688 gs_free_requests(port->port_usb->in, &port->write_pool);
689 status = -EIO;
690 }
691
692 return status;
693}
694
695
696
697
698
699
700
701
702
703
704static int gs_open(struct tty_struct *tty, struct file *file)
705{
706 int port_num = tty->index;
707 struct gs_port *port;
708 int status;
709
710 if (port_num < 0 || port_num >= n_ports)
711 return -ENXIO;
712
713 do {
714 mutex_lock(&ports[port_num].lock);
715 port = ports[port_num].port;
716 if (!port)
717 status = -ENODEV;
718 else {
719 spin_lock_irq(&port->port_lock);
720
721
722 if (port->open_count) {
723 status = 0;
724 port->open_count++;
725
726
727 } else if (port->openclose) {
728 status = -EBUSY;
729
730
731 } else {
732 status = -EAGAIN;
733 port->openclose = true;
734 }
735 spin_unlock_irq(&port->port_lock);
736 }
737 mutex_unlock(&ports[port_num].lock);
738
739 switch (status) {
740 default:
741
742 return status;
743 case -EAGAIN:
744
745 break;
746 case -EBUSY:
747
748 msleep(1);
749
750
751
752 break;
753 }
754 } while (status != -EAGAIN);
755
756
757 spin_lock_irq(&port->port_lock);
758
759
760 if (port->port_write_buf.buf_buf == NULL) {
761
762 spin_unlock_irq(&port->port_lock);
763 status = gs_buf_alloc(&port->port_write_buf, WRITE_BUF_SIZE);
764 spin_lock_irq(&port->port_lock);
765
766 if (status) {
767 pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
768 port->port_num, tty, file);
769 port->openclose = false;
770 goto exit_unlock_port;
771 }
772 }
773
774
775
776
777
778
779
780 tty->driver_data = port;
781 port->port_tty = tty;
782
783 port->open_count = 1;
784 port->openclose = false;
785
786
787
788
789 tty->low_latency = 1;
790
791
792 if (port->port_usb) {
793 struct gserial *gser = port->port_usb;
794
795 pr_debug("gs_open: start ttyGS%d\n", port->port_num);
796 gs_start_io(port);
797
798 if (gser->connect)
799 gser->connect(gser);
800 }
801
802 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
803
804 status = 0;
805
806exit_unlock_port:
807 spin_unlock_irq(&port->port_lock);
808 return status;
809}
810
811static int gs_writes_finished(struct gs_port *p)
812{
813 int cond;
814
815
816 spin_lock_irq(&p->port_lock);
817 cond = (p->port_usb == NULL) || !gs_buf_data_avail(&p->port_write_buf);
818 spin_unlock_irq(&p->port_lock);
819
820 return cond;
821}
822
823static void gs_close(struct tty_struct *tty, struct file *file)
824{
825 struct gs_port *port = tty->driver_data;
826 struct gserial *gser;
827
828 spin_lock_irq(&port->port_lock);
829
830 if (port->open_count != 1) {
831 if (port->open_count == 0)
832 WARN_ON(1);
833 else
834 --port->open_count;
835 goto exit;
836 }
837
838 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
839
840
841
842
843 port->openclose = true;
844 port->open_count = 0;
845
846 gser = port->port_usb;
847 if (gser && gser->disconnect)
848 gser->disconnect(gser);
849
850
851
852
853 if (gs_buf_data_avail(&port->port_write_buf) > 0 && gser) {
854 spin_unlock_irq(&port->port_lock);
855 wait_event_interruptible_timeout(port->drain_wait,
856 gs_writes_finished(port),
857 GS_CLOSE_TIMEOUT * HZ);
858 spin_lock_irq(&port->port_lock);
859 gser = port->port_usb;
860 }
861
862
863
864
865
866 if (gser == NULL)
867 gs_buf_free(&port->port_write_buf);
868 else
869 gs_buf_clear(&port->port_write_buf);
870
871 tty->driver_data = NULL;
872 port->port_tty = NULL;
873
874 port->openclose = false;
875
876 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
877 port->port_num, tty, file);
878
879 wake_up_interruptible(&port->close_wait);
880exit:
881 spin_unlock_irq(&port->port_lock);
882}
883
884static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count)
885{
886 struct gs_port *port = tty->driver_data;
887 unsigned long flags;
888 int status;
889
890 pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n",
891 port->port_num, tty, count);
892
893 spin_lock_irqsave(&port->port_lock, flags);
894 if (count)
895 count = gs_buf_put(&port->port_write_buf, buf, count);
896
897 if (port->port_usb)
898 status = gs_start_tx(port);
899 spin_unlock_irqrestore(&port->port_lock, flags);
900
901 return count;
902}
903
904static int gs_put_char(struct tty_struct *tty, unsigned char ch)
905{
906 struct gs_port *port = tty->driver_data;
907 unsigned long flags;
908 int status;
909
910 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %p\n",
911 port->port_num, tty, ch, __builtin_return_address(0));
912
913 spin_lock_irqsave(&port->port_lock, flags);
914 status = gs_buf_put(&port->port_write_buf, &ch, 1);
915 spin_unlock_irqrestore(&port->port_lock, flags);
916
917 return status;
918}
919
920static void gs_flush_chars(struct tty_struct *tty)
921{
922 struct gs_port *port = tty->driver_data;
923 unsigned long flags;
924
925 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
926
927 spin_lock_irqsave(&port->port_lock, flags);
928 if (port->port_usb)
929 gs_start_tx(port);
930 spin_unlock_irqrestore(&port->port_lock, flags);
931}
932
933static int gs_write_room(struct tty_struct *tty)
934{
935 struct gs_port *port = tty->driver_data;
936 unsigned long flags;
937 int room = 0;
938
939 spin_lock_irqsave(&port->port_lock, flags);
940 if (port->port_usb)
941 room = gs_buf_space_avail(&port->port_write_buf);
942 spin_unlock_irqrestore(&port->port_lock, flags);
943
944 pr_vdebug("gs_write_room: (%d,%p) room=%d\n",
945 port->port_num, tty, room);
946
947 return room;
948}
949
950static int gs_chars_in_buffer(struct tty_struct *tty)
951{
952 struct gs_port *port = tty->driver_data;
953 unsigned long flags;
954 int chars = 0;
955
956 spin_lock_irqsave(&port->port_lock, flags);
957 chars = gs_buf_data_avail(&port->port_write_buf);
958 spin_unlock_irqrestore(&port->port_lock, flags);
959
960 pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n",
961 port->port_num, tty, chars);
962
963 return chars;
964}
965
966
967static void gs_unthrottle(struct tty_struct *tty)
968{
969 struct gs_port *port = tty->driver_data;
970 unsigned long flags;
971
972 spin_lock_irqsave(&port->port_lock, flags);
973 if (port->port_usb) {
974
975
976
977
978 tasklet_schedule(&port->push);
979 pr_vdebug(PREFIX "%d: unthrottle\n", port->port_num);
980 }
981 spin_unlock_irqrestore(&port->port_lock, flags);
982}
983
984static int gs_break_ctl(struct tty_struct *tty, int duration)
985{
986 struct gs_port *port = tty->driver_data;
987 int status = 0;
988 struct gserial *gser;
989
990 pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
991 port->port_num, duration);
992
993 spin_lock_irq(&port->port_lock);
994 gser = port->port_usb;
995 if (gser && gser->send_break)
996 status = gser->send_break(gser, duration);
997 spin_unlock_irq(&port->port_lock);
998
999 return status;
1000}
1001
1002static const struct tty_operations gs_tty_ops = {
1003 .open = gs_open,
1004 .close = gs_close,
1005 .write = gs_write,
1006 .put_char = gs_put_char,
1007 .flush_chars = gs_flush_chars,
1008 .write_room = gs_write_room,
1009 .chars_in_buffer = gs_chars_in_buffer,
1010 .unthrottle = gs_unthrottle,
1011 .break_ctl = gs_break_ctl,
1012};
1013
1014
1015
1016static struct tty_driver *gs_tty_driver;
1017
1018static int __init
1019gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
1020{
1021 struct gs_port *port;
1022
1023 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
1024 if (port == NULL)
1025 return -ENOMEM;
1026
1027 spin_lock_init(&port->port_lock);
1028 init_waitqueue_head(&port->close_wait);
1029 init_waitqueue_head(&port->drain_wait);
1030
1031 tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
1032
1033 INIT_LIST_HEAD(&port->read_pool);
1034 INIT_LIST_HEAD(&port->read_queue);
1035 INIT_LIST_HEAD(&port->write_pool);
1036
1037 port->port_num = port_num;
1038 port->port_line_coding = *coding;
1039
1040 ports[port_num].port = port;
1041
1042 return 0;
1043}
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064int __init gserial_setup(struct usb_gadget *g, unsigned count)
1065{
1066 unsigned i;
1067 struct usb_cdc_line_coding coding;
1068 int status;
1069
1070 if (count == 0 || count > N_PORTS)
1071 return -EINVAL;
1072
1073 gs_tty_driver = alloc_tty_driver(count);
1074 if (!gs_tty_driver)
1075 return -ENOMEM;
1076
1077 gs_tty_driver->owner = THIS_MODULE;
1078 gs_tty_driver->driver_name = "g_serial";
1079 gs_tty_driver->name = PREFIX;
1080
1081
1082 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1083 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1084 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1085 gs_tty_driver->init_termios = tty_std_termios;
1086
1087
1088
1089
1090
1091 gs_tty_driver->init_termios.c_cflag =
1092 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1093 gs_tty_driver->init_termios.c_ispeed = 9600;
1094 gs_tty_driver->init_termios.c_ospeed = 9600;
1095
1096 coding.dwDTERate = cpu_to_le32(9600);
1097 coding.bCharFormat = 8;
1098 coding.bParityType = USB_CDC_NO_PARITY;
1099 coding.bDataBits = USB_CDC_1_STOP_BITS;
1100
1101 tty_set_operations(gs_tty_driver, &gs_tty_ops);
1102
1103
1104 for (i = 0; i < count; i++) {
1105 mutex_init(&ports[i].lock);
1106 status = gs_port_alloc(i, &coding);
1107 if (status) {
1108 count = i;
1109 goto fail;
1110 }
1111 }
1112 n_ports = count;
1113
1114
1115 status = tty_register_driver(gs_tty_driver);
1116 if (status) {
1117 pr_err("%s: cannot register, err %d\n",
1118 __func__, status);
1119 goto fail;
1120 }
1121
1122
1123 for (i = 0; i < count; i++) {
1124 struct device *tty_dev;
1125
1126 tty_dev = tty_register_device(gs_tty_driver, i, &g->dev);
1127 if (IS_ERR(tty_dev))
1128 pr_warning("%s: no classdev for port %d, err %ld\n",
1129 __func__, i, PTR_ERR(tty_dev));
1130 }
1131
1132 pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
1133 count, (count == 1) ? "" : "s");
1134
1135 return status;
1136fail:
1137 while (count--)
1138 kfree(ports[count].port);
1139 put_tty_driver(gs_tty_driver);
1140 gs_tty_driver = NULL;
1141 return status;
1142}
1143
1144static int gs_closed(struct gs_port *port)
1145{
1146 int cond;
1147
1148 spin_lock_irq(&port->port_lock);
1149 cond = (port->open_count == 0) && !port->openclose;
1150 spin_unlock_irq(&port->port_lock);
1151 return cond;
1152}
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166void gserial_cleanup(void)
1167{
1168 unsigned i;
1169 struct gs_port *port;
1170
1171 if (!gs_tty_driver)
1172 return;
1173
1174
1175 for (i = 0; i < n_ports; i++)
1176 tty_unregister_device(gs_tty_driver, i);
1177
1178 for (i = 0; i < n_ports; i++) {
1179
1180 mutex_lock(&ports[i].lock);
1181 port = ports[i].port;
1182 ports[i].port = NULL;
1183 mutex_unlock(&ports[i].lock);
1184
1185 tasklet_kill(&port->push);
1186
1187
1188 wait_event(port->close_wait, gs_closed(port));
1189
1190 WARN_ON(port->port_usb != NULL);
1191
1192 kfree(port);
1193 }
1194 n_ports = 0;
1195
1196 tty_unregister_driver(gs_tty_driver);
1197 gs_tty_driver = NULL;
1198
1199 pr_debug("%s: cleaned up ttyGS* support\n", __func__);
1200}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223int gserial_connect(struct gserial *gser, u8 port_num)
1224{
1225 struct gs_port *port;
1226 unsigned long flags;
1227 int status;
1228
1229 if (!gs_tty_driver || port_num >= n_ports)
1230 return -ENXIO;
1231
1232
1233 port = ports[port_num].port;
1234
1235
1236 status = usb_ep_enable(gser->in, gser->in_desc);
1237 if (status < 0)
1238 return status;
1239 gser->in->driver_data = port;
1240
1241 status = usb_ep_enable(gser->out, gser->out_desc);
1242 if (status < 0)
1243 goto fail_out;
1244 gser->out->driver_data = port;
1245
1246
1247 spin_lock_irqsave(&port->port_lock, flags);
1248 gser->ioport = port;
1249 port->port_usb = gser;
1250
1251
1252
1253
1254 gser->port_line_coding = port->port_line_coding;
1255
1256
1257
1258
1259
1260
1261 if (port->open_count) {
1262 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1263 gs_start_io(port);
1264 if (gser->connect)
1265 gser->connect(gser);
1266 } else {
1267 if (gser->disconnect)
1268 gser->disconnect(gser);
1269 }
1270
1271 spin_unlock_irqrestore(&port->port_lock, flags);
1272
1273 return status;
1274
1275fail_out:
1276 usb_ep_disable(gser->in);
1277 gser->in->driver_data = NULL;
1278 return status;
1279}
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292void gserial_disconnect(struct gserial *gser)
1293{
1294 struct gs_port *port = gser->ioport;
1295 unsigned long flags;
1296
1297 if (!port)
1298 return;
1299
1300
1301 spin_lock_irqsave(&port->port_lock, flags);
1302
1303
1304 port->port_line_coding = gser->port_line_coding;
1305
1306 port->port_usb = NULL;
1307 gser->ioport = NULL;
1308 if (port->open_count > 0 || port->openclose) {
1309 wake_up_interruptible(&port->drain_wait);
1310 if (port->port_tty)
1311 tty_hangup(port->port_tty);
1312 }
1313 spin_unlock_irqrestore(&port->port_lock, flags);
1314
1315
1316 usb_ep_disable(gser->out);
1317 gser->out->driver_data = NULL;
1318
1319 usb_ep_disable(gser->in);
1320 gser->in->driver_data = NULL;
1321
1322
1323 spin_lock_irqsave(&port->port_lock, flags);
1324 if (port->open_count == 0 && !port->openclose)
1325 gs_buf_free(&port->port_write_buf);
1326 gs_free_requests(gser->out, &port->read_pool);
1327 gs_free_requests(gser->out, &port->read_queue);
1328 gs_free_requests(gser->in, &port->write_pool);
1329 spin_unlock_irqrestore(&port->port_lock, flags);
1330}
1331