serial_core.c 63 KB

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