serial_core.c 62 KB

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