serial_core.c 62 KB

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