serial_core.c 61 KB

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