serial_core.c 63 KB

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