serial_core.c 55 KB

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