serial_core.c 57 KB

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