ch.c 24 KB

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