oti6858.c 33.6 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*
 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
 *
 * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
 * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2003 IBM Corp.
 *
 * Many thanks to the authors of pl2303 driver: all functions in this file
 * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
 *
 * Warning! You use this driver on your own risk! The only official
 * description of this device I have is datasheet from manufacturer,
 * and it doesn't contain almost any information needed to write a driver.
 * Almost all knowlegde used while writing this driver was gathered by:
 *  - analyzing traffic between device and the M$ Windows 2000 driver,
 *  - trying different bit combinations and checking pin states
 *    with a voltmeter,
 *  - receiving malformed frames and producing buffer overflows
 *    to learn how errors are reported,
 * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
 * CONNECTED TO IT!
 *
 * 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.
 *
 * See Documentation/usb/usb-serial.txt for more information on using this driver
 *
 * TODO:
 *  - implement correct flushing for ioctls and oti6858_close()
 *  - check how errors (rx overflow, parity error, framing error) are reported
 *  - implement oti6858_break_ctl()
 *  - implement more ioctls
 *  - test/implement flow control
 *  - allow setting custom baud rates
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#include <asm/uaccess.h>
#include "oti6858.h"

#define OTI6858_DESCRIPTION \
	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
#define OTI6858_VERSION "0.1"

static struct usb_device_id id_table [] = {
	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
	{ }
};

MODULE_DEVICE_TABLE(usb, id_table);

static struct usb_driver oti6858_driver = {
	.name =		"oti6858",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table,
	.no_dynamic_id = 	1,
};

static int debug;


/* buffering code, copied from pl2303 driver */
#define PL2303_BUF_SIZE		1024
#define PL2303_TMP_BUF_SIZE	1024

A
Alan Cox 已提交
82
struct oti6858_buf {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	unsigned int	buf_size;
	char		*buf_buf;
	char		*buf_get;
	char		*buf_put;
};

/* requests */
#define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
#define	OTI6858_REQ_T_GET_STATUS	0x01

#define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
#define	OTI6858_REQ_T_SET_LINE		0x00

#define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
#define	OTI6858_REQ_T_CHECK_TXBUFF	0x00

/* format of the control packet */
struct oti6858_control_pkt {
A
Al Viro 已提交
101
	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#define OTI6858_MAX_BAUD_RATE	3000000
	u8	frame_fmt;
#define FMT_STOP_BITS_MASK	0xc0
#define FMT_STOP_BITS_1		0x00
#define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
#define FMT_PARITY_MASK		0x38
#define FMT_PARITY_NONE		0x00
#define FMT_PARITY_ODD		0x08
#define FMT_PARITY_EVEN		0x18
#define FMT_PARITY_MARK		0x28
#define FMT_PARITY_SPACE	0x38
#define FMT_DATA_BITS_MASK	0x03
#define FMT_DATA_BITS_5		0x00
#define FMT_DATA_BITS_6		0x01
#define FMT_DATA_BITS_7		0x02
#define FMT_DATA_BITS_8		0x03
	u8	something;	/* always equals 0x43 */
	u8	control;	/* settings of flow control lines */
#define CONTROL_MASK		0x0c
#define CONTROL_DTR_HIGH	0x08
#define CONTROL_RTS_HIGH	0x04
	u8	tx_status;
#define	TX_BUFFER_EMPTIED	0x09
	u8	pin_state;
#define PIN_MASK		0x3f
#define PIN_RTS			0x20	/* output pin */
#define PIN_CTS			0x10	/* input pin, active low */
#define PIN_DSR			0x08	/* input pin, active low */
#define PIN_DTR			0x04	/* output pin */
#define PIN_RI			0x02	/* input pin, active low */
#define PIN_DCD			0x01	/* input pin, active low */
	u8	rx_bytes_avail;		/* number of bytes in rx buffer */;
};

#define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
	(    ((a)->divisor == (priv)->pending_setup.divisor) \
	  && ((a)->control == (priv)->pending_setup.control) \
	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt) )

/* function prototypes */
A
Alan Cox 已提交
143 144 145 146 147 148 149
static int oti6858_open(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *filp);
static void oti6858_close(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *filp);
static void oti6858_set_termios(struct tty_struct *tty,
			struct usb_serial_port *port, struct ktermios *old);
static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
150 151 152 153
			unsigned int cmd, unsigned long arg);
static void oti6858_read_int_callback(struct urb *urb);
static void oti6858_read_bulk_callback(struct urb *urb);
static void oti6858_write_bulk_callback(struct urb *urb);
A
Alan Cox 已提交
154
static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
155
			const unsigned char *buf, int count);
A
Alan Cox 已提交
156 157 158 159
static int oti6858_write_room(struct tty_struct *tty);
static int oti6858_chars_in_buffer(struct tty_struct *tty);
static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
160 161 162 163 164
				unsigned int set, unsigned int clear);
static int oti6858_startup(struct usb_serial *serial);
static void oti6858_shutdown(struct usb_serial *serial);

