amiserial.c 50 KB

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