serial_core.c 63 KB

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