/* functions operating on buffers */
A
Alan Cox 已提交
165 166 167 168 169 170
static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
static void oti6858_buf_free(struct oti6858_buf *pb);
static void oti6858_buf_clear(struct oti6858_buf *pb);
static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
171
					unsigned int count);
A
Alan Cox 已提交
172
static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
					unsigned int count);


/* device info */
static struct usb_serial_driver oti6858_device = {
	.driver = {
		.owner =	THIS_MODULE,
		.name =		"oti6858",
	},
	.id_table =		id_table,
	.num_ports =		1,
	.open =			oti6858_open,
	.close =		oti6858_close,
	.write =		oti6858_write,
	.ioctl =		oti6858_ioctl,
	.set_termios =		oti6858_set_termios,
	.tiocmget =		oti6858_tiocmget,
	.tiocmset =		oti6858_tiocmset,
	.read_bulk_callback =	oti6858_read_bulk_callback,
	.read_int_callback =	oti6858_read_int_callback,
	.write_bulk_callback =	oti6858_write_bulk_callback,
	.write_room =		oti6858_write_room,
	.chars_in_buffer =	oti6858_chars_in_buffer,
	.attach =		oti6858_startup,
	.shutdown =		oti6858_shutdown,
};

struct oti6858_private {
	spinlock_t lock;

A
Alan Cox 已提交
203
	struct oti6858_buf *buf;
204 205 206 207 208 209 210 211 212 213
	struct oti6858_control_pkt status;

	struct {
		u8 read_urb_in_use;
		u8 write_urb_in_use;
		u8 termios_initialized;
	} flags;
	struct delayed_work delayed_write_work;

	struct {
A
Al Viro 已提交
214
		__le16 divisor;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		u8 frame_fmt;
		u8 control;
	} pending_setup;
	u8 transient;
	u8 setup_done;
	struct delayed_work delayed_setup_work;

	wait_queue_head_t intr_wait;
        struct usb_serial_port *port;   /* USB port with which associated */
};

#undef dbg
/* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
#define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)

static void setup_line(struct work_struct *work)
{
	struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_setup_work.work);
	struct usb_serial_port *port = priv->port;
	struct oti6858_control_pkt *new_setup;
	unsigned long flags;
	int result;

238
	dbg("%s(port = %d)", __func__, port->number);
239 240

	if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
241
		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
242 243 244 245 246 247 248 249 250 251 252 253 254 255
		/* we will try again */
		schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
		return;
	}

	result = usb_control_msg(port->serial->dev,
				usb_rcvctrlpipe(port->serial->dev, 0),
				OTI6858_REQ_T_GET_STATUS,
				OTI6858_REQ_GET_STATUS,
				0, 0,
				new_setup, OTI6858_CTRL_PKT_SIZE,
				100);

	if (result != OTI6858_CTRL_PKT_SIZE) {
256
		dev_err(&port->dev, "%s(): error reading status\n", __func__);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		kfree(new_setup);
		/* we will try again */
		schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
		new_setup->divisor = priv->pending_setup.divisor;
		new_setup->control = priv->pending_setup.control;
		new_setup->frame_fmt = priv->pending_setup.frame_fmt;

		spin_unlock_irqrestore(&priv->lock, flags);
		result = usb_control_msg(port->serial->dev,
					usb_sndctrlpipe(port->serial->dev, 0),
					OTI6858_REQ_T_SET_LINE,
					OTI6858_REQ_SET_LINE,
					0, 0,
					new_setup, OTI6858_CTRL_PKT_SIZE,
					100);
	} else {
		spin_unlock_irqrestore(&priv->lock, flags);
		result = 0;
	}
	kfree(new_setup);

	spin_lock_irqsave(&priv->lock, flags);
	if (result != OTI6858_CTRL_PKT_SIZE)
		priv->transient = 0;
	priv->setup_done = 1;
	spin_unlock_irqrestore(&priv->lock, flags);

289
	dbg("%s(): submitting interrupt urb", __func__);
290 291 292 293
	port->interrupt_in_urb->dev = port->serial->dev;
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result != 0) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
294
				" with error %d\n", __func__, result);
295 296 297 298 299 300 301 302 303 304 305
	}
}

void send_data(struct work_struct *work)
{
	struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_write_work.work);
	struct usb_serial_port *port = priv->port;
	int count = 0, result;
	unsigned long flags;
	unsigned char allow;

306
	dbg("%s(port = %d)", __func__, port->number);
307 308 309 310 311 312 313 314 315

	spin_lock_irqsave(&priv->lock, flags);
	if (priv->flags.write_urb_in_use) {
		spin_unlock_irqrestore(&priv->lock, flags);
		schedule_delayed_work(&priv->delayed_write_work, msecs_to_jiffies(2));
		return;
	}
	priv->flags.write_urb_in_use = 1;

