pl2303.c 28 KB

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