ch.c 25 KB

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