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