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