serial_core.c 71 KB

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