interface.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * interface to user space for the gigaset driver
 *
 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
 *
 * =====================================================================
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as
 *    published by the Free Software Foundation; either version 2 of
 *    the License, or (at your option) any later version.
 * =====================================================================
 */

#include "gigaset.h"
#include <linux/gigaset_dev.h>
#include <linux/tty_flip.h>

/*** our ioctls ***/

static int if_lock(struct cardstate *cs, int *arg)
{
	int cmd = *arg;

24
	gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
25 26 27 28 29

	if (cmd > 1)
		return -EINVAL;

	if (cmd < 0) {
T
Tilman Schmidt 已提交
30
		*arg = cs->mstate == MS_LOCKED;
31 32 33
		return 0;
	}

T
Tilman Schmidt 已提交
34
	if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
35 36 37 38 39 40 41 42
		cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
		cs->ops->baud_rate(cs, B115200);
		cs->ops->set_line_ctrl(cs, CS8);
		cs->control_state = TIOCM_DTR|TIOCM_RTS;
	}

	cs->waiting = 1;
	if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
43
			       NULL, cmd, NULL)) {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
		cs->waiting = 0;
		return -ENOMEM;
	}
	gigaset_schedule_event(cs);

	wait_event(cs->waitqueue, !cs->waiting);

	if (cs->cmd_result >= 0) {
		*arg = cs->cmd_result;
		return 0;
	}

	return cs->cmd_result;
}

static int if_version(struct cardstate *cs, unsigned arg[4])
{
	static const unsigned version[4] = GIG_VERSION;
	static const unsigned compat[4] = GIG_COMPAT;
	unsigned cmd = arg[0];

65
	gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
66 67 68 69 70 71 72 73 74 75 76

	switch (cmd) {
	case GIGVER_DRIVER:
		memcpy(arg, version, sizeof version);
		return 0;
	case GIGVER_COMPAT:
		memcpy(arg, compat, sizeof compat);
		return 0;
	case GIGVER_FWBASE:
		cs->waiting = 1;
		if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
77
				       NULL, 0, arg)) {
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			cs->waiting = 0;
			return -ENOMEM;
		}
		gigaset_schedule_event(cs);

		wait_event(cs->waitqueue, !cs->waiting);

		if (cs->cmd_result >= 0)
			return 0;

		return cs->cmd_result;
	default:
		return -EINVAL;
	}
}

static int if_config(struct cardstate *cs, int *arg)
{
96
	gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
97 98 99 100

	if (*arg != 1)
		return -EINVAL;

T
Tilman Schmidt 已提交
101
	if (cs->mstate != MS_LOCKED)
102 103
		return -EBUSY;

104
	if (!cs->connected) {
105
		pr_err("%s: not connected\n", __func__);
106 107 108
		return -ENODEV;
	}

109 110 111 112 113 114 115 116 117 118
	*arg = 0;
	return gigaset_enterconfigmode(cs);
}

/*** the terminal driver ***/
/* stolen from usbserial and some other tty drivers */

static int  if_open(struct tty_struct *tty, struct file *filp);
static void if_close(struct tty_struct *tty, struct file *filp);
static int  if_ioctl(struct tty_struct *tty, struct file *file,
119
		     unsigned int cmd, unsigned long arg);
120 121 122 123
static int  if_write_room(struct tty_struct *tty);
static int  if_chars_in_buffer(struct tty_struct *tty);
static void if_throttle(struct tty_struct *tty);
static void if_unthrottle(struct tty_struct *tty);
A
Alan Cox 已提交
124
static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
125
static int  if_tiocmget(struct tty_struct *tty);
126
static int  if_tiocmset(struct tty_struct *tty, struct file *file,
127
			unsigned int set, unsigned int clear);
128
static int  if_write(struct tty_struct *tty,
129
		     const unsigned char *buf, int count);
130

J
Jeff Dike 已提交
131
static const struct tty_operations if_ops = {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	.open =			if_open,
	.close =		if_close,
	.ioctl =		if_ioctl,
	.write =		if_write,
	.write_room =		if_write_room,
	.chars_in_buffer =	if_chars_in_buffer,
	.set_termios =		if_set_termios,
	.throttle =		if_throttle,
	.unthrottle =		if_unthrottle,
	.tiocmget =		if_tiocmget,
	.tiocmset =		if_tiocmset,
};

