serial_core.c 62 KB

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