amiserial.c 53 KB

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