serial_core.c 70 KB

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