amiserial.c 54 KB

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