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