con3215.c 30 KB

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