serial_core.c 55 KB

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