serial_core.c 93 KB

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