]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/isdn/gigaset/interface.c
Merge tag 'xfs-4.20-merge-2' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[mirror_ubuntu-eoan-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_flip.h>
17 #include <linux/module.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 gigaset_schedule_event(cs);
49
50 wait_event(cs->waitqueue, !cs->waiting);
51
52 if (cs->cmd_result >= 0) {
53 *arg = cs->cmd_result;
54 return 0;
55 }
56
57 return cs->cmd_result;
58 }
59
60 static int if_version(struct cardstate *cs, unsigned arg[4])
61 {
62 static const unsigned version[4] = GIG_VERSION;
63 static const unsigned compat[4] = GIG_COMPAT;
64 unsigned cmd = arg[0];
65
66 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
67
68 switch (cmd) {
69 case GIGVER_DRIVER:
70 memcpy(arg, version, sizeof version);
71 return 0;
72 case GIGVER_COMPAT:
73 memcpy(arg, compat, sizeof compat);
74 return 0;
75 case GIGVER_FWBASE:
76 cs->waiting = 1;
77 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
78 NULL, 0, arg)) {
79 cs->waiting = 0;
80 return -ENOMEM;
81 }
82 gigaset_schedule_event(cs);
83
84 wait_event(cs->waitqueue, !cs->waiting);
85
86 if (cs->cmd_result >= 0)
87 return 0;
88
89 return cs->cmd_result;
90 default:
91 return -EINVAL;
92 }
93 }
94
95 static int if_config(struct cardstate *cs, int *arg)
96 {
97 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
98
99 if (*arg != 1)
100 return -EINVAL;
101
102 if (cs->mstate != MS_LOCKED)
103 return -EBUSY;
104
105 if (!cs->connected) {
106 pr_err("%s: not connected\n", __func__);
107 return -ENODEV;
108 }
109
110 *arg = 0;
111 return gigaset_enterconfigmode(cs);
112 }
113
114 /*** the terminal driver ***/
115
116 static int if_open(struct tty_struct *tty, struct file *filp)
117 {
118 struct cardstate *cs;
119
120 gig_dbg(DEBUG_IF, "%d+%d: %s()",
121 tty->driver->minor_start, tty->index, __func__);
122
123 cs = gigaset_get_cs_by_tty(tty);
124 if (!cs || !try_module_get(cs->driver->owner))
125 return -ENODEV;
126
127 if (mutex_lock_interruptible(&cs->mutex)) {
128 module_put(cs->driver->owner);
129 return -ERESTARTSYS;
130 }
131 tty->driver_data = cs;
132
133 ++cs->port.count;
134
135 if (cs->port.count == 1) {
136 tty_port_tty_set(&cs->port, tty);
137 cs->port.low_latency = 1;
138 }
139
140 mutex_unlock(&cs->mutex);
141 return 0;
142 }
143
144 static void if_close(struct tty_struct *tty, struct file *filp)
145 {
146 struct cardstate *cs = tty->driver_data;
147
148 if (!cs) { /* happens if we didn't find cs in open */
149 gig_dbg(DEBUG_IF, "%s: no cardstate", __func__);
150 return;
151 }
152
153 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
154
155 mutex_lock(&cs->mutex);
156
157 if (!cs->connected)
158 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
159 else if (!cs->port.count)
160 dev_warn(cs->dev, "%s: device not opened\n", __func__);
161 else if (!--cs->port.count)
162 tty_port_tty_set(&cs->port, NULL);
163
164 mutex_unlock(&cs->mutex);
165
166 module_put(cs->driver->owner);
167 }
168
169 static int if_ioctl(struct tty_struct *tty,
170 unsigned int cmd, unsigned long arg)
171 {
172 struct cardstate *cs = tty->driver_data;
173 int retval = -ENODEV;
174 int int_arg;
175 unsigned char buf[6];
176 unsigned version[4];
177
178 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
179
180 if (mutex_lock_interruptible(&cs->mutex))
181 return -ERESTARTSYS;
182
183 if (!cs->connected) {
184 gig_dbg(DEBUG_IF, "not connected");
185 retval = -ENODEV;
186 } else {
187 retval = 0;
188 switch (cmd) {
189 case GIGASET_REDIR:
190 retval = get_user(int_arg, (int __user *) arg);
191 if (retval >= 0)
192 retval = if_lock(cs, &int_arg);
193 if (retval >= 0)
194 retval = put_user(int_arg, (int __user *) arg);
195 break;
196 case GIGASET_CONFIG:
197 retval = get_user(int_arg, (int __user *) arg);
198 if (retval >= 0)
199 retval = if_config(cs, &int_arg);
200 if (retval >= 0)
201 retval = put_user(int_arg, (int __user *) arg);
202 break;
203 case GIGASET_BRKCHARS:
204 retval = copy_from_user(&buf,
205 (const unsigned char __user *) arg, 6)
206 ? -EFAULT : 0;
207 if (retval >= 0) {
208 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
209 6, buf);
210 retval = cs->ops->brkchars(cs, buf);
211 }
212 break;
213 case GIGASET_VERSION:
214 retval = copy_from_user(version,
215 (unsigned __user *) arg, sizeof version)
216 ? -EFAULT : 0;
217 if (retval >= 0)
218 retval = if_version(cs, version);
219 if (retval >= 0)
220 retval = copy_to_user((unsigned __user *) arg,
221 version, sizeof version)
222 ? -EFAULT : 0;
223 break;
224 default:
225 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
226 __func__, cmd);
227 retval = -ENOIOCTLCMD;
228 }
229 }
230
231 mutex_unlock(&cs->mutex);
232
233 return retval;
234 }
235
236 #ifdef CONFIG_COMPAT
237 static long if_compat_ioctl(struct tty_struct *tty,
238 unsigned int cmd, unsigned long arg)
239 {
240 return if_ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
241 }
242 #endif
243
244 static int if_tiocmget(struct tty_struct *tty)
245 {
246 struct cardstate *cs = tty->driver_data;
247 int retval;
248
249 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
250
251 if (mutex_lock_interruptible(&cs->mutex))
252 return -ERESTARTSYS;
253
254 retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
255
256 mutex_unlock(&cs->mutex);
257
258 return retval;
259 }
260
261 static int if_tiocmset(struct tty_struct *tty,
262 unsigned int set, unsigned int clear)
263 {
264 struct cardstate *cs = tty->driver_data;
265 int retval;
266 unsigned mc;
267
268 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
269 cs->minor_index, __func__, set, clear);
270
271 if (mutex_lock_interruptible(&cs->mutex))
272 return -ERESTARTSYS;
273
274 if (!cs->connected) {
275 gig_dbg(DEBUG_IF, "not connected");
276 retval = -ENODEV;
277 } else {
278 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
279 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
280 cs->control_state = mc;
281 }
282
283 mutex_unlock(&cs->mutex);
284
285 return retval;
286 }
287
288 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
289 {
290 struct cardstate *cs = tty->driver_data;
291 struct cmdbuf_t *cb;
292 int retval;
293
294 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
295
296 if (mutex_lock_interruptible(&cs->mutex))
297 return -ERESTARTSYS;
298
299 if (!cs->connected) {
300 gig_dbg(DEBUG_IF, "not connected");
301 retval = -ENODEV;
302 goto done;
303 }
304 if (cs->mstate != MS_LOCKED) {
305 dev_warn(cs->dev, "can't write to unlocked device\n");
306 retval = -EBUSY;
307 goto done;
308 }
309 if (count <= 0) {
310 /* nothing to do */
311 retval = 0;
312 goto done;
313 }
314
315 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
316 if (!cb) {
317 dev_err(cs->dev, "%s: out of memory\n", __func__);
318 retval = -ENOMEM;
319 goto done;
320 }
321
322 memcpy(cb->buf, buf, count);
323 cb->len = count;
324 cb->offset = 0;
325 cb->next = NULL;
326 cb->wake_tasklet = &cs->if_wake_tasklet;
327 retval = cs->ops->write_cmd(cs, cb);
328 done:
329 mutex_unlock(&cs->mutex);
330 return retval;
331 }
332
333 static int if_write_room(struct tty_struct *tty)
334 {
335 struct cardstate *cs = tty->driver_data;
336 int retval;
337
338 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
339
340 if (mutex_lock_interruptible(&cs->mutex))
341 return -ERESTARTSYS;
342
343 if (!cs->connected) {
344 gig_dbg(DEBUG_IF, "not connected");
345 retval = -ENODEV;
346 } else if (cs->mstate != MS_LOCKED) {
347 dev_warn(cs->dev, "can't write to unlocked device\n");
348 retval = -EBUSY;
349 } else
350 retval = cs->ops->write_room(cs);
351
352 mutex_unlock(&cs->mutex);
353
354 return retval;
355 }
356
357 static int if_chars_in_buffer(struct tty_struct *tty)
358 {
359 struct cardstate *cs = tty->driver_data;
360 int retval = 0;
361
362 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
363
364 mutex_lock(&cs->mutex);
365
366 if (!cs->connected)
367 gig_dbg(DEBUG_IF, "not connected");
368 else if (cs->mstate != MS_LOCKED)
369 dev_warn(cs->dev, "can't write to unlocked device\n");
370 else
371 retval = cs->ops->chars_in_buffer(cs);
372
373 mutex_unlock(&cs->mutex);
374
375 return retval;
376 }
377
378 static void if_throttle(struct tty_struct *tty)
379 {
380 struct cardstate *cs = tty->driver_data;
381
382 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
383
384 mutex_lock(&cs->mutex);
385
386 if (!cs->connected)
387 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
388 else
389 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
390
391 mutex_unlock(&cs->mutex);
392 }
393
394 static void if_unthrottle(struct tty_struct *tty)
395 {
396 struct cardstate *cs = tty->driver_data;
397
398 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
399
400 mutex_lock(&cs->mutex);
401
402 if (!cs->connected)
403 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
404 else
405 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
406
407 mutex_unlock(&cs->mutex);
408 }
409
410 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
411 {
412 struct cardstate *cs = tty->driver_data;
413 unsigned int iflag;
414 unsigned int cflag;
415 unsigned int old_cflag;
416 unsigned int control_state, new_state;
417
418 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
419
420 mutex_lock(&cs->mutex);
421
422 if (!cs->connected) {
423 gig_dbg(DEBUG_IF, "not connected");
424 goto out;
425 }
426
427 iflag = tty->termios.c_iflag;
428 cflag = tty->termios.c_cflag;
429 old_cflag = old ? old->c_cflag : cflag;
430 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
431 cs->minor_index, iflag, cflag, old_cflag);
432
433 /* get a local copy of the current port settings */
434 control_state = cs->control_state;
435
436 /*
437 * Update baud rate.
438 * Do not attempt to cache old rates and skip settings,
439 * disconnects screw such tricks up completely.
440 * Premature optimization is the root of all evil.
441 */
442
443 /* reassert DTR and (maybe) RTS on transition from B0 */
444 if ((old_cflag & CBAUD) == B0) {
445 new_state = control_state | TIOCM_DTR;
446 /* don't set RTS if using hardware flow control */
447 if (!(old_cflag & CRTSCTS))
448 new_state |= TIOCM_RTS;
449 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
450 cs->minor_index,
451 (new_state & TIOCM_RTS) ? " only" : "/RTS");
452 cs->ops->set_modem_ctrl(cs, control_state, new_state);
453 control_state = new_state;
454 }
455
456 cs->ops->baud_rate(cs, cflag & CBAUD);
457
458 if ((cflag & CBAUD) == B0) {
459 /* Drop RTS and DTR */
460 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
461 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
462 cs->ops->set_modem_ctrl(cs, control_state, new_state);
463 control_state = new_state;
464 }
465
466 /*
467 * Update line control register (LCR)
468 */
469
470 cs->ops->set_line_ctrl(cs, cflag);
471
472 /* save off the modified port settings */
473 cs->control_state = control_state;
474
475 out:
476 mutex_unlock(&cs->mutex);
477 }
478
479 static const struct tty_operations if_ops = {
480 .open = if_open,
481 .close = if_close,
482 .ioctl = if_ioctl,
483 #ifdef CONFIG_COMPAT
484 .compat_ioctl = if_compat_ioctl,
485 #endif
486 .write = if_write,
487 .write_room = if_write_room,
488 .chars_in_buffer = if_chars_in_buffer,
489 .set_termios = if_set_termios,
490 .throttle = if_throttle,
491 .unthrottle = if_unthrottle,
492 .tiocmget = if_tiocmget,
493 .tiocmset = if_tiocmset,
494 };
495
496
497 /* wakeup tasklet for the write operation */
498 static void if_wake(unsigned long data)
499 {
500 struct cardstate *cs = (struct cardstate *)data;
501
502 tty_port_tty_wakeup(&cs->port);
503 }
504
505 /*** interface to common ***/
506
507 void gigaset_if_init(struct cardstate *cs)
508 {
509 struct gigaset_driver *drv;
510
511 drv = cs->driver;
512 if (!drv->have_tty)
513 return;
514
515 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
516
517 mutex_lock(&cs->mutex);
518 cs->tty_dev = tty_port_register_device(&cs->port, drv->tty,
519 cs->minor_index, NULL);
520
521 if (!IS_ERR(cs->tty_dev))
522 dev_set_drvdata(cs->tty_dev, cs);
523 else {
524 pr_warning("could not register device to the tty subsystem\n");
525 cs->tty_dev = NULL;
526 }
527 mutex_unlock(&cs->mutex);
528 }
529
530 void gigaset_if_free(struct cardstate *cs)
531 {
532 struct gigaset_driver *drv;
533
534 drv = cs->driver;
535 if (!drv->have_tty)
536 return;
537
538 tasklet_disable(&cs->if_wake_tasklet);
539 tasklet_kill(&cs->if_wake_tasklet);
540 cs->tty_dev = NULL;
541 tty_unregister_device(drv->tty, cs->minor_index);
542 }
543
544 /**
545 * gigaset_if_receive() - pass a received block of data to the tty device
546 * @cs: device descriptor structure.
547 * @buffer: received data.
548 * @len: number of bytes received.
549 *
550 * Called by asyncdata/isocdata if a block of data received from the
551 * device must be sent to userspace through the ttyG* device.
552 */
553 void gigaset_if_receive(struct cardstate *cs,
554 unsigned char *buffer, size_t len)
555 {
556 tty_insert_flip_string(&cs->port, buffer, len);
557 tty_flip_buffer_push(&cs->port);
558 }
559 EXPORT_SYMBOL_GPL(gigaset_if_receive);
560
561 /* gigaset_if_initdriver
562 * Initialize tty interface.
563 * parameters:
564 * drv Driver
565 * procname Name of the driver (e.g. for /proc/tty/drivers)
566 * devname Name of the device files (prefix without minor number)
567 */
568 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
569 const char *devname)
570 {
571 int ret;
572 struct tty_driver *tty;
573
574 drv->have_tty = 0;
575
576 drv->tty = tty = alloc_tty_driver(drv->minors);
577 if (tty == NULL)
578 goto enomem;
579
580 tty->type = TTY_DRIVER_TYPE_SERIAL;
581 tty->subtype = SERIAL_TYPE_NORMAL;
582 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
583
584 tty->driver_name = procname;
585 tty->name = devname;
586 tty->minor_start = drv->minor;
587
588 tty->init_termios = tty_std_termios;
589 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
590 tty_set_operations(tty, &if_ops);
591
592 ret = tty_register_driver(tty);
593 if (ret < 0) {
594 pr_err("error %d registering tty driver\n", ret);
595 goto error;
596 }
597 gig_dbg(DEBUG_IF, "tty driver initialized");
598 drv->have_tty = 1;
599 return;
600
601 enomem:
602 pr_err("out of memory\n");
603 error:
604 if (drv->tty)
605 put_tty_driver(drv->tty);
606 }
607
608 void gigaset_if_freedriver(struct gigaset_driver *drv)
609 {
610 if (!drv->have_tty)
611 return;
612
613 drv->have_tty = 0;
614 tty_unregister_driver(drv->tty);
615 put_tty_driver(drv->tty);
616 }