]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/isdn/gigaset/interface.c
Merge branch 'for-2.6.33' of git://linux-nfs.org/~bfields/linux
[mirror_ubuntu-zesty-kernel.git] / drivers / isdn / gigaset / interface.c
1 /*
2 * interface to user space for the gigaset driver
3 *
4 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
5 *
6 * =====================================================================
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
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 /*** our ioctls ***/
20
21 static 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
62 static 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
99 static 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 /*** the terminal driver ***/
119 /* stolen from usbserial and some other tty drivers */
120
121 static int if_open(struct tty_struct *tty, struct file *filp);
122 static void if_close(struct tty_struct *tty, struct file *filp);
123 static int if_ioctl(struct tty_struct *tty, struct file *file,
124 unsigned int cmd, unsigned long arg);
125 static int if_write_room(struct tty_struct *tty);
126 static int if_chars_in_buffer(struct tty_struct *tty);
127 static void if_throttle(struct tty_struct *tty);
128 static void if_unthrottle(struct tty_struct *tty);
129 static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
130 static int if_tiocmget(struct tty_struct *tty, struct file *file);
131 static int if_tiocmset(struct tty_struct *tty, struct file *file,
132 unsigned int set, unsigned int clear);
133 static int if_write(struct tty_struct *tty,
134 const unsigned char *buf, int count);
135
136 static 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
150 static 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
181 static 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"); /* nothing to do */
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
213 static 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
288 static 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 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
305
306 mutex_unlock(&cs->mutex);
307
308 return retval;
309 }
310
311 static int if_tiocmset(struct tty_struct *tty, struct file *file,
312 unsigned int set, unsigned int clear)
313 {
314 struct cardstate *cs;
315 int retval;
316 unsigned mc;
317
318 cs = (struct cardstate *) tty->driver_data;
319 if (!cs) {
320 pr_err("%s: no cardstate\n", __func__);
321 return -ENODEV;
322 }
323
324 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
325 cs->minor_index, __func__, set, clear);
326
327 if (mutex_lock_interruptible(&cs->mutex))
328 return -ERESTARTSYS;
329
330 if (!cs->connected) {
331 gig_dbg(DEBUG_IF, "not connected");
332 retval = -ENODEV;
333 } else {
334 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
335 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
336 cs->control_state = mc;
337 }
338
339 mutex_unlock(&cs->mutex);
340
341 return retval;
342 }
343
344 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
345 {
346 struct cardstate *cs;
347 int retval = -ENODEV;
348
349 cs = (struct cardstate *) tty->driver_data;
350 if (!cs) {
351 pr_err("%s: no cardstate\n", __func__);
352 return -ENODEV;
353 }
354
355 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
356
357 if (mutex_lock_interruptible(&cs->mutex))
358 return -ERESTARTSYS;
359
360 if (!cs->connected) {
361 gig_dbg(DEBUG_IF, "not connected");
362 retval = -ENODEV;
363 } else if (!cs->open_count)
364 dev_warn(cs->dev, "%s: device not opened\n", __func__);
365 else if (cs->mstate != MS_LOCKED) {
366 dev_warn(cs->dev, "can't write to unlocked device\n");
367 retval = -EBUSY;
368 } else {
369 retval = cs->ops->write_cmd(cs, buf, count,
370 &cs->if_wake_tasklet);
371 }
372
373 mutex_unlock(&cs->mutex);
374
375 return retval;
376 }
377
378 static int if_write_room(struct tty_struct *tty)
379 {
380 struct cardstate *cs;
381 int retval = -ENODEV;
382
383 cs = (struct cardstate *) tty->driver_data;
384 if (!cs) {
385 pr_err("%s: no cardstate\n", __func__);
386 return -ENODEV;
387 }
388
389 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
390
391 if (mutex_lock_interruptible(&cs->mutex))
392 return -ERESTARTSYS;
393
394 if (!cs->connected) {
395 gig_dbg(DEBUG_IF, "not connected");
396 retval = -ENODEV;
397 } else if (!cs->open_count)
398 dev_warn(cs->dev, "%s: device not opened\n", __func__);
399 else if (cs->mstate != MS_LOCKED) {
400 dev_warn(cs->dev, "can't write to unlocked device\n");
401 retval = -EBUSY;
402 } else
403 retval = cs->ops->write_room(cs);
404
405 mutex_unlock(&cs->mutex);
406
407 return retval;
408 }
409
410 static int if_chars_in_buffer(struct tty_struct *tty)
411 {
412 struct cardstate *cs;
413 int retval = 0;
414
415 cs = (struct cardstate *) tty->driver_data;
416 if (!cs) {
417 pr_err("%s: no cardstate\n", __func__);
418 return 0;
419 }
420
421 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
422
423 mutex_lock(&cs->mutex);
424
425 if (!cs->connected)
426 gig_dbg(DEBUG_IF, "not connected");
427 else if (!cs->open_count)
428 dev_warn(cs->dev, "%s: device not opened\n", __func__);
429 else if (cs->mstate != MS_LOCKED)
430 dev_warn(cs->dev, "can't write to unlocked device\n");
431 else
432 retval = cs->ops->chars_in_buffer(cs);
433
434 mutex_unlock(&cs->mutex);
435
436 return retval;
437 }
438
439 static void if_throttle(struct tty_struct *tty)
440 {
441 struct cardstate *cs;
442
443 cs = (struct cardstate *) tty->driver_data;
444 if (!cs) {
445 pr_err("%s: no cardstate\n", __func__);
446 return;
447 }
448
449 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
450
451 mutex_lock(&cs->mutex);
452
453 if (!cs->connected)
454 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
455 else if (!cs->open_count)
456 dev_warn(cs->dev, "%s: device not opened\n", __func__);
457 else
458 gig_dbg(DEBUG_ANY, "%s: not implemented\n", __func__);
459
460 mutex_unlock(&cs->mutex);
461 }
462
463 static void if_unthrottle(struct tty_struct *tty)
464 {
465 struct cardstate *cs;
466
467 cs = (struct cardstate *) tty->driver_data;
468 if (!cs) {
469 pr_err("%s: no cardstate\n", __func__);
470 return;
471 }
472
473 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
474
475 mutex_lock(&cs->mutex);
476
477 if (!cs->connected)
478 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
479 else if (!cs->open_count)
480 dev_warn(cs->dev, "%s: device not opened\n", __func__);
481 else
482 gig_dbg(DEBUG_ANY, "%s: not implemented\n", __func__);
483
484 mutex_unlock(&cs->mutex);
485 }
486
487 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
488 {
489 struct cardstate *cs;
490 unsigned int iflag;
491 unsigned int cflag;
492 unsigned int old_cflag;
493 unsigned int control_state, new_state;
494
495 cs = (struct cardstate *) tty->driver_data;
496 if (!cs) {
497 pr_err("%s: no cardstate\n", __func__);
498 return;
499 }
500
501 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
502
503 mutex_lock(&cs->mutex);
504
505 if (!cs->connected) {
506 gig_dbg(DEBUG_IF, "not connected");
507 goto out;
508 }
509
510 if (!cs->open_count) {
511 dev_warn(cs->dev, "%s: device not opened\n", __func__);
512 goto out;
513 }
514
515 iflag = tty->termios->c_iflag;
516 cflag = tty->termios->c_cflag;
517 old_cflag = old ? old->c_cflag : cflag;
518 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
519 cs->minor_index, iflag, cflag, old_cflag);
520
521 /* get a local copy of the current port settings */
522 control_state = cs->control_state;
523
524 /*
525 * Update baud rate.
526 * Do not attempt to cache old rates and skip settings,
527 * disconnects screw such tricks up completely.
528 * Premature optimization is the root of all evil.
529 */
530
531 /* reassert DTR and (maybe) RTS on transition from B0 */
532 if ((old_cflag & CBAUD) == B0) {
533 new_state = control_state | TIOCM_DTR;
534 /* don't set RTS if using hardware flow control */
535 if (!(old_cflag & CRTSCTS))
536 new_state |= TIOCM_RTS;
537 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
538 cs->minor_index,
539 (new_state & TIOCM_RTS) ? " only" : "/RTS");
540 cs->ops->set_modem_ctrl(cs, control_state, new_state);
541 control_state = new_state;
542 }
543
544 cs->ops->baud_rate(cs, cflag & CBAUD);
545
546 if ((cflag & CBAUD) == B0) {
547 /* Drop RTS and DTR */
548 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
549 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
550 cs->ops->set_modem_ctrl(cs, control_state, new_state);
551 control_state = new_state;
552 }
553
554 /*
555 * Update line control register (LCR)
556 */
557
558 cs->ops->set_line_ctrl(cs, cflag);
559
560 /* save off the modified port settings */
561 cs->control_state = control_state;
562
563 out:
564 mutex_unlock(&cs->mutex);
565 }
566
567
568 /* wakeup tasklet for the write operation */
569 static void if_wake(unsigned long data)
570 {
571 struct cardstate *cs = (struct cardstate *) data;
572
573 if (cs->tty)
574 tty_wakeup(cs->tty);
575 }
576
577 /*** interface to common ***/
578
579 void gigaset_if_init(struct cardstate *cs)
580 {
581 struct gigaset_driver *drv;
582
583 drv = cs->driver;
584 if (!drv->have_tty)
585 return;
586
587 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
588
589 mutex_lock(&cs->mutex);
590 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
591
592 if (!IS_ERR(cs->tty_dev))
593 dev_set_drvdata(cs->tty_dev, cs);
594 else {
595 pr_warning("could not register device to the tty subsystem\n");
596 cs->tty_dev = NULL;
597 }
598 mutex_unlock(&cs->mutex);
599 }
600
601 void gigaset_if_free(struct cardstate *cs)
602 {
603 struct gigaset_driver *drv;
604
605 drv = cs->driver;
606 if (!drv->have_tty)
607 return;
608
609 tasklet_disable(&cs->if_wake_tasklet);
610 tasklet_kill(&cs->if_wake_tasklet);
611 cs->tty_dev = NULL;
612 tty_unregister_device(drv->tty, cs->minor_index);
613 }
614
615 /**
616 * gigaset_if_receive() - pass a received block of data to the tty device
617 * @cs: device descriptor structure.
618 * @buffer: received data.
619 * @len: number of bytes received.
620 *
621 * Called by asyncdata/isocdata if a block of data received from the
622 * device must be sent to userspace through the ttyG* device.
623 */
624 void gigaset_if_receive(struct cardstate *cs,
625 unsigned char *buffer, size_t len)
626 {
627 unsigned long flags;
628 struct tty_struct *tty;
629
630 spin_lock_irqsave(&cs->lock, flags);
631 tty = cs->tty;
632 if (tty == NULL)
633 gig_dbg(DEBUG_ANY, "receive on closed device");
634 else {
635 tty_buffer_request_room(tty, len);
636 tty_insert_flip_string(tty, buffer, len);
637 tty_flip_buffer_push(tty);
638 }
639 spin_unlock_irqrestore(&cs->lock, flags);
640 }
641 EXPORT_SYMBOL_GPL(gigaset_if_receive);
642
643 /* gigaset_if_initdriver
644 * Initialize tty interface.
645 * parameters:
646 * drv Driver
647 * procname Name of the driver (e.g. for /proc/tty/drivers)
648 * devname Name of the device files (prefix without minor number)
649 */
650 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
651 const char *devname)
652 {
653 unsigned minors = drv->minors;
654 int ret;
655 struct tty_driver *tty;
656
657 drv->have_tty = 0;
658
659 drv->tty = tty = alloc_tty_driver(minors);
660 if (tty == NULL)
661 goto enomem;
662
663 tty->magic = TTY_DRIVER_MAGIC,
664 tty->major = GIG_MAJOR,
665 tty->type = TTY_DRIVER_TYPE_SERIAL,
666 tty->subtype = SERIAL_TYPE_NORMAL,
667 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
668
669 tty->driver_name = procname;
670 tty->name = devname;
671 tty->minor_start = drv->minor;
672 tty->num = drv->minors;
673
674 tty->owner = THIS_MODULE;
675
676 tty->init_termios = tty_std_termios;
677 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
678 tty_set_operations(tty, &if_ops);
679
680 ret = tty_register_driver(tty);
681 if (ret < 0) {
682 pr_err("error %d registering tty driver\n", ret);
683 goto error;
684 }
685 gig_dbg(DEBUG_IF, "tty driver initialized");
686 drv->have_tty = 1;
687 return;
688
689 enomem:
690 pr_err("out of memory\n");
691 error:
692 if (drv->tty)
693 put_tty_driver(drv->tty);
694 }
695
696 void gigaset_if_freedriver(struct gigaset_driver *drv)
697 {
698 if (!drv->have_tty)
699 return;
700
701 drv->have_tty = 0;
702 tty_unregister_driver(drv->tty);
703 put_tty_driver(drv->tty);
704 }