pl2303.c 28 KB

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