serial_core.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  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. goto exit; // Added to return the correct error -Ram Gupta
  676. }
  677. }
  678. port->irq = new_serial.irq;
  679. port->uartclk = new_serial.baud_base * 16;
  680. port->flags = (port->flags & ~UPF_CHANGE_MASK) |
  681. (new_flags & UPF_CHANGE_MASK);
  682. port->custom_divisor = new_serial.custom_divisor;
  683. state->close_delay = close_delay;
  684. state->closing_wait = closing_wait;
  685. if (new_serial.xmit_fifo_size)
  686. port->fifosize = new_serial.xmit_fifo_size;
  687. if (state->info->tty)
  688. state->info->tty->low_latency =
  689. (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
  690. check_and_exit:
  691. retval = 0;
  692. if (port->type == PORT_UNKNOWN)
  693. goto exit;
  694. if (state->info->flags & UIF_INITIALIZED) {
  695. if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
  696. old_custom_divisor != port->custom_divisor) {
  697. /*
  698. * If they're setting up a custom divisor or speed,
  699. * instead of clearing it, then bitch about it. No
  700. * need to rate-limit; it's CAP_SYS_ADMIN only.
  701. */
  702. if (port->flags & UPF_SPD_MASK) {
  703. char buf[64];
  704. printk(KERN_NOTICE
  705. "%s sets custom speed on %s. This "
  706. "is deprecated.\n", current->comm,
  707. tty_name(state->info->tty, buf));
  708. }
  709. uart_change_speed(state, NULL);
  710. }
  711. } else
  712. retval = uart_startup(state, 1);
  713. exit:
  714. mutex_unlock(&state->mutex);
  715. return retval;
  716. }
  717. /*
  718. * uart_get_lsr_info - get line status register info.
  719. * Note: uart_ioctl protects us against hangups.
  720. */
  721. static int uart_get_lsr_info(struct uart_state *state,
  722. unsigned int __user *value)
  723. {
  724. struct uart_port *port = state->port;
  725. unsigned int result;
  726. result = port->ops->tx_empty(port);
  727. /*
  728. * If we're about to load something into the transmit
  729. * register, we'll pretend the transmitter isn't empty to
  730. * avoid a race condition (depending on when the transmit
  731. * interrupt happens).
  732. */
  733. if (port->x_char ||
  734. ((uart_circ_chars_pending(&state->info->xmit) > 0) &&
  735. !state->info->tty->stopped && !state->info->tty->hw_stopped))
  736. result &= ~TIOCSER_TEMT;
  737. return put_user(result, value);
  738. }
  739. static int uart_tiocmget(struct tty_struct *tty, struct file *file)
  740. {
  741. struct uart_state *state = tty->driver_data;
  742. struct uart_port *port = state->port;
  743. int result = -EIO;
  744. mutex_lock(&state->mutex);
  745. if ((!file || !tty_hung_up_p(file)) &&
  746. !(tty->flags & (1 << TTY_IO_ERROR))) {
  747. result = port->mctrl;
  748. spin_lock_irq(&port->lock);
  749. result |= port->ops->get_mctrl(port);
  750. spin_unlock_irq(&port->lock);
  751. }
  752. mutex_unlock(&state->mutex);
  753. return result;
  754. }
  755. static int
  756. uart_tiocmset(struct tty_struct *tty, struct file *file,
  757. unsigned int set, unsigned int clear)
  758. {
  759. struct uart_state *state = tty->driver_data;
  760. struct uart_port *port = state->port;
  761. int ret = -EIO;
  762. mutex_lock(&state->mutex);
  763. if ((!file || !tty_hung_up_p(file)) &&
  764. !(tty->flags & (1 << TTY_IO_ERROR))) {
  765. uart_update_mctrl(port, set, clear);
  766. ret = 0;
  767. }
  768. mutex_unlock(&state->mutex);
  769. return ret;
  770. }
  771. static void uart_break_ctl(struct tty_struct *tty, int break_state)
  772. {
  773. struct uart_state *state = tty->driver_data;
  774. struct uart_port *port = state->port;
  775. BUG_ON(!kernel_locked());
  776. mutex_lock(&state->mutex);
  777. if (port->type != PORT_UNKNOWN)
  778. port->ops->break_ctl(port, break_state);
  779. mutex_unlock(&state->mutex);
  780. }
  781. static int uart_do_autoconfig(struct uart_state *state)
  782. {
  783. struct uart_port *port = state->port;
  784. int flags, ret;
  785. if (!capable(CAP_SYS_ADMIN))
  786. return -EPERM;
  787. /*
  788. * Take the per-port semaphore. This prevents count from
  789. * changing, and hence any extra opens of the port while
  790. * we're auto-configuring.
  791. */
  792. if (mutex_lock_interruptible(&state->mutex))
  793. return -ERESTARTSYS;
  794. ret = -EBUSY;
  795. if (uart_users(state) == 1) {
  796. uart_shutdown(state);
  797. /*
  798. * If we already have a port type configured,
  799. * we must release its resources.
  800. */
  801. if (port->type != PORT_UNKNOWN)
  802. port->ops->release_port(port);
  803. flags = UART_CONFIG_TYPE;
  804. if (port->flags & UPF_AUTO_IRQ)
  805. flags |= UART_CONFIG_IRQ;
  806. /*
  807. * This will claim the ports resources if
  808. * a port is found.
  809. */
  810. port->ops->config_port(port, flags);
  811. ret = uart_startup(state, 1);
  812. }
  813. mutex_unlock(&state->mutex);
  814. return ret;
  815. }
  816. /*
  817. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  818. * - mask passed in arg for lines of interest
  819. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  820. * Caller should use TIOCGICOUNT to see which one it was
  821. */
  822. static int
  823. uart_wait_modem_status(struct uart_state *state, unsigned long arg)
  824. {
  825. struct uart_port *port = state->port;
  826. DECLARE_WAITQUEUE(wait, current);
  827. struct uart_icount cprev, cnow;
  828. int ret;
  829. /*
  830. * note the counters on entry
  831. */
  832. spin_lock_irq(&port->lock);
  833. memcpy(&cprev, &port->icount, sizeof(struct uart_icount));
  834. /*
  835. * Force modem status interrupts on
  836. */
  837. port->ops->enable_ms(port);
  838. spin_unlock_irq(&port->lock);
  839. add_wait_queue(&state->info->delta_msr_wait, &wait);
  840. for (;;) {
  841. spin_lock_irq(&port->lock);
  842. memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
  843. spin_unlock_irq(&port->lock);
  844. set_current_state(TASK_INTERRUPTIBLE);
  845. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  846. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  847. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  848. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  849. ret = 0;
  850. break;
  851. }
  852. schedule();
  853. /* see if a signal did it */
  854. if (signal_pending(current)) {
  855. ret = -ERESTARTSYS;
  856. break;
  857. }
  858. cprev = cnow;
  859. }
  860. current->state = TASK_RUNNING;
  861. remove_wait_queue(&state->info->delta_msr_wait, &wait);
  862. return ret;
  863. }
  864. /*
  865. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  866. * Return: write counters to the user passed counter struct
  867. * NB: both 1->0 and 0->1 transitions are counted except for
  868. * RI where only 0->1 is counted.
  869. */
  870. static int uart_get_count(struct uart_state *state,
  871. struct serial_icounter_struct __user *icnt)
  872. {
  873. struct serial_icounter_struct icount;
  874. struct uart_icount cnow;
  875. struct uart_port *port = state->port;
  876. spin_lock_irq(&port->lock);
  877. memcpy(&cnow, &port->icount, sizeof(struct uart_icount));
  878. spin_unlock_irq(&port->lock);
  879. icount.cts = cnow.cts;
  880. icount.dsr = cnow.dsr;
  881. icount.rng = cnow.rng;
  882. icount.dcd = cnow.dcd;
  883. icount.rx = cnow.rx;
  884. icount.tx = cnow.tx;
  885. icount.frame = cnow.frame;
  886. icount.overrun = cnow.overrun;
  887. icount.parity = cnow.parity;
  888. icount.brk = cnow.brk;
  889. icount.buf_overrun = cnow.buf_overrun;
  890. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  891. }
  892. /*
  893. * Called via sys_ioctl under the BKL. We can use spin_lock_irq() here.
  894. */
  895. static int
  896. uart_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd,
  897. unsigned long arg)
  898. {
  899. struct uart_state *state = tty->driver_data;
  900. void __user *uarg = (void __user *)arg;
  901. int ret = -ENOIOCTLCMD;
  902. BUG_ON(!kernel_locked());
  903. /*
  904. * These ioctls don't rely on the hardware to be present.
  905. */
  906. switch (cmd) {
  907. case TIOCGSERIAL:
  908. ret = uart_get_info(state, uarg);
  909. break;
  910. case TIOCSSERIAL:
  911. ret = uart_set_info(state, uarg);
  912. break;
  913. case TIOCSERCONFIG:
  914. ret = uart_do_autoconfig(state);
  915. break;
  916. case TIOCSERGWILD: /* obsolete */
  917. case TIOCSERSWILD: /* obsolete */
  918. ret = 0;
  919. break;
  920. }
  921. if (ret != -ENOIOCTLCMD)
  922. goto out;
  923. if (tty->flags & (1 << TTY_IO_ERROR)) {
  924. ret = -EIO;
  925. goto out;
  926. }
  927. /*
  928. * The following should only be used when hardware is present.
  929. */
  930. switch (cmd) {
  931. case TIOCMIWAIT:
  932. ret = uart_wait_modem_status(state, arg);
  933. break;
  934. case TIOCGICOUNT:
  935. ret = uart_get_count(state, uarg);
  936. break;
  937. }
  938. if (ret != -ENOIOCTLCMD)
  939. goto out;
  940. mutex_lock(&state->mutex);
  941. if (tty_hung_up_p(filp)) {
  942. ret = -EIO;
  943. goto out_up;
  944. }
  945. /*
  946. * All these rely on hardware being present and need to be
  947. * protected against the tty being hung up.
  948. */
  949. switch (cmd) {
  950. case TIOCSERGETLSR: /* Get line status register */
  951. ret = uart_get_lsr_info(state, uarg);
  952. break;
  953. default: {
  954. struct uart_port *port = state->port;
  955. if (port->ops->ioctl)
  956. ret = port->ops->ioctl(port, cmd, arg);
  957. break;
  958. }
  959. }
  960. out_up:
  961. mutex_unlock(&state->mutex);
  962. out:
  963. return ret;
  964. }
  965. static void uart_set_termios(struct tty_struct *tty, struct termios *old_termios)
  966. {
  967. struct uart_state *state = tty->driver_data;
  968. unsigned long flags;
  969. unsigned int cflag = tty->termios->c_cflag;
  970. BUG_ON(!kernel_locked());
  971. /*
  972. * These are the bits that are used to setup various
  973. * flags in the low level driver.
  974. */
  975. #define RELEVANT_IFLAG(iflag) ((iflag) & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  976. if ((cflag ^ old_termios->c_cflag) == 0 &&
  977. RELEVANT_IFLAG(tty->termios->c_iflag ^ old_termios->c_iflag) == 0)
  978. return;
  979. uart_change_speed(state, old_termios);
  980. /* Handle transition to B0 status */
  981. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  982. uart_clear_mctrl(state->port, TIOCM_RTS | TIOCM_DTR);
  983. /* Handle transition away from B0 status */
  984. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  985. unsigned int mask = TIOCM_DTR;
  986. if (!(cflag & CRTSCTS) ||
  987. !test_bit(TTY_THROTTLED, &tty->flags))
  988. mask |= TIOCM_RTS;
  989. uart_set_mctrl(state->port, mask);
  990. }
  991. /* Handle turning off CRTSCTS */
  992. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  993. spin_lock_irqsave(&state->port->lock, flags);
  994. tty->hw_stopped = 0;
  995. __uart_start(tty);
  996. spin_unlock_irqrestore(&state->port->lock, flags);
  997. }
  998. /* Handle turning on CRTSCTS */
  999. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  1000. spin_lock_irqsave(&state->port->lock, flags);
  1001. if (!(state->port->ops->get_mctrl(state->port) & TIOCM_CTS)) {
  1002. tty->hw_stopped = 1;
  1003. state->port->ops->stop_tx(state->port);
  1004. }
  1005. spin_unlock_irqrestore(&state->port->lock, flags);
  1006. }
  1007. #if 0
  1008. /*
  1009. * No need to wake up processes in open wait, since they
  1010. * sample the CLOCAL flag once, and don't recheck it.
  1011. * XXX It's not clear whether the current behavior is correct
  1012. * or not. Hence, this may change.....
  1013. */
  1014. if (!(old_termios->c_cflag & CLOCAL) &&
  1015. (tty->termios->c_cflag & CLOCAL))
  1016. wake_up_interruptible(&state->info->open_wait);
  1017. #endif
  1018. }
  1019. /*
  1020. * In 2.4.5, calls to this will be serialized via the BKL in
  1021. * linux/drivers/char/tty_io.c:tty_release()
  1022. * linux/drivers/char/tty_io.c:do_tty_handup()
  1023. */
  1024. static void uart_close(struct tty_struct *tty, struct file *filp)
  1025. {
  1026. struct uart_state *state = tty->driver_data;
  1027. struct uart_port *port;
  1028. BUG_ON(!kernel_locked());
  1029. if (!state || !state->port)
  1030. return;
  1031. port = state->port;
  1032. DPRINTK("uart_close(%d) called\n", port->line);
  1033. mutex_lock(&state->mutex);
  1034. if (tty_hung_up_p(filp))
  1035. goto done;
  1036. if ((tty->count == 1) && (state->count != 1)) {
  1037. /*
  1038. * Uh, oh. tty->count is 1, which means that the tty
  1039. * structure will be freed. state->count should always
  1040. * be one in these conditions. If it's greater than
  1041. * one, we've got real problems, since it means the
  1042. * serial port won't be shutdown.
  1043. */
  1044. printk(KERN_ERR "uart_close: bad serial port count; tty->count is 1, "
  1045. "state->count is %d\n", state->count);
  1046. state->count = 1;
  1047. }
  1048. if (--state->count < 0) {
  1049. printk(KERN_ERR "uart_close: bad serial port count for %s: %d\n",
  1050. tty->name, state->count);
  1051. state->count = 0;
  1052. }
  1053. if (state->count)
  1054. goto done;
  1055. /*
  1056. * Now we wait for the transmit buffer to clear; and we notify
  1057. * the line discipline to only process XON/XOFF characters by
  1058. * setting tty->closing.
  1059. */
  1060. tty->closing = 1;
  1061. if (state->closing_wait != USF_CLOSING_WAIT_NONE)
  1062. tty_wait_until_sent(tty, msecs_to_jiffies(state->closing_wait));
  1063. /*
  1064. * At this point, we stop accepting input. To do this, we
  1065. * disable the receive line status interrupts.
  1066. */
  1067. if (state->info->flags & UIF_INITIALIZED) {
  1068. unsigned long flags;
  1069. spin_lock_irqsave(&port->lock, flags);
  1070. port->ops->stop_rx(port);
  1071. spin_unlock_irqrestore(&port->lock, flags);
  1072. /*
  1073. * Before we drop DTR, make sure the UART transmitter
  1074. * has completely drained; this is especially
  1075. * important if there is a transmit FIFO!
  1076. */
  1077. uart_wait_until_sent(tty, port->timeout);
  1078. }
  1079. uart_shutdown(state);
  1080. uart_flush_buffer(tty);
  1081. tty_ldisc_flush(tty);
  1082. tty->closing = 0;
  1083. state->info->tty = NULL;
  1084. if (state->info->blocked_open) {
  1085. if (state->close_delay)
  1086. msleep_interruptible(state->close_delay);
  1087. } else if (!uart_console(port)) {
  1088. uart_change_pm(state, 3);
  1089. }
  1090. /*
  1091. * Wake up anyone trying to open this port.
  1092. */
  1093. state->info->flags &= ~UIF_NORMAL_ACTIVE;
  1094. wake_up_interruptible(&state->info->open_wait);
  1095. done:
  1096. mutex_unlock(&state->mutex);
  1097. }
  1098. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1099. {
  1100. struct uart_state *state = tty->driver_data;
  1101. struct uart_port *port = state->port;
  1102. unsigned long char_time, expire;
  1103. BUG_ON(!kernel_locked());
  1104. if (port->type == PORT_UNKNOWN || port->fifosize == 0)
  1105. return;
  1106. /*
  1107. * Set the check interval to be 1/5 of the estimated time to
  1108. * send a single character, and make it at least 1. The check
  1109. * interval should also be less than the timeout.
  1110. *
  1111. * Note: we have to use pretty tight timings here to satisfy
  1112. * the NIST-PCTS.
  1113. */
  1114. char_time = (port->timeout - HZ/50) / port->fifosize;
  1115. char_time = char_time / 5;
  1116. if (char_time == 0)
  1117. char_time = 1;
  1118. if (timeout && timeout < char_time)
  1119. char_time = timeout;
  1120. /*
  1121. * If the transmitter hasn't cleared in twice the approximate
  1122. * amount of time to send the entire FIFO, it probably won't
  1123. * ever clear. This assumes the UART isn't doing flow
  1124. * control, which is currently the case. Hence, if it ever
  1125. * takes longer than port->timeout, this is probably due to a
  1126. * UART bug of some kind. So, we clamp the timeout parameter at
  1127. * 2*port->timeout.
  1128. */
  1129. if (timeout == 0 || timeout > 2 * port->timeout)
  1130. timeout = 2 * port->timeout;
  1131. expire = jiffies + timeout;
  1132. DPRINTK("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
  1133. port->line, jiffies, expire);
  1134. /*
  1135. * Check whether the transmitter is empty every 'char_time'.
  1136. * 'timeout' / 'expire' give us the maximum amount of time
  1137. * we wait.
  1138. */
  1139. while (!port->ops->tx_empty(port)) {
  1140. msleep_interruptible(jiffies_to_msecs(char_time));
  1141. if (signal_pending(current))
  1142. break;
  1143. if (time_after(jiffies, expire))
  1144. break;
  1145. }
  1146. set_current_state(TASK_RUNNING); /* might not be needed */
  1147. }
  1148. /*
  1149. * This is called with the BKL held in
  1150. * linux/drivers/char/tty_io.c:do_tty_hangup()
  1151. * We're called from the eventd thread, so we can sleep for
  1152. * a _short_ time only.
  1153. */
  1154. static void uart_hangup(struct tty_struct *tty)
  1155. {
  1156. struct uart_state *state = tty->driver_data;
  1157. BUG_ON(!kernel_locked());
  1158. DPRINTK("uart_hangup(%d)\n", state->port->line);
  1159. mutex_lock(&state->mutex);
  1160. if (state->info && state->info->flags & UIF_NORMAL_ACTIVE) {
  1161. uart_flush_buffer(tty);
  1162. uart_shutdown(state);
  1163. state->count = 0;
  1164. state->info->flags &= ~UIF_NORMAL_ACTIVE;
  1165. state->info->tty = NULL;
  1166. wake_up_interruptible(&state->info->open_wait);
  1167. wake_up_interruptible(&state->info->delta_msr_wait);
  1168. }
  1169. mutex_unlock(&state->mutex);
  1170. }
  1171. /*
  1172. * Copy across the serial console cflag setting into the termios settings
  1173. * for the initial open of the port. This allows continuity between the
  1174. * kernel settings, and the settings init adopts when it opens the port
  1175. * for the first time.
  1176. */
  1177. static void uart_update_termios(struct uart_state *state)
  1178. {
  1179. struct tty_struct *tty = state->info->tty;
  1180. struct uart_port *port = state->port;
  1181. if (uart_console(port) && port->cons->cflag) {
  1182. tty->termios->c_cflag = port->cons->cflag;
  1183. port->cons->cflag = 0;
  1184. }
  1185. /*
  1186. * If the device failed to grab its irq resources,
  1187. * or some other error occurred, don't try to talk
  1188. * to the port hardware.
  1189. */
  1190. if (!(tty->flags & (1 << TTY_IO_ERROR))) {
  1191. /*
  1192. * Make termios settings take effect.
  1193. */
  1194. uart_change_speed(state, NULL);
  1195. /*
  1196. * And finally enable the RTS and DTR signals.
  1197. */
  1198. if (tty->termios->c_cflag & CBAUD)
  1199. uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  1200. }
  1201. }
  1202. /*
  1203. * Block the open until the port is ready. We must be called with
  1204. * the per-port semaphore held.
  1205. */
  1206. static int
  1207. uart_block_til_ready(struct file *filp, struct uart_state *state)
  1208. {
  1209. DECLARE_WAITQUEUE(wait, current);
  1210. struct uart_info *info = state->info;
  1211. struct uart_port *port = state->port;
  1212. unsigned int mctrl;
  1213. info->blocked_open++;
  1214. state->count--;
  1215. add_wait_queue(&info->open_wait, &wait);
  1216. while (1) {
  1217. set_current_state(TASK_INTERRUPTIBLE);
  1218. /*
  1219. * If we have been hung up, tell userspace/restart open.
  1220. */
  1221. if (tty_hung_up_p(filp) || info->tty == NULL)
  1222. break;
  1223. /*
  1224. * If the port has been closed, tell userspace/restart open.
  1225. */
  1226. if (!(info->flags & UIF_INITIALIZED))
  1227. break;
  1228. /*
  1229. * If non-blocking mode is set, or CLOCAL mode is set,
  1230. * we don't want to wait for the modem status lines to
  1231. * indicate that the port is ready.
  1232. *
  1233. * Also, if the port is not enabled/configured, we want
  1234. * to allow the open to succeed here. Note that we will
  1235. * have set TTY_IO_ERROR for a non-existant port.
  1236. */
  1237. if ((filp->f_flags & O_NONBLOCK) ||
  1238. (info->tty->termios->c_cflag & CLOCAL) ||
  1239. (info->tty->flags & (1 << TTY_IO_ERROR))) {
  1240. break;
  1241. }
  1242. /*
  1243. * Set DTR to allow modem to know we're waiting. Do
  1244. * not set RTS here - we want to make sure we catch
  1245. * the data from the modem.
  1246. */
  1247. if (info->tty->termios->c_cflag & CBAUD)
  1248. uart_set_mctrl(port, TIOCM_DTR);
  1249. /*
  1250. * and wait for the carrier to indicate that the
  1251. * modem is ready for us.
  1252. */
  1253. spin_lock_irq(&port->lock);
  1254. port->ops->enable_ms(port);
  1255. mctrl = port->ops->get_mctrl(port);
  1256. spin_unlock_irq(&port->lock);
  1257. if (mctrl & TIOCM_CAR)
  1258. break;
  1259. mutex_unlock(&state->mutex);
  1260. schedule();
  1261. mutex_lock(&state->mutex);
  1262. if (signal_pending(current))
  1263. break;
  1264. }
  1265. set_current_state(TASK_RUNNING);
  1266. remove_wait_queue(&info->open_wait, &wait);
  1267. state->count++;
  1268. info->blocked_open--;
  1269. if (signal_pending(current))
  1270. return -ERESTARTSYS;
  1271. if (!info->tty || tty_hung_up_p(filp))
  1272. return -EAGAIN;
  1273. return 0;
  1274. }
  1275. static struct uart_state *uart_get(struct uart_driver *drv, int line)
  1276. {
  1277. struct uart_state *state;
  1278. int ret = 0;
  1279. state = drv->state + line;
  1280. if (mutex_lock_interruptible(&state->mutex)) {
  1281. ret = -ERESTARTSYS;
  1282. goto err;
  1283. }
  1284. state->count++;
  1285. if (!state->port || state->port->flags & UPF_DEAD) {
  1286. ret = -ENXIO;
  1287. goto err_unlock;
  1288. }
  1289. if (!state->info) {
  1290. state->info = kmalloc(sizeof(struct uart_info), GFP_KERNEL);
  1291. if (state->info) {
  1292. memset(state->info, 0, sizeof(struct uart_info));
  1293. init_waitqueue_head(&state->info->open_wait);
  1294. init_waitqueue_head(&state->info->delta_msr_wait);
  1295. /*
  1296. * Link the info into the other structures.
  1297. */
  1298. state->port->info = state->info;
  1299. tasklet_init(&state->info->tlet, uart_tasklet_action,
  1300. (unsigned long)state);
  1301. } else {
  1302. ret = -ENOMEM;
  1303. goto err_unlock;
  1304. }
  1305. }
  1306. return state;
  1307. err_unlock:
  1308. state->count--;
  1309. mutex_unlock(&state->mutex);
  1310. err:
  1311. return ERR_PTR(ret);
  1312. }
  1313. /*
  1314. * In 2.4.5, calls to uart_open are serialised by the BKL in
  1315. * linux/fs/devices.c:chrdev_open()
  1316. * Note that if this fails, then uart_close() _will_ be called.
  1317. *
  1318. * In time, we want to scrap the "opening nonpresent ports"
  1319. * behaviour and implement an alternative way for setserial
  1320. * to set base addresses/ports/types. This will allow us to
  1321. * get rid of a certain amount of extra tests.
  1322. */
  1323. static int uart_open(struct tty_struct *tty, struct file *filp)
  1324. {
  1325. struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
  1326. struct uart_state *state;
  1327. int retval, line = tty->index;
  1328. BUG_ON(!kernel_locked());
  1329. DPRINTK("uart_open(%d) called\n", line);
  1330. /*
  1331. * tty->driver->num won't change, so we won't fail here with
  1332. * tty->driver_data set to something non-NULL (and therefore
  1333. * we won't get caught by uart_close()).
  1334. */
  1335. retval = -ENODEV;
  1336. if (line >= tty->driver->num)
  1337. goto fail;
  1338. /*
  1339. * We take the semaphore inside uart_get to guarantee that we won't
  1340. * be re-entered while allocating the info structure, or while we
  1341. * request any IRQs that the driver may need. This also has the nice
  1342. * side-effect that it delays the action of uart_hangup, so we can
  1343. * guarantee that info->tty will always contain something reasonable.
  1344. */
  1345. state = uart_get(drv, line);
  1346. if (IS_ERR(state)) {
  1347. retval = PTR_ERR(state);
  1348. goto fail;
  1349. }
  1350. /*
  1351. * Once we set tty->driver_data here, we are guaranteed that
  1352. * uart_close() will decrement the driver module use count.
  1353. * Any failures from here onwards should not touch the count.
  1354. */
  1355. tty->driver_data = state;
  1356. tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1357. tty->alt_speed = 0;
  1358. state->info->tty = tty;
  1359. /*
  1360. * If the port is in the middle of closing, bail out now.
  1361. */
  1362. if (tty_hung_up_p(filp)) {
  1363. retval = -EAGAIN;
  1364. state->count--;
  1365. mutex_unlock(&state->mutex);
  1366. goto fail;
  1367. }
  1368. /*
  1369. * Make sure the device is in D0 state.
  1370. */
  1371. if (state->count == 1)
  1372. uart_change_pm(state, 0);
  1373. /*
  1374. * Start up the serial port.
  1375. */
  1376. retval = uart_startup(state, 0);
  1377. /*
  1378. * If we succeeded, wait until the port is ready.
  1379. */
  1380. if (retval == 0)
  1381. retval = uart_block_til_ready(filp, state);
  1382. mutex_unlock(&state->mutex);
  1383. /*
  1384. * If this is the first open to succeed, adjust things to suit.
  1385. */
  1386. if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {
  1387. state->info->flags |= UIF_NORMAL_ACTIVE;
  1388. uart_update_termios(state);
  1389. }
  1390. fail:
  1391. return retval;
  1392. }
  1393. static const char *uart_type(struct uart_port *port)
  1394. {
  1395. const char *str = NULL;
  1396. if (port->ops->type)
  1397. str = port->ops->type(port);
  1398. if (!str)
  1399. str = "unknown";
  1400. return str;
  1401. }
  1402. #ifdef CONFIG_PROC_FS
  1403. static int uart_line_info(char *buf, struct uart_driver *drv, int i)
  1404. {
  1405. struct uart_state *state = drv->state + i;
  1406. struct uart_port *port = state->port;
  1407. char stat_buf[32];
  1408. unsigned int status;
  1409. int mmio, ret;
  1410. if (!port)
  1411. return 0;
  1412. mmio = port->iotype >= UPIO_MEM;
  1413. ret = sprintf(buf, "%d: uart:%s %s%08lX irq:%d",
  1414. port->line, uart_type(port),
  1415. mmio ? "mmio:0x" : "port:",
  1416. mmio ? port->mapbase : (unsigned long) port->iobase,
  1417. port->irq);
  1418. if (port->type == PORT_UNKNOWN) {
  1419. strcat(buf, "\n");
  1420. return ret + 1;
  1421. }
  1422. if(capable(CAP_SYS_ADMIN))
  1423. {
  1424. spin_lock_irq(&port->lock);
  1425. status = port->ops->get_mctrl(port);
  1426. spin_unlock_irq(&port->lock);
  1427. ret += sprintf(buf + ret, " tx:%d rx:%d",
  1428. port->icount.tx, port->icount.rx);
  1429. if (port->icount.frame)
  1430. ret += sprintf(buf + ret, " fe:%d",
  1431. port->icount.frame);
  1432. if (port->icount.parity)
  1433. ret += sprintf(buf + ret, " pe:%d",
  1434. port->icount.parity);
  1435. if (port->icount.brk)
  1436. ret += sprintf(buf + ret, " brk:%d",
  1437. port->icount.brk);
  1438. if (port->icount.overrun)
  1439. ret += sprintf(buf + ret, " oe:%d",
  1440. port->icount.overrun);
  1441. #define INFOBIT(bit,str) \
  1442. if (port->mctrl & (bit)) \
  1443. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1444. strlen(stat_buf) - 2)
  1445. #define STATBIT(bit,str) \
  1446. if (status & (bit)) \
  1447. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1448. strlen(stat_buf) - 2)
  1449. stat_buf[0] = '\0';
  1450. stat_buf[1] = '\0';
  1451. INFOBIT(TIOCM_RTS, "|RTS");
  1452. STATBIT(TIOCM_CTS, "|CTS");
  1453. INFOBIT(TIOCM_DTR, "|DTR");
  1454. STATBIT(TIOCM_DSR, "|DSR");
  1455. STATBIT(TIOCM_CAR, "|CD");
  1456. STATBIT(TIOCM_RNG, "|RI");
  1457. if (stat_buf[0])
  1458. stat_buf[0] = ' ';
  1459. strcat(stat_buf, "\n");
  1460. ret += sprintf(buf + ret, stat_buf);
  1461. } else {
  1462. strcat(buf, "\n");
  1463. ret++;
  1464. }
  1465. #undef STATBIT
  1466. #undef INFOBIT
  1467. return ret;
  1468. }
  1469. static int uart_read_proc(char *page, char **start, off_t off,
  1470. int count, int *eof, void *data)
  1471. {
  1472. struct tty_driver *ttydrv = data;
  1473. struct uart_driver *drv = ttydrv->driver_state;
  1474. int i, len = 0, l;
  1475. off_t begin = 0;
  1476. len += sprintf(page, "serinfo:1.0 driver%s%s revision:%s\n",
  1477. "", "", "");
  1478. for (i = 0; i < drv->nr && len < PAGE_SIZE - 96; i++) {
  1479. l = uart_line_info(page + len, drv, i);
  1480. len += l;
  1481. if (len + begin > off + count)
  1482. goto done;
  1483. if (len + begin < off) {
  1484. begin += len;
  1485. len = 0;
  1486. }
  1487. }
  1488. *eof = 1;
  1489. done:
  1490. if (off >= len + begin)
  1491. return 0;
  1492. *start = page + (off - begin);
  1493. return (count < begin + len - off) ? count : (begin + len - off);
  1494. }
  1495. #endif
  1496. #ifdef CONFIG_SERIAL_CORE_CONSOLE
  1497. /*
  1498. * uart_console_write - write a console message to a serial port
  1499. * @port: the port to write the message
  1500. * @s: array of characters
  1501. * @count: number of characters in string to write
  1502. * @write: function to write character to port
  1503. */
  1504. void uart_console_write(struct uart_port *port, const char *s,
  1505. unsigned int count,
  1506. void (*putchar)(struct uart_port *, int))
  1507. {
  1508. unsigned int i;
  1509. for (i = 0; i < count; i++, s++) {
  1510. if (*s == '\n')
  1511. putchar(port, '\r');
  1512. putchar(port, *s);
  1513. }
  1514. }
  1515. EXPORT_SYMBOL_GPL(uart_console_write);
  1516. /*
  1517. * Check whether an invalid uart number has been specified, and
  1518. * if so, search for the first available port that does have
  1519. * console support.
  1520. */
  1521. struct uart_port * __init
  1522. uart_get_console(struct uart_port *ports, int nr, struct console *co)
  1523. {
  1524. int idx = co->index;
  1525. if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
  1526. ports[idx].membase == NULL))
  1527. for (idx = 0; idx < nr; idx++)
  1528. if (ports[idx].iobase != 0 ||
  1529. ports[idx].membase != NULL)
  1530. break;
  1531. co->index = idx;
  1532. return ports + idx;
  1533. }
  1534. /**
  1535. * uart_parse_options - Parse serial port baud/parity/bits/flow contro.
  1536. * @options: pointer to option string
  1537. * @baud: pointer to an 'int' variable for the baud rate.
  1538. * @parity: pointer to an 'int' variable for the parity.
  1539. * @bits: pointer to an 'int' variable for the number of data bits.
  1540. * @flow: pointer to an 'int' variable for the flow control character.
  1541. *
  1542. * uart_parse_options decodes a string containing the serial console
  1543. * options. The format of the string is <baud><parity><bits><flow>,
  1544. * eg: 115200n8r
  1545. */
  1546. void __init
  1547. uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
  1548. {
  1549. char *s = options;
  1550. *baud = simple_strtoul(s, NULL, 10);
  1551. while (*s >= '0' && *s <= '9')
  1552. s++;
  1553. if (*s)
  1554. *parity = *s++;
  1555. if (*s)
  1556. *bits = *s++ - '0';
  1557. if (*s)
  1558. *flow = *s;
  1559. }
  1560. struct baud_rates {
  1561. unsigned int rate;
  1562. unsigned int cflag;
  1563. };
  1564. static const struct baud_rates baud_rates[] = {
  1565. { 921600, B921600 },
  1566. { 460800, B460800 },
  1567. { 230400, B230400 },
  1568. { 115200, B115200 },
  1569. { 57600, B57600 },
  1570. { 38400, B38400 },
  1571. { 19200, B19200 },
  1572. { 9600, B9600 },
  1573. { 4800, B4800 },
  1574. { 2400, B2400 },
  1575. { 1200, B1200 },
  1576. { 0, B38400 }
  1577. };
  1578. /**
  1579. * uart_set_options - setup the serial console parameters
  1580. * @port: pointer to the serial ports uart_port structure
  1581. * @co: console pointer
  1582. * @baud: baud rate
  1583. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1584. * @bits: number of data bits
  1585. * @flow: flow control character - 'r' (rts)
  1586. */
  1587. int __init
  1588. uart_set_options(struct uart_port *port, struct console *co,
  1589. int baud, int parity, int bits, int flow)
  1590. {
  1591. struct termios termios;
  1592. int i;
  1593. /*
  1594. * Ensure that the serial console lock is initialised
  1595. * early.
  1596. */
  1597. spin_lock_init(&port->lock);
  1598. lockdep_set_class(&port->lock, &port_lock_key);
  1599. memset(&termios, 0, sizeof(struct termios));
  1600. termios.c_cflag = CREAD | HUPCL | CLOCAL;
  1601. /*
  1602. * Construct a cflag setting.
  1603. */
  1604. for (i = 0; baud_rates[i].rate; i++)
  1605. if (baud_rates[i].rate <= baud)
  1606. break;
  1607. termios.c_cflag |= baud_rates[i].cflag;
  1608. if (bits == 7)
  1609. termios.c_cflag |= CS7;
  1610. else
  1611. termios.c_cflag |= CS8;
  1612. switch (parity) {
  1613. case 'o': case 'O':
  1614. termios.c_cflag |= PARODD;
  1615. /*fall through*/
  1616. case 'e': case 'E':
  1617. termios.c_cflag |= PARENB;
  1618. break;
  1619. }
  1620. if (flow == 'r')
  1621. termios.c_cflag |= CRTSCTS;
  1622. port->ops->set_termios(port, &termios, NULL);
  1623. co->cflag = termios.c_cflag;
  1624. return 0;
  1625. }
  1626. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1627. static void uart_change_pm(struct uart_state *state, int pm_state)
  1628. {
  1629. struct uart_port *port = state->port;
  1630. if (state->pm_state != pm_state) {
  1631. if (port->ops->pm)
  1632. port->ops->pm(port, pm_state, state->pm_state);
  1633. state->pm_state = pm_state;
  1634. }
  1635. }
  1636. int uart_suspend_port(struct uart_driver *drv, struct uart_port *port)
  1637. {
  1638. struct uart_state *state = drv->state + port->line;
  1639. mutex_lock(&state->mutex);
  1640. #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
  1641. if (uart_console(port)) {
  1642. mutex_unlock(&state->mutex);
  1643. return 0;
  1644. }
  1645. #endif
  1646. if (state->info && state->info->flags & UIF_INITIALIZED) {
  1647. const struct uart_ops *ops = port->ops;
  1648. state->info->flags = (state->info->flags & ~UIF_INITIALIZED)
  1649. | UIF_SUSPENDED;
  1650. spin_lock_irq(&port->lock);
  1651. ops->stop_tx(port);
  1652. ops->set_mctrl(port, 0);
  1653. ops->stop_rx(port);
  1654. spin_unlock_irq(&port->lock);
  1655. /*
  1656. * Wait for the transmitter to empty.
  1657. */
  1658. while (!ops->tx_empty(port)) {
  1659. msleep(10);
  1660. }
  1661. ops->shutdown(port);
  1662. }
  1663. /*
  1664. * Disable the console device before suspending.
  1665. */
  1666. if (uart_console(port))
  1667. console_stop(port->cons);
  1668. uart_change_pm(state, 3);
  1669. mutex_unlock(&state->mutex);
  1670. return 0;
  1671. }
  1672. int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
  1673. {
  1674. struct uart_state *state = drv->state + port->line;
  1675. mutex_lock(&state->mutex);
  1676. #ifdef CONFIG_DISABLE_CONSOLE_SUSPEND
  1677. if (uart_console(port)) {
  1678. mutex_unlock(&state->mutex);
  1679. return 0;
  1680. }
  1681. #endif
  1682. uart_change_pm(state, 0);
  1683. /*
  1684. * Re-enable the console device after suspending.
  1685. */
  1686. if (uart_console(port)) {
  1687. struct termios termios;
  1688. /*
  1689. * First try to use the console cflag setting.
  1690. */
  1691. memset(&termios, 0, sizeof(struct termios));
  1692. termios.c_cflag = port->cons->cflag;
  1693. /*
  1694. * If that's unset, use the tty termios setting.
  1695. */
  1696. if (state->info && state->info->tty && termios.c_cflag == 0)
  1697. termios = *state->info->tty->termios;
  1698. port->ops->set_termios(port, &termios, NULL);
  1699. console_start(port->cons);
  1700. }
  1701. if (state->info && state->info->flags & UIF_SUSPENDED) {
  1702. const struct uart_ops *ops = port->ops;
  1703. int ret;
  1704. ops->set_mctrl(port, 0);
  1705. ret = ops->startup(port);
  1706. if (ret == 0) {
  1707. uart_change_speed(state, NULL);
  1708. spin_lock_irq(&port->lock);
  1709. ops->set_mctrl(port, port->mctrl);
  1710. ops->start_tx(port);
  1711. spin_unlock_irq(&port->lock);
  1712. state->info->flags |= UIF_INITIALIZED;
  1713. } else {
  1714. /*
  1715. * Failed to resume - maybe hardware went away?
  1716. * Clear the "initialized" flag so we won't try
  1717. * to call the low level drivers shutdown method.
  1718. */
  1719. uart_shutdown(state);
  1720. }
  1721. state->info->flags &= ~UIF_SUSPENDED;
  1722. }
  1723. mutex_unlock(&state->mutex);
  1724. return 0;
  1725. }
  1726. static inline void
  1727. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  1728. {
  1729. char address[64];
  1730. switch (port->iotype) {
  1731. case UPIO_PORT:
  1732. snprintf(address, sizeof(address),
  1733. "I/O 0x%x", port->iobase);
  1734. break;
  1735. case UPIO_HUB6:
  1736. snprintf(address, sizeof(address),
  1737. "I/O 0x%x offset 0x%x", port->iobase, port->hub6);
  1738. break;
  1739. case UPIO_MEM:
  1740. case UPIO_MEM32:
  1741. case UPIO_AU:
  1742. case UPIO_TSI:
  1743. snprintf(address, sizeof(address),
  1744. "MMIO 0x%lx", port->mapbase);
  1745. break;
  1746. default:
  1747. strlcpy(address, "*unknown*", sizeof(address));
  1748. break;
  1749. }
  1750. printk(KERN_INFO "%s%s%s%d at %s (irq = %d) is a %s\n",
  1751. port->dev ? port->dev->bus_id : "",
  1752. port->dev ? ": " : "",
  1753. drv->dev_name, port->line, address, port->irq, uart_type(port));
  1754. }
  1755. static void
  1756. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  1757. struct uart_port *port)
  1758. {
  1759. unsigned int flags;
  1760. /*
  1761. * If there isn't a port here, don't do anything further.
  1762. */
  1763. if (!port->iobase && !port->mapbase && !port->membase)
  1764. return;
  1765. /*
  1766. * Now do the auto configuration stuff. Note that config_port
  1767. * is expected to claim the resources and map the port for us.
  1768. */
  1769. flags = UART_CONFIG_TYPE;
  1770. if (port->flags & UPF_AUTO_IRQ)
  1771. flags |= UART_CONFIG_IRQ;
  1772. if (port->flags & UPF_BOOT_AUTOCONF) {
  1773. port->type = PORT_UNKNOWN;
  1774. port->ops->config_port(port, flags);
  1775. }
  1776. if (port->type != PORT_UNKNOWN) {
  1777. unsigned long flags;
  1778. uart_report_port(drv, port);
  1779. /*
  1780. * Ensure that the modem control lines are de-activated.
  1781. * We probably don't need a spinlock around this, but
  1782. */
  1783. spin_lock_irqsave(&port->lock, flags);
  1784. port->ops->set_mctrl(port, 0);
  1785. spin_unlock_irqrestore(&port->lock, flags);
  1786. /*
  1787. * Power down all ports by default, except the
  1788. * console if we have one.
  1789. */
  1790. if (!uart_console(port))
  1791. uart_change_pm(state, 3);
  1792. }
  1793. }
  1794. static const struct tty_operations uart_ops = {
  1795. .open = uart_open,
  1796. .close = uart_close,
  1797. .write = uart_write,
  1798. .put_char = uart_put_char,
  1799. .flush_chars = uart_flush_chars,
  1800. .write_room = uart_write_room,
  1801. .chars_in_buffer= uart_chars_in_buffer,
  1802. .flush_buffer = uart_flush_buffer,
  1803. .ioctl = uart_ioctl,
  1804. .throttle = uart_throttle,
  1805. .unthrottle = uart_unthrottle,
  1806. .send_xchar = uart_send_xchar,
  1807. .set_termios = uart_set_termios,
  1808. .stop = uart_stop,
  1809. .start = uart_start,
  1810. .hangup = uart_hangup,
  1811. .break_ctl = uart_break_ctl,
  1812. .wait_until_sent= uart_wait_until_sent,
  1813. #ifdef CONFIG_PROC_FS
  1814. .read_proc = uart_read_proc,
  1815. #endif
  1816. .tiocmget = uart_tiocmget,
  1817. .tiocmset = uart_tiocmset,
  1818. };
  1819. /**
  1820. * uart_register_driver - register a driver with the uart core layer
  1821. * @drv: low level driver structure
  1822. *
  1823. * Register a uart driver with the core driver. We in turn register
  1824. * with the tty layer, and initialise the core driver per-port state.
  1825. *
  1826. * We have a proc file in /proc/tty/driver which is named after the
  1827. * normal driver.
  1828. *
  1829. * drv->port should be NULL, and the per-port structures should be
  1830. * registered using uart_add_one_port after this call has succeeded.
  1831. */
  1832. int uart_register_driver(struct uart_driver *drv)
  1833. {
  1834. struct tty_driver *normal = NULL;
  1835. int i, retval;
  1836. BUG_ON(drv->state);
  1837. /*
  1838. * Maybe we should be using a slab cache for this, especially if
  1839. * we have a large number of ports to handle.
  1840. */
  1841. drv->state = kmalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
  1842. retval = -ENOMEM;
  1843. if (!drv->state)
  1844. goto out;
  1845. memset(drv->state, 0, sizeof(struct uart_state) * drv->nr);
  1846. normal = alloc_tty_driver(drv->nr);
  1847. if (!normal)
  1848. goto out;
  1849. drv->tty_driver = normal;
  1850. normal->owner = drv->owner;
  1851. normal->driver_name = drv->driver_name;
  1852. normal->name = drv->dev_name;
  1853. normal->major = drv->major;
  1854. normal->minor_start = drv->minor;
  1855. normal->type = TTY_DRIVER_TYPE_SERIAL;
  1856. normal->subtype = SERIAL_TYPE_NORMAL;
  1857. normal->init_termios = tty_std_termios;
  1858. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1859. normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1860. normal->driver_state = drv;
  1861. tty_set_operations(normal, &uart_ops);
  1862. /*
  1863. * Initialise the UART state(s).
  1864. */
  1865. for (i = 0; i < drv->nr; i++) {
  1866. struct uart_state *state = drv->state + i;
  1867. state->close_delay = 500; /* .5 seconds */
  1868. state->closing_wait = 30000; /* 30 seconds */
  1869. mutex_init(&state->mutex);
  1870. }
  1871. retval = tty_register_driver(normal);
  1872. out:
  1873. if (retval < 0) {
  1874. put_tty_driver(normal);
  1875. kfree(drv->state);
  1876. }
  1877. return retval;
  1878. }
  1879. /**
  1880. * uart_unregister_driver - remove a driver from the uart core layer
  1881. * @drv: low level driver structure
  1882. *
  1883. * Remove all references to a driver from the core driver. The low
  1884. * level driver must have removed all its ports via the
  1885. * uart_remove_one_port() if it registered them with uart_add_one_port().
  1886. * (ie, drv->port == NULL)
  1887. */
  1888. void uart_unregister_driver(struct uart_driver *drv)
  1889. {
  1890. struct tty_driver *p = drv->tty_driver;
  1891. tty_unregister_driver(p);
  1892. put_tty_driver(p);
  1893. kfree(drv->state);
  1894. drv->tty_driver = NULL;
  1895. }
  1896. struct tty_driver *uart_console_device(struct console *co, int *index)
  1897. {
  1898. struct uart_driver *p = co->data;
  1899. *index = co->index;
  1900. return p->tty_driver;
  1901. }
  1902. /**
  1903. * uart_add_one_port - attach a driver-defined port structure
  1904. * @drv: pointer to the uart low level driver structure for this port
  1905. * @port: uart port structure to use for this port.
  1906. *
  1907. * This allows the driver to register its own uart_port structure
  1908. * with the core driver. The main purpose is to allow the low
  1909. * level uart drivers to expand uart_port, rather than having yet
  1910. * more levels of structures.
  1911. */
  1912. int uart_add_one_port(struct uart_driver *drv, struct uart_port *port)
  1913. {
  1914. struct uart_state *state;
  1915. int ret = 0;
  1916. BUG_ON(in_interrupt());
  1917. if (port->line >= drv->nr)
  1918. return -EINVAL;
  1919. state = drv->state + port->line;
  1920. mutex_lock(&port_mutex);
  1921. mutex_lock(&state->mutex);
  1922. if (state->port) {
  1923. ret = -EINVAL;
  1924. goto out;
  1925. }
  1926. state->port = port;
  1927. port->cons = drv->cons;
  1928. port->info = state->info;
  1929. /*
  1930. * If this port is a console, then the spinlock is already
  1931. * initialised.
  1932. */
  1933. if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
  1934. spin_lock_init(&port->lock);
  1935. lockdep_set_class(&port->lock, &port_lock_key);
  1936. }
  1937. uart_configure_port(drv, state, port);
  1938. /*
  1939. * Register the port whether it's detected or not. This allows
  1940. * setserial to be used to alter this ports parameters.
  1941. */
  1942. tty_register_device(drv->tty_driver, port->line, port->dev);
  1943. /*
  1944. * If this driver supports console, and it hasn't been
  1945. * successfully registered yet, try to re-register it.
  1946. * It may be that the port was not available.
  1947. */
  1948. if (port->type != PORT_UNKNOWN &&
  1949. port->cons && !(port->cons->flags & CON_ENABLED))
  1950. register_console(port->cons);
  1951. /*
  1952. * Ensure UPF_DEAD is not set.
  1953. */
  1954. port->flags &= ~UPF_DEAD;
  1955. out:
  1956. mutex_unlock(&state->mutex);
  1957. mutex_unlock(&port_mutex);
  1958. return ret;
  1959. }
  1960. /**
  1961. * uart_remove_one_port - detach a driver defined port structure
  1962. * @drv: pointer to the uart low level driver structure for this port
  1963. * @port: uart port structure for this port
  1964. *
  1965. * This unhooks (and hangs up) the specified port structure from the
  1966. * core driver. No further calls will be made to the low-level code
  1967. * for this port.
  1968. */
  1969. int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port)
  1970. {
  1971. struct uart_state *state = drv->state + port->line;
  1972. struct uart_info *info;
  1973. BUG_ON(in_interrupt());
  1974. if (state->port != port)
  1975. printk(KERN_ALERT "Removing wrong port: %p != %p\n",
  1976. state->port, port);
  1977. mutex_lock(&port_mutex);
  1978. /*
  1979. * Mark the port "dead" - this prevents any opens from
  1980. * succeeding while we shut down the port.
  1981. */
  1982. mutex_lock(&state->mutex);
  1983. port->flags |= UPF_DEAD;
  1984. mutex_unlock(&state->mutex);
  1985. /*
  1986. * Remove the devices from the tty layer
  1987. */
  1988. tty_unregister_device(drv->tty_driver, port->line);
  1989. info = state->info;
  1990. if (info && info->tty)
  1991. tty_vhangup(info->tty);
  1992. /*
  1993. * All users of this port should now be disconnected from
  1994. * this driver, and the port shut down. We should be the
  1995. * only thread fiddling with this port from now on.
  1996. */
  1997. state->info = NULL;
  1998. /*
  1999. * Free the port IO and memory resources, if any.
  2000. */
  2001. if (port->type != PORT_UNKNOWN)
  2002. port->ops->release_port(port);
  2003. /*
  2004. * Indicate that there isn't a port here anymore.
  2005. */
  2006. port->type = PORT_UNKNOWN;
  2007. /*
  2008. * Kill the tasklet, and free resources.
  2009. */
  2010. if (info) {
  2011. tasklet_kill(&info->tlet);
  2012. kfree(info);
  2013. }
  2014. state->port = NULL;
  2015. mutex_unlock(&port_mutex);
  2016. return 0;
  2017. }
  2018. /*
  2019. * Are the two ports equivalent?
  2020. */
  2021. int uart_match_port(struct uart_port *port1, struct uart_port *port2)
  2022. {
  2023. if (port1->iotype != port2->iotype)
  2024. return 0;
  2025. switch (port1->iotype) {
  2026. case UPIO_PORT:
  2027. return (port1->iobase == port2->iobase);
  2028. case UPIO_HUB6:
  2029. return (port1->iobase == port2->iobase) &&
  2030. (port1->hub6 == port2->hub6);
  2031. case UPIO_MEM:
  2032. case UPIO_MEM32:
  2033. case UPIO_AU:
  2034. case UPIO_TSI:
  2035. return (port1->mapbase == port2->mapbase);
  2036. }
  2037. return 0;
  2038. }
  2039. EXPORT_SYMBOL(uart_match_port);
  2040. EXPORT_SYMBOL(uart_write_wakeup);
  2041. EXPORT_SYMBOL(uart_register_driver);
  2042. EXPORT_SYMBOL(uart_unregister_driver);
  2043. EXPORT_SYMBOL(uart_suspend_port);
  2044. EXPORT_SYMBOL(uart_resume_port);
  2045. EXPORT_SYMBOL(uart_add_one_port);
  2046. EXPORT_SYMBOL(uart_remove_one_port);
  2047. MODULE_DESCRIPTION("Serial driver core");
  2048. MODULE_LICENSE("GPL");