usbtmc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /**
  2. * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
  3. *
  4. * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
  5. * Copyright (C) 2008 Novell, Inc.
  6. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * The GNU General Public License is available at
  19. * http://www.gnu.org/copyleft/gpl.html.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/kref.h>
  27. #include <linux/slab.h>
  28. #include <linux/mutex.h>
  29. #include <linux/usb.h>
  30. #include <linux/usb/tmc.h>
  31. #define USBTMC_MINOR_BASE 176
  32. /*
  33. * Size of driver internal IO buffer. Must be multiple of 4 and at least as
  34. * large as wMaxPacketSize (which is usually 512 bytes).
  35. */
  36. #define USBTMC_SIZE_IOBUFFER 2048
  37. /* Default USB timeout (in milliseconds) */
  38. #define USBTMC_TIMEOUT 5000
  39. /*
  40. * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
  41. * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
  42. * packet is never read.
  43. */
  44. #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
  45. static const struct usb_device_id usbtmc_devices[] = {
  46. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
  47. { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
  48. { 0, } /* terminating entry */
  49. };
  50. MODULE_DEVICE_TABLE(usb, usbtmc_devices);
  51. /*
  52. * This structure is the capabilities for the device
  53. * See section 4.2.1.8 of the USBTMC specification,
  54. * and section 4.2.2 of the USBTMC usb488 subclass
  55. * specification for details.
  56. */
  57. struct usbtmc_dev_capabilities {
  58. __u8 interface_capabilities;
  59. __u8 device_capabilities;
  60. __u8 usb488_interface_capabilities;
  61. __u8 usb488_device_capabilities;
  62. };
  63. /* This structure holds private data for each USBTMC device. One copy is
  64. * allocated for each USBTMC device in the driver's probe function.
  65. */
  66. struct usbtmc_device_data {
  67. const struct usb_device_id *id;
  68. struct usb_device *usb_dev;
  69. struct usb_interface *intf;
  70. unsigned int bulk_in;
  71. unsigned int bulk_out;
  72. u8 bTag;
  73. u8 bTag_last_write; /* needed for abort */
  74. u8 bTag_last_read; /* needed for abort */
  75. /* attributes from the USB TMC spec for this device */
  76. u8 TermChar;
  77. bool TermCharEnabled;
  78. bool auto_abort;
  79. bool zombie; /* fd of disconnected device */
  80. struct usbtmc_dev_capabilities capabilities;
  81. struct kref kref;
  82. struct mutex io_mutex; /* only one i/o function running at a time */
  83. };
  84. #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
  85. /* Forward declarations */
  86. static struct usb_driver usbtmc_driver;
  87. static void usbtmc_delete(struct kref *kref)
  88. {
  89. struct usbtmc_device_data *data = to_usbtmc_data(kref);
  90. usb_put_dev(data->usb_dev);
  91. kfree(data);
  92. }
  93. static int usbtmc_open(struct inode *inode, struct file *filp)
  94. {
  95. struct usb_interface *intf;
  96. struct usbtmc_device_data *data;
  97. int retval = 0;
  98. intf = usb_find_interface(&usbtmc_driver, iminor(inode));
  99. if (!intf) {
  100. printk(KERN_ERR KBUILD_MODNAME
  101. ": can not find device for minor %d", iminor(inode));
  102. retval = -ENODEV;
  103. goto exit;
  104. }
  105. data = usb_get_intfdata(intf);
  106. kref_get(&data->kref);
  107. /* Store pointer in file structure's private data field */
  108. filp->private_data = data;
  109. exit:
  110. return retval;
  111. }
  112. static int usbtmc_release(struct inode *inode, struct file *file)
  113. {
  114. struct usbtmc_device_data *data = file->private_data;
  115. kref_put(&data->kref, usbtmc_delete);
  116. return 0;
  117. }
  118. static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
  119. {
  120. u8 *buffer;
  121. struct device *dev;
  122. int rv;
  123. int n;
  124. int actual;
  125. struct usb_host_interface *current_setting;
  126. int max_size;
  127. dev = &data->intf->dev;
  128. buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
  129. if (!buffer)
  130. return -ENOMEM;
  131. rv = usb_control_msg(data->usb_dev,
  132. usb_rcvctrlpipe(data->usb_dev, 0),
  133. USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
  134. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  135. data->bTag_last_read, data->bulk_in,
  136. buffer, 2, USBTMC_TIMEOUT);
  137. if (rv < 0) {
  138. dev_err(dev, "usb_control_msg returned %d\n", rv);
  139. goto exit;
  140. }
  141. dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
  142. if (buffer[0] == USBTMC_STATUS_FAILED) {
  143. rv = 0;
  144. goto exit;
  145. }
  146. if (buffer[0] != USBTMC_STATUS_SUCCESS) {
  147. dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
  148. buffer[0]);
  149. rv = -EPERM;
  150. goto exit;
  151. }
  152. max_size = 0;
  153. current_setting = data->intf->cur_altsetting;
  154. for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
  155. if (current_setting->endpoint[n].desc.bEndpointAddress ==
  156. data->bulk_in)
  157. max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
  158. if (max_size == 0) {
  159. dev_err(dev, "Couldn't get wMaxPacketSize\n");
  160. rv = -EPERM;
  161. goto exit;
  162. }
  163. dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
  164. n = 0;
  165. do {
  166. dev_dbg(dev, "Reading from bulk in EP\n");
  167. rv = usb_bulk_msg(data->usb_dev,
  168. usb_rcvbulkpipe(data->usb_dev,
  169. data->bulk_in),
  170. buffer, USBTMC_SIZE_IOBUFFER,
  171. &actual, USBTMC_TIMEOUT);
  172. n++;
  173. if (rv < 0) {
  174. dev_err(dev, "usb_bulk_msg returned %d\n", rv);
  175. goto exit;
  176. }
  177. } while ((actual == max_size) &&
  178. (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
  179. if (actual == max_size) {
  180. dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
  181. USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
  182. rv = -EPERM;
  183. goto exit;
  184. }
  185. n = 0;
  186. usbtmc_abort_bulk_in_status:
  187. rv = usb_control_msg(data->usb_dev,
  188. usb_rcvctrlpipe(data->usb_dev, 0),
  189. USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
  190. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  191. 0, data->bulk_in, buffer, 0x08,
  192. USBTMC_TIMEOUT);
  193. if (rv < 0) {
  194. dev_err(dev, "usb_control_msg returned %d\n", rv);
  195. goto exit;
  196. }
  197. dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
  198. if (buffer[0] == USBTMC_STATUS_SUCCESS) {
  199. rv = 0;
  200. goto exit;
  201. }
  202. if (buffer[0] != USBTMC_STATUS_PENDING) {
  203. dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
  204. rv = -EPERM;
  205. goto exit;
  206. }
  207. if (buffer[1] == 1)
  208. do {
  209. dev_dbg(dev, "Reading from bulk in EP\n");
  210. rv = usb_bulk_msg(data->usb_dev,
  211. usb_rcvbulkpipe(data->usb_dev,
  212. data->bulk_in),
  213. buffer, USBTMC_SIZE_IOBUFFER,
  214. &actual, USBTMC_TIMEOUT);
  215. n++;
  216. if (rv < 0) {
  217. dev_err(dev, "usb_bulk_msg returned %d\n", rv);
  218. goto exit;
  219. }
  220. } while ((actual == max_size) &&
  221. (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
  222. if (actual == max_size) {
  223. dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
  224. USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
  225. rv = -EPERM;
  226. goto exit;
  227. }
  228. goto usbtmc_abort_bulk_in_status;
  229. exit:
  230. kfree(buffer);
  231. return rv;
  232. }
  233. static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
  234. {
  235. struct device *dev;
  236. u8 *buffer;
  237. int rv;
  238. int n;
  239. dev = &data->intf->dev;
  240. buffer = kmalloc(8, GFP_KERNEL);
  241. if (!buffer)
  242. return -ENOMEM;
  243. rv = usb_control_msg(data->usb_dev,
  244. usb_rcvctrlpipe(data->usb_dev, 0),
  245. USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
  246. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  247. data->bTag_last_write, data->bulk_out,
  248. buffer, 2, USBTMC_TIMEOUT);
  249. if (rv < 0) {
  250. dev_err(dev, "usb_control_msg returned %d\n", rv);
  251. goto exit;
  252. }
  253. dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
  254. if (buffer[0] != USBTMC_STATUS_SUCCESS) {
  255. dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
  256. buffer[0]);
  257. rv = -EPERM;
  258. goto exit;
  259. }
  260. n = 0;
  261. usbtmc_abort_bulk_out_check_status:
  262. rv = usb_control_msg(data->usb_dev,
  263. usb_rcvctrlpipe(data->usb_dev, 0),
  264. USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
  265. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  266. 0, data->bulk_out, buffer, 0x08,
  267. USBTMC_TIMEOUT);
  268. n++;
  269. if (rv < 0) {
  270. dev_err(dev, "usb_control_msg returned %d\n", rv);
  271. goto exit;
  272. }
  273. dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
  274. if (buffer[0] == USBTMC_STATUS_SUCCESS)
  275. goto usbtmc_abort_bulk_out_clear_halt;
  276. if ((buffer[0] == USBTMC_STATUS_PENDING) &&
  277. (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
  278. goto usbtmc_abort_bulk_out_check_status;
  279. rv = -EPERM;
  280. goto exit;
  281. usbtmc_abort_bulk_out_clear_halt:
  282. rv = usb_clear_halt(data->usb_dev,
  283. usb_sndbulkpipe(data->usb_dev, data->bulk_out));
  284. if (rv < 0) {
  285. dev_err(dev, "usb_control_msg returned %d\n", rv);
  286. goto exit;
  287. }
  288. rv = 0;
  289. exit:
  290. kfree(buffer);
  291. return rv;
  292. }
  293. static ssize_t usbtmc_read(struct file *filp, char __user *buf,
  294. size_t count, loff_t *f_pos)
  295. {
  296. struct usbtmc_device_data *data;
  297. struct device *dev;
  298. u32 n_characters;
  299. u8 *buffer;
  300. int actual;
  301. size_t done;
  302. size_t remaining;
  303. int retval;
  304. size_t this_part;
  305. /* Get pointer to private data structure */
  306. data = filp->private_data;
  307. dev = &data->intf->dev;
  308. buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
  309. if (!buffer)
  310. return -ENOMEM;
  311. mutex_lock(&data->io_mutex);
  312. if (data->zombie) {
  313. retval = -ENODEV;
  314. goto exit;
  315. }
  316. remaining = count;
  317. done = 0;
  318. while (remaining > 0) {
  319. if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
  320. this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
  321. else
  322. this_part = remaining;
  323. /* Setup IO buffer for DEV_DEP_MSG_IN message
  324. * Refer to class specs for details
  325. */
  326. buffer[0] = 2;
  327. buffer[1] = data->bTag;
  328. buffer[2] = ~(data->bTag);
  329. buffer[3] = 0; /* Reserved */
  330. buffer[4] = (this_part) & 255;
  331. buffer[5] = ((this_part) >> 8) & 255;
  332. buffer[6] = ((this_part) >> 16) & 255;
  333. buffer[7] = ((this_part) >> 24) & 255;
  334. buffer[8] = data->TermCharEnabled * 2;
  335. /* Use term character? */
  336. buffer[9] = data->TermChar;
  337. buffer[10] = 0; /* Reserved */
  338. buffer[11] = 0; /* Reserved */
  339. /* Send bulk URB */
  340. retval = usb_bulk_msg(data->usb_dev,
  341. usb_sndbulkpipe(data->usb_dev,
  342. data->bulk_out),
  343. buffer, 12, &actual, USBTMC_TIMEOUT);
  344. /* Store bTag (in case we need to abort) */
  345. data->bTag_last_write = data->bTag;
  346. /* Increment bTag -- and increment again if zero */
  347. data->bTag++;
  348. if (!data->bTag)
  349. (data->bTag)++;
  350. if (retval < 0) {
  351. dev_err(dev, "usb_bulk_msg returned %d\n", retval);
  352. if (data->auto_abort)
  353. usbtmc_ioctl_abort_bulk_out(data);
  354. goto exit;
  355. }
  356. /* Send bulk URB */
  357. retval = usb_bulk_msg(data->usb_dev,
  358. usb_rcvbulkpipe(data->usb_dev,
  359. data->bulk_in),
  360. buffer, USBTMC_SIZE_IOBUFFER, &actual,
  361. USBTMC_TIMEOUT);
  362. /* Store bTag (in case we need to abort) */
  363. data->bTag_last_read = data->bTag;
  364. if (retval < 0) {
  365. dev_err(dev, "Unable to read data, error %d\n", retval);
  366. if (data->auto_abort)
  367. usbtmc_ioctl_abort_bulk_in(data);
  368. goto exit;
  369. }
  370. /* How many characters did the instrument send? */
  371. n_characters = buffer[4] +
  372. (buffer[5] << 8) +
  373. (buffer[6] << 16) +
  374. (buffer[7] << 24);
  375. /* Ensure the instrument doesn't lie about it */
  376. if(n_characters > actual - 12) {
  377. dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12);
  378. n_characters = actual - 12;
  379. }
  380. /* Ensure the instrument doesn't send more back than requested */
  381. if(n_characters > this_part) {
  382. dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
  383. n_characters = this_part;
  384. }
  385. /* Bound amount of data received by amount of data requested */
  386. if (n_characters > this_part)
  387. n_characters = this_part;
  388. /* Copy buffer to user space */
  389. if (copy_to_user(buf + done, &buffer[12], n_characters)) {
  390. /* There must have been an addressing problem */
  391. retval = -EFAULT;
  392. goto exit;
  393. }
  394. done += n_characters;
  395. /* Terminate if end-of-message bit received from device */
  396. if ((buffer[8] & 0x01) && (actual >= n_characters + 12))
  397. remaining = 0;
  398. else
  399. remaining -= n_characters;
  400. }
  401. /* Update file position value */
  402. *f_pos = *f_pos + done;
  403. retval = done;
  404. exit:
  405. mutex_unlock(&data->io_mutex);
  406. kfree(buffer);
  407. return retval;
  408. }
  409. static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
  410. size_t count, loff_t *f_pos)
  411. {
  412. struct usbtmc_device_data *data;
  413. u8 *buffer;
  414. int retval;
  415. int actual;
  416. unsigned long int n_bytes;
  417. int remaining;
  418. int done;
  419. int this_part;
  420. data = filp->private_data;
  421. buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
  422. if (!buffer)
  423. return -ENOMEM;
  424. mutex_lock(&data->io_mutex);
  425. if (data->zombie) {
  426. retval = -ENODEV;
  427. goto exit;
  428. }
  429. remaining = count;
  430. done = 0;
  431. while (remaining > 0) {
  432. if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
  433. this_part = USBTMC_SIZE_IOBUFFER - 12;
  434. buffer[8] = 0;
  435. } else {
  436. this_part = remaining;
  437. buffer[8] = 1;
  438. }
  439. /* Setup IO buffer for DEV_DEP_MSG_OUT message */
  440. buffer[0] = 1;
  441. buffer[1] = data->bTag;
  442. buffer[2] = ~(data->bTag);
  443. buffer[3] = 0; /* Reserved */
  444. buffer[4] = this_part & 255;
  445. buffer[5] = (this_part >> 8) & 255;
  446. buffer[6] = (this_part >> 16) & 255;
  447. buffer[7] = (this_part >> 24) & 255;
  448. /* buffer[8] is set above... */
  449. buffer[9] = 0; /* Reserved */
  450. buffer[10] = 0; /* Reserved */
  451. buffer[11] = 0; /* Reserved */
  452. if (copy_from_user(&buffer[12], buf + done, this_part)) {
  453. retval = -EFAULT;
  454. goto exit;
  455. }
  456. n_bytes = roundup(12 + this_part, 4);
  457. memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
  458. do {
  459. retval = usb_bulk_msg(data->usb_dev,
  460. usb_sndbulkpipe(data->usb_dev,
  461. data->bulk_out),
  462. buffer, n_bytes,
  463. &actual, USBTMC_TIMEOUT);
  464. if (retval != 0)
  465. break;
  466. n_bytes -= actual;
  467. } while (n_bytes);
  468. data->bTag_last_write = data->bTag;
  469. data->bTag++;
  470. if (!data->bTag)
  471. data->bTag++;
  472. if (retval < 0) {
  473. dev_err(&data->intf->dev,
  474. "Unable to send data, error %d\n", retval);
  475. if (data->auto_abort)
  476. usbtmc_ioctl_abort_bulk_out(data);
  477. goto exit;
  478. }
  479. remaining -= this_part;
  480. done += this_part;
  481. }
  482. retval = count;
  483. exit:
  484. mutex_unlock(&data->io_mutex);
  485. kfree(buffer);
  486. return retval;
  487. }
  488. static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
  489. {
  490. struct usb_host_interface *current_setting;
  491. struct usb_endpoint_descriptor *desc;
  492. struct device *dev;
  493. u8 *buffer;
  494. int rv;
  495. int n;
  496. int actual;
  497. int max_size;
  498. dev = &data->intf->dev;
  499. dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
  500. buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
  501. if (!buffer)
  502. return -ENOMEM;
  503. rv = usb_control_msg(data->usb_dev,
  504. usb_rcvctrlpipe(data->usb_dev, 0),
  505. USBTMC_REQUEST_INITIATE_CLEAR,
  506. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  507. 0, 0, buffer, 1, USBTMC_TIMEOUT);
  508. if (rv < 0) {
  509. dev_err(dev, "usb_control_msg returned %d\n", rv);
  510. goto exit;
  511. }
  512. dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
  513. if (buffer[0] != USBTMC_STATUS_SUCCESS) {
  514. dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
  515. rv = -EPERM;
  516. goto exit;
  517. }
  518. max_size = 0;
  519. current_setting = data->intf->cur_altsetting;
  520. for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
  521. desc = &current_setting->endpoint[n].desc;
  522. if (desc->bEndpointAddress == data->bulk_in)
  523. max_size = usb_endpoint_maxp(desc);
  524. }
  525. if (max_size == 0) {
  526. dev_err(dev, "Couldn't get wMaxPacketSize\n");
  527. rv = -EPERM;
  528. goto exit;
  529. }
  530. dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
  531. n = 0;
  532. usbtmc_clear_check_status:
  533. dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
  534. rv = usb_control_msg(data->usb_dev,
  535. usb_rcvctrlpipe(data->usb_dev, 0),
  536. USBTMC_REQUEST_CHECK_CLEAR_STATUS,
  537. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  538. 0, 0, buffer, 2, USBTMC_TIMEOUT);
  539. if (rv < 0) {
  540. dev_err(dev, "usb_control_msg returned %d\n", rv);
  541. goto exit;
  542. }
  543. dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
  544. if (buffer[0] == USBTMC_STATUS_SUCCESS)
  545. goto usbtmc_clear_bulk_out_halt;
  546. if (buffer[0] != USBTMC_STATUS_PENDING) {
  547. dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
  548. rv = -EPERM;
  549. goto exit;
  550. }
  551. if (buffer[1] == 1)
  552. do {
  553. dev_dbg(dev, "Reading from bulk in EP\n");
  554. rv = usb_bulk_msg(data->usb_dev,
  555. usb_rcvbulkpipe(data->usb_dev,
  556. data->bulk_in),
  557. buffer, USBTMC_SIZE_IOBUFFER,
  558. &actual, USBTMC_TIMEOUT);
  559. n++;
  560. if (rv < 0) {
  561. dev_err(dev, "usb_control_msg returned %d\n",
  562. rv);
  563. goto exit;
  564. }
  565. } while ((actual == max_size) &&
  566. (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
  567. if (actual == max_size) {
  568. dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
  569. USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
  570. rv = -EPERM;
  571. goto exit;
  572. }
  573. goto usbtmc_clear_check_status;
  574. usbtmc_clear_bulk_out_halt:
  575. rv = usb_clear_halt(data->usb_dev,
  576. usb_sndbulkpipe(data->usb_dev, data->bulk_out));
  577. if (rv < 0) {
  578. dev_err(dev, "usb_control_msg returned %d\n", rv);
  579. goto exit;
  580. }
  581. rv = 0;
  582. exit:
  583. kfree(buffer);
  584. return rv;
  585. }
  586. static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
  587. {
  588. u8 *buffer;
  589. int rv;
  590. buffer = kmalloc(2, GFP_KERNEL);
  591. if (!buffer)
  592. return -ENOMEM;
  593. rv = usb_clear_halt(data->usb_dev,
  594. usb_sndbulkpipe(data->usb_dev, data->bulk_out));
  595. if (rv < 0) {
  596. dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
  597. rv);
  598. goto exit;
  599. }
  600. rv = 0;
  601. exit:
  602. kfree(buffer);
  603. return rv;
  604. }
  605. static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
  606. {
  607. u8 *buffer;
  608. int rv;
  609. buffer = kmalloc(2, GFP_KERNEL);
  610. if (!buffer)
  611. return -ENOMEM;
  612. rv = usb_clear_halt(data->usb_dev,
  613. usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
  614. if (rv < 0) {
  615. dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
  616. rv);
  617. goto exit;
  618. }
  619. rv = 0;
  620. exit:
  621. kfree(buffer);
  622. return rv;
  623. }
  624. static int get_capabilities(struct usbtmc_device_data *data)
  625. {
  626. struct device *dev = &data->usb_dev->dev;
  627. char *buffer;
  628. int rv = 0;
  629. buffer = kmalloc(0x18, GFP_KERNEL);
  630. if (!buffer)
  631. return -ENOMEM;
  632. rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
  633. USBTMC_REQUEST_GET_CAPABILITIES,
  634. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  635. 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
  636. if (rv < 0) {
  637. dev_err(dev, "usb_control_msg returned %d\n", rv);
  638. goto err_out;
  639. }
  640. dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
  641. if (buffer[0] != USBTMC_STATUS_SUCCESS) {
  642. dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
  643. rv = -EPERM;
  644. goto err_out;
  645. }
  646. dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
  647. dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
  648. dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
  649. dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
  650. data->capabilities.interface_capabilities = buffer[4];
  651. data->capabilities.device_capabilities = buffer[5];
  652. data->capabilities.usb488_interface_capabilities = buffer[14];
  653. data->capabilities.usb488_device_capabilities = buffer[15];
  654. rv = 0;
  655. err_out:
  656. kfree(buffer);
  657. return rv;
  658. }
  659. #define capability_attribute(name) \
  660. static ssize_t show_##name(struct device *dev, \
  661. struct device_attribute *attr, char *buf) \
  662. { \
  663. struct usb_interface *intf = to_usb_interface(dev); \
  664. struct usbtmc_device_data *data = usb_get_intfdata(intf); \
  665. \
  666. return sprintf(buf, "%d\n", data->capabilities.name); \
  667. } \
  668. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  669. capability_attribute(interface_capabilities);
  670. capability_attribute(device_capabilities);
  671. capability_attribute(usb488_interface_capabilities);
  672. capability_attribute(usb488_device_capabilities);
  673. static struct attribute *capability_attrs[] = {
  674. &dev_attr_interface_capabilities.attr,
  675. &dev_attr_device_capabilities.attr,
  676. &dev_attr_usb488_interface_capabilities.attr,
  677. &dev_attr_usb488_device_capabilities.attr,
  678. NULL,
  679. };
  680. static struct attribute_group capability_attr_grp = {
  681. .attrs = capability_attrs,
  682. };
  683. static ssize_t show_TermChar(struct device *dev,
  684. struct device_attribute *attr, char *buf)
  685. {
  686. struct usb_interface *intf = to_usb_interface(dev);
  687. struct usbtmc_device_data *data = usb_get_intfdata(intf);
  688. return sprintf(buf, "%c\n", data->TermChar);
  689. }
  690. static ssize_t store_TermChar(struct device *dev,
  691. struct device_attribute *attr,
  692. const char *buf, size_t count)
  693. {
  694. struct usb_interface *intf = to_usb_interface(dev);
  695. struct usbtmc_device_data *data = usb_get_intfdata(intf);
  696. if (count < 1)
  697. return -EINVAL;
  698. data->TermChar = buf[0];
  699. return count;
  700. }
  701. static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
  702. #define data_attribute(name) \
  703. static ssize_t show_##name(struct device *dev, \
  704. struct device_attribute *attr, char *buf) \
  705. { \
  706. struct usb_interface *intf = to_usb_interface(dev); \
  707. struct usbtmc_device_data *data = usb_get_intfdata(intf); \
  708. \
  709. return sprintf(buf, "%d\n", data->name); \
  710. } \
  711. static ssize_t store_##name(struct device *dev, \
  712. struct device_attribute *attr, \
  713. const char *buf, size_t count) \
  714. { \
  715. struct usb_interface *intf = to_usb_interface(dev); \
  716. struct usbtmc_device_data *data = usb_get_intfdata(intf); \
  717. ssize_t result; \
  718. unsigned val; \
  719. \
  720. result = sscanf(buf, "%u\n", &val); \
  721. if (result != 1) \
  722. result = -EINVAL; \
  723. data->name = val; \
  724. if (result < 0) \
  725. return result; \
  726. else \
  727. return count; \
  728. } \
  729. static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
  730. data_attribute(TermCharEnabled);
  731. data_attribute(auto_abort);
  732. static struct attribute *data_attrs[] = {
  733. &dev_attr_TermChar.attr,
  734. &dev_attr_TermCharEnabled.attr,
  735. &dev_attr_auto_abort.attr,
  736. NULL,
  737. };
  738. static struct attribute_group data_attr_grp = {
  739. .attrs = data_attrs,
  740. };
  741. static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
  742. {
  743. struct device *dev;
  744. u8 *buffer;
  745. int rv;
  746. dev = &data->intf->dev;
  747. buffer = kmalloc(2, GFP_KERNEL);
  748. if (!buffer)
  749. return -ENOMEM;
  750. rv = usb_control_msg(data->usb_dev,
  751. usb_rcvctrlpipe(data->usb_dev, 0),
  752. USBTMC_REQUEST_INDICATOR_PULSE,
  753. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  754. 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
  755. if (rv < 0) {
  756. dev_err(dev, "usb_control_msg returned %d\n", rv);
  757. goto exit;
  758. }
  759. dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
  760. if (buffer[0] != USBTMC_STATUS_SUCCESS) {
  761. dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
  762. rv = -EPERM;
  763. goto exit;
  764. }
  765. rv = 0;
  766. exit:
  767. kfree(buffer);
  768. return rv;
  769. }
  770. static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  771. {
  772. struct usbtmc_device_data *data;
  773. int retval = -EBADRQC;
  774. data = file->private_data;
  775. mutex_lock(&data->io_mutex);
  776. if (data->zombie) {
  777. retval = -ENODEV;
  778. goto skip_io_on_zombie;
  779. }
  780. switch (cmd) {
  781. case USBTMC_IOCTL_CLEAR_OUT_HALT:
  782. retval = usbtmc_ioctl_clear_out_halt(data);
  783. break;
  784. case USBTMC_IOCTL_CLEAR_IN_HALT:
  785. retval = usbtmc_ioctl_clear_in_halt(data);
  786. break;
  787. case USBTMC_IOCTL_INDICATOR_PULSE:
  788. retval = usbtmc_ioctl_indicator_pulse(data);
  789. break;
  790. case USBTMC_IOCTL_CLEAR:
  791. retval = usbtmc_ioctl_clear(data);
  792. break;
  793. case USBTMC_IOCTL_ABORT_BULK_OUT:
  794. retval = usbtmc_ioctl_abort_bulk_out(data);
  795. break;
  796. case USBTMC_IOCTL_ABORT_BULK_IN:
  797. retval = usbtmc_ioctl_abort_bulk_in(data);
  798. break;
  799. }
  800. skip_io_on_zombie:
  801. mutex_unlock(&data->io_mutex);
  802. return retval;
  803. }
  804. static const struct file_operations fops = {
  805. .owner = THIS_MODULE,
  806. .read = usbtmc_read,
  807. .write = usbtmc_write,
  808. .open = usbtmc_open,
  809. .release = usbtmc_release,
  810. .unlocked_ioctl = usbtmc_ioctl,
  811. .llseek = default_llseek,
  812. };
  813. static struct usb_class_driver usbtmc_class = {
  814. .name = "usbtmc%d",
  815. .fops = &fops,
  816. .minor_base = USBTMC_MINOR_BASE,
  817. };
  818. static int usbtmc_probe(struct usb_interface *intf,
  819. const struct usb_device_id *id)
  820. {
  821. struct usbtmc_device_data *data;
  822. struct usb_host_interface *iface_desc;
  823. struct usb_endpoint_descriptor *endpoint;
  824. int n;
  825. int retcode;
  826. dev_dbg(&intf->dev, "%s called\n", __func__);
  827. data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
  828. if (!data) {
  829. dev_err(&intf->dev, "Unable to allocate kernel memory\n");
  830. return -ENOMEM;
  831. }
  832. data->intf = intf;
  833. data->id = id;
  834. data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
  835. usb_set_intfdata(intf, data);
  836. kref_init(&data->kref);
  837. mutex_init(&data->io_mutex);
  838. data->zombie = 0;
  839. /* Initialize USBTMC bTag and other fields */
  840. data->bTag = 1;
  841. data->TermCharEnabled = 0;
  842. data->TermChar = '\n';
  843. /* USBTMC devices have only one setting, so use that */
  844. iface_desc = data->intf->cur_altsetting;
  845. /* Find bulk in endpoint */
  846. for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
  847. endpoint = &iface_desc->endpoint[n].desc;
  848. if (usb_endpoint_is_bulk_in(endpoint)) {
  849. data->bulk_in = endpoint->bEndpointAddress;
  850. dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
  851. data->bulk_in);
  852. break;
  853. }
  854. }
  855. /* Find bulk out endpoint */
  856. for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
  857. endpoint = &iface_desc->endpoint[n].desc;
  858. if (usb_endpoint_is_bulk_out(endpoint)) {
  859. data->bulk_out = endpoint->bEndpointAddress;
  860. dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
  861. data->bulk_out);
  862. break;
  863. }
  864. }
  865. retcode = get_capabilities(data);
  866. if (retcode)
  867. dev_err(&intf->dev, "can't read capabilities\n");
  868. else
  869. retcode = sysfs_create_group(&intf->dev.kobj,
  870. &capability_attr_grp);
  871. retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
  872. retcode = usb_register_dev(intf, &usbtmc_class);
  873. if (retcode) {
  874. dev_err(&intf->dev, "Not able to get a minor"
  875. " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
  876. retcode);
  877. goto error_register;
  878. }
  879. dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
  880. return 0;
  881. error_register:
  882. sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
  883. sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
  884. kref_put(&data->kref, usbtmc_delete);
  885. return retcode;
  886. }
  887. static void usbtmc_disconnect(struct usb_interface *intf)
  888. {
  889. struct usbtmc_device_data *data;
  890. dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
  891. data = usb_get_intfdata(intf);
  892. usb_deregister_dev(intf, &usbtmc_class);
  893. sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
  894. sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
  895. mutex_lock(&data->io_mutex);
  896. data->zombie = 1;
  897. mutex_unlock(&data->io_mutex);
  898. kref_put(&data->kref, usbtmc_delete);
  899. }
  900. static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
  901. {
  902. /* this driver does not have pending URBs */
  903. return 0;
  904. }
  905. static int usbtmc_resume(struct usb_interface *intf)
  906. {
  907. return 0;
  908. }
  909. static struct usb_driver usbtmc_driver = {
  910. .name = "usbtmc",
  911. .id_table = usbtmc_devices,
  912. .probe = usbtmc_probe,
  913. .disconnect = usbtmc_disconnect,
  914. .suspend = usbtmc_suspend,
  915. .resume = usbtmc_resume,
  916. };
  917. module_usb_driver(usbtmc_driver);
  918. MODULE_LICENSE("GPL");