con3215.c 28 KB

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