A
Alan Cox 已提交
316
	count = oti6858_buf_data_avail(priv->buf);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	spin_unlock_irqrestore(&priv->lock, flags);
	if (count > port->bulk_out_size)
		count = port->bulk_out_size;

	if (count != 0) {
		result = usb_control_msg(port->serial->dev,
				usb_rcvctrlpipe(port->serial->dev, 0),
				OTI6858_REQ_T_CHECK_TXBUFF,
				OTI6858_REQ_CHECK_TXBUFF,
				count, 0, &allow, 1, 100);
		if (result != 1 || allow != 0)
			count = 0;
	}

	if (count == 0) {
		priv->flags.write_urb_in_use = 0;

334
		dbg("%s(): submitting interrupt urb", __func__);
335 336 337 338
		port->interrupt_in_urb->dev = port->serial->dev;
		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
		if (result != 0) {
			dev_err(&port->dev, "%s(): usb_submit_urb() failed"
339
				" with error %d\n", __func__, result);
340 341 342 343 344
		}
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
345
	oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
346 347 348 349 350 351 352
	spin_unlock_irqrestore(&priv->lock, flags);

	port->write_urb->transfer_buffer_length = count;
	port->write_urb->dev = port->serial->dev;
	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result != 0) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
353
			       " with error %d\n", __func__, result);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		priv->flags.write_urb_in_use = 0;
	}

	usb_serial_port_softint(port);
}

static int oti6858_startup(struct usb_serial *serial)
{
        struct usb_serial_port *port = serial->port[0];
        struct oti6858_private *priv;
	int i;

	for (i = 0; i < serial->num_ports; ++i) {
		priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
		if (!priv)
			break;
A
Alan Cox 已提交
370
		priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		if (priv->buf == NULL) {
			kfree(priv);
			break;
		}

		spin_lock_init(&priv->lock);
		init_waitqueue_head(&priv->intr_wait);
//		INIT_WORK(&priv->setup_work, setup_line, serial->port[i]);
//		INIT_WORK(&priv->write_work, send_data, serial->port[i]);
		priv->port = port;
		INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
		INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);

		usb_set_serial_port_data(serial->port[i], priv);
	}
	if (i == serial->num_ports)
		return 0;

	for (--i; i >= 0; --i) {
		priv = usb_get_serial_port_data(serial->port[i]);
A
Alan Cox 已提交
391
		oti6858_buf_free(priv->buf);
392 393 394 395 396 397
		kfree(priv);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
	return -ENOMEM;
}

A
Alan Cox 已提交
398
static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
399 400 401 402 403
			const unsigned char *buf, int count)
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;

404
	dbg("%s(port = %d, count = %d)", __func__, port->number, count);
405 406 407 408 409

	if (!count)
		return count;

	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
410
	count = oti6858_buf_put(priv->buf, buf, count);
411 412 413 414 415
	spin_unlock_irqrestore(&priv->lock, flags);

	return count;
}

A
Alan Cox 已提交
416
static int oti6858_write_room(struct tty_struct *tty)
417
{
A
Alan Cox 已提交
418
	struct usb_serial_port *port = tty->driver_data;
419 420 421 422
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

423
	dbg("%s(port = %d)", __func__, port->number);
424 425

	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
426
	room = oti6858_buf_space_avail(priv->buf);
427 428 429 430 431
	spin_unlock_irqrestore(&priv->lock, flags);

	return room;
}

A
Alan Cox 已提交
432
static int oti6858_chars_in_buffer(struct tty_struct *tty)
433
{
A
Alan Cox 已提交
434
	struct usb_serial_port *port = tty->driver_data;
435 436 437 438
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

439
	dbg("%s(port = %d)", __func__, port->number);
440 441

	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
442
	chars = oti6858_buf_data_avail(priv->buf);
443 444 445 446 447
	spin_unlock_irqrestore(&priv->lock, flags);

	return chars;
}

A
Alan Cox 已提交
448 449
static void oti6858_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
450 451 452 453 454
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned int cflag;
	u8 frame_fmt, control;
A
Al Viro 已提交
455
	__le16 divisor;
456 457
	int br;

458
	dbg("%s(port = %d)", __func__, port->number);
459

