cp210x.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. #include <linux/gpio/driver.h>
  26. #include <linux/bitops.h>
  27. #include <linux/mutex.h>
  28. #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
  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 void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
  35. static void cp210x_get_termios_port(struct usb_serial_port *port,
  36. tcflag_t *cflagp, unsigned int *baudp);
  37. static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
  38. struct ktermios *);
  39. static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
  40. struct ktermios*);
  41. static bool cp210x_tx_empty(struct usb_serial_port *port);
  42. static int cp210x_tiocmget(struct tty_struct *);
  43. static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
  44. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  45. unsigned int, unsigned int);
  46. static void cp210x_break_ctl(struct tty_struct *, int);
  47. static int cp210x_attach(struct usb_serial *);
  48. static void cp210x_disconnect(struct usb_serial *);
  49. static void cp210x_release(struct usb_serial *);
  50. static int cp210x_port_probe(struct usb_serial_port *);
  51. static int cp210x_port_remove(struct usb_serial_port *);
  52. static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
  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, 0x82F4) }, /* Starizona MicroTouch */
  115. { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
  116. { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
  117. { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
  118. { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
  119. { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
  120. { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
  121. { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
  122. { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
  123. { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
  124. { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
  125. { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
  126. { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
  127. { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
  128. { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
  129. { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
  130. { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
  131. { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
  132. { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
  133. { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
  134. { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
  135. { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
  136. { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
  137. { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
  138. { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
  139. { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
  140. { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
  141. { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
  142. { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
  143. { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
  144. { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
  145. { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
  146. { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
  147. { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
  148. { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
  149. { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
  150. { USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
  151. { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
  152. { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
  153. { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
  154. { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
  155. { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
  156. { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
  157. { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
  158. { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
  159. { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
  160. { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
  161. { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
  162. { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
  163. { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
  164. { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
  165. { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
  166. { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
  167. { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
  168. { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
  169. { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
  170. { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
  171. { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
  172. { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
  173. { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
  174. { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
  175. { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
  176. { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
  177. { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
  178. { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
  179. { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
  180. { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
  181. { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
  182. { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
  183. { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
  184. { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
  185. { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
  186. { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
  187. { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
  188. { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
  189. { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
  190. { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
  191. { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
  192. { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
  193. { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
  194. { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
  195. { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
  196. { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
  197. { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
  198. { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
  199. { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
  200. { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
  201. { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
  202. { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
  203. { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
  204. { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
  205. { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
  206. { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
  207. { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
  208. { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
  209. { } /* Terminating Entry */
  210. };
  211. MODULE_DEVICE_TABLE(usb, id_table);
  212. struct cp210x_serial_private {
  213. #ifdef CONFIG_GPIOLIB
  214. struct gpio_chip gc;
  215. u8 config;
  216. u8 gpio_mode;
  217. bool gpio_registered;
  218. #endif
  219. u8 partnum;
  220. };
  221. struct cp210x_port_private {
  222. __u8 bInterfaceNumber;
  223. bool has_swapped_line_ctl;
  224. };
  225. static struct usb_serial_driver cp210x_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "cp210x",
  229. },
  230. .id_table = id_table,
  231. .num_ports = 1,
  232. .bulk_in_size = 256,
  233. .bulk_out_size = 256,
  234. .open = cp210x_open,
  235. .close = cp210x_close,
  236. .break_ctl = cp210x_break_ctl,
  237. .set_termios = cp210x_set_termios,
  238. .tx_empty = cp210x_tx_empty,
  239. .tiocmget = cp210x_tiocmget,
  240. .tiocmset = cp210x_tiocmset,
  241. .attach = cp210x_attach,
  242. .disconnect = cp210x_disconnect,
  243. .release = cp210x_release,
  244. .port_probe = cp210x_port_probe,
  245. .port_remove = cp210x_port_remove,
  246. .dtr_rts = cp210x_dtr_rts
  247. };
  248. static struct usb_serial_driver * const serial_drivers[] = {
  249. &cp210x_device, NULL
  250. };
  251. /* Config request types */
  252. #define REQTYPE_HOST_TO_INTERFACE 0x41
  253. #define REQTYPE_INTERFACE_TO_HOST 0xc1
  254. #define REQTYPE_HOST_TO_DEVICE 0x40
  255. #define REQTYPE_DEVICE_TO_HOST 0xc0
  256. /* Config request codes */
  257. #define CP210X_IFC_ENABLE 0x00
  258. #define CP210X_SET_BAUDDIV 0x01
  259. #define CP210X_GET_BAUDDIV 0x02
  260. #define CP210X_SET_LINE_CTL 0x03
  261. #define CP210X_GET_LINE_CTL 0x04
  262. #define CP210X_SET_BREAK 0x05
  263. #define CP210X_IMM_CHAR 0x06
  264. #define CP210X_SET_MHS 0x07
  265. #define CP210X_GET_MDMSTS 0x08
  266. #define CP210X_SET_XON 0x09
  267. #define CP210X_SET_XOFF 0x0A
  268. #define CP210X_SET_EVENTMASK 0x0B
  269. #define CP210X_GET_EVENTMASK 0x0C
  270. #define CP210X_SET_CHAR 0x0D
  271. #define CP210X_GET_CHARS 0x0E
  272. #define CP210X_GET_PROPS 0x0F
  273. #define CP210X_GET_COMM_STATUS 0x10
  274. #define CP210X_RESET 0x11
  275. #define CP210X_PURGE 0x12
  276. #define CP210X_SET_FLOW 0x13
  277. #define CP210X_GET_FLOW 0x14
  278. #define CP210X_EMBED_EVENTS 0x15
  279. #define CP210X_GET_EVENTSTATE 0x16
  280. #define CP210X_SET_CHARS 0x19
  281. #define CP210X_GET_BAUDRATE 0x1D
  282. #define CP210X_SET_BAUDRATE 0x1E
  283. #define CP210X_VENDOR_SPECIFIC 0xFF
  284. /* CP210X_IFC_ENABLE */
  285. #define UART_ENABLE 0x0001
  286. #define UART_DISABLE 0x0000
  287. /* CP210X_(SET|GET)_BAUDDIV */
  288. #define BAUD_RATE_GEN_FREQ 0x384000
  289. /* CP210X_(SET|GET)_LINE_CTL */
  290. #define BITS_DATA_MASK 0X0f00
  291. #define BITS_DATA_5 0X0500
  292. #define BITS_DATA_6 0X0600
  293. #define BITS_DATA_7 0X0700
  294. #define BITS_DATA_8 0X0800
  295. #define BITS_DATA_9 0X0900
  296. #define BITS_PARITY_MASK 0x00f0
  297. #define BITS_PARITY_NONE 0x0000
  298. #define BITS_PARITY_ODD 0x0010
  299. #define BITS_PARITY_EVEN 0x0020
  300. #define BITS_PARITY_MARK 0x0030
  301. #define BITS_PARITY_SPACE 0x0040
  302. #define BITS_STOP_MASK 0x000f
  303. #define BITS_STOP_1 0x0000
  304. #define BITS_STOP_1_5 0x0001
  305. #define BITS_STOP_2 0x0002
  306. /* CP210X_SET_BREAK */
  307. #define BREAK_ON 0x0001
  308. #define BREAK_OFF 0x0000
  309. /* CP210X_(SET_MHS|GET_MDMSTS) */
  310. #define CONTROL_DTR 0x0001
  311. #define CONTROL_RTS 0x0002
  312. #define CONTROL_CTS 0x0010
  313. #define CONTROL_DSR 0x0020
  314. #define CONTROL_RING 0x0040
  315. #define CONTROL_DCD 0x0080
  316. #define CONTROL_WRITE_DTR 0x0100
  317. #define CONTROL_WRITE_RTS 0x0200
  318. /* CP210X_VENDOR_SPECIFIC values */
  319. #define CP210X_READ_LATCH 0x00C2
  320. #define CP210X_GET_PARTNUM 0x370B
  321. #define CP210X_GET_PORTCONFIG 0x370C
  322. #define CP210X_GET_DEVICEMODE 0x3711
  323. #define CP210X_WRITE_LATCH 0x37E1
  324. /* Part number definitions */
  325. #define CP210X_PARTNUM_CP2101 0x01
  326. #define CP210X_PARTNUM_CP2102 0x02
  327. #define CP210X_PARTNUM_CP2103 0x03
  328. #define CP210X_PARTNUM_CP2104 0x04
  329. #define CP210X_PARTNUM_CP2105 0x05
  330. #define CP210X_PARTNUM_CP2108 0x08
  331. /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
  332. struct cp210x_comm_status {
  333. __le32 ulErrors;
  334. __le32 ulHoldReasons;
  335. __le32 ulAmountInInQueue;
  336. __le32 ulAmountInOutQueue;
  337. u8 bEofReceived;
  338. u8 bWaitForImmediate;
  339. u8 bReserved;
  340. } __packed;
  341. /*
  342. * CP210X_PURGE - 16 bits passed in wValue of USB request.
  343. * SiLabs app note AN571 gives a strange description of the 4 bits:
  344. * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
  345. * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
  346. */
  347. #define PURGE_ALL 0x000f
  348. /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
  349. struct cp210x_flow_ctl {
  350. __le32 ulControlHandshake;
  351. __le32 ulFlowReplace;
  352. __le32 ulXonLimit;
  353. __le32 ulXoffLimit;
  354. } __packed;
  355. /* cp210x_flow_ctl::ulControlHandshake */
  356. #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
  357. #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
  358. #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
  359. #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
  360. #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
  361. #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
  362. /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
  363. #define CP210X_SERIAL_DTR_INACTIVE 0
  364. #define CP210X_SERIAL_DTR_ACTIVE 1
  365. #define CP210X_SERIAL_DTR_FLOW_CTL 2
  366. /* cp210x_flow_ctl::ulFlowReplace */
  367. #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
  368. #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
  369. #define CP210X_SERIAL_ERROR_CHAR BIT(2)
  370. #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
  371. #define CP210X_SERIAL_BREAK_CHAR BIT(4)
  372. #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
  373. #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
  374. #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
  375. /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
  376. #define CP210X_SERIAL_RTS_INACTIVE 0
  377. #define CP210X_SERIAL_RTS_ACTIVE 1
  378. #define CP210X_SERIAL_RTS_FLOW_CTL 2
  379. /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
  380. struct cp210x_pin_mode {
  381. u8 eci;
  382. u8 sci;
  383. } __packed;
  384. #define CP210X_PIN_MODE_MODEM 0
  385. #define CP210X_PIN_MODE_GPIO BIT(0)
  386. /*
  387. * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes.
  388. * Structure needs padding due to unused/unspecified bytes.
  389. */
  390. struct cp210x_config {
  391. __le16 gpio_mode;
  392. u8 __pad0[2];
  393. __le16 reset_state;
  394. u8 __pad1[4];
  395. __le16 suspend_state;
  396. u8 sci_cfg;
  397. u8 eci_cfg;
  398. u8 device_cfg;
  399. } __packed;
  400. /* GPIO modes */
  401. #define CP210X_SCI_GPIO_MODE_OFFSET 9
  402. #define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9)
  403. #define CP210X_ECI_GPIO_MODE_OFFSET 2
  404. #define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2)
  405. /* CP2105 port configuration values */
  406. #define CP2105_GPIO0_TXLED_MODE BIT(0)
  407. #define CP2105_GPIO1_RXLED_MODE BIT(1)
  408. #define CP2105_GPIO1_RS485_MODE BIT(2)
  409. /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
  410. struct cp210x_gpio_write {
  411. u8 mask;
  412. u8 state;
  413. } __packed;
  414. /*
  415. * Helper to get interface number when we only have struct usb_serial.
  416. */
  417. static u8 cp210x_interface_num(struct usb_serial *serial)
  418. {
  419. struct usb_host_interface *cur_altsetting;
  420. cur_altsetting = serial->interface->cur_altsetting;
  421. return cur_altsetting->desc.bInterfaceNumber;
  422. }
  423. /*
  424. * Reads a variable-sized block of CP210X_ registers, identified by req.
  425. * Returns data into buf in native USB byte order.
  426. */
  427. static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
  428. void *buf, int bufsize)
  429. {
  430. struct usb_serial *serial = port->serial;
  431. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  432. void *dmabuf;
  433. int result;
  434. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  435. if (!dmabuf) {
  436. /*
  437. * FIXME Some callers don't bother to check for error,
  438. * at least give them consistent junk until they are fixed
  439. */
  440. memset(buf, 0, bufsize);
  441. return -ENOMEM;
  442. }
  443. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  444. req, REQTYPE_INTERFACE_TO_HOST, 0,
  445. port_priv->bInterfaceNumber, dmabuf, bufsize,
  446. USB_CTRL_SET_TIMEOUT);
  447. if (result == bufsize) {
  448. memcpy(buf, dmabuf, bufsize);
  449. result = 0;
  450. } else {
  451. dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
  452. req, bufsize, result);
  453. if (result >= 0)
  454. result = -EIO;
  455. /*
  456. * FIXME Some callers don't bother to check for error,
  457. * at least give them consistent junk until they are fixed
  458. */
  459. memset(buf, 0, bufsize);
  460. }
  461. kfree(dmabuf);
  462. return result;
  463. }
  464. /*
  465. * Reads any 32-bit CP210X_ register identified by req.
  466. */
  467. static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
  468. {
  469. __le32 le32_val;
  470. int err;
  471. err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
  472. if (err) {
  473. /*
  474. * FIXME Some callers don't bother to check for error,
  475. * at least give them consistent junk until they are fixed
  476. */
  477. *val = 0;
  478. return err;
  479. }
  480. *val = le32_to_cpu(le32_val);
  481. return 0;
  482. }
  483. /*
  484. * Reads any 16-bit CP210X_ register identified by req.
  485. */
  486. static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
  487. {
  488. __le16 le16_val;
  489. int err;
  490. err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
  491. if (err)
  492. return err;
  493. *val = le16_to_cpu(le16_val);
  494. return 0;
  495. }
  496. /*
  497. * Reads any 8-bit CP210X_ register identified by req.
  498. */
  499. static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
  500. {
  501. return cp210x_read_reg_block(port, req, val, sizeof(*val));
  502. }
  503. /*
  504. * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
  505. * Returns data into buf in native USB byte order.
  506. */
  507. static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
  508. void *buf, int bufsize)
  509. {
  510. void *dmabuf;
  511. int result;
  512. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  513. if (!dmabuf)
  514. return -ENOMEM;
  515. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  516. CP210X_VENDOR_SPECIFIC, type, val,
  517. cp210x_interface_num(serial), dmabuf, bufsize,
  518. USB_CTRL_GET_TIMEOUT);
  519. if (result == bufsize) {
  520. memcpy(buf, dmabuf, bufsize);
  521. result = 0;
  522. } else {
  523. dev_err(&serial->interface->dev,
  524. "failed to get vendor val 0x%04x size %d: %d\n", val,
  525. bufsize, result);
  526. if (result >= 0)
  527. result = -EIO;
  528. }
  529. kfree(dmabuf);
  530. return result;
  531. }
  532. /*
  533. * Writes any 16-bit CP210X_ register (req) whose value is passed
  534. * entirely in the wValue field of the USB request.
  535. */
  536. static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
  537. {
  538. struct usb_serial *serial = port->serial;
  539. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  540. int result;
  541. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  542. req, REQTYPE_HOST_TO_INTERFACE, val,
  543. port_priv->bInterfaceNumber, NULL, 0,
  544. USB_CTRL_SET_TIMEOUT);
  545. if (result < 0) {
  546. dev_err(&port->dev, "failed set request 0x%x status: %d\n",
  547. req, result);
  548. }
  549. return result;
  550. }
  551. /*
  552. * Writes a variable-sized block of CP210X_ registers, identified by req.
  553. * Data in buf must be in native USB byte order.
  554. */
  555. static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
  556. void *buf, int bufsize)
  557. {
  558. struct usb_serial *serial = port->serial;
  559. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  560. void *dmabuf;
  561. int result;
  562. dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
  563. if (!dmabuf)
  564. return -ENOMEM;
  565. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  566. req, REQTYPE_HOST_TO_INTERFACE, 0,
  567. port_priv->bInterfaceNumber, dmabuf, bufsize,
  568. USB_CTRL_SET_TIMEOUT);
  569. kfree(dmabuf);
  570. if (result == bufsize) {
  571. result = 0;
  572. } else {
  573. dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
  574. req, bufsize, result);
  575. if (result >= 0)
  576. result = -EIO;
  577. }
  578. return result;
  579. }
  580. /*
  581. * Writes any 32-bit CP210X_ register identified by req.
  582. */
  583. static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
  584. {
  585. __le32 le32_val;
  586. le32_val = cpu_to_le32(val);
  587. return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
  588. }
  589. #ifdef CONFIG_GPIOLIB
  590. /*
  591. * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
  592. * Data in buf must be in native USB byte order.
  593. */
  594. static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
  595. u16 val, void *buf, int bufsize)
  596. {
  597. void *dmabuf;
  598. int result;
  599. dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
  600. if (!dmabuf)
  601. return -ENOMEM;
  602. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  603. CP210X_VENDOR_SPECIFIC, type, val,
  604. cp210x_interface_num(serial), dmabuf, bufsize,
  605. USB_CTRL_SET_TIMEOUT);
  606. kfree(dmabuf);
  607. if (result == bufsize) {
  608. result = 0;
  609. } else {
  610. dev_err(&serial->interface->dev,
  611. "failed to set vendor val 0x%04x size %d: %d\n", val,
  612. bufsize, result);
  613. if (result >= 0)
  614. result = -EIO;
  615. }
  616. return result;
  617. }
  618. #endif
  619. /*
  620. * Detect CP2108 GET_LINE_CTL bug and activate workaround.
  621. * Write a known good value 0x800, read it back.
  622. * If it comes back swapped the bug is detected.
  623. * Preserve the original register value.
  624. */
  625. static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
  626. {
  627. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  628. u16 line_ctl_save;
  629. u16 line_ctl_test;
  630. int err;
  631. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
  632. if (err)
  633. return err;
  634. err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
  635. if (err)
  636. return err;
  637. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
  638. if (err)
  639. return err;
  640. if (line_ctl_test == 8) {
  641. port_priv->has_swapped_line_ctl = true;
  642. line_ctl_save = swab16(line_ctl_save);
  643. }
  644. return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
  645. }
  646. /*
  647. * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
  648. * to workaround cp2108 bug and get correct value.
  649. */
  650. static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
  651. {
  652. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  653. int err;
  654. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
  655. if (err)
  656. return err;
  657. /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
  658. if (port_priv->has_swapped_line_ctl)
  659. *ctl = swab16(*ctl);
  660. return 0;
  661. }
  662. /*
  663. * cp210x_quantise_baudrate
  664. * Quantises the baud rate as per AN205 Table 1
  665. */
  666. static unsigned int cp210x_quantise_baudrate(unsigned int baud)
  667. {
  668. if (baud <= 300)
  669. baud = 300;
  670. else if (baud <= 600) baud = 600;
  671. else if (baud <= 1200) baud = 1200;
  672. else if (baud <= 1800) baud = 1800;
  673. else if (baud <= 2400) baud = 2400;
  674. else if (baud <= 4000) baud = 4000;
  675. else if (baud <= 4803) baud = 4800;
  676. else if (baud <= 7207) baud = 7200;
  677. else if (baud <= 9612) baud = 9600;
  678. else if (baud <= 14428) baud = 14400;
  679. else if (baud <= 16062) baud = 16000;
  680. else if (baud <= 19250) baud = 19200;
  681. else if (baud <= 28912) baud = 28800;
  682. else if (baud <= 38601) baud = 38400;
  683. else if (baud <= 51558) baud = 51200;
  684. else if (baud <= 56280) baud = 56000;
  685. else if (baud <= 58053) baud = 57600;
  686. else if (baud <= 64111) baud = 64000;
  687. else if (baud <= 77608) baud = 76800;
  688. else if (baud <= 117028) baud = 115200;
  689. else if (baud <= 129347) baud = 128000;
  690. else if (baud <= 156868) baud = 153600;
  691. else if (baud <= 237832) baud = 230400;
  692. else if (baud <= 254234) baud = 250000;
  693. else if (baud <= 273066) baud = 256000;
  694. else if (baud <= 491520) baud = 460800;
  695. else if (baud <= 567138) baud = 500000;
  696. else if (baud <= 670254) baud = 576000;
  697. else if (baud < 1000000)
  698. baud = 921600;
  699. else if (baud > 2000000)
  700. baud = 2000000;
  701. return baud;
  702. }
  703. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
  704. {
  705. int result;
  706. result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
  707. if (result) {
  708. dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
  709. return result;
  710. }
  711. /* Configure the termios structure */
  712. cp210x_get_termios(tty, port);
  713. /* The baud rate must be initialised on cp2104 */
  714. if (tty)
  715. cp210x_change_speed(tty, port, NULL);
  716. return usb_serial_generic_open(tty, port);
  717. }
  718. static void cp210x_close(struct usb_serial_port *port)
  719. {
  720. usb_serial_generic_close(port);
  721. /* Clear both queues; cp2108 needs this to avoid an occasional hang */
  722. cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
  723. cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
  724. }
  725. /*
  726. * Read how many bytes are waiting in the TX queue.
  727. */
  728. static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
  729. u32 *count)
  730. {
  731. struct usb_serial *serial = port->serial;
  732. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  733. struct cp210x_comm_status *sts;
  734. int result;
  735. sts = kmalloc(sizeof(*sts), GFP_KERNEL);
  736. if (!sts)
  737. return -ENOMEM;
  738. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  739. CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
  740. 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
  741. USB_CTRL_GET_TIMEOUT);
  742. if (result == sizeof(*sts)) {
  743. *count = le32_to_cpu(sts->ulAmountInOutQueue);
  744. result = 0;
  745. } else {
  746. dev_err(&port->dev, "failed to get comm status: %d\n", result);
  747. if (result >= 0)
  748. result = -EIO;
  749. }
  750. kfree(sts);
  751. return result;
  752. }
  753. static bool cp210x_tx_empty(struct usb_serial_port *port)
  754. {
  755. int err;
  756. u32 count;
  757. err = cp210x_get_tx_queue_byte_count(port, &count);
  758. if (err)
  759. return true;
  760. return !count;
  761. }
  762. /*
  763. * cp210x_get_termios
  764. * Reads the baud rate, data bits, parity, stop bits and flow control mode
  765. * from the device, corrects any unsupported values, and configures the
  766. * termios structure to reflect the state of the device
  767. */
  768. static void cp210x_get_termios(struct tty_struct *tty,
  769. struct usb_serial_port *port)
  770. {
  771. unsigned int baud;
  772. if (tty) {
  773. cp210x_get_termios_port(tty->driver_data,
  774. &tty->termios.c_cflag, &baud);
  775. tty_encode_baud_rate(tty, baud, baud);
  776. } else {
  777. tcflag_t cflag;
  778. cflag = 0;
  779. cp210x_get_termios_port(port, &cflag, &baud);
  780. }
  781. }
  782. /*
  783. * cp210x_get_termios_port
  784. * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
  785. */
  786. static void cp210x_get_termios_port(struct usb_serial_port *port,
  787. tcflag_t *cflagp, unsigned int *baudp)
  788. {
  789. struct device *dev = &port->dev;
  790. tcflag_t cflag;
  791. struct cp210x_flow_ctl flow_ctl;
  792. u32 baud;
  793. u16 bits;
  794. u32 ctl_hs;
  795. cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
  796. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  797. *baudp = baud;
  798. cflag = *cflagp;
  799. cp210x_get_line_ctl(port, &bits);
  800. cflag &= ~CSIZE;
  801. switch (bits & BITS_DATA_MASK) {
  802. case BITS_DATA_5:
  803. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  804. cflag |= CS5;
  805. break;
  806. case BITS_DATA_6:
  807. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  808. cflag |= CS6;
  809. break;
  810. case BITS_DATA_7:
  811. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  812. cflag |= CS7;
  813. break;
  814. case BITS_DATA_8:
  815. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  816. cflag |= CS8;
  817. break;
  818. case BITS_DATA_9:
  819. dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
  820. cflag |= CS8;
  821. bits &= ~BITS_DATA_MASK;
  822. bits |= BITS_DATA_8;
  823. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  824. break;
  825. default:
  826. dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
  827. cflag |= CS8;
  828. bits &= ~BITS_DATA_MASK;
  829. bits |= BITS_DATA_8;
  830. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  831. break;
  832. }
  833. switch (bits & BITS_PARITY_MASK) {
  834. case BITS_PARITY_NONE:
  835. dev_dbg(dev, "%s - parity = NONE\n", __func__);
  836. cflag &= ~PARENB;
  837. break;
  838. case BITS_PARITY_ODD:
  839. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  840. cflag |= (PARENB|PARODD);
  841. break;
  842. case BITS_PARITY_EVEN:
  843. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  844. cflag &= ~PARODD;
  845. cflag |= PARENB;
  846. break;
  847. case BITS_PARITY_MARK:
  848. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  849. cflag |= (PARENB|PARODD|CMSPAR);
  850. break;
  851. case BITS_PARITY_SPACE:
  852. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  853. cflag &= ~PARODD;
  854. cflag |= (PARENB|CMSPAR);
  855. break;
  856. default:
  857. dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
  858. cflag &= ~PARENB;
  859. bits &= ~BITS_PARITY_MASK;
  860. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  861. break;
  862. }
  863. cflag &= ~CSTOPB;
  864. switch (bits & BITS_STOP_MASK) {
  865. case BITS_STOP_1:
  866. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  867. break;
  868. case BITS_STOP_1_5:
  869. dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
  870. bits &= ~BITS_STOP_MASK;
  871. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  872. break;
  873. case BITS_STOP_2:
  874. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  875. cflag |= CSTOPB;
  876. break;
  877. default:
  878. dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
  879. bits &= ~BITS_STOP_MASK;
  880. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  881. break;
  882. }
  883. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  884. sizeof(flow_ctl));
  885. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  886. if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
  887. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  888. cflag |= CRTSCTS;
  889. } else {
  890. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  891. cflag &= ~CRTSCTS;
  892. }
  893. *cflagp = cflag;
  894. }
  895. /*
  896. * CP2101 supports the following baud rates:
  897. *
  898. * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
  899. * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
  900. *
  901. * CP2102 and CP2103 support the following additional rates:
  902. *
  903. * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
  904. * 576000
  905. *
  906. * The device will map a requested rate to a supported one, but the result
  907. * of requests for rates greater than 1053257 is undefined (see AN205).
  908. *
  909. * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
  910. * respectively, with an error less than 1%. The actual rates are determined
  911. * by
  912. *
  913. * div = round(freq / (2 x prescale x request))
  914. * actual = freq / (2 x prescale x div)
  915. *
  916. * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
  917. * or 1 otherwise.
  918. * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
  919. * otherwise.
  920. */
  921. static void cp210x_change_speed(struct tty_struct *tty,
  922. struct usb_serial_port *port, struct ktermios *old_termios)
  923. {
  924. u32 baud;
  925. baud = tty->termios.c_ospeed;
  926. /* This maps the requested rate to a rate valid on cp2102 or cp2103,
  927. * or to an arbitrary rate in [1M,2M].
  928. *
  929. * NOTE: B0 is not implemented.
  930. */
  931. baud = cp210x_quantise_baudrate(baud);
  932. dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
  933. if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
  934. dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
  935. if (old_termios)
  936. baud = old_termios->c_ospeed;
  937. else
  938. baud = 9600;
  939. }
  940. tty_encode_baud_rate(tty, baud, baud);
  941. }
  942. static void cp210x_set_termios(struct tty_struct *tty,
  943. struct usb_serial_port *port, struct ktermios *old_termios)
  944. {
  945. struct device *dev = &port->dev;
  946. unsigned int cflag, old_cflag;
  947. u16 bits;
  948. cflag = tty->termios.c_cflag;
  949. old_cflag = old_termios->c_cflag;
  950. if (tty->termios.c_ospeed != old_termios->c_ospeed)
  951. cp210x_change_speed(tty, port, old_termios);
  952. /* If the number of data bits is to be updated */
  953. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  954. cp210x_get_line_ctl(port, &bits);
  955. bits &= ~BITS_DATA_MASK;
  956. switch (cflag & CSIZE) {
  957. case CS5:
  958. bits |= BITS_DATA_5;
  959. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  960. break;
  961. case CS6:
  962. bits |= BITS_DATA_6;
  963. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  964. break;
  965. case CS7:
  966. bits |= BITS_DATA_7;
  967. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  968. break;
  969. case CS8:
  970. default:
  971. bits |= BITS_DATA_8;
  972. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  973. break;
  974. }
  975. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  976. dev_dbg(dev, "Number of data bits requested not supported by device\n");
  977. }
  978. if ((cflag & (PARENB|PARODD|CMSPAR)) !=
  979. (old_cflag & (PARENB|PARODD|CMSPAR))) {
  980. cp210x_get_line_ctl(port, &bits);
  981. bits &= ~BITS_PARITY_MASK;
  982. if (cflag & PARENB) {
  983. if (cflag & CMSPAR) {
  984. if (cflag & PARODD) {
  985. bits |= BITS_PARITY_MARK;
  986. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  987. } else {
  988. bits |= BITS_PARITY_SPACE;
  989. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  990. }
  991. } else {
  992. if (cflag & PARODD) {
  993. bits |= BITS_PARITY_ODD;
  994. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  995. } else {
  996. bits |= BITS_PARITY_EVEN;
  997. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  998. }
  999. }
  1000. }
  1001. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1002. dev_dbg(dev, "Parity mode not supported by device\n");
  1003. }
  1004. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  1005. cp210x_get_line_ctl(port, &bits);
  1006. bits &= ~BITS_STOP_MASK;
  1007. if (cflag & CSTOPB) {
  1008. bits |= BITS_STOP_2;
  1009. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1010. } else {
  1011. bits |= BITS_STOP_1;
  1012. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1013. }
  1014. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1015. dev_dbg(dev, "Number of stop bits requested not supported by device\n");
  1016. }
  1017. if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  1018. struct cp210x_flow_ctl flow_ctl;
  1019. u32 ctl_hs;
  1020. u32 flow_repl;
  1021. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  1022. sizeof(flow_ctl));
  1023. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  1024. flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
  1025. dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1026. __func__, ctl_hs, flow_repl);
  1027. ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
  1028. ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
  1029. ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
  1030. ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
  1031. ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
  1032. if (cflag & CRTSCTS) {
  1033. ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
  1034. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1035. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1036. CP210X_SERIAL_RTS_FLOW_CTL);
  1037. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  1038. } else {
  1039. ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
  1040. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1041. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1042. CP210X_SERIAL_RTS_ACTIVE);
  1043. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  1044. }
  1045. dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1046. __func__, ctl_hs, flow_repl);
  1047. flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
  1048. flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
  1049. cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
  1050. sizeof(flow_ctl));
  1051. }
  1052. }
  1053. static int cp210x_tiocmset(struct tty_struct *tty,
  1054. unsigned int set, unsigned int clear)
  1055. {
  1056. struct usb_serial_port *port = tty->driver_data;
  1057. return cp210x_tiocmset_port(port, set, clear);
  1058. }
  1059. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  1060. unsigned int set, unsigned int clear)
  1061. {
  1062. u16 control = 0;
  1063. if (set & TIOCM_RTS) {
  1064. control |= CONTROL_RTS;
  1065. control |= CONTROL_WRITE_RTS;
  1066. }
  1067. if (set & TIOCM_DTR) {
  1068. control |= CONTROL_DTR;
  1069. control |= CONTROL_WRITE_DTR;
  1070. }
  1071. if (clear & TIOCM_RTS) {
  1072. control &= ~CONTROL_RTS;
  1073. control |= CONTROL_WRITE_RTS;
  1074. }
  1075. if (clear & TIOCM_DTR) {
  1076. control &= ~CONTROL_DTR;
  1077. control |= CONTROL_WRITE_DTR;
  1078. }
  1079. dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
  1080. return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
  1081. }
  1082. static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
  1083. {
  1084. if (on)
  1085. cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
  1086. else
  1087. cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
  1088. }
  1089. static int cp210x_tiocmget(struct tty_struct *tty)
  1090. {
  1091. struct usb_serial_port *port = tty->driver_data;
  1092. u8 control;
  1093. int result;
  1094. result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
  1095. if (result)
  1096. return result;
  1097. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  1098. |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  1099. |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
  1100. |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
  1101. |((control & CONTROL_RING)? TIOCM_RI : 0)
  1102. |((control & CONTROL_DCD) ? TIOCM_CD : 0);
  1103. dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
  1104. return result;
  1105. }
  1106. static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
  1107. {
  1108. struct usb_serial_port *port = tty->driver_data;
  1109. u16 state;
  1110. if (break_state == 0)
  1111. state = BREAK_OFF;
  1112. else
  1113. state = BREAK_ON;
  1114. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  1115. state == BREAK_OFF ? "off" : "on");
  1116. cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
  1117. }
  1118. #ifdef CONFIG_GPIOLIB
  1119. static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  1120. {
  1121. struct usb_serial *serial = gpiochip_get_data(gc);
  1122. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1123. switch (offset) {
  1124. case 0:
  1125. if (priv->config & CP2105_GPIO0_TXLED_MODE)
  1126. return -ENODEV;
  1127. break;
  1128. case 1:
  1129. if (priv->config & (CP2105_GPIO1_RXLED_MODE |
  1130. CP2105_GPIO1_RS485_MODE))
  1131. return -ENODEV;
  1132. break;
  1133. }
  1134. return 0;
  1135. }
  1136. static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  1137. {
  1138. struct usb_serial *serial = gpiochip_get_data(gc);
  1139. int result;
  1140. u8 buf;
  1141. result = cp210x_read_vendor_block(serial, REQTYPE_INTERFACE_TO_HOST,
  1142. CP210X_READ_LATCH, &buf, sizeof(buf));
  1143. if (result < 0)
  1144. return result;
  1145. return !!(buf & BIT(gpio));
  1146. }
  1147. static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
  1148. {
  1149. struct usb_serial *serial = gpiochip_get_data(gc);
  1150. struct cp210x_gpio_write buf;
  1151. if (value == 1)
  1152. buf.state = BIT(gpio);
  1153. else
  1154. buf.state = 0;
  1155. buf.mask = BIT(gpio);
  1156. cp210x_write_vendor_block(serial, REQTYPE_HOST_TO_INTERFACE,
  1157. CP210X_WRITE_LATCH, &buf, sizeof(buf));
  1158. }
  1159. static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
  1160. {
  1161. /* Hardware does not support an input mode */
  1162. return 0;
  1163. }
  1164. static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
  1165. {
  1166. /* Hardware does not support an input mode */
  1167. return -ENOTSUPP;
  1168. }
  1169. static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
  1170. int value)
  1171. {
  1172. return 0;
  1173. }
  1174. static int cp210x_gpio_set_single_ended(struct gpio_chip *gc, unsigned int gpio,
  1175. enum single_ended_mode mode)
  1176. {
  1177. struct usb_serial *serial = gpiochip_get_data(gc);
  1178. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1179. /* Succeed only if in correct mode (this can't be set at runtime) */
  1180. if ((mode == LINE_MODE_PUSH_PULL) && (priv->gpio_mode & BIT(gpio)))
  1181. return 0;
  1182. if ((mode == LINE_MODE_OPEN_DRAIN) && !(priv->gpio_mode & BIT(gpio)))
  1183. return 0;
  1184. return -ENOTSUPP;
  1185. }
  1186. /*
  1187. * This function is for configuring GPIO using shared pins, where other signals
  1188. * are made unavailable by configuring the use of GPIO. This is believed to be
  1189. * only applicable to the cp2105 at this point, the other devices supported by
  1190. * this driver that provide GPIO do so in a way that does not impact other
  1191. * signals and are thus expected to have very different initialisation.
  1192. */
  1193. static int cp2105_shared_gpio_init(struct usb_serial *serial)
  1194. {
  1195. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1196. struct cp210x_pin_mode mode;
  1197. struct cp210x_config config;
  1198. u8 intf_num = cp210x_interface_num(serial);
  1199. int result;
  1200. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1201. CP210X_GET_DEVICEMODE, &mode,
  1202. sizeof(mode));
  1203. if (result < 0)
  1204. return result;
  1205. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1206. CP210X_GET_PORTCONFIG, &config,
  1207. sizeof(config));
  1208. if (result < 0)
  1209. return result;
  1210. /* 2 banks of GPIO - One for the pins taken from each serial port */
  1211. if (intf_num == 0) {
  1212. if (mode.eci == CP210X_PIN_MODE_MODEM)
  1213. return 0;
  1214. priv->config = config.eci_cfg;
  1215. priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) &
  1216. CP210X_ECI_GPIO_MODE_MASK) >>
  1217. CP210X_ECI_GPIO_MODE_OFFSET);
  1218. priv->gc.ngpio = 2;
  1219. } else if (intf_num == 1) {
  1220. if (mode.sci == CP210X_PIN_MODE_MODEM)
  1221. return 0;
  1222. priv->config = config.sci_cfg;
  1223. priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) &
  1224. CP210X_SCI_GPIO_MODE_MASK) >>
  1225. CP210X_SCI_GPIO_MODE_OFFSET);
  1226. priv->gc.ngpio = 3;
  1227. } else {
  1228. return -ENODEV;
  1229. }
  1230. priv->gc.label = "cp210x";
  1231. priv->gc.request = cp210x_gpio_request;
  1232. priv->gc.get_direction = cp210x_gpio_direction_get;
  1233. priv->gc.direction_input = cp210x_gpio_direction_input;
  1234. priv->gc.direction_output = cp210x_gpio_direction_output;
  1235. priv->gc.get = cp210x_gpio_get;
  1236. priv->gc.set = cp210x_gpio_set;
  1237. priv->gc.set_single_ended = cp210x_gpio_set_single_ended;
  1238. priv->gc.owner = THIS_MODULE;
  1239. priv->gc.parent = &serial->interface->dev;
  1240. priv->gc.base = -1;
  1241. priv->gc.can_sleep = true;
  1242. result = gpiochip_add_data(&priv->gc, serial);
  1243. if (!result)
  1244. priv->gpio_registered = true;
  1245. return result;
  1246. }
  1247. static void cp210x_gpio_remove(struct usb_serial *serial)
  1248. {
  1249. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1250. if (priv->gpio_registered) {
  1251. gpiochip_remove(&priv->gc);
  1252. priv->gpio_registered = false;
  1253. }
  1254. }
  1255. #else
  1256. static int cp2105_shared_gpio_init(struct usb_serial *serial)
  1257. {
  1258. return 0;
  1259. }
  1260. static void cp210x_gpio_remove(struct usb_serial *serial)
  1261. {
  1262. /* Nothing to do */
  1263. }
  1264. #endif
  1265. static int cp210x_port_probe(struct usb_serial_port *port)
  1266. {
  1267. struct usb_serial *serial = port->serial;
  1268. struct cp210x_port_private *port_priv;
  1269. int ret;
  1270. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  1271. if (!port_priv)
  1272. return -ENOMEM;
  1273. port_priv->bInterfaceNumber = cp210x_interface_num(serial);
  1274. usb_set_serial_port_data(port, port_priv);
  1275. ret = cp210x_detect_swapped_line_ctl(port);
  1276. if (ret) {
  1277. kfree(port_priv);
  1278. return ret;
  1279. }
  1280. return 0;
  1281. }
  1282. static int cp210x_port_remove(struct usb_serial_port *port)
  1283. {
  1284. struct cp210x_port_private *port_priv;
  1285. port_priv = usb_get_serial_port_data(port);
  1286. kfree(port_priv);
  1287. return 0;
  1288. }
  1289. static int cp210x_attach(struct usb_serial *serial)
  1290. {
  1291. int result;
  1292. struct cp210x_serial_private *priv;
  1293. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1294. if (!priv)
  1295. return -ENOMEM;
  1296. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1297. CP210X_GET_PARTNUM, &priv->partnum,
  1298. sizeof(priv->partnum));
  1299. if (result < 0)
  1300. goto err_free_priv;
  1301. usb_set_serial_data(serial, priv);
  1302. if (priv->partnum == CP210X_PARTNUM_CP2105) {
  1303. result = cp2105_shared_gpio_init(serial);
  1304. if (result < 0) {
  1305. dev_err(&serial->interface->dev,
  1306. "GPIO initialisation failed, continuing without GPIO support\n");
  1307. }
  1308. }
  1309. return 0;
  1310. err_free_priv:
  1311. kfree(priv);
  1312. return result;
  1313. }
  1314. static void cp210x_disconnect(struct usb_serial *serial)
  1315. {
  1316. cp210x_gpio_remove(serial);
  1317. }
  1318. static void cp210x_release(struct usb_serial *serial)
  1319. {
  1320. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1321. cp210x_gpio_remove(serial);
  1322. kfree(priv);
  1323. }
  1324. module_usb_serial_driver(serial_drivers, id_table);
  1325. MODULE_DESCRIPTION(DRIVER_DESC);
  1326. MODULE_LICENSE("GPL");