serial_core.c 57 KB

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