serial_core.c 63 KB

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