con3215.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/reboot.h>
  24. #include <linux/slab.h>
  25. #include <linux/bootmem.h>
  26. #include <asm/ccwdev.h>
  27. #include <asm/cio.h>
  28. #include <asm/io.h>
  29. #include <asm/ebcdic.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/delay.h>
  32. #include <asm/cpcmd.h>
  33. #include <asm/setup.h>
  34. #include "ctrlchar.h"
  35. #define NR_3215 1
  36. #define NR_3215_REQ (4*NR_3215)
  37. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  38. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  39. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  40. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  41. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  42. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  43. #define RAW3215_NR_CCWS 3
  44. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  45. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  46. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  47. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  48. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  49. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  50. #define RAW3215_CLOSING 32 /* set while in close process */
  51. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  52. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  53. #define TAB_STOP_SIZE 8 /* tab stop size */
  54. /*
  55. * Request types for a 3215 device
  56. */
  57. enum raw3215_type {
  58. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  59. };
  60. /*
  61. * Request structure for a 3215 device
  62. */
  63. struct raw3215_req {
  64. enum raw3215_type type; /* type of the request */
  65. int start, len; /* start index & len in output buffer */
  66. int delayable; /* indication to wait for more data */
  67. int residual; /* residual count for read request */
  68. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  69. struct raw3215_info *info; /* pointer to main structure */
  70. struct raw3215_req *next; /* pointer to next request */
  71. } __attribute__ ((aligned(8)));
  72. struct raw3215_info {
  73. struct ccw_device *cdev; /* device for tty driver */
  74. spinlock_t *lock; /* pointer to irq lock */
  75. int flags; /* state flags */
  76. char *buffer; /* pointer to output buffer */
  77. char *inbuf; /* pointer to input buffer */
  78. int head; /* first free byte in output buffer */
  79. int count; /* number of bytes in output buffer */
  80. int written; /* number of bytes in write requests */
  81. struct tty_struct *tty; /* pointer to tty structure if present */
  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. * Try to start the next IO and wake up processes waiting on the tty.
  315. */
  316. static void raw3215_next_io(struct raw3215_info *raw)
  317. {
  318. struct tty_struct *tty;
  319. raw3215_mk_write_req(raw);
  320. raw3215_try_io(raw);
  321. tty = raw->tty;
  322. if (tty != NULL &&
  323. RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
  324. tty_wakeup(tty);
  325. }
  326. }
  327. /*
  328. * Interrupt routine, called from common io layer
  329. */
  330. static void
  331. raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  332. {
  333. struct raw3215_info *raw;
  334. struct raw3215_req *req;
  335. struct tty_struct *tty;
  336. int cstat, dstat;
  337. int count;
  338. raw = cdev->dev.driver_data;
  339. req = (struct raw3215_req *) intparm;
  340. cstat = irb->scsw.cmd.cstat;
  341. dstat = irb->scsw.cmd.dstat;
  342. if (cstat != 0)
  343. raw3215_next_io(raw);
  344. if (dstat & 0x01) { /* we got a unit exception */
  345. dstat &= ~0x01; /* we can ignore it */
  346. }
  347. switch (dstat) {
  348. case 0x80:
  349. if (cstat != 0)
  350. break;
  351. /* Attention interrupt, someone hit the enter key */
  352. raw3215_mk_read_req(raw);
  353. raw3215_next_io(raw);
  354. break;
  355. case 0x08:
  356. case 0x0C:
  357. /* Channel end interrupt. */
  358. if ((raw = req->info) == NULL)
  359. return; /* That shouldn't happen ... */
  360. if (req->type == RAW3215_READ) {
  361. /* store residual count, then wait for device end */
  362. req->residual = irb->scsw.cmd.count;
  363. }
  364. if (dstat == 0x08)
  365. break;
  366. case 0x04:
  367. /* Device end interrupt. */
  368. if ((raw = req->info) == NULL)
  369. return; /* That shouldn't happen ... */
  370. if (req->type == RAW3215_READ && raw->tty != NULL) {
  371. unsigned int cchar;
  372. tty = raw->tty;
  373. count = 160 - req->residual;
  374. EBCASC(raw->inbuf, count);
  375. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  376. switch (cchar & CTRLCHAR_MASK) {
  377. case CTRLCHAR_SYSRQ:
  378. break;
  379. case CTRLCHAR_CTRL:
  380. tty_insert_flip_char(tty, cchar, TTY_NORMAL);
  381. tty_flip_buffer_push(raw->tty);
  382. break;
  383. case CTRLCHAR_NONE:
  384. if (count < 2 ||
  385. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  386. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  387. /* add the auto \n */
  388. raw->inbuf[count] = '\n';
  389. count++;
  390. } else
  391. count -= 2;
  392. tty_insert_flip_string(tty, raw->inbuf, count);
  393. tty_flip_buffer_push(raw->tty);
  394. break;
  395. }
  396. } else if (req->type == RAW3215_WRITE) {
  397. raw->count -= req->len;
  398. raw->written -= req->len;
  399. }
  400. raw->flags &= ~RAW3215_WORKING;
  401. raw3215_free_req(req);
  402. /* check for empty wait */
  403. if (waitqueue_active(&raw->empty_wait) &&
  404. raw->queued_write == NULL &&
  405. raw->queued_read == NULL) {
  406. wake_up_interruptible(&raw->empty_wait);
  407. }
  408. raw3215_next_io(raw);
  409. break;
  410. default:
  411. /* Strange interrupt, I'll do my best to clean up */
  412. if (req != NULL && req->type != RAW3215_FREE) {
  413. if (req->type == RAW3215_WRITE) {
  414. raw->count -= req->len;
  415. raw->written -= req->len;
  416. }
  417. raw->flags &= ~RAW3215_WORKING;
  418. raw3215_free_req(req);
  419. }
  420. raw3215_next_io(raw);
  421. }
  422. return;
  423. }
  424. /*
  425. * Wait until length bytes are available int the output buffer.
  426. * Has to be called with the s390irq lock held. Can be called
  427. * disabled.
  428. */
  429. static void
  430. raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  431. {
  432. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  433. /* there might be a request pending */
  434. raw->flags |= RAW3215_FLUSHING;
  435. raw3215_mk_write_req(raw);
  436. raw3215_try_io(raw);
  437. raw->flags &= ~RAW3215_FLUSHING;
  438. #ifdef CONFIG_TN3215_CONSOLE
  439. wait_cons_dev();
  440. #endif
  441. /* Enough room freed up ? */
  442. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  443. break;
  444. /* there might be another cpu waiting for the lock */
  445. spin_unlock(get_ccwdev_lock(raw->cdev));
  446. udelay(100);
  447. spin_lock(get_ccwdev_lock(raw->cdev));
  448. }
  449. }
  450. /*
  451. * String write routine for 3215 devices
  452. */
  453. static void
  454. raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
  455. {
  456. unsigned long flags;
  457. int c, count;
  458. while (length > 0) {
  459. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  460. count = (length > RAW3215_BUFFER_SIZE) ?
  461. RAW3215_BUFFER_SIZE : length;
  462. length -= count;
  463. raw3215_make_room(raw, count);
  464. /* copy string to output buffer and convert it to EBCDIC */
  465. while (1) {
  466. c = min_t(int, count,
  467. min(RAW3215_BUFFER_SIZE - raw->count,
  468. RAW3215_BUFFER_SIZE - raw->head));
  469. if (c <= 0)
  470. break;
  471. memcpy(raw->buffer + raw->head, str, c);
  472. ASCEBC(raw->buffer + raw->head, c);
  473. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  474. raw->count += c;
  475. raw->line_pos += c;
  476. str += c;
  477. count -= c;
  478. }
  479. if (!(raw->flags & RAW3215_WORKING)) {
  480. raw3215_mk_write_req(raw);
  481. /* start or queue request */
  482. raw3215_try_io(raw);
  483. }
  484. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  485. }
  486. }
  487. /*
  488. * Put character routine for 3215 devices
  489. */
  490. static void
  491. raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  492. {
  493. unsigned long flags;
  494. unsigned int length, i;
  495. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  496. if (ch == '\t') {
  497. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  498. raw->line_pos += length;
  499. ch = ' ';
  500. } else if (ch == '\n') {
  501. length = 1;
  502. raw->line_pos = 0;
  503. } else {
  504. length = 1;
  505. raw->line_pos++;
  506. }
  507. raw3215_make_room(raw, length);
  508. for (i = 0; i < length; i++) {
  509. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  510. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  511. raw->count++;
  512. }
  513. if (!(raw->flags & RAW3215_WORKING)) {
  514. raw3215_mk_write_req(raw);
  515. /* start or queue request */
  516. raw3215_try_io(raw);
  517. }
  518. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  519. }
  520. /*
  521. * Flush routine, it simply sets the flush flag and tries to start
  522. * pending IO.
  523. */
  524. static void
  525. raw3215_flush_buffer(struct raw3215_info *raw)
  526. {
  527. unsigned long flags;
  528. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  529. if (raw->count > 0) {
  530. raw->flags |= RAW3215_FLUSHING;
  531. raw3215_try_io(raw);
  532. raw->flags &= ~RAW3215_FLUSHING;
  533. }
  534. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  535. }
  536. /*
  537. * Fire up a 3215 device.
  538. */
  539. static int
  540. raw3215_startup(struct raw3215_info *raw)
  541. {
  542. unsigned long flags;
  543. if (raw->flags & RAW3215_ACTIVE)
  544. return 0;
  545. raw->line_pos = 0;
  546. raw->flags |= RAW3215_ACTIVE;
  547. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  548. raw3215_try_io(raw);
  549. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  550. return 0;
  551. }
  552. /*
  553. * Shutdown a 3215 device.
  554. */
  555. static void
  556. raw3215_shutdown(struct raw3215_info *raw)
  557. {
  558. DECLARE_WAITQUEUE(wait, current);
  559. unsigned long flags;
  560. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  561. return;
  562. /* Wait for outstanding requests, then free irq */
  563. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  564. if ((raw->flags & RAW3215_WORKING) ||
  565. raw->queued_write != NULL ||
  566. raw->queued_read != NULL) {
  567. raw->flags |= RAW3215_CLOSING;
  568. add_wait_queue(&raw->empty_wait, &wait);
  569. set_current_state(TASK_INTERRUPTIBLE);
  570. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  571. schedule();
  572. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  573. remove_wait_queue(&raw->empty_wait, &wait);
  574. set_current_state(TASK_RUNNING);
  575. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  576. }
  577. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  578. }
  579. static int
  580. raw3215_probe (struct ccw_device *cdev)
  581. {
  582. struct raw3215_info *raw;
  583. int line;
  584. /* Console is special. */
  585. if (raw3215[0] && (cdev->dev.driver_data == raw3215[0]))
  586. return 0;
  587. raw = kmalloc(sizeof(struct raw3215_info) +
  588. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  589. if (raw == NULL)
  590. return -ENOMEM;
  591. spin_lock(&raw3215_device_lock);
  592. for (line = 0; line < NR_3215; line++) {
  593. if (!raw3215[line]) {
  594. raw3215[line] = raw;
  595. break;
  596. }
  597. }
  598. spin_unlock(&raw3215_device_lock);
  599. if (line == NR_3215) {
  600. kfree(raw);
  601. return -ENODEV;
  602. }
  603. raw->cdev = cdev;
  604. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  605. memset(raw, 0, sizeof(struct raw3215_info));
  606. raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
  607. GFP_KERNEL|GFP_DMA);
  608. if (raw->buffer == NULL) {
  609. spin_lock(&raw3215_device_lock);
  610. raw3215[line] = NULL;
  611. spin_unlock(&raw3215_device_lock);
  612. kfree(raw);
  613. return -ENOMEM;
  614. }
  615. init_waitqueue_head(&raw->empty_wait);
  616. cdev->dev.driver_data = raw;
  617. cdev->handler = raw3215_irq;
  618. return 0;
  619. }
  620. static void
  621. raw3215_remove (struct ccw_device *cdev)
  622. {
  623. struct raw3215_info *raw;
  624. ccw_device_set_offline(cdev);
  625. raw = cdev->dev.driver_data;
  626. if (raw) {
  627. cdev->dev.driver_data = NULL;
  628. kfree(raw->buffer);
  629. kfree(raw);
  630. }
  631. }
  632. static int
  633. raw3215_set_online (struct ccw_device *cdev)
  634. {
  635. struct raw3215_info *raw;
  636. raw = cdev->dev.driver_data;
  637. if (!raw)
  638. return -ENODEV;
  639. return raw3215_startup(raw);
  640. }
  641. static int
  642. raw3215_set_offline (struct ccw_device *cdev)
  643. {
  644. struct raw3215_info *raw;
  645. raw = cdev->dev.driver_data;
  646. if (!raw)
  647. return -ENODEV;
  648. raw3215_shutdown(raw);
  649. return 0;
  650. }
  651. static struct ccw_device_id raw3215_id[] = {
  652. { CCW_DEVICE(0x3215, 0) },
  653. { /* end of list */ },
  654. };
  655. static struct ccw_driver raw3215_ccw_driver = {
  656. .name = "3215",
  657. .owner = THIS_MODULE,
  658. .ids = raw3215_id,
  659. .probe = &raw3215_probe,
  660. .remove = &raw3215_remove,
  661. .set_online = &raw3215_set_online,
  662. .set_offline = &raw3215_set_offline,
  663. };
  664. #ifdef CONFIG_TN3215_CONSOLE
  665. /*
  666. * Write a string to the 3215 console
  667. */
  668. static void
  669. con3215_write(struct console *co, const char *str, unsigned int count)
  670. {
  671. struct raw3215_info *raw;
  672. int i;
  673. if (count <= 0)
  674. return;
  675. raw = raw3215[0]; /* console 3215 is the first one */
  676. while (count > 0) {
  677. for (i = 0; i < count; i++)
  678. if (str[i] == '\t' || str[i] == '\n')
  679. break;
  680. raw3215_write(raw, str, i);
  681. count -= i;
  682. str += i;
  683. if (count > 0) {
  684. raw3215_putchar(raw, *str);
  685. count--;
  686. str++;
  687. }
  688. }
  689. }
  690. static struct tty_driver *con3215_device(struct console *c, int *index)
  691. {
  692. *index = c->index;
  693. return tty3215_driver;
  694. }
  695. /*
  696. * panic() calls con3215_flush through a panic_notifier
  697. * before the system enters a disabled, endless loop.
  698. */
  699. static void
  700. con3215_flush(void)
  701. {
  702. struct raw3215_info *raw;
  703. unsigned long flags;
  704. raw = raw3215[0]; /* console 3215 is the first one */
  705. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  706. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  707. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  708. }
  709. static int con3215_notify(struct notifier_block *self,
  710. unsigned long event, void *data)
  711. {
  712. con3215_flush();
  713. return NOTIFY_OK;
  714. }
  715. static struct notifier_block on_panic_nb = {
  716. .notifier_call = con3215_notify,
  717. .priority = 0,
  718. };
  719. static struct notifier_block on_reboot_nb = {
  720. .notifier_call = con3215_notify,
  721. .priority = 0,
  722. };
  723. /*
  724. * The console structure for the 3215 console
  725. */
  726. static struct console con3215 = {
  727. .name = "ttyS",
  728. .write = con3215_write,
  729. .device = con3215_device,
  730. .flags = CON_PRINTBUFFER,
  731. };
  732. /*
  733. * 3215 console initialization code called from console_init().
  734. * NOTE: This is called before kmalloc is available.
  735. */
  736. static int __init
  737. con3215_init(void)
  738. {
  739. struct ccw_device *cdev;
  740. struct raw3215_info *raw;
  741. struct raw3215_req *req;
  742. int i;
  743. /* Check if 3215 is to be the console */
  744. if (!CONSOLE_IS_3215)
  745. return -ENODEV;
  746. /* Set the console mode for VM */
  747. if (MACHINE_IS_VM) {
  748. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  749. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  750. }
  751. /* allocate 3215 request structures */
  752. raw3215_freelist = NULL;
  753. spin_lock_init(&raw3215_freelist_lock);
  754. for (i = 0; i < NR_3215_REQ; i++) {
  755. req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
  756. req->next = raw3215_freelist;
  757. raw3215_freelist = req;
  758. }
  759. cdev = ccw_device_probe_console();
  760. if (IS_ERR(cdev))
  761. return -ENODEV;
  762. raw3215[0] = raw = (struct raw3215_info *)
  763. alloc_bootmem_low(sizeof(struct raw3215_info));
  764. memset(raw, 0, sizeof(struct raw3215_info));
  765. raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
  766. raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
  767. raw->cdev = cdev;
  768. cdev->dev.driver_data = raw;
  769. cdev->handler = raw3215_irq;
  770. raw->flags |= RAW3215_FIXED;
  771. init_waitqueue_head(&raw->empty_wait);
  772. /* Request the console irq */
  773. if (raw3215_startup(raw) != 0) {
  774. free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
  775. free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
  776. free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
  777. raw3215[0] = NULL;
  778. return -ENODEV;
  779. }
  780. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  781. register_reboot_notifier(&on_reboot_nb);
  782. register_console(&con3215);
  783. return 0;
  784. }
  785. console_initcall(con3215_init);
  786. #endif
  787. /*
  788. * tty3215_open
  789. *
  790. * This routine is called whenever a 3215 tty is opened.
  791. */
  792. static int
  793. tty3215_open(struct tty_struct *tty, struct file * filp)
  794. {
  795. struct raw3215_info *raw;
  796. int retval, line;
  797. line = tty->index;
  798. if ((line < 0) || (line >= NR_3215))
  799. return -ENODEV;
  800. raw = raw3215[line];
  801. if (raw == NULL)
  802. return -ENODEV;
  803. tty->driver_data = raw;
  804. raw->tty = tty;
  805. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  806. /*
  807. * Start up 3215 device
  808. */
  809. retval = raw3215_startup(raw);
  810. if (retval)
  811. return retval;
  812. return 0;
  813. }
  814. /*
  815. * tty3215_close()
  816. *
  817. * This routine is called when the 3215 tty is closed. We wait
  818. * for the remaining request to be completed. Then we clean up.
  819. */
  820. static void
  821. tty3215_close(struct tty_struct *tty, struct file * filp)
  822. {
  823. struct raw3215_info *raw;
  824. raw = (struct raw3215_info *) tty->driver_data;
  825. if (raw == NULL || tty->count > 1)
  826. return;
  827. tty->closing = 1;
  828. /* Shutdown the terminal */
  829. raw3215_shutdown(raw);
  830. tty->closing = 0;
  831. raw->tty = NULL;
  832. }
  833. /*
  834. * Returns the amount of free space in the output buffer.
  835. */
  836. static int
  837. tty3215_write_room(struct tty_struct *tty)
  838. {
  839. struct raw3215_info *raw;
  840. raw = (struct raw3215_info *) tty->driver_data;
  841. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  842. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  843. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  844. else
  845. return 0;
  846. }
  847. /*
  848. * String write routine for 3215 ttys
  849. */
  850. static int
  851. tty3215_write(struct tty_struct * tty,
  852. const unsigned char *buf, int count)
  853. {
  854. struct raw3215_info *raw;
  855. if (!tty)
  856. return 0;
  857. raw = (struct raw3215_info *) tty->driver_data;
  858. raw3215_write(raw, buf, count);
  859. return count;
  860. }
  861. /*
  862. * Put character routine for 3215 ttys
  863. */
  864. static int
  865. tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  866. {
  867. struct raw3215_info *raw;
  868. if (!tty)
  869. return 0;
  870. raw = (struct raw3215_info *) tty->driver_data;
  871. raw3215_putchar(raw, ch);
  872. return 1;
  873. }
  874. static void
  875. tty3215_flush_chars(struct tty_struct *tty)
  876. {
  877. }
  878. /*
  879. * Returns the number of characters in the output buffer
  880. */
  881. static int
  882. tty3215_chars_in_buffer(struct tty_struct *tty)
  883. {
  884. struct raw3215_info *raw;
  885. raw = (struct raw3215_info *) tty->driver_data;
  886. return raw->count;
  887. }
  888. static void
  889. tty3215_flush_buffer(struct tty_struct *tty)
  890. {
  891. struct raw3215_info *raw;
  892. raw = (struct raw3215_info *) tty->driver_data;
  893. raw3215_flush_buffer(raw);
  894. tty_wakeup(tty);
  895. }
  896. /*
  897. * Currently we don't have any io controls for 3215 ttys
  898. */
  899. static int
  900. tty3215_ioctl(struct tty_struct *tty, struct file * file,
  901. unsigned int cmd, unsigned long arg)
  902. {
  903. if (tty->flags & (1 << TTY_IO_ERROR))
  904. return -EIO;
  905. switch (cmd) {
  906. default:
  907. return -ENOIOCTLCMD;
  908. }
  909. return 0;
  910. }
  911. /*
  912. * Disable reading from a 3215 tty
  913. */
  914. static void
  915. tty3215_throttle(struct tty_struct * tty)
  916. {
  917. struct raw3215_info *raw;
  918. raw = (struct raw3215_info *) tty->driver_data;
  919. raw->flags |= RAW3215_THROTTLED;
  920. }
  921. /*
  922. * Enable reading from a 3215 tty
  923. */
  924. static void
  925. tty3215_unthrottle(struct tty_struct * tty)
  926. {
  927. struct raw3215_info *raw;
  928. unsigned long flags;
  929. raw = (struct raw3215_info *) tty->driver_data;
  930. if (raw->flags & RAW3215_THROTTLED) {
  931. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  932. raw->flags &= ~RAW3215_THROTTLED;
  933. raw3215_try_io(raw);
  934. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  935. }
  936. }
  937. /*
  938. * Disable writing to a 3215 tty
  939. */
  940. static void
  941. tty3215_stop(struct tty_struct *tty)
  942. {
  943. struct raw3215_info *raw;
  944. raw = (struct raw3215_info *) tty->driver_data;
  945. raw->flags |= RAW3215_STOPPED;
  946. }
  947. /*
  948. * Enable writing to a 3215 tty
  949. */
  950. static void
  951. tty3215_start(struct tty_struct *tty)
  952. {
  953. struct raw3215_info *raw;
  954. unsigned long flags;
  955. raw = (struct raw3215_info *) tty->driver_data;
  956. if (raw->flags & RAW3215_STOPPED) {
  957. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  958. raw->flags &= ~RAW3215_STOPPED;
  959. raw3215_try_io(raw);
  960. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  961. }
  962. }
  963. static const struct tty_operations tty3215_ops = {
  964. .open = tty3215_open,
  965. .close = tty3215_close,
  966. .write = tty3215_write,
  967. .put_char = tty3215_put_char,
  968. .flush_chars = tty3215_flush_chars,
  969. .write_room = tty3215_write_room,
  970. .chars_in_buffer = tty3215_chars_in_buffer,
  971. .flush_buffer = tty3215_flush_buffer,
  972. .ioctl = tty3215_ioctl,
  973. .throttle = tty3215_throttle,
  974. .unthrottle = tty3215_unthrottle,
  975. .stop = tty3215_stop,
  976. .start = tty3215_start,
  977. };
  978. /*
  979. * 3215 tty registration code called from tty_init().
  980. * Most kernel services (incl. kmalloc) are available at this poimt.
  981. */
  982. static int __init
  983. tty3215_init(void)
  984. {
  985. struct tty_driver *driver;
  986. int ret;
  987. if (!CONSOLE_IS_3215)
  988. return 0;
  989. driver = alloc_tty_driver(NR_3215);
  990. if (!driver)
  991. return -ENOMEM;
  992. ret = ccw_driver_register(&raw3215_ccw_driver);
  993. if (ret) {
  994. put_tty_driver(driver);
  995. return ret;
  996. }
  997. /*
  998. * Initialize the tty_driver structure
  999. * Entries in tty3215_driver that are NOT initialized:
  1000. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1001. */
  1002. driver->owner = THIS_MODULE;
  1003. driver->driver_name = "tty3215";
  1004. driver->name = "ttyS";
  1005. driver->major = TTY_MAJOR;
  1006. driver->minor_start = 64;
  1007. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1008. driver->subtype = SYSTEM_TYPE_TTY;
  1009. driver->init_termios = tty_std_termios;
  1010. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1011. driver->init_termios.c_oflag = ONLCR | XTABS;
  1012. driver->init_termios.c_lflag = ISIG;
  1013. driver->flags = TTY_DRIVER_REAL_RAW;
  1014. tty_set_operations(driver, &tty3215_ops);
  1015. ret = tty_register_driver(driver);
  1016. if (ret) {
  1017. put_tty_driver(driver);
  1018. return ret;
  1019. }
  1020. tty3215_driver = driver;
  1021. return 0;
  1022. }
  1023. static void __exit
  1024. tty3215_exit(void)
  1025. {
  1026. tty_unregister_driver(tty3215_driver);
  1027. put_tty_driver(tty3215_driver);
  1028. ccw_driver_unregister(&raw3215_ccw_driver);
  1029. }
  1030. module_init(tty3215_init);
  1031. module_exit(tty3215_exit);