amiserial.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /*
  2. * linux/drivers/char/amiserial.c
  3. *
  4. * Serial driver for the amiga builtin port.
  5. *
  6. * This code was created by taking serial.c version 4.30 from kernel
  7. * release 2.3.22, replacing all hardware related stuff with the
  8. * corresponding amiga hardware actions, and removing all irrelevant
  9. * code. As a consequence, it uses many of the constants and names
  10. * associated with the registers and bits of 16550 compatible UARTS -
  11. * but only to keep track of status, etc in the state variables. It
  12. * was done this was to make it easier to keep the code in line with
  13. * (non hardware specific) changes to serial.c.
  14. *
  15. * The port is registered with the tty driver as minor device 64, and
  16. * therefore other ports should should only use 65 upwards.
  17. *
  18. * Richard Lucock 28/12/99
  19. *
  20. * Copyright (C) 1991, 1992 Linus Torvalds
  21. * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
  22. * 1998, 1999 Theodore Ts'o
  23. *
  24. */
  25. /*
  26. * Serial driver configuration section. Here are the various options:
  27. *
  28. * SERIAL_PARANOIA_CHECK
  29. * Check the magic number for the async_structure where
  30. * ever possible.
  31. */
  32. #include <linux/config.h>
  33. #include <linux/delay.h>
  34. #undef SERIAL_PARANOIA_CHECK
  35. #define SERIAL_DO_RESTART
  36. /* Set of debugging defines */
  37. #undef SERIAL_DEBUG_INTR
  38. #undef SERIAL_DEBUG_OPEN
  39. #undef SERIAL_DEBUG_FLOW
  40. #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  41. /* Sanity checks */
  42. #define SERIAL_INLINE
  43. #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
  44. #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
  45. tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
  46. #else
  47. #define DBG_CNT(s)
  48. #endif
  49. /*
  50. * End of serial driver configuration section.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/types.h>
  54. #include <linux/serial.h>
  55. #include <linux/serialP.h>
  56. #include <linux/serial_reg.h>
  57. static char *serial_version = "4.30";
  58. #include <linux/errno.h>
  59. #include <linux/signal.h>
  60. #include <linux/sched.h>
  61. #include <linux/kernel.h>
  62. #include <linux/timer.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/tty.h>
  65. #include <linux/tty_flip.h>
  66. #include <linux/console.h>
  67. #include <linux/major.h>
  68. #include <linux/string.h>
  69. #include <linux/fcntl.h>
  70. #include <linux/ptrace.h>
  71. #include <linux/ioport.h>
  72. #include <linux/mm.h>
  73. #include <linux/slab.h>
  74. #include <linux/init.h>
  75. #include <linux/bitops.h>
  76. #include <asm/setup.h>
  77. #include <asm/system.h>
  78. #include <asm/irq.h>
  79. #include <asm/amigahw.h>
  80. #include <asm/amigaints.h>
  81. #ifdef SERIAL_INLINE
  82. #define _INLINE_ inline
  83. #endif
  84. static char *serial_name = "Amiga-builtin serial driver";
  85. static struct tty_driver *serial_driver;
  86. /* number of characters left in xmit buffer before we ask for more */
  87. #define WAKEUP_CHARS 256
  88. static struct async_struct *IRQ_ports;
  89. static unsigned char current_ctl_bits;
  90. static void change_speed(struct async_struct *info, struct termios *old);
  91. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  92. static struct serial_state rs_table[1];
  93. #define NR_PORTS ARRAY_SIZE(rs_table)
  94. /*
  95. * tmp_buf is used as a temporary buffer by serial_write. We need to
  96. * lock it in case the copy_from_user blocks while swapping in a page,
  97. * and some other program tries to do a serial write at the same time.
  98. * Since the lock will only come under contention when the system is
  99. * swapping and available memory is low, it makes sense to share one
  100. * buffer across all the serial ports, since it significantly saves
  101. * memory if large numbers of serial ports are open.
  102. */
  103. static unsigned char *tmp_buf;
  104. static DECLARE_MUTEX(tmp_buf_sem);
  105. #include <asm/uaccess.h>
  106. #define serial_isroot() (capable(CAP_SYS_ADMIN))
  107. static inline int serial_paranoia_check(struct async_struct *info,
  108. char *name, const char *routine)
  109. {
  110. #ifdef SERIAL_PARANOIA_CHECK
  111. static const char *badmagic =
  112. "Warning: bad magic number for serial struct (%s) in %s\n";
  113. static const char *badinfo =
  114. "Warning: null async_struct for (%s) in %s\n";
  115. if (!info) {
  116. printk(badinfo, name, routine);
  117. return 1;
  118. }
  119. if (info->magic != SERIAL_MAGIC) {
  120. printk(badmagic, name, routine);
  121. return 1;
  122. }
  123. #endif
  124. return 0;
  125. }
  126. /* some serial hardware definitions */
  127. #define SDR_OVRUN (1<<15)
  128. #define SDR_RBF (1<<14)
  129. #define SDR_TBE (1<<13)
  130. #define SDR_TSRE (1<<12)
  131. #define SERPER_PARENB (1<<15)
  132. #define AC_SETCLR (1<<15)
  133. #define AC_UARTBRK (1<<11)
  134. #define SER_DTR (1<<7)
  135. #define SER_RTS (1<<6)
  136. #define SER_DCD (1<<5)
  137. #define SER_CTS (1<<4)
  138. #define SER_DSR (1<<3)
  139. static __inline__ void rtsdtr_ctrl(int bits)
  140. {
  141. ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
  142. }
  143. /*
  144. * ------------------------------------------------------------
  145. * rs_stop() and rs_start()
  146. *
  147. * This routines are called before setting or resetting tty->stopped.
  148. * They enable or disable transmitter interrupts, as necessary.
  149. * ------------------------------------------------------------
  150. */
  151. static void rs_stop(struct tty_struct *tty)
  152. {
  153. struct async_struct *info = (struct async_struct *)tty->driver_data;
  154. unsigned long flags;
  155. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  156. return;
  157. local_irq_save(flags);
  158. if (info->IER & UART_IER_THRI) {
  159. info->IER &= ~UART_IER_THRI;
  160. /* disable Tx interrupt and remove any pending interrupts */
  161. custom.intena = IF_TBE;
  162. mb();
  163. custom.intreq = IF_TBE;
  164. mb();
  165. }
  166. local_irq_restore(flags);
  167. }
  168. static void rs_start(struct tty_struct *tty)
  169. {
  170. struct async_struct *info = (struct async_struct *)tty->driver_data;
  171. unsigned long flags;
  172. if (serial_paranoia_check(info, tty->name, "rs_start"))
  173. return;
  174. local_irq_save(flags);
  175. if (info->xmit.head != info->xmit.tail
  176. && info->xmit.buf
  177. && !(info->IER & UART_IER_THRI)) {
  178. info->IER |= UART_IER_THRI;
  179. custom.intena = IF_SETCLR | IF_TBE;
  180. mb();
  181. /* set a pending Tx Interrupt, transmitter should restart now */
  182. custom.intreq = IF_SETCLR | IF_TBE;
  183. mb();
  184. }
  185. local_irq_restore(flags);
  186. }
  187. /*
  188. * ----------------------------------------------------------------------
  189. *
  190. * Here starts the interrupt handling routines. All of the following
  191. * subroutines are declared as inline and are folded into
  192. * rs_interrupt(). They were separated out for readability's sake.
  193. *
  194. * Note: rs_interrupt() is a "fast" interrupt, which means that it
  195. * runs with interrupts turned off. People who may want to modify
  196. * rs_interrupt() should try to keep the interrupt handler as fast as
  197. * possible. After you are done making modifications, it is not a bad
  198. * idea to do:
  199. *
  200. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  201. *
  202. * and look at the resulting assemble code in serial.s.
  203. *
  204. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  205. * -----------------------------------------------------------------------
  206. */
  207. /*
  208. * This routine is used by the interrupt handler to schedule
  209. * processing in the software interrupt portion of the driver.
  210. */
  211. static _INLINE_ void rs_sched_event(struct async_struct *info,
  212. int event)
  213. {
  214. info->event |= 1 << event;
  215. tasklet_schedule(&info->tlet);
  216. }
  217. static _INLINE_ void receive_chars(struct async_struct *info)
  218. {
  219. int status;
  220. int serdatr;
  221. struct tty_struct *tty = info->tty;
  222. unsigned char ch, flag;
  223. struct async_icount *icount;
  224. int oe = 0;
  225. icount = &info->state->icount;
  226. status = UART_LSR_DR; /* We obviously have a character! */
  227. serdatr = custom.serdatr;
  228. mb();
  229. custom.intreq = IF_RBF;
  230. mb();
  231. if((serdatr & 0x1ff) == 0)
  232. status |= UART_LSR_BI;
  233. if(serdatr & SDR_OVRUN)
  234. status |= UART_LSR_OE;
  235. ch = serdatr & 0xff;
  236. icount->rx++;
  237. #ifdef SERIAL_DEBUG_INTR
  238. printk("DR%02x:%02x...", ch, status);
  239. #endif
  240. flag = TTY_NORMAL;
  241. /*
  242. * We don't handle parity or frame errors - but I have left
  243. * the code in, since I'm not sure that the errors can't be
  244. * detected.
  245. */
  246. if (status & (UART_LSR_BI | UART_LSR_PE |
  247. UART_LSR_FE | UART_LSR_OE)) {
  248. /*
  249. * For statistics only
  250. */
  251. if (status & UART_LSR_BI) {
  252. status &= ~(UART_LSR_FE | UART_LSR_PE);
  253. icount->brk++;
  254. } else if (status & UART_LSR_PE)
  255. icount->parity++;
  256. else if (status & UART_LSR_FE)
  257. icount->frame++;
  258. if (status & UART_LSR_OE)
  259. icount->overrun++;
  260. /*
  261. * Now check to see if character should be
  262. * ignored, and mask off conditions which
  263. * should be ignored.
  264. */
  265. if (status & info->ignore_status_mask)
  266. goto out;
  267. status &= info->read_status_mask;
  268. if (status & (UART_LSR_BI)) {
  269. #ifdef SERIAL_DEBUG_INTR
  270. printk("handling break....");
  271. #endif
  272. flag = TTY_BREAK;
  273. if (info->flags & ASYNC_SAK)
  274. do_SAK(tty);
  275. } else if (status & UART_LSR_PE)
  276. flag = TTY_PARITY;
  277. else if (status & UART_LSR_FE)
  278. flag = TTY_FRAME;
  279. if (status & UART_LSR_OE) {
  280. /*
  281. * Overrun is special, since it's
  282. * reported immediately, and doesn't
  283. * affect the current character
  284. */
  285. oe = 1;
  286. }
  287. }
  288. tty_insert_flip_char(tty, ch, flag);
  289. if (oe == 1)
  290. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  291. tty_flip_buffer_push(tty);
  292. out:
  293. return;
  294. }
  295. static _INLINE_ void transmit_chars(struct async_struct *info)
  296. {
  297. custom.intreq = IF_TBE;
  298. mb();
  299. if (info->x_char) {
  300. custom.serdat = info->x_char | 0x100;
  301. mb();
  302. info->state->icount.tx++;
  303. info->x_char = 0;
  304. return;
  305. }
  306. if (info->xmit.head == info->xmit.tail
  307. || info->tty->stopped
  308. || info->tty->hw_stopped) {
  309. info->IER &= ~UART_IER_THRI;
  310. custom.intena = IF_TBE;
  311. mb();
  312. return;
  313. }
  314. custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
  315. mb();
  316. info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
  317. info->state->icount.tx++;
  318. if (CIRC_CNT(info->xmit.head,
  319. info->xmit.tail,
  320. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  321. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  322. #ifdef SERIAL_DEBUG_INTR
  323. printk("THRE...");
  324. #endif
  325. if (info->xmit.head == info->xmit.tail) {
  326. custom.intena = IF_TBE;
  327. mb();
  328. info->IER &= ~UART_IER_THRI;
  329. }
  330. }
  331. static _INLINE_ void check_modem_status(struct async_struct *info)
  332. {
  333. unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  334. unsigned char dstatus;
  335. struct async_icount *icount;
  336. /* Determine bits that have changed */
  337. dstatus = status ^ current_ctl_bits;
  338. current_ctl_bits = status;
  339. if (dstatus) {
  340. icount = &info->state->icount;
  341. /* update input line counters */
  342. if (dstatus & SER_DSR)
  343. icount->dsr++;
  344. if (dstatus & SER_DCD) {
  345. icount->dcd++;
  346. #ifdef CONFIG_HARD_PPS
  347. if ((info->flags & ASYNC_HARDPPS_CD) &&
  348. !(status & SER_DCD))
  349. hardpps();
  350. #endif
  351. }
  352. if (dstatus & SER_CTS)
  353. icount->cts++;
  354. wake_up_interruptible(&info->delta_msr_wait);
  355. }
  356. if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
  357. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  358. printk("ttyS%d CD now %s...", info->line,
  359. (!(status & SER_DCD)) ? "on" : "off");
  360. #endif
  361. if (!(status & SER_DCD))
  362. wake_up_interruptible(&info->open_wait);
  363. else {
  364. #ifdef SERIAL_DEBUG_OPEN
  365. printk("doing serial hangup...");
  366. #endif
  367. if (info->tty)
  368. tty_hangup(info->tty);
  369. }
  370. }
  371. if (info->flags & ASYNC_CTS_FLOW) {
  372. if (info->tty->hw_stopped) {
  373. if (!(status & SER_CTS)) {
  374. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  375. printk("CTS tx start...");
  376. #endif
  377. info->tty->hw_stopped = 0;
  378. info->IER |= UART_IER_THRI;
  379. custom.intena = IF_SETCLR | IF_TBE;
  380. mb();
  381. /* set a pending Tx Interrupt, transmitter should restart now */
  382. custom.intreq = IF_SETCLR | IF_TBE;
  383. mb();
  384. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  385. return;
  386. }
  387. } else {
  388. if ((status & SER_CTS)) {
  389. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  390. printk("CTS tx stop...");
  391. #endif
  392. info->tty->hw_stopped = 1;
  393. info->IER &= ~UART_IER_THRI;
  394. /* disable Tx interrupt and remove any pending interrupts */
  395. custom.intena = IF_TBE;
  396. mb();
  397. custom.intreq = IF_TBE;
  398. mb();
  399. }
  400. }
  401. }
  402. }
  403. static irqreturn_t ser_vbl_int( int irq, void *data, struct pt_regs *regs)
  404. {
  405. /* vbl is just a periodic interrupt we tie into to update modem status */
  406. struct async_struct * info = IRQ_ports;
  407. /*
  408. * TBD - is it better to unregister from this interrupt or to
  409. * ignore it if MSI is clear ?
  410. */
  411. if(info->IER & UART_IER_MSI)
  412. check_modem_status(info);
  413. return IRQ_HANDLED;
  414. }
  415. static irqreturn_t ser_rx_int(int irq, void *dev_id, struct pt_regs * regs)
  416. {
  417. struct async_struct * info;
  418. #ifdef SERIAL_DEBUG_INTR
  419. printk("ser_rx_int...");
  420. #endif
  421. info = IRQ_ports;
  422. if (!info || !info->tty)
  423. return IRQ_NONE;
  424. receive_chars(info);
  425. info->last_active = jiffies;
  426. #ifdef SERIAL_DEBUG_INTR
  427. printk("end.\n");
  428. #endif
  429. return IRQ_HANDLED;
  430. }
  431. static irqreturn_t ser_tx_int(int irq, void *dev_id, struct pt_regs * regs)
  432. {
  433. struct async_struct * info;
  434. if (custom.serdatr & SDR_TBE) {
  435. #ifdef SERIAL_DEBUG_INTR
  436. printk("ser_tx_int...");
  437. #endif
  438. info = IRQ_ports;
  439. if (!info || !info->tty)
  440. return IRQ_NONE;
  441. transmit_chars(info);
  442. info->last_active = jiffies;
  443. #ifdef SERIAL_DEBUG_INTR
  444. printk("end.\n");
  445. #endif
  446. }
  447. return IRQ_HANDLED;
  448. }
  449. /*
  450. * -------------------------------------------------------------------
  451. * Here ends the serial interrupt routines.
  452. * -------------------------------------------------------------------
  453. */
  454. /*
  455. * This routine is used to handle the "bottom half" processing for the
  456. * serial driver, known also the "software interrupt" processing.
  457. * This processing is done at the kernel interrupt level, after the
  458. * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
  459. * is where time-consuming activities which can not be done in the
  460. * interrupt driver proper are done; the interrupt driver schedules
  461. * them using rs_sched_event(), and they get done here.
  462. */
  463. static void do_softint(unsigned long private_)
  464. {
  465. struct async_struct *info = (struct async_struct *) private_;
  466. struct tty_struct *tty;
  467. tty = info->tty;
  468. if (!tty)
  469. return;
  470. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  471. tty_wakeup(tty);
  472. wake_up_interruptible(&tty->write_wait);
  473. }
  474. }
  475. /*
  476. * ---------------------------------------------------------------
  477. * Low level utility subroutines for the serial driver: routines to
  478. * figure out the appropriate timeout for an interrupt chain, routines
  479. * to initialize and startup a serial port, and routines to shutdown a
  480. * serial port. Useful stuff like that.
  481. * ---------------------------------------------------------------
  482. */
  483. static int startup(struct async_struct * info)
  484. {
  485. unsigned long flags;
  486. int retval=0;
  487. unsigned long page;
  488. page = get_zeroed_page(GFP_KERNEL);
  489. if (!page)
  490. return -ENOMEM;
  491. local_irq_save(flags);
  492. if (info->flags & ASYNC_INITIALIZED) {
  493. free_page(page);
  494. goto errout;
  495. }
  496. if (info->xmit.buf)
  497. free_page(page);
  498. else
  499. info->xmit.buf = (unsigned char *) page;
  500. #ifdef SERIAL_DEBUG_OPEN
  501. printk("starting up ttys%d ...", info->line);
  502. #endif
  503. /* Clear anything in the input buffer */
  504. custom.intreq = IF_RBF;
  505. mb();
  506. retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
  507. if (retval) {
  508. if (serial_isroot()) {
  509. if (info->tty)
  510. set_bit(TTY_IO_ERROR,
  511. &info->tty->flags);
  512. retval = 0;
  513. }
  514. goto errout;
  515. }
  516. /* enable both Rx and Tx interrupts */
  517. custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
  518. mb();
  519. info->IER = UART_IER_MSI;
  520. /* remember current state of the DCD and CTS bits */
  521. current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  522. IRQ_ports = info;
  523. info->MCR = 0;
  524. if (info->tty->termios->c_cflag & CBAUD)
  525. info->MCR = SER_DTR | SER_RTS;
  526. rtsdtr_ctrl(info->MCR);
  527. if (info->tty)
  528. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  529. info->xmit.head = info->xmit.tail = 0;
  530. /*
  531. * Set up the tty->alt_speed kludge
  532. */
  533. if (info->tty) {
  534. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  535. info->tty->alt_speed = 57600;
  536. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  537. info->tty->alt_speed = 115200;
  538. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  539. info->tty->alt_speed = 230400;
  540. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  541. info->tty->alt_speed = 460800;
  542. }
  543. /*
  544. * and set the speed of the serial port
  545. */
  546. change_speed(info, NULL);
  547. info->flags |= ASYNC_INITIALIZED;
  548. local_irq_restore(flags);
  549. return 0;
  550. errout:
  551. local_irq_restore(flags);
  552. return retval;
  553. }
  554. /*
  555. * This routine will shutdown a serial port; interrupts are disabled, and
  556. * DTR is dropped if the hangup on close termio flag is on.
  557. */
  558. static void shutdown(struct async_struct * info)
  559. {
  560. unsigned long flags;
  561. struct serial_state *state;
  562. if (!(info->flags & ASYNC_INITIALIZED))
  563. return;
  564. state = info->state;
  565. #ifdef SERIAL_DEBUG_OPEN
  566. printk("Shutting down serial port %d ....\n", info->line);
  567. #endif
  568. local_irq_save(flags); /* Disable interrupts */
  569. /*
  570. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  571. * here so the queue might never be waken up
  572. */
  573. wake_up_interruptible(&info->delta_msr_wait);
  574. IRQ_ports = NULL;
  575. /*
  576. * Free the IRQ, if necessary
  577. */
  578. free_irq(IRQ_AMIGA_VERTB, info);
  579. if (info->xmit.buf) {
  580. free_page((unsigned long) info->xmit.buf);
  581. info->xmit.buf = NULL;
  582. }
  583. info->IER = 0;
  584. custom.intena = IF_RBF | IF_TBE;
  585. mb();
  586. /* disable break condition */
  587. custom.adkcon = AC_UARTBRK;
  588. mb();
  589. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  590. info->MCR &= ~(SER_DTR|SER_RTS);
  591. rtsdtr_ctrl(info->MCR);
  592. if (info->tty)
  593. set_bit(TTY_IO_ERROR, &info->tty->flags);
  594. info->flags &= ~ASYNC_INITIALIZED;
  595. local_irq_restore(flags);
  596. }
  597. /*
  598. * This routine is called to set the UART divisor registers to match
  599. * the specified baud rate for a serial port.
  600. */
  601. static void change_speed(struct async_struct *info,
  602. struct termios *old_termios)
  603. {
  604. int quot = 0, baud_base, baud;
  605. unsigned cflag, cval = 0;
  606. int bits;
  607. unsigned long flags;
  608. if (!info->tty || !info->tty->termios)
  609. return;
  610. cflag = info->tty->termios->c_cflag;
  611. /* Byte size is always 8 bits plus parity bit if requested */
  612. cval = 3; bits = 10;
  613. if (cflag & CSTOPB) {
  614. cval |= 0x04;
  615. bits++;
  616. }
  617. if (cflag & PARENB) {
  618. cval |= UART_LCR_PARITY;
  619. bits++;
  620. }
  621. if (!(cflag & PARODD))
  622. cval |= UART_LCR_EPAR;
  623. #ifdef CMSPAR
  624. if (cflag & CMSPAR)
  625. cval |= UART_LCR_SPAR;
  626. #endif
  627. /* Determine divisor based on baud rate */
  628. baud = tty_get_baud_rate(info->tty);
  629. if (!baud)
  630. baud = 9600; /* B0 transition handled in rs_set_termios */
  631. baud_base = info->state->baud_base;
  632. if (baud == 38400 &&
  633. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  634. quot = info->state->custom_divisor;
  635. else {
  636. if (baud == 134)
  637. /* Special case since 134 is really 134.5 */
  638. quot = (2*baud_base / 269);
  639. else if (baud)
  640. quot = baud_base / baud;
  641. }
  642. /* If the quotient is zero refuse the change */
  643. if (!quot && old_termios) {
  644. info->tty->termios->c_cflag &= ~CBAUD;
  645. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  646. baud = tty_get_baud_rate(info->tty);
  647. if (!baud)
  648. baud = 9600;
  649. if (baud == 38400 &&
  650. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  651. quot = info->state->custom_divisor;
  652. else {
  653. if (baud == 134)
  654. /* Special case since 134 is really 134.5 */
  655. quot = (2*baud_base / 269);
  656. else if (baud)
  657. quot = baud_base / baud;
  658. }
  659. }
  660. /* As a last resort, if the quotient is zero, default to 9600 bps */
  661. if (!quot)
  662. quot = baud_base / 9600;
  663. info->quot = quot;
  664. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  665. info->timeout += HZ/50; /* Add .02 seconds of slop */
  666. /* CTS flow control flag and modem status interrupts */
  667. info->IER &= ~UART_IER_MSI;
  668. if (info->flags & ASYNC_HARDPPS_CD)
  669. info->IER |= UART_IER_MSI;
  670. if (cflag & CRTSCTS) {
  671. info->flags |= ASYNC_CTS_FLOW;
  672. info->IER |= UART_IER_MSI;
  673. } else
  674. info->flags &= ~ASYNC_CTS_FLOW;
  675. if (cflag & CLOCAL)
  676. info->flags &= ~ASYNC_CHECK_CD;
  677. else {
  678. info->flags |= ASYNC_CHECK_CD;
  679. info->IER |= UART_IER_MSI;
  680. }
  681. /* TBD:
  682. * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
  683. */
  684. /*
  685. * Set up parity check flag
  686. */
  687. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  688. info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  689. if (I_INPCK(info->tty))
  690. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  691. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  692. info->read_status_mask |= UART_LSR_BI;
  693. /*
  694. * Characters to ignore
  695. */
  696. info->ignore_status_mask = 0;
  697. if (I_IGNPAR(info->tty))
  698. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  699. if (I_IGNBRK(info->tty)) {
  700. info->ignore_status_mask |= UART_LSR_BI;
  701. /*
  702. * If we're ignore parity and break indicators, ignore
  703. * overruns too. (For real raw support).
  704. */
  705. if (I_IGNPAR(info->tty))
  706. info->ignore_status_mask |= UART_LSR_OE;
  707. }
  708. /*
  709. * !!! ignore all characters if CREAD is not set
  710. */
  711. if ((cflag & CREAD) == 0)
  712. info->ignore_status_mask |= UART_LSR_DR;
  713. local_irq_save(flags);
  714. {
  715. short serper;
  716. /* Set up the baud rate */
  717. serper = quot - 1;
  718. /* Enable or disable parity bit */
  719. if(cval & UART_LCR_PARITY)
  720. serper |= (SERPER_PARENB);
  721. custom.serper = serper;
  722. mb();
  723. }
  724. info->LCR = cval; /* Save LCR */
  725. local_irq_restore(flags);
  726. }
  727. static void rs_put_char(struct tty_struct *tty, unsigned char ch)
  728. {
  729. struct async_struct *info;
  730. unsigned long flags;
  731. if (!tty)
  732. return;
  733. info = tty->driver_data;
  734. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  735. return;
  736. if (!info->xmit.buf)
  737. return;
  738. local_irq_save(flags);
  739. if (CIRC_SPACE(info->xmit.head,
  740. info->xmit.tail,
  741. SERIAL_XMIT_SIZE) == 0) {
  742. local_irq_restore(flags);
  743. return;
  744. }
  745. info->xmit.buf[info->xmit.head++] = ch;
  746. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  747. local_irq_restore(flags);
  748. }
  749. static void rs_flush_chars(struct tty_struct *tty)
  750. {
  751. struct async_struct *info = (struct async_struct *)tty->driver_data;
  752. unsigned long flags;
  753. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  754. return;
  755. if (info->xmit.head == info->xmit.tail
  756. || tty->stopped
  757. || tty->hw_stopped
  758. || !info->xmit.buf)
  759. return;
  760. local_irq_save(flags);
  761. info->IER |= UART_IER_THRI;
  762. custom.intena = IF_SETCLR | IF_TBE;
  763. mb();
  764. /* set a pending Tx Interrupt, transmitter should restart now */
  765. custom.intreq = IF_SETCLR | IF_TBE;
  766. mb();
  767. local_irq_restore(flags);
  768. }
  769. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  770. {
  771. int c, ret = 0;
  772. struct async_struct *info;
  773. unsigned long flags;
  774. if (!tty)
  775. return 0;
  776. info = tty->driver_data;
  777. if (serial_paranoia_check(info, tty->name, "rs_write"))
  778. return 0;
  779. if (!info->xmit.buf || !tmp_buf)
  780. return 0;
  781. local_save_flags(flags);
  782. local_irq_disable();
  783. while (1) {
  784. c = CIRC_SPACE_TO_END(info->xmit.head,
  785. info->xmit.tail,
  786. SERIAL_XMIT_SIZE);
  787. if (count < c)
  788. c = count;
  789. if (c <= 0) {
  790. break;
  791. }
  792. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  793. info->xmit.head = ((info->xmit.head + c) &
  794. (SERIAL_XMIT_SIZE-1));
  795. buf += c;
  796. count -= c;
  797. ret += c;
  798. }
  799. local_irq_restore(flags);
  800. if (info->xmit.head != info->xmit.tail
  801. && !tty->stopped
  802. && !tty->hw_stopped
  803. && !(info->IER & UART_IER_THRI)) {
  804. info->IER |= UART_IER_THRI;
  805. local_irq_disable();
  806. custom.intena = IF_SETCLR | IF_TBE;
  807. mb();
  808. /* set a pending Tx Interrupt, transmitter should restart now */
  809. custom.intreq = IF_SETCLR | IF_TBE;
  810. mb();
  811. local_irq_restore(flags);
  812. }
  813. return ret;
  814. }
  815. static int rs_write_room(struct tty_struct *tty)
  816. {
  817. struct async_struct *info = (struct async_struct *)tty->driver_data;
  818. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  819. return 0;
  820. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  821. }
  822. static int rs_chars_in_buffer(struct tty_struct *tty)
  823. {
  824. struct async_struct *info = (struct async_struct *)tty->driver_data;
  825. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  826. return 0;
  827. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  828. }
  829. static void rs_flush_buffer(struct tty_struct *tty)
  830. {
  831. struct async_struct *info = (struct async_struct *)tty->driver_data;
  832. unsigned long flags;
  833. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  834. return;
  835. local_irq_save(flags);
  836. info->xmit.head = info->xmit.tail = 0;
  837. local_irq_restore(flags);
  838. wake_up_interruptible(&tty->write_wait);
  839. tty_wakeup(tty);
  840. }
  841. /*
  842. * This function is used to send a high-priority XON/XOFF character to
  843. * the device
  844. */
  845. static void rs_send_xchar(struct tty_struct *tty, char ch)
  846. {
  847. struct async_struct *info = (struct async_struct *)tty->driver_data;
  848. unsigned long flags;
  849. if (serial_paranoia_check(info, tty->name, "rs_send_char"))
  850. return;
  851. info->x_char = ch;
  852. if (ch) {
  853. /* Make sure transmit interrupts are on */
  854. /* Check this ! */
  855. local_irq_save(flags);
  856. if(!(custom.intenar & IF_TBE)) {
  857. custom.intena = IF_SETCLR | IF_TBE;
  858. mb();
  859. /* set a pending Tx Interrupt, transmitter should restart now */
  860. custom.intreq = IF_SETCLR | IF_TBE;
  861. mb();
  862. }
  863. local_irq_restore(flags);
  864. info->IER |= UART_IER_THRI;
  865. }
  866. }
  867. /*
  868. * ------------------------------------------------------------
  869. * rs_throttle()
  870. *
  871. * This routine is called by the upper-layer tty layer to signal that
  872. * incoming characters should be throttled.
  873. * ------------------------------------------------------------
  874. */
  875. static void rs_throttle(struct tty_struct * tty)
  876. {
  877. struct async_struct *info = (struct async_struct *)tty->driver_data;
  878. unsigned long flags;
  879. #ifdef SERIAL_DEBUG_THROTTLE
  880. char buf[64];
  881. printk("throttle %s: %d....\n", tty_name(tty, buf),
  882. tty->ldisc.chars_in_buffer(tty));
  883. #endif
  884. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  885. return;
  886. if (I_IXOFF(tty))
  887. rs_send_xchar(tty, STOP_CHAR(tty));
  888. if (tty->termios->c_cflag & CRTSCTS)
  889. info->MCR &= ~SER_RTS;
  890. local_irq_save(flags);
  891. rtsdtr_ctrl(info->MCR);
  892. local_irq_restore(flags);
  893. }
  894. static void rs_unthrottle(struct tty_struct * tty)
  895. {
  896. struct async_struct *info = (struct async_struct *)tty->driver_data;
  897. unsigned long flags;
  898. #ifdef SERIAL_DEBUG_THROTTLE
  899. char buf[64];
  900. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  901. tty->ldisc.chars_in_buffer(tty));
  902. #endif
  903. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  904. return;
  905. if (I_IXOFF(tty)) {
  906. if (info->x_char)
  907. info->x_char = 0;
  908. else
  909. rs_send_xchar(tty, START_CHAR(tty));
  910. }
  911. if (tty->termios->c_cflag & CRTSCTS)
  912. info->MCR |= SER_RTS;
  913. local_irq_save(flags);
  914. rtsdtr_ctrl(info->MCR);
  915. local_irq_restore(flags);
  916. }
  917. /*
  918. * ------------------------------------------------------------
  919. * rs_ioctl() and friends
  920. * ------------------------------------------------------------
  921. */
  922. static int get_serial_info(struct async_struct * info,
  923. struct serial_struct * retinfo)
  924. {
  925. struct serial_struct tmp;
  926. struct serial_state *state = info->state;
  927. if (!retinfo)
  928. return -EFAULT;
  929. memset(&tmp, 0, sizeof(tmp));
  930. tmp.type = state->type;
  931. tmp.line = state->line;
  932. tmp.port = state->port;
  933. tmp.irq = state->irq;
  934. tmp.flags = state->flags;
  935. tmp.xmit_fifo_size = state->xmit_fifo_size;
  936. tmp.baud_base = state->baud_base;
  937. tmp.close_delay = state->close_delay;
  938. tmp.closing_wait = state->closing_wait;
  939. tmp.custom_divisor = state->custom_divisor;
  940. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  941. return -EFAULT;
  942. return 0;
  943. }
  944. static int set_serial_info(struct async_struct * info,
  945. struct serial_struct * new_info)
  946. {
  947. struct serial_struct new_serial;
  948. struct serial_state old_state, *state;
  949. unsigned int change_irq,change_port;
  950. int retval = 0;
  951. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  952. return -EFAULT;
  953. state = info->state;
  954. old_state = *state;
  955. change_irq = new_serial.irq != state->irq;
  956. change_port = (new_serial.port != state->port);
  957. if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
  958. return -EINVAL;
  959. if (!serial_isroot()) {
  960. if ((new_serial.baud_base != state->baud_base) ||
  961. (new_serial.close_delay != state->close_delay) ||
  962. (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
  963. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  964. (state->flags & ~ASYNC_USR_MASK)))
  965. return -EPERM;
  966. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  967. (new_serial.flags & ASYNC_USR_MASK));
  968. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  969. (new_serial.flags & ASYNC_USR_MASK));
  970. state->custom_divisor = new_serial.custom_divisor;
  971. goto check_and_exit;
  972. }
  973. if (new_serial.baud_base < 9600)
  974. return -EINVAL;
  975. /*
  976. * OK, past this point, all the error checking has been done.
  977. * At this point, we start making changes.....
  978. */
  979. state->baud_base = new_serial.baud_base;
  980. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  981. (new_serial.flags & ASYNC_FLAGS));
  982. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  983. (info->flags & ASYNC_INTERNAL_FLAGS));
  984. state->custom_divisor = new_serial.custom_divisor;
  985. state->close_delay = new_serial.close_delay * HZ/100;
  986. state->closing_wait = new_serial.closing_wait * HZ/100;
  987. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  988. check_and_exit:
  989. if (info->flags & ASYNC_INITIALIZED) {
  990. if (((old_state.flags & ASYNC_SPD_MASK) !=
  991. (state->flags & ASYNC_SPD_MASK)) ||
  992. (old_state.custom_divisor != state->custom_divisor)) {
  993. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  994. info->tty->alt_speed = 57600;
  995. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  996. info->tty->alt_speed = 115200;
  997. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  998. info->tty->alt_speed = 230400;
  999. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  1000. info->tty->alt_speed = 460800;
  1001. change_speed(info, NULL);
  1002. }
  1003. } else
  1004. retval = startup(info);
  1005. return retval;
  1006. }
  1007. /*
  1008. * get_lsr_info - get line status register info
  1009. *
  1010. * Purpose: Let user call ioctl() to get info when the UART physically
  1011. * is emptied. On bus types like RS485, the transmitter must
  1012. * release the bus after transmitting. This must be done when
  1013. * the transmit shift register is empty, not be done when the
  1014. * transmit holding register is empty. This functionality
  1015. * allows an RS485 driver to be written in user space.
  1016. */
  1017. static int get_lsr_info(struct async_struct * info, unsigned int *value)
  1018. {
  1019. unsigned char status;
  1020. unsigned int result;
  1021. unsigned long flags;
  1022. local_irq_save(flags);
  1023. status = custom.serdatr;
  1024. mb();
  1025. local_irq_restore(flags);
  1026. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  1027. if (copy_to_user(value, &result, sizeof(int)))
  1028. return -EFAULT;
  1029. return 0;
  1030. }
  1031. static int rs_tiocmget(struct tty_struct *tty, struct file *file)
  1032. {
  1033. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1034. unsigned char control, status;
  1035. unsigned long flags;
  1036. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1037. return -ENODEV;
  1038. if (tty->flags & (1 << TTY_IO_ERROR))
  1039. return -EIO;
  1040. control = info->MCR;
  1041. local_irq_save(flags);
  1042. status = ciab.pra;
  1043. local_irq_restore(flags);
  1044. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  1045. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  1046. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  1047. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  1048. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  1049. }
  1050. static int rs_tiocmset(struct tty_struct *tty, struct file *file,
  1051. unsigned int set, unsigned int clear)
  1052. {
  1053. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1054. unsigned long flags;
  1055. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1056. return -ENODEV;
  1057. if (tty->flags & (1 << TTY_IO_ERROR))
  1058. return -EIO;
  1059. local_irq_save(flags);
  1060. if (set & TIOCM_RTS)
  1061. info->MCR |= SER_RTS;
  1062. if (set & TIOCM_DTR)
  1063. info->MCR |= SER_DTR;
  1064. if (clear & TIOCM_RTS)
  1065. info->MCR &= ~SER_RTS;
  1066. if (clear & TIOCM_DTR)
  1067. info->MCR &= ~SER_DTR;
  1068. rtsdtr_ctrl(info->MCR);
  1069. local_irq_restore(flags);
  1070. return 0;
  1071. }
  1072. /*
  1073. * rs_break() --- routine which turns the break handling on or off
  1074. */
  1075. static void rs_break(struct tty_struct *tty, int break_state)
  1076. {
  1077. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1078. unsigned long flags;
  1079. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1080. return;
  1081. local_irq_save(flags);
  1082. if (break_state == -1)
  1083. custom.adkcon = AC_SETCLR | AC_UARTBRK;
  1084. else
  1085. custom.adkcon = AC_UARTBRK;
  1086. mb();
  1087. local_irq_restore(flags);
  1088. }
  1089. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1090. unsigned int cmd, unsigned long arg)
  1091. {
  1092. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1093. struct async_icount cprev, cnow; /* kernel counter temps */
  1094. struct serial_icounter_struct icount;
  1095. unsigned long flags;
  1096. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1097. return -ENODEV;
  1098. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1099. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1100. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1101. if (tty->flags & (1 << TTY_IO_ERROR))
  1102. return -EIO;
  1103. }
  1104. switch (cmd) {
  1105. case TIOCGSERIAL:
  1106. return get_serial_info(info,
  1107. (struct serial_struct *) arg);
  1108. case TIOCSSERIAL:
  1109. return set_serial_info(info,
  1110. (struct serial_struct *) arg);
  1111. case TIOCSERCONFIG:
  1112. return 0;
  1113. case TIOCSERGETLSR: /* Get line status register */
  1114. return get_lsr_info(info, (unsigned int *) arg);
  1115. case TIOCSERGSTRUCT:
  1116. if (copy_to_user((struct async_struct *) arg,
  1117. info, sizeof(struct async_struct)))
  1118. return -EFAULT;
  1119. return 0;
  1120. /*
  1121. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1122. * - mask passed in arg for lines of interest
  1123. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1124. * Caller should use TIOCGICOUNT to see which one it was
  1125. */
  1126. case TIOCMIWAIT:
  1127. local_irq_save(flags);
  1128. /* note the counters on entry */
  1129. cprev = info->state->icount;
  1130. local_irq_restore(flags);
  1131. while (1) {
  1132. interruptible_sleep_on(&info->delta_msr_wait);
  1133. /* see if a signal did it */
  1134. if (signal_pending(current))
  1135. return -ERESTARTSYS;
  1136. local_irq_save(flags);
  1137. cnow = info->state->icount; /* atomic copy */
  1138. local_irq_restore(flags);
  1139. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1140. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1141. return -EIO; /* no change => error */
  1142. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1143. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1144. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1145. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1146. return 0;
  1147. }
  1148. cprev = cnow;
  1149. }
  1150. /* NOTREACHED */
  1151. /*
  1152. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1153. * Return: write counters to the user passed counter struct
  1154. * NB: both 1->0 and 0->1 transitions are counted except for
  1155. * RI where only 0->1 is counted.
  1156. */
  1157. case TIOCGICOUNT:
  1158. local_irq_save(flags);
  1159. cnow = info->state->icount;
  1160. local_irq_restore(flags);
  1161. icount.cts = cnow.cts;
  1162. icount.dsr = cnow.dsr;
  1163. icount.rng = cnow.rng;
  1164. icount.dcd = cnow.dcd;
  1165. icount.rx = cnow.rx;
  1166. icount.tx = cnow.tx;
  1167. icount.frame = cnow.frame;
  1168. icount.overrun = cnow.overrun;
  1169. icount.parity = cnow.parity;
  1170. icount.brk = cnow.brk;
  1171. icount.buf_overrun = cnow.buf_overrun;
  1172. if (copy_to_user((void *)arg, &icount, sizeof(icount)))
  1173. return -EFAULT;
  1174. return 0;
  1175. case TIOCSERGWILD:
  1176. case TIOCSERSWILD:
  1177. /* "setserial -W" is called in Debian boot */
  1178. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1179. return 0;
  1180. default:
  1181. return -ENOIOCTLCMD;
  1182. }
  1183. return 0;
  1184. }
  1185. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1186. {
  1187. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1188. unsigned long flags;
  1189. unsigned int cflag = tty->termios->c_cflag;
  1190. if ( (cflag == old_termios->c_cflag)
  1191. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  1192. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1193. return;
  1194. change_speed(info, old_termios);
  1195. /* Handle transition to B0 status */
  1196. if ((old_termios->c_cflag & CBAUD) &&
  1197. !(cflag & CBAUD)) {
  1198. info->MCR &= ~(SER_DTR|SER_RTS);
  1199. local_irq_save(flags);
  1200. rtsdtr_ctrl(info->MCR);
  1201. local_irq_restore(flags);
  1202. }
  1203. /* Handle transition away from B0 status */
  1204. if (!(old_termios->c_cflag & CBAUD) &&
  1205. (cflag & CBAUD)) {
  1206. info->MCR |= SER_DTR;
  1207. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1208. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1209. info->MCR |= SER_RTS;
  1210. }
  1211. local_irq_save(flags);
  1212. rtsdtr_ctrl(info->MCR);
  1213. local_irq_restore(flags);
  1214. }
  1215. /* Handle turning off CRTSCTS */
  1216. if ((old_termios->c_cflag & CRTSCTS) &&
  1217. !(tty->termios->c_cflag & CRTSCTS)) {
  1218. tty->hw_stopped = 0;
  1219. rs_start(tty);
  1220. }
  1221. #if 0
  1222. /*
  1223. * No need to wake up processes in open wait, since they
  1224. * sample the CLOCAL flag once, and don't recheck it.
  1225. * XXX It's not clear whether the current behavior is correct
  1226. * or not. Hence, this may change.....
  1227. */
  1228. if (!(old_termios->c_cflag & CLOCAL) &&
  1229. (tty->termios->c_cflag & CLOCAL))
  1230. wake_up_interruptible(&info->open_wait);
  1231. #endif
  1232. }
  1233. /*
  1234. * ------------------------------------------------------------
  1235. * rs_close()
  1236. *
  1237. * This routine is called when the serial port gets closed. First, we
  1238. * wait for the last remaining data to be sent. Then, we unlink its
  1239. * async structure from the interrupt chain if necessary, and we free
  1240. * that IRQ if nothing is left in the chain.
  1241. * ------------------------------------------------------------
  1242. */
  1243. static void rs_close(struct tty_struct *tty, struct file * filp)
  1244. {
  1245. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1246. struct serial_state *state;
  1247. unsigned long flags;
  1248. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1249. return;
  1250. state = info->state;
  1251. local_irq_save(flags);
  1252. if (tty_hung_up_p(filp)) {
  1253. DBG_CNT("before DEC-hung");
  1254. local_irq_restore(flags);
  1255. return;
  1256. }
  1257. #ifdef SERIAL_DEBUG_OPEN
  1258. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1259. #endif
  1260. if ((tty->count == 1) && (state->count != 1)) {
  1261. /*
  1262. * Uh, oh. tty->count is 1, which means that the tty
  1263. * structure will be freed. state->count should always
  1264. * be one in these conditions. If it's greater than
  1265. * one, we've got real problems, since it means the
  1266. * serial port won't be shutdown.
  1267. */
  1268. printk("rs_close: bad serial port count; tty->count is 1, "
  1269. "state->count is %d\n", state->count);
  1270. state->count = 1;
  1271. }
  1272. if (--state->count < 0) {
  1273. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1274. info->line, state->count);
  1275. state->count = 0;
  1276. }
  1277. if (state->count) {
  1278. DBG_CNT("before DEC-2");
  1279. local_irq_restore(flags);
  1280. return;
  1281. }
  1282. info->flags |= ASYNC_CLOSING;
  1283. /*
  1284. * Now we wait for the transmit buffer to clear; and we notify
  1285. * the line discipline to only process XON/XOFF characters.
  1286. */
  1287. tty->closing = 1;
  1288. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1289. tty_wait_until_sent(tty, info->closing_wait);
  1290. /*
  1291. * At this point we stop accepting input. To do this, we
  1292. * disable the receive line status interrupts, and tell the
  1293. * interrupt driver to stop checking the data ready bit in the
  1294. * line status register.
  1295. */
  1296. info->read_status_mask &= ~UART_LSR_DR;
  1297. if (info->flags & ASYNC_INITIALIZED) {
  1298. /* disable receive interrupts */
  1299. custom.intena = IF_RBF;
  1300. mb();
  1301. /* clear any pending receive interrupt */
  1302. custom.intreq = IF_RBF;
  1303. mb();
  1304. /*
  1305. * Before we drop DTR, make sure the UART transmitter
  1306. * has completely drained; this is especially
  1307. * important if there is a transmit FIFO!
  1308. */
  1309. rs_wait_until_sent(tty, info->timeout);
  1310. }
  1311. shutdown(info);
  1312. if (tty->driver->flush_buffer)
  1313. tty->driver->flush_buffer(tty);
  1314. tty_ldisc_flush(tty);
  1315. tty->closing = 0;
  1316. info->event = 0;
  1317. info->tty = NULL;
  1318. if (info->blocked_open) {
  1319. if (info->close_delay) {
  1320. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1321. }
  1322. wake_up_interruptible(&info->open_wait);
  1323. }
  1324. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1325. wake_up_interruptible(&info->close_wait);
  1326. local_irq_restore(flags);
  1327. }
  1328. /*
  1329. * rs_wait_until_sent() --- wait until the transmitter is empty
  1330. */
  1331. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1332. {
  1333. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1334. unsigned long orig_jiffies, char_time;
  1335. int lsr;
  1336. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1337. return;
  1338. if (info->xmit_fifo_size == 0)
  1339. return; /* Just in case.... */
  1340. orig_jiffies = jiffies;
  1341. /*
  1342. * Set the check interval to be 1/5 of the estimated time to
  1343. * send a single character, and make it at least 1. The check
  1344. * interval should also be less than the timeout.
  1345. *
  1346. * Note: we have to use pretty tight timings here to satisfy
  1347. * the NIST-PCTS.
  1348. */
  1349. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1350. char_time = char_time / 5;
  1351. if (char_time == 0)
  1352. char_time = 1;
  1353. if (timeout)
  1354. char_time = min_t(unsigned long, char_time, timeout);
  1355. /*
  1356. * If the transmitter hasn't cleared in twice the approximate
  1357. * amount of time to send the entire FIFO, it probably won't
  1358. * ever clear. This assumes the UART isn't doing flow
  1359. * control, which is currently the case. Hence, if it ever
  1360. * takes longer than info->timeout, this is probably due to a
  1361. * UART bug of some kind. So, we clamp the timeout parameter at
  1362. * 2*info->timeout.
  1363. */
  1364. if (!timeout || timeout > 2*info->timeout)
  1365. timeout = 2*info->timeout;
  1366. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1367. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1368. printk("jiff=%lu...", jiffies);
  1369. #endif
  1370. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1371. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1372. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1373. #endif
  1374. msleep_interruptible(jiffies_to_msecs(char_time));
  1375. if (signal_pending(current))
  1376. break;
  1377. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1378. break;
  1379. }
  1380. current->state = TASK_RUNNING;
  1381. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1382. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1383. #endif
  1384. }
  1385. /*
  1386. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1387. */
  1388. static void rs_hangup(struct tty_struct *tty)
  1389. {
  1390. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1391. struct serial_state *state = info->state;
  1392. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1393. return;
  1394. state = info->state;
  1395. rs_flush_buffer(tty);
  1396. shutdown(info);
  1397. info->event = 0;
  1398. state->count = 0;
  1399. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1400. info->tty = NULL;
  1401. wake_up_interruptible(&info->open_wait);
  1402. }
  1403. /*
  1404. * ------------------------------------------------------------
  1405. * rs_open() and friends
  1406. * ------------------------------------------------------------
  1407. */
  1408. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1409. struct async_struct *info)
  1410. {
  1411. #ifdef DECLARE_WAITQUEUE
  1412. DECLARE_WAITQUEUE(wait, current);
  1413. #else
  1414. struct wait_queue wait = { current, NULL };
  1415. #endif
  1416. struct serial_state *state = info->state;
  1417. int retval;
  1418. int do_clocal = 0, extra_count = 0;
  1419. unsigned long flags;
  1420. /*
  1421. * If the device is in the middle of being closed, then block
  1422. * until it's done, and then try again.
  1423. */
  1424. if (tty_hung_up_p(filp) ||
  1425. (info->flags & ASYNC_CLOSING)) {
  1426. if (info->flags & ASYNC_CLOSING)
  1427. interruptible_sleep_on(&info->close_wait);
  1428. #ifdef SERIAL_DO_RESTART
  1429. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1430. -EAGAIN : -ERESTARTSYS);
  1431. #else
  1432. return -EAGAIN;
  1433. #endif
  1434. }
  1435. /*
  1436. * If non-blocking mode is set, or the port is not enabled,
  1437. * then make the check up front and then exit.
  1438. */
  1439. if ((filp->f_flags & O_NONBLOCK) ||
  1440. (tty->flags & (1 << TTY_IO_ERROR))) {
  1441. info->flags |= ASYNC_NORMAL_ACTIVE;
  1442. return 0;
  1443. }
  1444. if (tty->termios->c_cflag & CLOCAL)
  1445. do_clocal = 1;
  1446. /*
  1447. * Block waiting for the carrier detect and the line to become
  1448. * free (i.e., not in use by the callout). While we are in
  1449. * this loop, state->count is dropped by one, so that
  1450. * rs_close() knows when to free things. We restore it upon
  1451. * exit, either normal or abnormal.
  1452. */
  1453. retval = 0;
  1454. add_wait_queue(&info->open_wait, &wait);
  1455. #ifdef SERIAL_DEBUG_OPEN
  1456. printk("block_til_ready before block: ttys%d, count = %d\n",
  1457. state->line, state->count);
  1458. #endif
  1459. local_irq_save(flags);
  1460. if (!tty_hung_up_p(filp)) {
  1461. extra_count = 1;
  1462. state->count--;
  1463. }
  1464. local_irq_restore(flags);
  1465. info->blocked_open++;
  1466. while (1) {
  1467. local_irq_save(flags);
  1468. if (tty->termios->c_cflag & CBAUD)
  1469. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1470. local_irq_restore(flags);
  1471. set_current_state(TASK_INTERRUPTIBLE);
  1472. if (tty_hung_up_p(filp) ||
  1473. !(info->flags & ASYNC_INITIALIZED)) {
  1474. #ifdef SERIAL_DO_RESTART
  1475. if (info->flags & ASYNC_HUP_NOTIFY)
  1476. retval = -EAGAIN;
  1477. else
  1478. retval = -ERESTARTSYS;
  1479. #else
  1480. retval = -EAGAIN;
  1481. #endif
  1482. break;
  1483. }
  1484. if (!(info->flags & ASYNC_CLOSING) &&
  1485. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1486. break;
  1487. if (signal_pending(current)) {
  1488. retval = -ERESTARTSYS;
  1489. break;
  1490. }
  1491. #ifdef SERIAL_DEBUG_OPEN
  1492. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1493. info->line, state->count);
  1494. #endif
  1495. schedule();
  1496. }
  1497. current->state = TASK_RUNNING;
  1498. remove_wait_queue(&info->open_wait, &wait);
  1499. if (extra_count)
  1500. state->count++;
  1501. info->blocked_open--;
  1502. #ifdef SERIAL_DEBUG_OPEN
  1503. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1504. info->line, state->count);
  1505. #endif
  1506. if (retval)
  1507. return retval;
  1508. info->flags |= ASYNC_NORMAL_ACTIVE;
  1509. return 0;
  1510. }
  1511. static int get_async_struct(int line, struct async_struct **ret_info)
  1512. {
  1513. struct async_struct *info;
  1514. struct serial_state *sstate;
  1515. sstate = rs_table + line;
  1516. sstate->count++;
  1517. if (sstate->info) {
  1518. *ret_info = sstate->info;
  1519. return 0;
  1520. }
  1521. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  1522. if (!info) {
  1523. sstate->count--;
  1524. return -ENOMEM;
  1525. }
  1526. memset(info, 0, sizeof(struct async_struct));
  1527. #ifdef DECLARE_WAITQUEUE
  1528. init_waitqueue_head(&info->open_wait);
  1529. init_waitqueue_head(&info->close_wait);
  1530. init_waitqueue_head(&info->delta_msr_wait);
  1531. #endif
  1532. info->magic = SERIAL_MAGIC;
  1533. info->port = sstate->port;
  1534. info->flags = sstate->flags;
  1535. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1536. info->line = line;
  1537. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1538. info->state = sstate;
  1539. if (sstate->info) {
  1540. kfree(info);
  1541. *ret_info = sstate->info;
  1542. return 0;
  1543. }
  1544. *ret_info = sstate->info = info;
  1545. return 0;
  1546. }
  1547. /*
  1548. * This routine is called whenever a serial port is opened. It
  1549. * enables interrupts for a serial port, linking in its async structure into
  1550. * the IRQ chain. It also performs the serial-specific
  1551. * initialization for the tty structure.
  1552. */
  1553. static int rs_open(struct tty_struct *tty, struct file * filp)
  1554. {
  1555. struct async_struct *info;
  1556. int retval, line;
  1557. unsigned long page;
  1558. line = tty->index;
  1559. if ((line < 0) || (line >= NR_PORTS)) {
  1560. return -ENODEV;
  1561. }
  1562. retval = get_async_struct(line, &info);
  1563. if (retval) {
  1564. return retval;
  1565. }
  1566. tty->driver_data = info;
  1567. info->tty = tty;
  1568. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1569. return -ENODEV;
  1570. #ifdef SERIAL_DEBUG_OPEN
  1571. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1572. #endif
  1573. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1574. if (!tmp_buf) {
  1575. page = get_zeroed_page(GFP_KERNEL);
  1576. if (!page) {
  1577. return -ENOMEM;
  1578. }
  1579. if (tmp_buf)
  1580. free_page(page);
  1581. else
  1582. tmp_buf = (unsigned char *) page;
  1583. }
  1584. /*
  1585. * If the port is the middle of closing, bail out now
  1586. */
  1587. if (tty_hung_up_p(filp) ||
  1588. (info->flags & ASYNC_CLOSING)) {
  1589. if (info->flags & ASYNC_CLOSING)
  1590. interruptible_sleep_on(&info->close_wait);
  1591. #ifdef SERIAL_DO_RESTART
  1592. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1593. -EAGAIN : -ERESTARTSYS);
  1594. #else
  1595. return -EAGAIN;
  1596. #endif
  1597. }
  1598. /*
  1599. * Start up serial port
  1600. */
  1601. retval = startup(info);
  1602. if (retval) {
  1603. return retval;
  1604. }
  1605. retval = block_til_ready(tty, filp, info);
  1606. if (retval) {
  1607. #ifdef SERIAL_DEBUG_OPEN
  1608. printk("rs_open returning after block_til_ready with %d\n",
  1609. retval);
  1610. #endif
  1611. return retval;
  1612. }
  1613. #ifdef SERIAL_DEBUG_OPEN
  1614. printk("rs_open %s successful...", tty->name);
  1615. #endif
  1616. return 0;
  1617. }
  1618. /*
  1619. * /proc fs routines....
  1620. */
  1621. static inline int line_info(char *buf, struct serial_state *state)
  1622. {
  1623. struct async_struct *info = state->info, scr_info;
  1624. char stat_buf[30], control, status;
  1625. int ret;
  1626. unsigned long flags;
  1627. ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
  1628. /*
  1629. * Figure out the current RS-232 lines
  1630. */
  1631. if (!info) {
  1632. info = &scr_info; /* This is just for serial_{in,out} */
  1633. info->magic = SERIAL_MAGIC;
  1634. info->flags = state->flags;
  1635. info->quot = 0;
  1636. info->tty = NULL;
  1637. }
  1638. local_irq_save(flags);
  1639. status = ciab.pra;
  1640. control = info ? info->MCR : status;
  1641. local_irq_restore(flags);
  1642. stat_buf[0] = 0;
  1643. stat_buf[1] = 0;
  1644. if(!(control & SER_RTS))
  1645. strcat(stat_buf, "|RTS");
  1646. if(!(status & SER_CTS))
  1647. strcat(stat_buf, "|CTS");
  1648. if(!(control & SER_DTR))
  1649. strcat(stat_buf, "|DTR");
  1650. if(!(status & SER_DSR))
  1651. strcat(stat_buf, "|DSR");
  1652. if(!(status & SER_DCD))
  1653. strcat(stat_buf, "|CD");
  1654. if (info->quot) {
  1655. ret += sprintf(buf+ret, " baud:%d",
  1656. state->baud_base / info->quot);
  1657. }
  1658. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1659. state->icount.tx, state->icount.rx);
  1660. if (state->icount.frame)
  1661. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1662. if (state->icount.parity)
  1663. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1664. if (state->icount.brk)
  1665. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1666. if (state->icount.overrun)
  1667. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1668. /*
  1669. * Last thing is the RS-232 status lines
  1670. */
  1671. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1672. return ret;
  1673. }
  1674. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1675. int *eof, void *data)
  1676. {
  1677. int len = 0, l;
  1678. off_t begin = 0;
  1679. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  1680. l = line_info(page + len, &rs_table[0]);
  1681. len += l;
  1682. if (len+begin > off+count)
  1683. goto done;
  1684. if (len+begin < off) {
  1685. begin += len;
  1686. len = 0;
  1687. }
  1688. *eof = 1;
  1689. done:
  1690. if (off >= len+begin)
  1691. return 0;
  1692. *start = page + (off-begin);
  1693. return ((count < begin+len-off) ? count : begin+len-off);
  1694. }
  1695. /*
  1696. * ---------------------------------------------------------------------
  1697. * rs_init() and friends
  1698. *
  1699. * rs_init() is called at boot-time to initialize the serial driver.
  1700. * ---------------------------------------------------------------------
  1701. */
  1702. /*
  1703. * This routine prints out the appropriate serial driver version
  1704. * number, and identifies which options were configured into this
  1705. * driver.
  1706. */
  1707. static _INLINE_ void show_serial_version(void)
  1708. {
  1709. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1710. }
  1711. static struct tty_operations serial_ops = {
  1712. .open = rs_open,
  1713. .close = rs_close,
  1714. .write = rs_write,
  1715. .put_char = rs_put_char,
  1716. .flush_chars = rs_flush_chars,
  1717. .write_room = rs_write_room,
  1718. .chars_in_buffer = rs_chars_in_buffer,
  1719. .flush_buffer = rs_flush_buffer,
  1720. .ioctl = rs_ioctl,
  1721. .throttle = rs_throttle,
  1722. .unthrottle = rs_unthrottle,
  1723. .set_termios = rs_set_termios,
  1724. .stop = rs_stop,
  1725. .start = rs_start,
  1726. .hangup = rs_hangup,
  1727. .break_ctl = rs_break,
  1728. .send_xchar = rs_send_xchar,
  1729. .wait_until_sent = rs_wait_until_sent,
  1730. .read_proc = rs_read_proc,
  1731. .tiocmget = rs_tiocmget,
  1732. .tiocmset = rs_tiocmset,
  1733. };
  1734. /*
  1735. * The serial driver boot-time initialization code!
  1736. */
  1737. static int __init rs_init(void)
  1738. {
  1739. unsigned long flags;
  1740. struct serial_state * state;
  1741. if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
  1742. return -ENODEV;
  1743. serial_driver = alloc_tty_driver(1);
  1744. if (!serial_driver)
  1745. return -ENOMEM;
  1746. /*
  1747. * We request SERDAT and SERPER only, because the serial registers are
  1748. * too spreaded over the custom register space
  1749. */
  1750. if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
  1751. return -EBUSY;
  1752. IRQ_ports = NULL;
  1753. show_serial_version();
  1754. /* Initialize the tty_driver structure */
  1755. serial_driver->owner = THIS_MODULE;
  1756. serial_driver->driver_name = "amiserial";
  1757. serial_driver->name = "ttyS";
  1758. serial_driver->major = TTY_MAJOR;
  1759. serial_driver->minor_start = 64;
  1760. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1761. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1762. serial_driver->init_termios = tty_std_termios;
  1763. serial_driver->init_termios.c_cflag =
  1764. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1765. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1766. tty_set_operations(serial_driver, &serial_ops);
  1767. if (tty_register_driver(serial_driver))
  1768. panic("Couldn't register serial driver\n");
  1769. state = rs_table;
  1770. state->magic = SSTATE_MAGIC;
  1771. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1772. state->line = 0;
  1773. state->custom_divisor = 0;
  1774. state->close_delay = 5*HZ/10;
  1775. state->closing_wait = 30*HZ;
  1776. state->icount.cts = state->icount.dsr =
  1777. state->icount.rng = state->icount.dcd = 0;
  1778. state->icount.rx = state->icount.tx = 0;
  1779. state->icount.frame = state->icount.parity = 0;
  1780. state->icount.overrun = state->icount.brk = 0;
  1781. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1782. state->line);
  1783. /* Hardware set up */
  1784. state->baud_base = amiga_colorclock;
  1785. state->xmit_fifo_size = 1;
  1786. local_irq_save(flags);
  1787. /* set ISRs, and then disable the rx interrupts */
  1788. request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1789. request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
  1790. /* turn off Rx and Tx interrupts */
  1791. custom.intena = IF_RBF | IF_TBE;
  1792. mb();
  1793. /* clear any pending interrupt */
  1794. custom.intreq = IF_RBF | IF_TBE;
  1795. mb();
  1796. local_irq_restore(flags);
  1797. /*
  1798. * set the appropriate directions for the modem control flags,
  1799. * and clear RTS and DTR
  1800. */
  1801. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1802. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1803. return 0;
  1804. }
  1805. static __exit void rs_exit(void)
  1806. {
  1807. int error;
  1808. struct async_struct *info = rs_table[0].info;
  1809. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1810. tasklet_kill(&info->tlet);
  1811. if ((error = tty_unregister_driver(serial_driver)))
  1812. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1813. error);
  1814. put_tty_driver(serial_driver);
  1815. if (info) {
  1816. rs_table[0].info = NULL;
  1817. kfree(info);
  1818. }
  1819. if (tmp_buf) {
  1820. free_page((unsigned long) tmp_buf);
  1821. tmp_buf = NULL;
  1822. }
  1823. release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
  1824. }
  1825. module_init(rs_init)
  1826. module_exit(rs_exit)
  1827. /*
  1828. * ------------------------------------------------------------
  1829. * Serial console driver
  1830. * ------------------------------------------------------------
  1831. */
  1832. #ifdef CONFIG_SERIAL_CONSOLE
  1833. static void amiga_serial_putc(char c)
  1834. {
  1835. custom.serdat = (unsigned char)c | 0x100;
  1836. while (!(custom.serdatr & 0x2000))
  1837. barrier();
  1838. }
  1839. /*
  1840. * Print a string to the serial port trying not to disturb
  1841. * any possible real use of the port...
  1842. *
  1843. * The console must be locked when we get here.
  1844. */
  1845. static void serial_console_write(struct console *co, const char *s,
  1846. unsigned count)
  1847. {
  1848. unsigned short intena = custom.intenar;
  1849. custom.intena = IF_TBE;
  1850. while (count--) {
  1851. if (*s == '\n')
  1852. amiga_serial_putc('\r');
  1853. amiga_serial_putc(*s++);
  1854. }
  1855. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1856. }
  1857. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1858. {
  1859. *index = 0;
  1860. return serial_driver;
  1861. }
  1862. static struct console sercons = {
  1863. .name = "ttyS",
  1864. .write = serial_console_write,
  1865. .device = serial_console_device,
  1866. .flags = CON_PRINTBUFFER,
  1867. .index = -1,
  1868. };
  1869. /*
  1870. * Register console.
  1871. */
  1872. static int __init amiserial_console_init(void)
  1873. {
  1874. register_console(&sercons);
  1875. return 0;
  1876. }
  1877. console_initcall(amiserial_console_init);
  1878. #endif
  1879. MODULE_LICENSE("GPL");