A
Alan Cox 已提交
460
	if (!tty) {
461
		dbg("%s(): no tty structures", __func__);
462 463 464 465 466
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
	if (!priv->flags.termios_initialized) {
A
Alan Cox 已提交
467 468 469 470
		*(tty->termios) = tty_std_termios;
		tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
		tty->termios->c_ispeed = 38400;
		tty->termios->c_ospeed = 38400;
471 472 473 474
		priv->flags.termios_initialized = 1;
	}
	spin_unlock_irqrestore(&priv->lock, flags);

A
Alan Cox 已提交
475
	cflag = tty->termios->c_cflag;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

	spin_lock_irqsave(&priv->lock, flags);
	divisor = priv->pending_setup.divisor;
	frame_fmt = priv->pending_setup.frame_fmt;
	control = priv->pending_setup.control;
	spin_unlock_irqrestore(&priv->lock, flags);

	frame_fmt &= ~FMT_DATA_BITS_MASK;
	switch (cflag & CSIZE) {
		case CS5:
			frame_fmt |= FMT_DATA_BITS_5;
			break;
		case CS6:
			frame_fmt |= FMT_DATA_BITS_6;
			break;
		case CS7:
			frame_fmt |= FMT_DATA_BITS_7;
			break;
		default:
		case CS8:
			frame_fmt |= FMT_DATA_BITS_8;
			break;
	}

	/* manufacturer claims that this device can work with baud rates
	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
	 * guarantee that any other baud rate will work (especially
	 * the higher ones)
	 */
A
Alan Cox 已提交
505
	br = tty_get_baud_rate(tty);
506 507
	if (br == 0) {
		divisor = 0;
A
Alan Cox 已提交
508
	} else {
509
		int real_br;
A
Al Viro 已提交
510
		int new_divisor;
A
Alan Cox 已提交
511
		br = min(br, OTI6858_MAX_BAUD_RATE);
512

A
Al Viro 已提交
513 514 515
		new_divisor = (96000000 + 8 * br) / (16 * br);
		real_br = 96000000 / (16 * new_divisor);
		divisor = cpu_to_le16(new_divisor);
A
Alan Cox 已提交
516
		tty_encode_baud_rate(tty, real_br, real_br);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	}

	frame_fmt &= ~FMT_STOP_BITS_MASK;
	if ((cflag & CSTOPB) != 0) {
		frame_fmt |= FMT_STOP_BITS_2;
	} else {
		frame_fmt |= FMT_STOP_BITS_1;
	}

	frame_fmt &= ~FMT_PARITY_MASK;
	if ((cflag & PARENB) != 0) {
		if ((cflag & PARODD) != 0) {
			frame_fmt |= FMT_PARITY_ODD;
		} else {
			frame_fmt |= FMT_PARITY_EVEN;
		}
	} else {
		frame_fmt |= FMT_PARITY_NONE;
	}

	control &= ~CONTROL_MASK;
	if ((cflag & CRTSCTS) != 0)
		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);

	/* change control lines if we are switching to or from B0 */
	/* FIXME:
	spin_lock_irqsave(&priv->lock, flags);
	control = priv->line_control;
	if ((cflag & CBAUD) == B0)
		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
	else
		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
	if (control != priv->line_control) {
		control = priv->line_control;
		spin_unlock_irqrestore(&priv->lock, flags);
		set_control_lines(serial->dev, control);
	} else {
		spin_unlock_irqrestore(&priv->lock, flags);
	}
	*/

	spin_lock_irqsave(&priv->lock, flags);
	if (divisor != priv->pending_setup.divisor
			|| control != priv->pending_setup.control
			|| frame_fmt != priv->pending_setup.frame_fmt) {
		priv->pending_setup.divisor = divisor;
		priv->pending_setup.control = control;
		priv->pending_setup.frame_fmt = frame_fmt;
	}
	spin_unlock_irqrestore(&priv->lock, flags);
}

A
Alan Cox 已提交
569 570
static int oti6858_open(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *filp)
571 572 573 574 575 576 577 578
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	struct ktermios tmp_termios;
	struct usb_serial *serial = port->serial;
	struct oti6858_control_pkt *buf;
	unsigned long flags;
	int result;

579
	dbg("%s(port = %d)", __func__, port->number);
580 581 582 583

	usb_clear_halt(serial->dev, port->write_urb->pipe);
	usb_clear_halt(serial->dev, port->read_urb->pipe);

A
Alan Cox 已提交
584
	if (port->port.count != 1)
585 586 587
		return 0;

	if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
588
		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		return -ENOMEM;
	}

	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				OTI6858_REQ_T_GET_STATUS,
				OTI6858_REQ_GET_STATUS,
				0, 0,
				buf, OTI6858_CTRL_PKT_SIZE,
				100);
	if (result != OTI6858_CTRL_PKT_SIZE) {
		/* assume default (after power-on reset) values */
		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
		buf->frame_fmt = 0x03;	/* 8N1 */
		buf->something = 0x43;
		buf->control = 0x4c;	/* DTR, RTS */
		buf->tx_status = 0x00;
		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
		buf->rx_bytes_avail = 0x00;
	}

	spin_lock_irqsave(&priv->lock, flags);
	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
	priv->pending_setup.divisor = buf->divisor;
	priv->pending_setup.frame_fmt = buf->frame_fmt;
	priv->pending_setup.control = buf->control;
	spin_unlock_irqrestore(&priv->lock, flags);
	kfree(buf);

617
	dbg("%s(): submitting interrupt urb", __func__);
618 619 620 621
	port->interrupt_in_urb->dev = serial->dev;
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (result != 0) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
622
			       " with error %d\n", __func__, result);
