serial_core.c 64 KB

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