amiserial.c 53 KB

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