A
Alan Cox 已提交
623
		oti6858_close(tty, port, NULL);
624 625 626 627
		return -EPROTO;
	}

	/* setup termios */
A
Alan Cox 已提交
628 629
	if (tty)
		oti6858_set_termios(tty, port, &tmp_termios);
630 631 632 633

	return 0;
}

A
Alan Cox 已提交
634 635
static void oti6858_close(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *filp)
636 637 638 639 640 641
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	long timeout;
	wait_queue_t wait;

642
	dbg("%s(port = %d)", __func__, port->number);
643 644 645 646 647

	/* wait for data to drain from the buffer */
	spin_lock_irqsave(&priv->lock, flags);
	timeout = 30 * HZ;	/* PL2303_CLOSING_WAIT */
	init_waitqueue_entry(&wait, current);
A
Alan Cox 已提交
648
	add_wait_queue(&tty->write_wait, &wait);
649
	dbg("%s(): entering wait loop", __func__);
650 651
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
A
Alan Cox 已提交
652
		if (oti6858_buf_data_avail(priv->buf) == 0
653
		|| timeout == 0 || signal_pending(current)
654
		|| port->serial->disconnected)
655 656 657 658 659 660
			break;
		spin_unlock_irqrestore(&priv->lock, flags);
		timeout = schedule_timeout(timeout);
		spin_lock_irqsave(&priv->lock, flags);
	}
	set_current_state(TASK_RUNNING);
A
Alan Cox 已提交
661
	remove_wait_queue(&tty->write_wait, &wait);
662
	dbg("%s(): after wait loop", __func__);
663 664

	/* clear out any remaining data in the buffer */
A
Alan Cox 已提交
665
	oti6858_buf_clear(priv->buf);
666 667 668 669 670 671 672 673 674 675
	spin_unlock_irqrestore(&priv->lock, flags);

	/* wait for characters to drain from the device */
	/* (this is long enough for the entire 256 byte */
	/* pl2303 hardware buffer to drain with no flow */
	/* control for data rates of 1200 bps or more, */
	/* for lower rates we should really know how much */
	/* data is in the buffer to compute a delay */
	/* that is not unnecessarily long) */
	/* FIXME
A
Alan Cox 已提交
676
	bps = tty_get_baud_rate(tty);
677 678 679 680 681 682
	if (bps > 1200)
		timeout = max((HZ*2560)/bps,HZ/10);
	else
	*/
		timeout = 2*HZ;
	schedule_timeout_interruptible(timeout);
683
	dbg("%s(): after schedule_timeout_interruptible()", __func__);
684 685 686 687 688 689 690

	/* cancel scheduled setup */
	cancel_delayed_work(&priv->delayed_setup_work);
	cancel_delayed_work(&priv->delayed_write_work);
	flush_scheduled_work();

	/* shutdown our urbs */
691
	dbg("%s(): shutting down urbs", __func__);
692 693 694 695 696
	usb_kill_urb(port->write_urb);
	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->interrupt_in_urb);

	/*
A
Alan Cox 已提交
697
	if (tty && (tty->termios->c_cflag) & HUPCL) {
698 699 700 701 702 703 704 705
		// drop DTR and RTS
		spin_lock_irqsave(&priv->lock, flags);
		priv->pending_setup.control &= ~CONTROL_MASK;
		spin_unlock_irqrestore(&priv->lock, flags);
	}
	*/
}

A
Alan Cox 已提交
706
static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
707 708
				unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
709
	struct usb_serial_port *port = tty->driver_data;
710 711 712 713 714
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	u8 control;

	dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
715
				__func__, port->number, set, clear);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

	if (!usb_get_intfdata(port->serial->interface))
		return -ENODEV;

	/* FIXME: check if this is correct (active high/low) */
	spin_lock_irqsave(&priv->lock, flags);
	control = priv->pending_setup.control;
	if ((set & TIOCM_RTS) != 0)
		control |= CONTROL_RTS_HIGH;
	if ((set & TIOCM_DTR) != 0)
		control |= CONTROL_DTR_HIGH;
	if ((clear & TIOCM_RTS) != 0)
		control &= ~CONTROL_RTS_HIGH;
	if ((clear & TIOCM_DTR) != 0)
		control &= ~CONTROL_DTR_HIGH;

	if (control != priv->pending_setup.control) {
		priv->pending_setup.control = control;
	}
	spin_unlock_irqrestore(&priv->lock, flags);

	return 0;
}

