serial_core.c 60 KB

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