serial_core.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  1. /*
  2. * Driver core for serial ports
  3. *
  4. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  5. *
  6. * Copyright 1999 ARM Limited
  7. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/console.h>
  29. #include <linux/of.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/device.h>
  33. #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
  34. #include <linux/serial_core.h>
  35. #include <linux/delay.h>
  36. #include <linux/mutex.h>
  37. #include <asm/irq.h>
  38. #include <asm/uaccess.h>
  39. /*
  40. * This is used to lock changes in serial line configuration.
  41. */
  42. static DEFINE_MUTEX(port_mutex);
  43. /*
  44. * lockdep: port->lock is initialized in two places, but we
  45. * want only one lock-class:
  46. */
  47. static struct lock_class_key port_lock_key;
  48. #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
  49. static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
  50. struct ktermios *old_termios);
  51. static void uart_wait_until_sent(struct tty_struct *tty, int timeout);
  52. static void uart_change_pm(struct uart_state *state,
  53. enum uart_pm_state pm_state);
  54. static void uart_port_shutdown(struct tty_port *port);
  55. static int uart_dcd_enabled(struct uart_port *uport)
  56. {
  57. return !!(uport->status & UPSTAT_DCD_ENABLE);
  58. }
  59. static inline struct uart_port *uart_port_ref(struct uart_state *state)
  60. {
  61. if (atomic_add_unless(&state->refcount, 1, 0))
  62. return state->uart_port;
  63. return NULL;
  64. }
  65. static inline void uart_port_deref(struct uart_port *uport)
  66. {
  67. if (uport && atomic_dec_and_test(&uport->state->refcount))
  68. wake_up(&uport->state->remove_wait);
  69. }
  70. #define uart_port_lock(state, flags) \
  71. ({ \
  72. struct uart_port *__uport = uart_port_ref(state); \
  73. if (__uport) \
  74. spin_lock_irqsave(&__uport->lock, flags); \
  75. __uport; \
  76. })
  77. #define uart_port_unlock(uport, flags) \
  78. ({ \
  79. struct uart_port *__uport = uport; \
  80. if (__uport) \
  81. spin_unlock_irqrestore(&__uport->lock, flags); \
  82. uart_port_deref(__uport); \
  83. })
  84. static inline struct uart_port *uart_port_check(struct uart_state *state)
  85. {
  86. lockdep_assert_held(&state->port.mutex);
  87. return state->uart_port;
  88. }
  89. /*
  90. * This routine is used by the interrupt handler to schedule processing in
  91. * the software interrupt portion of the driver.
  92. */
  93. void uart_write_wakeup(struct uart_port *port)
  94. {
  95. struct uart_state *state = port->state;
  96. /*
  97. * This means you called this function _after_ the port was
  98. * closed. No cookie for you.
  99. */
  100. BUG_ON(!state);
  101. tty_wakeup(state->port.tty);
  102. }
  103. static void uart_stop(struct tty_struct *tty)
  104. {
  105. struct uart_state *state = tty->driver_data;
  106. struct uart_port *port;
  107. unsigned long flags;
  108. port = uart_port_lock(state, flags);
  109. if (port)
  110. port->ops->stop_tx(port);
  111. uart_port_unlock(port, flags);
  112. }
  113. static void __uart_start(struct tty_struct *tty)
  114. {
  115. struct uart_state *state = tty->driver_data;
  116. struct uart_port *port = state->uart_port;
  117. if (port && !uart_tx_stopped(port))
  118. port->ops->start_tx(port);
  119. }
  120. static void uart_start(struct tty_struct *tty)
  121. {
  122. struct uart_state *state = tty->driver_data;
  123. struct uart_port *port;
  124. unsigned long flags;
  125. port = uart_port_lock(state, flags);
  126. __uart_start(tty);
  127. uart_port_unlock(port, flags);
  128. }
  129. static void
  130. uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
  131. {
  132. unsigned long flags;
  133. unsigned int old;
  134. spin_lock_irqsave(&port->lock, flags);
  135. old = port->mctrl;
  136. port->mctrl = (old & ~clear) | set;
  137. if (old != port->mctrl)
  138. port->ops->set_mctrl(port, port->mctrl);
  139. spin_unlock_irqrestore(&port->lock, flags);
  140. }
  141. #define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
  142. #define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
  143. /*
  144. * Startup the port. This will be called once per open. All calls
  145. * will be serialised by the per-port mutex.
  146. */
  147. static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
  148. int init_hw)
  149. {
  150. struct uart_port *uport = uart_port_check(state);
  151. unsigned long page;
  152. int retval = 0;
  153. if (uport->type == PORT_UNKNOWN)
  154. return 1;
  155. /*
  156. * Make sure the device is in D0 state.
  157. */
  158. uart_change_pm(state, UART_PM_STATE_ON);
  159. /*
  160. * Initialise and allocate the transmit and temporary
  161. * buffer.
  162. */
  163. if (!state->xmit.buf) {
  164. /* This is protected by the per port mutex */
  165. page = get_zeroed_page(GFP_KERNEL);
  166. if (!page)
  167. return -ENOMEM;
  168. state->xmit.buf = (unsigned char *) page;
  169. uart_circ_clear(&state->xmit);
  170. }
  171. retval = uport->ops->startup(uport);
  172. if (retval == 0) {
  173. if (uart_console(uport) && uport->cons->cflag) {
  174. tty->termios.c_cflag = uport->cons->cflag;
  175. uport->cons->cflag = 0;
  176. }
  177. /*
  178. * Initialise the hardware port settings.
  179. */
  180. uart_change_speed(tty, state, NULL);
  181. /*
  182. * Setup the RTS and DTR signals once the
  183. * port is open and ready to respond.
  184. */
  185. if (init_hw && C_BAUD(tty))
  186. uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
  187. }
  188. /*
  189. * This is to allow setserial on this port. People may want to set
  190. * port/irq/type and then reconfigure the port properly if it failed
  191. * now.
  192. */
  193. if (retval && capable(CAP_SYS_ADMIN))
  194. return 1;
  195. return retval;
  196. }
  197. static int uart_startup(struct tty_struct *tty, struct uart_state *state,
  198. int init_hw)
  199. {
  200. struct tty_port *port = &state->port;
  201. int retval;
  202. if (tty_port_initialized(port))
  203. return 0;
  204. /*
  205. * Set the TTY IO error marker - we will only clear this
  206. * once we have successfully opened the port.
  207. */
  208. set_bit(TTY_IO_ERROR, &tty->flags);
  209. retval = uart_port_startup(tty, state, init_hw);
  210. if (!retval) {
  211. tty_port_set_initialized(port, 1);
  212. clear_bit(TTY_IO_ERROR, &tty->flags);
  213. } else if (retval > 0)
  214. retval = 0;
  215. return retval;
  216. }
  217. /*
  218. * This routine will shutdown a serial port; interrupts are disabled, and
  219. * DTR is dropped if the hangup on close termio flag is on. Calls to
  220. * uart_shutdown are serialised by the per-port semaphore.
  221. *
  222. * uport == NULL if uart_port has already been removed
  223. */
  224. static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
  225. {
  226. struct uart_port *uport = uart_port_check(state);
  227. struct tty_port *port = &state->port;
  228. /*
  229. * Set the TTY IO error marker
  230. */
  231. if (tty)
  232. set_bit(TTY_IO_ERROR, &tty->flags);
  233. if (tty_port_initialized(port)) {
  234. tty_port_set_initialized(port, 0);
  235. /*
  236. * Turn off DTR and RTS early.
  237. */
  238. if (uport && uart_console(uport) && tty)
  239. uport->cons->cflag = tty->termios.c_cflag;
  240. if (!tty || C_HUPCL(tty))
  241. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  242. uart_port_shutdown(port);
  243. }
  244. /*
  245. * It's possible for shutdown to be called after suspend if we get
  246. * a DCD drop (hangup) at just the right time. Clear suspended bit so
  247. * we don't try to resume a port that has been shutdown.
  248. */
  249. tty_port_set_suspended(port, 0);
  250. /*
  251. * Free the transmit buffer page.
  252. */
  253. if (state->xmit.buf) {
  254. free_page((unsigned long)state->xmit.buf);
  255. state->xmit.buf = NULL;
  256. }
  257. }
  258. /**
  259. * uart_update_timeout - update per-port FIFO timeout.
  260. * @port: uart_port structure describing the port
  261. * @cflag: termios cflag value
  262. * @baud: speed of the port
  263. *
  264. * Set the port FIFO timeout value. The @cflag value should
  265. * reflect the actual hardware settings.
  266. */
  267. void
  268. uart_update_timeout(struct uart_port *port, unsigned int cflag,
  269. unsigned int baud)
  270. {
  271. unsigned int bits;
  272. /* byte size and parity */
  273. switch (cflag & CSIZE) {
  274. case CS5:
  275. bits = 7;
  276. break;
  277. case CS6:
  278. bits = 8;
  279. break;
  280. case CS7:
  281. bits = 9;
  282. break;
  283. default:
  284. bits = 10;
  285. break; /* CS8 */
  286. }
  287. if (cflag & CSTOPB)
  288. bits++;
  289. if (cflag & PARENB)
  290. bits++;
  291. /*
  292. * The total number of bits to be transmitted in the fifo.
  293. */
  294. bits = bits * port->fifosize;
  295. /*
  296. * Figure the timeout to send the above number of bits.
  297. * Add .02 seconds of slop
  298. */
  299. port->timeout = (HZ * bits) / baud + HZ/50;
  300. }
  301. EXPORT_SYMBOL(uart_update_timeout);
  302. /**
  303. * uart_get_baud_rate - return baud rate for a particular port
  304. * @port: uart_port structure describing the port in question.
  305. * @termios: desired termios settings.
  306. * @old: old termios (or NULL)
  307. * @min: minimum acceptable baud rate
  308. * @max: maximum acceptable baud rate
  309. *
  310. * Decode the termios structure into a numeric baud rate,
  311. * taking account of the magic 38400 baud rate (with spd_*
  312. * flags), and mapping the %B0 rate to 9600 baud.
  313. *
  314. * If the new baud rate is invalid, try the old termios setting.
  315. * If it's still invalid, we try 9600 baud.
  316. *
  317. * Update the @termios structure to reflect the baud rate
  318. * we're actually going to be using. Don't do this for the case
  319. * where B0 is requested ("hang up").
  320. */
  321. unsigned int
  322. uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
  323. struct ktermios *old, unsigned int min, unsigned int max)
  324. {
  325. unsigned int try;
  326. unsigned int baud;
  327. unsigned int altbaud;
  328. int hung_up = 0;
  329. upf_t flags = port->flags & UPF_SPD_MASK;
  330. switch (flags) {
  331. case UPF_SPD_HI:
  332. altbaud = 57600;
  333. break;
  334. case UPF_SPD_VHI:
  335. altbaud = 115200;
  336. break;
  337. case UPF_SPD_SHI:
  338. altbaud = 230400;
  339. break;
  340. case UPF_SPD_WARP:
  341. altbaud = 460800;
  342. break;
  343. default:
  344. altbaud = 38400;
  345. break;
  346. }
  347. for (try = 0; try < 2; try++) {
  348. baud = tty_termios_baud_rate(termios);
  349. /*
  350. * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
  351. * Die! Die! Die!
  352. */
  353. if (try == 0 && baud == 38400)
  354. baud = altbaud;
  355. /*
  356. * Special case: B0 rate.
  357. */
  358. if (baud == 0) {
  359. hung_up = 1;
  360. baud = 9600;
  361. }
  362. if (baud >= min && baud <= max)
  363. return baud;
  364. /*
  365. * Oops, the quotient was zero. Try again with
  366. * the old baud rate if possible.
  367. */
  368. termios->c_cflag &= ~CBAUD;
  369. if (old) {
  370. baud = tty_termios_baud_rate(old);
  371. if (!hung_up)
  372. tty_termios_encode_baud_rate(termios,
  373. baud, baud);
  374. old = NULL;
  375. continue;
  376. }
  377. /*
  378. * As a last resort, if the range cannot be met then clip to
  379. * the nearest chip supported rate.
  380. */
  381. if (!hung_up) {
  382. if (baud <= min)
  383. tty_termios_encode_baud_rate(termios,
  384. min + 1, min + 1);
  385. else
  386. tty_termios_encode_baud_rate(termios,
  387. max - 1, max - 1);
  388. }
  389. }
  390. /* Should never happen */
  391. WARN_ON(1);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(uart_get_baud_rate);
  395. /**
  396. * uart_get_divisor - return uart clock divisor
  397. * @port: uart_port structure describing the port.
  398. * @baud: desired baud rate
  399. *
  400. * Calculate the uart clock divisor for the port.
  401. */
  402. unsigned int
  403. uart_get_divisor(struct uart_port *port, unsigned int baud)
  404. {
  405. unsigned int quot;
  406. /*
  407. * Old custom speed handling.
  408. */
  409. if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
  410. quot = port->custom_divisor;
  411. else
  412. quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
  413. return quot;
  414. }
  415. EXPORT_SYMBOL(uart_get_divisor);
  416. /* Caller holds port mutex */
  417. static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
  418. struct ktermios *old_termios)
  419. {
  420. struct uart_port *uport = uart_port_check(state);
  421. struct ktermios *termios;
  422. int hw_stopped;
  423. /*
  424. * If we have no tty, termios, or the port does not exist,
  425. * then we can't set the parameters for this port.
  426. */
  427. if (!tty || uport->type == PORT_UNKNOWN)
  428. return;
  429. termios = &tty->termios;
  430. uport->ops->set_termios(uport, termios, old_termios);
  431. /*
  432. * Set modem status enables based on termios cflag
  433. */
  434. spin_lock_irq(&uport->lock);
  435. if (termios->c_cflag & CRTSCTS)
  436. uport->status |= UPSTAT_CTS_ENABLE;
  437. else
  438. uport->status &= ~UPSTAT_CTS_ENABLE;
  439. if (termios->c_cflag & CLOCAL)
  440. uport->status &= ~UPSTAT_DCD_ENABLE;
  441. else
  442. uport->status |= UPSTAT_DCD_ENABLE;
  443. /* reset sw-assisted CTS flow control based on (possibly) new mode */
  444. hw_stopped = uport->hw_stopped;
  445. uport->hw_stopped = uart_softcts_mode(uport) &&
  446. !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
  447. if (uport->hw_stopped) {
  448. if (!hw_stopped)
  449. uport->ops->stop_tx(uport);
  450. } else {
  451. if (hw_stopped)
  452. __uart_start(tty);
  453. }
  454. spin_unlock_irq(&uport->lock);
  455. }
  456. static int uart_put_char(struct tty_struct *tty, unsigned char c)
  457. {
  458. struct uart_state *state = tty->driver_data;
  459. struct uart_port *port;
  460. struct circ_buf *circ;
  461. unsigned long flags;
  462. int ret = 0;
  463. circ = &state->xmit;
  464. if (!circ->buf)
  465. return 0;
  466. port = uart_port_lock(state, flags);
  467. if (port && uart_circ_chars_free(circ) != 0) {
  468. circ->buf[circ->head] = c;
  469. circ->head = (circ->head + 1) & (UART_XMIT_SIZE - 1);
  470. ret = 1;
  471. }
  472. uart_port_unlock(port, flags);
  473. return ret;
  474. }
  475. static void uart_flush_chars(struct tty_struct *tty)
  476. {
  477. uart_start(tty);
  478. }
  479. static int uart_write(struct tty_struct *tty,
  480. const unsigned char *buf, int count)
  481. {
  482. struct uart_state *state = tty->driver_data;
  483. struct uart_port *port;
  484. struct circ_buf *circ;
  485. unsigned long flags;
  486. int c, ret = 0;
  487. /*
  488. * This means you called this function _after_ the port was
  489. * closed. No cookie for you.
  490. */
  491. if (!state) {
  492. WARN_ON(1);
  493. return -EL3HLT;
  494. }
  495. circ = &state->xmit;
  496. if (!circ->buf)
  497. return 0;
  498. port = uart_port_lock(state, flags);
  499. while (port) {
  500. c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);
  501. if (count < c)
  502. c = count;
  503. if (c <= 0)
  504. break;
  505. memcpy(circ->buf + circ->head, buf, c);
  506. circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);
  507. buf += c;
  508. count -= c;
  509. ret += c;
  510. }
  511. __uart_start(tty);
  512. uart_port_unlock(port, flags);
  513. return ret;
  514. }
  515. static int uart_write_room(struct tty_struct *tty)
  516. {
  517. struct uart_state *state = tty->driver_data;
  518. struct uart_port *port;
  519. unsigned long flags;
  520. int ret;
  521. port = uart_port_lock(state, flags);
  522. ret = uart_circ_chars_free(&state->xmit);
  523. uart_port_unlock(port, flags);
  524. return ret;
  525. }
  526. static int uart_chars_in_buffer(struct tty_struct *tty)
  527. {
  528. struct uart_state *state = tty->driver_data;
  529. struct uart_port *port;
  530. unsigned long flags;
  531. int ret;
  532. port = uart_port_lock(state, flags);
  533. ret = uart_circ_chars_pending(&state->xmit);
  534. uart_port_unlock(port, flags);
  535. return ret;
  536. }
  537. static void uart_flush_buffer(struct tty_struct *tty)
  538. {
  539. struct uart_state *state = tty->driver_data;
  540. struct uart_port *port;
  541. unsigned long flags;
  542. /*
  543. * This means you called this function _after_ the port was
  544. * closed. No cookie for you.
  545. */
  546. if (!state) {
  547. WARN_ON(1);
  548. return;
  549. }
  550. pr_debug("uart_flush_buffer(%d) called\n", tty->index);
  551. port = uart_port_lock(state, flags);
  552. if (!port)
  553. return;
  554. uart_circ_clear(&state->xmit);
  555. if (port->ops->flush_buffer)
  556. port->ops->flush_buffer(port);
  557. uart_port_unlock(port, flags);
  558. tty_wakeup(tty);
  559. }
  560. /*
  561. * This function is used to send a high-priority XON/XOFF character to
  562. * the device
  563. */
  564. static void uart_send_xchar(struct tty_struct *tty, char ch)
  565. {
  566. struct uart_state *state = tty->driver_data;
  567. struct uart_port *port;
  568. unsigned long flags;
  569. port = uart_port_ref(state);
  570. if (!port)
  571. return;
  572. if (port->ops->send_xchar)
  573. port->ops->send_xchar(port, ch);
  574. else {
  575. spin_lock_irqsave(&port->lock, flags);
  576. port->x_char = ch;
  577. if (ch)
  578. port->ops->start_tx(port);
  579. spin_unlock_irqrestore(&port->lock, flags);
  580. }
  581. uart_port_deref(port);
  582. }
  583. static void uart_throttle(struct tty_struct *tty)
  584. {
  585. struct uart_state *state = tty->driver_data;
  586. struct uart_port *port;
  587. upstat_t mask = 0;
  588. port = uart_port_ref(state);
  589. if (!port)
  590. return;
  591. if (I_IXOFF(tty))
  592. mask |= UPSTAT_AUTOXOFF;
  593. if (C_CRTSCTS(tty))
  594. mask |= UPSTAT_AUTORTS;
  595. if (port->status & mask) {
  596. port->ops->throttle(port);
  597. mask &= ~port->status;
  598. }
  599. if (mask & UPSTAT_AUTORTS)
  600. uart_clear_mctrl(port, TIOCM_RTS);
  601. if (mask & UPSTAT_AUTOXOFF)
  602. uart_send_xchar(tty, STOP_CHAR(tty));
  603. uart_port_deref(port);
  604. }
  605. static void uart_unthrottle(struct tty_struct *tty)
  606. {
  607. struct uart_state *state = tty->driver_data;
  608. struct uart_port *port;
  609. upstat_t mask = 0;
  610. port = uart_port_ref(state);
  611. if (!port)
  612. return;
  613. if (I_IXOFF(tty))
  614. mask |= UPSTAT_AUTOXOFF;
  615. if (C_CRTSCTS(tty))
  616. mask |= UPSTAT_AUTORTS;
  617. if (port->status & mask) {
  618. port->ops->unthrottle(port);
  619. mask &= ~port->status;
  620. }
  621. if (mask & UPSTAT_AUTORTS)
  622. uart_set_mctrl(port, TIOCM_RTS);
  623. if (mask & UPSTAT_AUTOXOFF)
  624. uart_send_xchar(tty, START_CHAR(tty));
  625. uart_port_deref(port);
  626. }
  627. static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
  628. {
  629. struct uart_state *state = container_of(port, struct uart_state, port);
  630. struct uart_port *uport;
  631. int ret = -ENODEV;
  632. memset(retinfo, 0, sizeof(*retinfo));
  633. /*
  634. * Ensure the state we copy is consistent and no hardware changes
  635. * occur as we go
  636. */
  637. mutex_lock(&port->mutex);
  638. uport = uart_port_check(state);
  639. if (!uport)
  640. goto out;
  641. retinfo->type = uport->type;
  642. retinfo->line = uport->line;
  643. retinfo->port = uport->iobase;
  644. if (HIGH_BITS_OFFSET)
  645. retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
  646. retinfo->irq = uport->irq;
  647. retinfo->flags = uport->flags;
  648. retinfo->xmit_fifo_size = uport->fifosize;
  649. retinfo->baud_base = uport->uartclk / 16;
  650. retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
  651. retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  652. ASYNC_CLOSING_WAIT_NONE :
  653. jiffies_to_msecs(port->closing_wait) / 10;
  654. retinfo->custom_divisor = uport->custom_divisor;
  655. retinfo->hub6 = uport->hub6;
  656. retinfo->io_type = uport->iotype;
  657. retinfo->iomem_reg_shift = uport->regshift;
  658. retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
  659. ret = 0;
  660. out:
  661. mutex_unlock(&port->mutex);
  662. return ret;
  663. }
  664. static int uart_get_info_user(struct tty_port *port,
  665. struct serial_struct __user *retinfo)
  666. {
  667. struct serial_struct tmp;
  668. if (uart_get_info(port, &tmp) < 0)
  669. return -EIO;
  670. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  671. return -EFAULT;
  672. return 0;
  673. }
  674. static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
  675. struct uart_state *state,
  676. struct serial_struct *new_info)
  677. {
  678. struct uart_port *uport = uart_port_check(state);
  679. unsigned long new_port;
  680. unsigned int change_irq, change_port, closing_wait;
  681. unsigned int old_custom_divisor, close_delay;
  682. upf_t old_flags, new_flags;
  683. int retval = 0;
  684. if (!uport)
  685. return -EIO;
  686. new_port = new_info->port;
  687. if (HIGH_BITS_OFFSET)
  688. new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
  689. new_info->irq = irq_canonicalize(new_info->irq);
  690. close_delay = msecs_to_jiffies(new_info->close_delay * 10);
  691. closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  692. ASYNC_CLOSING_WAIT_NONE :
  693. msecs_to_jiffies(new_info->closing_wait * 10);
  694. change_irq = !(uport->flags & UPF_FIXED_PORT)
  695. && new_info->irq != uport->irq;
  696. /*
  697. * Since changing the 'type' of the port changes its resource
  698. * allocations, we should treat type changes the same as
  699. * IO port changes.
  700. */
  701. change_port = !(uport->flags & UPF_FIXED_PORT)
  702. && (new_port != uport->iobase ||
  703. (unsigned long)new_info->iomem_base != uport->mapbase ||
  704. new_info->hub6 != uport->hub6 ||
  705. new_info->io_type != uport->iotype ||
  706. new_info->iomem_reg_shift != uport->regshift ||
  707. new_info->type != uport->type);
  708. old_flags = uport->flags;
  709. new_flags = new_info->flags;
  710. old_custom_divisor = uport->custom_divisor;
  711. if (!capable(CAP_SYS_ADMIN)) {
  712. retval = -EPERM;
  713. if (change_irq || change_port ||
  714. (new_info->baud_base != uport->uartclk / 16) ||
  715. (close_delay != port->close_delay) ||
  716. (closing_wait != port->closing_wait) ||
  717. (new_info->xmit_fifo_size &&
  718. new_info->xmit_fifo_size != uport->fifosize) ||
  719. (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
  720. goto exit;
  721. uport->flags = ((uport->flags & ~UPF_USR_MASK) |
  722. (new_flags & UPF_USR_MASK));
  723. uport->custom_divisor = new_info->custom_divisor;
  724. goto check_and_exit;
  725. }
  726. /*
  727. * Ask the low level driver to verify the settings.
  728. */
  729. if (uport->ops->verify_port)
  730. retval = uport->ops->verify_port(uport, new_info);
  731. if ((new_info->irq >= nr_irqs) || (new_info->irq < 0) ||
  732. (new_info->baud_base < 9600))
  733. retval = -EINVAL;
  734. if (retval)
  735. goto exit;
  736. if (change_port || change_irq) {
  737. retval = -EBUSY;
  738. /*
  739. * Make sure that we are the sole user of this port.
  740. */
  741. if (tty_port_users(port) > 1)
  742. goto exit;
  743. /*
  744. * We need to shutdown the serial port at the old
  745. * port/type/irq combination.
  746. */
  747. uart_shutdown(tty, state);
  748. }
  749. if (change_port) {
  750. unsigned long old_iobase, old_mapbase;
  751. unsigned int old_type, old_iotype, old_hub6, old_shift;
  752. old_iobase = uport->iobase;
  753. old_mapbase = uport->mapbase;
  754. old_type = uport->type;
  755. old_hub6 = uport->hub6;
  756. old_iotype = uport->iotype;
  757. old_shift = uport->regshift;
  758. /*
  759. * Free and release old regions
  760. */
  761. if (old_type != PORT_UNKNOWN && uport->ops->release_port)
  762. uport->ops->release_port(uport);
  763. uport->iobase = new_port;
  764. uport->type = new_info->type;
  765. uport->hub6 = new_info->hub6;
  766. uport->iotype = new_info->io_type;
  767. uport->regshift = new_info->iomem_reg_shift;
  768. uport->mapbase = (unsigned long)new_info->iomem_base;
  769. /*
  770. * Claim and map the new regions
  771. */
  772. if (uport->type != PORT_UNKNOWN && uport->ops->request_port) {
  773. retval = uport->ops->request_port(uport);
  774. } else {
  775. /* Always success - Jean II */
  776. retval = 0;
  777. }
  778. /*
  779. * If we fail to request resources for the
  780. * new port, try to restore the old settings.
  781. */
  782. if (retval) {
  783. uport->iobase = old_iobase;
  784. uport->type = old_type;
  785. uport->hub6 = old_hub6;
  786. uport->iotype = old_iotype;
  787. uport->regshift = old_shift;
  788. uport->mapbase = old_mapbase;
  789. if (old_type != PORT_UNKNOWN) {
  790. retval = uport->ops->request_port(uport);
  791. /*
  792. * If we failed to restore the old settings,
  793. * we fail like this.
  794. */
  795. if (retval)
  796. uport->type = PORT_UNKNOWN;
  797. /*
  798. * We failed anyway.
  799. */
  800. retval = -EBUSY;
  801. }
  802. /* Added to return the correct error -Ram Gupta */
  803. goto exit;
  804. }
  805. }
  806. if (change_irq)
  807. uport->irq = new_info->irq;
  808. if (!(uport->flags & UPF_FIXED_PORT))
  809. uport->uartclk = new_info->baud_base * 16;
  810. uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
  811. (new_flags & UPF_CHANGE_MASK);
  812. uport->custom_divisor = new_info->custom_divisor;
  813. port->close_delay = close_delay;
  814. port->closing_wait = closing_wait;
  815. if (new_info->xmit_fifo_size)
  816. uport->fifosize = new_info->xmit_fifo_size;
  817. port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
  818. check_and_exit:
  819. retval = 0;
  820. if (uport->type == PORT_UNKNOWN)
  821. goto exit;
  822. if (tty_port_initialized(port)) {
  823. if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
  824. old_custom_divisor != uport->custom_divisor) {
  825. /*
  826. * If they're setting up a custom divisor or speed,
  827. * instead of clearing it, then bitch about it. No
  828. * need to rate-limit; it's CAP_SYS_ADMIN only.
  829. */
  830. if (uport->flags & UPF_SPD_MASK) {
  831. dev_notice(uport->dev,
  832. "%s sets custom speed on %s. This is deprecated.\n",
  833. current->comm,
  834. tty_name(port->tty));
  835. }
  836. uart_change_speed(tty, state, NULL);
  837. }
  838. } else
  839. retval = uart_startup(tty, state, 1);
  840. exit:
  841. return retval;
  842. }
  843. static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
  844. struct serial_struct __user *newinfo)
  845. {
  846. struct serial_struct new_serial;
  847. struct tty_port *port = &state->port;
  848. int retval;
  849. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  850. return -EFAULT;
  851. /*
  852. * This semaphore protects port->count. It is also
  853. * very useful to prevent opens. Also, take the
  854. * port configuration semaphore to make sure that a
  855. * module insertion/removal doesn't change anything
  856. * under us.
  857. */
  858. mutex_lock(&port->mutex);
  859. retval = uart_set_info(tty, port, state, &new_serial);
  860. mutex_unlock(&port->mutex);
  861. return retval;
  862. }
  863. /**
  864. * uart_get_lsr_info - get line status register info
  865. * @tty: tty associated with the UART
  866. * @state: UART being queried
  867. * @value: returned modem value
  868. */
  869. static int uart_get_lsr_info(struct tty_struct *tty,
  870. struct uart_state *state, unsigned int __user *value)
  871. {
  872. struct uart_port *uport = uart_port_check(state);
  873. unsigned int result;
  874. result = uport->ops->tx_empty(uport);
  875. /*
  876. * If we're about to load something into the transmit
  877. * register, we'll pretend the transmitter isn't empty to
  878. * avoid a race condition (depending on when the transmit
  879. * interrupt happens).
  880. */
  881. if (uport->x_char ||
  882. ((uart_circ_chars_pending(&state->xmit) > 0) &&
  883. !uart_tx_stopped(uport)))
  884. result &= ~TIOCSER_TEMT;
  885. return put_user(result, value);
  886. }
  887. static int uart_tiocmget(struct tty_struct *tty)
  888. {
  889. struct uart_state *state = tty->driver_data;
  890. struct tty_port *port = &state->port;
  891. struct uart_port *uport;
  892. int result = -EIO;
  893. mutex_lock(&port->mutex);
  894. uport = uart_port_check(state);
  895. if (!uport)
  896. goto out;
  897. if (!tty_io_error(tty)) {
  898. result = uport->mctrl;
  899. spin_lock_irq(&uport->lock);
  900. result |= uport->ops->get_mctrl(uport);
  901. spin_unlock_irq(&uport->lock);
  902. }
  903. out:
  904. mutex_unlock(&port->mutex);
  905. return result;
  906. }
  907. static int
  908. uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
  909. {
  910. struct uart_state *state = tty->driver_data;
  911. struct tty_port *port = &state->port;
  912. struct uart_port *uport;
  913. int ret = -EIO;
  914. mutex_lock(&port->mutex);
  915. uport = uart_port_check(state);
  916. if (!uport)
  917. goto out;
  918. if (!tty_io_error(tty)) {
  919. uart_update_mctrl(uport, set, clear);
  920. ret = 0;
  921. }
  922. out:
  923. mutex_unlock(&port->mutex);
  924. return ret;
  925. }
  926. static int uart_break_ctl(struct tty_struct *tty, int break_state)
  927. {
  928. struct uart_state *state = tty->driver_data;
  929. struct tty_port *port = &state->port;
  930. struct uart_port *uport;
  931. int ret = -EIO;
  932. mutex_lock(&port->mutex);
  933. uport = uart_port_check(state);
  934. if (!uport)
  935. goto out;
  936. if (uport->type != PORT_UNKNOWN)
  937. uport->ops->break_ctl(uport, break_state);
  938. ret = 0;
  939. out:
  940. mutex_unlock(&port->mutex);
  941. return ret;
  942. }
  943. static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
  944. {
  945. struct tty_port *port = &state->port;
  946. struct uart_port *uport;
  947. int flags, ret;
  948. if (!capable(CAP_SYS_ADMIN))
  949. return -EPERM;
  950. /*
  951. * Take the per-port semaphore. This prevents count from
  952. * changing, and hence any extra opens of the port while
  953. * we're auto-configuring.
  954. */
  955. if (mutex_lock_interruptible(&port->mutex))
  956. return -ERESTARTSYS;
  957. uport = uart_port_check(state);
  958. if (!uport) {
  959. ret = -EIO;
  960. goto out;
  961. }
  962. ret = -EBUSY;
  963. if (tty_port_users(port) == 1) {
  964. uart_shutdown(tty, state);
  965. /*
  966. * If we already have a port type configured,
  967. * we must release its resources.
  968. */
  969. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  970. uport->ops->release_port(uport);
  971. flags = UART_CONFIG_TYPE;
  972. if (uport->flags & UPF_AUTO_IRQ)
  973. flags |= UART_CONFIG_IRQ;
  974. /*
  975. * This will claim the ports resources if
  976. * a port is found.
  977. */
  978. uport->ops->config_port(uport, flags);
  979. ret = uart_startup(tty, state, 1);
  980. }
  981. out:
  982. mutex_unlock(&port->mutex);
  983. return ret;
  984. }
  985. static void uart_enable_ms(struct uart_port *uport)
  986. {
  987. /*
  988. * Force modem status interrupts on
  989. */
  990. if (uport->ops->enable_ms)
  991. uport->ops->enable_ms(uport);
  992. }
  993. /*
  994. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  995. * - mask passed in arg for lines of interest
  996. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  997. * Caller should use TIOCGICOUNT to see which one it was
  998. *
  999. * FIXME: This wants extracting into a common all driver implementation
  1000. * of TIOCMWAIT using tty_port.
  1001. */
  1002. static int uart_wait_modem_status(struct uart_state *state, unsigned long arg)
  1003. {
  1004. struct uart_port *uport;
  1005. struct tty_port *port = &state->port;
  1006. DECLARE_WAITQUEUE(wait, current);
  1007. struct uart_icount cprev, cnow;
  1008. int ret;
  1009. /*
  1010. * note the counters on entry
  1011. */
  1012. uport = uart_port_ref(state);
  1013. if (!uport)
  1014. return -EIO;
  1015. spin_lock_irq(&uport->lock);
  1016. memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
  1017. uart_enable_ms(uport);
  1018. spin_unlock_irq(&uport->lock);
  1019. add_wait_queue(&port->delta_msr_wait, &wait);
  1020. for (;;) {
  1021. spin_lock_irq(&uport->lock);
  1022. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1023. spin_unlock_irq(&uport->lock);
  1024. set_current_state(TASK_INTERRUPTIBLE);
  1025. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1026. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1027. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1028. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1029. ret = 0;
  1030. break;
  1031. }
  1032. schedule();
  1033. /* see if a signal did it */
  1034. if (signal_pending(current)) {
  1035. ret = -ERESTARTSYS;
  1036. break;
  1037. }
  1038. cprev = cnow;
  1039. }
  1040. __set_current_state(TASK_RUNNING);
  1041. remove_wait_queue(&port->delta_msr_wait, &wait);
  1042. uart_port_deref(uport);
  1043. return ret;
  1044. }
  1045. /*
  1046. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1047. * Return: write counters to the user passed counter struct
  1048. * NB: both 1->0 and 0->1 transitions are counted except for
  1049. * RI where only 0->1 is counted.
  1050. */
  1051. static int uart_get_icount(struct tty_struct *tty,
  1052. struct serial_icounter_struct *icount)
  1053. {
  1054. struct uart_state *state = tty->driver_data;
  1055. struct uart_icount cnow;
  1056. struct uart_port *uport;
  1057. uport = uart_port_ref(state);
  1058. if (!uport)
  1059. return -EIO;
  1060. spin_lock_irq(&uport->lock);
  1061. memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
  1062. spin_unlock_irq(&uport->lock);
  1063. uart_port_deref(uport);
  1064. icount->cts = cnow.cts;
  1065. icount->dsr = cnow.dsr;
  1066. icount->rng = cnow.rng;
  1067. icount->dcd = cnow.dcd;
  1068. icount->rx = cnow.rx;
  1069. icount->tx = cnow.tx;
  1070. icount->frame = cnow.frame;
  1071. icount->overrun = cnow.overrun;
  1072. icount->parity = cnow.parity;
  1073. icount->brk = cnow.brk;
  1074. icount->buf_overrun = cnow.buf_overrun;
  1075. return 0;
  1076. }
  1077. static int uart_get_rs485_config(struct uart_port *port,
  1078. struct serial_rs485 __user *rs485)
  1079. {
  1080. unsigned long flags;
  1081. struct serial_rs485 aux;
  1082. spin_lock_irqsave(&port->lock, flags);
  1083. aux = port->rs485;
  1084. spin_unlock_irqrestore(&port->lock, flags);
  1085. if (copy_to_user(rs485, &aux, sizeof(aux)))
  1086. return -EFAULT;
  1087. return 0;
  1088. }
  1089. static int uart_set_rs485_config(struct uart_port *port,
  1090. struct serial_rs485 __user *rs485_user)
  1091. {
  1092. struct serial_rs485 rs485;
  1093. int ret;
  1094. unsigned long flags;
  1095. if (!port->rs485_config)
  1096. return -ENOIOCTLCMD;
  1097. if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
  1098. return -EFAULT;
  1099. spin_lock_irqsave(&port->lock, flags);
  1100. ret = port->rs485_config(port, &rs485);
  1101. spin_unlock_irqrestore(&port->lock, flags);
  1102. if (ret)
  1103. return ret;
  1104. if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
  1105. return -EFAULT;
  1106. return 0;
  1107. }
  1108. /*
  1109. * Called via sys_ioctl. We can use spin_lock_irq() here.
  1110. */
  1111. static int
  1112. uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
  1113. {
  1114. struct uart_state *state = tty->driver_data;
  1115. struct tty_port *port = &state->port;
  1116. struct uart_port *uport;
  1117. void __user *uarg = (void __user *)arg;
  1118. int ret = -ENOIOCTLCMD;
  1119. /*
  1120. * These ioctls don't rely on the hardware to be present.
  1121. */
  1122. switch (cmd) {
  1123. case TIOCGSERIAL:
  1124. ret = uart_get_info_user(port, uarg);
  1125. break;
  1126. case TIOCSSERIAL:
  1127. down_write(&tty->termios_rwsem);
  1128. ret = uart_set_info_user(tty, state, uarg);
  1129. up_write(&tty->termios_rwsem);
  1130. break;
  1131. case TIOCSERCONFIG:
  1132. down_write(&tty->termios_rwsem);
  1133. ret = uart_do_autoconfig(tty, state);
  1134. up_write(&tty->termios_rwsem);
  1135. break;
  1136. case TIOCSERGWILD: /* obsolete */
  1137. case TIOCSERSWILD: /* obsolete */
  1138. ret = 0;
  1139. break;
  1140. }
  1141. if (ret != -ENOIOCTLCMD)
  1142. goto out;
  1143. if (tty_io_error(tty)) {
  1144. ret = -EIO;
  1145. goto out;
  1146. }
  1147. /*
  1148. * The following should only be used when hardware is present.
  1149. */
  1150. switch (cmd) {
  1151. case TIOCMIWAIT:
  1152. ret = uart_wait_modem_status(state, arg);
  1153. break;
  1154. }
  1155. if (ret != -ENOIOCTLCMD)
  1156. goto out;
  1157. mutex_lock(&port->mutex);
  1158. uport = uart_port_check(state);
  1159. if (!uport || tty_io_error(tty)) {
  1160. ret = -EIO;
  1161. goto out_up;
  1162. }
  1163. /*
  1164. * All these rely on hardware being present and need to be
  1165. * protected against the tty being hung up.
  1166. */
  1167. switch (cmd) {
  1168. case TIOCSERGETLSR: /* Get line status register */
  1169. ret = uart_get_lsr_info(tty, state, uarg);
  1170. break;
  1171. case TIOCGRS485:
  1172. ret = uart_get_rs485_config(uport, uarg);
  1173. break;
  1174. case TIOCSRS485:
  1175. ret = uart_set_rs485_config(uport, uarg);
  1176. break;
  1177. default:
  1178. if (uport->ops->ioctl)
  1179. ret = uport->ops->ioctl(uport, cmd, arg);
  1180. break;
  1181. }
  1182. out_up:
  1183. mutex_unlock(&port->mutex);
  1184. out:
  1185. return ret;
  1186. }
  1187. static void uart_set_ldisc(struct tty_struct *tty)
  1188. {
  1189. struct uart_state *state = tty->driver_data;
  1190. struct uart_port *uport;
  1191. mutex_lock(&state->port.mutex);
  1192. uport = uart_port_check(state);
  1193. if (uport && uport->ops->set_ldisc)
  1194. uport->ops->set_ldisc(uport, &tty->termios);
  1195. mutex_unlock(&state->port.mutex);
  1196. }
  1197. static void uart_set_termios(struct tty_struct *tty,
  1198. struct ktermios *old_termios)
  1199. {
  1200. struct uart_state *state = tty->driver_data;
  1201. struct uart_port *uport;
  1202. unsigned int cflag = tty->termios.c_cflag;
  1203. unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
  1204. bool sw_changed = false;
  1205. mutex_lock(&state->port.mutex);
  1206. uport = uart_port_check(state);
  1207. if (!uport)
  1208. goto out;
  1209. /*
  1210. * Drivers doing software flow control also need to know
  1211. * about changes to these input settings.
  1212. */
  1213. if (uport->flags & UPF_SOFT_FLOW) {
  1214. iflag_mask |= IXANY|IXON|IXOFF;
  1215. sw_changed =
  1216. tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
  1217. tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
  1218. }
  1219. /*
  1220. * These are the bits that are used to setup various
  1221. * flags in the low level driver. We can ignore the Bfoo
  1222. * bits in c_cflag; c_[io]speed will always be set
  1223. * appropriately by set_termios() in tty_ioctl.c
  1224. */
  1225. if ((cflag ^ old_termios->c_cflag) == 0 &&
  1226. tty->termios.c_ospeed == old_termios->c_ospeed &&
  1227. tty->termios.c_ispeed == old_termios->c_ispeed &&
  1228. ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
  1229. !sw_changed) {
  1230. goto out;
  1231. }
  1232. uart_change_speed(tty, state, old_termios);
  1233. /* reload cflag from termios; port driver may have overriden flags */
  1234. cflag = tty->termios.c_cflag;
  1235. /* Handle transition to B0 status */
  1236. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  1237. uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
  1238. /* Handle transition away from B0 status */
  1239. else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  1240. unsigned int mask = TIOCM_DTR;
  1241. if (!(cflag & CRTSCTS) || !tty_throttled(tty))
  1242. mask |= TIOCM_RTS;
  1243. uart_set_mctrl(uport, mask);
  1244. }
  1245. out:
  1246. mutex_unlock(&state->port.mutex);
  1247. }
  1248. /*
  1249. * Calls to uart_close() are serialised via the tty_lock in
  1250. * drivers/tty/tty_io.c:tty_release()
  1251. * drivers/tty/tty_io.c:do_tty_hangup()
  1252. */
  1253. static void uart_close(struct tty_struct *tty, struct file *filp)
  1254. {
  1255. struct uart_state *state = tty->driver_data;
  1256. struct tty_port *port;
  1257. struct uart_port *uport;
  1258. if (!state) {
  1259. struct uart_driver *drv = tty->driver->driver_state;
  1260. state = drv->state + tty->index;
  1261. port = &state->port;
  1262. spin_lock_irq(&port->lock);
  1263. --port->count;
  1264. spin_unlock_irq(&port->lock);
  1265. return;
  1266. }
  1267. port = &state->port;
  1268. pr_debug("uart_close(%d) called\n", tty->index);
  1269. if (tty_port_close_start(port, tty, filp) == 0)
  1270. return;
  1271. mutex_lock(&port->mutex);
  1272. uport = uart_port_check(state);
  1273. /*
  1274. * At this point, we stop accepting input. To do this, we
  1275. * disable the receive line status interrupts.
  1276. */
  1277. if (tty_port_initialized(port) &&
  1278. !WARN(!uport, "detached port still initialized!\n")) {
  1279. spin_lock_irq(&uport->lock);
  1280. uport->ops->stop_rx(uport);
  1281. spin_unlock_irq(&uport->lock);
  1282. /*
  1283. * Before we drop DTR, make sure the UART transmitter
  1284. * has completely drained; this is especially
  1285. * important if there is a transmit FIFO!
  1286. */
  1287. uart_wait_until_sent(tty, uport->timeout);
  1288. }
  1289. uart_shutdown(tty, state);
  1290. tty_port_tty_set(port, NULL);
  1291. spin_lock_irq(&port->lock);
  1292. if (port->blocked_open) {
  1293. spin_unlock_irq(&port->lock);
  1294. if (port->close_delay)
  1295. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  1296. spin_lock_irq(&port->lock);
  1297. } else if (uport && !uart_console(uport)) {
  1298. spin_unlock_irq(&port->lock);
  1299. uart_change_pm(state, UART_PM_STATE_OFF);
  1300. spin_lock_irq(&port->lock);
  1301. }
  1302. spin_unlock_irq(&port->lock);
  1303. tty_port_set_active(port, 0);
  1304. /*
  1305. * Wake up anyone trying to open this port.
  1306. */
  1307. wake_up_interruptible(&port->open_wait);
  1308. mutex_unlock(&port->mutex);
  1309. tty_ldisc_flush(tty);
  1310. tty->closing = 0;
  1311. }
  1312. static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
  1313. {
  1314. struct uart_state *state = tty->driver_data;
  1315. struct uart_port *port;
  1316. unsigned long char_time, expire;
  1317. port = uart_port_ref(state);
  1318. if (!port || port->type == PORT_UNKNOWN || port->fifosize == 0) {
  1319. uart_port_deref(port);
  1320. return;
  1321. }
  1322. /*
  1323. * Set the check interval to be 1/5 of the estimated time to
  1324. * send a single character, and make it at least 1. The check
  1325. * interval should also be less than the timeout.
  1326. *
  1327. * Note: we have to use pretty tight timings here to satisfy
  1328. * the NIST-PCTS.
  1329. */
  1330. char_time = (port->timeout - HZ/50) / port->fifosize;
  1331. char_time = char_time / 5;
  1332. if (char_time == 0)
  1333. char_time = 1;
  1334. if (timeout && timeout < char_time)
  1335. char_time = timeout;
  1336. /*
  1337. * If the transmitter hasn't cleared in twice the approximate
  1338. * amount of time to send the entire FIFO, it probably won't
  1339. * ever clear. This assumes the UART isn't doing flow
  1340. * control, which is currently the case. Hence, if it ever
  1341. * takes longer than port->timeout, this is probably due to a
  1342. * UART bug of some kind. So, we clamp the timeout parameter at
  1343. * 2*port->timeout.
  1344. */
  1345. if (timeout == 0 || timeout > 2 * port->timeout)
  1346. timeout = 2 * port->timeout;
  1347. expire = jiffies + timeout;
  1348. pr_debug("uart_wait_until_sent(%d), jiffies=%lu, expire=%lu...\n",
  1349. port->line, jiffies, expire);
  1350. /*
  1351. * Check whether the transmitter is empty every 'char_time'.
  1352. * 'timeout' / 'expire' give us the maximum amount of time
  1353. * we wait.
  1354. */
  1355. while (!port->ops->tx_empty(port)) {
  1356. msleep_interruptible(jiffies_to_msecs(char_time));
  1357. if (signal_pending(current))
  1358. break;
  1359. if (time_after(jiffies, expire))
  1360. break;
  1361. }
  1362. uart_port_deref(port);
  1363. }
  1364. /*
  1365. * Calls to uart_hangup() are serialised by the tty_lock in
  1366. * drivers/tty/tty_io.c:do_tty_hangup()
  1367. * This runs from a workqueue and can sleep for a _short_ time only.
  1368. */
  1369. static void uart_hangup(struct tty_struct *tty)
  1370. {
  1371. struct uart_state *state = tty->driver_data;
  1372. struct tty_port *port = &state->port;
  1373. struct uart_port *uport;
  1374. unsigned long flags;
  1375. pr_debug("uart_hangup(%d)\n", tty->index);
  1376. mutex_lock(&port->mutex);
  1377. uport = uart_port_check(state);
  1378. WARN(!uport, "hangup of detached port!\n");
  1379. if (tty_port_active(port)) {
  1380. uart_flush_buffer(tty);
  1381. uart_shutdown(tty, state);
  1382. spin_lock_irqsave(&port->lock, flags);
  1383. port->count = 0;
  1384. spin_unlock_irqrestore(&port->lock, flags);
  1385. tty_port_set_active(port, 0);
  1386. tty_port_tty_set(port, NULL);
  1387. if (uport && !uart_console(uport))
  1388. uart_change_pm(state, UART_PM_STATE_OFF);
  1389. wake_up_interruptible(&port->open_wait);
  1390. wake_up_interruptible(&port->delta_msr_wait);
  1391. }
  1392. mutex_unlock(&port->mutex);
  1393. }
  1394. /* uport == NULL if uart_port has already been removed */
  1395. static void uart_port_shutdown(struct tty_port *port)
  1396. {
  1397. struct uart_state *state = container_of(port, struct uart_state, port);
  1398. struct uart_port *uport = uart_port_check(state);
  1399. /*
  1400. * clear delta_msr_wait queue to avoid mem leaks: we may free
  1401. * the irq here so the queue might never be woken up. Note
  1402. * that we won't end up waiting on delta_msr_wait again since
  1403. * any outstanding file descriptors should be pointing at
  1404. * hung_up_tty_fops now.
  1405. */
  1406. wake_up_interruptible(&port->delta_msr_wait);
  1407. /*
  1408. * Free the IRQ and disable the port.
  1409. */
  1410. if (uport)
  1411. uport->ops->shutdown(uport);
  1412. /*
  1413. * Ensure that the IRQ handler isn't running on another CPU.
  1414. */
  1415. if (uport)
  1416. synchronize_irq(uport->irq);
  1417. }
  1418. static int uart_carrier_raised(struct tty_port *port)
  1419. {
  1420. struct uart_state *state = container_of(port, struct uart_state, port);
  1421. struct uart_port *uport;
  1422. int mctrl;
  1423. uport = uart_port_ref(state);
  1424. /*
  1425. * Should never observe uport == NULL since checks for hangup should
  1426. * abort the tty_port_block_til_ready() loop before checking for carrier
  1427. * raised -- but report carrier raised if it does anyway so open will
  1428. * continue and not sleep
  1429. */
  1430. if (WARN_ON(!uport))
  1431. return 1;
  1432. spin_lock_irq(&uport->lock);
  1433. uart_enable_ms(uport);
  1434. mctrl = uport->ops->get_mctrl(uport);
  1435. spin_unlock_irq(&uport->lock);
  1436. uart_port_deref(uport);
  1437. if (mctrl & TIOCM_CAR)
  1438. return 1;
  1439. return 0;
  1440. }
  1441. static void uart_dtr_rts(struct tty_port *port, int onoff)
  1442. {
  1443. struct uart_state *state = container_of(port, struct uart_state, port);
  1444. struct uart_port *uport;
  1445. uport = uart_port_ref(state);
  1446. if (!uport)
  1447. return;
  1448. if (onoff)
  1449. uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1450. else
  1451. uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
  1452. uart_port_deref(uport);
  1453. }
  1454. /*
  1455. * Calls to uart_open are serialised by the tty_lock in
  1456. * drivers/tty/tty_io.c:tty_open()
  1457. * Note that if this fails, then uart_close() _will_ be called.
  1458. *
  1459. * In time, we want to scrap the "opening nonpresent ports"
  1460. * behaviour and implement an alternative way for setserial
  1461. * to set base addresses/ports/types. This will allow us to
  1462. * get rid of a certain amount of extra tests.
  1463. */
  1464. static int uart_open(struct tty_struct *tty, struct file *filp)
  1465. {
  1466. struct uart_driver *drv = tty->driver->driver_state;
  1467. int retval, line = tty->index;
  1468. struct uart_state *state = drv->state + line;
  1469. struct tty_port *port = &state->port;
  1470. struct uart_port *uport;
  1471. pr_debug("uart_open(%d) called\n", line);
  1472. spin_lock_irq(&port->lock);
  1473. ++port->count;
  1474. spin_unlock_irq(&port->lock);
  1475. /*
  1476. * We take the semaphore here to guarantee that we won't be re-entered
  1477. * while allocating the state structure, or while we request any IRQs
  1478. * that the driver may need. This also has the nice side-effect that
  1479. * it delays the action of uart_hangup, so we can guarantee that
  1480. * state->port.tty will always contain something reasonable.
  1481. */
  1482. if (mutex_lock_interruptible(&port->mutex)) {
  1483. retval = -ERESTARTSYS;
  1484. goto end;
  1485. }
  1486. uport = uart_port_check(state);
  1487. if (!uport || uport->flags & UPF_DEAD) {
  1488. retval = -ENXIO;
  1489. goto err_unlock;
  1490. }
  1491. tty->driver_data = state;
  1492. uport->state = state;
  1493. port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1494. tty_port_tty_set(port, tty);
  1495. /*
  1496. * Start up the serial port.
  1497. */
  1498. retval = uart_startup(tty, state, 0);
  1499. /*
  1500. * If we succeeded, wait until the port is ready.
  1501. */
  1502. err_unlock:
  1503. mutex_unlock(&port->mutex);
  1504. if (retval == 0)
  1505. retval = tty_port_block_til_ready(port, tty, filp);
  1506. end:
  1507. return retval;
  1508. }
  1509. static const char *uart_type(struct uart_port *port)
  1510. {
  1511. const char *str = NULL;
  1512. if (port->ops->type)
  1513. str = port->ops->type(port);
  1514. if (!str)
  1515. str = "unknown";
  1516. return str;
  1517. }
  1518. #ifdef CONFIG_PROC_FS
  1519. static void uart_line_info(struct seq_file *m, struct uart_driver *drv, int i)
  1520. {
  1521. struct uart_state *state = drv->state + i;
  1522. struct tty_port *port = &state->port;
  1523. enum uart_pm_state pm_state;
  1524. struct uart_port *uport;
  1525. char stat_buf[32];
  1526. unsigned int status;
  1527. int mmio;
  1528. mutex_lock(&port->mutex);
  1529. uport = uart_port_check(state);
  1530. if (!uport)
  1531. goto out;
  1532. mmio = uport->iotype >= UPIO_MEM;
  1533. seq_printf(m, "%d: uart:%s %s%08llX irq:%d",
  1534. uport->line, uart_type(uport),
  1535. mmio ? "mmio:0x" : "port:",
  1536. mmio ? (unsigned long long)uport->mapbase
  1537. : (unsigned long long)uport->iobase,
  1538. uport->irq);
  1539. if (uport->type == PORT_UNKNOWN) {
  1540. seq_putc(m, '\n');
  1541. goto out;
  1542. }
  1543. if (capable(CAP_SYS_ADMIN)) {
  1544. pm_state = state->pm_state;
  1545. if (pm_state != UART_PM_STATE_ON)
  1546. uart_change_pm(state, UART_PM_STATE_ON);
  1547. spin_lock_irq(&uport->lock);
  1548. status = uport->ops->get_mctrl(uport);
  1549. spin_unlock_irq(&uport->lock);
  1550. if (pm_state != UART_PM_STATE_ON)
  1551. uart_change_pm(state, pm_state);
  1552. seq_printf(m, " tx:%d rx:%d",
  1553. uport->icount.tx, uport->icount.rx);
  1554. if (uport->icount.frame)
  1555. seq_printf(m, " fe:%d", uport->icount.frame);
  1556. if (uport->icount.parity)
  1557. seq_printf(m, " pe:%d", uport->icount.parity);
  1558. if (uport->icount.brk)
  1559. seq_printf(m, " brk:%d", uport->icount.brk);
  1560. if (uport->icount.overrun)
  1561. seq_printf(m, " oe:%d", uport->icount.overrun);
  1562. #define INFOBIT(bit, str) \
  1563. if (uport->mctrl & (bit)) \
  1564. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1565. strlen(stat_buf) - 2)
  1566. #define STATBIT(bit, str) \
  1567. if (status & (bit)) \
  1568. strncat(stat_buf, (str), sizeof(stat_buf) - \
  1569. strlen(stat_buf) - 2)
  1570. stat_buf[0] = '\0';
  1571. stat_buf[1] = '\0';
  1572. INFOBIT(TIOCM_RTS, "|RTS");
  1573. STATBIT(TIOCM_CTS, "|CTS");
  1574. INFOBIT(TIOCM_DTR, "|DTR");
  1575. STATBIT(TIOCM_DSR, "|DSR");
  1576. STATBIT(TIOCM_CAR, "|CD");
  1577. STATBIT(TIOCM_RNG, "|RI");
  1578. if (stat_buf[0])
  1579. stat_buf[0] = ' ';
  1580. seq_puts(m, stat_buf);
  1581. }
  1582. seq_putc(m, '\n');
  1583. #undef STATBIT
  1584. #undef INFOBIT
  1585. out:
  1586. mutex_unlock(&port->mutex);
  1587. }
  1588. static int uart_proc_show(struct seq_file *m, void *v)
  1589. {
  1590. struct tty_driver *ttydrv = m->private;
  1591. struct uart_driver *drv = ttydrv->driver_state;
  1592. int i;
  1593. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
  1594. for (i = 0; i < drv->nr; i++)
  1595. uart_line_info(m, drv, i);
  1596. return 0;
  1597. }
  1598. static int uart_proc_open(struct inode *inode, struct file *file)
  1599. {
  1600. return single_open(file, uart_proc_show, PDE_DATA(inode));
  1601. }
  1602. static const struct file_operations uart_proc_fops = {
  1603. .owner = THIS_MODULE,
  1604. .open = uart_proc_open,
  1605. .read = seq_read,
  1606. .llseek = seq_lseek,
  1607. .release = single_release,
  1608. };
  1609. #endif
  1610. #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
  1611. /**
  1612. * uart_console_write - write a console message to a serial port
  1613. * @port: the port to write the message
  1614. * @s: array of characters
  1615. * @count: number of characters in string to write
  1616. * @putchar: function to write character to port
  1617. */
  1618. void uart_console_write(struct uart_port *port, const char *s,
  1619. unsigned int count,
  1620. void (*putchar)(struct uart_port *, int))
  1621. {
  1622. unsigned int i;
  1623. for (i = 0; i < count; i++, s++) {
  1624. if (*s == '\n')
  1625. putchar(port, '\r');
  1626. putchar(port, *s);
  1627. }
  1628. }
  1629. EXPORT_SYMBOL_GPL(uart_console_write);
  1630. /*
  1631. * Check whether an invalid uart number has been specified, and
  1632. * if so, search for the first available port that does have
  1633. * console support.
  1634. */
  1635. struct uart_port * __init
  1636. uart_get_console(struct uart_port *ports, int nr, struct console *co)
  1637. {
  1638. int idx = co->index;
  1639. if (idx < 0 || idx >= nr || (ports[idx].iobase == 0 &&
  1640. ports[idx].membase == NULL))
  1641. for (idx = 0; idx < nr; idx++)
  1642. if (ports[idx].iobase != 0 ||
  1643. ports[idx].membase != NULL)
  1644. break;
  1645. co->index = idx;
  1646. return ports + idx;
  1647. }
  1648. /**
  1649. * uart_parse_earlycon - Parse earlycon options
  1650. * @p: ptr to 2nd field (ie., just beyond '<name>,')
  1651. * @iotype: ptr for decoded iotype (out)
  1652. * @addr: ptr for decoded mapbase/iobase (out)
  1653. * @options: ptr for <options> field; NULL if not present (out)
  1654. *
  1655. * Decodes earlycon kernel command line parameters of the form
  1656. * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1657. * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
  1658. *
  1659. * The optional form
  1660. * earlycon=<name>,0x<addr>,<options>
  1661. * console=<name>,0x<addr>,<options>
  1662. * is also accepted; the returned @iotype will be UPIO_MEM.
  1663. *
  1664. * Returns 0 on success or -EINVAL on failure
  1665. */
  1666. int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
  1667. char **options)
  1668. {
  1669. if (strncmp(p, "mmio,", 5) == 0) {
  1670. *iotype = UPIO_MEM;
  1671. p += 5;
  1672. } else if (strncmp(p, "mmio16,", 7) == 0) {
  1673. *iotype = UPIO_MEM16;
  1674. p += 7;
  1675. } else if (strncmp(p, "mmio32,", 7) == 0) {
  1676. *iotype = UPIO_MEM32;
  1677. p += 7;
  1678. } else if (strncmp(p, "mmio32be,", 9) == 0) {
  1679. *iotype = UPIO_MEM32BE;
  1680. p += 9;
  1681. } else if (strncmp(p, "mmio32native,", 13) == 0) {
  1682. *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
  1683. UPIO_MEM32BE : UPIO_MEM32;
  1684. p += 13;
  1685. } else if (strncmp(p, "io,", 3) == 0) {
  1686. *iotype = UPIO_PORT;
  1687. p += 3;
  1688. } else if (strncmp(p, "0x", 2) == 0) {
  1689. *iotype = UPIO_MEM;
  1690. } else {
  1691. return -EINVAL;
  1692. }
  1693. *addr = simple_strtoul(p, NULL, 0);
  1694. p = strchr(p, ',');
  1695. if (p)
  1696. p++;
  1697. *options = p;
  1698. return 0;
  1699. }
  1700. EXPORT_SYMBOL_GPL(uart_parse_earlycon);
  1701. /**
  1702. * uart_parse_options - Parse serial port baud/parity/bits/flow control.
  1703. * @options: pointer to option string
  1704. * @baud: pointer to an 'int' variable for the baud rate.
  1705. * @parity: pointer to an 'int' variable for the parity.
  1706. * @bits: pointer to an 'int' variable for the number of data bits.
  1707. * @flow: pointer to an 'int' variable for the flow control character.
  1708. *
  1709. * uart_parse_options decodes a string containing the serial console
  1710. * options. The format of the string is <baud><parity><bits><flow>,
  1711. * eg: 115200n8r
  1712. */
  1713. void
  1714. uart_parse_options(char *options, int *baud, int *parity, int *bits, int *flow)
  1715. {
  1716. char *s = options;
  1717. *baud = simple_strtoul(s, NULL, 10);
  1718. while (*s >= '0' && *s <= '9')
  1719. s++;
  1720. if (*s)
  1721. *parity = *s++;
  1722. if (*s)
  1723. *bits = *s++ - '0';
  1724. if (*s)
  1725. *flow = *s;
  1726. }
  1727. EXPORT_SYMBOL_GPL(uart_parse_options);
  1728. /**
  1729. * uart_set_options - setup the serial console parameters
  1730. * @port: pointer to the serial ports uart_port structure
  1731. * @co: console pointer
  1732. * @baud: baud rate
  1733. * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
  1734. * @bits: number of data bits
  1735. * @flow: flow control character - 'r' (rts)
  1736. */
  1737. int
  1738. uart_set_options(struct uart_port *port, struct console *co,
  1739. int baud, int parity, int bits, int flow)
  1740. {
  1741. struct ktermios termios;
  1742. static struct ktermios dummy;
  1743. /*
  1744. * Ensure that the serial console lock is initialised
  1745. * early.
  1746. * If this port is a console, then the spinlock is already
  1747. * initialised.
  1748. */
  1749. if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
  1750. spin_lock_init(&port->lock);
  1751. lockdep_set_class(&port->lock, &port_lock_key);
  1752. }
  1753. memset(&termios, 0, sizeof(struct ktermios));
  1754. termios.c_cflag |= CREAD | HUPCL | CLOCAL;
  1755. tty_termios_encode_baud_rate(&termios, baud, baud);
  1756. if (bits == 7)
  1757. termios.c_cflag |= CS7;
  1758. else
  1759. termios.c_cflag |= CS8;
  1760. switch (parity) {
  1761. case 'o': case 'O':
  1762. termios.c_cflag |= PARODD;
  1763. /*fall through*/
  1764. case 'e': case 'E':
  1765. termios.c_cflag |= PARENB;
  1766. break;
  1767. }
  1768. if (flow == 'r')
  1769. termios.c_cflag |= CRTSCTS;
  1770. /*
  1771. * some uarts on other side don't support no flow control.
  1772. * So we set * DTR in host uart to make them happy
  1773. */
  1774. port->mctrl |= TIOCM_DTR;
  1775. port->ops->set_termios(port, &termios, &dummy);
  1776. /*
  1777. * Allow the setting of the UART parameters with a NULL console
  1778. * too:
  1779. */
  1780. if (co)
  1781. co->cflag = termios.c_cflag;
  1782. return 0;
  1783. }
  1784. EXPORT_SYMBOL_GPL(uart_set_options);
  1785. #endif /* CONFIG_SERIAL_CORE_CONSOLE */
  1786. /**
  1787. * uart_change_pm - set power state of the port
  1788. *
  1789. * @state: port descriptor
  1790. * @pm_state: new state
  1791. *
  1792. * Locking: port->mutex has to be held
  1793. */
  1794. static void uart_change_pm(struct uart_state *state,
  1795. enum uart_pm_state pm_state)
  1796. {
  1797. struct uart_port *port = uart_port_check(state);
  1798. if (state->pm_state != pm_state) {
  1799. if (port && port->ops->pm)
  1800. port->ops->pm(port, pm_state, state->pm_state);
  1801. state->pm_state = pm_state;
  1802. }
  1803. }
  1804. struct uart_match {
  1805. struct uart_port *port;
  1806. struct uart_driver *driver;
  1807. };
  1808. static int serial_match_port(struct device *dev, void *data)
  1809. {
  1810. struct uart_match *match = data;
  1811. struct tty_driver *tty_drv = match->driver->tty_driver;
  1812. dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
  1813. match->port->line;
  1814. return dev->devt == devt; /* Actually, only one tty per port */
  1815. }
  1816. int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
  1817. {
  1818. struct uart_state *state = drv->state + uport->line;
  1819. struct tty_port *port = &state->port;
  1820. struct device *tty_dev;
  1821. struct uart_match match = {uport, drv};
  1822. mutex_lock(&port->mutex);
  1823. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1824. if (device_may_wakeup(tty_dev)) {
  1825. if (!enable_irq_wake(uport->irq))
  1826. uport->irq_wake = 1;
  1827. put_device(tty_dev);
  1828. mutex_unlock(&port->mutex);
  1829. return 0;
  1830. }
  1831. put_device(tty_dev);
  1832. /* Nothing to do if the console is not suspending */
  1833. if (!console_suspend_enabled && uart_console(uport))
  1834. goto unlock;
  1835. uport->suspended = 1;
  1836. if (tty_port_initialized(port)) {
  1837. const struct uart_ops *ops = uport->ops;
  1838. int tries;
  1839. tty_port_set_suspended(port, 1);
  1840. tty_port_set_initialized(port, 0);
  1841. spin_lock_irq(&uport->lock);
  1842. ops->stop_tx(uport);
  1843. ops->set_mctrl(uport, 0);
  1844. ops->stop_rx(uport);
  1845. spin_unlock_irq(&uport->lock);
  1846. /*
  1847. * Wait for the transmitter to empty.
  1848. */
  1849. for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
  1850. msleep(10);
  1851. if (!tries)
  1852. dev_err(uport->dev, "%s%d: Unable to drain transmitter\n",
  1853. drv->dev_name,
  1854. drv->tty_driver->name_base + uport->line);
  1855. ops->shutdown(uport);
  1856. }
  1857. /*
  1858. * Disable the console device before suspending.
  1859. */
  1860. if (uart_console(uport))
  1861. console_stop(uport->cons);
  1862. uart_change_pm(state, UART_PM_STATE_OFF);
  1863. unlock:
  1864. mutex_unlock(&port->mutex);
  1865. return 0;
  1866. }
  1867. int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
  1868. {
  1869. struct uart_state *state = drv->state + uport->line;
  1870. struct tty_port *port = &state->port;
  1871. struct device *tty_dev;
  1872. struct uart_match match = {uport, drv};
  1873. struct ktermios termios;
  1874. mutex_lock(&port->mutex);
  1875. tty_dev = device_find_child(uport->dev, &match, serial_match_port);
  1876. if (!uport->suspended && device_may_wakeup(tty_dev)) {
  1877. if (uport->irq_wake) {
  1878. disable_irq_wake(uport->irq);
  1879. uport->irq_wake = 0;
  1880. }
  1881. put_device(tty_dev);
  1882. mutex_unlock(&port->mutex);
  1883. return 0;
  1884. }
  1885. put_device(tty_dev);
  1886. uport->suspended = 0;
  1887. /*
  1888. * Re-enable the console device after suspending.
  1889. */
  1890. if (uart_console(uport)) {
  1891. /*
  1892. * First try to use the console cflag setting.
  1893. */
  1894. memset(&termios, 0, sizeof(struct ktermios));
  1895. termios.c_cflag = uport->cons->cflag;
  1896. /*
  1897. * If that's unset, use the tty termios setting.
  1898. */
  1899. if (port->tty && termios.c_cflag == 0)
  1900. termios = port->tty->termios;
  1901. if (console_suspend_enabled)
  1902. uart_change_pm(state, UART_PM_STATE_ON);
  1903. uport->ops->set_termios(uport, &termios, NULL);
  1904. if (console_suspend_enabled)
  1905. console_start(uport->cons);
  1906. }
  1907. if (tty_port_suspended(port)) {
  1908. const struct uart_ops *ops = uport->ops;
  1909. int ret;
  1910. uart_change_pm(state, UART_PM_STATE_ON);
  1911. spin_lock_irq(&uport->lock);
  1912. ops->set_mctrl(uport, 0);
  1913. spin_unlock_irq(&uport->lock);
  1914. if (console_suspend_enabled || !uart_console(uport)) {
  1915. /* Protected by port mutex for now */
  1916. struct tty_struct *tty = port->tty;
  1917. ret = ops->startup(uport);
  1918. if (ret == 0) {
  1919. if (tty)
  1920. uart_change_speed(tty, state, NULL);
  1921. spin_lock_irq(&uport->lock);
  1922. ops->set_mctrl(uport, uport->mctrl);
  1923. ops->start_tx(uport);
  1924. spin_unlock_irq(&uport->lock);
  1925. tty_port_set_initialized(port, 1);
  1926. } else {
  1927. /*
  1928. * Failed to resume - maybe hardware went away?
  1929. * Clear the "initialized" flag so we won't try
  1930. * to call the low level drivers shutdown method.
  1931. */
  1932. uart_shutdown(tty, state);
  1933. }
  1934. }
  1935. tty_port_set_suspended(port, 0);
  1936. }
  1937. mutex_unlock(&port->mutex);
  1938. return 0;
  1939. }
  1940. static inline void
  1941. uart_report_port(struct uart_driver *drv, struct uart_port *port)
  1942. {
  1943. char address[64];
  1944. switch (port->iotype) {
  1945. case UPIO_PORT:
  1946. snprintf(address, sizeof(address), "I/O 0x%lx", port->iobase);
  1947. break;
  1948. case UPIO_HUB6:
  1949. snprintf(address, sizeof(address),
  1950. "I/O 0x%lx offset 0x%x", port->iobase, port->hub6);
  1951. break;
  1952. case UPIO_MEM:
  1953. case UPIO_MEM16:
  1954. case UPIO_MEM32:
  1955. case UPIO_MEM32BE:
  1956. case UPIO_AU:
  1957. case UPIO_TSI:
  1958. snprintf(address, sizeof(address),
  1959. "MMIO 0x%llx", (unsigned long long)port->mapbase);
  1960. break;
  1961. default:
  1962. strlcpy(address, "*unknown*", sizeof(address));
  1963. break;
  1964. }
  1965. printk(KERN_INFO "%s%s%s%d at %s (irq = %d, base_baud = %d) is a %s\n",
  1966. port->dev ? dev_name(port->dev) : "",
  1967. port->dev ? ": " : "",
  1968. drv->dev_name,
  1969. drv->tty_driver->name_base + port->line,
  1970. address, port->irq, port->uartclk / 16, uart_type(port));
  1971. }
  1972. static void
  1973. uart_configure_port(struct uart_driver *drv, struct uart_state *state,
  1974. struct uart_port *port)
  1975. {
  1976. unsigned int flags;
  1977. /*
  1978. * If there isn't a port here, don't do anything further.
  1979. */
  1980. if (!port->iobase && !port->mapbase && !port->membase)
  1981. return;
  1982. /*
  1983. * Now do the auto configuration stuff. Note that config_port
  1984. * is expected to claim the resources and map the port for us.
  1985. */
  1986. flags = 0;
  1987. if (port->flags & UPF_AUTO_IRQ)
  1988. flags |= UART_CONFIG_IRQ;
  1989. if (port->flags & UPF_BOOT_AUTOCONF) {
  1990. if (!(port->flags & UPF_FIXED_TYPE)) {
  1991. port->type = PORT_UNKNOWN;
  1992. flags |= UART_CONFIG_TYPE;
  1993. }
  1994. port->ops->config_port(port, flags);
  1995. }
  1996. if (port->type != PORT_UNKNOWN) {
  1997. unsigned long flags;
  1998. uart_report_port(drv, port);
  1999. /* Power up port for set_mctrl() */
  2000. uart_change_pm(state, UART_PM_STATE_ON);
  2001. /*
  2002. * Ensure that the modem control lines are de-activated.
  2003. * keep the DTR setting that is set in uart_set_options()
  2004. * We probably don't need a spinlock around this, but
  2005. */
  2006. spin_lock_irqsave(&port->lock, flags);
  2007. port->ops->set_mctrl(port, port->mctrl & TIOCM_DTR);
  2008. spin_unlock_irqrestore(&port->lock, flags);
  2009. /*
  2010. * If this driver supports console, and it hasn't been
  2011. * successfully registered yet, try to re-register it.
  2012. * It may be that the port was not available.
  2013. */
  2014. if (port->cons && !(port->cons->flags & CON_ENABLED))
  2015. register_console(port->cons);
  2016. /*
  2017. * Power down all ports by default, except the
  2018. * console if we have one.
  2019. */
  2020. if (!uart_console(port))
  2021. uart_change_pm(state, UART_PM_STATE_OFF);
  2022. }
  2023. }
  2024. #ifdef CONFIG_CONSOLE_POLL
  2025. static int uart_poll_init(struct tty_driver *driver, int line, char *options)
  2026. {
  2027. struct uart_driver *drv = driver->driver_state;
  2028. struct uart_state *state = drv->state + line;
  2029. struct tty_port *tport;
  2030. struct uart_port *port;
  2031. int baud = 9600;
  2032. int bits = 8;
  2033. int parity = 'n';
  2034. int flow = 'n';
  2035. int ret = 0;
  2036. if (!state)
  2037. return -1;
  2038. tport = &state->port;
  2039. mutex_lock(&tport->mutex);
  2040. port = uart_port_check(state);
  2041. if (!port || !(port->ops->poll_get_char && port->ops->poll_put_char)) {
  2042. ret = -1;
  2043. goto out;
  2044. }
  2045. if (port->ops->poll_init) {
  2046. /*
  2047. * We don't set initialized as we only initialized the hw,
  2048. * e.g. state->xmit is still uninitialized.
  2049. */
  2050. if (!tty_port_initialized(tport))
  2051. ret = port->ops->poll_init(port);
  2052. }
  2053. if (!ret && options) {
  2054. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2055. ret = uart_set_options(port, NULL, baud, parity, bits, flow);
  2056. }
  2057. out:
  2058. mutex_unlock(&tport->mutex);
  2059. return ret;
  2060. }
  2061. static int uart_poll_get_char(struct tty_driver *driver, int line)
  2062. {
  2063. struct uart_driver *drv = driver->driver_state;
  2064. struct uart_state *state = drv->state + line;
  2065. struct uart_port *port;
  2066. int ret = -1;
  2067. if (state) {
  2068. port = uart_port_ref(state);
  2069. if (port)
  2070. ret = port->ops->poll_get_char(port);
  2071. uart_port_deref(port);
  2072. }
  2073. return ret;
  2074. }
  2075. static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
  2076. {
  2077. struct uart_driver *drv = driver->driver_state;
  2078. struct uart_state *state = drv->state + line;
  2079. struct uart_port *port;
  2080. if (!state)
  2081. return;
  2082. port = uart_port_ref(state);
  2083. if (!port)
  2084. return;
  2085. if (ch == '\n')
  2086. port->ops->poll_put_char(port, '\r');
  2087. port->ops->poll_put_char(port, ch);
  2088. uart_port_deref(port);
  2089. }
  2090. #endif
  2091. static const struct tty_operations uart_ops = {
  2092. .open = uart_open,
  2093. .close = uart_close,
  2094. .write = uart_write,
  2095. .put_char = uart_put_char,
  2096. .flush_chars = uart_flush_chars,
  2097. .write_room = uart_write_room,
  2098. .chars_in_buffer= uart_chars_in_buffer,
  2099. .flush_buffer = uart_flush_buffer,
  2100. .ioctl = uart_ioctl,
  2101. .throttle = uart_throttle,
  2102. .unthrottle = uart_unthrottle,
  2103. .send_xchar = uart_send_xchar,
  2104. .set_termios = uart_set_termios,
  2105. .set_ldisc = uart_set_ldisc,
  2106. .stop = uart_stop,
  2107. .start = uart_start,
  2108. .hangup = uart_hangup,
  2109. .break_ctl = uart_break_ctl,
  2110. .wait_until_sent= uart_wait_until_sent,
  2111. #ifdef CONFIG_PROC_FS
  2112. .proc_fops = &uart_proc_fops,
  2113. #endif
  2114. .tiocmget = uart_tiocmget,
  2115. .tiocmset = uart_tiocmset,
  2116. .get_icount = uart_get_icount,
  2117. #ifdef CONFIG_CONSOLE_POLL
  2118. .poll_init = uart_poll_init,
  2119. .poll_get_char = uart_poll_get_char,
  2120. .poll_put_char = uart_poll_put_char,
  2121. #endif
  2122. };
  2123. static const struct tty_port_operations uart_port_ops = {
  2124. .carrier_raised = uart_carrier_raised,
  2125. .dtr_rts = uart_dtr_rts,
  2126. };
  2127. /**
  2128. * uart_register_driver - register a driver with the uart core layer
  2129. * @drv: low level driver structure
  2130. *
  2131. * Register a uart driver with the core driver. We in turn register
  2132. * with the tty layer, and initialise the core driver per-port state.
  2133. *
  2134. * We have a proc file in /proc/tty/driver which is named after the
  2135. * normal driver.
  2136. *
  2137. * drv->port should be NULL, and the per-port structures should be
  2138. * registered using uart_add_one_port after this call has succeeded.
  2139. */
  2140. int uart_register_driver(struct uart_driver *drv)
  2141. {
  2142. struct tty_driver *normal;
  2143. int i, retval;
  2144. BUG_ON(drv->state);
  2145. /*
  2146. * Maybe we should be using a slab cache for this, especially if
  2147. * we have a large number of ports to handle.
  2148. */
  2149. drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);
  2150. if (!drv->state)
  2151. goto out;
  2152. normal = alloc_tty_driver(drv->nr);
  2153. if (!normal)
  2154. goto out_kfree;
  2155. drv->tty_driver = normal;
  2156. normal->driver_name = drv->driver_name;
  2157. normal->name = drv->dev_name;
  2158. normal->major = drv->major;
  2159. normal->minor_start = drv->minor;
  2160. normal->type = TTY_DRIVER_TYPE_SERIAL;
  2161. normal->subtype = SERIAL_TYPE_NORMAL;
  2162. normal->init_termios = tty_std_termios;
  2163. normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2164. normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
  2165. normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2166. normal->driver_state = drv;
  2167. tty_set_operations(normal, &uart_ops);
  2168. /*
  2169. * Initialise the UART state(s).
  2170. */
  2171. for (i = 0; i < drv->nr; i++) {
  2172. struct uart_state *state = drv->state + i;
  2173. struct tty_port *port = &state->port;
  2174. tty_port_init(port);
  2175. port->ops = &uart_port_ops;
  2176. }
  2177. retval = tty_register_driver(normal);
  2178. if (retval >= 0)
  2179. return retval;
  2180. for (i = 0; i < drv->nr; i++)
  2181. tty_port_destroy(&drv->state[i].port);
  2182. put_tty_driver(normal);
  2183. out_kfree:
  2184. kfree(drv->state);
  2185. out:
  2186. return -ENOMEM;
  2187. }
  2188. /**
  2189. * uart_unregister_driver - remove a driver from the uart core layer
  2190. * @drv: low level driver structure
  2191. *
  2192. * Remove all references to a driver from the core driver. The low
  2193. * level driver must have removed all its ports via the
  2194. * uart_remove_one_port() if it registered them with uart_add_one_port().
  2195. * (ie, drv->port == NULL)
  2196. */
  2197. void uart_unregister_driver(struct uart_driver *drv)
  2198. {
  2199. struct tty_driver *p = drv->tty_driver;
  2200. unsigned int i;
  2201. tty_unregister_driver(p);
  2202. put_tty_driver(p);
  2203. for (i = 0; i < drv->nr; i++)
  2204. tty_port_destroy(&drv->state[i].port);
  2205. kfree(drv->state);
  2206. drv->state = NULL;
  2207. drv->tty_driver = NULL;
  2208. }
  2209. struct tty_driver *uart_console_device(struct console *co, int *index)
  2210. {
  2211. struct uart_driver *p = co->data;
  2212. *index = co->index;
  2213. return p->tty_driver;
  2214. }
  2215. static ssize_t uart_get_attr_uartclk(struct device *dev,
  2216. struct device_attribute *attr, char *buf)
  2217. {
  2218. struct serial_struct tmp;
  2219. struct tty_port *port = dev_get_drvdata(dev);
  2220. uart_get_info(port, &tmp);
  2221. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
  2222. }
  2223. static ssize_t uart_get_attr_type(struct device *dev,
  2224. struct device_attribute *attr, char *buf)
  2225. {
  2226. struct serial_struct tmp;
  2227. struct tty_port *port = dev_get_drvdata(dev);
  2228. uart_get_info(port, &tmp);
  2229. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
  2230. }
  2231. static ssize_t uart_get_attr_line(struct device *dev,
  2232. struct device_attribute *attr, char *buf)
  2233. {
  2234. struct serial_struct tmp;
  2235. struct tty_port *port = dev_get_drvdata(dev);
  2236. uart_get_info(port, &tmp);
  2237. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
  2238. }
  2239. static ssize_t uart_get_attr_port(struct device *dev,
  2240. struct device_attribute *attr, char *buf)
  2241. {
  2242. struct serial_struct tmp;
  2243. struct tty_port *port = dev_get_drvdata(dev);
  2244. unsigned long ioaddr;
  2245. uart_get_info(port, &tmp);
  2246. ioaddr = tmp.port;
  2247. if (HIGH_BITS_OFFSET)
  2248. ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
  2249. return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
  2250. }
  2251. static ssize_t uart_get_attr_irq(struct device *dev,
  2252. struct device_attribute *attr, char *buf)
  2253. {
  2254. struct serial_struct tmp;
  2255. struct tty_port *port = dev_get_drvdata(dev);
  2256. uart_get_info(port, &tmp);
  2257. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
  2258. }
  2259. static ssize_t uart_get_attr_flags(struct device *dev,
  2260. struct device_attribute *attr, char *buf)
  2261. {
  2262. struct serial_struct tmp;
  2263. struct tty_port *port = dev_get_drvdata(dev);
  2264. uart_get_info(port, &tmp);
  2265. return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
  2266. }
  2267. static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
  2268. struct device_attribute *attr, char *buf)
  2269. {
  2270. struct serial_struct tmp;
  2271. struct tty_port *port = dev_get_drvdata(dev);
  2272. uart_get_info(port, &tmp);
  2273. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
  2274. }
  2275. static ssize_t uart_get_attr_close_delay(struct device *dev,
  2276. struct device_attribute *attr, char *buf)
  2277. {
  2278. struct serial_struct tmp;
  2279. struct tty_port *port = dev_get_drvdata(dev);
  2280. uart_get_info(port, &tmp);
  2281. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
  2282. }
  2283. static ssize_t uart_get_attr_closing_wait(struct device *dev,
  2284. struct device_attribute *attr, char *buf)
  2285. {
  2286. struct serial_struct tmp;
  2287. struct tty_port *port = dev_get_drvdata(dev);
  2288. uart_get_info(port, &tmp);
  2289. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
  2290. }
  2291. static ssize_t uart_get_attr_custom_divisor(struct device *dev,
  2292. struct device_attribute *attr, char *buf)
  2293. {
  2294. struct serial_struct tmp;
  2295. struct tty_port *port = dev_get_drvdata(dev);
  2296. uart_get_info(port, &tmp);
  2297. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
  2298. }
  2299. static ssize_t uart_get_attr_io_type(struct device *dev,
  2300. struct device_attribute *attr, char *buf)
  2301. {
  2302. struct serial_struct tmp;
  2303. struct tty_port *port = dev_get_drvdata(dev);
  2304. uart_get_info(port, &tmp);
  2305. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
  2306. }
  2307. static ssize_t uart_get_attr_iomem_base(struct device *dev,
  2308. struct device_attribute *attr, char *buf)
  2309. {
  2310. struct serial_struct tmp;
  2311. struct tty_port *port = dev_get_drvdata(dev);
  2312. uart_get_info(port, &tmp);
  2313. return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
  2314. }
  2315. static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
  2316. struct device_attribute *attr, char *buf)
  2317. {
  2318. struct serial_struct tmp;
  2319. struct tty_port *port = dev_get_drvdata(dev);
  2320. uart_get_info(port, &tmp);
  2321. return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
  2322. }
  2323. static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
  2324. static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
  2325. static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
  2326. static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
  2327. static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
  2328. static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
  2329. static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
  2330. static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
  2331. static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
  2332. static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
  2333. static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
  2334. static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
  2335. static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
  2336. static struct attribute *tty_dev_attrs[] = {
  2337. &dev_attr_type.attr,
  2338. &dev_attr_line.attr,
  2339. &dev_attr_port.attr,
  2340. &dev_attr_irq.attr,
  2341. &dev_attr_flags.attr,
  2342. &dev_attr_xmit_fifo_size.attr,
  2343. &dev_attr_uartclk.attr,
  2344. &dev_attr_close_delay.attr,
  2345. &dev_attr_closing_wait.attr,
  2346. &dev_attr_custom_divisor.attr,
  2347. &dev_attr_io_type.attr,
  2348. &dev_attr_iomem_base.attr,
  2349. &dev_attr_iomem_reg_shift.attr,
  2350. NULL,
  2351. };
  2352. static const struct attribute_group tty_dev_attr_group = {
  2353. .attrs = tty_dev_attrs,
  2354. };
  2355. /**
  2356. * uart_add_one_port - attach a driver-defined port structure
  2357. * @drv: pointer to the uart low level driver structure for this port
  2358. * @uport: uart port structure to use for this port.
  2359. *
  2360. * This allows the driver to register its own uart_port structure
  2361. * with the core driver. The main purpose is to allow the low
  2362. * level uart drivers to expand uart_port, rather than having yet
  2363. * more levels of structures.
  2364. */
  2365. int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
  2366. {
  2367. struct uart_state *state;
  2368. struct tty_port *port;
  2369. int ret = 0;
  2370. struct device *tty_dev;
  2371. int num_groups;
  2372. BUG_ON(in_interrupt());
  2373. if (uport->line >= drv->nr)
  2374. return -EINVAL;
  2375. state = drv->state + uport->line;
  2376. port = &state->port;
  2377. mutex_lock(&port_mutex);
  2378. mutex_lock(&port->mutex);
  2379. if (state->uart_port) {
  2380. ret = -EINVAL;
  2381. goto out;
  2382. }
  2383. /* Link the port to the driver state table and vice versa */
  2384. atomic_set(&state->refcount, 1);
  2385. init_waitqueue_head(&state->remove_wait);
  2386. state->uart_port = uport;
  2387. uport->state = state;
  2388. state->pm_state = UART_PM_STATE_UNDEFINED;
  2389. uport->cons = drv->cons;
  2390. uport->minor = drv->tty_driver->minor_start + uport->line;
  2391. /*
  2392. * If this port is a console, then the spinlock is already
  2393. * initialised.
  2394. */
  2395. if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
  2396. spin_lock_init(&uport->lock);
  2397. lockdep_set_class(&uport->lock, &port_lock_key);
  2398. }
  2399. if (uport->cons && uport->dev)
  2400. of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
  2401. uart_configure_port(drv, state, uport);
  2402. num_groups = 2;
  2403. if (uport->attr_group)
  2404. num_groups++;
  2405. uport->tty_groups = kcalloc(num_groups, sizeof(*uport->tty_groups),
  2406. GFP_KERNEL);
  2407. if (!uport->tty_groups) {
  2408. ret = -ENOMEM;
  2409. goto out;
  2410. }
  2411. uport->tty_groups[0] = &tty_dev_attr_group;
  2412. if (uport->attr_group)
  2413. uport->tty_groups[1] = uport->attr_group;
  2414. /*
  2415. * Register the port whether it's detected or not. This allows
  2416. * setserial to be used to alter this port's parameters.
  2417. */
  2418. tty_dev = tty_port_register_device_attr(port, drv->tty_driver,
  2419. uport->line, uport->dev, port, uport->tty_groups);
  2420. if (likely(!IS_ERR(tty_dev))) {
  2421. device_set_wakeup_capable(tty_dev, 1);
  2422. } else {
  2423. dev_err(uport->dev, "Cannot register tty device on line %d\n",
  2424. uport->line);
  2425. }
  2426. /*
  2427. * Ensure UPF_DEAD is not set.
  2428. */
  2429. uport->flags &= ~UPF_DEAD;
  2430. out:
  2431. mutex_unlock(&port->mutex);
  2432. mutex_unlock(&port_mutex);
  2433. return ret;
  2434. }
  2435. /**
  2436. * uart_remove_one_port - detach a driver defined port structure
  2437. * @drv: pointer to the uart low level driver structure for this port
  2438. * @uport: uart port structure for this port
  2439. *
  2440. * This unhooks (and hangs up) the specified port structure from the
  2441. * core driver. No further calls will be made to the low-level code
  2442. * for this port.
  2443. */
  2444. int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
  2445. {
  2446. struct uart_state *state = drv->state + uport->line;
  2447. struct tty_port *port = &state->port;
  2448. struct uart_port *uart_port;
  2449. struct tty_struct *tty;
  2450. int ret = 0;
  2451. BUG_ON(in_interrupt());
  2452. mutex_lock(&port_mutex);
  2453. /*
  2454. * Mark the port "dead" - this prevents any opens from
  2455. * succeeding while we shut down the port.
  2456. */
  2457. mutex_lock(&port->mutex);
  2458. uart_port = uart_port_check(state);
  2459. if (uart_port != uport)
  2460. dev_alert(uport->dev, "Removing wrong port: %p != %p\n",
  2461. uart_port, uport);
  2462. if (!uart_port) {
  2463. mutex_unlock(&port->mutex);
  2464. ret = -EINVAL;
  2465. goto out;
  2466. }
  2467. uport->flags |= UPF_DEAD;
  2468. mutex_unlock(&port->mutex);
  2469. /*
  2470. * Remove the devices from the tty layer
  2471. */
  2472. tty_unregister_device(drv->tty_driver, uport->line);
  2473. tty = tty_port_tty_get(port);
  2474. if (tty) {
  2475. tty_vhangup(port->tty);
  2476. tty_kref_put(tty);
  2477. }
  2478. /*
  2479. * If the port is used as a console, unregister it
  2480. */
  2481. if (uart_console(uport))
  2482. unregister_console(uport->cons);
  2483. /*
  2484. * Free the port IO and memory resources, if any.
  2485. */
  2486. if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
  2487. uport->ops->release_port(uport);
  2488. kfree(uport->tty_groups);
  2489. /*
  2490. * Indicate that there isn't a port here anymore.
  2491. */
  2492. uport->type = PORT_UNKNOWN;
  2493. mutex_lock(&port->mutex);
  2494. WARN_ON(atomic_dec_return(&state->refcount) < 0);
  2495. wait_event(state->remove_wait, !atomic_read(&state->refcount));
  2496. state->uart_port = NULL;
  2497. mutex_unlock(&port->mutex);
  2498. out:
  2499. mutex_unlock(&port_mutex);
  2500. return ret;
  2501. }
  2502. /*
  2503. * Are the two ports equivalent?
  2504. */
  2505. int uart_match_port(struct uart_port *port1, struct uart_port *port2)
  2506. {
  2507. if (port1->iotype != port2->iotype)
  2508. return 0;
  2509. switch (port1->iotype) {
  2510. case UPIO_PORT:
  2511. return (port1->iobase == port2->iobase);
  2512. case UPIO_HUB6:
  2513. return (port1->iobase == port2->iobase) &&
  2514. (port1->hub6 == port2->hub6);
  2515. case UPIO_MEM:
  2516. case UPIO_MEM16:
  2517. case UPIO_MEM32:
  2518. case UPIO_MEM32BE:
  2519. case UPIO_AU:
  2520. case UPIO_TSI:
  2521. return (port1->mapbase == port2->mapbase);
  2522. }
  2523. return 0;
  2524. }
  2525. EXPORT_SYMBOL(uart_match_port);
  2526. /**
  2527. * uart_handle_dcd_change - handle a change of carrier detect state
  2528. * @uport: uart_port structure for the open port
  2529. * @status: new carrier detect status, nonzero if active
  2530. *
  2531. * Caller must hold uport->lock
  2532. */
  2533. void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
  2534. {
  2535. struct tty_port *port = &uport->state->port;
  2536. struct tty_struct *tty = port->tty;
  2537. struct tty_ldisc *ld;
  2538. lockdep_assert_held_once(&uport->lock);
  2539. if (tty) {
  2540. ld = tty_ldisc_ref(tty);
  2541. if (ld) {
  2542. if (ld->ops->dcd_change)
  2543. ld->ops->dcd_change(tty, status);
  2544. tty_ldisc_deref(ld);
  2545. }
  2546. }
  2547. uport->icount.dcd++;
  2548. if (uart_dcd_enabled(uport)) {
  2549. if (status)
  2550. wake_up_interruptible(&port->open_wait);
  2551. else if (tty)
  2552. tty_hangup(tty);
  2553. }
  2554. }
  2555. EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
  2556. /**
  2557. * uart_handle_cts_change - handle a change of clear-to-send state
  2558. * @uport: uart_port structure for the open port
  2559. * @status: new clear to send status, nonzero if active
  2560. *
  2561. * Caller must hold uport->lock
  2562. */
  2563. void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
  2564. {
  2565. lockdep_assert_held_once(&uport->lock);
  2566. uport->icount.cts++;
  2567. if (uart_softcts_mode(uport)) {
  2568. if (uport->hw_stopped) {
  2569. if (status) {
  2570. uport->hw_stopped = 0;
  2571. uport->ops->start_tx(uport);
  2572. uart_write_wakeup(uport);
  2573. }
  2574. } else {
  2575. if (!status) {
  2576. uport->hw_stopped = 1;
  2577. uport->ops->stop_tx(uport);
  2578. }
  2579. }
  2580. }
  2581. }
  2582. EXPORT_SYMBOL_GPL(uart_handle_cts_change);
  2583. /**
  2584. * uart_insert_char - push a char to the uart layer
  2585. *
  2586. * User is responsible to call tty_flip_buffer_push when they are done with
  2587. * insertion.
  2588. *
  2589. * @port: corresponding port
  2590. * @status: state of the serial port RX buffer (LSR for 8250)
  2591. * @overrun: mask of overrun bits in @status
  2592. * @ch: character to push
  2593. * @flag: flag for the character (see TTY_NORMAL and friends)
  2594. */
  2595. void uart_insert_char(struct uart_port *port, unsigned int status,
  2596. unsigned int overrun, unsigned int ch, unsigned int flag)
  2597. {
  2598. struct tty_port *tport = &port->state->port;
  2599. if ((status & port->ignore_status_mask & ~overrun) == 0)
  2600. if (tty_insert_flip_char(tport, ch, flag) == 0)
  2601. ++port->icount.buf_overrun;
  2602. /*
  2603. * Overrun is special. Since it's reported immediately,
  2604. * it doesn't affect the current character.
  2605. */
  2606. if (status & ~port->ignore_status_mask & overrun)
  2607. if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
  2608. ++port->icount.buf_overrun;
  2609. }
  2610. EXPORT_SYMBOL_GPL(uart_insert_char);
  2611. EXPORT_SYMBOL(uart_write_wakeup);
  2612. EXPORT_SYMBOL(uart_register_driver);
  2613. EXPORT_SYMBOL(uart_unregister_driver);
  2614. EXPORT_SYMBOL(uart_suspend_port);
  2615. EXPORT_SYMBOL(uart_resume_port);
  2616. EXPORT_SYMBOL(uart_add_one_port);
  2617. EXPORT_SYMBOL(uart_remove_one_port);
  2618. MODULE_DESCRIPTION("Serial driver core");
  2619. MODULE_LICENSE("GPL");