amiserial.c 53 KB

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