amiserial.c 50 KB

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