ch.c 24 KB

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