amiserial.c 52 KB

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