amiserial.c 54 KB

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