pl2303.c 26 KB

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