ch.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * SCSI Media Changer device driver for Linux 2.6
  3. *
  4. * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
  5. *
  6. */
  7. #define VERSION "0.25"
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/major.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/completion.h>
  21. #include <linux/compat.h>
  22. #include <linux/chio.h> /* here are all the ioctls */
  23. #include <linux/mutex.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_driver.h>
  27. #include <scsi/scsi_ioctl.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_eh.h>
  31. #include <scsi/scsi_dbg.h>
  32. #define CH_DT_MAX 16
  33. #define CH_TYPES 8
  34. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  35. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  36. MODULE_LICENSE("GPL");
  37. 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 const 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 mutex 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 const 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 __user *dest)
  501. {
  502. int retval = 0;
  503. u_char data[16];
  504. unsigned int i;
  505. mutex_lock(&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. mutex_unlock(&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. void __user *argp = (void __user *)arg;
  566. switch (cmd) {
  567. case CHIOGPARAMS:
  568. {
  569. struct changer_params params;
  570. params.cp_curpicker = 0;
  571. params.cp_npickers = ch->counts[CHET_MT];
  572. params.cp_nslots = ch->counts[CHET_ST];
  573. params.cp_nportals = ch->counts[CHET_IE];
  574. params.cp_ndrives = ch->counts[CHET_DT];
  575. if (copy_to_user(argp, &params, sizeof(params)))
  576. return -EFAULT;
  577. return 0;
  578. }
  579. case CHIOGVPARAMS:
  580. {
  581. struct changer_vendor_params vparams;
  582. memset(&vparams,0,sizeof(vparams));
  583. if (ch->counts[CHET_V1]) {
  584. vparams.cvp_n1 = ch->counts[CHET_V1];
  585. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  586. }
  587. if (ch->counts[CHET_V2]) {
  588. vparams.cvp_n2 = ch->counts[CHET_V2];
  589. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  590. }
  591. if (ch->counts[CHET_V3]) {
  592. vparams.cvp_n3 = ch->counts[CHET_V3];
  593. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  594. }
  595. if (ch->counts[CHET_V4]) {
  596. vparams.cvp_n4 = ch->counts[CHET_V4];
  597. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  598. }
  599. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  600. return -EFAULT;
  601. return 0;
  602. }
  603. case CHIOPOSITION:
  604. {
  605. struct changer_position pos;
  606. if (copy_from_user(&pos, argp, sizeof (pos)))
  607. return -EFAULT;
  608. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  609. dprintk("CHIOPOSITION: invalid parameter\n");
  610. return -EBADSLT;
  611. }
  612. mutex_lock(&ch->lock);
  613. retval = ch_position(ch,0,
  614. ch->firsts[pos.cp_type] + pos.cp_unit,
  615. pos.cp_flags & CP_INVERT);
  616. mutex_unlock(&ch->lock);
  617. return retval;
  618. }
  619. case CHIOMOVE:
  620. {
  621. struct changer_move mv;
  622. if (copy_from_user(&mv, argp, sizeof (mv)))
  623. return -EFAULT;
  624. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  625. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  626. dprintk("CHIOMOVE: invalid parameter\n");
  627. return -EBADSLT;
  628. }
  629. mutex_lock(&ch->lock);
  630. retval = ch_move(ch,0,
  631. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  632. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  633. mv.cm_flags & CM_INVERT);
  634. mutex_unlock(&ch->lock);
  635. return retval;
  636. }
  637. case CHIOEXCHANGE:
  638. {
  639. struct changer_exchange mv;
  640. if (copy_from_user(&mv, argp, sizeof (mv)))
  641. return -EFAULT;
  642. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  643. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  644. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  645. dprintk("CHIOEXCHANGE: invalid parameter\n");
  646. return -EBADSLT;
  647. }
  648. mutex_lock(&ch->lock);
  649. retval = ch_exchange
  650. (ch,0,
  651. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  652. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  653. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  654. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  655. mutex_unlock(&ch->lock);
  656. return retval;
  657. }
  658. case CHIOGSTATUS:
  659. {
  660. struct changer_element_status ces;
  661. if (copy_from_user(&ces, argp, sizeof (ces)))
  662. return -EFAULT;
  663. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  664. return -EINVAL;
  665. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  666. }
  667. case CHIOGELEM:
  668. {
  669. struct changer_get_element cge;
  670. u_char cmd[12];
  671. u_char *buffer;
  672. unsigned int elem;
  673. int result,i;
  674. if (copy_from_user(&cge, argp, sizeof (cge)))
  675. return -EFAULT;
  676. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  677. return -EINVAL;
  678. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  679. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  680. if (!buffer)
  681. return -ENOMEM;
  682. mutex_lock(&ch->lock);
  683. voltag_retry:
  684. memset(cmd,0,sizeof(cmd));
  685. cmd[0] = READ_ELEMENT_STATUS;
  686. cmd[1] = (ch->device->lun << 5) |
  687. (ch->voltags ? 0x10 : 0) |
  688. ch_elem_to_typecode(ch,elem);
  689. cmd[2] = (elem >> 8) & 0xff;
  690. cmd[3] = elem & 0xff;
  691. cmd[5] = 1;
  692. cmd[9] = 255;
  693. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  694. cge.cge_status = buffer[18];
  695. cge.cge_flags = 0;
  696. if (buffer[18] & CESTATUS_EXCEPT) {
  697. cge.cge_errno = EIO;
  698. }
  699. if (buffer[25] & 0x80) {
  700. cge.cge_flags |= CGE_SRC;
  701. if (buffer[25] & 0x40)
  702. cge.cge_flags |= CGE_INVERT;
  703. elem = (buffer[26]<<8) | buffer[27];
  704. for (i = 0; i < 4; i++) {
  705. if (elem >= ch->firsts[i] &&
  706. elem < ch->firsts[i] + ch->counts[i]) {
  707. cge.cge_srctype = i;
  708. cge.cge_srcunit = elem-ch->firsts[i];
  709. }
  710. }
  711. }
  712. if ((buffer[22] & 0x30) == 0x30) {
  713. cge.cge_flags |= CGE_IDLUN;
  714. cge.cge_id = buffer[23];
  715. cge.cge_lun = buffer[22] & 7;
  716. }
  717. if (buffer[9] & 0x80) {
  718. cge.cge_flags |= CGE_PVOLTAG;
  719. memcpy(cge.cge_pvoltag,buffer+28,36);
  720. }
  721. if (buffer[9] & 0x40) {
  722. cge.cge_flags |= CGE_AVOLTAG;
  723. memcpy(cge.cge_avoltag,buffer+64,36);
  724. }
  725. } else if (ch->voltags) {
  726. ch->voltags = 0;
  727. vprintk("device has no volume tag support\n");
  728. goto voltag_retry;
  729. }
  730. kfree(buffer);
  731. mutex_unlock(&ch->lock);
  732. if (copy_to_user(argp, &cge, sizeof (cge)))
  733. return -EFAULT;
  734. return result;
  735. }
  736. case CHIOINITELEM:
  737. {
  738. mutex_lock(&ch->lock);
  739. retval = ch_init_elem(ch);
  740. mutex_unlock(&ch->lock);
  741. return retval;
  742. }
  743. case CHIOSVOLTAG:
  744. {
  745. struct changer_set_voltag csv;
  746. int elem;
  747. if (copy_from_user(&csv, argp, sizeof(csv)))
  748. return -EFAULT;
  749. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  750. dprintk("CHIOSVOLTAG: invalid parameter\n");
  751. return -EBADSLT;
  752. }
  753. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  754. mutex_lock(&ch->lock);
  755. retval = ch_set_voltag(ch, elem,
  756. csv.csv_flags & CSV_AVOLTAG,
  757. csv.csv_flags & CSV_CLEARTAG,
  758. csv.csv_voltag);
  759. mutex_unlock(&ch->lock);
  760. return retval;
  761. }
  762. default:
  763. return scsi_ioctl(ch->device, cmd, argp);
  764. }
  765. }
  766. #ifdef CONFIG_COMPAT
  767. struct changer_element_status32 {
  768. int ces_type;
  769. compat_uptr_t ces_data;
  770. };
  771. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  772. static long ch_ioctl_compat(struct file * file,
  773. unsigned int cmd, unsigned long arg)
  774. {
  775. scsi_changer *ch = file->private_data;
  776. switch (cmd) {
  777. case CHIOGPARAMS:
  778. case CHIOGVPARAMS:
  779. case CHIOPOSITION:
  780. case CHIOMOVE:
  781. case CHIOEXCHANGE:
  782. case CHIOGELEM:
  783. case CHIOINITELEM:
  784. case CHIOSVOLTAG:
  785. /* compatible */
  786. return ch_ioctl(NULL /* inode, unused */,
  787. file, cmd, arg);
  788. case CHIOGSTATUS32:
  789. {
  790. struct changer_element_status32 ces32;
  791. unsigned char __user *data;
  792. if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
  793. return -EFAULT;
  794. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  795. return -EINVAL;
  796. data = compat_ptr(ces32.ces_data);
  797. return ch_gstatus(ch, ces32.ces_type, data);
  798. }
  799. default:
  800. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  801. return -ENOIOCTLCMD;
  802. }
  803. }
  804. #endif
  805. /* ------------------------------------------------------------------------ */
  806. static int ch_probe(struct device *dev)
  807. {
  808. struct scsi_device *sd = to_scsi_device(dev);
  809. scsi_changer *ch;
  810. if (sd->type != TYPE_MEDIUM_CHANGER)
  811. return -ENODEV;
  812. ch = kmalloc(sizeof(*ch), GFP_KERNEL);
  813. if (NULL == ch)
  814. return -ENOMEM;
  815. memset(ch,0,sizeof(*ch));
  816. ch->minor = ch_devcount;
  817. sprintf(ch->name,"ch%d",ch->minor);
  818. mutex_init(&ch->lock);
  819. ch->device = sd;
  820. ch_readconfig(ch);
  821. if (init)
  822. ch_init_elem(ch);
  823. class_device_create(ch_sysfs_class, NULL,
  824. MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
  825. dev, "s%s", ch->name);
  826. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  827. spin_lock(&ch_devlist_lock);
  828. list_add_tail(&ch->list,&ch_devlist);
  829. ch_devcount++;
  830. spin_unlock(&ch_devlist_lock);
  831. return 0;
  832. }
  833. static int ch_remove(struct device *dev)
  834. {
  835. struct scsi_device *sd = to_scsi_device(dev);
  836. scsi_changer *tmp, *ch;
  837. spin_lock(&ch_devlist_lock);
  838. ch = NULL;
  839. list_for_each_entry(tmp,&ch_devlist,list) {
  840. if (tmp->device == sd)
  841. ch = tmp;
  842. }
  843. BUG_ON(NULL == ch);
  844. list_del(&ch->list);
  845. spin_unlock(&ch_devlist_lock);
  846. class_device_destroy(ch_sysfs_class,
  847. MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  848. kfree(ch->dt);
  849. kfree(ch);
  850. ch_devcount--;
  851. return 0;
  852. }
  853. static int __init init_ch_module(void)
  854. {
  855. int rc;
  856. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  857. ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
  858. if (IS_ERR(ch_sysfs_class)) {
  859. rc = PTR_ERR(ch_sysfs_class);
  860. return rc;
  861. }
  862. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  863. if (rc < 0) {
  864. printk("Unable to get major %d for SCSI-Changer\n",
  865. SCSI_CHANGER_MAJOR);
  866. goto fail1;
  867. }
  868. rc = scsi_register_driver(&ch_template.gendrv);
  869. if (rc < 0)
  870. goto fail2;
  871. return 0;
  872. fail2:
  873. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  874. fail1:
  875. class_destroy(ch_sysfs_class);
  876. return rc;
  877. }
  878. static void __exit exit_ch_module(void)
  879. {
  880. scsi_unregister_driver(&ch_template.gendrv);
  881. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  882. class_destroy(ch_sysfs_class);
  883. }
  884. module_init(init_ch_module);
  885. module_exit(exit_ch_module);
  886. /*
  887. * Local variables:
  888. * c-basic-offset: 8
  889. * End:
  890. */