serial_core.c 62 KB

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