serial_core.c 70 KB

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