serial_core.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  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. We can ignore the Bfoo
  972. * bits in c_cflag; c_[io]speed will always be set
  973. * appropriately by set_termios() in tty_ioctl.c
  974. */
  975. #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  976. if ((cflag ^ old_termios->c_cflag) == 0 &&
  977. tty->termios->c_ospeed == old_termios->c_ospeed &&
  978. tty->termios->c_ispeed == old_termios->c_ispeed &&
  979. RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
  980. return;
  981. uart_change_speed(state, old_termios);
  982. /* Handle transition to B0 status */
  983. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  984. uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
  985. /* Handle transition away from B0 status */
  986. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  987. unsigned int mask = TIOCM_DTR;
  988. if (!(cflag & CRTSCTS) ||
  989. !test_bit(TTY_THROTTLED, &tty->flags))
  990. mask |= TIOCM_RTS;
  991. uart_set_mctrl(state->port, mask);
  992. }
  993. /* Handle turning off CRTSCTS */
  994. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  995. spin_lock_irqsave(&state->port->lock, flags);
  996. tty->hw_stopped = 0;
  997. __uart_start(tty);
  998. spin_unlock_irqrestore(&state->port->lock, flags);
  999. }
  1000. /* Handle turning on CRTSCTS */
  1001. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  1002. spin_lock_irqsave(&state->port->lock, flags);
  1003. if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
  1004. tty->hw_stopped = 1;
  1005. state->port->ops->stop_tx(state->port);
  1006. }
  1007. spin_unlock_irqrestore(&state->port->lock, flags);
  1008. }
  1009. #if 0
  1010. /*
  1011. * No need to wake up processes in open wait, since they
  1012. * sample the CLOCAL flag once, and don't recheck it.
  1013. * XXX It's not clear whether the current behavior is correct
  1014. * or not. Hence, this may change.....
  1015. */
  1016. if (!(old_termios->c_cflag & CLOCAL) &&
  1017. (tty->termios->c_cflag & CLOCAL))
  1018. wake_up_interruptible(&state->info->open_wait);
  1019. #endif
  1020. }
  1021. /*
  1022. * In 2.4.5, calls to this will be serialized via the BKL in
  1023. * linux/drivers/char/tty_io.c:tty_release()
  1024. * linux/drivers/char/tty_io.c:do_tty_handup()
  1025. */
  1026. static void uart_close(struct tty_struct *tty, struct file *filp)
  1027. {
  1028. struct uart_state *state = tty->driver_data;
  1029. struct uart_port *port;
  1030. BUG_ON(!kernel_locked());
  1031. if (!state || !state->port)
  1032. return;
  1033. port = state->port;
  1034. pr_debug("uart_close(%d) called\n", port->line);
  1035. mutex_lock(&state->mutex);
  1036. if (tty_hung_up_p(filp))
  1037. goto done;
  1038. if ((tty->count == 1) && (state->count != 1)) {
  1039. /*
  1040. * Uh, oh. tty->count is 1, which means that the tty
  1041. * structure will be freed. state->count should always
  1042. * be one in these conditions. If it's greater than
  1043. * one, we've got real problems, since it means the
  1044. * serial port won't be shutdown.
  1045. */
  1046. printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
  1047. "state->count is %d\n", state->count);
  1048. state->count = 1;
  1049. }
  1050. if (--state->count < 0) {
  1051. printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
  1052. tty->name, state->count);
  1053. state->count = 0;
  1054. }
  1055. if (state->count)
  1056. goto done;
  1057. /*
  1058. * Now we wait for the transmit buffer to clear; and we notify
  1059. * the line discipline to only process XON/XOFF characters by
  1060. * setting tty->closing.
  1061. */
  1062. tty->closing = 1;
  1063. if (state->closing_wait != USF_CLOSING_WAIT_NONE)
  1064. tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
  1065. /*
  1066. * At this point, we stop accepting input. To do this, we
  1067. * disable the receive line status interrupts.
  1068. */
  1069. if (state->info->flags & UIF_INITIALIZED) {
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&port->lock, flags);
  1072. port->ops->stop_rx(port);
  1073. spin_unlock_irqrestore(&port->lock, flags);
  1074. /*
  1075. * Before we drop DTR, make sure the UART transmitter
  1076. * has completely drained; this is especially
  1077. * important if there is a transmit FIFO!
  1078. */
  1079. uart_wait_until_sent(tty, port->timeout);
  1080. }
  1081. uart_shutdown(state);
  1082. uart_flush_buffer(tty);
  1083. tty_ldisc_flush(tty);
  1084. tty->closing = 0;
  1085. state->info->tty = NULL;
  1086. if (state->info->blocked_open) {
  1087. if (state->close_delay)
  1088. msleep_interruptible(state->close_delay);
  1089. } else if (!uart_console(port)) {
  1090. uart_change_pm(state, 3);
  1091. }
  1092. /*
  1093. * Wake up anyone trying to open this port.
  1094. */
  1095. state->info->flags &= ~UIF_NORMAL_ACTIVE;
  1096. wake_up_interruptible(&state->info->open_wait);
  1097. done:
  1098. mutex_unlock(&state->mutex);
  1099. }
  1100. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1101. {
  1102. struct uart_state *state = tty->driver_data;
  1103. struct uart_port *port = state->port;
  1104. unsigned long char_time, expire;
  1105. BUG_ON(!kernel_locked());
  1106. if (port->type == PORT_UNKNOWN || port->fifosize == 0)
  1107. return;
  1108. /*
  1109. * Set the check interval to be 1/5 of the estimated time to
  1110. * send a single character, and make it at least 1. The check
  1111. * interval should also be less than the timeout.
  1112. *
  1113. * Note: we have to use pretty tight timings here to satisfy
  1114. * the NIST-PCTS.
  1115. */
  1116. char_time = (port->timeout - HZ/50) / port->fifosize;
  1117. char_time = char_time / 5;
  1118. if (char_time == 0)
  1119. char_time = 1;
  1120. if (timeout && timeout < char_time)
  1121. char_time = timeout;
  1122. /*
  1123. * If the transmitter hasn't cleared in twice the approximate
  1124. * amount of time to send the entire FIFO, it probably won't
  1125. * ever clear. This assumes the UART isn't doing flow
  1126. * control, which is currently the case. Hence, if it ever
  1127. * takes longer than port->timeout, this is probably due to a
  1128. * UART bug of some kind. So, we clamp the timeout parameter at
  1129. * 2*port->timeout.
  1130. */
  1131. if (timeout == 0 || timeout > 2 * port->timeout)
  1132. timeout = 2 * port->timeout;
  1133. expire = jiffies + timeout;
  1134. pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
  1135. port->line, jiffies, expire);
  1136. /*
  1137. * Check whether the transmitter is empty every 'char_time'.
  1138. * 'timeout' / 'expire' give us the maximum amount of time
  1139. * we wait.
  1140. */
  1141. while (!port->ops->tx_empty(port)) {
  1142. msleep_interruptible(jiffies_to_msecs(char_time));
  1143. if (signal_pending(current))
  1144. break;
  1145. if (time_after(jiffies, expire))
  1146. break;
  1147. }
  1148. set_current_state(TASK_RUNNING); /* might not be needed */
  1149. }
  1150. /*
  1151. * This is called with the BKL held in
  1152. * linux/drivers/char/tty_io.c:do_tty_hangup()
  1153. * We're called from the eventd thread, so we can sleep for
  1154. * a _short_ time only.
  1155. */
  1156. static void uart_hangup(struct tty_struct *tty)
  1157. {
  1158. struct uart_state *state = tty->driver_data;
  1159. BUG_ON(!kernel_locked());
  1160. pr_debug("uart_hangup(%d)\n", state->port->line);
  1161. mutex_lock(&state->mutex);
  1162. if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
  1163. uart_flush_buffer(tty);
  1164. uart_shutdown(state);
  1165. state->count = 0;
  1166. state->info->flags &= ~UIF_NORMAL_ACTIVE;
  1167. state->info->tty = NULL;
  1168. wake_up_interruptible(&state->info->open_wait);
  1169. wake_up_interruptible(&state->info->delta_msr_wait);
  1170. }
  1171. mutex_unlock(&state->mutex);
  1172. }
  1173. /*
  1174. * Copy across the serial console cflag setting into the termios settings
  1175. * for the initial open of the port. This allows continuity between the
  1176. * kernel settings, and the settings init adopts when it opens the port
  1177. * for the first time.
  1178. */
  1179. static void uart_update_termios(struct uart_state *state)
  1180. {
  1181. struct tty_struct *tty = state->info->tty;
  1182. struct uart_port *port = state->port;
  1183. if (uart_console(port) && port->cons->cflag) {
  1184. tty->termios->c_cflag = port->cons->cflag;
  1185. port->cons->cflag = 0;
  1186. }
  1187. /*
  1188. * If the device failed to grab its irq resources,
  1189. * or some other error occurred, don't try to talk
  1190. * to the port hardware.
  1191. */
  1192. if (!(tty->flags & (1 << TTY_IO_ERROR))) {
  1193. /*
  1194. * Make termios settings take effect.
  1195. */
  1196. uart_change_speed(state, NULL);
  1197. /*
  1198. * And finally enable the RTS and DTR signals.
  1199. */
  1200. if (tty->termios->c_cflag & CBAUD)
  1201. uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1202. }
  1203. }
  1204. /*
  1205. * Block the open until the port is ready. We must be called with
  1206. * the per-port semaphore held.
  1207. */
  1208. static int
  1209. uart_block_til_ready(struct file *filp, struct uart_state *state)
  1210. {
  1211. DECLARE_WAITQUEUE(wait, current);
  1212. struct uart_info *info = state->info;
  1213. struct uart_port *port = state->port;
  1214. unsigned int mctrl;
  1215. info->blocked_open++;
  1216. state->count--;
  1217. add_wait_queue(&info->open_wait, &wait);
  1218. while (1) {
  1219. set_current_state(TASK_INTERRUPTIBLE);
  1220. /*
  1221. * If we have been hung up, tell userspace/restart open.
  1222. */
  1223. if (tty_hung_up_p(filp) || info->tty == NULL)
  1224. break;
  1225. /*
  1226. * If the port has been closed, tell userspace/restart open.
  1227. */
  1228. if (!(info->flags & UIF_INITIALIZED))
  1229. break;
  1230. /*
  1231. * If non-blocking mode is set, or CLOCAL mode is set,
  1232. * we don't want to wait for the modem status lines to
  1233. * indicate that the port is ready.
  1234. *
  1235. * Also, if the port is not enabled/configured, we want
  1236. * to allow the open to succeed here. Note that we will
  1237. * have set TTY_IO_ERROR for a non-existant port.
  1238. */
  1239. if ((filp->f_flags & O_NONBLOCK) ||
  1240. (info->tty->termios->c_cflag & CLOCAL) ||
  1241. (info->tty->flags & (1 << TTY_IO_ERROR))) {
  1242. break;
  1243. }
  1244. /*
  1245. * Set DTR to allow modem to know we're waiting. Do
  1246. * not set RTS here - we want to make sure we catch
  1247. * the data from the modem.
  1248. */
  1249. if (info->tty->termios->c_cflag & CBAUD)
  1250. uart_set_mctrl(port, TIOCM_DTR);
  1251. /*
  1252. * and wait for the carrier to indicate that the
  1253. * modem is ready for us.
  1254. */
  1255. spin_lock_irq(&port->lock);
  1256. port->ops->enable_ms(port);
  1257. mctrl = port->ops->get_mctrl(port);
  1258. spin_unlock_irq(&port->lock);
  1259. if (mctrl & TIOCM_CAR)
  1260. break;
  1261. mutex_unlock(&state->mutex);
  1262. schedule();
  1263. mutex_lock(&state->mutex);
  1264. if (signal_pending(current))
  1265. break;
  1266. }
  1267. set_current_state(TASK_RUNNING);
  1268. remove_wait_queue(&info->open_wait, &wait);
  1269. state->count++;
  1270. info->blocked_open--;
  1271. if (signal_pending(current))
  1272. return -ERESTARTSYS;
  1273. if (!info->tty || tty_hung_up_p(filp))
  1274. return -EAGAIN;
  1275. return 0;
  1276. }
  1277. static struct uart_state *uart_get(struct uart_driver *drv, int line)
  1278. {
  1279. struct uart_state *state;
  1280. int ret = 0;
  1281. state = drv->state + line;
  1282. if (mutex_lock_interruptible(&state->mutex)) {
  1283. ret = -ERESTARTSYS;
  1284. goto err;
  1285. }
  1286. state->count++;
  1287. if (!state->port || state->port->flags & UPF_DEAD) {
  1288. ret = -ENXIO;
  1289. goto err_unlock;
  1290. }
  1291. if (!state->info) {
  1292. state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);
  1293. if (state->info) {
  1294. init_waitqueue_head(&state->info->open_wait);
  1295. init_waitqueue_head(&state->info->delta_msr_wait);
  1296. /*
  1297. * Link the info into the other structures.
  1298. */
  1299. state->port->info = state->info;
  1300. tasklet_init(&state->info->tlet, uart_tasklet_action,
  1301. (unsigned long)state);
  1302. } else {
  1303. ret = -ENOMEM;
  1304. goto err_unlock;
  1305. }
  1306. }
  1307. return state;
  1308. err_unlock:
  1309. state->count--;
  1310. mutex_unlock(&state->mutex);
  1311. err:
  1312. return ERR_PTR(ret);
  1313. }
  1314. /*
  1315. * In 2.4.5, calls to uart_open are serialised by the BKL in
  1316. * linux/fs/devices.c:chrdev_open()
  1317. * Note that if this fails, then uart_close() _will_ be called.
  1318. *
  1319. * In time, we want to scrap the "opening nonpresent ports"
  1320. * behaviour and implement an alternative way for setserial
  1321. * to set base addresses/ports/types. This will allow us to
  1322. * get rid of a certain amount of extra tests.
  1323. */
  1324. static int uart_open(struct tty_struct *tty, struct file *filp)
  1325. {
  1326. struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
  1327. struct uart_state *state;
  1328. int retval, line = tty->index;
  1329. BUG_ON(!kernel_locked());
  1330. pr_debug("uart_open(%d) called\n", line);
  1331. /*
  1332. * tty->driver->num won't change, so we won't fail here with
  1333. * tty->driver_data set to something non-NULL (and therefore
  1334. * we won't get caught by uart_close()).
  1335. */
  1336. retval = -ENODEV;
  1337. if (line >= tty->driver->num)
  1338. goto fail;
  1339. /*
  1340. * We take the semaphore inside uart_get to guarantee that we won't
  1341. * be re-entered while allocating the info structure, or while we
  1342. * request any IRQs that the driver may need. This also has the nice
  1343. * side-effect that it delays the action of uart_hangup, so we can
  1344. * guarantee that info->tty will always contain something reasonable.
  1345. */
  1346. state = uart_get(drv, line);
  1347. if (IS_ERR(state)) {
  1348. retval = PTR_ERR(state);
  1349. goto fail;
  1350. }
  1351. /*
  1352. * Once we set tty->driver_data here, we are guaranteed that
  1353. * uart_close() will decrement the driver module use count.
  1354. * Any failures from here onwards should not touch the count.
  1355. */
  1356. tty->driver_data = state;
  1357. tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1358. tty->alt_speed = 0;
  1359. state->info->tty = tty;
  1360. /*
  1361. * If the port is in the middle of closing, bail out now.
  1362. */
  1363. if (tty_hung_up_p(filp)) {
  1364. retval = -EAGAIN;
  1365. state->count--;
  1366. mutex_unlock(&state->mutex);
  1367. goto fail;
  1368. }
  1369. /*
  1370. * Make sure the device is in D0 state.
  1371. */
  1372. if (state->count == 1)
  1373. uart_change_pm(state, 0);
  1374. /*
  1375. * Start up the serial port.
  1376. */
  1377. retval = uart_startup(state, 0);
  1378. /*
  1379. * If we succeeded, wait until the port is ready.
  1380. */
  1381. if (retval == 0)
  1382. retval = uart_block_til_ready(filp, state);
  1383. mutex_unlock(&state->mutex);
  1384. /*
  1385. * If this is the first open to succeed, adjust things to suit.
  1386. */
  1387. if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
  1388. state->info->flags |= UIF_NORMAL_ACTIVE;
  1389. uart_update_termios(state);
  1390. }
  1391. fail:
  1392. return retval;
  1393. }
  1394. static const char *uart_type(struct uart_port *port)
  1395. {
  1396. const char *str = NULL;
  1397. if (port->ops->type)
  1398. str = port->ops->type(port);
  1399. if (!str)
  1400. str = "unknown";
  1401. return str;
  1402. }
  1403. #ifdef CONFIG_PROC_FS
  1404. static int uart_line_info(char *buf, struct uart_driver *drv, int i)
  1405. {
  1406. struct uart_state *state = drv->state + i;
  1407. int pm_state;
  1408. struct uart_port *port = state->port;
  1409. char stat_buf[32];
  1410. unsigned int status;
  1411. int mmio, ret;
  1412. if (!port)
  1413. return 0;
  1414. mmio = port->iotype >= UPIO_MEM;
  1415. ret = sprintf(buf, "%d: uart:%s %s%08llX irq:%d",
  1416. port->line, uart_type(port),
  1417. mmio ? "mmio:0x" : "port:",
  1418. mmio ? (unsigned long long)port->mapbase
  1419. : (unsigned long long) port->iobase,
  1420. port->irq);
  1421. if (port->type == PORT_UNKNOWN) {
  1422. strcat(buf, "\n");
  1423. return ret + 1;
  1424. }
  1425. if(capable(CAP_SYS_ADMIN))
  1426. {
  1427. mutex_lock(&state->mutex);
  1428. pm_state = state->pm_state;
  1429. if (pm_state)
  1430. uart_change_pm(state, 0);
  1431. spin_lock_irq(&port->lock);
  1432. status = port->ops->get_mctrl(port);
  1433. spin_unlock_irq(&port->lock);
  1434. if (pm_state)
  1435. uart_change_pm(state, pm_state);
  1436. mutex_unlock(&state->mutex);
  1437. ret += sprintf(buf + ret, " tx:%d rx:%d",
  1438. port->icount.tx, port->icount.rx);
  1439. if (port->icount.frame)
  1440. ret += sprintf(buf + ret, " fe:%d",
  1441. port->icount.frame);
  1442. if (port->icount.parity)
  1443. ret += sprintf(buf + ret, " pe:%d",
  1444. port->icount.parity);
  1445. if (port->icount.brk)
  1446. ret += sprintf(buf + ret, " brk:%d",
  1447. port->icount.brk);
  1448. if (port->icount.overrun)
  1449. ret += sprintf(buf + ret, " oe:%d",
  1450. port->icount.overrun);
  1451. #define INFOBIT(bit,str) \
  1452. if (port->mctrl & (bit)) \
  1453. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1454. strlen(stat_buf) - 2)
  1455. #define STATBIT(bit,str) \
  1456. if (status & (bit)) \
  1457. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1458. strlen(stat_buf) - 2)
  1459. stat_buf[0] = '\0';
  1460. stat_buf[1] = '\0';
  1461. INFOBIT(TIOCM_RTS, "|RTS");
  1462. STATBIT(TIOCM_CTS, "|CTS");
  1463. INFOBIT(TIOCM_DTR, "|DTR");
  1464. STATBIT(TIOCM_DSR, "|DSR");
  1465. STATBIT(TIOCM_CAR, "|CD");
  1466. STATBIT(TIOCM_RNG, "|RI");
  1467. if (stat_buf[0])
  1468. stat_buf[0] = ' ';
  1469. strcat(stat_buf, "\n");
  1470. ret += sprintf(buf + ret, stat_buf);
  1471. } else {
  1472. strcat(buf, "\n");
  1473. ret++;
  1474. }
  1475. #undef STATBIT
  1476. #undef INFOBIT
  1477. return ret;
  1478. }
  1479. static int uart_read_proc(char *page, char **start, off_t off,
  1480. int count, int *eof, void *data)
  1481. {
  1482. struct tty_driver *ttydrv = data;
  1483. struct uart_driver *drv = ttydrv->driver_state;
  1484. int i, len = 0, l;
  1485. off_t begin = 0;
  1486. len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
  1487. "", "", "");
  1488. for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
  1489. l = uart_line_info(page + len, drv, i);
  1490. len += l;
  1491. if (len + begin > off + count)
  1492. goto done;
  1493. if (len + begin < off) {
  1494. begin += len;
  1495. len = 0;
  1496. }
  1497. }
  1498. *eof = 1;
  1499. done:
  1500. if (off >= len + begin)
  1501. return 0;
  1502. *start = page + (off - begin);
  1503. return (count < begin + len - off) ? count : (begin + len - off);
  1504. }
  1505. #endif
  1506. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  1507. /*
  1508. * uart_console_write - write a console message to a serial port
  1509. * @port: the port to write the message
  1510. * @s: array of characters
  1511. * @count: number of characters in string to write
  1512. * @write: function to write character to port
  1513. */
  1514. void uart_console_write(struct uart_port *port, const char *s,
  1515. unsigned int count,
  1516. void (*putchar)(struct uart_port *, int))
  1517. {
  1518. unsigned int i;
  1519. for (i = 0; i < count; i++, s++) {
  1520. if (*s == '\n')
  1521. putchar(port, '\r');
  1522. putchar(port, *s);
  1523. }
  1524. }
  1525. EXPORT_SYMBOL_GPL(uart_console_write);
  1526. /*
  1527. * Check whether an invalid uart number has been specified, and
  1528. * if so, search for the first available port that does have
  1529. * console support.
  1530. */
  1531. struct uart_port * __init
  1532. uart_get_console(struct uart_port *ports, int nr, struct console *co)
  1533. {
  1534. int idx = co->index;
  1535. if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
  1536. ports[idx].membase == NULL))
  1537. for (idx = 0; idx < nr; idx++)
  1538. if (ports[idx].iobase != 0 ||
  1539. ports[idx].membase != NULL)
  1540. break;
  1541. co->index = idx;
  1542. return ports + idx;
  1543. }
  1544. /**
  1545. * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
  1546. * @options: pointer to option string
  1547. * @baud: pointer to an 'int' variable for the baud rate.
  1548. * @parity: pointer to an 'int' variable for the parity.
  1549. * @bits: pointer to an 'int' variable for the number of data bits.
  1550. * @flow: pointer to an 'int' variable for the flow control character.
  1551. *
  1552. * uart_parse_options decodes a string containing the serial console
  1553. * options. The format of the string is <baud><parity><bits><flow>,
  1554. * eg: 115200n8r
  1555. */
  1556. void __init
  1557. uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
  1558. {
  1559. char *s = options;
  1560. *baud = simple_strtoul(s, NULL, 10);
  1561. while (*s >= '0' && *s <= '9')
  1562. s++;
  1563. if (*s)
  1564. *parity = *s++;
  1565. if (*s)
  1566. *bits = *s++ - '0';
  1567. if (*s)
  1568. *flow = *s;
  1569. }
  1570. struct baud_rates {
  1571. unsigned int rate;
  1572. unsigned int cflag;
  1573. };
  1574. static const struct baud_rates baud_rates[] = {
  1575. { 921600, B921600 },
  1576. { 460800, B460800 },
  1577. { 230400, B230400 },
  1578. { 115200, B115200 },
  1579. { 57600, B57600 },
  1580. { 38400, B38400 },
  1581. { 19200, B19200 },
  1582. { 9600, B9600 },
  1583. { 4800, B4800 },
  1584. { 2400, B2400 },
  1585. { 1200, B1200 },
  1586. { 0, B38400 }
  1587. };
  1588. /**
  1589. * uart_set_options - setup the serial console parameters
  1590. * @port: pointer to the serial ports uart_port structure
  1591. * @co: console pointer
  1592. * @baud: baud rate
  1593. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1594. * @bits: number of data bits
  1595. * @flow: flow control character - 'r' (rts)
  1596. */
  1597. int __init
  1598. uart_set_options(struct uart_port *port, struct console *co,
  1599. int baud, int parity, int bits, int flow)
  1600. {
  1601. struct ktermios termios;
  1602. int i;
  1603. /*
  1604. * Ensure that the serial console lock is initialised
  1605. * early.
  1606. */
  1607. spin_lock_init(&port->lock);
  1608. lockdep_set_class(&port->lock, &port_lock_key);
  1609. memset(&termios, 0, sizeof(struct ktermios));
  1610. termios.c_cflag = CREAD | HUPCL | CLOCAL;
  1611. /*
  1612. * Construct a cflag setting.
  1613. */
  1614. for (i = 0; baud_rates[i].rate; i++)
  1615. if (baud_rates[i].rate <= baud)
  1616. break;
  1617. termios.c_cflag |= baud_rates[i].cflag;
  1618. if (bits == 7)
  1619. termios.c_cflag |= CS7;
  1620. else
  1621. termios.c_cflag |= CS8;
  1622. switch (parity) {
  1623. case 'o': case 'O':
  1624. termios.c_cflag |= PARODD;
  1625. /*fall through*/
  1626. case 'e': case 'E':
  1627. termios.c_cflag |= PARENB;
  1628. break;
  1629. }
  1630. if (flow == 'r')
  1631. termios.c_cflag |= CRTSCTS;
  1632. /*
  1633. * some uarts on other side don't support no flow control.
  1634. * So we set * DTR in host uart to make them happy
  1635. */
  1636. port->mctrl |= TIOCM_DTR;
  1637. port->ops->set_termios(port, &termios, NULL);
  1638. co->cflag = termios.c_cflag;
  1639. return 0;
  1640. }
  1641. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1642. static void uart_change_pm(struct uart_state *state, int pm_state)
  1643. {
  1644. struct uart_port *port = state->port;
  1645. if (state->pm_state != pm_state) {
  1646. if (port->ops->pm)
  1647. port->ops->pm(port, pm_state, state->pm_state);
  1648. state->pm_state = pm_state;
  1649. }
  1650. }
  1651. struct uart_match {
  1652. struct uart_port *port;
  1653. struct uart_driver *driver;
  1654. };
  1655. static int serial_match_port(struct device *dev, void *data)
  1656. {
  1657. struct uart_match *match = data;
  1658. dev_t devt = MKDEV(match->driver->major, match->driver->minor) + match->port->line;
  1659. return dev->devt == devt; /* Actually, only one tty per port */
  1660. }
  1661. int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
  1662. {
  1663. struct uart_state *state = drv->state + port->line;
  1664. struct device *tty_dev;
  1665. struct uart_match match = {port, drv};
  1666. mutex_lock(&state->mutex);
  1667. if (!console_suspend_enabled && uart_console(port)) {
  1668. /* we're going to avoid suspending serial console */
  1669. mutex_unlock(&state->mutex);
  1670. return 0;
  1671. }
  1672. tty_dev = device_find_child(port->dev, &match, serial_match_port);
  1673. if (device_may_wakeup(tty_dev)) {
  1674. enable_irq_wake(port->irq);
  1675. put_device(tty_dev);
  1676. mutex_unlock(&state->mutex);
  1677. return 0;
  1678. }
  1679. port->suspended = 1;
  1680. if (state->info && state->info->flags & UIF_INITIALIZED) {
  1681. const struct uart_ops *ops = port->ops;
  1682. state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
  1683. | UIF_SUSPENDED;
  1684. spin_lock_irq(&port->lock);
  1685. ops->stop_tx(port);
  1686. ops->set_mctrl(port, 0);
  1687. ops->stop_rx(port);
  1688. spin_unlock_irq(&port->lock);
  1689. /*
  1690. * Wait for the transmitter to empty.
  1691. */
  1692. while (!ops->tx_empty(port)) {
  1693. msleep(10);
  1694. }
  1695. ops->shutdown(port);
  1696. }
  1697. /*
  1698. * Disable the console device before suspending.
  1699. */
  1700. if (uart_console(port))
  1701. console_stop(port->cons);
  1702. uart_change_pm(state, 3);
  1703. mutex_unlock(&state->mutex);
  1704. return 0;
  1705. }
  1706. int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
  1707. {
  1708. struct uart_state *state = drv->state + port->line;
  1709. mutex_lock(&state->mutex);
  1710. if (!console_suspend_enabled && uart_console(port)) {
  1711. /* no need to resume serial console, it wasn't suspended */
  1712. mutex_unlock(&state->mutex);
  1713. return 0;
  1714. }
  1715. if (!port->suspended) {
  1716. disable_irq_wake(port->irq);
  1717. mutex_unlock(&state->mutex);
  1718. return 0;
  1719. }
  1720. port->suspended = 0;
  1721. uart_change_pm(state, 0);
  1722. /*
  1723. * Re-enable the console device after suspending.
  1724. */
  1725. if (uart_console(port)) {
  1726. struct ktermios termios;
  1727. /*
  1728. * First try to use the console cflag setting.
  1729. */
  1730. memset(&termios, 0, sizeof(struct ktermios));
  1731. termios.c_cflag = port->cons->cflag;
  1732. /*
  1733. * If that's unset, use the tty termios setting.
  1734. */
  1735. if (state->info && state->info->tty && termios.c_cflag == 0)
  1736. termios = *state->info->tty->termios;
  1737. port->ops->set_termios(port, &termios, NULL);
  1738. console_start(port->cons);
  1739. }
  1740. if (state->info && state->info->flags & UIF_SUSPENDED) {
  1741. const struct uart_ops *ops = port->ops;
  1742. int ret;
  1743. ops->set_mctrl(port, 0);
  1744. ret = ops->startup(port);
  1745. if (ret == 0) {
  1746. uart_change_speed(state, NULL);
  1747. spin_lock_irq(&port->lock);
  1748. ops->set_mctrl(port, port->mctrl);
  1749. ops->start_tx(port);
  1750. spin_unlock_irq(&port->lock);
  1751. state->info->flags |= UIF_INITIALIZED;
  1752. } else {
  1753. /*
  1754. * Failed to resume - maybe hardware went away?
  1755. * Clear the "initialized" flag so we won't try
  1756. * to call the low level drivers shutdown method.
  1757. */
  1758. uart_shutdown(state);
  1759. }
  1760. state->info->flags &= ~UIF_SUSPENDED;
  1761. }
  1762. mutex_unlock(&state->mutex);
  1763. return 0;
  1764. }
  1765. static inline void
  1766. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  1767. {
  1768. char address[64];
  1769. switch (port->iotype) {
  1770. case UPIO_PORT:
  1771. snprintf(address, sizeof(address),
  1772. "I/O 0x%x", port->iobase);
  1773. break;
  1774. case UPIO_HUB6:
  1775. snprintf(address, sizeof(address),
  1776. "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
  1777. break;
  1778. case UPIO_MEM:
  1779. case UPIO_MEM32:
  1780. case UPIO_AU:
  1781. case UPIO_TSI:
  1782. case UPIO_DWAPB:
  1783. snprintf(address, sizeof(address),
  1784. "MMIO 0x%llx", (unsigned long long)port->mapbase);
  1785. break;
  1786. default:
  1787. strlcpy(address, "*unknown*", sizeof(address));
  1788. break;
  1789. }
  1790. printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
  1791. port->dev ? port->dev->bus_id : "",
  1792. port->dev ? ": " : "",
  1793. drv->dev_name, port->line, address, port->irq, uart_type(port));
  1794. }
  1795. static void
  1796. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  1797. struct uart_port *port)
  1798. {
  1799. unsigned int flags;
  1800. /*
  1801. * If there isn't a port here, don't do anything further.
  1802. */
  1803. if (!port->iobase && !port->mapbase && !port->membase)
  1804. return;
  1805. /*
  1806. * Now do the auto configuration stuff. Note that config_port
  1807. * is expected to claim the resources and map the port for us.
  1808. */
  1809. flags = UART_CONFIG_TYPE;
  1810. if (port->flags & UPF_AUTO_IRQ)
  1811. flags |= UART_CONFIG_IRQ;
  1812. if (port->flags & UPF_BOOT_AUTOCONF) {
  1813. port->type = PORT_UNKNOWN;
  1814. port->ops->config_port(port, flags);
  1815. }
  1816. if (port->type != PORT_UNKNOWN) {
  1817. unsigned long flags;
  1818. uart_report_port(drv, port);
  1819. /* Power up port for set_mctrl() */
  1820. uart_change_pm(state, 0);
  1821. /*
  1822. * Ensure that the modem control lines are de-activated.
  1823. * We probably don't need a spinlock around this, but
  1824. */
  1825. spin_lock_irqsave(&port->lock, flags);
  1826. port->ops->set_mctrl(port, 0);
  1827. spin_unlock_irqrestore(&port->lock, flags);
  1828. /*
  1829. * If this driver supports console, and it hasn't been
  1830. * successfully registered yet, try to re-register it.
  1831. * It may be that the port was not available.
  1832. */
  1833. if (port->cons && !(port->cons->flags & CON_ENABLED))
  1834. register_console(port->cons);
  1835. /*
  1836. * Power down all ports by default, except the
  1837. * console if we have one.
  1838. */
  1839. if (!uart_console(port))
  1840. uart_change_pm(state, 3);
  1841. }
  1842. }
  1843. static const struct tty_operations uart_ops = {
  1844. .open = uart_open,
  1845. .close = uart_close,
  1846. .write = uart_write,
  1847. .put_char = uart_put_char,
  1848. .flush_chars = uart_flush_chars,
  1849. .write_room = uart_write_room,
  1850. .chars_in_buffer= uart_chars_in_buffer,
  1851. .flush_buffer = uart_flush_buffer,
  1852. .ioctl = uart_ioctl,
  1853. .throttle = uart_throttle,
  1854. .unthrottle = uart_unthrottle,
  1855. .send_xchar = uart_send_xchar,
  1856. .set_termios = uart_set_termios,
  1857. .stop = uart_stop,
  1858. .start = uart_start,
  1859. .hangup = uart_hangup,
  1860. .break_ctl = uart_break_ctl,
  1861. .wait_until_sent= uart_wait_until_sent,
  1862. #ifdef CONFIG_PROC_FS
  1863. .read_proc = uart_read_proc,
  1864. #endif
  1865. .tiocmget = uart_tiocmget,
  1866. .tiocmset = uart_tiocmset,
  1867. };
  1868. /**
  1869. * uart_register_driver - register a driver with the uart core layer
  1870. * @drv: low level driver structure
  1871. *
  1872. * Register a uart driver with the core driver. We in turn register
  1873. * with the tty layer, and initialise the core driver per-port state.
  1874. *
  1875. * We have a proc file in /proc/tty/driver which is named after the
  1876. * normal driver.
  1877. *
  1878. * drv->port should be NULL, and the per-port structures should be
  1879. * registered using uart_add_one_port after this call has succeeded.
  1880. */
  1881. int uart_register_driver(struct uart_driver *drv)
  1882. {
  1883. struct tty_driver *normal = NULL;
  1884. int i, retval;
  1885. BUG_ON(drv->state);
  1886. /*
  1887. * Maybe we should be using a slab cache for this, especially if
  1888. * we have a large number of ports to handle.
  1889. */
  1890. drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
  1891. retval = -ENOMEM;
  1892. if (!drv->state)
  1893. goto out;
  1894. normal = alloc_tty_driver(drv->nr);
  1895. if (!normal)
  1896. goto out;
  1897. drv->tty_driver = normal;
  1898. normal->owner = drv->owner;
  1899. normal->driver_name = drv->driver_name;
  1900. normal->name = drv->dev_name;
  1901. normal->major = drv->major;
  1902. normal->minor_start = drv->minor;
  1903. normal->type = TTY_DRIVER_TYPE_SERIAL;
  1904. normal->subtype = SERIAL_TYPE_NORMAL;
  1905. normal->init_termios = tty_std_termios;
  1906. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1907. normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
  1908. normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1909. normal->driver_state = drv;
  1910. tty_set_operations(normal, &uart_ops);
  1911. /*
  1912. * Initialise the UART state(s).
  1913. */
  1914. for (i = 0; i < drv->nr; i++) {
  1915. struct uart_state *state = drv->state + i;
  1916. state->close_delay = 500; /* .5 seconds */
  1917. state->closing_wait = 30000; /* 30 seconds */
  1918. mutex_init(&state->mutex);
  1919. }
  1920. retval = tty_register_driver(normal);
  1921. out:
  1922. if (retval < 0) {
  1923. put_tty_driver(normal);
  1924. kfree(drv->state);
  1925. }
  1926. return retval;
  1927. }
  1928. /**
  1929. * uart_unregister_driver - remove a driver from the uart core layer
  1930. * @drv: low level driver structure
  1931. *
  1932. * Remove all references to a driver from the core driver. The low
  1933. * level driver must have removed all its ports via the
  1934. * uart_remove_one_port() if it registered them with uart_add_one_port().
  1935. * (ie, drv->port == NULL)
  1936. */
  1937. void uart_unregister_driver(struct uart_driver *drv)
  1938. {
  1939. struct tty_driver *p = drv->tty_driver;
  1940. tty_unregister_driver(p);
  1941. put_tty_driver(p);
  1942. kfree(drv->state);
  1943. drv->tty_driver = NULL;
  1944. }
  1945. struct tty_driver *uart_console_device(struct console *co, int *index)
  1946. {
  1947. struct uart_driver *p = co->data;
  1948. *index = co->index;
  1949. return p->tty_driver;
  1950. }
  1951. /**
  1952. * uart_add_one_port - attach a driver-defined port structure
  1953. * @drv: pointer to the uart low level driver structure for this port
  1954. * @port: uart port structure to use for this port.
  1955. *
  1956. * This allows the driver to register its own uart_port structure
  1957. * with the core driver. The main purpose is to allow the low
  1958. * level uart drivers to expand uart_port, rather than having yet
  1959. * more levels of structures.
  1960. */
  1961. int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
  1962. {
  1963. struct uart_state *state;
  1964. int ret = 0;
  1965. struct device *tty_dev;
  1966. BUG_ON(in_interrupt());
  1967. if (port->line >= drv->nr)
  1968. return -EINVAL;
  1969. state = drv->state + port->line;
  1970. mutex_lock(&port_mutex);
  1971. mutex_lock(&state->mutex);
  1972. if (state->port) {
  1973. ret = -EINVAL;
  1974. goto out;
  1975. }
  1976. state->port = port;
  1977. state->pm_state = -1;
  1978. port->cons = drv->cons;
  1979. port->info = state->info;
  1980. /*
  1981. * If this port is a console, then the spinlock is already
  1982. * initialised.
  1983. */
  1984. if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
  1985. spin_lock_init(&port->lock);
  1986. lockdep_set_class(&port->lock, &port_lock_key);
  1987. }
  1988. uart_configure_port(drv, state, port);
  1989. /*
  1990. * Register the port whether it's detected or not. This allows
  1991. * setserial to be used to alter this ports parameters.
  1992. */
  1993. tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);
  1994. if (likely(!IS_ERR(tty_dev))) {
  1995. device_can_wakeup(tty_dev) = 1;
  1996. device_set_wakeup_enable(tty_dev, 0);
  1997. } else
  1998. printk(KERN_ERR "Cannot register tty device on line %d\n",
  1999. port->line);
  2000. /*
  2001. * Ensure UPF_DEAD is not set.
  2002. */
  2003. port->flags &= ~UPF_DEAD;
  2004. out:
  2005. mutex_unlock(&state->mutex);
  2006. mutex_unlock(&port_mutex);
  2007. return ret;
  2008. }
  2009. /**
  2010. * uart_remove_one_port - detach a driver defined port structure
  2011. * @drv: pointer to the uart low level driver structure for this port
  2012. * @port: uart port structure for this port
  2013. *
  2014. * This unhooks (and hangs up) the specified port structure from the
  2015. * core driver. No further calls will be made to the low-level code
  2016. * for this port.
  2017. */
  2018. int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
  2019. {
  2020. struct uart_state *state = drv->state + port->line;
  2021. struct uart_info *info;
  2022. BUG_ON(in_interrupt());
  2023. if (state->port != port)
  2024. printk(KERN_ALERT "Removing wrong port: %p != %p\n",
  2025. state->port, port);
  2026. mutex_lock(&port_mutex);
  2027. /*
  2028. * Mark the port "dead" - this prevents any opens from
  2029. * succeeding while we shut down the port.
  2030. */
  2031. mutex_lock(&state->mutex);
  2032. port->flags |= UPF_DEAD;
  2033. mutex_unlock(&state->mutex);
  2034. /*
  2035. * Remove the devices from the tty layer
  2036. */
  2037. tty_unregister_device(drv->tty_driver, port->line);
  2038. info = state->info;
  2039. if (info && info->tty)
  2040. tty_vhangup(info->tty);
  2041. /*
  2042. * All users of this port should now be disconnected from
  2043. * this driver, and the port shut down. We should be the
  2044. * only thread fiddling with this port from now on.
  2045. */
  2046. state->info = NULL;
  2047. /*
  2048. * Free the port IO and memory resources, if any.
  2049. */
  2050. if (port->type != PORT_UNKNOWN)
  2051. port->ops->release_port(port);
  2052. /*
  2053. * Indicate that there isn't a port here anymore.
  2054. */
  2055. port->type = PORT_UNKNOWN;
  2056. /*
  2057. * Kill the tasklet, and free resources.
  2058. */
  2059. if (info) {
  2060. tasklet_kill(&info->tlet);
  2061. kfree(info);
  2062. }
  2063. state->port = NULL;
  2064. mutex_unlock(&port_mutex);
  2065. return 0;
  2066. }
  2067. /*
  2068. * Are the two ports equivalent?
  2069. */
  2070. int uart_match_port(struct uart_port *port1, struct uart_port *port2)
  2071. {
  2072. if (port1->iotype != port2->iotype)
  2073. return 0;
  2074. switch (port1->iotype) {
  2075. case UPIO_PORT:
  2076. return (port1->iobase == port2->iobase);
  2077. case UPIO_HUB6:
  2078. return (port1->iobase == port2->iobase) &&
  2079. (port1->hub6 == port2->hub6);
  2080. case UPIO_MEM:
  2081. case UPIO_MEM32:
  2082. case UPIO_AU:
  2083. case UPIO_TSI:
  2084. case UPIO_DWAPB:
  2085. return (port1->mapbase == port2->mapbase);
  2086. }
  2087. return 0;
  2088. }
  2089. EXPORT_SYMBOL(uart_match_port);
  2090. EXPORT_SYMBOL(uart_write_wakeup);
  2091. EXPORT_SYMBOL(uart_register_driver);
  2092. EXPORT_SYMBOL(uart_unregister_driver);
  2093. EXPORT_SYMBOL(uart_suspend_port);
  2094. EXPORT_SYMBOL(uart_resume_port);
  2095. EXPORT_SYMBOL(uart_add_one_port);
  2096. EXPORT_SYMBOL(uart_remove_one_port);
  2097. MODULE_DESCRIPTION("Serial driver core");
  2098. MODULE_LICENSE("GPL");