serial_core.c 63 KB

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