serial_core.c 73 KB

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