static int if_open(struct tty_struct *tty, struct file *filp)
{
	struct cardstate *cs;
	unsigned long flags;

150 151
	gig_dbg(DEBUG_IF, "%d+%d: %s()",
		tty->driver->minor_start, tty->index, __func__);
152 153 154 155

	tty->driver_data = NULL;

	cs = gigaset_get_cs_by_tty(tty);
T
Tilman Schmidt 已提交
156
	if (!cs || !try_module_get(cs->driver->owner))
157 158
		return -ENODEV;

159
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
160
		return -ERESTARTSYS;
161 162 163 164 165 166 167 168
	tty->driver_data = cs;

	++cs->open_count;

	if (cs->open_count == 1) {
		spin_lock_irqsave(&cs->lock, flags);
		cs->tty = tty;
		spin_unlock_irqrestore(&cs->lock, flags);
T
Tilman Schmidt 已提交
169
		tty->low_latency = 1;
170 171
	}

172
	mutex_unlock(&cs->mutex);
173 174 175 176 177 178 179 180 181 182
	return 0;
}

static void if_close(struct tty_struct *tty, struct file *filp)
{
	struct cardstate *cs;
	unsigned long flags;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
183
		pr_err("%s: no cardstate\n", __func__);
184 185 186
		return;
	}

187
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
188

189
	mutex_lock(&cs->mutex);
190

191 192 193
	if (!cs->connected)
		gig_dbg(DEBUG_IF, "not connected");	/* nothing to do */
	else if (!cs->open_count)
194
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
195 196 197 198 199 200 201 202
	else {
		if (!--cs->open_count) {
			spin_lock_irqsave(&cs->lock, flags);
			cs->tty = NULL;
			spin_unlock_irqrestore(&cs->lock, flags);
		}
	}

203
	mutex_unlock(&cs->mutex);
T
Tilman Schmidt 已提交
204 205

	module_put(cs->driver->owner);
206 207 208
}

static int if_ioctl(struct tty_struct *tty, struct file *file,
209
		    unsigned int cmd, unsigned long arg)
210 211 212 213 214 215 216 217 218
{
	struct cardstate *cs;
	int retval = -ENODEV;
	int int_arg;
	unsigned char buf[6];
	unsigned version[4];

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
219
		pr_err("%s: no cardstate\n", __func__);
220 221 222
		return -ENODEV;
	}

223
	gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
224

225
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
226
		return -ERESTARTSYS;
227

228 229 230 231
	if (!cs->connected) {
		gig_dbg(DEBUG_IF, "not connected");
		retval = -ENODEV;
	} else if (!cs->open_count)
232
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	else {
		retval = 0;
		switch (cmd) {
		case GIGASET_REDIR:
			retval = get_user(int_arg, (int __user *) arg);
			if (retval >= 0)
				retval = if_lock(cs, &int_arg);
			if (retval >= 0)
				retval = put_user(int_arg, (int __user *) arg);
			break;
		case GIGASET_CONFIG:
			retval = get_user(int_arg, (int __user *) arg);
			if (retval >= 0)
				retval = if_config(cs, &int_arg);
			if (retval >= 0)
				retval = put_user(int_arg, (int __user *) arg);
			break;
		case GIGASET_BRKCHARS:
			retval = copy_from_user(&buf,
252 253
					(const unsigned char __user *) arg, 6)
				? -EFAULT : 0;
254 255 256
			if (retval >= 0) {
				gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
						6, (const unsigned char *) arg);
257
				retval = cs->ops->brkchars(cs, buf);
258
			}
259 260
			break;
		case GIGASET_VERSION:
261
			retval = copy_from_user(version,
262 263
					(unsigned __user *) arg, sizeof version)
				? -EFAULT : 0;
264 265 266
			if (retval >= 0)
				retval = if_version(cs, version);
			if (retval >= 0)
267
				retval = copy_to_user((unsigned __user *) arg,
268 269
						      version, sizeof version)
					? -EFAULT : 0;
270
			break;
271
		default:
T
Tilman Schmidt 已提交
272
			gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
273
				__func__, cmd);
274 275 276 277
			retval = -ENOIOCTLCMD;
		}
	}

278
	mutex_unlock(&cs->mutex);
279 280 281 282

	return retval;
}

