ch341.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2007, Frank A Kingswood <[email protected]>
  4. * Copyright 2007, Werner Cornelius <[email protected]>
  5. * Copyright 2009, Boris Hajduk <[email protected]>
  6. *
  7. * ch341.c implements a serial port driver for the Winchiphead CH341.
  8. *
  9. * The CH341 device can be used to implement an RS232 asynchronous
  10. * serial port, an IEEE-1284 parallel printer port or a memory-like
  11. * interface. In all cases the CH341 supports an I2C interface as well.
  12. * This driver only supports the asynchronous serial interface.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/tty.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/serial.h>
  20. #include <linux/serial.h>
  21. #include <asm/unaligned.h>
  22. #define DEFAULT_BAUD_RATE 9600
  23. #define DEFAULT_TIMEOUT 1000
  24. /* flags for IO-Bits */
  25. #define CH341_BIT_RTS (1 << 6)
  26. #define CH341_BIT_DTR (1 << 5)
  27. /******************************/
  28. /* interrupt pipe definitions */
  29. /******************************/
  30. /* always 4 interrupt bytes */
  31. /* first irq byte normally 0x08 */
  32. /* second irq byte base 0x7d + below */
  33. /* third irq byte base 0x94 + below */
  34. /* fourth irq byte normally 0xee */
  35. /* second interrupt byte */
  36. #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
  37. /* status returned in third interrupt answer byte, inverted in data
  38. from irq */
  39. #define CH341_BIT_CTS 0x01
  40. #define CH341_BIT_DSR 0x02
  41. #define CH341_BIT_RI 0x04
  42. #define CH341_BIT_DCD 0x08
  43. #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
  44. /* Break support - the information used to implement this was gleaned from
  45. * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato.
  46. */
  47. #define CH341_REQ_READ_VERSION 0x5F
  48. #define CH341_REQ_WRITE_REG 0x9A
  49. #define CH341_REQ_READ_REG 0x95
  50. #define CH341_REQ_SERIAL_INIT 0xA1
  51. #define CH341_REQ_MODEM_CTRL 0xA4
  52. #define CH341_REG_BREAK 0x05
  53. #define CH341_REG_PRESCALER 0x12
  54. #define CH341_REG_DIVISOR 0x13
  55. #define CH341_REG_LCR 0x18
  56. #define CH341_REG_LCR2 0x25
  57. #define CH341_NBREAK_BITS 0x01
  58. #define CH341_LCR_ENABLE_RX 0x80
  59. #define CH341_LCR_ENABLE_TX 0x40
  60. #define CH341_LCR_MARK_SPACE 0x20
  61. #define CH341_LCR_PAR_EVEN 0x10
  62. #define CH341_LCR_ENABLE_PAR 0x08
  63. #define CH341_LCR_STOP_BITS_2 0x04
  64. #define CH341_LCR_CS8 0x03
  65. #define CH341_LCR_CS7 0x02
  66. #define CH341_LCR_CS6 0x01
  67. #define CH341_LCR_CS5 0x00
  68. #define CH341_QUIRK_LIMITED_PRESCALER BIT(0)
  69. #define CH341_QUIRK_SIMULATE_BREAK BIT(1)
  70. static const struct usb_device_id id_table[] = {
  71. { USB_DEVICE(0x1a86, 0x5523) },
  72. { USB_DEVICE(0x1a86, 0x7522) },
  73. { USB_DEVICE(0x1a86, 0x7523) },
  74. { USB_DEVICE(0x2184, 0x0057) },
  75. { USB_DEVICE(0x4348, 0x5523) },
  76. { USB_DEVICE(0x9986, 0x7523) },
  77. { },
  78. };
  79. MODULE_DEVICE_TABLE(usb, id_table);
  80. struct ch341_private {
  81. spinlock_t lock; /* access lock */
  82. unsigned baud_rate; /* set baud rate */
  83. u8 mcr;
  84. u8 msr;
  85. u8 lcr;
  86. unsigned long quirks;
  87. u8 version;
  88. unsigned long break_end;
  89. };
  90. static void ch341_set_termios(struct tty_struct *tty,
  91. struct usb_serial_port *port,
  92. const struct ktermios *old_termios);
  93. static int ch341_control_out(struct usb_device *dev, u8 request,
  94. u16 value, u16 index)
  95. {
  96. int r;
  97. dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
  98. request, value, index);
  99. r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  100. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  101. value, index, NULL, 0, DEFAULT_TIMEOUT);
  102. if (r < 0)
  103. dev_err(&dev->dev, "failed to send control message: %d\n", r);
  104. return r;
  105. }
  106. static int ch341_control_in(struct usb_device *dev,
  107. u8 request, u16 value, u16 index,
  108. char *buf, unsigned bufsize)
  109. {
  110. int r;
  111. dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
  112. request, value, index, bufsize);
  113. r = usb_control_msg_recv(dev, 0, request,
  114. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  115. value, index, buf, bufsize, DEFAULT_TIMEOUT,
  116. GFP_KERNEL);
  117. if (r) {
  118. dev_err(&dev->dev, "failed to receive control message: %d\n",
  119. r);
  120. return r;
  121. }
  122. return 0;
  123. }
  124. #define CH341_CLKRATE 48000000
  125. #define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact)))
  126. #define CH341_MIN_RATE(ps) (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512))
  127. static const speed_t ch341_min_rates[] = {
  128. CH341_MIN_RATE(0),
  129. CH341_MIN_RATE(1),
  130. CH341_MIN_RATE(2),
  131. CH341_MIN_RATE(3),
  132. };
  133. /* Supported range is 46 to 3000000 bps. */
  134. #define CH341_MIN_BPS DIV_ROUND_UP(CH341_CLKRATE, CH341_CLK_DIV(0, 0) * 256)
  135. #define CH341_MAX_BPS (CH341_CLKRATE / (CH341_CLK_DIV(3, 0) * 2))
  136. /*
  137. * The device line speed is given by the following equation:
  138. *
  139. * baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
  140. *
  141. * 0 <= ps <= 3,
  142. * 0 <= fact <= 1,
  143. * 2 <= div <= 256 if fact = 0, or
  144. * 9 <= div <= 256 if fact = 1
  145. */
  146. static int ch341_get_divisor(struct ch341_private *priv, speed_t speed)
  147. {
  148. unsigned int fact, div, clk_div;
  149. bool force_fact0 = false;
  150. int ps;
  151. /*
  152. * Clamp to supported range, this makes the (ps < 0) and (div < 2)
  153. * sanity checks below redundant.
  154. */
  155. speed = clamp_val(speed, CH341_MIN_BPS, CH341_MAX_BPS);
  156. /*
  157. * Start with highest possible base clock (fact = 1) that will give a
  158. * divisor strictly less than 512.
  159. */
  160. fact = 1;
  161. for (ps = 3; ps >= 0; ps--) {
  162. if (speed > ch341_min_rates[ps])
  163. break;
  164. }
  165. if (ps < 0)
  166. return -EINVAL;
  167. /* Determine corresponding divisor, rounding down. */
  168. clk_div = CH341_CLK_DIV(ps, fact);
  169. div = CH341_CLKRATE / (clk_div * speed);
  170. /* Some devices require a lower base clock if ps < 3. */
  171. if (ps < 3 && (priv->quirks & CH341_QUIRK_LIMITED_PRESCALER))
  172. force_fact0 = true;
  173. /* Halve base clock (fact = 0) if required. */
  174. if (div < 9 || div > 255 || force_fact0) {
  175. div /= 2;
  176. clk_div *= 2;
  177. fact = 0;
  178. }
  179. if (div < 2)
  180. return -EINVAL;
  181. /*
  182. * Pick next divisor if resulting rate is closer to the requested one,
  183. * scale up to avoid rounding errors on low rates.
  184. */
  185. if (16 * CH341_CLKRATE / (clk_div * div) - 16 * speed >=
  186. 16 * speed - 16 * CH341_CLKRATE / (clk_div * (div + 1)))
  187. div++;
  188. /*
  189. * Prefer lower base clock (fact = 0) if even divisor.
  190. *
  191. * Note that this makes the receiver more tolerant to errors.
  192. */
  193. if (fact == 1 && div % 2 == 0) {
  194. div /= 2;
  195. fact = 0;
  196. }
  197. return (0x100 - div) << 8 | fact << 2 | ps;
  198. }
  199. static int ch341_set_baudrate_lcr(struct usb_device *dev,
  200. struct ch341_private *priv,
  201. speed_t baud_rate, u8 lcr)
  202. {
  203. int val;
  204. int r;
  205. if (!baud_rate)
  206. return -EINVAL;
  207. val = ch341_get_divisor(priv, baud_rate);
  208. if (val < 0)
  209. return -EINVAL;
  210. /*
  211. * CH341A buffers data until a full endpoint-size packet (32 bytes)
  212. * has been received unless bit 7 is set.
  213. *
  214. * At least one device with version 0x27 appears to have this bit
  215. * inverted.
  216. */
  217. if (priv->version > 0x27)
  218. val |= BIT(7);
  219. r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
  220. CH341_REG_DIVISOR << 8 | CH341_REG_PRESCALER,
  221. val);
  222. if (r)
  223. return r;
  224. /*
  225. * Chip versions before version 0x30 as read using
  226. * CH341_REQ_READ_VERSION used separate registers for line control
  227. * (stop bits, parity and word length). Version 0x30 and above use
  228. * CH341_REG_LCR only and CH341_REG_LCR2 is always set to zero.
  229. */
  230. if (priv->version < 0x30)
  231. return 0;
  232. r = ch341_control_out(dev, CH341_REQ_WRITE_REG,
  233. CH341_REG_LCR2 << 8 | CH341_REG_LCR, lcr);
  234. if (r)
  235. return r;
  236. return r;
  237. }
  238. static int ch341_set_handshake(struct usb_device *dev, u8 control)
  239. {
  240. return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
  241. }
  242. static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
  243. {
  244. const unsigned int size = 2;
  245. u8 buffer[2];
  246. int r;
  247. unsigned long flags;
  248. r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
  249. if (r)
  250. return r;
  251. spin_lock_irqsave(&priv->lock, flags);
  252. priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
  253. spin_unlock_irqrestore(&priv->lock, flags);
  254. return 0;
  255. }
  256. /* -------------------------------------------------------------------------- */
  257. static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
  258. {
  259. const unsigned int size = 2;
  260. u8 buffer[2];
  261. int r;
  262. /* expect two bytes 0x27 0x00 */
  263. r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
  264. if (r)
  265. return r;
  266. priv->version = buffer[0];
  267. dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version);
  268. r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
  269. if (r < 0)
  270. return r;
  271. r = ch341_set_baudrate_lcr(dev, priv, priv->baud_rate, priv->lcr);
  272. if (r < 0)
  273. return r;
  274. r = ch341_set_handshake(dev, priv->mcr);
  275. if (r < 0)
  276. return r;
  277. return 0;
  278. }
  279. static int ch341_detect_quirks(struct usb_serial_port *port)
  280. {
  281. struct ch341_private *priv = usb_get_serial_port_data(port);
  282. struct usb_device *udev = port->serial->dev;
  283. const unsigned int size = 2;
  284. unsigned long quirks = 0;
  285. u8 buffer[2];
  286. int r;
  287. /*
  288. * A subset of CH34x devices does not support all features. The
  289. * prescaler is limited and there is no support for sending a RS232
  290. * break condition. A read failure when trying to set up the latter is
  291. * used to detect these devices.
  292. */
  293. r = usb_control_msg_recv(udev, 0, CH341_REQ_READ_REG,
  294. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  295. CH341_REG_BREAK, 0, &buffer, size,
  296. DEFAULT_TIMEOUT, GFP_KERNEL);
  297. if (r == -EPIPE) {
  298. dev_info(&port->dev, "break control not supported, using simulated break\n");
  299. quirks = CH341_QUIRK_LIMITED_PRESCALER | CH341_QUIRK_SIMULATE_BREAK;
  300. r = 0;
  301. } else if (r) {
  302. dev_err(&port->dev, "failed to read break control: %d\n", r);
  303. }
  304. if (quirks) {
  305. dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks);
  306. priv->quirks |= quirks;
  307. }
  308. return r;
  309. }
  310. static int ch341_port_probe(struct usb_serial_port *port)
  311. {
  312. struct ch341_private *priv;
  313. int r;
  314. priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
  315. if (!priv)
  316. return -ENOMEM;
  317. spin_lock_init(&priv->lock);
  318. priv->baud_rate = DEFAULT_BAUD_RATE;
  319. /*
  320. * Some CH340 devices appear unable to change the initial LCR
  321. * settings, so set a sane 8N1 default.
  322. */
  323. priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
  324. r = ch341_configure(port->serial->dev, priv);
  325. if (r < 0)
  326. goto error;
  327. usb_set_serial_port_data(port, priv);
  328. r = ch341_detect_quirks(port);
  329. if (r < 0)
  330. goto error;
  331. return 0;
  332. error: kfree(priv);
  333. return r;
  334. }
  335. static void ch341_port_remove(struct usb_serial_port *port)
  336. {
  337. struct ch341_private *priv;
  338. priv = usb_get_serial_port_data(port);
  339. kfree(priv);
  340. }
  341. static int ch341_carrier_raised(struct usb_serial_port *port)
  342. {
  343. struct ch341_private *priv = usb_get_serial_port_data(port);
  344. if (priv->msr & CH341_BIT_DCD)
  345. return 1;
  346. return 0;
  347. }
  348. static void ch341_dtr_rts(struct usb_serial_port *port, int on)
  349. {
  350. struct ch341_private *priv = usb_get_serial_port_data(port);
  351. unsigned long flags;
  352. /* drop DTR and RTS */
  353. spin_lock_irqsave(&priv->lock, flags);
  354. if (on)
  355. priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
  356. else
  357. priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
  358. spin_unlock_irqrestore(&priv->lock, flags);
  359. ch341_set_handshake(port->serial->dev, priv->mcr);
  360. }
  361. static void ch341_close(struct usb_serial_port *port)
  362. {
  363. usb_serial_generic_close(port);
  364. usb_kill_urb(port->interrupt_in_urb);
  365. }
  366. /* open this device, set default parameters */
  367. static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
  368. {
  369. struct ch341_private *priv = usb_get_serial_port_data(port);
  370. int r;
  371. if (tty)
  372. ch341_set_termios(tty, port, NULL);
  373. dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
  374. r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  375. if (r) {
  376. dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
  377. __func__, r);
  378. return r;
  379. }
  380. r = ch341_get_status(port->serial->dev, priv);
  381. if (r < 0) {
  382. dev_err(&port->dev, "failed to read modem status: %d\n", r);
  383. goto err_kill_interrupt_urb;
  384. }
  385. r = usb_serial_generic_open(tty, port);
  386. if (r)
  387. goto err_kill_interrupt_urb;
  388. return 0;
  389. err_kill_interrupt_urb:
  390. usb_kill_urb(port->interrupt_in_urb);
  391. return r;
  392. }
  393. /* Old_termios contains the original termios settings and
  394. * tty->termios contains the new setting to be used.
  395. */
  396. static void ch341_set_termios(struct tty_struct *tty,
  397. struct usb_serial_port *port,
  398. const struct ktermios *old_termios)
  399. {
  400. struct ch341_private *priv = usb_get_serial_port_data(port);
  401. unsigned baud_rate;
  402. unsigned long flags;
  403. u8 lcr;
  404. int r;
  405. /* redundant changes may cause the chip to lose bytes */
  406. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  407. return;
  408. baud_rate = tty_get_baud_rate(tty);
  409. lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
  410. switch (C_CSIZE(tty)) {
  411. case CS5:
  412. lcr |= CH341_LCR_CS5;
  413. break;
  414. case CS6:
  415. lcr |= CH341_LCR_CS6;
  416. break;
  417. case CS7:
  418. lcr |= CH341_LCR_CS7;
  419. break;
  420. case CS8:
  421. lcr |= CH341_LCR_CS8;
  422. break;
  423. }
  424. if (C_PARENB(tty)) {
  425. lcr |= CH341_LCR_ENABLE_PAR;
  426. if (C_PARODD(tty) == 0)
  427. lcr |= CH341_LCR_PAR_EVEN;
  428. if (C_CMSPAR(tty))
  429. lcr |= CH341_LCR_MARK_SPACE;
  430. }
  431. if (C_CSTOPB(tty))
  432. lcr |= CH341_LCR_STOP_BITS_2;
  433. if (baud_rate) {
  434. priv->baud_rate = baud_rate;
  435. r = ch341_set_baudrate_lcr(port->serial->dev, priv,
  436. priv->baud_rate, lcr);
  437. if (r < 0 && old_termios) {
  438. priv->baud_rate = tty_termios_baud_rate(old_termios);
  439. tty_termios_copy_hw(&tty->termios, old_termios);
  440. } else if (r == 0) {
  441. priv->lcr = lcr;
  442. }
  443. }
  444. spin_lock_irqsave(&priv->lock, flags);
  445. if (C_BAUD(tty) == B0)
  446. priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
  447. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  448. priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
  449. spin_unlock_irqrestore(&priv->lock, flags);
  450. ch341_set_handshake(port->serial->dev, priv->mcr);
  451. }
  452. /*
  453. * A subset of all CH34x devices don't support a real break condition and
  454. * reading CH341_REG_BREAK fails (see also ch341_detect_quirks). This function
  455. * simulates a break condition by lowering the baud rate to the minimum
  456. * supported by the hardware upon enabling the break condition and sending
  457. * a NUL byte.
  458. *
  459. * Incoming data is corrupted while the break condition is being simulated.
  460. *
  461. * Normally the duration of the break condition can be controlled individually
  462. * by userspace using TIOCSBRK and TIOCCBRK or by passing an argument to
  463. * TCSBRKP. Due to how the simulation is implemented the duration can't be
  464. * controlled. The duration is always about (1s / 46bd * 9bit) = 196ms.
  465. */
  466. static void ch341_simulate_break(struct tty_struct *tty, int break_state)
  467. {
  468. struct usb_serial_port *port = tty->driver_data;
  469. struct ch341_private *priv = usb_get_serial_port_data(port);
  470. unsigned long now, delay;
  471. int r;
  472. if (break_state != 0) {
  473. dev_dbg(&port->dev, "enter break state requested\n");
  474. r = ch341_set_baudrate_lcr(port->serial->dev, priv,
  475. CH341_MIN_BPS,
  476. CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8);
  477. if (r < 0) {
  478. dev_err(&port->dev,
  479. "failed to change baud rate to %u: %d\n",
  480. CH341_MIN_BPS, r);
  481. goto restore;
  482. }
  483. r = tty_put_char(tty, '\0');
  484. if (r < 0) {
  485. dev_err(&port->dev,
  486. "failed to write NUL byte for simulated break condition: %d\n",
  487. r);
  488. goto restore;
  489. }
  490. /*
  491. * Compute expected transmission duration including safety
  492. * margin. The original baud rate is only restored after the
  493. * computed point in time.
  494. *
  495. * 11 bits = 1 start, 8 data, 1 stop, 1 margin
  496. */
  497. priv->break_end = jiffies + (11 * HZ / CH341_MIN_BPS);
  498. return;
  499. }
  500. dev_dbg(&port->dev, "leave break state requested\n");
  501. now = jiffies;
  502. if (time_before(now, priv->break_end)) {
  503. /* Wait until NUL byte is written */
  504. delay = priv->break_end - now;
  505. dev_dbg(&port->dev,
  506. "wait %d ms while transmitting NUL byte at %u baud\n",
  507. jiffies_to_msecs(delay), CH341_MIN_BPS);
  508. schedule_timeout_interruptible(delay);
  509. }
  510. restore:
  511. /* Restore original baud rate */
  512. r = ch341_set_baudrate_lcr(port->serial->dev, priv, priv->baud_rate,
  513. priv->lcr);
  514. if (r < 0)
  515. dev_err(&port->dev,
  516. "restoring original baud rate of %u failed: %d\n",
  517. priv->baud_rate, r);
  518. }
  519. static void ch341_break_ctl(struct tty_struct *tty, int break_state)
  520. {
  521. const uint16_t ch341_break_reg =
  522. ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
  523. struct usb_serial_port *port = tty->driver_data;
  524. struct ch341_private *priv = usb_get_serial_port_data(port);
  525. int r;
  526. uint16_t reg_contents;
  527. uint8_t break_reg[2];
  528. if (priv->quirks & CH341_QUIRK_SIMULATE_BREAK) {
  529. ch341_simulate_break(tty, break_state);
  530. return;
  531. }
  532. r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
  533. ch341_break_reg, 0, break_reg, 2);
  534. if (r) {
  535. dev_err(&port->dev, "%s - USB control read error (%d)\n",
  536. __func__, r);
  537. return;
  538. }
  539. dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
  540. __func__, break_reg[0], break_reg[1]);
  541. if (break_state != 0) {
  542. dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
  543. break_reg[0] &= ~CH341_NBREAK_BITS;
  544. break_reg[1] &= ~CH341_LCR_ENABLE_TX;
  545. } else {
  546. dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
  547. break_reg[0] |= CH341_NBREAK_BITS;
  548. break_reg[1] |= CH341_LCR_ENABLE_TX;
  549. }
  550. dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
  551. __func__, break_reg[0], break_reg[1]);
  552. reg_contents = get_unaligned_le16(break_reg);
  553. r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
  554. ch341_break_reg, reg_contents);
  555. if (r < 0)
  556. dev_err(&port->dev, "%s - USB control write error (%d)\n",
  557. __func__, r);
  558. }
  559. static int ch341_tiocmset(struct tty_struct *tty,
  560. unsigned int set, unsigned int clear)
  561. {
  562. struct usb_serial_port *port = tty->driver_data;
  563. struct ch341_private *priv = usb_get_serial_port_data(port);
  564. unsigned long flags;
  565. u8 control;
  566. spin_lock_irqsave(&priv->lock, flags);
  567. if (set & TIOCM_RTS)
  568. priv->mcr |= CH341_BIT_RTS;
  569. if (set & TIOCM_DTR)
  570. priv->mcr |= CH341_BIT_DTR;
  571. if (clear & TIOCM_RTS)
  572. priv->mcr &= ~CH341_BIT_RTS;
  573. if (clear & TIOCM_DTR)
  574. priv->mcr &= ~CH341_BIT_DTR;
  575. control = priv->mcr;
  576. spin_unlock_irqrestore(&priv->lock, flags);
  577. return ch341_set_handshake(port->serial->dev, control);
  578. }
  579. static void ch341_update_status(struct usb_serial_port *port,
  580. unsigned char *data, size_t len)
  581. {
  582. struct ch341_private *priv = usb_get_serial_port_data(port);
  583. struct tty_struct *tty;
  584. unsigned long flags;
  585. u8 status;
  586. u8 delta;
  587. if (len < 4)
  588. return;
  589. status = ~data[2] & CH341_BITS_MODEM_STAT;
  590. spin_lock_irqsave(&priv->lock, flags);
  591. delta = status ^ priv->msr;
  592. priv->msr = status;
  593. spin_unlock_irqrestore(&priv->lock, flags);
  594. if (data[1] & CH341_MULT_STAT)
  595. dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
  596. if (!delta)
  597. return;
  598. if (delta & CH341_BIT_CTS)
  599. port->icount.cts++;
  600. if (delta & CH341_BIT_DSR)
  601. port->icount.dsr++;
  602. if (delta & CH341_BIT_RI)
  603. port->icount.rng++;
  604. if (delta & CH341_BIT_DCD) {
  605. port->icount.dcd++;
  606. tty = tty_port_tty_get(&port->port);
  607. if (tty) {
  608. usb_serial_handle_dcd_change(port, tty,
  609. status & CH341_BIT_DCD);
  610. tty_kref_put(tty);
  611. }
  612. }
  613. wake_up_interruptible(&port->port.delta_msr_wait);
  614. }
  615. static void ch341_read_int_callback(struct urb *urb)
  616. {
  617. struct usb_serial_port *port = urb->context;
  618. unsigned char *data = urb->transfer_buffer;
  619. unsigned int len = urb->actual_length;
  620. int status;
  621. switch (urb->status) {
  622. case 0:
  623. /* success */
  624. break;
  625. case -ECONNRESET:
  626. case -ENOENT:
  627. case -ESHUTDOWN:
  628. /* this urb is terminated, clean up */
  629. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  630. __func__, urb->status);
  631. return;
  632. default:
  633. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  634. __func__, urb->status);
  635. goto exit;
  636. }
  637. usb_serial_debug_data(&port->dev, __func__, len, data);
  638. ch341_update_status(port, data, len);
  639. exit:
  640. status = usb_submit_urb(urb, GFP_ATOMIC);
  641. if (status) {
  642. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
  643. __func__, status);
  644. }
  645. }
  646. static int ch341_tiocmget(struct tty_struct *tty)
  647. {
  648. struct usb_serial_port *port = tty->driver_data;
  649. struct ch341_private *priv = usb_get_serial_port_data(port);
  650. unsigned long flags;
  651. u8 mcr;
  652. u8 status;
  653. unsigned int result;
  654. spin_lock_irqsave(&priv->lock, flags);
  655. mcr = priv->mcr;
  656. status = priv->msr;
  657. spin_unlock_irqrestore(&priv->lock, flags);
  658. result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
  659. | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
  660. | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
  661. | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
  662. | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
  663. | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
  664. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  665. return result;
  666. }
  667. static int ch341_reset_resume(struct usb_serial *serial)
  668. {
  669. struct usb_serial_port *port = serial->port[0];
  670. struct ch341_private *priv;
  671. int ret;
  672. priv = usb_get_serial_port_data(port);
  673. if (!priv)
  674. return 0;
  675. /* reconfigure ch341 serial port after bus-reset */
  676. ch341_configure(serial->dev, priv);
  677. if (tty_port_initialized(&port->port)) {
  678. ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  679. if (ret) {
  680. dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
  681. ret);
  682. return ret;
  683. }
  684. ret = ch341_get_status(port->serial->dev, priv);
  685. if (ret < 0) {
  686. dev_err(&port->dev, "failed to read modem status: %d\n",
  687. ret);
  688. }
  689. }
  690. return usb_serial_generic_resume(serial);
  691. }
  692. static struct usb_serial_driver ch341_device = {
  693. .driver = {
  694. .owner = THIS_MODULE,
  695. .name = "ch341-uart",
  696. },
  697. .id_table = id_table,
  698. .num_ports = 1,
  699. .open = ch341_open,
  700. .dtr_rts = ch341_dtr_rts,
  701. .carrier_raised = ch341_carrier_raised,
  702. .close = ch341_close,
  703. .set_termios = ch341_set_termios,
  704. .break_ctl = ch341_break_ctl,
  705. .tiocmget = ch341_tiocmget,
  706. .tiocmset = ch341_tiocmset,
  707. .tiocmiwait = usb_serial_generic_tiocmiwait,
  708. .read_int_callback = ch341_read_int_callback,
  709. .port_probe = ch341_port_probe,
  710. .port_remove = ch341_port_remove,
  711. .reset_resume = ch341_reset_resume,
  712. };
  713. static struct usb_serial_driver * const serial_drivers[] = {
  714. &ch341_device, NULL
  715. };
  716. module_usb_serial_driver(serial_drivers, id_table);
  717. MODULE_LICENSE("GPL v2");