serial_core.c 58 KB

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