serial_core.c 63 KB

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