serial_core.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. /*
  2. * Driver core for serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright 1999 ARM Limited
  7. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched/signal.h>
  28. #include <linux/init.h>
  29. #include <linux/console.h>
  30. #include <linux/of.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/device.h>
  34. #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
  35. #include <linux/serial_core.h>
  36. #include <linux/delay.h>
  37. #include <linux/mutex.h>
  38. #include <asm/irq.h>
  39. #include <linux/uaccess.h>
  40. /*
  41. * This is used to lock changes in serial line configuration.
  42. */
  43. static DEFINE_MUTEX(port_mutex);
  44. /*
  45. * lockdep: port->lock is initialized in two places, but we
  46. * want only one lock-class:
  47. */
  48. static struct lock_class_key port_lock_key;
  49. #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
  50. static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
  51. struct ktermios *old_termios);
  52. static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
  53. static void uart_change_pm(struct uart_state *state,
  54. enum uart_pm_state pm_state);
  55. static void uart_port_shutdown(struct tty_port *port);
  56. static int uart_dcd_enabled(struct uart_port *uport)
  57. {
  58. return !!(uport->status & UPSTAT_DCD_ENABLE);
  59. }
  60. static inline struct uart_port *uart_port_ref(struct uart_state *state)
  61. {
  62. if (atomic_add_unless(&state->refcount, 1, 0))
  63. return state->uart_port;
  64. return NULL;
  65. }
  66. static inline void uart_port_deref(struct uart_port *uport)
  67. {
  68. if (atomic_dec_and_test(&uport->state->refcount))
  69. wake_up(&uport->state->remove_wait);
  70. }
  71. #define uart_port_lock(state, flags) \
  72. ({ \
  73. struct uart_port *__uport = uart_port_ref(state); \
  74. if (__uport) \
  75. spin_lock_irqsave(&__uport->lock, flags); \
  76. __uport; \
  77. })
  78. #define uart_port_unlock(uport, flags) \
  79. ({ \
  80. struct uart_port *__uport = uport; \
  81. if (__uport) { \
  82. spin_unlock_irqrestore(&__uport->lock, flags); \
  83. uart_port_deref(__uport); \
  84. } \
  85. })
  86. static inline struct uart_port *uart_port_check(struct uart_state *state)
  87. {
  88. lockdep_assert_held(&state->port.mutex);
  89. return state->uart_port;
  90. }
  91. /*
  92. * This routine is used by the interrupt handler to schedule processing in
  93. * the software interrupt portion of the driver.
  94. */
  95. void uart_write_wakeup(struct uart_port *port)
  96. {
  97. struct uart_state *state = port->state;
  98. /*
  99. * This means you called this function _after_ the port was
  100. * closed. No cookie for you.
  101. */
  102. BUG_ON(!state);
  103. tty_port_tty_wakeup(&state->port);
  104. }
  105. static void uart_stop(struct tty_struct *tty)
  106. {
  107. struct uart_state *state = tty->driver_data;
  108. struct uart_port *port;
  109. unsigned long flags;
  110. port = uart_port_lock(state, flags);
  111. if (port)
  112. port->ops->stop_tx(port);
  113. uart_port_unlock(port, flags);
  114. }
  115. static void __uart_start(struct tty_struct *tty)
  116. {
  117. struct uart_state *state = tty->driver_data;
  118. struct uart_port *port = state->uart_port;
  119. if (port && !uart_tx_stopped(port))
  120. port->ops->start_tx(port);
  121. }
  122. static void uart_start(struct tty_struct *tty)
  123. {
  124. struct uart_state *state = tty->driver_data;
  125. struct uart_port *port;
  126. unsigned long flags;
  127. port = uart_port_lock(state, flags);
  128. __uart_start(tty);
  129. uart_port_unlock(port, flags);
  130. }
  131. static void
  132. uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
  133. {
  134. unsigned long flags;
  135. unsigned int old;
  136. spin_lock_irqsave(&port->lock, flags);
  137. old = port->mctrl;
  138. port->mctrl = (old & ~clear) | set;
  139. if (old != port->mctrl)
  140. port->ops->set_mctrl(port, port->mctrl);
  141. spin_unlock_irqrestore(&port->lock, flags);
  142. }
  143. #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
  144. #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
  145. /*
  146. * Startup the port. This will be called once per open. All calls
  147. * will be serialised by the per-port mutex.
  148. */
  149. static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
  150. int init_hw)
  151. {
  152. struct uart_port *uport = uart_port_check(state);
  153. unsigned long page;
  154. int retval = 0;
  155. if (uport->type == PORT_UNKNOWN)
  156. return 1;
  157. /*
  158. * Make sure the device is in D0 state.
  159. */
  160. uart_change_pm(state, UART_PM_STATE_ON);
  161. /*
  162. * Initialise and allocate the transmit and temporary
  163. * buffer.
  164. */
  165. if (!state->xmit.buf) {
  166. /* This is protected by the per port mutex */
  167. page = get_zeroed_page(GFP_KERNEL);
  168. if (!page)
  169. return -ENOMEM;
  170. state->xmit.buf = (unsigned char *) page;
  171. uart_circ_clear(&state->xmit);
  172. }
  173. retval = uport->ops->startup(uport);
  174. if (retval == 0) {
  175. if (uart_console(uport) && uport->cons->cflag) {
  176. tty->termios.c_cflag = uport->cons->cflag;
  177. uport->cons->cflag = 0;
  178. }
  179. /*
  180. * Initialise the hardware port settings.
  181. */
  182. uart_change_speed(tty, state, NULL);
  183. /*
  184. * Setup the RTS and DTR signals once the
  185. * port is open and ready to respond.
  186. */
  187. if (init_hw && C_BAUD(tty))
  188. uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
  189. }
  190. /*
  191. * This is to allow setserial on this port. People may want to set
  192. * port/irq/type and then reconfigure the port properly if it failed
  193. * now.
  194. */
  195. if (retval && capable(CAP_SYS_ADMIN))
  196. return 1;
  197. return retval;
  198. }
  199. static int uart_startup(struct tty_struct *tty, struct uart_state *state,
  200. int init_hw)
  201. {
  202. struct tty_port *port = &state->port;
  203. int retval;
  204. if (tty_port_initialized(port))
  205. return 0;
  206. retval = uart_port_startup(tty, state, init_hw);
  207. if (retval)
  208. set_bit(TTY_IO_ERROR, &tty->flags);
  209. return retval;
  210. }
  211. /*
  212. * This routine will shutdown a serial port; interrupts are disabled, and
  213. * DTR is dropped if the hangup on close termio flag is on. Calls to
  214. * uart_shutdown are serialised by the per-port semaphore.
  215. *
  216. * uport == NULL if uart_port has already been removed
  217. */
  218. static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
  219. {
  220. struct uart_port *uport = uart_port_check(state);
  221. struct tty_port *port = &state->port;
  222. /*
  223. * Set the TTY IO error marker
  224. */
  225. if (tty)
  226. set_bit(TTY_IO_ERROR, &tty->flags);
  227. if (tty_port_initialized(port)) {
  228. tty_port_set_initialized(port, 0);
  229. /*
  230. * Turn off DTR and RTS early.
  231. */
  232. if (uport && uart_console(uport) && tty)
  233. uport->cons->cflag = tty->termios.c_cflag;
  234. if (!tty || C_HUPCL(tty))
  235. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  236. uart_port_shutdown(port);
  237. }
  238. /*
  239. * It's possible for shutdown to be called after suspend if we get
  240. * a DCD drop (hangup) at just the right time. Clear suspended bit so
  241. * we don't try to resume a port that has been shutdown.
  242. */
  243. tty_port_set_suspended(port, 0);
  244. /*
  245. * Free the transmit buffer page.
  246. */
  247. if (state->xmit.buf) {
  248. free_page((unsigned long)state->xmit.buf);
  249. state->xmit.buf = NULL;
  250. }
  251. }
  252. /**
  253. * uart_update_timeout - update per-port FIFO timeout.
  254. * @port: uart_port structure describing the port
  255. * @cflag: termios cflag value
  256. * @baud: speed of the port
  257. *
  258. * Set the port FIFO timeout value. The @cflag value should
  259. * reflect the actual hardware settings.
  260. */
  261. void
  262. uart_update_timeout(struct uart_port *port, unsigned int cflag,
  263. unsigned int baud)
  264. {
  265. unsigned int bits;
  266. /* byte size and parity */
  267. switch (cflag & CSIZE) {
  268. case CS5:
  269. bits = 7;
  270. break;
  271. case CS6:
  272. bits = 8;
  273. break;
  274. case CS7:
  275. bits = 9;
  276. break;
  277. default:
  278. bits = 10;
  279. break; /* CS8 */
  280. }
  281. if (cflag & CSTOPB)
  282. bits++;
  283. if (cflag & PARENB)
  284. bits++;
  285. /*
  286. * The total number of bits to be transmitted in the fifo.
  287. */
  288. bits = bits * port->fifosize;
  289. /*
  290. * Figure the timeout to send the above number of bits.
  291. * Add .02 seconds of slop
  292. */
  293. port->timeout = (HZ * bits) / baud + HZ/50;
  294. }
  295. EXPORT_SYMBOL(uart_update_timeout);
  296. /**
  297. * uart_get_baud_rate - return baud rate for a particular port
  298. * @port: uart_port structure describing the port in question.
  299. * @termios: desired termios settings.
  300. * @old: old termios (or NULL)
  301. * @min: minimum acceptable baud rate
  302. * @max: maximum acceptable baud rate
  303. *
  304. * Decode the termios structure into a numeric baud rate,
  305. * taking account of the magic 38400 baud rate (with spd_*
  306. * flags), and mapping the %B0 rate to 9600 baud.
  307. *
  308. * If the new baud rate is invalid, try the old termios setting.
  309. * If it's still invalid, we try 9600 baud.
  310. *
  311. * Update the @termios structure to reflect the baud rate
  312. * we're actually going to be using. Don't do this for the case
  313. * where B0 is requested ("hang up").
  314. */
  315. unsigned int
  316. uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
  317. struct ktermios *old, unsigned int min, unsigned int max)
  318. {
  319. unsigned int try;
  320. unsigned int baud;
  321. unsigned int altbaud;
  322. int hung_up = 0;
  323. upf_t flags = port->flags & UPF_SPD_MASK;
  324. switch (flags) {
  325. case UPF_SPD_HI:
  326. altbaud = 57600;
  327. break;
  328. case UPF_SPD_VHI:
  329. altbaud = 115200;
  330. break;
  331. case UPF_SPD_SHI:
  332. altbaud = 230400;
  333. break;
  334. case UPF_SPD_WARP:
  335. altbaud = 460800;
  336. break;
  337. default:
  338. altbaud = 38400;
  339. break;
  340. }
  341. for (try = 0; try < 2; try++) {
  342. baud = tty_termios_baud_rate(termios);
  343. /*
  344. * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
  345. * Die! Die! Die!
  346. */
  347. if (try == 0 && baud == 38400)
  348. baud = altbaud;
  349. /*
  350. * Special case: B0 rate.
  351. */
  352. if (baud == 0) {
  353. hung_up = 1;
  354. baud = 9600;
  355. }
  356. if (baud >= min && baud <= max)
  357. return baud;
  358. /*
  359. * Oops, the quotient was zero. Try again with
  360. * the old baud rate if possible.
  361. */
  362. termios->c_cflag &= ~CBAUD;
  363. if (old) {
  364. baud = tty_termios_baud_rate(old);
  365. if (!hung_up)
  366. tty_termios_encode_baud_rate(termios,
  367. baud, baud);
  368. old = NULL;
  369. continue;
  370. }
  371. /*
  372. * As a last resort, if the range cannot be met then clip to
  373. * the nearest chip supported rate.
  374. */
  375. if (!hung_up) {
  376. if (baud <= min)
  377. tty_termios_encode_baud_rate(termios,
  378. min + 1, min + 1);
  379. else
  380. tty_termios_encode_baud_rate(termios,
  381. max - 1, max - 1);
  382. }
  383. }
  384. /* Should never happen */
  385. WARN_ON(1);
  386. return 0;
  387. }
  388. EXPORT_SYMBOL(uart_get_baud_rate);
  389. /**
  390. * uart_get_divisor - return uart clock divisor
  391. * @port: uart_port structure describing the port.
  392. * @baud: desired baud rate
  393. *
  394. * Calculate the uart clock divisor for the port.
  395. */
  396. unsigned int
  397. uart_get_divisor(struct uart_port *port, unsigned int baud)
  398. {
  399. unsigned int quot;
  400. /*
  401. * Old custom speed handling.
  402. */
  403. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  404. quot = port->custom_divisor;
  405. else
  406. quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
  407. return quot;
  408. }
  409. EXPORT_SYMBOL(uart_get_divisor);
  410. /* Caller holds port mutex */
  411. static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
  412. struct ktermios *old_termios)
  413. {
  414. struct uart_port *uport = uart_port_check(state);
  415. struct ktermios *termios;
  416. int hw_stopped;
  417. /*
  418. * If we have no tty, termios, or the port does not exist,
  419. * then we can't set the parameters for this port.
  420. */
  421. if (!tty || uport->type == PORT_UNKNOWN)
  422. return;
  423. termios = &tty->termios;
  424. uport->ops->set_termios(uport, termios, old_termios);
  425. /*
  426. * Set modem status enables based on termios cflag
  427. */
  428. spin_lock_irq(&uport->lock);
  429. if (termios->c_cflag & CRTSCTS)
  430. uport->status |= UPSTAT_CTS_ENABLE;
  431. else
  432. uport->status &= ~UPSTAT_CTS_ENABLE;
  433. if (termios->c_cflag & CLOCAL)
  434. uport->status &= ~UPSTAT_DCD_ENABLE;
  435. else
  436. uport->status |= UPSTAT_DCD_ENABLE;
  437. /* reset sw-assisted CTS flow control based on (possibly) new mode */
  438. hw_stopped = uport->hw_stopped;
  439. uport->hw_stopped = uart_softcts_mode(uport) &&
  440. !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
  441. if (uport->hw_stopped) {
  442. if (!hw_stopped)
  443. uport->ops->stop_tx(uport);
  444. } else {
  445. if (hw_stopped)
  446. __uart_start(tty);
  447. }
  448. spin_unlock_irq(&uport->lock);
  449. }
  450. static int uart_put_char(struct tty_struct *tty, unsigned char c)
  451. {
  452. struct uart_state *state = tty->driver_data;
  453. struct uart_port *port;
  454. struct circ_buf *circ;
  455. unsigned long flags;
  456. int ret = 0;
  457. circ = &state->xmit;
  458. if (!circ->buf)
  459. return 0;
  460. port = uart_port_lock(state, flags);
  461. if (port && uart_circ_chars_free(circ) != 0) {
  462. circ->buf[circ->head] = c;
  463. circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
  464. ret = 1;
  465. }
  466. uart_port_unlock(port, flags);
  467. return ret;
  468. }
  469. static void uart_flush_chars(struct tty_struct *tty)
  470. {
  471. uart_start(tty);
  472. }
  473. static int uart_write(struct tty_struct *tty,
  474. const unsigned char *buf, int count)
  475. {
  476. struct uart_state *state = tty->driver_data;
  477. struct uart_port *port;
  478. struct circ_buf *circ;
  479. unsigned long flags;
  480. int c, ret = 0;
  481. /*
  482. * This means you called this function _after_ the port was
  483. * closed. No cookie for you.
  484. */
  485. if (!state) {
  486. WARN_ON(1);
  487. return -EL3HLT;
  488. }
  489. circ = &state->xmit;
  490. if (!circ->buf)
  491. return 0;
  492. port = uart_port_lock(state, flags);
  493. while (port) {
  494. c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
  495. if (count < c)
  496. c = count;
  497. if (c <= 0)
  498. break;
  499. memcpy(circ->buf + circ->head, buf, c);
  500. circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
  501. buf += c;
  502. count -= c;
  503. ret += c;
  504. }
  505. __uart_start(tty);
  506. uart_port_unlock(port, flags);
  507. return ret;
  508. }
  509. static int uart_write_room(struct tty_struct *tty)
  510. {
  511. struct uart_state *state = tty->driver_data;
  512. struct uart_port *port;
  513. unsigned long flags;
  514. int ret;
  515. port = uart_port_lock(state, flags);
  516. ret = uart_circ_chars_free(&state->xmit);
  517. uart_port_unlock(port, flags);
  518. return ret;
  519. }
  520. static int uart_chars_in_buffer(struct tty_struct *tty)
  521. {
  522. struct uart_state *state = tty->driver_data;
  523. struct uart_port *port;
  524. unsigned long flags;
  525. int ret;
  526. port = uart_port_lock(state, flags);
  527. ret = uart_circ_chars_pending(&state->xmit);
  528. uart_port_unlock(port, flags);
  529. return ret;
  530. }
  531. static void uart_flush_buffer(struct tty_struct *tty)
  532. {
  533. struct uart_state *state = tty->driver_data;
  534. struct uart_port *port;
  535. unsigned long flags;
  536. /*
  537. * This means you called this function _after_ the port was
  538. * closed. No cookie for you.
  539. */
  540. if (!state) {
  541. WARN_ON(1);
  542. return;
  543. }
  544. pr_debug("uart_flush_buffer(%d) called\n", tty->index);
  545. port = uart_port_lock(state, flags);
  546. if (!port)
  547. return;
  548. uart_circ_clear(&state->xmit);
  549. if (port->ops->flush_buffer)
  550. port->ops->flush_buffer(port);
  551. uart_port_unlock(port, flags);
  552. tty_port_tty_wakeup(&state->port);
  553. }
  554. /*
  555. * This function is used to send a high-priority XON/XOFF character to
  556. * the device
  557. */
  558. static void uart_send_xchar(struct tty_struct *tty, char ch)
  559. {
  560. struct uart_state *state = tty->driver_data;
  561. struct uart_port *port;
  562. unsigned long flags;
  563. port = uart_port_ref(state);
  564. if (!port)
  565. return;
  566. if (port->ops->send_xchar)
  567. port->ops->send_xchar(port, ch);
  568. else {
  569. spin_lock_irqsave(&port->lock, flags);
  570. port->x_char = ch;
  571. if (ch)
  572. port->ops->start_tx(port);
  573. spin_unlock_irqrestore(&port->lock, flags);
  574. }
  575. uart_port_deref(port);
  576. }
  577. static void uart_throttle(struct tty_struct *tty)
  578. {
  579. struct uart_state *state = tty->driver_data;
  580. struct uart_port *port;
  581. upstat_t mask = 0;
  582. port = uart_port_ref(state);
  583. if (!port)
  584. return;
  585. if (I_IXOFF(tty))
  586. mask |= UPSTAT_AUTOXOFF;
  587. if (C_CRTSCTS(tty))
  588. mask |= UPSTAT_AUTORTS;
  589. if (port->status & mask) {
  590. port->ops->throttle(port);
  591. mask &= ~port->status;
  592. }
  593. if (mask & UPSTAT_AUTORTS)
  594. uart_clear_mctrl(port, TIOCM_RTS);
  595. if (mask & UPSTAT_AUTOXOFF)
  596. uart_send_xchar(tty, STOP_CHAR(tty));
  597. uart_port_deref(port);
  598. }
  599. static void uart_unthrottle(struct tty_struct *tty)
  600. {
  601. struct uart_state *state = tty->driver_data;
  602. struct uart_port *port;
  603. upstat_t mask = 0;
  604. port = uart_port_ref(state);
  605. if (!port)
  606. return;
  607. if (I_IXOFF(tty))
  608. mask |= UPSTAT_AUTOXOFF;
  609. if (C_CRTSCTS(tty))
  610. mask |= UPSTAT_AUTORTS;
  611. if (port->status & mask) {
  612. port->ops->unthrottle(port);
  613. mask &= ~port->status;
  614. }
  615. if (mask & UPSTAT_AUTORTS)
  616. uart_set_mctrl(port, TIOCM_RTS);
  617. if (mask & UPSTAT_AUTOXOFF)
  618. uart_send_xchar(tty, START_CHAR(tty));
  619. uart_port_deref(port);
  620. }
  621. static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
  622. {
  623. struct uart_state *state = container_of(port, struct uart_state, port);
  624. struct uart_port *uport;
  625. int ret = -ENODEV;
  626. memset(retinfo, 0, sizeof(*retinfo));
  627. /*
  628. * Ensure the state we copy is consistent and no hardware changes
  629. * occur as we go
  630. */
  631. mutex_lock(&port->mutex);
  632. uport = uart_port_check(state);
  633. if (!uport)
  634. goto out;
  635. retinfo->type = uport->type;
  636. retinfo->line = uport->line;
  637. retinfo->port = uport->iobase;
  638. if (HIGH_BITS_OFFSET)
  639. retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
  640. retinfo->irq = uport->irq;
  641. retinfo->flags = uport->flags;
  642. retinfo->xmit_fifo_size = uport->fifosize;
  643. retinfo->baud_base = uport->uartclk / 16;
  644. retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
  645. retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  646. ASYNC_CLOSING_WAIT_NONE :
  647. jiffies_to_msecs(port->closing_wait) / 10;
  648. retinfo->custom_divisor = uport->custom_divisor;
  649. retinfo->hub6 = uport->hub6;
  650. retinfo->io_type = uport->iotype;
  651. retinfo->iomem_reg_shift = uport->regshift;
  652. retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
  653. ret = 0;
  654. out:
  655. mutex_unlock(&port->mutex);
  656. return ret;
  657. }
  658. static int uart_get_info_user(struct tty_port *port,
  659. struct serial_struct __user *retinfo)
  660. {
  661. struct serial_struct tmp;
  662. if (uart_get_info(port, &tmp) < 0)
  663. return -EIO;
  664. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  665. return -EFAULT;
  666. return 0;
  667. }
  668. static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
  669. struct uart_state *state,
  670. struct serial_struct *new_info)
  671. {
  672. struct uart_port *uport = uart_port_check(state);
  673. unsigned long new_port;
  674. unsigned int change_irq, change_port, closing_wait;
  675. unsigned int old_custom_divisor, close_delay;
  676. upf_t old_flags, new_flags;
  677. int retval = 0;
  678. if (!uport)
  679. return -EIO;
  680. new_port = new_info->port;
  681. if (HIGH_BITS_OFFSET)
  682. new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
  683. new_info->irq = irq_canonicalize(new_info->irq);
  684. close_delay = msecs_to_jiffies(new_info->close_delay * 10);
  685. closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  686. ASYNC_CLOSING_WAIT_NONE :
  687. msecs_to_jiffies(new_info->closing_wait * 10);
  688. change_irq = !(uport->flags & UPF_FIXED_PORT)
  689. && new_info->irq != uport->irq;
  690. /*
  691. * Since changing the 'type' of the port changes its resource
  692. * allocations, we should treat type changes the same as
  693. * IO port changes.
  694. */
  695. change_port = !(uport->flags & UPF_FIXED_PORT)
  696. && (new_port != uport->iobase ||
  697. (unsigned long)new_info->iomem_base != uport->mapbase ||
  698. new_info->hub6 != uport->hub6 ||
  699. new_info->io_type != uport->iotype ||
  700. new_info->iomem_reg_shift != uport->regshift ||
  701. new_info->type != uport->type);
  702. old_flags = uport->flags;
  703. new_flags = new_info->flags;
  704. old_custom_divisor = uport->custom_divisor;
  705. if (!capable(CAP_SYS_ADMIN)) {
  706. retval = -EPERM;
  707. if (change_irq || change_port ||
  708. (new_info->baud_base != uport->uartclk / 16) ||
  709. (close_delay != port->close_delay) ||
  710. (closing_wait != port->closing_wait) ||
  711. (new_info->xmit_fifo_size &&
  712. new_info->xmit_fifo_size != uport->fifosize) ||
  713. (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
  714. goto exit;
  715. uport->flags = ((uport->flags & ~UPF_USR_MASK) |
  716. (new_flags & UPF_USR_MASK));
  717. uport->custom_divisor = new_info->custom_divisor;
  718. goto check_and_exit;
  719. }
  720. /*
  721. * Ask the low level driver to verify the settings.
  722. */
  723. if (uport->ops->verify_port)
  724. retval = uport->ops->verify_port(uport, new_info);
  725. if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
  726. (new_info->baud_base < 9600))
  727. retval = -EINVAL;
  728. if (retval)
  729. goto exit;
  730. if (change_port || change_irq) {
  731. retval = -EBUSY;
  732. /*
  733. * Make sure that we are the sole user of this port.
  734. */
  735. if (tty_port_users(port) > 1)
  736. goto exit;
  737. /*
  738. * We need to shutdown the serial port at the old
  739. * port/type/irq combination.
  740. */
  741. uart_shutdown(tty, state);
  742. }
  743. if (change_port) {
  744. unsigned long old_iobase, old_mapbase;
  745. unsigned int old_type, old_iotype, old_hub6, old_shift;
  746. old_iobase = uport->iobase;
  747. old_mapbase = uport->mapbase;
  748. old_type = uport->type;
  749. old_hub6 = uport->hub6;
  750. old_iotype = uport->iotype;
  751. old_shift = uport->regshift;
  752. /*
  753. * Free and release old regions
  754. */
  755. if (old_type != PORT_UNKNOWN && uport->ops->release_port)
  756. uport->ops->release_port(uport);
  757. uport->iobase = new_port;
  758. uport->type = new_info->type;
  759. uport->hub6 = new_info->hub6;
  760. uport->iotype = new_info->io_type;
  761. uport->regshift = new_info->iomem_reg_shift;
  762. uport->mapbase = (unsigned long)new_info->iomem_base;
  763. /*
  764. * Claim and map the new regions
  765. */
  766. if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
  767. retval = uport->ops->request_port(uport);
  768. } else {
  769. /* Always success - Jean II */
  770. retval = 0;
  771. }
  772. /*
  773. * If we fail to request resources for the
  774. * new port, try to restore the old settings.
  775. */
  776. if (retval) {
  777. uport->iobase = old_iobase;
  778. uport->type = old_type;
  779. uport->hub6 = old_hub6;
  780. uport->iotype = old_iotype;
  781. uport->regshift = old_shift;
  782. uport->mapbase = old_mapbase;
  783. if (old_type != PORT_UNKNOWN) {
  784. retval = uport->ops->request_port(uport);
  785. /*
  786. * If we failed to restore the old settings,
  787. * we fail like this.
  788. */
  789. if (retval)
  790. uport->type = PORT_UNKNOWN;
  791. /*
  792. * We failed anyway.
  793. */
  794. retval = -EBUSY;
  795. }
  796. /* Added to return the correct error -Ram Gupta */
  797. goto exit;
  798. }
  799. }
  800. if (change_irq)
  801. uport->irq = new_info->irq;
  802. if (!(uport->flags & UPF_FIXED_PORT))
  803. uport->uartclk = new_info->baud_base * 16;
  804. uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
  805. (new_flags & UPF_CHANGE_MASK);
  806. uport->custom_divisor = new_info->custom_divisor;
  807. port->close_delay = close_delay;
  808. port->closing_wait = closing_wait;
  809. if (new_info->xmit_fifo_size)
  810. uport->fifosize = new_info->xmit_fifo_size;
  811. port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
  812. check_and_exit:
  813. retval = 0;
  814. if (uport->type == PORT_UNKNOWN)
  815. goto exit;
  816. if (tty_port_initialized(port)) {
  817. if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
  818. old_custom_divisor != uport->custom_divisor) {
  819. /*
  820. * If they're setting up a custom divisor or speed,
  821. * instead of clearing it, then bitch about it.
  822. */
  823. if (uport->flags & UPF_SPD_MASK) {
  824. dev_notice_ratelimited(uport->dev,
  825. "%s sets custom speed on %s. This is deprecated.\n",
  826. current->comm,
  827. tty_name(port->tty));
  828. }
  829. uart_change_speed(tty, state, NULL);
  830. }
  831. } else {
  832. retval = uart_startup(tty, state, 1);
  833. if (retval > 0)
  834. retval = 0;
  835. }
  836. exit:
  837. return retval;
  838. }
  839. static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
  840. struct serial_struct __user *newinfo)
  841. {
  842. struct serial_struct new_serial;
  843. struct tty_port *port = &state->port;
  844. int retval;
  845. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  846. return -EFAULT;
  847. /*
  848. * This semaphore protects port->count. It is also
  849. * very useful to prevent opens. Also, take the
  850. * port configuration semaphore to make sure that a
  851. * module insertion/removal doesn't change anything
  852. * under us.
  853. */
  854. mutex_lock(&port->mutex);
  855. retval = uart_set_info(tty, port, state, &new_serial);
  856. mutex_unlock(&port->mutex);
  857. return retval;
  858. }
  859. /**
  860. * uart_get_lsr_info - get line status register info
  861. * @tty: tty associated with the UART
  862. * @state: UART being queried
  863. * @value: returned modem value
  864. */
  865. static int uart_get_lsr_info(struct tty_struct *tty,
  866. struct uart_state *state, unsigned int __user *value)
  867. {
  868. struct uart_port *uport = uart_port_check(state);
  869. unsigned int result;
  870. result = uport->ops->tx_empty(uport);
  871. /*
  872. * If we're about to load something into the transmit
  873. * register, we'll pretend the transmitter isn't empty to
  874. * avoid a race condition (depending on when the transmit
  875. * interrupt happens).
  876. */
  877. if (uport->x_char ||
  878. ((uart_circ_chars_pending(&state->xmit) > 0) &&
  879. !uart_tx_stopped(uport)))
  880. result &= ~TIOCSER_TEMT;
  881. return put_user(result, value);
  882. }
  883. static int uart_tiocmget(struct tty_struct *tty)
  884. {
  885. struct uart_state *state = tty->driver_data;
  886. struct tty_port *port = &state->port;
  887. struct uart_port *uport;
  888. int result = -EIO;
  889. mutex_lock(&port->mutex);
  890. uport = uart_port_check(state);
  891. if (!uport)
  892. goto out;
  893. if (!tty_io_error(tty)) {
  894. result = uport->mctrl;
  895. spin_lock_irq(&uport->lock);
  896. result |= uport->ops->get_mctrl(uport);
  897. spin_unlock_irq(&uport->lock);
  898. }
  899. out:
  900. mutex_unlock(&port->mutex);
  901. return result;
  902. }
  903. static int
  904. uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
  905. {
  906. struct uart_state *state = tty->driver_data;
  907. struct tty_port *port = &state->port;
  908. struct uart_port *uport;
  909. int ret = -EIO;
  910. mutex_lock(&port->mutex);
  911. uport = uart_port_check(state);
  912. if (!uport)
  913. goto out;
  914. if (!tty_io_error(tty)) {
  915. uart_update_mctrl(uport, set, clear);
  916. ret = 0;
  917. }
  918. out:
  919. mutex_unlock(&port->mutex);
  920. return ret;
  921. }
  922. static int uart_break_ctl(struct tty_struct *tty, int break_state)
  923. {
  924. struct uart_state *state = tty->driver_data;
  925. struct tty_port *port = &state->port;
  926. struct uart_port *uport;
  927. int ret = -EIO;
  928. mutex_lock(&port->mutex);
  929. uport = uart_port_check(state);
  930. if (!uport)
  931. goto out;
  932. if (uport->type != PORT_UNKNOWN)
  933. uport->ops->break_ctl(uport, break_state);
  934. ret = 0;
  935. out:
  936. mutex_unlock(&port->mutex);
  937. return ret;
  938. }
  939. static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
  940. {
  941. struct tty_port *port = &state->port;
  942. struct uart_port *uport;
  943. int flags, ret;
  944. if (!capable(CAP_SYS_ADMIN))
  945. return -EPERM;
  946. /*
  947. * Take the per-port semaphore. This prevents count from
  948. * changing, and hence any extra opens of the port while
  949. * we're auto-configuring.
  950. */
  951. if (mutex_lock_interruptible(&port->mutex))
  952. return -ERESTARTSYS;
  953. uport = uart_port_check(state);
  954. if (!uport) {
  955. ret = -EIO;
  956. goto out;
  957. }
  958. ret = -EBUSY;
  959. if (tty_port_users(port) == 1) {
  960. uart_shutdown(tty, state);
  961. /*
  962. * If we already have a port type configured,
  963. * we must release its resources.
  964. */
  965. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  966. uport->ops->release_port(uport);
  967. flags = UART_CONFIG_TYPE;
  968. if (uport->flags & UPF_AUTO_IRQ)
  969. flags |= UART_CONFIG_IRQ;
  970. /*
  971. * This will claim the ports resources if
  972. * a port is found.
  973. */
  974. uport->ops->config_port(uport, flags);
  975. ret = uart_startup(tty, state, 1);
  976. if (ret > 0)
  977. ret = 0;
  978. }
  979. out:
  980. mutex_unlock(&port->mutex);
  981. return ret;
  982. }
  983. static void uart_enable_ms(struct uart_port *uport)
  984. {
  985. /*
  986. * Force modem status interrupts on
  987. */
  988. if (uport->ops->enable_ms)
  989. uport->ops->enable_ms(uport);
  990. }
  991. /*
  992. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  993. * - mask passed in arg for lines of interest
  994. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  995. * Caller should use TIOCGICOUNT to see which one it was
  996. *
  997. * FIXME: This wants extracting into a common all driver implementation
  998. * of TIOCMWAIT using tty_port.
  999. */
  1000. static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
  1001. {
  1002. struct uart_port *uport;
  1003. struct tty_port *port = &state->port;
  1004. DECLARE_WAITQUEUE(wait, current);
  1005. struct uart_icount cprev, cnow;
  1006. int ret;
  1007. /*
  1008. * note the counters on entry
  1009. */
  1010. uport = uart_port_ref(state);
  1011. if (!uport)
  1012. return -EIO;
  1013. spin_lock_irq(&uport->lock);
  1014. memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
  1015. uart_enable_ms(uport);
  1016. spin_unlock_irq(&uport->lock);
  1017. add_wait_queue(&port->delta_msr_wait, &wait);
  1018. for (;;) {
  1019. spin_lock_irq(&uport->lock);
  1020. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1021. spin_unlock_irq(&uport->lock);
  1022. set_current_state(TASK_INTERRUPTIBLE);
  1023. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1024. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1025. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1026. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1027. ret = 0;
  1028. break;
  1029. }
  1030. schedule();
  1031. /* see if a signal did it */
  1032. if (signal_pending(current)) {
  1033. ret = -ERESTARTSYS;
  1034. break;
  1035. }
  1036. cprev = cnow;
  1037. }
  1038. __set_current_state(TASK_RUNNING);
  1039. remove_wait_queue(&port->delta_msr_wait, &wait);
  1040. uart_port_deref(uport);
  1041. return ret;
  1042. }
  1043. /*
  1044. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1045. * Return: write counters to the user passed counter struct
  1046. * NB: both 1->0 and 0->1 transitions are counted except for
  1047. * RI where only 0->1 is counted.
  1048. */
  1049. static int uart_get_icount(struct tty_struct *tty,
  1050. struct serial_icounter_struct *icount)
  1051. {
  1052. struct uart_state *state = tty->driver_data;
  1053. struct uart_icount cnow;
  1054. struct uart_port *uport;
  1055. uport = uart_port_ref(state);
  1056. if (!uport)
  1057. return -EIO;
  1058. spin_lock_irq(&uport->lock);
  1059. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1060. spin_unlock_irq(&uport->lock);
  1061. uart_port_deref(uport);
  1062. icount->cts = cnow.cts;
  1063. icount->dsr = cnow.dsr;
  1064. icount->rng = cnow.rng;
  1065. icount->dcd = cnow.dcd;
  1066. icount->rx = cnow.rx;
  1067. icount->tx = cnow.tx;
  1068. icount->frame = cnow.frame;
  1069. icount->overrun = cnow.overrun;
  1070. icount->parity = cnow.parity;
  1071. icount->brk = cnow.brk;
  1072. icount->buf_overrun = cnow.buf_overrun;
  1073. return 0;
  1074. }
  1075. static int uart_get_rs485_config(struct uart_port *port,
  1076. struct serial_rs485 __user *rs485)
  1077. {
  1078. unsigned long flags;
  1079. struct serial_rs485 aux;
  1080. spin_lock_irqsave(&port->lock, flags);
  1081. aux = port->rs485;
  1082. spin_unlock_irqrestore(&port->lock, flags);
  1083. if (copy_to_user(rs485, &aux, sizeof(aux)))
  1084. return -EFAULT;
  1085. return 0;
  1086. }
  1087. static int uart_set_rs485_config(struct uart_port *port,
  1088. struct serial_rs485 __user *rs485_user)
  1089. {
  1090. struct serial_rs485 rs485;
  1091. int ret;
  1092. unsigned long flags;
  1093. if (!port->rs485_config)
  1094. return -ENOIOCTLCMD;
  1095. if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
  1096. return -EFAULT;
  1097. spin_lock_irqsave(&port->lock, flags);
  1098. ret = port->rs485_config(port, &rs485);
  1099. spin_unlock_irqrestore(&port->lock, flags);
  1100. if (ret)
  1101. return ret;
  1102. if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
  1103. return -EFAULT;
  1104. return 0;
  1105. }
  1106. /*
  1107. * Called via sys_ioctl. We can use spin_lock_irq() here.
  1108. */
  1109. static int
  1110. uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  1111. {
  1112. struct uart_state *state = tty->driver_data;
  1113. struct tty_port *port = &state->port;
  1114. struct uart_port *uport;
  1115. void __user *uarg = (void __user *)arg;
  1116. int ret = -ENOIOCTLCMD;
  1117. /*
  1118. * These ioctls don't rely on the hardware to be present.
  1119. */
  1120. switch (cmd) {
  1121. case TIOCGSERIAL:
  1122. ret = uart_get_info_user(port, uarg);
  1123. break;
  1124. case TIOCSSERIAL:
  1125. down_write(&tty->termios_rwsem);
  1126. ret = uart_set_info_user(tty, state, uarg);
  1127. up_write(&tty->termios_rwsem);
  1128. break;
  1129. case TIOCSERCONFIG:
  1130. down_write(&tty->termios_rwsem);
  1131. ret = uart_do_autoconfig(tty, state);
  1132. up_write(&tty->termios_rwsem);
  1133. break;
  1134. case TIOCSERGWILD: /* obsolete */
  1135. case TIOCSERSWILD: /* obsolete */
  1136. ret = 0;
  1137. break;
  1138. }
  1139. if (ret != -ENOIOCTLCMD)
  1140. goto out;
  1141. if (tty_io_error(tty)) {
  1142. ret = -EIO;
  1143. goto out;
  1144. }
  1145. /*
  1146. * The following should only be used when hardware is present.
  1147. */
  1148. switch (cmd) {
  1149. case TIOCMIWAIT:
  1150. ret = uart_wait_modem_status(state, arg);
  1151. break;
  1152. }
  1153. if (ret != -ENOIOCTLCMD)
  1154. goto out;
  1155. mutex_lock(&port->mutex);
  1156. uport = uart_port_check(state);
  1157. if (!uport || tty_io_error(tty)) {
  1158. ret = -EIO;
  1159. goto out_up;
  1160. }
  1161. /*
  1162. * All these rely on hardware being present and need to be
  1163. * protected against the tty being hung up.
  1164. */
  1165. switch (cmd) {
  1166. case TIOCSERGETLSR: /* Get line status register */
  1167. ret = uart_get_lsr_info(tty, state, uarg);
  1168. break;
  1169. case TIOCGRS485:
  1170. ret = uart_get_rs485_config(uport, uarg);
  1171. break;
  1172. case TIOCSRS485:
  1173. ret = uart_set_rs485_config(uport, uarg);
  1174. break;
  1175. default:
  1176. if (uport->ops->ioctl)
  1177. ret = uport->ops->ioctl(uport, cmd, arg);
  1178. break;
  1179. }
  1180. out_up:
  1181. mutex_unlock(&port->mutex);
  1182. out:
  1183. return ret;
  1184. }
  1185. static void uart_set_ldisc(struct tty_struct *tty)
  1186. {
  1187. struct uart_state *state = tty->driver_data;
  1188. struct uart_port *uport;
  1189. mutex_lock(&state->port.mutex);
  1190. uport = uart_port_check(state);
  1191. if (uport && uport->ops->set_ldisc)
  1192. uport->ops->set_ldisc(uport, &tty->termios);
  1193. mutex_unlock(&state->port.mutex);
  1194. }
  1195. static void uart_set_termios(struct tty_struct *tty,
  1196. struct ktermios *old_termios)
  1197. {
  1198. struct uart_state *state = tty->driver_data;
  1199. struct uart_port *uport;
  1200. unsigned int cflag = tty->termios.c_cflag;
  1201. unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
  1202. bool sw_changed = false;
  1203. mutex_lock(&state->port.mutex);
  1204. uport = uart_port_check(state);
  1205. if (!uport)
  1206. goto out;
  1207. /*
  1208. * Drivers doing software flow control also need to know
  1209. * about changes to these input settings.
  1210. */
  1211. if (uport->flags & UPF_SOFT_FLOW) {
  1212. iflag_mask |= IXANY|IXON|IXOFF;
  1213. sw_changed =
  1214. tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
  1215. tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
  1216. }
  1217. /*
  1218. * These are the bits that are used to setup various
  1219. * flags in the low level driver. We can ignore the Bfoo
  1220. * bits in c_cflag; c_[io]speed will always be set
  1221. * appropriately by set_termios() in tty_ioctl.c
  1222. */
  1223. if ((cflag ^ old_termios->c_cflag) == 0 &&
  1224. tty->termios.c_ospeed == old_termios->c_ospeed &&
  1225. tty->termios.c_ispeed == old_termios->c_ispeed &&
  1226. ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
  1227. !sw_changed) {
  1228. goto out;
  1229. }
  1230. uart_change_speed(tty, state, old_termios);
  1231. /* reload cflag from termios; port driver may have overriden flags */
  1232. cflag = tty->termios.c_cflag;
  1233. /* Handle transition to B0 status */
  1234. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  1235. uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
  1236. /* Handle transition away from B0 status */
  1237. else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  1238. unsigned int mask = TIOCM_DTR;
  1239. if (!(cflag & CRTSCTS) || !tty_throttled(tty))
  1240. mask |= TIOCM_RTS;
  1241. uart_set_mctrl(uport, mask);
  1242. }
  1243. out:
  1244. mutex_unlock(&state->port.mutex);
  1245. }
  1246. /*
  1247. * Calls to uart_close() are serialised via the tty_lock in
  1248. * drivers/tty/tty_io.c:tty_release()
  1249. * drivers/tty/tty_io.c:do_tty_hangup()
  1250. */
  1251. static void uart_close(struct tty_struct *tty, struct file *filp)
  1252. {
  1253. struct uart_state *state = tty->driver_data;
  1254. struct tty_port *port;
  1255. if (!state) {
  1256. struct uart_driver *drv = tty->driver->driver_state;
  1257. state = drv->state + tty->index;
  1258. port = &state->port;
  1259. spin_lock_irq(&port->lock);
  1260. --port->count;
  1261. spin_unlock_irq(&port->lock);
  1262. return;
  1263. }
  1264. port = &state->port;
  1265. pr_debug("uart_close(%d) called\n", tty->index);
  1266. tty_port_close(tty->port, tty, filp);
  1267. }
  1268. static void uart_tty_port_shutdown(struct tty_port *port)
  1269. {
  1270. struct uart_state *state = container_of(port, struct uart_state, port);
  1271. struct uart_port *uport = uart_port_check(state);
  1272. /*
  1273. * At this point, we stop accepting input. To do this, we
  1274. * disable the receive line status interrupts.
  1275. */
  1276. if (WARN(!uport, "detached port still initialized!\n"))
  1277. return;
  1278. spin_lock_irq(&uport->lock);
  1279. uport->ops->stop_rx(uport);
  1280. spin_unlock_irq(&uport->lock);
  1281. uart_port_shutdown(port);
  1282. /*
  1283. * It's possible for shutdown to be called after suspend if we get
  1284. * a DCD drop (hangup) at just the right time. Clear suspended bit so
  1285. * we don't try to resume a port that has been shutdown.
  1286. */
  1287. tty_port_set_suspended(port, 0);
  1288. uart_change_pm(state, UART_PM_STATE_OFF);
  1289. }
  1290. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1291. {
  1292. struct uart_state *state = tty->driver_data;
  1293. struct uart_port *port;
  1294. unsigned long char_time, expire;
  1295. port = uart_port_ref(state);
  1296. if (!port)
  1297. return;
  1298. if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
  1299. uart_port_deref(port);
  1300. return;
  1301. }
  1302. /*
  1303. * Set the check interval to be 1/5 of the estimated time to
  1304. * send a single character, and make it at least 1. The check
  1305. * interval should also be less than the timeout.
  1306. *
  1307. * Note: we have to use pretty tight timings here to satisfy
  1308. * the NIST-PCTS.
  1309. */
  1310. char_time = (port->timeout - HZ/50) / port->fifosize;
  1311. char_time = char_time / 5;
  1312. if (char_time == 0)
  1313. char_time = 1;
  1314. if (timeout && timeout < char_time)
  1315. char_time = timeout;
  1316. /*
  1317. * If the transmitter hasn't cleared in twice the approximate
  1318. * amount of time to send the entire FIFO, it probably won't
  1319. * ever clear. This assumes the UART isn't doing flow
  1320. * control, which is currently the case. Hence, if it ever
  1321. * takes longer than port->timeout, this is probably due to a
  1322. * UART bug of some kind. So, we clamp the timeout parameter at
  1323. * 2*port->timeout.
  1324. */
  1325. if (timeout == 0 || timeout > 2 * port->timeout)
  1326. timeout = 2 * port->timeout;
  1327. expire = jiffies + timeout;
  1328. pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
  1329. port->line, jiffies, expire);
  1330. /*
  1331. * Check whether the transmitter is empty every 'char_time'.
  1332. * 'timeout' / 'expire' give us the maximum amount of time
  1333. * we wait.
  1334. */
  1335. while (!port->ops->tx_empty(port)) {
  1336. msleep_interruptible(jiffies_to_msecs(char_time));
  1337. if (signal_pending(current))
  1338. break;
  1339. if (time_after(jiffies, expire))
  1340. break;
  1341. }
  1342. uart_port_deref(port);
  1343. }
  1344. /*
  1345. * Calls to uart_hangup() are serialised by the tty_lock in
  1346. * drivers/tty/tty_io.c:do_tty_hangup()
  1347. * This runs from a workqueue and can sleep for a _short_ time only.
  1348. */
  1349. static void uart_hangup(struct tty_struct *tty)
  1350. {
  1351. struct uart_state *state = tty->driver_data;
  1352. struct tty_port *port = &state->port;
  1353. struct uart_port *uport;
  1354. unsigned long flags;
  1355. pr_debug("uart_hangup(%d)\n", tty->index);
  1356. mutex_lock(&port->mutex);
  1357. uport = uart_port_check(state);
  1358. WARN(!uport, "hangup of detached port!\n");
  1359. if (tty_port_active(port)) {
  1360. uart_flush_buffer(tty);
  1361. uart_shutdown(tty, state);
  1362. spin_lock_irqsave(&port->lock, flags);
  1363. port->count = 0;
  1364. spin_unlock_irqrestore(&port->lock, flags);
  1365. tty_port_set_active(port, 0);
  1366. tty_port_tty_set(port, NULL);
  1367. if (uport && !uart_console(uport))
  1368. uart_change_pm(state, UART_PM_STATE_OFF);
  1369. wake_up_interruptible(&port->open_wait);
  1370. wake_up_interruptible(&port->delta_msr_wait);
  1371. }
  1372. mutex_unlock(&port->mutex);
  1373. }
  1374. /* uport == NULL if uart_port has already been removed */
  1375. static void uart_port_shutdown(struct tty_port *port)
  1376. {
  1377. struct uart_state *state = container_of(port, struct uart_state, port);
  1378. struct uart_port *uport = uart_port_check(state);
  1379. /*
  1380. * clear delta_msr_wait queue to avoid mem leaks: we may free
  1381. * the irq here so the queue might never be woken up. Note
  1382. * that we won't end up waiting on delta_msr_wait again since
  1383. * any outstanding file descriptors should be pointing at
  1384. * hung_up_tty_fops now.
  1385. */
  1386. wake_up_interruptible(&port->delta_msr_wait);
  1387. /*
  1388. * Free the IRQ and disable the port.
  1389. */
  1390. if (uport)
  1391. uport->ops->shutdown(uport);
  1392. /*
  1393. * Ensure that the IRQ handler isn't running on another CPU.
  1394. */
  1395. if (uport)
  1396. synchronize_irq(uport->irq);
  1397. }
  1398. static int uart_carrier_raised(struct tty_port *port)
  1399. {
  1400. struct uart_state *state = container_of(port, struct uart_state, port);
  1401. struct uart_port *uport;
  1402. int mctrl;
  1403. uport = uart_port_ref(state);
  1404. /*
  1405. * Should never observe uport == NULL since checks for hangup should
  1406. * abort the tty_port_block_til_ready() loop before checking for carrier
  1407. * raised -- but report carrier raised if it does anyway so open will
  1408. * continue and not sleep
  1409. */
  1410. if (WARN_ON(!uport))
  1411. return 1;
  1412. spin_lock_irq(&uport->lock);
  1413. uart_enable_ms(uport);
  1414. mctrl = uport->ops->get_mctrl(uport);
  1415. spin_unlock_irq(&uport->lock);
  1416. uart_port_deref(uport);
  1417. if (mctrl & TIOCM_CAR)
  1418. return 1;
  1419. return 0;
  1420. }
  1421. static void uart_dtr_rts(struct tty_port *port, int onoff)
  1422. {
  1423. struct uart_state *state = container_of(port, struct uart_state, port);
  1424. struct uart_port *uport;
  1425. uport = uart_port_ref(state);
  1426. if (!uport)
  1427. return;
  1428. if (onoff)
  1429. uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1430. else
  1431. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1432. uart_port_deref(uport);
  1433. }
  1434. /*
  1435. * Calls to uart_open are serialised by the tty_lock in
  1436. * drivers/tty/tty_io.c:tty_open()
  1437. * Note that if this fails, then uart_close() _will_ be called.
  1438. *
  1439. * In time, we want to scrap the "opening nonpresent ports"
  1440. * behaviour and implement an alternative way for setserial
  1441. * to set base addresses/ports/types. This will allow us to
  1442. * get rid of a certain amount of extra tests.
  1443. */
  1444. static int uart_open(struct tty_struct *tty, struct file *filp)
  1445. {
  1446. struct uart_driver *drv = tty->driver->driver_state;
  1447. int retval, line = tty->index;
  1448. struct uart_state *state = drv->state + line;
  1449. tty->driver_data = state;
  1450. retval = tty_port_open(&state->port, tty, filp);
  1451. if (retval > 0)
  1452. retval = 0;
  1453. return retval;
  1454. }
  1455. static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
  1456. {
  1457. struct uart_state *state = container_of(port, struct uart_state, port);
  1458. struct uart_port *uport;
  1459. uport = uart_port_check(state);
  1460. if (!uport || uport->flags & UPF_DEAD)
  1461. return -ENXIO;
  1462. port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1463. /*
  1464. * Start up the serial port.
  1465. */
  1466. return uart_startup(tty, state, 0);
  1467. }
  1468. static const char *uart_type(struct uart_port *port)
  1469. {
  1470. const char *str = NULL;
  1471. if (port->ops->type)
  1472. str = port->ops->type(port);
  1473. if (!str)
  1474. str = "unknown";
  1475. return str;
  1476. }
  1477. #ifdef CONFIG_PROC_FS
  1478. static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
  1479. {
  1480. struct uart_state *state = drv->state + i;
  1481. struct tty_port *port = &state->port;
  1482. enum uart_pm_state pm_state;
  1483. struct uart_port *uport;
  1484. char stat_buf[32];
  1485. unsigned int status;
  1486. int mmio;
  1487. mutex_lock(&port->mutex);
  1488. uport = uart_port_check(state);
  1489. if (!uport)
  1490. goto out;
  1491. mmio = uport->iotype >= UPIO_MEM;
  1492. seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
  1493. uport->line, uart_type(uport),
  1494. mmio ? "mmio:0x" : "port:",
  1495. mmio ? (unsigned long long)uport->mapbase
  1496. : (unsigned long long)uport->iobase,
  1497. uport->irq);
  1498. if (uport->type == PORT_UNKNOWN) {
  1499. seq_putc(m, '\n');
  1500. goto out;
  1501. }
  1502. if (capable(CAP_SYS_ADMIN)) {
  1503. pm_state = state->pm_state;
  1504. if (pm_state != UART_PM_STATE_ON)
  1505. uart_change_pm(state, UART_PM_STATE_ON);
  1506. spin_lock_irq(&uport->lock);
  1507. status = uport->ops->get_mctrl(uport);
  1508. spin_unlock_irq(&uport->lock);
  1509. if (pm_state != UART_PM_STATE_ON)
  1510. uart_change_pm(state, pm_state);
  1511. seq_printf(m, " tx:%d rx:%d",
  1512. uport->icount.tx, uport->icount.rx);
  1513. if (uport->icount.frame)
  1514. seq_printf(m, " fe:%d", uport->icount.frame);
  1515. if (uport->icount.parity)
  1516. seq_printf(m, " pe:%d", uport->icount.parity);
  1517. if (uport->icount.brk)
  1518. seq_printf(m, " brk:%d", uport->icount.brk);
  1519. if (uport->icount.overrun)
  1520. seq_printf(m, " oe:%d", uport->icount.overrun);
  1521. #define INFOBIT(bit, str) \
  1522. if (uport->mctrl & (bit)) \
  1523. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1524. strlen(stat_buf) - 2)
  1525. #define STATBIT(bit, str) \
  1526. if (status & (bit)) \
  1527. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1528. strlen(stat_buf) - 2)
  1529. stat_buf[0] = '\0';
  1530. stat_buf[1] = '\0';
  1531. INFOBIT(TIOCM_RTS, "|RTS");
  1532. STATBIT(TIOCM_CTS, "|CTS");
  1533. INFOBIT(TIOCM_DTR, "|DTR");
  1534. STATBIT(TIOCM_DSR, "|DSR");
  1535. STATBIT(TIOCM_CAR, "|CD");
  1536. STATBIT(TIOCM_RNG, "|RI");
  1537. if (stat_buf[0])
  1538. stat_buf[0] = ' ';
  1539. seq_puts(m, stat_buf);
  1540. }
  1541. seq_putc(m, '\n');
  1542. #undef STATBIT
  1543. #undef INFOBIT
  1544. out:
  1545. mutex_unlock(&port->mutex);
  1546. }
  1547. static int uart_proc_show(struct seq_file *m, void *v)
  1548. {
  1549. struct tty_driver *ttydrv = m->private;
  1550. struct uart_driver *drv = ttydrv->driver_state;
  1551. int i;
  1552. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
  1553. for (i = 0; i < drv->nr; i++)
  1554. uart_line_info(m, drv, i);
  1555. return 0;
  1556. }
  1557. static int uart_proc_open(struct inode *inode, struct file *file)
  1558. {
  1559. return single_open(file, uart_proc_show, PDE_DATA(inode));
  1560. }
  1561. static const struct file_operations uart_proc_fops = {
  1562. .owner = THIS_MODULE,
  1563. .open = uart_proc_open,
  1564. .read = seq_read,
  1565. .llseek = seq_lseek,
  1566. .release = single_release,
  1567. };
  1568. #endif
  1569. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  1570. /**
  1571. * uart_console_write - write a console message to a serial port
  1572. * @port: the port to write the message
  1573. * @s: array of characters
  1574. * @count: number of characters in string to write
  1575. * @putchar: function to write character to port
  1576. */
  1577. void uart_console_write(struct uart_port *port, const char *s,
  1578. unsigned int count,
  1579. void (*putchar)(struct uart_port *, int))
  1580. {
  1581. unsigned int i;
  1582. for (i = 0; i < count; i++, s++) {
  1583. if (*s == '\n')
  1584. putchar(port, '\r');
  1585. putchar(port, *s);
  1586. }
  1587. }
  1588. EXPORT_SYMBOL_GPL(uart_console_write);
  1589. /*
  1590. * Check whether an invalid uart number has been specified, and
  1591. * if so, search for the first available port that does have
  1592. * console support.
  1593. */
  1594. struct uart_port * __init
  1595. uart_get_console(struct uart_port *ports, int nr, struct console *co)
  1596. {
  1597. int idx = co->index;
  1598. if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
  1599. ports[idx].membase == NULL))
  1600. for (idx = 0; idx < nr; idx++)
  1601. if (ports[idx].iobase != 0 ||
  1602. ports[idx].membase != NULL)
  1603. break;
  1604. co->index = idx;
  1605. return ports + idx;
  1606. }
  1607. /**
  1608. * uart_parse_earlycon - Parse earlycon options
  1609. * @p: ptr to 2nd field (ie., just beyond '<name>,')
  1610. * @iotype: ptr for decoded iotype (out)
  1611. * @addr: ptr for decoded mapbase/iobase (out)
  1612. * @options: ptr for <options> field; NULL if not present (out)
  1613. *
  1614. * Decodes earlycon kernel command line parameters of the form
  1615. * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1616. * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1617. *
  1618. * The optional form
  1619. * earlycon=<name>,0x<addr>,<options>
  1620. * console=<name>,0x<addr>,<options>
  1621. * is also accepted; the returned @iotype will be UPIO_MEM.
  1622. *
  1623. * Returns 0 on success or -EINVAL on failure
  1624. */
  1625. int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
  1626. char **options)
  1627. {
  1628. if (strncmp(p, "mmio,", 5) == 0) {
  1629. *iotype = UPIO_MEM;
  1630. p += 5;
  1631. } else if (strncmp(p, "mmio16,", 7) == 0) {
  1632. *iotype = UPIO_MEM16;
  1633. p += 7;
  1634. } else if (strncmp(p, "mmio32,", 7) == 0) {
  1635. *iotype = UPIO_MEM32;
  1636. p += 7;
  1637. } else if (strncmp(p, "mmio32be,", 9) == 0) {
  1638. *iotype = UPIO_MEM32BE;
  1639. p += 9;
  1640. } else if (strncmp(p, "mmio32native,", 13) == 0) {
  1641. *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
  1642. UPIO_MEM32BE : UPIO_MEM32;
  1643. p += 13;
  1644. } else if (strncmp(p, "io,", 3) == 0) {
  1645. *iotype = UPIO_PORT;
  1646. p += 3;
  1647. } else if (strncmp(p, "0x", 2) == 0) {
  1648. *iotype = UPIO_MEM;
  1649. } else {
  1650. return -EINVAL;
  1651. }
  1652. /*
  1653. * Before you replace it with kstrtoull(), think about options separator
  1654. * (',') it will not tolerate
  1655. */
  1656. *addr = simple_strtoull(p, NULL, 0);
  1657. p = strchr(p, ',');
  1658. if (p)
  1659. p++;
  1660. *options = p;
  1661. return 0;
  1662. }
  1663. EXPORT_SYMBOL_GPL(uart_parse_earlycon);
  1664. /**
  1665. * uart_parse_options - Parse serial port baud/parity/bits/flow control.
  1666. * @options: pointer to option string
  1667. * @baud: pointer to an 'int' variable for the baud rate.
  1668. * @parity: pointer to an 'int' variable for the parity.
  1669. * @bits: pointer to an 'int' variable for the number of data bits.
  1670. * @flow: pointer to an 'int' variable for the flow control character.
  1671. *
  1672. * uart_parse_options decodes a string containing the serial console
  1673. * options. The format of the string is <baud><parity><bits><flow>,
  1674. * eg: 115200n8r
  1675. */
  1676. void
  1677. uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
  1678. {
  1679. char *s = options;
  1680. *baud = simple_strtoul(s, NULL, 10);
  1681. while (*s >= '0' && *s <= '9')
  1682. s++;
  1683. if (*s)
  1684. *parity = *s++;
  1685. if (*s)
  1686. *bits = *s++ - '0';
  1687. if (*s)
  1688. *flow = *s;
  1689. }
  1690. EXPORT_SYMBOL_GPL(uart_parse_options);
  1691. /**
  1692. * uart_set_options - setup the serial console parameters
  1693. * @port: pointer to the serial ports uart_port structure
  1694. * @co: console pointer
  1695. * @baud: baud rate
  1696. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1697. * @bits: number of data bits
  1698. * @flow: flow control character - 'r' (rts)
  1699. */
  1700. int
  1701. uart_set_options(struct uart_port *port, struct console *co,
  1702. int baud, int parity, int bits, int flow)
  1703. {
  1704. struct ktermios termios;
  1705. static struct ktermios dummy;
  1706. /*
  1707. * Ensure that the serial console lock is initialised
  1708. * early.
  1709. * If this port is a console, then the spinlock is already
  1710. * initialised.
  1711. */
  1712. if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
  1713. spin_lock_init(&port->lock);
  1714. lockdep_set_class(&port->lock, &port_lock_key);
  1715. }
  1716. memset(&termios, 0, sizeof(struct ktermios));
  1717. termios.c_cflag |= CREAD | HUPCL | CLOCAL;
  1718. tty_termios_encode_baud_rate(&termios, baud, baud);
  1719. if (bits == 7)
  1720. termios.c_cflag |= CS7;
  1721. else
  1722. termios.c_cflag |= CS8;
  1723. switch (parity) {
  1724. case 'o': case 'O':
  1725. termios.c_cflag |= PARODD;
  1726. /*fall through*/
  1727. case 'e': case 'E':
  1728. termios.c_cflag |= PARENB;
  1729. break;
  1730. }
  1731. if (flow == 'r')
  1732. termios.c_cflag |= CRTSCTS;
  1733. /*
  1734. * some uarts on other side don't support no flow control.
  1735. * So we set * DTR in host uart to make them happy
  1736. */
  1737. port->mctrl |= TIOCM_DTR;
  1738. port->ops->set_termios(port, &termios, &dummy);
  1739. /*
  1740. * Allow the setting of the UART parameters with a NULL console
  1741. * too:
  1742. */
  1743. if (co)
  1744. co->cflag = termios.c_cflag;
  1745. return 0;
  1746. }
  1747. EXPORT_SYMBOL_GPL(uart_set_options);
  1748. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1749. /**
  1750. * uart_change_pm - set power state of the port
  1751. *
  1752. * @state: port descriptor
  1753. * @pm_state: new state
  1754. *
  1755. * Locking: port->mutex has to be held
  1756. */
  1757. static void uart_change_pm(struct uart_state *state,
  1758. enum uart_pm_state pm_state)
  1759. {
  1760. struct uart_port *port = uart_port_check(state);
  1761. if (state->pm_state != pm_state) {
  1762. if (port && port->ops->pm)
  1763. port->ops->pm(port, pm_state, state->pm_state);
  1764. state->pm_state = pm_state;
  1765. }
  1766. }
  1767. struct uart_match {
  1768. struct uart_port *port;
  1769. struct uart_driver *driver;
  1770. };
  1771. static int serial_match_port(struct device *dev, void *data)
  1772. {
  1773. struct uart_match *match = data;
  1774. struct tty_driver *tty_drv = match->driver->tty_driver;
  1775. dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
  1776. match->port->line;
  1777. return dev->devt == devt; /* Actually, only one tty per port */
  1778. }
  1779. int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
  1780. {
  1781. struct uart_state *state = drv->state + uport->line;
  1782. struct tty_port *port = &state->port;
  1783. struct device *tty_dev;
  1784. struct uart_match match = {uport, drv};
  1785. mutex_lock(&port->mutex);
  1786. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1787. if (tty_dev && device_may_wakeup(tty_dev)) {
  1788. if (!enable_irq_wake(uport->irq))
  1789. uport->irq_wake = 1;
  1790. put_device(tty_dev);
  1791. mutex_unlock(&port->mutex);
  1792. return 0;
  1793. }
  1794. put_device(tty_dev);
  1795. /* Nothing to do if the console is not suspending */
  1796. if (!console_suspend_enabled && uart_console(uport))
  1797. goto unlock;
  1798. uport->suspended = 1;
  1799. if (tty_port_initialized(port)) {
  1800. const struct uart_ops *ops = uport->ops;
  1801. int tries;
  1802. tty_port_set_suspended(port, 1);
  1803. tty_port_set_initialized(port, 0);
  1804. spin_lock_irq(&uport->lock);
  1805. ops->stop_tx(uport);
  1806. ops->set_mctrl(uport, 0);
  1807. ops->stop_rx(uport);
  1808. spin_unlock_irq(&uport->lock);
  1809. /*
  1810. * Wait for the transmitter to empty.
  1811. */
  1812. for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
  1813. msleep(10);
  1814. if (!tries)
  1815. dev_err(uport->dev, "%s: Unable to drain transmitter\n",
  1816. uport->name);
  1817. ops->shutdown(uport);
  1818. }
  1819. /*
  1820. * Disable the console device before suspending.
  1821. */
  1822. if (uart_console(uport))
  1823. console_stop(uport->cons);
  1824. uart_change_pm(state, UART_PM_STATE_OFF);
  1825. unlock:
  1826. mutex_unlock(&port->mutex);
  1827. return 0;
  1828. }
  1829. int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
  1830. {
  1831. struct uart_state *state = drv->state + uport->line;
  1832. struct tty_port *port = &state->port;
  1833. struct device *tty_dev;
  1834. struct uart_match match = {uport, drv};
  1835. struct ktermios termios;
  1836. mutex_lock(&port->mutex);
  1837. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1838. if (!uport->suspended && device_may_wakeup(tty_dev)) {
  1839. if (uport->irq_wake) {
  1840. disable_irq_wake(uport->irq);
  1841. uport->irq_wake = 0;
  1842. }
  1843. put_device(tty_dev);
  1844. mutex_unlock(&port->mutex);
  1845. return 0;
  1846. }
  1847. put_device(tty_dev);
  1848. uport->suspended = 0;
  1849. /*
  1850. * Re-enable the console device after suspending.
  1851. */
  1852. if (uart_console(uport)) {
  1853. /*
  1854. * First try to use the console cflag setting.
  1855. */
  1856. memset(&termios, 0, sizeof(struct ktermios));
  1857. termios.c_cflag = uport->cons->cflag;
  1858. /*
  1859. * If that's unset, use the tty termios setting.
  1860. */
  1861. if (port->tty && termios.c_cflag == 0)
  1862. termios = port->tty->termios;
  1863. if (console_suspend_enabled)
  1864. uart_change_pm(state, UART_PM_STATE_ON);
  1865. uport->ops->set_termios(uport, &termios, NULL);
  1866. if (console_suspend_enabled)
  1867. console_start(uport->cons);
  1868. }
  1869. if (tty_port_suspended(port)) {
  1870. const struct uart_ops *ops = uport->ops;
  1871. int ret;
  1872. uart_change_pm(state, UART_PM_STATE_ON);
  1873. spin_lock_irq(&uport->lock);
  1874. ops->set_mctrl(uport, 0);
  1875. spin_unlock_irq(&uport->lock);
  1876. if (console_suspend_enabled || !uart_console(uport)) {
  1877. /* Protected by port mutex for now */
  1878. struct tty_struct *tty = port->tty;
  1879. ret = ops->startup(uport);
  1880. if (ret == 0) {
  1881. if (tty)
  1882. uart_change_speed(tty, state, NULL);
  1883. spin_lock_irq(&uport->lock);
  1884. ops->set_mctrl(uport, uport->mctrl);
  1885. ops->start_tx(uport);
  1886. spin_unlock_irq(&uport->lock);
  1887. tty_port_set_initialized(port, 1);
  1888. } else {
  1889. /*
  1890. * Failed to resume - maybe hardware went away?
  1891. * Clear the "initialized" flag so we won't try
  1892. * to call the low level drivers shutdown method.
  1893. */
  1894. uart_shutdown(tty, state);
  1895. }
  1896. }
  1897. tty_port_set_suspended(port, 0);
  1898. }
  1899. mutex_unlock(&port->mutex);
  1900. return 0;
  1901. }
  1902. static inline void
  1903. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  1904. {
  1905. char address[64];
  1906. switch (port->iotype) {
  1907. case UPIO_PORT:
  1908. snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
  1909. break;
  1910. case UPIO_HUB6:
  1911. snprintf(address, sizeof(address),
  1912. "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
  1913. break;
  1914. case UPIO_MEM:
  1915. case UPIO_MEM16:
  1916. case UPIO_MEM32:
  1917. case UPIO_MEM32BE:
  1918. case UPIO_AU:
  1919. case UPIO_TSI:
  1920. snprintf(address, sizeof(address),
  1921. "MMIO 0x%llx", (unsigned long long)port->mapbase);
  1922. break;
  1923. default:
  1924. strlcpy(address, "*unknown*", sizeof(address));
  1925. break;
  1926. }
  1927. pr_info("%s%s%s at %s (irq = %d, base_baud = %d) is a %s\n",
  1928. port->dev ? dev_name(port->dev) : "",
  1929. port->dev ? ": " : "",
  1930. port->name,
  1931. address, port->irq, port->uartclk / 16, uart_type(port));
  1932. }
  1933. static void
  1934. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  1935. struct uart_port *port)
  1936. {
  1937. unsigned int flags;
  1938. /*
  1939. * If there isn't a port here, don't do anything further.
  1940. */
  1941. if (!port->iobase && !port->mapbase && !port->membase)
  1942. return;
  1943. /*
  1944. * Now do the auto configuration stuff. Note that config_port
  1945. * is expected to claim the resources and map the port for us.
  1946. */
  1947. flags = 0;
  1948. if (port->flags & UPF_AUTO_IRQ)
  1949. flags |= UART_CONFIG_IRQ;
  1950. if (port->flags & UPF_BOOT_AUTOCONF) {
  1951. if (!(port->flags & UPF_FIXED_TYPE)) {
  1952. port->type = PORT_UNKNOWN;
  1953. flags |= UART_CONFIG_TYPE;
  1954. }
  1955. port->ops->config_port(port, flags);
  1956. }
  1957. if (port->type != PORT_UNKNOWN) {
  1958. unsigned long flags;
  1959. uart_report_port(drv, port);
  1960. /* Power up port for set_mctrl() */
  1961. uart_change_pm(state, UART_PM_STATE_ON);
  1962. /*
  1963. * Ensure that the modem control lines are de-activated.
  1964. * keep the DTR setting that is set in uart_set_options()
  1965. * We probably don't need a spinlock around this, but
  1966. */
  1967. spin_lock_irqsave(&port->lock, flags);
  1968. port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
  1969. spin_unlock_irqrestore(&port->lock, flags);
  1970. /*
  1971. * If this driver supports console, and it hasn't been
  1972. * successfully registered yet, try to re-register it.
  1973. * It may be that the port was not available.
  1974. */
  1975. if (port->cons && !(port->cons->flags & CON_ENABLED))
  1976. register_console(port->cons);
  1977. /*
  1978. * Power down all ports by default, except the
  1979. * console if we have one.
  1980. */
  1981. if (!uart_console(port))
  1982. uart_change_pm(state, UART_PM_STATE_OFF);
  1983. }
  1984. }
  1985. #ifdef CONFIG_CONSOLE_POLL
  1986. static int uart_poll_init(struct tty_driver *driver, int line, char *options)
  1987. {
  1988. struct uart_driver *drv = driver->driver_state;
  1989. struct uart_state *state = drv->state + line;
  1990. struct tty_port *tport;
  1991. struct uart_port *port;
  1992. int baud = 9600;
  1993. int bits = 8;
  1994. int parity = 'n';
  1995. int flow = 'n';
  1996. int ret = 0;
  1997. tport = &state->port;
  1998. mutex_lock(&tport->mutex);
  1999. port = uart_port_check(state);
  2000. if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
  2001. ret = -1;
  2002. goto out;
  2003. }
  2004. if (port->ops->poll_init) {
  2005. /*
  2006. * We don't set initialized as we only initialized the hw,
  2007. * e.g. state->xmit is still uninitialized.
  2008. */
  2009. if (!tty_port_initialized(tport))
  2010. ret = port->ops->poll_init(port);
  2011. }
  2012. if (!ret && options) {
  2013. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2014. ret = uart_set_options(port, NULL, baud, parity, bits, flow);
  2015. }
  2016. out:
  2017. mutex_unlock(&tport->mutex);
  2018. return ret;
  2019. }
  2020. static int uart_poll_get_char(struct tty_driver *driver, int line)
  2021. {
  2022. struct uart_driver *drv = driver->driver_state;
  2023. struct uart_state *state = drv->state + line;
  2024. struct uart_port *port;
  2025. int ret = -1;
  2026. port = uart_port_ref(state);
  2027. if (port) {
  2028. ret = port->ops->poll_get_char(port);
  2029. uart_port_deref(port);
  2030. }
  2031. return ret;
  2032. }
  2033. static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
  2034. {
  2035. struct uart_driver *drv = driver->driver_state;
  2036. struct uart_state *state = drv->state + line;
  2037. struct uart_port *port;
  2038. port = uart_port_ref(state);
  2039. if (!port)
  2040. return;
  2041. if (ch == '\n')
  2042. port->ops->poll_put_char(port, '\r');
  2043. port->ops->poll_put_char(port, ch);
  2044. uart_port_deref(port);
  2045. }
  2046. #endif
  2047. static const struct tty_operations uart_ops = {
  2048. .open = uart_open,
  2049. .close = uart_close,
  2050. .write = uart_write,
  2051. .put_char = uart_put_char,
  2052. .flush_chars = uart_flush_chars,
  2053. .write_room = uart_write_room,
  2054. .chars_in_buffer= uart_chars_in_buffer,
  2055. .flush_buffer = uart_flush_buffer,
  2056. .ioctl = uart_ioctl,
  2057. .throttle = uart_throttle,
  2058. .unthrottle = uart_unthrottle,
  2059. .send_xchar = uart_send_xchar,
  2060. .set_termios = uart_set_termios,
  2061. .set_ldisc = uart_set_ldisc,
  2062. .stop = uart_stop,
  2063. .start = uart_start,
  2064. .hangup = uart_hangup,
  2065. .break_ctl = uart_break_ctl,
  2066. .wait_until_sent= uart_wait_until_sent,
  2067. #ifdef CONFIG_PROC_FS
  2068. .proc_fops = &uart_proc_fops,
  2069. #endif
  2070. .tiocmget = uart_tiocmget,
  2071. .tiocmset = uart_tiocmset,
  2072. .get_icount = uart_get_icount,
  2073. #ifdef CONFIG_CONSOLE_POLL
  2074. .poll_init = uart_poll_init,
  2075. .poll_get_char = uart_poll_get_char,
  2076. .poll_put_char = uart_poll_put_char,
  2077. #endif
  2078. };
  2079. static const struct tty_port_operations uart_port_ops = {
  2080. .carrier_raised = uart_carrier_raised,
  2081. .dtr_rts = uart_dtr_rts,
  2082. .activate = uart_port_activate,
  2083. .shutdown = uart_tty_port_shutdown,
  2084. };
  2085. /**
  2086. * uart_register_driver - register a driver with the uart core layer
  2087. * @drv: low level driver structure
  2088. *
  2089. * Register a uart driver with the core driver. We in turn register
  2090. * with the tty layer, and initialise the core driver per-port state.
  2091. *
  2092. * We have a proc file in /proc/tty/driver which is named after the
  2093. * normal driver.
  2094. *
  2095. * drv->port should be NULL, and the per-port structures should be
  2096. * registered using uart_add_one_port after this call has succeeded.
  2097. */
  2098. int uart_register_driver(struct uart_driver *drv)
  2099. {
  2100. struct tty_driver *normal;
  2101. int i, retval;
  2102. BUG_ON(drv->state);
  2103. /*
  2104. * Maybe we should be using a slab cache for this, especially if
  2105. * we have a large number of ports to handle.
  2106. */
  2107. drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
  2108. if (!drv->state)
  2109. goto out;
  2110. normal = alloc_tty_driver(drv->nr);
  2111. if (!normal)
  2112. goto out_kfree;
  2113. drv->tty_driver = normal;
  2114. normal->driver_name = drv->driver_name;
  2115. normal->name = drv->dev_name;
  2116. normal->major = drv->major;
  2117. normal->minor_start = drv->minor;
  2118. normal->type = TTY_DRIVER_TYPE_SERIAL;
  2119. normal->subtype = SERIAL_TYPE_NORMAL;
  2120. normal->init_termios = tty_std_termios;
  2121. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2122. normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
  2123. normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2124. normal->driver_state = drv;
  2125. tty_set_operations(normal, &uart_ops);
  2126. /*
  2127. * Initialise the UART state(s).
  2128. */
  2129. for (i = 0; i < drv->nr; i++) {
  2130. struct uart_state *state = drv->state + i;
  2131. struct tty_port *port = &state->port;
  2132. tty_port_init(port);
  2133. port->ops = &uart_port_ops;
  2134. }
  2135. retval = tty_register_driver(normal);
  2136. if (retval >= 0)
  2137. return retval;
  2138. for (i = 0; i < drv->nr; i++)
  2139. tty_port_destroy(&drv->state[i].port);
  2140. put_tty_driver(normal);
  2141. out_kfree:
  2142. kfree(drv->state);
  2143. out:
  2144. return -ENOMEM;
  2145. }
  2146. /**
  2147. * uart_unregister_driver - remove a driver from the uart core layer
  2148. * @drv: low level driver structure
  2149. *
  2150. * Remove all references to a driver from the core driver. The low
  2151. * level driver must have removed all its ports via the
  2152. * uart_remove_one_port() if it registered them with uart_add_one_port().
  2153. * (ie, drv->port == NULL)
  2154. */
  2155. void uart_unregister_driver(struct uart_driver *drv)
  2156. {
  2157. struct tty_driver *p = drv->tty_driver;
  2158. unsigned int i;
  2159. tty_unregister_driver(p);
  2160. put_tty_driver(p);
  2161. for (i = 0; i < drv->nr; i++)
  2162. tty_port_destroy(&drv->state[i].port);
  2163. kfree(drv->state);
  2164. drv->state = NULL;
  2165. drv->tty_driver = NULL;
  2166. }
  2167. struct tty_driver *uart_console_device(struct console *co, int *index)
  2168. {
  2169. struct uart_driver *p = co->data;
  2170. *index = co->index;
  2171. return p->tty_driver;
  2172. }
  2173. static ssize_t uart_get_attr_uartclk(struct device *dev,
  2174. struct device_attribute *attr, char *buf)
  2175. {
  2176. struct serial_struct tmp;
  2177. struct tty_port *port = dev_get_drvdata(dev);
  2178. uart_get_info(port, &tmp);
  2179. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
  2180. }
  2181. static ssize_t uart_get_attr_type(struct device *dev,
  2182. struct device_attribute *attr, char *buf)
  2183. {
  2184. struct serial_struct tmp;
  2185. struct tty_port *port = dev_get_drvdata(dev);
  2186. uart_get_info(port, &tmp);
  2187. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
  2188. }
  2189. static ssize_t uart_get_attr_line(struct device *dev,
  2190. struct device_attribute *attr, char *buf)
  2191. {
  2192. struct serial_struct tmp;
  2193. struct tty_port *port = dev_get_drvdata(dev);
  2194. uart_get_info(port, &tmp);
  2195. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
  2196. }
  2197. static ssize_t uart_get_attr_port(struct device *dev,
  2198. struct device_attribute *attr, char *buf)
  2199. {
  2200. struct serial_struct tmp;
  2201. struct tty_port *port = dev_get_drvdata(dev);
  2202. unsigned long ioaddr;
  2203. uart_get_info(port, &tmp);
  2204. ioaddr = tmp.port;
  2205. if (HIGH_BITS_OFFSET)
  2206. ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
  2207. return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
  2208. }
  2209. static ssize_t uart_get_attr_irq(struct device *dev,
  2210. struct device_attribute *attr, char *buf)
  2211. {
  2212. struct serial_struct tmp;
  2213. struct tty_port *port = dev_get_drvdata(dev);
  2214. uart_get_info(port, &tmp);
  2215. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
  2216. }
  2217. static ssize_t uart_get_attr_flags(struct device *dev,
  2218. struct device_attribute *attr, char *buf)
  2219. {
  2220. struct serial_struct tmp;
  2221. struct tty_port *port = dev_get_drvdata(dev);
  2222. uart_get_info(port, &tmp);
  2223. return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
  2224. }
  2225. static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
  2226. struct device_attribute *attr, char *buf)
  2227. {
  2228. struct serial_struct tmp;
  2229. struct tty_port *port = dev_get_drvdata(dev);
  2230. uart_get_info(port, &tmp);
  2231. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
  2232. }
  2233. static ssize_t uart_get_attr_close_delay(struct device *dev,
  2234. struct device_attribute *attr, char *buf)
  2235. {
  2236. struct serial_struct tmp;
  2237. struct tty_port *port = dev_get_drvdata(dev);
  2238. uart_get_info(port, &tmp);
  2239. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
  2240. }
  2241. static ssize_t uart_get_attr_closing_wait(struct device *dev,
  2242. struct device_attribute *attr, char *buf)
  2243. {
  2244. struct serial_struct tmp;
  2245. struct tty_port *port = dev_get_drvdata(dev);
  2246. uart_get_info(port, &tmp);
  2247. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
  2248. }
  2249. static ssize_t uart_get_attr_custom_divisor(struct device *dev,
  2250. struct device_attribute *attr, char *buf)
  2251. {
  2252. struct serial_struct tmp;
  2253. struct tty_port *port = dev_get_drvdata(dev);
  2254. uart_get_info(port, &tmp);
  2255. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
  2256. }
  2257. static ssize_t uart_get_attr_io_type(struct device *dev,
  2258. struct device_attribute *attr, char *buf)
  2259. {
  2260. struct serial_struct tmp;
  2261. struct tty_port *port = dev_get_drvdata(dev);
  2262. uart_get_info(port, &tmp);
  2263. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
  2264. }
  2265. static ssize_t uart_get_attr_iomem_base(struct device *dev,
  2266. struct device_attribute *attr, char *buf)
  2267. {
  2268. struct serial_struct tmp;
  2269. struct tty_port *port = dev_get_drvdata(dev);
  2270. uart_get_info(port, &tmp);
  2271. return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
  2272. }
  2273. static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
  2274. struct device_attribute *attr, char *buf)
  2275. {
  2276. struct serial_struct tmp;
  2277. struct tty_port *port = dev_get_drvdata(dev);
  2278. uart_get_info(port, &tmp);
  2279. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
  2280. }
  2281. static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
  2282. static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
  2283. static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
  2284. static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
  2285. static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
  2286. static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
  2287. static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
  2288. static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
  2289. static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
  2290. static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
  2291. static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
  2292. static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
  2293. static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
  2294. static struct attribute *tty_dev_attrs[] = {
  2295. &dev_attr_type.attr,
  2296. &dev_attr_line.attr,
  2297. &dev_attr_port.attr,
  2298. &dev_attr_irq.attr,
  2299. &dev_attr_flags.attr,
  2300. &dev_attr_xmit_fifo_size.attr,
  2301. &dev_attr_uartclk.attr,
  2302. &dev_attr_close_delay.attr,
  2303. &dev_attr_closing_wait.attr,
  2304. &dev_attr_custom_divisor.attr,
  2305. &dev_attr_io_type.attr,
  2306. &dev_attr_iomem_base.attr,
  2307. &dev_attr_iomem_reg_shift.attr,
  2308. NULL,
  2309. };
  2310. static const struct attribute_group tty_dev_attr_group = {
  2311. .attrs = tty_dev_attrs,
  2312. };
  2313. /**
  2314. * uart_add_one_port - attach a driver-defined port structure
  2315. * @drv: pointer to the uart low level driver structure for this port
  2316. * @uport: uart port structure to use for this port.
  2317. *
  2318. * This allows the driver to register its own uart_port structure
  2319. * with the core driver. The main purpose is to allow the low
  2320. * level uart drivers to expand uart_port, rather than having yet
  2321. * more levels of structures.
  2322. */
  2323. int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
  2324. {
  2325. struct uart_state *state;
  2326. struct tty_port *port;
  2327. int ret = 0;
  2328. struct device *tty_dev;
  2329. int num_groups;
  2330. BUG_ON(in_interrupt());
  2331. if (uport->line >= drv->nr)
  2332. return -EINVAL;
  2333. state = drv->state + uport->line;
  2334. port = &state->port;
  2335. mutex_lock(&port_mutex);
  2336. mutex_lock(&port->mutex);
  2337. if (state->uart_port) {
  2338. ret = -EINVAL;
  2339. goto out;
  2340. }
  2341. /* Link the port to the driver state table and vice versa */
  2342. atomic_set(&state->refcount, 1);
  2343. init_waitqueue_head(&state->remove_wait);
  2344. state->uart_port = uport;
  2345. uport->state = state;
  2346. state->pm_state = UART_PM_STATE_UNDEFINED;
  2347. uport->cons = drv->cons;
  2348. uport->minor = drv->tty_driver->minor_start + uport->line;
  2349. uport->name = kasprintf(GFP_KERNEL, "%s%d", drv->dev_name,
  2350. drv->tty_driver->name_base + uport->line);
  2351. if (!uport->name) {
  2352. ret = -ENOMEM;
  2353. goto out;
  2354. }
  2355. /*
  2356. * If this port is a console, then the spinlock is already
  2357. * initialised.
  2358. */
  2359. if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
  2360. spin_lock_init(&uport->lock);
  2361. lockdep_set_class(&uport->lock, &port_lock_key);
  2362. }
  2363. if (uport->cons && uport->dev)
  2364. of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
  2365. uart_configure_port(drv, state, uport);
  2366. port->console = uart_console(uport);
  2367. num_groups = 2;
  2368. if (uport->attr_group)
  2369. num_groups++;
  2370. uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
  2371. GFP_KERNEL);
  2372. if (!uport->tty_groups) {
  2373. ret = -ENOMEM;
  2374. goto out;
  2375. }
  2376. uport->tty_groups[0] = &tty_dev_attr_group;
  2377. if (uport->attr_group)
  2378. uport->tty_groups[1] = uport->attr_group;
  2379. /*
  2380. * Register the port whether it's detected or not. This allows
  2381. * setserial to be used to alter this port's parameters.
  2382. */
  2383. tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
  2384. uport->line, uport->dev, port, uport->tty_groups);
  2385. if (likely(!IS_ERR(tty_dev))) {
  2386. device_set_wakeup_capable(tty_dev, 1);
  2387. } else {
  2388. dev_err(uport->dev, "Cannot register tty device on line %d\n",
  2389. uport->line);
  2390. }
  2391. /*
  2392. * Ensure UPF_DEAD is not set.
  2393. */
  2394. uport->flags &= ~UPF_DEAD;
  2395. out:
  2396. mutex_unlock(&port->mutex);
  2397. mutex_unlock(&port_mutex);
  2398. return ret;
  2399. }
  2400. /**
  2401. * uart_remove_one_port - detach a driver defined port structure
  2402. * @drv: pointer to the uart low level driver structure for this port
  2403. * @uport: uart port structure for this port
  2404. *
  2405. * This unhooks (and hangs up) the specified port structure from the
  2406. * core driver. No further calls will be made to the low-level code
  2407. * for this port.
  2408. */
  2409. int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
  2410. {
  2411. struct uart_state *state = drv->state + uport->line;
  2412. struct tty_port *port = &state->port;
  2413. struct uart_port *uart_port;
  2414. struct tty_struct *tty;
  2415. int ret = 0;
  2416. BUG_ON(in_interrupt());
  2417. mutex_lock(&port_mutex);
  2418. /*
  2419. * Mark the port "dead" - this prevents any opens from
  2420. * succeeding while we shut down the port.
  2421. */
  2422. mutex_lock(&port->mutex);
  2423. uart_port = uart_port_check(state);
  2424. if (uart_port != uport)
  2425. dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
  2426. uart_port, uport);
  2427. if (!uart_port) {
  2428. mutex_unlock(&port->mutex);
  2429. ret = -EINVAL;
  2430. goto out;
  2431. }
  2432. uport->flags |= UPF_DEAD;
  2433. mutex_unlock(&port->mutex);
  2434. /*
  2435. * Remove the devices from the tty layer
  2436. */
  2437. tty_port_unregister_device(port, drv->tty_driver, uport->line);
  2438. tty = tty_port_tty_get(port);
  2439. if (tty) {
  2440. tty_vhangup(port->tty);
  2441. tty_kref_put(tty);
  2442. }
  2443. /*
  2444. * If the port is used as a console, unregister it
  2445. */
  2446. if (uart_console(uport))
  2447. unregister_console(uport->cons);
  2448. /*
  2449. * Free the port IO and memory resources, if any.
  2450. */
  2451. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  2452. uport->ops->release_port(uport);
  2453. kfree(uport->tty_groups);
  2454. kfree(uport->name);
  2455. /*
  2456. * Indicate that there isn't a port here anymore.
  2457. */
  2458. uport->type = PORT_UNKNOWN;
  2459. mutex_lock(&port->mutex);
  2460. WARN_ON(atomic_dec_return(&state->refcount) < 0);
  2461. wait_event(state->remove_wait, !atomic_read(&state->refcount));
  2462. state->uart_port = NULL;
  2463. mutex_unlock(&port->mutex);
  2464. out:
  2465. mutex_unlock(&port_mutex);
  2466. return ret;
  2467. }
  2468. /*
  2469. * Are the two ports equivalent?
  2470. */
  2471. int uart_match_port(struct uart_port *port1, struct uart_port *port2)
  2472. {
  2473. if (port1->iotype != port2->iotype)
  2474. return 0;
  2475. switch (port1->iotype) {
  2476. case UPIO_PORT:
  2477. return (port1->iobase == port2->iobase);
  2478. case UPIO_HUB6:
  2479. return (port1->iobase == port2->iobase) &&
  2480. (port1->hub6 == port2->hub6);
  2481. case UPIO_MEM:
  2482. case UPIO_MEM16:
  2483. case UPIO_MEM32:
  2484. case UPIO_MEM32BE:
  2485. case UPIO_AU:
  2486. case UPIO_TSI:
  2487. return (port1->mapbase == port2->mapbase);
  2488. }
  2489. return 0;
  2490. }
  2491. EXPORT_SYMBOL(uart_match_port);
  2492. /**
  2493. * uart_handle_dcd_change - handle a change of carrier detect state
  2494. * @uport: uart_port structure for the open port
  2495. * @status: new carrier detect status, nonzero if active
  2496. *
  2497. * Caller must hold uport->lock
  2498. */
  2499. void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
  2500. {
  2501. struct tty_port *port = &uport->state->port;
  2502. struct tty_struct *tty = port->tty;
  2503. struct tty_ldisc *ld;
  2504. lockdep_assert_held_once(&uport->lock);
  2505. if (tty) {
  2506. ld = tty_ldisc_ref(tty);
  2507. if (ld) {
  2508. if (ld->ops->dcd_change)
  2509. ld->ops->dcd_change(tty, status);
  2510. tty_ldisc_deref(ld);
  2511. }
  2512. }
  2513. uport->icount.dcd++;
  2514. if (uart_dcd_enabled(uport)) {
  2515. if (status)
  2516. wake_up_interruptible(&port->open_wait);
  2517. else if (tty)
  2518. tty_hangup(tty);
  2519. }
  2520. }
  2521. EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
  2522. /**
  2523. * uart_handle_cts_change - handle a change of clear-to-send state
  2524. * @uport: uart_port structure for the open port
  2525. * @status: new clear to send status, nonzero if active
  2526. *
  2527. * Caller must hold uport->lock
  2528. */
  2529. void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
  2530. {
  2531. lockdep_assert_held_once(&uport->lock);
  2532. uport->icount.cts++;
  2533. if (uart_softcts_mode(uport)) {
  2534. if (uport->hw_stopped) {
  2535. if (status) {
  2536. uport->hw_stopped = 0;
  2537. uport->ops->start_tx(uport);
  2538. uart_write_wakeup(uport);
  2539. }
  2540. } else {
  2541. if (!status) {
  2542. uport->hw_stopped = 1;
  2543. uport->ops->stop_tx(uport);
  2544. }
  2545. }
  2546. }
  2547. }
  2548. EXPORT_SYMBOL_GPL(uart_handle_cts_change);
  2549. /**
  2550. * uart_insert_char - push a char to the uart layer
  2551. *
  2552. * User is responsible to call tty_flip_buffer_push when they are done with
  2553. * insertion.
  2554. *
  2555. * @port: corresponding port
  2556. * @status: state of the serial port RX buffer (LSR for 8250)
  2557. * @overrun: mask of overrun bits in @status
  2558. * @ch: character to push
  2559. * @flag: flag for the character (see TTY_NORMAL and friends)
  2560. */
  2561. void uart_insert_char(struct uart_port *port, unsigned int status,
  2562. unsigned int overrun, unsigned int ch, unsigned int flag)
  2563. {
  2564. struct tty_port *tport = &port->state->port;
  2565. if ((status & port->ignore_status_mask & ~overrun) == 0)
  2566. if (tty_insert_flip_char(tport, ch, flag) == 0)
  2567. ++port->icount.buf_overrun;
  2568. /*
  2569. * Overrun is special. Since it's reported immediately,
  2570. * it doesn't affect the current character.
  2571. */
  2572. if (status & ~port->ignore_status_mask & overrun)
  2573. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
  2574. ++port->icount.buf_overrun;
  2575. }
  2576. EXPORT_SYMBOL_GPL(uart_insert_char);
  2577. EXPORT_SYMBOL(uart_write_wakeup);
  2578. EXPORT_SYMBOL(uart_register_driver);
  2579. EXPORT_SYMBOL(uart_unregister_driver);
  2580. EXPORT_SYMBOL(uart_suspend_port);
  2581. EXPORT_SYMBOL(uart_resume_port);
  2582. EXPORT_SYMBOL(uart_add_one_port);
  2583. EXPORT_SYMBOL(uart_remove_one_port);
  2584. MODULE_DESCRIPTION("Serial driver core");
  2585. MODULE_LICENSE("GPL");