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
27
28
29
30
31
32
33#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/fs.h>
36#include <linux/sched.h>
37#include <linux/seq_file.h>
38#include <linux/termios.h>
39#include <linux/tty.h>
40#include <linux/interrupt.h>
41#include <linux/device.h>
42
43#include <asm/uaccess.h>
44
45#include <net/irda/irda.h>
46#include <net/irda/irmod.h>
47
48#include <net/irda/ircomm_core.h>
49#include <net/irda/ircomm_param.h>
50#include <net/irda/ircomm_tty_attach.h>
51#include <net/irda/ircomm_tty.h>
52
53static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
54static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
55static int ircomm_tty_write(struct tty_struct * tty,
56 const unsigned char *buf, int count);
57static int ircomm_tty_write_room(struct tty_struct *tty);
58static void ircomm_tty_throttle(struct tty_struct *tty);
59static void ircomm_tty_unthrottle(struct tty_struct *tty);
60static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
61static void ircomm_tty_flush_buffer(struct tty_struct *tty);
62static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
63static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
64static void ircomm_tty_hangup(struct tty_struct *tty);
65static void ircomm_tty_do_softint(struct work_struct *work);
66static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
67static void ircomm_tty_stop(struct tty_struct *tty);
68
69static int ircomm_tty_data_indication(void *instance, void *sap,
70 struct sk_buff *skb);
71static int ircomm_tty_control_indication(void *instance, void *sap,
72 struct sk_buff *skb);
73static void ircomm_tty_flow_indication(void *instance, void *sap,
74 LOCAL_FLOW cmd);
75#ifdef CONFIG_PROC_FS
76static const struct file_operations ircomm_tty_proc_fops;
77#endif
78static struct tty_driver *driver;
79
80static hashbin_t *ircomm_tty = NULL;
81
82static const struct tty_operations ops = {
83 .open = ircomm_tty_open,
84 .close = ircomm_tty_close,
85 .write = ircomm_tty_write,
86 .write_room = ircomm_tty_write_room,
87 .chars_in_buffer = ircomm_tty_chars_in_buffer,
88 .flush_buffer = ircomm_tty_flush_buffer,
89 .ioctl = ircomm_tty_ioctl,
90 .tiocmget = ircomm_tty_tiocmget,
91 .tiocmset = ircomm_tty_tiocmset,
92 .throttle = ircomm_tty_throttle,
93 .unthrottle = ircomm_tty_unthrottle,
94 .send_xchar = ircomm_tty_send_xchar,
95 .set_termios = ircomm_tty_set_termios,
96 .stop = ircomm_tty_stop,
97 .start = ircomm_tty_start,
98 .hangup = ircomm_tty_hangup,
99 .wait_until_sent = ircomm_tty_wait_until_sent,
100#ifdef CONFIG_PROC_FS
101 .proc_fops = &ircomm_tty_proc_fops,
102#endif
103};
104
105
106
107
108
109
110
111static int __init ircomm_tty_init(void)
112{
113 driver = alloc_tty_driver(IRCOMM_TTY_PORTS);
114 if (!driver)
115 return -ENOMEM;
116 ircomm_tty = hashbin_new(HB_LOCK);
117 if (ircomm_tty == NULL) {
118 IRDA_ERROR("%s(), can't allocate hashbin!\n", __func__);
119 put_tty_driver(driver);
120 return -ENOMEM;
121 }
122
123 driver->owner = THIS_MODULE;
124 driver->driver_name = "ircomm";
125 driver->name = "ircomm";
126 driver->major = IRCOMM_TTY_MAJOR;
127 driver->minor_start = IRCOMM_TTY_MINOR;
128 driver->type = TTY_DRIVER_TYPE_SERIAL;
129 driver->subtype = SERIAL_TYPE_NORMAL;
130 driver->init_termios = tty_std_termios;
131 driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
132 driver->flags = TTY_DRIVER_REAL_RAW;
133 tty_set_operations(driver, &ops);
134 if (tty_register_driver(driver)) {
135 IRDA_ERROR("%s(): Couldn't register serial driver\n",
136 __func__);
137 put_tty_driver(driver);
138 return -1;
139 }
140 return 0;
141}
142
143static void __exit __ircomm_tty_cleanup(struct ircomm_tty_cb *self)
144{
145 IRDA_DEBUG(0, "%s()\n", __func__ );
146
147 IRDA_ASSERT(self != NULL, return;);
148 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
149
150 ircomm_tty_shutdown(self);
151
152 self->magic = 0;
153 kfree(self);
154}
155
156
157
158
159
160
161
162static void __exit ircomm_tty_cleanup(void)
163{
164 int ret;
165
166 IRDA_DEBUG(4, "%s()\n", __func__ );
167
168 ret = tty_unregister_driver(driver);
169 if (ret) {
170 IRDA_ERROR("%s(), failed to unregister driver\n",
171 __func__);
172 return;
173 }
174
175 hashbin_delete(ircomm_tty, (FREE_FUNC) __ircomm_tty_cleanup);
176 put_tty_driver(driver);
177}
178
179
180
181
182
183
184
185static int ircomm_tty_startup(struct ircomm_tty_cb *self)
186{
187 notify_t notify;
188 int ret = -ENODEV;
189
190 IRDA_DEBUG(2, "%s()\n", __func__ );
191
192 IRDA_ASSERT(self != NULL, return -1;);
193 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
194
195
196 if (test_and_set_bit(ASYNC_B_INITIALIZED, &self->flags)) {
197 IRDA_DEBUG(2, "%s(), already open so break out!\n", __func__ );
198 return 0;
199 }
200
201
202 irda_notify_init(¬ify);
203
204 notify.data_indication = ircomm_tty_data_indication;
205 notify.udata_indication = ircomm_tty_control_indication;
206 notify.flow_indication = ircomm_tty_flow_indication;
207
208
209 notify.disconnect_indication = ircomm_tty_disconnect_indication;
210 notify.connect_confirm = ircomm_tty_connect_confirm;
211 notify.connect_indication = ircomm_tty_connect_indication;
212 strlcpy(notify.name, "ircomm_tty", sizeof(notify.name));
213 notify.instance = self;
214
215 if (!self->ircomm) {
216 self->ircomm = ircomm_open(¬ify, self->service_type,
217 self->line);
218 }
219 if (!self->ircomm)
220 goto err;
221
222 self->slsap_sel = self->ircomm->slsap_sel;
223
224
225 ret = ircomm_tty_attach_cable(self);
226 if (ret < 0) {
227 IRDA_ERROR("%s(), error attaching cable!\n", __func__);
228 goto err;
229 }
230
231 return 0;
232err:
233 clear_bit(ASYNC_B_INITIALIZED, &self->flags);
234 return ret;
235}
236
237
238
239
240
241
242
243static int ircomm_tty_block_til_ready(struct ircomm_tty_cb *self,
244 struct file *filp)
245{
246 DECLARE_WAITQUEUE(wait, current);
247 int retval;
248 int do_clocal = 0, extra_count = 0;
249 unsigned long flags;
250 struct tty_struct *tty;
251
252 IRDA_DEBUG(2, "%s()\n", __func__ );
253
254 tty = self->tty;
255
256
257
258
259
260 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
261
262 self->flags |= ASYNC_NORMAL_ACTIVE;
263 IRDA_DEBUG(1, "%s(), O_NONBLOCK requested!\n", __func__ );
264 return 0;
265 }
266
267 if (tty->termios->c_cflag & CLOCAL) {
268 IRDA_DEBUG(1, "%s(), doing CLOCAL!\n", __func__ );
269 do_clocal = 1;
270 }
271
272
273
274
275
276
277
278
279 retval = 0;
280 add_wait_queue(&self->open_wait, &wait);
281
282 IRDA_DEBUG(2, "%s(%d):block_til_ready before block on %s open_count=%d\n",
283 __FILE__,__LINE__, tty->driver->name, self->open_count );
284
285
286 spin_lock_irqsave(&self->spinlock, flags);
287 if (!tty_hung_up_p(filp)) {
288 extra_count = 1;
289 self->open_count--;
290 }
291 spin_unlock_irqrestore(&self->spinlock, flags);
292 self->blocked_open++;
293
294 while (1) {
295 if (tty->termios->c_cflag & CBAUD) {
296
297
298
299
300 self->settings.dte |= IRCOMM_RTS + IRCOMM_DTR;
301
302 ircomm_param_request(self, IRCOMM_DTE, TRUE);
303 }
304
305 current->state = TASK_INTERRUPTIBLE;
306
307 if (tty_hung_up_p(filp) ||
308 !test_bit(ASYNC_B_INITIALIZED, &self->flags)) {
309 retval = (self->flags & ASYNC_HUP_NOTIFY) ?
310 -EAGAIN : -ERESTARTSYS;
311 break;
312 }
313
314
315
316
317
318
319 if (!test_bit(ASYNC_B_CLOSING, &self->flags) &&
320 (do_clocal || (self->settings.dce & IRCOMM_CD)) &&
321 self->state == IRCOMM_TTY_READY)
322 {
323 break;
324 }
325
326 if (signal_pending(current)) {
327 retval = -ERESTARTSYS;
328 break;
329 }
330
331 IRDA_DEBUG(1, "%s(%d):block_til_ready blocking on %s open_count=%d\n",
332 __FILE__,__LINE__, tty->driver->name, self->open_count );
333
334 schedule();
335 }
336
337 __set_current_state(TASK_RUNNING);
338 remove_wait_queue(&self->open_wait, &wait);
339
340 if (extra_count) {
341
342 spin_lock_irqsave(&self->spinlock, flags);
343 self->open_count++;
344 spin_unlock_irqrestore(&self->spinlock, flags);
345 }
346 self->blocked_open--;
347
348 IRDA_DEBUG(1, "%s(%d):block_til_ready after blocking on %s open_count=%d\n",
349 __FILE__,__LINE__, tty->driver->name, self->open_count);
350
351 if (!retval)
352 self->flags |= ASYNC_NORMAL_ACTIVE;
353
354 return retval;
355}
356
357
358
359
360
361
362
363
364static int ircomm_tty_open(struct tty_struct *tty, struct file *filp)
365{
366 struct ircomm_tty_cb *self;
367 unsigned int line;
368 unsigned long flags;
369 int ret;
370
371 IRDA_DEBUG(2, "%s()\n", __func__ );
372
373 line = tty->index;
374 if (line >= IRCOMM_TTY_PORTS)
375 return -ENODEV;
376
377
378 self = hashbin_lock_find(ircomm_tty, line, NULL);
379 if (!self) {
380
381 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
382 if (self == NULL) {
383 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
384 return -ENOMEM;
385 }
386
387 self->magic = IRCOMM_TTY_MAGIC;
388 self->flow = FLOW_STOP;
389
390 self->line = line;
391 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
392 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
393 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
394 self->close_delay = 5*HZ/10;
395 self->closing_wait = 30*HZ;
396
397
398 init_timer(&self->watchdog_timer);
399 init_waitqueue_head(&self->open_wait);
400 init_waitqueue_head(&self->close_wait);
401 spin_lock_init(&self->spinlock);
402
403
404
405
406
407
408
409
410 tty->termios->c_iflag = 0;
411 tty->termios->c_oflag = 0;
412
413
414 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
415 }
416
417 spin_lock_irqsave(&self->spinlock, flags);
418 self->open_count++;
419
420 tty->driver_data = self;
421 self->tty = tty;
422 spin_unlock_irqrestore(&self->spinlock, flags);
423
424 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
425 self->line, self->open_count);
426
427
428 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
429
430
431
432
433 if (tty_hung_up_p(filp) ||
434 test_bit(ASYNC_B_CLOSING, &self->flags)) {
435
436
437
438
439
440
441
442
443
444 if (wait_event_interruptible(self->close_wait, !test_bit(ASYNC_B_CLOSING, &self->flags))) {
445 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
446 __func__);
447 return -ERESTARTSYS;
448 }
449
450#ifdef SERIAL_DO_RESTART
451 return ((self->flags & ASYNC_HUP_NOTIFY) ?
452 -EAGAIN : -ERESTARTSYS);
453#else
454 return -EAGAIN;
455#endif
456 }
457
458
459 if (line < 0x10) {
460 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
461 self->settings.service_type = IRCOMM_9_WIRE;
462
463 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI;
464 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
465 } else {
466 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
467 self->service_type = IRCOMM_3_WIRE_RAW;
468 self->settings.service_type = IRCOMM_3_WIRE_RAW;
469 }
470
471 ret = ircomm_tty_startup(self);
472 if (ret)
473 return ret;
474
475 ret = ircomm_tty_block_til_ready(self, filp);
476 if (ret) {
477 IRDA_DEBUG(2,
478 "%s(), returning after block_til_ready with %d\n", __func__ ,
479 ret);
480
481 return ret;
482 }
483 return 0;
484}
485
486
487
488
489
490
491
492static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
493{
494 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
495 unsigned long flags;
496
497 IRDA_DEBUG(0, "%s()\n", __func__ );
498
499 if (!tty)
500 return;
501
502 IRDA_ASSERT(self != NULL, return;);
503 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
504
505 spin_lock_irqsave(&self->spinlock, flags);
506
507 if (tty_hung_up_p(filp)) {
508 spin_unlock_irqrestore(&self->spinlock, flags);
509
510 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
511 return;
512 }
513
514 if ((tty->count == 1) && (self->open_count != 1)) {
515
516
517
518
519
520
521
522 IRDA_DEBUG(0, "%s(), bad serial port count; "
523 "tty->count is 1, state->count is %d\n", __func__ ,
524 self->open_count);
525 self->open_count = 1;
526 }
527
528 if (--self->open_count < 0) {
529 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
530 __func__, self->line, self->open_count);
531 self->open_count = 0;
532 }
533 if (self->open_count) {
534 spin_unlock_irqrestore(&self->spinlock, flags);
535
536 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
537 return;
538 }
539
540
541 set_bit(ASYNC_B_CLOSING, &self->flags);
542
543
544
545
546
547 spin_unlock_irqrestore(&self->spinlock, flags);
548
549
550
551
552
553 tty->closing = 1;
554 if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
555 tty_wait_until_sent(tty, self->closing_wait);
556
557 ircomm_tty_shutdown(self);
558
559 tty_driver_flush_buffer(tty);
560 tty_ldisc_flush(tty);
561
562 tty->closing = 0;
563 self->tty = NULL;
564
565 if (self->blocked_open) {
566 if (self->close_delay)
567 schedule_timeout_interruptible(self->close_delay);
568 wake_up_interruptible(&self->open_wait);
569 }
570
571 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
572 wake_up_interruptible(&self->close_wait);
573}
574
575
576
577
578
579
580
581static void ircomm_tty_flush_buffer(struct tty_struct *tty)
582{
583 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
584
585 IRDA_ASSERT(self != NULL, return;);
586 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
587
588
589
590
591
592 schedule_work(&self->tqueue);
593}
594
595
596
597
598
599
600
601
602static void ircomm_tty_do_softint(struct work_struct *work)
603{
604 struct ircomm_tty_cb *self =
605 container_of(work, struct ircomm_tty_cb, tqueue);
606 struct tty_struct *tty;
607 unsigned long flags;
608 struct sk_buff *skb, *ctrl_skb;
609
610 IRDA_DEBUG(2, "%s()\n", __func__ );
611
612 if (!self || self->magic != IRCOMM_TTY_MAGIC)
613 return;
614
615 tty = self->tty;
616 if (!tty)
617 return;
618
619
620 spin_lock_irqsave(&self->spinlock, flags);
621
622 ctrl_skb = self->ctrl_skb;
623 self->ctrl_skb = NULL;
624
625 spin_unlock_irqrestore(&self->spinlock, flags);
626
627
628 if(ctrl_skb) {
629 if(self->flow == FLOW_START)
630 ircomm_control_request(self->ircomm, ctrl_skb);
631
632 dev_kfree_skb(ctrl_skb);
633 }
634
635 if (tty->hw_stopped)
636 return;
637
638
639 spin_lock_irqsave(&self->spinlock, flags);
640
641 skb = self->tx_skb;
642 self->tx_skb = NULL;
643
644 spin_unlock_irqrestore(&self->spinlock, flags);
645
646
647 if (skb) {
648 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
649
650 dev_kfree_skb(skb);
651 }
652
653
654 tty_wakeup(tty);
655}
656
657
658
659
660
661
662
663
664
665static int ircomm_tty_write(struct tty_struct *tty,
666 const unsigned char *buf, int count)
667{
668 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
669 unsigned long flags;
670 struct sk_buff *skb;
671 int tailroom = 0;
672 int len = 0;
673 int size;
674
675 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
676 tty->hw_stopped);
677
678 IRDA_ASSERT(self != NULL, return -1;);
679 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
698 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
699#ifdef IRCOMM_NO_TX_BEFORE_INIT
700
701 return 0;
702#endif
703 }
704
705 if (count < 1)
706 return 0;
707
708
709 spin_lock_irqsave(&self->spinlock, flags);
710
711
712 skb = self->tx_skb;
713
714
715
716
717
718
719
720
721
722 while (count) {
723 size = count;
724
725
726 if (size > self->max_data_size)
727 size = self->max_data_size;
728
729
730
731
732
733 if (skb) {
734
735
736
737
738
739
740
741
742
743 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
744
745 if (size > tailroom)
746 size = tailroom;
747 } else {
748
749
750
751
752 break;
753 }
754 } else {
755
756 skb = alloc_skb(self->max_data_size+
757 self->max_header_size,
758 GFP_ATOMIC);
759 if (!skb) {
760 spin_unlock_irqrestore(&self->spinlock, flags);
761 return -ENOBUFS;
762 }
763 skb_reserve(skb, self->max_header_size);
764 self->tx_skb = skb;
765
766
767 self->tx_data_size = self->max_data_size;
768 }
769
770
771 memcpy(skb_put(skb,size), buf + len, size);
772
773 count -= size;
774 len += size;
775 }
776
777 spin_unlock_irqrestore(&self->spinlock, flags);
778
779
780
781
782
783
784
785
786 schedule_work(&self->tqueue);
787
788 return len;
789}
790
791
792
793
794
795
796
797
798static int ircomm_tty_write_room(struct tty_struct *tty)
799{
800 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
801 unsigned long flags;
802 int ret;
803
804 IRDA_ASSERT(self != NULL, return -1;);
805 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
806
807#ifdef IRCOMM_NO_TX_BEFORE_INIT
808
809 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
810
811 return 0;
812#endif
813
814
815
816
817 if (tty->hw_stopped)
818 ret = 0;
819 else {
820 spin_lock_irqsave(&self->spinlock, flags);
821 if (self->tx_skb)
822 ret = self->tx_data_size - self->tx_skb->len;
823 else
824 ret = self->max_data_size;
825 spin_unlock_irqrestore(&self->spinlock, flags);
826 }
827 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
828
829 return ret;
830}
831
832
833
834
835
836
837
838static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
839{
840 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
841 unsigned long orig_jiffies, poll_time;
842 unsigned long flags;
843
844 IRDA_DEBUG(2, "%s()\n", __func__ );
845
846 IRDA_ASSERT(self != NULL, return;);
847 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
848
849 orig_jiffies = jiffies;
850
851
852 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
853
854 spin_lock_irqsave(&self->spinlock, flags);
855 while (self->tx_skb && self->tx_skb->len) {
856 spin_unlock_irqrestore(&self->spinlock, flags);
857 schedule_timeout_interruptible(poll_time);
858 spin_lock_irqsave(&self->spinlock, flags);
859 if (signal_pending(current))
860 break;
861 if (timeout && time_after(jiffies, orig_jiffies + timeout))
862 break;
863 }
864 spin_unlock_irqrestore(&self->spinlock, flags);
865 current->state = TASK_RUNNING;
866}
867
868
869
870
871
872
873
874
875static void ircomm_tty_throttle(struct tty_struct *tty)
876{
877 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
878
879 IRDA_DEBUG(2, "%s()\n", __func__ );
880
881 IRDA_ASSERT(self != NULL, return;);
882 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
883
884
885 if (I_IXOFF(tty))
886 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
887
888
889 if (tty->termios->c_cflag & CRTSCTS) {
890 self->settings.dte &= ~IRCOMM_RTS;
891 self->settings.dte |= IRCOMM_DELTA_RTS;
892
893 ircomm_param_request(self, IRCOMM_DTE, TRUE);
894 }
895
896 ircomm_flow_request(self->ircomm, FLOW_STOP);
897}
898
899
900
901
902
903
904
905
906static void ircomm_tty_unthrottle(struct tty_struct *tty)
907{
908 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
909
910 IRDA_DEBUG(2, "%s()\n", __func__ );
911
912 IRDA_ASSERT(self != NULL, return;);
913 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
914
915
916 if (I_IXOFF(tty)) {
917 ircomm_tty_send_xchar(tty, START_CHAR(tty));
918 }
919
920
921 if (tty->termios->c_cflag & CRTSCTS) {
922 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
923
924 ircomm_param_request(self, IRCOMM_DTE, TRUE);
925 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
926 }
927 ircomm_flow_request(self->ircomm, FLOW_START);
928}
929
930
931
932
933
934
935
936static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
937{
938 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
939 unsigned long flags;
940 int len = 0;
941
942 IRDA_ASSERT(self != NULL, return -1;);
943 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
944
945 spin_lock_irqsave(&self->spinlock, flags);
946
947 if (self->tx_skb)
948 len = self->tx_skb->len;
949
950 spin_unlock_irqrestore(&self->spinlock, flags);
951
952 return len;
953}
954
955static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
956{
957 unsigned long flags;
958
959 IRDA_ASSERT(self != NULL, return;);
960 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
961
962 IRDA_DEBUG(0, "%s()\n", __func__ );
963
964 if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
965 return;
966
967 ircomm_tty_detach_cable(self);
968
969 spin_lock_irqsave(&self->spinlock, flags);
970
971 del_timer(&self->watchdog_timer);
972
973
974 if (self->ctrl_skb) {
975 dev_kfree_skb(self->ctrl_skb);
976 self->ctrl_skb = NULL;
977 }
978
979
980 if (self->tx_skb) {
981 dev_kfree_skb(self->tx_skb);
982 self->tx_skb = NULL;
983 }
984
985 if (self->ircomm) {
986 ircomm_close(self->ircomm);
987 self->ircomm = NULL;
988 }
989
990 spin_unlock_irqrestore(&self->spinlock, flags);
991}
992
993
994
995
996
997
998
999
1000static void ircomm_tty_hangup(struct tty_struct *tty)
1001{
1002 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1003 unsigned long flags;
1004
1005 IRDA_DEBUG(0, "%s()\n", __func__ );
1006
1007 IRDA_ASSERT(self != NULL, return;);
1008 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1009
1010 if (!tty)
1011 return;
1012
1013
1014 ircomm_tty_shutdown(self);
1015
1016
1017 spin_lock_irqsave(&self->spinlock, flags);
1018 self->flags &= ~ASYNC_NORMAL_ACTIVE;
1019 self->tty = NULL;
1020 self->open_count = 0;
1021 spin_unlock_irqrestore(&self->spinlock, flags);
1022
1023 wake_up_interruptible(&self->open_wait);
1024}
1025
1026
1027
1028
1029
1030
1031
1032static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1033{
1034 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
1035}
1036
1037
1038
1039
1040
1041
1042
1043void ircomm_tty_start(struct tty_struct *tty)
1044{
1045 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1046
1047 ircomm_flow_request(self->ircomm, FLOW_START);
1048}
1049
1050
1051
1052
1053
1054
1055
1056static void ircomm_tty_stop(struct tty_struct *tty)
1057{
1058 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1059
1060 IRDA_ASSERT(self != NULL, return;);
1061 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1062
1063 ircomm_flow_request(self->ircomm, FLOW_STOP);
1064}
1065
1066
1067
1068
1069
1070
1071
1072
1073void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1074{
1075 struct tty_struct *tty;
1076 int status;
1077
1078 IRDA_DEBUG(0, "%s()\n", __func__ );
1079
1080 IRDA_ASSERT(self != NULL, return;);
1081 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1082
1083 tty = self->tty;
1084
1085 status = self->settings.dce;
1086
1087 if (status & IRCOMM_DCE_DELTA_ANY) {
1088
1089 }
1090 if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1091 IRDA_DEBUG(2,
1092 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
1093 (status & IRCOMM_CD) ? "on" : "off");
1094
1095 if (status & IRCOMM_CD) {
1096 wake_up_interruptible(&self->open_wait);
1097 } else {
1098 IRDA_DEBUG(2,
1099 "%s(), Doing serial hangup..\n", __func__ );
1100 if (tty)
1101 tty_hangup(tty);
1102
1103
1104 return;
1105 }
1106 }
1107 if (self->flags & ASYNC_CTS_FLOW) {
1108 if (tty->hw_stopped) {
1109 if (status & IRCOMM_CTS) {
1110 IRDA_DEBUG(2,
1111 "%s(), CTS tx start...\n", __func__ );
1112 tty->hw_stopped = 0;
1113
1114
1115 wake_up_interruptible(&self->open_wait);
1116
1117 schedule_work(&self->tqueue);
1118 return;
1119 }
1120 } else {
1121 if (!(status & IRCOMM_CTS)) {
1122 IRDA_DEBUG(2,
1123 "%s(), CTS tx stop...\n", __func__ );
1124 tty->hw_stopped = 1;
1125 }
1126 }
1127 }
1128}
1129
1130
1131
1132
1133
1134
1135
1136static int ircomm_tty_data_indication(void *instance, void *sap,
1137 struct sk_buff *skb)
1138{
1139 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1140 struct tty_ldisc *ld;
1141
1142 IRDA_DEBUG(2, "%s()\n", __func__ );
1143
1144 IRDA_ASSERT(self != NULL, return -1;);
1145 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1146 IRDA_ASSERT(skb != NULL, return -1;);
1147
1148 if (!self->tty) {
1149 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
1150 return 0;
1151 }
1152
1153
1154
1155
1156
1157
1158
1159 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1160 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
1161 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1162
1163
1164 ircomm_tty_send_initial_parameters(self);
1165 ircomm_tty_link_established(self);
1166 }
1167
1168
1169
1170
1171
1172
1173
1174 ld = tty_ldisc_ref(self->tty);
1175 if (ld)
1176 ld->ops->receive_buf(self->tty, skb->data, NULL, skb->len);
1177 tty_ldisc_deref(ld);
1178
1179
1180
1181 return 0;
1182}
1183
1184
1185
1186
1187
1188
1189
1190static int ircomm_tty_control_indication(void *instance, void *sap,
1191 struct sk_buff *skb)
1192{
1193 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1194 int clen;
1195
1196 IRDA_DEBUG(4, "%s()\n", __func__ );
1197
1198 IRDA_ASSERT(self != NULL, return -1;);
1199 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1200 IRDA_ASSERT(skb != NULL, return -1;);
1201
1202 clen = skb->data[0];
1203
1204 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1205 &ircomm_param_info);
1206
1207
1208
1209 return 0;
1210}
1211
1212
1213
1214
1215
1216
1217
1218
1219static void ircomm_tty_flow_indication(void *instance, void *sap,
1220 LOCAL_FLOW cmd)
1221{
1222 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1223 struct tty_struct *tty;
1224
1225 IRDA_ASSERT(self != NULL, return;);
1226 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1227
1228 tty = self->tty;
1229
1230 switch (cmd) {
1231 case FLOW_START:
1232 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
1233 tty->hw_stopped = 0;
1234
1235
1236 schedule_work(&self->tqueue);
1237 break;
1238 default:
1239 case FLOW_STOP:
1240 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
1241 tty->hw_stopped = 1;
1242 break;
1243 }
1244 self->flow = cmd;
1245}
1246
1247#ifdef CONFIG_PROC_FS
1248static void ircomm_tty_line_info(struct ircomm_tty_cb *self, struct seq_file *m)
1249{
1250 char sep;
1251
1252 seq_printf(m, "State: %s\n", ircomm_tty_state[self->state]);
1253
1254 seq_puts(m, "Service type: ");
1255 if (self->service_type & IRCOMM_9_WIRE)
1256 seq_puts(m, "9_WIRE");
1257 else if (self->service_type & IRCOMM_3_WIRE)
1258 seq_puts(m, "3_WIRE");
1259 else if (self->service_type & IRCOMM_3_WIRE_RAW)
1260 seq_puts(m, "3_WIRE_RAW");
1261 else
1262 seq_puts(m, "No common service type!\n");
1263 seq_putc(m, '\n');
1264
1265 seq_printf(m, "Port name: %s\n", self->settings.port_name);
1266
1267 seq_printf(m, "DTE status:");
1268 sep = ' ';
1269 if (self->settings.dte & IRCOMM_RTS) {
1270 seq_printf(m, "%cRTS", sep);
1271 sep = '|';
1272 }
1273 if (self->settings.dte & IRCOMM_DTR) {
1274 seq_printf(m, "%cDTR", sep);
1275 sep = '|';
1276 }
1277 seq_putc(m, '\n');
1278
1279 seq_puts(m, "DCE status:");
1280 sep = ' ';
1281 if (self->settings.dce & IRCOMM_CTS) {
1282 seq_printf(m, "%cCTS", sep);
1283 sep = '|';
1284 }
1285 if (self->settings.dce & IRCOMM_DSR) {
1286 seq_printf(m, "%cDSR", sep);
1287 sep = '|';
1288 }
1289 if (self->settings.dce & IRCOMM_CD) {
1290 seq_printf(m, "%cCD", sep);
1291 sep = '|';
1292 }
1293 if (self->settings.dce & IRCOMM_RI) {
1294 seq_printf(m, "%cRI", sep);
1295 sep = '|';
1296 }
1297 seq_putc(m, '\n');
1298
1299 seq_puts(m, "Configuration: ");
1300 if (!self->settings.null_modem)
1301 seq_puts(m, "DTE <-> DCE\n");
1302 else
1303 seq_puts(m, "DTE <-> DTE (null modem emulation)\n");
1304
1305 seq_printf(m, "Data rate: %d\n", self->settings.data_rate);
1306
1307 seq_puts(m, "Flow control:");
1308 sep = ' ';
1309 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN) {
1310 seq_printf(m, "%cXON_XOFF_IN", sep);
1311 sep = '|';
1312 }
1313 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT) {
1314 seq_printf(m, "%cXON_XOFF_OUT", sep);
1315 sep = '|';
1316 }
1317 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN) {
1318 seq_printf(m, "%cRTS_CTS_IN", sep);
1319 sep = '|';
1320 }
1321 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT) {
1322 seq_printf(m, "%cRTS_CTS_OUT", sep);
1323 sep = '|';
1324 }
1325 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN) {
1326 seq_printf(m, "%cDSR_DTR_IN", sep);
1327 sep = '|';
1328 }
1329 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT) {
1330 seq_printf(m, "%cDSR_DTR_OUT", sep);
1331 sep = '|';
1332 }
1333 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN) {
1334 seq_printf(m, "%cENQ_ACK_IN", sep);
1335 sep = '|';
1336 }
1337 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT) {
1338 seq_printf(m, "%cENQ_ACK_OUT", sep);
1339 sep = '|';
1340 }
1341 seq_putc(m, '\n');
1342
1343 seq_puts(m, "Flags:");
1344 sep = ' ';
1345 if (self->flags & ASYNC_CTS_FLOW) {
1346 seq_printf(m, "%cASYNC_CTS_FLOW", sep);
1347 sep = '|';
1348 }
1349 if (self->flags & ASYNC_CHECK_CD) {
1350 seq_printf(m, "%cASYNC_CHECK_CD", sep);
1351 sep = '|';
1352 }
1353 if (self->flags & ASYNC_INITIALIZED) {
1354 seq_printf(m, "%cASYNC_INITIALIZED", sep);
1355 sep = '|';
1356 }
1357 if (self->flags & ASYNC_LOW_LATENCY) {
1358 seq_printf(m, "%cASYNC_LOW_LATENCY", sep);
1359 sep = '|';
1360 }
1361 if (self->flags & ASYNC_CLOSING) {
1362 seq_printf(m, "%cASYNC_CLOSING", sep);
1363 sep = '|';
1364 }
1365 if (self->flags & ASYNC_NORMAL_ACTIVE) {
1366 seq_printf(m, "%cASYNC_NORMAL_ACTIVE", sep);
1367 sep = '|';
1368 }
1369 seq_putc(m, '\n');
1370
1371 seq_printf(m, "Role: %s\n", self->client ? "client" : "server");
1372 seq_printf(m, "Open count: %d\n", self->open_count);
1373 seq_printf(m, "Max data size: %d\n", self->max_data_size);
1374 seq_printf(m, "Max header size: %d\n", self->max_header_size);
1375
1376 if (self->tty)
1377 seq_printf(m, "Hardware: %s\n",
1378 self->tty->hw_stopped ? "Stopped" : "Running");
1379}
1380
1381static int ircomm_tty_proc_show(struct seq_file *m, void *v)
1382{
1383 struct ircomm_tty_cb *self;
1384 unsigned long flags;
1385
1386 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1387
1388 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1389 while (self != NULL) {
1390 if (self->magic != IRCOMM_TTY_MAGIC)
1391 break;
1392
1393 ircomm_tty_line_info(self, m);
1394 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1395 }
1396 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1397 return 0;
1398}
1399
1400static int ircomm_tty_proc_open(struct inode *inode, struct file *file)
1401{
1402 return single_open(file, ircomm_tty_proc_show, NULL);
1403}
1404
1405static const struct file_operations ircomm_tty_proc_fops = {
1406 .owner = THIS_MODULE,
1407 .open = ircomm_tty_proc_open,
1408 .read = seq_read,
1409 .llseek = seq_lseek,
1410 .release = single_release,
1411};
1412#endif
1413
1414MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1415MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1416MODULE_LICENSE("GPL");
1417MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1418
1419module_init(ircomm_tty_init);
1420module_exit(ircomm_tty_cleanup);
1421