serial_core.c 75 KB

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