ch.c 24 KB

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