serial_core.c 63 KB

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