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