ch.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 <scsi/scsi.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_driver.h>
  25. #include <scsi/scsi_ioctl.h>
  26. #include <scsi/scsi_host.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_eh.h>
  29. #include <scsi/scsi_dbg.h>
  30. #define CH_DT_MAX 16
  31. #define CH_TYPES 8
  32. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  33. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  34. MODULE_LICENSE("GPL");
  35. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  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 mutex 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 const 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 = kzalloc(512, GFP_KERNEL | GFP_DMA);
  281. if (!buffer)
  282. return -ENOMEM;
  283. memset(cmd,0,sizeof(cmd));
  284. cmd[0] = MODE_SENSE;
  285. cmd[1] = ch->device->lun << 5;
  286. cmd[2] = 0x1d;
  287. cmd[4] = 255;
  288. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  289. if (0 != result) {
  290. cmd[1] |= (1<<3);
  291. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  292. }
  293. if (0 == result) {
  294. ch->firsts[CHET_MT] =
  295. (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
  296. ch->counts[CHET_MT] =
  297. (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
  298. ch->firsts[CHET_ST] =
  299. (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
  300. ch->counts[CHET_ST] =
  301. (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
  302. ch->firsts[CHET_IE] =
  303. (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
  304. ch->counts[CHET_IE] =
  305. (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
  306. ch->firsts[CHET_DT] =
  307. (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
  308. ch->counts[CHET_DT] =
  309. (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
  310. vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
  311. ch->firsts[CHET_MT],
  312. ch->counts[CHET_MT]);
  313. vprintk("type #2 (st): 0x%x+%d [storage]\n",
  314. ch->firsts[CHET_ST],
  315. ch->counts[CHET_ST]);
  316. vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
  317. ch->firsts[CHET_IE],
  318. ch->counts[CHET_IE]);
  319. vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
  320. ch->firsts[CHET_DT],
  321. ch->counts[CHET_DT]);
  322. } else {
  323. vprintk("reading element address assigment page failed!\n");
  324. }
  325. /* vendor specific element types */
  326. for (i = 0; i < 4; i++) {
  327. if (0 == vendor_counts[i])
  328. continue;
  329. if (NULL == vendor_labels[i])
  330. continue;
  331. ch->firsts[CHET_V1+i] = vendor_firsts[i];
  332. ch->counts[CHET_V1+i] = vendor_counts[i];
  333. vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
  334. i+5,i+1,vendor_firsts[i],vendor_counts[i],
  335. vendor_labels[i]);
  336. }
  337. /* look up the devices of the data transfer elements */
  338. ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
  339. GFP_KERNEL);
  340. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  341. id = -1;
  342. lun = 0;
  343. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  344. id = dt_id[elem];
  345. lun = dt_lun[elem];
  346. vprintk("dt 0x%x: [insmod option] ",
  347. elem+ch->firsts[CHET_DT]);
  348. } else if (0 != ch_read_element_status
  349. (ch,elem+ch->firsts[CHET_DT],data)) {
  350. vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
  351. elem+ch->firsts[CHET_DT]);
  352. } else {
  353. vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  354. if (data[6] & 0x80) {
  355. if (verbose)
  356. printk("not this SCSI bus\n");
  357. ch->dt[elem] = NULL;
  358. } else if (0 == (data[6] & 0x30)) {
  359. if (verbose)
  360. printk("ID/LUN unknown\n");
  361. ch->dt[elem] = NULL;
  362. } else {
  363. id = ch->device->id;
  364. lun = 0;
  365. if (data[6] & 0x20) id = data[7];
  366. if (data[6] & 0x10) lun = data[6] & 7;
  367. }
  368. }
  369. if (-1 != id) {
  370. if (verbose)
  371. printk("ID %i, LUN %i, ",id,lun);
  372. ch->dt[elem] =
  373. scsi_device_lookup(ch->device->host,
  374. ch->device->channel,
  375. id,lun);
  376. if (!ch->dt[elem]) {
  377. /* should not happen */
  378. if (verbose)
  379. printk("Huh? device not found!\n");
  380. } else {
  381. if (verbose)
  382. printk("name: %8.8s %16.16s %4.4s\n",
  383. ch->dt[elem]->vendor,
  384. ch->dt[elem]->model,
  385. ch->dt[elem]->rev);
  386. }
  387. }
  388. }
  389. ch->voltags = 1;
  390. kfree(buffer);
  391. return 0;
  392. }
  393. /* ------------------------------------------------------------------------ */
  394. static int
  395. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  396. {
  397. u_char cmd[10];
  398. dprintk("position: 0x%x\n",elem);
  399. if (0 == trans)
  400. trans = ch->firsts[CHET_MT];
  401. memset(cmd,0,sizeof(cmd));
  402. cmd[0] = POSITION_TO_ELEMENT;
  403. cmd[1] = ch->device->lun << 5;
  404. cmd[2] = (trans >> 8) & 0xff;
  405. cmd[3] = trans & 0xff;
  406. cmd[4] = (elem >> 8) & 0xff;
  407. cmd[5] = elem & 0xff;
  408. cmd[8] = rotate ? 1 : 0;
  409. return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  410. }
  411. static int
  412. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  413. {
  414. u_char cmd[12];
  415. dprintk("move: 0x%x => 0x%x\n",src,dest);
  416. if (0 == trans)
  417. trans = ch->firsts[CHET_MT];
  418. memset(cmd,0,sizeof(cmd));
  419. cmd[0] = MOVE_MEDIUM;
  420. cmd[1] = ch->device->lun << 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] = (dest >> 8) & 0xff;
  426. cmd[7] = dest & 0xff;
  427. cmd[10] = rotate ? 1 : 0;
  428. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  429. }
  430. static int
  431. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  432. u_int dest1, u_int dest2, int rotate1, int rotate2)
  433. {
  434. u_char cmd[12];
  435. dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
  436. src,dest1,dest2);
  437. if (0 == trans)
  438. trans = ch->firsts[CHET_MT];
  439. memset(cmd,0,sizeof(cmd));
  440. cmd[0] = EXCHANGE_MEDIUM;
  441. cmd[1] = ch->device->lun << 5;
  442. cmd[2] = (trans >> 8) & 0xff;
  443. cmd[3] = trans & 0xff;
  444. cmd[4] = (src >> 8) & 0xff;
  445. cmd[5] = src & 0xff;
  446. cmd[6] = (dest1 >> 8) & 0xff;
  447. cmd[7] = dest1 & 0xff;
  448. cmd[8] = (dest2 >> 8) & 0xff;
  449. cmd[9] = dest2 & 0xff;
  450. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  451. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  452. }
  453. static void
  454. ch_check_voltag(char *tag)
  455. {
  456. int i;
  457. for (i = 0; i < 32; i++) {
  458. /* restrict to ascii */
  459. if (tag[i] >= 0x7f || tag[i] < 0x20)
  460. tag[i] = ' ';
  461. /* don't allow search wildcards */
  462. if (tag[i] == '?' ||
  463. tag[i] == '*')
  464. tag[i] = ' ';
  465. }
  466. }
  467. static int
  468. ch_set_voltag(scsi_changer *ch, u_int elem,
  469. int alternate, int clear, u_char *tag)
  470. {
  471. u_char cmd[12];
  472. u_char *buffer;
  473. int result;
  474. buffer = kzalloc(512, GFP_KERNEL);
  475. if (!buffer)
  476. return -ENOMEM;
  477. dprintk("%s %s voltag: 0x%x => \"%s\"\n",
  478. clear ? "clear" : "set",
  479. alternate ? "alternate" : "primary",
  480. elem, tag);
  481. memset(cmd,0,sizeof(cmd));
  482. cmd[0] = SEND_VOLUME_TAG;
  483. cmd[1] = (ch->device->lun << 5) |
  484. ch_elem_to_typecode(ch,elem);
  485. cmd[2] = (elem >> 8) & 0xff;
  486. cmd[3] = elem & 0xff;
  487. cmd[5] = clear
  488. ? (alternate ? 0x0d : 0x0c)
  489. : (alternate ? 0x0b : 0x0a);
  490. cmd[9] = 255;
  491. memcpy(buffer,tag,32);
  492. ch_check_voltag(buffer);
  493. result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
  494. kfree(buffer);
  495. return result;
  496. }
  497. static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
  498. {
  499. int retval = 0;
  500. u_char data[16];
  501. unsigned int i;
  502. mutex_lock(&ch->lock);
  503. for (i = 0; i < ch->counts[type]; i++) {
  504. if (0 != ch_read_element_status
  505. (ch, ch->firsts[type]+i,data)) {
  506. retval = -EIO;
  507. break;
  508. }
  509. put_user(data[2], dest+i);
  510. if (data[2] & CESTATUS_EXCEPT)
  511. vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
  512. ch->firsts[type]+i,
  513. (int)data[4],(int)data[5]);
  514. retval = ch_read_element_status
  515. (ch, ch->firsts[type]+i,data);
  516. if (0 != retval)
  517. break;
  518. }
  519. mutex_unlock(&ch->lock);
  520. return retval;
  521. }
  522. /* ------------------------------------------------------------------------ */
  523. static int
  524. ch_release(struct inode *inode, struct file *file)
  525. {
  526. scsi_changer *ch = file->private_data;
  527. scsi_device_put(ch->device);
  528. file->private_data = NULL;
  529. return 0;
  530. }
  531. static int
  532. ch_open(struct inode *inode, struct file *file)
  533. {
  534. scsi_changer *tmp, *ch;
  535. int minor = iminor(inode);
  536. spin_lock(&ch_devlist_lock);
  537. ch = NULL;
  538. list_for_each_entry(tmp,&ch_devlist,list) {
  539. if (tmp->minor == minor)
  540. ch = tmp;
  541. }
  542. if (NULL == ch || scsi_device_get(ch->device)) {
  543. spin_unlock(&ch_devlist_lock);
  544. return -ENXIO;
  545. }
  546. spin_unlock(&ch_devlist_lock);
  547. file->private_data = ch;
  548. return 0;
  549. }
  550. static int
  551. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  552. {
  553. if (type >= CH_TYPES || unit >= ch->counts[type])
  554. return -1;
  555. return 0;
  556. }
  557. static int ch_ioctl(struct inode * inode, struct file * file,
  558. unsigned int cmd, unsigned long arg)
  559. {
  560. scsi_changer *ch = file->private_data;
  561. int retval;
  562. void __user *argp = (void __user *)arg;
  563. switch (cmd) {
  564. case CHIOGPARAMS:
  565. {
  566. struct changer_params params;
  567. params.cp_curpicker = 0;
  568. params.cp_npickers = ch->counts[CHET_MT];
  569. params.cp_nslots = ch->counts[CHET_ST];
  570. params.cp_nportals = ch->counts[CHET_IE];
  571. params.cp_ndrives = ch->counts[CHET_DT];
  572. if (copy_to_user(argp, &params, sizeof(params)))
  573. return -EFAULT;
  574. return 0;
  575. }
  576. case CHIOGVPARAMS:
  577. {
  578. struct changer_vendor_params vparams;
  579. memset(&vparams,0,sizeof(vparams));
  580. if (ch->counts[CHET_V1]) {
  581. vparams.cvp_n1 = ch->counts[CHET_V1];
  582. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  583. }
  584. if (ch->counts[CHET_V2]) {
  585. vparams.cvp_n2 = ch->counts[CHET_V2];
  586. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  587. }
  588. if (ch->counts[CHET_V3]) {
  589. vparams.cvp_n3 = ch->counts[CHET_V3];
  590. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  591. }
  592. if (ch->counts[CHET_V4]) {
  593. vparams.cvp_n4 = ch->counts[CHET_V4];
  594. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  595. }
  596. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  597. return -EFAULT;
  598. return 0;
  599. }
  600. case CHIOPOSITION:
  601. {
  602. struct changer_position pos;
  603. if (copy_from_user(&pos, argp, sizeof (pos)))
  604. return -EFAULT;
  605. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  606. dprintk("CHIOPOSITION: invalid parameter\n");
  607. return -EBADSLT;
  608. }
  609. mutex_lock(&ch->lock);
  610. retval = ch_position(ch,0,
  611. ch->firsts[pos.cp_type] + pos.cp_unit,
  612. pos.cp_flags & CP_INVERT);
  613. mutex_unlock(&ch->lock);
  614. return retval;
  615. }
  616. case CHIOMOVE:
  617. {
  618. struct changer_move mv;
  619. if (copy_from_user(&mv, argp, sizeof (mv)))
  620. return -EFAULT;
  621. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  622. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  623. dprintk("CHIOMOVE: invalid parameter\n");
  624. return -EBADSLT;
  625. }
  626. mutex_lock(&ch->lock);
  627. retval = ch_move(ch,0,
  628. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  629. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  630. mv.cm_flags & CM_INVERT);
  631. mutex_unlock(&ch->lock);
  632. return retval;
  633. }
  634. case CHIOEXCHANGE:
  635. {
  636. struct changer_exchange mv;
  637. if (copy_from_user(&mv, argp, sizeof (mv)))
  638. return -EFAULT;
  639. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  640. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  641. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  642. dprintk("CHIOEXCHANGE: invalid parameter\n");
  643. return -EBADSLT;
  644. }
  645. mutex_lock(&ch->lock);
  646. retval = ch_exchange
  647. (ch,0,
  648. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  649. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  650. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  651. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  652. mutex_unlock(&ch->lock);
  653. return retval;
  654. }
  655. case CHIOGSTATUS:
  656. {
  657. struct changer_element_status ces;
  658. if (copy_from_user(&ces, argp, sizeof (ces)))
  659. return -EFAULT;
  660. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  661. return -EINVAL;
  662. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  663. }
  664. case CHIOGELEM:
  665. {
  666. struct changer_get_element cge;
  667. u_char cmd[12];
  668. u_char *buffer;
  669. unsigned int elem;
  670. int result,i;
  671. if (copy_from_user(&cge, argp, sizeof (cge)))
  672. return -EFAULT;
  673. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  674. return -EINVAL;
  675. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  676. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  677. if (!buffer)
  678. return -ENOMEM;
  679. mutex_lock(&ch->lock);
  680. voltag_retry:
  681. memset(cmd,0,sizeof(cmd));
  682. cmd[0] = READ_ELEMENT_STATUS;
  683. cmd[1] = (ch->device->lun << 5) |
  684. (ch->voltags ? 0x10 : 0) |
  685. ch_elem_to_typecode(ch,elem);
  686. cmd[2] = (elem >> 8) & 0xff;
  687. cmd[3] = elem & 0xff;
  688. cmd[5] = 1;
  689. cmd[9] = 255;
  690. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  691. cge.cge_status = buffer[18];
  692. cge.cge_flags = 0;
  693. if (buffer[18] & CESTATUS_EXCEPT) {
  694. cge.cge_errno = EIO;
  695. }
  696. if (buffer[25] & 0x80) {
  697. cge.cge_flags |= CGE_SRC;
  698. if (buffer[25] & 0x40)
  699. cge.cge_flags |= CGE_INVERT;
  700. elem = (buffer[26]<<8) | buffer[27];
  701. for (i = 0; i < 4; i++) {
  702. if (elem >= ch->firsts[i] &&
  703. elem < ch->firsts[i] + ch->counts[i]) {
  704. cge.cge_srctype = i;
  705. cge.cge_srcunit = elem-ch->firsts[i];
  706. }
  707. }
  708. }
  709. if ((buffer[22] & 0x30) == 0x30) {
  710. cge.cge_flags |= CGE_IDLUN;
  711. cge.cge_id = buffer[23];
  712. cge.cge_lun = buffer[22] & 7;
  713. }
  714. if (buffer[9] & 0x80) {
  715. cge.cge_flags |= CGE_PVOLTAG;
  716. memcpy(cge.cge_pvoltag,buffer+28,36);
  717. }
  718. if (buffer[9] & 0x40) {
  719. cge.cge_flags |= CGE_AVOLTAG;
  720. memcpy(cge.cge_avoltag,buffer+64,36);
  721. }
  722. } else if (ch->voltags) {
  723. ch->voltags = 0;
  724. vprintk("device has no volume tag support\n");
  725. goto voltag_retry;
  726. }
  727. kfree(buffer);
  728. mutex_unlock(&ch->lock);
  729. if (copy_to_user(argp, &cge, sizeof (cge)))
  730. return -EFAULT;
  731. return result;
  732. }
  733. case CHIOINITELEM:
  734. {
  735. mutex_lock(&ch->lock);
  736. retval = ch_init_elem(ch);
  737. mutex_unlock(&ch->lock);
  738. return retval;
  739. }
  740. case CHIOSVOLTAG:
  741. {
  742. struct changer_set_voltag csv;
  743. int elem;
  744. if (copy_from_user(&csv, argp, sizeof(csv)))
  745. return -EFAULT;
  746. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  747. dprintk("CHIOSVOLTAG: invalid parameter\n");
  748. return -EBADSLT;
  749. }
  750. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  751. mutex_lock(&ch->lock);
  752. retval = ch_set_voltag(ch, elem,
  753. csv.csv_flags & CSV_AVOLTAG,
  754. csv.csv_flags & CSV_CLEARTAG,
  755. csv.csv_voltag);
  756. mutex_unlock(&ch->lock);
  757. return retval;
  758. }
  759. default:
  760. return scsi_ioctl(ch->device, cmd, argp);
  761. }
  762. }
  763. #ifdef CONFIG_COMPAT
  764. struct changer_element_status32 {
  765. int ces_type;
  766. compat_uptr_t ces_data;
  767. };
  768. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  769. static long ch_ioctl_compat(struct file * file,
  770. unsigned int cmd, unsigned long arg)
  771. {
  772. scsi_changer *ch = file->private_data;
  773. switch (cmd) {
  774. case CHIOGPARAMS:
  775. case CHIOGVPARAMS:
  776. case CHIOPOSITION:
  777. case CHIOMOVE:
  778. case CHIOEXCHANGE:
  779. case CHIOGELEM:
  780. case CHIOINITELEM:
  781. case CHIOSVOLTAG:
  782. /* compatible */
  783. return ch_ioctl(NULL /* inode, unused */,
  784. file, cmd, arg);
  785. case CHIOGSTATUS32:
  786. {
  787. struct changer_element_status32 ces32;
  788. unsigned char __user *data;
  789. if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
  790. return -EFAULT;
  791. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  792. return -EINVAL;
  793. data = compat_ptr(ces32.ces_data);
  794. return ch_gstatus(ch, ces32.ces_type, data);
  795. }
  796. default:
  797. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  798. return -ENOIOCTLCMD;
  799. }
  800. }
  801. #endif
  802. /* ------------------------------------------------------------------------ */
  803. static int ch_probe(struct device *dev)
  804. {
  805. struct scsi_device *sd = to_scsi_device(dev);
  806. scsi_changer *ch;
  807. if (sd->type != TYPE_MEDIUM_CHANGER)
  808. return -ENODEV;
  809. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  810. if (NULL == ch)
  811. return -ENOMEM;
  812. ch->minor = ch_devcount;
  813. sprintf(ch->name,"ch%d",ch->minor);
  814. mutex_init(&ch->lock);
  815. ch->device = sd;
  816. ch_readconfig(ch);
  817. if (init)
  818. ch_init_elem(ch);
  819. class_device_create(ch_sysfs_class, NULL,
  820. MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
  821. dev, "s%s", ch->name);
  822. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  823. spin_lock(&ch_devlist_lock);
  824. list_add_tail(&ch->list,&ch_devlist);
  825. ch_devcount++;
  826. spin_unlock(&ch_devlist_lock);
  827. return 0;
  828. }
  829. static int ch_remove(struct device *dev)
  830. {
  831. struct scsi_device *sd = to_scsi_device(dev);
  832. scsi_changer *tmp, *ch;
  833. spin_lock(&ch_devlist_lock);
  834. ch = NULL;
  835. list_for_each_entry(tmp,&ch_devlist,list) {
  836. if (tmp->device == sd)
  837. ch = tmp;
  838. }
  839. BUG_ON(NULL == ch);
  840. list_del(&ch->list);
  841. spin_unlock(&ch_devlist_lock);
  842. class_device_destroy(ch_sysfs_class,
  843. MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  844. kfree(ch->dt);
  845. kfree(ch);
  846. ch_devcount--;
  847. return 0;
  848. }
  849. static int __init init_ch_module(void)
  850. {
  851. int rc;
  852. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  853. ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
  854. if (IS_ERR(ch_sysfs_class)) {
  855. rc = PTR_ERR(ch_sysfs_class);
  856. return rc;
  857. }
  858. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  859. if (rc < 0) {
  860. printk("Unable to get major %d for SCSI-Changer\n",
  861. SCSI_CHANGER_MAJOR);
  862. goto fail1;
  863. }
  864. rc = scsi_register_driver(&ch_template.gendrv);
  865. if (rc < 0)
  866. goto fail2;
  867. return 0;
  868. fail2:
  869. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  870. fail1:
  871. class_destroy(ch_sysfs_class);
  872. return rc;
  873. }
  874. static void __exit exit_ch_module(void)
  875. {
  876. scsi_unregister_driver(&ch_template.gendrv);
  877. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  878. class_destroy(ch_sysfs_class);
  879. }
  880. module_init(init_ch_module);
  881. module_exit(exit_ch_module);
  882. /*
  883. * Local variables:
  884. * c-basic-offset: 8
  885. * End:
  886. */