serial_core.c 75 KB

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