amiserial.c 55 KB

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