-
Notifications
You must be signed in to change notification settings - Fork 1
/
dexdrive.c
1391 lines (1093 loc) · 32.1 KB
/
dexdrive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
dexdrive.c: Linux block driver for the DexDrive
Copyright (C) 2002,2009 Frédéric Brière
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/slab.h> /* kmalloc() */
#include <linux/string.h> /* memcpy() */
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/blkdev.h>
#include <linux/tty.h>
#include <linux/tty_ldisc.h>
#include "dexdrive.h"
#include "compat.h"
/*
* The maximum message length is 261, during READ/WRITE for the N64 model:
* I A I <opcode> <256 bytes of data> <checksum>.
* (Although PAGE replies can be much longer than this.)
*/
#define DEX_NAME "dexdrive" /* Driver name */
#define DEX_BUFSIZE 261 /* Size of input/output buffer */
#define DEX_TIMEOUT 100 /* Timeout in msecs when waiting */
#define DEX_MAX_RETRY 2 /* Maximum number of retries */
#define DEX_MAX_DEVICES 4 /* Maximum number of devices */
/* DexDrive models */
enum dex_model { DEX_MODEL_PSX, DEX_MODEL_N64 };
/* List of operations we perform with the device */
enum dex_command {
DEX_CMD_NONE,
DEX_CMD_READ,
DEX_CMD_SEEK,
DEX_CMD_WRITE,
DEX_CMD_INIT,
DEX_CMD_MAGIC,
DEX_CMD_ON,
DEX_CMD_OFF,
DEX_CMD_STATUS,
DEX_CMD_PAGE, /* TODO: Not implemented yet */
};
/* List of opcodes */
enum dex_opcode {
DEX_OPCODE_INIT = 0x00,
DEX_OPCODE_STATUS = 0x01,
DEX_OPCODE_READ = 0x02,
DEX_OPCODE_SEEK = 0x03,
DEX_OPCODE_WRITE = 0x04,
DEX_OPCODE_PAGE = 0x05,
DEX_OPCODE_LIGHT = 0x07,
DEX_OPCODE_MAGIC = 0x27,
DEX_OPCODE_POUT = 0x20,
DEX_OPCODE_ERROR = 0x21,
DEX_OPCODE_NOCARD = 0x22,
DEX_OPCODE_CARD = 0x23,
DEX_OPCODE_CARD_NEW = 0x25,
DEX_OPCODE_SEEK_OK = 0x27,
DEX_OPCODE_WOK = 0x28,
DEX_OPCODE_WSAME = 0x29,
DEX_OPCODE_WAIT = 0x2a,
DEX_OPCODE_ID = 0x40,
DEX_OPCODE_DATA = 0x41,
};
/* Prefix sent with all commands/replies */
#define DEX_CMD_PREFIX "IAI"
/* Default init string used by InterAct's software */
#define DEX_INIT_STR "\x10\x29\x23\xbe\x84\xe1\x6c\xd6\xae\x52" \
"\x90\x49\xf1\xf1\xbb\xe9\xeb"
/* Logical block size for our block request queue */
#define DEX_REQUEST_QUEUE_BLOCK_SIZE 512
static unsigned int major;
module_param(major, uint, 0);
MODULE_PARM_DESC(major, "Major device number (automatically assigned by default)");
/* This must be configurable until the day we get our own value */
static int ldisc = DEX_LDISC;
module_param(ldisc, int, 0);
MODULE_PARM_DESC(ldisc, "Line discipline number");
#define warn(msg, args...) \
printk(KERN_WARNING DEX_NAME ": " msg "\n" , ## args)
#define PDEBUG(msg, args...) \
printk(KERN_DEBUG DEX_NAME ": " msg "\n" , ## args)
/* Data associated with each device */
struct dex_device {
int i;
/* spinlock -- should be held almost all the time */
spinlock_t lock;
/* tty attached to the device */
struct tty_struct *tty;
/* number of open handles that point to this device */
int open_count;
/* mutex to be held while a command is active */
struct mutex command_mutex;
/* current command */
enum dex_command command;
/* frame number to read/write */
int frame;
/* where to fetch/store data */
char *data;
/* command is completed */
struct completion command_done;
/* return value of command */
int command_return;
/* input/output buffer */
char buf[DEX_BUFSIZE];
/* number of bytes read / to write */
int count_in, count_out;
/* pointer to the next byte to be written */
char *ptr_out;
/* model: PSX or N64 */
enum dex_model model;
/* Firmware version byte */
unsigned char firmware_version;
/* Disk device we have created */
struct gendisk *gd;
/* Dummy request queue -- which we don't use */
struct request_queue *request_queue;
/* Work queue holding our init and pending bio requests */
struct workqueue_struct *wq;
/* Work queue name */
char wq_name[20];
/* Work queue item for initialization */
struct work_struct init_work;
};
/* This is just to remember which values are currently in use */
static DECLARE_BITMAP(dex_devices, DEX_MAX_DEVICES);
static DEFINE_MUTEX(dex_devices_mutex);
/* Find and set a free bit */
static int dex_get_i(void)
{
int i;
mutex_lock(&dex_devices_mutex);
i = find_first_zero_bit(dex_devices, DEX_MAX_DEVICES);
if (i < DEX_MAX_DEVICES)
set_bit(i, dex_devices);
else
i = -1;
mutex_unlock(&dex_devices_mutex);
return i;
}
static void dex_put_i(int i)
{
/* We can't return -ERESTARTSYS, so just block */
mutex_lock(&dex_devices_mutex);
clear_bit(i, dex_devices);
mutex_unlock(&dex_devices_mutex);
}
/*
* Record that we are now using this device. Returns the previous number
* of open handles, or <0 in case of error.
*/
static int dex_get(struct dex_device *dex)
{
unsigned long flags;
int ret = 0;
PDEBUG("> dex_get(%p)", dex);
spin_lock_irqsave(&dex->lock, flags);
if (dex->tty) {
ret = dex->open_count++;
/* Substract one for the tty */
ret--;
} else {
ret = -ENXIO;
}
spin_unlock_irqrestore(&dex->lock, flags);
PDEBUG("< dex_get := %d", ret);
return ret;
}
/*
* Record that we are no longer using this device. If it is no longer used,
* then it will be destroyed. Returns the current number of open handles, or
* <0 if the device was freed.
*/
static void dex_block_teardown(struct dex_device *dex);
static int dex_put(struct dex_device *dex)
{
unsigned long flags;
int tmp;
PDEBUG("> dex_put(%p)", dex);
spin_lock_irqsave(&dex->lock, flags);
tmp = --dex->open_count;
spin_unlock_irqrestore(&dex->lock, flags);
if (tmp == 0) {
dex_block_teardown(dex);
dex_put_i(dex->i);
kfree(dex);
}
/* Substract one for the tty */
tmp--;
PDEBUG("< dex_put := %i", tmp);
return tmp;
}
/* Low-level functions */
/*
* Split a 16-bit frame number into two bytes, for use as argument to
* SEEK/READ/WRITE, and for computing the checksum on READ.
*/
static inline unsigned char lsb(int x)
{
return x;
}
static inline unsigned char msb(int x)
{
return (x >> 8);
}
/*
* PSX frames have 256 bytes; N64 frames, 512
*/
static inline int dex_frame_shift(const struct dex_device *dex)
{
return (dex->model == DEX_MODEL_PSX ? 7 : 8);
}
static inline int dex_frame_size(const struct dex_device *dex)
{
return (1 << dex_frame_shift(dex));
}
/*
* Reverse the bits in a byte, copied from
* <http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits>.
*/
static inline unsigned char reverse_byte(unsigned char b)
{
return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU))
* 0x10101LU >> 16;
}
static inline unsigned char dex_checksum(const unsigned char *ptr, int len)
{
unsigned char res = 0;
int i;
for (i = 0; i < len; i++)
res ^= ptr[i];
return res;
}
/* Add a character to dex->buf[] */
static inline void add2bufc(struct dex_device *dex, char c)
{
dex->buf[dex->count_out++] = c;
}
/* Add a string to dex->buf[] */
static inline void add2bufs(struct dex_device *dex, const char *str, int len)
{
memcpy(dex->buf + dex->count_out, str, len);
dex->count_out += len;
}
/*
* Fills dex->buf[] with the data that will be sent to the device. Returns <0
* in case of error.
*/
static int dex_prepare_cmd(struct dex_device *dex)
{
PDEBUG("> dex_prepare_cmd(%p)", dex);
dex->count_out = 0;
add2bufs(dex, DEX_CMD_PREFIX, sizeof(DEX_CMD_PREFIX)-1);
switch (dex->command) {
case DEX_CMD_READ:
add2bufc(dex, DEX_OPCODE_READ);
add2bufc(dex, lsb(dex->frame));
add2bufc(dex, msb(dex->frame));
break;
case DEX_CMD_SEEK:
if (dex->model == DEX_MODEL_PSX)
return -1;
add2bufc(dex, DEX_OPCODE_SEEK);
add2bufc(dex, lsb(dex->frame));
add2bufc(dex, msb(dex->frame));
break;
case DEX_CMD_WRITE:
add2bufc(dex, DEX_OPCODE_WRITE);
if (dex->model == DEX_MODEL_PSX) {
add2bufc(dex, msb(dex->frame));
add2bufc(dex, lsb(dex->frame));
add2bufc(dex, reverse_byte(msb(dex->frame)));
add2bufc(dex, reverse_byte(lsb(dex->frame)));
}
add2bufs(dex, dex->data, dex_frame_size(dex));
add2bufc(dex, dex_checksum((dex->buf + 4), (dex->count_out - 4)));
break;
case DEX_CMD_INIT:
add2bufc(dex, DEX_OPCODE_INIT);
add2bufs(dex, DEX_INIT_STR, sizeof(DEX_INIT_STR)-1);
break;
case DEX_CMD_MAGIC:
add2bufc(dex, DEX_OPCODE_MAGIC);
break;
case DEX_CMD_ON:
add2bufc(dex, DEX_OPCODE_LIGHT);
add2bufc(dex, 1);
break;
case DEX_CMD_OFF:
add2bufc(dex, DEX_OPCODE_LIGHT);
add2bufc(dex, 0);
break;
case DEX_CMD_STATUS:
add2bufc(dex, DEX_OPCODE_STATUS);
break;
default:
warn("Unknown command: %d", dex->command);
return -1;
}
PDEBUG("< dex_prepare_cmd");
return 0;
}
/*
* Processes what has already been received in dex->buf[]. Returns >0 if the
* response has been processed, 0 if is currently incomplete, and <0 if there
* was an error.
*/
#define mkpair(req, reply) (((req) << 8) | (reply))
static int dex_read_cmd(struct dex_device *dex)
{
int reply = dex->buf[3];
int n_args = dex->count_in - 4;
PDEBUG("> dex_read_cmd(%p) [ reply:%i n_args:%i ]", dex, reply, n_args);
if (dex->count_in < 4)
return(0);
/* There should be a better way to do this... */
if ((dex->command == DEX_CMD_ON) || (dex->command == DEX_CMD_OFF)) {
PDEBUG("faking NOCARD for CMD_LIGHT");
reply = DEX_OPCODE_NOCARD;
}
if (dex->command == DEX_CMD_MAGIC) {
PDEBUG("faking NOCARD for CMD_MAGIC");
reply = DEX_OPCODE_NOCARD;
}
if (reply == DEX_OPCODE_ERROR) {
PDEBUG("got CMD_ERROR");
return -EIO;
}
if (reply == DEX_OPCODE_POUT) {
PDEBUG("got CMD_POUT");
return -EIO;
}
switch (mkpair(dex->command, reply)) {
case mkpair(DEX_CMD_READ, DEX_OPCODE_DATA):
if (n_args < (dex_frame_size(dex) + 1))
return 0;
if ((dex_checksum((dex->buf + 4), (dex_frame_size(dex) + 1))
^ lsb(dex->frame) ^ msb(dex->frame)) != 0) {
return -EIO;
}
memcpy(dex->data, (dex->buf + 4), dex_frame_size(dex));
return 1;
case mkpair(DEX_CMD_SEEK, DEX_OPCODE_SEEK_OK):
case mkpair(DEX_CMD_WRITE, DEX_OPCODE_WOK):
case mkpair(DEX_CMD_WRITE, DEX_OPCODE_WSAME):
return 1;
case mkpair(DEX_CMD_READ, DEX_OPCODE_NOCARD):
case mkpair(DEX_CMD_SEEK, DEX_OPCODE_NOCARD):
case mkpair(DEX_CMD_WRITE, DEX_OPCODE_NOCARD):
return -EIO;
case mkpair(DEX_CMD_INIT, DEX_OPCODE_ID):
if (n_args < 5) return 0;
memcpy(dex->data, (dex->buf + 4), 5);
return 1;
case mkpair(DEX_CMD_MAGIC, DEX_OPCODE_NOCARD):
case mkpair(DEX_CMD_ON, DEX_OPCODE_NOCARD):
case mkpair(DEX_CMD_OFF, DEX_OPCODE_NOCARD):
return 1;
case mkpair(DEX_CMD_STATUS, DEX_OPCODE_NOCARD):
return -ENXIO;
case mkpair(DEX_CMD_STATUS, DEX_OPCODE_CARD):
case mkpair(DEX_CMD_STATUS, DEX_OPCODE_CARD_NEW):
if ((dex->model == DEX_MODEL_PSX) && (n_args < 1))
return 0;
return 1;
default:
PDEBUG("got unknown reply %i from device", reply);
return -EIO;
}
PDEBUG("< dex_read_cmd");
}
#undef mkpair
/*
* Perform one attempt at sending a command, and wait for the reply.
*/
static void dex_tty_write(struct dex_device *dex);
static int dex_attempt_cmd(struct dex_device *dex, unsigned long *flags)
{
int tmp;
PDEBUG("> dex_attempt_cmd(%p, %p)", dex, flags);
if (!dex->tty)
return -EIO;
if (dex_prepare_cmd(dex) < 0)
return -EIO;
dex->ptr_out = dex->buf;
dex->count_in = 0;
/* Default in case of timeout */
dex->command_return = -EIO;
/* The N64 model might not reply to these, but we don't mind */
if (dex->model == DEX_MODEL_N64) {
switch (dex->command) {
case DEX_CMD_MAGIC:
case DEX_CMD_ON:
case DEX_CMD_OFF:
dex->command_return = 0;
/* Silence gcc warning about not checking the other enum values */
default:
;
}
}
init_completion(&dex->command_done);
spin_unlock_irqrestore(&dex->lock, *flags);
dex_tty_write(dex);
/* TODO: Skip this for N64 and DEX_CMD_ON/OFF */
tmp = wait_for_completion_interruptible_timeout(&dex->command_done,
msecs_to_jiffies(DEX_TIMEOUT));
/* Throw -ERESTARTSYS if needed */
if (tmp < 0)
return tmp;
spin_lock_irqsave(&dex->lock, *flags);
PDEBUG("< dex_attempt_cmd := %i", dex->command_return);
return dex->command_return;
}
/* High-level functions */
/*
* Check if we have received a complete response, and process it if this is
* the case.
*/
static void dex_check_reply(struct dex_device *dex)
{
unsigned long flags;
int ret;
PDEBUG("> dex_check_reply(%p)", dex);
spin_lock_irqsave(&dex->lock, flags);
if (dex->command) {
ret = dex_read_cmd(dex);
PDEBUG(" got %i", ret);
if (ret != 0) {
dex->command = DEX_CMD_NONE;
dex->command_return = ret < 0 ? ret : 0;
complete(&dex->command_done);
}
}
spin_unlock_irqrestore(&dex->lock, flags);
PDEBUG("< dex_check_reply");
}
/*
* Send a command to the device and wait until the response has been
* processed. Returns <0 in case of an error.
*
* The meaning of n and *ptr are specific to each command:
*
* - For DEX_CMD_SEEK (N64) and DEX_CMD_READ/WRITE (PSX), n contains the
* frame number.
* - For DEX_CMD_READ/WRITE (both models), the frame data will be read
* from or written to *ptr.
* - For DEX_CMD_INIT, the 5-byte ID reply will be stored in *ptr.
*
* Otherwise, these arguments are not used.
*
* dex_do_cmd() will acquire command_mutex; dex_do_cmd_locked() will not.
*/
static int dex_do_cmd_locked(struct dex_device *dex, int cmd, int n, void *ptr)
{
unsigned long flags;
int ret, i;
PDEBUG("> dex_do_cmd_locked(%p, %d", dex, cmd);
spin_lock_irqsave(&dex->lock, flags);
dex->frame = n;
dex->data = ptr;
for (i = 0; i <= DEX_MAX_RETRY; i++) {
PDEBUG(" Attempt #%i", i);
dex->command = cmd;
ret = dex_attempt_cmd(dex, &flags);
PDEBUG(" Result: %i", ret);
if (ret == 0)
break;
}
dex->command = DEX_CMD_NONE;
spin_unlock_irqrestore(&dex->lock, flags);
PDEBUG("< dex_do_cmd_locked := %i", ret);
return ret;
}
static int dex_do_cmd(struct dex_device *dex, int cmd, int n, void *ptr)
{
int ret;
PDEBUG("> dex_do_cmd(%p, %d", dex, cmd);
if (mutex_lock_interruptible(&dex->command_mutex))
return -ERESTARTSYS;
ret = dex_do_cmd_locked(dex, cmd, n, ptr);
mutex_unlock(&dex->command_mutex);
PDEBUG("< dex_do_cmd := %i", ret);
return ret;
}
/*
* Read/write a number of consecutive frames from/to the device. Returns <0
* in case of an error.
*/
static int dex_transfer(struct dex_device *dex,
unsigned int frame, unsigned int len,
char *buffer, int write)
{
int error = 0;
PDEBUG("> dex_transfer(%p, %u, %u, %p, %i", dex, frame, len,
buffer, write);
for (; len > 0; frame++, len--) {
/* Make sure to keep SEEK/WRITE together */
if (mutex_lock_interruptible(&dex->command_mutex))
return -ERESTARTSYS;
if (write && (dex->model == DEX_MODEL_N64)) {
error = dex_do_cmd_locked(dex,
DEX_CMD_SEEK, frame, NULL);
if (error < 0)
break;
}
error = dex_do_cmd_locked(dex,
(write ? DEX_CMD_WRITE : DEX_CMD_READ),
frame, buffer);
mutex_unlock(&dex->command_mutex);
if (error < 0)
break;
buffer += dex_frame_size(dex);
}
PDEBUG("< dex_transfer := %i", error);
return error;
}
/*
* Called by dex_init_device(), to avoid a bunch of gotos. Not for external
* use.
*/
static int dex_init_device_locked(struct dex_device *dex)
{
char init_data[5];
int ret;
PDEBUG("> dex_init_device_locked(%p)", dex);
ret = dex_do_cmd_locked(dex, DEX_CMD_INIT, 0, init_data);
if (ret < 0)
return ret;
if (init_data[1] == 'P' && init_data[2] == 'S' && init_data[3] == 'X')
dex->model = DEX_MODEL_PSX;
else if (init_data[1] == 'N' && init_data[2] == '6' && init_data[3] == '4')
dex->model = DEX_MODEL_N64;
else
return -EIO;
PDEBUG(" model is %i", dex->model);
dex->firmware_version = init_data[4];
ret = dex_do_cmd_locked(dex, DEX_CMD_MAGIC, 0, NULL);
PDEBUG("< dex_init_device_locked := %i", ret);
return ret;
}
/*
* Initialize the device, bringing it out of its "pouting" stage.
*/
static int dex_init_device(struct dex_device *dex)
{
int ret;
if (mutex_lock_interruptible(&dex->command_mutex))
return -ERESTARTSYS;
ret = dex_init_device_locked(dex);
mutex_unlock(&dex->command_mutex);
/* A regular PSX memory card holds 128 KiB; a N64 card holds 32 KiB */
set_capacity(dex->gd, (dex->model == DEX_MODEL_PSX ? 128 : 32) * 2);
return ret;
}
/*
* Spin up the device. (This currently re-initializes it as well, but this
* may go away in the future.)
*
* Returns -ENXIO if no card is inserted.
*/
static int dex_spin_up(struct dex_device *dex)
{
int ret;
/*
* Re-initialize the device. This is not needed, but it doesn't take
* much time, and it allows people to plug/unplug the device between
* open calls. It also saves us the trouble of remembering if this
* failed the first time. <g>
*/
ret = dex_init_device(dex);
if (ret < 0)
return ret;
ret = dex_do_cmd(dex, DEX_CMD_STATUS, 0, NULL);
if (ret < 0)
return ret;
/* We don't really care if this fails */
dex_do_cmd(dex, DEX_CMD_ON, 0, NULL);
return ret;
}
/*
* Spin down the device. All this currently does is turn off the light.
*/
static void dex_spin_down(struct dex_device *dex)
{
dex_do_cmd(dex, DEX_CMD_OFF, 0, NULL);
}
/* sysfs functions */
/*
* Helper function for dex_show_firmware_version(). Returns the n
* consecutive bits in word, starting with lowest.
*/
static inline unsigned int extract_bits(unsigned int word,
unsigned int lowest, unsigned int n)
{
return (word >> lowest) & ((1 << n) - 1);
}
static ssize_t dex_show_model(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct dex_device *dex = dev_to_disk(dev)->private_data;
if (dex->tty) {
return snprintf(buf, 5, "%s\n",
(dex->model == DEX_MODEL_PSX ? "PSX" : "N64"));
} else {
return -ENODEV;
}
}
static ssize_t dex_show_firmware_version(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct dex_device *dex = dev_to_disk(dev)->private_data;
if (dex->tty) {
/* Version x.yz is encoded as xxyyyyzz */
return snprintf(buf, 7, "%d.%d%d\n",
extract_bits(dex->firmware_version, 6, 2),
extract_bits(dex->firmware_version, 2, 4),
extract_bits(dex->firmware_version, 0, 2));
} else {
return -ENODEV;
}
}
static DEVICE_ATTR(model, S_IRUGO, dex_show_model, NULL);
static DEVICE_ATTR(firmware_version, S_IRUGO, dex_show_firmware_version, NULL);
/* Block device functions */
/*
* We cannot store any private data in a work_struct, so we create a
* container for this purpose.
*/
struct dex_bio_work {
struct dex_device *dex;
struct bio *bio;
struct work_struct work;
};
/*
* Handle a pending block IO operation.
*/
static inline void dex_block_do_bio(struct dex_device *dex, struct bio *bio)
{
sector_t frame;
COMPAT_BIO_VEC_TYPE bvec;
COMPAT_BVEC_ITER_TYPE iter;
int error = 0;
PDEBUG(">> dex_block_do_bio(%p, %p)", dex, bio);
frame = compat_bio_bi_sector(bio) << (9 - dex_frame_shift(dex));
bio_for_each_segment(bvec, bio, iter) {
sector_t len = (compat_bvec(bvec).bv_len >> dex_frame_shift(dex));
error = dex_transfer(dex, frame, len,
kmap(compat_bvec(bvec).bv_page) + compat_bvec(bvec).bv_offset,
bio_data_dir(bio) == WRITE);
if (error < 0)
break;
frame += len;
}
compat_bio_endio(bio, error);
PDEBUG("<< dex_block_do_bio");
}
/*
* Process a block IO work item from the work queue.
*/
static void dex_block_do_bio_work(struct work_struct *work)
{
struct dex_bio_work *bio_work =
container_of(work, struct dex_bio_work, work);
dex_block_do_bio(bio_work->dex, bio_work->bio);
kfree(bio_work);
}
/*
* Called by the kernel when a new block IO operation is created, which we
* add to the work queue.
*
* TODO: This should probably be renamed dex_block_submit_bio() at some point.
*/
static COMPAT_REQUEST_RETTYPE
dex_block_make_request(COMPAT_REQUEST_PARAMS)
{
struct dex_device *dex = compat_request_get_queue()->queuedata;
struct dex_bio_work *bio_work;
PDEBUG("> dex_block_make_request(%p)", bio);
if (!dex) {
/* We are shutting down -- drop everything on the floor */
bio_io_error(bio);
COMPAT_REQUEST_RETURN();
}
if ((bio_work = kmalloc(sizeof(*bio_work), GFP_KERNEL)) == NULL) {
warn("cannot allocate bio_work struct");
bio_io_error(bio);
COMPAT_REQUEST_RETURN();
}
bio_work->dex = dex;
bio_work->bio = bio;
INIT_WORK(&bio_work->work, dex_block_do_bio_work);
queue_work(dex->wq, &bio_work->work);
PDEBUG("< dex_block_make_request");
COMPAT_REQUEST_RETURN();
}
/*
* Mutex to prevent conflict between multiple open()/release(). We may end
* up freeing dex when calling dex_put(), so a global mutex is safer (well,
* easier) than storing it within dex_device. Unfortunately, it does mean
* that calls to distinct devices will block each other, but does anybody
* care?
*/
static DEFINE_MUTEX(open_release_mutex);
/*
* Called when our block device is opened.
*/
static int dex_block_open(COMPAT_BLOCK_OPEN_PARAMS)
{
struct dex_device *dex;
int ret;
PDEBUG("> dex_block_open(...)");
if (mutex_lock_interruptible(&open_release_mutex))
return -ERESTARTSYS;
dex = compat_block_open_disk_ref->private_data;
ret = dex_get(dex);
/* Initialize the device if we are the first to open it */
if (ret == 0) {
/* Make sure that initialization is done */
flush_workqueue(dex->wq);
ret = dex_spin_up(dex);
if (ret < 0)
goto out;
compat_check_disk_change(compat_block_open_checkchg_arg);
}
out:
if (ret < 0)
dex_put(dex);
mutex_unlock(&open_release_mutex);
PDEBUG("< dex_block_open := %d", ret);
return ret;
}
/*
* Called when our block device is closed.
*/
static COMPAT_RELEASE_RETTYPE dex_block_release(COMPAT_BLOCK_RELEASE_PARAMS)
{
struct dex_device *dex;
PDEBUG("> dex_block_release(...)");
if (mutex_lock_interruptible(&open_release_mutex)) {
WARN_ON(1);
COMPAT_RELEASE_RETURN(-ERESTARTSYS);
}
dex = disk->private_data;
/* FIXME: Yuck */
if (dex->tty && dex->open_count == 2)
dex_spin_down(dex);
dex_put(dex);
mutex_unlock(&open_release_mutex);
PDEBUG("< dex_block_release := %d", 0);
COMPAT_RELEASE_RETURN(0);
}
/*
* Called by our own call to check_disk_change() in dex_block_open().
*
* Unfortunately, there's not much for us to report. While the PSX model can
* indeed detect a media change (unlike the N64 model), it will keep reporting
* the media as changed until the next write. That's of no use to us, unless
* we're willing to write to the card just for the sake of it.
*
* It is somewhat unclear what we should do in our case, where we have a
* removable media, but cannot tell if it has been changed. The safest
* option is probably to always signal a media change, which is what we do.
*/
static unsigned int dex_block_check_events(struct gendisk *gd,
unsigned int clearing)
{
return DISK_EVENT_MEDIA_CHANGE;
}
static struct block_device_operations dex_bdops = {
.owner = THIS_MODULE,
.open = dex_block_open,
.release = dex_block_release,
.check_events = dex_block_check_events,
/* Set .submit_bio if applicable (the lack of trailing comma is intentional) */
COMPAT_SET_SUBMIT_BIO(dex_block_make_request)
};