ch.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * SCSI Media Changer device driver for Linux 2.6
  3. *
  4. * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
  5. *
  6. */
  7. #define VERSION "0.25"
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/major.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/completion.h>
  19. #include <linux/compat.h>
  20. #include <linux/chio.h> /* here are all the ioctls */
  21. #include <linux/mutex.h>
  22. #include <linux/idr.h>
  23. #include <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. #define CH_MAX_DEVS 128
  34. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  35. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  36. MODULE_LICENSE("GPL");
  37. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  38. static int init = 1;
  39. module_param(init, int, 0444);
  40. MODULE_PARM_DESC(init, \
  41. "initialize element status on driver load (default: on)");
  42. static int timeout_move = 300;
  43. module_param(timeout_move, int, 0644);
  44. MODULE_PARM_DESC(timeout_move,"timeout for move commands "
  45. "(default: 300 seconds)");
  46. static int timeout_init = 3600;
  47. module_param(timeout_init, int, 0644);
  48. MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
  49. "(default: 3600 seconds)");
  50. static int verbose = 1;
  51. module_param(verbose, int, 0644);
  52. MODULE_PARM_DESC(verbose,"be verbose (default: on)");
  53. static int debug = 0;
  54. module_param(debug, int, 0644);
  55. MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
  56. "detailed sense codes on scsi errors (default: off)");
  57. static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
  58. static int dt_lun[CH_DT_MAX];
  59. module_param_array(dt_id, int, NULL, 0444);
  60. module_param_array(dt_lun, int, NULL, 0444);
  61. /* tell the driver about vendor-specific slots */
  62. static int vendor_firsts[CH_TYPES-4];
  63. static int vendor_counts[CH_TYPES-4];
  64. module_param_array(vendor_firsts, int, NULL, 0444);
  65. module_param_array(vendor_counts, int, NULL, 0444);
  66. static const char * vendor_labels[CH_TYPES-4] = {
  67. "v0", "v1", "v2", "v3"
  68. };
  69. // module_param_string_array(vendor_labels, NULL, 0444);
  70. #define dprintk(fmt, arg...) if (debug) \
  71. printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg)
  72. #define vprintk(fmt, arg...) if (verbose) \
  73. printk(KERN_INFO "%s: " fmt, ch->name , ## arg)
  74. /* ------------------------------------------------------------------- */
  75. #define MAX_RETRIES 1
  76. static int ch_probe(struct device *);
  77. static int ch_remove(struct device *);
  78. static int ch_open(struct inode * inode, struct file * filp);
  79. static int ch_release(struct inode * inode, struct file * filp);
  80. static long ch_ioctl(struct file *filp, 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 mutex lock;
  97. } scsi_changer;
  98. static DEFINE_IDR(ch_index_idr);
  99. static DEFINE_SPINLOCK(ch_index_lock);
  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. .unlocked_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 *ch;
  535. int minor = iminor(inode);
  536. spin_lock(&ch_index_lock);
  537. ch = idr_find(&ch_index_idr, minor);
  538. if (NULL == ch || scsi_device_get(ch->device)) {
  539. spin_unlock(&ch_index_lock);
  540. return -ENXIO;
  541. }
  542. spin_unlock(&ch_index_lock);
  543. file->private_data = ch;
  544. return 0;
  545. }
  546. static int
  547. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  548. {
  549. if (type >= CH_TYPES || unit >= ch->counts[type])
  550. return -1;
  551. return 0;
  552. }
  553. static long ch_ioctl(struct file *file,
  554. unsigned int cmd, unsigned long arg)
  555. {
  556. scsi_changer *ch = file->private_data;
  557. int retval;
  558. void __user *argp = (void __user *)arg;
  559. switch (cmd) {
  560. case CHIOGPARAMS:
  561. {
  562. struct changer_params params;
  563. params.cp_curpicker = 0;
  564. params.cp_npickers = ch->counts[CHET_MT];
  565. params.cp_nslots = ch->counts[CHET_ST];
  566. params.cp_nportals = ch->counts[CHET_IE];
  567. params.cp_ndrives = ch->counts[CHET_DT];
  568. if (copy_to_user(argp, &params, sizeof(params)))
  569. return -EFAULT;
  570. return 0;
  571. }
  572. case CHIOGVPARAMS:
  573. {
  574. struct changer_vendor_params vparams;
  575. memset(&vparams,0,sizeof(vparams));
  576. if (ch->counts[CHET_V1]) {
  577. vparams.cvp_n1 = ch->counts[CHET_V1];
  578. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  579. }
  580. if (ch->counts[CHET_V2]) {
  581. vparams.cvp_n2 = ch->counts[CHET_V2];
  582. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  583. }
  584. if (ch->counts[CHET_V3]) {
  585. vparams.cvp_n3 = ch->counts[CHET_V3];
  586. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  587. }
  588. if (ch->counts[CHET_V4]) {
  589. vparams.cvp_n4 = ch->counts[CHET_V4];
  590. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  591. }
  592. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  593. return -EFAULT;
  594. return 0;
  595. }
  596. case CHIOPOSITION:
  597. {
  598. struct changer_position pos;
  599. if (copy_from_user(&pos, argp, sizeof (pos)))
  600. return -EFAULT;
  601. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  602. dprintk("CHIOPOSITION: invalid parameter\n");
  603. return -EBADSLT;
  604. }
  605. mutex_lock(&ch->lock);
  606. retval = ch_position(ch,0,
  607. ch->firsts[pos.cp_type] + pos.cp_unit,
  608. pos.cp_flags & CP_INVERT);
  609. mutex_unlock(&ch->lock);
  610. return retval;
  611. }
  612. case CHIOMOVE:
  613. {
  614. struct changer_move mv;
  615. if (copy_from_user(&mv, argp, sizeof (mv)))
  616. return -EFAULT;
  617. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  618. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  619. dprintk("CHIOMOVE: invalid parameter\n");
  620. return -EBADSLT;
  621. }
  622. mutex_lock(&ch->lock);
  623. retval = ch_move(ch,0,
  624. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  625. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  626. mv.cm_flags & CM_INVERT);
  627. mutex_unlock(&ch->lock);
  628. return retval;
  629. }
  630. case CHIOEXCHANGE:
  631. {
  632. struct changer_exchange mv;
  633. if (copy_from_user(&mv, argp, sizeof (mv)))
  634. return -EFAULT;
  635. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  636. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  637. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  638. dprintk("CHIOEXCHANGE: invalid parameter\n");
  639. return -EBADSLT;
  640. }
  641. mutex_lock(&ch->lock);
  642. retval = ch_exchange
  643. (ch,0,
  644. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  645. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  646. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  647. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  648. mutex_unlock(&ch->lock);
  649. return retval;
  650. }
  651. case CHIOGSTATUS:
  652. {
  653. struct changer_element_status ces;
  654. if (copy_from_user(&ces, argp, sizeof (ces)))
  655. return -EFAULT;
  656. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  657. return -EINVAL;
  658. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  659. }
  660. case CHIOGELEM:
  661. {
  662. struct changer_get_element cge;
  663. u_char cmd[12];
  664. u_char *buffer;
  665. unsigned int elem;
  666. int result,i;
  667. if (copy_from_user(&cge, argp, sizeof (cge)))
  668. return -EFAULT;
  669. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  670. return -EINVAL;
  671. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  672. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  673. if (!buffer)
  674. return -ENOMEM;
  675. mutex_lock(&ch->lock);
  676. voltag_retry:
  677. memset(cmd,0,sizeof(cmd));
  678. cmd[0] = READ_ELEMENT_STATUS;
  679. cmd[1] = (ch->device->lun << 5) |
  680. (ch->voltags ? 0x10 : 0) |
  681. ch_elem_to_typecode(ch,elem);
  682. cmd[2] = (elem >> 8) & 0xff;
  683. cmd[3] = elem & 0xff;
  684. cmd[5] = 1;
  685. cmd[9] = 255;
  686. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  687. cge.cge_status = buffer[18];
  688. cge.cge_flags = 0;
  689. if (buffer[18] & CESTATUS_EXCEPT) {
  690. cge.cge_errno = EIO;
  691. }
  692. if (buffer[25] & 0x80) {
  693. cge.cge_flags |= CGE_SRC;
  694. if (buffer[25] & 0x40)
  695. cge.cge_flags |= CGE_INVERT;
  696. elem = (buffer[26]<<8) | buffer[27];
  697. for (i = 0; i < 4; i++) {
  698. if (elem >= ch->firsts[i] &&
  699. elem < ch->firsts[i] + ch->counts[i]) {
  700. cge.cge_srctype = i;
  701. cge.cge_srcunit = elem-ch->firsts[i];
  702. }
  703. }
  704. }
  705. if ((buffer[22] & 0x30) == 0x30) {
  706. cge.cge_flags |= CGE_IDLUN;
  707. cge.cge_id = buffer[23];
  708. cge.cge_lun = buffer[22] & 7;
  709. }
  710. if (buffer[9] & 0x80) {
  711. cge.cge_flags |= CGE_PVOLTAG;
  712. memcpy(cge.cge_pvoltag,buffer+28,36);
  713. }
  714. if (buffer[9] & 0x40) {
  715. cge.cge_flags |= CGE_AVOLTAG;
  716. memcpy(cge.cge_avoltag,buffer+64,36);
  717. }
  718. } else if (ch->voltags) {
  719. ch->voltags = 0;
  720. vprintk("device has no volume tag support\n");
  721. goto voltag_retry;
  722. }
  723. kfree(buffer);
  724. mutex_unlock(&ch->lock);
  725. if (copy_to_user(argp, &cge, sizeof (cge)))
  726. return -EFAULT;
  727. return result;
  728. }
  729. case CHIOINITELEM:
  730. {
  731. mutex_lock(&ch->lock);
  732. retval = ch_init_elem(ch);
  733. mutex_unlock(&ch->lock);
  734. return retval;
  735. }
  736. case CHIOSVOLTAG:
  737. {
  738. struct changer_set_voltag csv;
  739. int elem;
  740. if (copy_from_user(&csv, argp, sizeof(csv)))
  741. return -EFAULT;
  742. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  743. dprintk("CHIOSVOLTAG: invalid parameter\n");
  744. return -EBADSLT;
  745. }
  746. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  747. mutex_lock(&ch->lock);
  748. retval = ch_set_voltag(ch, elem,
  749. csv.csv_flags & CSV_AVOLTAG,
  750. csv.csv_flags & CSV_CLEARTAG,
  751. csv.csv_voltag);
  752. mutex_unlock(&ch->lock);
  753. return retval;
  754. }
  755. default:
  756. return scsi_ioctl(ch->device, cmd, argp);
  757. }
  758. }
  759. #ifdef CONFIG_COMPAT
  760. struct changer_element_status32 {
  761. int ces_type;
  762. compat_uptr_t ces_data;
  763. };
  764. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  765. static long ch_ioctl_compat(struct file * file,
  766. unsigned int cmd, unsigned long arg)
  767. {
  768. scsi_changer *ch = file->private_data;
  769. switch (cmd) {
  770. case CHIOGPARAMS:
  771. case CHIOGVPARAMS:
  772. case CHIOPOSITION:
  773. case CHIOMOVE:
  774. case CHIOEXCHANGE:
  775. case CHIOGELEM:
  776. case CHIOINITELEM:
  777. case CHIOSVOLTAG:
  778. /* compatible */
  779. return ch_ioctl(file, cmd, arg);
  780. case CHIOGSTATUS32:
  781. {
  782. struct changer_element_status32 ces32;
  783. unsigned char __user *data;
  784. if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
  785. return -EFAULT;
  786. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  787. return -EINVAL;
  788. data = compat_ptr(ces32.ces_data);
  789. return ch_gstatus(ch, ces32.ces_type, data);
  790. }
  791. default:
  792. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  793. return -ENOIOCTLCMD;
  794. }
  795. }
  796. #endif
  797. /* ------------------------------------------------------------------------ */
  798. static int ch_probe(struct device *dev)
  799. {
  800. struct scsi_device *sd = to_scsi_device(dev);
  801. struct class_device *class_dev;
  802. int minor, ret = -ENOMEM;
  803. scsi_changer *ch;
  804. if (sd->type != TYPE_MEDIUM_CHANGER)
  805. return -ENODEV;
  806. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  807. if (NULL == ch)
  808. return -ENOMEM;
  809. if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
  810. goto free_ch;
  811. spin_lock(&ch_index_lock);
  812. ret = idr_get_new(&ch_index_idr, ch, &minor);
  813. spin_unlock(&ch_index_lock);
  814. if (ret)
  815. goto free_ch;
  816. if (minor > CH_MAX_DEVS) {
  817. ret = -ENODEV;
  818. goto remove_idr;
  819. }
  820. ch->minor = minor;
  821. sprintf(ch->name,"ch%d",ch->minor);
  822. class_dev = class_device_create(ch_sysfs_class, NULL,
  823. MKDEV(SCSI_CHANGER_MAJOR, ch->minor),
  824. dev, "s%s", ch->name);
  825. if (IS_ERR(class_dev)) {
  826. printk(KERN_WARNING "ch%d: class_device_create failed\n",
  827. ch->minor);
  828. ret = PTR_ERR(class_dev);
  829. goto remove_idr;
  830. }
  831. mutex_init(&ch->lock);
  832. ch->device = sd;
  833. ch_readconfig(ch);
  834. if (init)
  835. ch_init_elem(ch);
  836. dev_set_drvdata(dev, ch);
  837. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  838. return 0;
  839. remove_idr:
  840. idr_remove(&ch_index_idr, minor);
  841. free_ch:
  842. kfree(ch);
  843. return ret;
  844. }
  845. static int ch_remove(struct device *dev)
  846. {
  847. scsi_changer *ch = dev_get_drvdata(dev);
  848. spin_lock(&ch_index_lock);
  849. idr_remove(&ch_index_idr, ch->minor);
  850. spin_unlock(&ch_index_lock);
  851. class_device_destroy(ch_sysfs_class,
  852. MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  853. kfree(ch->dt);
  854. kfree(ch);
  855. return 0;
  856. }
  857. static int __init init_ch_module(void)
  858. {
  859. int rc;
  860. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  861. ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
  862. if (IS_ERR(ch_sysfs_class)) {
  863. rc = PTR_ERR(ch_sysfs_class);
  864. return rc;
  865. }
  866. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  867. if (rc < 0) {
  868. printk("Unable to get major %d for SCSI-Changer\n",
  869. SCSI_CHANGER_MAJOR);
  870. goto fail1;
  871. }
  872. rc = scsi_register_driver(&ch_template.gendrv);
  873. if (rc < 0)
  874. goto fail2;
  875. return 0;
  876. fail2:
  877. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  878. fail1:
  879. class_destroy(ch_sysfs_class);
  880. return rc;
  881. }
  882. static void __exit exit_ch_module(void)
  883. {
  884. scsi_unregister_driver(&ch_template.gendrv);
  885. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  886. class_destroy(ch_sysfs_class);
  887. idr_destroy(&ch_index_idr);
  888. }
  889. module_init(init_ch_module);
  890. module_exit(exit_ch_module);
  891. /*
  892. * Local variables:
  893. * c-basic-offset: 8
  894. * End:
  895. */