serial_core.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  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. if (!state) {
  1182. struct uart_driver *drv = tty->driver->driver_state;
  1183. state = drv->state + tty->index;
  1184. port = &state->port;
  1185. spin_lock_irq(&port->lock);
  1186. --port->count;
  1187. spin_unlock_irq(&port->lock);
  1188. return;
  1189. }
  1190. uport = state->uart_port;
  1191. port = &state->port;
  1192. pr_debug("uart_close(%d) called\n", uport ? uport->line : -1);
  1193. if (!port->count || tty_port_close_start(port, tty, filp) == 0)
  1194. return;
  1195. /*
  1196. * At this point, we stop accepting input. To do this, we
  1197. * disable the receive line status interrupts.
  1198. */
  1199. if (port->flags & ASYNC_INITIALIZED) {
  1200. spin_lock_irq(&uport->lock);
  1201. uport->ops->stop_rx(uport);
  1202. spin_unlock_irq(&uport->lock);
  1203. /*
  1204. * Before we drop DTR, make sure the UART transmitter
  1205. * has completely drained; this is especially
  1206. * important if there is a transmit FIFO!
  1207. */
  1208. uart_wait_until_sent(tty, uport->timeout);
  1209. }
  1210. mutex_lock(&port->mutex);
  1211. uart_shutdown(tty, state);
  1212. tty_port_tty_set(port, NULL);
  1213. spin_lock_irq(&port->lock);
  1214. if (port->blocked_open) {
  1215. spin_unlock_irq(&port->lock);
  1216. if (port->close_delay)
  1217. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  1218. spin_lock_irq(&port->lock);
  1219. } else if (!uart_console(uport)) {
  1220. spin_unlock_irq(&port->lock);
  1221. uart_change_pm(state, UART_PM_STATE_OFF);
  1222. spin_lock_irq(&port->lock);
  1223. }
  1224. /*
  1225. * Wake up anyone trying to open this port.
  1226. */
  1227. clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
  1228. clear_bit(ASYNCB_CLOSING, &port->flags);
  1229. spin_unlock_irq(&port->lock);
  1230. wake_up_interruptible(&port->open_wait);
  1231. mutex_unlock(&port->mutex);
  1232. tty_ldisc_flush(tty);
  1233. tty->closing = 0;
  1234. }
  1235. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1236. {
  1237. struct uart_state *state = tty->driver_data;
  1238. struct uart_port *port = state->uart_port;
  1239. unsigned long char_time, expire;
  1240. if (port->type == PORT_UNKNOWN || port->fifosize == 0)
  1241. return;
  1242. /*
  1243. * Set the check interval to be 1/5 of the estimated time to
  1244. * send a single character, and make it at least 1. The check
  1245. * interval should also be less than the timeout.
  1246. *
  1247. * Note: we have to use pretty tight timings here to satisfy
  1248. * the NIST-PCTS.
  1249. */
  1250. char_time = (port->timeout - HZ/50) / port->fifosize;
  1251. char_time = char_time / 5;
  1252. if (char_time == 0)
  1253. char_time = 1;
  1254. if (timeout && timeout < char_time)
  1255. char_time = timeout;
  1256. /*
  1257. * If the transmitter hasn't cleared in twice the approximate
  1258. * amount of time to send the entire FIFO, it probably won't
  1259. * ever clear. This assumes the UART isn't doing flow
  1260. * control, which is currently the case. Hence, if it ever
  1261. * takes longer than port->timeout, this is probably due to a
  1262. * UART bug of some kind. So, we clamp the timeout parameter at
  1263. * 2*port->timeout.
  1264. */
  1265. if (timeout == 0 || timeout > 2 * port->timeout)
  1266. timeout = 2 * port->timeout;
  1267. expire = jiffies + timeout;
  1268. pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
  1269. port->line, jiffies, expire);
  1270. /*
  1271. * Check whether the transmitter is empty every 'char_time'.
  1272. * 'timeout' / 'expire' give us the maximum amount of time
  1273. * we wait.
  1274. */
  1275. while (!port->ops->tx_empty(port)) {
  1276. msleep_interruptible(jiffies_to_msecs(char_time));
  1277. if (signal_pending(current))
  1278. break;
  1279. if (time_after(jiffies, expire))
  1280. break;
  1281. }
  1282. }
  1283. /*
  1284. * Calls to uart_hangup() are serialised by the tty_lock in
  1285. * drivers/tty/tty_io.c:do_tty_hangup()
  1286. * This runs from a workqueue and can sleep for a _short_ time only.
  1287. */
  1288. static void uart_hangup(struct tty_struct *tty)
  1289. {
  1290. struct uart_state *state = tty->driver_data;
  1291. struct tty_port *port = &state->port;
  1292. unsigned long flags;
  1293. pr_debug("uart_hangup(%d)\n", state->uart_port->line);
  1294. mutex_lock(&port->mutex);
  1295. if (port->flags & ASYNC_NORMAL_ACTIVE) {
  1296. uart_flush_buffer(tty);
  1297. uart_shutdown(tty, state);
  1298. spin_lock_irqsave(&port->lock, flags);
  1299. port->count = 0;
  1300. clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
  1301. spin_unlock_irqrestore(&port->lock, flags);
  1302. tty_port_tty_set(port, NULL);
  1303. if (!uart_console(state->uart_port))
  1304. uart_change_pm(state, UART_PM_STATE_OFF);
  1305. wake_up_interruptible(&port->open_wait);
  1306. wake_up_interruptible(&port->delta_msr_wait);
  1307. }
  1308. mutex_unlock(&port->mutex);
  1309. }
  1310. static void uart_port_shutdown(struct tty_port *port)
  1311. {
  1312. struct uart_state *state = container_of(port, struct uart_state, port);
  1313. struct uart_port *uport = state->uart_port;
  1314. /*
  1315. * clear delta_msr_wait queue to avoid mem leaks: we may free
  1316. * the irq here so the queue might never be woken up. Note
  1317. * that we won't end up waiting on delta_msr_wait again since
  1318. * any outstanding file descriptors should be pointing at
  1319. * hung_up_tty_fops now.
  1320. */
  1321. wake_up_interruptible(&port->delta_msr_wait);
  1322. /*
  1323. * Free the IRQ and disable the port.
  1324. */
  1325. uport->ops->shutdown(uport);
  1326. /*
  1327. * Ensure that the IRQ handler isn't running on another CPU.
  1328. */
  1329. synchronize_irq(uport->irq);
  1330. }
  1331. static int uart_carrier_raised(struct tty_port *port)
  1332. {
  1333. struct uart_state *state = container_of(port, struct uart_state, port);
  1334. struct uart_port *uport = state->uart_port;
  1335. int mctrl;
  1336. spin_lock_irq(&uport->lock);
  1337. uart_enable_ms(uport);
  1338. mctrl = uport->ops->get_mctrl(uport);
  1339. spin_unlock_irq(&uport->lock);
  1340. if (mctrl & TIOCM_CAR)
  1341. return 1;
  1342. return 0;
  1343. }
  1344. static void uart_dtr_rts(struct tty_port *port, int onoff)
  1345. {
  1346. struct uart_state *state = container_of(port, struct uart_state, port);
  1347. struct uart_port *uport = state->uart_port;
  1348. if (onoff)
  1349. uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1350. else
  1351. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1352. }
  1353. /*
  1354. * Calls to uart_open are serialised by the tty_lock in
  1355. * drivers/tty/tty_io.c:tty_open()
  1356. * Note that if this fails, then uart_close() _will_ be called.
  1357. *
  1358. * In time, we want to scrap the "opening nonpresent ports"
  1359. * behaviour and implement an alternative way for setserial
  1360. * to set base addresses/ports/types. This will allow us to
  1361. * get rid of a certain amount of extra tests.
  1362. */
  1363. static int uart_open(struct tty_struct *tty, struct file *filp)
  1364. {
  1365. struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;
  1366. int retval, line = tty->index;
  1367. struct uart_state *state = drv->state + line;
  1368. struct tty_port *port = &state->port;
  1369. pr_debug("uart_open(%d) called\n", line);
  1370. spin_lock_irq(&port->lock);
  1371. ++port->count;
  1372. spin_unlock_irq(&port->lock);
  1373. /*
  1374. * We take the semaphore here to guarantee that we won't be re-entered
  1375. * while allocating the state structure, or while we request any IRQs
  1376. * that the driver may need. This also has the nice side-effect that
  1377. * it delays the action of uart_hangup, so we can guarantee that
  1378. * state->port.tty will always contain something reasonable.
  1379. */
  1380. if (mutex_lock_interruptible(&port->mutex)) {
  1381. retval = -ERESTARTSYS;
  1382. goto end;
  1383. }
  1384. if (!state->uart_port || state->uart_port->flags & UPF_DEAD) {
  1385. retval = -ENXIO;
  1386. goto err_unlock;
  1387. }
  1388. tty->driver_data = state;
  1389. state->uart_port->state = state;
  1390. state->port.low_latency =
  1391. (state->uart_port->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1392. tty_port_tty_set(port, tty);
  1393. /*
  1394. * Start up the serial port.
  1395. */
  1396. retval = uart_startup(tty, state, 0);
  1397. /*
  1398. * If we succeeded, wait until the port is ready.
  1399. */
  1400. mutex_unlock(&port->mutex);
  1401. if (retval == 0)
  1402. retval = tty_port_block_til_ready(port, tty, filp);
  1403. end:
  1404. return retval;
  1405. err_unlock:
  1406. mutex_unlock(&port->mutex);
  1407. goto end;
  1408. }
  1409. static const char *uart_type(struct uart_port *port)
  1410. {
  1411. const char *str = NULL;
  1412. if (port->ops->type)
  1413. str = port->ops->type(port);
  1414. if (!str)
  1415. str = "unknown";
  1416. return str;
  1417. }
  1418. #ifdef CONFIG_PROC_FS
  1419. static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
  1420. {
  1421. struct uart_state *state = drv->state + i;
  1422. struct tty_port *port = &state->port;
  1423. enum uart_pm_state pm_state;
  1424. struct uart_port *uport = state->uart_port;
  1425. char stat_buf[32];
  1426. unsigned int status;
  1427. int mmio;
  1428. if (!uport)
  1429. return;
  1430. mmio = uport->iotype >= UPIO_MEM;
  1431. seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
  1432. uport->line, uart_type(uport),
  1433. mmio ? "mmio:0x" : "port:",
  1434. mmio ? (unsigned long long)uport->mapbase
  1435. : (unsigned long long)uport->iobase,
  1436. uport->irq);
  1437. if (uport->type == PORT_UNKNOWN) {
  1438. seq_putc(m, '\n');
  1439. return;
  1440. }
  1441. if (capable(CAP_SYS_ADMIN)) {
  1442. mutex_lock(&port->mutex);
  1443. pm_state = state->pm_state;
  1444. if (pm_state != UART_PM_STATE_ON)
  1445. uart_change_pm(state, UART_PM_STATE_ON);
  1446. spin_lock_irq(&uport->lock);
  1447. status = uport->ops->get_mctrl(uport);
  1448. spin_unlock_irq(&uport->lock);
  1449. if (pm_state != UART_PM_STATE_ON)
  1450. uart_change_pm(state, pm_state);
  1451. mutex_unlock(&port->mutex);
  1452. seq_printf(m, " tx:%d rx:%d",
  1453. uport->icount.tx, uport->icount.rx);
  1454. if (uport->icount.frame)
  1455. seq_printf(m, " fe:%d",
  1456. uport->icount.frame);
  1457. if (uport->icount.parity)
  1458. seq_printf(m, " pe:%d",
  1459. uport->icount.parity);
  1460. if (uport->icount.brk)
  1461. seq_printf(m, " brk:%d",
  1462. uport->icount.brk);
  1463. if (uport->icount.overrun)
  1464. seq_printf(m, " oe:%d",
  1465. uport->icount.overrun);
  1466. #define INFOBIT(bit, str) \
  1467. if (uport->mctrl & (bit)) \
  1468. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1469. strlen(stat_buf) - 2)
  1470. #define STATBIT(bit, str) \
  1471. if (status & (bit)) \
  1472. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1473. strlen(stat_buf) - 2)
  1474. stat_buf[0] = '\0';
  1475. stat_buf[1] = '\0';
  1476. INFOBIT(TIOCM_RTS, "|RTS");
  1477. STATBIT(TIOCM_CTS, "|CTS");
  1478. INFOBIT(TIOCM_DTR, "|DTR");
  1479. STATBIT(TIOCM_DSR, "|DSR");
  1480. STATBIT(TIOCM_CAR, "|CD");
  1481. STATBIT(TIOCM_RNG, "|RI");
  1482. if (stat_buf[0])
  1483. stat_buf[0] = ' ';
  1484. seq_puts(m, stat_buf);
  1485. }
  1486. seq_putc(m, '\n');
  1487. #undef STATBIT
  1488. #undef INFOBIT
  1489. }
  1490. static int uart_proc_show(struct seq_file *m, void *v)
  1491. {
  1492. struct tty_driver *ttydrv = m->private;
  1493. struct uart_driver *drv = ttydrv->driver_state;
  1494. int i;
  1495. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  1496. "", "", "");
  1497. for (i = 0; i < drv->nr; i++)
  1498. uart_line_info(m, drv, i);
  1499. return 0;
  1500. }
  1501. static int uart_proc_open(struct inode *inode, struct file *file)
  1502. {
  1503. return single_open(file, uart_proc_show, PDE_DATA(inode));
  1504. }
  1505. static const struct file_operations uart_proc_fops = {
  1506. .owner = THIS_MODULE,
  1507. .open = uart_proc_open,
  1508. .read = seq_read,
  1509. .llseek = seq_lseek,
  1510. .release = single_release,
  1511. };
  1512. #endif
  1513. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  1514. /**
  1515. * uart_console_write - write a console message to a serial port
  1516. * @port: the port to write the message
  1517. * @s: array of characters
  1518. * @count: number of characters in string to write
  1519. * @putchar: function to write character to port
  1520. */
  1521. void uart_console_write(struct uart_port *port, const char *s,
  1522. unsigned int count,
  1523. void (*putchar)(struct uart_port *, int))
  1524. {
  1525. unsigned int i;
  1526. for (i = 0; i < count; i++, s++) {
  1527. if (*s == '\n')
  1528. putchar(port, '\r');
  1529. putchar(port, *s);
  1530. }
  1531. }
  1532. EXPORT_SYMBOL_GPL(uart_console_write);
  1533. /*
  1534. * Check whether an invalid uart number has been specified, and
  1535. * if so, search for the first available port that does have
  1536. * console support.
  1537. */
  1538. struct uart_port * __init
  1539. uart_get_console(struct uart_port *ports, int nr, struct console *co)
  1540. {
  1541. int idx = co->index;
  1542. if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
  1543. ports[idx].membase == NULL))
  1544. for (idx = 0; idx < nr; idx++)
  1545. if (ports[idx].iobase != 0 ||
  1546. ports[idx].membase != NULL)
  1547. break;
  1548. co->index = idx;
  1549. return ports + idx;
  1550. }
  1551. /**
  1552. * uart_parse_earlycon - Parse earlycon options
  1553. * @p: ptr to 2nd field (ie., just beyond '<name>,')
  1554. * @iotype: ptr for decoded iotype (out)
  1555. * @addr: ptr for decoded mapbase/iobase (out)
  1556. * @options: ptr for <options> field; NULL if not present (out)
  1557. *
  1558. * Decodes earlycon kernel command line parameters of the form
  1559. * earlycon=<name>,io|mmio|mmio32|mmio32be|mmio32native,<addr>,<options>
  1560. * console=<name>,io|mmio|mmio32|mmio32be|mmio32native,<addr>,<options>
  1561. *
  1562. * The optional form
  1563. * earlycon=<name>,0x<addr>,<options>
  1564. * console=<name>,0x<addr>,<options>
  1565. * is also accepted; the returned @iotype will be UPIO_MEM.
  1566. *
  1567. * Returns 0 on success or -EINVAL on failure
  1568. */
  1569. int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
  1570. char **options)
  1571. {
  1572. if (strncmp(p, "mmio,", 5) == 0) {
  1573. *iotype = UPIO_MEM;
  1574. p += 5;
  1575. } else if (strncmp(p, "mmio32,", 7) == 0) {
  1576. *iotype = UPIO_MEM32;
  1577. p += 7;
  1578. } else if (strncmp(p, "mmio32be,", 9) == 0) {
  1579. *iotype = UPIO_MEM32BE;
  1580. p += 9;
  1581. } else if (strncmp(p, "mmio32native,", 13) == 0) {
  1582. *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
  1583. UPIO_MEM32BE : UPIO_MEM32;
  1584. p += 13;
  1585. } else if (strncmp(p, "io,", 3) == 0) {
  1586. *iotype = UPIO_PORT;
  1587. p += 3;
  1588. } else if (strncmp(p, "0x", 2) == 0) {
  1589. *iotype = UPIO_MEM;
  1590. } else {
  1591. return -EINVAL;
  1592. }
  1593. *addr = simple_strtoul(p, NULL, 0);
  1594. p = strchr(p, ',');
  1595. if (p)
  1596. p++;
  1597. *options = p;
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL_GPL(uart_parse_earlycon);
  1601. /**
  1602. * uart_parse_options - Parse serial port baud/parity/bits/flow control.
  1603. * @options: pointer to option string
  1604. * @baud: pointer to an 'int' variable for the baud rate.
  1605. * @parity: pointer to an 'int' variable for the parity.
  1606. * @bits: pointer to an 'int' variable for the number of data bits.
  1607. * @flow: pointer to an 'int' variable for the flow control character.
  1608. *
  1609. * uart_parse_options decodes a string containing the serial console
  1610. * options. The format of the string is <baud><parity><bits><flow>,
  1611. * eg: 115200n8r
  1612. */
  1613. void
  1614. uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
  1615. {
  1616. char *s = options;
  1617. *baud = simple_strtoul(s, NULL, 10);
  1618. while (*s >= '0' && *s <= '9')
  1619. s++;
  1620. if (*s)
  1621. *parity = *s++;
  1622. if (*s)
  1623. *bits = *s++ - '0';
  1624. if (*s)
  1625. *flow = *s;
  1626. }
  1627. EXPORT_SYMBOL_GPL(uart_parse_options);
  1628. struct baud_rates {
  1629. unsigned int rate;
  1630. unsigned int cflag;
  1631. };
  1632. static const struct baud_rates baud_rates[] = {
  1633. { 921600, B921600 },
  1634. { 460800, B460800 },
  1635. { 230400, B230400 },
  1636. { 115200, B115200 },
  1637. { 57600, B57600 },
  1638. { 38400, B38400 },
  1639. { 19200, B19200 },
  1640. { 9600, B9600 },
  1641. { 4800, B4800 },
  1642. { 2400, B2400 },
  1643. { 1200, B1200 },
  1644. { 0, B38400 }
  1645. };
  1646. /**
  1647. * uart_set_options - setup the serial console parameters
  1648. * @port: pointer to the serial ports uart_port structure
  1649. * @co: console pointer
  1650. * @baud: baud rate
  1651. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1652. * @bits: number of data bits
  1653. * @flow: flow control character - 'r' (rts)
  1654. */
  1655. int
  1656. uart_set_options(struct uart_port *port, struct console *co,
  1657. int baud, int parity, int bits, int flow)
  1658. {
  1659. struct ktermios termios;
  1660. static struct ktermios dummy;
  1661. int i;
  1662. /*
  1663. * Ensure that the serial console lock is initialised
  1664. * early.
  1665. * If this port is a console, then the spinlock is already
  1666. * initialised.
  1667. */
  1668. if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
  1669. spin_lock_init(&port->lock);
  1670. lockdep_set_class(&port->lock, &port_lock_key);
  1671. }
  1672. memset(&termios, 0, sizeof(struct ktermios));
  1673. termios.c_cflag = CREAD | HUPCL | CLOCAL;
  1674. /*
  1675. * Construct a cflag setting.
  1676. */
  1677. for (i = 0; baud_rates[i].rate; i++)
  1678. if (baud_rates[i].rate <= baud)
  1679. break;
  1680. termios.c_cflag |= baud_rates[i].cflag;
  1681. if (bits == 7)
  1682. termios.c_cflag |= CS7;
  1683. else
  1684. termios.c_cflag |= CS8;
  1685. switch (parity) {
  1686. case 'o': case 'O':
  1687. termios.c_cflag |= PARODD;
  1688. /*fall through*/
  1689. case 'e': case 'E':
  1690. termios.c_cflag |= PARENB;
  1691. break;
  1692. }
  1693. if (flow == 'r')
  1694. termios.c_cflag |= CRTSCTS;
  1695. /*
  1696. * some uarts on other side don't support no flow control.
  1697. * So we set * DTR in host uart to make them happy
  1698. */
  1699. port->mctrl |= TIOCM_DTR;
  1700. port->ops->set_termios(port, &termios, &dummy);
  1701. /*
  1702. * Allow the setting of the UART parameters with a NULL console
  1703. * too:
  1704. */
  1705. if (co)
  1706. co->cflag = termios.c_cflag;
  1707. return 0;
  1708. }
  1709. EXPORT_SYMBOL_GPL(uart_set_options);
  1710. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1711. /**
  1712. * uart_change_pm - set power state of the port
  1713. *
  1714. * @state: port descriptor
  1715. * @pm_state: new state
  1716. *
  1717. * Locking: port->mutex has to be held
  1718. */
  1719. static void uart_change_pm(struct uart_state *state,
  1720. enum uart_pm_state pm_state)
  1721. {
  1722. struct uart_port *port = state->uart_port;
  1723. if (state->pm_state != pm_state) {
  1724. if (port->ops->pm)
  1725. port->ops->pm(port, pm_state, state->pm_state);
  1726. state->pm_state = pm_state;
  1727. }
  1728. }
  1729. struct uart_match {
  1730. struct uart_port *port;
  1731. struct uart_driver *driver;
  1732. };
  1733. static int serial_match_port(struct device *dev, void *data)
  1734. {
  1735. struct uart_match *match = data;
  1736. struct tty_driver *tty_drv = match->driver->tty_driver;
  1737. dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
  1738. match->port->line;
  1739. return dev->devt == devt; /* Actually, only one tty per port */
  1740. }
  1741. int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
  1742. {
  1743. struct uart_state *state = drv->state + uport->line;
  1744. struct tty_port *port = &state->port;
  1745. struct device *tty_dev;
  1746. struct uart_match match = {uport, drv};
  1747. mutex_lock(&port->mutex);
  1748. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1749. if (device_may_wakeup(tty_dev)) {
  1750. if (!enable_irq_wake(uport->irq))
  1751. uport->irq_wake = 1;
  1752. put_device(tty_dev);
  1753. mutex_unlock(&port->mutex);
  1754. return 0;
  1755. }
  1756. put_device(tty_dev);
  1757. /* Nothing to do if the console is not suspending */
  1758. if (!console_suspend_enabled && uart_console(uport))
  1759. goto unlock;
  1760. uport->suspended = 1;
  1761. if (port->flags & ASYNC_INITIALIZED) {
  1762. const struct uart_ops *ops = uport->ops;
  1763. int tries;
  1764. set_bit(ASYNCB_SUSPENDED, &port->flags);
  1765. clear_bit(ASYNCB_INITIALIZED, &port->flags);
  1766. spin_lock_irq(&uport->lock);
  1767. ops->stop_tx(uport);
  1768. ops->set_mctrl(uport, 0);
  1769. ops->stop_rx(uport);
  1770. spin_unlock_irq(&uport->lock);
  1771. /*
  1772. * Wait for the transmitter to empty.
  1773. */
  1774. for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
  1775. msleep(10);
  1776. if (!tries)
  1777. dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
  1778. drv->dev_name,
  1779. drv->tty_driver->name_base + uport->line);
  1780. ops->shutdown(uport);
  1781. }
  1782. /*
  1783. * Disable the console device before suspending.
  1784. */
  1785. if (uart_console(uport))
  1786. console_stop(uport->cons);
  1787. uart_change_pm(state, UART_PM_STATE_OFF);
  1788. unlock:
  1789. mutex_unlock(&port->mutex);
  1790. return 0;
  1791. }
  1792. int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
  1793. {
  1794. struct uart_state *state = drv->state + uport->line;
  1795. struct tty_port *port = &state->port;
  1796. struct device *tty_dev;
  1797. struct uart_match match = {uport, drv};
  1798. struct ktermios termios;
  1799. mutex_lock(&port->mutex);
  1800. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1801. if (!uport->suspended && device_may_wakeup(tty_dev)) {
  1802. if (uport->irq_wake) {
  1803. disable_irq_wake(uport->irq);
  1804. uport->irq_wake = 0;
  1805. }
  1806. put_device(tty_dev);
  1807. mutex_unlock(&port->mutex);
  1808. return 0;
  1809. }
  1810. put_device(tty_dev);
  1811. uport->suspended = 0;
  1812. /*
  1813. * Re-enable the console device after suspending.
  1814. */
  1815. if (uart_console(uport)) {
  1816. /*
  1817. * First try to use the console cflag setting.
  1818. */
  1819. memset(&termios, 0, sizeof(struct ktermios));
  1820. termios.c_cflag = uport->cons->cflag;
  1821. /*
  1822. * If that's unset, use the tty termios setting.
  1823. */
  1824. if (port->tty && termios.c_cflag == 0)
  1825. termios = port->tty->termios;
  1826. if (console_suspend_enabled)
  1827. uart_change_pm(state, UART_PM_STATE_ON);
  1828. uport->ops->set_termios(uport, &termios, NULL);
  1829. if (console_suspend_enabled)
  1830. console_start(uport->cons);
  1831. }
  1832. if (port->flags & ASYNC_SUSPENDED) {
  1833. const struct uart_ops *ops = uport->ops;
  1834. int ret;
  1835. uart_change_pm(state, UART_PM_STATE_ON);
  1836. spin_lock_irq(&uport->lock);
  1837. ops->set_mctrl(uport, 0);
  1838. spin_unlock_irq(&uport->lock);
  1839. if (console_suspend_enabled || !uart_console(uport)) {
  1840. /* Protected by port mutex for now */
  1841. struct tty_struct *tty = port->tty;
  1842. ret = ops->startup(uport);
  1843. if (ret == 0) {
  1844. if (tty)
  1845. uart_change_speed(tty, state, NULL);
  1846. spin_lock_irq(&uport->lock);
  1847. ops->set_mctrl(uport, uport->mctrl);
  1848. ops->start_tx(uport);
  1849. spin_unlock_irq(&uport->lock);
  1850. set_bit(ASYNCB_INITIALIZED, &port->flags);
  1851. } else {
  1852. /*
  1853. * Failed to resume - maybe hardware went away?
  1854. * Clear the "initialized" flag so we won't try
  1855. * to call the low level drivers shutdown method.
  1856. */
  1857. uart_shutdown(tty, state);
  1858. }
  1859. }
  1860. clear_bit(ASYNCB_SUSPENDED, &port->flags);
  1861. }
  1862. mutex_unlock(&port->mutex);
  1863. return 0;
  1864. }
  1865. static inline void
  1866. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  1867. {
  1868. char address[64];
  1869. switch (port->iotype) {
  1870. case UPIO_PORT:
  1871. snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
  1872. break;
  1873. case UPIO_HUB6:
  1874. snprintf(address, sizeof(address),
  1875. "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
  1876. break;
  1877. case UPIO_MEM:
  1878. case UPIO_MEM32:
  1879. case UPIO_MEM32BE:
  1880. case UPIO_AU:
  1881. case UPIO_TSI:
  1882. snprintf(address, sizeof(address),
  1883. "MMIO 0x%llx", (unsigned long long)port->mapbase);
  1884. break;
  1885. default:
  1886. strlcpy(address, "*unknown*", sizeof(address));
  1887. break;
  1888. }
  1889. printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
  1890. port->dev ? dev_name(port->dev) : "",
  1891. port->dev ? ": " : "",
  1892. drv->dev_name,
  1893. drv->tty_driver->name_base + port->line,
  1894. address, port->irq, port->uartclk / 16, uart_type(port));
  1895. }
  1896. static void
  1897. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  1898. struct uart_port *port)
  1899. {
  1900. unsigned int flags;
  1901. /*
  1902. * If there isn't a port here, don't do anything further.
  1903. */
  1904. if (!port->iobase && !port->mapbase && !port->membase)
  1905. return;
  1906. /*
  1907. * Now do the auto configuration stuff. Note that config_port
  1908. * is expected to claim the resources and map the port for us.
  1909. */
  1910. flags = 0;
  1911. if (port->flags & UPF_AUTO_IRQ)
  1912. flags |= UART_CONFIG_IRQ;
  1913. if (port->flags & UPF_BOOT_AUTOCONF) {
  1914. if (!(port->flags & UPF_FIXED_TYPE)) {
  1915. port->type = PORT_UNKNOWN;
  1916. flags |= UART_CONFIG_TYPE;
  1917. }
  1918. port->ops->config_port(port, flags);
  1919. }
  1920. if (port->type != PORT_UNKNOWN) {
  1921. unsigned long flags;
  1922. uart_report_port(drv, port);
  1923. /* Power up port for set_mctrl() */
  1924. uart_change_pm(state, UART_PM_STATE_ON);
  1925. /*
  1926. * Ensure that the modem control lines are de-activated.
  1927. * keep the DTR setting that is set in uart_set_options()
  1928. * We probably don't need a spinlock around this, but
  1929. */
  1930. spin_lock_irqsave(&port->lock, flags);
  1931. port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
  1932. spin_unlock_irqrestore(&port->lock, flags);
  1933. /*
  1934. * If this driver supports console, and it hasn't been
  1935. * successfully registered yet, try to re-register it.
  1936. * It may be that the port was not available.
  1937. */
  1938. if (port->cons && !(port->cons->flags & CON_ENABLED))
  1939. register_console(port->cons);
  1940. /*
  1941. * Power down all ports by default, except the
  1942. * console if we have one.
  1943. */
  1944. if (!uart_console(port))
  1945. uart_change_pm(state, UART_PM_STATE_OFF);
  1946. }
  1947. }
  1948. #ifdef CONFIG_CONSOLE_POLL
  1949. static int uart_poll_init(struct tty_driver *driver, int line, char *options)
  1950. {
  1951. struct uart_driver *drv = driver->driver_state;
  1952. struct uart_state *state = drv->state + line;
  1953. struct uart_port *port;
  1954. int baud = 9600;
  1955. int bits = 8;
  1956. int parity = 'n';
  1957. int flow = 'n';
  1958. int ret;
  1959. if (!state || !state->uart_port)
  1960. return -1;
  1961. port = state->uart_port;
  1962. if (!(port->ops->poll_get_char && port->ops->poll_put_char))
  1963. return -1;
  1964. if (port->ops->poll_init) {
  1965. struct tty_port *tport = &state->port;
  1966. ret = 0;
  1967. mutex_lock(&tport->mutex);
  1968. /*
  1969. * We don't set ASYNCB_INITIALIZED as we only initialized the
  1970. * hw, e.g. state->xmit is still uninitialized.
  1971. */
  1972. if (!test_bit(ASYNCB_INITIALIZED, &tport->flags))
  1973. ret = port->ops->poll_init(port);
  1974. mutex_unlock(&tport->mutex);
  1975. if (ret)
  1976. return ret;
  1977. }
  1978. if (options) {
  1979. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1980. return uart_set_options(port, NULL, baud, parity, bits, flow);
  1981. }
  1982. return 0;
  1983. }
  1984. static int uart_poll_get_char(struct tty_driver *driver, int line)
  1985. {
  1986. struct uart_driver *drv = driver->driver_state;
  1987. struct uart_state *state = drv->state + line;
  1988. struct uart_port *port;
  1989. if (!state || !state->uart_port)
  1990. return -1;
  1991. port = state->uart_port;
  1992. return port->ops->poll_get_char(port);
  1993. }
  1994. static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
  1995. {
  1996. struct uart_driver *drv = driver->driver_state;
  1997. struct uart_state *state = drv->state + line;
  1998. struct uart_port *port;
  1999. if (!state || !state->uart_port)
  2000. return;
  2001. port = state->uart_port;
  2002. if (ch == '\n')
  2003. port->ops->poll_put_char(port, '\r');
  2004. port->ops->poll_put_char(port, ch);
  2005. }
  2006. #endif
  2007. static const struct tty_operations uart_ops = {
  2008. .open = uart_open,
  2009. .close = uart_close,
  2010. .write = uart_write,
  2011. .put_char = uart_put_char,
  2012. .flush_chars = uart_flush_chars,
  2013. .write_room = uart_write_room,
  2014. .chars_in_buffer= uart_chars_in_buffer,
  2015. .flush_buffer = uart_flush_buffer,
  2016. .ioctl = uart_ioctl,
  2017. .throttle = uart_throttle,
  2018. .unthrottle = uart_unthrottle,
  2019. .send_xchar = uart_send_xchar,
  2020. .set_termios = uart_set_termios,
  2021. .set_ldisc = uart_set_ldisc,
  2022. .stop = uart_stop,
  2023. .start = uart_start,
  2024. .hangup = uart_hangup,
  2025. .break_ctl = uart_break_ctl,
  2026. .wait_until_sent= uart_wait_until_sent,
  2027. #ifdef CONFIG_PROC_FS
  2028. .proc_fops = &uart_proc_fops,
  2029. #endif
  2030. .tiocmget = uart_tiocmget,
  2031. .tiocmset = uart_tiocmset,
  2032. .get_icount = uart_get_icount,
  2033. #ifdef CONFIG_CONSOLE_POLL
  2034. .poll_init = uart_poll_init,
  2035. .poll_get_char = uart_poll_get_char,
  2036. .poll_put_char = uart_poll_put_char,
  2037. #endif
  2038. };
  2039. static const struct tty_port_operations uart_port_ops = {
  2040. .carrier_raised = uart_carrier_raised,
  2041. .dtr_rts = uart_dtr_rts,
  2042. };
  2043. /**
  2044. * uart_register_driver - register a driver with the uart core layer
  2045. * @drv: low level driver structure
  2046. *
  2047. * Register a uart driver with the core driver. We in turn register
  2048. * with the tty layer, and initialise the core driver per-port state.
  2049. *
  2050. * We have a proc file in /proc/tty/driver which is named after the
  2051. * normal driver.
  2052. *
  2053. * drv->port should be NULL, and the per-port structures should be
  2054. * registered using uart_add_one_port after this call has succeeded.
  2055. */
  2056. int uart_register_driver(struct uart_driver *drv)
  2057. {
  2058. struct tty_driver *normal;
  2059. int i, retval;
  2060. BUG_ON(drv->state);
  2061. /*
  2062. * Maybe we should be using a slab cache for this, especially if
  2063. * we have a large number of ports to handle.
  2064. */
  2065. drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
  2066. if (!drv->state)
  2067. goto out;
  2068. normal = alloc_tty_driver(drv->nr);
  2069. if (!normal)
  2070. goto out_kfree;
  2071. drv->tty_driver = normal;
  2072. normal->driver_name = drv->driver_name;
  2073. normal->name = drv->dev_name;
  2074. normal->major = drv->major;
  2075. normal->minor_start = drv->minor;
  2076. normal->type = TTY_DRIVER_TYPE_SERIAL;
  2077. normal->subtype = SERIAL_TYPE_NORMAL;
  2078. normal->init_termios = tty_std_termios;
  2079. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2080. normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
  2081. normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2082. normal->driver_state = drv;
  2083. tty_set_operations(normal, &uart_ops);
  2084. /*
  2085. * Initialise the UART state(s).
  2086. */
  2087. for (i = 0; i < drv->nr; i++) {
  2088. struct uart_state *state = drv->state + i;
  2089. struct tty_port *port = &state->port;
  2090. tty_port_init(port);
  2091. port->ops = &uart_port_ops;
  2092. }
  2093. retval = tty_register_driver(normal);
  2094. if (retval >= 0)
  2095. return retval;
  2096. for (i = 0; i < drv->nr; i++)
  2097. tty_port_destroy(&drv->state[i].port);
  2098. put_tty_driver(normal);
  2099. out_kfree:
  2100. kfree(drv->state);
  2101. out:
  2102. return -ENOMEM;
  2103. }
  2104. /**
  2105. * uart_unregister_driver - remove a driver from the uart core layer
  2106. * @drv: low level driver structure
  2107. *
  2108. * Remove all references to a driver from the core driver. The low
  2109. * level driver must have removed all its ports via the
  2110. * uart_remove_one_port() if it registered them with uart_add_one_port().
  2111. * (ie, drv->port == NULL)
  2112. */
  2113. void uart_unregister_driver(struct uart_driver *drv)
  2114. {
  2115. struct tty_driver *p = drv->tty_driver;
  2116. unsigned int i;
  2117. tty_unregister_driver(p);
  2118. put_tty_driver(p);
  2119. for (i = 0; i < drv->nr; i++)
  2120. tty_port_destroy(&drv->state[i].port);
  2121. kfree(drv->state);
  2122. drv->state = NULL;
  2123. drv->tty_driver = NULL;
  2124. }
  2125. struct tty_driver *uart_console_device(struct console *co, int *index)
  2126. {
  2127. struct uart_driver *p = co->data;
  2128. *index = co->index;
  2129. return p->tty_driver;
  2130. }
  2131. static ssize_t uart_get_attr_uartclk(struct device *dev,
  2132. struct device_attribute *attr, char *buf)
  2133. {
  2134. struct serial_struct tmp;
  2135. struct tty_port *port = dev_get_drvdata(dev);
  2136. uart_get_info(port, &tmp);
  2137. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
  2138. }
  2139. static ssize_t uart_get_attr_type(struct device *dev,
  2140. struct device_attribute *attr, char *buf)
  2141. {
  2142. struct serial_struct tmp;
  2143. struct tty_port *port = dev_get_drvdata(dev);
  2144. uart_get_info(port, &tmp);
  2145. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
  2146. }
  2147. static ssize_t uart_get_attr_line(struct device *dev,
  2148. struct device_attribute *attr, char *buf)
  2149. {
  2150. struct serial_struct tmp;
  2151. struct tty_port *port = dev_get_drvdata(dev);
  2152. uart_get_info(port, &tmp);
  2153. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
  2154. }
  2155. static ssize_t uart_get_attr_port(struct device *dev,
  2156. struct device_attribute *attr, char *buf)
  2157. {
  2158. struct serial_struct tmp;
  2159. struct tty_port *port = dev_get_drvdata(dev);
  2160. unsigned long ioaddr;
  2161. uart_get_info(port, &tmp);
  2162. ioaddr = tmp.port;
  2163. if (HIGH_BITS_OFFSET)
  2164. ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
  2165. return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
  2166. }
  2167. static ssize_t uart_get_attr_irq(struct device *dev,
  2168. struct device_attribute *attr, char *buf)
  2169. {
  2170. struct serial_struct tmp;
  2171. struct tty_port *port = dev_get_drvdata(dev);
  2172. uart_get_info(port, &tmp);
  2173. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
  2174. }
  2175. static ssize_t uart_get_attr_flags(struct device *dev,
  2176. struct device_attribute *attr, char *buf)
  2177. {
  2178. struct serial_struct tmp;
  2179. struct tty_port *port = dev_get_drvdata(dev);
  2180. uart_get_info(port, &tmp);
  2181. return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
  2182. }
  2183. static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
  2184. struct device_attribute *attr, char *buf)
  2185. {
  2186. struct serial_struct tmp;
  2187. struct tty_port *port = dev_get_drvdata(dev);
  2188. uart_get_info(port, &tmp);
  2189. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
  2190. }
  2191. static ssize_t uart_get_attr_close_delay(struct device *dev,
  2192. struct device_attribute *attr, char *buf)
  2193. {
  2194. struct serial_struct tmp;
  2195. struct tty_port *port = dev_get_drvdata(dev);
  2196. uart_get_info(port, &tmp);
  2197. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
  2198. }
  2199. static ssize_t uart_get_attr_closing_wait(struct device *dev,
  2200. struct device_attribute *attr, char *buf)
  2201. {
  2202. struct serial_struct tmp;
  2203. struct tty_port *port = dev_get_drvdata(dev);
  2204. uart_get_info(port, &tmp);
  2205. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
  2206. }
  2207. static ssize_t uart_get_attr_custom_divisor(struct device *dev,
  2208. struct device_attribute *attr, char *buf)
  2209. {
  2210. struct serial_struct tmp;
  2211. struct tty_port *port = dev_get_drvdata(dev);
  2212. uart_get_info(port, &tmp);
  2213. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
  2214. }
  2215. static ssize_t uart_get_attr_io_type(struct device *dev,
  2216. struct device_attribute *attr, char *buf)
  2217. {
  2218. struct serial_struct tmp;
  2219. struct tty_port *port = dev_get_drvdata(dev);
  2220. uart_get_info(port, &tmp);
  2221. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
  2222. }
  2223. static ssize_t uart_get_attr_iomem_base(struct device *dev,
  2224. struct device_attribute *attr, char *buf)
  2225. {
  2226. struct serial_struct tmp;
  2227. struct tty_port *port = dev_get_drvdata(dev);
  2228. uart_get_info(port, &tmp);
  2229. return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
  2230. }
  2231. static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
  2232. struct device_attribute *attr, char *buf)
  2233. {
  2234. struct serial_struct tmp;
  2235. struct tty_port *port = dev_get_drvdata(dev);
  2236. uart_get_info(port, &tmp);
  2237. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
  2238. }
  2239. static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
  2240. static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
  2241. static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
  2242. static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
  2243. static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
  2244. static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
  2245. static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
  2246. static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
  2247. static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
  2248. static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
  2249. static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
  2250. static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
  2251. static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
  2252. static struct attribute *tty_dev_attrs[] = {
  2253. &dev_attr_type.attr,
  2254. &dev_attr_line.attr,
  2255. &dev_attr_port.attr,
  2256. &dev_attr_irq.attr,
  2257. &dev_attr_flags.attr,
  2258. &dev_attr_xmit_fifo_size.attr,
  2259. &dev_attr_uartclk.attr,
  2260. &dev_attr_close_delay.attr,
  2261. &dev_attr_closing_wait.attr,
  2262. &dev_attr_custom_divisor.attr,
  2263. &dev_attr_io_type.attr,
  2264. &dev_attr_iomem_base.attr,
  2265. &dev_attr_iomem_reg_shift.attr,
  2266. NULL,
  2267. };
  2268. static const struct attribute_group tty_dev_attr_group = {
  2269. .attrs = tty_dev_attrs,
  2270. };
  2271. /**
  2272. * uart_add_one_port - attach a driver-defined port structure
  2273. * @drv: pointer to the uart low level driver structure for this port
  2274. * @uport: uart port structure to use for this port.
  2275. *
  2276. * This allows the driver to register its own uart_port structure
  2277. * with the core driver. The main purpose is to allow the low
  2278. * level uart drivers to expand uart_port, rather than having yet
  2279. * more levels of structures.
  2280. */
  2281. int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
  2282. {
  2283. struct uart_state *state;
  2284. struct tty_port *port;
  2285. int ret = 0;
  2286. struct device *tty_dev;
  2287. int num_groups;
  2288. BUG_ON(in_interrupt());
  2289. if (uport->line >= drv->nr)
  2290. return -EINVAL;
  2291. state = drv->state + uport->line;
  2292. port = &state->port;
  2293. mutex_lock(&port_mutex);
  2294. mutex_lock(&port->mutex);
  2295. if (state->uart_port) {
  2296. ret = -EINVAL;
  2297. goto out;
  2298. }
  2299. /* Link the port to the driver state table and vice versa */
  2300. state->uart_port = uport;
  2301. uport->state = state;
  2302. state->pm_state = UART_PM_STATE_UNDEFINED;
  2303. uport->cons = drv->cons;
  2304. uport->minor = drv->tty_driver->minor_start + uport->line;
  2305. /*
  2306. * If this port is a console, then the spinlock is already
  2307. * initialised.
  2308. */
  2309. if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
  2310. spin_lock_init(&uport->lock);
  2311. lockdep_set_class(&uport->lock, &port_lock_key);
  2312. }
  2313. if (uport->cons && uport->dev)
  2314. of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
  2315. uart_configure_port(drv, state, uport);
  2316. num_groups = 2;
  2317. if (uport->attr_group)
  2318. num_groups++;
  2319. uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
  2320. GFP_KERNEL);
  2321. if (!uport->tty_groups) {
  2322. ret = -ENOMEM;
  2323. goto out;
  2324. }
  2325. uport->tty_groups[0] = &tty_dev_attr_group;
  2326. if (uport->attr_group)
  2327. uport->tty_groups[1] = uport->attr_group;
  2328. /*
  2329. * Register the port whether it's detected or not. This allows
  2330. * setserial to be used to alter this port's parameters.
  2331. */
  2332. tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
  2333. uport->line, uport->dev, port, uport->tty_groups);
  2334. if (likely(!IS_ERR(tty_dev))) {
  2335. device_set_wakeup_capable(tty_dev, 1);
  2336. } else {
  2337. dev_err(uport->dev, "Cannot register tty device on line %d\n",
  2338. uport->line);
  2339. }
  2340. /*
  2341. * Ensure UPF_DEAD is not set.
  2342. */
  2343. uport->flags &= ~UPF_DEAD;
  2344. out:
  2345. mutex_unlock(&port->mutex);
  2346. mutex_unlock(&port_mutex);
  2347. return ret;
  2348. }
  2349. /**
  2350. * uart_remove_one_port - detach a driver defined port structure
  2351. * @drv: pointer to the uart low level driver structure for this port
  2352. * @uport: uart port structure for this port
  2353. *
  2354. * This unhooks (and hangs up) the specified port structure from the
  2355. * core driver. No further calls will be made to the low-level code
  2356. * for this port.
  2357. */
  2358. int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
  2359. {
  2360. struct uart_state *state = drv->state + uport->line;
  2361. struct tty_port *port = &state->port;
  2362. struct tty_struct *tty;
  2363. int ret = 0;
  2364. BUG_ON(in_interrupt());
  2365. if (state->uart_port != uport)
  2366. dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
  2367. state->uart_port, uport);
  2368. mutex_lock(&port_mutex);
  2369. /*
  2370. * Mark the port "dead" - this prevents any opens from
  2371. * succeeding while we shut down the port.
  2372. */
  2373. mutex_lock(&port->mutex);
  2374. if (!state->uart_port) {
  2375. mutex_unlock(&port->mutex);
  2376. ret = -EINVAL;
  2377. goto out;
  2378. }
  2379. uport->flags |= UPF_DEAD;
  2380. mutex_unlock(&port->mutex);
  2381. /*
  2382. * Remove the devices from the tty layer
  2383. */
  2384. tty_unregister_device(drv->tty_driver, uport->line);
  2385. tty = tty_port_tty_get(port);
  2386. if (tty) {
  2387. tty_vhangup(port->tty);
  2388. tty_kref_put(tty);
  2389. }
  2390. /*
  2391. * If the port is used as a console, unregister it
  2392. */
  2393. if (uart_console(uport))
  2394. unregister_console(uport->cons);
  2395. /*
  2396. * Free the port IO and memory resources, if any.
  2397. */
  2398. if (uport->type != PORT_UNKNOWN)
  2399. uport->ops->release_port(uport);
  2400. kfree(uport->tty_groups);
  2401. /*
  2402. * Indicate that there isn't a port here anymore.
  2403. */
  2404. uport->type = PORT_UNKNOWN;
  2405. state->uart_port = NULL;
  2406. out:
  2407. mutex_unlock(&port_mutex);
  2408. return ret;
  2409. }
  2410. /*
  2411. * Are the two ports equivalent?
  2412. */
  2413. int uart_match_port(struct uart_port *port1, struct uart_port *port2)
  2414. {
  2415. if (port1->iotype != port2->iotype)
  2416. return 0;
  2417. switch (port1->iotype) {
  2418. case UPIO_PORT:
  2419. return (port1->iobase == port2->iobase);
  2420. case UPIO_HUB6:
  2421. return (port1->iobase == port2->iobase) &&
  2422. (port1->hub6 == port2->hub6);
  2423. case UPIO_MEM:
  2424. case UPIO_MEM32:
  2425. case UPIO_MEM32BE:
  2426. case UPIO_AU:
  2427. case UPIO_TSI:
  2428. return (port1->mapbase == port2->mapbase);
  2429. }
  2430. return 0;
  2431. }
  2432. EXPORT_SYMBOL(uart_match_port);
  2433. /**
  2434. * uart_handle_dcd_change - handle a change of carrier detect state
  2435. * @uport: uart_port structure for the open port
  2436. * @status: new carrier detect status, nonzero if active
  2437. *
  2438. * Caller must hold uport->lock
  2439. */
  2440. void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
  2441. {
  2442. struct tty_port *port = &uport->state->port;
  2443. struct tty_struct *tty = port->tty;
  2444. struct tty_ldisc *ld;
  2445. lockdep_assert_held_once(&uport->lock);
  2446. if (tty) {
  2447. ld = tty_ldisc_ref(tty);
  2448. if (ld) {
  2449. if (ld->ops->dcd_change)
  2450. ld->ops->dcd_change(tty, status);
  2451. tty_ldisc_deref(ld);
  2452. }
  2453. }
  2454. uport->icount.dcd++;
  2455. if (uart_dcd_enabled(uport)) {
  2456. if (status)
  2457. wake_up_interruptible(&port->open_wait);
  2458. else if (tty)
  2459. tty_hangup(tty);
  2460. }
  2461. }
  2462. EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
  2463. /**
  2464. * uart_handle_cts_change - handle a change of clear-to-send state
  2465. * @uport: uart_port structure for the open port
  2466. * @status: new clear to send status, nonzero if active
  2467. *
  2468. * Caller must hold uport->lock
  2469. */
  2470. void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
  2471. {
  2472. lockdep_assert_held_once(&uport->lock);
  2473. uport->icount.cts++;
  2474. if (uart_softcts_mode(uport)) {
  2475. if (uport->hw_stopped) {
  2476. if (status) {
  2477. uport->hw_stopped = 0;
  2478. uport->ops->start_tx(uport);
  2479. uart_write_wakeup(uport);
  2480. }
  2481. } else {
  2482. if (!status) {
  2483. uport->hw_stopped = 1;
  2484. uport->ops->stop_tx(uport);
  2485. }
  2486. }
  2487. }
  2488. }
  2489. EXPORT_SYMBOL_GPL(uart_handle_cts_change);
  2490. /**
  2491. * uart_insert_char - push a char to the uart layer
  2492. *
  2493. * User is responsible to call tty_flip_buffer_push when they are done with
  2494. * insertion.
  2495. *
  2496. * @port: corresponding port
  2497. * @status: state of the serial port RX buffer (LSR for 8250)
  2498. * @overrun: mask of overrun bits in @status
  2499. * @ch: character to push
  2500. * @flag: flag for the character (see TTY_NORMAL and friends)
  2501. */
  2502. void uart_insert_char(struct uart_port *port, unsigned int status,
  2503. unsigned int overrun, unsigned int ch, unsigned int flag)
  2504. {
  2505. struct tty_port *tport = &port->state->port;
  2506. if ((status & port->ignore_status_mask & ~overrun) == 0)
  2507. if (tty_insert_flip_char(tport, ch, flag) == 0)
  2508. ++port->icount.buf_overrun;
  2509. /*
  2510. * Overrun is special. Since it's reported immediately,
  2511. * it doesn't affect the current character.
  2512. */
  2513. if (status & ~port->ignore_status_mask & overrun)
  2514. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
  2515. ++port->icount.buf_overrun;
  2516. }
  2517. EXPORT_SYMBOL_GPL(uart_insert_char);
  2518. EXPORT_SYMBOL(uart_write_wakeup);
  2519. EXPORT_SYMBOL(uart_register_driver);
  2520. EXPORT_SYMBOL(uart_unregister_driver);
  2521. EXPORT_SYMBOL(uart_suspend_port);
  2522. EXPORT_SYMBOL(uart_resume_port);
  2523. EXPORT_SYMBOL(uart_add_one_port);
  2524. EXPORT_SYMBOL(uart_remove_one_port);
  2525. MODULE_DESCRIPTION("Serial driver core");
  2526. MODULE_LICENSE("GPL");