cp210x.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/usb.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/usb/serial.h>
  25. #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
  26. /*
  27. * Function Prototypes
  28. */
  29. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
  30. static void cp210x_close(struct usb_serial_port *);
  31. static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
  32. static void cp210x_get_termios_port(struct usb_serial_port *port,
  33. unsigned int *cflagp, unsigned int *baudp);
  34. static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
  35. struct ktermios *);
  36. static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
  37. struct ktermios*);
  38. static bool cp210x_tx_empty(struct usb_serial_port *port);
  39. static int cp210x_tiocmget(struct tty_struct *);
  40. static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
  41. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  42. unsigned int, unsigned int);
  43. static void cp210x_break_ctl(struct tty_struct *, int);
  44. static int cp210x_port_probe(struct usb_serial_port *);
  45. static int cp210x_port_remove(struct usb_serial_port *);
  46. static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
  47. static const struct usb_device_id id_table[] = {
  48. { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
  49. { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
  50. { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  51. { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  52. { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
  53. { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
  54. { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
  55. { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
  56. { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
  57. { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
  58. { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
  59. { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
  60. { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
  61. { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
  62. { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
  63. { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
  64. { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
  65. { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
  66. { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
  67. { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
  68. { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
  69. { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
  70. { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
  71. { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
  72. { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
  73. { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
  74. { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
  75. { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
  76. { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
  77. { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
  78. { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
  79. { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
  80. { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
  81. { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
  82. { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
  83. { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
  84. { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
  85. { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
  86. { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
  87. { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
  88. { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
  89. { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
  90. { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
  91. { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
  92. { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
  93. { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
  94. { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
  95. { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
  96. { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
  97. { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
  98. { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
  99. { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
  100. { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
  101. { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
  102. { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
  103. { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
  104. { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
  105. { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
  106. { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
  107. { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
  108. { USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
  109. { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
  110. { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
  111. { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
  112. { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
  113. { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
  114. { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
  115. { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
  116. { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
  117. { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
  118. { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
  119. { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
  120. { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
  121. { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
  122. { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
  123. { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
  124. { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
  125. { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
  126. { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
  127. { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
  128. { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
  129. { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
  130. { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
  131. { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
  132. { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
  133. { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
  134. { USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
  135. { USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
  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(0x12B8, 0xEC60) }, /* Link G4 ECU */
  147. { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
  148. { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
  149. { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
  150. { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
  151. { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
  152. { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
  153. { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
  154. { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
  155. { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
  156. { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
  157. { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
  158. { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
  159. { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
  160. { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
  161. { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
  162. { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
  163. { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
  164. { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
  165. { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
  166. { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
  167. { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
  168. { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
  169. { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
  170. { USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
  171. { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
  172. { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
  173. { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
  174. { USB_DEVICE(0x1901, 0x0195) }, /* GE B850/B650/B450 CP2104 DP UART interface */
  175. { USB_DEVICE(0x1901, 0x0196) }, /* GE B850 CP2105 DP UART interface */
  176. { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
  177. { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
  178. { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
  179. { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
  180. { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
  181. { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
  182. { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
  183. { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
  184. { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
  185. { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
  186. { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
  187. { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
  188. { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
  189. { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
  190. { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
  191. { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
  192. { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
  193. { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
  194. { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
  195. { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
  196. { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
  197. { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
  198. { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
  199. { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
  200. { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
  201. { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
  202. { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
  203. { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
  204. { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
  205. { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
  206. { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
  207. { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
  208. { } /* Terminating Entry */
  209. };
  210. MODULE_DEVICE_TABLE(usb, id_table);
  211. struct cp210x_port_private {
  212. __u8 bInterfaceNumber;
  213. bool has_swapped_line_ctl;
  214. };
  215. static struct usb_serial_driver cp210x_device = {
  216. .driver = {
  217. .owner = THIS_MODULE,
  218. .name = "cp210x",
  219. },
  220. .id_table = id_table,
  221. .num_ports = 1,
  222. .bulk_in_size = 256,
  223. .bulk_out_size = 256,
  224. .open = cp210x_open,
  225. .close = cp210x_close,
  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. static struct usb_serial_driver * const serial_drivers[] = {
  236. &cp210x_device, NULL
  237. };
  238. /* Config request types */
  239. #define REQTYPE_HOST_TO_INTERFACE 0x41
  240. #define REQTYPE_INTERFACE_TO_HOST 0xc1
  241. #define REQTYPE_HOST_TO_DEVICE 0x40
  242. #define REQTYPE_DEVICE_TO_HOST 0xc0
  243. /* Config request codes */
  244. #define CP210X_IFC_ENABLE 0x00
  245. #define CP210X_SET_BAUDDIV 0x01
  246. #define CP210X_GET_BAUDDIV 0x02
  247. #define CP210X_SET_LINE_CTL 0x03
  248. #define CP210X_GET_LINE_CTL 0x04
  249. #define CP210X_SET_BREAK 0x05
  250. #define CP210X_IMM_CHAR 0x06
  251. #define CP210X_SET_MHS 0x07
  252. #define CP210X_GET_MDMSTS 0x08
  253. #define CP210X_SET_XON 0x09
  254. #define CP210X_SET_XOFF 0x0A
  255. #define CP210X_SET_EVENTMASK 0x0B
  256. #define CP210X_GET_EVENTMASK 0x0C
  257. #define CP210X_SET_CHAR 0x0D
  258. #define CP210X_GET_CHARS 0x0E
  259. #define CP210X_GET_PROPS 0x0F
  260. #define CP210X_GET_COMM_STATUS 0x10
  261. #define CP210X_RESET 0x11
  262. #define CP210X_PURGE 0x12
  263. #define CP210X_SET_FLOW 0x13
  264. #define CP210X_GET_FLOW 0x14
  265. #define CP210X_EMBED_EVENTS 0x15
  266. #define CP210X_GET_EVENTSTATE 0x16
  267. #define CP210X_SET_CHARS 0x19
  268. #define CP210X_GET_BAUDRATE 0x1D
  269. #define CP210X_SET_BAUDRATE 0x1E
  270. /* CP210X_IFC_ENABLE */
  271. #define UART_ENABLE 0x0001
  272. #define UART_DISABLE 0x0000
  273. /* CP210X_(SET|GET)_BAUDDIV */
  274. #define BAUD_RATE_GEN_FREQ 0x384000
  275. /* CP210X_(SET|GET)_LINE_CTL */
  276. #define BITS_DATA_MASK 0X0f00
  277. #define BITS_DATA_5 0X0500
  278. #define BITS_DATA_6 0X0600
  279. #define BITS_DATA_7 0X0700
  280. #define BITS_DATA_8 0X0800
  281. #define BITS_DATA_9 0X0900
  282. #define BITS_PARITY_MASK 0x00f0
  283. #define BITS_PARITY_NONE 0x0000
  284. #define BITS_PARITY_ODD 0x0010
  285. #define BITS_PARITY_EVEN 0x0020
  286. #define BITS_PARITY_MARK 0x0030
  287. #define BITS_PARITY_SPACE 0x0040
  288. #define BITS_STOP_MASK 0x000f
  289. #define BITS_STOP_1 0x0000
  290. #define BITS_STOP_1_5 0x0001
  291. #define BITS_STOP_2 0x0002
  292. /* CP210X_SET_BREAK */
  293. #define BREAK_ON 0x0001
  294. #define BREAK_OFF 0x0000
  295. /* CP210X_(SET_MHS|GET_MDMSTS) */
  296. #define CONTROL_DTR 0x0001
  297. #define CONTROL_RTS 0x0002
  298. #define CONTROL_CTS 0x0010
  299. #define CONTROL_DSR 0x0020
  300. #define CONTROL_RING 0x0040
  301. #define CONTROL_DCD 0x0080
  302. #define CONTROL_WRITE_DTR 0x0100
  303. #define CONTROL_WRITE_RTS 0x0200
  304. /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
  305. struct cp210x_comm_status {
  306. __le32 ulErrors;
  307. __le32 ulHoldReasons;
  308. __le32 ulAmountInInQueue;
  309. __le32 ulAmountInOutQueue;
  310. u8 bEofReceived;
  311. u8 bWaitForImmediate;
  312. u8 bReserved;
  313. } __packed;
  314. /*
  315. * CP210X_PURGE - 16 bits passed in wValue of USB request.
  316. * SiLabs app note AN571 gives a strange description of the 4 bits:
  317. * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
  318. * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
  319. */
  320. #define PURGE_ALL 0x000f
  321. /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
  322. struct cp210x_flow_ctl {
  323. __le32 ulControlHandshake;
  324. __le32 ulFlowReplace;
  325. __le32 ulXonLimit;
  326. __le32 ulXoffLimit;
  327. } __packed;
  328. /* cp210x_flow_ctl::ulControlHandshake */
  329. #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
  330. #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
  331. #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
  332. #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
  333. #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
  334. #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
  335. /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
  336. #define CP210X_SERIAL_DTR_INACTIVE 0
  337. #define CP210X_SERIAL_DTR_ACTIVE 1
  338. #define CP210X_SERIAL_DTR_FLOW_CTL 2
  339. /* cp210x_flow_ctl::ulFlowReplace */
  340. #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
  341. #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
  342. #define CP210X_SERIAL_ERROR_CHAR BIT(2)
  343. #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
  344. #define CP210X_SERIAL_BREAK_CHAR BIT(4)
  345. #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
  346. #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
  347. #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
  348. /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
  349. #define CP210X_SERIAL_RTS_INACTIVE 0
  350. #define CP210X_SERIAL_RTS_ACTIVE 1
  351. #define CP210X_SERIAL_RTS_FLOW_CTL 2
  352. /*
  353. * Reads a variable-sized block of CP210X_ registers, identified by req.
  354. * Returns data into buf in native USB byte order.
  355. */
  356. static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
  357. void *buf, int bufsize)
  358. {
  359. struct usb_serial *serial = port->serial;
  360. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  361. void *dmabuf;
  362. int result;
  363. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  364. if (!dmabuf) {
  365. /*
  366. * FIXME Some callers don't bother to check for error,
  367. * at least give them consistent junk until they are fixed
  368. */
  369. memset(buf, 0, bufsize);
  370. return -ENOMEM;
  371. }
  372. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  373. req, REQTYPE_INTERFACE_TO_HOST, 0,
  374. port_priv->bInterfaceNumber, dmabuf, bufsize,
  375. USB_CTRL_SET_TIMEOUT);
  376. if (result == bufsize) {
  377. memcpy(buf, dmabuf, bufsize);
  378. result = 0;
  379. } else {
  380. dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
  381. req, bufsize, result);
  382. if (result >= 0)
  383. result = -EPROTO;
  384. /*
  385. * FIXME Some callers don't bother to check for error,
  386. * at least give them consistent junk until they are fixed
  387. */
  388. memset(buf, 0, bufsize);
  389. }
  390. kfree(dmabuf);
  391. return result;
  392. }
  393. /*
  394. * Reads any 32-bit CP210X_ register identified by req.
  395. */
  396. static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
  397. {
  398. __le32 le32_val;
  399. int err;
  400. err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
  401. if (err) {
  402. /*
  403. * FIXME Some callers don't bother to check for error,
  404. * at least give them consistent junk until they are fixed
  405. */
  406. *val = 0;
  407. return err;
  408. }
  409. *val = le32_to_cpu(le32_val);
  410. return 0;
  411. }
  412. /*
  413. * Reads any 16-bit CP210X_ register identified by req.
  414. */
  415. static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
  416. {
  417. __le16 le16_val;
  418. int err;
  419. err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
  420. if (err)
  421. return err;
  422. *val = le16_to_cpu(le16_val);
  423. return 0;
  424. }
  425. /*
  426. * Reads any 8-bit CP210X_ register identified by req.
  427. */
  428. static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
  429. {
  430. return cp210x_read_reg_block(port, req, val, sizeof(*val));
  431. }
  432. /*
  433. * Writes any 16-bit CP210X_ register (req) whose value is passed
  434. * entirely in the wValue field of the USB request.
  435. */
  436. static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
  437. {
  438. struct usb_serial *serial = port->serial;
  439. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  440. int result;
  441. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  442. req, REQTYPE_HOST_TO_INTERFACE, val,
  443. port_priv->bInterfaceNumber, NULL, 0,
  444. USB_CTRL_SET_TIMEOUT);
  445. if (result < 0) {
  446. dev_err(&port->dev, "failed set request 0x%x status: %d\n",
  447. req, result);
  448. }
  449. return result;
  450. }
  451. /*
  452. * Writes a variable-sized block of CP210X_ registers, identified by req.
  453. * Data in buf must be in native USB byte order.
  454. */
  455. static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
  456. void *buf, int bufsize)
  457. {
  458. struct usb_serial *serial = port->serial;
  459. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  460. void *dmabuf;
  461. int result;
  462. dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
  463. if (!dmabuf)
  464. return -ENOMEM;
  465. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  466. req, REQTYPE_HOST_TO_INTERFACE, 0,
  467. port_priv->bInterfaceNumber, dmabuf, bufsize,
  468. USB_CTRL_SET_TIMEOUT);
  469. kfree(dmabuf);
  470. if (result == bufsize) {
  471. result = 0;
  472. } else {
  473. dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
  474. req, bufsize, result);
  475. if (result >= 0)
  476. result = -EPROTO;
  477. }
  478. return result;
  479. }
  480. /*
  481. * Writes any 32-bit CP210X_ register identified by req.
  482. */
  483. static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
  484. {
  485. __le32 le32_val;
  486. le32_val = cpu_to_le32(val);
  487. return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
  488. }
  489. /*
  490. * Detect CP2108 GET_LINE_CTL bug and activate workaround.
  491. * Write a known good value 0x800, read it back.
  492. * If it comes back swapped the bug is detected.
  493. * Preserve the original register value.
  494. */
  495. static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
  496. {
  497. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  498. u16 line_ctl_save;
  499. u16 line_ctl_test;
  500. int err;
  501. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
  502. if (err)
  503. return err;
  504. err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
  505. if (err)
  506. return err;
  507. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
  508. if (err)
  509. return err;
  510. if (line_ctl_test == 8) {
  511. port_priv->has_swapped_line_ctl = true;
  512. line_ctl_save = swab16(line_ctl_save);
  513. }
  514. return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
  515. }
  516. /*
  517. * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
  518. * to workaround cp2108 bug and get correct value.
  519. */
  520. static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
  521. {
  522. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  523. int err;
  524. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
  525. if (err)
  526. return err;
  527. /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
  528. if (port_priv->has_swapped_line_ctl)
  529. *ctl = swab16(*ctl);
  530. return 0;
  531. }
  532. /*
  533. * cp210x_quantise_baudrate
  534. * Quantises the baud rate as per AN205 Table 1
  535. */
  536. static unsigned int cp210x_quantise_baudrate(unsigned int baud)
  537. {
  538. if (baud <= 300)
  539. baud = 300;
  540. else if (baud <= 600) baud = 600;
  541. else if (baud <= 1200) baud = 1200;
  542. else if (baud <= 1800) baud = 1800;
  543. else if (baud <= 2400) baud = 2400;
  544. else if (baud <= 4000) baud = 4000;
  545. else if (baud <= 4803) baud = 4800;
  546. else if (baud <= 7207) baud = 7200;
  547. else if (baud <= 9612) baud = 9600;
  548. else if (baud <= 14428) baud = 14400;
  549. else if (baud <= 16062) baud = 16000;
  550. else if (baud <= 19250) baud = 19200;
  551. else if (baud <= 28912) baud = 28800;
  552. else if (baud <= 38601) baud = 38400;
  553. else if (baud <= 51558) baud = 51200;
  554. else if (baud <= 56280) baud = 56000;
  555. else if (baud <= 58053) baud = 57600;
  556. else if (baud <= 64111) baud = 64000;
  557. else if (baud <= 77608) baud = 76800;
  558. else if (baud <= 117028) baud = 115200;
  559. else if (baud <= 129347) baud = 128000;
  560. else if (baud <= 156868) baud = 153600;
  561. else if (baud <= 237832) baud = 230400;
  562. else if (baud <= 254234) baud = 250000;
  563. else if (baud <= 273066) baud = 256000;
  564. else if (baud <= 491520) baud = 460800;
  565. else if (baud <= 567138) baud = 500000;
  566. else if (baud <= 670254) baud = 576000;
  567. else if (baud < 1000000)
  568. baud = 921600;
  569. else if (baud > 2000000)
  570. baud = 2000000;
  571. return baud;
  572. }
  573. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
  574. {
  575. int result;
  576. result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
  577. if (result) {
  578. dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
  579. return result;
  580. }
  581. /* Configure the termios structure */
  582. cp210x_get_termios(tty, port);
  583. /* The baud rate must be initialised on cp2104 */
  584. if (tty)
  585. cp210x_change_speed(tty, port, NULL);
  586. return usb_serial_generic_open(tty, port);
  587. }
  588. static void cp210x_close(struct usb_serial_port *port)
  589. {
  590. usb_serial_generic_close(port);
  591. /* Clear both queues; cp2108 needs this to avoid an occasional hang */
  592. cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
  593. cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
  594. }
  595. /*
  596. * Read how many bytes are waiting in the TX queue.
  597. */
  598. static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
  599. u32 *count)
  600. {
  601. struct usb_serial *serial = port->serial;
  602. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  603. struct cp210x_comm_status *sts;
  604. int result;
  605. sts = kmalloc(sizeof(*sts), GFP_KERNEL);
  606. if (!sts)
  607. return -ENOMEM;
  608. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  609. CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
  610. 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
  611. USB_CTRL_GET_TIMEOUT);
  612. if (result == sizeof(*sts)) {
  613. *count = le32_to_cpu(sts->ulAmountInOutQueue);
  614. result = 0;
  615. } else {
  616. dev_err(&port->dev, "failed to get comm status: %d\n", result);
  617. if (result >= 0)
  618. result = -EPROTO;
  619. }
  620. kfree(sts);
  621. return result;
  622. }
  623. static bool cp210x_tx_empty(struct usb_serial_port *port)
  624. {
  625. int err;
  626. u32 count;
  627. err = cp210x_get_tx_queue_byte_count(port, &count);
  628. if (err)
  629. return true;
  630. return !count;
  631. }
  632. /*
  633. * cp210x_get_termios
  634. * Reads the baud rate, data bits, parity, stop bits and flow control mode
  635. * from the device, corrects any unsupported values, and configures the
  636. * termios structure to reflect the state of the device
  637. */
  638. static void cp210x_get_termios(struct tty_struct *tty,
  639. struct usb_serial_port *port)
  640. {
  641. unsigned int baud;
  642. if (tty) {
  643. cp210x_get_termios_port(tty->driver_data,
  644. &tty->termios.c_cflag, &baud);
  645. tty_encode_baud_rate(tty, baud, baud);
  646. } else {
  647. unsigned int cflag;
  648. cflag = 0;
  649. cp210x_get_termios_port(port, &cflag, &baud);
  650. }
  651. }
  652. /*
  653. * cp210x_get_termios_port
  654. * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
  655. */
  656. static void cp210x_get_termios_port(struct usb_serial_port *port,
  657. unsigned int *cflagp, unsigned int *baudp)
  658. {
  659. struct device *dev = &port->dev;
  660. unsigned int cflag;
  661. struct cp210x_flow_ctl flow_ctl;
  662. u32 baud;
  663. u16 bits;
  664. u32 ctl_hs;
  665. cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
  666. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  667. *baudp = baud;
  668. cflag = *cflagp;
  669. cp210x_get_line_ctl(port, &bits);
  670. cflag &= ~CSIZE;
  671. switch (bits & BITS_DATA_MASK) {
  672. case BITS_DATA_5:
  673. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  674. cflag |= CS5;
  675. break;
  676. case BITS_DATA_6:
  677. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  678. cflag |= CS6;
  679. break;
  680. case BITS_DATA_7:
  681. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  682. cflag |= CS7;
  683. break;
  684. case BITS_DATA_8:
  685. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  686. cflag |= CS8;
  687. break;
  688. case BITS_DATA_9:
  689. dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
  690. cflag |= CS8;
  691. bits &= ~BITS_DATA_MASK;
  692. bits |= BITS_DATA_8;
  693. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  694. break;
  695. default:
  696. dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
  697. cflag |= CS8;
  698. bits &= ~BITS_DATA_MASK;
  699. bits |= BITS_DATA_8;
  700. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  701. break;
  702. }
  703. switch (bits & BITS_PARITY_MASK) {
  704. case BITS_PARITY_NONE:
  705. dev_dbg(dev, "%s - parity = NONE\n", __func__);
  706. cflag &= ~PARENB;
  707. break;
  708. case BITS_PARITY_ODD:
  709. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  710. cflag |= (PARENB|PARODD);
  711. break;
  712. case BITS_PARITY_EVEN:
  713. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  714. cflag &= ~PARODD;
  715. cflag |= PARENB;
  716. break;
  717. case BITS_PARITY_MARK:
  718. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  719. cflag |= (PARENB|PARODD|CMSPAR);
  720. break;
  721. case BITS_PARITY_SPACE:
  722. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  723. cflag &= ~PARODD;
  724. cflag |= (PARENB|CMSPAR);
  725. break;
  726. default:
  727. dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
  728. cflag &= ~PARENB;
  729. bits &= ~BITS_PARITY_MASK;
  730. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  731. break;
  732. }
  733. cflag &= ~CSTOPB;
  734. switch (bits & BITS_STOP_MASK) {
  735. case BITS_STOP_1:
  736. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  737. break;
  738. case BITS_STOP_1_5:
  739. dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
  740. bits &= ~BITS_STOP_MASK;
  741. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  742. break;
  743. case BITS_STOP_2:
  744. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  745. cflag |= CSTOPB;
  746. break;
  747. default:
  748. dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
  749. bits &= ~BITS_STOP_MASK;
  750. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  751. break;
  752. }
  753. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  754. sizeof(flow_ctl));
  755. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  756. if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
  757. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  758. cflag |= CRTSCTS;
  759. } else {
  760. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  761. cflag &= ~CRTSCTS;
  762. }
  763. *cflagp = cflag;
  764. }
  765. /*
  766. * CP2101 supports the following baud rates:
  767. *
  768. * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
  769. * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
  770. *
  771. * CP2102 and CP2103 support the following additional rates:
  772. *
  773. * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
  774. * 576000
  775. *
  776. * The device will map a requested rate to a supported one, but the result
  777. * of requests for rates greater than 1053257 is undefined (see AN205).
  778. *
  779. * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
  780. * respectively, with an error less than 1%. The actual rates are determined
  781. * by
  782. *
  783. * div = round(freq / (2 x prescale x request))
  784. * actual = freq / (2 x prescale x div)
  785. *
  786. * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
  787. * or 1 otherwise.
  788. * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
  789. * otherwise.
  790. */
  791. static void cp210x_change_speed(struct tty_struct *tty,
  792. struct usb_serial_port *port, struct ktermios *old_termios)
  793. {
  794. u32 baud;
  795. baud = tty->termios.c_ospeed;
  796. /* This maps the requested rate to a rate valid on cp2102 or cp2103,
  797. * or to an arbitrary rate in [1M,2M].
  798. *
  799. * NOTE: B0 is not implemented.
  800. */
  801. baud = cp210x_quantise_baudrate(baud);
  802. dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
  803. if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
  804. dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
  805. if (old_termios)
  806. baud = old_termios->c_ospeed;
  807. else
  808. baud = 9600;
  809. }
  810. tty_encode_baud_rate(tty, baud, baud);
  811. }
  812. static void cp210x_set_termios(struct tty_struct *tty,
  813. struct usb_serial_port *port, struct ktermios *old_termios)
  814. {
  815. struct device *dev = &port->dev;
  816. unsigned int cflag, old_cflag;
  817. u16 bits;
  818. cflag = tty->termios.c_cflag;
  819. old_cflag = old_termios->c_cflag;
  820. if (tty->termios.c_ospeed != old_termios->c_ospeed)
  821. cp210x_change_speed(tty, port, old_termios);
  822. /* If the number of data bits is to be updated */
  823. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  824. cp210x_get_line_ctl(port, &bits);
  825. bits &= ~BITS_DATA_MASK;
  826. switch (cflag & CSIZE) {
  827. case CS5:
  828. bits |= BITS_DATA_5;
  829. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  830. break;
  831. case CS6:
  832. bits |= BITS_DATA_6;
  833. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  834. break;
  835. case CS7:
  836. bits |= BITS_DATA_7;
  837. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  838. break;
  839. case CS8:
  840. bits |= BITS_DATA_8;
  841. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  842. break;
  843. /*case CS9:
  844. bits |= BITS_DATA_9;
  845. dev_dbg(dev, "%s - data bits = 9\n", __func__);
  846. break;*/
  847. default:
  848. dev_dbg(dev, "cp210x driver does not support the number of bits requested, using 8 bit mode\n");
  849. bits |= BITS_DATA_8;
  850. break;
  851. }
  852. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  853. dev_dbg(dev, "Number of data bits requested not supported by device\n");
  854. }
  855. if ((cflag & (PARENB|PARODD|CMSPAR)) !=
  856. (old_cflag & (PARENB|PARODD|CMSPAR))) {
  857. cp210x_get_line_ctl(port, &bits);
  858. bits &= ~BITS_PARITY_MASK;
  859. if (cflag & PARENB) {
  860. if (cflag & CMSPAR) {
  861. if (cflag & PARODD) {
  862. bits |= BITS_PARITY_MARK;
  863. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  864. } else {
  865. bits |= BITS_PARITY_SPACE;
  866. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  867. }
  868. } else {
  869. if (cflag & PARODD) {
  870. bits |= BITS_PARITY_ODD;
  871. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  872. } else {
  873. bits |= BITS_PARITY_EVEN;
  874. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  875. }
  876. }
  877. }
  878. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  879. dev_dbg(dev, "Parity mode not supported by device\n");
  880. }
  881. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  882. cp210x_get_line_ctl(port, &bits);
  883. bits &= ~BITS_STOP_MASK;
  884. if (cflag & CSTOPB) {
  885. bits |= BITS_STOP_2;
  886. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  887. } else {
  888. bits |= BITS_STOP_1;
  889. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  890. }
  891. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  892. dev_dbg(dev, "Number of stop bits requested not supported by device\n");
  893. }
  894. if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  895. struct cp210x_flow_ctl flow_ctl;
  896. u32 ctl_hs;
  897. u32 flow_repl;
  898. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  899. sizeof(flow_ctl));
  900. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  901. flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
  902. dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  903. __func__, ctl_hs, flow_repl);
  904. ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
  905. ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
  906. ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
  907. ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
  908. ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
  909. if (cflag & CRTSCTS) {
  910. ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
  911. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  912. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  913. CP210X_SERIAL_RTS_FLOW_CTL);
  914. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  915. } else {
  916. ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
  917. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  918. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  919. CP210X_SERIAL_RTS_ACTIVE);
  920. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  921. }
  922. dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  923. __func__, ctl_hs, flow_repl);
  924. flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
  925. flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
  926. cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
  927. sizeof(flow_ctl));
  928. }
  929. }
  930. static int cp210x_tiocmset(struct tty_struct *tty,
  931. unsigned int set, unsigned int clear)
  932. {
  933. struct usb_serial_port *port = tty->driver_data;
  934. return cp210x_tiocmset_port(port, set, clear);
  935. }
  936. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  937. unsigned int set, unsigned int clear)
  938. {
  939. u16 control = 0;
  940. if (set & TIOCM_RTS) {
  941. control |= CONTROL_RTS;
  942. control |= CONTROL_WRITE_RTS;
  943. }
  944. if (set & TIOCM_DTR) {
  945. control |= CONTROL_DTR;
  946. control |= CONTROL_WRITE_DTR;
  947. }
  948. if (clear & TIOCM_RTS) {
  949. control &= ~CONTROL_RTS;
  950. control |= CONTROL_WRITE_RTS;
  951. }
  952. if (clear & TIOCM_DTR) {
  953. control &= ~CONTROL_DTR;
  954. control |= CONTROL_WRITE_DTR;
  955. }
  956. dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
  957. return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
  958. }
  959. static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
  960. {
  961. if (on)
  962. cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
  963. else
  964. cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
  965. }
  966. static int cp210x_tiocmget(struct tty_struct *tty)
  967. {
  968. struct usb_serial_port *port = tty->driver_data;
  969. u8 control;
  970. int result;
  971. result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
  972. if (result)
  973. return result;
  974. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  975. |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  976. |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
  977. |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
  978. |((control & CONTROL_RING)? TIOCM_RI : 0)
  979. |((control & CONTROL_DCD) ? TIOCM_CD : 0);
  980. dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
  981. return result;
  982. }
  983. static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
  984. {
  985. struct usb_serial_port *port = tty->driver_data;
  986. u16 state;
  987. if (break_state == 0)
  988. state = BREAK_OFF;
  989. else
  990. state = BREAK_ON;
  991. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  992. state == BREAK_OFF ? "off" : "on");
  993. cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
  994. }
  995. static int cp210x_port_probe(struct usb_serial_port *port)
  996. {
  997. struct usb_serial *serial = port->serial;
  998. struct usb_host_interface *cur_altsetting;
  999. struct cp210x_port_private *port_priv;
  1000. int ret;
  1001. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  1002. if (!port_priv)
  1003. return -ENOMEM;
  1004. cur_altsetting = serial->interface->cur_altsetting;
  1005. port_priv->bInterfaceNumber = cur_altsetting->desc.bInterfaceNumber;
  1006. usb_set_serial_port_data(port, port_priv);
  1007. ret = cp210x_detect_swapped_line_ctl(port);
  1008. if (ret) {
  1009. kfree(port_priv);
  1010. return ret;
  1011. }
  1012. return 0;
  1013. }
  1014. static int cp210x_port_remove(struct usb_serial_port *port)
  1015. {
  1016. struct cp210x_port_private *port_priv;
  1017. port_priv = usb_get_serial_port_data(port);
  1018. kfree(port_priv);
  1019. return 0;
  1020. }
  1021. module_usb_serial_driver(serial_drivers, id_table);
  1022. MODULE_DESCRIPTION(DRIVER_DESC);
  1023. MODULE_LICENSE("GPL");