vmur.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Linux driver for System z and s390 unit record devices
  3. * (z/VM virtual punch, reader, printer)
  4. *
  5. * Copyright IBM Corp. 2001, 2007
  6. * Authors: Malcolm Beattie <beattiem@uk.ibm.com>
  7. * Michael Holzheu <holzheu@de.ibm.com>
  8. * Frank Munzert <munzert@de.ibm.com>
  9. */
  10. #include <linux/cdev.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/cio.h>
  13. #include <asm/ccwdev.h>
  14. #include <asm/debug.h>
  15. #include "vmur.h"
  16. /*
  17. * Driver overview
  18. *
  19. * Unit record device support is implemented as a character device driver.
  20. * We can fit at least 16 bits into a device minor number and use the
  21. * simple method of mapping a character device number with minor abcd
  22. * to the unit record device with devno abcd.
  23. * I/O to virtual unit record devices is handled as follows:
  24. * Reads: Diagnose code 0x14 (input spool file manipulation)
  25. * is used to read spool data page-wise.
  26. * Writes: The CCW used is WRITE_CCW_CMD (0x01). The device's record length
  27. * is available by reading sysfs attr reclen. Each write() to the device
  28. * must specify an integral multiple (maximal 511) of reclen.
  29. */
  30. static char ur_banner[] = "z/VM virtual unit record device driver";
  31. MODULE_AUTHOR("IBM Corporation");
  32. MODULE_DESCRIPTION("s390 z/VM virtual unit record device driver");
  33. MODULE_LICENSE("GPL");
  34. #define PRINTK_HEADER "vmur: "
  35. static dev_t ur_first_dev_maj_min;
  36. static struct class *vmur_class;
  37. static struct debug_info *vmur_dbf;
  38. /* We put the device's record length (for writes) in the driver_info field */
  39. static struct ccw_device_id ur_ids[] = {
  40. { CCWDEV_CU_DI(READER_PUNCH_DEVTYPE, 80) },
  41. { CCWDEV_CU_DI(PRINTER_DEVTYPE, 132) },
  42. { /* end of list */ }
  43. };
  44. MODULE_DEVICE_TABLE(ccw, ur_ids);
  45. static int ur_probe(struct ccw_device *cdev);
  46. static void ur_remove(struct ccw_device *cdev);
  47. static int ur_set_online(struct ccw_device *cdev);
  48. static int ur_set_offline(struct ccw_device *cdev);
  49. static struct ccw_driver ur_driver = {
  50. .name = "vmur",
  51. .owner = THIS_MODULE,
  52. .ids = ur_ids,
  53. .probe = ur_probe,
  54. .remove = ur_remove,
  55. .set_online = ur_set_online,
  56. .set_offline = ur_set_offline,
  57. };
  58. /*
  59. * Allocation, freeing, getting and putting of urdev structures
  60. */
  61. static struct urdev *urdev_alloc(struct ccw_device *cdev)
  62. {
  63. struct urdev *urd;
  64. urd = kzalloc(sizeof(struct urdev), GFP_KERNEL);
  65. if (!urd)
  66. return NULL;
  67. urd->cdev = cdev;
  68. urd->reclen = cdev->id.driver_info;
  69. ccw_device_get_id(cdev, &urd->dev_id);
  70. mutex_init(&urd->io_mutex);
  71. mutex_init(&urd->open_mutex);
  72. return urd;
  73. }
  74. static void urdev_free(struct urdev *urd)
  75. {
  76. kfree(urd);
  77. }
  78. /*
  79. * This is how the character device driver gets a reference to a
  80. * ur device. When this call returns successfully, a reference has
  81. * been taken (by get_device) on the underlying kobject. The recipient
  82. * of this urdev pointer must eventually drop it with urdev_put(urd)
  83. * which does the corresponding put_device().
  84. */
  85. static struct urdev *urdev_get_from_devno(u16 devno)
  86. {
  87. char bus_id[16];
  88. struct ccw_device *cdev;
  89. sprintf(bus_id, "0.0.%04x", devno);
  90. cdev = get_ccwdev_by_busid(&ur_driver, bus_id);
  91. if (!cdev)
  92. return NULL;
  93. return cdev->dev.driver_data;
  94. }
  95. static void urdev_put(struct urdev *urd)
  96. {
  97. put_device(&urd->cdev->dev);
  98. }
  99. /*
  100. * Low-level functions to do I/O to a ur device.
  101. * alloc_chan_prog
  102. * free_chan_prog
  103. * do_ur_io
  104. * ur_int_handler
  105. *
  106. * alloc_chan_prog allocates and builds the channel program
  107. * free_chan_prog frees memory of the channel program
  108. *
  109. * do_ur_io issues the channel program to the device and blocks waiting
  110. * on a completion event it publishes at urd->io_done. The function
  111. * serialises itself on the device's mutex so that only one I/O
  112. * is issued at a time (and that I/O is synchronous).
  113. *
  114. * ur_int_handler catches the "I/O done" interrupt, writes the
  115. * subchannel status word into the scsw member of the urdev structure
  116. * and complete()s the io_done to wake the waiting do_ur_io.
  117. *
  118. * The caller of do_ur_io is responsible for kfree()ing the channel program
  119. * address pointer that alloc_chan_prog returned.
  120. */
  121. static void free_chan_prog(struct ccw1 *cpa)
  122. {
  123. struct ccw1 *ptr = cpa;
  124. while (ptr->cda) {
  125. kfree((void *)(addr_t) ptr->cda);
  126. ptr++;
  127. }
  128. kfree(cpa);
  129. }
  130. /*
  131. * alloc_chan_prog
  132. * The channel program we use is write commands chained together
  133. * with a final NOP CCW command-chained on (which ensures that CE and DE
  134. * are presented together in a single interrupt instead of as separate
  135. * interrupts unless an incorrect length indication kicks in first). The
  136. * data length in each CCW is reclen.
  137. */
  138. static struct ccw1 *alloc_chan_prog(const char __user *ubuf, int rec_count,
  139. int reclen)
  140. {
  141. struct ccw1 *cpa;
  142. void *kbuf;
  143. int i;
  144. TRACE("alloc_chan_prog(%p, %i, %i)\n", ubuf, rec_count, reclen);
  145. /*
  146. * We chain a NOP onto the writes to force CE+DE together.
  147. * That means we allocate room for CCWs to cover count/reclen
  148. * records plus a NOP.
  149. */
  150. cpa = kzalloc((rec_count + 1) * sizeof(struct ccw1),
  151. GFP_KERNEL | GFP_DMA);
  152. if (!cpa)
  153. return ERR_PTR(-ENOMEM);
  154. for (i = 0; i < rec_count; i++) {
  155. cpa[i].cmd_code = WRITE_CCW_CMD;
  156. cpa[i].flags = CCW_FLAG_CC | CCW_FLAG_SLI;
  157. cpa[i].count = reclen;
  158. kbuf = kmalloc(reclen, GFP_KERNEL | GFP_DMA);
  159. if (!kbuf) {
  160. free_chan_prog(cpa);
  161. return ERR_PTR(-ENOMEM);
  162. }
  163. cpa[i].cda = (u32)(addr_t) kbuf;
  164. if (copy_from_user(kbuf, ubuf, reclen)) {
  165. free_chan_prog(cpa);
  166. return ERR_PTR(-EFAULT);
  167. }
  168. ubuf += reclen;
  169. }
  170. /* The following NOP CCW forces CE+DE to be presented together */
  171. cpa[i].cmd_code = CCW_CMD_NOOP;
  172. return cpa;
  173. }
  174. static int do_ur_io(struct urdev *urd, struct ccw1 *cpa)
  175. {
  176. int rc;
  177. struct ccw_device *cdev = urd->cdev;
  178. DECLARE_COMPLETION_ONSTACK(event);
  179. TRACE("do_ur_io: cpa=%p\n", cpa);
  180. rc = mutex_lock_interruptible(&urd->io_mutex);
  181. if (rc)
  182. return rc;
  183. urd->io_done = &event;
  184. spin_lock_irq(get_ccwdev_lock(cdev));
  185. rc = ccw_device_start(cdev, cpa, 1, 0, 0);
  186. spin_unlock_irq(get_ccwdev_lock(cdev));
  187. TRACE("do_ur_io: ccw_device_start returned %d\n", rc);
  188. if (rc)
  189. goto out;
  190. wait_for_completion(&event);
  191. TRACE("do_ur_io: I/O complete\n");
  192. rc = 0;
  193. out:
  194. mutex_unlock(&urd->io_mutex);
  195. return rc;
  196. }
  197. /*
  198. * ur interrupt handler, called from the ccw_device layer
  199. */
  200. static void ur_int_handler(struct ccw_device *cdev, unsigned long intparm,
  201. struct irb *irb)
  202. {
  203. struct urdev *urd;
  204. TRACE("ur_int_handler: intparm=0x%lx cstat=%02x dstat=%02x res=%u\n",
  205. intparm, irb->scsw.cstat, irb->scsw.dstat, irb->scsw.count);
  206. if (!intparm) {
  207. TRACE("ur_int_handler: unsolicited interrupt\n");
  208. return;
  209. }
  210. urd = cdev->dev.driver_data;
  211. /* On special conditions irb is an error pointer */
  212. if (IS_ERR(irb))
  213. urd->io_request_rc = PTR_ERR(irb);
  214. else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  215. urd->io_request_rc = 0;
  216. else
  217. urd->io_request_rc = -EIO;
  218. complete(urd->io_done);
  219. }
  220. /*
  221. * reclen sysfs attribute - The record length to be used for write CCWs
  222. */
  223. static ssize_t ur_attr_reclen_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct urdev *urd = dev->driver_data;
  227. return sprintf(buf, "%zu\n", urd->reclen);
  228. }
  229. static DEVICE_ATTR(reclen, 0444, ur_attr_reclen_show, NULL);
  230. static int ur_create_attributes(struct device *dev)
  231. {
  232. return device_create_file(dev, &dev_attr_reclen);
  233. }
  234. static void ur_remove_attributes(struct device *dev)
  235. {
  236. device_remove_file(dev, &dev_attr_reclen);
  237. }
  238. /*
  239. * diagnose code 0x210 - retrieve device information
  240. * cc=0 normal completion, we have a real device
  241. * cc=1 CP paging error
  242. * cc=2 The virtual device exists, but is not associated with a real device
  243. * cc=3 Invalid device address, or the virtual device does not exist
  244. */
  245. static int get_urd_class(struct urdev *urd)
  246. {
  247. static struct diag210 ur_diag210;
  248. int cc;
  249. ur_diag210.vrdcdvno = urd->dev_id.devno;
  250. ur_diag210.vrdclen = sizeof(struct diag210);
  251. cc = diag210(&ur_diag210);
  252. switch (cc) {
  253. case 0:
  254. return -ENOTSUPP;
  255. case 2:
  256. return ur_diag210.vrdcvcla; /* virtual device class */
  257. case 3:
  258. return -ENODEV;
  259. default:
  260. return -EIO;
  261. }
  262. }
  263. /*
  264. * Allocation and freeing of urfile structures
  265. */
  266. static struct urfile *urfile_alloc(struct urdev *urd)
  267. {
  268. struct urfile *urf;
  269. urf = kzalloc(sizeof(struct urfile), GFP_KERNEL);
  270. if (!urf)
  271. return NULL;
  272. urf->urd = urd;
  273. TRACE("urfile_alloc: urd=%p urf=%p rl=%zu\n", urd, urf,
  274. urf->dev_reclen);
  275. return urf;
  276. }
  277. static void urfile_free(struct urfile *urf)
  278. {
  279. TRACE("urfile_free: urf=%p urd=%p\n", urf, urf->urd);
  280. kfree(urf);
  281. }
  282. /*
  283. * The fops implementation of the character device driver
  284. */
  285. static ssize_t do_write(struct urdev *urd, const char __user *udata,
  286. size_t count, size_t reclen, loff_t *ppos)
  287. {
  288. struct ccw1 *cpa;
  289. int rc;
  290. cpa = alloc_chan_prog(udata, count / reclen, reclen);
  291. if (IS_ERR(cpa))
  292. return PTR_ERR(cpa);
  293. rc = do_ur_io(urd, cpa);
  294. if (rc)
  295. goto fail_kfree_cpa;
  296. if (urd->io_request_rc) {
  297. rc = urd->io_request_rc;
  298. goto fail_kfree_cpa;
  299. }
  300. *ppos += count;
  301. rc = count;
  302. fail_kfree_cpa:
  303. free_chan_prog(cpa);
  304. return rc;
  305. }
  306. static ssize_t ur_write(struct file *file, const char __user *udata,
  307. size_t count, loff_t *ppos)
  308. {
  309. struct urfile *urf = file->private_data;
  310. TRACE("ur_write: count=%zu\n", count);
  311. if (count == 0)
  312. return 0;
  313. if (count % urf->dev_reclen)
  314. return -EINVAL; /* count must be a multiple of reclen */
  315. if (count > urf->dev_reclen * MAX_RECS_PER_IO)
  316. count = urf->dev_reclen * MAX_RECS_PER_IO;
  317. return do_write(urf->urd, udata, count, urf->dev_reclen, ppos);
  318. }
  319. static int do_diag_14(unsigned long rx, unsigned long ry1,
  320. unsigned long subcode)
  321. {
  322. register unsigned long _ry1 asm("2") = ry1;
  323. register unsigned long _ry2 asm("3") = subcode;
  324. int rc = 0;
  325. asm volatile(
  326. #ifdef CONFIG_64BIT
  327. " sam31\n"
  328. " diag %2,2,0x14\n"
  329. " sam64\n"
  330. #else
  331. " diag %2,2,0x14\n"
  332. #endif
  333. " ipm %0\n"
  334. " srl %0,28\n"
  335. : "=d" (rc), "+d" (_ry2)
  336. : "d" (rx), "d" (_ry1)
  337. : "cc");
  338. TRACE("diag 14: subcode=0x%lx, cc=%i\n", subcode, rc);
  339. return rc;
  340. }
  341. /*
  342. * diagnose code 0x14 subcode 0x0028 - position spool file to designated
  343. * record
  344. * cc=0 normal completion
  345. * cc=2 no file active on the virtual reader or device not ready
  346. * cc=3 record specified is beyond EOF
  347. */
  348. static int diag_position_to_record(int devno, int record)
  349. {
  350. int cc;
  351. cc = do_diag_14(record, devno, 0x28);
  352. switch (cc) {
  353. case 0:
  354. return 0;
  355. case 2:
  356. return -ENOMEDIUM;
  357. case 3:
  358. return -ENODATA; /* position beyond end of file */
  359. default:
  360. return -EIO;
  361. }
  362. }
  363. /*
  364. * diagnose code 0x14 subcode 0x0000 - read next spool file buffer
  365. * cc=0 normal completion
  366. * cc=1 EOF reached
  367. * cc=2 no file active on the virtual reader, and no file eligible
  368. * cc=3 file already active on the virtual reader or specified virtual
  369. * reader does not exist or is not a reader
  370. */
  371. static int diag_read_file(int devno, char *buf)
  372. {
  373. int cc;
  374. cc = do_diag_14((unsigned long) buf, devno, 0x00);
  375. switch (cc) {
  376. case 0:
  377. return 0;
  378. case 1:
  379. return -ENODATA;
  380. case 2:
  381. return -ENOMEDIUM;
  382. default:
  383. return -EIO;
  384. }
  385. }
  386. static ssize_t diag14_read(struct file *file, char __user *ubuf, size_t count,
  387. loff_t *offs)
  388. {
  389. size_t len, copied, res;
  390. char *buf;
  391. int rc;
  392. u16 reclen;
  393. struct urdev *urd;
  394. urd = ((struct urfile *) file->private_data)->urd;
  395. reclen = ((struct urfile *) file->private_data)->file_reclen;
  396. rc = diag_position_to_record(urd->dev_id.devno, *offs / PAGE_SIZE + 1);
  397. if (rc == -ENODATA)
  398. return 0;
  399. if (rc)
  400. return rc;
  401. len = min((size_t) PAGE_SIZE, count);
  402. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  403. if (!buf)
  404. return -ENOMEM;
  405. copied = 0;
  406. res = (size_t) (*offs % PAGE_SIZE);
  407. do {
  408. rc = diag_read_file(urd->dev_id.devno, buf);
  409. if (rc == -ENODATA) {
  410. break;
  411. }
  412. if (rc)
  413. goto fail;
  414. if (reclen && (copied == 0) && (*offs < PAGE_SIZE))
  415. *((u16 *) &buf[FILE_RECLEN_OFFSET]) = reclen;
  416. len = min(count - copied, PAGE_SIZE - res);
  417. if (copy_to_user(ubuf + copied, buf + res, len)) {
  418. rc = -EFAULT;
  419. goto fail;
  420. }
  421. res = 0;
  422. copied += len;
  423. } while (copied != count);
  424. *offs += copied;
  425. rc = copied;
  426. fail:
  427. kfree(buf);
  428. return rc;
  429. }
  430. static ssize_t ur_read(struct file *file, char __user *ubuf, size_t count,
  431. loff_t *offs)
  432. {
  433. struct urdev *urd;
  434. int rc;
  435. TRACE("ur_read: count=%zu ppos=%li\n", count, (unsigned long) *offs);
  436. if (count == 0)
  437. return 0;
  438. urd = ((struct urfile *) file->private_data)->urd;
  439. rc = mutex_lock_interruptible(&urd->io_mutex);
  440. if (rc)
  441. return rc;
  442. rc = diag14_read(file, ubuf, count, offs);
  443. mutex_unlock(&urd->io_mutex);
  444. return rc;
  445. }
  446. /*
  447. * diagnose code 0x14 subcode 0x0fff - retrieve next file descriptor
  448. * cc=0 normal completion
  449. * cc=1 no files on reader queue or no subsequent file
  450. * cc=2 spid specified is invalid
  451. */
  452. static int diag_read_next_file_info(struct file_control_block *buf, int spid)
  453. {
  454. int cc;
  455. cc = do_diag_14((unsigned long) buf, spid, 0xfff);
  456. switch (cc) {
  457. case 0:
  458. return 0;
  459. default:
  460. return -ENODATA;
  461. }
  462. }
  463. static int verify_device(struct urdev *urd)
  464. {
  465. struct file_control_block fcb;
  466. char *buf;
  467. int rc;
  468. switch (urd->class) {
  469. case DEV_CLASS_UR_O:
  470. return 0; /* no check needed here */
  471. case DEV_CLASS_UR_I:
  472. /* check for empty reader device (beginning of chain) */
  473. rc = diag_read_next_file_info(&fcb, 0);
  474. if (rc)
  475. return rc;
  476. /* if file is in hold status, we do not read it */
  477. if (fcb.file_stat & (FLG_SYSTEM_HOLD | FLG_USER_HOLD))
  478. return -EPERM;
  479. /* open file on virtual reader */
  480. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  481. if (!buf)
  482. return -ENOMEM;
  483. rc = diag_read_file(urd->dev_id.devno, buf);
  484. kfree(buf);
  485. if ((rc != 0) && (rc != -ENODATA)) /* EOF does not hurt */
  486. return rc;
  487. return 0;
  488. default:
  489. return -ENOTSUPP;
  490. }
  491. }
  492. static int get_file_reclen(struct urdev *urd)
  493. {
  494. struct file_control_block fcb;
  495. int rc;
  496. switch (urd->class) {
  497. case DEV_CLASS_UR_O:
  498. return 0;
  499. case DEV_CLASS_UR_I:
  500. rc = diag_read_next_file_info(&fcb, 0);
  501. if (rc)
  502. return rc;
  503. break;
  504. default:
  505. return -ENOTSUPP;
  506. }
  507. if (fcb.file_stat & FLG_CP_DUMP)
  508. return 0;
  509. return fcb.rec_len;
  510. }
  511. static int ur_open(struct inode *inode, struct file *file)
  512. {
  513. u16 devno;
  514. struct urdev *urd;
  515. struct urfile *urf;
  516. unsigned short accmode;
  517. int rc;
  518. accmode = file->f_flags & O_ACCMODE;
  519. if (accmode == O_RDWR)
  520. return -EACCES;
  521. /*
  522. * We treat the minor number as the devno of the ur device
  523. * to find in the driver tree.
  524. */
  525. devno = MINOR(file->f_dentry->d_inode->i_rdev);
  526. urd = urdev_get_from_devno(devno);
  527. if (!urd)
  528. return -ENXIO;
  529. if (file->f_flags & O_NONBLOCK) {
  530. if (!mutex_trylock(&urd->open_mutex)) {
  531. rc = -EBUSY;
  532. goto fail_put;
  533. }
  534. } else {
  535. if (mutex_lock_interruptible(&urd->open_mutex)) {
  536. rc = -ERESTARTSYS;
  537. goto fail_put;
  538. }
  539. }
  540. TRACE("ur_open\n");
  541. if (((accmode == O_RDONLY) && (urd->class != DEV_CLASS_UR_I)) ||
  542. ((accmode == O_WRONLY) && (urd->class != DEV_CLASS_UR_O))) {
  543. TRACE("ur_open: unsupported dev class (%d)\n", urd->class);
  544. rc = -EACCES;
  545. goto fail_unlock;
  546. }
  547. rc = verify_device(urd);
  548. if (rc)
  549. goto fail_unlock;
  550. urf = urfile_alloc(urd);
  551. if (!urf) {
  552. rc = -ENOMEM;
  553. goto fail_unlock;
  554. }
  555. urf->dev_reclen = urd->reclen;
  556. rc = get_file_reclen(urd);
  557. if (rc < 0)
  558. goto fail_urfile_free;
  559. urf->file_reclen = rc;
  560. file->private_data = urf;
  561. return 0;
  562. fail_urfile_free:
  563. urfile_free(urf);
  564. fail_unlock:
  565. mutex_unlock(&urd->open_mutex);
  566. fail_put:
  567. urdev_put(urd);
  568. return rc;
  569. }
  570. static int ur_release(struct inode *inode, struct file *file)
  571. {
  572. struct urfile *urf = file->private_data;
  573. TRACE("ur_release\n");
  574. mutex_unlock(&urf->urd->open_mutex);
  575. urdev_put(urf->urd);
  576. urfile_free(urf);
  577. return 0;
  578. }
  579. static loff_t ur_llseek(struct file *file, loff_t offset, int whence)
  580. {
  581. loff_t newpos;
  582. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  583. return -ESPIPE; /* seek allowed only for reader */
  584. if (offset % PAGE_SIZE)
  585. return -ESPIPE; /* only multiples of 4K allowed */
  586. switch (whence) {
  587. case 0: /* SEEK_SET */
  588. newpos = offset;
  589. break;
  590. case 1: /* SEEK_CUR */
  591. newpos = file->f_pos + offset;
  592. break;
  593. default:
  594. return -EINVAL;
  595. }
  596. file->f_pos = newpos;
  597. return newpos;
  598. }
  599. static struct file_operations ur_fops = {
  600. .owner = THIS_MODULE,
  601. .open = ur_open,
  602. .release = ur_release,
  603. .read = ur_read,
  604. .write = ur_write,
  605. .llseek = ur_llseek,
  606. };
  607. /*
  608. * ccw_device infrastructure:
  609. * ur_probe gets its own ref to the device (i.e. get_device),
  610. * creates the struct urdev, the device attributes, sets up
  611. * the interrupt handler and validates the virtual unit record device.
  612. * ur_remove removes the device attributes, frees the struct urdev
  613. * and drops (put_device) the ref to the device we got in ur_probe.
  614. */
  615. static int ur_probe(struct ccw_device *cdev)
  616. {
  617. struct urdev *urd;
  618. int rc;
  619. TRACE("ur_probe: cdev=%p state=%d\n", cdev, *(int *) cdev->private);
  620. if (!get_device(&cdev->dev))
  621. return -ENODEV;
  622. urd = urdev_alloc(cdev);
  623. if (!urd) {
  624. rc = -ENOMEM;
  625. goto fail;
  626. }
  627. rc = ur_create_attributes(&cdev->dev);
  628. if (rc) {
  629. rc = -ENOMEM;
  630. goto fail;
  631. }
  632. cdev->dev.driver_data = urd;
  633. cdev->handler = ur_int_handler;
  634. /* validate virtual unit record device */
  635. urd->class = get_urd_class(urd);
  636. if (urd->class < 0) {
  637. rc = urd->class;
  638. goto fail;
  639. }
  640. if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
  641. rc = -ENOTSUPP;
  642. goto fail;
  643. }
  644. return 0;
  645. fail:
  646. urdev_free(urd);
  647. put_device(&cdev->dev);
  648. return rc;
  649. }
  650. static void ur_remove(struct ccw_device *cdev)
  651. {
  652. struct urdev *urd = cdev->dev.driver_data;
  653. TRACE("ur_remove\n");
  654. if (cdev->online)
  655. ur_set_offline(cdev);
  656. ur_remove_attributes(&cdev->dev);
  657. urdev_free(urd);
  658. put_device(&cdev->dev);
  659. }
  660. static int ur_set_online(struct ccw_device *cdev)
  661. {
  662. struct urdev *urd;
  663. int minor, major, rc;
  664. char node_id[16];
  665. TRACE("ur_set_online: cdev=%p state=%d\n", cdev,
  666. *(int *) cdev->private);
  667. if (!try_module_get(ur_driver.owner))
  668. return -EINVAL;
  669. urd = (struct urdev *) cdev->dev.driver_data;
  670. minor = urd->dev_id.devno;
  671. major = MAJOR(ur_first_dev_maj_min);
  672. urd->char_device = cdev_alloc();
  673. if (!urd->char_device) {
  674. rc = -ENOMEM;
  675. goto fail_module_put;
  676. }
  677. cdev_init(urd->char_device, &ur_fops);
  678. urd->char_device->dev = MKDEV(major, minor);
  679. urd->char_device->owner = ur_fops.owner;
  680. rc = cdev_add(urd->char_device, urd->char_device->dev, 1);
  681. if (rc)
  682. goto fail_free_cdev;
  683. if (urd->cdev->id.cu_type == READER_PUNCH_DEVTYPE) {
  684. if (urd->class == DEV_CLASS_UR_I)
  685. sprintf(node_id, "vmrdr-%s", cdev->dev.bus_id);
  686. if (urd->class == DEV_CLASS_UR_O)
  687. sprintf(node_id, "vmpun-%s", cdev->dev.bus_id);
  688. } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
  689. sprintf(node_id, "vmprt-%s", cdev->dev.bus_id);
  690. } else {
  691. rc = -ENOTSUPP;
  692. goto fail_free_cdev;
  693. }
  694. urd->device = device_create(vmur_class, NULL, urd->char_device->dev,
  695. "%s", node_id);
  696. if (IS_ERR(urd->device)) {
  697. rc = PTR_ERR(urd->device);
  698. TRACE("ur_set_online: device_create rc=%d\n", rc);
  699. goto fail_free_cdev;
  700. }
  701. return 0;
  702. fail_free_cdev:
  703. cdev_del(urd->char_device);
  704. fail_module_put:
  705. module_put(ur_driver.owner);
  706. return rc;
  707. }
  708. static int ur_set_offline(struct ccw_device *cdev)
  709. {
  710. struct urdev *urd;
  711. TRACE("ur_set_offline: cdev=%p cdev->private=%p state=%d\n",
  712. cdev, cdev->private, *(int *) cdev->private);
  713. urd = (struct urdev *) cdev->dev.driver_data;
  714. device_destroy(vmur_class, urd->char_device->dev);
  715. cdev_del(urd->char_device);
  716. module_put(ur_driver.owner);
  717. return 0;
  718. }
  719. /*
  720. * Module initialisation and cleanup
  721. */
  722. static int __init ur_init(void)
  723. {
  724. int rc;
  725. dev_t dev;
  726. if (!MACHINE_IS_VM) {
  727. PRINT_ERR("%s is only available under z/VM.\n", ur_banner);
  728. return -ENODEV;
  729. }
  730. vmur_dbf = debug_register("vmur", 4, 1, 4 * sizeof(long));
  731. if (!vmur_dbf)
  732. return -ENOMEM;
  733. rc = debug_register_view(vmur_dbf, &debug_sprintf_view);
  734. if (rc)
  735. goto fail_free_dbf;
  736. debug_set_level(vmur_dbf, 6);
  737. rc = ccw_driver_register(&ur_driver);
  738. if (rc)
  739. goto fail_free_dbf;
  740. rc = alloc_chrdev_region(&dev, 0, NUM_MINORS, "vmur");
  741. if (rc) {
  742. PRINT_ERR("alloc_chrdev_region failed: err = %d\n", rc);
  743. goto fail_unregister_driver;
  744. }
  745. ur_first_dev_maj_min = MKDEV(MAJOR(dev), 0);
  746. vmur_class = class_create(THIS_MODULE, "vmur");
  747. if (IS_ERR(vmur_class)) {
  748. rc = PTR_ERR(vmur_class);
  749. goto fail_unregister_region;
  750. }
  751. PRINT_INFO("%s loaded.\n", ur_banner);
  752. return 0;
  753. fail_unregister_region:
  754. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  755. fail_unregister_driver:
  756. ccw_driver_unregister(&ur_driver);
  757. fail_free_dbf:
  758. debug_unregister(vmur_dbf);
  759. return rc;
  760. }
  761. static void __exit ur_exit(void)
  762. {
  763. class_destroy(vmur_class);
  764. unregister_chrdev_region(ur_first_dev_maj_min, NUM_MINORS);
  765. ccw_driver_unregister(&ur_driver);
  766. debug_unregister(vmur_dbf);
  767. PRINT_INFO("%s unloaded.\n", ur_banner);
  768. }
  769. module_init(ur_init);
  770. module_exit(ur_exit);