serial_core.c 73 KB

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