ch.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  315. id = -1;
  316. lun = 0;
  317. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  318. id = dt_id[elem];
  319. lun = dt_lun[elem];
  320. vprintk("dt 0x%x: [insmod option] ",
  321. elem+ch->firsts[CHET_DT]);
  322. } else if (0 != ch_read_element_status
  323. (ch,elem+ch->firsts[CHET_DT],data)) {
  324. vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
  325. elem+ch->firsts[CHET_DT]);
  326. } else {
  327. vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  328. if (data[6] & 0x80) {
  329. if (verbose)
  330. printk("not this SCSI bus\n");
  331. ch->dt[elem] = NULL;
  332. } else if (0 == (data[6] & 0x30)) {
  333. if (verbose)
  334. printk("ID/LUN unknown\n");
  335. ch->dt[elem] = NULL;
  336. } else {
  337. id = ch->device->id;
  338. lun = 0;
  339. if (data[6] & 0x20) id = data[7];
  340. if (data[6] & 0x10) lun = data[6] & 7;
  341. }
  342. }
  343. if (-1 != id) {
  344. if (verbose)
  345. printk("ID %i, LUN %i, ",id,lun);
  346. ch->dt[elem] =
  347. scsi_device_lookup(ch->device->host,
  348. ch->device->channel,
  349. id,lun);
  350. if (!ch->dt[elem]) {
  351. /* should not happen */
  352. if (verbose)
  353. printk("Huh? device not found!\n");
  354. } else {
  355. if (verbose)
  356. printk("name: %8.8s %16.16s %4.4s\n",
  357. ch->dt[elem]->vendor,
  358. ch->dt[elem]->model,
  359. ch->dt[elem]->rev);
  360. }
  361. }
  362. }
  363. ch->voltags = 1;
  364. kfree(buffer);
  365. return 0;
  366. }
  367. /* ------------------------------------------------------------------------ */
  368. static int
  369. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  370. {
  371. u_char cmd[10];
  372. dprintk("position: 0x%x\n",elem);
  373. if (0 == trans)
  374. trans = ch->firsts[CHET_MT];
  375. memset(cmd,0,sizeof(cmd));
  376. cmd[0] = POSITION_TO_ELEMENT;
  377. cmd[1] = ch->device->lun << 5;
  378. cmd[2] = (trans >> 8) & 0xff;
  379. cmd[3] = trans & 0xff;
  380. cmd[4] = (elem >> 8) & 0xff;
  381. cmd[5] = elem & 0xff;
  382. cmd[8] = rotate ? 1 : 0;
  383. return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  384. }
  385. static int
  386. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  387. {
  388. u_char cmd[12];
  389. dprintk("move: 0x%x => 0x%x\n",src,dest);
  390. if (0 == trans)
  391. trans = ch->firsts[CHET_MT];
  392. memset(cmd,0,sizeof(cmd));
  393. cmd[0] = MOVE_MEDIUM;
  394. cmd[1] = ch->device->lun << 5;
  395. cmd[2] = (trans >> 8) & 0xff;
  396. cmd[3] = trans & 0xff;
  397. cmd[4] = (src >> 8) & 0xff;
  398. cmd[5] = src & 0xff;
  399. cmd[6] = (dest >> 8) & 0xff;
  400. cmd[7] = dest & 0xff;
  401. cmd[10] = rotate ? 1 : 0;
  402. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  403. }
  404. static int
  405. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  406. u_int dest1, u_int dest2, int rotate1, int rotate2)
  407. {
  408. u_char cmd[12];
  409. dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
  410. src,dest1,dest2);
  411. if (0 == trans)
  412. trans = ch->firsts[CHET_MT];
  413. memset(cmd,0,sizeof(cmd));
  414. cmd[0] = EXCHANGE_MEDIUM;
  415. cmd[1] = ch->device->lun << 5;
  416. cmd[2] = (trans >> 8) & 0xff;
  417. cmd[3] = trans & 0xff;
  418. cmd[4] = (src >> 8) & 0xff;
  419. cmd[5] = src & 0xff;
  420. cmd[6] = (dest1 >> 8) & 0xff;
  421. cmd[7] = dest1 & 0xff;
  422. cmd[8] = (dest2 >> 8) & 0xff;
  423. cmd[9] = dest2 & 0xff;
  424. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  425. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  426. }
  427. static void
  428. ch_check_voltag(char *tag)
  429. {
  430. int i;
  431. for (i = 0; i < 32; i++) {
  432. /* restrict to ascii */
  433. if (tag[i] >= 0x7f || tag[i] < 0x20)
  434. tag[i] = ' ';
  435. /* don't allow search wildcards */
  436. if (tag[i] == '?' ||
  437. tag[i] == '*')
  438. tag[i] = ' ';
  439. }
  440. }
  441. static int
  442. ch_set_voltag(scsi_changer *ch, u_int elem,
  443. int alternate, int clear, u_char *tag)
  444. {
  445. u_char cmd[12];
  446. u_char *buffer;
  447. int result;
  448. buffer = kzalloc(512, GFP_KERNEL);
  449. if (!buffer)
  450. return -ENOMEM;
  451. dprintk("%s %s voltag: 0x%x => \"%s\"\n",
  452. clear ? "clear" : "set",
  453. alternate ? "alternate" : "primary",
  454. elem, tag);
  455. memset(cmd,0,sizeof(cmd));
  456. cmd[0] = SEND_VOLUME_TAG;
  457. cmd[1] = (ch->device->lun << 5) |
  458. ch_elem_to_typecode(ch,elem);
  459. cmd[2] = (elem >> 8) & 0xff;
  460. cmd[3] = elem & 0xff;
  461. cmd[5] = clear
  462. ? (alternate ? 0x0d : 0x0c)
  463. : (alternate ? 0x0b : 0x0a);
  464. cmd[9] = 255;
  465. memcpy(buffer,tag,32);
  466. ch_check_voltag(buffer);
  467. result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
  468. kfree(buffer);
  469. return result;
  470. }
  471. static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
  472. {
  473. int retval = 0;
  474. u_char data[16];
  475. unsigned int i;
  476. mutex_lock(&ch->lock);
  477. for (i = 0; i < ch->counts[type]; i++) {
  478. if (0 != ch_read_element_status
  479. (ch, ch->firsts[type]+i,data)) {
  480. retval = -EIO;
  481. break;
  482. }
  483. put_user(data[2], dest+i);
  484. if (data[2] & CESTATUS_EXCEPT)
  485. vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
  486. ch->firsts[type]+i,
  487. (int)data[4],(int)data[5]);
  488. retval = ch_read_element_status
  489. (ch, ch->firsts[type]+i,data);
  490. if (0 != retval)
  491. break;
  492. }
  493. mutex_unlock(&ch->lock);
  494. return retval;
  495. }
  496. /* ------------------------------------------------------------------------ */
  497. static int
  498. ch_release(struct inode *inode, struct file *file)
  499. {
  500. scsi_changer *ch = file->private_data;
  501. scsi_device_put(ch->device);
  502. file->private_data = NULL;
  503. return 0;
  504. }
  505. static int
  506. ch_open(struct inode *inode, struct file *file)
  507. {
  508. scsi_changer *ch;
  509. int minor = iminor(inode);
  510. lock_kernel();
  511. spin_lock(&ch_index_lock);
  512. ch = idr_find(&ch_index_idr, minor);
  513. if (NULL == ch || scsi_device_get(ch->device)) {
  514. spin_unlock(&ch_index_lock);
  515. unlock_kernel();
  516. return -ENXIO;
  517. }
  518. spin_unlock(&ch_index_lock);
  519. file->private_data = ch;
  520. unlock_kernel();
  521. return 0;
  522. }
  523. static int
  524. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  525. {
  526. if (type >= CH_TYPES || unit >= ch->counts[type])
  527. return -1;
  528. return 0;
  529. }
  530. static long ch_ioctl(struct file *file,
  531. unsigned int cmd, unsigned long arg)
  532. {
  533. scsi_changer *ch = file->private_data;
  534. int retval;
  535. void __user *argp = (void __user *)arg;
  536. switch (cmd) {
  537. case CHIOGPARAMS:
  538. {
  539. struct changer_params params;
  540. params.cp_curpicker = 0;
  541. params.cp_npickers = ch->counts[CHET_MT];
  542. params.cp_nslots = ch->counts[CHET_ST];
  543. params.cp_nportals = ch->counts[CHET_IE];
  544. params.cp_ndrives = ch->counts[CHET_DT];
  545. if (copy_to_user(argp, &params, sizeof(params)))
  546. return -EFAULT;
  547. return 0;
  548. }
  549. case CHIOGVPARAMS:
  550. {
  551. struct changer_vendor_params vparams;
  552. memset(&vparams,0,sizeof(vparams));
  553. if (ch->counts[CHET_V1]) {
  554. vparams.cvp_n1 = ch->counts[CHET_V1];
  555. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  556. }
  557. if (ch->counts[CHET_V2]) {
  558. vparams.cvp_n2 = ch->counts[CHET_V2];
  559. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  560. }
  561. if (ch->counts[CHET_V3]) {
  562. vparams.cvp_n3 = ch->counts[CHET_V3];
  563. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  564. }
  565. if (ch->counts[CHET_V4]) {
  566. vparams.cvp_n4 = ch->counts[CHET_V4];
  567. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  568. }
  569. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  570. return -EFAULT;
  571. return 0;
  572. }
  573. case CHIOPOSITION:
  574. {
  575. struct changer_position pos;
  576. if (copy_from_user(&pos, argp, sizeof (pos)))
  577. return -EFAULT;
  578. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  579. dprintk("CHIOPOSITION: invalid parameter\n");
  580. return -EBADSLT;
  581. }
  582. mutex_lock(&ch->lock);
  583. retval = ch_position(ch,0,
  584. ch->firsts[pos.cp_type] + pos.cp_unit,
  585. pos.cp_flags & CP_INVERT);
  586. mutex_unlock(&ch->lock);
  587. return retval;
  588. }
  589. case CHIOMOVE:
  590. {
  591. struct changer_move mv;
  592. if (copy_from_user(&mv, argp, sizeof (mv)))
  593. return -EFAULT;
  594. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  595. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  596. dprintk("CHIOMOVE: invalid parameter\n");
  597. return -EBADSLT;
  598. }
  599. mutex_lock(&ch->lock);
  600. retval = ch_move(ch,0,
  601. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  602. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  603. mv.cm_flags & CM_INVERT);
  604. mutex_unlock(&ch->lock);
  605. return retval;
  606. }
  607. case CHIOEXCHANGE:
  608. {
  609. struct changer_exchange mv;
  610. if (copy_from_user(&mv, argp, sizeof (mv)))
  611. return -EFAULT;
  612. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  613. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  614. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  615. dprintk("CHIOEXCHANGE: invalid parameter\n");
  616. return -EBADSLT;
  617. }
  618. mutex_lock(&ch->lock);
  619. retval = ch_exchange
  620. (ch,0,
  621. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  622. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  623. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  624. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  625. mutex_unlock(&ch->lock);
  626. return retval;
  627. }
  628. case CHIOGSTATUS:
  629. {
  630. struct changer_element_status ces;
  631. if (copy_from_user(&ces, argp, sizeof (ces)))
  632. return -EFAULT;
  633. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  634. return -EINVAL;
  635. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  636. }
  637. case CHIOGELEM:
  638. {
  639. struct changer_get_element cge;
  640. u_char ch_cmd[12];
  641. u_char *buffer;
  642. unsigned int elem;
  643. int result,i;
  644. if (copy_from_user(&cge, argp, sizeof (cge)))
  645. return -EFAULT;
  646. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  647. return -EINVAL;
  648. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  649. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  650. if (!buffer)
  651. return -ENOMEM;
  652. mutex_lock(&ch->lock);
  653. voltag_retry:
  654. memset(ch_cmd, 0, sizeof(ch_cmd));
  655. ch_cmd[0] = READ_ELEMENT_STATUS;
  656. ch_cmd[1] = (ch->device->lun << 5) |
  657. (ch->voltags ? 0x10 : 0) |
  658. ch_elem_to_typecode(ch,elem);
  659. ch_cmd[2] = (elem >> 8) & 0xff;
  660. ch_cmd[3] = elem & 0xff;
  661. ch_cmd[5] = 1;
  662. ch_cmd[9] = 255;
  663. result = ch_do_scsi(ch, ch_cmd, buffer, 256, DMA_FROM_DEVICE);
  664. if (!result) {
  665. cge.cge_status = buffer[18];
  666. cge.cge_flags = 0;
  667. if (buffer[18] & CESTATUS_EXCEPT) {
  668. cge.cge_errno = EIO;
  669. }
  670. if (buffer[25] & 0x80) {
  671. cge.cge_flags |= CGE_SRC;
  672. if (buffer[25] & 0x40)
  673. cge.cge_flags |= CGE_INVERT;
  674. elem = (buffer[26]<<8) | buffer[27];
  675. for (i = 0; i < 4; i++) {
  676. if (elem >= ch->firsts[i] &&
  677. elem < ch->firsts[i] + ch->counts[i]) {
  678. cge.cge_srctype = i;
  679. cge.cge_srcunit = elem-ch->firsts[i];
  680. }
  681. }
  682. }
  683. if ((buffer[22] & 0x30) == 0x30) {
  684. cge.cge_flags |= CGE_IDLUN;
  685. cge.cge_id = buffer[23];
  686. cge.cge_lun = buffer[22] & 7;
  687. }
  688. if (buffer[9] & 0x80) {
  689. cge.cge_flags |= CGE_PVOLTAG;
  690. memcpy(cge.cge_pvoltag,buffer+28,36);
  691. }
  692. if (buffer[9] & 0x40) {
  693. cge.cge_flags |= CGE_AVOLTAG;
  694. memcpy(cge.cge_avoltag,buffer+64,36);
  695. }
  696. } else if (ch->voltags) {
  697. ch->voltags = 0;
  698. vprintk("device has no volume tag support\n");
  699. goto voltag_retry;
  700. }
  701. kfree(buffer);
  702. mutex_unlock(&ch->lock);
  703. if (copy_to_user(argp, &cge, sizeof (cge)))
  704. return -EFAULT;
  705. return result;
  706. }
  707. case CHIOINITELEM:
  708. {
  709. mutex_lock(&ch->lock);
  710. retval = ch_init_elem(ch);
  711. mutex_unlock(&ch->lock);
  712. return retval;
  713. }
  714. case CHIOSVOLTAG:
  715. {
  716. struct changer_set_voltag csv;
  717. int elem;
  718. if (copy_from_user(&csv, argp, sizeof(csv)))
  719. return -EFAULT;
  720. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  721. dprintk("CHIOSVOLTAG: invalid parameter\n");
  722. return -EBADSLT;
  723. }
  724. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  725. mutex_lock(&ch->lock);
  726. retval = ch_set_voltag(ch, elem,
  727. csv.csv_flags & CSV_AVOLTAG,
  728. csv.csv_flags & CSV_CLEARTAG,
  729. csv.csv_voltag);
  730. mutex_unlock(&ch->lock);
  731. return retval;
  732. }
  733. default:
  734. return scsi_ioctl(ch->device, cmd, argp);
  735. }
  736. }
  737. #ifdef CONFIG_COMPAT
  738. struct changer_element_status32 {
  739. int ces_type;
  740. compat_uptr_t ces_data;
  741. };
  742. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  743. static long ch_ioctl_compat(struct file * file,
  744. unsigned int cmd, unsigned long arg)
  745. {
  746. scsi_changer *ch = file->private_data;
  747. switch (cmd) {
  748. case CHIOGPARAMS:
  749. case CHIOGVPARAMS:
  750. case CHIOPOSITION:
  751. case CHIOMOVE:
  752. case CHIOEXCHANGE:
  753. case CHIOGELEM:
  754. case CHIOINITELEM:
  755. case CHIOSVOLTAG:
  756. /* compatible */
  757. return ch_ioctl(file, cmd, arg);
  758. case CHIOGSTATUS32:
  759. {
  760. struct changer_element_status32 ces32;
  761. unsigned char __user *data;
  762. if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
  763. return -EFAULT;
  764. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  765. return -EINVAL;
  766. data = compat_ptr(ces32.ces_data);
  767. return ch_gstatus(ch, ces32.ces_type, data);
  768. }
  769. default:
  770. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  771. return -ENOIOCTLCMD;
  772. }
  773. }
  774. #endif
  775. /* ------------------------------------------------------------------------ */
  776. static int ch_probe(struct device *dev)
  777. {
  778. struct scsi_device *sd = to_scsi_device(dev);
  779. struct device *class_dev;
  780. int minor, ret = -ENOMEM;
  781. scsi_changer *ch;
  782. if (sd->type != TYPE_MEDIUM_CHANGER)
  783. return -ENODEV;
  784. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  785. if (NULL == ch)
  786. return -ENOMEM;
  787. if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
  788. goto free_ch;
  789. spin_lock(&ch_index_lock);
  790. ret = idr_get_new(&ch_index_idr, ch, &minor);
  791. spin_unlock(&ch_index_lock);
  792. if (ret)
  793. goto free_ch;
  794. if (minor > CH_MAX_DEVS) {
  795. ret = -ENODEV;
  796. goto remove_idr;
  797. }
  798. ch->minor = minor;
  799. sprintf(ch->name,"ch%d",ch->minor);
  800. class_dev = device_create(ch_sysfs_class, dev,
  801. MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
  802. "s%s", ch->name);
  803. if (IS_ERR(class_dev)) {
  804. printk(KERN_WARNING "ch%d: device_create failed\n",
  805. ch->minor);
  806. ret = PTR_ERR(class_dev);
  807. goto remove_idr;
  808. }
  809. mutex_init(&ch->lock);
  810. ch->device = sd;
  811. ch_readconfig(ch);
  812. if (init)
  813. ch_init_elem(ch);
  814. dev_set_drvdata(dev, ch);
  815. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  816. return 0;
  817. remove_idr:
  818. idr_remove(&ch_index_idr, minor);
  819. free_ch:
  820. kfree(ch);
  821. return ret;
  822. }
  823. static int ch_remove(struct device *dev)
  824. {
  825. scsi_changer *ch = dev_get_drvdata(dev);
  826. spin_lock(&ch_index_lock);
  827. idr_remove(&ch_index_idr, ch->minor);
  828. spin_unlock(&ch_index_lock);
  829. device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  830. kfree(ch->dt);
  831. kfree(ch);
  832. return 0;
  833. }
  834. static struct scsi_driver ch_template = {
  835. .owner = THIS_MODULE,
  836. .gendrv = {
  837. .name = "ch",
  838. .probe = ch_probe,
  839. .remove = ch_remove,
  840. },
  841. };
  842. static const struct file_operations changer_fops = {
  843. .owner = THIS_MODULE,
  844. .open = ch_open,
  845. .release = ch_release,
  846. .unlocked_ioctl = ch_ioctl,
  847. #ifdef CONFIG_COMPAT
  848. .compat_ioctl = ch_ioctl_compat,
  849. #endif
  850. };
  851. static int __init init_ch_module(void)
  852. {
  853. int rc;
  854. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  855. ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
  856. if (IS_ERR(ch_sysfs_class)) {
  857. rc = PTR_ERR(ch_sysfs_class);
  858. return rc;
  859. }
  860. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  861. if (rc < 0) {
  862. printk("Unable to get major %d for SCSI-Changer\n",
  863. SCSI_CHANGER_MAJOR);
  864. goto fail1;
  865. }
  866. rc = scsi_register_driver(&ch_template.gendrv);
  867. if (rc < 0)
  868. goto fail2;
  869. return 0;
  870. fail2:
  871. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  872. fail1:
  873. class_destroy(ch_sysfs_class);
  874. return rc;
  875. }
  876. static void __exit exit_ch_module(void)
  877. {
  878. scsi_unregister_driver(&ch_template.gendrv);
  879. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  880. class_destroy(ch_sysfs_class);
  881. idr_destroy(&ch_index_idr);
  882. }
  883. module_init(init_ch_module);
  884. module_exit(exit_ch_module);
  885. /*
  886. * Local variables:
  887. * c-basic-offset: 8
  888. * End:
  889. */