1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "gigaset.h"
15#include <linux/gigaset_dev.h>
16#include <linux/tty.h>
17#include <linux/tty_flip.h>
18
19
20
21static int if_lock(struct cardstate *cs, int *arg)
22{
23 int cmd = *arg;
24
25 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
26
27 if (cmd > 1)
28 return -EINVAL;
29
30 if (cmd < 0) {
31 *arg = cs->mstate == MS_LOCKED;
32 return 0;
33 }
34
35 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
36 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
37 cs->ops->baud_rate(cs, B115200);
38 cs->ops->set_line_ctrl(cs, CS8);
39 cs->control_state = TIOCM_DTR|TIOCM_RTS;
40 }
41
42 cs->waiting = 1;
43 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
44 NULL, cmd, NULL)) {
45 cs->waiting = 0;
46 return -ENOMEM;
47 }
48
49 gig_dbg(DEBUG_CMD, "scheduling IF_LOCK");
50 gigaset_schedule_event(cs);
51
52 wait_event(cs->waitqueue, !cs->waiting);
53
54 if (cs->cmd_result >= 0) {
55 *arg = cs->cmd_result;
56 return 0;
57 }
58
59 return cs->cmd_result;
60}
61
62static int if_version(struct cardstate *cs, unsigned arg[4])
63{
64 static const unsigned version[4] = GIG_VERSION;
65 static const unsigned compat[4] = GIG_COMPAT;
66 unsigned cmd = arg[0];
67
68 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
69
70 switch (cmd) {
71 case GIGVER_DRIVER:
72 memcpy(arg, version, sizeof version);
73 return 0;
74 case GIGVER_COMPAT:
75 memcpy(arg, compat, sizeof compat);
76 return 0;
77 case GIGVER_FWBASE:
78 cs->waiting = 1;
79 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
80 NULL, 0, arg)) {
81 cs->waiting = 0;
82 return -ENOMEM;
83 }
84
85 gig_dbg(DEBUG_CMD, "scheduling IF_VER");
86 gigaset_schedule_event(cs);
87
88 wait_event(cs->waitqueue, !cs->waiting);
89
90 if (cs->cmd_result >= 0)
91 return 0;
92
93 return cs->cmd_result;
94 default:
95 return -EINVAL;
96 }
97}
98
99static int if_config(struct cardstate *cs, int *arg)
100{
101 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
102
103 if (*arg != 1)
104 return -EINVAL;
105
106 if (cs->mstate != MS_LOCKED)
107 return -EBUSY;
108
109 if (!cs->connected) {
110 pr_err("%s: not connected\n", __func__);
111 return -ENODEV;
112 }
113
114 *arg = 0;
115 return gigaset_enterconfigmode(cs);
116}
117
118
119
120
121static int if_open(struct tty_struct *tty, struct file *filp);
122static void if_close(struct tty_struct *tty, struct file *filp);
123static int if_ioctl(struct tty_struct *tty, struct file *file,
124 unsigned int cmd, unsigned long arg);
125static int if_write_room(struct tty_struct *tty);
126static int if_chars_in_buffer(struct tty_struct *tty);
127static void if_throttle(struct tty_struct *tty);
128static void if_unthrottle(struct tty_struct *tty);
129static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
130static int if_tiocmget(struct tty_struct *tty, struct file *file);
131static int if_tiocmset(struct tty_struct *tty, struct file *file,
132 unsigned int set, unsigned int clear);
133static int if_write(struct tty_struct *tty,
134 const unsigned char *buf, int count);
135
136static const struct tty_operations if_ops = {
137 .open = if_open,
138 .close = if_close,
139 .ioctl = if_ioctl,
140 .write = if_write,
141 .write_room = if_write_room,
142 .chars_in_buffer = if_chars_in_buffer,
143 .set_termios = if_set_termios,
144 .throttle = if_throttle,
145 .unthrottle = if_unthrottle,
146 .tiocmget = if_tiocmget,
147 .tiocmset = if_tiocmset,
148};
149
150static int if_open(struct tty_struct *tty, struct file *filp)
151{
152 struct cardstate *cs;
153 unsigned long flags;
154
155 gig_dbg(DEBUG_IF, "%d+%d: %s()",
156 tty->driver->minor_start, tty->index, __func__);
157
158 tty->driver_data = NULL;
159
160 cs = gigaset_get_cs_by_tty(tty);
161 if (!cs || !try_module_get(cs->driver->owner))
162 return -ENODEV;
163
164 if (mutex_lock_interruptible(&cs->mutex))
165 return -ERESTARTSYS;
166 tty->driver_data = cs;
167
168 ++cs->open_count;
169
170 if (cs->open_count == 1) {
171 spin_lock_irqsave(&cs->lock, flags);
172 cs->tty = tty;
173 spin_unlock_irqrestore(&cs->lock, flags);
174 tty->low_latency = 1;
175 }
176
177 mutex_unlock(&cs->mutex);
178 return 0;
179}
180
181static void if_close(struct tty_struct *tty, struct file *filp)
182{
183 struct cardstate *cs;
184 unsigned long flags;
185
186 cs = (struct cardstate *) tty->driver_data;
187 if (!cs) {
188 pr_err("%s: no cardstate\n", __func__);
189 return;
190 }
191
192 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
193
194 mutex_lock(&cs->mutex);
195
196 if (!cs->connected)
197 gig_dbg(DEBUG_IF, "not connected");
198 else if (!cs->open_count)
199 dev_warn(cs->dev, "%s: device not opened\n", __func__);
200 else {
201 if (!--cs->open_count) {
202 spin_lock_irqsave(&cs->lock, flags);
203 cs->tty = NULL;
204 spin_unlock_irqrestore(&cs->lock, flags);
205 }
206 }
207
208 mutex_unlock(&cs->mutex);
209
210 module_put(cs->driver->owner);
211}
212
213static int if_ioctl(struct tty_struct *tty, struct file *file,
214 unsigned int cmd, unsigned long arg)
215{
216 struct cardstate *cs;
217 int retval = -ENODEV;
218 int int_arg;
219 unsigned char buf[6];
220 unsigned version[4];
221
222 cs = (struct cardstate *) tty->driver_data;
223 if (!cs) {
224 pr_err("%s: no cardstate\n", __func__);
225 return -ENODEV;
226 }
227
228 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
229
230 if (mutex_lock_interruptible(&cs->mutex))
231 return -ERESTARTSYS;
232
233 if (!cs->connected) {
234 gig_dbg(DEBUG_IF, "not connected");
235 retval = -ENODEV;
236 } else if (!cs->open_count)
237 dev_warn(cs->dev, "%s: device not opened\n", __func__);
238 else {
239 retval = 0;
240 switch (cmd) {
241 case GIGASET_REDIR:
242 retval = get_user(int_arg, (int __user *) arg);
243 if (retval >= 0)
244 retval = if_lock(cs, &int_arg);
245 if (retval >= 0)
246 retval = put_user(int_arg, (int __user *) arg);
247 break;
248 case GIGASET_CONFIG:
249 retval = get_user(int_arg, (int __user *) arg);
250 if (retval >= 0)
251 retval = if_config(cs, &int_arg);
252 if (retval >= 0)
253 retval = put_user(int_arg, (int __user *) arg);
254 break;
255 case GIGASET_BRKCHARS:
256 retval = copy_from_user(&buf,
257 (const unsigned char __user *) arg, 6)
258 ? -EFAULT : 0;
259 if (retval >= 0) {
260 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
261 6, (const unsigned char *) arg);
262 retval = cs->ops->brkchars(cs, buf);
263 }
264 break;
265 case GIGASET_VERSION:
266 retval = copy_from_user(version,
267 (unsigned __user *) arg, sizeof version)
268 ? -EFAULT : 0;
269 if (retval >= 0)
270 retval = if_version(cs, version);
271 if (retval >= 0)
272 retval = copy_to_user((unsigned __user *) arg,
273 version, sizeof version)
274 ? -EFAULT : 0;
275 break;
276 default:
277 gig_dbg(DEBUG_ANY, "%s: arg not supported - 0x%04x",
278 __func__, cmd);
279 retval = -ENOIOCTLCMD;
280 }
281 }
282
283 mutex_unlock(&cs->mutex);
284
285 return retval;
286}
287
288static int if_tiocmget(struct tty_struct *tty, struct file *file)
289{
290 struct cardstate *cs;
291 int retval;
292
293 cs = (struct cardstate *) tty->driver_data;
294 if (!cs) {
295 pr_err("%s: no cardstate\n", __func__);
296 return -ENODEV;
297 }
298
299 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
300
301 if (mutex_lock_interruptible(&cs->mutex))
302 return -ERESTARTSYS;
303
304
305 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
306
307 mutex_unlock(&cs->mutex);
308
309 return retval;
310}
311
312static int if_tiocmset(struct tty_struct *tty, struct file *file,
313 unsigned int set, unsigned int clear)
314{
315 struct cardstate *cs;
316 int retval;
317 unsigned mc;
318
319 cs = (struct cardstate *) tty->driver_data;
320 if (!cs) {
321 pr_err("%s: no cardstate\n", __func__);
322 return -ENODEV;
323 }
324
325 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
326 cs->minor_index, __func__, set, clear);
327
328 if (mutex_lock_interruptible(&cs->mutex))
329 return -ERESTARTSYS;
330
331 if (!cs->connected) {
332 gig_dbg(DEBUG_IF, "not connected");
333 retval = -ENODEV;
334 } else {
335 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
336 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
337 cs->control_state = mc;
338 }
339
340 mutex_unlock(&cs->mutex);
341
342 return retval;
343}
344
345static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
346{
347 struct cardstate *cs;
348 int retval = -ENODEV;
349
350 cs = (struct cardstate *) tty->driver_data;
351 if (!cs) {
352 pr_err("%s: no cardstate\n", __func__);
353 return -ENODEV;
354 }
355
356 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
357
358 if (mutex_lock_interruptible(&cs->mutex))
359 return -ERESTARTSYS;
360
361 if (!cs->connected) {
362 gig_dbg(DEBUG_IF, "not connected");
363 retval = -ENODEV;
364 } else if (!cs->open_count)
365 dev_warn(cs->dev, "%s: device not opened\n", __func__);
366 else if (cs->mstate != MS_LOCKED) {
367 dev_warn(cs->dev, "can't write to unlocked device\n");
368 retval = -EBUSY;
369 } else {
370 retval = cs->ops->write_cmd(cs, buf, count,
371 &cs->if_wake_tasklet);
372 }
373
374 mutex_unlock(&cs->mutex);
375
376 return retval;
377}
378
379static int if_write_room(struct tty_struct *tty)
380{
381 struct cardstate *cs;
382 int retval = -ENODEV;
383
384 cs = (struct cardstate *) tty->driver_data;
385 if (!cs) {
386 pr_err("%s: no cardstate\n", __func__);
387 return -ENODEV;
388 }
389
390 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
391
392 if (mutex_lock_interruptible(&cs->mutex))
393 return -ERESTARTSYS;
394
395 if (!cs->connected) {
396 gig_dbg(DEBUG_IF, "not connected");
397 retval = -ENODEV;
398 } else if (!cs->open_count)
399 dev_warn(cs->dev, "%s: device not opened\n", __func__);
400 else if (cs->mstate != MS_LOCKED) {
401 dev_warn(cs->dev, "can't write to unlocked device\n");
402 retval = -EBUSY;
403 } else
404 retval = cs->ops->write_room(cs);
405
406 mutex_unlock(&cs->mutex);
407
408 return retval;
409}
410
411static int if_chars_in_buffer(struct tty_struct *tty)
412{
413 struct cardstate *cs;
414 int retval = 0;
415
416 cs = (struct cardstate *) tty->driver_data;
417 if (!cs) {
418 pr_err("%s: no cardstate\n", __func__);
419 return 0;
420 }
421
422 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
423
424 mutex_lock(&cs->mutex);
425
426 if (!cs->connected)
427 gig_dbg(DEBUG_IF, "not connected");
428 else if (!cs->open_count)
429 dev_warn(cs->dev, "%s: device not opened\n", __func__);
430 else if (cs->mstate != MS_LOCKED)
431 dev_warn(cs->dev, "can't write to unlocked device\n");
432 else
433 retval = cs->ops->chars_in_buffer(cs);
434
435 mutex_unlock(&cs->mutex);
436
437 return retval;
438}
439
440static void if_throttle(struct tty_struct *tty)
441{
442 struct cardstate *cs;
443
444 cs = (struct cardstate *) tty->driver_data;
445 if (!cs) {
446 pr_err("%s: no cardstate\n", __func__);
447 return;
448 }
449
450 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
451
452 mutex_lock(&cs->mutex);
453
454 if (!cs->connected)
455 gig_dbg(DEBUG_IF, "not connected");
456 else if (!cs->open_count)
457 dev_warn(cs->dev, "%s: device not opened\n", __func__);
458 else {
459
460 }
461
462 mutex_unlock(&cs->mutex);
463}
464
465static void if_unthrottle(struct tty_struct *tty)
466{
467 struct cardstate *cs;
468
469 cs = (struct cardstate *) tty->driver_data;
470 if (!cs) {
471 pr_err("%s: no cardstate\n", __func__);
472 return;
473 }
474
475 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
476
477 mutex_lock(&cs->mutex);
478
479 if (!cs->connected)
480 gig_dbg(DEBUG_IF, "not connected");
481 else if (!cs->open_count)
482 dev_warn(cs->dev, "%s: device not opened\n", __func__);
483 else {
484
485 }
486
487 mutex_unlock(&cs->mutex);
488}
489
490static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
491{
492 struct cardstate *cs;
493 unsigned int iflag;
494 unsigned int cflag;
495 unsigned int old_cflag;
496 unsigned int control_state, new_state;
497
498 cs = (struct cardstate *) tty->driver_data;
499 if (!cs) {
500 pr_err("%s: no cardstate\n", __func__);
501 return;
502 }
503
504 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
505
506 mutex_lock(&cs->mutex);
507
508 if (!cs->connected) {
509 gig_dbg(DEBUG_IF, "not connected");
510 goto out;
511 }
512
513 if (!cs->open_count) {
514 dev_warn(cs->dev, "%s: device not opened\n", __func__);
515 goto out;
516 }
517
518
519 iflag = tty->termios->c_iflag;
520 cflag = tty->termios->c_cflag;
521 old_cflag = old ? old->c_cflag : cflag;
522 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
523 cs->minor_index, iflag, cflag, old_cflag);
524
525
526 control_state = cs->control_state;
527
528
529
530
531
532
533
534
535
536 if ((old_cflag & CBAUD) == B0) {
537 new_state = control_state | TIOCM_DTR;
538
539 if (!(old_cflag & CRTSCTS))
540 new_state |= TIOCM_RTS;
541 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
542 cs->minor_index,
543 (new_state & TIOCM_RTS) ? " only" : "/RTS");
544 cs->ops->set_modem_ctrl(cs, control_state, new_state);
545 control_state = new_state;
546 }
547
548 cs->ops->baud_rate(cs, cflag & CBAUD);
549
550 if ((cflag & CBAUD) == B0) {
551
552 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
553 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
554 cs->ops->set_modem_ctrl(cs, control_state, new_state);
555 control_state = new_state;
556 }
557
558
559
560
561
562 cs->ops->set_line_ctrl(cs, cflag);
563
564
565 cs->control_state = control_state;
566
567out:
568 mutex_unlock(&cs->mutex);
569}
570
571
572
573static void if_wake(unsigned long data)
574{
575 struct cardstate *cs = (struct cardstate *) data;
576
577 if (cs->tty)
578 tty_wakeup(cs->tty);
579}
580
581
582
583void gigaset_if_init(struct cardstate *cs)
584{
585 struct gigaset_driver *drv;
586
587 drv = cs->driver;
588 if (!drv->have_tty)
589 return;
590
591 tasklet_init(&cs->if_wake_tasklet, &if_wake, (unsigned long) cs);
592
593 mutex_lock(&cs->mutex);
594 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
595
596 if (!IS_ERR(cs->tty_dev))
597 dev_set_drvdata(cs->tty_dev, cs);
598 else {
599 pr_warning("could not register device to the tty subsystem\n");
600 cs->tty_dev = NULL;
601 }
602 mutex_unlock(&cs->mutex);
603}
604
605void gigaset_if_free(struct cardstate *cs)
606{
607 struct gigaset_driver *drv;
608
609 drv = cs->driver;
610 if (!drv->have_tty)
611 return;
612
613 tasklet_disable(&cs->if_wake_tasklet);
614 tasklet_kill(&cs->if_wake_tasklet);
615 cs->tty_dev = NULL;
616 tty_unregister_device(drv->tty, cs->minor_index);
617}
618
619
620
621
622
623
624
625
626
627
628void gigaset_if_receive(struct cardstate *cs,
629 unsigned char *buffer, size_t len)
630{
631 unsigned long flags;
632 struct tty_struct *tty;
633
634 spin_lock_irqsave(&cs->lock, flags);
635 if ((tty = cs->tty) == NULL)
636 gig_dbg(DEBUG_ANY, "receive on closed device");
637 else {
638 tty_buffer_request_room(tty, len);
639 tty_insert_flip_string(tty, buffer, len);
640 tty_flip_buffer_push(tty);
641 }
642 spin_unlock_irqrestore(&cs->lock, flags);
643}
644EXPORT_SYMBOL_GPL(gigaset_if_receive);
645
646
647
648
649
650
651
652
653void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
654 const char *devname)
655{
656 unsigned minors = drv->minors;
657 int ret;
658 struct tty_driver *tty;
659
660 drv->have_tty = 0;
661
662 if ((drv->tty = alloc_tty_driver(minors)) == NULL)
663 goto enomem;
664 tty = drv->tty;
665
666 tty->magic = TTY_DRIVER_MAGIC,
667 tty->major = GIG_MAJOR,
668 tty->type = TTY_DRIVER_TYPE_SERIAL,
669 tty->subtype = SERIAL_TYPE_NORMAL,
670 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
671
672 tty->driver_name = procname;
673 tty->name = devname;
674 tty->minor_start = drv->minor;
675 tty->num = drv->minors;
676
677 tty->owner = THIS_MODULE;
678
679 tty->init_termios = tty_std_termios;
680 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
681 tty_set_operations(tty, &if_ops);
682
683 ret = tty_register_driver(tty);
684 if (ret < 0) {
685 pr_err("error %d registering tty driver\n", ret);
686 goto error;
687 }
688 gig_dbg(DEBUG_IF, "tty driver initialized");
689 drv->have_tty = 1;
690 return;
691
692enomem:
693 pr_err("out of memory\n");
694error:
695 if (drv->tty)
696 put_tty_driver(drv->tty);
697}
698
699void gigaset_if_freedriver(struct gigaset_driver *drv)
700{
701 if (!drv->have_tty)
702 return;
703
704 drv->have_tty = 0;
705 tty_unregister_driver(drv->tty);
706 put_tty_driver(drv->tty);
707}
708