PageRenderTime 1412ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/ch.c

https://github.com/Mengqi/linux-2.6
C | 1030 lines | 866 code | 134 blank | 30 comment | 141 complexity | a376abb152954bdd915733d5874a396a MD5 | raw file
  1. /*
  2. * SCSI Media Changer device driver for Linux 2.6
  3. *
  4. * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
  5. *
  6. */
  7. #define VERSION "0.25"
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/major.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/completion.h>
  19. #include <linux/compat.h>
  20. #include <linux/chio.h> /* here are all the ioctls */
  21. #include <linux/mutex.h>
  22. #include <linux/idr.h>
  23. #include <linux/slab.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_driver.h>
  27. #include <scsi/scsi_ioctl.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_eh.h>
  31. #include <scsi/scsi_dbg.h>
  32. #define CH_DT_MAX 16
  33. #define CH_TYPES 8
  34. #define CH_MAX_DEVS 128
  35. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  36. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  37. MODULE_LICENSE("GPL");
  38. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  39. MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
  40. static DEFINE_MUTEX(ch_mutex);
  41. static int init = 1;
  42. module_param(init, int, 0444);
  43. MODULE_PARM_DESC(init, \
  44. "initialize element status on driver load (default: on)");
  45. static int timeout_move = 300;
  46. module_param(timeout_move, int, 0644);
  47. MODULE_PARM_DESC(timeout_move,"timeout for move commands "
  48. "(default: 300 seconds)");
  49. static int timeout_init = 3600;
  50. module_param(timeout_init, int, 0644);
  51. MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
  52. "(default: 3600 seconds)");
  53. static int verbose = 1;
  54. module_param(verbose, int, 0644);
  55. MODULE_PARM_DESC(verbose,"be verbose (default: on)");
  56. static int debug = 0;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
  59. "detailed sense codes on scsi errors (default: off)");
  60. static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
  61. static int dt_lun[CH_DT_MAX];
  62. module_param_array(dt_id, int, NULL, 0444);
  63. module_param_array(dt_lun, int, NULL, 0444);
  64. /* tell the driver about vendor-specific slots */
  65. static int vendor_firsts[CH_TYPES-4];
  66. static int vendor_counts[CH_TYPES-4];
  67. module_param_array(vendor_firsts, int, NULL, 0444);
  68. module_param_array(vendor_counts, int, NULL, 0444);
  69. static const char * vendor_labels[CH_TYPES-4] = {
  70. "v0", "v1", "v2", "v3"
  71. };
  72. // module_param_string_array(vendor_labels, NULL, 0444);
  73. #define DPRINTK(fmt, arg...) \
  74. do { \
  75. if (debug) \
  76. printk(KERN_DEBUG "%s: " fmt, ch->name, ##arg); \
  77. } while (0)
  78. #define VPRINTK(level, fmt, arg...) \
  79. do { \
  80. if (verbose) \
  81. printk(level "%s: " fmt, ch->name, ##arg); \
  82. } while (0)
  83. /* ------------------------------------------------------------------- */
  84. #define MAX_RETRIES 1
  85. static struct class * ch_sysfs_class;
  86. typedef struct {
  87. struct list_head list;
  88. int minor;
  89. char name[8];
  90. struct scsi_device *device;
  91. struct scsi_device **dt; /* ptrs to data transfer elements */
  92. u_int firsts[CH_TYPES];
  93. u_int counts[CH_TYPES];
  94. u_int unit_attention;
  95. u_int voltags;
  96. struct mutex lock;
  97. } scsi_changer;
  98. static DEFINE_IDR(ch_index_idr);
  99. static DEFINE_SPINLOCK(ch_index_lock);
  100. static const struct {
  101. unsigned char sense;
  102. unsigned char asc;
  103. unsigned char ascq;
  104. int errno;
  105. } ch_err[] = {
  106. /* Just filled in what looks right. Hav'nt checked any standard paper for
  107. these errno assignments, so they may be wrong... */
  108. {
  109. .sense = ILLEGAL_REQUEST,
  110. .asc = 0x21,
  111. .ascq = 0x01,
  112. .errno = EBADSLT, /* Invalid element address */
  113. },{
  114. .sense = ILLEGAL_REQUEST,
  115. .asc = 0x28,
  116. .ascq = 0x01,
  117. .errno = EBADE, /* Import or export element accessed */
  118. },{
  119. .sense = ILLEGAL_REQUEST,
  120. .asc = 0x3B,
  121. .ascq = 0x0D,
  122. .errno = EXFULL, /* Medium destination element full */
  123. },{
  124. .sense = ILLEGAL_REQUEST,
  125. .asc = 0x3B,
  126. .ascq = 0x0E,
  127. .errno = EBADE, /* Medium source element empty */
  128. },{
  129. .sense = ILLEGAL_REQUEST,
  130. .asc = 0x20,
  131. .ascq = 0x00,
  132. .errno = EBADRQC, /* Invalid command operation code */
  133. },{
  134. /* end of list */
  135. }
  136. };
  137. /* ------------------------------------------------------------------- */
  138. static int ch_find_errno(struct scsi_sense_hdr *sshdr)
  139. {
  140. int i,errno = 0;
  141. /* Check to see if additional sense information is available */
  142. if (scsi_sense_valid(sshdr) &&
  143. sshdr->asc != 0) {
  144. for (i = 0; ch_err[i].errno != 0; i++) {
  145. if (ch_err[i].sense == sshdr->sense_key &&
  146. ch_err[i].asc == sshdr->asc &&
  147. ch_err[i].ascq == sshdr->ascq) {
  148. errno = -ch_err[i].errno;
  149. break;
  150. }
  151. }
  152. }
  153. if (errno == 0)
  154. errno = -EIO;
  155. return errno;
  156. }
  157. static int
  158. ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
  159. void *buffer, unsigned buflength,
  160. enum dma_data_direction direction)
  161. {
  162. int errno, retries = 0, timeout, result;
  163. struct scsi_sense_hdr sshdr;
  164. timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
  165. ? timeout_init : timeout_move;
  166. retry:
  167. errno = 0;
  168. if (debug) {
  169. DPRINTK("command: ");
  170. __scsi_print_command(cmd);
  171. }
  172. result = scsi_execute_req(ch->device, cmd, direction, buffer,
  173. buflength, &sshdr, timeout * HZ,
  174. MAX_RETRIES, NULL);
  175. DPRINTK("result: 0x%x\n",result);
  176. if (driver_byte(result) & DRIVER_SENSE) {
  177. if (debug)
  178. scsi_print_sense_hdr(ch->name, &sshdr);
  179. errno = ch_find_errno(&sshdr);
  180. switch(sshdr.sense_key) {
  181. case UNIT_ATTENTION:
  182. ch->unit_attention = 1;
  183. if (retries++ < 3)
  184. goto retry;
  185. break;
  186. }
  187. }
  188. return errno;
  189. }
  190. /* ------------------------------------------------------------------------ */
  191. static int
  192. ch_elem_to_typecode(scsi_changer *ch, u_int elem)
  193. {
  194. int i;
  195. for (i = 0; i < CH_TYPES; i++) {
  196. if (elem >= ch->firsts[i] &&
  197. elem < ch->firsts[i] +
  198. ch->counts[i])
  199. return i+1;
  200. }
  201. return 0;
  202. }
  203. static int
  204. ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
  205. {
  206. u_char cmd[12];
  207. u_char *buffer;
  208. int result;
  209. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  210. if(!buffer)
  211. return -ENOMEM;
  212. retry:
  213. memset(cmd,0,sizeof(cmd));
  214. cmd[0] = READ_ELEMENT_STATUS;
  215. cmd[1] = (ch->device->lun << 5) |
  216. (ch->voltags ? 0x10 : 0) |
  217. ch_elem_to_typecode(ch,elem);
  218. cmd[2] = (elem >> 8) & 0xff;
  219. cmd[3] = elem & 0xff;
  220. cmd[5] = 1;
  221. cmd[9] = 255;
  222. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  223. if (((buffer[16] << 8) | buffer[17]) != elem) {
  224. DPRINTK("asked for element 0x%02x, got 0x%02x\n",
  225. elem,(buffer[16] << 8) | buffer[17]);
  226. kfree(buffer);
  227. return -EIO;
  228. }
  229. memcpy(data,buffer+16,16);
  230. } else {
  231. if (ch->voltags) {
  232. ch->voltags = 0;
  233. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  234. goto retry;
  235. }
  236. DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
  237. }
  238. kfree(buffer);
  239. return result;
  240. }
  241. static int
  242. ch_init_elem(scsi_changer *ch)
  243. {
  244. int err;
  245. u_char cmd[6];
  246. VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
  247. memset(cmd,0,sizeof(cmd));
  248. cmd[0] = INITIALIZE_ELEMENT_STATUS;
  249. cmd[1] = ch->device->lun << 5;
  250. err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  251. VPRINTK(KERN_INFO, "... finished\n");
  252. return err;
  253. }
  254. static int
  255. ch_readconfig(scsi_changer *ch)
  256. {
  257. u_char cmd[10], data[16];
  258. u_char *buffer;
  259. int result,id,lun,i;
  260. u_int elem;
  261. buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
  262. if (!buffer)
  263. return -ENOMEM;
  264. memset(cmd,0,sizeof(cmd));
  265. cmd[0] = MODE_SENSE;
  266. cmd[1] = ch->device->lun << 5;
  267. cmd[2] = 0x1d;
  268. cmd[4] = 255;
  269. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  270. if (0 != result) {
  271. cmd[1] |= (1<<3);
  272. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  273. }
  274. if (0 == result) {
  275. ch->firsts[CHET_MT] =
  276. (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
  277. ch->counts[CHET_MT] =
  278. (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
  279. ch->firsts[CHET_ST] =
  280. (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
  281. ch->counts[CHET_ST] =
  282. (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
  283. ch->firsts[CHET_IE] =
  284. (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
  285. ch->counts[CHET_IE] =
  286. (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
  287. ch->firsts[CHET_DT] =
  288. (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
  289. ch->counts[CHET_DT] =
  290. (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
  291. VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
  292. ch->firsts[CHET_MT],
  293. ch->counts[CHET_MT]);
  294. VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
  295. ch->firsts[CHET_ST],
  296. ch->counts[CHET_ST]);
  297. VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
  298. ch->firsts[CHET_IE],
  299. ch->counts[CHET_IE]);
  300. VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
  301. ch->firsts[CHET_DT],
  302. ch->counts[CHET_DT]);
  303. } else {
  304. VPRINTK(KERN_INFO, "reading element address assigment page failed!\n");
  305. }
  306. /* vendor specific element types */
  307. for (i = 0; i < 4; i++) {
  308. if (0 == vendor_counts[i])
  309. continue;
  310. if (NULL == vendor_labels[i])
  311. continue;
  312. ch->firsts[CHET_V1+i] = vendor_firsts[i];
  313. ch->counts[CHET_V1+i] = vendor_counts[i];
  314. VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
  315. i+5,i+1,vendor_firsts[i],vendor_counts[i],
  316. vendor_labels[i]);
  317. }
  318. /* look up the devices of the data transfer elements */
  319. ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt),
  320. GFP_KERNEL);
  321. if (!ch->dt) {
  322. kfree(buffer);
  323. return -ENOMEM;
  324. }
  325. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  326. id = -1;
  327. lun = 0;
  328. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  329. id = dt_id[elem];
  330. lun = dt_lun[elem];
  331. VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
  332. elem+ch->firsts[CHET_DT]);
  333. } else if (0 != ch_read_element_status
  334. (ch,elem+ch->firsts[CHET_DT],data)) {
  335. VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
  336. elem+ch->firsts[CHET_DT]);
  337. } else {
  338. VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  339. if (data[6] & 0x80) {
  340. VPRINTK(KERN_CONT, "not this SCSI bus\n");
  341. ch->dt[elem] = NULL;
  342. } else if (0 == (data[6] & 0x30)) {
  343. VPRINTK(KERN_CONT, "ID/LUN unknown\n");
  344. ch->dt[elem] = NULL;
  345. } else {
  346. id = ch->device->id;
  347. lun = 0;
  348. if (data[6] & 0x20) id = data[7];
  349. if (data[6] & 0x10) lun = data[6] & 7;
  350. }
  351. }
  352. if (-1 != id) {
  353. VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
  354. ch->dt[elem] =
  355. scsi_device_lookup(ch->device->host,
  356. ch->device->channel,
  357. id,lun);
  358. if (!ch->dt[elem]) {
  359. /* should not happen */
  360. VPRINTK(KERN_CONT, "Huh? device not found!\n");
  361. } else {
  362. VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
  363. ch->dt[elem]->vendor,
  364. ch->dt[elem]->model,
  365. ch->dt[elem]->rev);
  366. }
  367. }
  368. }
  369. ch->voltags = 1;
  370. kfree(buffer);
  371. return 0;
  372. }
  373. /* ------------------------------------------------------------------------ */
  374. static int
  375. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  376. {
  377. u_char cmd[10];
  378. DPRINTK("position: 0x%x\n",elem);
  379. if (0 == trans)
  380. trans = ch->firsts[CHET_MT];
  381. memset(cmd,0,sizeof(cmd));
  382. cmd[0] = POSITION_TO_ELEMENT;
  383. cmd[1] = ch->device->lun << 5;
  384. cmd[2] = (trans >> 8) & 0xff;
  385. cmd[3] = trans & 0xff;
  386. cmd[4] = (elem >> 8) & 0xff;
  387. cmd[5] = elem & 0xff;
  388. cmd[8] = rotate ? 1 : 0;
  389. return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  390. }
  391. static int
  392. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  393. {
  394. u_char cmd[12];
  395. DPRINTK("move: 0x%x => 0x%x\n",src,dest);
  396. if (0 == trans)
  397. trans = ch->firsts[CHET_MT];
  398. memset(cmd,0,sizeof(cmd));
  399. cmd[0] = MOVE_MEDIUM;
  400. cmd[1] = ch->device->lun << 5;
  401. cmd[2] = (trans >> 8) & 0xff;
  402. cmd[3] = trans & 0xff;
  403. cmd[4] = (src >> 8) & 0xff;
  404. cmd[5] = src & 0xff;
  405. cmd[6] = (dest >> 8) & 0xff;
  406. cmd[7] = dest & 0xff;
  407. cmd[10] = rotate ? 1 : 0;
  408. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  409. }
  410. static int
  411. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  412. u_int dest1, u_int dest2, int rotate1, int rotate2)
  413. {
  414. u_char cmd[12];
  415. DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
  416. src,dest1,dest2);
  417. if (0 == trans)
  418. trans = ch->firsts[CHET_MT];
  419. memset(cmd,0,sizeof(cmd));
  420. cmd[0] = EXCHANGE_MEDIUM;
  421. cmd[1] = ch->device->lun << 5;
  422. cmd[2] = (trans >> 8) & 0xff;
  423. cmd[3] = trans & 0xff;
  424. cmd[4] = (src >> 8) & 0xff;
  425. cmd[5] = src & 0xff;
  426. cmd[6] = (dest1 >> 8) & 0xff;
  427. cmd[7] = dest1 & 0xff;
  428. cmd[8] = (dest2 >> 8) & 0xff;
  429. cmd[9] = dest2 & 0xff;
  430. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  431. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  432. }
  433. static void
  434. ch_check_voltag(char *tag)
  435. {
  436. int i;
  437. for (i = 0; i < 32; i++) {
  438. /* restrict to ascii */
  439. if (tag[i] >= 0x7f || tag[i] < 0x20)
  440. tag[i] = ' ';
  441. /* don't allow search wildcards */
  442. if (tag[i] == '?' ||
  443. tag[i] == '*')
  444. tag[i] = ' ';
  445. }
  446. }
  447. static int
  448. ch_set_voltag(scsi_changer *ch, u_int elem,
  449. int alternate, int clear, u_char *tag)
  450. {
  451. u_char cmd[12];
  452. u_char *buffer;
  453. int result;
  454. buffer = kzalloc(512, GFP_KERNEL);
  455. if (!buffer)
  456. return -ENOMEM;
  457. DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
  458. clear ? "clear" : "set",
  459. alternate ? "alternate" : "primary",
  460. elem, tag);
  461. memset(cmd,0,sizeof(cmd));
  462. cmd[0] = SEND_VOLUME_TAG;
  463. cmd[1] = (ch->device->lun << 5) |
  464. ch_elem_to_typecode(ch,elem);
  465. cmd[2] = (elem >> 8) & 0xff;
  466. cmd[3] = elem & 0xff;
  467. cmd[5] = clear
  468. ? (alternate ? 0x0d : 0x0c)
  469. : (alternate ? 0x0b : 0x0a);
  470. cmd[9] = 255;
  471. memcpy(buffer,tag,32);
  472. ch_check_voltag(buffer);
  473. result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
  474. kfree(buffer);
  475. return result;
  476. }
  477. static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
  478. {
  479. int retval = 0;
  480. u_char data[16];
  481. unsigned int i;
  482. mutex_lock(&ch->lock);
  483. for (i = 0; i < ch->counts[type]; i++) {
  484. if (0 != ch_read_element_status
  485. (ch, ch->firsts[type]+i,data)) {
  486. retval = -EIO;
  487. break;
  488. }
  489. put_user(data[2], dest+i);
  490. if (data[2] & CESTATUS_EXCEPT)
  491. VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
  492. ch->firsts[type]+i,
  493. (int)data[4],(int)data[5]);
  494. retval = ch_read_element_status
  495. (ch, ch->firsts[type]+i,data);
  496. if (0 != retval)
  497. break;
  498. }
  499. mutex_unlock(&ch->lock);
  500. return retval;
  501. }
  502. /* ------------------------------------------------------------------------ */
  503. static int
  504. ch_release(struct inode *inode, struct file *file)
  505. {
  506. scsi_changer *ch = file->private_data;
  507. scsi_device_put(ch->device);
  508. file->private_data = NULL;
  509. return 0;
  510. }
  511. static int
  512. ch_open(struct inode *inode, struct file *file)
  513. {
  514. scsi_changer *ch;
  515. int minor = iminor(inode);
  516. mutex_lock(&ch_mutex);
  517. spin_lock(&ch_index_lock);
  518. ch = idr_find(&ch_index_idr, minor);
  519. if (NULL == ch || scsi_device_get(ch->device)) {
  520. spin_unlock(&ch_index_lock);
  521. mutex_unlock(&ch_mutex);
  522. return -ENXIO;
  523. }
  524. spin_unlock(&ch_index_lock);
  525. file->private_data = ch;
  526. mutex_unlock(&ch_mutex);
  527. return 0;
  528. }
  529. static int
  530. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  531. {
  532. if (type >= CH_TYPES || unit >= ch->counts[type])
  533. return -1;
  534. return 0;
  535. }
  536. static long ch_ioctl(struct file *file,
  537. unsigned int cmd, unsigned long arg)
  538. {
  539. scsi_changer *ch = file->private_data;
  540. int retval;
  541. void __user *argp = (void __user *)arg;
  542. switch (cmd) {
  543. case CHIOGPARAMS:
  544. {
  545. struct changer_params params;
  546. params.cp_curpicker = 0;
  547. params.cp_npickers = ch->counts[CHET_MT];
  548. params.cp_nslots = ch->counts[CHET_ST];
  549. params.cp_nportals = ch->counts[CHET_IE];
  550. params.cp_ndrives = ch->counts[CHET_DT];
  551. if (copy_to_user(argp, &params, sizeof(params)))
  552. return -EFAULT;
  553. return 0;
  554. }
  555. case CHIOGVPARAMS:
  556. {
  557. struct changer_vendor_params vparams;
  558. memset(&vparams,0,sizeof(vparams));
  559. if (ch->counts[CHET_V1]) {
  560. vparams.cvp_n1 = ch->counts[CHET_V1];
  561. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  562. }
  563. if (ch->counts[CHET_V2]) {
  564. vparams.cvp_n2 = ch->counts[CHET_V2];
  565. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  566. }
  567. if (ch->counts[CHET_V3]) {
  568. vparams.cvp_n3 = ch->counts[CHET_V3];
  569. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  570. }
  571. if (ch->counts[CHET_V4]) {
  572. vparams.cvp_n4 = ch->counts[CHET_V4];
  573. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  574. }
  575. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  576. return -EFAULT;
  577. return 0;
  578. }
  579. case CHIOPOSITION:
  580. {
  581. struct changer_position pos;
  582. if (copy_from_user(&pos, argp, sizeof (pos)))
  583. return -EFAULT;
  584. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  585. DPRINTK("CHIOPOSITION: invalid parameter\n");
  586. return -EBADSLT;
  587. }
  588. mutex_lock(&ch->lock);
  589. retval = ch_position(ch,0,
  590. ch->firsts[pos.cp_type] + pos.cp_unit,
  591. pos.cp_flags & CP_INVERT);
  592. mutex_unlock(&ch->lock);
  593. return retval;
  594. }
  595. case CHIOMOVE:
  596. {
  597. struct changer_move mv;
  598. if (copy_from_user(&mv, argp, sizeof (mv)))
  599. return -EFAULT;
  600. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  601. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  602. DPRINTK("CHIOMOVE: invalid parameter\n");
  603. return -EBADSLT;
  604. }
  605. mutex_lock(&ch->lock);
  606. retval = ch_move(ch,0,
  607. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  608. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  609. mv.cm_flags & CM_INVERT);
  610. mutex_unlock(&ch->lock);
  611. return retval;
  612. }
  613. case CHIOEXCHANGE:
  614. {
  615. struct changer_exchange mv;
  616. if (copy_from_user(&mv, argp, sizeof (mv)))
  617. return -EFAULT;
  618. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  619. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  620. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  621. DPRINTK("CHIOEXCHANGE: invalid parameter\n");
  622. return -EBADSLT;
  623. }
  624. mutex_lock(&ch->lock);
  625. retval = ch_exchange
  626. (ch,0,
  627. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  628. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  629. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  630. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  631. mutex_unlock(&ch->lock);
  632. return retval;
  633. }
  634. case CHIOGSTATUS:
  635. {
  636. struct changer_element_status ces;
  637. if (copy_from_user(&ces, argp, sizeof (ces)))
  638. return -EFAULT;
  639. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  640. return -EINVAL;
  641. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  642. }
  643. case CHIOGELEM:
  644. {
  645. struct changer_get_element cge;
  646. u_char ch_cmd[12];
  647. u_char *buffer;
  648. unsigned int elem;
  649. int result,i;
  650. if (copy_from_user(&cge, argp, sizeof (cge)))
  651. return -EFAULT;
  652. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  653. return -EINVAL;
  654. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  655. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  656. if (!buffer)
  657. return -ENOMEM;
  658. mutex_lock(&ch->lock);
  659. voltag_retry:
  660. memset(ch_cmd, 0, sizeof(ch_cmd));
  661. ch_cmd[0] = READ_ELEMENT_STATUS;
  662. ch_cmd[1] = (ch->device->lun << 5) |
  663. (ch->voltags ? 0x10 : 0) |
  664. ch_elem_to_typecode(ch,elem);
  665. ch_cmd[2] = (elem >> 8) & 0xff;
  666. ch_cmd[3] = elem & 0xff;
  667. ch_cmd[5] = 1;
  668. ch_cmd[9] = 255;
  669. result = ch_do_scsi(ch, ch_cmd, buffer, 256, DMA_FROM_DEVICE);
  670. if (!result) {
  671. cge.cge_status = buffer[18];
  672. cge.cge_flags = 0;
  673. if (buffer[18] & CESTATUS_EXCEPT) {
  674. cge.cge_errno = EIO;
  675. }
  676. if (buffer[25] & 0x80) {
  677. cge.cge_flags |= CGE_SRC;
  678. if (buffer[25] & 0x40)
  679. cge.cge_flags |= CGE_INVERT;
  680. elem = (buffer[26]<<8) | buffer[27];
  681. for (i = 0; i < 4; i++) {
  682. if (elem >= ch->firsts[i] &&
  683. elem < ch->firsts[i] + ch->counts[i]) {
  684. cge.cge_srctype = i;
  685. cge.cge_srcunit = elem-ch->firsts[i];
  686. }
  687. }
  688. }
  689. if ((buffer[22] & 0x30) == 0x30) {
  690. cge.cge_flags |= CGE_IDLUN;
  691. cge.cge_id = buffer[23];
  692. cge.cge_lun = buffer[22] & 7;
  693. }
  694. if (buffer[9] & 0x80) {
  695. cge.cge_flags |= CGE_PVOLTAG;
  696. memcpy(cge.cge_pvoltag,buffer+28,36);
  697. }
  698. if (buffer[9] & 0x40) {
  699. cge.cge_flags |= CGE_AVOLTAG;
  700. memcpy(cge.cge_avoltag,buffer+64,36);
  701. }
  702. } else if (ch->voltags) {
  703. ch->voltags = 0;
  704. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  705. goto voltag_retry;
  706. }
  707. kfree(buffer);
  708. mutex_unlock(&ch->lock);
  709. if (copy_to_user(argp, &cge, sizeof (cge)))
  710. return -EFAULT;
  711. return result;
  712. }
  713. case CHIOINITELEM:
  714. {
  715. mutex_lock(&ch->lock);
  716. retval = ch_init_elem(ch);
  717. mutex_unlock(&ch->lock);
  718. return retval;
  719. }
  720. case CHIOSVOLTAG:
  721. {
  722. struct changer_set_voltag csv;
  723. int elem;
  724. if (copy_from_user(&csv, argp, sizeof(csv)))
  725. return -EFAULT;
  726. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  727. DPRINTK("CHIOSVOLTAG: invalid parameter\n");
  728. return -EBADSLT;
  729. }
  730. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  731. mutex_lock(&ch->lock);
  732. retval = ch_set_voltag(ch, elem,
  733. csv.csv_flags & CSV_AVOLTAG,
  734. csv.csv_flags & CSV_CLEARTAG,
  735. csv.csv_voltag);
  736. mutex_unlock(&ch->lock);
  737. return retval;
  738. }
  739. default:
  740. return scsi_ioctl(ch->device, cmd, argp);
  741. }
  742. }
  743. #ifdef CONFIG_COMPAT
  744. struct changer_element_status32 {
  745. int ces_type;
  746. compat_uptr_t ces_data;
  747. };
  748. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  749. static long ch_ioctl_compat(struct file * file,
  750. unsigned int cmd, unsigned long arg)
  751. {
  752. scsi_changer *ch = file->private_data;
  753. switch (cmd) {
  754. case CHIOGPARAMS:
  755. case CHIOGVPARAMS:
  756. case CHIOPOSITION:
  757. case CHIOMOVE:
  758. case CHIOEXCHANGE:
  759. case CHIOGELEM:
  760. case CHIOINITELEM:
  761. case CHIOSVOLTAG:
  762. /* compatible */
  763. return ch_ioctl(file, cmd, arg);
  764. case CHIOGSTATUS32:
  765. {
  766. struct changer_element_status32 ces32;
  767. unsigned char __user *data;
  768. if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
  769. return -EFAULT;
  770. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  771. return -EINVAL;
  772. data = compat_ptr(ces32.ces_data);
  773. return ch_gstatus(ch, ces32.ces_type, data);
  774. }
  775. default:
  776. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  777. return -ENOIOCTLCMD;
  778. }
  779. }
  780. #endif
  781. /* ------------------------------------------------------------------------ */
  782. static int ch_probe(struct device *dev)
  783. {
  784. struct scsi_device *sd = to_scsi_device(dev);
  785. struct device *class_dev;
  786. int minor, ret = -ENOMEM;
  787. scsi_changer *ch;
  788. if (sd->type != TYPE_MEDIUM_CHANGER)
  789. return -ENODEV;
  790. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  791. if (NULL == ch)
  792. return -ENOMEM;
  793. if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
  794. goto free_ch;
  795. spin_lock(&ch_index_lock);
  796. ret = idr_get_new(&ch_index_idr, ch, &minor);
  797. spin_unlock(&ch_index_lock);
  798. if (ret)
  799. goto free_ch;
  800. if (minor > CH_MAX_DEVS) {
  801. ret = -ENODEV;
  802. goto remove_idr;
  803. }
  804. ch->minor = minor;
  805. sprintf(ch->name,"ch%d",ch->minor);
  806. class_dev = device_create(ch_sysfs_class, dev,
  807. MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
  808. "s%s", ch->name);
  809. if (IS_ERR(class_dev)) {
  810. printk(KERN_WARNING "ch%d: device_create failed\n",
  811. ch->minor);
  812. ret = PTR_ERR(class_dev);
  813. goto remove_idr;
  814. }
  815. mutex_init(&ch->lock);
  816. ch->device = sd;
  817. ch_readconfig(ch);
  818. if (init)
  819. ch_init_elem(ch);
  820. dev_set_drvdata(dev, ch);
  821. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  822. return 0;
  823. remove_idr:
  824. idr_remove(&ch_index_idr, minor);
  825. free_ch:
  826. kfree(ch);
  827. return ret;
  828. }
  829. static int ch_remove(struct device *dev)
  830. {
  831. scsi_changer *ch = dev_get_drvdata(dev);
  832. spin_lock(&ch_index_lock);
  833. idr_remove(&ch_index_idr, ch->minor);
  834. spin_unlock(&ch_index_lock);
  835. device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  836. kfree(ch->dt);
  837. kfree(ch);
  838. return 0;
  839. }
  840. static struct scsi_driver ch_template = {
  841. .owner = THIS_MODULE,
  842. .gendrv = {
  843. .name = "ch",
  844. .probe = ch_probe,
  845. .remove = ch_remove,
  846. },
  847. };
  848. static const struct file_operations changer_fops = {
  849. .owner = THIS_MODULE,
  850. .open = ch_open,
  851. .release = ch_release,
  852. .unlocked_ioctl = ch_ioctl,
  853. #ifdef CONFIG_COMPAT
  854. .compat_ioctl = ch_ioctl_compat,
  855. #endif
  856. .llseek = noop_llseek,
  857. };
  858. static int __init init_ch_module(void)
  859. {
  860. int rc;
  861. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  862. ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
  863. if (IS_ERR(ch_sysfs_class)) {
  864. rc = PTR_ERR(ch_sysfs_class);
  865. return rc;
  866. }
  867. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  868. if (rc < 0) {
  869. printk("Unable to get major %d for SCSI-Changer\n",
  870. SCSI_CHANGER_MAJOR);
  871. goto fail1;
  872. }
  873. rc = scsi_register_driver(&ch_template.gendrv);
  874. if (rc < 0)
  875. goto fail2;
  876. return 0;
  877. fail2:
  878. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  879. fail1:
  880. class_destroy(ch_sysfs_class);
  881. return rc;
  882. }
  883. static void __exit exit_ch_module(void)
  884. {
  885. scsi_unregister_driver(&ch_template.gendrv);
  886. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  887. class_destroy(ch_sysfs_class);
  888. idr_destroy(&ch_index_idr);
  889. }
  890. module_init(init_ch_module);
  891. module_exit(exit_ch_module);
  892. /*
  893. * Local variables:
  894. * c-basic-offset: 8
  895. * End:
  896. */