usbtmc.c 30 KB

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