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/termios.h>
38#include <linux/tty.h>
39#include <linux/interrupt.h>
40#include <linux/device.h>
41
42#include <asm/uaccess.h>
43
44#include <net/irda/irda.h>
45#include <net/irda/irmod.h>
46
47#include <net/irda/ircomm_core.h>
48#include <net/irda/ircomm_param.h>
49#include <net/irda/ircomm_tty_attach.h>
50#include <net/irda/ircomm_tty.h>
51
52static int ircomm_tty_open(struct tty_struct *tty, struct file *filp);
53static void ircomm_tty_close(struct tty_struct * tty, struct file *filp);
54static int ircomm_tty_write(struct tty_struct * tty,
55 const unsigned char *buf, int count);
56static int ircomm_tty_write_room(struct tty_struct *tty);
57static void ircomm_tty_throttle(struct tty_struct *tty);
58static void ircomm_tty_unthrottle(struct tty_struct *tty);
59static int ircomm_tty_chars_in_buffer(struct tty_struct *tty);
60static void ircomm_tty_flush_buffer(struct tty_struct *tty);
61static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch);
62static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout);
63static void ircomm_tty_hangup(struct tty_struct *tty);
64static void ircomm_tty_do_softint(struct work_struct *work);
65static void ircomm_tty_shutdown(struct ircomm_tty_cb *self);
66static void ircomm_tty_stop(struct tty_struct *tty);
67
68static int ircomm_tty_data_indication(void *instance, void *sap,
69 struct sk_buff *skb);
70static int ircomm_tty_control_indication(void *instance, void *sap,
71 struct sk_buff *skb);
72static void ircomm_tty_flow_indication(void *instance, void *sap,
73 LOCAL_FLOW cmd);
74#ifdef CONFIG_PROC_FS
75static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
76 int *eof, void *unused);
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 .read_proc = ircomm_tty_read_proc,
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 < 0) || (line >= IRCOMM_TTY_PORTS)) {
375 return -ENODEV;
376 }
377
378
379 self = hashbin_lock_find(ircomm_tty, line, NULL);
380 if (!self) {
381
382 self = kzalloc(sizeof(struct ircomm_tty_cb), GFP_KERNEL);
383 if (self == NULL) {
384 IRDA_ERROR("%s(), kmalloc failed!\n", __func__);
385 return -ENOMEM;
386 }
387
388 self->magic = IRCOMM_TTY_MAGIC;
389 self->flow = FLOW_STOP;
390
391 self->line = line;
392 INIT_WORK(&self->tqueue, ircomm_tty_do_softint);
393 self->max_header_size = IRCOMM_TTY_HDR_UNINITIALISED;
394 self->max_data_size = IRCOMM_TTY_DATA_UNINITIALISED;
395 self->close_delay = 5*HZ/10;
396 self->closing_wait = 30*HZ;
397
398
399 init_timer(&self->watchdog_timer);
400 init_waitqueue_head(&self->open_wait);
401 init_waitqueue_head(&self->close_wait);
402 spin_lock_init(&self->spinlock);
403
404
405
406
407
408
409 tty->termios->c_iflag = 0;
410 tty->termios->c_oflag = 0;
411
412
413 hashbin_insert(ircomm_tty, (irda_queue_t *) self, line, NULL);
414 }
415
416 spin_lock_irqsave(&self->spinlock, flags);
417 self->open_count++;
418
419 tty->driver_data = self;
420 self->tty = tty;
421 spin_unlock_irqrestore(&self->spinlock, flags);
422
423 IRDA_DEBUG(1, "%s(), %s%d, count = %d\n", __func__ , tty->driver->name,
424 self->line, self->open_count);
425
426
427 self->tty->low_latency = (self->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
428
429
430
431
432 if (tty_hung_up_p(filp) ||
433 test_bit(ASYNC_B_CLOSING, &self->flags)) {
434
435
436
437
438
439
440
441
442
443 if (wait_event_interruptible(self->close_wait, !test_bit(ASYNC_B_CLOSING, &self->flags))) {
444 IRDA_WARNING("%s - got signal while blocking on ASYNC_CLOSING!\n",
445 __func__);
446 return -ERESTARTSYS;
447 }
448
449#ifdef SERIAL_DO_RESTART
450 return ((self->flags & ASYNC_HUP_NOTIFY) ?
451 -EAGAIN : -ERESTARTSYS);
452#else
453 return -EAGAIN;
454#endif
455 }
456
457
458 if (line < 0x10) {
459 self->service_type = IRCOMM_3_WIRE | IRCOMM_9_WIRE;
460 self->settings.service_type = IRCOMM_9_WIRE;
461
462 self->settings.dce = IRCOMM_CTS | IRCOMM_CD | IRCOMM_DSR | IRCOMM_RI;
463 IRDA_DEBUG(2, "%s(), IrCOMM device\n", __func__ );
464 } else {
465 IRDA_DEBUG(2, "%s(), IrLPT device\n", __func__ );
466 self->service_type = IRCOMM_3_WIRE_RAW;
467 self->settings.service_type = IRCOMM_3_WIRE_RAW;
468 }
469
470 ret = ircomm_tty_startup(self);
471 if (ret)
472 return ret;
473
474 ret = ircomm_tty_block_til_ready(self, filp);
475 if (ret) {
476 IRDA_DEBUG(2,
477 "%s(), returning after block_til_ready with %d\n", __func__ ,
478 ret);
479
480 return ret;
481 }
482 return 0;
483}
484
485
486
487
488
489
490
491static void ircomm_tty_close(struct tty_struct *tty, struct file *filp)
492{
493 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
494 unsigned long flags;
495
496 IRDA_DEBUG(0, "%s()\n", __func__ );
497
498 if (!tty)
499 return;
500
501 IRDA_ASSERT(self != NULL, return;);
502 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
503
504 spin_lock_irqsave(&self->spinlock, flags);
505
506 if (tty_hung_up_p(filp)) {
507 spin_unlock_irqrestore(&self->spinlock, flags);
508
509 IRDA_DEBUG(0, "%s(), returning 1\n", __func__ );
510 return;
511 }
512
513 if ((tty->count == 1) && (self->open_count != 1)) {
514
515
516
517
518
519
520
521 IRDA_DEBUG(0, "%s(), bad serial port count; "
522 "tty->count is 1, state->count is %d\n", __func__ ,
523 self->open_count);
524 self->open_count = 1;
525 }
526
527 if (--self->open_count < 0) {
528 IRDA_ERROR("%s(), bad serial port count for ttys%d: %d\n",
529 __func__, self->line, self->open_count);
530 self->open_count = 0;
531 }
532 if (self->open_count) {
533 spin_unlock_irqrestore(&self->spinlock, flags);
534
535 IRDA_DEBUG(0, "%s(), open count > 0\n", __func__ );
536 return;
537 }
538
539
540 set_bit(ASYNC_B_CLOSING, &self->flags);
541
542
543
544
545
546 spin_unlock_irqrestore(&self->spinlock, flags);
547
548
549
550
551
552 tty->closing = 1;
553 if (self->closing_wait != ASYNC_CLOSING_WAIT_NONE)
554 tty_wait_until_sent(tty, self->closing_wait);
555
556 ircomm_tty_shutdown(self);
557
558 tty_driver_flush_buffer(tty);
559 tty_ldisc_flush(tty);
560
561 tty->closing = 0;
562 self->tty = NULL;
563
564 if (self->blocked_open) {
565 if (self->close_delay)
566 schedule_timeout_interruptible(self->close_delay);
567 wake_up_interruptible(&self->open_wait);
568 }
569
570 self->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
571 wake_up_interruptible(&self->close_wait);
572}
573
574
575
576
577
578
579
580static void ircomm_tty_flush_buffer(struct tty_struct *tty)
581{
582 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
583
584 IRDA_ASSERT(self != NULL, return;);
585 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
586
587
588
589
590
591 schedule_work(&self->tqueue);
592}
593
594
595
596
597
598
599
600
601static void ircomm_tty_do_softint(struct work_struct *work)
602{
603 struct ircomm_tty_cb *self =
604 container_of(work, struct ircomm_tty_cb, tqueue);
605 struct tty_struct *tty;
606 unsigned long flags;
607 struct sk_buff *skb, *ctrl_skb;
608
609 IRDA_DEBUG(2, "%s()\n", __func__ );
610
611 if (!self || self->magic != IRCOMM_TTY_MAGIC)
612 return;
613
614 tty = self->tty;
615 if (!tty)
616 return;
617
618
619 spin_lock_irqsave(&self->spinlock, flags);
620
621 ctrl_skb = self->ctrl_skb;
622 self->ctrl_skb = NULL;
623
624 spin_unlock_irqrestore(&self->spinlock, flags);
625
626
627 if(ctrl_skb) {
628 if(self->flow == FLOW_START)
629 ircomm_control_request(self->ircomm, ctrl_skb);
630
631 dev_kfree_skb(ctrl_skb);
632 }
633
634 if (tty->hw_stopped)
635 return;
636
637
638 spin_lock_irqsave(&self->spinlock, flags);
639
640 skb = self->tx_skb;
641 self->tx_skb = NULL;
642
643 spin_unlock_irqrestore(&self->spinlock, flags);
644
645
646 if (skb) {
647 ircomm_tty_do_event(self, IRCOMM_TTY_DATA_REQUEST, skb, NULL);
648
649 dev_kfree_skb(skb);
650 }
651
652
653 tty_wakeup(tty);
654}
655
656
657
658
659
660
661
662
663
664static int ircomm_tty_write(struct tty_struct *tty,
665 const unsigned char *buf, int count)
666{
667 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
668 unsigned long flags;
669 struct sk_buff *skb;
670 int tailroom = 0;
671 int len = 0;
672 int size;
673
674 IRDA_DEBUG(2, "%s(), count=%d, hw_stopped=%d\n", __func__ , count,
675 tty->hw_stopped);
676
677 IRDA_ASSERT(self != NULL, return -1;);
678 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED) {
697 IRDA_DEBUG(1, "%s() : not initialised\n", __func__);
698#ifdef IRCOMM_NO_TX_BEFORE_INIT
699
700 return 0;
701#endif
702 }
703
704 if (count < 1)
705 return 0;
706
707
708 spin_lock_irqsave(&self->spinlock, flags);
709
710
711 skb = self->tx_skb;
712
713
714
715
716
717
718
719
720
721 while (count) {
722 size = count;
723
724
725 if (size > self->max_data_size)
726 size = self->max_data_size;
727
728
729
730
731
732 if (skb) {
733
734
735
736
737
738
739
740
741
742 if ((tailroom = (self->tx_data_size - skb->len)) > 0) {
743
744 if (size > tailroom)
745 size = tailroom;
746 } else {
747
748
749
750
751 break;
752 }
753 } else {
754
755 skb = alloc_skb(self->max_data_size+
756 self->max_header_size,
757 GFP_ATOMIC);
758 if (!skb) {
759 spin_unlock_irqrestore(&self->spinlock, flags);
760 return -ENOBUFS;
761 }
762 skb_reserve(skb, self->max_header_size);
763 self->tx_skb = skb;
764
765
766 self->tx_data_size = self->max_data_size;
767 }
768
769
770 memcpy(skb_put(skb,size), buf + len, size);
771
772 count -= size;
773 len += size;
774 }
775
776 spin_unlock_irqrestore(&self->spinlock, flags);
777
778
779
780
781
782
783
784
785 schedule_work(&self->tqueue);
786
787 return len;
788}
789
790
791
792
793
794
795
796
797static int ircomm_tty_write_room(struct tty_struct *tty)
798{
799 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
800 unsigned long flags;
801 int ret;
802
803 IRDA_ASSERT(self != NULL, return -1;);
804 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
805
806#ifdef IRCOMM_NO_TX_BEFORE_INIT
807
808 if (self->max_header_size == IRCOMM_TTY_HDR_UNINITIALISED)
809
810 return 0;
811#endif
812
813
814
815
816 if (tty->hw_stopped)
817 ret = 0;
818 else {
819 spin_lock_irqsave(&self->spinlock, flags);
820 if (self->tx_skb)
821 ret = self->tx_data_size - self->tx_skb->len;
822 else
823 ret = self->max_data_size;
824 spin_unlock_irqrestore(&self->spinlock, flags);
825 }
826 IRDA_DEBUG(2, "%s(), ret=%d\n", __func__ , ret);
827
828 return ret;
829}
830
831
832
833
834
835
836
837static void ircomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
838{
839 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
840 unsigned long orig_jiffies, poll_time;
841 unsigned long flags;
842
843 IRDA_DEBUG(2, "%s()\n", __func__ );
844
845 IRDA_ASSERT(self != NULL, return;);
846 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
847
848 orig_jiffies = jiffies;
849
850
851 poll_time = IRDA_MIN(timeout, msecs_to_jiffies(200));
852
853 spin_lock_irqsave(&self->spinlock, flags);
854 while (self->tx_skb && self->tx_skb->len) {
855 spin_unlock_irqrestore(&self->spinlock, flags);
856 schedule_timeout_interruptible(poll_time);
857 spin_lock_irqsave(&self->spinlock, flags);
858 if (signal_pending(current))
859 break;
860 if (timeout && time_after(jiffies, orig_jiffies + timeout))
861 break;
862 }
863 spin_unlock_irqrestore(&self->spinlock, flags);
864 current->state = TASK_RUNNING;
865}
866
867
868
869
870
871
872
873
874static void ircomm_tty_throttle(struct tty_struct *tty)
875{
876 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
877
878 IRDA_DEBUG(2, "%s()\n", __func__ );
879
880 IRDA_ASSERT(self != NULL, return;);
881 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
882
883
884 if (I_IXOFF(tty))
885 ircomm_tty_send_xchar(tty, STOP_CHAR(tty));
886
887
888 if (tty->termios->c_cflag & CRTSCTS) {
889 self->settings.dte &= ~IRCOMM_RTS;
890 self->settings.dte |= IRCOMM_DELTA_RTS;
891
892 ircomm_param_request(self, IRCOMM_DTE, TRUE);
893 }
894
895 ircomm_flow_request(self->ircomm, FLOW_STOP);
896}
897
898
899
900
901
902
903
904
905static void ircomm_tty_unthrottle(struct tty_struct *tty)
906{
907 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
908
909 IRDA_DEBUG(2, "%s()\n", __func__ );
910
911 IRDA_ASSERT(self != NULL, return;);
912 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
913
914
915 if (I_IXOFF(tty)) {
916 ircomm_tty_send_xchar(tty, START_CHAR(tty));
917 }
918
919
920 if (tty->termios->c_cflag & CRTSCTS) {
921 self->settings.dte |= (IRCOMM_RTS|IRCOMM_DELTA_RTS);
922
923 ircomm_param_request(self, IRCOMM_DTE, TRUE);
924 IRDA_DEBUG(1, "%s(), FLOW_START\n", __func__ );
925 }
926 ircomm_flow_request(self->ircomm, FLOW_START);
927}
928
929
930
931
932
933
934
935static int ircomm_tty_chars_in_buffer(struct tty_struct *tty)
936{
937 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
938 unsigned long flags;
939 int len = 0;
940
941 IRDA_ASSERT(self != NULL, return -1;);
942 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
943
944 spin_lock_irqsave(&self->spinlock, flags);
945
946 if (self->tx_skb)
947 len = self->tx_skb->len;
948
949 spin_unlock_irqrestore(&self->spinlock, flags);
950
951 return len;
952}
953
954static void ircomm_tty_shutdown(struct ircomm_tty_cb *self)
955{
956 unsigned long flags;
957
958 IRDA_ASSERT(self != NULL, return;);
959 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
960
961 IRDA_DEBUG(0, "%s()\n", __func__ );
962
963 if (!test_and_clear_bit(ASYNC_B_INITIALIZED, &self->flags))
964 return;
965
966 ircomm_tty_detach_cable(self);
967
968 spin_lock_irqsave(&self->spinlock, flags);
969
970 del_timer(&self->watchdog_timer);
971
972
973 if (self->ctrl_skb) {
974 dev_kfree_skb(self->ctrl_skb);
975 self->ctrl_skb = NULL;
976 }
977
978
979 if (self->tx_skb) {
980 dev_kfree_skb(self->tx_skb);
981 self->tx_skb = NULL;
982 }
983
984 if (self->ircomm) {
985 ircomm_close(self->ircomm);
986 self->ircomm = NULL;
987 }
988
989 spin_unlock_irqrestore(&self->spinlock, flags);
990}
991
992
993
994
995
996
997
998
999static void ircomm_tty_hangup(struct tty_struct *tty)
1000{
1001 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1002 unsigned long flags;
1003
1004 IRDA_DEBUG(0, "%s()\n", __func__ );
1005
1006 IRDA_ASSERT(self != NULL, return;);
1007 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1008
1009 if (!tty)
1010 return;
1011
1012
1013 ircomm_tty_shutdown(self);
1014
1015
1016 spin_lock_irqsave(&self->spinlock, flags);
1017 self->flags &= ~ASYNC_NORMAL_ACTIVE;
1018 self->tty = NULL;
1019 self->open_count = 0;
1020 spin_unlock_irqrestore(&self->spinlock, flags);
1021
1022 wake_up_interruptible(&self->open_wait);
1023}
1024
1025
1026
1027
1028
1029
1030
1031static void ircomm_tty_send_xchar(struct tty_struct *tty, char ch)
1032{
1033 IRDA_DEBUG(0, "%s(), not impl\n", __func__ );
1034}
1035
1036
1037
1038
1039
1040
1041
1042void ircomm_tty_start(struct tty_struct *tty)
1043{
1044 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1045
1046 ircomm_flow_request(self->ircomm, FLOW_START);
1047}
1048
1049
1050
1051
1052
1053
1054
1055static void ircomm_tty_stop(struct tty_struct *tty)
1056{
1057 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) tty->driver_data;
1058
1059 IRDA_ASSERT(self != NULL, return;);
1060 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1061
1062 ircomm_flow_request(self->ircomm, FLOW_STOP);
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072void ircomm_tty_check_modem_status(struct ircomm_tty_cb *self)
1073{
1074 struct tty_struct *tty;
1075 int status;
1076
1077 IRDA_DEBUG(0, "%s()\n", __func__ );
1078
1079 IRDA_ASSERT(self != NULL, return;);
1080 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1081
1082 tty = self->tty;
1083
1084 status = self->settings.dce;
1085
1086 if (status & IRCOMM_DCE_DELTA_ANY) {
1087
1088 }
1089 if ((self->flags & ASYNC_CHECK_CD) && (status & IRCOMM_DELTA_CD)) {
1090 IRDA_DEBUG(2,
1091 "%s(), ircomm%d CD now %s...\n", __func__ , self->line,
1092 (status & IRCOMM_CD) ? "on" : "off");
1093
1094 if (status & IRCOMM_CD) {
1095 wake_up_interruptible(&self->open_wait);
1096 } else {
1097 IRDA_DEBUG(2,
1098 "%s(), Doing serial hangup..\n", __func__ );
1099 if (tty)
1100 tty_hangup(tty);
1101
1102
1103 return;
1104 }
1105 }
1106 if (self->flags & ASYNC_CTS_FLOW) {
1107 if (tty->hw_stopped) {
1108 if (status & IRCOMM_CTS) {
1109 IRDA_DEBUG(2,
1110 "%s(), CTS tx start...\n", __func__ );
1111 tty->hw_stopped = 0;
1112
1113
1114 wake_up_interruptible(&self->open_wait);
1115
1116 schedule_work(&self->tqueue);
1117 return;
1118 }
1119 } else {
1120 if (!(status & IRCOMM_CTS)) {
1121 IRDA_DEBUG(2,
1122 "%s(), CTS tx stop...\n", __func__ );
1123 tty->hw_stopped = 1;
1124 }
1125 }
1126 }
1127}
1128
1129
1130
1131
1132
1133
1134
1135static int ircomm_tty_data_indication(void *instance, void *sap,
1136 struct sk_buff *skb)
1137{
1138 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1139 struct tty_ldisc *ld;
1140
1141 IRDA_DEBUG(2, "%s()\n", __func__ );
1142
1143 IRDA_ASSERT(self != NULL, return -1;);
1144 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1145 IRDA_ASSERT(skb != NULL, return -1;);
1146
1147 if (!self->tty) {
1148 IRDA_DEBUG(0, "%s(), no tty!\n", __func__ );
1149 return 0;
1150 }
1151
1152
1153
1154
1155
1156
1157
1158 if (self->tty->hw_stopped && (self->flow == FLOW_START)) {
1159 IRDA_DEBUG(0, "%s(), polling for line settings!\n", __func__ );
1160 ircomm_param_request(self, IRCOMM_POLL, TRUE);
1161
1162
1163 ircomm_tty_send_initial_parameters(self);
1164 ircomm_tty_link_established(self);
1165 }
1166
1167
1168
1169
1170
1171
1172
1173 ld = tty_ldisc_ref(self->tty);
1174 if (ld)
1175 ld->ops->receive_buf(self->tty, skb->data, NULL, skb->len);
1176 tty_ldisc_deref(ld);
1177
1178
1179
1180 return 0;
1181}
1182
1183
1184
1185
1186
1187
1188
1189static int ircomm_tty_control_indication(void *instance, void *sap,
1190 struct sk_buff *skb)
1191{
1192 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1193 int clen;
1194
1195 IRDA_DEBUG(4, "%s()\n", __func__ );
1196
1197 IRDA_ASSERT(self != NULL, return -1;);
1198 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return -1;);
1199 IRDA_ASSERT(skb != NULL, return -1;);
1200
1201 clen = skb->data[0];
1202
1203 irda_param_extract_all(self, skb->data+1, IRDA_MIN(skb->len-1, clen),
1204 &ircomm_param_info);
1205
1206
1207
1208 return 0;
1209}
1210
1211
1212
1213
1214
1215
1216
1217
1218static void ircomm_tty_flow_indication(void *instance, void *sap,
1219 LOCAL_FLOW cmd)
1220{
1221 struct ircomm_tty_cb *self = (struct ircomm_tty_cb *) instance;
1222 struct tty_struct *tty;
1223
1224 IRDA_ASSERT(self != NULL, return;);
1225 IRDA_ASSERT(self->magic == IRCOMM_TTY_MAGIC, return;);
1226
1227 tty = self->tty;
1228
1229 switch (cmd) {
1230 case FLOW_START:
1231 IRDA_DEBUG(2, "%s(), hw start!\n", __func__ );
1232 tty->hw_stopped = 0;
1233
1234
1235 schedule_work(&self->tqueue);
1236 break;
1237 default:
1238 case FLOW_STOP:
1239 IRDA_DEBUG(2, "%s(), hw stopped!\n", __func__ );
1240 tty->hw_stopped = 1;
1241 break;
1242 }
1243 self->flow = cmd;
1244}
1245
1246#ifdef CONFIG_PROC_FS
1247static int ircomm_tty_line_info(struct ircomm_tty_cb *self, char *buf)
1248{
1249 int ret=0;
1250
1251 ret += sprintf(buf+ret, "State: %s\n", ircomm_tty_state[self->state]);
1252
1253 ret += sprintf(buf+ret, "Service type: ");
1254 if (self->service_type & IRCOMM_9_WIRE)
1255 ret += sprintf(buf+ret, "9_WIRE");
1256 else if (self->service_type & IRCOMM_3_WIRE)
1257 ret += sprintf(buf+ret, "3_WIRE");
1258 else if (self->service_type & IRCOMM_3_WIRE_RAW)
1259 ret += sprintf(buf+ret, "3_WIRE_RAW");
1260 else
1261 ret += sprintf(buf+ret, "No common service type!\n");
1262 ret += sprintf(buf+ret, "\n");
1263
1264 ret += sprintf(buf+ret, "Port name: %s\n", self->settings.port_name);
1265
1266 ret += sprintf(buf+ret, "DTE status: ");
1267 if (self->settings.dte & IRCOMM_RTS)
1268 ret += sprintf(buf+ret, "RTS|");
1269 if (self->settings.dte & IRCOMM_DTR)
1270 ret += sprintf(buf+ret, "DTR|");
1271 if (self->settings.dte)
1272 ret--;
1273 ret += sprintf(buf+ret, "\n");
1274
1275 ret += sprintf(buf+ret, "DCE status: ");
1276 if (self->settings.dce & IRCOMM_CTS)
1277 ret += sprintf(buf+ret, "CTS|");
1278 if (self->settings.dce & IRCOMM_DSR)
1279 ret += sprintf(buf+ret, "DSR|");
1280 if (self->settings.dce & IRCOMM_CD)
1281 ret += sprintf(buf+ret, "CD|");
1282 if (self->settings.dce & IRCOMM_RI)
1283 ret += sprintf(buf+ret, "RI|");
1284 if (self->settings.dce)
1285 ret--;
1286 ret += sprintf(buf+ret, "\n");
1287
1288 ret += sprintf(buf+ret, "Configuration: ");
1289 if (!self->settings.null_modem)
1290 ret += sprintf(buf+ret, "DTE <-> DCE\n");
1291 else
1292 ret += sprintf(buf+ret,
1293 "DTE <-> DTE (null modem emulation)\n");
1294
1295 ret += sprintf(buf+ret, "Data rate: %d\n", self->settings.data_rate);
1296
1297 ret += sprintf(buf+ret, "Flow control: ");
1298 if (self->settings.flow_control & IRCOMM_XON_XOFF_IN)
1299 ret += sprintf(buf+ret, "XON_XOFF_IN|");
1300 if (self->settings.flow_control & IRCOMM_XON_XOFF_OUT)
1301 ret += sprintf(buf+ret, "XON_XOFF_OUT|");
1302 if (self->settings.flow_control & IRCOMM_RTS_CTS_IN)
1303 ret += sprintf(buf+ret, "RTS_CTS_IN|");
1304 if (self->settings.flow_control & IRCOMM_RTS_CTS_OUT)
1305 ret += sprintf(buf+ret, "RTS_CTS_OUT|");
1306 if (self->settings.flow_control & IRCOMM_DSR_DTR_IN)
1307 ret += sprintf(buf+ret, "DSR_DTR_IN|");
1308 if (self->settings.flow_control & IRCOMM_DSR_DTR_OUT)
1309 ret += sprintf(buf+ret, "DSR_DTR_OUT|");
1310 if (self->settings.flow_control & IRCOMM_ENQ_ACK_IN)
1311 ret += sprintf(buf+ret, "ENQ_ACK_IN|");
1312 if (self->settings.flow_control & IRCOMM_ENQ_ACK_OUT)
1313 ret += sprintf(buf+ret, "ENQ_ACK_OUT|");
1314 if (self->settings.flow_control)
1315 ret--;
1316 ret += sprintf(buf+ret, "\n");
1317
1318 ret += sprintf(buf+ret, "Flags: ");
1319 if (self->flags & ASYNC_CTS_FLOW)
1320 ret += sprintf(buf+ret, "ASYNC_CTS_FLOW|");
1321 if (self->flags & ASYNC_CHECK_CD)
1322 ret += sprintf(buf+ret, "ASYNC_CHECK_CD|");
1323 if (self->flags & ASYNC_INITIALIZED)
1324 ret += sprintf(buf+ret, "ASYNC_INITIALIZED|");
1325 if (self->flags & ASYNC_LOW_LATENCY)
1326 ret += sprintf(buf+ret, "ASYNC_LOW_LATENCY|");
1327 if (self->flags & ASYNC_CLOSING)
1328 ret += sprintf(buf+ret, "ASYNC_CLOSING|");
1329 if (self->flags & ASYNC_NORMAL_ACTIVE)
1330 ret += sprintf(buf+ret, "ASYNC_NORMAL_ACTIVE|");
1331 if (self->flags)
1332 ret--;
1333 ret += sprintf(buf+ret, "\n");
1334
1335 ret += sprintf(buf+ret, "Role: %s\n", self->client ?
1336 "client" : "server");
1337 ret += sprintf(buf+ret, "Open count: %d\n", self->open_count);
1338 ret += sprintf(buf+ret, "Max data size: %d\n", self->max_data_size);
1339 ret += sprintf(buf+ret, "Max header size: %d\n", self->max_header_size);
1340
1341 if (self->tty)
1342 ret += sprintf(buf+ret, "Hardware: %s\n",
1343 self->tty->hw_stopped ? "Stopped" : "Running");
1344
1345 ret += sprintf(buf+ret, "\n");
1346 return ret;
1347}
1348
1349
1350
1351
1352
1353
1354
1355
1356static int ircomm_tty_read_proc(char *buf, char **start, off_t offset, int len,
1357 int *eof, void *unused)
1358{
1359 struct ircomm_tty_cb *self;
1360 int count = 0, l;
1361 off_t begin = 0;
1362 unsigned long flags;
1363
1364 spin_lock_irqsave(&ircomm_tty->hb_spinlock, flags);
1365
1366 self = (struct ircomm_tty_cb *) hashbin_get_first(ircomm_tty);
1367 while ((self != NULL) && (count < 4000)) {
1368 if (self->magic != IRCOMM_TTY_MAGIC)
1369 break;
1370
1371 l = ircomm_tty_line_info(self, buf + count);
1372 count += l;
1373 if (count+begin > offset+len)
1374 goto done;
1375 if (count+begin < offset) {
1376 begin += count;
1377 count = 0;
1378 }
1379
1380 self = (struct ircomm_tty_cb *) hashbin_get_next(ircomm_tty);
1381 }
1382 *eof = 1;
1383done:
1384 spin_unlock_irqrestore(&ircomm_tty->hb_spinlock, flags);
1385
1386 if (offset >= count+begin)
1387 return 0;
1388 *start = buf + (offset-begin);
1389 return ((len < begin+count-offset) ? len : begin+count-offset);
1390}
1391#endif
1392
1393MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1394MODULE_DESCRIPTION("IrCOMM serial TTY driver");
1395MODULE_LICENSE("GPL");
1396MODULE_ALIAS_CHARDEV_MAJOR(IRCOMM_TTY_MAJOR);
1397
1398module_init(ircomm_tty_init);
1399module_exit(ircomm_tty_cleanup);
1400