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