serial_core.c 63 KB

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