f81534.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * F81532/F81534 USB to Serial Ports Bridge
  4. *
  5. * F81532 => 2 Serial Ports
  6. * F81534 => 4 Serial Ports
  7. *
  8. * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
  9. * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
  10. * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
  11. *
  12. * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
  13. * for all serial port TX and 1 endpoint bulk-in for all serial port read in
  14. * (Read Data/MSR/LSR).
  15. *
  16. * Write URB is fixed with 512bytes, per serial port used 128Bytes.
  17. * It can be described by f81534_prepare_write_buffer()
  18. *
  19. * Read URB is 512Bytes max, per serial port used 128Bytes.
  20. * It can be described by f81534_process_read_urb() and maybe received with
  21. * 128x1,2,3,4 bytes.
  22. *
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/module.h>
  31. #include <linux/uaccess.h>
  32. /* Serial Port register Address */
  33. #define F81534_UART_BASE_ADDRESS 0x1200
  34. #define F81534_UART_OFFSET 0x10
  35. #define F81534_DIVISOR_LSB_REG (0x00 + F81534_UART_BASE_ADDRESS)
  36. #define F81534_DIVISOR_MSB_REG (0x01 + F81534_UART_BASE_ADDRESS)
  37. #define F81534_INTERRUPT_ENABLE_REG (0x01 + F81534_UART_BASE_ADDRESS)
  38. #define F81534_FIFO_CONTROL_REG (0x02 + F81534_UART_BASE_ADDRESS)
  39. #define F81534_LINE_CONTROL_REG (0x03 + F81534_UART_BASE_ADDRESS)
  40. #define F81534_MODEM_CONTROL_REG (0x04 + F81534_UART_BASE_ADDRESS)
  41. #define F81534_LINE_STATUS_REG (0x05 + F81534_UART_BASE_ADDRESS)
  42. #define F81534_MODEM_STATUS_REG (0x06 + F81534_UART_BASE_ADDRESS)
  43. #define F81534_CLOCK_REG (0x08 + F81534_UART_BASE_ADDRESS)
  44. #define F81534_CONFIG1_REG (0x09 + F81534_UART_BASE_ADDRESS)
  45. #define F81534_DEF_CONF_ADDRESS_START 0x3000
  46. #define F81534_DEF_CONF_SIZE 12
  47. #define F81534_CUSTOM_ADDRESS_START 0x2f00
  48. #define F81534_CUSTOM_DATA_SIZE 0x10
  49. #define F81534_CUSTOM_NO_CUSTOM_DATA 0xff
  50. #define F81534_CUSTOM_VALID_TOKEN 0xf0
  51. #define F81534_CONF_OFFSET 1
  52. #define F81534_CONF_INIT_GPIO_OFFSET 4
  53. #define F81534_CONF_WORK_GPIO_OFFSET 8
  54. #define F81534_CONF_GPIO_SHUTDOWN 7
  55. #define F81534_CONF_GPIO_RS232 1
  56. #define F81534_MAX_DATA_BLOCK 64
  57. #define F81534_MAX_BUS_RETRY 20
  58. /* Default URB timeout for USB operations */
  59. #define F81534_USB_MAX_RETRY 10
  60. #define F81534_USB_TIMEOUT 2000
  61. #define F81534_SET_GET_REGISTER 0xA0
  62. #define F81534_NUM_PORT 4
  63. #define F81534_UNUSED_PORT 0xff
  64. #define F81534_WRITE_BUFFER_SIZE 512
  65. #define DRIVER_DESC "Fintek F81532/F81534"
  66. #define FINTEK_VENDOR_ID_1 0x1934
  67. #define FINTEK_VENDOR_ID_2 0x2C42
  68. #define FINTEK_DEVICE_ID 0x1202
  69. #define F81534_MAX_TX_SIZE 124
  70. #define F81534_MAX_RX_SIZE 124
  71. #define F81534_RECEIVE_BLOCK_SIZE 128
  72. #define F81534_MAX_RECEIVE_BLOCK_SIZE 512
  73. #define F81534_TOKEN_RECEIVE 0x01
  74. #define F81534_TOKEN_WRITE 0x02
  75. #define F81534_TOKEN_TX_EMPTY 0x03
  76. #define F81534_TOKEN_MSR_CHANGE 0x04
  77. /*
  78. * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
  79. * idle if we performed any command.
  80. *
  81. * SPI Bus status register: F81534_BUS_REG_STATUS
  82. * Bit 0/1 : BUSY
  83. * Bit 2 : IDLE
  84. */
  85. #define F81534_BUS_BUSY (BIT(0) | BIT(1))
  86. #define F81534_BUS_IDLE BIT(2)
  87. #define F81534_BUS_READ_DATA 0x1004
  88. #define F81534_BUS_REG_STATUS 0x1003
  89. #define F81534_BUS_REG_START 0x1002
  90. #define F81534_BUS_REG_END 0x1001
  91. #define F81534_CMD_READ 0x03
  92. #define F81534_DEFAULT_BAUD_RATE 9600
  93. #define F81534_PORT_CONF_RS232 0
  94. #define F81534_PORT_CONF_RS485 BIT(0)
  95. #define F81534_PORT_CONF_RS485_INVERT (BIT(0) | BIT(1))
  96. #define F81534_PORT_CONF_MODE_MASK GENMASK(1, 0)
  97. #define F81534_PORT_CONF_DISABLE_PORT BIT(3)
  98. #define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
  99. #define F81534_PORT_UNAVAILABLE \
  100. (F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
  101. #define F81534_1X_RXTRIGGER 0xc3
  102. #define F81534_8X_RXTRIGGER 0xcf
  103. /*
  104. * F81532/534 Clock registers (offset +08h)
  105. *
  106. * Bit0: UART Enable (always on)
  107. * Bit2-1: Clock source selector
  108. * 00: 1.846MHz.
  109. * 01: 18.46MHz.
  110. * 10: 24MHz.
  111. * 11: 14.77MHz.
  112. * Bit4: Auto direction(RTS) control (RTS pin Low when TX)
  113. * Bit5: Invert direction(RTS) when Bit4 enabled (RTS pin high when TX)
  114. */
  115. #define F81534_UART_EN BIT(0)
  116. #define F81534_CLK_1_846_MHZ 0
  117. #define F81534_CLK_18_46_MHZ BIT(1)
  118. #define F81534_CLK_24_MHZ BIT(2)
  119. #define F81534_CLK_14_77_MHZ (BIT(1) | BIT(2))
  120. #define F81534_CLK_MASK GENMASK(2, 1)
  121. #define F81534_CLK_TX_DELAY_1BIT BIT(3)
  122. #define F81534_CLK_RS485_MODE BIT(4)
  123. #define F81534_CLK_RS485_INVERT BIT(5)
  124. static const struct usb_device_id f81534_id_table[] = {
  125. { USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
  126. { USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
  127. {} /* Terminating entry */
  128. };
  129. #define F81534_TX_EMPTY_BIT 0
  130. struct f81534_serial_private {
  131. u8 conf_data[F81534_DEF_CONF_SIZE];
  132. int tty_idx[F81534_NUM_PORT];
  133. u8 setting_idx;
  134. int opened_port;
  135. struct mutex urb_mutex;
  136. };
  137. struct f81534_port_private {
  138. struct mutex mcr_mutex;
  139. struct mutex lcr_mutex;
  140. struct work_struct lsr_work;
  141. struct usb_serial_port *port;
  142. unsigned long tx_empty;
  143. spinlock_t msr_lock;
  144. u32 baud_base;
  145. u8 shadow_mcr;
  146. u8 shadow_lcr;
  147. u8 shadow_msr;
  148. u8 shadow_clk;
  149. u8 phy_num;
  150. };
  151. struct f81534_pin_data {
  152. const u16 reg_addr;
  153. const u8 reg_mask;
  154. };
  155. struct f81534_port_out_pin {
  156. struct f81534_pin_data pin[3];
  157. };
  158. /* Pin output value for M2/M1/M0(SD) */
  159. static const struct f81534_port_out_pin f81534_port_out_pins[] = {
  160. { { { 0x2ae8, BIT(7) }, { 0x2a90, BIT(5) }, { 0x2a90, BIT(4) } } },
  161. { { { 0x2ae8, BIT(6) }, { 0x2ae8, BIT(0) }, { 0x2ae8, BIT(3) } } },
  162. { { { 0x2a90, BIT(0) }, { 0x2ae8, BIT(2) }, { 0x2a80, BIT(6) } } },
  163. { { { 0x2a90, BIT(3) }, { 0x2a90, BIT(2) }, { 0x2a90, BIT(1) } } },
  164. };
  165. static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
  166. static u8 const clock_table[] = { F81534_CLK_1_846_MHZ, F81534_CLK_14_77_MHZ,
  167. F81534_CLK_18_46_MHZ, F81534_CLK_24_MHZ };
  168. static int f81534_logic_to_phy_port(struct usb_serial *serial,
  169. struct usb_serial_port *port)
  170. {
  171. struct f81534_serial_private *serial_priv =
  172. usb_get_serial_data(port->serial);
  173. int count = 0;
  174. int i;
  175. for (i = 0; i < F81534_NUM_PORT; ++i) {
  176. if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
  177. continue;
  178. if (port->port_number == count)
  179. return i;
  180. ++count;
  181. }
  182. return -ENODEV;
  183. }
  184. static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
  185. {
  186. struct usb_interface *interface = serial->interface;
  187. struct usb_device *dev = serial->dev;
  188. size_t count = F81534_USB_MAX_RETRY;
  189. int status;
  190. u8 *tmp;
  191. tmp = kmalloc(sizeof(u8), GFP_KERNEL);
  192. if (!tmp)
  193. return -ENOMEM;
  194. *tmp = data;
  195. /*
  196. * Our device maybe not reply when heavily loading, We'll retry for
  197. * F81534_USB_MAX_RETRY times.
  198. */
  199. while (count--) {
  200. status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  201. F81534_SET_GET_REGISTER,
  202. USB_TYPE_VENDOR | USB_DIR_OUT,
  203. reg, 0, tmp, sizeof(u8),
  204. F81534_USB_TIMEOUT);
  205. if (status > 0) {
  206. status = 0;
  207. break;
  208. } else if (status == 0) {
  209. status = -EIO;
  210. }
  211. }
  212. if (status < 0) {
  213. dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
  214. __func__, reg, data, status);
  215. }
  216. kfree(tmp);
  217. return status;
  218. }
  219. static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
  220. {
  221. struct usb_interface *interface = serial->interface;
  222. struct usb_device *dev = serial->dev;
  223. size_t count = F81534_USB_MAX_RETRY;
  224. int status;
  225. u8 *tmp;
  226. tmp = kmalloc(sizeof(u8), GFP_KERNEL);
  227. if (!tmp)
  228. return -ENOMEM;
  229. /*
  230. * Our device maybe not reply when heavily loading, We'll retry for
  231. * F81534_USB_MAX_RETRY times.
  232. */
  233. while (count--) {
  234. status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  235. F81534_SET_GET_REGISTER,
  236. USB_TYPE_VENDOR | USB_DIR_IN,
  237. reg, 0, tmp, sizeof(u8),
  238. F81534_USB_TIMEOUT);
  239. if (status > 0) {
  240. status = 0;
  241. break;
  242. } else if (status == 0) {
  243. status = -EIO;
  244. }
  245. }
  246. if (status < 0) {
  247. dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
  248. reg, status);
  249. goto end;
  250. }
  251. *data = *tmp;
  252. end:
  253. kfree(tmp);
  254. return status;
  255. }
  256. static int f81534_set_mask_register(struct usb_serial *serial, u16 reg,
  257. u8 mask, u8 data)
  258. {
  259. int status;
  260. u8 tmp;
  261. status = f81534_get_register(serial, reg, &tmp);
  262. if (status)
  263. return status;
  264. tmp &= ~mask;
  265. tmp |= (mask & data);
  266. return f81534_set_register(serial, reg, tmp);
  267. }
  268. static int f81534_set_phy_port_register(struct usb_serial *serial, int phy,
  269. u16 reg, u8 data)
  270. {
  271. return f81534_set_register(serial, reg + F81534_UART_OFFSET * phy,
  272. data);
  273. }
  274. static int f81534_get_phy_port_register(struct usb_serial *serial, int phy,
  275. u16 reg, u8 *data)
  276. {
  277. return f81534_get_register(serial, reg + F81534_UART_OFFSET * phy,
  278. data);
  279. }
  280. static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
  281. u8 data)
  282. {
  283. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  284. return f81534_set_register(port->serial,
  285. reg + port_priv->phy_num * F81534_UART_OFFSET, data);
  286. }
  287. static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
  288. u8 *data)
  289. {
  290. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  291. return f81534_get_register(port->serial,
  292. reg + port_priv->phy_num * F81534_UART_OFFSET, data);
  293. }
  294. /*
  295. * If we try to access the internal flash via SPI bus, we should check the bus
  296. * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
  297. */
  298. static int f81534_wait_for_spi_idle(struct usb_serial *serial)
  299. {
  300. size_t count = F81534_MAX_BUS_RETRY;
  301. u8 tmp;
  302. int status;
  303. do {
  304. status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
  305. &tmp);
  306. if (status)
  307. return status;
  308. if (tmp & F81534_BUS_BUSY)
  309. continue;
  310. if (tmp & F81534_BUS_IDLE)
  311. break;
  312. } while (--count);
  313. if (!count) {
  314. dev_err(&serial->interface->dev,
  315. "%s: timed out waiting for idle SPI bus\n",
  316. __func__);
  317. return -EIO;
  318. }
  319. return f81534_set_register(serial, F81534_BUS_REG_STATUS,
  320. tmp & ~F81534_BUS_IDLE);
  321. }
  322. static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
  323. u8 *data)
  324. {
  325. int status;
  326. status = f81534_get_register(serial, reg, data);
  327. if (status)
  328. return status;
  329. return f81534_wait_for_spi_idle(serial);
  330. }
  331. static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
  332. {
  333. int status;
  334. status = f81534_set_register(serial, reg, data);
  335. if (status)
  336. return status;
  337. return f81534_wait_for_spi_idle(serial);
  338. }
  339. static int f81534_read_flash(struct usb_serial *serial, u32 address,
  340. size_t size, u8 *buf)
  341. {
  342. u8 tmp_buf[F81534_MAX_DATA_BLOCK];
  343. size_t block = 0;
  344. size_t read_size;
  345. size_t count;
  346. int status;
  347. int offset;
  348. u16 reg_tmp;
  349. status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
  350. F81534_CMD_READ);
  351. if (status)
  352. return status;
  353. status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
  354. (address >> 16) & 0xff);
  355. if (status)
  356. return status;
  357. status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
  358. (address >> 8) & 0xff);
  359. if (status)
  360. return status;
  361. status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
  362. (address >> 0) & 0xff);
  363. if (status)
  364. return status;
  365. /* Continuous read mode */
  366. do {
  367. read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
  368. for (count = 0; count < read_size; ++count) {
  369. /* To write F81534_BUS_REG_END when final byte */
  370. if (size <= F81534_MAX_DATA_BLOCK &&
  371. read_size == count + 1)
  372. reg_tmp = F81534_BUS_REG_END;
  373. else
  374. reg_tmp = F81534_BUS_REG_START;
  375. /*
  376. * Dummy code, force IC to generate a read pulse, the
  377. * set of value 0xf1 is dont care (any value is ok)
  378. */
  379. status = f81534_set_spi_register(serial, reg_tmp,
  380. 0xf1);
  381. if (status)
  382. return status;
  383. status = f81534_get_spi_register(serial,
  384. F81534_BUS_READ_DATA,
  385. &tmp_buf[count]);
  386. if (status)
  387. return status;
  388. offset = count + block * F81534_MAX_DATA_BLOCK;
  389. buf[offset] = tmp_buf[count];
  390. }
  391. size -= read_size;
  392. ++block;
  393. } while (size);
  394. return 0;
  395. }
  396. static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
  397. {
  398. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  399. int phy_num = port_priv->phy_num;
  400. u8 tx_len;
  401. int i;
  402. /*
  403. * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
  404. * index 0: port phy idx (e.g., 0,1,2,3)
  405. * index 1: only F81534_TOKEN_WRITE
  406. * index 2: serial TX out length
  407. * index 3: fix to 0
  408. * index 4~127: serial out data block
  409. */
  410. for (i = 0; i < F81534_NUM_PORT; ++i) {
  411. buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
  412. buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
  413. buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
  414. buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
  415. }
  416. tx_len = kfifo_out_locked(&port->write_fifo,
  417. &buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
  418. F81534_MAX_TX_SIZE, &port->lock);
  419. buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
  420. }
  421. static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
  422. {
  423. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  424. struct urb *urb;
  425. unsigned long flags;
  426. int result;
  427. /* Check is any data in write_fifo */
  428. spin_lock_irqsave(&port->lock, flags);
  429. if (kfifo_is_empty(&port->write_fifo)) {
  430. spin_unlock_irqrestore(&port->lock, flags);
  431. return 0;
  432. }
  433. spin_unlock_irqrestore(&port->lock, flags);
  434. /* Check H/W is TXEMPTY */
  435. if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
  436. return 0;
  437. urb = port->write_urbs[0];
  438. f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
  439. urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
  440. result = usb_submit_urb(urb, mem_flags);
  441. if (result) {
  442. set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
  443. dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
  444. result);
  445. return result;
  446. }
  447. usb_serial_port_softint(port);
  448. return 0;
  449. }
  450. static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
  451. {
  452. if (!baudrate)
  453. return 0;
  454. /* Round to nearest divisor */
  455. return DIV_ROUND_CLOSEST(clockrate, baudrate);
  456. }
  457. static int f81534_find_clk(u32 baudrate)
  458. {
  459. int idx;
  460. for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
  461. if (baudrate <= baudrate_table[idx] &&
  462. baudrate_table[idx] % baudrate == 0)
  463. return idx;
  464. }
  465. return -EINVAL;
  466. }
  467. static int f81534_set_port_config(struct usb_serial_port *port,
  468. struct tty_struct *tty, u32 baudrate, u32 old_baudrate, u8 lcr)
  469. {
  470. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  471. u32 divisor;
  472. int status;
  473. int i;
  474. int idx;
  475. u8 value;
  476. u32 baud_list[] = {baudrate, old_baudrate, F81534_DEFAULT_BAUD_RATE};
  477. for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
  478. idx = f81534_find_clk(baud_list[i]);
  479. if (idx >= 0) {
  480. baudrate = baud_list[i];
  481. tty_encode_baud_rate(tty, baudrate, baudrate);
  482. break;
  483. }
  484. }
  485. if (idx < 0)
  486. return -EINVAL;
  487. port_priv->baud_base = baudrate_table[idx];
  488. port_priv->shadow_clk &= ~F81534_CLK_MASK;
  489. port_priv->shadow_clk |= clock_table[idx];
  490. status = f81534_set_port_register(port, F81534_CLOCK_REG,
  491. port_priv->shadow_clk);
  492. if (status) {
  493. dev_err(&port->dev, "CLOCK_REG setting failed\n");
  494. return status;
  495. }
  496. if (baudrate <= 1200)
  497. value = F81534_1X_RXTRIGGER; /* 128 FIFO & TL: 1x */
  498. else
  499. value = F81534_8X_RXTRIGGER; /* 128 FIFO & TL: 8x */
  500. status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
  501. if (status) {
  502. dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
  503. return status;
  504. }
  505. if (baudrate <= 1200)
  506. value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
  507. else
  508. value = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO; /* TL: 8 */
  509. status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
  510. value);
  511. if (status) {
  512. dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
  513. return status;
  514. }
  515. divisor = f81534_calc_baud_divisor(baudrate, port_priv->baud_base);
  516. mutex_lock(&port_priv->lcr_mutex);
  517. value = UART_LCR_DLAB;
  518. status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
  519. value);
  520. if (status) {
  521. dev_err(&port->dev, "%s: set LCR failed\n", __func__);
  522. goto out_unlock;
  523. }
  524. value = divisor & 0xff;
  525. status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
  526. if (status) {
  527. dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
  528. goto out_unlock;
  529. }
  530. value = (divisor >> 8) & 0xff;
  531. status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
  532. if (status) {
  533. dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
  534. goto out_unlock;
  535. }
  536. value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
  537. status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
  538. value);
  539. if (status) {
  540. dev_err(&port->dev, "%s: set LCR failed\n", __func__);
  541. goto out_unlock;
  542. }
  543. port_priv->shadow_lcr = value;
  544. out_unlock:
  545. mutex_unlock(&port_priv->lcr_mutex);
  546. return status;
  547. }
  548. static void f81534_break_ctl(struct tty_struct *tty, int break_state)
  549. {
  550. struct usb_serial_port *port = tty->driver_data;
  551. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  552. int status;
  553. mutex_lock(&port_priv->lcr_mutex);
  554. if (break_state)
  555. port_priv->shadow_lcr |= UART_LCR_SBC;
  556. else
  557. port_priv->shadow_lcr &= ~UART_LCR_SBC;
  558. status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
  559. port_priv->shadow_lcr);
  560. if (status)
  561. dev_err(&port->dev, "set break failed: %d\n", status);
  562. mutex_unlock(&port_priv->lcr_mutex);
  563. }
  564. static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
  565. unsigned int clear)
  566. {
  567. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  568. int status;
  569. u8 tmp;
  570. if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
  571. return 0; /* no change */
  572. mutex_lock(&port_priv->mcr_mutex);
  573. /* 'Set' takes precedence over 'Clear' */
  574. clear &= ~set;
  575. /* Always enable UART_MCR_OUT2 */
  576. tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
  577. if (clear & TIOCM_DTR)
  578. tmp &= ~UART_MCR_DTR;
  579. if (clear & TIOCM_RTS)
  580. tmp &= ~UART_MCR_RTS;
  581. if (set & TIOCM_DTR)
  582. tmp |= UART_MCR_DTR;
  583. if (set & TIOCM_RTS)
  584. tmp |= UART_MCR_RTS;
  585. status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
  586. if (status < 0) {
  587. dev_err(&port->dev, "%s: MCR write failed\n", __func__);
  588. mutex_unlock(&port_priv->mcr_mutex);
  589. return status;
  590. }
  591. port_priv->shadow_mcr = tmp;
  592. mutex_unlock(&port_priv->mcr_mutex);
  593. return 0;
  594. }
  595. /*
  596. * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
  597. * for latest configuration index. If nothing found
  598. * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
  599. * F81534_DEF_CONF_ADDRESS_START section.
  600. *
  601. * Due to we only use block0 to save data, so *index should be 0 or
  602. * F81534_CUSTOM_NO_CUSTOM_DATA.
  603. */
  604. static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
  605. {
  606. u8 tmp;
  607. int status;
  608. status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
  609. &tmp);
  610. if (status) {
  611. dev_err(&serial->interface->dev, "%s: read failed: %d\n",
  612. __func__, status);
  613. return status;
  614. }
  615. /* We'll use the custom data when the data is valid. */
  616. if (tmp == F81534_CUSTOM_VALID_TOKEN)
  617. *index = 0;
  618. else
  619. *index = F81534_CUSTOM_NO_CUSTOM_DATA;
  620. return 0;
  621. }
  622. /*
  623. * The F81532/534 will not report serial port to USB serial subsystem when
  624. * H/W DCD/DSR/CTS/RI/RX pin connected to ground.
  625. *
  626. * To detect RX pin status, we'll enable MCR interal loopback, disable it and
  627. * delayed for 60ms. It connected to ground If LSR register report UART_LSR_BI.
  628. */
  629. static bool f81534_check_port_hw_disabled(struct usb_serial *serial, int phy)
  630. {
  631. int status;
  632. u8 old_mcr;
  633. u8 msr;
  634. u8 lsr;
  635. u8 msr_mask;
  636. msr_mask = UART_MSR_DCD | UART_MSR_RI | UART_MSR_DSR | UART_MSR_CTS;
  637. status = f81534_get_phy_port_register(serial, phy,
  638. F81534_MODEM_STATUS_REG, &msr);
  639. if (status)
  640. return false;
  641. if ((msr & msr_mask) != msr_mask)
  642. return false;
  643. status = f81534_set_phy_port_register(serial, phy,
  644. F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
  645. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  646. if (status)
  647. return false;
  648. status = f81534_get_phy_port_register(serial, phy,
  649. F81534_MODEM_CONTROL_REG, &old_mcr);
  650. if (status)
  651. return false;
  652. status = f81534_set_phy_port_register(serial, phy,
  653. F81534_MODEM_CONTROL_REG, UART_MCR_LOOP);
  654. if (status)
  655. return false;
  656. status = f81534_set_phy_port_register(serial, phy,
  657. F81534_MODEM_CONTROL_REG, 0x0);
  658. if (status)
  659. return false;
  660. msleep(60);
  661. status = f81534_get_phy_port_register(serial, phy,
  662. F81534_LINE_STATUS_REG, &lsr);
  663. if (status)
  664. return false;
  665. status = f81534_set_phy_port_register(serial, phy,
  666. F81534_MODEM_CONTROL_REG, old_mcr);
  667. if (status)
  668. return false;
  669. if ((lsr & UART_LSR_BI) == UART_LSR_BI)
  670. return true;
  671. return false;
  672. }
  673. /*
  674. * We had 2 generation of F81532/534 IC. All has an internal storage.
  675. *
  676. * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
  677. * internal data will used. All mode and gpio control should manually set
  678. * by AP or Driver and all storage space value are 0xff. The
  679. * f81534_calc_num_ports() will run to final we marked as "oldest version"
  680. * for this IC.
  681. *
  682. * 2rd is designed to more generic to use any transceiver and this is our
  683. * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
  684. * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
  685. * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
  686. * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
  687. * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
  688. * The f81534_calc_num_ports() will run to "new style" with checking
  689. * F81534_PORT_UNAVAILABLE section.
  690. */
  691. static int f81534_calc_num_ports(struct usb_serial *serial,
  692. struct usb_serial_endpoints *epds)
  693. {
  694. struct f81534_serial_private *serial_priv;
  695. struct device *dev = &serial->interface->dev;
  696. int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
  697. int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
  698. u8 num_port = 0;
  699. int index = 0;
  700. int status;
  701. int i;
  702. if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
  703. size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
  704. dev_err(dev, "unsupported endpoint max packet size\n");
  705. return -ENODEV;
  706. }
  707. serial_priv = devm_kzalloc(&serial->interface->dev,
  708. sizeof(*serial_priv), GFP_KERNEL);
  709. if (!serial_priv)
  710. return -ENOMEM;
  711. usb_set_serial_data(serial, serial_priv);
  712. mutex_init(&serial_priv->urb_mutex);
  713. /* Check had custom setting */
  714. status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
  715. if (status) {
  716. dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
  717. __func__, status);
  718. return status;
  719. }
  720. /*
  721. * We'll read custom data only when data available, otherwise we'll
  722. * read default value instead.
  723. */
  724. if (serial_priv->setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
  725. status = f81534_read_flash(serial,
  726. F81534_CUSTOM_ADDRESS_START +
  727. F81534_CONF_OFFSET,
  728. sizeof(serial_priv->conf_data),
  729. serial_priv->conf_data);
  730. if (status) {
  731. dev_err(&serial->interface->dev,
  732. "%s: get custom data failed: %d\n",
  733. __func__, status);
  734. return status;
  735. }
  736. dev_dbg(&serial->interface->dev,
  737. "%s: read config from block: %d\n", __func__,
  738. serial_priv->setting_idx);
  739. } else {
  740. /* Read default board setting */
  741. status = f81534_read_flash(serial,
  742. F81534_DEF_CONF_ADDRESS_START,
  743. sizeof(serial_priv->conf_data),
  744. serial_priv->conf_data);
  745. if (status) {
  746. dev_err(&serial->interface->dev,
  747. "%s: read failed: %d\n", __func__,
  748. status);
  749. return status;
  750. }
  751. dev_dbg(&serial->interface->dev, "%s: read default config\n",
  752. __func__);
  753. }
  754. /* New style, find all possible ports */
  755. for (i = 0; i < F81534_NUM_PORT; ++i) {
  756. if (f81534_check_port_hw_disabled(serial, i))
  757. serial_priv->conf_data[i] |= F81534_PORT_UNAVAILABLE;
  758. if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
  759. continue;
  760. ++num_port;
  761. }
  762. if (!num_port) {
  763. dev_warn(&serial->interface->dev,
  764. "no config found, assuming 4 ports\n");
  765. num_port = 4; /* Nothing found, oldest version IC */
  766. }
  767. /* Assign phy-to-logic mapping */
  768. for (i = 0; i < F81534_NUM_PORT; ++i) {
  769. if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
  770. continue;
  771. serial_priv->tty_idx[i] = index++;
  772. dev_dbg(&serial->interface->dev,
  773. "%s: phy_num: %d, tty_idx: %d\n", __func__, i,
  774. serial_priv->tty_idx[i]);
  775. }
  776. /*
  777. * Setup bulk-out endpoint multiplexing. All ports share the same
  778. * bulk-out endpoint.
  779. */
  780. BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
  781. for (i = 1; i < num_port; ++i)
  782. epds->bulk_out[i] = epds->bulk_out[0];
  783. epds->num_bulk_out = num_port;
  784. return num_port;
  785. }
  786. static void f81534_set_termios(struct tty_struct *tty,
  787. struct usb_serial_port *port,
  788. struct ktermios *old_termios)
  789. {
  790. u8 new_lcr = 0;
  791. int status;
  792. u32 baud;
  793. u32 old_baud;
  794. if (C_BAUD(tty) == B0)
  795. f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
  796. else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
  797. f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
  798. if (C_PARENB(tty)) {
  799. new_lcr |= UART_LCR_PARITY;
  800. if (!C_PARODD(tty))
  801. new_lcr |= UART_LCR_EPAR;
  802. if (C_CMSPAR(tty))
  803. new_lcr |= UART_LCR_SPAR;
  804. }
  805. if (C_CSTOPB(tty))
  806. new_lcr |= UART_LCR_STOP;
  807. switch (C_CSIZE(tty)) {
  808. case CS5:
  809. new_lcr |= UART_LCR_WLEN5;
  810. break;
  811. case CS6:
  812. new_lcr |= UART_LCR_WLEN6;
  813. break;
  814. case CS7:
  815. new_lcr |= UART_LCR_WLEN7;
  816. break;
  817. default:
  818. case CS8:
  819. new_lcr |= UART_LCR_WLEN8;
  820. break;
  821. }
  822. baud = tty_get_baud_rate(tty);
  823. if (!baud)
  824. return;
  825. if (old_termios)
  826. old_baud = tty_termios_baud_rate(old_termios);
  827. else
  828. old_baud = F81534_DEFAULT_BAUD_RATE;
  829. dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
  830. status = f81534_set_port_config(port, tty, baud, old_baud, new_lcr);
  831. if (status < 0) {
  832. dev_err(&port->dev, "%s: set port config failed: %d\n",
  833. __func__, status);
  834. }
  835. }
  836. static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
  837. {
  838. return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
  839. }
  840. static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
  841. {
  842. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  843. struct tty_struct *tty;
  844. unsigned long flags;
  845. u8 old_msr;
  846. if (!(msr & UART_MSR_ANY_DELTA))
  847. return;
  848. spin_lock_irqsave(&port_priv->msr_lock, flags);
  849. old_msr = port_priv->shadow_msr;
  850. port_priv->shadow_msr = msr;
  851. spin_unlock_irqrestore(&port_priv->msr_lock, flags);
  852. dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
  853. msr);
  854. /* Update input line counters */
  855. if (msr & UART_MSR_DCTS)
  856. port->icount.cts++;
  857. if (msr & UART_MSR_DDSR)
  858. port->icount.dsr++;
  859. if (msr & UART_MSR_DDCD)
  860. port->icount.dcd++;
  861. if (msr & UART_MSR_TERI)
  862. port->icount.rng++;
  863. wake_up_interruptible(&port->port.delta_msr_wait);
  864. if (!(msr & UART_MSR_DDCD))
  865. return;
  866. dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
  867. __func__, port_priv->phy_num, old_msr, msr);
  868. tty = tty_port_tty_get(&port->port);
  869. if (!tty)
  870. return;
  871. usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
  872. tty_kref_put(tty);
  873. }
  874. static int f81534_read_msr(struct usb_serial_port *port)
  875. {
  876. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  877. unsigned long flags;
  878. int status;
  879. u8 msr;
  880. /* Get MSR initial value */
  881. status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
  882. if (status)
  883. return status;
  884. /* Force update current state */
  885. spin_lock_irqsave(&port_priv->msr_lock, flags);
  886. port_priv->shadow_msr = msr;
  887. spin_unlock_irqrestore(&port_priv->msr_lock, flags);
  888. return 0;
  889. }
  890. static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
  891. {
  892. struct f81534_serial_private *serial_priv =
  893. usb_get_serial_data(port->serial);
  894. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  895. int status;
  896. status = f81534_set_port_register(port,
  897. F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
  898. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  899. if (status) {
  900. dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
  901. status);
  902. return status;
  903. }
  904. if (tty)
  905. f81534_set_termios(tty, port, NULL);
  906. status = f81534_read_msr(port);
  907. if (status)
  908. return status;
  909. mutex_lock(&serial_priv->urb_mutex);
  910. /* Submit Read URBs for first port opened */
  911. if (!serial_priv->opened_port) {
  912. status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
  913. if (status)
  914. goto exit;
  915. }
  916. serial_priv->opened_port++;
  917. exit:
  918. mutex_unlock(&serial_priv->urb_mutex);
  919. set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
  920. return status;
  921. }
  922. static void f81534_close(struct usb_serial_port *port)
  923. {
  924. struct f81534_serial_private *serial_priv =
  925. usb_get_serial_data(port->serial);
  926. struct usb_serial_port *port0 = port->serial->port[0];
  927. unsigned long flags;
  928. size_t i;
  929. usb_kill_urb(port->write_urbs[0]);
  930. spin_lock_irqsave(&port->lock, flags);
  931. kfifo_reset_out(&port->write_fifo);
  932. spin_unlock_irqrestore(&port->lock, flags);
  933. /* Kill Read URBs when final port closed */
  934. mutex_lock(&serial_priv->urb_mutex);
  935. serial_priv->opened_port--;
  936. if (!serial_priv->opened_port) {
  937. for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
  938. usb_kill_urb(port0->read_urbs[i]);
  939. }
  940. mutex_unlock(&serial_priv->urb_mutex);
  941. }
  942. static int f81534_get_serial_info(struct usb_serial_port *port,
  943. struct serial_struct __user *retinfo)
  944. {
  945. struct f81534_port_private *port_priv;
  946. struct serial_struct tmp;
  947. port_priv = usb_get_serial_port_data(port);
  948. memset(&tmp, 0, sizeof(tmp));
  949. tmp.type = PORT_16550A;
  950. tmp.port = port->port_number;
  951. tmp.line = port->minor;
  952. tmp.baud_base = port_priv->baud_base;
  953. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  954. return -EFAULT;
  955. return 0;
  956. }
  957. static int f81534_ioctl(struct tty_struct *tty, unsigned int cmd,
  958. unsigned long arg)
  959. {
  960. struct usb_serial_port *port = tty->driver_data;
  961. struct serial_struct __user *buf = (struct serial_struct __user *)arg;
  962. switch (cmd) {
  963. case TIOCGSERIAL:
  964. return f81534_get_serial_info(port, buf);
  965. default:
  966. break;
  967. }
  968. return -ENOIOCTLCMD;
  969. }
  970. static void f81534_process_per_serial_block(struct usb_serial_port *port,
  971. u8 *data)
  972. {
  973. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  974. int phy_num = data[0];
  975. size_t read_size = 0;
  976. size_t i;
  977. char tty_flag;
  978. int status;
  979. u8 lsr;
  980. /*
  981. * The block layout is 128 Bytes
  982. * index 0: port phy idx (e.g., 0,1,2,3),
  983. * index 1: It's could be
  984. * F81534_TOKEN_RECEIVE
  985. * F81534_TOKEN_TX_EMPTY
  986. * F81534_TOKEN_MSR_CHANGE
  987. * index 2: serial in size (data+lsr, must be even)
  988. * meaningful for F81534_TOKEN_RECEIVE only
  989. * index 3: current MSR with this device
  990. * index 4~127: serial in data block (data+lsr, must be even)
  991. */
  992. switch (data[1]) {
  993. case F81534_TOKEN_TX_EMPTY:
  994. set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
  995. /* Try to submit writer */
  996. status = f81534_submit_writer(port, GFP_ATOMIC);
  997. if (status)
  998. dev_err(&port->dev, "%s: submit failed\n", __func__);
  999. return;
  1000. case F81534_TOKEN_MSR_CHANGE:
  1001. f81534_msr_changed(port, data[3]);
  1002. return;
  1003. case F81534_TOKEN_RECEIVE:
  1004. read_size = data[2];
  1005. if (read_size > F81534_MAX_RX_SIZE) {
  1006. dev_err(&port->dev,
  1007. "%s: phy: %d read_size: %zu larger than: %d\n",
  1008. __func__, phy_num, read_size,
  1009. F81534_MAX_RX_SIZE);
  1010. return;
  1011. }
  1012. break;
  1013. default:
  1014. dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
  1015. data[1]);
  1016. return;
  1017. }
  1018. for (i = 4; i < 4 + read_size; i += 2) {
  1019. tty_flag = TTY_NORMAL;
  1020. lsr = data[i + 1];
  1021. if (lsr & UART_LSR_BRK_ERROR_BITS) {
  1022. if (lsr & UART_LSR_BI) {
  1023. tty_flag = TTY_BREAK;
  1024. port->icount.brk++;
  1025. usb_serial_handle_break(port);
  1026. } else if (lsr & UART_LSR_PE) {
  1027. tty_flag = TTY_PARITY;
  1028. port->icount.parity++;
  1029. } else if (lsr & UART_LSR_FE) {
  1030. tty_flag = TTY_FRAME;
  1031. port->icount.frame++;
  1032. }
  1033. if (lsr & UART_LSR_OE) {
  1034. port->icount.overrun++;
  1035. tty_insert_flip_char(&port->port, 0,
  1036. TTY_OVERRUN);
  1037. }
  1038. schedule_work(&port_priv->lsr_work);
  1039. }
  1040. if (port->port.console && port->sysrq) {
  1041. if (usb_serial_handle_sysrq_char(port, data[i]))
  1042. continue;
  1043. }
  1044. tty_insert_flip_char(&port->port, data[i], tty_flag);
  1045. }
  1046. tty_flip_buffer_push(&port->port);
  1047. }
  1048. static void f81534_process_read_urb(struct urb *urb)
  1049. {
  1050. struct f81534_serial_private *serial_priv;
  1051. struct usb_serial_port *port;
  1052. struct usb_serial *serial;
  1053. u8 *buf;
  1054. int phy_port_num;
  1055. int tty_port_num;
  1056. size_t i;
  1057. if (!urb->actual_length ||
  1058. urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
  1059. return;
  1060. }
  1061. port = urb->context;
  1062. serial = port->serial;
  1063. buf = urb->transfer_buffer;
  1064. serial_priv = usb_get_serial_data(serial);
  1065. for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
  1066. phy_port_num = buf[i];
  1067. if (phy_port_num >= F81534_NUM_PORT) {
  1068. dev_err(&port->dev,
  1069. "%s: phy_port_num: %d larger than: %d\n",
  1070. __func__, phy_port_num, F81534_NUM_PORT);
  1071. continue;
  1072. }
  1073. tty_port_num = serial_priv->tty_idx[phy_port_num];
  1074. port = serial->port[tty_port_num];
  1075. if (tty_port_initialized(&port->port))
  1076. f81534_process_per_serial_block(port, &buf[i]);
  1077. }
  1078. }
  1079. static void f81534_write_usb_callback(struct urb *urb)
  1080. {
  1081. struct usb_serial_port *port = urb->context;
  1082. switch (urb->status) {
  1083. case 0:
  1084. break;
  1085. case -ENOENT:
  1086. case -ECONNRESET:
  1087. case -ESHUTDOWN:
  1088. dev_dbg(&port->dev, "%s - urb stopped: %d\n",
  1089. __func__, urb->status);
  1090. return;
  1091. case -EPIPE:
  1092. dev_err(&port->dev, "%s - urb stopped: %d\n",
  1093. __func__, urb->status);
  1094. return;
  1095. default:
  1096. dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
  1097. __func__, urb->status);
  1098. break;
  1099. }
  1100. }
  1101. static void f81534_lsr_worker(struct work_struct *work)
  1102. {
  1103. struct f81534_port_private *port_priv;
  1104. struct usb_serial_port *port;
  1105. int status;
  1106. u8 tmp;
  1107. port_priv = container_of(work, struct f81534_port_private, lsr_work);
  1108. port = port_priv->port;
  1109. status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
  1110. if (status)
  1111. dev_warn(&port->dev, "read LSR failed: %d\n", status);
  1112. }
  1113. static int f81534_set_port_output_pin(struct usb_serial_port *port)
  1114. {
  1115. struct f81534_serial_private *serial_priv;
  1116. struct f81534_port_private *port_priv;
  1117. struct usb_serial *serial;
  1118. const struct f81534_port_out_pin *pins;
  1119. int status;
  1120. int i;
  1121. u8 value;
  1122. u8 idx;
  1123. serial = port->serial;
  1124. serial_priv = usb_get_serial_data(serial);
  1125. port_priv = usb_get_serial_port_data(port);
  1126. idx = F81534_CONF_INIT_GPIO_OFFSET + port_priv->phy_num;
  1127. value = serial_priv->conf_data[idx];
  1128. if (value >= F81534_CONF_GPIO_SHUTDOWN) {
  1129. /*
  1130. * Newer IC configure will make transceiver in shutdown mode on
  1131. * initial power on. We need enable it before using UARTs.
  1132. */
  1133. idx = F81534_CONF_WORK_GPIO_OFFSET + port_priv->phy_num;
  1134. value = serial_priv->conf_data[idx];
  1135. if (value >= F81534_CONF_GPIO_SHUTDOWN)
  1136. value = F81534_CONF_GPIO_RS232;
  1137. }
  1138. pins = &f81534_port_out_pins[port_priv->phy_num];
  1139. for (i = 0; i < ARRAY_SIZE(pins->pin); ++i) {
  1140. status = f81534_set_mask_register(serial,
  1141. pins->pin[i].reg_addr, pins->pin[i].reg_mask,
  1142. value & BIT(i) ? pins->pin[i].reg_mask : 0);
  1143. if (status)
  1144. return status;
  1145. }
  1146. dev_dbg(&port->dev, "Output pin (M0/M1/M2): %d\n", value);
  1147. return 0;
  1148. }
  1149. static int f81534_port_probe(struct usb_serial_port *port)
  1150. {
  1151. struct f81534_serial_private *serial_priv;
  1152. struct f81534_port_private *port_priv;
  1153. int ret;
  1154. u8 value;
  1155. serial_priv = usb_get_serial_data(port->serial);
  1156. port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
  1157. if (!port_priv)
  1158. return -ENOMEM;
  1159. /*
  1160. * We'll make tx frame error when baud rate from 384~500kps. So we'll
  1161. * delay all tx data frame with 1bit.
  1162. */
  1163. port_priv->shadow_clk = F81534_UART_EN | F81534_CLK_TX_DELAY_1BIT;
  1164. spin_lock_init(&port_priv->msr_lock);
  1165. mutex_init(&port_priv->mcr_mutex);
  1166. mutex_init(&port_priv->lcr_mutex);
  1167. INIT_WORK(&port_priv->lsr_work, f81534_lsr_worker);
  1168. /* Assign logic-to-phy mapping */
  1169. ret = f81534_logic_to_phy_port(port->serial, port);
  1170. if (ret < 0)
  1171. return ret;
  1172. port_priv->phy_num = ret;
  1173. port_priv->port = port;
  1174. usb_set_serial_port_data(port, port_priv);
  1175. dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
  1176. port->port_number, port_priv->phy_num);
  1177. /*
  1178. * The F81532/534 will hang-up when enable LSR interrupt in IER and
  1179. * occur data overrun. So we'll disable the LSR interrupt in probe()
  1180. * and submit the LSR worker to clear LSR state when reported LSR error
  1181. * bit with bulk-in data in f81534_process_per_serial_block().
  1182. */
  1183. ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
  1184. UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
  1185. if (ret)
  1186. return ret;
  1187. value = serial_priv->conf_data[port_priv->phy_num];
  1188. switch (value & F81534_PORT_CONF_MODE_MASK) {
  1189. case F81534_PORT_CONF_RS485_INVERT:
  1190. port_priv->shadow_clk |= F81534_CLK_RS485_MODE |
  1191. F81534_CLK_RS485_INVERT;
  1192. dev_dbg(&port->dev, "RS485 invert mode\n");
  1193. break;
  1194. case F81534_PORT_CONF_RS485:
  1195. port_priv->shadow_clk |= F81534_CLK_RS485_MODE;
  1196. dev_dbg(&port->dev, "RS485 mode\n");
  1197. break;
  1198. default:
  1199. case F81534_PORT_CONF_RS232:
  1200. dev_dbg(&port->dev, "RS232 mode\n");
  1201. break;
  1202. }
  1203. return f81534_set_port_output_pin(port);
  1204. }
  1205. static int f81534_port_remove(struct usb_serial_port *port)
  1206. {
  1207. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  1208. flush_work(&port_priv->lsr_work);
  1209. return 0;
  1210. }
  1211. static int f81534_tiocmget(struct tty_struct *tty)
  1212. {
  1213. struct usb_serial_port *port = tty->driver_data;
  1214. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  1215. int status;
  1216. int r;
  1217. u8 msr;
  1218. u8 mcr;
  1219. /* Read current MSR from device */
  1220. status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
  1221. if (status)
  1222. return status;
  1223. mutex_lock(&port_priv->mcr_mutex);
  1224. mcr = port_priv->shadow_mcr;
  1225. mutex_unlock(&port_priv->mcr_mutex);
  1226. r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
  1227. (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
  1228. (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
  1229. (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
  1230. (msr & UART_MSR_RI ? TIOCM_RI : 0) |
  1231. (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
  1232. return r;
  1233. }
  1234. static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
  1235. unsigned int clear)
  1236. {
  1237. struct usb_serial_port *port = tty->driver_data;
  1238. return f81534_update_mctrl(port, set, clear);
  1239. }
  1240. static void f81534_dtr_rts(struct usb_serial_port *port, int on)
  1241. {
  1242. if (on)
  1243. f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
  1244. else
  1245. f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
  1246. }
  1247. static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
  1248. const u8 *buf, int count)
  1249. {
  1250. int bytes_out, status;
  1251. if (!count)
  1252. return 0;
  1253. bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
  1254. &port->lock);
  1255. status = f81534_submit_writer(port, GFP_ATOMIC);
  1256. if (status) {
  1257. dev_err(&port->dev, "%s: submit failed\n", __func__);
  1258. return status;
  1259. }
  1260. return bytes_out;
  1261. }
  1262. static bool f81534_tx_empty(struct usb_serial_port *port)
  1263. {
  1264. struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
  1265. return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
  1266. }
  1267. static int f81534_resume(struct usb_serial *serial)
  1268. {
  1269. struct f81534_serial_private *serial_priv =
  1270. usb_get_serial_data(serial);
  1271. struct usb_serial_port *port;
  1272. int error = 0;
  1273. int status;
  1274. size_t i;
  1275. /*
  1276. * We'll register port 0 bulkin when port had opened, It'll take all
  1277. * port received data, MSR register change and TX_EMPTY information.
  1278. */
  1279. mutex_lock(&serial_priv->urb_mutex);
  1280. if (serial_priv->opened_port) {
  1281. status = f81534_submit_read_urb(serial, GFP_NOIO);
  1282. if (status) {
  1283. mutex_unlock(&serial_priv->urb_mutex);
  1284. return status;
  1285. }
  1286. }
  1287. mutex_unlock(&serial_priv->urb_mutex);
  1288. for (i = 0; i < serial->num_ports; i++) {
  1289. port = serial->port[i];
  1290. if (!tty_port_initialized(&port->port))
  1291. continue;
  1292. status = f81534_submit_writer(port, GFP_NOIO);
  1293. if (status) {
  1294. dev_err(&port->dev, "%s: submit failed\n", __func__);
  1295. ++error;
  1296. }
  1297. }
  1298. if (error)
  1299. return -EIO;
  1300. return 0;
  1301. }
  1302. static struct usb_serial_driver f81534_device = {
  1303. .driver = {
  1304. .owner = THIS_MODULE,
  1305. .name = "f81534",
  1306. },
  1307. .description = DRIVER_DESC,
  1308. .id_table = f81534_id_table,
  1309. .num_bulk_in = 1,
  1310. .num_bulk_out = 1,
  1311. .open = f81534_open,
  1312. .close = f81534_close,
  1313. .write = f81534_write,
  1314. .tx_empty = f81534_tx_empty,
  1315. .calc_num_ports = f81534_calc_num_ports,
  1316. .port_probe = f81534_port_probe,
  1317. .port_remove = f81534_port_remove,
  1318. .break_ctl = f81534_break_ctl,
  1319. .dtr_rts = f81534_dtr_rts,
  1320. .process_read_urb = f81534_process_read_urb,
  1321. .ioctl = f81534_ioctl,
  1322. .tiocmget = f81534_tiocmget,
  1323. .tiocmset = f81534_tiocmset,
  1324. .write_bulk_callback = f81534_write_usb_callback,
  1325. .set_termios = f81534_set_termios,
  1326. .resume = f81534_resume,
  1327. };
  1328. static struct usb_serial_driver *const serial_drivers[] = {
  1329. &f81534_device, NULL
  1330. };
  1331. module_usb_serial_driver(serial_drivers, f81534_id_table);
  1332. MODULE_DEVICE_TABLE(usb, f81534_id_table);
  1333. MODULE_DESCRIPTION(DRIVER_DESC);
  1334. MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
  1335. MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
  1336. MODULE_LICENSE("GPL");