amiserial.c 53 KB

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