serial_core.c 75 KB

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