amiserial.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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. info->tty->termios->c_cflag &= ~CBAUD;
  628. info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
  629. baud = tty_get_baud_rate(info->tty);
  630. if (!baud)
  631. baud = 9600;
  632. if (baud == 38400 &&
  633. ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
  634. quot = info->state->custom_divisor;
  635. else {
  636. if (baud == 134)
  637. /* Special case since 134 is really 134.5 */
  638. quot = (2*baud_base / 269);
  639. else if (baud)
  640. quot = baud_base / baud;
  641. }
  642. }
  643. /* As a last resort, if the quotient is zero, default to 9600 bps */
  644. if (!quot)
  645. quot = baud_base / 9600;
  646. info->quot = quot;
  647. info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
  648. info->timeout += HZ/50; /* Add .02 seconds of slop */
  649. /* CTS flow control flag and modem status interrupts */
  650. info->IER &= ~UART_IER_MSI;
  651. if (info->flags & ASYNC_HARDPPS_CD)
  652. info->IER |= UART_IER_MSI;
  653. if (cflag & CRTSCTS) {
  654. info->flags |= ASYNC_CTS_FLOW;
  655. info->IER |= UART_IER_MSI;
  656. } else
  657. info->flags &= ~ASYNC_CTS_FLOW;
  658. if (cflag & CLOCAL)
  659. info->flags &= ~ASYNC_CHECK_CD;
  660. else {
  661. info->flags |= ASYNC_CHECK_CD;
  662. info->IER |= UART_IER_MSI;
  663. }
  664. /* TBD:
  665. * Does clearing IER_MSI imply that we should disbale the VBL interrupt ?
  666. */
  667. /*
  668. * Set up parity check flag
  669. */
  670. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  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 void 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;
  716. info = tty->driver_data;
  717. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  718. return;
  719. if (!info->xmit.buf)
  720. return;
  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;
  727. }
  728. info->xmit.buf[info->xmit.head++] = ch;
  729. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  730. local_irq_restore(flags);
  731. }
  732. static void rs_flush_chars(struct tty_struct *tty)
  733. {
  734. struct async_struct *info = (struct async_struct *)tty->driver_data;
  735. unsigned long flags;
  736. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  737. return;
  738. if (info->xmit.head == info->xmit.tail
  739. || tty->stopped
  740. || tty->hw_stopped
  741. || !info->xmit.buf)
  742. return;
  743. local_irq_save(flags);
  744. info->IER |= UART_IER_THRI;
  745. custom.intena = IF_SETCLR | IF_TBE;
  746. mb();
  747. /* set a pending Tx Interrupt, transmitter should restart now */
  748. custom.intreq = IF_SETCLR | IF_TBE;
  749. mb();
  750. local_irq_restore(flags);
  751. }
  752. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  753. {
  754. int c, ret = 0;
  755. struct async_struct *info;
  756. unsigned long flags;
  757. if (!tty)
  758. return 0;
  759. info = tty->driver_data;
  760. if (serial_paranoia_check(info, tty->name, "rs_write"))
  761. return 0;
  762. if (!info->xmit.buf)
  763. return 0;
  764. local_save_flags(flags);
  765. local_irq_disable();
  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. tmp.type = state->type;
  913. tmp.line = state->line;
  914. tmp.port = state->port;
  915. tmp.irq = state->irq;
  916. tmp.flags = state->flags;
  917. tmp.xmit_fifo_size = state->xmit_fifo_size;
  918. tmp.baud_base = state->baud_base;
  919. tmp.close_delay = state->close_delay;
  920. tmp.closing_wait = state->closing_wait;
  921. tmp.custom_divisor = state->custom_divisor;
  922. if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
  923. return -EFAULT;
  924. return 0;
  925. }
  926. static int set_serial_info(struct async_struct * info,
  927. struct serial_struct __user * new_info)
  928. {
  929. struct serial_struct new_serial;
  930. struct serial_state old_state, *state;
  931. unsigned int change_irq,change_port;
  932. int retval = 0;
  933. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  934. return -EFAULT;
  935. state = info->state;
  936. old_state = *state;
  937. change_irq = new_serial.irq != state->irq;
  938. change_port = (new_serial.port != state->port);
  939. if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size))
  940. return -EINVAL;
  941. if (!serial_isroot()) {
  942. if ((new_serial.baud_base != state->baud_base) ||
  943. (new_serial.close_delay != state->close_delay) ||
  944. (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
  945. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  946. (state->flags & ~ASYNC_USR_MASK)))
  947. return -EPERM;
  948. state->flags = ((state->flags & ~ASYNC_USR_MASK) |
  949. (new_serial.flags & ASYNC_USR_MASK));
  950. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  951. (new_serial.flags & ASYNC_USR_MASK));
  952. state->custom_divisor = new_serial.custom_divisor;
  953. goto check_and_exit;
  954. }
  955. if (new_serial.baud_base < 9600)
  956. return -EINVAL;
  957. /*
  958. * OK, past this point, all the error checking has been done.
  959. * At this point, we start making changes.....
  960. */
  961. state->baud_base = new_serial.baud_base;
  962. state->flags = ((state->flags & ~ASYNC_FLAGS) |
  963. (new_serial.flags & ASYNC_FLAGS));
  964. info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
  965. (info->flags & ASYNC_INTERNAL_FLAGS));
  966. state->custom_divisor = new_serial.custom_divisor;
  967. state->close_delay = new_serial.close_delay * HZ/100;
  968. state->closing_wait = new_serial.closing_wait * HZ/100;
  969. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  970. check_and_exit:
  971. if (info->flags & ASYNC_INITIALIZED) {
  972. if (((old_state.flags & ASYNC_SPD_MASK) !=
  973. (state->flags & ASYNC_SPD_MASK)) ||
  974. (old_state.custom_divisor != state->custom_divisor)) {
  975. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  976. info->tty->alt_speed = 57600;
  977. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  978. info->tty->alt_speed = 115200;
  979. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  980. info->tty->alt_speed = 230400;
  981. if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  982. info->tty->alt_speed = 460800;
  983. change_speed(info, NULL);
  984. }
  985. } else
  986. retval = startup(info);
  987. return retval;
  988. }
  989. /*
  990. * get_lsr_info - get line status register info
  991. *
  992. * Purpose: Let user call ioctl() to get info when the UART physically
  993. * is emptied. On bus types like RS485, the transmitter must
  994. * release the bus after transmitting. This must be done when
  995. * the transmit shift register is empty, not be done when the
  996. * transmit holding register is empty. This functionality
  997. * allows an RS485 driver to be written in user space.
  998. */
  999. static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
  1000. {
  1001. unsigned char status;
  1002. unsigned int result;
  1003. unsigned long flags;
  1004. local_irq_save(flags);
  1005. status = custom.serdatr;
  1006. mb();
  1007. local_irq_restore(flags);
  1008. result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
  1009. if (copy_to_user(value, &result, sizeof(int)))
  1010. return -EFAULT;
  1011. return 0;
  1012. }
  1013. static int rs_tiocmget(struct tty_struct *tty, struct file *file)
  1014. {
  1015. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1016. unsigned char control, status;
  1017. unsigned long flags;
  1018. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1019. return -ENODEV;
  1020. if (tty->flags & (1 << TTY_IO_ERROR))
  1021. return -EIO;
  1022. control = info->MCR;
  1023. local_irq_save(flags);
  1024. status = ciab.pra;
  1025. local_irq_restore(flags);
  1026. return ((control & SER_RTS) ? TIOCM_RTS : 0)
  1027. | ((control & SER_DTR) ? TIOCM_DTR : 0)
  1028. | (!(status & SER_DCD) ? TIOCM_CAR : 0)
  1029. | (!(status & SER_DSR) ? TIOCM_DSR : 0)
  1030. | (!(status & SER_CTS) ? TIOCM_CTS : 0);
  1031. }
  1032. static int rs_tiocmset(struct tty_struct *tty, struct file *file,
  1033. unsigned int set, unsigned int clear)
  1034. {
  1035. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1036. unsigned long flags;
  1037. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1038. return -ENODEV;
  1039. if (tty->flags & (1 << TTY_IO_ERROR))
  1040. return -EIO;
  1041. local_irq_save(flags);
  1042. if (set & TIOCM_RTS)
  1043. info->MCR |= SER_RTS;
  1044. if (set & TIOCM_DTR)
  1045. info->MCR |= SER_DTR;
  1046. if (clear & TIOCM_RTS)
  1047. info->MCR &= ~SER_RTS;
  1048. if (clear & TIOCM_DTR)
  1049. info->MCR &= ~SER_DTR;
  1050. rtsdtr_ctrl(info->MCR);
  1051. local_irq_restore(flags);
  1052. return 0;
  1053. }
  1054. /*
  1055. * rs_break() --- routine which turns the break handling on or off
  1056. */
  1057. static void rs_break(struct tty_struct *tty, int break_state)
  1058. {
  1059. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1060. unsigned long flags;
  1061. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1062. return;
  1063. local_irq_save(flags);
  1064. if (break_state == -1)
  1065. custom.adkcon = AC_SETCLR | AC_UARTBRK;
  1066. else
  1067. custom.adkcon = AC_UARTBRK;
  1068. mb();
  1069. local_irq_restore(flags);
  1070. }
  1071. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1072. unsigned int cmd, unsigned long arg)
  1073. {
  1074. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1075. struct async_icount cprev, cnow; /* kernel counter temps */
  1076. struct serial_icounter_struct icount;
  1077. void __user *argp = (void __user *)arg;
  1078. unsigned long flags;
  1079. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1080. return -ENODEV;
  1081. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1082. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1083. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1084. if (tty->flags & (1 << TTY_IO_ERROR))
  1085. return -EIO;
  1086. }
  1087. switch (cmd) {
  1088. case TIOCGSERIAL:
  1089. return get_serial_info(info, argp);
  1090. case TIOCSSERIAL:
  1091. return set_serial_info(info, argp);
  1092. case TIOCSERCONFIG:
  1093. return 0;
  1094. case TIOCSERGETLSR: /* Get line status register */
  1095. return get_lsr_info(info, argp);
  1096. case TIOCSERGSTRUCT:
  1097. if (copy_to_user(argp,
  1098. info, sizeof(struct async_struct)))
  1099. return -EFAULT;
  1100. return 0;
  1101. /*
  1102. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1103. * - mask passed in arg for lines of interest
  1104. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1105. * Caller should use TIOCGICOUNT to see which one it was
  1106. */
  1107. case TIOCMIWAIT:
  1108. local_irq_save(flags);
  1109. /* note the counters on entry */
  1110. cprev = info->state->icount;
  1111. local_irq_restore(flags);
  1112. while (1) {
  1113. interruptible_sleep_on(&info->delta_msr_wait);
  1114. /* see if a signal did it */
  1115. if (signal_pending(current))
  1116. return -ERESTARTSYS;
  1117. local_irq_save(flags);
  1118. cnow = info->state->icount; /* atomic copy */
  1119. local_irq_restore(flags);
  1120. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1121. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1122. return -EIO; /* no change => error */
  1123. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1124. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1125. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1126. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1127. return 0;
  1128. }
  1129. cprev = cnow;
  1130. }
  1131. /* NOTREACHED */
  1132. /*
  1133. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1134. * Return: write counters to the user passed counter struct
  1135. * NB: both 1->0 and 0->1 transitions are counted except for
  1136. * RI where only 0->1 is counted.
  1137. */
  1138. case TIOCGICOUNT:
  1139. local_irq_save(flags);
  1140. cnow = info->state->icount;
  1141. local_irq_restore(flags);
  1142. icount.cts = cnow.cts;
  1143. icount.dsr = cnow.dsr;
  1144. icount.rng = cnow.rng;
  1145. icount.dcd = cnow.dcd;
  1146. icount.rx = cnow.rx;
  1147. icount.tx = cnow.tx;
  1148. icount.frame = cnow.frame;
  1149. icount.overrun = cnow.overrun;
  1150. icount.parity = cnow.parity;
  1151. icount.brk = cnow.brk;
  1152. icount.buf_overrun = cnow.buf_overrun;
  1153. if (copy_to_user(argp, &icount, sizeof(icount)))
  1154. return -EFAULT;
  1155. return 0;
  1156. case TIOCSERGWILD:
  1157. case TIOCSERSWILD:
  1158. /* "setserial -W" is called in Debian boot */
  1159. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1160. return 0;
  1161. default:
  1162. return -ENOIOCTLCMD;
  1163. }
  1164. return 0;
  1165. }
  1166. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1167. {
  1168. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1169. unsigned long flags;
  1170. unsigned int cflag = tty->termios->c_cflag;
  1171. if ( (cflag == old_termios->c_cflag)
  1172. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  1173. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1174. return;
  1175. change_speed(info, old_termios);
  1176. /* Handle transition to B0 status */
  1177. if ((old_termios->c_cflag & CBAUD) &&
  1178. !(cflag & CBAUD)) {
  1179. info->MCR &= ~(SER_DTR|SER_RTS);
  1180. local_irq_save(flags);
  1181. rtsdtr_ctrl(info->MCR);
  1182. local_irq_restore(flags);
  1183. }
  1184. /* Handle transition away from B0 status */
  1185. if (!(old_termios->c_cflag & CBAUD) &&
  1186. (cflag & CBAUD)) {
  1187. info->MCR |= SER_DTR;
  1188. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1189. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1190. info->MCR |= SER_RTS;
  1191. }
  1192. local_irq_save(flags);
  1193. rtsdtr_ctrl(info->MCR);
  1194. local_irq_restore(flags);
  1195. }
  1196. /* Handle turning off CRTSCTS */
  1197. if ((old_termios->c_cflag & CRTSCTS) &&
  1198. !(tty->termios->c_cflag & CRTSCTS)) {
  1199. tty->hw_stopped = 0;
  1200. rs_start(tty);
  1201. }
  1202. #if 0
  1203. /*
  1204. * No need to wake up processes in open wait, since they
  1205. * sample the CLOCAL flag once, and don't recheck it.
  1206. * XXX It's not clear whether the current behavior is correct
  1207. * or not. Hence, this may change.....
  1208. */
  1209. if (!(old_termios->c_cflag & CLOCAL) &&
  1210. (tty->termios->c_cflag & CLOCAL))
  1211. wake_up_interruptible(&info->open_wait);
  1212. #endif
  1213. }
  1214. /*
  1215. * ------------------------------------------------------------
  1216. * rs_close()
  1217. *
  1218. * This routine is called when the serial port gets closed. First, we
  1219. * wait for the last remaining data to be sent. Then, we unlink its
  1220. * async structure from the interrupt chain if necessary, and we free
  1221. * that IRQ if nothing is left in the chain.
  1222. * ------------------------------------------------------------
  1223. */
  1224. static void rs_close(struct tty_struct *tty, struct file * filp)
  1225. {
  1226. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1227. struct serial_state *state;
  1228. unsigned long flags;
  1229. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1230. return;
  1231. state = info->state;
  1232. local_irq_save(flags);
  1233. if (tty_hung_up_p(filp)) {
  1234. DBG_CNT("before DEC-hung");
  1235. local_irq_restore(flags);
  1236. return;
  1237. }
  1238. #ifdef SERIAL_DEBUG_OPEN
  1239. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1240. #endif
  1241. if ((tty->count == 1) && (state->count != 1)) {
  1242. /*
  1243. * Uh, oh. tty->count is 1, which means that the tty
  1244. * structure will be freed. state->count should always
  1245. * be one in these conditions. If it's greater than
  1246. * one, we've got real problems, since it means the
  1247. * serial port won't be shutdown.
  1248. */
  1249. printk("rs_close: bad serial port count; tty->count is 1, "
  1250. "state->count is %d\n", state->count);
  1251. state->count = 1;
  1252. }
  1253. if (--state->count < 0) {
  1254. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1255. info->line, state->count);
  1256. state->count = 0;
  1257. }
  1258. if (state->count) {
  1259. DBG_CNT("before DEC-2");
  1260. local_irq_restore(flags);
  1261. return;
  1262. }
  1263. info->flags |= ASYNC_CLOSING;
  1264. /*
  1265. * Now we wait for the transmit buffer to clear; and we notify
  1266. * the line discipline to only process XON/XOFF characters.
  1267. */
  1268. tty->closing = 1;
  1269. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1270. tty_wait_until_sent(tty, info->closing_wait);
  1271. /*
  1272. * At this point we stop accepting input. To do this, we
  1273. * disable the receive line status interrupts, and tell the
  1274. * interrupt driver to stop checking the data ready bit in the
  1275. * line status register.
  1276. */
  1277. info->read_status_mask &= ~UART_LSR_DR;
  1278. if (info->flags & ASYNC_INITIALIZED) {
  1279. /* disable receive interrupts */
  1280. custom.intena = IF_RBF;
  1281. mb();
  1282. /* clear any pending receive interrupt */
  1283. custom.intreq = IF_RBF;
  1284. mb();
  1285. /*
  1286. * Before we drop DTR, make sure the UART transmitter
  1287. * has completely drained; this is especially
  1288. * important if there is a transmit FIFO!
  1289. */
  1290. rs_wait_until_sent(tty, info->timeout);
  1291. }
  1292. shutdown(info);
  1293. if (tty->driver->flush_buffer)
  1294. tty->driver->flush_buffer(tty);
  1295. tty_ldisc_flush(tty);
  1296. tty->closing = 0;
  1297. info->event = 0;
  1298. info->tty = NULL;
  1299. if (info->blocked_open) {
  1300. if (info->close_delay) {
  1301. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1302. }
  1303. wake_up_interruptible(&info->open_wait);
  1304. }
  1305. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1306. wake_up_interruptible(&info->close_wait);
  1307. local_irq_restore(flags);
  1308. }
  1309. /*
  1310. * rs_wait_until_sent() --- wait until the transmitter is empty
  1311. */
  1312. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1313. {
  1314. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1315. unsigned long orig_jiffies, char_time;
  1316. int lsr;
  1317. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1318. return;
  1319. if (info->xmit_fifo_size == 0)
  1320. return; /* Just in case.... */
  1321. orig_jiffies = jiffies;
  1322. /*
  1323. * Set the check interval to be 1/5 of the estimated time to
  1324. * send a single character, and make it at least 1. The check
  1325. * interval should also be less than the timeout.
  1326. *
  1327. * Note: we have to use pretty tight timings here to satisfy
  1328. * the NIST-PCTS.
  1329. */
  1330. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1331. char_time = char_time / 5;
  1332. if (char_time == 0)
  1333. char_time = 1;
  1334. if (timeout)
  1335. char_time = min_t(unsigned long, char_time, timeout);
  1336. /*
  1337. * If the transmitter hasn't cleared in twice the approximate
  1338. * amount of time to send the entire FIFO, it probably won't
  1339. * ever clear. This assumes the UART isn't doing flow
  1340. * control, which is currently the case. Hence, if it ever
  1341. * takes longer than info->timeout, this is probably due to a
  1342. * UART bug of some kind. So, we clamp the timeout parameter at
  1343. * 2*info->timeout.
  1344. */
  1345. if (!timeout || timeout > 2*info->timeout)
  1346. timeout = 2*info->timeout;
  1347. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1348. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1349. printk("jiff=%lu...", jiffies);
  1350. #endif
  1351. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1352. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1353. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1354. #endif
  1355. msleep_interruptible(jiffies_to_msecs(char_time));
  1356. if (signal_pending(current))
  1357. break;
  1358. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1359. break;
  1360. }
  1361. current->state = TASK_RUNNING;
  1362. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1363. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1364. #endif
  1365. }
  1366. /*
  1367. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1368. */
  1369. static void rs_hangup(struct tty_struct *tty)
  1370. {
  1371. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1372. struct serial_state *state = info->state;
  1373. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1374. return;
  1375. state = info->state;
  1376. rs_flush_buffer(tty);
  1377. shutdown(info);
  1378. info->event = 0;
  1379. state->count = 0;
  1380. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1381. info->tty = NULL;
  1382. wake_up_interruptible(&info->open_wait);
  1383. }
  1384. /*
  1385. * ------------------------------------------------------------
  1386. * rs_open() and friends
  1387. * ------------------------------------------------------------
  1388. */
  1389. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1390. struct async_struct *info)
  1391. {
  1392. #ifdef DECLARE_WAITQUEUE
  1393. DECLARE_WAITQUEUE(wait, current);
  1394. #else
  1395. struct wait_queue wait = { current, NULL };
  1396. #endif
  1397. struct serial_state *state = info->state;
  1398. int retval;
  1399. int do_clocal = 0, extra_count = 0;
  1400. unsigned long flags;
  1401. /*
  1402. * If the device is in the middle of being closed, then block
  1403. * until it's done, and then try again.
  1404. */
  1405. if (tty_hung_up_p(filp) ||
  1406. (info->flags & ASYNC_CLOSING)) {
  1407. if (info->flags & ASYNC_CLOSING)
  1408. interruptible_sleep_on(&info->close_wait);
  1409. #ifdef SERIAL_DO_RESTART
  1410. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1411. -EAGAIN : -ERESTARTSYS);
  1412. #else
  1413. return -EAGAIN;
  1414. #endif
  1415. }
  1416. /*
  1417. * If non-blocking mode is set, or the port is not enabled,
  1418. * then make the check up front and then exit.
  1419. */
  1420. if ((filp->f_flags & O_NONBLOCK) ||
  1421. (tty->flags & (1 << TTY_IO_ERROR))) {
  1422. info->flags |= ASYNC_NORMAL_ACTIVE;
  1423. return 0;
  1424. }
  1425. if (tty->termios->c_cflag & CLOCAL)
  1426. do_clocal = 1;
  1427. /*
  1428. * Block waiting for the carrier detect and the line to become
  1429. * free (i.e., not in use by the callout). While we are in
  1430. * this loop, state->count is dropped by one, so that
  1431. * rs_close() knows when to free things. We restore it upon
  1432. * exit, either normal or abnormal.
  1433. */
  1434. retval = 0;
  1435. add_wait_queue(&info->open_wait, &wait);
  1436. #ifdef SERIAL_DEBUG_OPEN
  1437. printk("block_til_ready before block: ttys%d, count = %d\n",
  1438. state->line, state->count);
  1439. #endif
  1440. local_irq_save(flags);
  1441. if (!tty_hung_up_p(filp)) {
  1442. extra_count = 1;
  1443. state->count--;
  1444. }
  1445. local_irq_restore(flags);
  1446. info->blocked_open++;
  1447. while (1) {
  1448. local_irq_save(flags);
  1449. if (tty->termios->c_cflag & CBAUD)
  1450. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1451. local_irq_restore(flags);
  1452. set_current_state(TASK_INTERRUPTIBLE);
  1453. if (tty_hung_up_p(filp) ||
  1454. !(info->flags & ASYNC_INITIALIZED)) {
  1455. #ifdef SERIAL_DO_RESTART
  1456. if (info->flags & ASYNC_HUP_NOTIFY)
  1457. retval = -EAGAIN;
  1458. else
  1459. retval = -ERESTARTSYS;
  1460. #else
  1461. retval = -EAGAIN;
  1462. #endif
  1463. break;
  1464. }
  1465. if (!(info->flags & ASYNC_CLOSING) &&
  1466. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1467. break;
  1468. if (signal_pending(current)) {
  1469. retval = -ERESTARTSYS;
  1470. break;
  1471. }
  1472. #ifdef SERIAL_DEBUG_OPEN
  1473. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1474. info->line, state->count);
  1475. #endif
  1476. schedule();
  1477. }
  1478. current->state = TASK_RUNNING;
  1479. remove_wait_queue(&info->open_wait, &wait);
  1480. if (extra_count)
  1481. state->count++;
  1482. info->blocked_open--;
  1483. #ifdef SERIAL_DEBUG_OPEN
  1484. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1485. info->line, state->count);
  1486. #endif
  1487. if (retval)
  1488. return retval;
  1489. info->flags |= ASYNC_NORMAL_ACTIVE;
  1490. return 0;
  1491. }
  1492. static int get_async_struct(int line, struct async_struct **ret_info)
  1493. {
  1494. struct async_struct *info;
  1495. struct serial_state *sstate;
  1496. sstate = rs_table + line;
  1497. sstate->count++;
  1498. if (sstate->info) {
  1499. *ret_info = sstate->info;
  1500. return 0;
  1501. }
  1502. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  1503. if (!info) {
  1504. sstate->count--;
  1505. return -ENOMEM;
  1506. }
  1507. memset(info, 0, sizeof(struct async_struct));
  1508. #ifdef DECLARE_WAITQUEUE
  1509. init_waitqueue_head(&info->open_wait);
  1510. init_waitqueue_head(&info->close_wait);
  1511. init_waitqueue_head(&info->delta_msr_wait);
  1512. #endif
  1513. info->magic = SERIAL_MAGIC;
  1514. info->port = sstate->port;
  1515. info->flags = sstate->flags;
  1516. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1517. info->line = line;
  1518. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1519. info->state = sstate;
  1520. if (sstate->info) {
  1521. kfree(info);
  1522. *ret_info = sstate->info;
  1523. return 0;
  1524. }
  1525. *ret_info = sstate->info = info;
  1526. return 0;
  1527. }
  1528. /*
  1529. * This routine is called whenever a serial port is opened. It
  1530. * enables interrupts for a serial port, linking in its async structure into
  1531. * the IRQ chain. It also performs the serial-specific
  1532. * initialization for the tty structure.
  1533. */
  1534. static int rs_open(struct tty_struct *tty, struct file * filp)
  1535. {
  1536. struct async_struct *info;
  1537. int retval, line;
  1538. line = tty->index;
  1539. if ((line < 0) || (line >= NR_PORTS)) {
  1540. return -ENODEV;
  1541. }
  1542. retval = get_async_struct(line, &info);
  1543. if (retval) {
  1544. return retval;
  1545. }
  1546. tty->driver_data = info;
  1547. info->tty = tty;
  1548. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1549. return -ENODEV;
  1550. #ifdef SERIAL_DEBUG_OPEN
  1551. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1552. #endif
  1553. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1554. /*
  1555. * If the port is the middle of closing, bail out now
  1556. */
  1557. if (tty_hung_up_p(filp) ||
  1558. (info->flags & ASYNC_CLOSING)) {
  1559. if (info->flags & ASYNC_CLOSING)
  1560. interruptible_sleep_on(&info->close_wait);
  1561. #ifdef SERIAL_DO_RESTART
  1562. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1563. -EAGAIN : -ERESTARTSYS);
  1564. #else
  1565. return -EAGAIN;
  1566. #endif
  1567. }
  1568. /*
  1569. * Start up serial port
  1570. */
  1571. retval = startup(info);
  1572. if (retval) {
  1573. return retval;
  1574. }
  1575. retval = block_til_ready(tty, filp, info);
  1576. if (retval) {
  1577. #ifdef SERIAL_DEBUG_OPEN
  1578. printk("rs_open returning after block_til_ready with %d\n",
  1579. retval);
  1580. #endif
  1581. return retval;
  1582. }
  1583. #ifdef SERIAL_DEBUG_OPEN
  1584. printk("rs_open %s successful...", tty->name);
  1585. #endif
  1586. return 0;
  1587. }
  1588. /*
  1589. * /proc fs routines....
  1590. */
  1591. static inline int line_info(char *buf, struct serial_state *state)
  1592. {
  1593. struct async_struct *info = state->info, scr_info;
  1594. char stat_buf[30], control, status;
  1595. int ret;
  1596. unsigned long flags;
  1597. ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
  1598. /*
  1599. * Figure out the current RS-232 lines
  1600. */
  1601. if (!info) {
  1602. info = &scr_info; /* This is just for serial_{in,out} */
  1603. info->magic = SERIAL_MAGIC;
  1604. info->flags = state->flags;
  1605. info->quot = 0;
  1606. info->tty = NULL;
  1607. }
  1608. local_irq_save(flags);
  1609. status = ciab.pra;
  1610. control = info ? info->MCR : status;
  1611. local_irq_restore(flags);
  1612. stat_buf[0] = 0;
  1613. stat_buf[1] = 0;
  1614. if(!(control & SER_RTS))
  1615. strcat(stat_buf, "|RTS");
  1616. if(!(status & SER_CTS))
  1617. strcat(stat_buf, "|CTS");
  1618. if(!(control & SER_DTR))
  1619. strcat(stat_buf, "|DTR");
  1620. if(!(status & SER_DSR))
  1621. strcat(stat_buf, "|DSR");
  1622. if(!(status & SER_DCD))
  1623. strcat(stat_buf, "|CD");
  1624. if (info->quot) {
  1625. ret += sprintf(buf+ret, " baud:%d",
  1626. state->baud_base / info->quot);
  1627. }
  1628. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1629. state->icount.tx, state->icount.rx);
  1630. if (state->icount.frame)
  1631. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1632. if (state->icount.parity)
  1633. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1634. if (state->icount.brk)
  1635. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1636. if (state->icount.overrun)
  1637. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1638. /*
  1639. * Last thing is the RS-232 status lines
  1640. */
  1641. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1642. return ret;
  1643. }
  1644. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1645. int *eof, void *data)
  1646. {
  1647. int len = 0, l;
  1648. off_t begin = 0;
  1649. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  1650. l = line_info(page + len, &rs_table[0]);
  1651. len += l;
  1652. if (len+begin > off+count)
  1653. goto done;
  1654. if (len+begin < off) {
  1655. begin += len;
  1656. len = 0;
  1657. }
  1658. *eof = 1;
  1659. done:
  1660. if (off >= len+begin)
  1661. return 0;
  1662. *start = page + (off-begin);
  1663. return ((count < begin+len-off) ? count : begin+len-off);
  1664. }
  1665. /*
  1666. * ---------------------------------------------------------------------
  1667. * rs_init() and friends
  1668. *
  1669. * rs_init() is called at boot-time to initialize the serial driver.
  1670. * ---------------------------------------------------------------------
  1671. */
  1672. /*
  1673. * This routine prints out the appropriate serial driver version
  1674. * number, and identifies which options were configured into this
  1675. * driver.
  1676. */
  1677. static void show_serial_version(void)
  1678. {
  1679. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1680. }
  1681. static const struct tty_operations serial_ops = {
  1682. .open = rs_open,
  1683. .close = rs_close,
  1684. .write = rs_write,
  1685. .put_char = rs_put_char,
  1686. .flush_chars = rs_flush_chars,
  1687. .write_room = rs_write_room,
  1688. .chars_in_buffer = rs_chars_in_buffer,
  1689. .flush_buffer = rs_flush_buffer,
  1690. .ioctl = rs_ioctl,
  1691. .throttle = rs_throttle,
  1692. .unthrottle = rs_unthrottle,
  1693. .set_termios = rs_set_termios,
  1694. .stop = rs_stop,
  1695. .start = rs_start,
  1696. .hangup = rs_hangup,
  1697. .break_ctl = rs_break,
  1698. .send_xchar = rs_send_xchar,
  1699. .wait_until_sent = rs_wait_until_sent,
  1700. .read_proc = rs_read_proc,
  1701. .tiocmget = rs_tiocmget,
  1702. .tiocmset = rs_tiocmset,
  1703. };
  1704. /*
  1705. * The serial driver boot-time initialization code!
  1706. */
  1707. static int __init rs_init(void)
  1708. {
  1709. unsigned long flags;
  1710. struct serial_state * state;
  1711. if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
  1712. return -ENODEV;
  1713. serial_driver = alloc_tty_driver(1);
  1714. if (!serial_driver)
  1715. return -ENOMEM;
  1716. /*
  1717. * We request SERDAT and SERPER only, because the serial registers are
  1718. * too spreaded over the custom register space
  1719. */
  1720. if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
  1721. return -EBUSY;
  1722. IRQ_ports = NULL;
  1723. show_serial_version();
  1724. /* Initialize the tty_driver structure */
  1725. serial_driver->owner = THIS_MODULE;
  1726. serial_driver->driver_name = "amiserial";
  1727. serial_driver->name = "ttyS";
  1728. serial_driver->major = TTY_MAJOR;
  1729. serial_driver->minor_start = 64;
  1730. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1731. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1732. serial_driver->init_termios = tty_std_termios;
  1733. serial_driver->init_termios.c_cflag =
  1734. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1735. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1736. tty_set_operations(serial_driver, &serial_ops);
  1737. if (tty_register_driver(serial_driver))
  1738. panic("Couldn't register serial driver\n");
  1739. state = rs_table;
  1740. state->magic = SSTATE_MAGIC;
  1741. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1742. state->line = 0;
  1743. state->custom_divisor = 0;
  1744. state->close_delay = 5*HZ/10;
  1745. state->closing_wait = 30*HZ;
  1746. state->icount.cts = state->icount.dsr =
  1747. state->icount.rng = state->icount.dcd = 0;
  1748. state->icount.rx = state->icount.tx = 0;
  1749. state->icount.frame = state->icount.parity = 0;
  1750. state->icount.overrun = state->icount.brk = 0;
  1751. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1752. state->line);
  1753. /* Hardware set up */
  1754. state->baud_base = amiga_colorclock;
  1755. state->xmit_fifo_size = 1;
  1756. local_irq_save(flags);
  1757. /* set ISRs, and then disable the rx interrupts */
  1758. request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1759. request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED, "serial RX", state);
  1760. /* turn off Rx and Tx interrupts */
  1761. custom.intena = IF_RBF | IF_TBE;
  1762. mb();
  1763. /* clear any pending interrupt */
  1764. custom.intreq = IF_RBF | IF_TBE;
  1765. mb();
  1766. local_irq_restore(flags);
  1767. /*
  1768. * set the appropriate directions for the modem control flags,
  1769. * and clear RTS and DTR
  1770. */
  1771. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1772. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1773. return 0;
  1774. }
  1775. static __exit void rs_exit(void)
  1776. {
  1777. int error;
  1778. struct async_struct *info = rs_table[0].info;
  1779. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1780. tasklet_kill(&info->tlet);
  1781. if ((error = tty_unregister_driver(serial_driver)))
  1782. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1783. error);
  1784. put_tty_driver(serial_driver);
  1785. if (info) {
  1786. rs_table[0].info = NULL;
  1787. kfree(info);
  1788. }
  1789. release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
  1790. }
  1791. module_init(rs_init)
  1792. module_exit(rs_exit)
  1793. /*
  1794. * ------------------------------------------------------------
  1795. * Serial console driver
  1796. * ------------------------------------------------------------
  1797. */
  1798. #ifdef CONFIG_SERIAL_CONSOLE
  1799. static void amiga_serial_putc(char c)
  1800. {
  1801. custom.serdat = (unsigned char)c | 0x100;
  1802. while (!(custom.serdatr & 0x2000))
  1803. barrier();
  1804. }
  1805. /*
  1806. * Print a string to the serial port trying not to disturb
  1807. * any possible real use of the port...
  1808. *
  1809. * The console must be locked when we get here.
  1810. */
  1811. static void serial_console_write(struct console *co, const char *s,
  1812. unsigned count)
  1813. {
  1814. unsigned short intena = custom.intenar;
  1815. custom.intena = IF_TBE;
  1816. while (count--) {
  1817. if (*s == '\n')
  1818. amiga_serial_putc('\r');
  1819. amiga_serial_putc(*s++);
  1820. }
  1821. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1822. }
  1823. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1824. {
  1825. *index = 0;
  1826. return serial_driver;
  1827. }
  1828. static struct console sercons = {
  1829. .name = "ttyS",
  1830. .write = serial_console_write,
  1831. .device = serial_console_device,
  1832. .flags = CON_PRINTBUFFER,
  1833. .index = -1,
  1834. };
  1835. /*
  1836. * Register console.
  1837. */
  1838. static int __init amiserial_console_init(void)
  1839. {
  1840. register_console(&sercons);
  1841. return 0;
  1842. }
  1843. console_initcall(amiserial_console_init);
  1844. #endif
  1845. MODULE_LICENSE("GPL");