serial_core.c 63 KB

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