serial_core.c 71 KB

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