serial_core.c 62 KB

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