serial_core.c 74 KB

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