A
Alan Cox 已提交
740
static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
741
{
A
Alan Cox 已提交
742
	struct usb_serial_port *port = tty->driver_data;
743 744 745 746 747
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned pin_state;
	unsigned result = 0;

748
	dbg("%s(port = %d)", __func__, port->number);
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

	if (!usb_get_intfdata(port->serial->interface))
		return -ENODEV;

	spin_lock_irqsave(&priv->lock, flags);
	pin_state = priv->status.pin_state & PIN_MASK;
	spin_unlock_irqrestore(&priv->lock, flags);

	/* FIXME: check if this is correct (active high/low) */
	if ((pin_state & PIN_RTS) != 0)
		result |= TIOCM_RTS;
	if ((pin_state & PIN_CTS) != 0)
		result |= TIOCM_CTS;
	if ((pin_state & PIN_DSR) != 0)
		result |= TIOCM_DSR;
	if ((pin_state & PIN_DTR) != 0)
		result |= TIOCM_DTR;
	if ((pin_state & PIN_RI) != 0)
		result |= TIOCM_RI;
	if ((pin_state & PIN_DCD) != 0)
		result |= TIOCM_CD;

771
	dbg("%s() = 0x%08x", __func__, result);
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

	return result;
}

static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned int prev, status;
	unsigned int changed;

	spin_lock_irqsave(&priv->lock, flags);
	prev = priv->status.pin_state;
	spin_unlock_irqrestore(&priv->lock, flags);

	while (1) {
		wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
		if (signal_pending(current))
			return -ERESTARTSYS;

		spin_lock_irqsave(&priv->lock, flags);
		status = priv->status.pin_state & PIN_MASK;
		spin_unlock_irqrestore(&priv->lock, flags);

		changed = prev ^ status;
		/* FIXME: check if this is correct (active high/low) */
		if (	((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
			((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
			((arg & TIOCM_CD)  && (changed & PIN_DCD)) ||
			((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
				return 0;
		}
		prev = status;
	}

	/* NOTREACHED */
	return 0;
}

A
Alan Cox 已提交
811
static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
812 813
			unsigned int cmd, unsigned long arg)
{
A
Alan Cox 已提交
814
	struct usb_serial_port *port = tty->driver_data;
815 816

	dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
817
				__func__, port->number, cmd, arg);
818 819 820

	switch (cmd) {
		case TIOCMIWAIT:
821
			dbg("%s(): TIOCMIWAIT", __func__);
822 823 824
			return wait_modem_info(port, arg);

		default:
825
			dbg("%s(): 0x%04x not supported", __func__, cmd);
826 827 828 829 830 831 832 833 834 835 836 837
			break;
	}

	return -ENOIOCTLCMD;
}


static void oti6858_shutdown(struct usb_serial *serial)
{
	struct oti6858_private *priv;
	int i;

838
	dbg("%s()", __func__);
839 840 841 842

	for (i = 0; i < serial->num_ports; ++i) {
		priv = usb_get_serial_port_data(serial->port[i]);
		if (priv) {
A
Alan Cox 已提交
843
			oti6858_buf_free(priv->buf);
844 845 846 847 848 849 850 851
			kfree(priv);
			usb_set_serial_port_data(serial->port[i], NULL);
		}
	}
}

static void oti6858_read_int_callback(struct urb *urb)
{
852
	struct usb_serial_port *port =  urb->context;
853 854
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	int transient = 0, can_recv = 0, resubmit = 1;
855
	int status = urb->status;
856

857
	dbg("%s(port = %d, status = %d)",
858
				__func__, port->number, status);
859

860
	switch (status) {
861 862 863 864 865 866 867 868
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s(): urb shutting down with status: %d",
869
					__func__, status);
870 871 872
		return;
	default:
		dbg("%s(): nonzero urb status received: %d",
873
					__func__, status);
874 875 876
		break;
	}

877
	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
878 879 880 881 882 883 884 885 886 887 888 889
		struct oti6858_control_pkt *xs = urb->transfer_buffer;
		unsigned long flags;

		spin_lock_irqsave(&priv->lock, flags);

		if (!priv->transient) {
			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
				if (xs->rx_bytes_avail == 0) {
					priv->transient = 4;
					priv->setup_done = 0;
					resubmit = 0;
					dbg("%s(): scheduling setup_line()",
890
					    __func__);
891 892 893 894 895 896 897 898 899 900 901 902 903 904
					schedule_delayed_work(&priv->delayed_setup_work, 0);
				}
			}
		} else {
			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
				priv->transient = 0;
			} else if (!priv->setup_done) {
				resubmit = 0;
			} else if (--priv->transient == 0) {
				if (xs->rx_bytes_avail == 0) {
					priv->transient = 4;
					priv->setup_done = 0;
					resubmit = 0;
					dbg("%s(): scheduling setup_line()",
905
					    __func__);
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
					schedule_delayed_work(&priv->delayed_setup_work, 0);
				}
			}
		}

		if (!priv->transient) {
			if (xs->pin_state != priv->status.pin_state)
				wake_up_interruptible(&priv->intr_wait);
			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
		}

		if (!priv->transient && xs->rx_bytes_avail != 0) {
			can_recv = xs->rx_bytes_avail;
			priv->flags.read_urb_in_use = 1;
		}

		transient = priv->transient;
		spin_unlock_irqrestore(&priv->lock, flags);
	}

	if (can_recv) {
		int result;

		port->read_urb->dev = port->serial->dev;
		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
		if (result != 0) {
			priv->flags.read_urb_in_use = 0;
			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
934
					" error %d\n", __func__, result);
935 936 937 938 939 940 941 942
		} else {
			resubmit = 0;
		}
	} else if (!transient) {
		unsigned long flags;

		spin_lock_irqsave(&priv->lock, flags);
		if (priv->flags.write_urb_in_use == 0
A
Alan Cox 已提交
943
				&& oti6858_buf_data_avail(priv->buf) != 0) {
944 945 946 947 948 949 950 951 952
			schedule_delayed_work(&priv->delayed_write_work,0);
			resubmit = 0;
		}
		spin_unlock_irqrestore(&priv->lock, flags);
	}

	if (resubmit) {
		int result;

953
//		dbg("%s(): submitting interrupt urb", __func__);
954 955 956 957 958
		urb->dev = port->serial->dev;
		result = usb_submit_urb(urb, GFP_ATOMIC);
		if (result != 0) {
			dev_err(&urb->dev->dev,
					"%s(): usb_submit_urb() failed with"
959
					" error %d\n", __func__, result);
960 961 962 963 964 965
		}
	}
}