283
static int if_tiocmget(struct tty_struct *tty)
284 285 286 287 288 289
{
	struct cardstate *cs;
	int retval;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
290
		pr_err("%s: no cardstate\n", __func__);
291 292 293
		return -ENODEV;
	}

294
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
295

296
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
297
		return -ERESTARTSYS;
298 299 300

	retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);

301
	mutex_unlock(&cs->mutex);
302 303 304 305 306

	return retval;
}

static int if_tiocmset(struct tty_struct *tty, struct file *file,
307
		       unsigned int set, unsigned int clear)
308 309 310 311 312 313 314
{
	struct cardstate *cs;
	int retval;
	unsigned mc;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
315
		pr_err("%s: no cardstate\n", __func__);
316 317 318
		return -ENODEV;
	}

319 320
	gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
		cs->minor_index, __func__, set, clear);
321

322
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
323
		return -ERESTARTSYS;
324

325
	if (!cs->connected) {
326
		gig_dbg(DEBUG_IF, "not connected");
327 328 329 330 331 332 333
		retval = -ENODEV;
	} else {
		mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
		retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
		cs->control_state = mc;
	}

334
	mutex_unlock(&cs->mutex);
335 336 337 338 339 340 341

	return retval;
}

static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
	struct cardstate *cs;
342 343
	struct cmdbuf_t *cb;
	int retval;
344 345 346

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
347
		pr_err("%s: no cardstate\n", __func__);
348 349 350
		return -ENODEV;
	}

351
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
352

353
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
354
		return -ERESTARTSYS;
355

356 357 358
	if (!cs->connected) {
		gig_dbg(DEBUG_IF, "not connected");
		retval = -ENODEV;
359 360 361
		goto done;
	}
	if (!cs->open_count) {
362
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
363 364 365 366
		retval = -ENODEV;
		goto done;
	}
	if (cs->mstate != MS_LOCKED) {
367
		dev_warn(cs->dev, "can't write to unlocked device\n");
368
		retval = -EBUSY;
369 370 371 372 373 374
		goto done;
	}
	if (count <= 0) {
		/* nothing to do */
		retval = 0;
		goto done;
375 376
	}

377 378 379 380 381 382
	cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
	if (!cb) {
		dev_err(cs->dev, "%s: out of memory\n", __func__);
		retval = -ENOMEM;
		goto done;
	}
383

384 385 386 387 388 389 390 391
	memcpy(cb->buf, buf, count);
	cb->len = count;
	cb->offset = 0;
	cb->next = NULL;
	cb->wake_tasklet = &cs->if_wake_tasklet;
	retval = cs->ops->write_cmd(cs, cb);
done:
	mutex_unlock(&cs->mutex);
392 393 394 395 396 397 398 399 400 401
	return retval;
}

static int if_write_room(struct tty_struct *tty)
{
	struct cardstate *cs;
	int retval = -ENODEV;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
402
		pr_err("%s: no cardstate\n", __func__);
403 404 405
		return -ENODEV;
	}

406
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
407

408
	if (mutex_lock_interruptible(&cs->mutex))
T
Tilman Schmidt 已提交
409
		return -ERESTARTSYS;
410

411 412 413 414
	if (!cs->connected) {
		gig_dbg(DEBUG_IF, "not connected");
		retval = -ENODEV;
	} else if (!cs->open_count)
415
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
T
Tilman Schmidt 已提交
416
	else if (cs->mstate != MS_LOCKED) {
417
		dev_warn(cs->dev, "can't write to unlocked device\n");
T
Tilman Schmidt 已提交
418
		retval = -EBUSY;
419 420 421
	} else
		retval = cs->ops->write_room(cs);

422
	mutex_unlock(&cs->mutex);
423 424 425 426 427 428 429

	return retval;
}

static int if_chars_in_buffer(struct tty_struct *tty)
{
	struct cardstate *cs;
430
	int retval = 0;
431 432 433

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
434
		pr_err("%s: no cardstate\n", __func__);
435
		return 0;
436 437
	}

438
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
439

440
	mutex_lock(&cs->mutex);
441

442
	if (!cs->connected)
443
		gig_dbg(DEBUG_IF, "not connected");
444
	else if (!cs->open_count)
445
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
446
	else if (cs->mstate != MS_LOCKED)
447
		dev_warn(cs->dev, "can't write to unlocked device\n");
