ch.c 24 KB

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