serial_core.c 71 KB

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