amiserial.c 46 KB

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