pl2303.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. * Copyright (C) 2009, 2013 Frank Schäfer <fschaefer.oss@googlemail.com>
  8. * - fixes, improvements and documentation for the baud rate encoding methods
  9. * Copyright (C) 2013 Reinhard Max <max@suse.de>
  10. * - fixes and improvements for the divisor based baud rate encoding method
  11. *
  12. * Original driver for 2.2.x by anonymous
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License version
  16. * 2 as published by the Free Software Foundation.
  17. *
  18. * See Documentation/usb/usb-serial.txt for more information on using this
  19. * driver
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/tty.h>
  27. #include <linux/tty_driver.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/serial.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <asm/unaligned.h>
  37. #include "pl2303.h"
  38. /*
  39. * Version Information
  40. */
  41. #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
  42. static const struct usb_device_id id_table[] = {
  43. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
  44. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
  45. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
  46. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
  47. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
  48. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
  49. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
  50. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
  51. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
  52. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
  53. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
  54. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
  55. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
  56. { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
  57. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
  58. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
  59. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
  60. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
  61. { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
  62. { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
  63. { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
  64. { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
  65. { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
  66. { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
  67. { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
  68. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
  69. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
  70. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
  71. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
  72. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
  73. { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
  74. { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
  75. { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
  76. { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
  77. { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
  78. { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
  79. { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
  80. { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
  81. { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
  82. { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
  83. { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
  84. { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
  85. { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
  86. { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
  87. { USB_DEVICE(HP_VENDOR_ID, HP_LD220_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 0x08
  114. #define UART_STATE_TRANSIENT_MASK 0x74
  115. #define UART_DCD 0x01
  116. #define UART_DSR 0x02
  117. #define UART_BREAK_ERROR 0x04
  118. #define UART_RING 0x08
  119. #define UART_FRAME_ERROR 0x10
  120. #define UART_PARITY_ERROR 0x20
  121. #define UART_OVERRUN_ERROR 0x40
  122. #define UART_CTS 0x80
  123. enum pl2303_type {
  124. type_0, /* don't know the difference between type 0 and */
  125. type_1, /* type 1, until someone from prolific tells us... */
  126. HX, /* HX version of the pl2303 chip */
  127. };
  128. struct pl2303_serial_private {
  129. enum pl2303_type type;
  130. };
  131. struct pl2303_private {
  132. spinlock_t lock;
  133. u8 line_control;
  134. u8 line_status;
  135. };
  136. static int pl2303_vendor_read(__u16 value, __u16 index,
  137. struct usb_serial *serial, unsigned char *buf)
  138. {
  139. int res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  140. VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
  141. value, index, buf, 1, 100);
  142. dev_dbg(&serial->interface->dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n",
  143. VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, value, index,
  144. res, buf[0]);
  145. return res;
  146. }
  147. static int pl2303_vendor_write(__u16 value, __u16 index,
  148. struct usb_serial *serial)
  149. {
  150. int res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  151. VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
  152. value, index, NULL, 0, 100);
  153. dev_dbg(&serial->interface->dev, "0x%x:0x%x:0x%x:0x%x %d\n",
  154. VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, value, index,
  155. res);
  156. return res;
  157. }
  158. static int pl2303_startup(struct usb_serial *serial)
  159. {
  160. struct pl2303_serial_private *spriv;
  161. enum pl2303_type type = type_0;
  162. unsigned char *buf;
  163. spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
  164. if (!spriv)
  165. return -ENOMEM;
  166. buf = kmalloc(10, GFP_KERNEL);
  167. if (!buf) {
  168. kfree(spriv);
  169. return -ENOMEM;
  170. }
  171. if (serial->dev->descriptor.bDeviceClass == 0x02)
  172. type = type_0;
  173. else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
  174. type = HX;
  175. else if (serial->dev->descriptor.bDeviceClass == 0x00
  176. || serial->dev->descriptor.bDeviceClass == 0xFF)
  177. type = type_1;
  178. dev_dbg(&serial->interface->dev, "device type: %d\n", type);
  179. spriv->type = type;
  180. usb_set_serial_data(serial, spriv);
  181. pl2303_vendor_read(0x8484, 0, serial, buf);
  182. pl2303_vendor_write(0x0404, 0, serial);
  183. pl2303_vendor_read(0x8484, 0, serial, buf);
  184. pl2303_vendor_read(0x8383, 0, serial, buf);
  185. pl2303_vendor_read(0x8484, 0, serial, buf);
  186. pl2303_vendor_write(0x0404, 1, serial);
  187. pl2303_vendor_read(0x8484, 0, serial, buf);
  188. pl2303_vendor_read(0x8383, 0, serial, buf);
  189. pl2303_vendor_write(0, 1, serial);
  190. pl2303_vendor_write(1, 0, serial);
  191. if (type == HX)
  192. pl2303_vendor_write(2, 0x44, serial);
  193. else
  194. pl2303_vendor_write(2, 0x24, serial);
  195. kfree(buf);
  196. return 0;
  197. }
  198. static void pl2303_release(struct usb_serial *serial)
  199. {
  200. struct pl2303_serial_private *spriv;
  201. spriv = usb_get_serial_data(serial);
  202. kfree(spriv);
  203. }
  204. static int pl2303_port_probe(struct usb_serial_port *port)
  205. {
  206. struct pl2303_private *priv;
  207. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  208. if (!priv)
  209. return -ENOMEM;
  210. spin_lock_init(&priv->lock);
  211. usb_set_serial_port_data(port, priv);
  212. port->port.drain_delay = 256;
  213. return 0;
  214. }
  215. static int pl2303_port_remove(struct usb_serial_port *port)
  216. {
  217. struct pl2303_private *priv;
  218. priv = usb_get_serial_port_data(port);
  219. kfree(priv);
  220. return 0;
  221. }
  222. static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
  223. {
  224. struct usb_device *dev = port->serial->dev;
  225. int retval;
  226. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  227. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  228. value, 0, NULL, 0, 100);
  229. dev_dbg(&port->dev, "%s - value = %d, retval = %d\n", __func__,
  230. value, retval);
  231. return retval;
  232. }
  233. static int pl2303_baudrate_encode_direct(int baud, enum pl2303_type type,
  234. u8 buf[4])
  235. {
  236. /*
  237. * NOTE: Only the values defined in baud_sup are supported !
  238. * => if unsupported values are set, the PL2303 seems to
  239. * use 9600 baud (at least my PL2303X always does)
  240. */
  241. const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
  242. 4800, 7200, 9600, 14400, 19200, 28800, 38400,
  243. 57600, 115200, 230400, 460800, 614400, 921600,
  244. 1228800, 2457600, 3000000, 6000000 };
  245. /*
  246. * NOTE: The PL2303HX (tested with rev. 3A) also supports the following
  247. * baud rates: 128000, 134400, 161280, 201600, 268800, 403200, 806400.
  248. * As long as we are not using this encoding method for them, there is
  249. * no point in complicating the code to support them.
  250. */
  251. int i;
  252. /* Set baudrate to nearest supported value */
  253. for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
  254. if (baud_sup[i] > baud)
  255. break;
  256. }
  257. if (i == ARRAY_SIZE(baud_sup))
  258. baud = baud_sup[i - 1];
  259. else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
  260. baud = baud_sup[i - 1];
  261. else
  262. baud = baud_sup[i];
  263. /* type_0, type_1 only support up to 1228800 baud */
  264. if (type != HX)
  265. baud = min_t(int, baud, 1228800);
  266. /* Direct (standard) baud rate encoding method */
  267. put_unaligned_le32(baud, buf);
  268. return baud;
  269. }
  270. static int pl2303_baudrate_encode_divisor(int baud, enum pl2303_type type,
  271. u8 buf[4])
  272. {
  273. /*
  274. * Divisor based baud rate encoding method
  275. *
  276. * NOTE: it's not clear if the type_0/1 chips support this method
  277. *
  278. * divisor = 12MHz * 32 / baudrate = 2^A * B
  279. *
  280. * with
  281. *
  282. * A = buf[1] & 0x0e
  283. * B = buf[0] + (buf[1] & 0x01) << 8
  284. *
  285. * Special cases:
  286. * => 8 < B < 16: device seems to work not properly
  287. * => B <= 8: device uses the max. value B = 512 instead
  288. */
  289. unsigned int A, B;
  290. /*
  291. * NOTE: The Windows driver allows maximum baud rates of 110% of the
  292. * specified maximium value.
  293. * Quick tests with early (2004) HX (rev. A) chips suggest, that even
  294. * higher baud rates (up to the maximum of 24M baud !) are working fine,
  295. * but that should really be tested carefully in "real life" scenarios
  296. * before removing the upper limit completely.
  297. * Baud rates smaller than the specified 75 baud are definitely working
  298. * fine.
  299. */
  300. if (type == HX)
  301. baud = min_t(int, baud, 6000000 * 1.1);
  302. else
  303. baud = min_t(int, baud, 1228800 * 1.1);
  304. /* Determine factors A and B */
  305. A = 0;
  306. B = 12000000 * 32 / baud; /* 12MHz */
  307. B <<= 1; /* Add one bit for rounding */
  308. while (B > (512 << 1) && A <= 14) {
  309. A += 2;
  310. B >>= 2;
  311. }
  312. if (A > 14) { /* max. divisor = min. baudrate reached */
  313. A = 14;
  314. B = 512;
  315. /* => ~45.78 baud */
  316. } else {
  317. B = (B + 1) >> 1; /* Round the last bit */
  318. }
  319. /* Handle special cases */
  320. if (B == 512)
  321. B = 0; /* also: 1 to 8 */
  322. else if (B < 16)
  323. /*
  324. * NOTE: With the current algorithm this happens
  325. * only for A=0 and means that the min. divisor
  326. * (respectively: the max. baudrate) is reached.
  327. */
  328. B = 16; /* => 24 MBaud */
  329. /* Encode the baud rate */
  330. buf[3] = 0x80; /* Select divisor encoding method */
  331. buf[2] = 0;
  332. buf[1] = (A & 0x0e); /* A */
  333. buf[1] |= ((B & 0x100) >> 8); /* MSB of B */
  334. buf[0] = B & 0xff; /* 8 LSBs of B */
  335. /* Calculate the actual/resulting baud rate */
  336. if (B <= 8)
  337. B = 512;
  338. baud = 12000000 * 32 / ((1 << A) * B);
  339. return baud;
  340. }
  341. static void pl2303_encode_baudrate(struct tty_struct *tty,
  342. struct usb_serial_port *port,
  343. enum pl2303_type type,
  344. u8 buf[4])
  345. {
  346. int baud;
  347. baud = tty_get_baud_rate(tty);
  348. dev_dbg(&port->dev, "baud requested = %d\n", baud);
  349. if (!baud)
  350. return;
  351. /*
  352. * There are two methods for setting/encoding the baud rate
  353. * 1) Direct method: encodes the baud rate value directly
  354. * => supported by all chip types
  355. * 2) Divisor based method: encodes a divisor to a base value (12MHz*32)
  356. * => supported by HX chips (and likely not by type_0/1 chips)
  357. *
  358. * NOTE: Although the divisor based baud rate encoding method is much
  359. * more flexible, some of the standard baud rate values can not be
  360. * realized exactly. But the difference is very small (max. 0.2%) and
  361. * the device likely uses the same baud rate generator for both methods
  362. * so that there is likley no difference.
  363. */
  364. if (type != HX)
  365. baud = pl2303_baudrate_encode_direct(baud, type, buf);
  366. else
  367. baud = pl2303_baudrate_encode_divisor(baud, type, buf);
  368. /* Save resulting baud rate */
  369. tty_encode_baud_rate(tty, baud, baud);
  370. dev_dbg(&port->dev, "baud set = %d\n", baud);
  371. }
  372. static void pl2303_set_termios(struct tty_struct *tty,
  373. struct usb_serial_port *port, struct ktermios *old_termios)
  374. {
  375. struct usb_serial *serial = port->serial;
  376. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  377. struct pl2303_private *priv = usb_get_serial_port_data(port);
  378. unsigned long flags;
  379. unsigned char *buf;
  380. int i;
  381. u8 control;
  382. /*
  383. * The PL2303 is reported to lose bytes if you change serial settings
  384. * even to the same values as before. Thus we actually need to filter
  385. * in this specific case.
  386. */
  387. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  388. return;
  389. buf = kzalloc(7, GFP_KERNEL);
  390. if (!buf) {
  391. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  392. /* Report back no change occurred */
  393. if (old_termios)
  394. tty->termios = *old_termios;
  395. return;
  396. }
  397. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  398. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  399. 0, 0, buf, 7, 100);
  400. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %7ph\n", i, buf);
  401. if (C_CSIZE(tty)) {
  402. switch (C_CSIZE(tty)) {
  403. case CS5:
  404. buf[6] = 5;
  405. break;
  406. case CS6:
  407. buf[6] = 6;
  408. break;
  409. case CS7:
  410. buf[6] = 7;
  411. break;
  412. default:
  413. case CS8:
  414. buf[6] = 8;
  415. }
  416. dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
  417. }
  418. /* For reference: buf[0]:buf[3] baud rate value */
  419. pl2303_encode_baudrate(tty, port, spriv->type, buf);
  420. /* For reference buf[4]=0 is 1 stop bits */
  421. /* For reference buf[4]=1 is 1.5 stop bits */
  422. /* For reference buf[4]=2 is 2 stop bits */
  423. if (C_CSTOPB(tty)) {
  424. /*
  425. * NOTE: Comply with "real" UARTs / RS232:
  426. * use 1.5 instead of 2 stop bits with 5 data bits
  427. */
  428. if (C_CSIZE(tty) == CS5) {
  429. buf[4] = 1;
  430. dev_dbg(&port->dev, "stop bits = 1.5\n");
  431. } else {
  432. buf[4] = 2;
  433. dev_dbg(&port->dev, "stop bits = 2\n");
  434. }
  435. } else {
  436. buf[4] = 0;
  437. dev_dbg(&port->dev, "stop bits = 1\n");
  438. }
  439. if (C_PARENB(tty)) {
  440. /* For reference buf[5]=0 is none parity */
  441. /* For reference buf[5]=1 is odd parity */
  442. /* For reference buf[5]=2 is even parity */
  443. /* For reference buf[5]=3 is mark parity */
  444. /* For reference buf[5]=4 is space parity */
  445. if (C_PARODD(tty)) {
  446. if (tty->termios.c_cflag & CMSPAR) {
  447. buf[5] = 3;
  448. dev_dbg(&port->dev, "parity = mark\n");
  449. } else {
  450. buf[5] = 1;
  451. dev_dbg(&port->dev, "parity = odd\n");
  452. }
  453. } else {
  454. if (tty->termios.c_cflag & CMSPAR) {
  455. buf[5] = 4;
  456. dev_dbg(&port->dev, "parity = space\n");
  457. } else {
  458. buf[5] = 2;
  459. dev_dbg(&port->dev, "parity = even\n");
  460. }
  461. }
  462. } else {
  463. buf[5] = 0;
  464. dev_dbg(&port->dev, "parity = none\n");
  465. }
  466. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  467. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  468. 0, 0, buf, 7, 100);
  469. dev_dbg(&port->dev, "0x21:0x20:0:0 %d\n", i);
  470. /* change control lines if we are switching to or from B0 */
  471. spin_lock_irqsave(&priv->lock, flags);
  472. control = priv->line_control;
  473. if (C_BAUD(tty) == B0)
  474. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  475. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  476. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  477. if (control != priv->line_control) {
  478. control = priv->line_control;
  479. spin_unlock_irqrestore(&priv->lock, flags);
  480. pl2303_set_control_lines(port, control);
  481. } else {
  482. spin_unlock_irqrestore(&priv->lock, flags);
  483. }
  484. memset(buf, 0, 7);
  485. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  486. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  487. 0, 0, buf, 7, 100);
  488. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %7ph\n", i, buf);
  489. if (C_CRTSCTS(tty)) {
  490. if (spriv->type == HX)
  491. pl2303_vendor_write(0x0, 0x61, serial);
  492. else
  493. pl2303_vendor_write(0x0, 0x41, serial);
  494. } else {
  495. pl2303_vendor_write(0x0, 0x0, serial);
  496. }
  497. kfree(buf);
  498. }
  499. static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
  500. {
  501. struct pl2303_private *priv = usb_get_serial_port_data(port);
  502. unsigned long flags;
  503. u8 control;
  504. spin_lock_irqsave(&priv->lock, flags);
  505. /* Change DTR and RTS */
  506. if (on)
  507. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  508. else
  509. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  510. control = priv->line_control;
  511. spin_unlock_irqrestore(&priv->lock, flags);
  512. pl2303_set_control_lines(port, control);
  513. }
  514. static void pl2303_close(struct usb_serial_port *port)
  515. {
  516. usb_serial_generic_close(port);
  517. usb_kill_urb(port->interrupt_in_urb);
  518. }
  519. static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
  520. {
  521. struct usb_serial *serial = port->serial;
  522. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  523. int result;
  524. if (spriv->type != HX) {
  525. usb_clear_halt(serial->dev, port->write_urb->pipe);
  526. usb_clear_halt(serial->dev, port->read_urb->pipe);
  527. } else {
  528. /* reset upstream data pipes */
  529. pl2303_vendor_write(8, 0, serial);
  530. pl2303_vendor_write(9, 0, serial);
  531. }
  532. /* Setup termios */
  533. if (tty)
  534. pl2303_set_termios(tty, port, NULL);
  535. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  536. if (result) {
  537. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  538. " error %d\n", __func__, result);
  539. return result;
  540. }
  541. result = usb_serial_generic_open(tty, port);
  542. if (result) {
  543. usb_kill_urb(port->interrupt_in_urb);
  544. return result;
  545. }
  546. return 0;
  547. }
  548. static int pl2303_tiocmset(struct tty_struct *tty,
  549. unsigned int set, unsigned int clear)
  550. {
  551. struct usb_serial_port *port = tty->driver_data;
  552. struct pl2303_private *priv = usb_get_serial_port_data(port);
  553. unsigned long flags;
  554. u8 control;
  555. int ret;
  556. spin_lock_irqsave(&priv->lock, flags);
  557. if (set & TIOCM_RTS)
  558. priv->line_control |= CONTROL_RTS;
  559. if (set & TIOCM_DTR)
  560. priv->line_control |= CONTROL_DTR;
  561. if (clear & TIOCM_RTS)
  562. priv->line_control &= ~CONTROL_RTS;
  563. if (clear & TIOCM_DTR)
  564. priv->line_control &= ~CONTROL_DTR;
  565. control = priv->line_control;
  566. spin_unlock_irqrestore(&priv->lock, flags);
  567. ret = pl2303_set_control_lines(port, control);
  568. if (ret)
  569. return usb_translate_errors(ret);
  570. return 0;
  571. }
  572. static int pl2303_tiocmget(struct tty_struct *tty)
  573. {
  574. struct usb_serial_port *port = tty->driver_data;
  575. struct pl2303_private *priv = usb_get_serial_port_data(port);
  576. unsigned long flags;
  577. unsigned int mcr;
  578. unsigned int status;
  579. unsigned int result;
  580. spin_lock_irqsave(&priv->lock, flags);
  581. mcr = priv->line_control;
  582. status = priv->line_status;
  583. spin_unlock_irqrestore(&priv->lock, flags);
  584. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  585. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  586. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  587. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  588. | ((status & UART_RING) ? TIOCM_RI : 0)
  589. | ((status & UART_DCD) ? TIOCM_CD : 0);
  590. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  591. return result;
  592. }
  593. static int pl2303_carrier_raised(struct usb_serial_port *port)
  594. {
  595. struct pl2303_private *priv = usb_get_serial_port_data(port);
  596. if (priv->line_status & UART_DCD)
  597. return 1;
  598. return 0;
  599. }
  600. static int pl2303_tiocmiwait(struct tty_struct *tty, unsigned long arg)
  601. {
  602. struct usb_serial_port *port = tty->driver_data;
  603. struct pl2303_private *priv = usb_get_serial_port_data(port);
  604. unsigned long flags;
  605. unsigned int prevstatus;
  606. unsigned int status;
  607. unsigned int changed;
  608. spin_lock_irqsave(&priv->lock, flags);
  609. prevstatus = priv->line_status;
  610. spin_unlock_irqrestore(&priv->lock, flags);
  611. while (1) {
  612. interruptible_sleep_on(&port->port.delta_msr_wait);
  613. /* see if a signal did it */
  614. if (signal_pending(current))
  615. return -ERESTARTSYS;
  616. if (port->serial->disconnected)
  617. return -EIO;
  618. spin_lock_irqsave(&priv->lock, flags);
  619. status = priv->line_status;
  620. spin_unlock_irqrestore(&priv->lock, flags);
  621. changed = prevstatus ^ status;
  622. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  623. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  624. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  625. ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
  626. return 0;
  627. }
  628. prevstatus = status;
  629. }
  630. /* NOTREACHED */
  631. return 0;
  632. }
  633. static int pl2303_ioctl(struct tty_struct *tty,
  634. unsigned int cmd, unsigned long arg)
  635. {
  636. struct serial_struct ser;
  637. struct usb_serial_port *port = tty->driver_data;
  638. dev_dbg(&port->dev, "%s cmd = 0x%04x\n", __func__, cmd);
  639. switch (cmd) {
  640. case TIOCGSERIAL:
  641. memset(&ser, 0, sizeof ser);
  642. ser.type = PORT_16654;
  643. ser.line = port->minor;
  644. ser.port = port->port_number;
  645. ser.baud_base = 460800;
  646. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  647. return -EFAULT;
  648. return 0;
  649. default:
  650. dev_dbg(&port->dev, "%s not supported = 0x%04x\n", __func__, cmd);
  651. break;
  652. }
  653. return -ENOIOCTLCMD;
  654. }
  655. static void pl2303_break_ctl(struct tty_struct *tty, int break_state)
  656. {
  657. struct usb_serial_port *port = tty->driver_data;
  658. struct usb_serial *serial = port->serial;
  659. u16 state;
  660. int result;
  661. if (break_state == 0)
  662. state = BREAK_OFF;
  663. else
  664. state = BREAK_ON;
  665. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  666. state == BREAK_OFF ? "off" : "on");
  667. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  668. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  669. 0, NULL, 0, 100);
  670. if (result)
  671. dev_err(&port->dev, "error sending break = %d\n", result);
  672. }
  673. static void pl2303_update_line_status(struct usb_serial_port *port,
  674. unsigned char *data,
  675. unsigned int actual_length)
  676. {
  677. struct pl2303_private *priv = usb_get_serial_port_data(port);
  678. struct tty_struct *tty;
  679. unsigned long flags;
  680. u8 status_idx = UART_STATE;
  681. u8 length = UART_STATE + 1;
  682. u8 prev_line_status;
  683. u16 idv, idp;
  684. idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
  685. idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
  686. if (idv == SIEMENS_VENDOR_ID) {
  687. if (idp == SIEMENS_PRODUCT_ID_X65 ||
  688. idp == SIEMENS_PRODUCT_ID_SX1 ||
  689. idp == SIEMENS_PRODUCT_ID_X75) {
  690. length = 1;
  691. status_idx = 0;
  692. }
  693. }
  694. if (actual_length < length)
  695. return;
  696. /* Save off the uart status for others to look at */
  697. spin_lock_irqsave(&priv->lock, flags);
  698. prev_line_status = priv->line_status;
  699. priv->line_status = data[status_idx];
  700. spin_unlock_irqrestore(&priv->lock, flags);
  701. if (priv->line_status & UART_BREAK_ERROR)
  702. usb_serial_handle_break(port);
  703. wake_up_interruptible(&port->port.delta_msr_wait);
  704. tty = tty_port_tty_get(&port->port);
  705. if (!tty)
  706. return;
  707. if ((priv->line_status ^ prev_line_status) & UART_DCD)
  708. usb_serial_handle_dcd_change(port, tty,
  709. priv->line_status & UART_DCD);
  710. tty_kref_put(tty);
  711. }
  712. static void pl2303_read_int_callback(struct urb *urb)
  713. {
  714. struct usb_serial_port *port = urb->context;
  715. unsigned char *data = urb->transfer_buffer;
  716. unsigned int actual_length = urb->actual_length;
  717. int status = urb->status;
  718. int retval;
  719. switch (status) {
  720. case 0:
  721. /* success */
  722. break;
  723. case -ECONNRESET:
  724. case -ENOENT:
  725. case -ESHUTDOWN:
  726. /* this urb is terminated, clean up */
  727. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  728. __func__, status);
  729. return;
  730. default:
  731. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  732. __func__, status);
  733. goto exit;
  734. }
  735. usb_serial_debug_data(&port->dev, __func__,
  736. urb->actual_length, urb->transfer_buffer);
  737. pl2303_update_line_status(port, data, actual_length);
  738. exit:
  739. retval = usb_submit_urb(urb, GFP_ATOMIC);
  740. if (retval)
  741. dev_err(&port->dev,
  742. "%s - usb_submit_urb failed with result %d\n",
  743. __func__, retval);
  744. }
  745. static void pl2303_process_read_urb(struct urb *urb)
  746. {
  747. struct usb_serial_port *port = urb->context;
  748. struct pl2303_private *priv = usb_get_serial_port_data(port);
  749. unsigned char *data = urb->transfer_buffer;
  750. char tty_flag = TTY_NORMAL;
  751. unsigned long flags;
  752. u8 line_status;
  753. int i;
  754. /* update line status */
  755. spin_lock_irqsave(&priv->lock, flags);
  756. line_status = priv->line_status;
  757. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  758. spin_unlock_irqrestore(&priv->lock, flags);
  759. wake_up_interruptible(&port->port.delta_msr_wait);
  760. if (!urb->actual_length)
  761. return;
  762. /* break takes precedence over parity, */
  763. /* which takes precedence over framing errors */
  764. if (line_status & UART_BREAK_ERROR)
  765. tty_flag = TTY_BREAK;
  766. else if (line_status & UART_PARITY_ERROR)
  767. tty_flag = TTY_PARITY;
  768. else if (line_status & UART_FRAME_ERROR)
  769. tty_flag = TTY_FRAME;
  770. if (tty_flag != TTY_NORMAL)
  771. dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
  772. tty_flag);
  773. /* overrun is special, not associated with a char */
  774. if (line_status & UART_OVERRUN_ERROR)
  775. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  776. if (port->port.console && port->sysrq) {
  777. for (i = 0; i < urb->actual_length; ++i)
  778. if (!usb_serial_handle_sysrq_char(port, data[i]))
  779. tty_insert_flip_char(&port->port, data[i],
  780. tty_flag);
  781. } else {
  782. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  783. urb->actual_length);
  784. }
  785. tty_flip_buffer_push(&port->port);
  786. }
  787. /* All of the device info needed for the PL2303 SIO serial converter */
  788. static struct usb_serial_driver pl2303_device = {
  789. .driver = {
  790. .owner = THIS_MODULE,
  791. .name = "pl2303",
  792. },
  793. .id_table = id_table,
  794. .num_ports = 1,
  795. .bulk_in_size = 256,
  796. .bulk_out_size = 256,
  797. .open = pl2303_open,
  798. .close = pl2303_close,
  799. .dtr_rts = pl2303_dtr_rts,
  800. .carrier_raised = pl2303_carrier_raised,
  801. .ioctl = pl2303_ioctl,
  802. .break_ctl = pl2303_break_ctl,
  803. .set_termios = pl2303_set_termios,
  804. .tiocmget = pl2303_tiocmget,
  805. .tiocmset = pl2303_tiocmset,
  806. .tiocmiwait = pl2303_tiocmiwait,
  807. .process_read_urb = pl2303_process_read_urb,
  808. .read_int_callback = pl2303_read_int_callback,
  809. .attach = pl2303_startup,
  810. .release = pl2303_release,
  811. .port_probe = pl2303_port_probe,
  812. .port_remove = pl2303_port_remove,
  813. };
  814. static struct usb_serial_driver * const serial_drivers[] = {
  815. &pl2303_device, NULL
  816. };
  817. module_usb_serial_driver(serial_drivers, id_table);
  818. MODULE_DESCRIPTION(DRIVER_DESC);
  819. MODULE_LICENSE("GPL");