serial_core.c 71 KB

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