11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Prolific PL2303 USB to serial adaptor driver
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
44d0dce3e0b794942407391c52f8dd2760802f391Greg Kroah-Hartman * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2003 IBM Corp.
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Original driver for 2.2.x by anonymous
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
94d0dce3e0b794942407391c52f8dd2760802f391Greg Kroah-Hartman *	This program is free software; you can redistribute it and/or
104d0dce3e0b794942407391c52f8dd2760802f391Greg Kroah-Hartman *	modify it under the terms of the GNU General Public License version
114d0dce3e0b794942407391c52f8dd2760802f391Greg Kroah-Hartman *	2 as published by the Free Software Foundation.
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
133a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox * See Documentation/usb/usb-serial.txt for more information on using this
143a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox * driver
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h>
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty.h>
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty_driver.h>
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty_flip.h>
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/serial.h>
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/moduleparam.h>
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/spinlock.h>
273a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox#include <linux/uaccess.h>
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/usb.h>
29a969888ce91673c7f4b86520d851a6f0d5a5fa7dGreg Kroah-Hartman#include <linux/usb/serial.h>
30b2d6d98fc71bb94ada78d433f26a93498802d3f8Johan Hovold#include <asm/unaligned.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "pl2303.h"
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold
34228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold#define PL2303_QUIRK_UART_STATE_IDX0		BIT(0)
3523c6acb9684ac87c2e752f61900c7a1a2dd543acJohan Hovold#define PL2303_QUIRK_LEGACY			BIT(1)
36228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold
377d40d7e85a25e01948bcb4dc3eda1355af318337Németh Mártonstatic const struct usb_device_id id_table[] = {
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
403d861494729c70d9ebeb7d93caa107897925c355Peter Moulder	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
43b483b6aaa56f0db72fa50e85b6499a32d82009bfMax Arnold	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
444be2fa186d54758296d30c565d7b5111dd45b000Steve Murphy	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
45727df3569b358ef440683787c2b9fe8cc55a0954Greg Kroah-Hartman	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
4618344a1cd5889d48dac67229fcf024ed300030d5Simone Contini	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
4796a3e79edff6f41b0f115a82f1a39d66218077a7Dario Lombardo	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
4891fcb1ce420e0a5f8d92d556d7008a78bc6ce1ebGreg KH	{ USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
508a28dea3accda319d51a1bf4d3e280771d946f78Masakazu Mokuno	{ USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
5658381719845d9ee19a321c2eb69cfa9b7886be9aWang Jun	{ USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
65228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1),
66228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
67228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65),
68228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
69228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75),
70228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold		.driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
71e7beb667842ad0f6ec95a22e7c88e71dfbd60649Andreas Loibl	{ USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
72912299f6f0587bb6c221705ed9949709b36b3c56Alan Cox	{ USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
73acbb36f116243bed515357264ecbb6ff9c6d2a5bPeter Favrholdt	{ USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
74c6c27721a42b991965bb792d5c196b8331d008d5Christian Lindner	{ USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
75c6c27721a42b991965bb792d5c196b8331d008d5Christian Lindner	{ USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
766cceb05f8df6e28ab90f44bdeba50d33928cdee5Denis MONTERRAT	{ USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
77c6c27721a42b991965bb792d5c196b8331d008d5Christian Lindner	{ USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
78491b04ce1c9adfa0cd73f095086f3c37da81b667Dick Streefland	{ USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
793b92847425a98d26ad9d2b8682d3ce6020c90752Matthew Meno	{ USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
80b7aa94b682dc6b6dcdc01d36f8e65cef5aae81e2Kim Oldfield	{ USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
818fd801339350b63cbb90730ff8b2be349fb3dc67Johannes Steingraeber	{ USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
822d94b981c7fcb0fba4aa3442cd180066dbedbbc8YOSHIFUJI Hideaki	{ USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
839e3285dba5cac12d656da66fd7d420ff1bc0ecc0Magnus Damm	{ USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
84cc311ee7d29d96f0bf15599f4456012d6f5ea23cDamien Stuart	{ USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
857c99200142c04d0f1ed3f048014591f841efdaedMatthew Arnold	{ USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
86af4b8514aafd53d97b05a0a30b7d4cfd2cbb7b81Mike Provencher	{ USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
87b16c02fbfb963fa2941b7517ebf1f8a21946775eAaron Sanders	{ USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
88b16c02fbfb963fa2941b7517ebf1f8a21946775eAaron Sanders	{ USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
89b16c02fbfb963fa2941b7517ebf1f8a21946775eAaron Sanders	{ USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
908540d66615c39010168ab97eaafb476ec2851298Gianpaolo Cugola	{ USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
91f36ecd5de93e4c85a9e3d25100c6e233155b12e5Jef Driesen	{ USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
9249276560c9004fce24c42e3c0ad75f34d956fc63Khanh-Dang Nguyen Thu Lam	{ USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
9335904e6b5106fda51b04c8b8080c04466865415fPawel Ludwikow	{ USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
949a61d72602771906e11a5944e8571f8006387b39Manuel Jander	{ USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
95598f0b703506da841d3459dc0c48506be14d1778Eric Benoit	{ USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ }					/* Terminating entry */
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
99372db8a780f63368c6960a167b7a19aad776d704Thiago GalesiMODULE_DEVICE_TABLE(usb, id_table);
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SET_LINE_REQUEST_TYPE		0x21
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SET_LINE_REQUEST		0x20
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SET_CONTROL_REQUEST_TYPE	0x21
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define SET_CONTROL_REQUEST		0x22
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CONTROL_DTR			0x01
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CONTROL_RTS			0x02
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define BREAK_REQUEST_TYPE		0x21
1103a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox#define BREAK_REQUEST			0x23
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define BREAK_ON			0xffff
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define BREAK_OFF			0x0000
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define GET_LINE_REQUEST_TYPE		0xa1
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define GET_LINE_REQUEST		0x21
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define VENDOR_WRITE_REQUEST_TYPE	0x40
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define VENDOR_WRITE_REQUEST		0x01
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define VENDOR_READ_REQUEST_TYPE	0xc0
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define VENDOR_READ_REQUEST		0x01
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
123228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold#define UART_STATE_INDEX		8
124dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold#define UART_STATE_MSR_MASK		0x8b
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_STATE_TRANSIENT_MASK	0x74
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_DCD			0x01
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_DSR			0x02
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_BREAK_ERROR		0x04
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_RING			0x08
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_FRAME_ERROR		0x10
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_PARITY_ERROR		0x20
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_OVERRUN_ERROR		0x40
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define UART_CTS			0x80
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsenum pl2303_type {
1377f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold	TYPE_01,	/* Type 0 and 1 (difference unknown) */
1387f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold	TYPE_HX,	/* HX version of the pl2303 chip */
139359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	TYPE_COUNT
140359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold};
141359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold
142359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovoldstruct pl2303_type_data {
143359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	speed_t max_baud_rate;
144359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	unsigned long quirks;
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1478bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovoldstruct pl2303_serial_private {
148b693468155f5650dcaf01089ecf2b56ab66f9271Johan Hovold	const struct pl2303_type_data *type;
149228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	unsigned long quirks;
1508bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold};
1518bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct pl2303_private {
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spinlock_t lock;
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 line_control;
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 line_status;
156623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold
157623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	u8 line_settings[7];
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
160b693468155f5650dcaf01089ecf2b56ab66f9271Johan Hovoldstatic const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
161359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	[TYPE_01] = {
162359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold		.max_baud_rate =	1228800,
163359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold		.quirks =		PL2303_QUIRK_LEGACY,
164359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	},
165399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala	[TYPE_HX] = {
166399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala		.max_baud_rate =	12000000,
167399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala	},
168359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold};
169359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold
170362eb02603be7bb835c47f2cf585954a5080449dJohan Hovoldstatic int pl2303_vendor_read(struct usb_serial *serial, u16 value,
171362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold							unsigned char buf[1])
172eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp{
173362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	struct device *dev = &serial->interface->dev;
174ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	int res;
175ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
176ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
177eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp			VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
178362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold			value, 0, buf, 1, 100);
179362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	if (res != 1) {
180362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
181362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold								value, res);
182362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		if (res >= 0)
183362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold			res = -EIO;
184362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold
185362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		return res;
186362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	}
187ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
188362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
189ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
190362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	return 0;
191eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp}
192eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp
193362eb02603be7bb835c47f2cf585954a5080449dJohan Hovoldstatic int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
194eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp{
195362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	struct device *dev = &serial->interface->dev;
196ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	int res;
197ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
198362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
199362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold
200ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
201eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp			VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
202eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp			value, index, NULL, 0, 100);
203362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	if (res) {
204362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
205362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold								value, res);
206362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		return res;
207362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	}
208ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
209362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	return 0;
210eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp}
211eb44da0b3aa0105cb38d81c5747a8feae64834beSarah Sharp
212228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovoldstatic int pl2303_probe(struct usb_serial *serial,
213228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold					const struct usb_device_id *id)
214228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold{
215228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	usb_set_serial_data(serial, (void *)id->driver_info);
216228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold
217228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	return 0;
218228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold}
219228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold
220372db8a780f63368c6960a167b7a19aad776d704Thiago Galesistatic int pl2303_startup(struct usb_serial *serial)
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2228bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	struct pl2303_serial_private *spriv;
2237f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold	enum pl2303_type type = TYPE_01;
2243e152505a57db6622deb1322c22551c046e33d16Sarah Sharp	unsigned char *buf;
2258bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2268bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
2278bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	if (!spriv)
2288bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold		return -ENOMEM;
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
230362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	buf = kmalloc(1, GFP_KERNEL);
2318bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	if (!buf) {
2328bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold		kfree(spriv);
2333e152505a57db6622deb1322c22551c046e33d16Sarah Sharp		return -ENOMEM;
2348bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	}
2353e152505a57db6622deb1322c22551c046e33d16Sarah Sharp
236b52e111363e366202386f3e67f71681dbbb8e5d9Greg Kroah-Hartman	if (serial->dev->descriptor.bDeviceClass == 0x02)
2377f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		type = TYPE_01;		/* type 0 */
238b52e111363e366202386f3e67f71681dbbb8e5d9Greg Kroah-Hartman	else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
2397f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		type = TYPE_HX;
240281393ad0bcfc309434d2bff38abc15805c2cbc4Greg Kroah-Hartman	else if (serial->dev->descriptor.bDeviceClass == 0x00)
2417f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		type = TYPE_01;		/* type 1 */
242281393ad0bcfc309434d2bff38abc15805c2cbc4Greg Kroah-Hartman	else if (serial->dev->descriptor.bDeviceClass == 0xFF)
2437f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		type = TYPE_01;		/* type 1 */
244b52e111363e366202386f3e67f71681dbbb8e5d9Greg Kroah-Hartman	dev_dbg(&serial->interface->dev, "device type: %d\n", type);
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
246359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	spriv->type = &pl2303_type_data[type];
247228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	spriv->quirks = (unsigned long)usb_get_serial_data(serial);
248359defdaa46c6ecf6a29600f3d57c663c9c35fd2Johan Hovold	spriv->quirks |= spriv->type->quirks;
249228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold
2508bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	usb_set_serial_data(serial, spriv);
2513e152505a57db6622deb1322c22551c046e33d16Sarah Sharp
252362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8484, buf);
253362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_write(serial, 0x0404, 0);
254362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8484, buf);
255362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8383, buf);
256362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8484, buf);
257362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_write(serial, 0x0404, 1);
258362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8484, buf);
259362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_read(serial, 0x8383, buf);
260362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_write(serial, 0, 1);
261362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold	pl2303_vendor_write(serial, 1, 0);
26223c6acb9684ac87c2e752f61900c7a1a2dd543acJohan Hovold	if (spriv->quirks & PL2303_QUIRK_LEGACY)
263362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		pl2303_vendor_write(serial, 2, 0x24);
2647f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold	else
2657f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		pl2303_vendor_write(serial, 2, 0x44);
2663e152505a57db6622deb1322c22551c046e33d16Sarah Sharp
2673e152505a57db6622deb1322c22551c046e33d16Sarah Sharp	kfree(buf);
268ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2708bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold}
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2728bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovoldstatic void pl2303_release(struct usb_serial *serial)
2738bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold{
274ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
2758bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2768bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	kfree(spriv);
2778bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold}
2788bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2798bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovoldstatic int pl2303_port_probe(struct usb_serial_port *port)
2808bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold{
2818bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	struct pl2303_private *priv;
2828bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2838bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2848bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	if (!priv)
2858bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold		return -ENOMEM;
2868bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2878bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	spin_lock_init(&priv->lock);
2888bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2898bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	usb_set_serial_port_data(port, priv);
2908bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
291d7be62211125c85fa1dd796e92aadce84961a502Johan Hovold	port->port.drain_delay = 256;
292d7be62211125c85fa1dd796e92aadce84961a502Johan Hovold
2938bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	return 0;
2948bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold}
2958bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
2968bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovoldstatic int pl2303_port_remove(struct usb_serial_port *port)
2978bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold{
298ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	struct pl2303_private *priv = usb_get_serial_port_data(port);
2998bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
3008bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	kfree(priv);
3018bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold
3028bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	return 0;
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
305f45d0a5aa593cdf48a37489fc61c145e16964288Johan Hovoldstatic int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
307f45d0a5aa593cdf48a37489fc61c145e16964288Johan Hovold	struct usb_device *dev = port->serial->dev;
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval;
3093a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox
310a6ec8245bf09fd51a0561ff372a12473b48d269bJohan Hovold	dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
311a6ec8245bf09fd51a0561ff372a12473b48d269bJohan Hovold
312372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
313372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi				 SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
314372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi				 value, 0, NULL, 0, 100);
315a6ec8245bf09fd51a0561ff372a12473b48d269bJohan Hovold	if (retval)
316a6ec8245bf09fd51a0561ff372a12473b48d269bJohan Hovold		dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
317ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32159afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold/*
3225d85045f4b082800beb384094fc67fa0d2096563Johan Hovold * Returns the nearest supported baud rate that can be set directly without
3235d85045f4b082800beb384094fc67fa0d2096563Johan Hovold * using divisors.
32459afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold */
32559afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovoldstatic speed_t pl2303_get_supported_baud_rate(speed_t baud)
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
32759afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	static const speed_t baud_sup[] = {
32859afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold		75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
32959afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold		14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
3305d85045f4b082800beb384094fc67fa0d2096563Johan Hovold		614400, 921600, 1228800, 2457600, 3000000, 6000000
33159afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	};
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33359afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	unsigned i;
334692ed4ddf0010dd643d38d6ef1a15bf64a7fbc6dGreg Kroah-Hartman
3357e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman	for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
3367e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman		if (baud_sup[i] > baud)
3377e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman			break;
3387e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman	}
339692ed4ddf0010dd643d38d6ef1a15bf64a7fbc6dGreg Kroah-Hartman
3407e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman	if (i == ARRAY_SIZE(baud_sup))
3417e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman		baud = baud_sup[i - 1];
3427e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman	else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
3437e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman		baud = baud_sup[i - 1];
3447e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman	else
3457e12a6fcbf266eb0d5b19761f91b2964ad18e371Greg Kroah-Hartman		baud = baud_sup[i];
346692ed4ddf0010dd643d38d6ef1a15bf64a7fbc6dGreg Kroah-Hartman
34759afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	return baud;
34859afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold}
34959afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold
35020b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold/*
35120b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
35220b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold *       use 9600 baud.
35320b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold */
35420b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovoldstatic speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
35520b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold								speed_t baud)
35620b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold{
35720b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold	put_unaligned_le32(baud, buf);
35820b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold
35920b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold	return baud;
36020b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold}
36120b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold
362c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovoldstatic speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
363c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold								speed_t baud)
364c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold{
365c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	unsigned int tmp;
366c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold
367c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	/*
368c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	 * Apparently the formula is:
369c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	 * baudrate = 12M * 32 / (2^buf[1]) / buf[0]
370c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	 */
371c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	tmp = 12000000 * 32 / baud;
372c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	buf[3] = 0x80;
373c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	buf[2] = 0;
374c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	buf[1] = (tmp >= 256);
375c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	while (tmp >= 256) {
376c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold		tmp >>= 2;
377c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold		buf[1] <<= 1;
378c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	}
379c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	buf[0] = tmp;
380c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold
381c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold	return baud;
382c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold}
383c82c6d45a2fc882fedfde517ba86690b2d5ed555Johan Hovold
38459afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovoldstatic void pl2303_encode_baud_rate(struct tty_struct *tty,
38559afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold					struct usb_serial_port *port,
38659afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold					u8 buf[4])
38759afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold{
38859afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	struct usb_serial *serial = port->serial;
38959afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
3905d85045f4b082800beb384094fc67fa0d2096563Johan Hovold	speed_t	baud_sup;
39159afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	speed_t baud;
39259afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold
39359afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	baud = tty_get_baud_rate(tty);
39459afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	dev_dbg(&port->dev, "baud requested = %u\n", baud);
39559afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold	if (!baud)
39659afe10e8dd33d26f8c2ae7572f9023044e71babJohan Hovold		return;
397871996ede12306cd1d75ed8135bed6f1fcbcd0e6Johan Hovold
398871996ede12306cd1d75ed8135bed6f1fcbcd0e6Johan Hovold	if (spriv->type->max_baud_rate)
399871996ede12306cd1d75ed8135bed6f1fcbcd0e6Johan Hovold		baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
4005d85045f4b082800beb384094fc67fa0d2096563Johan Hovold	/*
401399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala	 * Use direct method for supported baud rates, otherwise use divisors.
4025d85045f4b082800beb384094fc67fa0d2096563Johan Hovold	 */
4035d85045f4b082800beb384094fc67fa0d2096563Johan Hovold	baud_sup = pl2303_get_supported_baud_rate(baud);
40420b4c787199f4fcf0fae5ed78a4ff0104e2afaa3Johan Hovold
405399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala	if (baud == baud_sup)
406399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala		baud = pl2303_encode_baud_rate_direct(buf, baud);
4075d85045f4b082800beb384094fc67fa0d2096563Johan Hovold	else
408399aa9a75ad372b301e5050f3653a297a767fdc4Lauri Hintsala		baud = pl2303_encode_baud_rate_divisor(buf, baud);
409692ed4ddf0010dd643d38d6ef1a15bf64a7fbc6dGreg Kroah-Hartman
41015e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	/* Save resulting baud rate */
411b2d6d98fc71bb94ada78d433f26a93498802d3f8Johan Hovold	tty_encode_baud_rate(tty, baud, baud);
412f84ee3b2f5e5e39041c39268a9eab5046a050d44Johan Hovold	dev_dbg(&port->dev, "baud set = %u\n", baud);
41315e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold}
41415e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
415383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovoldstatic int pl2303_get_line_request(struct usb_serial_port *port,
416383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold							unsigned char buf[7])
417383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold{
418383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	struct usb_device *udev = port->serial->dev;
419383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	int ret;
420383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
421383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
422383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold				GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
423383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold				0, 0, buf, 7, 100);
424383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	if (ret != 7) {
425383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
426383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
427383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		if (ret > 0)
428383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold			ret = -EIO;
429383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
430383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		return ret;
431383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	}
432383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
433383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
434383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
435383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	return 0;
436383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold}
437383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
438383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovoldstatic int pl2303_set_line_request(struct usb_serial_port *port,
439383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold							unsigned char buf[7])
440383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold{
441383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	struct usb_device *udev = port->serial->dev;
442383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	int ret;
443383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
444383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
445383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold				SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
446383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold				0, 0, buf, 7, 100);
447383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	if (ret != 7) {
448383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
449383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
450383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		if (ret > 0)
451383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold			ret = -EIO;
452383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
453383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		return ret;
454383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	}
455383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
456383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
457383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
458383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	return 0;
459383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold}
460383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold
46115e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovoldstatic void pl2303_set_termios(struct tty_struct *tty,
46215e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold		struct usb_serial_port *port, struct ktermios *old_termios)
46315e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold{
46415e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	struct usb_serial *serial = port->serial;
46515e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
46615e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	struct pl2303_private *priv = usb_get_serial_port_data(port);
46715e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	unsigned long flags;
46815e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	unsigned char *buf;
469383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	int ret;
47015e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	u8 control;
47115e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
47215e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
47315e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold		return;
47415e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
47515e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	buf = kzalloc(7, GFP_KERNEL);
47615e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	if (!buf) {
47715e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold		/* Report back no change occurred */
47815e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold		if (old_termios)
47915e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold			tty->termios = *old_termios;
48015e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold		return;
48115e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	}
48215e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
483383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold	pl2303_get_line_request(port, buf);
48415e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
485a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	switch (C_CSIZE(tty)) {
486a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	case CS5:
487a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		buf[6] = 5;
488a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		break;
489a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	case CS6:
490a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		buf[6] = 6;
491a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		break;
492a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	case CS7:
493a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		buf[6] = 7;
494a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		break;
495a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	default:
496a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	case CS8:
497a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner		buf[6] = 8;
49815e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold	}
499a313249937820f8b1996133fc285efbd6aad2c5bColin Leitner	dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
50015e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
501692ed4ddf0010dd643d38d6ef1a15bf64a7fbc6dGreg Kroah-Hartman	/* For reference buf[0]:buf[3] baud rate value */
50279816824c1ae73542d9523283ce353142b67563eJohan Hovold	pl2303_encode_baud_rate(tty, port, &buf[0]);
50315e7cead1ed89d445cf86c75a72368e98a7a9039Johan Hovold
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* For reference buf[4]=0 is 1 stop bits */
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* For reference buf[4]=1 is 1.5 stop bits */
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* For reference buf[4]=2 is 2 stop bits */
50787265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold	if (C_CSTOPB(tty)) {
50887265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold		/*
50987265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold		 * NOTE: Comply with "real" UARTs / RS232:
51029cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer		 *       use 1.5 instead of 2 stop bits with 5 data bits
51129cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer		 */
51287265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold		if (C_CSIZE(tty) == CS5) {
51329cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer			buf[4] = 1;
514d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman			dev_dbg(&port->dev, "stop bits = 1.5\n");
51529cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer		} else {
51629cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer			buf[4] = 2;
517d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman			dev_dbg(&port->dev, "stop bits = 2\n");
51829cf1b72f34519413b3fafbccc9ac776eb948edeFrank Schaefer		}
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[4] = 0;
521d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_dbg(&port->dev, "stop bits = 1\n");
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
52487265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold	if (C_PARENB(tty)) {
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* For reference buf[5]=0 is none parity */
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* For reference buf[5]=1 is odd parity */
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* For reference buf[5]=2 is even parity */
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* For reference buf[5]=3 is mark parity */
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* For reference buf[5]=4 is space parity */
53087265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold		if (C_PARODD(tty)) {
531619c43543a946dd2f094d2f1bfc81a3fd48356d8Johan Hovold			if (C_CMSPAR(tty)) {
5326dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer				buf[5] = 3;
533d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman				dev_dbg(&port->dev, "parity = mark\n");
5346dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer			} else {
5356dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer				buf[5] = 1;
536d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman				dev_dbg(&port->dev, "parity = odd\n");
5376dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer			}
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
539619c43543a946dd2f094d2f1bfc81a3fd48356d8Johan Hovold			if (C_CMSPAR(tty)) {
5406dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer				buf[5] = 4;
541d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman				dev_dbg(&port->dev, "parity = space\n");
5426dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer			} else {
5436dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer				buf[5] = 2;
544d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman				dev_dbg(&port->dev, "parity = even\n");
5456dd81b45fd7628f3eb308f387aee696366718f25Frank Schaefer			}
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[5] = 0;
549d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_dbg(&port->dev, "parity = none\n");
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
552623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	/*
553623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * Some PL2303 are known to lose bytes if you change serial settings
554623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * even to the same values as before. Thus we actually need to filter
555623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * in this specific case.
556623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 *
557623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * Note that the tty_termios_hw_change check above is not sufficient
558623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * as a previously requested baud rate may differ from the one
559623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * actually used (and stored in old_termios).
560623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 *
561623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 * NOTE: No additional locking needed for line_settings as it is
562623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 *       only used in set_termios, which is serialised against itself.
563623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	 */
564623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
565383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		ret = pl2303_set_line_request(port, buf);
566383d19c58729e34b3b94e47da20aa7fe4970a577Johan Hovold		if (!ret)
567623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold			memcpy(priv->line_settings, buf, 7);
568623c8263376c0b8a4b0c220232e7313d762cd0ccJohan Hovold	}
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* change control lines if we are switching to or from B0 */
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock_irqsave(&priv->lock, flags);
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	control = priv->line_control;
57387265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold	if (C_BAUD(tty) == B0)
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
5752d8f4447b58bba5f8cb895c07690434c02307eafJohan Hovold	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (control != priv->line_control) {
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		control = priv->line_control;
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(&priv->lock, flags);
580f45d0a5aa593cdf48a37489fc61c145e16964288Johan Hovold		pl2303_set_control_lines(port, control);
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_unlock_irqrestore(&priv->lock, flags);
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
584372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi
58587265b4514f5a0b79efdccdcb1c6331285652640Johan Hovold	if (C_CRTSCTS(tty)) {
58623c6acb9684ac87c2e752f61900c7a1a2dd543acJohan Hovold		if (spriv->quirks & PL2303_QUIRK_LEGACY)
587362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold			pl2303_vendor_write(serial, 0x0, 0x41);
5887f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold		else
5897f966ac7a939633ff6fa8cec58982676c243b4f8Johan Hovold			pl2303_vendor_write(serial, 0x0, 0x61);
590715f9527c1c1edd1a9c7a55ab4535211279c9374t.sefzick	} else {
591362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		pl2303_vendor_write(serial, 0x0, 0x0);
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
593572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi
594572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	kfree(buf);
595572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi}
596572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi
597335f8514f200e63d689113d29cb7253a5c282967Alan Coxstatic void pl2303_dtr_rts(struct usb_serial_port *port, int on)
598335f8514f200e63d689113d29cb7253a5c282967Alan Cox{
599335f8514f200e63d689113d29cb7253a5c282967Alan Cox	struct pl2303_private *priv = usb_get_serial_port_data(port);
600335f8514f200e63d689113d29cb7253a5c282967Alan Cox	unsigned long flags;
601335f8514f200e63d689113d29cb7253a5c282967Alan Cox	u8 control;
602335f8514f200e63d689113d29cb7253a5c282967Alan Cox
603335f8514f200e63d689113d29cb7253a5c282967Alan Cox	spin_lock_irqsave(&priv->lock, flags);
604335f8514f200e63d689113d29cb7253a5c282967Alan Cox	if (on)
605335f8514f200e63d689113d29cb7253a5c282967Alan Cox		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
606335f8514f200e63d689113d29cb7253a5c282967Alan Cox	else
607335f8514f200e63d689113d29cb7253a5c282967Alan Cox		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
608335f8514f200e63d689113d29cb7253a5c282967Alan Cox	control = priv->line_control;
609335f8514f200e63d689113d29cb7253a5c282967Alan Cox	spin_unlock_irqrestore(&priv->lock, flags);
610ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
611f45d0a5aa593cdf48a37489fc61c145e16964288Johan Hovold	pl2303_set_control_lines(port, control);
612335f8514f200e63d689113d29cb7253a5c282967Alan Cox}
613335f8514f200e63d689113d29cb7253a5c282967Alan Cox
614335f8514f200e63d689113d29cb7253a5c282967Alan Coxstatic void pl2303_close(struct usb_serial_port *port)
615572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi{
6168b0127b2082601e40295045414a8318f2c8ee5a0Johan Hovold	usb_serial_generic_close(port);
617572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	usb_kill_urb(port->interrupt_in_urb);
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
620a509a7e478e4766114d69f12d19d644ac63e9765Alan Coxstatic int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial = port->serial;
6238bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
62623c6acb9684ac87c2e752f61900c7a1a2dd543acJohan Hovold	if (spriv->quirks & PL2303_QUIRK_LEGACY) {
6271694899fd1af43636351aac97f415fd3c9cefb1dDariusz M		usb_clear_halt(serial->dev, port->write_urb->pipe);
6281694899fd1af43636351aac97f415fd3c9cefb1dDariusz M		usb_clear_halt(serial->dev, port->read_urb->pipe);
6293e152505a57db6622deb1322c22551c046e33d16Sarah Sharp	} else {
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* reset upstream data pipes */
631362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		pl2303_vendor_write(serial, 8, 0);
632362eb02603be7bb835c47f2cf585954a5080449dJohan Hovold		pl2303_vendor_write(serial, 9, 0);
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Setup termios */
63695da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	if (tty)
6372d8f4447b58bba5f8cb895c07690434c02307eafJohan Hovold		pl2303_set_termios(tty, port, NULL);
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
639372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (result) {
641ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold		dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
642ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold			result);
643db6e9186c90188edea20aaa5161ea073440cc2a1Johan Hovold		return result;
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
645d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold
646f5230a53c1d551811b077ccad219105786da1becJohan Hovold	result = usb_serial_generic_open(tty, port);
647d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold	if (result) {
648d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold		usb_kill_urb(port->interrupt_in_urb);
649d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold		return result;
650d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold	}
651d4691c3fa3d371835b1eacb39066ab113c4b9d8cJohan Hovold
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
65520b9d17715017ae4dd4ec87fabc36d33b9de708eAlan Coxstatic int pl2303_tiocmset(struct tty_struct *tty,
656372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi			   unsigned int set, unsigned int clear)
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
65895da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	struct usb_serial_port *port = tty->driver_data;
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct pl2303_private *priv = usb_get_serial_port_data(port);
6601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
6611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 control;
6626f1efd6c5aa63ddcfe1ffc60ade716f5421766f4Johan Hovold	int ret;
6636fdd8e8e33730a2abc886113bd0b6c4343f63cc9Flavio Leitner
664372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	spin_lock_irqsave(&priv->lock, flags);
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (set & TIOCM_RTS)
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control |= CONTROL_RTS;
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (set & TIOCM_DTR)
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control |= CONTROL_DTR;
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (clear & TIOCM_RTS)
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control &= ~CONTROL_RTS;
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (clear & TIOCM_DTR)
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->line_control &= ~CONTROL_DTR;
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	control = priv->line_control;
674372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	spin_unlock_irqrestore(&priv->lock, flags);
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6765ddbb26b8b571231cace9f013b2d0ae66229b316Johan Hovold	ret = pl2303_set_control_lines(port, control);
6775ddbb26b8b571231cace9f013b2d0ae66229b316Johan Hovold	if (ret)
6785ddbb26b8b571231cace9f013b2d0ae66229b316Johan Hovold		return usb_translate_errors(ret);
6796f1efd6c5aa63ddcfe1ffc60ade716f5421766f4Johan Hovold
6805ddbb26b8b571231cace9f013b2d0ae66229b316Johan Hovold	return 0;
6811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
68360b33c133ca0b7c0b6072c87234b63fee6e80558Alan Coxstatic int pl2303_tiocmget(struct tty_struct *tty)
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
68595da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	struct usb_serial_port *port = tty->driver_data;
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct pl2303_private *priv = usb_get_serial_port_data(port);
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int mcr;
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int status;
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int result;
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
692372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	spin_lock_irqsave(&priv->lock, flags);
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mcr = priv->line_control;
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	status = priv->line_status;
695372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	spin_unlock_irqrestore(&priv->lock, flags);
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	result = ((mcr & CONTROL_DTR)		? TIOCM_DTR : 0)
6981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		  | ((mcr & CONTROL_RTS)	? TIOCM_RTS : 0)
6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		  | ((status & UART_CTS)	? TIOCM_CTS : 0)
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		  | ((status & UART_DSR)	? TIOCM_DSR : 0)
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		  | ((status & UART_RING)	? TIOCM_RI  : 0)
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		  | ((status & UART_DCD)	? TIOCM_CD  : 0);
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
704d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman	dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result;
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
709335f8514f200e63d689113d29cb7253a5c282967Alan Coxstatic int pl2303_carrier_raised(struct usb_serial_port *port)
710335f8514f200e63d689113d29cb7253a5c282967Alan Cox{
711335f8514f200e63d689113d29cb7253a5c282967Alan Cox	struct pl2303_private *priv = usb_get_serial_port_data(port);
712ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
713335f8514f200e63d689113d29cb7253a5c282967Alan Cox	if (priv->line_status & UART_DCD)
714335f8514f200e63d689113d29cb7253a5c282967Alan Cox		return 1;
715ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
716335f8514f200e63d689113d29cb7253a5c282967Alan Cox	return 0;
717335f8514f200e63d689113d29cb7253a5c282967Alan Cox}
718335f8514f200e63d689113d29cb7253a5c282967Alan Cox
71900a0d0d65b61241a718d0aee96f46b9a2d93bf26Alan Coxstatic int pl2303_ioctl(struct tty_struct *tty,
720372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi			unsigned int cmd, unsigned long arg)
7211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
72267b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas	struct serial_struct ser;
72395da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	struct usb_serial_port *port = tty->driver_data;
724d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
72667b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas	case TIOCGSERIAL:
72767b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas		memset(&ser, 0, sizeof ser);
72867b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas		ser.type = PORT_16654;
729e5b1e2062e0535e8ffef79bb34d857e21380d101Greg Kroah-Hartman		ser.line = port->minor;
7301143832eca8f1d64da7d85642c956ae9d25c69e1Greg Kroah-Hartman		ser.port = port->port_number;
73167b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas		ser.baud_base = 460800;
73267b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas
73367b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas		if (copy_to_user((void __user *)arg, &ser, sizeof ser))
73467b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas			return -EFAULT;
73567b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas
73667b9946dd07eeef8188e4cab816d2c370bcaa7b2John Tsiombikas		return 0;
7373a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox	default:
7383a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox		break;
7391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
740ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -ENOIOCTLCMD;
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
74495da310e66ee8090119596c70ca8432e57f9a97fAlan Coxstatic void pl2303_break_ctl(struct tty_struct *tty, int break_state)
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
74695da310e66ee8090119596c70ca8432e57f9a97fAlan Cox	struct usb_serial_port *port = tty->driver_data;
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial = port->serial;
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u16 state;
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (break_state == 0)
7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		state = BREAK_OFF;
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		state = BREAK_ON;
755ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
756d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
7573a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox			state == BREAK_OFF ? "off" : "on");
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
759372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
760372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi				 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
761372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi				 0, NULL, 0, 100);
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (result)
763d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_err(&port->dev, "error sending break = %d\n", result);
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
76697bb13ec5bc156352cca8af90080597e04299a73Flavio Leitnerstatic void pl2303_update_line_status(struct usb_serial_port *port,
76797bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner				      unsigned char *data,
76897bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner				      unsigned int actual_length)
76997bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner{
770228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	struct usb_serial *serial = port->serial;
771228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
77297bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	struct pl2303_private *priv = usb_get_serial_port_data(port);
773d14fc1a74e846d7851f24fc9519fe87dc12a1231Libor Pechacek	struct tty_struct *tty;
77497bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	unsigned long flags;
775228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	unsigned int status_idx = UART_STATE_INDEX;
7766020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	u8 status;
7776020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	u8 delta;
77897bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner
779228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
780228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold		status_idx = 0;
78197bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner
782228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	if (actual_length < status_idx + 1)
783a009b75aa0ed55f0bc473c8a3b3e872cbca807ecLuiz Fernando N. Capitulino		return;
78497bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner
7856020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	status = data[status_idx];
7866020c3bec359873c1e4081785f220db99694c4e4Johan Hovold
7873a0f43e9deffd9619ac34e3b6b9ba7089aa1e511Alan Cox	/* Save off the uart status for others to look at */
78897bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	spin_lock_irqsave(&priv->lock, flags);
7896020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	delta = priv->line_status ^ status;
7906020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	priv->line_status = status;
79197bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	spin_unlock_irqrestore(&priv->lock, flags);
792ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
7936020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	if (status & UART_BREAK_ERROR)
794430eb0d27c1b36c5191c16b2472b26137673a8d4Jason Wessel		usb_serial_handle_break(port);
795ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold
796dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold	if (delta & UART_STATE_MSR_MASK) {
797a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold		if (delta & UART_CTS)
798a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold			port->icount.cts++;
799a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold		if (delta & UART_DSR)
800a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold			port->icount.dsr++;
801a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold		if (delta & UART_RING)
802a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold			port->icount.rng++;
803dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold		if (delta & UART_DCD) {
804a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold			port->icount.dcd++;
805dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold			tty = tty_port_tty_get(&port->port);
806dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold			if (tty) {
807dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold				usb_serial_handle_dcd_change(port, tty,
8086020c3bec359873c1e4081785f220db99694c4e4Johan Hovold							status & UART_DCD);
809dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold				tty_kref_put(tty);
810dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold			}
8116020c3bec359873c1e4081785f220db99694c4e4Johan Hovold		}
812dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold
813dbfd2866ac36bab52a4f37384e935eb5df76ad60Johan Hovold		wake_up_interruptible(&port->port.delta_msr_wait);
8146020c3bec359873c1e4081785f220db99694c4e4Johan Hovold	}
81597bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner}
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8177d12e780e003f93433d49ce78cfedf4b4c52adc5David Howellsstatic void pl2303_read_int_callback(struct urb *urb)
8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
819cdc97792289179974af6dda781c855696358d307Ming Lei	struct usb_serial_port *port =  urb->context;
8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char *data = urb->transfer_buffer;
82197bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	unsigned int actual_length = urb->actual_length;
822461d696aeeae294ad22dfcaae462d1757f955778Greg Kroah-Hartman	int status = urb->status;
823461d696aeeae294ad22dfcaae462d1757f955778Greg Kroah-Hartman	int retval;
8241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
825461d696aeeae294ad22dfcaae462d1757f955778Greg Kroah-Hartman	switch (status) {
8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case 0:
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* success */
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
8291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case -ECONNRESET:
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case -ENOENT:
8311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case -ESHUTDOWN:
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* this urb is terminated, clean up */
833d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
834d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman			__func__, status);
8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
837d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
838d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman			__func__, status);
8391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
8401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
84259d33f2fc2d63796296b1b76143e039d6e7cf532Greg Kroah-Hartman	usb_serial_debug_data(&port->dev, __func__,
843372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi			      urb->actual_length, urb->transfer_buffer);
844372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi
84597bb13ec5bc156352cca8af90080597e04299a73Flavio Leitner	pl2303_update_line_status(port, data, actual_length);
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsexit:
848461d696aeeae294ad22dfcaae462d1757f955778Greg Kroah-Hartman	retval = usb_submit_urb(urb, GFP_ATOMIC);
849ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	if (retval) {
850d8789b2b90aeb53ce59ac232d2ff0d8357d53893Greg Kroah-Hartman		dev_err(&port->dev,
851372db8a780f63368c6960a167b7a19aad776d704Thiago Galesi			"%s - usb_submit_urb failed with result %d\n",
852441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, retval);
853ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	}
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
856f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovoldstatic void pl2303_process_read_urb(struct urb *urb)
857d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox{
858f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	struct usb_serial_port *port = urb->context;
859f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	struct pl2303_private *priv = usb_get_serial_port_data(port);
860d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	unsigned char *data = urb->transfer_buffer;
861d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	char tty_flag = TTY_NORMAL;
862f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	unsigned long flags;
863f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	u8 line_status;
864f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	int i;
865f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold
866f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	/* update line status */
867f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	spin_lock_irqsave(&priv->lock, flags);
868f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	line_status = priv->line_status;
869f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
870f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	spin_unlock_irqrestore(&priv->lock, flags);
871f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold
872f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	if (!urb->actual_length)
873f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold		return;
874f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold
875ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	/*
876ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	 * Break takes precedence over parity, which takes precedence over
877ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	 * framing errors.
878ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	 */
879d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	if (line_status & UART_BREAK_ERROR)
880d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox		tty_flag = TTY_BREAK;
881d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	else if (line_status & UART_PARITY_ERROR)
882d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox		tty_flag = TTY_PARITY;
883d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	else if (line_status & UART_FRAME_ERROR)
884d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox		tty_flag = TTY_FRAME;
885d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox
8863ba19fe31b726371b45f91a8de9341f5625e75e4Johan Hovold	if (tty_flag != TTY_NORMAL)
8873ba19fe31b726371b45f91a8de9341f5625e75e4Johan Hovold		dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
8883ba19fe31b726371b45f91a8de9341f5625e75e4Johan Hovold								tty_flag);
889d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	/* overrun is special, not associated with a char */
890d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox	if (line_status & UART_OVERRUN_ERROR)
89192a19f9cec9a80ad93c06e115822deb729e2c6adJiri Slaby		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
8929388e2e71a51fab0aa2309bbb45e8a23d89a95a9Johan Hovold
893d45cc8df7f59eb4db28408076ce979cd5e18f2b7Johan Hovold	if (port->port.console && port->sysrq) {
894d4fc4a7bfc2dee626f4fec1e209e58eaa4312de6Alan Cox		for (i = 0; i < urb->actual_length; ++i)
8956ee9f4b4affe751d313d2538999aeec134d413a6Dmitry Torokhov			if (!usb_serial_handle_sysrq_char(port, data[i]))
89692a19f9cec9a80ad93c06e115822deb729e2c6adJiri Slaby				tty_insert_flip_char(&port->port, data[i],
89792a19f9cec9a80ad93c06e115822deb729e2c6adJiri Slaby						tty_flag);
898d45cc8df7f59eb4db28408076ce979cd5e18f2b7Johan Hovold	} else {
8992f69335710884ae6112fc8196ebe29b5cda7b79bJiri Slaby		tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
900d45cc8df7f59eb4db28408076ce979cd5e18f2b7Johan Hovold							urb->actual_length);
9019388e2e71a51fab0aa2309bbb45e8a23d89a95a9Johan Hovold	}
9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9032e124b4a390ca85325fae75764bef92f0547fa25Jiri Slaby	tty_flip_buffer_push(&port->port);
9041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
906572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesistatic struct usb_serial_driver pl2303_device = {
907572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.driver = {
908572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi		.owner =	THIS_MODULE,
909572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi		.name =		"pl2303",
910572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	},
911572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.id_table =		id_table,
912572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.num_ports =		1,
9137919c2fd9e07276403b9a4d9ae52305e0d70f923Johan Hovold	.bulk_in_size =		256,
9143efeaff6298290b36499532f0b4c87aa4bae8aefJohan Hovold	.bulk_out_size =	256,
915572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.open =			pl2303_open,
916572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.close =		pl2303_close,
917ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan Hovold	.dtr_rts =		pl2303_dtr_rts,
918335f8514f200e63d689113d29cb7253a5c282967Alan Cox	.carrier_raised =	pl2303_carrier_raised,
919572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.ioctl =		pl2303_ioctl,
920572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.break_ctl =		pl2303_break_ctl,
921572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.set_termios =		pl2303_set_termios,
922572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.tiocmget =		pl2303_tiocmget,
923572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.tiocmset =		pl2303_tiocmset,
924a4bcb2945926901bbacf978eb0c0fdda5b327dc7Johan Hovold	.tiocmiwait =		usb_serial_generic_tiocmiwait,
925f08e07ac8b87b1a4b8ff88a061195e99282dfbbdJohan Hovold	.process_read_urb =	pl2303_process_read_urb,
926572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.read_int_callback =	pl2303_read_int_callback,
927228e4105374cffd5372b1dbdf1f8ec8cf1d964aeJohan Hovold	.probe =		pl2303_probe,
928572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi	.attach =		pl2303_startup,
929f9c99bb8b3a1ec81af68d484a551307326c2e933Alan Stern	.release =		pl2303_release,
9308bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	.port_probe =		pl2303_port_probe,
9318bf769eb5f6efc33f95088850f33fcc05d28b508Johan Hovold	.port_remove =		pl2303_port_remove,
932572d3138eb0cf17a2bf36944cc1d2c753578862eThiago Galesi};
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
934f667ddad41e303ebc2c6d5bf3105dffe2fbdd717Alan Sternstatic struct usb_serial_driver * const serial_drivers[] = {
935f667ddad41e303ebc2c6d5bf3105dffe2fbdd717Alan Stern	&pl2303_device, NULL
936f667ddad41e303ebc2c6d5bf3105dffe2fbdd717Alan Stern};
937f667ddad41e303ebc2c6d5bf3105dffe2fbdd717Alan Stern
93868e24113457e437b1576670f2419b77ed0531e9eGreg Kroah-Hartmanmodule_usb_serial_driver(serial_drivers, id_table);
9391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
940ccfe8188a321f4039a7e52c8336bb4ff3ca35139Johan HovoldMODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
9411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
942