serial_core.c 57 KB

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