con3215.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*
  2. * drivers/s390/char/con3215.c
  3. * 3215 line mode terminal driver.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  8. *
  9. * Updated:
  10. * Aug-2000: Added tab support
  11. * Dan Morrison, IBM Corporation (dmorriso@cse.buffalo.edu)
  12. */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/tty.h>
  18. #include <linux/vt_kern.h>
  19. #include <linux/init.h>
  20. #include <linux/console.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/bootmem.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/cio.h>
  26. #include <asm/io.h>
  27. #include <asm/ebcdic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/delay.h>
  30. #include <asm/cpcmd.h>
  31. #include <asm/setup.h>
  32. #include "ctrlchar.h"
  33. #define NR_3215 1
  34. #define NR_3215_REQ (4*NR_3215)
  35. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  36. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  37. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  38. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  39. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  40. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  41. #define RAW3215_NR_CCWS 3
  42. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  43. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  44. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  45. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  46. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  47. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  48. #define RAW3215_CLOSING 32 /* set while in close process */
  49. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  50. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  51. #define TAB_STOP_SIZE 8 /* tab stop size */
  52. /*
  53. * Request types for a 3215 device
  54. */
  55. enum raw3215_type {
  56. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  57. };
  58. /*
  59. * Request structure for a 3215 device
  60. */
  61. struct raw3215_req {
  62. enum raw3215_type type; /* type of the request */
  63. int start, len; /* start index & len in output buffer */
  64. int delayable; /* indication to wait for more data */
  65. int residual; /* residual count for read request */
  66. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  67. struct raw3215_info *info; /* pointer to main structure */
  68. struct raw3215_req *next; /* pointer to next request */
  69. } __attribute__ ((aligned(8)));
  70. struct raw3215_info {
  71. struct ccw_device *cdev; /* device for tty driver */
  72. spinlock_t *lock; /* pointer to irq lock */
  73. int flags; /* state flags */
  74. char *buffer; /* pointer to output buffer */
  75. char *inbuf; /* pointer to input buffer */
  76. int head; /* first free byte in output buffer */
  77. int count; /* number of bytes in output buffer */
  78. int written; /* number of bytes in write requests */
  79. struct tty_struct *tty; /* pointer to tty structure if present */
  80. struct tasklet_struct tasklet;
  81. struct raw3215_req *queued_read; /* pointer to queued read requests */
  82. struct raw3215_req *queued_write;/* pointer to queued write requests */
  83. wait_queue_head_t empty_wait; /* wait queue for flushing */
  84. struct timer_list timer; /* timer for delayed output */
  85. char *message; /* pending message from raw3215_irq */
  86. int msg_dstat; /* dstat for pending message */
  87. int msg_cstat; /* cstat for pending message */
  88. int line_pos; /* position on the line (for tabs) */
  89. char ubuffer[80]; /* copy_from_user buffer */
  90. };
  91. /* array of 3215 devices structures */
  92. static struct raw3215_info *raw3215[NR_3215];
  93. /* spinlock to protect the raw3215 array */
  94. static DEFINE_SPINLOCK(raw3215_device_lock);
  95. /* list of free request structures */
  96. static struct raw3215_req *raw3215_freelist;
  97. /* spinlock to protect free list */
  98. static spinlock_t raw3215_freelist_lock;
  99. static struct tty_driver *tty3215_driver;
  100. /*
  101. * Get a request structure from the free list
  102. */
  103. static inline struct raw3215_req *
  104. raw3215_alloc_req(void) {
  105. struct raw3215_req *req;
  106. unsigned long flags;
  107. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  108. req = raw3215_freelist;
  109. raw3215_freelist = req->next;
  110. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  111. return req;
  112. }
  113. /*
  114. * Put a request structure back to the free list
  115. */
  116. static inline void
  117. raw3215_free_req(struct raw3215_req *req) {
  118. unsigned long flags;
  119. if (req->type == RAW3215_FREE)
  120. return; /* don't free a free request */
  121. req->type = RAW3215_FREE;
  122. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  123. req->next = raw3215_freelist;
  124. raw3215_freelist = req;
  125. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  126. }
  127. /*
  128. * Set up a read request that reads up to 160 byte from the 3215 device.
  129. * If there is a queued read request it is used, but that shouldn't happen
  130. * because a 3215 terminal won't accept a new read before the old one is
  131. * completed.
  132. */
  133. static void
  134. raw3215_mk_read_req(struct raw3215_info *raw)
  135. {
  136. struct raw3215_req *req;
  137. struct ccw1 *ccw;
  138. /* there can only be ONE read request at a time */
  139. req = raw->queued_read;
  140. if (req == NULL) {
  141. /* no queued read request, use new req structure */
  142. req = raw3215_alloc_req();
  143. req->type = RAW3215_READ;
  144. req->info = raw;
  145. raw->queued_read = req;
  146. }
  147. ccw = req->ccws;
  148. ccw->cmd_code = 0x0A; /* read inquiry */
  149. ccw->flags = 0x20; /* ignore incorrect length */
  150. ccw->count = 160;
  151. ccw->cda = (__u32) __pa(raw->inbuf);
  152. }
  153. /*
  154. * Set up a write request with the information from the main structure.
  155. * A ccw chain is created that writes as much as possible from the output
  156. * buffer to the 3215 device. If a queued write exists it is replaced by
  157. * the new, probably lengthened request.
  158. */
  159. static void
  160. raw3215_mk_write_req(struct raw3215_info *raw)
  161. {
  162. struct raw3215_req *req;
  163. struct ccw1 *ccw;
  164. int len, count, ix, lines;
  165. if (raw->count <= raw->written)
  166. return;
  167. /* check if there is a queued write request */
  168. req = raw->queued_write;
  169. if (req == NULL) {
  170. /* no queued write request, use new req structure */
  171. req = raw3215_alloc_req();
  172. req->type = RAW3215_WRITE;
  173. req->info = raw;
  174. raw->queued_write = req;
  175. } else {
  176. raw->written -= req->len;
  177. }
  178. ccw = req->ccws;
  179. req->start = (raw->head - raw->count + raw->written) &
  180. (RAW3215_BUFFER_SIZE - 1);
  181. /*
  182. * now we have to count newlines. We can at max accept
  183. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  184. * a restriction in VM
  185. */
  186. lines = 0;
  187. ix = req->start;
  188. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  189. if (raw->buffer[ix] == 0x15)
  190. lines++;
  191. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  192. }
  193. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  194. if (len > RAW3215_MAX_BYTES)
  195. len = RAW3215_MAX_BYTES;
  196. req->len = len;
  197. raw->written += len;
  198. /* set the indication if we should try to enlarge this request */
  199. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  200. ix = req->start;
  201. while (len > 0) {
  202. if (ccw > req->ccws)
  203. ccw[-1].flags |= 0x40; /* use command chaining */
  204. ccw->cmd_code = 0x01; /* write, auto carrier return */
  205. ccw->flags = 0x20; /* ignore incorrect length ind. */
  206. ccw->cda =
  207. (__u32) __pa(raw->buffer + ix);
  208. count = len;
  209. if (ix + count > RAW3215_BUFFER_SIZE)
  210. count = RAW3215_BUFFER_SIZE - ix;
  211. ccw->count = count;
  212. len -= count;
  213. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  214. ccw++;
  215. }
  216. /*
  217. * Add a NOP to the channel program. 3215 devices are purely
  218. * emulated and its much better to avoid the channel end
  219. * interrupt in this case.
  220. */
  221. if (ccw > req->ccws)
  222. ccw[-1].flags |= 0x40; /* use command chaining */
  223. ccw->cmd_code = 0x03; /* NOP */
  224. ccw->flags = 0;
  225. ccw->cda = 0;
  226. ccw->count = 1;
  227. }
  228. /*
  229. * Start a read or a write request
  230. */
  231. static void
  232. raw3215_start_io(struct raw3215_info *raw)
  233. {
  234. struct raw3215_req *req;
  235. int res;
  236. req = raw->queued_read;
  237. if (req != NULL &&
  238. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  239. /* dequeue request */
  240. raw->queued_read = NULL;
  241. res = ccw_device_start(raw->cdev, req->ccws,
  242. (unsigned long) req, 0, 0);
  243. if (res != 0) {
  244. /* do_IO failed, put request back to queue */
  245. raw->queued_read = req;
  246. } else {
  247. raw->flags |= RAW3215_WORKING;
  248. }
  249. }
  250. req = raw->queued_write;
  251. if (req != NULL &&
  252. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  253. /* dequeue request */
  254. raw->queued_write = NULL;
  255. res = ccw_device_start(raw->cdev, req->ccws,
  256. (unsigned long) req, 0, 0);
  257. if (res != 0) {
  258. /* do_IO failed, put request back to queue */
  259. raw->queued_write = req;
  260. } else {
  261. raw->flags |= RAW3215_WORKING;
  262. }
  263. }
  264. }
  265. /*
  266. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  267. */
  268. static void
  269. raw3215_timeout(unsigned long __data)
  270. {
  271. struct raw3215_info *raw = (struct raw3215_info *) __data;
  272. unsigned long flags;
  273. spin_lock_irqsave(raw->lock, flags);
  274. if (raw->flags & RAW3215_TIMER_RUNS) {
  275. del_timer(&raw->timer);
  276. raw->flags &= ~RAW3215_TIMER_RUNS;
  277. raw3215_mk_write_req(raw);
  278. raw3215_start_io(raw);
  279. }
  280. spin_unlock_irqrestore(raw->lock, flags);
  281. }
  282. /*
  283. * Function to conditionally start an IO. A read is started immediately,
  284. * a write is only started immediately if the flush flag is on or the
  285. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  286. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  287. */
  288. static inline void
  289. raw3215_try_io(struct raw3215_info *raw)
  290. {
  291. if (!(raw->flags & RAW3215_ACTIVE))
  292. return;
  293. if (raw->queued_read != NULL)
  294. raw3215_start_io(raw);
  295. else if (raw->queued_write != NULL) {
  296. if ((raw->queued_write->delayable == 0) ||
  297. (raw->flags & RAW3215_FLUSHING)) {
  298. /* execute write requests bigger than minimum size */
  299. raw3215_start_io(raw);
  300. if (raw->flags & RAW3215_TIMER_RUNS) {
  301. del_timer(&raw->timer);
  302. raw->flags &= ~RAW3215_TIMER_RUNS;
  303. }
  304. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  305. /* delay small writes */
  306. init_timer(&raw->timer);
  307. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  308. raw->timer.data = (unsigned long) raw;
  309. raw->timer.function = raw3215_timeout;
  310. add_timer(&raw->timer);
  311. raw->flags |= RAW3215_TIMER_RUNS;
  312. }
  313. }
  314. }
  315. /*
  316. * The bottom half handler routine for 3215 devices. It tries to start
  317. * the next IO and wakes up processes waiting on the tty.
  318. */
  319. static void
  320. raw3215_tasklet(void *data)
  321. {
  322. struct raw3215_info *raw;
  323. struct tty_struct *tty;
  324. unsigned long flags;
  325. raw = (struct raw3215_info *) data;
  326. spin_lock_irqsave(raw->lock, flags);
  327. raw3215_mk_write_req(raw);
  328. raw3215_try_io(raw);
  329. spin_unlock_irqrestore(raw->lock, flags);
  330. /* Check for pending message from raw3215_irq */
  331. if (raw->message != NULL) {
  332. printk(raw->message, raw->msg_dstat, raw->msg_cstat);
  333. raw->message = NULL;
  334. }
  335. tty = raw->tty;
  336. if (tty != NULL &&
  337. RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
  338. tty_wakeup(tty);
  339. }
  340. }
  341. /*
  342. * Interrupt routine, called from common io layer
  343. */
  344. static void
  345. raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  346. {
  347. struct raw3215_info *raw;
  348. struct raw3215_req *req;
  349. struct tty_struct *tty;
  350. int cstat, dstat;
  351. int count, slen;
  352. raw = cdev->dev.driver_data;
  353. req = (struct raw3215_req *) intparm;
  354. cstat = irb->scsw.cstat;
  355. dstat = irb->scsw.dstat;
  356. if (cstat != 0) {
  357. raw->message = KERN_WARNING
  358. "Got nonzero channel status in raw3215_irq "
  359. "(dev sts 0x%2x, sch sts 0x%2x)";
  360. raw->msg_dstat = dstat;
  361. raw->msg_cstat = cstat;
  362. tasklet_schedule(&raw->tasklet);
  363. }
  364. if (dstat & 0x01) { /* we got a unit exception */
  365. dstat &= ~0x01; /* we can ignore it */
  366. }
  367. switch (dstat) {
  368. case 0x80:
  369. if (cstat != 0)
  370. break;
  371. /* Attention interrupt, someone hit the enter key */
  372. raw3215_mk_read_req(raw);
  373. if (MACHINE_IS_P390)
  374. memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
  375. tasklet_schedule(&raw->tasklet);
  376. break;
  377. case 0x08:
  378. case 0x0C:
  379. /* Channel end interrupt. */
  380. if ((raw = req->info) == NULL)
  381. return; /* That shouldn't happen ... */
  382. if (req->type == RAW3215_READ) {
  383. /* store residual count, then wait for device end */
  384. req->residual = irb->scsw.count;
  385. }
  386. if (dstat == 0x08)
  387. break;
  388. case 0x04:
  389. /* Device end interrupt. */
  390. if ((raw = req->info) == NULL)
  391. return; /* That shouldn't happen ... */
  392. if (req->type == RAW3215_READ && raw->tty != NULL) {
  393. unsigned int cchar;
  394. tty = raw->tty;
  395. count = 160 - req->residual;
  396. if (MACHINE_IS_P390) {
  397. slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
  398. if (count > slen)
  399. count = slen;
  400. } else
  401. if (count >= TTY_FLIPBUF_SIZE - tty->flip.count)
  402. count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
  403. EBCASC(raw->inbuf, count);
  404. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  405. switch (cchar & CTRLCHAR_MASK) {
  406. case CTRLCHAR_SYSRQ:
  407. break;
  408. case CTRLCHAR_CTRL:
  409. tty->flip.count++;
  410. *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
  411. *tty->flip.char_buf_ptr++ = cchar;
  412. tty_flip_buffer_push(raw->tty);
  413. break;
  414. case CTRLCHAR_NONE:
  415. memcpy(tty->flip.char_buf_ptr,
  416. raw->inbuf, count);
  417. if (count < 2 ||
  418. (strncmp(raw->inbuf+count-2, "^n", 2) &&
  419. strncmp(raw->inbuf+count-2, "\252n", 2)) ) {
  420. /* don't add the auto \n */
  421. tty->flip.char_buf_ptr[count] = '\n';
  422. memset(tty->flip.flag_buf_ptr,
  423. TTY_NORMAL, count + 1);
  424. count++;
  425. } else
  426. count-=2;
  427. tty->flip.char_buf_ptr += count;
  428. tty->flip.flag_buf_ptr += count;
  429. tty->flip.count += count;
  430. tty_flip_buffer_push(raw->tty);
  431. break;
  432. }
  433. } else if (req->type == RAW3215_WRITE) {
  434. raw->count -= req->len;
  435. raw->written -= req->len;
  436. }
  437. raw->flags &= ~RAW3215_WORKING;
  438. raw3215_free_req(req);
  439. /* check for empty wait */
  440. if (waitqueue_active(&raw->empty_wait) &&
  441. raw->queued_write == NULL &&
  442. raw->queued_read == NULL) {
  443. wake_up_interruptible(&raw->empty_wait);
  444. }
  445. tasklet_schedule(&raw->tasklet);
  446. break;
  447. default:
  448. /* Strange interrupt, I'll do my best to clean up */
  449. if (req != NULL && req->type != RAW3215_FREE) {
  450. if (req->type == RAW3215_WRITE) {
  451. raw->count -= req->len;
  452. raw->written -= req->len;
  453. }
  454. raw->flags &= ~RAW3215_WORKING;
  455. raw3215_free_req(req);
  456. }
  457. raw->message = KERN_WARNING
  458. "Spurious interrupt in in raw3215_irq "
  459. "(dev sts 0x%2x, sch sts 0x%2x)";
  460. raw->msg_dstat = dstat;
  461. raw->msg_cstat = cstat;
  462. tasklet_schedule(&raw->tasklet);
  463. }
  464. return;
  465. }
  466. /*
  467. * Wait until length bytes are available int the output buffer.
  468. * Has to be called with the s390irq lock held. Can be called
  469. * disabled.
  470. */
  471. static void
  472. raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  473. {
  474. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  475. /* there might be a request pending */
  476. raw->flags |= RAW3215_FLUSHING;
  477. raw3215_mk_write_req(raw);
  478. raw3215_try_io(raw);
  479. raw->flags &= ~RAW3215_FLUSHING;
  480. #ifdef CONFIG_TN3215_CONSOLE
  481. wait_cons_dev();
  482. #endif
  483. /* Enough room freed up ? */
  484. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  485. break;
  486. /* there might be another cpu waiting for the lock */
  487. spin_unlock(raw->lock);
  488. udelay(100);
  489. spin_lock(raw->lock);
  490. }
  491. }
  492. /*
  493. * String write routine for 3215 devices
  494. */
  495. static void
  496. raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
  497. {
  498. unsigned long flags;
  499. int c, count;
  500. while (length > 0) {
  501. spin_lock_irqsave(raw->lock, flags);
  502. count = (length > RAW3215_BUFFER_SIZE) ?
  503. RAW3215_BUFFER_SIZE : length;
  504. length -= count;
  505. raw3215_make_room(raw, count);
  506. /* copy string to output buffer and convert it to EBCDIC */
  507. while (1) {
  508. c = min_t(int, count,
  509. min(RAW3215_BUFFER_SIZE - raw->count,
  510. RAW3215_BUFFER_SIZE - raw->head));
  511. if (c <= 0)
  512. break;
  513. memcpy(raw->buffer + raw->head, str, c);
  514. ASCEBC(raw->buffer + raw->head, c);
  515. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  516. raw->count += c;
  517. raw->line_pos += c;
  518. str += c;
  519. count -= c;
  520. }
  521. if (!(raw->flags & RAW3215_WORKING)) {
  522. raw3215_mk_write_req(raw);
  523. /* start or queue request */
  524. raw3215_try_io(raw);
  525. }
  526. spin_unlock_irqrestore(raw->lock, flags);
  527. }
  528. }
  529. /*
  530. * Put character routine for 3215 devices
  531. */
  532. static void
  533. raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  534. {
  535. unsigned long flags;
  536. unsigned int length, i;
  537. spin_lock_irqsave(raw->lock, flags);
  538. if (ch == '\t') {
  539. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  540. raw->line_pos += length;
  541. ch = ' ';
  542. } else if (ch == '\n') {
  543. length = 1;
  544. raw->line_pos = 0;
  545. } else {
  546. length = 1;
  547. raw->line_pos++;
  548. }
  549. raw3215_make_room(raw, length);
  550. for (i = 0; i < length; i++) {
  551. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  552. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  553. raw->count++;
  554. }
  555. if (!(raw->flags & RAW3215_WORKING)) {
  556. raw3215_mk_write_req(raw);
  557. /* start or queue request */
  558. raw3215_try_io(raw);
  559. }
  560. spin_unlock_irqrestore(raw->lock, flags);
  561. }
  562. /*
  563. * Flush routine, it simply sets the flush flag and tries to start
  564. * pending IO.
  565. */
  566. static void
  567. raw3215_flush_buffer(struct raw3215_info *raw)
  568. {
  569. unsigned long flags;
  570. spin_lock_irqsave(raw->lock, flags);
  571. if (raw->count > 0) {
  572. raw->flags |= RAW3215_FLUSHING;
  573. raw3215_try_io(raw);
  574. raw->flags &= ~RAW3215_FLUSHING;
  575. }
  576. spin_unlock_irqrestore(raw->lock, flags);
  577. }
  578. /*
  579. * Fire up a 3215 device.
  580. */
  581. static int
  582. raw3215_startup(struct raw3215_info *raw)
  583. {
  584. unsigned long flags;
  585. if (raw->flags & RAW3215_ACTIVE)
  586. return 0;
  587. raw->line_pos = 0;
  588. raw->flags |= RAW3215_ACTIVE;
  589. spin_lock_irqsave(raw->lock, flags);
  590. raw3215_try_io(raw);
  591. spin_unlock_irqrestore(raw->lock, flags);
  592. return 0;
  593. }
  594. /*
  595. * Shutdown a 3215 device.
  596. */
  597. static void
  598. raw3215_shutdown(struct raw3215_info *raw)
  599. {
  600. DECLARE_WAITQUEUE(wait, current);
  601. unsigned long flags;
  602. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  603. return;
  604. /* Wait for outstanding requests, then free irq */
  605. spin_lock_irqsave(raw->lock, flags);
  606. if ((raw->flags & RAW3215_WORKING) ||
  607. raw->queued_write != NULL ||
  608. raw->queued_read != NULL) {
  609. raw->flags |= RAW3215_CLOSING;
  610. add_wait_queue(&raw->empty_wait, &wait);
  611. set_current_state(TASK_INTERRUPTIBLE);
  612. spin_unlock_irqrestore(raw->lock, flags);
  613. schedule();
  614. spin_lock_irqsave(raw->lock, flags);
  615. remove_wait_queue(&raw->empty_wait, &wait);
  616. set_current_state(TASK_RUNNING);
  617. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  618. }
  619. spin_unlock_irqrestore(raw->lock, flags);
  620. }
  621. static int
  622. raw3215_probe (struct ccw_device *cdev)
  623. {
  624. struct raw3215_info *raw;
  625. int line;
  626. raw = kmalloc(sizeof(struct raw3215_info) +
  627. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  628. if (raw == NULL)
  629. return -ENOMEM;
  630. spin_lock(&raw3215_device_lock);
  631. for (line = 0; line < NR_3215; line++) {
  632. if (!raw3215[line]) {
  633. raw3215[line] = raw;
  634. break;
  635. }
  636. }
  637. spin_unlock(&raw3215_device_lock);
  638. if (line == NR_3215) {
  639. kfree(raw);
  640. return -ENODEV;
  641. }
  642. raw->cdev = cdev;
  643. raw->lock = get_ccwdev_lock(cdev);
  644. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  645. memset(raw, 0, sizeof(struct raw3215_info));
  646. raw->buffer = (char *) kmalloc(RAW3215_BUFFER_SIZE,
  647. GFP_KERNEL|GFP_DMA);
  648. if (raw->buffer == NULL) {
  649. spin_lock(&raw3215_device_lock);
  650. raw3215[line] = 0;
  651. spin_unlock(&raw3215_device_lock);
  652. kfree(raw);
  653. return -ENOMEM;
  654. }
  655. tasklet_init(&raw->tasklet,
  656. (void (*)(unsigned long)) raw3215_tasklet,
  657. (unsigned long) raw);
  658. init_waitqueue_head(&raw->empty_wait);
  659. cdev->dev.driver_data = raw;
  660. cdev->handler = raw3215_irq;
  661. return 0;
  662. }
  663. static void
  664. raw3215_remove (struct ccw_device *cdev)
  665. {
  666. struct raw3215_info *raw;
  667. ccw_device_set_offline(cdev);
  668. raw = cdev->dev.driver_data;
  669. if (raw) {
  670. cdev->dev.driver_data = NULL;
  671. if (raw->buffer)
  672. kfree(raw->buffer);
  673. kfree(raw);
  674. }
  675. }
  676. static int
  677. raw3215_set_online (struct ccw_device *cdev)
  678. {
  679. struct raw3215_info *raw;
  680. raw = cdev->dev.driver_data;
  681. if (!raw)
  682. return -ENODEV;
  683. return raw3215_startup(raw);
  684. }
  685. static int
  686. raw3215_set_offline (struct ccw_device *cdev)
  687. {
  688. struct raw3215_info *raw;
  689. raw = cdev->dev.driver_data;
  690. if (!raw)
  691. return -ENODEV;
  692. raw3215_shutdown(raw);
  693. return 0;
  694. }
  695. static struct ccw_device_id raw3215_id[] = {
  696. { CCW_DEVICE(0x3215, 0) },
  697. { /* end of list */ },
  698. };
  699. static struct ccw_driver raw3215_ccw_driver = {
  700. .name = "3215",
  701. .owner = THIS_MODULE,
  702. .ids = raw3215_id,
  703. .probe = &raw3215_probe,
  704. .remove = &raw3215_remove,
  705. .set_online = &raw3215_set_online,
  706. .set_offline = &raw3215_set_offline,
  707. };
  708. #ifdef CONFIG_TN3215_CONSOLE
  709. /*
  710. * Write a string to the 3215 console
  711. */
  712. static void
  713. con3215_write(struct console *co, const char *str, unsigned int count)
  714. {
  715. struct raw3215_info *raw;
  716. int i;
  717. if (count <= 0)
  718. return;
  719. raw = raw3215[0]; /* console 3215 is the first one */
  720. while (count > 0) {
  721. for (i = 0; i < count; i++)
  722. if (str[i] == '\t' || str[i] == '\n')
  723. break;
  724. raw3215_write(raw, str, i);
  725. count -= i;
  726. str += i;
  727. if (count > 0) {
  728. raw3215_putchar(raw, *str);
  729. count--;
  730. str++;
  731. }
  732. }
  733. }
  734. static struct tty_driver *con3215_device(struct console *c, int *index)
  735. {
  736. *index = c->index;
  737. return tty3215_driver;
  738. }
  739. /*
  740. * panic() calls console_unblank before the system enters a
  741. * disabled, endless loop.
  742. */
  743. static void
  744. con3215_unblank(void)
  745. {
  746. struct raw3215_info *raw;
  747. unsigned long flags;
  748. raw = raw3215[0]; /* console 3215 is the first one */
  749. spin_lock_irqsave(raw->lock, flags);
  750. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  751. spin_unlock_irqrestore(raw->lock, flags);
  752. }
  753. static int __init
  754. con3215_consetup(struct console *co, char *options)
  755. {
  756. return 0;
  757. }
  758. /*
  759. * The console structure for the 3215 console
  760. */
  761. static struct console con3215 = {
  762. .name = "ttyS",
  763. .write = con3215_write,
  764. .device = con3215_device,
  765. .unblank = con3215_unblank,
  766. .setup = con3215_consetup,
  767. .flags = CON_PRINTBUFFER,
  768. };
  769. /*
  770. * 3215 console initialization code called from console_init().
  771. * NOTE: This is called before kmalloc is available.
  772. */
  773. static int __init
  774. con3215_init(void)
  775. {
  776. struct ccw_device *cdev;
  777. struct raw3215_info *raw;
  778. struct raw3215_req *req;
  779. int i;
  780. /* Check if 3215 is to be the console */
  781. if (!CONSOLE_IS_3215)
  782. return -ENODEV;
  783. /* Set the console mode for VM */
  784. if (MACHINE_IS_VM) {
  785. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  786. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  787. }
  788. /* allocate 3215 request structures */
  789. raw3215_freelist = NULL;
  790. spin_lock_init(&raw3215_freelist_lock);
  791. for (i = 0; i < NR_3215_REQ; i++) {
  792. req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
  793. req->next = raw3215_freelist;
  794. raw3215_freelist = req;
  795. }
  796. cdev = ccw_device_probe_console();
  797. if (!cdev)
  798. return -ENODEV;
  799. raw3215[0] = raw = (struct raw3215_info *)
  800. alloc_bootmem_low(sizeof(struct raw3215_info));
  801. memset(raw, 0, sizeof(struct raw3215_info));
  802. raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
  803. raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
  804. raw->cdev = cdev;
  805. raw->lock = get_ccwdev_lock(cdev);
  806. cdev->dev.driver_data = raw;
  807. cdev->handler = raw3215_irq;
  808. raw->flags |= RAW3215_FIXED;
  809. tasklet_init(&raw->tasklet,
  810. (void (*)(unsigned long)) raw3215_tasklet,
  811. (unsigned long) raw);
  812. init_waitqueue_head(&raw->empty_wait);
  813. /* Request the console irq */
  814. if (raw3215_startup(raw) != 0) {
  815. free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
  816. free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
  817. free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
  818. raw3215[0] = NULL;
  819. printk("Couldn't find a 3215 console device\n");
  820. return -ENODEV;
  821. }
  822. register_console(&con3215);
  823. return 0;
  824. }
  825. console_initcall(con3215_init);
  826. #endif
  827. /*
  828. * tty3215_open
  829. *
  830. * This routine is called whenever a 3215 tty is opened.
  831. */
  832. static int
  833. tty3215_open(struct tty_struct *tty, struct file * filp)
  834. {
  835. struct raw3215_info *raw;
  836. int retval, line;
  837. line = tty->index;
  838. if ((line < 0) || (line >= NR_3215))
  839. return -ENODEV;
  840. raw = raw3215[line];
  841. if (raw == NULL)
  842. return -ENODEV;
  843. tty->driver_data = raw;
  844. raw->tty = tty;
  845. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  846. /*
  847. * Start up 3215 device
  848. */
  849. retval = raw3215_startup(raw);
  850. if (retval)
  851. return retval;
  852. return 0;
  853. }
  854. /*
  855. * tty3215_close()
  856. *
  857. * This routine is called when the 3215 tty is closed. We wait
  858. * for the remaining request to be completed. Then we clean up.
  859. */
  860. static void
  861. tty3215_close(struct tty_struct *tty, struct file * filp)
  862. {
  863. struct raw3215_info *raw;
  864. raw = (struct raw3215_info *) tty->driver_data;
  865. if (raw == NULL || tty->count > 1)
  866. return;
  867. tty->closing = 1;
  868. /* Shutdown the terminal */
  869. raw3215_shutdown(raw);
  870. tty->closing = 0;
  871. raw->tty = NULL;
  872. }
  873. /*
  874. * Returns the amount of free space in the output buffer.
  875. */
  876. static int
  877. tty3215_write_room(struct tty_struct *tty)
  878. {
  879. struct raw3215_info *raw;
  880. raw = (struct raw3215_info *) tty->driver_data;
  881. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  882. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  883. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  884. else
  885. return 0;
  886. }
  887. /*
  888. * String write routine for 3215 ttys
  889. */
  890. static int
  891. tty3215_write(struct tty_struct * tty,
  892. const unsigned char *buf, int count)
  893. {
  894. struct raw3215_info *raw;
  895. if (!tty)
  896. return 0;
  897. raw = (struct raw3215_info *) tty->driver_data;
  898. raw3215_write(raw, buf, count);
  899. return count;
  900. }
  901. /*
  902. * Put character routine for 3215 ttys
  903. */
  904. static void
  905. tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  906. {
  907. struct raw3215_info *raw;
  908. if (!tty)
  909. return;
  910. raw = (struct raw3215_info *) tty->driver_data;
  911. raw3215_putchar(raw, ch);
  912. }
  913. static void
  914. tty3215_flush_chars(struct tty_struct *tty)
  915. {
  916. }
  917. /*
  918. * Returns the number of characters in the output buffer
  919. */
  920. static int
  921. tty3215_chars_in_buffer(struct tty_struct *tty)
  922. {
  923. struct raw3215_info *raw;
  924. raw = (struct raw3215_info *) tty->driver_data;
  925. return raw->count;
  926. }
  927. static void
  928. tty3215_flush_buffer(struct tty_struct *tty)
  929. {
  930. struct raw3215_info *raw;
  931. raw = (struct raw3215_info *) tty->driver_data;
  932. raw3215_flush_buffer(raw);
  933. tty_wakeup(tty);
  934. }
  935. /*
  936. * Currently we don't have any io controls for 3215 ttys
  937. */
  938. static int
  939. tty3215_ioctl(struct tty_struct *tty, struct file * file,
  940. unsigned int cmd, unsigned long arg)
  941. {
  942. if (tty->flags & (1 << TTY_IO_ERROR))
  943. return -EIO;
  944. switch (cmd) {
  945. default:
  946. return -ENOIOCTLCMD;
  947. }
  948. return 0;
  949. }
  950. /*
  951. * Disable reading from a 3215 tty
  952. */
  953. static void
  954. tty3215_throttle(struct tty_struct * tty)
  955. {
  956. struct raw3215_info *raw;
  957. raw = (struct raw3215_info *) tty->driver_data;
  958. raw->flags |= RAW3215_THROTTLED;
  959. }
  960. /*
  961. * Enable reading from a 3215 tty
  962. */
  963. static void
  964. tty3215_unthrottle(struct tty_struct * tty)
  965. {
  966. struct raw3215_info *raw;
  967. unsigned long flags;
  968. raw = (struct raw3215_info *) tty->driver_data;
  969. if (raw->flags & RAW3215_THROTTLED) {
  970. spin_lock_irqsave(raw->lock, flags);
  971. raw->flags &= ~RAW3215_THROTTLED;
  972. raw3215_try_io(raw);
  973. spin_unlock_irqrestore(raw->lock, flags);
  974. }
  975. }
  976. /*
  977. * Disable writing to a 3215 tty
  978. */
  979. static void
  980. tty3215_stop(struct tty_struct *tty)
  981. {
  982. struct raw3215_info *raw;
  983. raw = (struct raw3215_info *) tty->driver_data;
  984. raw->flags |= RAW3215_STOPPED;
  985. }
  986. /*
  987. * Enable writing to a 3215 tty
  988. */
  989. static void
  990. tty3215_start(struct tty_struct *tty)
  991. {
  992. struct raw3215_info *raw;
  993. unsigned long flags;
  994. raw = (struct raw3215_info *) tty->driver_data;
  995. if (raw->flags & RAW3215_STOPPED) {
  996. spin_lock_irqsave(raw->lock, flags);
  997. raw->flags &= ~RAW3215_STOPPED;
  998. raw3215_try_io(raw);
  999. spin_unlock_irqrestore(raw->lock, flags);
  1000. }
  1001. }
  1002. static struct tty_operations tty3215_ops = {
  1003. .open = tty3215_open,
  1004. .close = tty3215_close,
  1005. .write = tty3215_write,
  1006. .put_char = tty3215_put_char,
  1007. .flush_chars = tty3215_flush_chars,
  1008. .write_room = tty3215_write_room,
  1009. .chars_in_buffer = tty3215_chars_in_buffer,
  1010. .flush_buffer = tty3215_flush_buffer,
  1011. .ioctl = tty3215_ioctl,
  1012. .throttle = tty3215_throttle,
  1013. .unthrottle = tty3215_unthrottle,
  1014. .stop = tty3215_stop,
  1015. .start = tty3215_start,
  1016. };
  1017. /*
  1018. * 3215 tty registration code called from tty_init().
  1019. * Most kernel services (incl. kmalloc) are available at this poimt.
  1020. */
  1021. int __init
  1022. tty3215_init(void)
  1023. {
  1024. struct tty_driver *driver;
  1025. int ret;
  1026. if (!CONSOLE_IS_3215)
  1027. return 0;
  1028. driver = alloc_tty_driver(NR_3215);
  1029. if (!driver)
  1030. return -ENOMEM;
  1031. ret = ccw_driver_register(&raw3215_ccw_driver);
  1032. if (ret) {
  1033. put_tty_driver(driver);
  1034. return ret;
  1035. }
  1036. /*
  1037. * Initialize the tty_driver structure
  1038. * Entries in tty3215_driver that are NOT initialized:
  1039. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1040. */
  1041. driver->owner = THIS_MODULE;
  1042. driver->driver_name = "tty3215";
  1043. driver->name = "ttyS";
  1044. driver->major = TTY_MAJOR;
  1045. driver->minor_start = 64;
  1046. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1047. driver->subtype = SYSTEM_TYPE_TTY;
  1048. driver->init_termios = tty_std_termios;
  1049. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1050. driver->init_termios.c_oflag = ONLCR | XTABS;
  1051. driver->init_termios.c_lflag = ISIG;
  1052. driver->flags = TTY_DRIVER_REAL_RAW;
  1053. tty_set_operations(driver, &tty3215_ops);
  1054. ret = tty_register_driver(driver);
  1055. if (ret) {
  1056. printk("Couldn't register tty3215 driver\n");
  1057. put_tty_driver(driver);
  1058. return ret;
  1059. }
  1060. tty3215_driver = driver;
  1061. return 0;
  1062. }
  1063. static void __exit
  1064. tty3215_exit(void)
  1065. {
  1066. tty_unregister_driver(tty3215_driver);
  1067. put_tty_driver(tty3215_driver);
  1068. ccw_driver_unregister(&raw3215_ccw_driver);
  1069. }
  1070. module_init(tty3215_init);
  1071. module_exit(tty3215_exit);