f81232.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Fintek F81232 USB to serial adaptor driver
  4. * Fintek F81532A/534A/535/536 USB to 2/4/8/12 serial adaptor driver
  5. *
  6. * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
  7. * Copyright (C) 2012 Linux Foundation
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/tty.h>
  13. #include <linux/tty_driver.h>
  14. #include <linux/tty_flip.h>
  15. #include <linux/serial.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/mutex.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/serial.h>
  22. #include <linux/serial_reg.h>
  23. #define F81232_ID \
  24. { USB_DEVICE(0x1934, 0x0706) } /* 1 port UART device */
  25. #define F81534A_SERIES_ID \
  26. { USB_DEVICE(0x2c42, 0x1602) }, /* In-Box 2 port UART device */ \
  27. { USB_DEVICE(0x2c42, 0x1604) }, /* In-Box 4 port UART device */ \
  28. { USB_DEVICE(0x2c42, 0x1605) }, /* In-Box 8 port UART device */ \
  29. { USB_DEVICE(0x2c42, 0x1606) }, /* In-Box 12 port UART device */ \
  30. { USB_DEVICE(0x2c42, 0x1608) }, /* Non-Flash type */ \
  31. { USB_DEVICE(0x2c42, 0x1632) }, /* 2 port UART device */ \
  32. { USB_DEVICE(0x2c42, 0x1634) }, /* 4 port UART device */ \
  33. { USB_DEVICE(0x2c42, 0x1635) }, /* 8 port UART device */ \
  34. { USB_DEVICE(0x2c42, 0x1636) } /* 12 port UART device */
  35. #define F81534A_CTRL_ID \
  36. { USB_DEVICE(0x2c42, 0x16f8) } /* Global control device */
  37. static const struct usb_device_id f81232_id_table[] = {
  38. F81232_ID,
  39. { } /* Terminating entry */
  40. };
  41. static const struct usb_device_id f81534a_id_table[] = {
  42. F81534A_SERIES_ID,
  43. { } /* Terminating entry */
  44. };
  45. static const struct usb_device_id f81534a_ctrl_id_table[] = {
  46. F81534A_CTRL_ID,
  47. { } /* Terminating entry */
  48. };
  49. static const struct usb_device_id combined_id_table[] = {
  50. F81232_ID,
  51. F81534A_SERIES_ID,
  52. F81534A_CTRL_ID,
  53. { } /* Terminating entry */
  54. };
  55. MODULE_DEVICE_TABLE(usb, combined_id_table);
  56. /* Maximum baudrate for F81232 */
  57. #define F81232_MAX_BAUDRATE 1500000
  58. #define F81232_DEF_BAUDRATE 9600
  59. /* USB Control EP parameter */
  60. #define F81232_REGISTER_REQUEST 0xa0
  61. #define F81232_GET_REGISTER 0xc0
  62. #define F81232_SET_REGISTER 0x40
  63. #define F81534A_ACCESS_REG_RETRY 2
  64. #define SERIAL_BASE_ADDRESS 0x0120
  65. #define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
  66. #define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
  67. #define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
  68. #define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
  69. #define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
  70. #define LINE_STATUS_REGISTER (0x05 + SERIAL_BASE_ADDRESS)
  71. #define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
  72. /*
  73. * F81232 Clock registers (106h)
  74. *
  75. * Bit1-0: Clock source selector
  76. * 00: 1.846MHz.
  77. * 01: 18.46MHz.
  78. * 10: 24MHz.
  79. * 11: 14.77MHz.
  80. */
  81. #define F81232_CLK_REGISTER 0x106
  82. #define F81232_CLK_1_846_MHZ 0
  83. #define F81232_CLK_18_46_MHZ BIT(0)
  84. #define F81232_CLK_24_MHZ BIT(1)
  85. #define F81232_CLK_14_77_MHZ (BIT(1) | BIT(0))
  86. #define F81232_CLK_MASK GENMASK(1, 0)
  87. #define F81534A_MODE_REG 0x107
  88. #define F81534A_TRIGGER_MASK GENMASK(3, 2)
  89. #define F81534A_TRIGGER_MULTIPLE_4X BIT(3)
  90. #define F81534A_FIFO_128BYTE (BIT(1) | BIT(0))
  91. /* Serial port self GPIO control, 2bytes [control&output data][input data] */
  92. #define F81534A_GPIO_REG 0x10e
  93. #define F81534A_GPIO_MODE2_DIR BIT(6) /* 1: input, 0: output */
  94. #define F81534A_GPIO_MODE1_DIR BIT(5)
  95. #define F81534A_GPIO_MODE0_DIR BIT(4)
  96. #define F81534A_GPIO_MODE2_OUTPUT BIT(2)
  97. #define F81534A_GPIO_MODE1_OUTPUT BIT(1)
  98. #define F81534A_GPIO_MODE0_OUTPUT BIT(0)
  99. #define F81534A_CTRL_CMD_ENABLE_PORT 0x116
  100. struct f81232_private {
  101. struct mutex lock;
  102. u8 modem_control;
  103. u8 modem_status;
  104. u8 shadow_lcr;
  105. speed_t baud_base;
  106. struct work_struct lsr_work;
  107. struct work_struct interrupt_work;
  108. struct usb_serial_port *port;
  109. };
  110. static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
  111. static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
  112. F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
  113. static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
  114. {
  115. return DIV_ROUND_CLOSEST(clockrate, baudrate);
  116. }
  117. static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
  118. {
  119. int status;
  120. struct usb_device *dev = port->serial->dev;
  121. status = usb_control_msg_recv(dev,
  122. 0,
  123. F81232_REGISTER_REQUEST,
  124. F81232_GET_REGISTER,
  125. reg,
  126. 0,
  127. val,
  128. sizeof(*val),
  129. USB_CTRL_GET_TIMEOUT,
  130. GFP_KERNEL);
  131. if (status) {
  132. dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
  133. status = usb_translate_errors(status);
  134. }
  135. return status;
  136. }
  137. static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
  138. {
  139. int status;
  140. struct usb_device *dev = port->serial->dev;
  141. status = usb_control_msg_send(dev,
  142. 0,
  143. F81232_REGISTER_REQUEST,
  144. F81232_SET_REGISTER,
  145. reg,
  146. 0,
  147. &val,
  148. sizeof(val),
  149. USB_CTRL_SET_TIMEOUT,
  150. GFP_KERNEL);
  151. if (status) {
  152. dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
  153. status = usb_translate_errors(status);
  154. }
  155. return status;
  156. }
  157. static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
  158. u8 mask, u8 val)
  159. {
  160. int status;
  161. u8 tmp;
  162. status = f81232_get_register(port, reg, &tmp);
  163. if (status)
  164. return status;
  165. tmp = (tmp & ~mask) | (val & mask);
  166. return f81232_set_register(port, reg, tmp);
  167. }
  168. static void f81232_read_msr(struct usb_serial_port *port)
  169. {
  170. int status;
  171. u8 current_msr;
  172. struct tty_struct *tty;
  173. struct f81232_private *priv = usb_get_serial_port_data(port);
  174. mutex_lock(&priv->lock);
  175. status = f81232_get_register(port, MODEM_STATUS_REGISTER,
  176. &current_msr);
  177. if (status) {
  178. dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
  179. mutex_unlock(&priv->lock);
  180. return;
  181. }
  182. if (!(current_msr & UART_MSR_ANY_DELTA)) {
  183. mutex_unlock(&priv->lock);
  184. return;
  185. }
  186. priv->modem_status = current_msr;
  187. if (current_msr & UART_MSR_DCTS)
  188. port->icount.cts++;
  189. if (current_msr & UART_MSR_DDSR)
  190. port->icount.dsr++;
  191. if (current_msr & UART_MSR_TERI)
  192. port->icount.rng++;
  193. if (current_msr & UART_MSR_DDCD) {
  194. port->icount.dcd++;
  195. tty = tty_port_tty_get(&port->port);
  196. if (tty) {
  197. usb_serial_handle_dcd_change(port, tty,
  198. current_msr & UART_MSR_DCD);
  199. tty_kref_put(tty);
  200. }
  201. }
  202. wake_up_interruptible(&port->port.delta_msr_wait);
  203. mutex_unlock(&priv->lock);
  204. }
  205. static int f81232_set_mctrl(struct usb_serial_port *port,
  206. unsigned int set, unsigned int clear)
  207. {
  208. u8 val;
  209. int status;
  210. struct f81232_private *priv = usb_get_serial_port_data(port);
  211. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
  212. return 0; /* no change */
  213. /* 'set' takes precedence over 'clear' */
  214. clear &= ~set;
  215. /* force enable interrupt with OUT2 */
  216. mutex_lock(&priv->lock);
  217. val = UART_MCR_OUT2 | priv->modem_control;
  218. if (clear & TIOCM_DTR)
  219. val &= ~UART_MCR_DTR;
  220. if (clear & TIOCM_RTS)
  221. val &= ~UART_MCR_RTS;
  222. if (set & TIOCM_DTR)
  223. val |= UART_MCR_DTR;
  224. if (set & TIOCM_RTS)
  225. val |= UART_MCR_RTS;
  226. dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
  227. val, priv->modem_control);
  228. status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
  229. if (status) {
  230. dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
  231. mutex_unlock(&priv->lock);
  232. return status;
  233. }
  234. priv->modem_control = val;
  235. mutex_unlock(&priv->lock);
  236. return 0;
  237. }
  238. static void f81232_update_line_status(struct usb_serial_port *port,
  239. unsigned char *data,
  240. size_t actual_length)
  241. {
  242. struct f81232_private *priv = usb_get_serial_port_data(port);
  243. if (!actual_length)
  244. return;
  245. switch (data[0] & 0x07) {
  246. case 0x00: /* msr change */
  247. dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
  248. schedule_work(&priv->interrupt_work);
  249. break;
  250. case 0x02: /* tx-empty */
  251. break;
  252. case 0x04: /* rx data available */
  253. break;
  254. case 0x06: /* lsr change */
  255. /* we can forget it. the LSR will read from bulk-in */
  256. dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
  257. break;
  258. }
  259. }
  260. static void f81232_read_int_callback(struct urb *urb)
  261. {
  262. struct usb_serial_port *port = urb->context;
  263. unsigned char *data = urb->transfer_buffer;
  264. unsigned int actual_length = urb->actual_length;
  265. int status = urb->status;
  266. int retval;
  267. switch (status) {
  268. case 0:
  269. /* success */
  270. break;
  271. case -ECONNRESET:
  272. case -ENOENT:
  273. case -ESHUTDOWN:
  274. /* this urb is terminated, clean up */
  275. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  276. __func__, status);
  277. return;
  278. default:
  279. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  280. __func__, status);
  281. goto exit;
  282. }
  283. usb_serial_debug_data(&port->dev, __func__,
  284. urb->actual_length, urb->transfer_buffer);
  285. f81232_update_line_status(port, data, actual_length);
  286. exit:
  287. retval = usb_submit_urb(urb, GFP_ATOMIC);
  288. if (retval)
  289. dev_err(&urb->dev->dev,
  290. "%s - usb_submit_urb failed with result %d\n",
  291. __func__, retval);
  292. }
  293. static char f81232_handle_lsr(struct usb_serial_port *port, u8 lsr)
  294. {
  295. struct f81232_private *priv = usb_get_serial_port_data(port);
  296. char tty_flag = TTY_NORMAL;
  297. if (!(lsr & UART_LSR_BRK_ERROR_BITS))
  298. return tty_flag;
  299. if (lsr & UART_LSR_BI) {
  300. tty_flag = TTY_BREAK;
  301. port->icount.brk++;
  302. usb_serial_handle_break(port);
  303. } else if (lsr & UART_LSR_PE) {
  304. tty_flag = TTY_PARITY;
  305. port->icount.parity++;
  306. } else if (lsr & UART_LSR_FE) {
  307. tty_flag = TTY_FRAME;
  308. port->icount.frame++;
  309. }
  310. if (lsr & UART_LSR_OE) {
  311. port->icount.overrun++;
  312. schedule_work(&priv->lsr_work);
  313. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  314. }
  315. return tty_flag;
  316. }
  317. static void f81232_process_read_urb(struct urb *urb)
  318. {
  319. struct usb_serial_port *port = urb->context;
  320. unsigned char *data = urb->transfer_buffer;
  321. char tty_flag;
  322. unsigned int i;
  323. u8 lsr;
  324. /*
  325. * When opening the port we get a 1-byte packet with the current LSR,
  326. * which we discard.
  327. */
  328. if ((urb->actual_length < 2) || (urb->actual_length % 2))
  329. return;
  330. /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
  331. for (i = 0; i < urb->actual_length; i += 2) {
  332. lsr = data[i];
  333. tty_flag = f81232_handle_lsr(port, lsr);
  334. if (port->sysrq) {
  335. if (usb_serial_handle_sysrq_char(port, data[i + 1]))
  336. continue;
  337. }
  338. tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
  339. }
  340. tty_flip_buffer_push(&port->port);
  341. }
  342. static void f81534a_process_read_urb(struct urb *urb)
  343. {
  344. struct usb_serial_port *port = urb->context;
  345. unsigned char *data = urb->transfer_buffer;
  346. char tty_flag;
  347. unsigned int i;
  348. u8 lsr;
  349. u8 len;
  350. if (urb->actual_length < 3) {
  351. dev_err(&port->dev, "short message received: %d\n",
  352. urb->actual_length);
  353. return;
  354. }
  355. len = data[0];
  356. if (len != urb->actual_length) {
  357. dev_err(&port->dev, "malformed message received: %d (%d)\n",
  358. urb->actual_length, len);
  359. return;
  360. }
  361. /* bulk-in data: [LEN][Data.....][LSR] */
  362. lsr = data[len - 1];
  363. tty_flag = f81232_handle_lsr(port, lsr);
  364. if (port->sysrq) {
  365. for (i = 1; i < len - 1; ++i) {
  366. if (!usb_serial_handle_sysrq_char(port, data[i])) {
  367. tty_insert_flip_char(&port->port, data[i],
  368. tty_flag);
  369. }
  370. }
  371. } else {
  372. tty_insert_flip_string_fixed_flag(&port->port, &data[1],
  373. tty_flag, len - 2);
  374. }
  375. tty_flip_buffer_push(&port->port);
  376. }
  377. static int f81232_break_ctl(struct tty_struct *tty, int break_state)
  378. {
  379. struct usb_serial_port *port = tty->driver_data;
  380. struct f81232_private *priv = usb_get_serial_port_data(port);
  381. int status;
  382. mutex_lock(&priv->lock);
  383. if (break_state)
  384. priv->shadow_lcr |= UART_LCR_SBC;
  385. else
  386. priv->shadow_lcr &= ~UART_LCR_SBC;
  387. status = f81232_set_register(port, LINE_CONTROL_REGISTER,
  388. priv->shadow_lcr);
  389. if (status)
  390. dev_err(&port->dev, "set break failed: %d\n", status);
  391. mutex_unlock(&priv->lock);
  392. return status;
  393. }
  394. static int f81232_find_clk(speed_t baudrate)
  395. {
  396. int idx;
  397. for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
  398. if (baudrate <= baudrate_table[idx] &&
  399. baudrate_table[idx] % baudrate == 0)
  400. return idx;
  401. }
  402. return -EINVAL;
  403. }
  404. static void f81232_set_baudrate(struct tty_struct *tty,
  405. struct usb_serial_port *port, speed_t baudrate,
  406. speed_t old_baudrate)
  407. {
  408. struct f81232_private *priv = usb_get_serial_port_data(port);
  409. u8 lcr;
  410. int divisor;
  411. int status = 0;
  412. int i;
  413. int idx;
  414. speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
  415. for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
  416. baudrate = baud_list[i];
  417. if (baudrate == 0) {
  418. tty_encode_baud_rate(tty, 0, 0);
  419. return;
  420. }
  421. idx = f81232_find_clk(baudrate);
  422. if (idx >= 0) {
  423. tty_encode_baud_rate(tty, baudrate, baudrate);
  424. break;
  425. }
  426. }
  427. if (idx < 0)
  428. return;
  429. priv->baud_base = baudrate_table[idx];
  430. divisor = calc_baud_divisor(baudrate, priv->baud_base);
  431. status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
  432. F81232_CLK_MASK, clock_table[idx]);
  433. if (status) {
  434. dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
  435. __func__, status);
  436. return;
  437. }
  438. status = f81232_get_register(port, LINE_CONTROL_REGISTER,
  439. &lcr); /* get LCR */
  440. if (status) {
  441. dev_err(&port->dev, "%s failed to get LCR: %d\n",
  442. __func__, status);
  443. return;
  444. }
  445. status = f81232_set_register(port, LINE_CONTROL_REGISTER,
  446. lcr | UART_LCR_DLAB); /* Enable DLAB */
  447. if (status) {
  448. dev_err(&port->dev, "%s failed to set DLAB: %d\n",
  449. __func__, status);
  450. return;
  451. }
  452. status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
  453. divisor & 0x00ff); /* low */
  454. if (status) {
  455. dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
  456. __func__, status);
  457. goto reapply_lcr;
  458. }
  459. status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
  460. (divisor & 0xff00) >> 8); /* high */
  461. if (status) {
  462. dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
  463. __func__, status);
  464. }
  465. reapply_lcr:
  466. status = f81232_set_register(port, LINE_CONTROL_REGISTER,
  467. lcr & ~UART_LCR_DLAB);
  468. if (status) {
  469. dev_err(&port->dev, "%s failed to set DLAB: %d\n",
  470. __func__, status);
  471. }
  472. }
  473. static int f81232_port_enable(struct usb_serial_port *port)
  474. {
  475. u8 val;
  476. int status;
  477. /* fifo on, trigger8, clear TX/RX*/
  478. val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
  479. UART_FCR_CLEAR_XMIT;
  480. status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
  481. if (status) {
  482. dev_err(&port->dev, "%s failed to set FCR: %d\n",
  483. __func__, status);
  484. return status;
  485. }
  486. /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
  487. status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
  488. UART_IER_MSI);
  489. if (status) {
  490. dev_err(&port->dev, "%s failed to set IER: %d\n",
  491. __func__, status);
  492. return status;
  493. }
  494. return 0;
  495. }
  496. static int f81232_port_disable(struct usb_serial_port *port)
  497. {
  498. int status;
  499. status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
  500. if (status) {
  501. dev_err(&port->dev, "%s failed to set IER: %d\n",
  502. __func__, status);
  503. return status;
  504. }
  505. return 0;
  506. }
  507. static void f81232_set_termios(struct tty_struct *tty,
  508. struct usb_serial_port *port,
  509. const struct ktermios *old_termios)
  510. {
  511. struct f81232_private *priv = usb_get_serial_port_data(port);
  512. u8 new_lcr = 0;
  513. int status = 0;
  514. speed_t baudrate;
  515. speed_t old_baud;
  516. /* Don't change anything if nothing has changed */
  517. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  518. return;
  519. if (C_BAUD(tty) == B0)
  520. f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
  521. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  522. f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
  523. baudrate = tty_get_baud_rate(tty);
  524. if (baudrate > 0) {
  525. if (old_termios)
  526. old_baud = tty_termios_baud_rate(old_termios);
  527. else
  528. old_baud = F81232_DEF_BAUDRATE;
  529. f81232_set_baudrate(tty, port, baudrate, old_baud);
  530. }
  531. if (C_PARENB(tty)) {
  532. new_lcr |= UART_LCR_PARITY;
  533. if (!C_PARODD(tty))
  534. new_lcr |= UART_LCR_EPAR;
  535. if (C_CMSPAR(tty))
  536. new_lcr |= UART_LCR_SPAR;
  537. }
  538. if (C_CSTOPB(tty))
  539. new_lcr |= UART_LCR_STOP;
  540. new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
  541. mutex_lock(&priv->lock);
  542. new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
  543. status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
  544. if (status) {
  545. dev_err(&port->dev, "%s failed to set LCR: %d\n",
  546. __func__, status);
  547. }
  548. priv->shadow_lcr = new_lcr;
  549. mutex_unlock(&priv->lock);
  550. }
  551. static int f81232_tiocmget(struct tty_struct *tty)
  552. {
  553. int r;
  554. struct usb_serial_port *port = tty->driver_data;
  555. struct f81232_private *port_priv = usb_get_serial_port_data(port);
  556. u8 mcr, msr;
  557. /* force get current MSR changed state */
  558. f81232_read_msr(port);
  559. mutex_lock(&port_priv->lock);
  560. mcr = port_priv->modem_control;
  561. msr = port_priv->modem_status;
  562. mutex_unlock(&port_priv->lock);
  563. r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
  564. (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
  565. (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
  566. (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
  567. (msr & UART_MSR_RI ? TIOCM_RI : 0) |
  568. (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
  569. return r;
  570. }
  571. static int f81232_tiocmset(struct tty_struct *tty,
  572. unsigned int set, unsigned int clear)
  573. {
  574. struct usb_serial_port *port = tty->driver_data;
  575. return f81232_set_mctrl(port, set, clear);
  576. }
  577. static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
  578. {
  579. int result;
  580. result = f81232_port_enable(port);
  581. if (result)
  582. return result;
  583. /* Setup termios */
  584. if (tty)
  585. f81232_set_termios(tty, port, NULL);
  586. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  587. if (result) {
  588. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  589. " error %d\n", __func__, result);
  590. return result;
  591. }
  592. result = usb_serial_generic_open(tty, port);
  593. if (result) {
  594. usb_kill_urb(port->interrupt_in_urb);
  595. return result;
  596. }
  597. return 0;
  598. }
  599. static int f81534a_open(struct tty_struct *tty, struct usb_serial_port *port)
  600. {
  601. int status;
  602. u8 mask;
  603. u8 val;
  604. val = F81534A_TRIGGER_MULTIPLE_4X | F81534A_FIFO_128BYTE;
  605. mask = F81534A_TRIGGER_MASK | F81534A_FIFO_128BYTE;
  606. status = f81232_set_mask_register(port, F81534A_MODE_REG, mask, val);
  607. if (status) {
  608. dev_err(&port->dev, "failed to set MODE_REG: %d\n", status);
  609. return status;
  610. }
  611. return f81232_open(tty, port);
  612. }
  613. static void f81232_close(struct usb_serial_port *port)
  614. {
  615. struct f81232_private *port_priv = usb_get_serial_port_data(port);
  616. f81232_port_disable(port);
  617. usb_serial_generic_close(port);
  618. usb_kill_urb(port->interrupt_in_urb);
  619. flush_work(&port_priv->interrupt_work);
  620. flush_work(&port_priv->lsr_work);
  621. }
  622. static void f81232_dtr_rts(struct usb_serial_port *port, int on)
  623. {
  624. if (on)
  625. f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
  626. else
  627. f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
  628. }
  629. static bool f81232_tx_empty(struct usb_serial_port *port)
  630. {
  631. int status;
  632. u8 tmp;
  633. status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
  634. if (!status) {
  635. if ((tmp & UART_LSR_TEMT) != UART_LSR_TEMT)
  636. return false;
  637. }
  638. return true;
  639. }
  640. static int f81232_carrier_raised(struct usb_serial_port *port)
  641. {
  642. u8 msr;
  643. struct f81232_private *priv = usb_get_serial_port_data(port);
  644. mutex_lock(&priv->lock);
  645. msr = priv->modem_status;
  646. mutex_unlock(&priv->lock);
  647. if (msr & UART_MSR_DCD)
  648. return 1;
  649. return 0;
  650. }
  651. static void f81232_get_serial(struct tty_struct *tty, struct serial_struct *ss)
  652. {
  653. struct usb_serial_port *port = tty->driver_data;
  654. struct f81232_private *priv = usb_get_serial_port_data(port);
  655. ss->baud_base = priv->baud_base;
  656. }
  657. static void f81232_interrupt_work(struct work_struct *work)
  658. {
  659. struct f81232_private *priv =
  660. container_of(work, struct f81232_private, interrupt_work);
  661. f81232_read_msr(priv->port);
  662. }
  663. static void f81232_lsr_worker(struct work_struct *work)
  664. {
  665. struct f81232_private *priv;
  666. struct usb_serial_port *port;
  667. int status;
  668. u8 tmp;
  669. priv = container_of(work, struct f81232_private, lsr_work);
  670. port = priv->port;
  671. status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
  672. if (status)
  673. dev_warn(&port->dev, "read LSR failed: %d\n", status);
  674. }
  675. static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
  676. u16 size, void *val)
  677. {
  678. struct usb_device *dev = interface_to_usbdev(intf);
  679. int retry = F81534A_ACCESS_REG_RETRY;
  680. int status;
  681. while (retry--) {
  682. status = usb_control_msg_send(dev,
  683. 0,
  684. F81232_REGISTER_REQUEST,
  685. F81232_SET_REGISTER,
  686. reg,
  687. 0,
  688. val,
  689. size,
  690. USB_CTRL_SET_TIMEOUT,
  691. GFP_KERNEL);
  692. if (status) {
  693. status = usb_translate_errors(status);
  694. if (status == -EIO)
  695. continue;
  696. }
  697. break;
  698. }
  699. if (status) {
  700. dev_err(&intf->dev, "failed to set register 0x%x: %d\n",
  701. reg, status);
  702. }
  703. return status;
  704. }
  705. static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
  706. {
  707. unsigned char enable[2] = {0};
  708. int status;
  709. /*
  710. * Enable all available serial ports, define as following:
  711. * bit 15 : Reset behavior (when HUB got soft reset)
  712. * 0: maintain all serial port enabled state.
  713. * 1: disable all serial port.
  714. * bit 0~11 : Serial port enable bit.
  715. */
  716. if (en) {
  717. enable[0] = 0xff;
  718. enable[1] = 0x8f;
  719. }
  720. status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
  721. sizeof(enable), enable);
  722. if (status)
  723. dev_err(&intf->dev, "failed to enable ports: %d\n", status);
  724. return status;
  725. }
  726. static int f81534a_ctrl_probe(struct usb_interface *intf,
  727. const struct usb_device_id *id)
  728. {
  729. return f81534a_ctrl_enable_all_ports(intf, true);
  730. }
  731. static void f81534a_ctrl_disconnect(struct usb_interface *intf)
  732. {
  733. f81534a_ctrl_enable_all_ports(intf, false);
  734. }
  735. static int f81534a_ctrl_resume(struct usb_interface *intf)
  736. {
  737. return f81534a_ctrl_enable_all_ports(intf, true);
  738. }
  739. static int f81232_port_probe(struct usb_serial_port *port)
  740. {
  741. struct f81232_private *priv;
  742. priv = devm_kzalloc(&port->dev, sizeof(*priv), GFP_KERNEL);
  743. if (!priv)
  744. return -ENOMEM;
  745. mutex_init(&priv->lock);
  746. INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
  747. INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
  748. usb_set_serial_port_data(port, priv);
  749. priv->port = port;
  750. return 0;
  751. }
  752. static int f81534a_port_probe(struct usb_serial_port *port)
  753. {
  754. int status;
  755. /* tri-state with pull-high, default RS232 Mode */
  756. status = f81232_set_register(port, F81534A_GPIO_REG,
  757. F81534A_GPIO_MODE2_DIR);
  758. if (status)
  759. return status;
  760. return f81232_port_probe(port);
  761. }
  762. static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
  763. {
  764. struct usb_serial_port *port = serial->port[0];
  765. struct f81232_private *port_priv = usb_get_serial_port_data(port);
  766. int i;
  767. for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
  768. usb_kill_urb(port->read_urbs[i]);
  769. usb_kill_urb(port->interrupt_in_urb);
  770. if (port_priv) {
  771. flush_work(&port_priv->interrupt_work);
  772. flush_work(&port_priv->lsr_work);
  773. }
  774. return 0;
  775. }
  776. static int f81232_resume(struct usb_serial *serial)
  777. {
  778. struct usb_serial_port *port = serial->port[0];
  779. int result;
  780. if (tty_port_initialized(&port->port)) {
  781. result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  782. if (result) {
  783. dev_err(&port->dev, "submit interrupt urb failed: %d\n",
  784. result);
  785. return result;
  786. }
  787. }
  788. return usb_serial_generic_resume(serial);
  789. }
  790. static struct usb_serial_driver f81232_device = {
  791. .driver = {
  792. .name = "f81232",
  793. },
  794. .id_table = f81232_id_table,
  795. .num_ports = 1,
  796. .bulk_in_size = 256,
  797. .bulk_out_size = 256,
  798. .open = f81232_open,
  799. .close = f81232_close,
  800. .dtr_rts = f81232_dtr_rts,
  801. .carrier_raised = f81232_carrier_raised,
  802. .get_serial = f81232_get_serial,
  803. .break_ctl = f81232_break_ctl,
  804. .set_termios = f81232_set_termios,
  805. .tiocmget = f81232_tiocmget,
  806. .tiocmset = f81232_tiocmset,
  807. .tiocmiwait = usb_serial_generic_tiocmiwait,
  808. .tx_empty = f81232_tx_empty,
  809. .process_read_urb = f81232_process_read_urb,
  810. .read_int_callback = f81232_read_int_callback,
  811. .port_probe = f81232_port_probe,
  812. .suspend = f81232_suspend,
  813. .resume = f81232_resume,
  814. };
  815. static struct usb_serial_driver f81534a_device = {
  816. .driver = {
  817. .name = "f81534a",
  818. },
  819. .id_table = f81534a_id_table,
  820. .num_ports = 1,
  821. .open = f81534a_open,
  822. .close = f81232_close,
  823. .dtr_rts = f81232_dtr_rts,
  824. .carrier_raised = f81232_carrier_raised,
  825. .get_serial = f81232_get_serial,
  826. .break_ctl = f81232_break_ctl,
  827. .set_termios = f81232_set_termios,
  828. .tiocmget = f81232_tiocmget,
  829. .tiocmset = f81232_tiocmset,
  830. .tiocmiwait = usb_serial_generic_tiocmiwait,
  831. .tx_empty = f81232_tx_empty,
  832. .process_read_urb = f81534a_process_read_urb,
  833. .read_int_callback = f81232_read_int_callback,
  834. .port_probe = f81534a_port_probe,
  835. .suspend = f81232_suspend,
  836. .resume = f81232_resume,
  837. };
  838. static struct usb_serial_driver * const serial_drivers[] = {
  839. &f81232_device,
  840. &f81534a_device,
  841. NULL,
  842. };
  843. static struct usb_driver f81534a_ctrl_driver = {
  844. .name = "f81534a_ctrl",
  845. .id_table = f81534a_ctrl_id_table,
  846. .probe = f81534a_ctrl_probe,
  847. .disconnect = f81534a_ctrl_disconnect,
  848. .resume = f81534a_ctrl_resume,
  849. };
  850. static int __init f81232_init(void)
  851. {
  852. int status;
  853. status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
  854. KBUILD_MODNAME);
  855. if (status)
  856. return status;
  857. status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
  858. combined_id_table);
  859. if (status) {
  860. usb_deregister(&f81534a_ctrl_driver);
  861. return status;
  862. }
  863. return 0;
  864. }
  865. static void __exit f81232_exit(void)
  866. {
  867. usb_serial_deregister_drivers(serial_drivers);
  868. usb_deregister(&f81534a_ctrl_driver);
  869. }
  870. module_init(f81232_init);
  871. module_exit(f81232_exit);
  872. MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
  873. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
  874. MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
  875. MODULE_LICENSE("GPL v2");