static void oti6858_read_bulk_callback(struct urb *urb)
{
966
	struct usb_serial_port *port =  urb->context;
967 968 969 970
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
	unsigned long flags;
971
	int status = urb->status;
A
Alan Cox 已提交
972
	int result;
973

974
	dbg("%s(port = %d, status = %d)",
975
				__func__, port->number, status);
976 977 978 979 980

	spin_lock_irqsave(&priv->lock, flags);
	priv->flags.read_urb_in_use = 0;
	spin_unlock_irqrestore(&priv->lock, flags);

981
	if (status != 0) {
A
Alan Cox 已提交
982
		if (!port->port.count) {
983
			dbg("%s(): port is closed, exiting", __func__);
984 985 986
			return;
		}
		/*
987
		if (status == -EPROTO) {
988
			// PL2303 mysteriously fails with -EPROTO reschedule the read
989
			dbg("%s - caught -EPROTO, resubmitting the urb", __func__);
990 991
			result = usb_submit_urb(urb, GFP_ATOMIC);
			if (result)
992
				dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
993 994 995
			return;
		}
		*/
996
		dbg("%s(): unable to handle the error, exiting", __func__);
997 998 999
		return;
	}

A
Alan Cox 已提交
1000
	tty = port->port.tty;
1001
	if (tty != NULL && urb->actual_length > 0) {
A
Alan Cox 已提交
1002
		tty_insert_flip_string(tty, data, urb->actual_length);
1003 1004 1005 1006
		tty_flip_buffer_push(tty);
	}

	// schedule the interrupt urb if we are still open */
A
Alan Cox 已提交
1007
	if (port->port.count != 0) {
1008 1009 1010 1011
		port->interrupt_in_urb->dev = port->serial->dev;
		result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
		if (result != 0) {
			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1012
					" error %d\n", __func__, result);
1013 1014 1015 1016 1017 1018
		}
	}
}

static void oti6858_write_bulk_callback(struct urb *urb)
{
1019
	struct usb_serial_port *port =  urb->context;
1020
	struct oti6858_private *priv = usb_get_serial_port_data(port);
1021
	int status = urb->status;
1022 1023
	int result;

1024
	dbg("%s(port = %d, status = %d)",
1025
				__func__, port->number, status);
1026

1027
	switch (status) {
1028 1029 1030 1031 1032 1033 1034 1035
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s(): urb shutting down with status: %d",
1036
					__func__, status);
1037 1038 1039 1040 1041
		priv->flags.write_urb_in_use = 0;
		return;
	default:
		/* error in the urb, so we have to resubmit it */
		dbg("%s(): nonzero write bulk status received: %d",
1042 1043
					__func__, status);
		dbg("%s(): overflow in write", __func__);
1044 1045 1046 1047 1048 1049

		port->write_urb->transfer_buffer_length = 1;
		port->write_urb->dev = port->serial->dev;
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1050
					" error %d\n", __func__, result);
1051 1052 1053 1054 1055 1056 1057 1058 1059
		} else {
			return;
		}
	}

	priv->flags.write_urb_in_use = 0;

	// schedule the interrupt urb if we are still open */
	port->interrupt_in_urb->dev = port->serial->dev;
1060
	dbg("%s(): submitting interrupt urb", __func__);
1061 1062 1063
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result != 0) {
		dev_err(&port->dev, "%s(): failed submitting int urb,"
1064
					" error %d\n", __func__, result);
1065 1066 1067 1068 1069
	}
}


/*
A
Alan Cox 已提交
1070
 * oti6858_buf_alloc
1071 1072 1073
 *
 * Allocate a circular buffer and all associated memory.
 */
