serial_core.c 76 KB

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