serial_core.c 62 KB

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