serial_core.c 74 KB

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