serial_core.c 63 KB

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