serial_core.c 62 KB

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