serial_core.c 56 KB

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