A
Alan Cox 已提交
1074
static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
1075
{
A
Alan Cox 已提交
1076
	struct oti6858_buf *pb;
1077 1078 1079 1080

	if (size == 0)
		return NULL;

A
Alan Cox 已提交
1081
	pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
	if (pb == NULL)
		return NULL;

	pb->buf_buf = kmalloc(size, GFP_KERNEL);
	if (pb->buf_buf == NULL) {
		kfree(pb);
		return NULL;
	}

	pb->buf_size = size;
	pb->buf_get = pb->buf_put = pb->buf_buf;

	return pb;
}

/*
A
Alan Cox 已提交
1098
 * oti6858_buf_free
1099 1100 1101
 *
 * Free the buffer and all associated memory.
 */
A
Alan Cox 已提交
1102
static void oti6858_buf_free(struct oti6858_buf *pb)
1103 1104 1105 1106 1107 1108 1109 1110
{
	if (pb) {
		kfree(pb->buf_buf);
		kfree(pb);
	}
}

/*
A
Alan Cox 已提交
1111
 * oti6858_buf_clear
1112 1113 1114
 *
 * Clear out all data in the circular buffer.
 */
A
Alan Cox 已提交
1115
static void oti6858_buf_clear(struct oti6858_buf *pb)
1116 1117 1118 1119 1120 1121 1122 1123
{
	if (pb != NULL) {
		/* equivalent to a get of all data available */
		pb->buf_get = pb->buf_put;
	}
}

/*
A
Alan Cox 已提交
1124
 * oti6858_buf_data_avail
1125 1126 1127 1128
 *
 * Return the number of bytes of data available in the circular
 * buffer.
 */
A
Alan Cox 已提交
1129
static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
1130 1131 1132 1133 1134 1135 1136
{
	if (pb == NULL)
		return 0;
	return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
}

/*
A
Alan Cox 已提交
1137
 * oti6858_buf_space_avail
1138 1139 1140 1141
 *
 * Return the number of bytes of space available in the circular
 * buffer.
 */
A
Alan Cox 已提交
1142
static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
1143 1144 1145 1146 1147 1148 1149
{
	if (pb == NULL)
		return 0;
	return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
}

/*
A
Alan Cox 已提交
1150
 * oti6858_buf_put
1151 1152 1153 1154 1155 1156
 *
 * Copy data data from a user buffer and put it into the circular buffer.
 * Restrict to the amount of space available.
 *
 * Return the number of bytes copied.
 */
A
Alan Cox 已提交
1157
static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
1158 1159 1160 1161 1162 1163 1164
					unsigned int count)
{
	unsigned int len;

	if (pb == NULL)
		return 0;

A
Alan Cox 已提交
1165
	len  = oti6858_buf_space_avail(pb);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = pb->buf_buf + pb->buf_size - pb->buf_put;
	if (count > len) {
		memcpy(pb->buf_put, buf, len);
		memcpy(pb->buf_buf, buf+len, count - len);
		pb->buf_put = pb->buf_buf + count - len;
	} else {
		memcpy(pb->buf_put, buf, count);
		if (count < len)
			pb->buf_put += count;
		else /* count == len */
			pb->buf_put = pb->buf_buf;
	}

	return count;
}

/*
A
Alan Cox 已提交
1189
 * oti6858_buf_get
1190 1191 1192 1193 1194 1195
 *
 * Get data from the circular buffer and copy to the given buffer.
 * Restrict to the amount of data available.
 *
 * Return the number of bytes copied.
 */
A
Alan Cox 已提交
1196
static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
1197 1198 1199 1200 1201 1202 1203
					unsigned int count)
{
	unsigned int len;

	if (pb == NULL)
		return 0;

A
Alan Cox 已提交
1204
	len = oti6858_buf_data_avail(pb);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	if (count > len)
		count = len;

	if (count == 0)
		return 0;

	len = pb->buf_buf + pb->buf_size - pb->buf_get;
	if (count > len) {
		memcpy(buf, pb->buf_get, len);
		memcpy(buf+len, pb->buf_buf, count - len);
		pb->buf_get = pb->buf_buf + count - len;
	} else {
		memcpy(buf, pb->buf_get, count);
		if (count < len)
			pb->buf_get += count;
		else /* count == len */
			pb->buf_get = pb->buf_buf;
	}

	return count;
}

/* module description and (de)initialization */

static int __init oti6858_init(void)
{
	int retval;

	if ((retval = usb_serial_register(&oti6858_device)) == 0) {
		if ((retval = usb_register(&oti6858_driver)) != 0)
			usb_serial_deregister(&oti6858_device);
		else
			return 0;
	}

	return retval;
}

static void __exit oti6858_exit(void)
{
	usb_deregister(&oti6858_driver);
	usb_serial_deregister(&oti6858_device);
}

module_init(oti6858_init);
module_exit(oti6858_exit);

MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
MODULE_AUTHOR(OTI6858_AUTHOR);
MODULE_VERSION(OTI6858_VERSION);
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "enable debug output");