probe.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Parallel port device probing code
  4. *
  5. * Authors: Carsten Gross, [email protected]
  6. * Philip Blundell <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/parport.h>
  10. #include <linux/string.h>
  11. #include <linux/string_helpers.h>
  12. #include <linux/slab.h>
  13. #include <linux/uaccess.h>
  14. static const struct {
  15. const char *token;
  16. const char *descr;
  17. } classes[] = {
  18. { "", "Legacy device" },
  19. { "PRINTER", "Printer" },
  20. { "MODEM", "Modem" },
  21. { "NET", "Network device" },
  22. { "HDC", "Hard disk" },
  23. { "PCMCIA", "PCMCIA" },
  24. { "MEDIA", "Multimedia device" },
  25. { "FDC", "Floppy disk" },
  26. { "PORTS", "Ports" },
  27. { "SCANNER", "Scanner" },
  28. { "DIGICAM", "Digital camera" },
  29. { "", "Unknown device" },
  30. { "", "Unspecified" },
  31. { "SCSIADAPTER", "SCSI adapter" },
  32. { NULL, NULL }
  33. };
  34. static void pretty_print(struct parport *port, int device)
  35. {
  36. struct parport_device_info *info = &port->probe_info[device + 1];
  37. pr_info("%s", port->name);
  38. if (device >= 0)
  39. pr_cont(" (addr %d)", device);
  40. pr_cont(": %s", classes[info->class].descr);
  41. if (info->class)
  42. pr_cont(", %s %s", info->mfr, info->model);
  43. pr_cont("\n");
  44. }
  45. static void parse_data(struct parport *port, int device, char *str)
  46. {
  47. char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
  48. char *p = txt, *q;
  49. int guessed_class = PARPORT_CLASS_UNSPEC;
  50. struct parport_device_info *info = &port->probe_info[device + 1];
  51. if (!txt) {
  52. pr_warn("%s probe: memory squeeze\n", port->name);
  53. return;
  54. }
  55. strcpy(txt, str);
  56. while (p) {
  57. char *sep;
  58. q = strchr(p, ';');
  59. if (q) *q = 0;
  60. sep = strchr(p, ':');
  61. if (sep) {
  62. char *u;
  63. *(sep++) = 0;
  64. /* Get rid of trailing blanks */
  65. u = sep + strlen (sep) - 1;
  66. while (u >= p && *u == ' ')
  67. *u-- = '\0';
  68. string_upper(p, p);
  69. if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
  70. kfree(info->mfr);
  71. info->mfr = kstrdup(sep, GFP_KERNEL);
  72. } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
  73. kfree(info->model);
  74. info->model = kstrdup(sep, GFP_KERNEL);
  75. } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
  76. int i;
  77. kfree(info->class_name);
  78. info->class_name = kstrdup(sep, GFP_KERNEL);
  79. string_upper(sep, sep);
  80. for (i = 0; classes[i].token; i++) {
  81. if (!strcmp(classes[i].token, sep)) {
  82. info->class = i;
  83. goto rock_on;
  84. }
  85. }
  86. pr_warn("%s probe: warning, class '%s' not understood\n",
  87. port->name, sep);
  88. info->class = PARPORT_CLASS_OTHER;
  89. } else if (!strcmp(p, "CMD") ||
  90. !strcmp(p, "COMMAND SET")) {
  91. kfree(info->cmdset);
  92. info->cmdset = kstrdup(sep, GFP_KERNEL);
  93. /* if it speaks printer language, it's
  94. probably a printer */
  95. if (strstr(sep, "PJL") || strstr(sep, "PCL"))
  96. guessed_class = PARPORT_CLASS_PRINTER;
  97. } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
  98. kfree(info->description);
  99. info->description = kstrdup(sep, GFP_KERNEL);
  100. }
  101. }
  102. rock_on:
  103. if (q)
  104. p = q + 1;
  105. else
  106. p = NULL;
  107. }
  108. /* If the device didn't tell us its class, maybe we have managed to
  109. guess one from the things it did say. */
  110. if (info->class == PARPORT_CLASS_UNSPEC)
  111. info->class = guessed_class;
  112. pretty_print (port, device);
  113. kfree(txt);
  114. }
  115. /* Read up to count-1 bytes of device id. Terminate buffer with
  116. * '\0'. Buffer begins with two Device ID length bytes as given by
  117. * device. */
  118. static ssize_t parport_read_device_id (struct parport *port, char *buffer,
  119. size_t count)
  120. {
  121. unsigned char length[2];
  122. unsigned lelen, belen;
  123. size_t idlens[4];
  124. unsigned numidlens;
  125. unsigned current_idlen;
  126. ssize_t retval;
  127. size_t len;
  128. /* First two bytes are MSB,LSB of inclusive length. */
  129. retval = parport_read (port, length, 2);
  130. if (retval < 0)
  131. return retval;
  132. if (retval != 2)
  133. return -EIO;
  134. if (count < 2)
  135. return 0;
  136. memcpy(buffer, length, 2);
  137. len = 2;
  138. /* Some devices wrongly send LE length, and some send it two
  139. * bytes short. Construct a sorted array of lengths to try. */
  140. belen = (length[0] << 8) + length[1];
  141. lelen = (length[1] << 8) + length[0];
  142. idlens[0] = min(belen, lelen);
  143. idlens[1] = idlens[0]+2;
  144. if (belen != lelen) {
  145. int off = 2;
  146. /* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
  147. if (idlens[0] <= 2)
  148. off = 0;
  149. idlens[off] = max(belen, lelen);
  150. idlens[off+1] = idlens[off]+2;
  151. numidlens = off+2;
  152. }
  153. else {
  154. /* Some devices don't truly implement Device ID, but
  155. * just return constant nibble forever. This catches
  156. * also those cases. */
  157. if (idlens[0] == 0 || idlens[0] > 0xFFF) {
  158. printk(KERN_DEBUG "%s: reported broken Device ID length of %#zX bytes\n",
  159. port->name, idlens[0]);
  160. return -EIO;
  161. }
  162. numidlens = 2;
  163. }
  164. /* Try to respect the given ID length despite all the bugs in
  165. * the ID length. Read according to shortest possible ID
  166. * first. */
  167. for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
  168. size_t idlen = idlens[current_idlen];
  169. if (idlen+1 >= count)
  170. break;
  171. retval = parport_read (port, buffer+len, idlen-len);
  172. if (retval < 0)
  173. return retval;
  174. len += retval;
  175. if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
  176. if (belen != len) {
  177. printk(KERN_DEBUG "%s: Device ID was %zd bytes while device told it would be %d bytes\n",
  178. port->name, len, belen);
  179. }
  180. goto done;
  181. }
  182. /* This might end reading the Device ID too
  183. * soon. Hopefully the needed fields were already in
  184. * the first 256 bytes or so that we must have read so
  185. * far. */
  186. if (buffer[len-1] == ';') {
  187. printk(KERN_DEBUG "%s: Device ID reading stopped before device told data not available. Current idlen %u of %u, len bytes %02X %02X\n",
  188. port->name, current_idlen, numidlens,
  189. length[0], length[1]);
  190. goto done;
  191. }
  192. }
  193. if (current_idlen < numidlens) {
  194. /* Buffer not large enough, read to end of buffer. */
  195. size_t idlen, len2;
  196. if (len+1 < count) {
  197. retval = parport_read (port, buffer+len, count-len-1);
  198. if (retval < 0)
  199. return retval;
  200. len += retval;
  201. }
  202. /* Read the whole ID since some devices would not
  203. * otherwise give back the Device ID from beginning
  204. * next time when asked. */
  205. idlen = idlens[current_idlen];
  206. len2 = len;
  207. while(len2 < idlen && retval > 0) {
  208. char tmp[4];
  209. retval = parport_read (port, tmp,
  210. min(sizeof tmp, idlen-len2));
  211. if (retval < 0)
  212. return retval;
  213. len2 += retval;
  214. }
  215. }
  216. /* In addition, there are broken devices out there that don't
  217. even finish off with a semi-colon. We do not need to care
  218. about those at this time. */
  219. done:
  220. buffer[len] = '\0';
  221. return len;
  222. }
  223. /* Get Std 1284 Device ID. */
  224. ssize_t parport_device_id (int devnum, char *buffer, size_t count)
  225. {
  226. ssize_t retval = -ENXIO;
  227. struct pardevice *dev = parport_open(devnum, daisy_dev_name);
  228. if (!dev)
  229. return -ENXIO;
  230. parport_claim_or_block (dev);
  231. /* Negotiate to compatibility mode, and then to device ID
  232. * mode. (This so that we start form beginning of device ID if
  233. * already in device ID mode.) */
  234. parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
  235. retval = parport_negotiate (dev->port,
  236. IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
  237. if (!retval) {
  238. retval = parport_read_device_id (dev->port, buffer, count);
  239. parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
  240. if (retval > 2)
  241. parse_data (dev->port, dev->daisy, buffer+2);
  242. }
  243. parport_release (dev);
  244. parport_close (dev);
  245. return retval;
  246. }