serial_core.c 61 KB

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