serial_core.c 58 KB

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