serial_core.c 63 KB

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