serial_core.c 58 KB

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