serial_core.c 61 KB

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