pl2303.c 29 KB

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