serial_core.c 63 KB

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