amiserial.c 53 KB

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