amiserial.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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 ktermios *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. }
  458. /*
  459. * ---------------------------------------------------------------
  460. * Low level utility subroutines for the serial driver: routines to
  461. * figure out the appropriate timeout for an interrupt chain, routines
  462. * to initialize and startup a serial port, and routines to shutdown a
  463. * serial port. Useful stuff like that.
  464. * ---------------------------------------------------------------
  465. */
  466. static int startup(struct async_struct * info)
  467. {
  468. unsigned long flags;
  469. int retval=0;
  470. unsigned long page;
  471. page = get_zeroed_page(GFP_KERNEL);
  472. if (!page)
  473. return -ENOMEM;
  474. local_irq_save(flags);
  475. if (info->flags & ASYNC_INITIALIZED) {
  476. free_page(page);
  477. goto errout;
  478. }
  479. if (info->xmit.buf)
  480. free_page(page);
  481. else
  482. info->xmit.buf = (unsigned char *) page;
  483. #ifdef SERIAL_DEBUG_OPEN
  484. printk("starting up ttys%d ...", info->line);
  485. #endif
  486. /* Clear anything in the input buffer */
  487. custom.intreq = IF_RBF;
  488. mb();
  489. retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
  490. if (retval) {
  491. if (serial_isroot()) {
  492. if (info->tty)
  493. set_bit(TTY_IO_ERROR,
  494. &info->tty->flags);
  495. retval = 0;
  496. }
  497. goto errout;
  498. }
  499. /* enable both Rx and Tx interrupts */
  500. custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
  501. mb();
  502. info->IER = UART_IER_MSI;
  503. /* remember current state of the DCD and CTS bits */
  504. current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  505. IRQ_ports = info;
  506. info->MCR = 0;
  507. if (info->tty->termios->c_cflag & CBAUD)
  508. info->MCR = SER_DTR | SER_RTS;
  509. rtsdtr_ctrl(info->MCR);
  510. if (info->tty)
  511. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  512. info->xmit.head = info->xmit.tail = 0;
  513. /*
  514. * Set up the tty->alt_speed kludge
  515. */
  516. if (info->tty) {
  517. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  518. info->tty->alt_speed = 57600;
  519. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  520. info->tty->alt_speed = 115200;
  521. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  522. info->tty->alt_speed = 230400;
  523. if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  524. info->tty->alt_speed = 460800;
  525. }
  526. /*
  527. * and set the speed of the serial port
  528. */
  529. change_speed(info, NULL);
  530. info->flags |= ASYNC_INITIALIZED;
  531. local_irq_restore(flags);
  532. return 0;
  533. errout:
  534. local_irq_restore(flags);
  535. return retval;
  536. }
  537. /*
  538. * This routine will shutdown a serial port; interrupts are disabled, and
  539. * DTR is dropped if the hangup on close termio flag is on.
  540. */
  541. static void shutdown(struct async_struct * info)
  542. {
  543. unsigned long flags;
  544. struct serial_state *state;
  545. if (!(info->flags & ASYNC_INITIALIZED))
  546. return;
  547. state = info->state;
  548. #ifdef SERIAL_DEBUG_OPEN
  549. printk("Shutting down serial port %d ....\n", info->line);
  550. #endif
  551. local_irq_save(flags); /* Disable interrupts */
  552. /*
  553. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  554. * here so the queue might never be waken up
  555. */
  556. wake_up_interruptible(&info->delta_msr_wait);
  557. IRQ_ports = NULL;
  558. /*
  559. * Free the IRQ, if necessary
  560. */
  561. free_irq(IRQ_AMIGA_VERTB, info);
  562. if (info->xmit.buf) {
  563. free_page((unsigned long) info->xmit.buf);
  564. info->xmit.buf = NULL;
  565. }
  566. info->IER = 0;
  567. custom.intena = IF_RBF | IF_TBE;
  568. mb();
  569. /* disable break condition */
  570. custom.adkcon = AC_UARTBRK;
  571. mb();
  572. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  573. info->MCR &= ~(SER_DTR|SER_RTS);
  574. rtsdtr_ctrl(info->MCR);
  575. if (info->tty)
  576. set_bit(TTY_IO_ERROR, &info->tty->flags);
  577. info->flags &= ~ASYNC_INITIALIZED;
  578. local_irq_restore(flags);
  579. }
  580. /*
  581. * This routine is called to set the UART divisor registers to match
  582. * the specified baud rate for a serial port.
  583. */
  584. static void change_speed(struct async_struct *info,
  585. struct ktermios *old_termios)
  586. {
  587. int quot = 0, baud_base, baud;
  588. unsigned cflag, cval = 0;
  589. int bits;
  590. unsigned long flags;
  591. if (!info->tty || !info->tty->termios)
  592. return;
  593. cflag = info->tty->termios->c_cflag;
  594. /* Byte size is always 8 bits plus parity bit if requested */
  595. cval = 3; bits = 10;
  596. if (cflag & CSTOPB) {
  597. cval |= 0x04;
  598. bits++;
  599. }
  600. if (cflag & PARENB) {
  601. cval |= UART_LCR_PARITY;
  602. bits++;
  603. }
  604. if (!(cflag & PARODD))
  605. cval |= UART_LCR_EPAR;
  606. #ifdef CMSPAR
  607. if (cflag & CMSPAR)
  608. cval |= UART_LCR_SPAR;
  609. #endif
  610. /* Determine divisor based on baud rate */
  611. baud = tty_get_baud_rate(info->tty);
  612. if (!baud)
  613. baud = 9600; /* B0 transition handled in rs_set_termios */
  614. baud_base = info->state->baud_base;
  615. if (baud == 38400 &&
  616. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  617. quot = info->state->custom_divisor;
  618. else {
  619. if (baud == 134)
  620. /* Special case since 134 is really 134.5 */
  621. quot = (2*baud_base / 269);
  622. else if (baud)
  623. quot = baud_base / baud;
  624. }
  625. /* If the quotient is zero refuse the change */
  626. if (!quot && old_termios) {
  627. /* FIXME: Will need updating for new tty in the end */
  628. info->tty->termios->c_cflag &= ~CBAUD;
  629. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  630. baud = tty_get_baud_rate(info->tty);
  631. if (!baud)
  632. baud = 9600;
  633. if (baud == 38400 &&
  634. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  635. quot = info->state->custom_divisor;
  636. else {
  637. if (baud == 134)
  638. /* Special case since 134 is really 134.5 */
  639. quot = (2*baud_base / 269);
  640. else if (baud)
  641. quot = baud_base / baud;
  642. }
  643. }
  644. /* As a last resort, if the quotient is zero, default to 9600 bps */
  645. if (!quot)
  646. quot = baud_base / 9600;
  647. info->quot = quot;
  648. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  649. info->timeout += HZ/50; /* Add .02 seconds of slop */
  650. /* CTS flow control flag and modem status interrupts */
  651. info->IER &= ~UART_IER_MSI;
  652. if (info->flags & ASYNC_HARDPPS_CD)
  653. info->IER |= UART_IER_MSI;
  654. if (cflag & CRTSCTS) {
  655. info->flags |= ASYNC_CTS_FLOW;
  656. info->IER |= UART_IER_MSI;
  657. } else
  658. info->flags &= ~ASYNC_CTS_FLOW;
  659. if (cflag & CLOCAL)
  660. info->flags &= ~ASYNC_CHECK_CD;
  661. else {
  662. info->flags |= ASYNC_CHECK_CD;
  663. info->IER |= UART_IER_MSI;
  664. }
  665. /* TBD:
  666. * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
  667. */
  668. /*
  669. * Set up parity check flag
  670. */
  671. info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
  672. if (I_INPCK(info->tty))
  673. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  674. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  675. info->read_status_mask |= UART_LSR_BI;
  676. /*
  677. * Characters to ignore
  678. */
  679. info->ignore_status_mask = 0;
  680. if (I_IGNPAR(info->tty))
  681. info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  682. if (I_IGNBRK(info->tty)) {
  683. info->ignore_status_mask |= UART_LSR_BI;
  684. /*
  685. * If we're ignore parity and break indicators, ignore
  686. * overruns too. (For real raw support).
  687. */
  688. if (I_IGNPAR(info->tty))
  689. info->ignore_status_mask |= UART_LSR_OE;
  690. }
  691. /*
  692. * !!! ignore all characters if CREAD is not set
  693. */
  694. if ((cflag & CREAD) == 0)
  695. info->ignore_status_mask |= UART_LSR_DR;
  696. local_irq_save(flags);
  697. {
  698. short serper;
  699. /* Set up the baud rate */
  700. serper = quot - 1;
  701. /* Enable or disable parity bit */
  702. if(cval & UART_LCR_PARITY)
  703. serper |= (SERPER_PARENB);
  704. custom.serper = serper;
  705. mb();
  706. }
  707. info->LCR = cval; /* Save LCR */
  708. local_irq_restore(flags);
  709. }
  710. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  711. {
  712. struct async_struct *info;
  713. unsigned long flags;
  714. if (!tty)
  715. return 0;
  716. info = tty->driver_data;
  717. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  718. return 0;
  719. if (!info->xmit.buf)
  720. return 0;
  721. local_irq_save(flags);
  722. if (CIRC_SPACE(info->xmit.head,
  723. info->xmit.tail,
  724. SERIAL_XMIT_SIZE) == 0) {
  725. local_irq_restore(flags);
  726. return 0;
  727. }
  728. info->xmit.buf[info->xmit.head++] = ch;
  729. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  730. local_irq_restore(flags);
  731. return 1;
  732. }
  733. static void rs_flush_chars(struct tty_struct *tty)
  734. {
  735. struct async_struct *info = (struct async_struct *)tty->driver_data;
  736. unsigned long flags;
  737. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  738. return;
  739. if (info->xmit.head == info->xmit.tail
  740. || tty->stopped
  741. || tty->hw_stopped
  742. || !info->xmit.buf)
  743. return;
  744. local_irq_save(flags);
  745. info->IER |= UART_IER_THRI;
  746. custom.intena = IF_SETCLR | IF_TBE;
  747. mb();
  748. /* set a pending Tx Interrupt, transmitter should restart now */
  749. custom.intreq = IF_SETCLR | IF_TBE;
  750. mb();
  751. local_irq_restore(flags);
  752. }
  753. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  754. {
  755. int c, ret = 0;
  756. struct async_struct *info;
  757. unsigned long flags;
  758. if (!tty)
  759. return 0;
  760. info = tty->driver_data;
  761. if (serial_paranoia_check(info, tty->name, "rs_write"))
  762. return 0;
  763. if (!info->xmit.buf)
  764. return 0;
  765. local_irq_save(flags);
  766. while (1) {
  767. c = CIRC_SPACE_TO_END(info->xmit.head,
  768. info->xmit.tail,
  769. SERIAL_XMIT_SIZE);
  770. if (count < c)
  771. c = count;
  772. if (c <= 0) {
  773. break;
  774. }
  775. memcpy(info->xmit.buf + info->xmit.head, buf, c);
  776. info->xmit.head = ((info->xmit.head + c) &
  777. (SERIAL_XMIT_SIZE-1));
  778. buf += c;
  779. count -= c;
  780. ret += c;
  781. }
  782. local_irq_restore(flags);
  783. if (info->xmit.head != info->xmit.tail
  784. && !tty->stopped
  785. && !tty->hw_stopped
  786. && !(info->IER & UART_IER_THRI)) {
  787. info->IER |= UART_IER_THRI;
  788. local_irq_disable();
  789. custom.intena = IF_SETCLR | IF_TBE;
  790. mb();
  791. /* set a pending Tx Interrupt, transmitter should restart now */
  792. custom.intreq = IF_SETCLR | IF_TBE;
  793. mb();
  794. local_irq_restore(flags);
  795. }
  796. return ret;
  797. }
  798. static int rs_write_room(struct tty_struct *tty)
  799. {
  800. struct async_struct *info = (struct async_struct *)tty->driver_data;
  801. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  802. return 0;
  803. return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  804. }
  805. static int rs_chars_in_buffer(struct tty_struct *tty)
  806. {
  807. struct async_struct *info = (struct async_struct *)tty->driver_data;
  808. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  809. return 0;
  810. return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
  811. }
  812. static void rs_flush_buffer(struct tty_struct *tty)
  813. {
  814. struct async_struct *info = (struct async_struct *)tty->driver_data;
  815. unsigned long flags;
  816. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  817. return;
  818. local_irq_save(flags);
  819. info->xmit.head = info->xmit.tail = 0;
  820. local_irq_restore(flags);
  821. tty_wakeup(tty);
  822. }
  823. /*
  824. * This function is used to send a high-priority XON/XOFF character to
  825. * the device
  826. */
  827. static void rs_send_xchar(struct tty_struct *tty, char ch)
  828. {
  829. struct async_struct *info = (struct async_struct *)tty->driver_data;
  830. unsigned long flags;
  831. if (serial_paranoia_check(info, tty->name, "rs_send_char"))
  832. return;
  833. info->x_char = ch;
  834. if (ch) {
  835. /* Make sure transmit interrupts are on */
  836. /* Check this ! */
  837. local_irq_save(flags);
  838. if(!(custom.intenar & IF_TBE)) {
  839. custom.intena = IF_SETCLR | IF_TBE;
  840. mb();
  841. /* set a pending Tx Interrupt, transmitter should restart now */
  842. custom.intreq = IF_SETCLR | IF_TBE;
  843. mb();
  844. }
  845. local_irq_restore(flags);
  846. info->IER |= UART_IER_THRI;
  847. }
  848. }
  849. /*
  850. * ------------------------------------------------------------
  851. * rs_throttle()
  852. *
  853. * This routine is called by the upper-layer tty layer to signal that
  854. * incoming characters should be throttled.
  855. * ------------------------------------------------------------
  856. */
  857. static void rs_throttle(struct tty_struct * tty)
  858. {
  859. struct async_struct *info = (struct async_struct *)tty->driver_data;
  860. unsigned long flags;
  861. #ifdef SERIAL_DEBUG_THROTTLE
  862. char buf[64];
  863. printk("throttle %s: %d....\n", tty_name(tty, buf),
  864. tty->ldisc.chars_in_buffer(tty));
  865. #endif
  866. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  867. return;
  868. if (I_IXOFF(tty))
  869. rs_send_xchar(tty, STOP_CHAR(tty));
  870. if (tty->termios->c_cflag & CRTSCTS)
  871. info->MCR &= ~SER_RTS;
  872. local_irq_save(flags);
  873. rtsdtr_ctrl(info->MCR);
  874. local_irq_restore(flags);
  875. }
  876. static void rs_unthrottle(struct tty_struct * tty)
  877. {
  878. struct async_struct *info = (struct async_struct *)tty->driver_data;
  879. unsigned long flags;
  880. #ifdef SERIAL_DEBUG_THROTTLE
  881. char buf[64];
  882. printk("unthrottle %s: %d....\n", tty_name(tty, buf),
  883. tty->ldisc.chars_in_buffer(tty));
  884. #endif
  885. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  886. return;
  887. if (I_IXOFF(tty)) {
  888. if (info->x_char)
  889. info->x_char = 0;
  890. else
  891. rs_send_xchar(tty, START_CHAR(tty));
  892. }
  893. if (tty->termios->c_cflag & CRTSCTS)
  894. info->MCR |= SER_RTS;
  895. local_irq_save(flags);
  896. rtsdtr_ctrl(info->MCR);
  897. local_irq_restore(flags);
  898. }
  899. /*
  900. * ------------------------------------------------------------
  901. * rs_ioctl() and friends
  902. * ------------------------------------------------------------
  903. */
  904. static int get_serial_info(struct async_struct * info,
  905. struct serial_struct __user * retinfo)
  906. {
  907. struct serial_struct tmp;
  908. struct serial_state *state = info->state;
  909. if (!retinfo)
  910. return -EFAULT;
  911. memset(&tmp, 0, sizeof(tmp));
  912. lock_kernel();
  913. tmp.type = state->type;
  914. tmp.line = state->line;
  915. tmp.port = state->port;
  916. tmp.irq = state->irq;
  917. tmp.flags = state->flags;
  918. tmp.xmit_fifo_size = state->xmit_fifo_size;
  919. tmp.baud_base = state->baud_base;
  920. tmp.close_delay = state->close_delay;
  921. tmp.closing_wait = state->closing_wait;
  922. tmp.custom_divisor = state->custom_divisor;
  923. unlock_kernel();
  924. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  925. return -EFAULT;
  926. return 0;
  927. }
  928. static int set_serial_info(struct async_struct * info,
  929. struct serial_struct __user * new_info)
  930. {
  931. struct serial_struct new_serial;
  932. struct serial_state old_state, *state;
  933. unsigned int change_irq,change_port;
  934. int retval = 0;
  935. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  936. return -EFAULT;
  937. lock_kernel();
  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. unlock_kernel();
  944. return -EINVAL;
  945. }
  946. if (!serial_isroot()) {
  947. if ((new_serial.baud_base != state->baud_base) ||
  948. (new_serial.close_delay != state->close_delay) ||
  949. (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
  950. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  951. (state->flags & ~ASYNC_USR_MASK)))
  952. return -EPERM;
  953. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  954. (new_serial.flags & ASYNC_USR_MASK));
  955. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  956. (new_serial.flags & ASYNC_USR_MASK));
  957. state->custom_divisor = new_serial.custom_divisor;
  958. goto check_and_exit;
  959. }
  960. if (new_serial.baud_base < 9600) {
  961. unlock_kernel();
  962. return -EINVAL;
  963. }
  964. /*
  965. * OK, past this point, all the error checking has been done.
  966. * At this point, we start making changes.....
  967. */
  968. state->baud_base = new_serial.baud_base;
  969. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  970. (new_serial.flags & ASYNC_FLAGS));
  971. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  972. (info->flags & ASYNC_INTERNAL_FLAGS));
  973. state->custom_divisor = new_serial.custom_divisor;
  974. state->close_delay = new_serial.close_delay * HZ/100;
  975. state->closing_wait = new_serial.closing_wait * HZ/100;
  976. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  977. check_and_exit:
  978. if (info->flags & ASYNC_INITIALIZED) {
  979. if (((old_state.flags & ASYNC_SPD_MASK) !=
  980. (state->flags & ASYNC_SPD_MASK)) ||
  981. (old_state.custom_divisor != state->custom_divisor)) {
  982. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  983. info->tty->alt_speed = 57600;
  984. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  985. info->tty->alt_speed = 115200;
  986. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  987. info->tty->alt_speed = 230400;
  988. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  989. info->tty->alt_speed = 460800;
  990. change_speed(info, NULL);
  991. }
  992. } else
  993. retval = startup(info);
  994. unlock_kernel();
  995. return retval;
  996. }
  997. /*
  998. * get_lsr_info - get line status register info
  999. *
  1000. * Purpose: Let user call ioctl() to get info when the UART physically
  1001. * is emptied. On bus types like RS485, the transmitter must
  1002. * release the bus after transmitting. This must be done when
  1003. * the transmit shift register is empty, not be done when the
  1004. * transmit holding register is empty. This functionality
  1005. * allows an RS485 driver to be written in user space.
  1006. */
  1007. static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
  1008. {
  1009. unsigned char status;
  1010. unsigned int result;
  1011. unsigned long flags;
  1012. local_irq_save(flags);
  1013. status = custom.serdatr;
  1014. mb();
  1015. local_irq_restore(flags);
  1016. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  1017. if (copy_to_user(value, &result, sizeof(int)))
  1018. return -EFAULT;
  1019. return 0;
  1020. }
  1021. static int rs_tiocmget(struct tty_struct *tty, struct file *file)
  1022. {
  1023. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1024. unsigned char control, status;
  1025. unsigned long flags;
  1026. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1027. return -ENODEV;
  1028. if (tty->flags & (1 << TTY_IO_ERROR))
  1029. return -EIO;
  1030. control = info->MCR;
  1031. local_irq_save(flags);
  1032. status = ciab.pra;
  1033. local_irq_restore(flags);
  1034. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  1035. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  1036. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  1037. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  1038. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  1039. }
  1040. static int rs_tiocmset(struct tty_struct *tty, struct file *file,
  1041. unsigned int set, unsigned int clear)
  1042. {
  1043. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1044. unsigned long flags;
  1045. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1046. return -ENODEV;
  1047. if (tty->flags & (1 << TTY_IO_ERROR))
  1048. return -EIO;
  1049. local_irq_save(flags);
  1050. if (set & TIOCM_RTS)
  1051. info->MCR |= SER_RTS;
  1052. if (set & TIOCM_DTR)
  1053. info->MCR |= SER_DTR;
  1054. if (clear & TIOCM_RTS)
  1055. info->MCR &= ~SER_RTS;
  1056. if (clear & TIOCM_DTR)
  1057. info->MCR &= ~SER_DTR;
  1058. rtsdtr_ctrl(info->MCR);
  1059. local_irq_restore(flags);
  1060. return 0;
  1061. }
  1062. /*
  1063. * rs_break() --- routine which turns the break handling on or off
  1064. */
  1065. static int rs_break(struct tty_struct *tty, int break_state)
  1066. {
  1067. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1068. unsigned long flags;
  1069. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1070. return -EINVAL;
  1071. local_irq_save(flags);
  1072. if (break_state == -1)
  1073. custom.adkcon = AC_SETCLR | AC_UARTBRK;
  1074. else
  1075. custom.adkcon = AC_UARTBRK;
  1076. mb();
  1077. local_irq_restore(flags);
  1078. return 0;
  1079. }
  1080. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1081. unsigned int cmd, unsigned long arg)
  1082. {
  1083. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1084. struct async_icount cprev, cnow; /* kernel counter temps */
  1085. struct serial_icounter_struct icount;
  1086. void __user *argp = (void __user *)arg;
  1087. unsigned long flags;
  1088. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1089. return -ENODEV;
  1090. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1091. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1092. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1093. if (tty->flags & (1 << TTY_IO_ERROR))
  1094. return -EIO;
  1095. }
  1096. switch (cmd) {
  1097. case TIOCGSERIAL:
  1098. return get_serial_info(info, argp);
  1099. case TIOCSSERIAL:
  1100. return set_serial_info(info, argp);
  1101. case TIOCSERCONFIG:
  1102. return 0;
  1103. case TIOCSERGETLSR: /* Get line status register */
  1104. return get_lsr_info(info, argp);
  1105. case TIOCSERGSTRUCT:
  1106. if (copy_to_user(argp,
  1107. info, sizeof(struct async_struct)))
  1108. return -EFAULT;
  1109. return 0;
  1110. /*
  1111. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1112. * - mask passed in arg for lines of interest
  1113. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1114. * Caller should use TIOCGICOUNT to see which one it was
  1115. */
  1116. case TIOCMIWAIT:
  1117. local_irq_save(flags);
  1118. /* note the counters on entry */
  1119. cprev = info->state->icount;
  1120. local_irq_restore(flags);
  1121. while (1) {
  1122. interruptible_sleep_on(&info->delta_msr_wait);
  1123. /* see if a signal did it */
  1124. if (signal_pending(current))
  1125. return -ERESTARTSYS;
  1126. local_irq_save(flags);
  1127. cnow = info->state->icount; /* atomic copy */
  1128. local_irq_restore(flags);
  1129. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1130. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1131. return -EIO; /* no change => error */
  1132. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1133. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1134. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1135. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1136. return 0;
  1137. }
  1138. cprev = cnow;
  1139. }
  1140. /* NOTREACHED */
  1141. /*
  1142. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1143. * Return: write counters to the user passed counter struct
  1144. * NB: both 1->0 and 0->1 transitions are counted except for
  1145. * RI where only 0->1 is counted.
  1146. */
  1147. case TIOCGICOUNT:
  1148. local_irq_save(flags);
  1149. cnow = info->state->icount;
  1150. local_irq_restore(flags);
  1151. icount.cts = cnow.cts;
  1152. icount.dsr = cnow.dsr;
  1153. icount.rng = cnow.rng;
  1154. icount.dcd = cnow.dcd;
  1155. icount.rx = cnow.rx;
  1156. icount.tx = cnow.tx;
  1157. icount.frame = cnow.frame;
  1158. icount.overrun = cnow.overrun;
  1159. icount.parity = cnow.parity;
  1160. icount.brk = cnow.brk;
  1161. icount.buf_overrun = cnow.buf_overrun;
  1162. if (copy_to_user(argp, &icount, sizeof(icount)))
  1163. return -EFAULT;
  1164. return 0;
  1165. case TIOCSERGWILD:
  1166. case TIOCSERSWILD:
  1167. /* "setserial -W" is called in Debian boot */
  1168. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1169. return 0;
  1170. default:
  1171. return -ENOIOCTLCMD;
  1172. }
  1173. return 0;
  1174. }
  1175. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1176. {
  1177. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1178. unsigned long flags;
  1179. unsigned int cflag = tty->termios->c_cflag;
  1180. change_speed(info, old_termios);
  1181. /* Handle transition to B0 status */
  1182. if ((old_termios->c_cflag & CBAUD) &&
  1183. !(cflag & CBAUD)) {
  1184. info->MCR &= ~(SER_DTR|SER_RTS);
  1185. local_irq_save(flags);
  1186. rtsdtr_ctrl(info->MCR);
  1187. local_irq_restore(flags);
  1188. }
  1189. /* Handle transition away from B0 status */
  1190. if (!(old_termios->c_cflag & CBAUD) &&
  1191. (cflag & CBAUD)) {
  1192. info->MCR |= SER_DTR;
  1193. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1194. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1195. info->MCR |= SER_RTS;
  1196. }
  1197. local_irq_save(flags);
  1198. rtsdtr_ctrl(info->MCR);
  1199. local_irq_restore(flags);
  1200. }
  1201. /* Handle turning off CRTSCTS */
  1202. if ((old_termios->c_cflag & CRTSCTS) &&
  1203. !(tty->termios->c_cflag & CRTSCTS)) {
  1204. tty->hw_stopped = 0;
  1205. rs_start(tty);
  1206. }
  1207. #if 0
  1208. /*
  1209. * No need to wake up processes in open wait, since they
  1210. * sample the CLOCAL flag once, and don't recheck it.
  1211. * XXX It's not clear whether the current behavior is correct
  1212. * or not. Hence, this may change.....
  1213. */
  1214. if (!(old_termios->c_cflag & CLOCAL) &&
  1215. (tty->termios->c_cflag & CLOCAL))
  1216. wake_up_interruptible(&info->open_wait);
  1217. #endif
  1218. }
  1219. /*
  1220. * ------------------------------------------------------------
  1221. * rs_close()
  1222. *
  1223. * This routine is called when the serial port gets closed. First, we
  1224. * wait for the last remaining data to be sent. Then, we unlink its
  1225. * async structure from the interrupt chain if necessary, and we free
  1226. * that IRQ if nothing is left in the chain.
  1227. * ------------------------------------------------------------
  1228. */
  1229. static void rs_close(struct tty_struct *tty, struct file * filp)
  1230. {
  1231. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1232. struct serial_state *state;
  1233. unsigned long flags;
  1234. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1235. return;
  1236. state = info->state;
  1237. local_irq_save(flags);
  1238. if (tty_hung_up_p(filp)) {
  1239. DBG_CNT("before DEC-hung");
  1240. local_irq_restore(flags);
  1241. return;
  1242. }
  1243. #ifdef SERIAL_DEBUG_OPEN
  1244. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1245. #endif
  1246. if ((tty->count == 1) && (state->count != 1)) {
  1247. /*
  1248. * Uh, oh. tty->count is 1, which means that the tty
  1249. * structure will be freed. state->count should always
  1250. * be one in these conditions. If it's greater than
  1251. * one, we've got real problems, since it means the
  1252. * serial port won't be shutdown.
  1253. */
  1254. printk("rs_close: bad serial port count; tty->count is 1, "
  1255. "state->count is %d\n", state->count);
  1256. state->count = 1;
  1257. }
  1258. if (--state->count < 0) {
  1259. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1260. info->line, state->count);
  1261. state->count = 0;
  1262. }
  1263. if (state->count) {
  1264. DBG_CNT("before DEC-2");
  1265. local_irq_restore(flags);
  1266. return;
  1267. }
  1268. info->flags |= ASYNC_CLOSING;
  1269. /*
  1270. * Now we wait for the transmit buffer to clear; and we notify
  1271. * the line discipline to only process XON/XOFF characters.
  1272. */
  1273. tty->closing = 1;
  1274. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1275. tty_wait_until_sent(tty, info->closing_wait);
  1276. /*
  1277. * At this point we stop accepting input. To do this, we
  1278. * disable the receive line status interrupts, and tell the
  1279. * interrupt driver to stop checking the data ready bit in the
  1280. * line status register.
  1281. */
  1282. info->read_status_mask &= ~UART_LSR_DR;
  1283. if (info->flags & ASYNC_INITIALIZED) {
  1284. /* disable receive interrupts */
  1285. custom.intena = IF_RBF;
  1286. mb();
  1287. /* clear any pending receive interrupt */
  1288. custom.intreq = IF_RBF;
  1289. mb();
  1290. /*
  1291. * Before we drop DTR, make sure the UART transmitter
  1292. * has completely drained; this is especially
  1293. * important if there is a transmit FIFO!
  1294. */
  1295. rs_wait_until_sent(tty, info->timeout);
  1296. }
  1297. shutdown(info);
  1298. rs_flush_buffer(tty);
  1299. tty_ldisc_flush(tty);
  1300. tty->closing = 0;
  1301. info->event = 0;
  1302. info->tty = NULL;
  1303. if (info->blocked_open) {
  1304. if (info->close_delay) {
  1305. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1306. }
  1307. wake_up_interruptible(&info->open_wait);
  1308. }
  1309. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1310. wake_up_interruptible(&info->close_wait);
  1311. local_irq_restore(flags);
  1312. }
  1313. /*
  1314. * rs_wait_until_sent() --- wait until the transmitter is empty
  1315. */
  1316. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1317. {
  1318. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1319. unsigned long orig_jiffies, char_time;
  1320. int lsr;
  1321. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1322. return;
  1323. if (info->xmit_fifo_size == 0)
  1324. return; /* Just in case.... */
  1325. orig_jiffies = jiffies;
  1326. lock_kernel();
  1327. /*
  1328. * Set the check interval to be 1/5 of the estimated time to
  1329. * send a single character, and make it at least 1. The check
  1330. * interval should also be less than the timeout.
  1331. *
  1332. * Note: we have to use pretty tight timings here to satisfy
  1333. * the NIST-PCTS.
  1334. */
  1335. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1336. char_time = char_time / 5;
  1337. if (char_time == 0)
  1338. char_time = 1;
  1339. if (timeout)
  1340. char_time = min_t(unsigned long, char_time, timeout);
  1341. /*
  1342. * If the transmitter hasn't cleared in twice the approximate
  1343. * amount of time to send the entire FIFO, it probably won't
  1344. * ever clear. This assumes the UART isn't doing flow
  1345. * control, which is currently the case. Hence, if it ever
  1346. * takes longer than info->timeout, this is probably due to a
  1347. * UART bug of some kind. So, we clamp the timeout parameter at
  1348. * 2*info->timeout.
  1349. */
  1350. if (!timeout || timeout > 2*info->timeout)
  1351. timeout = 2*info->timeout;
  1352. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1353. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1354. printk("jiff=%lu...", jiffies);
  1355. #endif
  1356. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1357. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1358. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1359. #endif
  1360. msleep_interruptible(jiffies_to_msecs(char_time));
  1361. if (signal_pending(current))
  1362. break;
  1363. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1364. break;
  1365. }
  1366. __set_current_state(TASK_RUNNING);
  1367. unlock_kernel();
  1368. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1369. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1370. #endif
  1371. }
  1372. /*
  1373. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1374. */
  1375. static void rs_hangup(struct tty_struct *tty)
  1376. {
  1377. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1378. struct serial_state *state = info->state;
  1379. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1380. return;
  1381. state = info->state;
  1382. rs_flush_buffer(tty);
  1383. shutdown(info);
  1384. info->event = 0;
  1385. state->count = 0;
  1386. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1387. info->tty = NULL;
  1388. wake_up_interruptible(&info->open_wait);
  1389. }
  1390. /*
  1391. * ------------------------------------------------------------
  1392. * rs_open() and friends
  1393. * ------------------------------------------------------------
  1394. */
  1395. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1396. struct async_struct *info)
  1397. {
  1398. #ifdef DECLARE_WAITQUEUE
  1399. DECLARE_WAITQUEUE(wait, current);
  1400. #else
  1401. struct wait_queue wait = { current, NULL };
  1402. #endif
  1403. struct serial_state *state = info->state;
  1404. int retval;
  1405. int do_clocal = 0, extra_count = 0;
  1406. unsigned long flags;
  1407. /*
  1408. * If the device is in the middle of being closed, then block
  1409. * until it's done, and then try again.
  1410. */
  1411. if (tty_hung_up_p(filp) ||
  1412. (info->flags & ASYNC_CLOSING)) {
  1413. if (info->flags & ASYNC_CLOSING)
  1414. interruptible_sleep_on(&info->close_wait);
  1415. #ifdef SERIAL_DO_RESTART
  1416. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1417. -EAGAIN : -ERESTARTSYS);
  1418. #else
  1419. return -EAGAIN;
  1420. #endif
  1421. }
  1422. /*
  1423. * If non-blocking mode is set, or the port is not enabled,
  1424. * then make the check up front and then exit.
  1425. */
  1426. if ((filp->f_flags & O_NONBLOCK) ||
  1427. (tty->flags & (1 << TTY_IO_ERROR))) {
  1428. info->flags |= ASYNC_NORMAL_ACTIVE;
  1429. return 0;
  1430. }
  1431. if (tty->termios->c_cflag & CLOCAL)
  1432. do_clocal = 1;
  1433. /*
  1434. * Block waiting for the carrier detect and the line to become
  1435. * free (i.e., not in use by the callout). While we are in
  1436. * this loop, state->count is dropped by one, so that
  1437. * rs_close() knows when to free things. We restore it upon
  1438. * exit, either normal or abnormal.
  1439. */
  1440. retval = 0;
  1441. add_wait_queue(&info->open_wait, &wait);
  1442. #ifdef SERIAL_DEBUG_OPEN
  1443. printk("block_til_ready before block: ttys%d, count = %d\n",
  1444. state->line, state->count);
  1445. #endif
  1446. local_irq_save(flags);
  1447. if (!tty_hung_up_p(filp)) {
  1448. extra_count = 1;
  1449. state->count--;
  1450. }
  1451. local_irq_restore(flags);
  1452. info->blocked_open++;
  1453. while (1) {
  1454. local_irq_save(flags);
  1455. if (tty->termios->c_cflag & CBAUD)
  1456. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1457. local_irq_restore(flags);
  1458. set_current_state(TASK_INTERRUPTIBLE);
  1459. if (tty_hung_up_p(filp) ||
  1460. !(info->flags & ASYNC_INITIALIZED)) {
  1461. #ifdef SERIAL_DO_RESTART
  1462. if (info->flags & ASYNC_HUP_NOTIFY)
  1463. retval = -EAGAIN;
  1464. else
  1465. retval = -ERESTARTSYS;
  1466. #else
  1467. retval = -EAGAIN;
  1468. #endif
  1469. break;
  1470. }
  1471. if (!(info->flags & ASYNC_CLOSING) &&
  1472. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1473. break;
  1474. if (signal_pending(current)) {
  1475. retval = -ERESTARTSYS;
  1476. break;
  1477. }
  1478. #ifdef SERIAL_DEBUG_OPEN
  1479. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1480. info->line, state->count);
  1481. #endif
  1482. schedule();
  1483. }
  1484. __set_current_state(TASK_RUNNING);
  1485. remove_wait_queue(&info->open_wait, &wait);
  1486. if (extra_count)
  1487. state->count++;
  1488. info->blocked_open--;
  1489. #ifdef SERIAL_DEBUG_OPEN
  1490. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1491. info->line, state->count);
  1492. #endif
  1493. if (retval)
  1494. return retval;
  1495. info->flags |= ASYNC_NORMAL_ACTIVE;
  1496. return 0;
  1497. }
  1498. static int get_async_struct(int line, struct async_struct **ret_info)
  1499. {
  1500. struct async_struct *info;
  1501. struct serial_state *sstate;
  1502. sstate = rs_table + line;
  1503. sstate->count++;
  1504. if (sstate->info) {
  1505. *ret_info = sstate->info;
  1506. return 0;
  1507. }
  1508. info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
  1509. if (!info) {
  1510. sstate->count--;
  1511. return -ENOMEM;
  1512. }
  1513. #ifdef DECLARE_WAITQUEUE
  1514. init_waitqueue_head(&info->open_wait);
  1515. init_waitqueue_head(&info->close_wait);
  1516. init_waitqueue_head(&info->delta_msr_wait);
  1517. #endif
  1518. info->magic = SERIAL_MAGIC;
  1519. info->port = sstate->port;
  1520. info->flags = sstate->flags;
  1521. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1522. info->line = line;
  1523. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1524. info->state = sstate;
  1525. if (sstate->info) {
  1526. kfree(info);
  1527. *ret_info = sstate->info;
  1528. return 0;
  1529. }
  1530. *ret_info = sstate->info = info;
  1531. return 0;
  1532. }
  1533. /*
  1534. * This routine is called whenever a serial port is opened. It
  1535. * enables interrupts for a serial port, linking in its async structure into
  1536. * the IRQ chain. It also performs the serial-specific
  1537. * initialization for the tty structure.
  1538. */
  1539. static int rs_open(struct tty_struct *tty, struct file * filp)
  1540. {
  1541. struct async_struct *info;
  1542. int retval, line;
  1543. line = tty->index;
  1544. if ((line < 0) || (line >= NR_PORTS)) {
  1545. return -ENODEV;
  1546. }
  1547. retval = get_async_struct(line, &info);
  1548. if (retval) {
  1549. return retval;
  1550. }
  1551. tty->driver_data = info;
  1552. info->tty = tty;
  1553. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1554. return -ENODEV;
  1555. #ifdef SERIAL_DEBUG_OPEN
  1556. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1557. #endif
  1558. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1559. /*
  1560. * If the port is the middle of closing, bail out now
  1561. */
  1562. if (tty_hung_up_p(filp) ||
  1563. (info->flags & ASYNC_CLOSING)) {
  1564. if (info->flags & ASYNC_CLOSING)
  1565. interruptible_sleep_on(&info->close_wait);
  1566. #ifdef SERIAL_DO_RESTART
  1567. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1568. -EAGAIN : -ERESTARTSYS);
  1569. #else
  1570. return -EAGAIN;
  1571. #endif
  1572. }
  1573. /*
  1574. * Start up serial port
  1575. */
  1576. retval = startup(info);
  1577. if (retval) {
  1578. return retval;
  1579. }
  1580. retval = block_til_ready(tty, filp, info);
  1581. if (retval) {
  1582. #ifdef SERIAL_DEBUG_OPEN
  1583. printk("rs_open returning after block_til_ready with %d\n",
  1584. retval);
  1585. #endif
  1586. return retval;
  1587. }
  1588. #ifdef SERIAL_DEBUG_OPEN
  1589. printk("rs_open %s successful...", tty->name);
  1590. #endif
  1591. return 0;
  1592. }
  1593. /*
  1594. * /proc fs routines....
  1595. */
  1596. static inline int line_info(char *buf, struct serial_state *state)
  1597. {
  1598. struct async_struct *info = state->info, scr_info;
  1599. char stat_buf[30], control, status;
  1600. int ret;
  1601. unsigned long flags;
  1602. ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
  1603. /*
  1604. * Figure out the current RS-232 lines
  1605. */
  1606. if (!info) {
  1607. info = &scr_info; /* This is just for serial_{in,out} */
  1608. info->magic = SERIAL_MAGIC;
  1609. info->flags = state->flags;
  1610. info->quot = 0;
  1611. info->tty = NULL;
  1612. }
  1613. local_irq_save(flags);
  1614. status = ciab.pra;
  1615. control = info ? info->MCR : status;
  1616. local_irq_restore(flags);
  1617. stat_buf[0] = 0;
  1618. stat_buf[1] = 0;
  1619. if(!(control & SER_RTS))
  1620. strcat(stat_buf, "|RTS");
  1621. if(!(status & SER_CTS))
  1622. strcat(stat_buf, "|CTS");
  1623. if(!(control & SER_DTR))
  1624. strcat(stat_buf, "|DTR");
  1625. if(!(status & SER_DSR))
  1626. strcat(stat_buf, "|DSR");
  1627. if(!(status & SER_DCD))
  1628. strcat(stat_buf, "|CD");
  1629. if (info->quot) {
  1630. ret += sprintf(buf+ret, " baud:%d",
  1631. state->baud_base / info->quot);
  1632. }
  1633. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1634. state->icount.tx, state->icount.rx);
  1635. if (state->icount.frame)
  1636. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1637. if (state->icount.parity)
  1638. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1639. if (state->icount.brk)
  1640. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1641. if (state->icount.overrun)
  1642. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1643. /*
  1644. * Last thing is the RS-232 status lines
  1645. */
  1646. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1647. return ret;
  1648. }
  1649. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1650. int *eof, void *data)
  1651. {
  1652. int len = 0, l;
  1653. off_t begin = 0;
  1654. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  1655. l = line_info(page + len, &rs_table[0]);
  1656. len += l;
  1657. if (len+begin > off+count)
  1658. goto done;
  1659. if (len+begin < off) {
  1660. begin += len;
  1661. len = 0;
  1662. }
  1663. *eof = 1;
  1664. done:
  1665. if (off >= len+begin)
  1666. return 0;
  1667. *start = page + (off-begin);
  1668. return ((count < begin+len-off) ? count : begin+len-off);
  1669. }
  1670. /*
  1671. * ---------------------------------------------------------------------
  1672. * rs_init() and friends
  1673. *
  1674. * rs_init() is called at boot-time to initialize the serial driver.
  1675. * ---------------------------------------------------------------------
  1676. */
  1677. /*
  1678. * This routine prints out the appropriate serial driver version
  1679. * number, and identifies which options were configured into this
  1680. * driver.
  1681. */
  1682. static void show_serial_version(void)
  1683. {
  1684. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1685. }
  1686. static const struct tty_operations serial_ops = {
  1687. .open = rs_open,
  1688. .close = rs_close,
  1689. .write = rs_write,
  1690. .put_char = rs_put_char,
  1691. .flush_chars = rs_flush_chars,
  1692. .write_room = rs_write_room,
  1693. .chars_in_buffer = rs_chars_in_buffer,
  1694. .flush_buffer = rs_flush_buffer,
  1695. .ioctl = rs_ioctl,
  1696. .throttle = rs_throttle,
  1697. .unthrottle = rs_unthrottle,
  1698. .set_termios = rs_set_termios,
  1699. .stop = rs_stop,
  1700. .start = rs_start,
  1701. .hangup = rs_hangup,
  1702. .break_ctl = rs_break,
  1703. .send_xchar = rs_send_xchar,
  1704. .wait_until_sent = rs_wait_until_sent,
  1705. .read_proc = rs_read_proc,
  1706. .tiocmget = rs_tiocmget,
  1707. .tiocmset = rs_tiocmset,
  1708. };
  1709. /*
  1710. * The serial driver boot-time initialization code!
  1711. */
  1712. static int __init rs_init(void)
  1713. {
  1714. unsigned long flags;
  1715. struct serial_state * state;
  1716. if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
  1717. return -ENODEV;
  1718. serial_driver = alloc_tty_driver(1);
  1719. if (!serial_driver)
  1720. return -ENOMEM;
  1721. /*
  1722. * We request SERDAT and SERPER only, because the serial registers are
  1723. * too spreaded over the custom register space
  1724. */
  1725. if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
  1726. return -EBUSY;
  1727. IRQ_ports = NULL;
  1728. show_serial_version();
  1729. /* Initialize the tty_driver structure */
  1730. serial_driver->owner = THIS_MODULE;
  1731. serial_driver->driver_name = "amiserial";
  1732. serial_driver->name = "ttyS";
  1733. serial_driver->major = TTY_MAJOR;
  1734. serial_driver->minor_start = 64;
  1735. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1736. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1737. serial_driver->init_termios = tty_std_termios;
  1738. serial_driver->init_termios.c_cflag =
  1739. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1740. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1741. tty_set_operations(serial_driver, &serial_ops);
  1742. if (tty_register_driver(serial_driver))
  1743. panic("Couldn't register serial driver\n");
  1744. state = rs_table;
  1745. state->magic = SSTATE_MAGIC;
  1746. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1747. state->line = 0;
  1748. state->custom_divisor = 0;
  1749. state->close_delay = 5*HZ/10;
  1750. state->closing_wait = 30*HZ;
  1751. state->icount.cts = state->icount.dsr =
  1752. state->icount.rng = state->icount.dcd = 0;
  1753. state->icount.rx = state->icount.tx = 0;
  1754. state->icount.frame = state->icount.parity = 0;
  1755. state->icount.overrun = state->icount.brk = 0;
  1756. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1757. state->line);
  1758. /* Hardware set up */
  1759. state->baud_base = amiga_colorclock;
  1760. state->xmit_fifo_size = 1;
  1761. local_irq_save(flags);
  1762. /* set ISRs, and then disable the rx interrupts */
  1763. request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1764. request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED, "serial RX", state);
  1765. /* turn off Rx and Tx interrupts */
  1766. custom.intena = IF_RBF | IF_TBE;
  1767. mb();
  1768. /* clear any pending interrupt */
  1769. custom.intreq = IF_RBF | IF_TBE;
  1770. mb();
  1771. local_irq_restore(flags);
  1772. /*
  1773. * set the appropriate directions for the modem control flags,
  1774. * and clear RTS and DTR
  1775. */
  1776. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1777. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1778. return 0;
  1779. }
  1780. static __exit void rs_exit(void)
  1781. {
  1782. int error;
  1783. struct async_struct *info = rs_table[0].info;
  1784. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1785. tasklet_kill(&info->tlet);
  1786. if ((error = tty_unregister_driver(serial_driver)))
  1787. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1788. error);
  1789. put_tty_driver(serial_driver);
  1790. if (info) {
  1791. rs_table[0].info = NULL;
  1792. kfree(info);
  1793. }
  1794. release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
  1795. }
  1796. module_init(rs_init)
  1797. module_exit(rs_exit)
  1798. /*
  1799. * ------------------------------------------------------------
  1800. * Serial console driver
  1801. * ------------------------------------------------------------
  1802. */
  1803. #ifdef CONFIG_SERIAL_CONSOLE
  1804. static void amiga_serial_putc(char c)
  1805. {
  1806. custom.serdat = (unsigned char)c | 0x100;
  1807. while (!(custom.serdatr & 0x2000))
  1808. barrier();
  1809. }
  1810. /*
  1811. * Print a string to the serial port trying not to disturb
  1812. * any possible real use of the port...
  1813. *
  1814. * The console must be locked when we get here.
  1815. */
  1816. static void serial_console_write(struct console *co, const char *s,
  1817. unsigned count)
  1818. {
  1819. unsigned short intena = custom.intenar;
  1820. custom.intena = IF_TBE;
  1821. while (count--) {
  1822. if (*s == '\n')
  1823. amiga_serial_putc('\r');
  1824. amiga_serial_putc(*s++);
  1825. }
  1826. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1827. }
  1828. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1829. {
  1830. *index = 0;
  1831. return serial_driver;
  1832. }
  1833. static struct console sercons = {
  1834. .name = "ttyS",
  1835. .write = serial_console_write,
  1836. .device = serial_console_device,
  1837. .flags = CON_PRINTBUFFER,
  1838. .index = -1,
  1839. };
  1840. /*
  1841. * Register console.
  1842. */
  1843. static int __init amiserial_console_init(void)
  1844. {
  1845. register_console(&sercons);
  1846. return 0;
  1847. }
  1848. console_initcall(amiserial_console_init);
  1849. #endif
  1850. MODULE_LICENSE("GPL");