serial_core.c 71 KB

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