serial_core.c 70 KB

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