448
	else
449 450
		retval = cs->ops->chars_in_buffer(cs);

451
	mutex_unlock(&cs->mutex);
452 453 454 455 456 457 458 459 460 461

	return retval;
}

static void if_throttle(struct tty_struct *tty)
{
	struct cardstate *cs;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
462
		pr_err("%s: no cardstate\n", __func__);
463 464 465
		return;
	}

466
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
467

468
	mutex_lock(&cs->mutex);
469

470 471 472
	if (!cs->connected)
		gig_dbg(DEBUG_IF, "not connected");	/* nothing to do */
	else if (!cs->open_count)
473
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
T
Tilman Schmidt 已提交
474
	else
T
Tilman Schmidt 已提交
475
		gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
476

477
	mutex_unlock(&cs->mutex);
478 479 480 481 482 483 484 485
}

static void if_unthrottle(struct tty_struct *tty)
{
	struct cardstate *cs;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
486
		pr_err("%s: no cardstate\n", __func__);
487 488 489
		return;
	}

490
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
491

492
	mutex_lock(&cs->mutex);
493

494 495 496
	if (!cs->connected)
		gig_dbg(DEBUG_IF, "not connected");	/* nothing to do */
	else if (!cs->open_count)
497
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
T
Tilman Schmidt 已提交
498
	else
T
Tilman Schmidt 已提交
499
		gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
500

501
	mutex_unlock(&cs->mutex);
502 503
}

A
Alan Cox 已提交
504
static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
505 506 507 508 509 510 511 512 513
{
	struct cardstate *cs;
	unsigned int iflag;
	unsigned int cflag;
	unsigned int old_cflag;
	unsigned int control_state, new_state;

	cs = (struct cardstate *) tty->driver_data;
	if (!cs) {
514
		pr_err("%s: no cardstate\n", __func__);
515 516 517
		return;
	}

518
	gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
519

520
	mutex_lock(&cs->mutex);
521

522 523
	if (!cs->connected) {
		gig_dbg(DEBUG_IF, "not connected");
524 525 526
		goto out;
	}

527 528
	if (!cs->open_count) {
		dev_warn(cs->dev, "%s: device not opened\n", __func__);
529 530 531 532 533
		goto out;
	}

	iflag = tty->termios->c_iflag;
	cflag = tty->termios->c_cflag;
T
Tilman Schmidt 已提交
534
	old_cflag = old ? old->c_cflag : cflag;
535 536
	gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
		cs->minor_index, iflag, cflag, old_cflag);
537 538 539 540 541 542 543 544 545 546 547

	/* get a local copy of the current port settings */
	control_state = cs->control_state;

	/*
	 * Update baud rate.
	 * Do not attempt to cache old rates and skip settings,
	 * disconnects screw such tricks up completely.
	 * Premature optimization is the root of all evil.
	 */

548
	/* reassert DTR and (maybe) RTS on transition from B0 */
549 550 551 552 553
	if ((old_cflag & CBAUD) == B0) {
		new_state = control_state | TIOCM_DTR;
		/* don't set RTS if using hardware flow control */
		if (!(old_cflag & CRTSCTS))
			new_state |= TIOCM_RTS;
554 555 556
		gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
			cs->minor_index,
			(new_state & TIOCM_RTS) ? " only" : "/RTS");
557 558 559 560 561 562 563 564
		cs->ops->set_modem_ctrl(cs, control_state, new_state);
		control_state = new_state;
	}

	cs->ops->baud_rate(cs, cflag & CBAUD);

	if ((cflag & CBAUD) == B0) {
		/* Drop RTS and DTR */
565
		gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
		new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
		cs->ops->set_modem_ctrl(cs, control_state, new_state);
		control_state = new_state;
	}

	/*
	 * Update line control register (LCR)
	 */

	cs->ops->set_line_ctrl(cs, cflag);

	/* save off the modified port settings */
	cs->control_state = control_state;

out:
581
	mutex_unlock(&cs->mutex);
582 583 584 585 586 587 588 589
}


/* wakeup tasklet for the write operation */
static void if_wake(unsigned long data)
{
	struct cardstate *cs = (struct cardstate *) data;

J
Jiri Slaby 已提交
590 591
	if (cs->tty)
		tty_wakeup(cs->tty);
592 593 594 595 596 597 598 599 600 601 602 603
}

