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#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/slab.h>
32#include <linux/tty.h>
33#include <linux/init.h>
34#include <asm/uaccess.h>
35#include <linux/delay.h>
36#include <linux/mutex.h>
37
38#include <net/irda/irda.h>
39#include <net/irda/irda_device.h>
40
41#include "sir-dev.h"
42#include "irtty-sir.h"
43
44static int qos_mtt_bits = 0x03;
45
46module_param(qos_mtt_bits, int, 0);
47MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
48
49
50
51
52
53
54
55
56
57
58
59
60static int irtty_chars_in_buffer(struct sir_dev *dev)
61{
62 struct sirtty_cb *priv = dev->priv;
63
64 IRDA_ASSERT(priv != NULL, return -1;);
65 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
66
67 return tty_chars_in_buffer(priv->tty);
68}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85#define USBSERIAL_TX_DONE_DELAY 60
86
87static void irtty_wait_until_sent(struct sir_dev *dev)
88{
89 struct sirtty_cb *priv = dev->priv;
90 struct tty_struct *tty;
91
92 IRDA_ASSERT(priv != NULL, return;);
93 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
94
95 tty = priv->tty;
96 if (tty->ops->wait_until_sent) {
97 tty->ops->wait_until_sent(tty, msecs_to_jiffies(100));
98 }
99 else {
100 msleep(USBSERIAL_TX_DONE_DELAY);
101 }
102}
103
104
105
106
107
108
109
110
111
112
113
114static int irtty_change_speed(struct sir_dev *dev, unsigned speed)
115{
116 struct sirtty_cb *priv = dev->priv;
117 struct tty_struct *tty;
118 struct ktermios old_termios;
119 int cflag;
120
121 IRDA_ASSERT(priv != NULL, return -1;);
122 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
123
124 tty = priv->tty;
125
126 down_write(&tty->termios_rwsem);
127 old_termios = tty->termios;
128 cflag = tty->termios.c_cflag;
129 tty_encode_baud_rate(tty, speed, speed);
130 if (tty->ops->set_termios)
131 tty->ops->set_termios(tty, &old_termios);
132 priv->io.speed = speed;
133 up_write(&tty->termios_rwsem);
134
135 return 0;
136}
137
138
139
140
141
142
143
144
145static int irtty_set_dtr_rts(struct sir_dev *dev, int dtr, int rts)
146{
147 struct sirtty_cb *priv = dev->priv;
148 int set = 0;
149 int clear = 0;
150
151 IRDA_ASSERT(priv != NULL, return -1;);
152 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
153
154 if (rts)
155 set |= TIOCM_RTS;
156 else
157 clear |= TIOCM_RTS;
158 if (dtr)
159 set |= TIOCM_DTR;
160 else
161 clear |= TIOCM_DTR;
162
163
164
165
166
167
168
169 IRDA_ASSERT(priv->tty->ops->tiocmset != NULL, return -1;);
170 priv->tty->ops->tiocmset(priv->tty, set, clear);
171
172 return 0;
173}
174
175
176
177
178
179
180
181
182static int irtty_do_write(struct sir_dev *dev, const unsigned char *ptr, size_t len)
183{
184 struct sirtty_cb *priv = dev->priv;
185 struct tty_struct *tty;
186 int writelen;
187
188 IRDA_ASSERT(priv != NULL, return -1;);
189 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -1;);
190
191 tty = priv->tty;
192 if (!tty->ops->write)
193 return 0;
194 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
195 writelen = tty_write_room(tty);
196 if (writelen > len)
197 writelen = len;
198 return tty->ops->write(tty, ptr, writelen);
199}
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219static void irtty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
220 char *fp, int count)
221{
222 struct sir_dev *dev;
223 struct sirtty_cb *priv = tty->disc_data;
224 int i;
225
226 IRDA_ASSERT(priv != NULL, return;);
227 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
228
229 if (unlikely(count==0))
230 return;
231
232 dev = priv->dev;
233 if (!dev) {
234 net_warn_ratelimited("%s(), not ready yet!\n", __func__);
235 return;
236 }
237
238 for (i = 0; i < count; i++) {
239
240
241
242 if (fp && *fp++) {
243 pr_debug("Framing or parity error!\n");
244 sirdev_receive(dev, NULL, 0);
245 return;
246 }
247 }
248
249 sirdev_receive(dev, cp, count);
250}
251
252
253
254
255
256
257
258
259static void irtty_write_wakeup(struct tty_struct *tty)
260{
261 struct sirtty_cb *priv = tty->disc_data;
262
263 IRDA_ASSERT(priv != NULL, return;);
264 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
265
266 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
267 if (priv->dev)
268 sirdev_write_complete(priv->dev);
269}
270
271
272
273
274
275
276
277
278static inline void irtty_stop_receiver(struct tty_struct *tty, int stop)
279{
280 struct ktermios old_termios;
281 int cflag;
282
283 down_write(&tty->termios_rwsem);
284 old_termios = tty->termios;
285 cflag = tty->termios.c_cflag;
286
287 if (stop)
288 cflag &= ~CREAD;
289 else
290 cflag |= CREAD;
291
292 tty->termios.c_cflag = cflag;
293 if (tty->ops->set_termios)
294 tty->ops->set_termios(tty, &old_termios);
295 up_write(&tty->termios_rwsem);
296}
297
298
299
300
301static DEFINE_MUTEX(irtty_mutex);
302
303
304
305static int irtty_start_dev(struct sir_dev *dev)
306{
307 struct sirtty_cb *priv;
308 struct tty_struct *tty;
309
310
311 mutex_lock(&irtty_mutex);
312
313 priv = dev->priv;
314 if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
315 mutex_unlock(&irtty_mutex);
316 return -ESTALE;
317 }
318
319 tty = priv->tty;
320
321 if (tty->ops->start)
322 tty->ops->start(tty);
323
324 irtty_stop_receiver(tty, FALSE);
325
326 mutex_unlock(&irtty_mutex);
327 return 0;
328}
329
330
331
332static int irtty_stop_dev(struct sir_dev *dev)
333{
334 struct sirtty_cb *priv;
335 struct tty_struct *tty;
336
337
338 mutex_lock(&irtty_mutex);
339
340 priv = dev->priv;
341 if (unlikely(!priv || priv->magic!=IRTTY_MAGIC)) {
342 mutex_unlock(&irtty_mutex);
343 return -ESTALE;
344 }
345
346 tty = priv->tty;
347
348
349 irtty_stop_receiver(tty, TRUE);
350 if (tty->ops->stop)
351 tty->ops->stop(tty);
352
353 mutex_unlock(&irtty_mutex);
354
355 return 0;
356}
357
358
359
360static struct sir_driver sir_tty_drv = {
361 .owner = THIS_MODULE,
362 .driver_name = "sir_tty",
363 .start_dev = irtty_start_dev,
364 .stop_dev = irtty_stop_dev,
365 .do_write = irtty_do_write,
366 .chars_in_buffer = irtty_chars_in_buffer,
367 .wait_until_sent = irtty_wait_until_sent,
368 .set_speed = irtty_change_speed,
369 .set_dtr_rts = irtty_set_dtr_rts,
370};
371
372
373
374
375
376
377
378
379
380static int irtty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
381{
382 struct irtty_info { char name[6]; } info;
383 struct sir_dev *dev;
384 struct sirtty_cb *priv = tty->disc_data;
385 int err = 0;
386
387 IRDA_ASSERT(priv != NULL, return -ENODEV;);
388 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return -EBADR;);
389
390 pr_debug("%s(cmd=0x%X)\n", __func__, cmd);
391
392 dev = priv->dev;
393 IRDA_ASSERT(dev != NULL, return -1;);
394
395 switch (cmd) {
396 case IRTTY_IOCTDONGLE:
397
398 err = sirdev_set_dongle(dev, (IRDA_DONGLE) arg);
399 break;
400
401 case IRTTY_IOCGET:
402 IRDA_ASSERT(dev->netdev != NULL, return -1;);
403
404 memset(&info, 0, sizeof(info));
405 strncpy(info.name, dev->netdev->name, sizeof(info.name)-1);
406
407 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
408 err = -EFAULT;
409 break;
410 default:
411 err = tty_mode_ioctl(tty, file, cmd, arg);
412 break;
413 }
414 return err;
415}
416
417
418
419
420
421
422
423
424
425static int irtty_open(struct tty_struct *tty)
426{
427 struct sir_dev *dev;
428 struct sirtty_cb *priv;
429 int ret = 0;
430
431
432
433
434 if (tty->disc_data != NULL) {
435 priv = tty->disc_data;
436 if (priv && priv->magic == IRTTY_MAGIC) {
437 ret = -EEXIST;
438 goto out;
439 }
440 tty->disc_data = NULL;
441 }
442
443
444 irtty_stop_receiver(tty, TRUE);
445 if (tty->ops->stop)
446 tty->ops->stop(tty);
447
448 tty_driver_flush_buffer(tty);
449
450
451 sir_tty_drv.qos_mtt_bits = qos_mtt_bits;
452
453
454 dev = sirdev_get_instance(&sir_tty_drv, tty->name);
455 if (!dev) {
456 ret = -ENODEV;
457 goto out;
458 }
459
460
461 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
462 if (!priv) {
463 ret = -ENOMEM;
464 goto out_put;
465 }
466
467 priv->magic = IRTTY_MAGIC;
468 priv->tty = tty;
469 priv->dev = dev;
470
471
472 mutex_lock(&irtty_mutex);
473
474 dev->priv = priv;
475 tty->disc_data = priv;
476 tty->receive_room = 65536;
477
478 mutex_unlock(&irtty_mutex);
479
480 pr_debug("%s - %s: irda line discipline opened\n", __func__, tty->name);
481
482 return 0;
483
484out_put:
485 sirdev_put_instance(dev);
486out:
487 return ret;
488}
489
490
491
492
493
494
495
496
497static void irtty_close(struct tty_struct *tty)
498{
499 struct sirtty_cb *priv = tty->disc_data;
500
501 IRDA_ASSERT(priv != NULL, return;);
502 IRDA_ASSERT(priv->magic == IRTTY_MAGIC, return;);
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 tty->disc_data = NULL;
521
522 sirdev_put_instance(priv->dev);
523
524
525 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
526 if (tty->ops->stop)
527 tty->ops->stop(tty);
528
529 kfree(priv);
530
531 pr_debug("%s - %s: irda line discipline closed\n", __func__, tty->name);
532}
533
534
535
536static struct tty_ldisc_ops irda_ldisc = {
537 .magic = TTY_LDISC_MAGIC,
538 .name = "irda",
539 .flags = 0,
540 .open = irtty_open,
541 .close = irtty_close,
542 .read = NULL,
543 .write = NULL,
544 .ioctl = irtty_ioctl,
545 .poll = NULL,
546 .receive_buf = irtty_receive_buf,
547 .write_wakeup = irtty_write_wakeup,
548 .owner = THIS_MODULE,
549};
550
551
552
553static int __init irtty_sir_init(void)
554{
555 int err;
556
557 if ((err = tty_register_ldisc(N_IRDA, &irda_ldisc)) != 0)
558 net_err_ratelimited("IrDA: can't register line discipline (err = %d)\n",
559 err);
560 return err;
561}
562
563static void __exit irtty_sir_cleanup(void)
564{
565 int err;
566
567 if ((err = tty_unregister_ldisc(N_IRDA))) {
568 net_err_ratelimited("%s(), can't unregister line discipline (err = %d)\n",
569 __func__, err);
570 }
571}
572
573module_init(irtty_sir_init);
574module_exit(irtty_sir_cleanup);
575
576MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
577MODULE_DESCRIPTION("IrDA TTY device driver");
578MODULE_ALIAS_LDISC(N_IRDA);
579MODULE_LICENSE("GPL");
580
581