serial_core.c 58 KB

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