serial_core.c 62 KB

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