disc_linux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* --------------------------------------------------------------------------
  2. MusicBrainz -- The Internet music metadatabase
  3. Copyright (C) 2013 Johannes Dewender
  4. Copyright (C) 2006 Matthias Friedrich
  5. Copyright (C) 2000 Robert Kaye
  6. Copyright (C) 1999 Marc E E van Woerkom
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. --------------------------------------------------------------------------- */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/ioctl.h>
  29. #include <linux/cdrom.h>
  30. #include <scsi/sg.h>
  31. #include "discid/discid.h"
  32. #include "discid/discid_private.h"
  33. #include "unix.h"
  34. /* timeout better shouldn't happen for scsi commands -> device is reset */
  35. #define DEFAULT_TIMEOUT 30000 /* in ms */
  36. #ifndef SG_MAX_SENSE
  37. #define SG_MAX_SENSE 16
  38. #endif
  39. #define MB_DEFAULT_DEVICE "/dev/cdrom"
  40. #define MAX_DEV_LEN 50
  41. #if (defined(__GNUC__) && (__GNUC__ >= 4)) || defined(__clang__)
  42. # define THREAD_LOCAL __thread
  43. #else
  44. # define THREAD_LOCAL
  45. #endif
  46. static THREAD_LOCAL char default_device[MAX_DEV_LEN] = "";
  47. static int get_device(int number, char *device, int device_len) {
  48. FILE *proc_file;
  49. char *current_device;
  50. char *lineptr = NULL;
  51. char *saveptr = NULL;
  52. size_t bufflen;
  53. int i, count, counter;
  54. int return_value = 0;
  55. proc_file = fopen("/proc/sys/dev/cdrom/info", "r");
  56. if (proc_file != NULL) {
  57. /* skip to line containing device names */
  58. do {
  59. if (getline(&lineptr, &bufflen, proc_file) < 0) {
  60. return 0;
  61. }
  62. } while (strstr(lineptr, "drive name:") == NULL);
  63. /* count number of devices = number of tabs - 1*/
  64. count = -1;
  65. for (i = 0; i < strlen(lineptr); i++) {
  66. if (lineptr[i] == '\t') count++;
  67. }
  68. /* go through devices, they are in reverse order */
  69. current_device = strtok_r(lineptr, "\t", &saveptr);
  70. /* skip column title */
  71. current_device = strtok_r(NULL, "\t", &saveptr);
  72. counter = count;
  73. while (current_device != NULL && counter >= number) {
  74. if (counter == number) {
  75. snprintf(device, device_len,
  76. "/dev/%s", current_device);
  77. return_value = 1;
  78. }
  79. /* go to next in list */
  80. current_device = strtok_r(NULL, "\t", &saveptr);
  81. counter--;
  82. }
  83. /* trim the trailing \n for the last entry = first device */
  84. if (return_value && device[strlen(device)-1] == '\n') {
  85. device[strlen(device)-1] = '\0';
  86. }
  87. free(lineptr);
  88. fclose(proc_file);
  89. }
  90. return return_value;
  91. }
  92. int mb_disc_unix_read_toc_header(int fd, mb_disc_toc *toc) {
  93. struct cdrom_tochdr th;
  94. int ret = ioctl(fd, CDROMREADTOCHDR, &th);
  95. if ( ret < 0 )
  96. return 0; /* error */
  97. toc->first_track_num = th.cdth_trk0;
  98. toc->last_track_num = th.cdth_trk1;
  99. return 1;
  100. }
  101. int mb_disc_unix_read_toc_entry(int fd, int track_num, mb_disc_toc_track *track) {
  102. struct cdrom_tocentry te;
  103. int ret;
  104. te.cdte_track = track_num;
  105. te.cdte_format = CDROM_LBA;
  106. ret = ioctl(fd, CDROMREADTOCENTRY, &te);
  107. assert( te.cdte_format == CDROM_LBA );
  108. if ( ret < 0 )
  109. return 0; /* error */
  110. track->address = te.cdte_addr.lba;
  111. track->control = te.cdte_ctrl;
  112. return 1;
  113. }
  114. char *mb_disc_get_default_device_unportable(void) {
  115. /* prefer the default device symlink to the internal names */
  116. if (mb_disc_unix_exists(MB_DEFAULT_DEVICE)) {
  117. return MB_DEFAULT_DEVICE;
  118. } else {
  119. if (get_device(1, default_device, MAX_DEV_LEN)) {
  120. return default_device;
  121. } else {
  122. return MB_DEFAULT_DEVICE;
  123. }
  124. }
  125. }
  126. void mb_disc_unix_read_mcn(int fd, mb_disc_private *disc) {
  127. struct cdrom_mcn mcn;
  128. memset(&mcn, 0, sizeof mcn);
  129. if(ioctl(fd, CDROM_GET_MCN, &mcn) == -1) {
  130. fprintf(stderr, "Warning: Unable to read the disc's media catalog number.\n");
  131. } else {
  132. strncpy( disc->mcn,
  133. (const char *)mcn.medium_catalog_number,
  134. MCN_STR_LENGTH );
  135. }
  136. }
  137. /* Send a scsi command and receive data. */
  138. static int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
  139. unsigned char *data, int data_len) {
  140. unsigned char sense_buffer[SG_MAX_SENSE]; /* for "error situations" */
  141. sg_io_hdr_t io_hdr;
  142. memset(&io_hdr, 0, sizeof io_hdr);
  143. assert(cmd_len <= 16);
  144. io_hdr.interface_id = 'S'; /* must always be 'S' (SCSI generic) */
  145. io_hdr.cmd_len = cmd_len;
  146. io_hdr.cmdp = cmd;
  147. io_hdr.timeout = DEFAULT_TIMEOUT; /* timeout in ms */
  148. io_hdr.sbp = sense_buffer;/* only used when status is CHECK_CONDITION */
  149. io_hdr.mx_sb_len = sizeof sense_buffer;
  150. io_hdr.flags = SG_FLAG_DIRECT_IO;
  151. io_hdr.dxferp = (void*)data;
  152. io_hdr.dxfer_len = data_len;
  153. io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  154. if (ioctl(fd, SG_IO, &io_hdr) != 0) {
  155. return errno;
  156. } else {
  157. return io_hdr.status; /* 0 = success */
  158. }
  159. }
  160. void mb_disc_unix_read_isrc(int fd, mb_disc_private *disc, int track_num) {
  161. int i;
  162. unsigned char cmd[10];
  163. unsigned char data[24];
  164. char buffer[ISRC_STR_LENGTH+1];
  165. memset(cmd, 0, sizeof cmd);
  166. memset(data, 0, sizeof data);
  167. memset(buffer, 0, sizeof buffer);
  168. /* data read from the last appropriate sector encountered
  169. * by a current or previous media access operation.
  170. * The Logical Unit accesses the media when there is/was no access.
  171. * TODO: force access at a specific block? -> no duplicate ISRCs?
  172. */
  173. cmd[0] = 0x42; /* READ SUB-CHANNEL */
  174. /* cmd[1] reserved / MSF bit (unused) */
  175. cmd[2] = 1 << 6; /* 6th bit set (SUBQ) -> get sub-channel data */
  176. cmd[3] = 0x03; /* get ISRC (ADR 3, Q sub-channel Mode-3) */
  177. /* 4+5 reserved */
  178. cmd[6] = track_num;
  179. /* cmd[7] = upper byte of the transfer length */
  180. cmd[8] = sizeof data; /* transfer length in bytes (4 header, 20 data)*/
  181. /* cmd[9] = control byte */
  182. if (scsi_cmd(fd, cmd, sizeof cmd, data, sizeof data) != 0) {
  183. fprintf(stderr, "Warning: Cannot get ISRC code for track %d\n",
  184. track_num);
  185. return;
  186. }
  187. /* data[1:4] = sub-q channel data header (audio status, data length) */
  188. if (data[8] & (1 << 7)) { /* TCVAL is set -> ISRCs valid */
  189. for (i = 0; i < ISRC_STR_LENGTH; i++) {
  190. buffer[i] = data[9 + i];
  191. }
  192. buffer[ISRC_STR_LENGTH] = 0;
  193. strncpy(disc->isrc[track_num], buffer, ISRC_STR_LENGTH);
  194. }
  195. /* data[21:23] = zero, AFRAME, reserved */
  196. }
  197. int mb_disc_has_feature_unportable(enum discid_feature feature) {
  198. switch(feature) {
  199. case DISCID_FEATURE_READ:
  200. case DISCID_FEATURE_MCN:
  201. case DISCID_FEATURE_ISRC:
  202. return 1;
  203. default:
  204. return 0;
  205. }
  206. }
  207. int mb_disc_read_unportable(mb_disc_private *disc, const char *device,
  208. unsigned int features) {
  209. char device_name[MAX_DEV_LEN] = "";
  210. int device_number;
  211. device_number = (int) strtol(device, NULL, 10);
  212. if (device_number > 0) {
  213. if(!get_device(device_number, device_name, MAX_DEV_LEN)) {
  214. snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
  215. "cannot find cd device with the number '%d'",
  216. device_number);
  217. return 0;
  218. } else {
  219. return mb_disc_unix_read(disc, device_name, features);
  220. }
  221. } else {
  222. return mb_disc_unix_read(disc, device, features);
  223. }
  224. }
  225. /* EOF */