pl2303.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. type = type_1;
  177. else if (serial->dev->descriptor.bDeviceClass == 0xFF)
  178. type = type_1;
  179. dev_dbg(&serial->interface->dev, "device type: %d\n", type);
  180. spriv->type = type;
  181. usb_set_serial_data(serial, spriv);
  182. pl2303_vendor_read(0x8484, 0, serial, buf);
  183. pl2303_vendor_write(0x0404, 0, serial);
  184. pl2303_vendor_read(0x8484, 0, serial, buf);
  185. pl2303_vendor_read(0x8383, 0, serial, buf);
  186. pl2303_vendor_read(0x8484, 0, serial, buf);
  187. pl2303_vendor_write(0x0404, 1, serial);
  188. pl2303_vendor_read(0x8484, 0, serial, buf);
  189. pl2303_vendor_read(0x8383, 0, serial, buf);
  190. pl2303_vendor_write(0, 1, serial);
  191. pl2303_vendor_write(1, 0, serial);
  192. if (type == HX)
  193. pl2303_vendor_write(2, 0x44, serial);
  194. else
  195. pl2303_vendor_write(2, 0x24, serial);
  196. kfree(buf);
  197. return 0;
  198. }
  199. static void pl2303_release(struct usb_serial *serial)
  200. {
  201. struct pl2303_serial_private *spriv;
  202. spriv = usb_get_serial_data(serial);
  203. kfree(spriv);
  204. }
  205. static int pl2303_port_probe(struct usb_serial_port *port)
  206. {
  207. struct pl2303_private *priv;
  208. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  209. if (!priv)
  210. return -ENOMEM;
  211. spin_lock_init(&priv->lock);
  212. usb_set_serial_port_data(port, priv);
  213. port->port.drain_delay = 256;
  214. return 0;
  215. }
  216. static int pl2303_port_remove(struct usb_serial_port *port)
  217. {
  218. struct pl2303_private *priv;
  219. priv = usb_get_serial_port_data(port);
  220. kfree(priv);
  221. return 0;
  222. }
  223. static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
  224. {
  225. struct usb_device *dev = port->serial->dev;
  226. int retval;
  227. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  228. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  229. value, 0, NULL, 0, 100);
  230. dev_dbg(&port->dev, "%s - value = %d, retval = %d\n", __func__,
  231. value, retval);
  232. return retval;
  233. }
  234. static int pl2303_baudrate_encode_direct(int baud, enum pl2303_type type,
  235. u8 buf[4])
  236. {
  237. /*
  238. * NOTE: Only the values defined in baud_sup are supported !
  239. * => if unsupported values are set, the PL2303 seems to
  240. * use 9600 baud (at least my PL2303X always does)
  241. */
  242. const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
  243. 4800, 7200, 9600, 14400, 19200, 28800, 38400,
  244. 57600, 115200, 230400, 460800, 614400, 921600,
  245. 1228800, 2457600, 3000000, 6000000 };
  246. /*
  247. * NOTE: The PL2303HX (tested with rev. 3A) also supports the following
  248. * baud rates: 128000, 134400, 161280, 201600, 268800, 403200, 806400.
  249. * As long as we are not using this encoding method for them, there is
  250. * no point in complicating the code to support them.
  251. */
  252. int i;
  253. /* Set baudrate to nearest supported value */
  254. for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
  255. if (baud_sup[i] > baud)
  256. break;
  257. }
  258. if (i == ARRAY_SIZE(baud_sup))
  259. baud = baud_sup[i - 1];
  260. else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
  261. baud = baud_sup[i - 1];
  262. else
  263. baud = baud_sup[i];
  264. /* type_0, type_1 only support up to 1228800 baud */
  265. if (type != HX)
  266. baud = min_t(int, baud, 1228800);
  267. /* Direct (standard) baud rate encoding method */
  268. put_unaligned_le32(baud, buf);
  269. return baud;
  270. }
  271. static int pl2303_baudrate_encode_divisor(int baud, enum pl2303_type type,
  272. u8 buf[4])
  273. {
  274. /*
  275. * Divisor based baud rate encoding method
  276. *
  277. * NOTE: it's not clear if the type_0/1 chips support this method
  278. *
  279. * divisor = 12MHz * 32 / baudrate = 2^A * B
  280. *
  281. * with
  282. *
  283. * A = buf[1] & 0x0e
  284. * B = buf[0] + (buf[1] & 0x01) << 8
  285. *
  286. * Special cases:
  287. * => 8 < B < 16: device seems to work not properly
  288. * => B <= 8: device uses the max. value B = 512 instead
  289. */
  290. unsigned int A, B;
  291. /*
  292. * NOTE: The Windows driver allows maximum baud rates of 110% of the
  293. * specified maximium value.
  294. * Quick tests with early (2004) HX (rev. A) chips suggest, that even
  295. * higher baud rates (up to the maximum of 24M baud !) are working fine,
  296. * but that should really be tested carefully in "real life" scenarios
  297. * before removing the upper limit completely.
  298. * Baud rates smaller than the specified 75 baud are definitely working
  299. * fine.
  300. */
  301. if (type == HX)
  302. baud = min_t(int, baud, 6000000 * 1.1);
  303. else
  304. baud = min_t(int, baud, 1228800 * 1.1);
  305. /* Determine factors A and B */
  306. A = 0;
  307. B = 12000000 * 32 / baud; /* 12MHz */
  308. B <<= 1; /* Add one bit for rounding */
  309. while (B > (512 << 1) && A <= 14) {
  310. A += 2;
  311. B >>= 2;
  312. }
  313. if (A > 14) { /* max. divisor = min. baudrate reached */
  314. A = 14;
  315. B = 512;
  316. /* => ~45.78 baud */
  317. } else {
  318. B = (B + 1) >> 1; /* Round the last bit */
  319. }
  320. /* Handle special cases */
  321. if (B == 512)
  322. B = 0; /* also: 1 to 8 */
  323. else if (B < 16)
  324. /*
  325. * NOTE: With the current algorithm this happens
  326. * only for A=0 and means that the min. divisor
  327. * (respectively: the max. baudrate) is reached.
  328. */
  329. B = 16; /* => 24 MBaud */
  330. /* Encode the baud rate */
  331. buf[3] = 0x80; /* Select divisor encoding method */
  332. buf[2] = 0;
  333. buf[1] = (A & 0x0e); /* A */
  334. buf[1] |= ((B & 0x100) >> 8); /* MSB of B */
  335. buf[0] = B & 0xff; /* 8 LSBs of B */
  336. /* Calculate the actual/resulting baud rate */
  337. if (B <= 8)
  338. B = 512;
  339. baud = 12000000 * 32 / ((1 << A) * B);
  340. return baud;
  341. }
  342. static void pl2303_encode_baudrate(struct tty_struct *tty,
  343. struct usb_serial_port *port,
  344. enum pl2303_type type,
  345. u8 buf[4])
  346. {
  347. int baud;
  348. baud = tty_get_baud_rate(tty);
  349. dev_dbg(&port->dev, "baud requested = %d\n", baud);
  350. if (!baud)
  351. return;
  352. /*
  353. * There are two methods for setting/encoding the baud rate
  354. * 1) Direct method: encodes the baud rate value directly
  355. * => supported by all chip types
  356. * 2) Divisor based method: encodes a divisor to a base value (12MHz*32)
  357. * => supported by HX chips (and likely not by type_0/1 chips)
  358. *
  359. * NOTE: Although the divisor based baud rate encoding method is much
  360. * more flexible, some of the standard baud rate values can not be
  361. * realized exactly. But the difference is very small (max. 0.2%) and
  362. * the device likely uses the same baud rate generator for both methods
  363. * so that there is likley no difference.
  364. */
  365. if (type != HX)
  366. baud = pl2303_baudrate_encode_direct(baud, type, buf);
  367. else
  368. baud = pl2303_baudrate_encode_divisor(baud, type, buf);
  369. /* Save resulting baud rate */
  370. tty_encode_baud_rate(tty, baud, baud);
  371. dev_dbg(&port->dev, "baud set = %d\n", baud);
  372. }
  373. static void pl2303_set_termios(struct tty_struct *tty,
  374. struct usb_serial_port *port, struct ktermios *old_termios)
  375. {
  376. struct usb_serial *serial = port->serial;
  377. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  378. struct pl2303_private *priv = usb_get_serial_port_data(port);
  379. unsigned long flags;
  380. unsigned char *buf;
  381. int i;
  382. u8 control;
  383. /*
  384. * The PL2303 is reported to lose bytes if you change serial settings
  385. * even to the same values as before. Thus we actually need to filter
  386. * in this specific case.
  387. */
  388. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  389. return;
  390. buf = kzalloc(7, GFP_KERNEL);
  391. if (!buf) {
  392. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  393. /* Report back no change occurred */
  394. if (old_termios)
  395. tty->termios = *old_termios;
  396. return;
  397. }
  398. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  399. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  400. 0, 0, buf, 7, 100);
  401. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %7ph\n", i, buf);
  402. if (C_CSIZE(tty)) {
  403. switch (C_CSIZE(tty)) {
  404. case CS5:
  405. buf[6] = 5;
  406. break;
  407. case CS6:
  408. buf[6] = 6;
  409. break;
  410. case CS7:
  411. buf[6] = 7;
  412. break;
  413. default:
  414. case CS8:
  415. buf[6] = 8;
  416. }
  417. dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
  418. }
  419. /* For reference: buf[0]:buf[3] baud rate value */
  420. pl2303_encode_baudrate(tty, port, spriv->type, buf);
  421. /* For reference buf[4]=0 is 1 stop bits */
  422. /* For reference buf[4]=1 is 1.5 stop bits */
  423. /* For reference buf[4]=2 is 2 stop bits */
  424. if (C_CSTOPB(tty)) {
  425. /*
  426. * NOTE: Comply with "real" UARTs / RS232:
  427. * use 1.5 instead of 2 stop bits with 5 data bits
  428. */
  429. if (C_CSIZE(tty) == CS5) {
  430. buf[4] = 1;
  431. dev_dbg(&port->dev, "stop bits = 1.5\n");
  432. } else {
  433. buf[4] = 2;
  434. dev_dbg(&port->dev, "stop bits = 2\n");
  435. }
  436. } else {
  437. buf[4] = 0;
  438. dev_dbg(&port->dev, "stop bits = 1\n");
  439. }
  440. if (C_PARENB(tty)) {
  441. /* For reference buf[5]=0 is none parity */
  442. /* For reference buf[5]=1 is odd parity */
  443. /* For reference buf[5]=2 is even parity */
  444. /* For reference buf[5]=3 is mark parity */
  445. /* For reference buf[5]=4 is space parity */
  446. if (C_PARODD(tty)) {
  447. if (tty->termios.c_cflag & CMSPAR) {
  448. buf[5] = 3;
  449. dev_dbg(&port->dev, "parity = mark\n");
  450. } else {
  451. buf[5] = 1;
  452. dev_dbg(&port->dev, "parity = odd\n");
  453. }
  454. } else {
  455. if (tty->termios.c_cflag & CMSPAR) {
  456. buf[5] = 4;
  457. dev_dbg(&port->dev, "parity = space\n");
  458. } else {
  459. buf[5] = 2;
  460. dev_dbg(&port->dev, "parity = even\n");
  461. }
  462. }
  463. } else {
  464. buf[5] = 0;
  465. dev_dbg(&port->dev, "parity = none\n");
  466. }
  467. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  468. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  469. 0, 0, buf, 7, 100);
  470. dev_dbg(&port->dev, "0x21:0x20:0:0 %d\n", i);
  471. /* change control lines if we are switching to or from B0 */
  472. spin_lock_irqsave(&priv->lock, flags);
  473. control = priv->line_control;
  474. if (C_BAUD(tty) == B0)
  475. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  476. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  477. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  478. if (control != priv->line_control) {
  479. control = priv->line_control;
  480. spin_unlock_irqrestore(&priv->lock, flags);
  481. pl2303_set_control_lines(port, control);
  482. } else {
  483. spin_unlock_irqrestore(&priv->lock, flags);
  484. }
  485. memset(buf, 0, 7);
  486. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  487. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  488. 0, 0, buf, 7, 100);
  489. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %7ph\n", i, buf);
  490. if (C_CRTSCTS(tty)) {
  491. if (spriv->type == HX)
  492. pl2303_vendor_write(0x0, 0x61, serial);
  493. else
  494. pl2303_vendor_write(0x0, 0x41, serial);
  495. } else {
  496. pl2303_vendor_write(0x0, 0x0, serial);
  497. }
  498. kfree(buf);
  499. }
  500. static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
  501. {
  502. struct pl2303_private *priv = usb_get_serial_port_data(port);
  503. unsigned long flags;
  504. u8 control;
  505. spin_lock_irqsave(&priv->lock, flags);
  506. /* Change DTR and RTS */
  507. if (on)
  508. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  509. else
  510. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  511. control = priv->line_control;
  512. spin_unlock_irqrestore(&priv->lock, flags);
  513. pl2303_set_control_lines(port, control);
  514. }
  515. static void pl2303_close(struct usb_serial_port *port)
  516. {
  517. usb_serial_generic_close(port);
  518. usb_kill_urb(port->interrupt_in_urb);
  519. }
  520. static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
  521. {
  522. struct usb_serial *serial = port->serial;
  523. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  524. int result;
  525. if (spriv->type != HX) {
  526. usb_clear_halt(serial->dev, port->write_urb->pipe);
  527. usb_clear_halt(serial->dev, port->read_urb->pipe);
  528. } else {
  529. /* reset upstream data pipes */
  530. pl2303_vendor_write(8, 0, serial);
  531. pl2303_vendor_write(9, 0, serial);
  532. }
  533. /* Setup termios */
  534. if (tty)
  535. pl2303_set_termios(tty, port, NULL);
  536. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  537. if (result) {
  538. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  539. " error %d\n", __func__, result);
  540. return result;
  541. }
  542. result = usb_serial_generic_open(tty, port);
  543. if (result) {
  544. usb_kill_urb(port->interrupt_in_urb);
  545. return result;
  546. }
  547. return 0;
  548. }
  549. static int pl2303_tiocmset(struct tty_struct *tty,
  550. unsigned int set, unsigned int clear)
  551. {
  552. struct usb_serial_port *port = tty->driver_data;
  553. struct pl2303_private *priv = usb_get_serial_port_data(port);
  554. unsigned long flags;
  555. u8 control;
  556. int ret;
  557. spin_lock_irqsave(&priv->lock, flags);
  558. if (set & TIOCM_RTS)
  559. priv->line_control |= CONTROL_RTS;
  560. if (set & TIOCM_DTR)
  561. priv->line_control |= CONTROL_DTR;
  562. if (clear & TIOCM_RTS)
  563. priv->line_control &= ~CONTROL_RTS;
  564. if (clear & TIOCM_DTR)
  565. priv->line_control &= ~CONTROL_DTR;
  566. control = priv->line_control;
  567. spin_unlock_irqrestore(&priv->lock, flags);
  568. ret = pl2303_set_control_lines(port, control);
  569. if (ret)
  570. return usb_translate_errors(ret);
  571. return 0;
  572. }
  573. static int pl2303_tiocmget(struct tty_struct *tty)
  574. {
  575. struct usb_serial_port *port = tty->driver_data;
  576. struct pl2303_private *priv = usb_get_serial_port_data(port);
  577. unsigned long flags;
  578. unsigned int mcr;
  579. unsigned int status;
  580. unsigned int result;
  581. spin_lock_irqsave(&priv->lock, flags);
  582. mcr = priv->line_control;
  583. status = priv->line_status;
  584. spin_unlock_irqrestore(&priv->lock, flags);
  585. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  586. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  587. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  588. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  589. | ((status & UART_RING) ? TIOCM_RI : 0)
  590. | ((status & UART_DCD) ? TIOCM_CD : 0);
  591. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  592. return result;
  593. }
  594. static int pl2303_carrier_raised(struct usb_serial_port *port)
  595. {
  596. struct pl2303_private *priv = usb_get_serial_port_data(port);
  597. if (priv->line_status & UART_DCD)
  598. return 1;
  599. return 0;
  600. }
  601. static int pl2303_tiocmiwait(struct tty_struct *tty, unsigned long arg)
  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 prevstatus;
  607. unsigned int status;
  608. unsigned int changed;
  609. spin_lock_irqsave(&priv->lock, flags);
  610. prevstatus = priv->line_status;
  611. spin_unlock_irqrestore(&priv->lock, flags);
  612. while (1) {
  613. interruptible_sleep_on(&port->port.delta_msr_wait);
  614. /* see if a signal did it */
  615. if (signal_pending(current))
  616. return -ERESTARTSYS;
  617. if (port->serial->disconnected)
  618. return -EIO;
  619. spin_lock_irqsave(&priv->lock, flags);
  620. status = priv->line_status;
  621. spin_unlock_irqrestore(&priv->lock, flags);
  622. changed = prevstatus ^ status;
  623. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  624. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  625. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  626. ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
  627. return 0;
  628. }
  629. prevstatus = status;
  630. }
  631. /* NOTREACHED */
  632. return 0;
  633. }
  634. static int pl2303_ioctl(struct tty_struct *tty,
  635. unsigned int cmd, unsigned long arg)
  636. {
  637. struct serial_struct ser;
  638. struct usb_serial_port *port = tty->driver_data;
  639. dev_dbg(&port->dev, "%s cmd = 0x%04x\n", __func__, cmd);
  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. dev_dbg(&port->dev, "%s not supported = 0x%04x\n", __func__, cmd);
  652. break;
  653. }
  654. return -ENOIOCTLCMD;
  655. }
  656. static void pl2303_break_ctl(struct tty_struct *tty, int break_state)
  657. {
  658. struct usb_serial_port *port = tty->driver_data;
  659. struct usb_serial *serial = port->serial;
  660. u16 state;
  661. int result;
  662. if (break_state == 0)
  663. state = BREAK_OFF;
  664. else
  665. state = BREAK_ON;
  666. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  667. state == BREAK_OFF ? "off" : "on");
  668. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  669. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  670. 0, NULL, 0, 100);
  671. if (result)
  672. dev_err(&port->dev, "error sending break = %d\n", result);
  673. }
  674. static void pl2303_update_line_status(struct usb_serial_port *port,
  675. unsigned char *data,
  676. unsigned int actual_length)
  677. {
  678. struct pl2303_private *priv = usb_get_serial_port_data(port);
  679. struct tty_struct *tty;
  680. unsigned long flags;
  681. u8 status_idx = UART_STATE;
  682. u8 length = UART_STATE + 1;
  683. u8 prev_line_status;
  684. u16 idv, idp;
  685. idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
  686. idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
  687. if (idv == SIEMENS_VENDOR_ID) {
  688. if (idp == SIEMENS_PRODUCT_ID_X65 ||
  689. idp == SIEMENS_PRODUCT_ID_SX1 ||
  690. idp == SIEMENS_PRODUCT_ID_X75) {
  691. length = 1;
  692. status_idx = 0;
  693. }
  694. }
  695. if (actual_length < length)
  696. return;
  697. /* Save off the uart status for others to look at */
  698. spin_lock_irqsave(&priv->lock, flags);
  699. prev_line_status = priv->line_status;
  700. priv->line_status = data[status_idx];
  701. spin_unlock_irqrestore(&priv->lock, flags);
  702. if (priv->line_status & UART_BREAK_ERROR)
  703. usb_serial_handle_break(port);
  704. wake_up_interruptible(&port->port.delta_msr_wait);
  705. tty = tty_port_tty_get(&port->port);
  706. if (!tty)
  707. return;
  708. if ((priv->line_status ^ prev_line_status) & UART_DCD)
  709. usb_serial_handle_dcd_change(port, tty,
  710. priv->line_status & UART_DCD);
  711. tty_kref_put(tty);
  712. }
  713. static void pl2303_read_int_callback(struct urb *urb)
  714. {
  715. struct usb_serial_port *port = urb->context;
  716. unsigned char *data = urb->transfer_buffer;
  717. unsigned int actual_length = urb->actual_length;
  718. int status = urb->status;
  719. int retval;
  720. switch (status) {
  721. case 0:
  722. /* success */
  723. break;
  724. case -ECONNRESET:
  725. case -ENOENT:
  726. case -ESHUTDOWN:
  727. /* this urb is terminated, clean up */
  728. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  729. __func__, status);
  730. return;
  731. default:
  732. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  733. __func__, status);
  734. goto exit;
  735. }
  736. usb_serial_debug_data(&port->dev, __func__,
  737. urb->actual_length, urb->transfer_buffer);
  738. pl2303_update_line_status(port, data, actual_length);
  739. exit:
  740. retval = usb_submit_urb(urb, GFP_ATOMIC);
  741. if (retval)
  742. dev_err(&port->dev,
  743. "%s - usb_submit_urb failed with result %d\n",
  744. __func__, retval);
  745. }
  746. static void pl2303_process_read_urb(struct urb *urb)
  747. {
  748. struct usb_serial_port *port = urb->context;
  749. struct pl2303_private *priv = usb_get_serial_port_data(port);
  750. unsigned char *data = urb->transfer_buffer;
  751. char tty_flag = TTY_NORMAL;
  752. unsigned long flags;
  753. u8 line_status;
  754. int i;
  755. /* update line status */
  756. spin_lock_irqsave(&priv->lock, flags);
  757. line_status = priv->line_status;
  758. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  759. spin_unlock_irqrestore(&priv->lock, flags);
  760. wake_up_interruptible(&port->port.delta_msr_wait);
  761. if (!urb->actual_length)
  762. return;
  763. /* break takes precedence over parity, */
  764. /* which takes precedence over framing errors */
  765. if (line_status & UART_BREAK_ERROR)
  766. tty_flag = TTY_BREAK;
  767. else if (line_status & UART_PARITY_ERROR)
  768. tty_flag = TTY_PARITY;
  769. else if (line_status & UART_FRAME_ERROR)
  770. tty_flag = TTY_FRAME;
  771. if (tty_flag != TTY_NORMAL)
  772. dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__,
  773. tty_flag);
  774. /* overrun is special, not associated with a char */
  775. if (line_status & UART_OVERRUN_ERROR)
  776. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  777. if (port->port.console && port->sysrq) {
  778. for (i = 0; i < urb->actual_length; ++i)
  779. if (!usb_serial_handle_sysrq_char(port, data[i]))
  780. tty_insert_flip_char(&port->port, data[i],
  781. tty_flag);
  782. } else {
  783. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  784. urb->actual_length);
  785. }
  786. tty_flip_buffer_push(&port->port);
  787. }
  788. /* All of the device info needed for the PL2303 SIO serial converter */
  789. static struct usb_serial_driver pl2303_device = {
  790. .driver = {
  791. .owner = THIS_MODULE,
  792. .name = "pl2303",
  793. },
  794. .id_table = id_table,
  795. .num_ports = 1,
  796. .bulk_in_size = 256,
  797. .bulk_out_size = 256,
  798. .open = pl2303_open,
  799. .close = pl2303_close,
  800. .dtr_rts = pl2303_dtr_rts,
  801. .carrier_raised = pl2303_carrier_raised,
  802. .ioctl = pl2303_ioctl,
  803. .break_ctl = pl2303_break_ctl,
  804. .set_termios = pl2303_set_termios,
  805. .tiocmget = pl2303_tiocmget,
  806. .tiocmset = pl2303_tiocmset,
  807. .tiocmiwait = pl2303_tiocmiwait,
  808. .process_read_urb = pl2303_process_read_urb,
  809. .read_int_callback = pl2303_read_int_callback,
  810. .attach = pl2303_startup,
  811. .release = pl2303_release,
  812. .port_probe = pl2303_port_probe,
  813. .port_remove = pl2303_port_remove,
  814. };
  815. static struct usb_serial_driver * const serial_drivers[] = {
  816. &pl2303_device, NULL
  817. };
  818. module_usb_serial_driver(serial_drivers, id_table);
  819. MODULE_DESCRIPTION(DRIVER_DESC);
  820. MODULE_LICENSE("GPL");