serial_core.c 74 KB

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