serial_core.c 57 KB

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