ch.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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_request.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 spinlock_t ch_devlist_lock = SPIN_LOCK_UNLOCKED;
  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(unsigned char *sense_buffer)
  159. {
  160. int i,errno = 0;
  161. /* Check to see if additional sense information is available */
  162. if (sense_buffer[7] > 5 &&
  163. sense_buffer[12] != 0) {
  164. for (i = 0; err[i].errno != 0; i++) {
  165. if (err[i].sense == sense_buffer[ 2] &&
  166. err[i].asc == sense_buffer[12] &&
  167. err[i].ascq == sense_buffer[13]) {
  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;
  183. struct scsi_request *sr;
  184. sr = scsi_allocate_request(ch->device, GFP_KERNEL);
  185. if (NULL == sr)
  186. return -ENOMEM;
  187. timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
  188. ? timeout_init : timeout_move;
  189. retry:
  190. errno = 0;
  191. if (debug) {
  192. dprintk("command: ");
  193. __scsi_print_command(cmd);
  194. }
  195. scsi_wait_req(sr, cmd, buffer, buflength,
  196. timeout * HZ, MAX_RETRIES);
  197. dprintk("result: 0x%x\n",sr->sr_result);
  198. if (driver_byte(sr->sr_result) & DRIVER_SENSE) {
  199. if (debug)
  200. scsi_print_req_sense(ch->name, sr);
  201. errno = ch_find_errno(sr->sr_sense_buffer);
  202. switch(sr->sr_sense_buffer[2] & 0xf) {
  203. case UNIT_ATTENTION:
  204. ch->unit_attention = 1;
  205. if (retries++ < 3)
  206. goto retry;
  207. break;
  208. }
  209. }
  210. scsi_release_request(sr);
  211. return errno;
  212. }
  213. /* ------------------------------------------------------------------------ */
  214. static int
  215. ch_elem_to_typecode(scsi_changer *ch, u_int elem)
  216. {
  217. int i;
  218. for (i = 0; i < CH_TYPES; i++) {
  219. if (elem >= ch->firsts[i] &&
  220. elem < ch->firsts[i] +
  221. ch->counts[i])
  222. return i+1;
  223. }
  224. return 0;
  225. }
  226. static int
  227. ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
  228. {
  229. u_char cmd[12];
  230. u_char *buffer;
  231. int result;
  232. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  233. if(!buffer)
  234. return -ENOMEM;
  235. retry:
  236. memset(cmd,0,sizeof(cmd));
  237. cmd[0] = READ_ELEMENT_STATUS;
  238. cmd[1] = (ch->device->lun << 5) |
  239. (ch->voltags ? 0x10 : 0) |
  240. ch_elem_to_typecode(ch,elem);
  241. cmd[2] = (elem >> 8) & 0xff;
  242. cmd[3] = elem & 0xff;
  243. cmd[5] = 1;
  244. cmd[9] = 255;
  245. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  246. if (((buffer[16] << 8) | buffer[17]) != elem) {
  247. dprintk("asked for element 0x%02x, got 0x%02x\n",
  248. elem,(buffer[16] << 8) | buffer[17]);
  249. kfree(buffer);
  250. return -EIO;
  251. }
  252. memcpy(data,buffer+16,16);
  253. } else {
  254. if (ch->voltags) {
  255. ch->voltags = 0;
  256. vprintk("device has no volume tag support\n");
  257. goto retry;
  258. }
  259. dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
  260. }
  261. kfree(buffer);
  262. return result;
  263. }
  264. static int
  265. ch_init_elem(scsi_changer *ch)
  266. {
  267. int err;
  268. u_char cmd[6];
  269. vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
  270. memset(cmd,0,sizeof(cmd));
  271. cmd[0] = INITIALIZE_ELEMENT_STATUS;
  272. cmd[1] = ch->device->lun << 5;
  273. err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  274. vprintk("... finished\n");
  275. return err;
  276. }
  277. static int
  278. ch_readconfig(scsi_changer *ch)
  279. {
  280. u_char cmd[10], data[16];
  281. u_char *buffer;
  282. int result,id,lun,i;
  283. u_int elem;
  284. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  285. if (!buffer)
  286. return -ENOMEM;
  287. memset(buffer,0,512);
  288. memset(cmd,0,sizeof(cmd));
  289. cmd[0] = MODE_SENSE;
  290. cmd[1] = ch->device->lun << 5;
  291. cmd[2] = 0x1d;
  292. cmd[4] = 255;
  293. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  294. if (0 != result) {
  295. cmd[1] |= (1<<3);
  296. result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
  297. }
  298. if (0 == result) {
  299. ch->firsts[CHET_MT] =
  300. (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
  301. ch->counts[CHET_MT] =
  302. (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
  303. ch->firsts[CHET_ST] =
  304. (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
  305. ch->counts[CHET_ST] =
  306. (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
  307. ch->firsts[CHET_IE] =
  308. (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
  309. ch->counts[CHET_IE] =
  310. (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
  311. ch->firsts[CHET_DT] =
  312. (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
  313. ch->counts[CHET_DT] =
  314. (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
  315. vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
  316. ch->firsts[CHET_MT],
  317. ch->counts[CHET_MT]);
  318. vprintk("type #2 (st): 0x%x+%d [storage]\n",
  319. ch->firsts[CHET_ST],
  320. ch->counts[CHET_ST]);
  321. vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
  322. ch->firsts[CHET_IE],
  323. ch->counts[CHET_IE]);
  324. vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
  325. ch->firsts[CHET_DT],
  326. ch->counts[CHET_DT]);
  327. } else {
  328. vprintk("reading element address assigment page failed!\n");
  329. }
  330. /* vendor specific element types */
  331. for (i = 0; i < 4; i++) {
  332. if (0 == vendor_counts[i])
  333. continue;
  334. if (NULL == vendor_labels[i])
  335. continue;
  336. ch->firsts[CHET_V1+i] = vendor_firsts[i];
  337. ch->counts[CHET_V1+i] = vendor_counts[i];
  338. vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
  339. i+5,i+1,vendor_firsts[i],vendor_counts[i],
  340. vendor_labels[i]);
  341. }
  342. /* look up the devices of the data transfer elements */
  343. ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
  344. GFP_KERNEL);
  345. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  346. id = -1;
  347. lun = 0;
  348. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  349. id = dt_id[elem];
  350. lun = dt_lun[elem];
  351. vprintk("dt 0x%x: [insmod option] ",
  352. elem+ch->firsts[CHET_DT]);
  353. } else if (0 != ch_read_element_status
  354. (ch,elem+ch->firsts[CHET_DT],data)) {
  355. vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
  356. elem+ch->firsts[CHET_DT]);
  357. } else {
  358. vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  359. if (data[6] & 0x80) {
  360. if (verbose)
  361. printk("not this SCSI bus\n");
  362. ch->dt[elem] = NULL;
  363. } else if (0 == (data[6] & 0x30)) {
  364. if (verbose)
  365. printk("ID/LUN unknown\n");
  366. ch->dt[elem] = NULL;
  367. } else {
  368. id = ch->device->id;
  369. lun = 0;
  370. if (data[6] & 0x20) id = data[7];
  371. if (data[6] & 0x10) lun = data[6] & 7;
  372. }
  373. }
  374. if (-1 != id) {
  375. if (verbose)
  376. printk("ID %i, LUN %i, ",id,lun);
  377. ch->dt[elem] =
  378. scsi_device_lookup(ch->device->host,
  379. ch->device->channel,
  380. id,lun);
  381. if (!ch->dt[elem]) {
  382. /* should not happen */
  383. if (verbose)
  384. printk("Huh? device not found!\n");
  385. } else {
  386. if (verbose)
  387. printk("name: %8.8s %16.16s %4.4s\n",
  388. ch->dt[elem]->vendor,
  389. ch->dt[elem]->model,
  390. ch->dt[elem]->rev);
  391. }
  392. }
  393. }
  394. ch->voltags = 1;
  395. kfree(buffer);
  396. return 0;
  397. }
  398. /* ------------------------------------------------------------------------ */
  399. static int
  400. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  401. {
  402. u_char cmd[10];
  403. dprintk("position: 0x%x\n",elem);
  404. if (0 == trans)
  405. trans = ch->firsts[CHET_MT];
  406. memset(cmd,0,sizeof(cmd));
  407. cmd[0] = POSITION_TO_ELEMENT;
  408. cmd[1] = ch->device->lun << 5;
  409. cmd[2] = (trans >> 8) & 0xff;
  410. cmd[3] = trans & 0xff;
  411. cmd[4] = (elem >> 8) & 0xff;
  412. cmd[5] = elem & 0xff;
  413. cmd[8] = rotate ? 1 : 0;
  414. return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
  415. }
  416. static int
  417. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  418. {
  419. u_char cmd[12];
  420. dprintk("move: 0x%x => 0x%x\n",src,dest);
  421. if (0 == trans)
  422. trans = ch->firsts[CHET_MT];
  423. memset(cmd,0,sizeof(cmd));
  424. cmd[0] = MOVE_MEDIUM;
  425. cmd[1] = ch->device->lun << 5;
  426. cmd[2] = (trans >> 8) & 0xff;
  427. cmd[3] = trans & 0xff;
  428. cmd[4] = (src >> 8) & 0xff;
  429. cmd[5] = src & 0xff;
  430. cmd[6] = (dest >> 8) & 0xff;
  431. cmd[7] = dest & 0xff;
  432. cmd[10] = rotate ? 1 : 0;
  433. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  434. }
  435. static int
  436. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  437. u_int dest1, u_int dest2, int rotate1, int rotate2)
  438. {
  439. u_char cmd[12];
  440. dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
  441. src,dest1,dest2);
  442. if (0 == trans)
  443. trans = ch->firsts[CHET_MT];
  444. memset(cmd,0,sizeof(cmd));
  445. cmd[0] = EXCHANGE_MEDIUM;
  446. cmd[1] = ch->device->lun << 5;
  447. cmd[2] = (trans >> 8) & 0xff;
  448. cmd[3] = trans & 0xff;
  449. cmd[4] = (src >> 8) & 0xff;
  450. cmd[5] = src & 0xff;
  451. cmd[6] = (dest1 >> 8) & 0xff;
  452. cmd[7] = dest1 & 0xff;
  453. cmd[8] = (dest2 >> 8) & 0xff;
  454. cmd[9] = dest2 & 0xff;
  455. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  456. return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
  457. }
  458. static void
  459. ch_check_voltag(char *tag)
  460. {
  461. int i;
  462. for (i = 0; i < 32; i++) {
  463. /* restrict to ascii */
  464. if (tag[i] >= 0x7f || tag[i] < 0x20)
  465. tag[i] = ' ';
  466. /* don't allow search wildcards */
  467. if (tag[i] == '?' ||
  468. tag[i] == '*')
  469. tag[i] = ' ';
  470. }
  471. }
  472. static int
  473. ch_set_voltag(scsi_changer *ch, u_int elem,
  474. int alternate, int clear, u_char *tag)
  475. {
  476. u_char cmd[12];
  477. u_char *buffer;
  478. int result;
  479. buffer = kmalloc(512, GFP_KERNEL);
  480. if (!buffer)
  481. return -ENOMEM;
  482. memset(buffer,0,512);
  483. dprintk("%s %s voltag: 0x%x => \"%s\"\n",
  484. clear ? "clear" : "set",
  485. alternate ? "alternate" : "primary",
  486. elem, tag);
  487. memset(cmd,0,sizeof(cmd));
  488. cmd[0] = SEND_VOLUME_TAG;
  489. cmd[1] = (ch->device->lun << 5) |
  490. ch_elem_to_typecode(ch,elem);
  491. cmd[2] = (elem >> 8) & 0xff;
  492. cmd[3] = elem & 0xff;
  493. cmd[5] = clear
  494. ? (alternate ? 0x0d : 0x0c)
  495. : (alternate ? 0x0b : 0x0a);
  496. cmd[9] = 255;
  497. memcpy(buffer,tag,32);
  498. ch_check_voltag(buffer);
  499. result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
  500. kfree(buffer);
  501. return result;
  502. }
  503. static int ch_gstatus(scsi_changer *ch, int type, unsigned char *dest)
  504. {
  505. int retval = 0;
  506. u_char data[16];
  507. unsigned int i;
  508. down(&ch->lock);
  509. for (i = 0; i < ch->counts[type]; i++) {
  510. if (0 != ch_read_element_status
  511. (ch, ch->firsts[type]+i,data)) {
  512. retval = -EIO;
  513. break;
  514. }
  515. put_user(data[2], dest+i);
  516. if (data[2] & CESTATUS_EXCEPT)
  517. vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
  518. ch->firsts[type]+i,
  519. (int)data[4],(int)data[5]);
  520. retval = ch_read_element_status
  521. (ch, ch->firsts[type]+i,data);
  522. if (0 != retval)
  523. break;
  524. }
  525. up(&ch->lock);
  526. return retval;
  527. }
  528. /* ------------------------------------------------------------------------ */
  529. static int
  530. ch_release(struct inode *inode, struct file *file)
  531. {
  532. scsi_changer *ch = file->private_data;
  533. scsi_device_put(ch->device);
  534. file->private_data = NULL;
  535. return 0;
  536. }
  537. static int
  538. ch_open(struct inode *inode, struct file *file)
  539. {
  540. scsi_changer *tmp, *ch;
  541. int minor = iminor(inode);
  542. spin_lock(&ch_devlist_lock);
  543. ch = NULL;
  544. list_for_each_entry(tmp,&ch_devlist,list) {
  545. if (tmp->minor == minor)
  546. ch = tmp;
  547. }
  548. if (NULL == ch || scsi_device_get(ch->device)) {
  549. spin_unlock(&ch_devlist_lock);
  550. return -ENXIO;
  551. }
  552. spin_unlock(&ch_devlist_lock);
  553. file->private_data = ch;
  554. return 0;
  555. }
  556. static int
  557. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  558. {
  559. if (type >= CH_TYPES || unit >= ch->counts[type])
  560. return -1;
  561. return 0;
  562. }
  563. static int ch_ioctl(struct inode * inode, struct file * file,
  564. unsigned int cmd, unsigned long arg)
  565. {
  566. scsi_changer *ch = file->private_data;
  567. int retval;
  568. switch (cmd) {
  569. case CHIOGPARAMS:
  570. {
  571. struct changer_params params;
  572. params.cp_curpicker = 0;
  573. params.cp_npickers = ch->counts[CHET_MT];
  574. params.cp_nslots = ch->counts[CHET_ST];
  575. params.cp_nportals = ch->counts[CHET_IE];
  576. params.cp_ndrives = ch->counts[CHET_DT];
  577. if (copy_to_user((void *) arg, &params, sizeof(params)))
  578. return -EFAULT;
  579. return 0;
  580. }
  581. case CHIOGVPARAMS:
  582. {
  583. struct changer_vendor_params vparams;
  584. memset(&vparams,0,sizeof(vparams));
  585. if (ch->counts[CHET_V1]) {
  586. vparams.cvp_n1 = ch->counts[CHET_V1];
  587. strncpy(vparams.cvp_label1,vendor_labels[0],16);
  588. }
  589. if (ch->counts[CHET_V2]) {
  590. vparams.cvp_n2 = ch->counts[CHET_V2];
  591. strncpy(vparams.cvp_label2,vendor_labels[1],16);
  592. }
  593. if (ch->counts[CHET_V3]) {
  594. vparams.cvp_n3 = ch->counts[CHET_V3];
  595. strncpy(vparams.cvp_label3,vendor_labels[2],16);
  596. }
  597. if (ch->counts[CHET_V4]) {
  598. vparams.cvp_n4 = ch->counts[CHET_V4];
  599. strncpy(vparams.cvp_label4,vendor_labels[3],16);
  600. }
  601. if (copy_to_user((void *) arg, &vparams, sizeof(vparams)))
  602. return -EFAULT;
  603. return 0;
  604. }
  605. case CHIOPOSITION:
  606. {
  607. struct changer_position pos;
  608. if (copy_from_user(&pos, (void*)arg, sizeof (pos)))
  609. return -EFAULT;
  610. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  611. dprintk("CHIOPOSITION: invalid parameter\n");
  612. return -EBADSLT;
  613. }
  614. down(&ch->lock);
  615. retval = ch_position(ch,0,
  616. ch->firsts[pos.cp_type] + pos.cp_unit,
  617. pos.cp_flags & CP_INVERT);
  618. up(&ch->lock);
  619. return retval;
  620. }
  621. case CHIOMOVE:
  622. {
  623. struct changer_move mv;
  624. if (copy_from_user(&mv, (void*)arg, sizeof (mv)))
  625. return -EFAULT;
  626. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  627. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  628. dprintk("CHIOMOVE: invalid parameter\n");
  629. return -EBADSLT;
  630. }
  631. down(&ch->lock);
  632. retval = ch_move(ch,0,
  633. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  634. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  635. mv.cm_flags & CM_INVERT);
  636. up(&ch->lock);
  637. return retval;
  638. }
  639. case CHIOEXCHANGE:
  640. {
  641. struct changer_exchange mv;
  642. if (copy_from_user(&mv, (void*)arg, sizeof (mv)))
  643. return -EFAULT;
  644. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  645. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  646. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  647. dprintk("CHIOEXCHANGE: invalid parameter\n");
  648. return -EBADSLT;
  649. }
  650. down(&ch->lock);
  651. retval = ch_exchange
  652. (ch,0,
  653. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  654. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  655. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  656. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  657. up(&ch->lock);
  658. return retval;
  659. }
  660. case CHIOGSTATUS:
  661. {
  662. struct changer_element_status ces;
  663. if (copy_from_user(&ces, (void*)arg, sizeof (ces)))
  664. return -EFAULT;
  665. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  666. return -EINVAL;
  667. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  668. }
  669. case CHIOGELEM:
  670. {
  671. struct changer_get_element cge;
  672. u_char cmd[12];
  673. u_char *buffer;
  674. unsigned int elem;
  675. int result,i;
  676. if (copy_from_user(&cge, (void*)arg, sizeof (cge)))
  677. return -EFAULT;
  678. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  679. return -EINVAL;
  680. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  681. buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
  682. if (!buffer)
  683. return -ENOMEM;
  684. down(&ch->lock);
  685. voltag_retry:
  686. memset(cmd,0,sizeof(cmd));
  687. cmd[0] = READ_ELEMENT_STATUS;
  688. cmd[1] = (ch->device->lun << 5) |
  689. (ch->voltags ? 0x10 : 0) |
  690. ch_elem_to_typecode(ch,elem);
  691. cmd[2] = (elem >> 8) & 0xff;
  692. cmd[3] = elem & 0xff;
  693. cmd[5] = 1;
  694. cmd[9] = 255;
  695. if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
  696. cge.cge_status = buffer[18];
  697. cge.cge_flags = 0;
  698. if (buffer[18] & CESTATUS_EXCEPT) {
  699. cge.cge_errno = EIO;
  700. }
  701. if (buffer[25] & 0x80) {
  702. cge.cge_flags |= CGE_SRC;
  703. if (buffer[25] & 0x40)
  704. cge.cge_flags |= CGE_INVERT;
  705. elem = (buffer[26]<<8) | buffer[27];
  706. for (i = 0; i < 4; i++) {
  707. if (elem >= ch->firsts[i] &&
  708. elem < ch->firsts[i] + ch->counts[i]) {
  709. cge.cge_srctype = i;
  710. cge.cge_srcunit = elem-ch->firsts[i];
  711. }
  712. }
  713. }
  714. if ((buffer[22] & 0x30) == 0x30) {
  715. cge.cge_flags |= CGE_IDLUN;
  716. cge.cge_id = buffer[23];
  717. cge.cge_lun = buffer[22] & 7;
  718. }
  719. if (buffer[9] & 0x80) {
  720. cge.cge_flags |= CGE_PVOLTAG;
  721. memcpy(cge.cge_pvoltag,buffer+28,36);
  722. }
  723. if (buffer[9] & 0x40) {
  724. cge.cge_flags |= CGE_AVOLTAG;
  725. memcpy(cge.cge_avoltag,buffer+64,36);
  726. }
  727. } else if (ch->voltags) {
  728. ch->voltags = 0;
  729. vprintk("device has no volume tag support\n");
  730. goto voltag_retry;
  731. }
  732. kfree(buffer);
  733. up(&ch->lock);
  734. if (copy_to_user((void*)arg, &cge, sizeof (cge)))
  735. return -EFAULT;
  736. return result;
  737. }
  738. case CHIOINITELEM:
  739. {
  740. down(&ch->lock);
  741. retval = ch_init_elem(ch);
  742. up(&ch->lock);
  743. return retval;
  744. }
  745. case CHIOSVOLTAG:
  746. {
  747. struct changer_set_voltag csv;
  748. int elem;
  749. if (copy_from_user(&csv, (void*)arg, sizeof(csv)))
  750. return -EFAULT;
  751. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  752. dprintk("CHIOSVOLTAG: invalid parameter\n");
  753. return -EBADSLT;
  754. }
  755. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  756. down(&ch->lock);
  757. retval = ch_set_voltag(ch, elem,
  758. csv.csv_flags & CSV_AVOLTAG,
  759. csv.csv_flags & CSV_CLEARTAG,
  760. csv.csv_voltag);
  761. up(&ch->lock);
  762. return retval;
  763. }
  764. default:
  765. return scsi_ioctl(ch->device, cmd, (void*)arg);
  766. }
  767. }
  768. #ifdef CONFIG_COMPAT
  769. struct changer_element_status32 {
  770. int ces_type;
  771. compat_uptr_t ces_data;
  772. };
  773. #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
  774. static long ch_ioctl_compat(struct file * file,
  775. unsigned int cmd, unsigned long arg)
  776. {
  777. scsi_changer *ch = file->private_data;
  778. switch (cmd) {
  779. case CHIOGPARAMS:
  780. case CHIOGVPARAMS:
  781. case CHIOPOSITION:
  782. case CHIOMOVE:
  783. case CHIOEXCHANGE:
  784. case CHIOGELEM:
  785. case CHIOINITELEM:
  786. case CHIOSVOLTAG:
  787. /* compatible */
  788. return ch_ioctl(NULL /* inode, unused */,
  789. file, cmd, arg);
  790. case CHIOGSTATUS32:
  791. {
  792. struct changer_element_status32 ces32;
  793. unsigned char *data;
  794. if (copy_from_user(&ces32, (void*)arg, sizeof (ces32)))
  795. return -EFAULT;
  796. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  797. return -EINVAL;
  798. data = compat_ptr(ces32.ces_data);
  799. return ch_gstatus(ch, ces32.ces_type, data);
  800. }
  801. default:
  802. // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
  803. return -ENOIOCTLCMD;
  804. }
  805. }
  806. #endif
  807. /* ------------------------------------------------------------------------ */
  808. static int ch_probe(struct device *dev)
  809. {
  810. struct scsi_device *sd = to_scsi_device(dev);
  811. scsi_changer *ch;
  812. if (sd->type != TYPE_MEDIUM_CHANGER)
  813. return -ENODEV;
  814. ch = kmalloc(sizeof(*ch), GFP_KERNEL);
  815. if (NULL == ch)
  816. return -ENOMEM;
  817. memset(ch,0,sizeof(*ch));
  818. ch->minor = ch_devcount;
  819. sprintf(ch->name,"ch%d",ch->minor);
  820. init_MUTEX(&ch->lock);
  821. ch->device = sd;
  822. ch_readconfig(ch);
  823. if (init)
  824. ch_init_elem(ch);
  825. class_device_create(ch_sysfs_class,
  826. MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
  827. dev, "s%s", ch->name);
  828. printk(KERN_INFO "Attached scsi changer %s "
  829. "at scsi%d, channel %d, id %d, lun %d\n",
  830. ch->name, sd->host->host_no, sd->channel, sd->id, sd->lun);
  831. spin_lock(&ch_devlist_lock);
  832. list_add_tail(&ch->list,&ch_devlist);
  833. ch_devcount++;
  834. spin_unlock(&ch_devlist_lock);
  835. return 0;
  836. }
  837. static int ch_remove(struct device *dev)
  838. {
  839. struct scsi_device *sd = to_scsi_device(dev);
  840. scsi_changer *tmp, *ch;
  841. spin_lock(&ch_devlist_lock);
  842. ch = NULL;
  843. list_for_each_entry(tmp,&ch_devlist,list) {
  844. if (tmp->device == sd)
  845. ch = tmp;
  846. }
  847. BUG_ON(NULL == ch);
  848. list_del(&ch->list);
  849. spin_unlock(&ch_devlist_lock);
  850. class_device_destroy(ch_sysfs_class,
  851. MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
  852. kfree(ch->dt);
  853. kfree(ch);
  854. ch_devcount--;
  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. }
  888. module_init(init_ch_module);
  889. module_exit(exit_ch_module);
  890. /*
  891. * Local variables:
  892. * c-basic-offset: 8
  893. * End:
  894. */