amiserial.c 53 KB

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