serial_core.c 71 KB

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