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