ch.c 25 KB

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