/*** interface to common ***/

void gigaset_if_init(struct cardstate *cs)
{
	struct gigaset_driver *drv;

	drv = cs->driver;
	if (!drv->have_tty)
		return;

604
	tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
605 606

	mutex_lock(&cs->mutex);
607
	cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
608

609 610
	if (!IS_ERR(cs->tty_dev))
		dev_set_drvdata(cs->tty_dev, cs);
611
	else {
612
		pr_warning("could not register device to the tty subsystem\n");
613
		cs->tty_dev = NULL;
614
	}
615
	mutex_unlock(&cs->mutex);
616 617 618 619 620 621 622 623 624 625 626 627
}

void gigaset_if_free(struct cardstate *cs)
{
	struct gigaset_driver *drv;

	drv = cs->driver;
	if (!drv->have_tty)
		return;

	tasklet_disable(&cs->if_wake_tasklet);
	tasklet_kill(&cs->if_wake_tasklet);
628
	cs->tty_dev = NULL;
629 630 631
	tty_unregister_device(drv->tty, cs->minor_index);
}

T
Tilman Schmidt 已提交
632 633 634 635 636 637 638 639 640
/**
 * gigaset_if_receive() - pass a received block of data to the tty device
 * @cs:		device descriptor structure.
 * @buffer:	received data.
 * @len:	number of bytes received.
 *
 * Called by asyncdata/isocdata if a block of data received from the
 * device must be sent to userspace through the ttyG* device.
 */
641
void gigaset_if_receive(struct cardstate *cs,
642
			unsigned char *buffer, size_t len)
643 644 645 646 647
{
	unsigned long flags;
	struct tty_struct *tty;

	spin_lock_irqsave(&cs->lock, flags);
T
Tilman Schmidt 已提交
648 649
	tty = cs->tty;
	if (tty == NULL)
T
Tilman Schmidt 已提交
650
		gig_dbg(DEBUG_IF, "receive on closed device");
651 652 653 654 655 656 657 658 659 660 661
	else {
		tty_insert_flip_string(tty, buffer, len);
		tty_flip_buffer_push(tty);
	}
	spin_unlock_irqrestore(&cs->lock, flags);
}
EXPORT_SYMBOL_GPL(gigaset_if_receive);

/* gigaset_if_initdriver
 * Initialize tty interface.
 * parameters:
662 663 664
 *	drv		Driver
 *	procname	Name of the driver (e.g. for /proc/tty/drivers)
 *	devname		Name of the device files (prefix without minor number)
665 666
 */
void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
667
			   const char *devname)
668 669 670 671 672 673 674
{
	unsigned minors = drv->minors;
	int ret;
	struct tty_driver *tty;

	drv->have_tty = 0;

T
Tilman Schmidt 已提交
675 676
	drv->tty = tty = alloc_tty_driver(minors);
	if (tty == NULL)
677 678 679 680 681
		goto enomem;

	tty->magic =		TTY_DRIVER_MAGIC,
	tty->type =		TTY_DRIVER_TYPE_SERIAL,
	tty->subtype =		SERIAL_TYPE_NORMAL,
682
	tty->flags =		TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
683 684 685 686 687 688 689 690

	tty->driver_name =	procname;
	tty->name =		devname;
	tty->minor_start =	drv->minor;
	tty->num =		drv->minors;

	tty->owner =		THIS_MODULE;

T
Tilman Schmidt 已提交
691 692
	tty->init_termios          = tty_std_termios;
	tty->init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
693 694 695 696
	tty_set_operations(tty, &if_ops);

	ret = tty_register_driver(tty);
	if (ret < 0) {
697
		pr_err("error %d registering tty driver\n", ret);
698 699
		goto error;
	}
700
	gig_dbg(DEBUG_IF, "tty driver initialized");
701 702 703 704
	drv->have_tty = 1;
	return;

enomem:
705
	pr_err("out of memory\n");
706 707 708 709 710 711 712 713 714 715 716 717 718 719
error:
	if (drv->tty)
		put_tty_driver(drv->tty);
}

void gigaset_if_freedriver(struct gigaset_driver *drv)
{
	if (!drv->have_tty)
		return;

	drv->have_tty = 0;
	tty_unregister_driver(drv->tty);
	put_tty_driver(drv->tty);
}