raw3270.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. /*
  2. * IBM/3270 Driver - core functions.
  3. *
  4. * Author(s):
  5. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  6. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Copyright IBM Corp. 2003, 2009
  8. */
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/list.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/wait.h>
  17. #include <asm/ccwdev.h>
  18. #include <asm/cio.h>
  19. #include <asm/ebcdic.h>
  20. #include <asm/diag.h>
  21. #include "raw3270.h"
  22. #include <linux/major.h>
  23. #include <linux/kdev_t.h>
  24. #include <linux/device.h>
  25. #include <linux/mutex.h>
  26. static struct class *class3270;
  27. /* The main 3270 data structure. */
  28. struct raw3270 {
  29. struct list_head list;
  30. struct ccw_device *cdev;
  31. int minor;
  32. short model, rows, cols;
  33. unsigned long flags;
  34. struct list_head req_queue; /* Request queue. */
  35. struct list_head view_list; /* List of available views. */
  36. struct raw3270_view *view; /* Active view. */
  37. struct timer_list timer; /* Device timer. */
  38. unsigned char *ascebc; /* ascii -> ebcdic table */
  39. struct device *clttydev; /* 3270-class tty device ptr */
  40. struct device *cltubdev; /* 3270-class tub device ptr */
  41. struct raw3270_request init_request;
  42. unsigned char init_data[256];
  43. };
  44. /* raw3270->flags */
  45. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  46. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  47. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  48. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  49. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  50. #define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
  51. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  52. static DEFINE_MUTEX(raw3270_mutex);
  53. /* List of 3270 devices. */
  54. static LIST_HEAD(raw3270_devices);
  55. /*
  56. * Flag to indicate if the driver has been registered. Some operations
  57. * like waiting for the end of i/o need to be done differently as long
  58. * as the kernel is still starting up (console support).
  59. */
  60. static int raw3270_registered;
  61. /* Module parameters */
  62. static bool tubxcorrect = 0;
  63. module_param(tubxcorrect, bool, 0);
  64. /*
  65. * Wait queue for device init/delete, view delete.
  66. */
  67. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  68. /*
  69. * Encode array for 12 bit 3270 addresses.
  70. */
  71. static unsigned char raw3270_ebcgraf[64] = {
  72. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  73. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  74. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  75. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  76. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  77. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  78. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  79. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  80. };
  81. void
  82. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  83. {
  84. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  85. cp[0] = (addr >> 8) & 0x3f;
  86. cp[1] = addr & 0xff;
  87. } else {
  88. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  89. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  90. }
  91. }
  92. /*
  93. * Allocate a new 3270 ccw request
  94. */
  95. struct raw3270_request *
  96. raw3270_request_alloc(size_t size)
  97. {
  98. struct raw3270_request *rq;
  99. /* Allocate request structure */
  100. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  101. if (!rq)
  102. return ERR_PTR(-ENOMEM);
  103. /* alloc output buffer. */
  104. if (size > 0) {
  105. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  106. if (!rq->buffer) {
  107. kfree(rq);
  108. return ERR_PTR(-ENOMEM);
  109. }
  110. }
  111. rq->size = size;
  112. INIT_LIST_HEAD(&rq->list);
  113. /*
  114. * Setup ccw.
  115. */
  116. rq->ccw.cda = __pa(rq->buffer);
  117. rq->ccw.flags = CCW_FLAG_SLI;
  118. return rq;
  119. }
  120. /*
  121. * Free 3270 ccw request
  122. */
  123. void
  124. raw3270_request_free (struct raw3270_request *rq)
  125. {
  126. kfree(rq->buffer);
  127. kfree(rq);
  128. }
  129. /*
  130. * Reset request to initial state.
  131. */
  132. void
  133. raw3270_request_reset(struct raw3270_request *rq)
  134. {
  135. BUG_ON(!list_empty(&rq->list));
  136. rq->ccw.cmd_code = 0;
  137. rq->ccw.count = 0;
  138. rq->ccw.cda = __pa(rq->buffer);
  139. rq->ccw.flags = CCW_FLAG_SLI;
  140. rq->rescnt = 0;
  141. rq->rc = 0;
  142. }
  143. /*
  144. * Set command code to ccw of a request.
  145. */
  146. void
  147. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  148. {
  149. rq->ccw.cmd_code = cmd;
  150. }
  151. /*
  152. * Add data fragment to output buffer.
  153. */
  154. int
  155. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  156. {
  157. if (size + rq->ccw.count > rq->size)
  158. return -E2BIG;
  159. memcpy(rq->buffer + rq->ccw.count, data, size);
  160. rq->ccw.count += size;
  161. return 0;
  162. }
  163. /*
  164. * Set address/length pair to ccw of a request.
  165. */
  166. void
  167. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  168. {
  169. rq->ccw.cda = __pa(data);
  170. rq->ccw.count = size;
  171. }
  172. /*
  173. * Set idal buffer to ccw of a request.
  174. */
  175. void
  176. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  177. {
  178. rq->ccw.cda = __pa(ib->data);
  179. rq->ccw.count = ib->size;
  180. rq->ccw.flags |= CCW_FLAG_IDA;
  181. }
  182. /*
  183. * Stop running ccw.
  184. */
  185. static int
  186. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  187. {
  188. int retries;
  189. int rc;
  190. if (raw3270_request_final(rq))
  191. return 0;
  192. /* Check if interrupt has already been processed */
  193. for (retries = 0; retries < 5; retries++) {
  194. if (retries < 2)
  195. rc = ccw_device_halt(rp->cdev, (long) rq);
  196. else
  197. rc = ccw_device_clear(rp->cdev, (long) rq);
  198. if (rc == 0)
  199. break; /* termination successful */
  200. }
  201. return rc;
  202. }
  203. static int
  204. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  205. {
  206. unsigned long flags;
  207. int rc;
  208. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  209. rc = raw3270_halt_io_nolock(rp, rq);
  210. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  211. return rc;
  212. }
  213. /*
  214. * Add the request to the request queue, try to start it if the
  215. * 3270 device is idle. Return without waiting for end of i/o.
  216. */
  217. static int
  218. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  219. struct raw3270_request *rq)
  220. {
  221. rq->view = view;
  222. raw3270_get_view(view);
  223. if (list_empty(&rp->req_queue) &&
  224. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  225. /* No other requests are on the queue. Start this one. */
  226. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  227. (unsigned long) rq, 0, 0);
  228. if (rq->rc) {
  229. raw3270_put_view(view);
  230. return rq->rc;
  231. }
  232. }
  233. list_add_tail(&rq->list, &rp->req_queue);
  234. return 0;
  235. }
  236. int
  237. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  238. {
  239. unsigned long flags;
  240. struct raw3270 *rp;
  241. int rc;
  242. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  243. rp = view->dev;
  244. if (!rp || rp->view != view ||
  245. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  246. rc = -EACCES;
  247. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  248. rc = -ENODEV;
  249. else
  250. rc = __raw3270_start(rp, view, rq);
  251. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  252. return rc;
  253. }
  254. int
  255. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  256. {
  257. struct raw3270 *rp;
  258. int rc;
  259. rp = view->dev;
  260. if (!rp || rp->view != view ||
  261. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  262. rc = -EACCES;
  263. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  264. rc = -ENODEV;
  265. else
  266. rc = __raw3270_start(rp, view, rq);
  267. return rc;
  268. }
  269. int
  270. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  271. {
  272. struct raw3270 *rp;
  273. rp = view->dev;
  274. rq->view = view;
  275. raw3270_get_view(view);
  276. list_add_tail(&rq->list, &rp->req_queue);
  277. return 0;
  278. }
  279. /*
  280. * 3270 interrupt routine, called from the ccw_device layer
  281. */
  282. static void
  283. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  284. {
  285. struct raw3270 *rp;
  286. struct raw3270_view *view;
  287. struct raw3270_request *rq;
  288. int rc;
  289. rp = dev_get_drvdata(&cdev->dev);
  290. if (!rp)
  291. return;
  292. rq = (struct raw3270_request *) intparm;
  293. view = rq ? rq->view : rp->view;
  294. if (IS_ERR(irb))
  295. rc = RAW3270_IO_RETRY;
  296. else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  297. rq->rc = -EIO;
  298. rc = RAW3270_IO_DONE;
  299. } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  300. DEV_STAT_UNIT_EXCEP)) {
  301. /* Handle CE-DE-UE and subsequent UDE */
  302. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  303. rc = RAW3270_IO_BUSY;
  304. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  305. /* Wait for UDE if busy flag is set. */
  306. if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  307. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  308. /* Got it, now retry. */
  309. rc = RAW3270_IO_RETRY;
  310. } else
  311. rc = RAW3270_IO_BUSY;
  312. } else if (view)
  313. rc = view->fn->intv(view, rq, irb);
  314. else
  315. rc = RAW3270_IO_DONE;
  316. switch (rc) {
  317. case RAW3270_IO_DONE:
  318. break;
  319. case RAW3270_IO_BUSY:
  320. /*
  321. * Intervention required by the operator. We have to wait
  322. * for unsolicited device end.
  323. */
  324. return;
  325. case RAW3270_IO_RETRY:
  326. if (!rq)
  327. break;
  328. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  329. (unsigned long) rq, 0, 0);
  330. if (rq->rc == 0)
  331. return; /* Successfully restarted. */
  332. break;
  333. case RAW3270_IO_STOP:
  334. if (!rq)
  335. break;
  336. raw3270_halt_io_nolock(rp, rq);
  337. rq->rc = -EIO;
  338. break;
  339. default:
  340. BUG();
  341. }
  342. if (rq) {
  343. BUG_ON(list_empty(&rq->list));
  344. /* The request completed, remove from queue and do callback. */
  345. list_del_init(&rq->list);
  346. if (rq->callback)
  347. rq->callback(rq, rq->callback_data);
  348. /* Do put_device for get_device in raw3270_start. */
  349. raw3270_put_view(view);
  350. }
  351. /*
  352. * Try to start each request on request queue until one is
  353. * started successful.
  354. */
  355. while (!list_empty(&rp->req_queue)) {
  356. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  357. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  358. (unsigned long) rq, 0, 0);
  359. if (rq->rc == 0)
  360. break;
  361. /* Start failed. Remove request and do callback. */
  362. list_del_init(&rq->list);
  363. if (rq->callback)
  364. rq->callback(rq, rq->callback_data);
  365. /* Do put_device for get_device in raw3270_start. */
  366. raw3270_put_view(view);
  367. }
  368. }
  369. /*
  370. * Size sensing.
  371. */
  372. struct raw3270_ua { /* Query Reply structure for Usable Area */
  373. struct { /* Usable Area Query Reply Base */
  374. short l; /* Length of this structured field */
  375. char sfid; /* 0x81 if Query Reply */
  376. char qcode; /* 0x81 if Usable Area */
  377. char flags0;
  378. char flags1;
  379. short w; /* Width of usable area */
  380. short h; /* Heigth of usavle area */
  381. char units; /* 0x00:in; 0x01:mm */
  382. int xr;
  383. int yr;
  384. char aw;
  385. char ah;
  386. short buffsz; /* Character buffer size, bytes */
  387. char xmin;
  388. char ymin;
  389. char xmax;
  390. char ymax;
  391. } __attribute__ ((packed)) uab;
  392. struct { /* Alternate Usable Area Self-Defining Parameter */
  393. char l; /* Length of this Self-Defining Parm */
  394. char sdpid; /* 0x02 if Alternate Usable Area */
  395. char res;
  396. char auaid; /* 0x01 is Id for the A U A */
  397. short wauai; /* Width of AUAi */
  398. short hauai; /* Height of AUAi */
  399. char auaunits; /* 0x00:in, 0x01:mm */
  400. int auaxr;
  401. int auayr;
  402. char awauai;
  403. char ahauai;
  404. } __attribute__ ((packed)) aua;
  405. } __attribute__ ((packed));
  406. static struct diag210 raw3270_init_diag210;
  407. static DEFINE_MUTEX(raw3270_init_mutex);
  408. static int
  409. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  410. struct irb *irb)
  411. {
  412. /*
  413. * Unit-Check Processing:
  414. * Expect Command Reject or Intervention Required.
  415. */
  416. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  417. /* Request finished abnormally. */
  418. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  419. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  420. return RAW3270_IO_BUSY;
  421. }
  422. }
  423. if (rq) {
  424. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  425. if (irb->ecw[0] & SNS0_CMD_REJECT)
  426. rq->rc = -EOPNOTSUPP;
  427. else
  428. rq->rc = -EIO;
  429. } else
  430. /* Request finished normally. Copy residual count. */
  431. rq->rescnt = irb->scsw.cmd.count;
  432. }
  433. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  434. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  435. wake_up(&raw3270_wait_queue);
  436. }
  437. return RAW3270_IO_DONE;
  438. }
  439. static struct raw3270_fn raw3270_init_fn = {
  440. .intv = raw3270_init_irq
  441. };
  442. static struct raw3270_view raw3270_init_view = {
  443. .fn = &raw3270_init_fn
  444. };
  445. /*
  446. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  447. * Wait for end of request. The request must have been started
  448. * with raw3270_start, rc = 0. The device lock may NOT have been
  449. * released between calling raw3270_start and raw3270_wait.
  450. */
  451. static void
  452. raw3270_wake_init(struct raw3270_request *rq, void *data)
  453. {
  454. wake_up((wait_queue_head_t *) data);
  455. }
  456. /*
  457. * Special wait function that can cope with console initialization.
  458. */
  459. static int
  460. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  461. struct raw3270_request *rq)
  462. {
  463. unsigned long flags;
  464. int rc;
  465. #ifdef CONFIG_TN3270_CONSOLE
  466. if (raw3270_registered == 0) {
  467. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  468. rq->callback = NULL;
  469. rc = __raw3270_start(rp, view, rq);
  470. if (rc == 0)
  471. while (!raw3270_request_final(rq)) {
  472. wait_cons_dev();
  473. barrier();
  474. }
  475. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  476. return rq->rc;
  477. }
  478. #endif
  479. rq->callback = raw3270_wake_init;
  480. rq->callback_data = &raw3270_wait_queue;
  481. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  482. rc = __raw3270_start(rp, view, rq);
  483. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  484. if (rc)
  485. return rc;
  486. /* Now wait for the completion. */
  487. rc = wait_event_interruptible(raw3270_wait_queue,
  488. raw3270_request_final(rq));
  489. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  490. raw3270_halt_io(view->dev, rq);
  491. /* No wait for the halt to complete. */
  492. wait_event(raw3270_wait_queue, raw3270_request_final(rq));
  493. return -ERESTARTSYS;
  494. }
  495. return rq->rc;
  496. }
  497. static int
  498. __raw3270_size_device_vm(struct raw3270 *rp)
  499. {
  500. int rc, model;
  501. struct ccw_dev_id dev_id;
  502. ccw_device_get_id(rp->cdev, &dev_id);
  503. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  504. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  505. rc = diag210(&raw3270_init_diag210);
  506. if (rc)
  507. return rc;
  508. model = raw3270_init_diag210.vrdccrmd;
  509. switch (model) {
  510. case 2:
  511. rp->model = model;
  512. rp->rows = 24;
  513. rp->cols = 80;
  514. break;
  515. case 3:
  516. rp->model = model;
  517. rp->rows = 32;
  518. rp->cols = 80;
  519. break;
  520. case 4:
  521. rp->model = model;
  522. rp->rows = 43;
  523. rp->cols = 80;
  524. break;
  525. case 5:
  526. rp->model = model;
  527. rp->rows = 27;
  528. rp->cols = 132;
  529. break;
  530. default:
  531. rc = -EOPNOTSUPP;
  532. break;
  533. }
  534. return rc;
  535. }
  536. static int
  537. __raw3270_size_device(struct raw3270 *rp)
  538. {
  539. static const unsigned char wbuf[] =
  540. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  541. struct raw3270_ua *uap;
  542. int rc;
  543. /*
  544. * To determine the size of the 3270 device we need to do:
  545. * 1) send a 'read partition' data stream to the device
  546. * 2) wait for the attn interrupt that precedes the query reply
  547. * 3) do a read modified to get the query reply
  548. * To make things worse we have to cope with intervention
  549. * required (3270 device switched to 'stand-by') and command
  550. * rejects (old devices that can't do 'read partition').
  551. */
  552. memset(&rp->init_request, 0, sizeof(rp->init_request));
  553. memset(&rp->init_data, 0, 256);
  554. /* Store 'read partition' data stream to init_data */
  555. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  556. INIT_LIST_HEAD(&rp->init_request.list);
  557. rp->init_request.ccw.cmd_code = TC_WRITESF;
  558. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  559. rp->init_request.ccw.count = sizeof(wbuf);
  560. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  561. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  562. if (rc)
  563. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  564. return rc;
  565. /* Wait for attention interrupt. */
  566. #ifdef CONFIG_TN3270_CONSOLE
  567. if (raw3270_registered == 0) {
  568. unsigned long flags;
  569. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  570. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  571. wait_cons_dev();
  572. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  573. } else
  574. #endif
  575. rc = wait_event_interruptible(raw3270_wait_queue,
  576. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  577. if (rc)
  578. return rc;
  579. /*
  580. * The device accepted the 'read partition' command. Now
  581. * set up a read ccw and issue it.
  582. */
  583. rp->init_request.ccw.cmd_code = TC_READMOD;
  584. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  585. rp->init_request.ccw.count = sizeof(rp->init_data);
  586. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  587. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  588. if (rc)
  589. return rc;
  590. /* Got a Query Reply */
  591. uap = (struct raw3270_ua *) (rp->init_data + 1);
  592. /* Paranoia check. */
  593. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  594. return -EOPNOTSUPP;
  595. /* Copy rows/columns of default Usable Area */
  596. rp->rows = uap->uab.h;
  597. rp->cols = uap->uab.w;
  598. /* Check for 14 bit addressing */
  599. if ((uap->uab.flags0 & 0x0d) == 0x01)
  600. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  601. /* Check for Alternate Usable Area */
  602. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  603. uap->aua.sdpid == 0x02) {
  604. rp->rows = uap->aua.hauai;
  605. rp->cols = uap->aua.wauai;
  606. }
  607. return 0;
  608. }
  609. static int
  610. raw3270_size_device(struct raw3270 *rp)
  611. {
  612. int rc;
  613. mutex_lock(&raw3270_init_mutex);
  614. rp->view = &raw3270_init_view;
  615. raw3270_init_view.dev = rp;
  616. if (MACHINE_IS_VM)
  617. rc = __raw3270_size_device_vm(rp);
  618. else
  619. rc = __raw3270_size_device(rp);
  620. raw3270_init_view.dev = NULL;
  621. rp->view = NULL;
  622. mutex_unlock(&raw3270_init_mutex);
  623. if (rc == 0) { /* Found something. */
  624. /* Try to find a model. */
  625. rp->model = 0;
  626. if (rp->rows == 24 && rp->cols == 80)
  627. rp->model = 2;
  628. if (rp->rows == 32 && rp->cols == 80)
  629. rp->model = 3;
  630. if (rp->rows == 43 && rp->cols == 80)
  631. rp->model = 4;
  632. if (rp->rows == 27 && rp->cols == 132)
  633. rp->model = 5;
  634. } else {
  635. /* Couldn't detect size. Use default model 2. */
  636. rp->model = 2;
  637. rp->rows = 24;
  638. rp->cols = 80;
  639. return 0;
  640. }
  641. return rc;
  642. }
  643. static int
  644. raw3270_reset_device(struct raw3270 *rp)
  645. {
  646. int rc;
  647. mutex_lock(&raw3270_init_mutex);
  648. memset(&rp->init_request, 0, sizeof(rp->init_request));
  649. memset(&rp->init_data, 0, sizeof(rp->init_data));
  650. /* Store reset data stream to init_data/init_request */
  651. rp->init_data[0] = TW_KR;
  652. INIT_LIST_HEAD(&rp->init_request.list);
  653. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  654. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  655. rp->init_request.ccw.count = 1;
  656. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  657. rp->view = &raw3270_init_view;
  658. raw3270_init_view.dev = rp;
  659. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  660. raw3270_init_view.dev = NULL;
  661. rp->view = NULL;
  662. mutex_unlock(&raw3270_init_mutex);
  663. return rc;
  664. }
  665. int
  666. raw3270_reset(struct raw3270_view *view)
  667. {
  668. struct raw3270 *rp;
  669. int rc;
  670. rp = view->dev;
  671. if (!rp || rp->view != view ||
  672. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  673. rc = -EACCES;
  674. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  675. rc = -ENODEV;
  676. else
  677. rc = raw3270_reset_device(view->dev);
  678. return rc;
  679. }
  680. /*
  681. * Setup new 3270 device.
  682. */
  683. static int
  684. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  685. {
  686. struct list_head *l;
  687. struct raw3270 *tmp;
  688. int minor;
  689. memset(rp, 0, sizeof(struct raw3270));
  690. /* Copy ebcdic -> ascii translation table. */
  691. memcpy(ascebc, _ascebc, 256);
  692. if (tubxcorrect) {
  693. /* correct brackets and circumflex */
  694. ascebc['['] = 0xad;
  695. ascebc[']'] = 0xbd;
  696. ascebc['^'] = 0xb0;
  697. }
  698. rp->ascebc = ascebc;
  699. /* Set defaults. */
  700. rp->rows = 24;
  701. rp->cols = 80;
  702. INIT_LIST_HEAD(&rp->req_queue);
  703. INIT_LIST_HEAD(&rp->view_list);
  704. /*
  705. * Add device to list and find the smallest unused minor
  706. * number for it. Note: there is no device with minor 0,
  707. * see special case for fs3270.c:fs3270_open().
  708. */
  709. mutex_lock(&raw3270_mutex);
  710. /* Keep the list sorted. */
  711. minor = RAW3270_FIRSTMINOR;
  712. rp->minor = -1;
  713. list_for_each(l, &raw3270_devices) {
  714. tmp = list_entry(l, struct raw3270, list);
  715. if (tmp->minor > minor) {
  716. rp->minor = minor;
  717. __list_add(&rp->list, l->prev, l);
  718. break;
  719. }
  720. minor++;
  721. }
  722. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  723. rp->minor = minor;
  724. list_add_tail(&rp->list, &raw3270_devices);
  725. }
  726. mutex_unlock(&raw3270_mutex);
  727. /* No free minor number? Then give up. */
  728. if (rp->minor == -1)
  729. return -EUSERS;
  730. rp->cdev = cdev;
  731. dev_set_drvdata(&cdev->dev, rp);
  732. cdev->handler = raw3270_irq;
  733. return 0;
  734. }
  735. #ifdef CONFIG_TN3270_CONSOLE
  736. /*
  737. * Setup 3270 device configured as console.
  738. */
  739. struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
  740. {
  741. struct raw3270 *rp;
  742. char *ascebc;
  743. int rc;
  744. rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  745. ascebc = kzalloc(256, GFP_KERNEL);
  746. rc = raw3270_setup_device(cdev, rp, ascebc);
  747. if (rc)
  748. return ERR_PTR(rc);
  749. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  750. rc = raw3270_reset_device(rp);
  751. if (rc)
  752. return ERR_PTR(rc);
  753. rc = raw3270_size_device(rp);
  754. if (rc)
  755. return ERR_PTR(rc);
  756. rc = raw3270_reset_device(rp);
  757. if (rc)
  758. return ERR_PTR(rc);
  759. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  760. return rp;
  761. }
  762. void
  763. raw3270_wait_cons_dev(struct raw3270 *rp)
  764. {
  765. unsigned long flags;
  766. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  767. wait_cons_dev();
  768. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  769. }
  770. #endif
  771. /*
  772. * Create a 3270 device structure.
  773. */
  774. static struct raw3270 *
  775. raw3270_create_device(struct ccw_device *cdev)
  776. {
  777. struct raw3270 *rp;
  778. char *ascebc;
  779. int rc;
  780. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  781. if (!rp)
  782. return ERR_PTR(-ENOMEM);
  783. ascebc = kmalloc(256, GFP_KERNEL);
  784. if (!ascebc) {
  785. kfree(rp);
  786. return ERR_PTR(-ENOMEM);
  787. }
  788. rc = raw3270_setup_device(cdev, rp, ascebc);
  789. if (rc) {
  790. kfree(rp->ascebc);
  791. kfree(rp);
  792. rp = ERR_PTR(rc);
  793. }
  794. /* Get reference to ccw_device structure. */
  795. get_device(&cdev->dev);
  796. return rp;
  797. }
  798. /*
  799. * Activate a view.
  800. */
  801. int
  802. raw3270_activate_view(struct raw3270_view *view)
  803. {
  804. struct raw3270 *rp;
  805. struct raw3270_view *oldview, *nv;
  806. unsigned long flags;
  807. int rc;
  808. rp = view->dev;
  809. if (!rp)
  810. return -ENODEV;
  811. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  812. if (rp->view == view)
  813. rc = 0;
  814. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  815. rc = -ENODEV;
  816. else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  817. rc = -EACCES;
  818. else {
  819. oldview = NULL;
  820. if (rp->view) {
  821. oldview = rp->view;
  822. oldview->fn->deactivate(oldview);
  823. }
  824. rp->view = view;
  825. rc = view->fn->activate(view);
  826. if (rc) {
  827. /* Didn't work. Try to reactivate the old view. */
  828. rp->view = oldview;
  829. if (!oldview || oldview->fn->activate(oldview) != 0) {
  830. /* Didn't work as well. Try any other view. */
  831. list_for_each_entry(nv, &rp->view_list, list)
  832. if (nv != view && nv != oldview) {
  833. rp->view = nv;
  834. if (nv->fn->activate(nv) == 0)
  835. break;
  836. rp->view = NULL;
  837. }
  838. }
  839. }
  840. }
  841. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  842. return rc;
  843. }
  844. /*
  845. * Deactivate current view.
  846. */
  847. void
  848. raw3270_deactivate_view(struct raw3270_view *view)
  849. {
  850. unsigned long flags;
  851. struct raw3270 *rp;
  852. rp = view->dev;
  853. if (!rp)
  854. return;
  855. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  856. if (rp->view == view) {
  857. view->fn->deactivate(view);
  858. rp->view = NULL;
  859. /* Move deactivated view to end of list. */
  860. list_del_init(&view->list);
  861. list_add_tail(&view->list, &rp->view_list);
  862. /* Try to activate another view. */
  863. if (test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  864. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  865. list_for_each_entry(view, &rp->view_list, list) {
  866. rp->view = view;
  867. if (view->fn->activate(view) == 0)
  868. break;
  869. rp->view = NULL;
  870. }
  871. }
  872. }
  873. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  874. }
  875. /*
  876. * Add view to device with minor "minor".
  877. */
  878. int
  879. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  880. {
  881. unsigned long flags;
  882. struct raw3270 *rp;
  883. int rc;
  884. if (minor <= 0)
  885. return -ENODEV;
  886. mutex_lock(&raw3270_mutex);
  887. rc = -ENODEV;
  888. list_for_each_entry(rp, &raw3270_devices, list) {
  889. if (rp->minor != minor)
  890. continue;
  891. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  892. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  893. atomic_set(&view->ref_count, 2);
  894. view->dev = rp;
  895. view->fn = fn;
  896. view->model = rp->model;
  897. view->rows = rp->rows;
  898. view->cols = rp->cols;
  899. view->ascebc = rp->ascebc;
  900. spin_lock_init(&view->lock);
  901. list_add(&view->list, &rp->view_list);
  902. rc = 0;
  903. }
  904. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  905. break;
  906. }
  907. mutex_unlock(&raw3270_mutex);
  908. return rc;
  909. }
  910. /*
  911. * Find specific view of device with minor "minor".
  912. */
  913. struct raw3270_view *
  914. raw3270_find_view(struct raw3270_fn *fn, int minor)
  915. {
  916. struct raw3270 *rp;
  917. struct raw3270_view *view, *tmp;
  918. unsigned long flags;
  919. mutex_lock(&raw3270_mutex);
  920. view = ERR_PTR(-ENODEV);
  921. list_for_each_entry(rp, &raw3270_devices, list) {
  922. if (rp->minor != minor)
  923. continue;
  924. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  925. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  926. view = ERR_PTR(-ENOENT);
  927. list_for_each_entry(tmp, &rp->view_list, list) {
  928. if (tmp->fn == fn) {
  929. raw3270_get_view(tmp);
  930. view = tmp;
  931. break;
  932. }
  933. }
  934. }
  935. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  936. break;
  937. }
  938. mutex_unlock(&raw3270_mutex);
  939. return view;
  940. }
  941. /*
  942. * Remove view from device and free view structure via call to view->fn->free.
  943. */
  944. void
  945. raw3270_del_view(struct raw3270_view *view)
  946. {
  947. unsigned long flags;
  948. struct raw3270 *rp;
  949. struct raw3270_view *nv;
  950. rp = view->dev;
  951. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  952. if (rp->view == view) {
  953. view->fn->deactivate(view);
  954. rp->view = NULL;
  955. }
  956. list_del_init(&view->list);
  957. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  958. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  959. /* Try to activate another view. */
  960. list_for_each_entry(nv, &rp->view_list, list) {
  961. if (nv->fn->activate(nv) == 0) {
  962. rp->view = nv;
  963. break;
  964. }
  965. }
  966. }
  967. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  968. /* Wait for reference counter to drop to zero. */
  969. atomic_dec(&view->ref_count);
  970. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  971. if (view->fn->free)
  972. view->fn->free(view);
  973. }
  974. /*
  975. * Remove a 3270 device structure.
  976. */
  977. static void
  978. raw3270_delete_device(struct raw3270 *rp)
  979. {
  980. struct ccw_device *cdev;
  981. /* Remove from device chain. */
  982. mutex_lock(&raw3270_mutex);
  983. if (rp->clttydev && !IS_ERR(rp->clttydev))
  984. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  985. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  986. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
  987. list_del_init(&rp->list);
  988. mutex_unlock(&raw3270_mutex);
  989. /* Disconnect from ccw_device. */
  990. cdev = rp->cdev;
  991. rp->cdev = NULL;
  992. dev_set_drvdata(&cdev->dev, NULL);
  993. cdev->handler = NULL;
  994. /* Put ccw_device structure. */
  995. put_device(&cdev->dev);
  996. /* Now free raw3270 structure. */
  997. kfree(rp->ascebc);
  998. kfree(rp);
  999. }
  1000. static int
  1001. raw3270_probe (struct ccw_device *cdev)
  1002. {
  1003. return 0;
  1004. }
  1005. /*
  1006. * Additional attributes for a 3270 device
  1007. */
  1008. static ssize_t
  1009. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1010. {
  1011. return snprintf(buf, PAGE_SIZE, "%i\n",
  1012. ((struct raw3270 *) dev_get_drvdata(dev))->model);
  1013. }
  1014. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1015. static ssize_t
  1016. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1017. {
  1018. return snprintf(buf, PAGE_SIZE, "%i\n",
  1019. ((struct raw3270 *) dev_get_drvdata(dev))->rows);
  1020. }
  1021. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1022. static ssize_t
  1023. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1024. {
  1025. return snprintf(buf, PAGE_SIZE, "%i\n",
  1026. ((struct raw3270 *) dev_get_drvdata(dev))->cols);
  1027. }
  1028. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1029. static struct attribute * raw3270_attrs[] = {
  1030. &dev_attr_model.attr,
  1031. &dev_attr_rows.attr,
  1032. &dev_attr_columns.attr,
  1033. NULL,
  1034. };
  1035. static struct attribute_group raw3270_attr_group = {
  1036. .attrs = raw3270_attrs,
  1037. };
  1038. static int raw3270_create_attributes(struct raw3270 *rp)
  1039. {
  1040. int rc;
  1041. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1042. if (rc)
  1043. goto out;
  1044. rp->clttydev = device_create(class3270, &rp->cdev->dev,
  1045. MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
  1046. "tty%s", dev_name(&rp->cdev->dev));
  1047. if (IS_ERR(rp->clttydev)) {
  1048. rc = PTR_ERR(rp->clttydev);
  1049. goto out_ttydev;
  1050. }
  1051. rp->cltubdev = device_create(class3270, &rp->cdev->dev,
  1052. MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
  1053. "tub%s", dev_name(&rp->cdev->dev));
  1054. if (!IS_ERR(rp->cltubdev))
  1055. goto out;
  1056. rc = PTR_ERR(rp->cltubdev);
  1057. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1058. out_ttydev:
  1059. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1060. out:
  1061. return rc;
  1062. }
  1063. /*
  1064. * Notifier for device addition/removal
  1065. */
  1066. struct raw3270_notifier {
  1067. struct list_head list;
  1068. void (*notifier)(int, int);
  1069. };
  1070. static LIST_HEAD(raw3270_notifier);
  1071. int raw3270_register_notifier(void (*notifier)(int, int))
  1072. {
  1073. struct raw3270_notifier *np;
  1074. struct raw3270 *rp;
  1075. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1076. if (!np)
  1077. return -ENOMEM;
  1078. np->notifier = notifier;
  1079. mutex_lock(&raw3270_mutex);
  1080. list_add_tail(&np->list, &raw3270_notifier);
  1081. list_for_each_entry(rp, &raw3270_devices, list) {
  1082. get_device(&rp->cdev->dev);
  1083. notifier(rp->minor, 1);
  1084. }
  1085. mutex_unlock(&raw3270_mutex);
  1086. return 0;
  1087. }
  1088. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1089. {
  1090. struct raw3270_notifier *np;
  1091. mutex_lock(&raw3270_mutex);
  1092. list_for_each_entry(np, &raw3270_notifier, list)
  1093. if (np->notifier == notifier) {
  1094. list_del(&np->list);
  1095. kfree(np);
  1096. break;
  1097. }
  1098. mutex_unlock(&raw3270_mutex);
  1099. }
  1100. /*
  1101. * Set 3270 device online.
  1102. */
  1103. static int
  1104. raw3270_set_online (struct ccw_device *cdev)
  1105. {
  1106. struct raw3270 *rp;
  1107. struct raw3270_notifier *np;
  1108. int rc;
  1109. rp = raw3270_create_device(cdev);
  1110. if (IS_ERR(rp))
  1111. return PTR_ERR(rp);
  1112. rc = raw3270_reset_device(rp);
  1113. if (rc)
  1114. goto failure;
  1115. rc = raw3270_size_device(rp);
  1116. if (rc)
  1117. goto failure;
  1118. rc = raw3270_reset_device(rp);
  1119. if (rc)
  1120. goto failure;
  1121. rc = raw3270_create_attributes(rp);
  1122. if (rc)
  1123. goto failure;
  1124. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1125. mutex_lock(&raw3270_mutex);
  1126. list_for_each_entry(np, &raw3270_notifier, list)
  1127. np->notifier(rp->minor, 1);
  1128. mutex_unlock(&raw3270_mutex);
  1129. return 0;
  1130. failure:
  1131. raw3270_delete_device(rp);
  1132. return rc;
  1133. }
  1134. /*
  1135. * Remove 3270 device structure.
  1136. */
  1137. static void
  1138. raw3270_remove (struct ccw_device *cdev)
  1139. {
  1140. unsigned long flags;
  1141. struct raw3270 *rp;
  1142. struct raw3270_view *v;
  1143. struct raw3270_notifier *np;
  1144. rp = dev_get_drvdata(&cdev->dev);
  1145. /*
  1146. * _remove is the opposite of _probe; it's probe that
  1147. * should set up rp. raw3270_remove gets entered for
  1148. * devices even if they haven't been varied online.
  1149. * Thus, rp may validly be NULL here.
  1150. */
  1151. if (rp == NULL)
  1152. return;
  1153. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1154. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1155. /* Deactivate current view and remove all views. */
  1156. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1157. if (rp->view) {
  1158. rp->view->fn->deactivate(rp->view);
  1159. rp->view = NULL;
  1160. }
  1161. while (!list_empty(&rp->view_list)) {
  1162. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1163. if (v->fn->release)
  1164. v->fn->release(v);
  1165. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1166. raw3270_del_view(v);
  1167. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1168. }
  1169. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1170. mutex_lock(&raw3270_mutex);
  1171. list_for_each_entry(np, &raw3270_notifier, list)
  1172. np->notifier(rp->minor, 0);
  1173. mutex_unlock(&raw3270_mutex);
  1174. /* Reset 3270 device. */
  1175. raw3270_reset_device(rp);
  1176. /* And finally remove it. */
  1177. raw3270_delete_device(rp);
  1178. }
  1179. /*
  1180. * Set 3270 device offline.
  1181. */
  1182. static int
  1183. raw3270_set_offline (struct ccw_device *cdev)
  1184. {
  1185. struct raw3270 *rp;
  1186. rp = dev_get_drvdata(&cdev->dev);
  1187. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1188. return -EBUSY;
  1189. raw3270_remove(cdev);
  1190. return 0;
  1191. }
  1192. static int raw3270_pm_stop(struct ccw_device *cdev)
  1193. {
  1194. struct raw3270 *rp;
  1195. struct raw3270_view *view;
  1196. unsigned long flags;
  1197. rp = dev_get_drvdata(&cdev->dev);
  1198. if (!rp)
  1199. return 0;
  1200. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1201. if (rp->view)
  1202. rp->view->fn->deactivate(rp->view);
  1203. if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
  1204. /*
  1205. * Release tty and fullscreen for all non-console
  1206. * devices.
  1207. */
  1208. list_for_each_entry(view, &rp->view_list, list) {
  1209. if (view->fn->release)
  1210. view->fn->release(view);
  1211. }
  1212. }
  1213. set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1214. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1215. return 0;
  1216. }
  1217. static int raw3270_pm_start(struct ccw_device *cdev)
  1218. {
  1219. struct raw3270 *rp;
  1220. unsigned long flags;
  1221. rp = dev_get_drvdata(&cdev->dev);
  1222. if (!rp)
  1223. return 0;
  1224. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1225. clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1226. if (rp->view)
  1227. rp->view->fn->activate(rp->view);
  1228. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1229. return 0;
  1230. }
  1231. void raw3270_pm_unfreeze(struct raw3270_view *view)
  1232. {
  1233. #ifdef CONFIG_TN3270_CONSOLE
  1234. struct raw3270 *rp;
  1235. rp = view->dev;
  1236. if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  1237. ccw_device_force_console();
  1238. #endif
  1239. }
  1240. static struct ccw_device_id raw3270_id[] = {
  1241. { CCW_DEVICE(0x3270, 0) },
  1242. { CCW_DEVICE(0x3271, 0) },
  1243. { CCW_DEVICE(0x3272, 0) },
  1244. { CCW_DEVICE(0x3273, 0) },
  1245. { CCW_DEVICE(0x3274, 0) },
  1246. { CCW_DEVICE(0x3275, 0) },
  1247. { CCW_DEVICE(0x3276, 0) },
  1248. { CCW_DEVICE(0x3277, 0) },
  1249. { CCW_DEVICE(0x3278, 0) },
  1250. { CCW_DEVICE(0x3279, 0) },
  1251. { CCW_DEVICE(0x3174, 0) },
  1252. { /* end of list */ },
  1253. };
  1254. static struct ccw_driver raw3270_ccw_driver = {
  1255. .driver = {
  1256. .name = "3270",
  1257. .owner = THIS_MODULE,
  1258. },
  1259. .ids = raw3270_id,
  1260. .probe = &raw3270_probe,
  1261. .remove = &raw3270_remove,
  1262. .set_online = &raw3270_set_online,
  1263. .set_offline = &raw3270_set_offline,
  1264. .freeze = &raw3270_pm_stop,
  1265. .thaw = &raw3270_pm_start,
  1266. .restore = &raw3270_pm_start,
  1267. .int_class = IOINT_C70,
  1268. };
  1269. static int
  1270. raw3270_init(void)
  1271. {
  1272. struct raw3270 *rp;
  1273. int rc;
  1274. if (raw3270_registered)
  1275. return 0;
  1276. raw3270_registered = 1;
  1277. rc = ccw_driver_register(&raw3270_ccw_driver);
  1278. if (rc == 0) {
  1279. /* Create attributes for early (= console) device. */
  1280. mutex_lock(&raw3270_mutex);
  1281. class3270 = class_create(THIS_MODULE, "3270");
  1282. list_for_each_entry(rp, &raw3270_devices, list) {
  1283. get_device(&rp->cdev->dev);
  1284. raw3270_create_attributes(rp);
  1285. }
  1286. mutex_unlock(&raw3270_mutex);
  1287. }
  1288. return rc;
  1289. }
  1290. static void
  1291. raw3270_exit(void)
  1292. {
  1293. ccw_driver_unregister(&raw3270_ccw_driver);
  1294. class_destroy(class3270);
  1295. }
  1296. MODULE_LICENSE("GPL");
  1297. module_init(raw3270_init);
  1298. module_exit(raw3270_exit);
  1299. EXPORT_SYMBOL(raw3270_request_alloc);
  1300. EXPORT_SYMBOL(raw3270_request_free);
  1301. EXPORT_SYMBOL(raw3270_request_reset);
  1302. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1303. EXPORT_SYMBOL(raw3270_request_add_data);
  1304. EXPORT_SYMBOL(raw3270_request_set_data);
  1305. EXPORT_SYMBOL(raw3270_request_set_idal);
  1306. EXPORT_SYMBOL(raw3270_buffer_address);
  1307. EXPORT_SYMBOL(raw3270_add_view);
  1308. EXPORT_SYMBOL(raw3270_del_view);
  1309. EXPORT_SYMBOL(raw3270_find_view);
  1310. EXPORT_SYMBOL(raw3270_activate_view);
  1311. EXPORT_SYMBOL(raw3270_deactivate_view);
  1312. EXPORT_SYMBOL(raw3270_start);
  1313. EXPORT_SYMBOL(raw3270_start_locked);
  1314. EXPORT_SYMBOL(raw3270_start_irq);
  1315. EXPORT_SYMBOL(raw3270_reset);
  1316. EXPORT_SYMBOL(raw3270_register_notifier);
  1317. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1318. EXPORT_SYMBOL(raw3270_wait_queue);