ch.c 24 KB

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