ch.c 25 KB

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