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