serial_core.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver core for serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright 1999 ARM Limited
  8. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_flip.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/init.h>
  16. #include <linux/console.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/kernel.h>
  19. #include <linux/of.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/device.h>
  24. #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
  25. #include <linux/serial_core.h>
  26. #include <linux/sysrq.h>
  27. #include <linux/delay.h>
  28. #include <linux/mutex.h>
  29. #include <linux/math64.h>
  30. #include <linux/security.h>
  31. #include <linux/irq.h>
  32. #include <linux/uaccess.h>
  33. #include "serial_base.h"
  34. /*
  35. * This is used to lock changes in serial line configuration.
  36. */
  37. static DEFINE_MUTEX(port_mutex);
  38. /*
  39. * lockdep: port->lock is initialized in two places, but we
  40. * want only one lock-class:
  41. */
  42. static struct lock_class_key port_lock_key;
  43. #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
  44. /*
  45. * Max time with active RTS before/after data is sent.
  46. */
  47. #define RS485_MAX_RTS_DELAY 100 /* msecs */
  48. static void uart_change_pm(struct uart_state *state,
  49. enum uart_pm_state pm_state);
  50. static void uart_port_shutdown(struct tty_port *port);
  51. static int uart_dcd_enabled(struct uart_port *uport)
  52. {
  53. return !!(uport->status & UPSTAT_DCD_ENABLE);
  54. }
  55. static inline struct uart_port *uart_port_ref(struct uart_state *state)
  56. {
  57. if (atomic_add_unless(&state->refcount, 1, 0))
  58. return state->uart_port;
  59. return NULL;
  60. }
  61. static inline void uart_port_deref(struct uart_port *uport)
  62. {
  63. if (atomic_dec_and_test(&uport->state->refcount))
  64. wake_up(&uport->state->remove_wait);
  65. }
  66. static inline struct uart_port *uart_port_ref_lock(struct uart_state *state, unsigned long *flags)
  67. {
  68. struct uart_port *uport = uart_port_ref(state);
  69. if (uport)
  70. uart_port_lock_irqsave(uport, flags);
  71. return uport;
  72. }
  73. static inline void uart_port_unlock_deref(struct uart_port *uport, unsigned long flags)
  74. {
  75. if (uport) {
  76. uart_port_unlock_irqrestore(uport, flags);
  77. uart_port_deref(uport);
  78. }
  79. }
  80. static inline struct uart_port *uart_port_check(struct uart_state *state)
  81. {
  82. lockdep_assert_held(&state->port.mutex);
  83. return state->uart_port;
  84. }
  85. /**
  86. * uart_write_wakeup - schedule write processing
  87. * @port: port to be processed
  88. *
  89. * This routine is used by the interrupt handler to schedule processing in the
  90. * software interrupt portion of the driver. A driver is expected to call this
  91. * function when the number of characters in the transmit buffer have dropped
  92. * below a threshold.
  93. *
  94. * Locking: @port->lock should be held
  95. */
  96. void uart_write_wakeup(struct uart_port *port)
  97. {
  98. struct uart_state *state = port->state;
  99. /*
  100. * This means you called this function _after_ the port was
  101. * closed. No cookie for you.
  102. */
  103. BUG_ON(!state);
  104. tty_port_tty_wakeup(&state->port);
  105. }
  106. EXPORT_SYMBOL(uart_write_wakeup);
  107. static void uart_stop(struct tty_struct *tty)
  108. {
  109. struct uart_state *state = tty->driver_data;
  110. struct uart_port *port;
  111. unsigned long flags;
  112. port = uart_port_ref_lock(state, &flags);
  113. if (port)
  114. port->ops->stop_tx(port);
  115. uart_port_unlock_deref(port, flags);
  116. }
  117. static void __uart_start(struct uart_state *state)
  118. {
  119. struct uart_port *port = state->uart_port;
  120. struct serial_port_device *port_dev;
  121. int err;
  122. if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port))
  123. return;
  124. port_dev = port->port_dev;
  125. /* Increment the runtime PM usage count for the active check below */
  126. err = pm_runtime_get(&port_dev->dev);
  127. if (err < 0 && err != -EINPROGRESS) {
  128. pm_runtime_put_noidle(&port_dev->dev);
  129. return;
  130. }
  131. /*
  132. * Start TX if enabled, and kick runtime PM. If the device is not
  133. * enabled, serial_port_runtime_resume() calls start_tx() again
  134. * after enabling the device.
  135. */
  136. if (!pm_runtime_enabled(port->dev) || pm_runtime_active(&port_dev->dev))
  137. port->ops->start_tx(port);
  138. pm_runtime_mark_last_busy(&port_dev->dev);
  139. pm_runtime_put_autosuspend(&port_dev->dev);
  140. }
  141. static void uart_start(struct tty_struct *tty)
  142. {
  143. struct uart_state *state = tty->driver_data;
  144. struct uart_port *port;
  145. unsigned long flags;
  146. port = uart_port_ref_lock(state, &flags);
  147. __uart_start(state);
  148. uart_port_unlock_deref(port, flags);
  149. }
  150. static void
  151. uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
  152. {
  153. unsigned int old;
  154. guard(uart_port_lock_irqsave)(port);
  155. old = port->mctrl;
  156. port->mctrl = (old & ~clear) | set;
  157. if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
  158. port->ops->set_mctrl(port, port->mctrl);
  159. }
  160. #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
  161. #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
  162. static void uart_port_dtr_rts(struct uart_port *uport, bool active)
  163. {
  164. if (active)
  165. uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  166. else
  167. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  168. }
  169. /* Caller holds port mutex */
  170. static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state,
  171. const struct ktermios *old_termios)
  172. {
  173. struct uart_port *uport = uart_port_check(state);
  174. struct ktermios *termios;
  175. bool old_hw_stopped;
  176. /*
  177. * If we have no tty, termios, or the port does not exist,
  178. * then we can't set the parameters for this port.
  179. */
  180. if (!tty || uport->type == PORT_UNKNOWN)
  181. return;
  182. termios = &tty->termios;
  183. uport->ops->set_termios(uport, termios, old_termios);
  184. /*
  185. * Set modem status enables based on termios cflag
  186. */
  187. guard(uart_port_lock_irq)(uport);
  188. if (termios->c_cflag & CRTSCTS)
  189. uport->status |= UPSTAT_CTS_ENABLE;
  190. else
  191. uport->status &= ~UPSTAT_CTS_ENABLE;
  192. if (termios->c_cflag & CLOCAL)
  193. uport->status &= ~UPSTAT_DCD_ENABLE;
  194. else
  195. uport->status |= UPSTAT_DCD_ENABLE;
  196. /* reset sw-assisted CTS flow control based on (possibly) new mode */
  197. old_hw_stopped = uport->hw_stopped;
  198. uport->hw_stopped = uart_softcts_mode(uport) &&
  199. !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
  200. if (uport->hw_stopped != old_hw_stopped) {
  201. if (!old_hw_stopped)
  202. uport->ops->stop_tx(uport);
  203. else
  204. __uart_start(state);
  205. }
  206. }
  207. static int uart_alloc_xmit_buf(struct tty_port *port)
  208. {
  209. struct uart_state *state = container_of(port, struct uart_state, port);
  210. struct uart_port *uport;
  211. unsigned long flags;
  212. unsigned long page;
  213. /*
  214. * Initialise and allocate the transmit and temporary
  215. * buffer.
  216. */
  217. page = get_zeroed_page(GFP_KERNEL);
  218. if (!page)
  219. return -ENOMEM;
  220. uport = uart_port_ref_lock(state, &flags);
  221. if (!state->port.xmit_buf) {
  222. state->port.xmit_buf = (unsigned char *)page;
  223. kfifo_init(&state->port.xmit_fifo, state->port.xmit_buf,
  224. PAGE_SIZE);
  225. uart_port_unlock_deref(uport, flags);
  226. } else {
  227. uart_port_unlock_deref(uport, flags);
  228. /*
  229. * Do not free() the page under the port lock, see
  230. * uart_free_xmit_buf().
  231. */
  232. free_page(page);
  233. }
  234. return 0;
  235. }
  236. static void uart_free_xmit_buf(struct tty_port *port)
  237. {
  238. struct uart_state *state = container_of(port, struct uart_state, port);
  239. struct uart_port *uport;
  240. unsigned long flags;
  241. char *xmit_buf;
  242. /*
  243. * Do not free() the transmit buffer page under the port lock since
  244. * this can create various circular locking scenarios. For instance,
  245. * console driver may need to allocate/free a debug object, which
  246. * can end up in printk() recursion.
  247. */
  248. uport = uart_port_ref_lock(state, &flags);
  249. xmit_buf = port->xmit_buf;
  250. port->xmit_buf = NULL;
  251. INIT_KFIFO(port->xmit_fifo);
  252. uart_port_unlock_deref(uport, flags);
  253. free_page((unsigned long)xmit_buf);
  254. }
  255. /*
  256. * Startup the port. This will be called once per open. All calls
  257. * will be serialised by the per-port mutex.
  258. */
  259. static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
  260. bool init_hw)
  261. {
  262. struct uart_port *uport = uart_port_check(state);
  263. int retval;
  264. if (uport->type == PORT_UNKNOWN)
  265. return 1;
  266. /*
  267. * Make sure the device is in D0 state.
  268. */
  269. uart_change_pm(state, UART_PM_STATE_ON);
  270. retval = uart_alloc_xmit_buf(&state->port);
  271. if (retval)
  272. return retval;
  273. retval = uport->ops->startup(uport);
  274. if (retval == 0) {
  275. if (uart_console(uport) && uport->cons->cflag) {
  276. tty->termios.c_cflag = uport->cons->cflag;
  277. tty->termios.c_ispeed = uport->cons->ispeed;
  278. tty->termios.c_ospeed = uport->cons->ospeed;
  279. uport->cons->cflag = 0;
  280. uport->cons->ispeed = 0;
  281. uport->cons->ospeed = 0;
  282. }
  283. /*
  284. * Initialise the hardware port settings.
  285. */
  286. uart_change_line_settings(tty, state, NULL);
  287. /*
  288. * Setup the RTS and DTR signals once the
  289. * port is open and ready to respond.
  290. */
  291. if (init_hw && C_BAUD(tty))
  292. uart_port_dtr_rts(uport, true);
  293. }
  294. /*
  295. * This is to allow setserial on this port. People may want to set
  296. * port/irq/type and then reconfigure the port properly if it failed
  297. * now.
  298. */
  299. if (retval && capable(CAP_SYS_ADMIN))
  300. return 1;
  301. return retval;
  302. }
  303. static int uart_startup(struct tty_struct *tty, struct uart_state *state,
  304. bool init_hw)
  305. {
  306. struct tty_port *port = &state->port;
  307. struct uart_port *uport;
  308. int retval;
  309. if (tty_port_initialized(port))
  310. goto out_base_port_startup;
  311. retval = uart_port_startup(tty, state, init_hw);
  312. if (retval) {
  313. set_bit(TTY_IO_ERROR, &tty->flags);
  314. return retval;
  315. }
  316. out_base_port_startup:
  317. uport = uart_port_check(state);
  318. if (!uport)
  319. return -EIO;
  320. serial_base_port_startup(uport);
  321. return 0;
  322. }
  323. /*
  324. * This routine will shutdown a serial port; interrupts are disabled, and
  325. * DTR is dropped if the hangup on close termio flag is on. Calls to
  326. * uart_shutdown are serialised by the per-port semaphore.
  327. *
  328. * uport == NULL if uart_port has already been removed
  329. */
  330. static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
  331. {
  332. struct uart_port *uport = uart_port_check(state);
  333. struct tty_port *port = &state->port;
  334. /*
  335. * Set the TTY IO error marker
  336. */
  337. if (tty)
  338. set_bit(TTY_IO_ERROR, &tty->flags);
  339. if (uport)
  340. serial_base_port_shutdown(uport);
  341. if (tty_port_initialized(port)) {
  342. tty_port_set_initialized(port, false);
  343. /*
  344. * Turn off DTR and RTS early.
  345. */
  346. if (uport) {
  347. if (uart_console(uport) && tty) {
  348. uport->cons->cflag = tty->termios.c_cflag;
  349. uport->cons->ispeed = tty->termios.c_ispeed;
  350. uport->cons->ospeed = tty->termios.c_ospeed;
  351. }
  352. if (!tty || C_HUPCL(tty))
  353. uart_port_dtr_rts(uport, false);
  354. }
  355. uart_port_shutdown(port);
  356. }
  357. /*
  358. * It's possible for shutdown to be called after suspend if we get
  359. * a DCD drop (hangup) at just the right time. Clear suspended bit so
  360. * we don't try to resume a port that has been shutdown.
  361. */
  362. tty_port_set_suspended(port, false);
  363. uart_free_xmit_buf(port);
  364. }
  365. /**
  366. * uart_update_timeout - update per-port frame timing information
  367. * @port: uart_port structure describing the port
  368. * @cflag: termios cflag value
  369. * @baud: speed of the port
  370. *
  371. * Set the @port frame timing information from which the FIFO timeout value is
  372. * derived. The @cflag value should reflect the actual hardware settings as
  373. * number of bits, parity, stop bits and baud rate is taken into account here.
  374. *
  375. * Locking: caller is expected to take @port->lock
  376. */
  377. void
  378. uart_update_timeout(struct uart_port *port, unsigned int cflag,
  379. unsigned int baud)
  380. {
  381. u64 temp = tty_get_frame_size(cflag);
  382. temp *= NSEC_PER_SEC;
  383. port->frame_time = (unsigned int)DIV64_U64_ROUND_UP(temp, baud);
  384. }
  385. EXPORT_SYMBOL(uart_update_timeout);
  386. /**
  387. * uart_get_baud_rate - return baud rate for a particular port
  388. * @port: uart_port structure describing the port in question.
  389. * @termios: desired termios settings
  390. * @old: old termios (or %NULL)
  391. * @min: minimum acceptable baud rate
  392. * @max: maximum acceptable baud rate
  393. *
  394. * Decode the termios structure into a numeric baud rate, taking account of the
  395. * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
  396. * baud.
  397. *
  398. * If the new baud rate is invalid, try the @old termios setting. If it's still
  399. * invalid, we try 9600 baud. If that is also invalid 0 is returned.
  400. *
  401. * The @termios structure is updated to reflect the baud rate we're actually
  402. * going to be using. Don't do this for the case where B0 is requested ("hang
  403. * up").
  404. *
  405. * Locking: caller dependent
  406. */
  407. unsigned int
  408. uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
  409. const struct ktermios *old, unsigned int min, unsigned int max)
  410. {
  411. unsigned int try;
  412. unsigned int baud;
  413. unsigned int altbaud;
  414. int hung_up = 0;
  415. upf_t flags = port->flags & UPF_SPD_MASK;
  416. switch (flags) {
  417. case UPF_SPD_HI:
  418. altbaud = 57600;
  419. break;
  420. case UPF_SPD_VHI:
  421. altbaud = 115200;
  422. break;
  423. case UPF_SPD_SHI:
  424. altbaud = 230400;
  425. break;
  426. case UPF_SPD_WARP:
  427. altbaud = 460800;
  428. break;
  429. default:
  430. altbaud = 38400;
  431. break;
  432. }
  433. for (try = 0; try < 2; try++) {
  434. baud = tty_termios_baud_rate(termios);
  435. /*
  436. * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
  437. * Die! Die! Die!
  438. */
  439. if (try == 0 && baud == 38400)
  440. baud = altbaud;
  441. /*
  442. * Special case: B0 rate.
  443. */
  444. if (baud == 0) {
  445. hung_up = 1;
  446. baud = 9600;
  447. }
  448. if (baud >= min && baud <= max)
  449. return baud;
  450. /*
  451. * Oops, the quotient was zero. Try again with
  452. * the old baud rate if possible.
  453. */
  454. termios->c_cflag &= ~CBAUD;
  455. if (old) {
  456. baud = tty_termios_baud_rate(old);
  457. if (!hung_up)
  458. tty_termios_encode_baud_rate(termios,
  459. baud, baud);
  460. old = NULL;
  461. continue;
  462. }
  463. /*
  464. * As a last resort, if the range cannot be met then clip to
  465. * the nearest chip supported rate.
  466. */
  467. if (!hung_up) {
  468. if (baud <= min)
  469. tty_termios_encode_baud_rate(termios,
  470. min + 1, min + 1);
  471. else
  472. tty_termios_encode_baud_rate(termios,
  473. max - 1, max - 1);
  474. }
  475. }
  476. return 0;
  477. }
  478. EXPORT_SYMBOL(uart_get_baud_rate);
  479. /**
  480. * uart_get_divisor - return uart clock divisor
  481. * @port: uart_port structure describing the port
  482. * @baud: desired baud rate
  483. *
  484. * Calculate the divisor (baud_base / baud) for the specified @baud,
  485. * appropriately rounded.
  486. *
  487. * If 38400 baud and custom divisor is selected, return the custom divisor
  488. * instead.
  489. *
  490. * Locking: caller dependent
  491. */
  492. unsigned int
  493. uart_get_divisor(struct uart_port *port, unsigned int baud)
  494. {
  495. unsigned int quot;
  496. /*
  497. * Old custom speed handling.
  498. */
  499. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  500. quot = port->custom_divisor;
  501. else
  502. quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
  503. return quot;
  504. }
  505. EXPORT_SYMBOL(uart_get_divisor);
  506. static int uart_put_char(struct tty_struct *tty, u8 c)
  507. {
  508. struct uart_state *state = tty->driver_data;
  509. struct uart_port *port;
  510. unsigned long flags;
  511. int ret = 0;
  512. port = uart_port_ref_lock(state, &flags);
  513. if (!state->port.xmit_buf) {
  514. uart_port_unlock_deref(port, flags);
  515. return 0;
  516. }
  517. if (port)
  518. ret = kfifo_put(&state->port.xmit_fifo, c);
  519. uart_port_unlock_deref(port, flags);
  520. return ret;
  521. }
  522. static void uart_flush_chars(struct tty_struct *tty)
  523. {
  524. uart_start(tty);
  525. }
  526. static ssize_t uart_write(struct tty_struct *tty, const u8 *buf, size_t count)
  527. {
  528. struct uart_state *state = tty->driver_data;
  529. struct uart_port *port;
  530. unsigned long flags;
  531. int ret = 0;
  532. /*
  533. * This means you called this function _after_ the port was
  534. * closed. No cookie for you.
  535. */
  536. if (WARN_ON(!state))
  537. return -EL3HLT;
  538. port = uart_port_ref_lock(state, &flags);
  539. if (!state->port.xmit_buf) {
  540. uart_port_unlock_deref(port, flags);
  541. return 0;
  542. }
  543. if (port)
  544. ret = kfifo_in(&state->port.xmit_fifo, buf, count);
  545. __uart_start(state);
  546. uart_port_unlock_deref(port, flags);
  547. return ret;
  548. }
  549. static unsigned int uart_write_room(struct tty_struct *tty)
  550. {
  551. struct uart_state *state = tty->driver_data;
  552. struct uart_port *port;
  553. unsigned long flags;
  554. unsigned int ret;
  555. port = uart_port_ref_lock(state, &flags);
  556. if (!state->port.xmit_buf)
  557. ret = 0;
  558. else
  559. ret = kfifo_avail(&state->port.xmit_fifo);
  560. uart_port_unlock_deref(port, flags);
  561. return ret;
  562. }
  563. static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
  564. {
  565. struct uart_state *state = tty->driver_data;
  566. struct uart_port *port;
  567. unsigned long flags;
  568. unsigned int ret;
  569. port = uart_port_ref_lock(state, &flags);
  570. ret = kfifo_len(&state->port.xmit_fifo);
  571. uart_port_unlock_deref(port, flags);
  572. return ret;
  573. }
  574. static void uart_flush_buffer(struct tty_struct *tty)
  575. {
  576. struct uart_state *state = tty->driver_data;
  577. struct uart_port *port;
  578. unsigned long flags;
  579. /*
  580. * This means you called this function _after_ the port was
  581. * closed. No cookie for you.
  582. */
  583. if (WARN_ON(!state))
  584. return;
  585. pr_debug("uart_flush_buffer(%d) called\n", tty->index);
  586. port = uart_port_ref_lock(state, &flags);
  587. if (!port)
  588. return;
  589. kfifo_reset(&state->port.xmit_fifo);
  590. if (port->ops->flush_buffer)
  591. port->ops->flush_buffer(port);
  592. uart_port_unlock_deref(port, flags);
  593. tty_port_tty_wakeup(&state->port);
  594. }
  595. /*
  596. * This function performs low-level write of high-priority XON/XOFF
  597. * character and accounting for it.
  598. *
  599. * Requires uart_port to implement .serial_out().
  600. */
  601. void uart_xchar_out(struct uart_port *uport, int offset)
  602. {
  603. serial_port_out(uport, offset, uport->x_char);
  604. uport->icount.tx++;
  605. uport->x_char = 0;
  606. }
  607. EXPORT_SYMBOL_GPL(uart_xchar_out);
  608. /*
  609. * This function is used to send a high-priority XON/XOFF character to
  610. * the device
  611. */
  612. static void uart_send_xchar(struct tty_struct *tty, u8 ch)
  613. {
  614. struct uart_state *state = tty->driver_data;
  615. struct uart_port *port;
  616. port = uart_port_ref(state);
  617. if (!port)
  618. return;
  619. if (port->ops->send_xchar)
  620. port->ops->send_xchar(port, ch);
  621. else {
  622. guard(uart_port_lock_irqsave)(port);
  623. port->x_char = ch;
  624. if (ch)
  625. port->ops->start_tx(port);
  626. }
  627. uart_port_deref(port);
  628. }
  629. static void uart_throttle(struct tty_struct *tty)
  630. {
  631. struct uart_state *state = tty->driver_data;
  632. upstat_t mask = UPSTAT_SYNC_FIFO;
  633. struct uart_port *port;
  634. port = uart_port_ref(state);
  635. if (!port)
  636. return;
  637. if (I_IXOFF(tty))
  638. mask |= UPSTAT_AUTOXOFF;
  639. if (C_CRTSCTS(tty))
  640. mask |= UPSTAT_AUTORTS;
  641. if (port->status & mask) {
  642. port->ops->throttle(port);
  643. mask &= ~port->status;
  644. }
  645. if (mask & UPSTAT_AUTORTS)
  646. uart_clear_mctrl(port, TIOCM_RTS);
  647. if (mask & UPSTAT_AUTOXOFF)
  648. uart_send_xchar(tty, STOP_CHAR(tty));
  649. uart_port_deref(port);
  650. }
  651. static void uart_unthrottle(struct tty_struct *tty)
  652. {
  653. struct uart_state *state = tty->driver_data;
  654. upstat_t mask = UPSTAT_SYNC_FIFO;
  655. struct uart_port *port;
  656. port = uart_port_ref(state);
  657. if (!port)
  658. return;
  659. if (I_IXOFF(tty))
  660. mask |= UPSTAT_AUTOXOFF;
  661. if (C_CRTSCTS(tty))
  662. mask |= UPSTAT_AUTORTS;
  663. if (port->status & mask) {
  664. port->ops->unthrottle(port);
  665. mask &= ~port->status;
  666. }
  667. if (mask & UPSTAT_AUTORTS)
  668. uart_set_mctrl(port, TIOCM_RTS);
  669. if (mask & UPSTAT_AUTOXOFF)
  670. uart_send_xchar(tty, START_CHAR(tty));
  671. uart_port_deref(port);
  672. }
  673. static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
  674. {
  675. struct uart_state *state = container_of(port, struct uart_state, port);
  676. struct uart_port *uport;
  677. /* Initialize structure in case we error out later to prevent any stack info leakage. */
  678. *retinfo = (struct serial_struct){};
  679. /*
  680. * Ensure the state we copy is consistent and no hardware changes
  681. * occur as we go
  682. */
  683. guard(mutex)(&port->mutex);
  684. uport = uart_port_check(state);
  685. if (!uport)
  686. return -ENODEV;
  687. retinfo->type = uport->type;
  688. retinfo->line = uport->line;
  689. retinfo->port = uport->iobase;
  690. if (HIGH_BITS_OFFSET)
  691. retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
  692. retinfo->irq = uport->irq;
  693. retinfo->flags = (__force int)uport->flags;
  694. retinfo->xmit_fifo_size = uport->fifosize;
  695. retinfo->baud_base = uport->uartclk / 16;
  696. retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
  697. retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  698. ASYNC_CLOSING_WAIT_NONE :
  699. jiffies_to_msecs(port->closing_wait) / 10;
  700. retinfo->custom_divisor = uport->custom_divisor;
  701. retinfo->hub6 = uport->hub6;
  702. retinfo->io_type = uport->iotype;
  703. retinfo->iomem_reg_shift = uport->regshift;
  704. retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
  705. return 0;
  706. }
  707. static int uart_get_info_user(struct tty_struct *tty,
  708. struct serial_struct *ss)
  709. {
  710. struct uart_state *state = tty->driver_data;
  711. struct tty_port *port = &state->port;
  712. return uart_get_info(port, ss) < 0 ? -EIO : 0;
  713. }
  714. static int uart_change_port(struct uart_port *uport,
  715. const struct serial_struct *new_info,
  716. unsigned long new_port)
  717. {
  718. unsigned long old_iobase, old_mapbase;
  719. unsigned int old_type, old_iotype, old_hub6, old_shift;
  720. int retval;
  721. old_iobase = uport->iobase;
  722. old_mapbase = uport->mapbase;
  723. old_type = uport->type;
  724. old_hub6 = uport->hub6;
  725. old_iotype = uport->iotype;
  726. old_shift = uport->regshift;
  727. if (old_type != PORT_UNKNOWN && uport->ops->release_port)
  728. uport->ops->release_port(uport);
  729. uport->iobase = new_port;
  730. uport->type = new_info->type;
  731. uport->hub6 = new_info->hub6;
  732. uport->iotype = new_info->io_type;
  733. uport->regshift = new_info->iomem_reg_shift;
  734. uport->mapbase = (unsigned long)new_info->iomem_base;
  735. if (uport->type == PORT_UNKNOWN || !uport->ops->request_port)
  736. return 0;
  737. retval = uport->ops->request_port(uport);
  738. if (retval == 0)
  739. return 0; /* succeeded => done */
  740. /*
  741. * If we fail to request resources for the new port, try to restore the
  742. * old settings.
  743. */
  744. uport->iobase = old_iobase;
  745. uport->type = old_type;
  746. uport->hub6 = old_hub6;
  747. uport->iotype = old_iotype;
  748. uport->regshift = old_shift;
  749. uport->mapbase = old_mapbase;
  750. if (old_type == PORT_UNKNOWN)
  751. return retval;
  752. retval = uport->ops->request_port(uport);
  753. /* If we failed to restore the old settings, we fail like this. */
  754. if (retval)
  755. uport->type = PORT_UNKNOWN;
  756. /* We failed anyway. */
  757. return -EBUSY;
  758. }
  759. static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
  760. struct uart_state *state,
  761. struct serial_struct *new_info)
  762. {
  763. struct uart_port *uport = uart_port_check(state);
  764. unsigned long new_port;
  765. unsigned int old_custom_divisor, close_delay, closing_wait;
  766. bool change_irq, change_port;
  767. upf_t old_flags, new_flags;
  768. int retval;
  769. if (!uport)
  770. return -EIO;
  771. new_port = new_info->port;
  772. if (HIGH_BITS_OFFSET)
  773. new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
  774. new_info->irq = irq_canonicalize(new_info->irq);
  775. close_delay = msecs_to_jiffies(new_info->close_delay * 10);
  776. closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  777. ASYNC_CLOSING_WAIT_NONE :
  778. msecs_to_jiffies(new_info->closing_wait * 10);
  779. change_irq = !(uport->flags & UPF_FIXED_PORT)
  780. && new_info->irq != uport->irq;
  781. /*
  782. * Since changing the 'type' of the port changes its resource
  783. * allocations, we should treat type changes the same as
  784. * IO port changes.
  785. */
  786. change_port = !(uport->flags & UPF_FIXED_PORT)
  787. && (new_port != uport->iobase ||
  788. (unsigned long)new_info->iomem_base != uport->mapbase ||
  789. new_info->hub6 != uport->hub6 ||
  790. new_info->io_type != uport->iotype ||
  791. new_info->iomem_reg_shift != uport->regshift ||
  792. new_info->type != uport->type);
  793. old_flags = uport->flags;
  794. new_flags = (__force upf_t)new_info->flags;
  795. old_custom_divisor = uport->custom_divisor;
  796. if (!(uport->flags & UPF_FIXED_PORT)) {
  797. unsigned int uartclk = new_info->baud_base * 16;
  798. /* check needs to be done here before other settings made */
  799. if (uartclk == 0)
  800. return -EINVAL;
  801. }
  802. if (!capable(CAP_SYS_ADMIN)) {
  803. if (change_irq || change_port ||
  804. (new_info->baud_base != uport->uartclk / 16) ||
  805. (close_delay != port->close_delay) ||
  806. (closing_wait != port->closing_wait) ||
  807. (new_info->xmit_fifo_size &&
  808. new_info->xmit_fifo_size != uport->fifosize) ||
  809. (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
  810. return -EPERM;
  811. uport->flags = ((uport->flags & ~UPF_USR_MASK) |
  812. (new_flags & UPF_USR_MASK));
  813. uport->custom_divisor = new_info->custom_divisor;
  814. goto check_and_exit;
  815. }
  816. if (change_irq || change_port) {
  817. retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
  818. if (retval)
  819. return retval;
  820. }
  821. /* Ask the low level driver to verify the settings. */
  822. if (uport->ops->verify_port) {
  823. retval = uport->ops->verify_port(uport, new_info);
  824. if (retval)
  825. return retval;
  826. }
  827. if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) ||
  828. (new_info->baud_base < 9600))
  829. return -EINVAL;
  830. if (change_port || change_irq) {
  831. /* Make sure that we are the sole user of this port. */
  832. if (tty_port_users(port) > 1)
  833. return -EBUSY;
  834. /*
  835. * We need to shutdown the serial port at the old
  836. * port/type/irq combination.
  837. */
  838. uart_shutdown(tty, state);
  839. }
  840. if (change_port) {
  841. retval = uart_change_port(uport, new_info, new_port);
  842. if (retval)
  843. return retval;
  844. }
  845. if (change_irq)
  846. uport->irq = new_info->irq;
  847. if (!(uport->flags & UPF_FIXED_PORT))
  848. uport->uartclk = new_info->baud_base * 16;
  849. uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
  850. (new_flags & UPF_CHANGE_MASK);
  851. uport->custom_divisor = new_info->custom_divisor;
  852. port->close_delay = close_delay;
  853. port->closing_wait = closing_wait;
  854. if (new_info->xmit_fifo_size)
  855. uport->fifosize = new_info->xmit_fifo_size;
  856. check_and_exit:
  857. if (uport->type == PORT_UNKNOWN)
  858. return 0;
  859. if (tty_port_initialized(port)) {
  860. if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
  861. old_custom_divisor != uport->custom_divisor) {
  862. /*
  863. * If they're setting up a custom divisor or speed,
  864. * instead of clearing it, then bitch about it.
  865. */
  866. if (uport->flags & UPF_SPD_MASK) {
  867. dev_notice_ratelimited(uport->dev,
  868. "%s sets custom speed on %s. This is deprecated.\n",
  869. current->comm,
  870. tty_name(port->tty));
  871. }
  872. uart_change_line_settings(tty, state, NULL);
  873. }
  874. return 0;
  875. }
  876. retval = uart_startup(tty, state, true);
  877. if (retval < 0)
  878. return retval;
  879. if (retval == 0)
  880. tty_port_set_initialized(port, true);
  881. return 0;
  882. }
  883. static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
  884. {
  885. struct uart_state *state = tty->driver_data;
  886. struct tty_port *port = &state->port;
  887. guard(rwsem_write)(&tty->termios_rwsem);
  888. /*
  889. * This semaphore protects port->count. It is also
  890. * very useful to prevent opens. Also, take the
  891. * port configuration semaphore to make sure that a
  892. * module insertion/removal doesn't change anything
  893. * under us.
  894. */
  895. guard(mutex)(&port->mutex);
  896. return uart_set_info(tty, port, state, ss);
  897. }
  898. /**
  899. * uart_get_lsr_info - get line status register info
  900. * @tty: tty associated with the UART
  901. * @state: UART being queried
  902. * @value: returned modem value
  903. */
  904. static int uart_get_lsr_info(struct tty_struct *tty,
  905. struct uart_state *state, unsigned int __user *value)
  906. {
  907. struct uart_port *uport = uart_port_check(state);
  908. unsigned int result;
  909. result = uport->ops->tx_empty(uport);
  910. /*
  911. * If we're about to load something into the transmit
  912. * register, we'll pretend the transmitter isn't empty to
  913. * avoid a race condition (depending on when the transmit
  914. * interrupt happens).
  915. */
  916. if (uport->x_char ||
  917. (!kfifo_is_empty(&state->port.xmit_fifo) &&
  918. !uart_tx_stopped(uport)))
  919. result &= ~TIOCSER_TEMT;
  920. return put_user(result, value);
  921. }
  922. static int uart_tiocmget(struct tty_struct *tty)
  923. {
  924. struct uart_state *state = tty->driver_data;
  925. struct tty_port *port = &state->port;
  926. struct uart_port *uport;
  927. guard(mutex)(&port->mutex);
  928. uport = uart_port_check(state);
  929. if (!uport || tty_io_error(tty))
  930. return -EIO;
  931. guard(uart_port_lock_irq)(uport);
  932. return uport->mctrl | uport->ops->get_mctrl(uport);
  933. }
  934. static int
  935. uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
  936. {
  937. struct uart_state *state = tty->driver_data;
  938. struct tty_port *port = &state->port;
  939. struct uart_port *uport;
  940. guard(mutex)(&port->mutex);
  941. uport = uart_port_check(state);
  942. if (!uport || tty_io_error(tty))
  943. return -EIO;
  944. uart_update_mctrl(uport, set, clear);
  945. return 0;
  946. }
  947. static int uart_break_ctl(struct tty_struct *tty, int break_state)
  948. {
  949. struct uart_state *state = tty->driver_data;
  950. struct tty_port *port = &state->port;
  951. struct uart_port *uport;
  952. guard(mutex)(&port->mutex);
  953. uport = uart_port_check(state);
  954. if (!uport)
  955. return -EIO;
  956. if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
  957. uport->ops->break_ctl(uport, break_state);
  958. return 0;
  959. }
  960. static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
  961. {
  962. struct tty_port *port = &state->port;
  963. struct uart_port *uport;
  964. int flags, ret;
  965. if (!capable(CAP_SYS_ADMIN))
  966. return -EPERM;
  967. /*
  968. * Take the per-port semaphore. This prevents count from
  969. * changing, and hence any extra opens of the port while
  970. * we're auto-configuring.
  971. */
  972. scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) {
  973. uport = uart_port_check(state);
  974. if (!uport)
  975. return -EIO;
  976. if (tty_port_users(port) != 1)
  977. return -EBUSY;
  978. uart_shutdown(tty, state);
  979. /*
  980. * If we already have a port type configured,
  981. * we must release its resources.
  982. */
  983. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  984. uport->ops->release_port(uport);
  985. flags = UART_CONFIG_TYPE;
  986. if (uport->flags & UPF_AUTO_IRQ)
  987. flags |= UART_CONFIG_IRQ;
  988. /*
  989. * This will claim the ports resources if
  990. * a port is found.
  991. */
  992. uport->ops->config_port(uport, flags);
  993. ret = uart_startup(tty, state, true);
  994. if (ret < 0)
  995. return ret;
  996. if (ret > 0)
  997. return 0;
  998. tty_port_set_initialized(port, true);
  999. }
  1000. return 0;
  1001. }
  1002. static void uart_enable_ms(struct uart_port *uport)
  1003. {
  1004. /*
  1005. * Force modem status interrupts on
  1006. */
  1007. if (uport->ops->enable_ms)
  1008. uport->ops->enable_ms(uport);
  1009. }
  1010. /*
  1011. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1012. * - mask passed in arg for lines of interest
  1013. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1014. * Caller should use TIOCGICOUNT to see which one it was
  1015. *
  1016. * FIXME: This wants extracting into a common all driver implementation
  1017. * of TIOCMWAIT using tty_port.
  1018. */
  1019. static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
  1020. {
  1021. struct uart_port *uport;
  1022. struct tty_port *port = &state->port;
  1023. DECLARE_WAITQUEUE(wait, current);
  1024. struct uart_icount cprev, cnow;
  1025. int ret;
  1026. /*
  1027. * note the counters on entry
  1028. */
  1029. uport = uart_port_ref(state);
  1030. if (!uport)
  1031. return -EIO;
  1032. scoped_guard(uart_port_lock_irq, uport) {
  1033. memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
  1034. uart_enable_ms(uport);
  1035. }
  1036. add_wait_queue(&port->delta_msr_wait, &wait);
  1037. for (;;) {
  1038. scoped_guard(uart_port_lock_irq, uport)
  1039. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1040. set_current_state(TASK_INTERRUPTIBLE);
  1041. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1042. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1043. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1044. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1045. ret = 0;
  1046. break;
  1047. }
  1048. schedule();
  1049. /* see if a signal did it */
  1050. if (signal_pending(current)) {
  1051. ret = -ERESTARTSYS;
  1052. break;
  1053. }
  1054. cprev = cnow;
  1055. }
  1056. __set_current_state(TASK_RUNNING);
  1057. remove_wait_queue(&port->delta_msr_wait, &wait);
  1058. uart_port_deref(uport);
  1059. return ret;
  1060. }
  1061. /*
  1062. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1063. * Return: write counters to the user passed counter struct
  1064. * NB: both 1->0 and 0->1 transitions are counted except for
  1065. * RI where only 0->1 is counted.
  1066. */
  1067. static int uart_get_icount(struct tty_struct *tty,
  1068. struct serial_icounter_struct *icount)
  1069. {
  1070. struct uart_state *state = tty->driver_data;
  1071. struct uart_icount cnow;
  1072. struct uart_port *uport;
  1073. unsigned long flags;
  1074. uport = uart_port_ref_lock(state, &flags);
  1075. if (!uport)
  1076. return -EIO;
  1077. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1078. uart_port_unlock_deref(uport, flags);
  1079. icount->cts = cnow.cts;
  1080. icount->dsr = cnow.dsr;
  1081. icount->rng = cnow.rng;
  1082. icount->dcd = cnow.dcd;
  1083. icount->rx = cnow.rx;
  1084. icount->tx = cnow.tx;
  1085. icount->frame = cnow.frame;
  1086. icount->overrun = cnow.overrun;
  1087. icount->parity = cnow.parity;
  1088. icount->brk = cnow.brk;
  1089. icount->buf_overrun = cnow.buf_overrun;
  1090. return 0;
  1091. }
  1092. #define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
  1093. SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
  1094. SER_RS485_TERMINATE_BUS)
  1095. static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
  1096. {
  1097. u32 flags = rs485->flags;
  1098. /* Don't return -EINVAL for unsupported legacy flags */
  1099. flags &= ~SER_RS485_LEGACY_FLAGS;
  1100. /*
  1101. * For any bit outside of the legacy ones that is not supported by
  1102. * the driver, return -EINVAL.
  1103. */
  1104. if (flags & ~port->rs485_supported.flags)
  1105. return -EINVAL;
  1106. /* Asking for address w/o addressing mode? */
  1107. if (!(rs485->flags & SER_RS485_ADDRB) &&
  1108. (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
  1109. return -EINVAL;
  1110. /* Address given but not enabled? */
  1111. if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
  1112. return -EINVAL;
  1113. if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
  1114. return -EINVAL;
  1115. return 0;
  1116. }
  1117. static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
  1118. struct serial_rs485 *rs485)
  1119. {
  1120. if (!port->rs485_supported.delay_rts_before_send) {
  1121. if (rs485->delay_rts_before_send) {
  1122. dev_warn_ratelimited(port->dev,
  1123. "%s (%u): RTS delay before sending not supported\n",
  1124. port->name, port->line);
  1125. }
  1126. rs485->delay_rts_before_send = 0;
  1127. } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
  1128. rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
  1129. dev_warn_ratelimited(port->dev,
  1130. "%s (%u): RTS delay before sending clamped to %u ms\n",
  1131. port->name, port->line, rs485->delay_rts_before_send);
  1132. }
  1133. if (!port->rs485_supported.delay_rts_after_send) {
  1134. if (rs485->delay_rts_after_send) {
  1135. dev_warn_ratelimited(port->dev,
  1136. "%s (%u): RTS delay after sending not supported\n",
  1137. port->name, port->line);
  1138. }
  1139. rs485->delay_rts_after_send = 0;
  1140. } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
  1141. rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
  1142. dev_warn_ratelimited(port->dev,
  1143. "%s (%u): RTS delay after sending clamped to %u ms\n",
  1144. port->name, port->line, rs485->delay_rts_after_send);
  1145. }
  1146. }
  1147. static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
  1148. {
  1149. u32 supported_flags = port->rs485_supported.flags;
  1150. if (!(rs485->flags & SER_RS485_ENABLED)) {
  1151. memset(rs485, 0, sizeof(*rs485));
  1152. return;
  1153. }
  1154. /* Clear other RS485 flags but SER_RS485_TERMINATE_BUS and return if enabling RS422 */
  1155. if (rs485->flags & SER_RS485_MODE_RS422) {
  1156. rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422 | SER_RS485_TERMINATE_BUS);
  1157. return;
  1158. }
  1159. rs485->flags &= supported_flags;
  1160. /* Pick sane settings if the user hasn't */
  1161. if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
  1162. !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
  1163. if (supported_flags & SER_RS485_RTS_ON_SEND) {
  1164. rs485->flags |= SER_RS485_RTS_ON_SEND;
  1165. rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
  1166. dev_warn_ratelimited(port->dev,
  1167. "%s (%u): invalid RTS setting, using RTS_ON_SEND instead\n",
  1168. port->name, port->line);
  1169. } else {
  1170. rs485->flags |= SER_RS485_RTS_AFTER_SEND;
  1171. rs485->flags &= ~SER_RS485_RTS_ON_SEND;
  1172. dev_warn_ratelimited(port->dev,
  1173. "%s (%u): invalid RTS setting, using RTS_AFTER_SEND instead\n",
  1174. port->name, port->line);
  1175. }
  1176. }
  1177. uart_sanitize_serial_rs485_delays(port, rs485);
  1178. /* Return clean padding area to userspace */
  1179. memset(rs485->padding0, 0, sizeof(rs485->padding0));
  1180. memset(rs485->padding1, 0, sizeof(rs485->padding1));
  1181. }
  1182. static void uart_set_rs485_termination(struct uart_port *port,
  1183. const struct serial_rs485 *rs485)
  1184. {
  1185. if (!(rs485->flags & SER_RS485_ENABLED))
  1186. return;
  1187. gpiod_set_value_cansleep(port->rs485_term_gpio,
  1188. !!(rs485->flags & SER_RS485_TERMINATE_BUS));
  1189. }
  1190. static void uart_set_rs485_rx_during_tx(struct uart_port *port,
  1191. const struct serial_rs485 *rs485)
  1192. {
  1193. if (!(rs485->flags & SER_RS485_ENABLED))
  1194. return;
  1195. gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
  1196. !!(rs485->flags & SER_RS485_RX_DURING_TX));
  1197. }
  1198. static int uart_rs485_config(struct uart_port *port)
  1199. {
  1200. struct serial_rs485 *rs485 = &port->rs485;
  1201. int ret;
  1202. if (!(rs485->flags & SER_RS485_ENABLED))
  1203. return 0;
  1204. uart_sanitize_serial_rs485(port, rs485);
  1205. uart_set_rs485_termination(port, rs485);
  1206. uart_set_rs485_rx_during_tx(port, rs485);
  1207. scoped_guard(uart_port_lock_irqsave, port)
  1208. ret = port->rs485_config(port, NULL, rs485);
  1209. if (ret) {
  1210. memset(rs485, 0, sizeof(*rs485));
  1211. /* unset GPIOs */
  1212. gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
  1213. gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
  1214. }
  1215. return ret;
  1216. }
  1217. static int uart_get_rs485_config(struct uart_port *port,
  1218. struct serial_rs485 __user *rs485)
  1219. {
  1220. struct serial_rs485 aux;
  1221. scoped_guard(uart_port_lock_irqsave, port)
  1222. aux = port->rs485;
  1223. if (copy_to_user(rs485, &aux, sizeof(aux)))
  1224. return -EFAULT;
  1225. return 0;
  1226. }
  1227. static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
  1228. struct serial_rs485 __user *rs485_user)
  1229. {
  1230. struct serial_rs485 rs485;
  1231. int ret;
  1232. if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
  1233. return -ENOTTY;
  1234. if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
  1235. return -EFAULT;
  1236. ret = uart_check_rs485_flags(port, &rs485);
  1237. if (ret)
  1238. return ret;
  1239. uart_sanitize_serial_rs485(port, &rs485);
  1240. uart_set_rs485_termination(port, &rs485);
  1241. uart_set_rs485_rx_during_tx(port, &rs485);
  1242. scoped_guard(uart_port_lock_irqsave, port) {
  1243. ret = port->rs485_config(port, &tty->termios, &rs485);
  1244. if (!ret) {
  1245. port->rs485 = rs485;
  1246. /* Reset RTS and other mctrl lines when disabling RS485 */
  1247. if (!(rs485.flags & SER_RS485_ENABLED))
  1248. port->ops->set_mctrl(port, port->mctrl);
  1249. }
  1250. }
  1251. if (ret) {
  1252. /* restore old GPIO settings */
  1253. gpiod_set_value_cansleep(port->rs485_term_gpio,
  1254. !!(port->rs485.flags & SER_RS485_TERMINATE_BUS));
  1255. gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
  1256. !!(port->rs485.flags & SER_RS485_RX_DURING_TX));
  1257. return ret;
  1258. }
  1259. if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
  1260. return -EFAULT;
  1261. return 0;
  1262. }
  1263. static int uart_get_iso7816_config(struct uart_port *port,
  1264. struct serial_iso7816 __user *iso7816)
  1265. {
  1266. struct serial_iso7816 aux;
  1267. if (!port->iso7816_config)
  1268. return -ENOTTY;
  1269. scoped_guard(uart_port_lock_irqsave, port)
  1270. aux = port->iso7816;
  1271. if (copy_to_user(iso7816, &aux, sizeof(aux)))
  1272. return -EFAULT;
  1273. return 0;
  1274. }
  1275. static int uart_set_iso7816_config(struct uart_port *port,
  1276. struct serial_iso7816 __user *iso7816_user)
  1277. {
  1278. struct serial_iso7816 iso7816;
  1279. int i;
  1280. if (!port->iso7816_config)
  1281. return -ENOTTY;
  1282. if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
  1283. return -EFAULT;
  1284. /*
  1285. * There are 5 words reserved for future use. Check that userspace
  1286. * doesn't put stuff in there to prevent breakages in the future.
  1287. */
  1288. for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++)
  1289. if (iso7816.reserved[i])
  1290. return -EINVAL;
  1291. scoped_guard(uart_port_lock_irqsave, port) {
  1292. int ret = port->iso7816_config(port, &iso7816);
  1293. if (ret)
  1294. return ret;
  1295. }
  1296. if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
  1297. return -EFAULT;
  1298. return 0;
  1299. }
  1300. /*
  1301. * Called via sys_ioctl. We can use spin_lock_irq() here.
  1302. */
  1303. static int
  1304. uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  1305. {
  1306. struct uart_state *state = tty->driver_data;
  1307. struct tty_port *port = &state->port;
  1308. struct uart_port *uport;
  1309. void __user *uarg = (void __user *)arg;
  1310. int ret = -ENOIOCTLCMD;
  1311. /* This ioctl doesn't rely on the hardware to be present. */
  1312. if (cmd == TIOCSERCONFIG) {
  1313. guard(rwsem_write)(&tty->termios_rwsem);
  1314. return uart_do_autoconfig(tty, state);
  1315. }
  1316. if (tty_io_error(tty))
  1317. return -EIO;
  1318. /* This should only be used when the hardware is present. */
  1319. if (cmd == TIOCMIWAIT)
  1320. return uart_wait_modem_status(state, arg);
  1321. /* rs485_config requires more locking than others */
  1322. if (cmd == TIOCSRS485)
  1323. down_write(&tty->termios_rwsem);
  1324. scoped_guard(mutex, &port->mutex) {
  1325. uport = uart_port_check(state);
  1326. if (!uport || tty_io_error(tty)) {
  1327. ret = -EIO;
  1328. break;
  1329. }
  1330. /*
  1331. * All these rely on hardware being present and need to be
  1332. * protected against the tty being hung up.
  1333. */
  1334. switch (cmd) {
  1335. case TIOCSERGETLSR: /* Get line status register */
  1336. ret = uart_get_lsr_info(tty, state, uarg);
  1337. break;
  1338. case TIOCGRS485:
  1339. ret = uart_get_rs485_config(uport, uarg);
  1340. break;
  1341. case TIOCSRS485:
  1342. ret = uart_set_rs485_config(tty, uport, uarg);
  1343. break;
  1344. case TIOCSISO7816:
  1345. ret = uart_set_iso7816_config(state->uart_port, uarg);
  1346. break;
  1347. case TIOCGISO7816:
  1348. ret = uart_get_iso7816_config(state->uart_port, uarg);
  1349. break;
  1350. default:
  1351. if (uport->ops->ioctl)
  1352. ret = uport->ops->ioctl(uport, cmd, arg);
  1353. break;
  1354. }
  1355. }
  1356. if (cmd == TIOCSRS485)
  1357. up_write(&tty->termios_rwsem);
  1358. return ret;
  1359. }
  1360. static void uart_set_ldisc(struct tty_struct *tty)
  1361. {
  1362. struct uart_state *state = tty->driver_data;
  1363. struct uart_port *uport;
  1364. struct tty_port *port = &state->port;
  1365. if (!tty_port_initialized(port))
  1366. return;
  1367. guard(mutex)(&state->port.mutex);
  1368. uport = uart_port_check(state);
  1369. if (uport && uport->ops->set_ldisc)
  1370. uport->ops->set_ldisc(uport, &tty->termios);
  1371. }
  1372. static void uart_set_termios(struct tty_struct *tty,
  1373. const struct ktermios *old_termios)
  1374. {
  1375. struct uart_state *state = tty->driver_data;
  1376. struct uart_port *uport;
  1377. unsigned int cflag = tty->termios.c_cflag;
  1378. unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
  1379. bool sw_changed = false;
  1380. guard(mutex)(&state->port.mutex);
  1381. uport = uart_port_check(state);
  1382. if (!uport)
  1383. return;
  1384. /*
  1385. * Drivers doing software flow control also need to know
  1386. * about changes to these input settings.
  1387. */
  1388. if (uport->flags & UPF_SOFT_FLOW) {
  1389. iflag_mask |= IXANY|IXON|IXOFF;
  1390. sw_changed =
  1391. tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
  1392. tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
  1393. }
  1394. /*
  1395. * These are the bits that are used to setup various
  1396. * flags in the low level driver. We can ignore the Bfoo
  1397. * bits in c_cflag; c_[io]speed will always be set
  1398. * appropriately by set_termios() in tty_ioctl.c
  1399. */
  1400. if ((cflag ^ old_termios->c_cflag) == 0 &&
  1401. tty->termios.c_ospeed == old_termios->c_ospeed &&
  1402. tty->termios.c_ispeed == old_termios->c_ispeed &&
  1403. ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
  1404. !sw_changed)
  1405. return;
  1406. uart_change_line_settings(tty, state, old_termios);
  1407. /* reload cflag from termios; port driver may have overridden flags */
  1408. cflag = tty->termios.c_cflag;
  1409. /* Handle transition to B0 status */
  1410. if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0))
  1411. uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
  1412. /* Handle transition away from B0 status */
  1413. else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) {
  1414. unsigned int mask = TIOCM_DTR;
  1415. if (!(cflag & CRTSCTS) || !tty_throttled(tty))
  1416. mask |= TIOCM_RTS;
  1417. uart_set_mctrl(uport, mask);
  1418. }
  1419. }
  1420. /*
  1421. * Calls to uart_close() are serialised via the tty_lock in
  1422. * drivers/tty/tty_io.c:tty_release()
  1423. * drivers/tty/tty_io.c:do_tty_hangup()
  1424. */
  1425. static void uart_close(struct tty_struct *tty, struct file *filp)
  1426. {
  1427. struct uart_state *state = tty->driver_data;
  1428. if (!state) {
  1429. struct uart_driver *drv = tty->driver->driver_state;
  1430. struct tty_port *port;
  1431. state = drv->state + tty->index;
  1432. port = &state->port;
  1433. guard(spinlock_irq)(&port->lock);
  1434. --port->count;
  1435. return;
  1436. }
  1437. pr_debug("uart_close(%d) called\n", tty->index);
  1438. tty_port_close(tty->port, tty, filp);
  1439. }
  1440. static void uart_tty_port_shutdown(struct tty_port *port)
  1441. {
  1442. struct uart_state *state = container_of(port, struct uart_state, port);
  1443. struct uart_port *uport = uart_port_check(state);
  1444. /*
  1445. * At this point, we stop accepting input. To do this, we
  1446. * disable the receive line status interrupts.
  1447. */
  1448. if (WARN(!uport, "detached port still initialized!\n"))
  1449. return;
  1450. scoped_guard(uart_port_lock_irq, uport)
  1451. uport->ops->stop_rx(uport);
  1452. serial_base_port_shutdown(uport);
  1453. uart_port_shutdown(port);
  1454. /*
  1455. * It's possible for shutdown to be called after suspend if we get
  1456. * a DCD drop (hangup) at just the right time. Clear suspended bit so
  1457. * we don't try to resume a port that has been shutdown.
  1458. */
  1459. tty_port_set_suspended(port, false);
  1460. uart_free_xmit_buf(port);
  1461. uart_change_pm(state, UART_PM_STATE_OFF);
  1462. }
  1463. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1464. {
  1465. struct uart_state *state = tty->driver_data;
  1466. struct uart_port *port;
  1467. unsigned long char_time, expire, fifo_timeout;
  1468. port = uart_port_ref(state);
  1469. if (!port)
  1470. return;
  1471. if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
  1472. uart_port_deref(port);
  1473. return;
  1474. }
  1475. /*
  1476. * Set the check interval to be 1/5 of the estimated time to
  1477. * send a single character, and make it at least 1. The check
  1478. * interval should also be less than the timeout.
  1479. *
  1480. * Note: we have to use pretty tight timings here to satisfy
  1481. * the NIST-PCTS.
  1482. */
  1483. char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
  1484. if (timeout && timeout < char_time)
  1485. char_time = timeout;
  1486. if (!uart_cts_enabled(port)) {
  1487. /*
  1488. * If the transmitter hasn't cleared in twice the approximate
  1489. * amount of time to send the entire FIFO, it probably won't
  1490. * ever clear. This assumes the UART isn't doing flow
  1491. * control, which is currently the case. Hence, if it ever
  1492. * takes longer than FIFO timeout, this is probably due to a
  1493. * UART bug of some kind. So, we clamp the timeout parameter at
  1494. * 2 * FIFO timeout.
  1495. */
  1496. fifo_timeout = uart_fifo_timeout(port);
  1497. if (timeout == 0 || timeout > 2 * fifo_timeout)
  1498. timeout = 2 * fifo_timeout;
  1499. }
  1500. expire = jiffies + timeout;
  1501. pr_debug("uart_wait_until_sent(%u), jiffies=%lu, expire=%lu...\n",
  1502. port->line, jiffies, expire);
  1503. /*
  1504. * Check whether the transmitter is empty every 'char_time'.
  1505. * 'timeout' / 'expire' give us the maximum amount of time
  1506. * we wait.
  1507. */
  1508. while (!port->ops->tx_empty(port)) {
  1509. msleep_interruptible(jiffies_to_msecs(char_time));
  1510. if (signal_pending(current))
  1511. break;
  1512. if (timeout && time_after(jiffies, expire))
  1513. break;
  1514. }
  1515. uart_port_deref(port);
  1516. }
  1517. /*
  1518. * Calls to uart_hangup() are serialised by the tty_lock in
  1519. * drivers/tty/tty_io.c:do_tty_hangup()
  1520. * This runs from a workqueue and can sleep for a _short_ time only.
  1521. */
  1522. static void uart_hangup(struct tty_struct *tty)
  1523. {
  1524. struct uart_state *state = tty->driver_data;
  1525. struct tty_port *port = &state->port;
  1526. struct uart_port *uport;
  1527. pr_debug("uart_hangup(%d)\n", tty->index);
  1528. guard(mutex)(&port->mutex);
  1529. uport = uart_port_check(state);
  1530. WARN(!uport, "hangup of detached port!\n");
  1531. if (tty_port_active(port)) {
  1532. uart_flush_buffer(tty);
  1533. uart_shutdown(tty, state);
  1534. scoped_guard(spinlock_irqsave, &port->lock)
  1535. port->count = 0;
  1536. tty_port_set_active(port, false);
  1537. tty_port_tty_set(port, NULL);
  1538. if (uport && !uart_console(uport))
  1539. uart_change_pm(state, UART_PM_STATE_OFF);
  1540. wake_up_interruptible(&port->open_wait);
  1541. wake_up_interruptible(&port->delta_msr_wait);
  1542. }
  1543. }
  1544. /* uport == NULL if uart_port has already been removed */
  1545. static void uart_port_shutdown(struct tty_port *port)
  1546. {
  1547. struct uart_state *state = container_of(port, struct uart_state, port);
  1548. struct uart_port *uport = uart_port_check(state);
  1549. /*
  1550. * clear delta_msr_wait queue to avoid mem leaks: we may free
  1551. * the irq here so the queue might never be woken up. Note
  1552. * that we won't end up waiting on delta_msr_wait again since
  1553. * any outstanding file descriptors should be pointing at
  1554. * hung_up_tty_fops now.
  1555. */
  1556. wake_up_interruptible(&port->delta_msr_wait);
  1557. if (uport) {
  1558. /* Free the IRQ and disable the port. */
  1559. uport->ops->shutdown(uport);
  1560. /* Ensure that the IRQ handler isn't running on another CPU. */
  1561. synchronize_irq(uport->irq);
  1562. }
  1563. }
  1564. static bool uart_carrier_raised(struct tty_port *port)
  1565. {
  1566. struct uart_state *state = container_of(port, struct uart_state, port);
  1567. struct uart_port *uport;
  1568. unsigned long flags;
  1569. int mctrl;
  1570. uport = uart_port_ref_lock(state, &flags);
  1571. /*
  1572. * Should never observe uport == NULL since checks for hangup should
  1573. * abort the tty_port_block_til_ready() loop before checking for carrier
  1574. * raised -- but report carrier raised if it does anyway so open will
  1575. * continue and not sleep
  1576. */
  1577. if (WARN_ON(!uport))
  1578. return true;
  1579. uart_enable_ms(uport);
  1580. mctrl = uport->ops->get_mctrl(uport);
  1581. uart_port_unlock_deref(uport, flags);
  1582. return mctrl & TIOCM_CAR;
  1583. }
  1584. static void uart_dtr_rts(struct tty_port *port, bool active)
  1585. {
  1586. struct uart_state *state = container_of(port, struct uart_state, port);
  1587. struct uart_port *uport;
  1588. uport = uart_port_ref(state);
  1589. if (!uport)
  1590. return;
  1591. uart_port_dtr_rts(uport, active);
  1592. uart_port_deref(uport);
  1593. }
  1594. static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
  1595. {
  1596. struct uart_driver *drv = driver->driver_state;
  1597. struct uart_state *state = drv->state + tty->index;
  1598. tty->driver_data = state;
  1599. return tty_standard_install(driver, tty);
  1600. }
  1601. /*
  1602. * Calls to uart_open are serialised by the tty_lock in
  1603. * drivers/tty/tty_io.c:tty_open()
  1604. * Note that if this fails, then uart_close() _will_ be called.
  1605. *
  1606. * In time, we want to scrap the "opening nonpresent ports"
  1607. * behaviour and implement an alternative way for setserial
  1608. * to set base addresses/ports/types. This will allow us to
  1609. * get rid of a certain amount of extra tests.
  1610. */
  1611. static int uart_open(struct tty_struct *tty, struct file *filp)
  1612. {
  1613. struct uart_state *state = tty->driver_data;
  1614. int retval;
  1615. retval = tty_port_open(&state->port, tty, filp);
  1616. if (retval > 0)
  1617. retval = 0;
  1618. return retval;
  1619. }
  1620. static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
  1621. {
  1622. struct uart_state *state = container_of(port, struct uart_state, port);
  1623. struct uart_port *uport;
  1624. int ret;
  1625. uport = uart_port_check(state);
  1626. if (!uport || uport->flags & UPF_DEAD)
  1627. return -ENXIO;
  1628. /*
  1629. * Start up the serial port.
  1630. */
  1631. ret = uart_startup(tty, state, false);
  1632. if (ret > 0)
  1633. tty_port_set_active(port, true);
  1634. return ret;
  1635. }
  1636. static const char *uart_type(struct uart_port *port)
  1637. {
  1638. const char *str = NULL;
  1639. if (port->ops->type)
  1640. str = port->ops->type(port);
  1641. if (!str)
  1642. str = "unknown";
  1643. return str;
  1644. }
  1645. #ifdef CONFIG_PROC_FS
  1646. static void uart_line_info(struct seq_file *m, struct uart_state *state)
  1647. {
  1648. struct tty_port *port = &state->port;
  1649. enum uart_pm_state pm_state;
  1650. struct uart_port *uport;
  1651. char stat_buf[32];
  1652. unsigned int status;
  1653. int mmio;
  1654. guard(mutex)(&port->mutex);
  1655. uport = uart_port_check(state);
  1656. if (!uport)
  1657. return;
  1658. mmio = uport->iotype >= UPIO_MEM;
  1659. seq_printf(m, "%u: uart:%s %s%08llX irq:%u",
  1660. uport->line, uart_type(uport),
  1661. mmio ? "mmio:0x" : "port:",
  1662. mmio ? (unsigned long long)uport->mapbase
  1663. : (unsigned long long)uport->iobase,
  1664. uport->irq);
  1665. if (uport->type == PORT_UNKNOWN) {
  1666. seq_putc(m, '\n');
  1667. return;
  1668. }
  1669. if (capable(CAP_SYS_ADMIN)) {
  1670. pm_state = state->pm_state;
  1671. if (pm_state != UART_PM_STATE_ON)
  1672. uart_change_pm(state, UART_PM_STATE_ON);
  1673. scoped_guard(uart_port_lock_irq, uport)
  1674. status = uport->ops->get_mctrl(uport);
  1675. if (pm_state != UART_PM_STATE_ON)
  1676. uart_change_pm(state, pm_state);
  1677. seq_printf(m, " tx:%u rx:%u",
  1678. uport->icount.tx, uport->icount.rx);
  1679. if (uport->icount.frame)
  1680. seq_printf(m, " fe:%u", uport->icount.frame);
  1681. if (uport->icount.parity)
  1682. seq_printf(m, " pe:%u", uport->icount.parity);
  1683. if (uport->icount.brk)
  1684. seq_printf(m, " brk:%u", uport->icount.brk);
  1685. if (uport->icount.overrun)
  1686. seq_printf(m, " oe:%u", uport->icount.overrun);
  1687. if (uport->icount.buf_overrun)
  1688. seq_printf(m, " bo:%u", uport->icount.buf_overrun);
  1689. #define INFOBIT(bit, str) \
  1690. if (uport->mctrl & (bit)) \
  1691. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1692. strlen(stat_buf) - 2)
  1693. #define STATBIT(bit, str) \
  1694. if (status & (bit)) \
  1695. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1696. strlen(stat_buf) - 2)
  1697. stat_buf[0] = '\0';
  1698. stat_buf[1] = '\0';
  1699. INFOBIT(TIOCM_RTS, "|RTS");
  1700. STATBIT(TIOCM_CTS, "|CTS");
  1701. INFOBIT(TIOCM_DTR, "|DTR");
  1702. STATBIT(TIOCM_DSR, "|DSR");
  1703. STATBIT(TIOCM_CAR, "|CD");
  1704. STATBIT(TIOCM_RNG, "|RI");
  1705. if (stat_buf[0])
  1706. stat_buf[0] = ' ';
  1707. seq_puts(m, stat_buf);
  1708. }
  1709. seq_putc(m, '\n');
  1710. #undef STATBIT
  1711. #undef INFOBIT
  1712. }
  1713. static int uart_proc_show(struct seq_file *m, void *v)
  1714. {
  1715. struct tty_driver *ttydrv = m->private;
  1716. struct uart_driver *drv = ttydrv->driver_state;
  1717. int i;
  1718. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
  1719. for (i = 0; i < drv->nr; i++)
  1720. uart_line_info(m, drv->state + i);
  1721. return 0;
  1722. }
  1723. #endif
  1724. static void uart_port_spin_lock_init(struct uart_port *port)
  1725. {
  1726. spin_lock_init(&port->lock);
  1727. lockdep_set_class(&port->lock, &port_lock_key);
  1728. }
  1729. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  1730. /**
  1731. * uart_console_write - write a console message to a serial port
  1732. * @port: the port to write the message
  1733. * @s: array of characters
  1734. * @count: number of characters in string to write
  1735. * @putchar: function to write character to port
  1736. */
  1737. void uart_console_write(struct uart_port *port, const char *s,
  1738. unsigned int count,
  1739. void (*putchar)(struct uart_port *, unsigned char))
  1740. {
  1741. unsigned int i;
  1742. for (i = 0; i < count; i++, s++) {
  1743. if (*s == '\n')
  1744. putchar(port, '\r');
  1745. putchar(port, *s);
  1746. }
  1747. }
  1748. EXPORT_SYMBOL_GPL(uart_console_write);
  1749. /**
  1750. * uart_parse_earlycon - Parse earlycon options
  1751. * @p: ptr to 2nd field (ie., just beyond '<name>,')
  1752. * @iotype: ptr for decoded iotype (out)
  1753. * @addr: ptr for decoded mapbase/iobase (out)
  1754. * @options: ptr for <options> field; %NULL if not present (out)
  1755. *
  1756. * Decodes earlycon kernel command line parameters of the form:
  1757. * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1758. * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1759. *
  1760. * The optional form:
  1761. * * earlycon=<name>,0x<addr>,<options>
  1762. * * console=<name>,0x<addr>,<options>
  1763. *
  1764. * is also accepted; the returned @iotype will be %UPIO_MEM.
  1765. *
  1766. * Returns: 0 on success or -%EINVAL on failure
  1767. */
  1768. int uart_parse_earlycon(char *p, enum uart_iotype *iotype,
  1769. resource_size_t *addr, char **options)
  1770. {
  1771. if (strncmp(p, "mmio,", 5) == 0) {
  1772. *iotype = UPIO_MEM;
  1773. p += 5;
  1774. } else if (strncmp(p, "mmio16,", 7) == 0) {
  1775. *iotype = UPIO_MEM16;
  1776. p += 7;
  1777. } else if (strncmp(p, "mmio32,", 7) == 0) {
  1778. *iotype = UPIO_MEM32;
  1779. p += 7;
  1780. } else if (strncmp(p, "mmio32be,", 9) == 0) {
  1781. *iotype = UPIO_MEM32BE;
  1782. p += 9;
  1783. } else if (strncmp(p, "mmio32native,", 13) == 0) {
  1784. *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
  1785. UPIO_MEM32BE : UPIO_MEM32;
  1786. p += 13;
  1787. } else if (strncmp(p, "io,", 3) == 0) {
  1788. *iotype = UPIO_PORT;
  1789. p += 3;
  1790. } else if (strncmp(p, "0x", 2) == 0) {
  1791. *iotype = UPIO_MEM;
  1792. } else {
  1793. return -EINVAL;
  1794. }
  1795. /*
  1796. * Before you replace it with kstrtoull(), think about options separator
  1797. * (',') it will not tolerate
  1798. */
  1799. *addr = simple_strtoull(p, NULL, 0);
  1800. p = strchr(p, ',');
  1801. if (p)
  1802. p++;
  1803. *options = p;
  1804. return 0;
  1805. }
  1806. EXPORT_SYMBOL_GPL(uart_parse_earlycon);
  1807. /**
  1808. * uart_parse_options - Parse serial port baud/parity/bits/flow control.
  1809. * @options: pointer to option string
  1810. * @baud: pointer to an 'int' variable for the baud rate.
  1811. * @parity: pointer to an 'int' variable for the parity.
  1812. * @bits: pointer to an 'int' variable for the number of data bits.
  1813. * @flow: pointer to an 'int' variable for the flow control character.
  1814. *
  1815. * uart_parse_options() decodes a string containing the serial console
  1816. * options. The format of the string is <baud><parity><bits><flow>,
  1817. * eg: 115200n8r
  1818. */
  1819. void
  1820. uart_parse_options(const char *options, int *baud, int *parity,
  1821. int *bits, int *flow)
  1822. {
  1823. const char *s = options;
  1824. *baud = simple_strtoul(s, NULL, 10);
  1825. while (*s >= '0' && *s <= '9')
  1826. s++;
  1827. if (*s)
  1828. *parity = *s++;
  1829. if (*s)
  1830. *bits = *s++ - '0';
  1831. if (*s)
  1832. *flow = *s;
  1833. }
  1834. EXPORT_SYMBOL_GPL(uart_parse_options);
  1835. /**
  1836. * uart_set_options - setup the serial console parameters
  1837. * @port: pointer to the serial ports uart_port structure
  1838. * @co: console pointer
  1839. * @baud: baud rate
  1840. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1841. * @bits: number of data bits
  1842. * @flow: flow control character - 'r' (rts)
  1843. *
  1844. * Locking: Caller must hold console_list_lock in order to serialize
  1845. * early initialization of the serial-console lock.
  1846. */
  1847. int
  1848. uart_set_options(struct uart_port *port, struct console *co,
  1849. int baud, int parity, int bits, int flow)
  1850. {
  1851. struct ktermios termios;
  1852. static struct ktermios dummy;
  1853. /*
  1854. * Ensure that the serial-console lock is initialised early.
  1855. *
  1856. * Note that the console-registered check is needed because
  1857. * kgdboc can call uart_set_options() for an already registered
  1858. * console via tty_find_polling_driver() and uart_poll_init().
  1859. */
  1860. if (!uart_console_registered_locked(port) && !port->console_reinit)
  1861. uart_port_spin_lock_init(port);
  1862. memset(&termios, 0, sizeof(struct ktermios));
  1863. termios.c_cflag |= CREAD | HUPCL | CLOCAL;
  1864. tty_termios_encode_baud_rate(&termios, baud, baud);
  1865. if (bits == 7)
  1866. termios.c_cflag |= CS7;
  1867. else
  1868. termios.c_cflag |= CS8;
  1869. switch (parity) {
  1870. case 'o': case 'O':
  1871. termios.c_cflag |= PARODD;
  1872. fallthrough;
  1873. case 'e': case 'E':
  1874. termios.c_cflag |= PARENB;
  1875. break;
  1876. }
  1877. if (flow == 'r')
  1878. termios.c_cflag |= CRTSCTS;
  1879. /*
  1880. * some uarts on other side don't support no flow control.
  1881. * So we set * DTR in host uart to make them happy
  1882. */
  1883. port->mctrl |= TIOCM_DTR;
  1884. port->ops->set_termios(port, &termios, &dummy);
  1885. /*
  1886. * Allow the setting of the UART parameters with a NULL console
  1887. * too:
  1888. */
  1889. if (co) {
  1890. co->cflag = termios.c_cflag;
  1891. co->ispeed = termios.c_ispeed;
  1892. co->ospeed = termios.c_ospeed;
  1893. }
  1894. return 0;
  1895. }
  1896. EXPORT_SYMBOL_GPL(uart_set_options);
  1897. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1898. /**
  1899. * uart_change_pm - set power state of the port
  1900. *
  1901. * @state: port descriptor
  1902. * @pm_state: new state
  1903. *
  1904. * Locking: port->mutex has to be held
  1905. */
  1906. static void uart_change_pm(struct uart_state *state,
  1907. enum uart_pm_state pm_state)
  1908. {
  1909. struct uart_port *port = uart_port_check(state);
  1910. if (state->pm_state != pm_state) {
  1911. if (port && port->ops->pm)
  1912. port->ops->pm(port, pm_state, state->pm_state);
  1913. state->pm_state = pm_state;
  1914. }
  1915. }
  1916. struct uart_match {
  1917. struct uart_port *port;
  1918. struct uart_driver *driver;
  1919. };
  1920. static int serial_match_port(struct device *dev, const void *data)
  1921. {
  1922. const struct uart_match *match = data;
  1923. struct tty_driver *tty_drv = match->driver->tty_driver;
  1924. dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
  1925. match->port->line;
  1926. return dev->devt == devt; /* Actually, only one tty per port */
  1927. }
  1928. int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
  1929. {
  1930. struct uart_state *state = drv->state + uport->line;
  1931. struct tty_port *port = &state->port;
  1932. struct device *tty_dev;
  1933. struct uart_match match = {uport, drv};
  1934. guard(mutex)(&port->mutex);
  1935. tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
  1936. if (tty_dev && device_may_wakeup(tty_dev)) {
  1937. enable_irq_wake(uport->irq);
  1938. put_device(tty_dev);
  1939. return 0;
  1940. }
  1941. put_device(tty_dev);
  1942. /*
  1943. * Nothing to do if the console is not suspending
  1944. * except stop_rx to prevent any asynchronous data
  1945. * over RX line. However ensure that we will be
  1946. * able to Re-start_rx later.
  1947. */
  1948. if (!console_suspend_enabled && uart_console(uport)) {
  1949. if (uport->ops->start_rx) {
  1950. guard(uart_port_lock_irq)(uport);
  1951. uport->ops->stop_rx(uport);
  1952. }
  1953. device_set_awake_path(uport->dev);
  1954. return 0;
  1955. }
  1956. uport->suspended = 1;
  1957. if (tty_port_initialized(port)) {
  1958. const struct uart_ops *ops = uport->ops;
  1959. int tries;
  1960. unsigned int mctrl;
  1961. tty_port_set_suspended(port, true);
  1962. tty_port_set_initialized(port, false);
  1963. scoped_guard(uart_port_lock_irq, uport) {
  1964. ops->stop_tx(uport);
  1965. if (!(uport->rs485.flags & SER_RS485_ENABLED))
  1966. ops->set_mctrl(uport, 0);
  1967. /* save mctrl so it can be restored on resume */
  1968. mctrl = uport->mctrl;
  1969. uport->mctrl = 0;
  1970. ops->stop_rx(uport);
  1971. }
  1972. /*
  1973. * Wait for the transmitter to empty.
  1974. */
  1975. for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
  1976. msleep(10);
  1977. if (!tries)
  1978. dev_err(uport->dev, "%s: Unable to drain transmitter\n",
  1979. uport->name);
  1980. ops->shutdown(uport);
  1981. uport->mctrl = mctrl;
  1982. }
  1983. /*
  1984. * Suspend the console device before suspending the port.
  1985. */
  1986. if (uart_console(uport))
  1987. console_suspend(uport->cons);
  1988. uart_change_pm(state, UART_PM_STATE_OFF);
  1989. return 0;
  1990. }
  1991. EXPORT_SYMBOL(uart_suspend_port);
  1992. int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
  1993. {
  1994. struct uart_state *state = drv->state + uport->line;
  1995. struct tty_port *port = &state->port;
  1996. struct device *tty_dev;
  1997. struct uart_match match = {uport, drv};
  1998. struct ktermios termios;
  1999. guard(mutex)(&port->mutex);
  2000. tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
  2001. if (!uport->suspended && device_may_wakeup(tty_dev)) {
  2002. if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
  2003. disable_irq_wake(uport->irq);
  2004. put_device(tty_dev);
  2005. return 0;
  2006. }
  2007. put_device(tty_dev);
  2008. uport->suspended = 0;
  2009. /*
  2010. * Re-enable the console device after suspending.
  2011. */
  2012. if (uart_console(uport)) {
  2013. /*
  2014. * First try to use the console cflag setting.
  2015. */
  2016. memset(&termios, 0, sizeof(struct ktermios));
  2017. termios.c_cflag = uport->cons->cflag;
  2018. termios.c_ispeed = uport->cons->ispeed;
  2019. termios.c_ospeed = uport->cons->ospeed;
  2020. /*
  2021. * If that's unset, use the tty termios setting.
  2022. */
  2023. if (port->tty && termios.c_cflag == 0)
  2024. termios = port->tty->termios;
  2025. if (console_suspend_enabled)
  2026. uart_change_pm(state, UART_PM_STATE_ON);
  2027. uport->ops->set_termios(uport, &termios, NULL);
  2028. if (!console_suspend_enabled && uport->ops->start_rx) {
  2029. guard(uart_port_lock_irq)(uport);
  2030. uport->ops->start_rx(uport);
  2031. }
  2032. if (console_suspend_enabled)
  2033. console_resume(uport->cons);
  2034. }
  2035. if (tty_port_suspended(port)) {
  2036. const struct uart_ops *ops = uport->ops;
  2037. int ret;
  2038. uart_change_pm(state, UART_PM_STATE_ON);
  2039. scoped_guard(uart_port_lock_irq, uport)
  2040. if (!(uport->rs485.flags & SER_RS485_ENABLED))
  2041. ops->set_mctrl(uport, 0);
  2042. if (console_suspend_enabled || !uart_console(uport)) {
  2043. /* Protected by port mutex for now */
  2044. struct tty_struct *tty = port->tty;
  2045. ret = ops->startup(uport);
  2046. if (ret == 0) {
  2047. if (tty)
  2048. uart_change_line_settings(tty, state, NULL);
  2049. uart_rs485_config(uport);
  2050. scoped_guard(uart_port_lock_irq, uport) {
  2051. if (!(uport->rs485.flags & SER_RS485_ENABLED))
  2052. ops->set_mctrl(uport, uport->mctrl);
  2053. ops->start_tx(uport);
  2054. }
  2055. tty_port_set_initialized(port, true);
  2056. } else {
  2057. /*
  2058. * Failed to resume - maybe hardware went away?
  2059. * Clear the "initialized" flag so we won't try
  2060. * to call the low level drivers shutdown method.
  2061. */
  2062. uart_shutdown(tty, state);
  2063. }
  2064. }
  2065. tty_port_set_suspended(port, false);
  2066. }
  2067. return 0;
  2068. }
  2069. EXPORT_SYMBOL(uart_resume_port);
  2070. static inline void
  2071. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  2072. {
  2073. char address[64];
  2074. switch (port->iotype) {
  2075. case UPIO_PORT:
  2076. snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
  2077. break;
  2078. case UPIO_HUB6:
  2079. snprintf(address, sizeof(address),
  2080. "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
  2081. break;
  2082. case UPIO_MEM:
  2083. case UPIO_MEM16:
  2084. case UPIO_MEM32:
  2085. case UPIO_MEM32BE:
  2086. case UPIO_AU:
  2087. case UPIO_TSI:
  2088. snprintf(address, sizeof(address),
  2089. "MMIO 0x%llx", (unsigned long long)port->mapbase);
  2090. break;
  2091. default:
  2092. strscpy(address, "*unknown*", sizeof(address));
  2093. break;
  2094. }
  2095. pr_info("%s%s%s at %s (irq = %u, base_baud = %u) is a %s\n",
  2096. port->dev ? dev_name(port->dev) : "",
  2097. port->dev ? ": " : "",
  2098. port->name,
  2099. address, port->irq, port->uartclk / 16, uart_type(port));
  2100. /* The magic multiplier feature is a bit obscure, so report it too. */
  2101. if (port->flags & UPF_MAGIC_MULTIPLIER)
  2102. pr_info("%s%s%s extra baud rates supported: %u, %u",
  2103. port->dev ? dev_name(port->dev) : "",
  2104. port->dev ? ": " : "",
  2105. port->name,
  2106. port->uartclk / 8, port->uartclk / 4);
  2107. }
  2108. static void
  2109. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  2110. struct uart_port *port)
  2111. {
  2112. unsigned int flags;
  2113. /*
  2114. * If there isn't a port here, don't do anything further.
  2115. */
  2116. if (!port->iobase && !port->mapbase && !port->membase)
  2117. return;
  2118. /*
  2119. * Now do the auto configuration stuff. Note that config_port
  2120. * is expected to claim the resources and map the port for us.
  2121. */
  2122. flags = 0;
  2123. if (port->flags & UPF_AUTO_IRQ)
  2124. flags |= UART_CONFIG_IRQ;
  2125. if (port->flags & UPF_BOOT_AUTOCONF) {
  2126. if (!(port->flags & UPF_FIXED_TYPE)) {
  2127. port->type = PORT_UNKNOWN;
  2128. flags |= UART_CONFIG_TYPE;
  2129. }
  2130. /* Synchronize with possible boot console. */
  2131. if (uart_console(port))
  2132. console_lock();
  2133. port->ops->config_port(port, flags);
  2134. if (uart_console(port))
  2135. console_unlock();
  2136. }
  2137. if (port->type != PORT_UNKNOWN) {
  2138. uart_report_port(drv, port);
  2139. /* Synchronize with possible boot console. */
  2140. if (uart_console(port))
  2141. console_lock();
  2142. /* Power up port for set_mctrl() */
  2143. uart_change_pm(state, UART_PM_STATE_ON);
  2144. /*
  2145. * Ensure that the modem control lines are de-activated.
  2146. * keep the DTR setting that is set in uart_set_options()
  2147. * We probably don't need a spinlock around this, but
  2148. */
  2149. scoped_guard(uart_port_lock_irqsave, port) {
  2150. port->mctrl &= TIOCM_DTR;
  2151. if (!(port->rs485.flags & SER_RS485_ENABLED))
  2152. port->ops->set_mctrl(port, port->mctrl);
  2153. }
  2154. uart_rs485_config(port);
  2155. if (uart_console(port))
  2156. console_unlock();
  2157. /*
  2158. * If this driver supports console, and it hasn't been
  2159. * successfully registered yet, try to re-register it.
  2160. * It may be that the port was not available.
  2161. */
  2162. if (port->cons && !console_is_registered(port->cons))
  2163. register_console(port->cons);
  2164. /*
  2165. * Power down all ports by default, except the
  2166. * console if we have one.
  2167. */
  2168. if (!uart_console(port))
  2169. uart_change_pm(state, UART_PM_STATE_OFF);
  2170. }
  2171. }
  2172. #ifdef CONFIG_CONSOLE_POLL
  2173. static int uart_poll_init(struct tty_driver *driver, int line, char *options)
  2174. {
  2175. struct uart_driver *drv = driver->driver_state;
  2176. struct uart_state *state = drv->state + line;
  2177. enum uart_pm_state pm_state;
  2178. struct tty_port *tport;
  2179. struct uart_port *port;
  2180. int baud = 9600;
  2181. int bits = 8;
  2182. int parity = 'n';
  2183. int flow = 'n';
  2184. int ret = 0;
  2185. tport = &state->port;
  2186. guard(mutex)(&tport->mutex);
  2187. port = uart_port_check(state);
  2188. if (!port || port->type == PORT_UNKNOWN ||
  2189. !(port->ops->poll_get_char && port->ops->poll_put_char))
  2190. return -1;
  2191. pm_state = state->pm_state;
  2192. uart_change_pm(state, UART_PM_STATE_ON);
  2193. if (port->ops->poll_init) {
  2194. /*
  2195. * We don't set initialized as we only initialized the hw,
  2196. * e.g. state->xmit is still uninitialized.
  2197. */
  2198. if (!tty_port_initialized(tport))
  2199. ret = port->ops->poll_init(port);
  2200. }
  2201. if (!ret && options) {
  2202. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2203. console_list_lock();
  2204. ret = uart_set_options(port, NULL, baud, parity, bits, flow);
  2205. console_list_unlock();
  2206. }
  2207. if (ret)
  2208. uart_change_pm(state, pm_state);
  2209. return ret;
  2210. }
  2211. static int uart_poll_get_char(struct tty_driver *driver, int line)
  2212. {
  2213. struct uart_driver *drv = driver->driver_state;
  2214. struct uart_state *state = drv->state + line;
  2215. struct uart_port *port;
  2216. int ret = -1;
  2217. port = uart_port_ref(state);
  2218. if (port) {
  2219. ret = port->ops->poll_get_char(port);
  2220. uart_port_deref(port);
  2221. }
  2222. return ret;
  2223. }
  2224. static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
  2225. {
  2226. struct uart_driver *drv = driver->driver_state;
  2227. struct uart_state *state = drv->state + line;
  2228. struct uart_port *port;
  2229. port = uart_port_ref(state);
  2230. if (!port)
  2231. return;
  2232. if (ch == '\n')
  2233. port->ops->poll_put_char(port, '\r');
  2234. port->ops->poll_put_char(port, ch);
  2235. uart_port_deref(port);
  2236. }
  2237. #endif
  2238. static const struct tty_operations uart_ops = {
  2239. .install = uart_install,
  2240. .open = uart_open,
  2241. .close = uart_close,
  2242. .write = uart_write,
  2243. .put_char = uart_put_char,
  2244. .flush_chars = uart_flush_chars,
  2245. .write_room = uart_write_room,
  2246. .chars_in_buffer= uart_chars_in_buffer,
  2247. .flush_buffer = uart_flush_buffer,
  2248. .ioctl = uart_ioctl,
  2249. .throttle = uart_throttle,
  2250. .unthrottle = uart_unthrottle,
  2251. .send_xchar = uart_send_xchar,
  2252. .set_termios = uart_set_termios,
  2253. .set_ldisc = uart_set_ldisc,
  2254. .stop = uart_stop,
  2255. .start = uart_start,
  2256. .hangup = uart_hangup,
  2257. .break_ctl = uart_break_ctl,
  2258. .wait_until_sent= uart_wait_until_sent,
  2259. #ifdef CONFIG_PROC_FS
  2260. .proc_show = uart_proc_show,
  2261. #endif
  2262. .tiocmget = uart_tiocmget,
  2263. .tiocmset = uart_tiocmset,
  2264. .set_serial = uart_set_info_user,
  2265. .get_serial = uart_get_info_user,
  2266. .get_icount = uart_get_icount,
  2267. #ifdef CONFIG_CONSOLE_POLL
  2268. .poll_init = uart_poll_init,
  2269. .poll_get_char = uart_poll_get_char,
  2270. .poll_put_char = uart_poll_put_char,
  2271. #endif
  2272. };
  2273. static const struct tty_port_operations uart_port_ops = {
  2274. .carrier_raised = uart_carrier_raised,
  2275. .dtr_rts = uart_dtr_rts,
  2276. .activate = uart_port_activate,
  2277. .shutdown = uart_tty_port_shutdown,
  2278. };
  2279. /**
  2280. * uart_register_driver - register a driver with the uart core layer
  2281. * @drv: low level driver structure
  2282. *
  2283. * Register a uart driver with the core driver. We in turn register with the
  2284. * tty layer, and initialise the core driver per-port state.
  2285. *
  2286. * We have a proc file in /proc/tty/driver which is named after the normal
  2287. * driver.
  2288. *
  2289. * @drv->port should be %NULL, and the per-port structures should be registered
  2290. * using uart_add_one_port() after this call has succeeded.
  2291. *
  2292. * Locking: none, Interrupts: enabled
  2293. */
  2294. int uart_register_driver(struct uart_driver *drv)
  2295. {
  2296. struct tty_driver *normal;
  2297. int i, retval = -ENOMEM;
  2298. BUG_ON(drv->state);
  2299. /*
  2300. * Maybe we should be using a slab cache for this, especially if
  2301. * we have a large number of ports to handle.
  2302. */
  2303. drv->state = kzalloc_objs(struct uart_state, drv->nr);
  2304. if (!drv->state)
  2305. goto out;
  2306. normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
  2307. TTY_DRIVER_DYNAMIC_DEV);
  2308. if (IS_ERR(normal)) {
  2309. retval = PTR_ERR(normal);
  2310. goto out_kfree;
  2311. }
  2312. drv->tty_driver = normal;
  2313. normal->driver_name = drv->driver_name;
  2314. normal->name = drv->dev_name;
  2315. normal->major = drv->major;
  2316. normal->minor_start = drv->minor;
  2317. normal->type = TTY_DRIVER_TYPE_SERIAL;
  2318. normal->subtype = SERIAL_TYPE_NORMAL;
  2319. normal->init_termios = tty_std_termios;
  2320. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2321. normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
  2322. normal->driver_state = drv;
  2323. tty_set_operations(normal, &uart_ops);
  2324. /*
  2325. * Initialise the UART state(s).
  2326. */
  2327. for (i = 0; i < drv->nr; i++) {
  2328. struct uart_state *state = drv->state + i;
  2329. struct tty_port *port = &state->port;
  2330. tty_port_init(port);
  2331. port->ops = &uart_port_ops;
  2332. }
  2333. retval = tty_register_driver(normal);
  2334. if (retval >= 0)
  2335. return retval;
  2336. for (i = 0; i < drv->nr; i++)
  2337. tty_port_destroy(&drv->state[i].port);
  2338. tty_driver_kref_put(normal);
  2339. out_kfree:
  2340. kfree(drv->state);
  2341. out:
  2342. return retval;
  2343. }
  2344. EXPORT_SYMBOL(uart_register_driver);
  2345. /**
  2346. * uart_unregister_driver - remove a driver from the uart core layer
  2347. * @drv: low level driver structure
  2348. *
  2349. * Remove all references to a driver from the core driver. The low level
  2350. * driver must have removed all its ports via the uart_remove_one_port() if it
  2351. * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
  2352. *
  2353. * Locking: none, Interrupts: enabled
  2354. */
  2355. void uart_unregister_driver(struct uart_driver *drv)
  2356. {
  2357. struct tty_driver *p = drv->tty_driver;
  2358. unsigned int i;
  2359. tty_unregister_driver(p);
  2360. tty_driver_kref_put(p);
  2361. for (i = 0; i < drv->nr; i++)
  2362. tty_port_destroy(&drv->state[i].port);
  2363. kfree(drv->state);
  2364. drv->state = NULL;
  2365. drv->tty_driver = NULL;
  2366. }
  2367. EXPORT_SYMBOL(uart_unregister_driver);
  2368. struct tty_driver *uart_console_device(struct console *co, int *index)
  2369. {
  2370. struct uart_driver *p = co->data;
  2371. *index = co->index;
  2372. return p->tty_driver;
  2373. }
  2374. EXPORT_SYMBOL_GPL(uart_console_device);
  2375. static ssize_t uartclk_show(struct device *dev,
  2376. struct device_attribute *attr, char *buf)
  2377. {
  2378. struct serial_struct tmp;
  2379. struct tty_port *port = dev_get_drvdata(dev);
  2380. uart_get_info(port, &tmp);
  2381. return sprintf(buf, "%d\n", tmp.baud_base * 16);
  2382. }
  2383. static ssize_t type_show(struct device *dev,
  2384. struct device_attribute *attr, char *buf)
  2385. {
  2386. struct serial_struct tmp;
  2387. struct tty_port *port = dev_get_drvdata(dev);
  2388. uart_get_info(port, &tmp);
  2389. return sprintf(buf, "%d\n", tmp.type);
  2390. }
  2391. static ssize_t line_show(struct device *dev,
  2392. struct device_attribute *attr, char *buf)
  2393. {
  2394. struct serial_struct tmp;
  2395. struct tty_port *port = dev_get_drvdata(dev);
  2396. uart_get_info(port, &tmp);
  2397. return sprintf(buf, "%d\n", tmp.line);
  2398. }
  2399. static ssize_t port_show(struct device *dev,
  2400. struct device_attribute *attr, char *buf)
  2401. {
  2402. struct serial_struct tmp;
  2403. struct tty_port *port = dev_get_drvdata(dev);
  2404. unsigned long ioaddr;
  2405. uart_get_info(port, &tmp);
  2406. ioaddr = tmp.port;
  2407. if (HIGH_BITS_OFFSET)
  2408. ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
  2409. return sprintf(buf, "0x%lX\n", ioaddr);
  2410. }
  2411. static ssize_t irq_show(struct device *dev,
  2412. struct device_attribute *attr, char *buf)
  2413. {
  2414. struct serial_struct tmp;
  2415. struct tty_port *port = dev_get_drvdata(dev);
  2416. uart_get_info(port, &tmp);
  2417. return sprintf(buf, "%d\n", tmp.irq);
  2418. }
  2419. static ssize_t flags_show(struct device *dev,
  2420. struct device_attribute *attr, char *buf)
  2421. {
  2422. struct serial_struct tmp;
  2423. struct tty_port *port = dev_get_drvdata(dev);
  2424. uart_get_info(port, &tmp);
  2425. return sprintf(buf, "0x%X\n", tmp.flags);
  2426. }
  2427. static ssize_t xmit_fifo_size_show(struct device *dev,
  2428. struct device_attribute *attr, char *buf)
  2429. {
  2430. struct serial_struct tmp;
  2431. struct tty_port *port = dev_get_drvdata(dev);
  2432. uart_get_info(port, &tmp);
  2433. return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
  2434. }
  2435. static ssize_t close_delay_show(struct device *dev,
  2436. struct device_attribute *attr, char *buf)
  2437. {
  2438. struct serial_struct tmp;
  2439. struct tty_port *port = dev_get_drvdata(dev);
  2440. uart_get_info(port, &tmp);
  2441. return sprintf(buf, "%u\n", tmp.close_delay);
  2442. }
  2443. static ssize_t closing_wait_show(struct device *dev,
  2444. struct device_attribute *attr, char *buf)
  2445. {
  2446. struct serial_struct tmp;
  2447. struct tty_port *port = dev_get_drvdata(dev);
  2448. uart_get_info(port, &tmp);
  2449. return sprintf(buf, "%u\n", tmp.closing_wait);
  2450. }
  2451. static ssize_t custom_divisor_show(struct device *dev,
  2452. struct device_attribute *attr, char *buf)
  2453. {
  2454. struct serial_struct tmp;
  2455. struct tty_port *port = dev_get_drvdata(dev);
  2456. uart_get_info(port, &tmp);
  2457. return sprintf(buf, "%d\n", tmp.custom_divisor);
  2458. }
  2459. static ssize_t io_type_show(struct device *dev,
  2460. struct device_attribute *attr, char *buf)
  2461. {
  2462. struct serial_struct tmp;
  2463. struct tty_port *port = dev_get_drvdata(dev);
  2464. uart_get_info(port, &tmp);
  2465. return sprintf(buf, "%u\n", tmp.io_type);
  2466. }
  2467. static ssize_t iomem_base_show(struct device *dev,
  2468. struct device_attribute *attr, char *buf)
  2469. {
  2470. struct serial_struct tmp;
  2471. struct tty_port *port = dev_get_drvdata(dev);
  2472. uart_get_info(port, &tmp);
  2473. return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
  2474. }
  2475. static ssize_t iomem_reg_shift_show(struct device *dev,
  2476. struct device_attribute *attr, char *buf)
  2477. {
  2478. struct serial_struct tmp;
  2479. struct tty_port *port = dev_get_drvdata(dev);
  2480. uart_get_info(port, &tmp);
  2481. return sprintf(buf, "%u\n", tmp.iomem_reg_shift);
  2482. }
  2483. static ssize_t console_show(struct device *dev,
  2484. struct device_attribute *attr, char *buf)
  2485. {
  2486. struct tty_port *port = dev_get_drvdata(dev);
  2487. struct uart_state *state = container_of(port, struct uart_state, port);
  2488. struct uart_port *uport;
  2489. bool console = false;
  2490. scoped_guard(mutex, &port->mutex) {
  2491. uport = uart_port_check(state);
  2492. if (uport)
  2493. console = uart_console_registered(uport);
  2494. }
  2495. return sprintf(buf, "%c\n", console ? 'Y' : 'N');
  2496. }
  2497. static ssize_t console_store(struct device *dev,
  2498. struct device_attribute *attr, const char *buf, size_t count)
  2499. {
  2500. struct tty_port *port = dev_get_drvdata(dev);
  2501. struct uart_state *state = container_of(port, struct uart_state, port);
  2502. struct uart_port *uport;
  2503. bool oldconsole, newconsole;
  2504. int ret;
  2505. ret = kstrtobool(buf, &newconsole);
  2506. if (ret)
  2507. return ret;
  2508. guard(mutex)(&port->mutex);
  2509. uport = uart_port_check(state);
  2510. if (!uport)
  2511. return -ENXIO;
  2512. oldconsole = uart_console_registered(uport);
  2513. if (oldconsole && !newconsole) {
  2514. ret = unregister_console(uport->cons);
  2515. if (ret < 0)
  2516. return ret;
  2517. } else if (!oldconsole && newconsole) {
  2518. if (!uart_console(uport))
  2519. return -ENOENT;
  2520. uport->console_reinit = 1;
  2521. register_console(uport->cons);
  2522. }
  2523. return count;
  2524. }
  2525. static DEVICE_ATTR_RO(uartclk);
  2526. static DEVICE_ATTR_RO(type);
  2527. static DEVICE_ATTR_RO(line);
  2528. static DEVICE_ATTR_RO(port);
  2529. static DEVICE_ATTR_RO(irq);
  2530. static DEVICE_ATTR_RO(flags);
  2531. static DEVICE_ATTR_RO(xmit_fifo_size);
  2532. static DEVICE_ATTR_RO(close_delay);
  2533. static DEVICE_ATTR_RO(closing_wait);
  2534. static DEVICE_ATTR_RO(custom_divisor);
  2535. static DEVICE_ATTR_RO(io_type);
  2536. static DEVICE_ATTR_RO(iomem_base);
  2537. static DEVICE_ATTR_RO(iomem_reg_shift);
  2538. static DEVICE_ATTR_RW(console);
  2539. static struct attribute *tty_dev_attrs[] = {
  2540. &dev_attr_uartclk.attr,
  2541. &dev_attr_type.attr,
  2542. &dev_attr_line.attr,
  2543. &dev_attr_port.attr,
  2544. &dev_attr_irq.attr,
  2545. &dev_attr_flags.attr,
  2546. &dev_attr_xmit_fifo_size.attr,
  2547. &dev_attr_close_delay.attr,
  2548. &dev_attr_closing_wait.attr,
  2549. &dev_attr_custom_divisor.attr,
  2550. &dev_attr_io_type.attr,
  2551. &dev_attr_iomem_base.attr,
  2552. &dev_attr_iomem_reg_shift.attr,
  2553. &dev_attr_console.attr,
  2554. NULL
  2555. };
  2556. static const struct attribute_group tty_dev_attr_group = {
  2557. .attrs = tty_dev_attrs,
  2558. };
  2559. /**
  2560. * serial_core_add_one_port - attach a driver-defined port structure
  2561. * @drv: pointer to the uart low level driver structure for this port
  2562. * @uport: uart port structure to use for this port.
  2563. *
  2564. * Context: task context, might sleep
  2565. *
  2566. * This allows the driver @drv to register its own uart_port structure with the
  2567. * core driver. The main purpose is to allow the low level uart drivers to
  2568. * expand uart_port, rather than having yet more levels of structures.
  2569. * Caller must hold port_mutex.
  2570. */
  2571. static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport)
  2572. {
  2573. struct uart_state *state;
  2574. struct tty_port *port;
  2575. struct device *tty_dev;
  2576. int num_groups;
  2577. if (uport->line >= drv->nr)
  2578. return -EINVAL;
  2579. state = drv->state + uport->line;
  2580. port = &state->port;
  2581. guard(mutex)(&port->mutex);
  2582. if (state->uart_port)
  2583. return -EINVAL;
  2584. /* Link the port to the driver state table and vice versa */
  2585. atomic_set(&state->refcount, 1);
  2586. init_waitqueue_head(&state->remove_wait);
  2587. state->uart_port = uport;
  2588. uport->state = state;
  2589. /*
  2590. * If this port is in use as a console then the spinlock is already
  2591. * initialised.
  2592. */
  2593. if (!uart_console_registered(uport))
  2594. uart_port_spin_lock_init(uport);
  2595. state->pm_state = UART_PM_STATE_UNDEFINED;
  2596. uart_port_set_cons(uport, drv->cons);
  2597. uport->minor = drv->tty_driver->minor_start + uport->line;
  2598. uport->name = kasprintf(GFP_KERNEL, "%s%u", drv->dev_name,
  2599. drv->tty_driver->name_base + uport->line);
  2600. if (!uport->name)
  2601. return -ENOMEM;
  2602. if (uport->cons && uport->dev)
  2603. of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
  2604. /*
  2605. * TTY port has to be linked with the driver before register_console()
  2606. * in uart_configure_port(), because user-space could open the console
  2607. * immediately after.
  2608. */
  2609. tty_port_link_device(port, drv->tty_driver, uport->line);
  2610. uart_configure_port(drv, state, uport);
  2611. port->console = uart_console(uport);
  2612. num_groups = 2;
  2613. if (uport->attr_group)
  2614. num_groups++;
  2615. uport->tty_groups = kzalloc_objs(*uport->tty_groups, num_groups);
  2616. if (!uport->tty_groups)
  2617. return -ENOMEM;
  2618. uport->tty_groups[0] = &tty_dev_attr_group;
  2619. if (uport->attr_group)
  2620. uport->tty_groups[1] = uport->attr_group;
  2621. /* Ensure serdev drivers can call serdev_device_open() right away */
  2622. uport->flags &= ~UPF_DEAD;
  2623. /*
  2624. * Register the port whether it's detected or not. This allows
  2625. * setserial to be used to alter this port's parameters.
  2626. */
  2627. tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
  2628. uport->line, uport->dev, &uport->port_dev->dev, port,
  2629. uport->tty_groups);
  2630. if (!IS_ERR(tty_dev)) {
  2631. device_set_wakeup_capable(tty_dev, 1);
  2632. } else {
  2633. uport->flags |= UPF_DEAD;
  2634. dev_err(uport->dev, "Cannot register tty device on line %u\n",
  2635. uport->line);
  2636. }
  2637. return 0;
  2638. }
  2639. /**
  2640. * serial_core_remove_one_port - detach a driver defined port structure
  2641. * @drv: pointer to the uart low level driver structure for this port
  2642. * @uport: uart port structure for this port
  2643. *
  2644. * Context: task context, might sleep
  2645. *
  2646. * This unhooks (and hangs up) the specified port structure from the core
  2647. * driver. No further calls will be made to the low-level code for this port.
  2648. * Caller must hold port_mutex.
  2649. */
  2650. static void serial_core_remove_one_port(struct uart_driver *drv,
  2651. struct uart_port *uport)
  2652. {
  2653. struct uart_state *state = drv->state + uport->line;
  2654. struct tty_port *port = &state->port;
  2655. struct uart_port *uart_port;
  2656. scoped_guard(mutex, &port->mutex) {
  2657. uart_port = uart_port_check(state);
  2658. if (uart_port != uport)
  2659. dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport);
  2660. if (!uart_port)
  2661. return;
  2662. }
  2663. /*
  2664. * Remove the devices from the tty layer
  2665. */
  2666. tty_port_unregister_device(port, drv->tty_driver, uport->line);
  2667. tty_port_tty_vhangup(port);
  2668. /*
  2669. * If the port is used as a console, unregister it
  2670. */
  2671. if (uart_console(uport))
  2672. unregister_console(uport->cons);
  2673. /*
  2674. * Free the port IO and memory resources, if any.
  2675. */
  2676. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  2677. uport->ops->release_port(uport);
  2678. kfree(uport->tty_groups);
  2679. kfree(uport->name);
  2680. /*
  2681. * Indicate that there isn't a port here anymore.
  2682. */
  2683. uport->type = PORT_UNKNOWN;
  2684. uport->port_dev = NULL;
  2685. guard(mutex)(&port->mutex);
  2686. WARN_ON(atomic_dec_return(&state->refcount) < 0);
  2687. wait_event(state->remove_wait, !atomic_read(&state->refcount));
  2688. state->uart_port = NULL;
  2689. }
  2690. /**
  2691. * uart_match_port - are the two ports equivalent?
  2692. * @port1: first port
  2693. * @port2: second port
  2694. *
  2695. * This utility function can be used to determine whether two uart_port
  2696. * structures describe the same port.
  2697. */
  2698. bool uart_match_port(const struct uart_port *port1,
  2699. const struct uart_port *port2)
  2700. {
  2701. if (port1->iotype != port2->iotype)
  2702. return false;
  2703. switch (port1->iotype) {
  2704. case UPIO_PORT:
  2705. return port1->iobase == port2->iobase;
  2706. case UPIO_HUB6:
  2707. return port1->iobase == port2->iobase &&
  2708. port1->hub6 == port2->hub6;
  2709. case UPIO_MEM:
  2710. case UPIO_MEM16:
  2711. case UPIO_MEM32:
  2712. case UPIO_MEM32BE:
  2713. case UPIO_AU:
  2714. case UPIO_TSI:
  2715. return port1->mapbase == port2->mapbase;
  2716. default:
  2717. return false;
  2718. }
  2719. }
  2720. EXPORT_SYMBOL(uart_match_port);
  2721. static struct serial_ctrl_device *
  2722. serial_core_get_ctrl_dev(struct serial_port_device *port_dev)
  2723. {
  2724. struct device *dev = &port_dev->dev;
  2725. return to_serial_base_ctrl_device(dev->parent);
  2726. }
  2727. /*
  2728. * Find a registered serial core controller device if one exists. Returns
  2729. * the first device matching the ctrl_id. Caller must hold port_mutex.
  2730. */
  2731. static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv,
  2732. struct device *phys_dev,
  2733. int ctrl_id)
  2734. {
  2735. struct uart_state *state;
  2736. int i;
  2737. lockdep_assert_held(&port_mutex);
  2738. for (i = 0; i < drv->nr; i++) {
  2739. state = drv->state + i;
  2740. if (!state->uart_port || !state->uart_port->port_dev)
  2741. continue;
  2742. if (state->uart_port->dev == phys_dev &&
  2743. state->uart_port->ctrl_id == ctrl_id)
  2744. return serial_core_get_ctrl_dev(state->uart_port->port_dev);
  2745. }
  2746. return NULL;
  2747. }
  2748. static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port)
  2749. {
  2750. return serial_base_ctrl_add(port, port->dev);
  2751. }
  2752. static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev,
  2753. struct uart_port *port)
  2754. {
  2755. struct serial_port_device *port_dev;
  2756. port_dev = serial_base_port_add(port, ctrl_dev);
  2757. if (IS_ERR(port_dev))
  2758. return PTR_ERR(port_dev);
  2759. port->port_dev = port_dev;
  2760. return 0;
  2761. }
  2762. /*
  2763. * Initialize a serial core port device, and a controller device if needed.
  2764. */
  2765. int serial_core_register_port(struct uart_driver *drv, struct uart_port *port)
  2766. {
  2767. struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL;
  2768. int ret;
  2769. guard(mutex)(&port_mutex);
  2770. /*
  2771. * Prevent serial_port_runtime_resume() from trying to use the port
  2772. * until serial_core_add_one_port() has completed
  2773. */
  2774. port->flags |= UPF_DEAD;
  2775. /* Inititalize a serial core controller device if needed */
  2776. ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id);
  2777. if (!ctrl_dev) {
  2778. new_ctrl_dev = serial_core_ctrl_device_add(port);
  2779. if (IS_ERR(new_ctrl_dev))
  2780. return PTR_ERR(new_ctrl_dev);
  2781. ctrl_dev = new_ctrl_dev;
  2782. }
  2783. /*
  2784. * Initialize a serial core port device. Tag the port dead to prevent
  2785. * serial_port_runtime_resume() trying to do anything until port has
  2786. * been registered. It gets cleared by serial_core_add_one_port().
  2787. */
  2788. ret = serial_core_port_device_add(ctrl_dev, port);
  2789. if (ret)
  2790. goto err_unregister_ctrl_dev;
  2791. ret = serial_base_match_and_update_preferred_console(drv, port);
  2792. if (ret)
  2793. goto err_unregister_port_dev;
  2794. ret = serial_core_add_one_port(drv, port);
  2795. if (ret)
  2796. goto err_unregister_port_dev;
  2797. return 0;
  2798. err_unregister_port_dev:
  2799. serial_base_port_device_remove(port->port_dev);
  2800. err_unregister_ctrl_dev:
  2801. serial_base_ctrl_device_remove(new_ctrl_dev);
  2802. return ret;
  2803. }
  2804. /*
  2805. * Removes a serial core port device, and the related serial core controller
  2806. * device if the last instance.
  2807. */
  2808. void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port)
  2809. {
  2810. struct device *phys_dev = port->dev;
  2811. struct serial_port_device *port_dev = port->port_dev;
  2812. struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev);
  2813. int ctrl_id = port->ctrl_id;
  2814. guard(mutex)(&port_mutex);
  2815. port->flags |= UPF_DEAD;
  2816. serial_core_remove_one_port(drv, port);
  2817. /* Note that struct uart_port *port is no longer valid at this point */
  2818. serial_base_port_device_remove(port_dev);
  2819. /* Drop the serial core controller device if no ports are using it */
  2820. if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id))
  2821. serial_base_ctrl_device_remove(ctrl_dev);
  2822. }
  2823. /**
  2824. * uart_handle_dcd_change - handle a change of carrier detect state
  2825. * @uport: uart_port structure for the open port
  2826. * @active: new carrier detect status
  2827. *
  2828. * Caller must hold uport->lock.
  2829. */
  2830. void uart_handle_dcd_change(struct uart_port *uport, bool active)
  2831. {
  2832. struct tty_port *port = &uport->state->port;
  2833. struct tty_struct *tty = port->tty;
  2834. struct tty_ldisc *ld;
  2835. lockdep_assert_held_once(&uport->lock);
  2836. if (tty) {
  2837. ld = tty_ldisc_ref(tty);
  2838. if (ld) {
  2839. if (ld->ops->dcd_change)
  2840. ld->ops->dcd_change(tty, active);
  2841. tty_ldisc_deref(ld);
  2842. }
  2843. }
  2844. uport->icount.dcd++;
  2845. if (uart_dcd_enabled(uport)) {
  2846. if (active)
  2847. wake_up_interruptible(&port->open_wait);
  2848. else if (tty)
  2849. tty_hangup(tty);
  2850. }
  2851. }
  2852. EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
  2853. /**
  2854. * uart_handle_cts_change - handle a change of clear-to-send state
  2855. * @uport: uart_port structure for the open port
  2856. * @active: new clear-to-send status
  2857. *
  2858. * Caller must hold uport->lock.
  2859. */
  2860. void uart_handle_cts_change(struct uart_port *uport, bool active)
  2861. {
  2862. lockdep_assert_held_once(&uport->lock);
  2863. uport->icount.cts++;
  2864. if (uart_softcts_mode(uport)) {
  2865. if (uport->hw_stopped) {
  2866. if (active) {
  2867. uport->hw_stopped = false;
  2868. uport->ops->start_tx(uport);
  2869. uart_write_wakeup(uport);
  2870. }
  2871. } else {
  2872. if (!active) {
  2873. uport->hw_stopped = true;
  2874. uport->ops->stop_tx(uport);
  2875. }
  2876. }
  2877. }
  2878. }
  2879. EXPORT_SYMBOL_GPL(uart_handle_cts_change);
  2880. /**
  2881. * uart_insert_char - push a char to the uart layer
  2882. *
  2883. * User is responsible to call tty_flip_buffer_push when they are done with
  2884. * insertion.
  2885. *
  2886. * @port: corresponding port
  2887. * @status: state of the serial port RX buffer (LSR for 8250)
  2888. * @overrun: mask of overrun bits in @status
  2889. * @ch: character to push
  2890. * @flag: flag for the character (see TTY_NORMAL and friends)
  2891. */
  2892. void uart_insert_char(struct uart_port *port, unsigned int status,
  2893. unsigned int overrun, u8 ch, u8 flag)
  2894. {
  2895. struct tty_port *tport = &port->state->port;
  2896. if ((status & port->ignore_status_mask & ~overrun) == 0)
  2897. if (tty_insert_flip_char(tport, ch, flag) == 0)
  2898. ++port->icount.buf_overrun;
  2899. /*
  2900. * Overrun is special. Since it's reported immediately,
  2901. * it doesn't affect the current character.
  2902. */
  2903. if (status & ~port->ignore_status_mask & overrun)
  2904. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
  2905. ++port->icount.buf_overrun;
  2906. }
  2907. EXPORT_SYMBOL_GPL(uart_insert_char);
  2908. #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
  2909. static const u8 sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
  2910. static void uart_sysrq_on(struct work_struct *w)
  2911. {
  2912. int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
  2913. sysrq_toggle_support(1);
  2914. pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
  2915. sysrq_toggle_seq_len, sysrq_toggle_seq);
  2916. }
  2917. static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
  2918. /**
  2919. * uart_try_toggle_sysrq - Enables SysRq from serial line
  2920. * @port: uart_port structure where char(s) after BREAK met
  2921. * @ch: new character in the sequence after received BREAK
  2922. *
  2923. * Enables magic SysRq when the required sequence is met on port
  2924. * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
  2925. *
  2926. * Returns: %false if @ch is out of enabling sequence and should be
  2927. * handled some other way, %true if @ch was consumed.
  2928. */
  2929. bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch)
  2930. {
  2931. int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
  2932. if (!sysrq_toggle_seq_len)
  2933. return false;
  2934. BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
  2935. if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
  2936. port->sysrq_seq = 0;
  2937. return false;
  2938. }
  2939. if (++port->sysrq_seq < sysrq_toggle_seq_len) {
  2940. port->sysrq = jiffies + SYSRQ_TIMEOUT;
  2941. return true;
  2942. }
  2943. schedule_work(&sysrq_enable_work);
  2944. port->sysrq = 0;
  2945. return true;
  2946. }
  2947. EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
  2948. #endif
  2949. /**
  2950. * uart_get_rs485_mode() - retrieve rs485 properties for given uart
  2951. * @port: uart device's target port
  2952. *
  2953. * This function implements the device tree binding described in
  2954. * Documentation/devicetree/bindings/serial/rs485.txt.
  2955. */
  2956. int uart_get_rs485_mode(struct uart_port *port)
  2957. {
  2958. struct serial_rs485 *rs485conf = &port->rs485;
  2959. struct device *dev = port->dev;
  2960. enum gpiod_flags dflags;
  2961. struct gpio_desc *desc;
  2962. u32 rs485_delay[2];
  2963. int ret;
  2964. if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
  2965. return 0;
  2966. /*
  2967. * Retrieve properties only if a firmware node exists. If no firmware
  2968. * node exists, then don't touch rs485 config and keep initial rs485
  2969. * properties set by driver.
  2970. */
  2971. if (!dev_fwnode(dev))
  2972. return 0;
  2973. ret = device_property_read_u32_array(dev, "rs485-rts-delay",
  2974. rs485_delay, 2);
  2975. if (!ret) {
  2976. rs485conf->delay_rts_before_send = rs485_delay[0];
  2977. rs485conf->delay_rts_after_send = rs485_delay[1];
  2978. } else {
  2979. rs485conf->delay_rts_before_send = 0;
  2980. rs485conf->delay_rts_after_send = 0;
  2981. }
  2982. uart_sanitize_serial_rs485_delays(port, rs485conf);
  2983. /*
  2984. * Clear full-duplex and enabled flags, set RTS polarity to active high
  2985. * to get to a defined state with the following properties:
  2986. */
  2987. rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
  2988. SER_RS485_TERMINATE_BUS |
  2989. SER_RS485_RTS_AFTER_SEND);
  2990. rs485conf->flags |= SER_RS485_RTS_ON_SEND;
  2991. if (device_property_read_bool(dev, "rs485-rx-during-tx"))
  2992. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  2993. if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
  2994. rs485conf->flags |= SER_RS485_ENABLED;
  2995. if (device_property_read_bool(dev, "rs485-rts-active-low")) {
  2996. rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
  2997. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  2998. }
  2999. /*
  3000. * Disabling termination by default is the safe choice: Else if many
  3001. * bus participants enable it, no communication is possible at all.
  3002. * Works fine for short cables and users may enable for longer cables.
  3003. */
  3004. desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
  3005. if (IS_ERR(desc))
  3006. return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
  3007. port->rs485_term_gpio = desc;
  3008. if (port->rs485_term_gpio)
  3009. port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
  3010. dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
  3011. GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  3012. desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags);
  3013. if (IS_ERR(desc))
  3014. return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
  3015. port->rs485_rx_during_tx_gpio = desc;
  3016. if (port->rs485_rx_during_tx_gpio)
  3017. port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;
  3018. return 0;
  3019. }
  3020. EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
  3021. /* Compile-time assertions for serial_rs485 layout */
  3022. static_assert(offsetof(struct serial_rs485, padding) ==
  3023. (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
  3024. static_assert(offsetof(struct serial_rs485, padding1) ==
  3025. offsetof(struct serial_rs485, padding[1]));
  3026. static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
  3027. sizeof(struct serial_rs485));
  3028. MODULE_DESCRIPTION("Serial driver core");
  3029. MODULE_LICENSE("GPL");