serial_core.c 62 KB

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