ch.c 24 KB

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