f81534.c 34 KB

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