ch.c 24 KB

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