serial_core.c 75 KB

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