serial_core.c 62 KB

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