serial_core.c 65 KB

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