ser_a2232.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* drivers/char/ser_a2232.c */
  2. /* $Id: ser_a2232.c,v 0.4 2000/01/25 12:00:00 ehaase Exp $ */
  3. /* Linux serial driver for the Amiga A2232 board */
  4. /* This driver is MAINTAINED. Before applying any changes, please contact
  5. * the author.
  6. */
  7. /* Copyright (c) 2000-2001 Enver Haase <ehaase@inf.fu-berlin.de>
  8. * alias The A2232 driver project <A2232@gmx.net>
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. /***************************** Documentation ************************/
  27. /*
  28. * This driver is in EXPERIMENTAL state. That means I could not find
  29. * someone with five A2232 boards with 35 ports running at 19200 bps
  30. * at the same time and test the machine's behaviour.
  31. * However, I know that you can performance-tweak this driver (see
  32. * the source code).
  33. * One thing to consider is the time this driver consumes during the
  34. * Amiga's vertical blank interrupt. Everything that is to be done
  35. * _IS DONE_ when entering the vertical blank interrupt handler of
  36. * this driver.
  37. * However, it would be more sane to only do the job for only ONE card
  38. * instead of ALL cards at a time; or, more generally, to handle only
  39. * SOME ports instead of ALL ports at a time.
  40. * However, as long as no-one runs into problems I guess I shouldn't
  41. * change the driver as it runs fine for me :) .
  42. *
  43. * Version history of this file:
  44. * 0.4 Resolved licensing issues.
  45. * 0.3 Inclusion in the Linux/m68k tree, small fixes.
  46. * 0.2 Added documentation, minor typo fixes.
  47. * 0.1 Initial release.
  48. *
  49. * TO DO:
  50. * - Handle incoming BREAK events. I guess "Stevens: Advanced
  51. * Programming in the UNIX(R) Environment" is a good reference
  52. * on what is to be done.
  53. * - When installing as a module, don't simply 'printk' text, but
  54. * send it to the TTY used by the user.
  55. *
  56. * THANKS TO:
  57. * - Jukka Marin (65EC02 code).
  58. * - The other NetBSD developers on whose A2232 driver I had a
  59. * pretty close look. However, I didn't copy any code so it
  60. * is okay to put my code under the GPL and include it into
  61. * Linux.
  62. */
  63. /***************************** End of Documentation *****************/
  64. /***************************** Defines ******************************/
  65. /*
  66. * Enables experimental 115200 (normal) 230400 (turbo) baud rate.
  67. * The A2232 specification states it can only operate at speeds up to
  68. * 19200 bits per second, and I was not able to send a file via
  69. * "sz"/"rz" and a null-modem cable from one A2232 port to another
  70. * at 115200 bits per second.
  71. * However, this might work for you.
  72. */
  73. #undef A2232_SPEEDHACK
  74. /*
  75. * Default is not to use RTS/CTS so you could be talked to death.
  76. */
  77. #define A2232_SUPPRESS_RTSCTS_WARNING
  78. /************************* End of Defines ***************************/
  79. /***************************** Includes *****************************/
  80. #include <linux/module.h>
  81. #include <linux/types.h>
  82. #include <linux/sched.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/kernel.h>
  85. #include <linux/errno.h>
  86. #include <linux/tty.h>
  87. #include <asm/setup.h>
  88. #include <asm/amigaints.h>
  89. #include <asm/amigahw.h>
  90. #include <linux/zorro.h>
  91. #include <asm/irq.h>
  92. #include <asm/semaphore.h>
  93. #include <linux/delay.h>
  94. #include <linux/serial.h>
  95. #include <linux/generic_serial.h>
  96. #include "ser_a2232.h"
  97. #include "ser_a2232fw.h"
  98. /************************* End of Includes **************************/
  99. /***************************** Prototypes ***************************/
  100. /* The interrupt service routine */
  101. static irqreturn_t a2232_vbl_inter(int irq, void *data, struct pt_regs *fp);
  102. /* Initialize the port structures */
  103. static void a2232_init_portstructs(void);
  104. /* Initialize and register TTY drivers. */
  105. /* returns 0 IFF successful */
  106. static int a2232_init_drivers(void);
  107. /* BEGIN GENERIC_SERIAL PROTOTYPES */
  108. static void a2232_disable_tx_interrupts(void *ptr);
  109. static void a2232_enable_tx_interrupts(void *ptr);
  110. static void a2232_disable_rx_interrupts(void *ptr);
  111. static void a2232_enable_rx_interrupts(void *ptr);
  112. static int a2232_get_CD(void *ptr);
  113. static void a2232_shutdown_port(void *ptr);
  114. static int a2232_set_real_termios(void *ptr);
  115. static int a2232_chars_in_buffer(void *ptr);
  116. static void a2232_close(void *ptr);
  117. static void a2232_hungup(void *ptr);
  118. /* static void a2232_getserial (void *ptr, struct serial_struct *sp); */
  119. /* END GENERIC_SERIAL PROTOTYPES */
  120. /* Functions that the TTY driver struct expects */
  121. static int a2232_ioctl(struct tty_struct *tty, struct file *file,
  122. unsigned int cmd, unsigned long arg);
  123. static void a2232_throttle(struct tty_struct *tty);
  124. static void a2232_unthrottle(struct tty_struct *tty);
  125. static int a2232_open(struct tty_struct * tty, struct file * filp);
  126. /************************* End of Prototypes ************************/
  127. /***************************** Global variables *********************/
  128. /*---------------------------------------------------------------------------
  129. * Interface from generic_serial.c back here
  130. *--------------------------------------------------------------------------*/
  131. static struct real_driver a2232_real_driver = {
  132. a2232_disable_tx_interrupts,
  133. a2232_enable_tx_interrupts,
  134. a2232_disable_rx_interrupts,
  135. a2232_enable_rx_interrupts,
  136. a2232_get_CD,
  137. a2232_shutdown_port,
  138. a2232_set_real_termios,
  139. a2232_chars_in_buffer,
  140. a2232_close,
  141. a2232_hungup,
  142. NULL /* a2232_getserial */
  143. };
  144. static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own.
  145. /* Ports structs */
  146. static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
  147. /* TTY driver structs */
  148. static struct tty_driver *a2232_driver;
  149. /* nr of cards completely (all ports) and correctly configured */
  150. static int nr_a2232;
  151. /* zorro_dev structs for the A2232's */
  152. static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS];
  153. /***************************** End of Global variables **************/
  154. /* Helper functions */
  155. static inline volatile struct a2232memory *a2232mem(unsigned int board)
  156. {
  157. return (volatile struct a2232memory *)ZTWO_VADDR(zd_a2232[board]->resource.start);
  158. }
  159. static inline volatile struct a2232status *a2232stat(unsigned int board,
  160. unsigned int portonboard)
  161. {
  162. volatile struct a2232memory *mem = a2232mem(board);
  163. return &(mem->Status[portonboard]);
  164. }
  165. static inline void a2232_receive_char(struct a2232_port *port, int ch, int err)
  166. {
  167. /* Mostly stolen from other drivers.
  168. Maybe one could implement a more efficient version by not only
  169. transferring one character at a time.
  170. */
  171. struct tty_struct *tty = port->gs.tty;
  172. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  173. return;
  174. tty->flip.count++;
  175. #if 0
  176. switch(err) {
  177. case TTY_BREAK:
  178. break;
  179. case TTY_PARITY:
  180. break;
  181. case TTY_OVERRUN:
  182. break;
  183. case TTY_FRAME:
  184. break;
  185. }
  186. #endif
  187. *tty->flip.flag_buf_ptr++ = err;
  188. *tty->flip.char_buf_ptr++ = ch;
  189. tty_flip_buffer_push(tty);
  190. }
  191. /***************************** Functions ****************************/
  192. /*** BEGIN OF REAL_DRIVER FUNCTIONS ***/
  193. static void a2232_disable_tx_interrupts(void *ptr)
  194. {
  195. struct a2232_port *port;
  196. volatile struct a2232status *stat;
  197. unsigned long flags;
  198. port = ptr;
  199. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  200. stat->OutDisable = -1;
  201. /* Does this here really have to be? */
  202. local_irq_save(flags);
  203. port->gs.flags &= ~GS_TX_INTEN;
  204. local_irq_restore(flags);
  205. }
  206. static void a2232_enable_tx_interrupts(void *ptr)
  207. {
  208. struct a2232_port *port;
  209. volatile struct a2232status *stat;
  210. unsigned long flags;
  211. port = ptr;
  212. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  213. stat->OutDisable = 0;
  214. /* Does this here really have to be? */
  215. local_irq_save(flags);
  216. port->gs.flags |= GS_TX_INTEN;
  217. local_irq_restore(flags);
  218. }
  219. static void a2232_disable_rx_interrupts(void *ptr)
  220. {
  221. struct a2232_port *port;
  222. port = ptr;
  223. port->disable_rx = -1;
  224. }
  225. static void a2232_enable_rx_interrupts(void *ptr)
  226. {
  227. struct a2232_port *port;
  228. port = ptr;
  229. port->disable_rx = 0;
  230. }
  231. static int a2232_get_CD(void *ptr)
  232. {
  233. return ((struct a2232_port *) ptr)->cd_status;
  234. }
  235. static void a2232_shutdown_port(void *ptr)
  236. {
  237. struct a2232_port *port;
  238. volatile struct a2232status *stat;
  239. unsigned long flags;
  240. port = ptr;
  241. stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
  242. local_irq_save(flags);
  243. port->gs.flags &= ~GS_ACTIVE;
  244. if (port->gs.tty && port->gs.tty->termios->c_cflag & HUPCL) {
  245. /* Set DTR and RTS to Low, flush output.
  246. The NetBSD driver "msc.c" does it this way. */
  247. stat->Command = ( (stat->Command & ~A2232CMD_CMask) |
  248. A2232CMD_Close );
  249. stat->OutFlush = -1;
  250. stat->Setup = -1;
  251. }
  252. local_irq_restore(flags);
  253. /* After analyzing control flow, I think a2232_shutdown_port
  254. is actually the last call from the system when at application
  255. level someone issues a "echo Hello >>/dev/ttyY0".
  256. Therefore I think the MOD_DEC_USE_COUNT should be here and
  257. not in "a2232_close()". See the comment in "sx.c", too.
  258. If you run into problems, compile this driver into the
  259. kernel instead of compiling it as a module. */
  260. }
  261. static int a2232_set_real_termios(void *ptr)
  262. {
  263. unsigned int cflag, baud, chsize, stopb, parity, softflow;
  264. int rate;
  265. int a2232_param, a2232_cmd;
  266. unsigned long flags;
  267. unsigned int i;
  268. struct a2232_port *port = ptr;
  269. volatile struct a2232status *status;
  270. volatile struct a2232memory *mem;
  271. if (!port->gs.tty || !port->gs.tty->termios) return 0;
  272. status = a2232stat(port->which_a2232, port->which_port_on_a2232);
  273. mem = a2232mem(port->which_a2232);
  274. a2232_param = a2232_cmd = 0;
  275. // get baud rate
  276. baud = port->gs.baud;
  277. if (baud == 0) {
  278. /* speed == 0 -> drop DTR, do nothing else */
  279. local_irq_save(flags);
  280. // Clear DTR (and RTS... mhhh).
  281. status->Command = ( (status->Command & ~A2232CMD_CMask) |
  282. A2232CMD_Close );
  283. status->OutFlush = -1;
  284. status->Setup = -1;
  285. local_irq_restore(flags);
  286. return 0;
  287. }
  288. rate = A2232_BAUD_TABLE_NOAVAIL;
  289. for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
  290. if (a2232_baud_table[i] == baud){
  291. if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
  292. else rate = a2232_baud_table[i+1];
  293. }
  294. }
  295. if (rate == A2232_BAUD_TABLE_NOAVAIL){
  296. printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
  297. // This is useful for both (turbo or normal) Crystal versions.
  298. rate = A2232PARAM_B9600;
  299. }
  300. a2232_param |= rate;
  301. cflag = port->gs.tty->termios->c_cflag;
  302. // get character size
  303. chsize = cflag & CSIZE;
  304. switch (chsize){
  305. case CS8: a2232_param |= A2232PARAM_8Bit; break;
  306. case CS7: a2232_param |= A2232PARAM_7Bit; break;
  307. case CS6: a2232_param |= A2232PARAM_6Bit; break;
  308. case CS5: a2232_param |= A2232PARAM_5Bit; break;
  309. default: printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
  310. port->which_a2232,port->which_port_on_a2232,chsize);
  311. a2232_param |= A2232PARAM_8Bit; break;
  312. }
  313. // get number of stop bits
  314. stopb = cflag & CSTOPB;
  315. if (stopb){ // two stop bits instead of one
  316. printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
  317. port->which_a2232,port->which_port_on_a2232);
  318. }
  319. // Warn if RTS/CTS not wanted
  320. if (!(cflag & CRTSCTS)){
  321. #ifndef A2232_SUPPRESS_RTSCTS_WARNING
  322. printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
  323. port->which_a2232,port->which_port_on_a2232);
  324. #endif
  325. }
  326. /* I think this is correct.
  327. However, IXOFF means _input_ flow control and I wonder
  328. if one should care about IXON _output_ flow control,
  329. too. If this makes problems, one should turn the A2232
  330. firmware XON/XOFF "SoftFlow" flow control off and use
  331. the conventional way of inserting START/STOP characters
  332. by hand in throttle()/unthrottle().
  333. */
  334. softflow = !!( port->gs.tty->termios->c_iflag & IXOFF );
  335. // get Parity (Enabled/Disabled? If Enabled, Odd or Even?)
  336. parity = cflag & (PARENB | PARODD);
  337. if (parity & PARENB){
  338. if (parity & PARODD){
  339. a2232_cmd |= A2232CMD_OddParity;
  340. }
  341. else{
  342. a2232_cmd |= A2232CMD_EvenParity;
  343. }
  344. }
  345. else a2232_cmd |= A2232CMD_NoParity;
  346. /* Hmm. Maybe an own a2232_port structure
  347. member would be cleaner? */
  348. if (cflag & CLOCAL)
  349. port->gs.flags &= ~ASYNC_CHECK_CD;
  350. else
  351. port->gs.flags |= ASYNC_CHECK_CD;
  352. /* Now we have all parameters and can go to set them: */
  353. local_irq_save(flags);
  354. status->Param = a2232_param | A2232PARAM_RcvBaud;
  355. status->Command = a2232_cmd | A2232CMD_Open | A2232CMD_Enable;
  356. status->SoftFlow = softflow;
  357. status->OutDisable = 0;
  358. status->Setup = -1;
  359. local_irq_restore(flags);
  360. return 0;
  361. }
  362. static int a2232_chars_in_buffer(void *ptr)
  363. {
  364. struct a2232_port *port;
  365. volatile struct a2232status *status;
  366. unsigned char ret; /* we need modulo-256 arithmetics */
  367. port = ptr;
  368. status = a2232stat(port->which_a2232, port->which_port_on_a2232);
  369. #if A2232_IOBUFLEN != 256
  370. #error "Re-Implement a2232_chars_in_buffer()!"
  371. #endif
  372. ret = (status->OutHead - status->OutTail);
  373. return ret;
  374. }
  375. static void a2232_close(void *ptr)
  376. {
  377. a2232_disable_tx_interrupts(ptr);
  378. a2232_disable_rx_interrupts(ptr);
  379. /* see the comment in a2232_shutdown_port above. */
  380. }
  381. static void a2232_hungup(void *ptr)
  382. {
  383. a2232_close(ptr);
  384. }
  385. /*** END OF REAL_DRIVER FUNCTIONS ***/
  386. /*** BEGIN FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
  387. static int a2232_ioctl( struct tty_struct *tty, struct file *file,
  388. unsigned int cmd, unsigned long arg)
  389. {
  390. return -ENOIOCTLCMD;
  391. }
  392. static void a2232_throttle(struct tty_struct *tty)
  393. {
  394. /* Throttle: System cannot take another chars: Drop RTS or
  395. send the STOP char or whatever.
  396. The A2232 firmware does RTS/CTS anyway, and XON/XOFF
  397. if switched on. So the only thing we can do at this
  398. layer here is not taking any characters out of the
  399. A2232 buffer any more. */
  400. struct a2232_port *port = (struct a2232_port *) tty->driver_data;
  401. port->throttle_input = -1;
  402. }
  403. static void a2232_unthrottle(struct tty_struct *tty)
  404. {
  405. /* Unthrottle: dual to "throttle()" above. */
  406. struct a2232_port *port = (struct a2232_port *) tty->driver_data;
  407. port->throttle_input = 0;
  408. }
  409. static int a2232_open(struct tty_struct * tty, struct file * filp)
  410. {
  411. /* More or less stolen from other drivers. */
  412. int line;
  413. int retval;
  414. struct a2232_port *port;
  415. line = tty->index;
  416. port = &a2232_ports[line];
  417. tty->driver_data = port;
  418. port->gs.tty = tty;
  419. port->gs.count++;
  420. retval = gs_init_port(&port->gs);
  421. if (retval) {
  422. port->gs.count--;
  423. return retval;
  424. }
  425. port->gs.flags |= GS_ACTIVE;
  426. retval = gs_block_til_ready(port, filp);
  427. if (retval) {
  428. port->gs.count--;
  429. return retval;
  430. }
  431. a2232_enable_rx_interrupts(port);
  432. return 0;
  433. }
  434. /*** END OF FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
  435. static irqreturn_t a2232_vbl_inter(int irq, void *data, struct pt_regs *fp)
  436. {
  437. #if A2232_IOBUFLEN != 256
  438. #error "Re-Implement a2232_vbl_inter()!"
  439. #endif
  440. struct a2232_port *port;
  441. volatile struct a2232memory *mem;
  442. volatile struct a2232status *status;
  443. unsigned char newhead;
  444. unsigned char bufpos; /* Must be unsigned char. We need the modulo-256 arithmetics */
  445. unsigned char ncd, ocd, ccd; /* names consistent with the NetBSD driver */
  446. volatile u_char *ibuf, *cbuf, *obuf;
  447. int ch, err, n, p;
  448. for (n = 0; n < nr_a2232; n++){ /* for every completely initialized A2232 board */
  449. mem = a2232mem(n);
  450. for (p = 0; p < NUMLINES; p++){ /* for every port on this board */
  451. err = 0;
  452. port = &a2232_ports[n*NUMLINES+p];
  453. if ( port->gs.flags & GS_ACTIVE ){ /* if the port is used */
  454. status = a2232stat(n,p);
  455. if (!port->disable_rx && !port->throttle_input){ /* If input is not disabled */
  456. newhead = status->InHead; /* 65EC02 write pointer */
  457. bufpos = status->InTail;
  458. /* check for input for this port */
  459. if (newhead != bufpos) {
  460. /* buffer for input chars/events */
  461. ibuf = mem->InBuf[p];
  462. /* data types of bytes in ibuf */
  463. cbuf = mem->InCtl[p];
  464. /* do for all chars */
  465. while (bufpos != newhead) {
  466. /* which type of input data? */
  467. switch (cbuf[bufpos]) {
  468. /* switch on input event (CD, BREAK, etc.) */
  469. case A2232INCTL_EVENT:
  470. switch (ibuf[bufpos++]) {
  471. case A2232EVENT_Break:
  472. /* TODO: Handle BREAK signal */
  473. break;
  474. /* A2232EVENT_CarrierOn and A2232EVENT_CarrierOff are
  475. handled in a separate queue and should not occur here. */
  476. case A2232EVENT_Sync:
  477. printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
  478. break;
  479. default:
  480. printk("A2232: 65EC02 software broken, unknown event type %d occurred.\n",ibuf[bufpos-1]);
  481. } /* event type switch */
  482. break;
  483. case A2232INCTL_CHAR:
  484. /* Receive incoming char */
  485. a2232_receive_char(port, ibuf[bufpos], err);
  486. bufpos++;
  487. break;
  488. default:
  489. printk("A2232: 65EC02 software broken, unknown data type %d occurred.\n",cbuf[bufpos]);
  490. bufpos++;
  491. } /* switch on input data type */
  492. } /* while there's something in the buffer */
  493. status->InTail = bufpos; /* tell 65EC02 what we've read */
  494. } /* if there was something in the buffer */
  495. } /* If input is not disabled */
  496. /* Now check if there's something to output */
  497. obuf = mem->OutBuf[p];
  498. bufpos = status->OutHead;
  499. while ( (port->gs.xmit_cnt > 0) &&
  500. (!port->gs.tty->stopped) &&
  501. (!port->gs.tty->hw_stopped) ){ /* While there are chars to transmit */
  502. if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) { /* If the A2232 buffer is not full */
  503. ch = port->gs.xmit_buf[port->gs.xmit_tail]; /* get the next char to transmit */
  504. port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1); /* modulo-addition for the gs.xmit_buf ring-buffer */
  505. obuf[bufpos++] = ch; /* put it into the A2232 buffer */
  506. port->gs.xmit_cnt--;
  507. }
  508. else{ /* If A2232 the buffer is full */
  509. break; /* simply stop filling it. */
  510. }
  511. }
  512. status->OutHead = bufpos;
  513. /* WakeUp if output buffer runs low */
  514. if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.tty) {
  515. tty_wakeup(port->gs.tty);
  516. }
  517. } // if the port is used
  518. } // for every port on the board
  519. /* Now check the CD message queue */
  520. newhead = mem->Common.CDHead;
  521. bufpos = mem->Common.CDTail;
  522. if (newhead != bufpos){ /* There are CD events in queue */
  523. ocd = mem->Common.CDStatus; /* get old status bits */
  524. while (newhead != bufpos){ /* read all events */
  525. ncd = mem->CDBuf[bufpos++]; /* get one event */
  526. ccd = ncd ^ ocd; /* mask of changed lines */
  527. ocd = ncd; /* save new status bits */
  528. for(p=0; p < NUMLINES; p++){ /* for all ports */
  529. if (ccd & 1){ /* this one changed */
  530. struct a2232_port *port = &a2232_ports[n*7+p];
  531. port->cd_status = !(ncd & 1); /* ncd&1 <=> CD is now off */
  532. if (!(port->gs.flags & ASYNC_CHECK_CD))
  533. ; /* Don't report DCD changes */
  534. else if (port->cd_status) { // if DCD on: DCD went UP!
  535. /* Are we blocking in open?*/
  536. wake_up_interruptible(&port->gs.open_wait);
  537. }
  538. else { // if DCD off: DCD went DOWN!
  539. if (port->gs.tty)
  540. tty_hangup (port->gs.tty);
  541. }
  542. } // if CD changed for this port
  543. ccd >>= 1;
  544. ncd >>= 1; /* Shift bits for next line */
  545. } // for every port
  546. } // while CD events in queue
  547. mem->Common.CDStatus = ocd; /* save new status */
  548. mem->Common.CDTail = bufpos; /* remove events */
  549. } // if events in CD queue
  550. } // for every completely initialized A2232 board
  551. return IRQ_HANDLED;
  552. }
  553. static void a2232_init_portstructs(void)
  554. {
  555. struct a2232_port *port;
  556. int i;
  557. for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
  558. port = a2232_ports + i;
  559. port->which_a2232 = i/NUMLINES;
  560. port->which_port_on_a2232 = i%NUMLINES;
  561. port->disable_rx = port->throttle_input = port->cd_status = 0;
  562. port->gs.magic = A2232_MAGIC;
  563. port->gs.close_delay = HZ/2;
  564. port->gs.closing_wait = 30 * HZ;
  565. port->gs.rd = &a2232_real_driver;
  566. #ifdef NEW_WRITE_LOCKING
  567. init_MUTEX(&(port->gs.port_write_sem));
  568. #endif
  569. init_waitqueue_head(&port->gs.open_wait);
  570. init_waitqueue_head(&port->gs.close_wait);
  571. }
  572. }
  573. static struct tty_operations a2232_ops = {
  574. .open = a2232_open,
  575. .close = gs_close,
  576. .write = gs_write,
  577. .put_char = gs_put_char,
  578. .flush_chars = gs_flush_chars,
  579. .write_room = gs_write_room,
  580. .chars_in_buffer = gs_chars_in_buffer,
  581. .flush_buffer = gs_flush_buffer,
  582. .ioctl = a2232_ioctl,
  583. .throttle = a2232_throttle,
  584. .unthrottle = a2232_unthrottle,
  585. .set_termios = gs_set_termios,
  586. .stop = gs_stop,
  587. .start = gs_start,
  588. .hangup = gs_hangup,
  589. };
  590. static int a2232_init_drivers(void)
  591. {
  592. int error;
  593. a2232_driver = alloc_tty_driver(NUMLINES * nr_a2232);
  594. if (!a2232_driver)
  595. return -ENOMEM;
  596. a2232_driver->owner = THIS_MODULE;
  597. a2232_driver->driver_name = "commodore_a2232";
  598. a2232_driver->name = "ttyY";
  599. a2232_driver->major = A2232_NORMAL_MAJOR;
  600. a2232_driver->type = TTY_DRIVER_TYPE_SERIAL;
  601. a2232_driver->subtype = SERIAL_TYPE_NORMAL;
  602. a2232_driver->init_termios = tty_std_termios;
  603. a2232_driver->init_termios.c_cflag =
  604. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  605. a2232_driver->flags = TTY_DRIVER_REAL_RAW;
  606. tty_set_operations(a2232_driver, &a2232_ops);
  607. if ((error = tty_register_driver(a2232_driver))) {
  608. printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
  609. error);
  610. put_tty_driver(a2232_driver);
  611. return 1;
  612. }
  613. return 0;
  614. }
  615. static int __init a2232board_init(void)
  616. {
  617. struct zorro_dev *z;
  618. unsigned int boardaddr;
  619. int bcount;
  620. short start;
  621. u_char *from;
  622. volatile u_char *to;
  623. volatile struct a2232memory *mem;
  624. #ifdef CONFIG_SMP
  625. return -ENODEV; /* This driver is not SMP aware. Is there an SMP ZorroII-bus-machine? */
  626. #endif
  627. if (!MACH_IS_AMIGA){
  628. return -ENODEV;
  629. }
  630. printk("Commodore A2232 driver initializing.\n"); /* Say that we're alive. */
  631. z = NULL;
  632. nr_a2232 = 0;
  633. while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
  634. if ( (z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) &&
  635. (z->id != ZORRO_PROD_CBM_A2232) ){
  636. continue; // The board found was no A2232
  637. }
  638. if (!zorro_request_device(z,"A2232 driver"))
  639. continue;
  640. printk("Commodore A2232 found (#%d).\n",nr_a2232);
  641. zd_a2232[nr_a2232] = z;
  642. boardaddr = ZTWO_VADDR( z->resource.start );
  643. printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
  644. mem = (volatile struct a2232memory *) boardaddr;
  645. (void) mem->Enable6502Reset; /* copy the code across to the board */
  646. to = (u_char *)mem; from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
  647. start = *(short *)from;
  648. from += sizeof(start);
  649. to += start;
  650. while(bcount--) *to++ = *from++;
  651. printk("65EC02 software uploaded to the A2232 memory.\n");
  652. mem->Common.Crystal = A2232_UNKNOWN; /* use automatic speed check */
  653. /* start 6502 running */
  654. (void) mem->ResetBoard;
  655. printk("A2232's 65EC02 CPU up and running.\n");
  656. /* wait until speed detector has finished */
  657. for (bcount = 0; bcount < 2000; bcount++) {
  658. udelay(1000);
  659. if (mem->Common.Crystal)
  660. break;
  661. }
  662. printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
  663. switch (mem->Common.Crystal){
  664. case A2232_UNKNOWN:
  665. printk("Unknown crystal.\n");
  666. break;
  667. case A2232_NORMAL:
  668. printk ("Normal crystal.\n");
  669. break;
  670. case A2232_TURBO:
  671. printk ("Turbo crystal.\n");
  672. break;
  673. default:
  674. printk ("0x%x. Huh?\n",mem->Common.Crystal);
  675. }
  676. nr_a2232++;
  677. }
  678. printk("Total: %d A2232 boards initialized.\n.", nr_a2232); /* Some status report if no card was found */
  679. a2232_init_portstructs();
  680. /*
  681. a2232_init_drivers also registers the drivers. Must be here because all boards
  682. have to be detected first.
  683. */
  684. if (a2232_init_drivers()) return -ENODEV; // maybe we should use a different -Exxx?
  685. request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0, "A2232 serial VBL", a2232_driver_ID);
  686. return 0;
  687. }
  688. static void __exit a2232board_exit(void)
  689. {
  690. int i;
  691. for (i = 0; i < nr_a2232; i++) {
  692. zorro_release_device(zd_a2232[i]);
  693. }
  694. tty_unregister_driver(a2232_driver);
  695. put_tty_driver(a2232_driver);
  696. free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
  697. }
  698. module_init(a2232board_init);
  699. module_exit(a2232board_exit);
  700. MODULE_AUTHOR("Enver Haase");
  701. MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
  702. MODULE_LICENSE("GPL");