serial_core.c 73 KB

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