Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.99 KB | None | 0 0
  1. /*
  2. * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
  3. *
  4. * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * Support to set flow control line levels using TIOCMGET and TIOCMSET
  11. * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
  12. * control thanks to Munir Nassar nassarmu@real-time.com
  13. *
  14. */
  15.  
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_flip.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/usb.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/usb/serial.h>
  26.  
  27. #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
  28.  
  29. /*
  30. * Function Prototypes
  31. */
  32. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
  33. static void cp210x_close(struct usb_serial_port *);
  34. static int cp210x_ioctl(struct tty_struct *tty,
  35. unsigned int cmd, unsigned long arg);
  36. static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
  37. static void cp210x_get_termios_port(struct usb_serial_port *port,
  38. unsigned int *cflagp, unsigned int *baudp);
  39. static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
  40. struct ktermios *);
  41. static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
  42. struct ktermios*);
  43. static bool cp210x_tx_empty(struct usb_serial_port *port);
  44. static int cp210x_tiocmget(struct tty_struct *);
  45. static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
  46. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  47. unsigned int, unsigned int);
  48. static void cp210x_break_ctl(struct tty_struct *, int);
  49. static int cp210x_port_probe(struct usb_serial_port *);
  50. static int cp210x_port_remove(struct usb_serial_port *);
  51. static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
  52.  
  53. static const struct usb_device_id id_table[] = {
  54. { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
  55. { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
  56. { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  57. { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  58. { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
  59. { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
  60. { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
  61. { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
  62. { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
  63. { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
  64. { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
  65. { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
  66. { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
  67. { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
  68. { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
  69. { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
  70. { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
  71. { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
  72. { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
  73. { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
  74. { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
  75. { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
  76. { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
  77. { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
  78. { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
  79. { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
  80. { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
  81. { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
  82. { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
  83. { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
  84. { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
  85. { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
  86. { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
  87. { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
  88. { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
  89. { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
  90. { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
  91. { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
  92. { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
  93. { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
  94. { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
  95. { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
  96. { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
  97. { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
  98. { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
  99. { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
  100. { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
  101. { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
  102. { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
  103. { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
  104. { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
  105. { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
  106. { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
  107. { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
  108. { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
  109. { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
  110. { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
  111. { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
  112. { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
  113. { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
  114. { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
  115. { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
  116. { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
  117. { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
  118. { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
  119. { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
  120. { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
  121. { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
  122. { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
  123. { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
  124. { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
  125. { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
  126. { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
  127. { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
  128. { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
  129. { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
  130. { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
  131. { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
  132. { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
  133. { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
  134. { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
  135. { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
  136. { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
  137. { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
  138. { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
  139. { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
  140. { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
  141. { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
  142. { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
  143. { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
  144. { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
  145. { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
  146. { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
  147. { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
  148. { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
  149. { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
  150. { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
  151. { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
  152. { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
  153. { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
  154. { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
  155. { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
  156. { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
  157. { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
  158. { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
  159. { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
  160. { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
  161. { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
  162. { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
  163. { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
  164. { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
  165. { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
  166. { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
  167. { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
  168. { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
  169. { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
  170. { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
  171. { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
  172. { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
  173. { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
  174. { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
  175. { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
  176. { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
  177. { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
  178. { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
  179. { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
  180. { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
  181. { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
  182. { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
  183. { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
  184. { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
  185. { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
  186. { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
  187. { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
  188. { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
  189. { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
  190. { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
  191. { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
  192. { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
  193. { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
  194. { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
  195. { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
  196. { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
  197. { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
  198. { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
  199. { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
  200. { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
  201. { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
  202. { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
  203. { } /* Terminating Entry */
  204. };
  205.  
  206. MODULE_DEVICE_TABLE(usb, id_table);
  207.  
  208. struct cp210x_port_private {
  209. __u8 bPartNumber;
  210. __u8 bInterfaceNumber;
  211. bool has_swapped_line_ctl;
  212. };
  213.  
  214. static struct usb_serial_driver cp210x_device = {
  215. .driver = {
  216. .owner = THIS_MODULE,
  217. .name = "cp210x",
  218. },
  219. .id_table = id_table,
  220. .num_ports = 1,
  221. .bulk_in_size = 256,
  222. .bulk_out_size = 256,
  223. .open = cp210x_open,
  224. .close = cp210x_close,
  225. .ioctl = cp210x_ioctl,
  226. .break_ctl = cp210x_break_ctl,
  227. .set_termios = cp210x_set_termios,
  228. .tx_empty = cp210x_tx_empty,
  229. .tiocmget = cp210x_tiocmget,
  230. .tiocmset = cp210x_tiocmset,
  231. .port_probe = cp210x_port_probe,
  232. .port_remove = cp210x_port_remove,
  233. .dtr_rts = cp210x_dtr_rts
  234. };
  235.  
  236. /* IOCTLs */
  237. #define IOCTL_GPIOGET 0x8000
  238. #define IOCTL_GPIOSET 0x8001
  239.  
  240. static struct usb_serial_driver * const serial_drivers[] = {
  241. &cp210x_device, NULL
  242. };
  243.  
  244. /* Config request types */
  245. #define REQTYPE_HOST_TO_INTERFACE 0x41
  246. #define REQTYPE_INTERFACE_TO_HOST 0xc1
  247. #define REQTYPE_HOST_TO_DEVICE 0x40
  248. #define REQTYPE_DEVICE_TO_HOST 0xc0
  249.  
  250. /* Config request codes */
  251. #define CP210X_IFC_ENABLE 0x00
  252. #define CP210X_SET_BAUDDIV 0x01
  253. #define CP210X_GET_BAUDDIV 0x02
  254. #define CP210X_SET_LINE_CTL 0x03
  255. #define CP210X_GET_LINE_CTL 0x04
  256. #define CP210X_SET_BREAK 0x05
  257. #define CP210X_IMM_CHAR 0x06
  258. #define CP210X_SET_MHS 0x07
  259. #define CP210X_GET_MDMSTS 0x08
  260. #define CP210X_SET_XON 0x09
  261. #define CP210X_SET_XOFF 0x0A
  262. #define CP210X_SET_EVENTMASK 0x0B
  263. #define CP210X_GET_EVENTMASK 0x0C
  264. #define CP210X_SET_CHAR 0x0D
  265. #define CP210X_GET_CHARS 0x0E
  266. #define CP210X_GET_PROPS 0x0F
  267. #define CP210X_GET_COMM_STATUS 0x10
  268. #define CP210X_RESET 0x11
  269. #define CP210X_PURGE 0x12
  270. #define CP210X_SET_FLOW 0x13
  271. #define CP210X_GET_FLOW 0x14
  272. #define CP210X_EMBED_EVENTS 0x15
  273. #define CP210X_GET_EVENTSTATE 0x16
  274. #define CP210X_SET_CHARS 0x19
  275. #define CP210X_GET_BAUDRATE 0x1D
  276. #define CP210X_SET_BAUDRATE 0x1E
  277. #define CP210X_VENDOR_SPECIFIC 0xFF
  278.  
  279. /* CP210X_IFC_ENABLE */
  280. #define UART_ENABLE 0x0001
  281. #define UART_DISABLE 0x0000
  282.  
  283. /* CP210X_(SET|GET)_BAUDDIV */
  284. #define BAUD_RATE_GEN_FREQ 0x384000
  285.  
  286. /* CP210X_(SET|GET)_LINE_CTL */
  287. #define BITS_DATA_MASK 0X0f00
  288. #define BITS_DATA_5 0X0500
  289. #define BITS_DATA_6 0X0600
  290. #define BITS_DATA_7 0X0700
  291. #define BITS_DATA_8 0X0800
  292. #define BITS_DATA_9 0X0900
  293.  
  294. #define BITS_PARITY_MASK 0x00f0
  295. #define BITS_PARITY_NONE 0x0000
  296. #define BITS_PARITY_ODD 0x0010
  297. #define BITS_PARITY_EVEN 0x0020
  298. #define BITS_PARITY_MARK 0x0030
  299. #define BITS_PARITY_SPACE 0x0040
  300.  
  301. #define BITS_STOP_MASK 0x000f
  302. #define BITS_STOP_1 0x0000
  303. #define BITS_STOP_1_5 0x0001
  304. #define BITS_STOP_2 0x0002
  305.  
  306. /* CP210X_SET_BREAK */
  307. #define BREAK_ON 0x0001
  308. #define BREAK_OFF 0x0000
  309.  
  310. /* CP210X_(SET_MHS|GET_MDMSTS) */
  311. #define CONTROL_DTR 0x0001
  312. #define CONTROL_RTS 0x0002
  313. #define CONTROL_CTS 0x0010
  314. #define CONTROL_DSR 0x0020
  315. #define CONTROL_RING 0x0040
  316. #define CONTROL_DCD 0x0080
  317. #define CONTROL_WRITE_DTR 0x0100
  318. #define CONTROL_WRITE_RTS 0x0200
  319.  
  320. /* CP210X_VENDOR_SPECIFIC sub-commands passed in wValue */
  321. #define CP210X_WRITE_LATCH 0x37E1
  322. #define CP210X_READ_LATCH 0x00C2
  323. #define CP210X_GET_PARTNUM 0x370B
  324.  
  325. /* CP210X_GET_PARTNUM returns one of these */
  326. #define CP2101_PARTNUM 0x01
  327. #define CP2102_PARTNUM 0x02
  328. #define CP2103_PARTNUM 0x03
  329. #define CP2104_PARTNUM 0x04
  330. #define CP2105_PARTNUM 0x05
  331. #define CP2108_PARTNUM 0x08
  332. #define CP210x_PARTNUM_CP2102N_QFN28 0x20
  333. #define CP210x_PARTNUM_CP2102N_QFN24 0x21
  334. #define CP210x_PARTNUM_CP2102N_QFN20 0x22
  335.  
  336. /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
  337. struct cp210x_comm_status {
  338. __le32 ulErrors;
  339. __le32 ulHoldReasons;
  340. __le32 ulAmountInInQueue;
  341. __le32 ulAmountInOutQueue;
  342. u8 bEofReceived;
  343. u8 bWaitForImmediate;
  344. u8 bReserved;
  345. } __packed;
  346.  
  347. /*
  348. * CP210X_PURGE - 16 bits passed in wValue of USB request.
  349. * SiLabs app note AN571 gives a strange description of the 4 bits:
  350. * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
  351. * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
  352. */
  353. #define PURGE_ALL 0x000f
  354.  
  355. /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
  356. struct cp210x_flow_ctl {
  357. __le32 ulControlHandshake;
  358. __le32 ulFlowReplace;
  359. __le32 ulXonLimit;
  360. __le32 ulXoffLimit;
  361. } __packed;
  362.  
  363. /* cp210x_flow_ctl::ulControlHandshake */
  364. #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
  365. #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
  366. #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
  367. #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
  368. #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
  369. #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
  370.  
  371. /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
  372. #define CP210X_SERIAL_DTR_INACTIVE 0
  373. #define CP210X_SERIAL_DTR_ACTIVE 1
  374. #define CP210X_SERIAL_DTR_FLOW_CTL 2
  375.  
  376. /* cp210x_flow_ctl::ulFlowReplace */
  377. #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
  378. #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
  379. #define CP210X_SERIAL_ERROR_CHAR BIT(2)
  380. #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
  381. #define CP210X_SERIAL_BREAK_CHAR BIT(4)
  382. #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
  383. #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
  384. #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
  385.  
  386. /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
  387. #define CP210X_SERIAL_RTS_INACTIVE 0
  388. #define CP210X_SERIAL_RTS_ACTIVE 1
  389. #define CP210X_SERIAL_RTS_FLOW_CTL 2
  390.  
  391. /*
  392. * Reads a variable-sized block of CP210X_ registers, identified by req.
  393. * Returns data into buf in native USB byte order.
  394. */
  395. static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
  396. void *buf, int bufsize)
  397. {
  398. struct usb_serial *serial = port->serial;
  399. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  400. void *dmabuf;
  401. int result;
  402.  
  403. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  404. if (!dmabuf) {
  405. /*
  406. * FIXME Some callers don't bother to check for error,
  407. * at least give them consistent junk until they are fixed
  408. */
  409. memset(buf, 0, bufsize);
  410. return -ENOMEM;
  411. }
  412.  
  413. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  414. req, REQTYPE_INTERFACE_TO_HOST, 0,
  415. port_priv->bInterfaceNumber, dmabuf, bufsize,
  416. USB_CTRL_SET_TIMEOUT);
  417. if (result == bufsize) {
  418. memcpy(buf, dmabuf, bufsize);
  419. result = 0;
  420. } else {
  421. dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
  422. req, bufsize, result);
  423. if (result >= 0)
  424. result = -EPROTO;
  425.  
  426. /*
  427. * FIXME Some callers don't bother to check for error,
  428. * at least give them consistent junk until they are fixed
  429. */
  430. memset(buf, 0, bufsize);
  431. }
  432.  
  433. kfree(dmabuf);
  434.  
  435. return result;
  436. }
  437.  
  438. /*
  439. * Reads any 32-bit CP210X_ register identified by req.
  440. */
  441. static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
  442. {
  443. __le32 le32_val;
  444. int err;
  445.  
  446. err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
  447. if (err) {
  448. /*
  449. * FIXME Some callers don't bother to check for error,
  450. * at least give them consistent junk until they are fixed
  451. */
  452. *val = 0;
  453. return err;
  454. }
  455.  
  456. *val = le32_to_cpu(le32_val);
  457.  
  458. return 0;
  459. }
  460.  
  461. /*
  462. * Reads any 16-bit CP210X_ register identified by req.
  463. */
  464. static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
  465. {
  466. __le16 le16_val;
  467. int err;
  468.  
  469. err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
  470. if (err)
  471. return err;
  472.  
  473. *val = le16_to_cpu(le16_val);
  474.  
  475. return 0;
  476. }
  477.  
  478. /*
  479. * Reads any 8-bit CP210X_ register identified by req.
  480. */
  481. static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
  482. {
  483. return cp210x_read_reg_block(port, req, val, sizeof(*val));
  484. }
  485.  
  486. /*
  487. * Writes any 16-bit CP210X_ register (req) whose value is passed
  488. * entirely in the wValue field of the USB request.
  489. */
  490. static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
  491. {
  492. struct usb_serial *serial = port->serial;
  493. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  494. int result;
  495.  
  496. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  497. req, REQTYPE_HOST_TO_INTERFACE, val,
  498. port_priv->bInterfaceNumber, NULL, 0,
  499. USB_CTRL_SET_TIMEOUT);
  500. if (result < 0) {
  501. dev_err(&port->dev, "failed set request 0x%x status: %d\n",
  502. req, result);
  503. }
  504.  
  505. return result;
  506. }
  507.  
  508. /*
  509. * Writes a variable-sized block of CP210X_ registers, identified by req.
  510. * Data in buf must be in native USB byte order.
  511. */
  512. static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
  513. void *buf, int bufsize)
  514. {
  515. struct usb_serial *serial = port->serial;
  516. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  517. void *dmabuf;
  518. int result;
  519.  
  520. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  521. if (!dmabuf)
  522. return -ENOMEM;
  523.  
  524. memcpy(dmabuf, buf, bufsize);
  525.  
  526. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  527. req, REQTYPE_HOST_TO_INTERFACE, 0,
  528. port_priv->bInterfaceNumber, dmabuf, bufsize,
  529. USB_CTRL_SET_TIMEOUT);
  530.  
  531. kfree(dmabuf);
  532.  
  533. if (result == bufsize) {
  534. result = 0;
  535. } else {
  536. dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
  537. req, bufsize, result);
  538. if (result >= 0)
  539. result = -EPROTO;
  540. }
  541.  
  542. return result;
  543. }
  544.  
  545. /*
  546. * Writes any 32-bit CP210X_ register identified by req.
  547. */
  548. static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
  549. {
  550. __le32 le32_val;
  551.  
  552. le32_val = cpu_to_le32(val);
  553.  
  554. return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
  555. }
  556.  
  557. /*
  558. * Detect CP2108 GET_LINE_CTL bug and activate workaround.
  559. * Write a known good value 0x800, read it back.
  560. * If it comes back swapped the bug is detected.
  561. * Preserve the original register value.
  562. */
  563. static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
  564. {
  565. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  566. u16 line_ctl_save;
  567. u16 line_ctl_test;
  568. int err;
  569.  
  570. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
  571. if (err)
  572. return err;
  573.  
  574. err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
  575. if (err)
  576. return err;
  577.  
  578. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
  579. if (err)
  580. return err;
  581.  
  582. if (line_ctl_test == 8) {
  583. port_priv->has_swapped_line_ctl = true;
  584. line_ctl_save = swab16(line_ctl_save);
  585. }
  586.  
  587. return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
  588. }
  589.  
  590. /*
  591. * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
  592. * to workaround cp2108 bug and get correct value.
  593. */
  594. static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
  595. {
  596. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  597. int err;
  598.  
  599. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
  600. if (err)
  601. return err;
  602.  
  603. /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
  604. if (port_priv->has_swapped_line_ctl)
  605. *ctl = swab16(*ctl);
  606.  
  607. return 0;
  608. }
  609.  
  610. /*
  611. * cp210x_quantise_baudrate
  612. * Quantises the baud rate as per AN205 Table 1
  613. */
  614. static unsigned int cp210x_quantise_baudrate(unsigned int baud)
  615. {
  616. if (baud <= 300)
  617. baud = 300;
  618. else if (baud <= 600) baud = 600;
  619. else if (baud <= 1200) baud = 1200;
  620. else if (baud <= 1800) baud = 1800;
  621. else if (baud <= 2400) baud = 2400;
  622. else if (baud <= 4000) baud = 4000;
  623. else if (baud <= 4803) baud = 4800;
  624. else if (baud <= 7207) baud = 7200;
  625. else if (baud <= 9612) baud = 9600;
  626. else if (baud <= 14428) baud = 14400;
  627. else if (baud <= 16062) baud = 16000;
  628. else if (baud <= 19250) baud = 19200;
  629. else if (baud <= 28912) baud = 28800;
  630. else if (baud <= 38601) baud = 38400;
  631. else if (baud <= 51558) baud = 51200;
  632. else if (baud <= 56280) baud = 56000;
  633. else if (baud <= 58053) baud = 57600;
  634. else if (baud <= 64111) baud = 64000;
  635. else if (baud <= 77608) baud = 76800;
  636. else if (baud <= 117028) baud = 115200;
  637. else if (baud <= 129347) baud = 128000;
  638. else if (baud <= 156868) baud = 153600;
  639. else if (baud <= 237832) baud = 230400;
  640. else if (baud <= 254234) baud = 250000;
  641. else if (baud <= 273066) baud = 256000;
  642. else if (baud <= 491520) baud = 460800;
  643. else if (baud <= 567138) baud = 500000;
  644. else if (baud <= 670254) baud = 576000;
  645. else if (baud < 1000000)
  646. baud = 921600;
  647. else if (baud > 2000000)
  648. baud = 2000000;
  649. return baud;
  650. }
  651.  
  652. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
  653. {
  654. int result;
  655.  
  656. result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
  657. if (result) {
  658. dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
  659. return result;
  660. }
  661.  
  662. /* Configure the termios structure */
  663. cp210x_get_termios(tty, port);
  664.  
  665. /* The baud rate must be initialised on cp2104 */
  666. if (tty)
  667. cp210x_change_speed(tty, port, NULL);
  668.  
  669. return usb_serial_generic_open(tty, port);
  670. }
  671.  
  672. static void cp210x_close(struct usb_serial_port *port)
  673. {
  674. usb_serial_generic_close(port);
  675.  
  676. /* Clear both queues; cp2108 needs this to avoid an occasional hang */
  677. cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
  678.  
  679. cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
  680. }
  681.  
  682. /*
  683. * Reads a variable-sized vendor-specific register identified by wValue
  684. * Returns data into buf in native USB byte order.
  685. */
  686. static int cp210x_read_vs_reg_block(struct usb_serial_port *port, u8 bmRequestType,
  687. u16 wValue, void *buf, int bufsize)
  688. {
  689. struct usb_serial *serial = port->serial;
  690. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  691. void *dmabuf;
  692. int result;
  693.  
  694. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  695. if (!dmabuf)
  696. return -ENOMEM;
  697.  
  698. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  699. CP210X_VENDOR_SPECIFIC, bmRequestType, wValue,
  700. port_priv->bInterfaceNumber, dmabuf, bufsize,
  701. USB_CTRL_SET_TIMEOUT);
  702. if (result == bufsize) {
  703. memcpy(buf, dmabuf, bufsize);
  704. result = 0;
  705. } else {
  706. dev_err(&port->dev, "failed get VENDOR_SPECIFIC wValue 0x%x size %d status: %d\n",
  707. wValue, bufsize, result);
  708. if (result >= 0)
  709. result = -EPROTO;
  710. }
  711.  
  712. kfree(dmabuf);
  713.  
  714. return result;
  715. }
  716.  
  717. /* GPIO register read from single-interface CP210x */
  718. static int cp210x_read_device_gpio_u8(struct usb_serial_port *port, u8 *val)
  719. {
  720. return cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST, CP210X_READ_LATCH, val, 1);
  721. }
  722.  
  723. /* GPIO register read from CP2105 */
  724. static int cp210x_read_interface_gpio_u8(struct usb_serial_port *port, u8 *val)
  725. {
  726. return cp210x_read_vs_reg_block(port, REQTYPE_INTERFACE_TO_HOST, CP210X_READ_LATCH, val, 1);
  727. }
  728.  
  729. /* GPIO register read from CP2108 */
  730. static int cp210x_read_device_gpio_u16(struct usb_serial_port *port, u16 *val)
  731. {
  732. __le16 le16_val;
  733. int err = cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST, CP210X_READ_LATCH, &le16_val, 2);
  734. if (err)
  735. return err;
  736.  
  737. *val = le16_to_cpu(le16_val);
  738. return 0;
  739. }
  740.  
  741. /* GPIO register write to single-interface CP210x */
  742. static int cp210x_write_device_gpio_u16(struct usb_serial_port *port, u16 val)
  743. {
  744. int result;
  745.  
  746. result = usb_control_msg(port->serial->dev,
  747. usb_sndctrlpipe(port->serial->dev, 0),
  748. CP210X_VENDOR_SPECIFIC,
  749. REQTYPE_HOST_TO_DEVICE,
  750. CP210X_WRITE_LATCH, /* wValue */
  751. val, /* wIndex */
  752. NULL, 0, USB_CTRL_SET_TIMEOUT);
  753. if (result != 0) {
  754. dev_err(&port->dev, "failed set WRITE_LATCH status: %d\n",
  755. result);
  756. if (result >= 0)
  757. result = -EPROTO;
  758. }
  759. return result;
  760. }
  761.  
  762. /*
  763. * Writes a variable-sized block of CP210X_ registers, identified by req.
  764. * Data in buf must be in native USB byte order.
  765. */
  766. static int cp210x_write_gpio_reg_block(struct usb_serial_port *port,
  767. u8 bmRequestType, void *buf, int bufsize)
  768. {
  769. struct usb_serial *serial = port->serial;
  770. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  771. void *dmabuf;
  772. int result;
  773.  
  774. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  775. if (!dmabuf)
  776. return -ENOMEM;
  777.  
  778. memcpy(dmabuf, buf, bufsize);
  779.  
  780. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  781. CP210X_VENDOR_SPECIFIC, bmRequestType,
  782. CP210X_WRITE_LATCH, /* wValue */
  783. port_priv->bInterfaceNumber, /* wIndex */
  784. dmabuf, bufsize,
  785. USB_CTRL_SET_TIMEOUT);
  786.  
  787. kfree(dmabuf);
  788.  
  789. if (result == bufsize) {
  790. result = 0;
  791. } else {
  792. dev_err(&port->dev, "failed set WRITE_LATCH size %d status: %d\n",
  793. bufsize, result);
  794. if (result >= 0)
  795. result = -EPROTO;
  796. }
  797.  
  798. return result;
  799. }
  800.  
  801. /* GPIO register write to CP2105 */
  802. static int cp210x_write_interface_gpio_u16(struct usb_serial_port *port, u16 val)
  803. {
  804. __le16 le16_val = cpu_to_le16(val);
  805.  
  806. return cp210x_write_gpio_reg_block(port, REQTYPE_HOST_TO_INTERFACE, &le16_val, 2);
  807. }
  808.  
  809. /* GPIO register write to CP2108 */
  810. static int cp210x_write_device_gpio_u32(struct usb_serial_port *port, u32 val)
  811. {
  812. __le32 le32_val = cpu_to_le32(val);
  813.  
  814. return cp210x_write_gpio_reg_block(port, REQTYPE_HOST_TO_DEVICE, &le32_val, 4);
  815. }
  816.  
  817. static int cp210x_ioctl(struct tty_struct *tty,
  818. unsigned int cmd, unsigned long arg)
  819. {
  820. struct usb_serial_port *port = tty->driver_data;
  821. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  822.  
  823. switch (cmd) {
  824. case IOCTL_GPIOGET:
  825. if ((port_priv->bPartNumber == CP2103_PARTNUM) ||
  826. (port_priv->bPartNumber == CP2104_PARTNUM) ||
  827. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN28) ||
  828. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN24) ||
  829. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN20)) {
  830. u8 gpio;
  831. int err = cp210x_read_device_gpio_u8(port, &gpio);
  832. if (err)
  833. return err;
  834. if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
  835. return -EFAULT;
  836. return 0;
  837. }
  838. else if (port_priv->bPartNumber == CP2105_PARTNUM) {
  839. u8 gpio;
  840. int err = cp210x_read_interface_gpio_u8(port, &gpio);
  841. if (err)
  842. return err;
  843. if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
  844. return -EFAULT;
  845. return 0;
  846. }
  847. else if (port_priv->bPartNumber == CP2108_PARTNUM) {
  848. u16 gpio;
  849. int err = cp210x_read_device_gpio_u16(port, &gpio);
  850. if (err)
  851. return err;
  852. if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
  853. return -EFAULT;
  854. return 0;
  855. }
  856. else {
  857. return -ENOTSUPP;
  858. }
  859. break;
  860. case IOCTL_GPIOSET:
  861. if ((port_priv->bPartNumber == CP2103_PARTNUM) ||
  862. (port_priv->bPartNumber == CP2104_PARTNUM) ||
  863. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN28) ||
  864. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN24) ||
  865. (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN20)) {
  866. u16 gpio;
  867. if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
  868. return -EFAULT;
  869. return cp210x_write_device_gpio_u16(port, gpio);
  870. }
  871. else if (port_priv->bPartNumber == CP2105_PARTNUM) {
  872. u16 gpio;
  873. if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
  874. return -EFAULT;
  875. return cp210x_write_interface_gpio_u16(port, gpio);
  876. }
  877. else if (port_priv->bPartNumber == CP2108_PARTNUM) {
  878. u32 gpio;
  879. if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
  880. return -EFAULT;
  881. return cp210x_write_device_gpio_u32(port, gpio);
  882. }
  883. else {
  884. return -ENOTSUPP;
  885. }
  886. break;
  887.  
  888. default:
  889. break;
  890. }
  891.  
  892. return -ENOIOCTLCMD;
  893. }
  894.  
  895. /*
  896. * Read how many bytes are waiting in the TX queue.
  897. */
  898. static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
  899. u32 *count)
  900. {
  901. struct usb_serial *serial = port->serial;
  902. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  903. struct cp210x_comm_status *sts;
  904. int result;
  905.  
  906. sts = kmalloc(sizeof(*sts), GFP_KERNEL);
  907. if (!sts)
  908. return -ENOMEM;
  909.  
  910. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  911. CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
  912. 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
  913. USB_CTRL_GET_TIMEOUT);
  914. if (result == sizeof(*sts)) {
  915. *count = le32_to_cpu(sts->ulAmountInOutQueue);
  916. result = 0;
  917. } else {
  918. dev_err(&port->dev, "failed to get comm status: %d\n", result);
  919. if (result >= 0)
  920. result = -EPROTO;
  921. }
  922.  
  923. kfree(sts);
  924.  
  925. return result;
  926. }
  927.  
  928. static bool cp210x_tx_empty(struct usb_serial_port *port)
  929. {
  930. int err;
  931. u32 count;
  932.  
  933. err = cp210x_get_tx_queue_byte_count(port, &count);
  934. if (err)
  935. return true;
  936.  
  937. return !count;
  938. }
  939.  
  940. /*
  941. * cp210x_get_termios
  942. * Reads the baud rate, data bits, parity, stop bits and flow control mode
  943. * from the device, corrects any unsupported values, and configures the
  944. * termios structure to reflect the state of the device
  945. */
  946. static void cp210x_get_termios(struct tty_struct *tty,
  947. struct usb_serial_port *port)
  948. {
  949. unsigned int baud;
  950.  
  951. if (tty) {
  952. cp210x_get_termios_port(tty->driver_data,
  953. &tty->termios.c_cflag, &baud);
  954. tty_encode_baud_rate(tty, baud, baud);
  955. } else {
  956. unsigned int cflag;
  957. cflag = 0;
  958. cp210x_get_termios_port(port, &cflag, &baud);
  959. }
  960. }
  961.  
  962. /*
  963. * cp210x_get_termios_port
  964. * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
  965. */
  966. static void cp210x_get_termios_port(struct usb_serial_port *port,
  967. unsigned int *cflagp, unsigned int *baudp)
  968. {
  969. struct device *dev = &port->dev;
  970. unsigned int cflag;
  971. struct cp210x_flow_ctl flow_ctl;
  972. u32 baud;
  973. u16 bits;
  974. u32 ctl_hs;
  975.  
  976. cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
  977.  
  978. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  979. *baudp = baud;
  980.  
  981. cflag = *cflagp;
  982.  
  983. cp210x_get_line_ctl(port, &bits);
  984. cflag &= ~CSIZE;
  985. switch (bits & BITS_DATA_MASK) {
  986. case BITS_DATA_5:
  987. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  988. cflag |= CS5;
  989. break;
  990. case BITS_DATA_6:
  991. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  992. cflag |= CS6;
  993. break;
  994. case BITS_DATA_7:
  995. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  996. cflag |= CS7;
  997. break;
  998. case BITS_DATA_8:
  999. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  1000. cflag |= CS8;
  1001. break;
  1002. case BITS_DATA_9:
  1003. dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
  1004. cflag |= CS8;
  1005. bits &= ~BITS_DATA_MASK;
  1006. bits |= BITS_DATA_8;
  1007. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  1008. break;
  1009. default:
  1010. dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
  1011. cflag |= CS8;
  1012. bits &= ~BITS_DATA_MASK;
  1013. bits |= BITS_DATA_8;
  1014. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  1015. break;
  1016. }
  1017.  
  1018. switch (bits & BITS_PARITY_MASK) {
  1019. case BITS_PARITY_NONE:
  1020. dev_dbg(dev, "%s - parity = NONE\n", __func__);
  1021. cflag &= ~PARENB;
  1022. break;
  1023. case BITS_PARITY_ODD:
  1024. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  1025. cflag |= (PARENB|PARODD);
  1026. break;
  1027. case BITS_PARITY_EVEN:
  1028. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  1029. cflag &= ~PARODD;
  1030. cflag |= PARENB;
  1031. break;
  1032. case BITS_PARITY_MARK:
  1033. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  1034. cflag |= (PARENB|PARODD|CMSPAR);
  1035. break;
  1036. case BITS_PARITY_SPACE:
  1037. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  1038. cflag &= ~PARODD;
  1039. cflag |= (PARENB|CMSPAR);
  1040. break;
  1041. default:
  1042. dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
  1043. cflag &= ~PARENB;
  1044. bits &= ~BITS_PARITY_MASK;
  1045. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  1046. break;
  1047. }
  1048.  
  1049. cflag &= ~CSTOPB;
  1050. switch (bits & BITS_STOP_MASK) {
  1051. case BITS_STOP_1:
  1052. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1053. break;
  1054. case BITS_STOP_1_5:
  1055. dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
  1056. bits &= ~BITS_STOP_MASK;
  1057. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  1058. break;
  1059. case BITS_STOP_2:
  1060. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1061. cflag |= CSTOPB;
  1062. break;
  1063. default:
  1064. dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
  1065. bits &= ~BITS_STOP_MASK;
  1066. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  1067. break;
  1068. }
  1069.  
  1070. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  1071. sizeof(flow_ctl));
  1072. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  1073. if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
  1074. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  1075. cflag |= CRTSCTS;
  1076. } else {
  1077. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  1078. cflag &= ~CRTSCTS;
  1079. }
  1080.  
  1081. *cflagp = cflag;
  1082. }
  1083.  
  1084. /*
  1085. * CP2101 supports the following baud rates:
  1086. *
  1087. * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
  1088. * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
  1089. *
  1090. * CP2102 and CP2103 support the following additional rates:
  1091. *
  1092. * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
  1093. * 576000
  1094. *
  1095. * The device will map a requested rate to a supported one, but the result
  1096. * of requests for rates greater than 1053257 is undefined (see AN205).
  1097. *
  1098. * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
  1099. * respectively, with an error less than 1%. The actual rates are determined
  1100. * by
  1101. *
  1102. * div = round(freq / (2 x prescale x request))
  1103. * actual = freq / (2 x prescale x div)
  1104. *
  1105. * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
  1106. * or 1 otherwise.
  1107. * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
  1108. * otherwise.
  1109. */
  1110. static void cp210x_change_speed(struct tty_struct *tty,
  1111. struct usb_serial_port *port, struct ktermios *old_termios)
  1112. {
  1113. u32 baud;
  1114.  
  1115. baud = tty->termios.c_ospeed;
  1116.  
  1117. /* This maps the requested rate to a rate valid on cp2102 or cp2103,
  1118. * or to an arbitrary rate in [1M,2M].
  1119. *
  1120. * NOTE: B0 is not implemented.
  1121. */
  1122. baud = cp210x_quantise_baudrate(baud);
  1123.  
  1124. dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
  1125. if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
  1126. dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
  1127. if (old_termios)
  1128. baud = old_termios->c_ospeed;
  1129. else
  1130. baud = 9600;
  1131. }
  1132.  
  1133. tty_encode_baud_rate(tty, baud, baud);
  1134. }
  1135.  
  1136. static void cp210x_set_termios(struct tty_struct *tty,
  1137. struct usb_serial_port *port, struct ktermios *old_termios)
  1138. {
  1139. struct device *dev = &port->dev;
  1140. unsigned int cflag, old_cflag;
  1141. u16 bits;
  1142.  
  1143. cflag = tty->termios.c_cflag;
  1144. old_cflag = old_termios->c_cflag;
  1145.  
  1146. if (tty->termios.c_ospeed != old_termios->c_ospeed)
  1147. cp210x_change_speed(tty, port, old_termios);
  1148.  
  1149. /* If the number of data bits is to be updated */
  1150. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  1151. cp210x_get_line_ctl(port, &bits);
  1152. bits &= ~BITS_DATA_MASK;
  1153. switch (cflag & CSIZE) {
  1154. case CS5:
  1155. bits |= BITS_DATA_5;
  1156. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  1157. break;
  1158. case CS6:
  1159. bits |= BITS_DATA_6;
  1160. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  1161. break;
  1162. case CS7:
  1163. bits |= BITS_DATA_7;
  1164. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  1165. break;
  1166. case CS8:
  1167. bits |= BITS_DATA_8;
  1168. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  1169. break;
  1170. /*case CS9:
  1171. bits |= BITS_DATA_9;
  1172. dev_dbg(dev, "%s - data bits = 9\n", __func__);
  1173. break;*/
  1174. default:
  1175. dev_dbg(dev, "cp210x driver does not support the number of bits requested, using 8 bit mode\n");
  1176. bits |= BITS_DATA_8;
  1177. break;
  1178. }
  1179. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1180. dev_dbg(dev, "Number of data bits requested not supported by device\n");
  1181. }
  1182.  
  1183. if ((cflag & (PARENB|PARODD|CMSPAR)) !=
  1184. (old_cflag & (PARENB|PARODD|CMSPAR))) {
  1185. cp210x_get_line_ctl(port, &bits);
  1186. bits &= ~BITS_PARITY_MASK;
  1187. if (cflag & PARENB) {
  1188. if (cflag & CMSPAR) {
  1189. if (cflag & PARODD) {
  1190. bits |= BITS_PARITY_MARK;
  1191. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  1192. } else {
  1193. bits |= BITS_PARITY_SPACE;
  1194. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  1195. }
  1196. } else {
  1197. if (cflag & PARODD) {
  1198. bits |= BITS_PARITY_ODD;
  1199. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  1200. } else {
  1201. bits |= BITS_PARITY_EVEN;
  1202. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  1203. }
  1204. }
  1205. }
  1206. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1207. dev_dbg(dev, "Parity mode not supported by device\n");
  1208. }
  1209.  
  1210. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  1211. cp210x_get_line_ctl(port, &bits);
  1212. bits &= ~BITS_STOP_MASK;
  1213. if (cflag & CSTOPB) {
  1214. bits |= BITS_STOP_2;
  1215. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1216. } else {
  1217. bits |= BITS_STOP_1;
  1218. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1219. }
  1220. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1221. dev_dbg(dev, "Number of stop bits requested not supported by device\n");
  1222. }
  1223.  
  1224. if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  1225. struct cp210x_flow_ctl flow_ctl;
  1226. u32 ctl_hs;
  1227. u32 flow_repl;
  1228.  
  1229. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  1230. sizeof(flow_ctl));
  1231. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  1232. flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
  1233. dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1234. __func__, ctl_hs, flow_repl);
  1235.  
  1236. ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
  1237. ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
  1238. ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
  1239. ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
  1240. ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
  1241. if (cflag & CRTSCTS) {
  1242. ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
  1243.  
  1244. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1245. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1246. CP210X_SERIAL_RTS_FLOW_CTL);
  1247. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  1248. } else {
  1249. ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
  1250.  
  1251. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1252. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1253. CP210X_SERIAL_RTS_ACTIVE);
  1254. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  1255. }
  1256.  
  1257. dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1258. __func__, ctl_hs, flow_repl);
  1259. flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
  1260. flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
  1261. cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
  1262. sizeof(flow_ctl));
  1263. }
  1264.  
  1265. }
  1266.  
  1267. static int cp210x_tiocmset(struct tty_struct *tty,
  1268. unsigned int set, unsigned int clear)
  1269. {
  1270. struct usb_serial_port *port = tty->driver_data;
  1271. return cp210x_tiocmset_port(port, set, clear);
  1272. }
  1273.  
  1274. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  1275. unsigned int set, unsigned int clear)
  1276. {
  1277. u16 control = 0;
  1278.  
  1279. if (set & TIOCM_RTS) {
  1280. control |= CONTROL_RTS;
  1281. control |= CONTROL_WRITE_RTS;
  1282. }
  1283. if (set & TIOCM_DTR) {
  1284. control |= CONTROL_DTR;
  1285. control |= CONTROL_WRITE_DTR;
  1286. }
  1287. if (clear & TIOCM_RTS) {
  1288. control &= ~CONTROL_RTS;
  1289. control |= CONTROL_WRITE_RTS;
  1290. }
  1291. if (clear & TIOCM_DTR) {
  1292. control &= ~CONTROL_DTR;
  1293. control |= CONTROL_WRITE_DTR;
  1294. }
  1295.  
  1296. dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
  1297.  
  1298. return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
  1299. }
  1300.  
  1301. static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
  1302. {
  1303. if (on)
  1304. cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
  1305. else
  1306. cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
  1307. }
  1308.  
  1309. static int cp210x_tiocmget(struct tty_struct *tty)
  1310. {
  1311. struct usb_serial_port *port = tty->driver_data;
  1312. u8 control;
  1313. int result;
  1314.  
  1315. cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
  1316.  
  1317. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  1318. |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  1319. |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
  1320. |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
  1321. |((control & CONTROL_RING)? TIOCM_RI : 0)
  1322. |((control & CONTROL_DCD) ? TIOCM_CD : 0);
  1323.  
  1324. dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
  1325.  
  1326. return result;
  1327. }
  1328.  
  1329. static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
  1330. {
  1331. struct usb_serial_port *port = tty->driver_data;
  1332. u16 state;
  1333.  
  1334. if (break_state == 0)
  1335. state = BREAK_OFF;
  1336. else
  1337. state = BREAK_ON;
  1338. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  1339. state == BREAK_OFF ? "off" : "on");
  1340. cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
  1341. }
  1342.  
  1343. static int cp210x_port_probe(struct usb_serial_port *port)
  1344. {
  1345. struct usb_serial *serial = port->serial;
  1346. struct usb_host_interface *cur_altsetting;
  1347. struct cp210x_port_private *port_priv;
  1348. int ret;
  1349.  
  1350. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  1351. if (!port_priv)
  1352. return -ENOMEM;
  1353.  
  1354. cur_altsetting = serial->interface->cur_altsetting;
  1355. port_priv->bInterfaceNumber = cur_altsetting->desc.bInterfaceNumber;
  1356.  
  1357. usb_set_serial_port_data(port, port_priv);
  1358.  
  1359. ret = cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST,
  1360. CP210X_GET_PARTNUM, &port_priv->bPartNumber, 1);
  1361. if (ret) {
  1362. kfree(port_priv);
  1363. return ret;
  1364. }
  1365.  
  1366. ret = cp210x_detect_swapped_line_ctl(port);
  1367. if (ret) {
  1368. kfree(port_priv);
  1369. return ret;
  1370. }
  1371.  
  1372. return 0;
  1373. }
  1374.  
  1375. static int cp210x_port_remove(struct usb_serial_port *port)
  1376. {
  1377. struct cp210x_port_private *port_priv;
  1378.  
  1379. port_priv = usb_get_serial_port_data(port);
  1380. kfree(port_priv);
  1381.  
  1382. return 0;
  1383. }
  1384.  
  1385. module_usb_serial_driver(serial_drivers, id_table);
  1386.  
  1387. MODULE_DESCRIPTION(DRIVER_DESC);
  1388. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement