serial_core.c 72 KB

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