amiserial.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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->tport.tty;
  197. unsigned char ch, flag;
  198. struct async_icount *icount;
  199. int oe = 0;
  200. icount = &info->icount;
  201. status = UART_LSR_DR; /* We obviously have a character! */
  202. serdatr = custom.serdatr;
  203. mb();
  204. custom.intreq = IF_RBF;
  205. mb();
  206. if((serdatr & 0x1ff) == 0)
  207. status |= UART_LSR_BI;
  208. if(serdatr & SDR_OVRUN)
  209. status |= UART_LSR_OE;
  210. ch = serdatr & 0xff;
  211. icount->rx++;
  212. #ifdef SERIAL_DEBUG_INTR
  213. printk("DR%02x:%02x...", ch, status);
  214. #endif
  215. flag = TTY_NORMAL;
  216. /*
  217. * We don't handle parity or frame errors - but I have left
  218. * the code in, since I'm not sure that the errors can't be
  219. * detected.
  220. */
  221. if (status & (UART_LSR_BI | UART_LSR_PE |
  222. UART_LSR_FE | UART_LSR_OE)) {
  223. /*
  224. * For statistics only
  225. */
  226. if (status & UART_LSR_BI) {
  227. status &= ~(UART_LSR_FE | UART_LSR_PE);
  228. icount->brk++;
  229. } else if (status & UART_LSR_PE)
  230. icount->parity++;
  231. else if (status & UART_LSR_FE)
  232. icount->frame++;
  233. if (status & UART_LSR_OE)
  234. icount->overrun++;
  235. /*
  236. * Now check to see if character should be
  237. * ignored, and mask off conditions which
  238. * should be ignored.
  239. */
  240. if (status & info->ignore_status_mask)
  241. goto out;
  242. status &= info->read_status_mask;
  243. if (status & (UART_LSR_BI)) {
  244. #ifdef SERIAL_DEBUG_INTR
  245. printk("handling break....");
  246. #endif
  247. flag = TTY_BREAK;
  248. if (info->flags & ASYNC_SAK)
  249. do_SAK(tty);
  250. } else if (status & UART_LSR_PE)
  251. flag = TTY_PARITY;
  252. else if (status & UART_LSR_FE)
  253. flag = TTY_FRAME;
  254. if (status & UART_LSR_OE) {
  255. /*
  256. * Overrun is special, since it's
  257. * reported immediately, and doesn't
  258. * affect the current character
  259. */
  260. oe = 1;
  261. }
  262. }
  263. tty_insert_flip_char(tty, ch, flag);
  264. if (oe == 1)
  265. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  266. tty_flip_buffer_push(tty);
  267. out:
  268. return;
  269. }
  270. static void transmit_chars(struct serial_state *info)
  271. {
  272. custom.intreq = IF_TBE;
  273. mb();
  274. if (info->x_char) {
  275. custom.serdat = info->x_char | 0x100;
  276. mb();
  277. info->icount.tx++;
  278. info->x_char = 0;
  279. return;
  280. }
  281. if (info->xmit.head == info->xmit.tail
  282. || info->tport.tty->stopped
  283. || info->tport.tty->hw_stopped) {
  284. info->IER &= ~UART_IER_THRI;
  285. custom.intena = IF_TBE;
  286. mb();
  287. return;
  288. }
  289. custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
  290. mb();
  291. info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
  292. info->icount.tx++;
  293. if (CIRC_CNT(info->xmit.head,
  294. info->xmit.tail,
  295. SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
  296. tty_wakeup(info->tport.tty);
  297. #ifdef SERIAL_DEBUG_INTR
  298. printk("THRE...");
  299. #endif
  300. if (info->xmit.head == info->xmit.tail) {
  301. custom.intena = IF_TBE;
  302. mb();
  303. info->IER &= ~UART_IER_THRI;
  304. }
  305. }
  306. static void check_modem_status(struct serial_state *info)
  307. {
  308. unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
  309. unsigned char dstatus;
  310. struct async_icount *icount;
  311. /* Determine bits that have changed */
  312. dstatus = status ^ current_ctl_bits;
  313. current_ctl_bits = status;
  314. if (dstatus) {
  315. icount = &info->icount;
  316. /* update input line counters */
  317. if (dstatus & SER_DSR)
  318. icount->dsr++;
  319. if (dstatus & SER_DCD) {
  320. icount->dcd++;
  321. #ifdef CONFIG_HARD_PPS
  322. if ((info->flags & ASYNC_HARDPPS_CD) &&
  323. !(status & SER_DCD))
  324. hardpps();
  325. #endif
  326. }
  327. if (dstatus & SER_CTS)
  328. icount->cts++;
  329. wake_up_interruptible(&info->tport.delta_msr_wait);
  330. }
  331. if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
  332. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  333. printk("ttyS%d CD now %s...", info->line,
  334. (!(status & SER_DCD)) ? "on" : "off");
  335. #endif
  336. if (!(status & SER_DCD))
  337. wake_up_interruptible(&info->tport.open_wait);
  338. else {
  339. #ifdef SERIAL_DEBUG_OPEN
  340. printk("doing serial hangup...");
  341. #endif
  342. if (info->tport.tty)
  343. tty_hangup(info->tport.tty);
  344. }
  345. }
  346. if (info->flags & ASYNC_CTS_FLOW) {
  347. if (info->tport.tty->hw_stopped) {
  348. if (!(status & SER_CTS)) {
  349. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  350. printk("CTS tx start...");
  351. #endif
  352. info->tport.tty->hw_stopped = 0;
  353. info->IER |= UART_IER_THRI;
  354. custom.intena = IF_SETCLR | IF_TBE;
  355. mb();
  356. /* set a pending Tx Interrupt, transmitter should restart now */
  357. custom.intreq = IF_SETCLR | IF_TBE;
  358. mb();
  359. tty_wakeup(info->tport.tty);
  360. return;
  361. }
  362. } else {
  363. if ((status & SER_CTS)) {
  364. #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
  365. printk("CTS tx stop...");
  366. #endif
  367. info->tport.tty->hw_stopped = 1;
  368. info->IER &= ~UART_IER_THRI;
  369. /* disable Tx interrupt and remove any pending interrupts */
  370. custom.intena = IF_TBE;
  371. mb();
  372. custom.intreq = IF_TBE;
  373. mb();
  374. }
  375. }
  376. }
  377. }
  378. static irqreturn_t ser_vbl_int( int irq, void *data)
  379. {
  380. /* vbl is just a periodic interrupt we tie into to update modem status */
  381. struct serial_state *info = data;
  382. /*
  383. * TBD - is it better to unregister from this interrupt or to
  384. * ignore it if MSI is clear ?
  385. */
  386. if(info->IER & UART_IER_MSI)
  387. check_modem_status(info);
  388. return IRQ_HANDLED;
  389. }
  390. static irqreturn_t ser_rx_int(int irq, void *dev_id)
  391. {
  392. struct serial_state *info = dev_id;
  393. #ifdef SERIAL_DEBUG_INTR
  394. printk("ser_rx_int...");
  395. #endif
  396. if (!info->tport.tty)
  397. return IRQ_NONE;
  398. receive_chars(info);
  399. #ifdef SERIAL_DEBUG_INTR
  400. printk("end.\n");
  401. #endif
  402. return IRQ_HANDLED;
  403. }
  404. static irqreturn_t ser_tx_int(int irq, void *dev_id)
  405. {
  406. struct serial_state *info = dev_id;
  407. if (custom.serdatr & SDR_TBE) {
  408. #ifdef SERIAL_DEBUG_INTR
  409. printk("ser_tx_int...");
  410. #endif
  411. if (!info->tport.tty)
  412. return IRQ_NONE;
  413. transmit_chars(info);
  414. #ifdef SERIAL_DEBUG_INTR
  415. printk("end.\n");
  416. #endif
  417. }
  418. return IRQ_HANDLED;
  419. }
  420. /*
  421. * -------------------------------------------------------------------
  422. * Here ends the serial interrupt routines.
  423. * -------------------------------------------------------------------
  424. */
  425. /*
  426. * ---------------------------------------------------------------
  427. * Low level utility subroutines for the serial driver: routines to
  428. * figure out the appropriate timeout for an interrupt chain, routines
  429. * to initialize and startup a serial port, and routines to shutdown a
  430. * serial port. Useful stuff like that.
  431. * ---------------------------------------------------------------
  432. */
  433. static int startup(struct tty_struct *tty, struct serial_state *info)
  434. {
  435. 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->tport.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->tport.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->tport.tty = NULL;
  1245. if (state->tport.blocked_open) {
  1246. if (state->close_delay) {
  1247. msleep_interruptible(jiffies_to_msecs(state->close_delay));
  1248. }
  1249. wake_up_interruptible(&state->tport.open_wait);
  1250. }
  1251. state->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1252. wake_up_interruptible(&state->tport.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->tport.tty = NULL;
  1325. wake_up_interruptible(&info->tport.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->tport.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->tport.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->tport.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->tport.open_wait, &wait);
  1424. if (extra_count)
  1425. info->count++;
  1426. info->tport.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->tport.tty = tty;
  1448. tty->driver_data = info;
  1449. tty->port = &info->tport;
  1450. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1451. return -ENODEV;
  1452. #ifdef SERIAL_DEBUG_OPEN
  1453. printk("rs_open %s, count = %d\n", tty->name, info->count);
  1454. #endif
  1455. tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  1456. /*
  1457. * If the port is the middle of closing, bail out now
  1458. */
  1459. if (tty_hung_up_p(filp) ||
  1460. (info->flags & ASYNC_CLOSING)) {
  1461. if (info->flags & ASYNC_CLOSING)
  1462. interruptible_sleep_on(&info->tport.close_wait);
  1463. #ifdef SERIAL_DO_RESTART
  1464. return ((info->flags & ASYNC_HUP_NOTIFY) ?
  1465. -EAGAIN : -ERESTARTSYS);
  1466. #else
  1467. return -EAGAIN;
  1468. #endif
  1469. }
  1470. /*
  1471. * Start up serial port
  1472. */
  1473. retval = startup(tty, info);
  1474. if (retval) {
  1475. return retval;
  1476. }
  1477. retval = block_til_ready(tty, filp, info);
  1478. if (retval) {
  1479. #ifdef SERIAL_DEBUG_OPEN
  1480. printk("rs_open returning after block_til_ready with %d\n",
  1481. retval);
  1482. #endif
  1483. return retval;
  1484. }
  1485. #ifdef SERIAL_DEBUG_OPEN
  1486. printk("rs_open %s successful...", tty->name);
  1487. #endif
  1488. return 0;
  1489. }
  1490. /*
  1491. * /proc fs routines....
  1492. */
  1493. static inline void line_info(struct seq_file *m, struct serial_state *state)
  1494. {
  1495. char stat_buf[30], control, status;
  1496. unsigned long flags;
  1497. seq_printf(m, "%d: uart:amiga_builtin",state->line);
  1498. local_irq_save(flags);
  1499. status = ciab.pra;
  1500. control = (state->flags & ASYNC_INITIALIZED) ? state->MCR : status;
  1501. local_irq_restore(flags);
  1502. stat_buf[0] = 0;
  1503. stat_buf[1] = 0;
  1504. if(!(control & SER_RTS))
  1505. strcat(stat_buf, "|RTS");
  1506. if(!(status & SER_CTS))
  1507. strcat(stat_buf, "|CTS");
  1508. if(!(control & SER_DTR))
  1509. strcat(stat_buf, "|DTR");
  1510. if(!(status & SER_DSR))
  1511. strcat(stat_buf, "|DSR");
  1512. if(!(status & SER_DCD))
  1513. strcat(stat_buf, "|CD");
  1514. if (state->quot)
  1515. seq_printf(m, " baud:%d", state->baud_base / state->quot);
  1516. seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
  1517. if (state->icount.frame)
  1518. seq_printf(m, " fe:%d", state->icount.frame);
  1519. if (state->icount.parity)
  1520. seq_printf(m, " pe:%d", state->icount.parity);
  1521. if (state->icount.brk)
  1522. seq_printf(m, " brk:%d", state->icount.brk);
  1523. if (state->icount.overrun)
  1524. seq_printf(m, " oe:%d", state->icount.overrun);
  1525. /*
  1526. * Last thing is the RS-232 status lines
  1527. */
  1528. seq_printf(m, " %s\n", stat_buf+1);
  1529. }
  1530. static int rs_proc_show(struct seq_file *m, void *v)
  1531. {
  1532. seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
  1533. line_info(m, &rs_table[0]);
  1534. return 0;
  1535. }
  1536. static int rs_proc_open(struct inode *inode, struct file *file)
  1537. {
  1538. return single_open(file, rs_proc_show, NULL);
  1539. }
  1540. static const struct file_operations rs_proc_fops = {
  1541. .owner = THIS_MODULE,
  1542. .open = rs_proc_open,
  1543. .read = seq_read,
  1544. .llseek = seq_lseek,
  1545. .release = single_release,
  1546. };
  1547. /*
  1548. * ---------------------------------------------------------------------
  1549. * rs_init() and friends
  1550. *
  1551. * rs_init() is called at boot-time to initialize the serial driver.
  1552. * ---------------------------------------------------------------------
  1553. */
  1554. /*
  1555. * This routine prints out the appropriate serial driver version
  1556. * number, and identifies which options were configured into this
  1557. * driver.
  1558. */
  1559. static void show_serial_version(void)
  1560. {
  1561. printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
  1562. }
  1563. static const struct tty_operations serial_ops = {
  1564. .open = rs_open,
  1565. .close = rs_close,
  1566. .write = rs_write,
  1567. .put_char = rs_put_char,
  1568. .flush_chars = rs_flush_chars,
  1569. .write_room = rs_write_room,
  1570. .chars_in_buffer = rs_chars_in_buffer,
  1571. .flush_buffer = rs_flush_buffer,
  1572. .ioctl = rs_ioctl,
  1573. .throttle = rs_throttle,
  1574. .unthrottle = rs_unthrottle,
  1575. .set_termios = rs_set_termios,
  1576. .stop = rs_stop,
  1577. .start = rs_start,
  1578. .hangup = rs_hangup,
  1579. .break_ctl = rs_break,
  1580. .send_xchar = rs_send_xchar,
  1581. .wait_until_sent = rs_wait_until_sent,
  1582. .tiocmget = rs_tiocmget,
  1583. .tiocmset = rs_tiocmset,
  1584. .get_icount = rs_get_icount,
  1585. .proc_fops = &rs_proc_fops,
  1586. };
  1587. /*
  1588. * The serial driver boot-time initialization code!
  1589. */
  1590. static int __init amiga_serial_probe(struct platform_device *pdev)
  1591. {
  1592. unsigned long flags;
  1593. struct serial_state * state;
  1594. int error;
  1595. serial_driver = alloc_tty_driver(NR_PORTS);
  1596. if (!serial_driver)
  1597. return -ENOMEM;
  1598. show_serial_version();
  1599. /* Initialize the tty_driver structure */
  1600. serial_driver->driver_name = "amiserial";
  1601. serial_driver->name = "ttyS";
  1602. serial_driver->major = TTY_MAJOR;
  1603. serial_driver->minor_start = 64;
  1604. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1605. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1606. serial_driver->init_termios = tty_std_termios;
  1607. serial_driver->init_termios.c_cflag =
  1608. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1609. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1610. tty_set_operations(serial_driver, &serial_ops);
  1611. error = tty_register_driver(serial_driver);
  1612. if (error)
  1613. goto fail_put_tty_driver;
  1614. state = rs_table;
  1615. state->port = (int)&custom.serdatr; /* Just to give it a value */
  1616. state->line = 0;
  1617. state->custom_divisor = 0;
  1618. state->close_delay = 5*HZ/10;
  1619. state->closing_wait = 30*HZ;
  1620. state->icount.cts = state->icount.dsr =
  1621. state->icount.rng = state->icount.dcd = 0;
  1622. state->icount.rx = state->icount.tx = 0;
  1623. state->icount.frame = state->icount.parity = 0;
  1624. state->icount.overrun = state->icount.brk = 0;
  1625. tty_port_init(&state->tport);
  1626. printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
  1627. state->line);
  1628. /* Hardware set up */
  1629. state->baud_base = amiga_colorclock;
  1630. state->xmit_fifo_size = 1;
  1631. /* set ISRs, and then disable the rx interrupts */
  1632. error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
  1633. if (error)
  1634. goto fail_unregister;
  1635. error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, 0,
  1636. "serial RX", state);
  1637. if (error)
  1638. goto fail_free_irq;
  1639. local_irq_save(flags);
  1640. /* turn off Rx and Tx interrupts */
  1641. custom.intena = IF_RBF | IF_TBE;
  1642. mb();
  1643. /* clear any pending interrupt */
  1644. custom.intreq = IF_RBF | IF_TBE;
  1645. mb();
  1646. local_irq_restore(flags);
  1647. /*
  1648. * set the appropriate directions for the modem control flags,
  1649. * and clear RTS and DTR
  1650. */
  1651. ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
  1652. ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
  1653. platform_set_drvdata(pdev, state);
  1654. return 0;
  1655. fail_free_irq:
  1656. free_irq(IRQ_AMIGA_TBE, state);
  1657. fail_unregister:
  1658. tty_unregister_driver(serial_driver);
  1659. fail_put_tty_driver:
  1660. put_tty_driver(serial_driver);
  1661. return error;
  1662. }
  1663. static int __exit amiga_serial_remove(struct platform_device *pdev)
  1664. {
  1665. int error;
  1666. struct serial_state *state = platform_get_drvdata(pdev);
  1667. /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
  1668. if ((error = tty_unregister_driver(serial_driver)))
  1669. printk("SERIAL: failed to unregister serial driver (%d)\n",
  1670. error);
  1671. put_tty_driver(serial_driver);
  1672. free_irq(IRQ_AMIGA_TBE, state);
  1673. free_irq(IRQ_AMIGA_RBF, state);
  1674. platform_set_drvdata(pdev, NULL);
  1675. return error;
  1676. }
  1677. static struct platform_driver amiga_serial_driver = {
  1678. .remove = __exit_p(amiga_serial_remove),
  1679. .driver = {
  1680. .name = "amiga-serial",
  1681. .owner = THIS_MODULE,
  1682. },
  1683. };
  1684. static int __init amiga_serial_init(void)
  1685. {
  1686. return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
  1687. }
  1688. module_init(amiga_serial_init);
  1689. static void __exit amiga_serial_exit(void)
  1690. {
  1691. platform_driver_unregister(&amiga_serial_driver);
  1692. }
  1693. module_exit(amiga_serial_exit);
  1694. #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
  1695. /*
  1696. * ------------------------------------------------------------
  1697. * Serial console driver
  1698. * ------------------------------------------------------------
  1699. */
  1700. static void amiga_serial_putc(char c)
  1701. {
  1702. custom.serdat = (unsigned char)c | 0x100;
  1703. while (!(custom.serdatr & 0x2000))
  1704. barrier();
  1705. }
  1706. /*
  1707. * Print a string to the serial port trying not to disturb
  1708. * any possible real use of the port...
  1709. *
  1710. * The console must be locked when we get here.
  1711. */
  1712. static void serial_console_write(struct console *co, const char *s,
  1713. unsigned count)
  1714. {
  1715. unsigned short intena = custom.intenar;
  1716. custom.intena = IF_TBE;
  1717. while (count--) {
  1718. if (*s == '\n')
  1719. amiga_serial_putc('\r');
  1720. amiga_serial_putc(*s++);
  1721. }
  1722. custom.intena = IF_SETCLR | (intena & IF_TBE);
  1723. }
  1724. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1725. {
  1726. *index = 0;
  1727. return serial_driver;
  1728. }
  1729. static struct console sercons = {
  1730. .name = "ttyS",
  1731. .write = serial_console_write,
  1732. .device = serial_console_device,
  1733. .flags = CON_PRINTBUFFER,
  1734. .index = -1,
  1735. };
  1736. /*
  1737. * Register console.
  1738. */
  1739. static int __init amiserial_console_init(void)
  1740. {
  1741. register_console(&sercons);
  1742. return 0;
  1743. }
  1744. console_initcall(amiserial_console_init);
  1745. #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
  1746. MODULE_LICENSE("GPL");
  1747. MODULE_ALIAS("platform:amiga-serial");