serial_core.c 74 KB

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