serial_core.c 74 KB

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