pl2303.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Prolific PL2303 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2003 IBM Corp.
  6. *
  7. * Original driver for 2.2.x by anonymous
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * See Documentation/usb/usb-serial.txt for more information on using this
  14. * driver
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/serial.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include <asm/unaligned.h>
  30. #include "pl2303.h"
  31. #define PL2303_QUIRK_UART_STATE_IDX0 BIT(0)
  32. #define PL2303_QUIRK_LEGACY BIT(1)
  33. static const struct usb_device_id id_table[] = {
  34. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
  35. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
  36. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
  37. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
  38. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_CHILITAG) },
  39. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
  40. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
  41. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
  42. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
  43. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
  44. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
  45. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ZTEK) },
  46. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
  47. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
  48. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
  49. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_UC485) },
  50. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID2) },
  51. { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
  52. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
  53. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
  54. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
  55. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
  56. { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
  57. { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
  58. { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
  59. { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
  60. { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
  61. { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
  62. { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
  63. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1),
  64. .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
  65. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65),
  66. .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
  67. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75),
  68. .driver_info = PL2303_QUIRK_UART_STATE_IDX0 },
  69. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
  70. { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
  71. { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
  72. { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
  73. { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
  74. { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
  75. { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
  76. { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
  77. { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
  78. { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
  79. { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
  80. { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
  81. { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
  82. { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
  83. { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
  84. { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
  85. { USB_DEVICE(HP_VENDOR_ID, HP_LD960_PRODUCT_ID) },
  86. { USB_DEVICE(HP_VENDOR_ID, HP_LCM220_PRODUCT_ID) },
  87. { USB_DEVICE(HP_VENDOR_ID, HP_LCM960_PRODUCT_ID) },
  88. { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
  89. { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
  90. { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
  91. { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
  92. { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
  93. { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
  94. { } /* Terminating entry */
  95. };
  96. MODULE_DEVICE_TABLE(usb, id_table);
  97. #define SET_LINE_REQUEST_TYPE 0x21
  98. #define SET_LINE_REQUEST 0x20
  99. #define SET_CONTROL_REQUEST_TYPE 0x21
  100. #define SET_CONTROL_REQUEST 0x22
  101. #define CONTROL_DTR 0x01
  102. #define CONTROL_RTS 0x02
  103. #define BREAK_REQUEST_TYPE 0x21
  104. #define BREAK_REQUEST 0x23
  105. #define BREAK_ON 0xffff
  106. #define BREAK_OFF 0x0000
  107. #define GET_LINE_REQUEST_TYPE 0xa1
  108. #define GET_LINE_REQUEST 0x21
  109. #define VENDOR_WRITE_REQUEST_TYPE 0x40
  110. #define VENDOR_WRITE_REQUEST 0x01
  111. #define VENDOR_READ_REQUEST_TYPE 0xc0
  112. #define VENDOR_READ_REQUEST 0x01
  113. #define UART_STATE_INDEX 8
  114. #define UART_STATE_MSR_MASK 0x8b
  115. #define UART_STATE_TRANSIENT_MASK 0x74
  116. #define UART_DCD 0x01
  117. #define UART_DSR 0x02
  118. #define UART_BREAK_ERROR 0x04
  119. #define UART_RING 0x08
  120. #define UART_FRAME_ERROR 0x10
  121. #define UART_PARITY_ERROR 0x20
  122. #define UART_OVERRUN_ERROR 0x40
  123. #define UART_CTS 0x80
  124. static void pl2303_set_break(struct usb_serial_port *port, bool enable);
  125. enum pl2303_type {
  126. TYPE_01, /* Type 0 and 1 (difference unknown) */
  127. TYPE_HX, /* HX version of the pl2303 chip */
  128. TYPE_COUNT
  129. };
  130. struct pl2303_type_data {
  131. speed_t max_baud_rate;
  132. unsigned long quirks;
  133. };
  134. struct pl2303_serial_private {
  135. const struct pl2303_type_data *type;
  136. unsigned long quirks;
  137. };
  138. struct pl2303_private {
  139. spinlock_t lock;
  140. u8 line_control;
  141. u8 line_status;
  142. u8 line_settings[7];
  143. };
  144. static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
  145. [TYPE_01] = {
  146. .max_baud_rate = 1228800,
  147. .quirks = PL2303_QUIRK_LEGACY,
  148. },
  149. [TYPE_HX] = {
  150. .max_baud_rate = 12000000,
  151. },
  152. };
  153. static int pl2303_vendor_read(struct usb_serial *serial, u16 value,
  154. unsigned char buf[1])
  155. {
  156. struct device *dev = &serial->interface->dev;
  157. int res;
  158. res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  159. VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
  160. value, 0, buf, 1, 100);
  161. if (res != 1) {
  162. dev_err(dev, "%s - failed to read [%04x]: %d\n", __func__,
  163. value, res);
  164. if (res >= 0)
  165. res = -EIO;
  166. return res;
  167. }
  168. dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, buf[0]);
  169. return 0;
  170. }
  171. static int pl2303_vendor_write(struct usb_serial *serial, u16 value, u16 index)
  172. {
  173. struct device *dev = &serial->interface->dev;
  174. int res;
  175. dev_dbg(dev, "%s - [%04x] = %02x\n", __func__, value, index);
  176. res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  177. VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
  178. value, index, NULL, 0, 100);
  179. if (res) {
  180. dev_err(dev, "%s - failed to write [%04x]: %d\n", __func__,
  181. value, res);
  182. return res;
  183. }
  184. return 0;
  185. }
  186. static int pl2303_probe(struct usb_serial *serial,
  187. const struct usb_device_id *id)
  188. {
  189. usb_set_serial_data(serial, (void *)id->driver_info);
  190. return 0;
  191. }
  192. static int pl2303_startup(struct usb_serial *serial)
  193. {
  194. struct pl2303_serial_private *spriv;
  195. unsigned char num_ports = serial->num_ports;
  196. enum pl2303_type type = TYPE_01;
  197. unsigned char *buf;
  198. if (serial->num_bulk_in < num_ports ||
  199. serial->num_bulk_out < num_ports ||
  200. serial->num_interrupt_in < num_ports) {
  201. dev_err(&serial->interface->dev, "missing endpoints\n");
  202. return -ENODEV;
  203. }
  204. spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
  205. if (!spriv)
  206. return -ENOMEM;
  207. buf = kmalloc(1, GFP_KERNEL);
  208. if (!buf) {
  209. kfree(spriv);
  210. return -ENOMEM;
  211. }
  212. if (serial->dev->descriptor.bDeviceClass == 0x02)
  213. type = TYPE_01; /* type 0 */
  214. else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
  215. type = TYPE_HX;
  216. else if (serial->dev->descriptor.bDeviceClass == 0x00)
  217. type = TYPE_01; /* type 1 */
  218. else if (serial->dev->descriptor.bDeviceClass == 0xFF)
  219. type = TYPE_01; /* type 1 */
  220. dev_dbg(&serial->interface->dev, "device type: %d\n", type);
  221. spriv->type = &pl2303_type_data[type];
  222. spriv->quirks = (unsigned long)usb_get_serial_data(serial);
  223. spriv->quirks |= spriv->type->quirks;
  224. usb_set_serial_data(serial, spriv);
  225. pl2303_vendor_read(serial, 0x8484, buf);
  226. pl2303_vendor_write(serial, 0x0404, 0);
  227. pl2303_vendor_read(serial, 0x8484, buf);
  228. pl2303_vendor_read(serial, 0x8383, buf);
  229. pl2303_vendor_read(serial, 0x8484, buf);
  230. pl2303_vendor_write(serial, 0x0404, 1);
  231. pl2303_vendor_read(serial, 0x8484, buf);
  232. pl2303_vendor_read(serial, 0x8383, buf);
  233. pl2303_vendor_write(serial, 0, 1);
  234. pl2303_vendor_write(serial, 1, 0);
  235. if (spriv->quirks & PL2303_QUIRK_LEGACY)
  236. pl2303_vendor_write(serial, 2, 0x24);
  237. else
  238. pl2303_vendor_write(serial, 2, 0x44);
  239. kfree(buf);
  240. return 0;
  241. }
  242. static void pl2303_release(struct usb_serial *serial)
  243. {
  244. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  245. kfree(spriv);
  246. }
  247. static int pl2303_port_probe(struct usb_serial_port *port)
  248. {
  249. struct pl2303_private *priv;
  250. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  251. if (!priv)
  252. return -ENOMEM;
  253. spin_lock_init(&priv->lock);
  254. usb_set_serial_port_data(port, priv);
  255. port->port.drain_delay = 256;
  256. return 0;
  257. }
  258. static int pl2303_port_remove(struct usb_serial_port *port)
  259. {
  260. struct pl2303_private *priv = usb_get_serial_port_data(port);
  261. kfree(priv);
  262. return 0;
  263. }
  264. static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
  265. {
  266. struct usb_device *dev = port->serial->dev;
  267. int retval;
  268. dev_dbg(&port->dev, "%s - %02x\n", __func__, value);
  269. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  270. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  271. value, 0, NULL, 0, 100);
  272. if (retval)
  273. dev_err(&port->dev, "%s - failed: %d\n", __func__, retval);
  274. return retval;
  275. }
  276. /*
  277. * Returns the nearest supported baud rate that can be set directly without
  278. * using divisors.
  279. */
  280. static speed_t pl2303_get_supported_baud_rate(speed_t baud)
  281. {
  282. static const speed_t baud_sup[] = {
  283. 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
  284. 14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
  285. 614400, 921600, 1228800, 2457600, 3000000, 6000000
  286. };
  287. unsigned i;
  288. for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
  289. if (baud_sup[i] > baud)
  290. break;
  291. }
  292. if (i == ARRAY_SIZE(baud_sup))
  293. baud = baud_sup[i - 1];
  294. else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
  295. baud = baud_sup[i - 1];
  296. else
  297. baud = baud_sup[i];
  298. return baud;
  299. }
  300. /*
  301. * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
  302. * use 9600 baud.
  303. */
  304. static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
  305. speed_t baud)
  306. {
  307. put_unaligned_le32(baud, buf);
  308. return baud;
  309. }
  310. static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
  311. speed_t baud)
  312. {
  313. unsigned int baseline, mantissa, exponent;
  314. /*
  315. * Apparently the formula is:
  316. * baudrate = 12M * 32 / (mantissa * 4^exponent)
  317. * where
  318. * mantissa = buf[8:0]
  319. * exponent = buf[11:9]
  320. */
  321. baseline = 12000000 * 32;
  322. mantissa = baseline / baud;
  323. if (mantissa == 0)
  324. mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
  325. exponent = 0;
  326. while (mantissa >= 512) {
  327. if (exponent < 7) {
  328. mantissa >>= 2; /* divide by 4 */
  329. exponent++;
  330. } else {
  331. /* Exponent is maxed. Trim mantissa and leave. */
  332. mantissa = 511;
  333. break;
  334. }
  335. }
  336. buf[3] = 0x80;
  337. buf[2] = 0;
  338. buf[1] = exponent << 1 | mantissa >> 8;
  339. buf[0] = mantissa & 0xff;
  340. /* Calculate and return the exact baud rate. */
  341. baud = (baseline / mantissa) >> (exponent << 1);
  342. return baud;
  343. }
  344. static void pl2303_encode_baud_rate(struct tty_struct *tty,
  345. struct usb_serial_port *port,
  346. u8 buf[4])
  347. {
  348. struct usb_serial *serial = port->serial;
  349. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  350. speed_t baud_sup;
  351. speed_t baud;
  352. baud = tty_get_baud_rate(tty);
  353. dev_dbg(&port->dev, "baud requested = %u\n", baud);
  354. if (!baud)
  355. return;
  356. if (spriv->type->max_baud_rate)
  357. baud = min_t(speed_t, baud, spriv->type->max_baud_rate);
  358. /*
  359. * Use direct method for supported baud rates, otherwise use divisors.
  360. */
  361. baud_sup = pl2303_get_supported_baud_rate(baud);
  362. if (baud == baud_sup)
  363. baud = pl2303_encode_baud_rate_direct(buf, baud);
  364. else
  365. baud = pl2303_encode_baud_rate_divisor(buf, baud);
  366. /* Save resulting baud rate */
  367. tty_encode_baud_rate(tty, baud, baud);
  368. dev_dbg(&port->dev, "baud set = %u\n", baud);
  369. }
  370. static int pl2303_get_line_request(struct usb_serial_port *port,
  371. unsigned char buf[7])
  372. {
  373. struct usb_device *udev = port->serial->dev;
  374. int ret;
  375. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  376. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  377. 0, 0, buf, 7, 100);
  378. if (ret != 7) {
  379. dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
  380. if (ret > 0)
  381. ret = -EIO;
  382. return ret;
  383. }
  384. dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
  385. return 0;
  386. }
  387. static int pl2303_set_line_request(struct usb_serial_port *port,
  388. unsigned char buf[7])
  389. {
  390. struct usb_device *udev = port->serial->dev;
  391. int ret;
  392. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  393. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  394. 0, 0, buf, 7, 100);
  395. if (ret != 7) {
  396. dev_err(&port->dev, "%s - failed: %d\n", __func__, ret);
  397. if (ret > 0)
  398. ret = -EIO;
  399. return ret;
  400. }
  401. dev_dbg(&port->dev, "%s - %7ph\n", __func__, buf);
  402. return 0;
  403. }
  404. static void pl2303_set_termios(struct tty_struct *tty,
  405. struct usb_serial_port *port, struct ktermios *old_termios)
  406. {
  407. struct usb_serial *serial = port->serial;
  408. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  409. struct pl2303_private *priv = usb_get_serial_port_data(port);
  410. unsigned long flags;
  411. unsigned char *buf;
  412. int ret;
  413. u8 control;
  414. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  415. return;
  416. buf = kzalloc(7, GFP_KERNEL);
  417. if (!buf) {
  418. /* Report back no change occurred */
  419. if (old_termios)
  420. tty->termios = *old_termios;
  421. return;
  422. }
  423. pl2303_get_line_request(port, buf);
  424. switch (C_CSIZE(tty)) {
  425. case CS5:
  426. buf[6] = 5;
  427. break;
  428. case CS6:
  429. buf[6] = 6;
  430. break;
  431. case CS7:
  432. buf[6] = 7;
  433. break;
  434. default:
  435. case CS8:
  436. buf[6] = 8;
  437. }
  438. dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
  439. /* For reference buf[0]:buf[3] baud rate value */
  440. pl2303_encode_baud_rate(tty, port, &buf[0]);
  441. /* For reference buf[4]=0 is 1 stop bits */
  442. /* For reference buf[4]=1 is 1.5 stop bits */
  443. /* For reference buf[4]=2 is 2 stop bits */
  444. if (C_CSTOPB(tty)) {
  445. /*
  446. * NOTE: Comply with "real" UARTs / RS232:
  447. * use 1.5 instead of 2 stop bits with 5 data bits
  448. */
  449. if (C_CSIZE(tty) == CS5) {
  450. buf[4] = 1;
  451. dev_dbg(&port->dev, "stop bits = 1.5\n");
  452. } else {
  453. buf[4] = 2;
  454. dev_dbg(&port->dev, "stop bits = 2\n");
  455. }
  456. } else {
  457. buf[4] = 0;
  458. dev_dbg(&port->dev, "stop bits = 1\n");
  459. }
  460. if (C_PARENB(tty)) {
  461. /* For reference buf[5]=0 is none parity */
  462. /* For reference buf[5]=1 is odd parity */
  463. /* For reference buf[5]=2 is even parity */
  464. /* For reference buf[5]=3 is mark parity */
  465. /* For reference buf[5]=4 is space parity */
  466. if (C_PARODD(tty)) {
  467. if (C_CMSPAR(tty)) {
  468. buf[5] = 3;
  469. dev_dbg(&port->dev, "parity = mark\n");
  470. } else {
  471. buf[5] = 1;
  472. dev_dbg(&port->dev, "parity = odd\n");
  473. }
  474. } else {
  475. if (C_CMSPAR(tty)) {
  476. buf[5] = 4;
  477. dev_dbg(&port->dev, "parity = space\n");
  478. } else {
  479. buf[5] = 2;
  480. dev_dbg(&port->dev, "parity = even\n");
  481. }
  482. }
  483. } else {
  484. buf[5] = 0;
  485. dev_dbg(&port->dev, "parity = none\n");
  486. }
  487. /*
  488. * Some PL2303 are known to lose bytes if you change serial settings
  489. * even to the same values as before. Thus we actually need to filter
  490. * in this specific case.
  491. *
  492. * Note that the tty_termios_hw_change check above is not sufficient
  493. * as a previously requested baud rate may differ from the one
  494. * actually used (and stored in old_termios).
  495. *
  496. * NOTE: No additional locking needed for line_settings as it is
  497. * only used in set_termios, which is serialised against itself.
  498. */
  499. if (!old_termios || memcmp(buf, priv->line_settings, 7)) {
  500. ret = pl2303_set_line_request(port, buf);
  501. if (!ret)
  502. memcpy(priv->line_settings, buf, 7);
  503. }
  504. /* change control lines if we are switching to or from B0 */
  505. spin_lock_irqsave(&priv->lock, flags);
  506. control = priv->line_control;
  507. if (C_BAUD(tty) == B0)
  508. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  509. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  510. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  511. if (control != priv->line_control) {
  512. control = priv->line_control;
  513. spin_unlock_irqrestore(&priv->lock, flags);
  514. pl2303_set_control_lines(port, control);
  515. } else {
  516. spin_unlock_irqrestore(&priv->lock, flags);
  517. }
  518. if (C_CRTSCTS(tty)) {
  519. if (spriv->quirks & PL2303_QUIRK_LEGACY)
  520. pl2303_vendor_write(serial, 0x0, 0x41);
  521. else
  522. pl2303_vendor_write(serial, 0x0, 0x61);
  523. } else {
  524. pl2303_vendor_write(serial, 0x0, 0x0);
  525. }
  526. kfree(buf);
  527. }
  528. static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
  529. {
  530. struct pl2303_private *priv = usb_get_serial_port_data(port);
  531. unsigned long flags;
  532. u8 control;
  533. spin_lock_irqsave(&priv->lock, flags);
  534. if (on)
  535. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  536. else
  537. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  538. control = priv->line_control;
  539. spin_unlock_irqrestore(&priv->lock, flags);
  540. pl2303_set_control_lines(port, control);
  541. }
  542. static void pl2303_close(struct usb_serial_port *port)
  543. {
  544. usb_serial_generic_close(port);
  545. usb_kill_urb(port->interrupt_in_urb);
  546. pl2303_set_break(port, false);
  547. }
  548. static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
  549. {
  550. struct usb_serial *serial = port->serial;
  551. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  552. int result;
  553. if (spriv->quirks & PL2303_QUIRK_LEGACY) {
  554. usb_clear_halt(serial->dev, port->write_urb->pipe);
  555. usb_clear_halt(serial->dev, port->read_urb->pipe);
  556. } else {
  557. /* reset upstream data pipes */
  558. pl2303_vendor_write(serial, 8, 0);
  559. pl2303_vendor_write(serial, 9, 0);
  560. }
  561. /* Setup termios */
  562. if (tty)
  563. pl2303_set_termios(tty, port, NULL);
  564. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  565. if (result) {
  566. dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
  567. result);
  568. return result;
  569. }
  570. result = usb_serial_generic_open(tty, port);
  571. if (result) {
  572. usb_kill_urb(port->interrupt_in_urb);
  573. return result;
  574. }
  575. return 0;
  576. }
  577. static int pl2303_tiocmset(struct tty_struct *tty,
  578. unsigned int set, unsigned int clear)
  579. {
  580. struct usb_serial_port *port = tty->driver_data;
  581. struct pl2303_private *priv = usb_get_serial_port_data(port);
  582. unsigned long flags;
  583. u8 control;
  584. int ret;
  585. spin_lock_irqsave(&priv->lock, flags);
  586. if (set & TIOCM_RTS)
  587. priv->line_control |= CONTROL_RTS;
  588. if (set & TIOCM_DTR)
  589. priv->line_control |= CONTROL_DTR;
  590. if (clear & TIOCM_RTS)
  591. priv->line_control &= ~CONTROL_RTS;
  592. if (clear & TIOCM_DTR)
  593. priv->line_control &= ~CONTROL_DTR;
  594. control = priv->line_control;
  595. spin_unlock_irqrestore(&priv->lock, flags);
  596. ret = pl2303_set_control_lines(port, control);
  597. if (ret)
  598. return usb_translate_errors(ret);
  599. return 0;
  600. }
  601. static int pl2303_tiocmget(struct tty_struct *tty)
  602. {
  603. struct usb_serial_port *port = tty->driver_data;
  604. struct pl2303_private *priv = usb_get_serial_port_data(port);
  605. unsigned long flags;
  606. unsigned int mcr;
  607. unsigned int status;
  608. unsigned int result;
  609. spin_lock_irqsave(&priv->lock, flags);
  610. mcr = priv->line_control;
  611. status = priv->line_status;
  612. spin_unlock_irqrestore(&priv->lock, flags);
  613. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  614. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  615. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  616. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  617. | ((status & UART_RING) ? TIOCM_RI : 0)
  618. | ((status & UART_DCD) ? TIOCM_CD : 0);
  619. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  620. return result;
  621. }
  622. static int pl2303_carrier_raised(struct usb_serial_port *port)
  623. {
  624. struct pl2303_private *priv = usb_get_serial_port_data(port);
  625. if (priv->line_status & UART_DCD)
  626. return 1;
  627. return 0;
  628. }
  629. static int pl2303_ioctl(struct tty_struct *tty,
  630. unsigned int cmd, unsigned long arg)
  631. {
  632. struct serial_struct ser;
  633. struct usb_serial_port *port = tty->driver_data;
  634. switch (cmd) {
  635. case TIOCGSERIAL:
  636. memset(&ser, 0, sizeof ser);
  637. ser.type = PORT_16654;
  638. ser.line = port->minor;
  639. ser.port = port->port_number;
  640. ser.baud_base = 460800;
  641. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  642. return -EFAULT;
  643. return 0;
  644. default:
  645. break;
  646. }
  647. return -ENOIOCTLCMD;
  648. }
  649. static void pl2303_set_break(struct usb_serial_port *port, bool enable)
  650. {
  651. struct usb_serial *serial = port->serial;
  652. u16 state;
  653. int result;
  654. if (enable)
  655. state = BREAK_ON;
  656. else
  657. state = BREAK_OFF;
  658. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  659. state == BREAK_OFF ? "off" : "on");
  660. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  661. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  662. 0, NULL, 0, 100);
  663. if (result)
  664. dev_err(&port->dev, "error sending break = %d\n", result);
  665. }
  666. static void pl2303_break_ctl(struct tty_struct *tty, int state)
  667. {
  668. struct usb_serial_port *port = tty->driver_data;
  669. pl2303_set_break(port, state);
  670. }
  671. static void pl2303_update_line_status(struct usb_serial_port *port,
  672. unsigned char *data,
  673. unsigned int actual_length)
  674. {
  675. struct usb_serial *serial = port->serial;
  676. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  677. struct pl2303_private *priv = usb_get_serial_port_data(port);
  678. struct tty_struct *tty;
  679. unsigned long flags;
  680. unsigned int status_idx = UART_STATE_INDEX;
  681. u8 status;
  682. u8 delta;
  683. if (spriv->quirks & PL2303_QUIRK_UART_STATE_IDX0)
  684. status_idx = 0;
  685. if (actual_length < status_idx + 1)
  686. return;
  687. status = data[status_idx];
  688. /* Save off the uart status for others to look at */
  689. spin_lock_irqsave(&priv->lock, flags);
  690. delta = priv->line_status ^ status;
  691. priv->line_status = status;
  692. spin_unlock_irqrestore(&priv->lock, flags);
  693. if (status & UART_BREAK_ERROR)
  694. usb_serial_handle_break(port);
  695. if (delta & UART_STATE_MSR_MASK) {
  696. if (delta & UART_CTS)
  697. port->icount.cts++;
  698. if (delta & UART_DSR)
  699. port->icount.dsr++;
  700. if (delta & UART_RING)
  701. port->icount.rng++;
  702. if (delta & UART_DCD) {
  703. port->icount.dcd++;
  704. tty = tty_port_tty_get(&port->port);
  705. if (tty) {
  706. usb_serial_handle_dcd_change(port, tty,
  707. status & UART_DCD);
  708. tty_kref_put(tty);
  709. }
  710. }
  711. wake_up_interruptible(&port->port.delta_msr_wait);
  712. }
  713. }
  714. static void pl2303_read_int_callback(struct urb *urb)
  715. {
  716. struct usb_serial_port *port = urb->context;
  717. unsigned char *data = urb->transfer_buffer;
  718. unsigned int actual_length = urb->actual_length;
  719. int status = urb->status;
  720. int retval;
  721. switch (status) {
  722. case 0:
  723. /* success */
  724. break;
  725. case -ECONNRESET:
  726. case -ENOENT:
  727. case -ESHUTDOWN:
  728. /* this urb is terminated, clean up */
  729. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  730. __func__, status);
  731. return;
  732. default:
  733. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  734. __func__, status);
  735. goto exit;
  736. }
  737. usb_serial_debug_data(&port->dev, __func__,
  738. urb->actual_length, urb->transfer_buffer);
  739. pl2303_update_line_status(port, data, actual_length);
  740. exit:
  741. retval = usb_submit_urb(urb, GFP_ATOMIC);
  742. if (retval) {
  743. dev_err(&port->dev,
  744. "%s - usb_submit_urb failed with result %d\n",
  745. __func__, retval);
  746. }
  747. }
  748. static void pl2303_process_read_urb(struct urb *urb)
  749. {
  750. struct usb_serial_port *port = urb->context;
  751. struct pl2303_private *priv = usb_get_serial_port_data(port);
  752. unsigned char *data = urb->transfer_buffer;
  753. char tty_flag = TTY_NORMAL;
  754. unsigned long flags;
  755. u8 line_status;
  756. int i;
  757. /* update line status */
  758. spin_lock_irqsave(&priv->lock, flags);
  759. line_status = priv->line_status;
  760. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  761. spin_unlock_irqrestore(&priv->lock, flags);
  762. if (!urb->actual_length)
  763. return;
  764. /*
  765. * Break takes precedence over parity, which takes precedence over
  766. * framing errors.
  767. */
  768. if (line_status & UART_BREAK_ERROR)
  769. tty_flag = TTY_BREAK;
  770. else if (line_status & UART_PARITY_ERROR)
  771. tty_flag = TTY_PARITY;
  772. else if (line_status & UART_FRAME_ERROR)
  773. tty_flag = TTY_FRAME;
  774. if (tty_flag != TTY_NORMAL)
  775. dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
  776. tty_flag);
  777. /* overrun is special, not associated with a char */
  778. if (line_status & UART_OVERRUN_ERROR)
  779. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  780. if (port->port.console && port->sysrq) {
  781. for (i = 0; i < urb->actual_length; ++i)
  782. if (!usb_serial_handle_sysrq_char(port, data[i]))
  783. tty_insert_flip_char(&port->port, data[i],
  784. tty_flag);
  785. } else {
  786. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  787. urb->actual_length);
  788. }
  789. tty_flip_buffer_push(&port->port);
  790. }
  791. static struct usb_serial_driver pl2303_device = {
  792. .driver = {
  793. .owner = THIS_MODULE,
  794. .name = "pl2303",
  795. },
  796. .id_table = id_table,
  797. .num_ports = 1,
  798. .bulk_in_size = 256,
  799. .bulk_out_size = 256,
  800. .open = pl2303_open,
  801. .close = pl2303_close,
  802. .dtr_rts = pl2303_dtr_rts,
  803. .carrier_raised = pl2303_carrier_raised,
  804. .ioctl = pl2303_ioctl,
  805. .break_ctl = pl2303_break_ctl,
  806. .set_termios = pl2303_set_termios,
  807. .tiocmget = pl2303_tiocmget,
  808. .tiocmset = pl2303_tiocmset,
  809. .tiocmiwait = usb_serial_generic_tiocmiwait,
  810. .process_read_urb = pl2303_process_read_urb,
  811. .read_int_callback = pl2303_read_int_callback,
  812. .probe = pl2303_probe,
  813. .attach = pl2303_startup,
  814. .release = pl2303_release,
  815. .port_probe = pl2303_port_probe,
  816. .port_remove = pl2303_port_remove,
  817. };
  818. static struct usb_serial_driver * const serial_drivers[] = {
  819. &pl2303_device, NULL
  820. };
  821. module_usb_serial_driver(serial_drivers, id_table);
  822. MODULE_DESCRIPTION("Prolific PL2303 USB to serial adaptor driver");
  823. MODULE_LICENSE("GPL");