serial_core.c 56 KB

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