amiserial.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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 = (struct async_struct *)tty->driver_data;
  736. unsigned long flags;
  737. if (serial_paranoia_check(info, tty->name, "rs_put_char"))
  738. return;
  739. if (!tty || !info->xmit.buf)
  740. return;
  741. local_irq_save(flags);
  742. if (CIRC_SPACE(info->xmit.head,
  743. info->xmit.tail,
  744. SERIAL_XMIT_SIZE) == 0) {
  745. local_irq_restore(flags);
  746. return;
  747. }
  748. info->xmit.buf[info->xmit.head++] = ch;
  749. info->xmit.head &= SERIAL_XMIT_SIZE-1;
  750. local_irq_restore(flags);
  751. }
  752. static void rs_flush_chars(struct tty_struct *tty)
  753. {
  754. struct async_struct *info = (struct async_struct *)tty->driver_data;
  755. unsigned long flags;
  756. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  757. return;
  758. if (info->xmit.head == info->xmit.tail
  759. || tty->stopped
  760. || tty->hw_stopped
  761. || !info->xmit.buf)
  762. return;
  763. local_irq_save(flags);
  764. info->IER |= UART_IER_THRI;
  765. custom.intena = IF_SETCLR | IF_TBE;
  766. mb();
  767. /* set a pending Tx Interrupt, transmitter should restart now */
  768. custom.intreq = IF_SETCLR | IF_TBE;
  769. mb();
  770. local_irq_restore(flags);
  771. }
  772. static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
  773. {
  774. int c, ret = 0;
  775. struct async_struct *info = (struct async_struct *)tty->driver_data;
  776. unsigned long flags;
  777. if (serial_paranoia_check(info, tty->name, "rs_write"))
  778. return 0;
  779. if (!tty || !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 * 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 * 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 *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. unsigned long flags;
  1096. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1097. return -ENODEV;
  1098. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1099. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
  1100. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1101. if (tty->flags & (1 << TTY_IO_ERROR))
  1102. return -EIO;
  1103. }
  1104. switch (cmd) {
  1105. case TIOCGSERIAL:
  1106. return get_serial_info(info,
  1107. (struct serial_struct *) arg);
  1108. case TIOCSSERIAL:
  1109. return set_serial_info(info,
  1110. (struct serial_struct *) arg);
  1111. case TIOCSERCONFIG:
  1112. return 0;
  1113. case TIOCSERGETLSR: /* Get line status register */
  1114. return get_lsr_info(info, (unsigned int *) arg);
  1115. case TIOCSERGSTRUCT:
  1116. if (copy_to_user((struct async_struct *) arg,
  1117. info, sizeof(struct async_struct)))
  1118. return -EFAULT;
  1119. return 0;
  1120. /*
  1121. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1122. * - mask passed in arg for lines of interest
  1123. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1124. * Caller should use TIOCGICOUNT to see which one it was
  1125. */
  1126. case TIOCMIWAIT:
  1127. local_irq_save(flags);
  1128. /* note the counters on entry */
  1129. cprev = info->state->icount;
  1130. local_irq_restore(flags);
  1131. while (1) {
  1132. interruptible_sleep_on(&info->delta_msr_wait);
  1133. /* see if a signal did it */
  1134. if (signal_pending(current))
  1135. return -ERESTARTSYS;
  1136. local_irq_save(flags);
  1137. cnow = info->state->icount; /* atomic copy */
  1138. local_irq_restore(flags);
  1139. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1140. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1141. return -EIO; /* no change => error */
  1142. if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1143. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1144. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1145. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
  1146. return 0;
  1147. }
  1148. cprev = cnow;
  1149. }
  1150. /* NOTREACHED */
  1151. /*
  1152. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1153. * Return: write counters to the user passed counter struct
  1154. * NB: both 1->0 and 0->1 transitions are counted except for
  1155. * RI where only 0->1 is counted.
  1156. */
  1157. case TIOCGICOUNT:
  1158. local_irq_save(flags);
  1159. cnow = info->state->icount;
  1160. local_irq_restore(flags);
  1161. icount.cts = cnow.cts;
  1162. icount.dsr = cnow.dsr;
  1163. icount.rng = cnow.rng;
  1164. icount.dcd = cnow.dcd;
  1165. icount.rx = cnow.rx;
  1166. icount.tx = cnow.tx;
  1167. icount.frame = cnow.frame;
  1168. icount.overrun = cnow.overrun;
  1169. icount.parity = cnow.parity;
  1170. icount.brk = cnow.brk;
  1171. icount.buf_overrun = cnow.buf_overrun;
  1172. if (copy_to_user((void *)arg, &icount, sizeof(icount)))
  1173. return -EFAULT;
  1174. return 0;
  1175. case TIOCSERGWILD:
  1176. case TIOCSERSWILD:
  1177. /* "setserial -W" is called in Debian boot */
  1178. printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
  1179. return 0;
  1180. default:
  1181. return -ENOIOCTLCMD;
  1182. }
  1183. return 0;
  1184. }
  1185. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1186. {
  1187. struct async_struct *info = (struct async_struct *)tty->driver_data;
  1188. unsigned long flags;
  1189. unsigned int cflag = tty->termios->c_cflag;
  1190. if ( (cflag == old_termios->c_cflag)
  1191. && ( RELEVANT_IFLAG(tty->termios->c_iflag)
  1192. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1193. return;
  1194. change_speed(info, old_termios);
  1195. /* Handle transition to B0 status */
  1196. if ((old_termios->c_cflag & CBAUD) &&
  1197. !(cflag & CBAUD)) {
  1198. info->MCR &= ~(SER_DTR|SER_RTS);
  1199. local_irq_save(flags);
  1200. rtsdtr_ctrl(info->MCR);
  1201. local_irq_restore(flags);
  1202. }
  1203. /* Handle transition away from B0 status */
  1204. if (!(old_termios->c_cflag & CBAUD) &&
  1205. (cflag & CBAUD)) {
  1206. info->MCR |= SER_DTR;
  1207. if (!(tty->termios->c_cflag & CRTSCTS) ||
  1208. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1209. info->MCR |= SER_RTS;
  1210. }
  1211. local_irq_save(flags);
  1212. rtsdtr_ctrl(info->MCR);
  1213. local_irq_restore(flags);
  1214. }
  1215. /* Handle turning off CRTSCTS */
  1216. if ((old_termios->c_cflag & CRTSCTS) &&
  1217. !(tty->termios->c_cflag & CRTSCTS)) {
  1218. tty->hw_stopped = 0;
  1219. rs_start(tty);
  1220. }
  1221. #if 0
  1222. /*
  1223. * No need to wake up processes in open wait, since they
  1224. * sample the CLOCAL flag once, and don't recheck it.
  1225. * XXX It's not clear whether the current behavior is correct
  1226. * or not. Hence, this may change.....
  1227. */
  1228. if (!(old_termios->c_cflag & CLOCAL) &&
  1229. (tty->termios->c_cflag & CLOCAL))
  1230. wake_up_interruptible(&info->open_wait);
  1231. #endif
  1232. }
  1233. /*
  1234. * ------------------------------------------------------------
  1235. * rs_close()
  1236. *
  1237. * This routine is called when the serial port gets closed. First, we
  1238. * wait for the last remaining data to be sent. Then, we unlink its
  1239. * async structure from the interrupt chain if necessary, and we free
  1240. * that IRQ if nothing is left in the chain.
  1241. * ------------------------------------------------------------
  1242. */
  1243. static void rs_close(struct tty_struct *tty, struct file * filp)
  1244. {
  1245. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1246. struct serial_state *state;
  1247. unsigned long flags;
  1248. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1249. return;
  1250. state = info->state;
  1251. local_irq_save(flags);
  1252. if (tty_hung_up_p(filp)) {
  1253. DBG_CNT("before DEC-hung");
  1254. local_irq_restore(flags);
  1255. return;
  1256. }
  1257. #ifdef SERIAL_DEBUG_OPEN
  1258. printk("rs_close ttys%d, count = %d\n", info->line, state->count);
  1259. #endif
  1260. if ((tty->count == 1) && (state->count != 1)) {
  1261. /*
  1262. * Uh, oh. tty->count is 1, which means that the tty
  1263. * structure will be freed. state->count should always
  1264. * be one in these conditions. If it's greater than
  1265. * one, we've got real problems, since it means the
  1266. * serial port won't be shutdown.
  1267. */
  1268. printk("rs_close: bad serial port count; tty->count is 1, "
  1269. "state->count is %d\n", state->count);
  1270. state->count = 1;
  1271. }
  1272. if (--state->count < 0) {
  1273. printk("rs_close: bad serial port count for ttys%d: %d\n",
  1274. info->line, state->count);
  1275. state->count = 0;
  1276. }
  1277. if (state->count) {
  1278. DBG_CNT("before DEC-2");
  1279. local_irq_restore(flags);
  1280. return;
  1281. }
  1282. info->flags |= ASYNC_CLOSING;
  1283. /*
  1284. * Now we wait for the transmit buffer to clear; and we notify
  1285. * the line discipline to only process XON/XOFF characters.
  1286. */
  1287. tty->closing = 1;
  1288. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1289. tty_wait_until_sent(tty, info->closing_wait);
  1290. /*
  1291. * At this point we stop accepting input. To do this, we
  1292. * disable the receive line status interrupts, and tell the
  1293. * interrupt driver to stop checking the data ready bit in the
  1294. * line status register.
  1295. */
  1296. info->read_status_mask &= ~UART_LSR_DR;
  1297. if (info->flags & ASYNC_INITIALIZED) {
  1298. /* disable receive interrupts */
  1299. custom.intena = IF_RBF;
  1300. mb();
  1301. /* clear any pending receive interrupt */
  1302. custom.intreq = IF_RBF;
  1303. mb();
  1304. /*
  1305. * Before we drop DTR, make sure the UART transmitter
  1306. * has completely drained; this is especially
  1307. * important if there is a transmit FIFO!
  1308. */
  1309. rs_wait_until_sent(tty, info->timeout);
  1310. }
  1311. shutdown(info);
  1312. if (tty->driver->flush_buffer)
  1313. tty->driver->flush_buffer(tty);
  1314. tty_ldisc_flush(tty);
  1315. tty->closing = 0;
  1316. info->event = 0;
  1317. info->tty = NULL;
  1318. if (info->blocked_open) {
  1319. if (info->close_delay) {
  1320. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1321. }
  1322. wake_up_interruptible(&info->open_wait);
  1323. }
  1324. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1325. wake_up_interruptible(&info->close_wait);
  1326. local_irq_restore(flags);
  1327. }
  1328. /*
  1329. * rs_wait_until_sent() --- wait until the transmitter is empty
  1330. */
  1331. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1332. {
  1333. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1334. unsigned long orig_jiffies, char_time;
  1335. int lsr;
  1336. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1337. return;
  1338. if (info->xmit_fifo_size == 0)
  1339. return; /* Just in case.... */
  1340. orig_jiffies = jiffies;
  1341. /*
  1342. * Set the check interval to be 1/5 of the estimated time to
  1343. * send a single character, and make it at least 1. The check
  1344. * interval should also be less than the timeout.
  1345. *
  1346. * Note: we have to use pretty tight timings here to satisfy
  1347. * the NIST-PCTS.
  1348. */
  1349. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1350. char_time = char_time / 5;
  1351. if (char_time == 0)
  1352. char_time = 1;
  1353. if (timeout)
  1354. char_time = min_t(unsigned long, char_time, timeout);
  1355. /*
  1356. * If the transmitter hasn't cleared in twice the approximate
  1357. * amount of time to send the entire FIFO, it probably won't
  1358. * ever clear. This assumes the UART isn't doing flow
  1359. * control, which is currently the case. Hence, if it ever
  1360. * takes longer than info->timeout, this is probably due to a
  1361. * UART bug of some kind. So, we clamp the timeout parameter at
  1362. * 2*info->timeout.
  1363. */
  1364. if (!timeout || timeout > 2*info->timeout)
  1365. timeout = 2*info->timeout;
  1366. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1367. printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1368. printk("jiff=%lu...", jiffies);
  1369. #endif
  1370. while(!((lsr = custom.serdatr) & SDR_TSRE)) {
  1371. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1372. printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
  1373. #endif
  1374. msleep_interruptible(jiffies_to_msecs(char_time));
  1375. if (signal_pending(current))
  1376. break;
  1377. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1378. break;
  1379. }
  1380. current->state = TASK_RUNNING;
  1381. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1382. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1383. #endif
  1384. }
  1385. /*
  1386. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1387. */
  1388. static void rs_hangup(struct tty_struct *tty)
  1389. {
  1390. struct async_struct * info = (struct async_struct *)tty->driver_data;
  1391. struct serial_state *state = info->state;
  1392. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1393. return;
  1394. state = info->state;
  1395. rs_flush_buffer(tty);
  1396. shutdown(info);
  1397. info->event = 0;
  1398. state->count = 0;
  1399. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1400. info->tty = NULL;
  1401. wake_up_interruptible(&info->open_wait);
  1402. }
  1403. /*
  1404. * ------------------------------------------------------------
  1405. * rs_open() and friends
  1406. * ------------------------------------------------------------
  1407. */
  1408. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1409. struct async_struct *info)
  1410. {
  1411. #ifdef DECLARE_WAITQUEUE
  1412. DECLARE_WAITQUEUE(wait, current);
  1413. #else
  1414. struct wait_queue wait = { current, NULL };
  1415. #endif
  1416. struct serial_state *state = info->state;
  1417. int retval;
  1418. int do_clocal = 0, extra_count = 0;
  1419. unsigned long flags;
  1420. /*
  1421. * If the device is in the middle of being closed, then block
  1422. * until it's done, and then try again.
  1423. */
  1424. if (tty_hung_up_p(filp) ||
  1425. (info->flags & ASYNC_CLOSING)) {
  1426. if (info->flags & ASYNC_CLOSING)
  1427. interruptible_sleep_on(&info->close_wait);
  1428. #ifdef SERIAL_DO_RESTART
  1429. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1430. -EAGAIN : -ERESTARTSYS);
  1431. #else
  1432. return -EAGAIN;
  1433. #endif
  1434. }
  1435. /*
  1436. * If non-blocking mode is set, or the port is not enabled,
  1437. * then make the check up front and then exit.
  1438. */
  1439. if ((filp->f_flags & O_NONBLOCK) ||
  1440. (tty->flags & (1 << TTY_IO_ERROR))) {
  1441. info->flags |= ASYNC_NORMAL_ACTIVE;
  1442. return 0;
  1443. }
  1444. if (tty->termios->c_cflag & CLOCAL)
  1445. do_clocal = 1;
  1446. /*
  1447. * Block waiting for the carrier detect and the line to become
  1448. * free (i.e., not in use by the callout). While we are in
  1449. * this loop, state->count is dropped by one, so that
  1450. * rs_close() knows when to free things. We restore it upon
  1451. * exit, either normal or abnormal.
  1452. */
  1453. retval = 0;
  1454. add_wait_queue(&info->open_wait, &wait);
  1455. #ifdef SERIAL_DEBUG_OPEN
  1456. printk("block_til_ready before block: ttys%d, count = %d\n",
  1457. state->line, state->count);
  1458. #endif
  1459. local_irq_save(flags);
  1460. if (!tty_hung_up_p(filp)) {
  1461. extra_count = 1;
  1462. state->count--;
  1463. }
  1464. local_irq_restore(flags);
  1465. info->blocked_open++;
  1466. while (1) {
  1467. local_irq_save(flags);
  1468. if (tty->termios->c_cflag & CBAUD)
  1469. rtsdtr_ctrl(SER_DTR|SER_RTS);
  1470. local_irq_restore(flags);
  1471. set_current_state(TASK_INTERRUPTIBLE);
  1472. if (tty_hung_up_p(filp) ||
  1473. !(info->flags & ASYNC_INITIALIZED)) {
  1474. #ifdef SERIAL_DO_RESTART
  1475. if (info->flags & ASYNC_HUP_NOTIFY)
  1476. retval = -EAGAIN;
  1477. else
  1478. retval = -ERESTARTSYS;
  1479. #else
  1480. retval = -EAGAIN;
  1481. #endif
  1482. break;
  1483. }
  1484. if (!(info->flags & ASYNC_CLOSING) &&
  1485. (do_clocal || (!(ciab.pra & SER_DCD)) ))
  1486. break;
  1487. if (signal_pending(current)) {
  1488. retval = -ERESTARTSYS;
  1489. break;
  1490. }
  1491. #ifdef SERIAL_DEBUG_OPEN
  1492. printk("block_til_ready blocking: ttys%d, count = %d\n",
  1493. info->line, state->count);
  1494. #endif
  1495. schedule();
  1496. }
  1497. current->state = TASK_RUNNING;
  1498. remove_wait_queue(&info->open_wait, &wait);
  1499. if (extra_count)
  1500. state->count++;
  1501. info->blocked_open--;
  1502. #ifdef SERIAL_DEBUG_OPEN
  1503. printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1504. info->line, state->count);
  1505. #endif
  1506. if (retval)
  1507. return retval;
  1508. info->flags |= ASYNC_NORMAL_ACTIVE;
  1509. return 0;
  1510. }
  1511. static int get_async_struct(int line, struct async_struct **ret_info)
  1512. {
  1513. struct async_struct *info;
  1514. struct serial_state *sstate;
  1515. sstate = rs_table + line;
  1516. sstate->count++;
  1517. if (sstate->info) {
  1518. *ret_info = sstate->info;
  1519. return 0;
  1520. }
  1521. info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
  1522. if (!info) {
  1523. sstate->count--;
  1524. return -ENOMEM;
  1525. }
  1526. memset(info, 0, sizeof(struct async_struct));
  1527. #ifdef DECLARE_WAITQUEUE
  1528. init_waitqueue_head(&info->open_wait);
  1529. init_waitqueue_head(&info->close_wait);
  1530. init_waitqueue_head(&info->delta_msr_wait);
  1531. #endif
  1532. info->magic = SERIAL_MAGIC;
  1533. info->port = sstate->port;
  1534. info->flags = sstate->flags;
  1535. info->xmit_fifo_size = sstate->xmit_fifo_size;
  1536. info->line = line;
  1537. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1538. info->state = sstate;
  1539. if (sstate->info) {
  1540. kfree(info);
  1541. *ret_info = sstate->info;
  1542. return 0;
  1543. }
  1544. *ret_info = sstate->info = info;
  1545. return 0;
  1546. }
  1547. /*
  1548. * This routine is called whenever a serial port is opened. It
  1549. * enables interrupts for a serial port, linking in its async structure into
  1550. * the IRQ chain. It also performs the serial-specific
  1551. * initialization for the tty structure.
  1552. */
  1553. static int rs_open(struct tty_struct *tty, struct file * filp)
  1554. {
  1555. struct async_struct *info;
  1556. int retval, line;
  1557. unsigned long page;
  1558. line = tty->index;
  1559. if ((line < 0) || (line >= NR_PORTS)) {
  1560. return -ENODEV;
  1561. }
  1562. retval = get_async_struct(line, &info);
  1563. if (retval) {
  1564. return retval;
  1565. }
  1566. tty->driver_data = info;
  1567. info->tty = tty;
  1568. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1569. return -ENODEV;
  1570. #ifdef SERIAL_DEBUG_OPEN
  1571. printk("rs_open %s, count = %d\n", tty->name, info->state->count);
  1572. #endif
  1573. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1574. if (!tmp_buf) {
  1575. page = get_zeroed_page(GFP_KERNEL);
  1576. if (!page) {
  1577. return -ENOMEM;
  1578. }
  1579. if (tmp_buf)
  1580. free_page(page);
  1581. else
  1582. tmp_buf = (unsigned char *) page;
  1583. }
  1584. /*
  1585. * If the port is the middle of closing, bail out now
  1586. */
  1587. if (tty_hung_up_p(filp) ||
  1588. (info->flags & ASYNC_CLOSING)) {
  1589. if (info->flags & ASYNC_CLOSING)
  1590. interruptible_sleep_on(&info->close_wait);
  1591. #ifdef SERIAL_DO_RESTART
  1592. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1593. -EAGAIN : -ERESTARTSYS);
  1594. #else
  1595. return -EAGAIN;
  1596. #endif
  1597. }
  1598. /*
  1599. * Start up serial port
  1600. */
  1601. retval = startup(info);
  1602. if (retval) {
  1603. return retval;
  1604. }
  1605. retval = block_til_ready(tty, filp, info);
  1606. if (retval) {
  1607. #ifdef SERIAL_DEBUG_OPEN
  1608. printk("rs_open returning after block_til_ready with %d\n",
  1609. retval);
  1610. #endif
  1611. return retval;
  1612. }
  1613. #ifdef SERIAL_DEBUG_OPEN
  1614. printk("rs_open %s successful...", tty->name);
  1615. #endif
  1616. return 0;
  1617. }
  1618. /*
  1619. * /proc fs routines....
  1620. */
  1621. static inline int line_info(char *buf, struct serial_state *state)
  1622. {
  1623. struct async_struct *info = state->info, scr_info;
  1624. char stat_buf[30], control, status;
  1625. int ret;
  1626. unsigned long flags;
  1627. ret = sprintf(buf, "%d: uart:amiga_builtin",state->line);
  1628. /*
  1629. * Figure out the current RS-232 lines
  1630. */
  1631. if (!info) {
  1632. info = &scr_info; /* This is just for serial_{in,out} */
  1633. info->magic = SERIAL_MAGIC;
  1634. info->flags = state->flags;
  1635. info->quot = 0;
  1636. info->tty = NULL;
  1637. }
  1638. local_irq_save(flags);
  1639. status = ciab.pra;
  1640. control = info ? info->MCR : status;
  1641. local_irq_restore(flags);
  1642. stat_buf[0] = 0;
  1643. stat_buf[1] = 0;
  1644. if(!(control & SER_RTS))
  1645. strcat(stat_buf, "|RTS");
  1646. if(!(status & SER_CTS))
  1647. strcat(stat_buf, "|CTS");
  1648. if(!(control & SER_DTR))
  1649. strcat(stat_buf, "|DTR");
  1650. if(!(status & SER_DSR))
  1651. strcat(stat_buf, "|DSR");
  1652. if(!(status & SER_DCD))
  1653. strcat(stat_buf, "|CD");
  1654. if (info->quot) {
  1655. ret += sprintf(buf+ret, " baud:%d",
  1656. state->baud_base / info->quot);
  1657. }
  1658. ret += sprintf(buf+ret, " tx:%d rx:%d",
  1659. state->icount.tx, state->icount.rx);
  1660. if (state->icount.frame)
  1661. ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
  1662. if (state->icount.parity)
  1663. ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
  1664. if (state->icount.brk)
  1665. ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
  1666. if (state->icount.overrun)
  1667. ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
  1668. /*
  1669. * Last thing is the RS-232 status lines
  1670. */
  1671. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1672. return ret;
  1673. }
  1674. static int rs_read_proc(char *page, char **start, off_t off, int count,
  1675. int *eof, void *data)
  1676. {
  1677. int len = 0, l;
  1678. off_t begin = 0;
  1679. len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
  1680. l = line_info(page + len, &rs_table[0]);
  1681. len += l;
  1682. if (len+begin > off+count)
  1683. goto done;
  1684. if (len+begin < off) {
  1685. begin += len;
  1686. len = 0;
  1687. }
  1688. *eof = 1;
  1689. done:
  1690. if (off >= len+begin)
  1691. return 0;
  1692. *start = page + (off-begin);
  1693. return ((count < begin+len-off) ? count : begin+len-off);
  1694. }
  1695. /*
  1696. * ---------------------------------------------------------------------
  1697. * rs_init() and friends
  1698. *
  1699. * rs_init() is called at boot-time to initialize the serial driver.
  1700. * ---------------------------------------------------------------------
  1701. */
  1702. /*
  1703. * This routine prints out the appropriate serial driver version
  1704. * number, and identifies which options were configured into this
  1705. * driver.
  1706. */
  1707. static _INLINE_ void show_serial_version(void)
  1708. {
  1709. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1710. }
  1711. int register_serial(struct serial_struct *req);
  1712. void unregister_serial(int line);
  1713. static struct tty_operations serial_ops = {
  1714. .open = rs_open,
  1715. .close = rs_close,
  1716. .write = rs_write,
  1717. .put_char = rs_put_char,
  1718. .flush_chars = rs_flush_chars,
  1719. .write_room = rs_write_room,
  1720. .chars_in_buffer = rs_chars_in_buffer,
  1721. .flush_buffer = rs_flush_buffer,
  1722. .ioctl = rs_ioctl,
  1723. .throttle = rs_throttle,
  1724. .unthrottle = rs_unthrottle,
  1725. .set_termios = rs_set_termios,
  1726. .stop = rs_stop,
  1727. .start = rs_start,
  1728. .hangup = rs_hangup,
  1729. .break_ctl = rs_break,
  1730. .send_xchar = rs_send_xchar,
  1731. .wait_until_sent = rs_wait_until_sent,
  1732. .read_proc = rs_read_proc,
  1733. .tiocmget = rs_tiocmget,
  1734. .tiocmset = rs_tiocmset,
  1735. };
  1736. /*
  1737. * The serial driver boot-time initialization code!
  1738. */
  1739. static int __init rs_init(void)
  1740. {
  1741. unsigned long flags;
  1742. struct serial_state * state;
  1743. if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
  1744. return -ENODEV;
  1745. serial_driver = alloc_tty_driver(1);
  1746. if (!serial_driver)
  1747. return -ENOMEM;
  1748. /*
  1749. * We request SERDAT and SERPER only, because the serial registers are
  1750. * too spreaded over the custom register space
  1751. */
  1752. if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4, "amiserial [Paula]"))
  1753. return -EBUSY;
  1754. IRQ_ports = NULL;
  1755. show_serial_version();
  1756. /* Initialize the tty_driver structure */
  1757. serial_driver->owner = THIS_MODULE;
  1758. serial_driver->driver_name = "amiserial";
  1759. serial_driver->name = "ttyS";
  1760. serial_driver->major = TTY_MAJOR;
  1761. serial_driver->minor_start = 64;
  1762. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1763. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1764. serial_driver->init_termios = tty_std_termios;
  1765. serial_driver->init_termios.c_cflag =
  1766. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1767. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1768. tty_set_operations(serial_driver, &serial_ops);
  1769. if (tty_register_driver(serial_driver))
  1770. panic("Couldn't register serial driver\n");
  1771. state = rs_table;
  1772. state->magic = SSTATE_MAGIC;
  1773. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1774. state->line = 0;
  1775. state->custom_divisor = 0;
  1776. state->close_delay = 5*HZ/10;
  1777. state->closing_wait = 30*HZ;
  1778. state->icount.cts = state->icount.dsr =
  1779. state->icount.rng = state->icount.dcd = 0;
  1780. state->icount.rx = state->icount.tx = 0;
  1781. state->icount.frame = state->icount.parity = 0;
  1782. state->icount.overrun = state->icount.brk = 0;
  1783. /*
  1784. if(state->port && check_region(state->port,REGION_LENGTH(state)))
  1785. continue;
  1786. */
  1787. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1788. state->line);
  1789. /* Hardware set up */
  1790. state->baud_base = amiga_colorclock;
  1791. state->xmit_fifo_size = 1;
  1792. local_irq_save(flags);
  1793. /* set ISRs, and then disable the rx interrupts */
  1794. request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1795. request_irq(IRQ_AMIGA_RBF, ser_rx_int, SA_INTERRUPT, "serial RX", state);
  1796. /* turn off Rx and Tx interrupts */
  1797. custom.intena = IF_RBF | IF_TBE;
  1798. mb();
  1799. /* clear any pending interrupt */
  1800. custom.intreq = IF_RBF | IF_TBE;
  1801. mb();
  1802. local_irq_restore(flags);
  1803. /*
  1804. * set the appropriate directions for the modem control flags,
  1805. * and clear RTS and DTR
  1806. */
  1807. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1808. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1809. return 0;
  1810. }
  1811. static __exit void rs_exit(void)
  1812. {
  1813. int error;
  1814. struct async_struct *info = rs_table[0].info;
  1815. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1816. tasklet_kill(&info->tlet);
  1817. if ((error = tty_unregister_driver(serial_driver)))
  1818. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1819. error);
  1820. put_tty_driver(serial_driver);
  1821. if (info) {
  1822. rs_table[0].info = NULL;
  1823. kfree(info);
  1824. }
  1825. if (tmp_buf) {
  1826. free_page((unsigned long) tmp_buf);
  1827. tmp_buf = NULL;
  1828. }
  1829. release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
  1830. }
  1831. module_init(rs_init)
  1832. module_exit(rs_exit)
  1833. /*
  1834. * ------------------------------------------------------------
  1835. * Serial console driver
  1836. * ------------------------------------------------------------
  1837. */
  1838. #ifdef CONFIG_SERIAL_CONSOLE
  1839. static void amiga_serial_putc(char c)
  1840. {
  1841. custom.serdat = (unsigned char)c | 0x100;
  1842. while (!(custom.serdatr & 0x2000))
  1843. barrier();
  1844. }
  1845. /*
  1846. * Print a string to the serial port trying not to disturb
  1847. * any possible real use of the port...
  1848. *
  1849. * The console must be locked when we get here.
  1850. */
  1851. static void serial_console_write(struct console *co, const char *s,
  1852. unsigned count)
  1853. {
  1854. unsigned short intena = custom.intenar;
  1855. custom.intena = IF_TBE;
  1856. while (count--) {
  1857. if (*s == '\n')
  1858. amiga_serial_putc('\r');
  1859. amiga_serial_putc(*s++);
  1860. }
  1861. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1862. }
  1863. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1864. {
  1865. *index = 0;
  1866. return serial_driver;
  1867. }
  1868. static struct console sercons = {
  1869. .name = "ttyS",
  1870. .write = serial_console_write,
  1871. .device = serial_console_device,
  1872. .flags = CON_PRINTBUFFER,
  1873. .index = -1,
  1874. };
  1875. /*
  1876. * Register console.
  1877. */
  1878. static int __init amiserial_console_init(void)
  1879. {
  1880. register_console(&sercons);
  1881. return 0;
  1882. }
  1883. console_initcall(amiserial_console_init);
  1884. #endif
  1885. MODULE_LICENSE("GPL");