serial_core.c 62 KB

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