serial_core.c 77 KB

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