serial_core.c 62 KB

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