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