cdc-acm.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
  6. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  10. *
  11. * USB Abstract Control Model driver for USB modems and ISDN adapters
  12. *
  13. * Sponsored by SuSE
  14. *
  15. * ChangeLog:
  16. * v0.9 - thorough cleaning, URBification, almost a rewrite
  17. * v0.10 - some more cleanups
  18. * v0.11 - fixed flow control, read error doesn't stop reads
  19. * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
  20. * v0.13 - added termios, added hangup
  21. * v0.14 - sized down struct acm
  22. * v0.15 - fixed flow control again - characters could be lost
  23. * v0.16 - added code for modems with swapped data and control interfaces
  24. * v0.17 - added new style probing
  25. * v0.18 - fixed new style probing for devices with more configurations
  26. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  27. * v0.20 - switched to probing on interface (rather than device) class
  28. * v0.21 - revert to probing on device for devices with multiple configs
  29. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  30. * config we want, sysadmin changes bConfigurationValue in sysfs.
  31. * v0.23 - use softirq for rx processing, as needed by tty layer
  32. * v0.24 - change probe method to evaluate CDC union descriptor
  33. * v0.25 - downstream tasks paralelized to maximize throughput
  34. */
  35. /*
  36. * This program is free software; you can redistribute it and/or modify
  37. * it under the terms of the GNU General Public License as published by
  38. * the Free Software Foundation; either version 2 of the License, or
  39. * (at your option) any later version.
  40. *
  41. * This program is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. * GNU General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU General Public License
  47. * along with this program; if not, write to the Free Software
  48. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  49. */
  50. #undef DEBUG
  51. #include <linux/kernel.h>
  52. #include <linux/errno.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/tty.h>
  56. #include <linux/tty_driver.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/module.h>
  59. #include <linux/mutex.h>
  60. #include <asm/uaccess.h>
  61. #include <linux/usb.h>
  62. #include <linux/usb/cdc.h>
  63. #include <asm/byteorder.h>
  64. #include <asm/unaligned.h>
  65. #include <linux/list.h>
  66. #include "cdc-acm.h"
  67. /*
  68. * Version Information
  69. */
  70. #define DRIVER_VERSION "v0.25"
  71. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  72. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  73. static struct usb_driver acm_driver;
  74. static struct tty_driver *acm_tty_driver;
  75. static struct acm *acm_table[ACM_TTY_MINORS];
  76. static DEFINE_MUTEX(open_mutex);
  77. #define ACM_READY(acm) (acm && acm->dev && acm->used)
  78. /*
  79. * Functions for ACM control messages.
  80. */
  81. static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
  82. {
  83. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  84. request, USB_RT_ACM, value,
  85. acm->control->altsetting[0].desc.bInterfaceNumber,
  86. buf, len, 5000);
  87. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
  88. return retval < 0 ? retval : 0;
  89. }
  90. /* devices aren't required to support these requests.
  91. * the cdc acm descriptor tells whether they do...
  92. */
  93. #define acm_set_control(acm, control) \
  94. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  95. #define acm_set_line(acm, line) \
  96. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  97. #define acm_send_break(acm, ms) \
  98. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  99. /*
  100. * Write buffer management.
  101. * All of these assume proper locks taken by the caller.
  102. */
  103. static int acm_wb_alloc(struct acm *acm)
  104. {
  105. int i, wbn;
  106. struct acm_wb *wb;
  107. wbn = acm->write_current;
  108. i = 0;
  109. for (;;) {
  110. wb = &acm->wb[wbn];
  111. if (!wb->use) {
  112. wb->use = 1;
  113. return wbn;
  114. }
  115. wbn = (wbn + 1) % ACM_NW;
  116. if (++i >= ACM_NW)
  117. return -1;
  118. }
  119. }
  120. static void acm_wb_free(struct acm *acm, int wbn)
  121. {
  122. acm->wb[wbn].use = 0;
  123. }
  124. static int acm_wb_is_avail(struct acm *acm)
  125. {
  126. int i, n;
  127. n = ACM_NW;
  128. for (i = 0; i < ACM_NW; i++) {
  129. n -= acm->wb[i].use;
  130. }
  131. return n;
  132. }
  133. static inline int acm_wb_is_used(struct acm *acm, int wbn)
  134. {
  135. return acm->wb[wbn].use;
  136. }
  137. /*
  138. * Finish write.
  139. */
  140. static void acm_write_done(struct acm *acm)
  141. {
  142. unsigned long flags;
  143. int wbn;
  144. spin_lock_irqsave(&acm->write_lock, flags);
  145. acm->write_ready = 1;
  146. wbn = acm->write_current;
  147. acm_wb_free(acm, wbn);
  148. acm->write_current = (wbn + 1) % ACM_NW;
  149. spin_unlock_irqrestore(&acm->write_lock, flags);
  150. }
  151. /*
  152. * Poke write.
  153. */
  154. static int acm_write_start(struct acm *acm)
  155. {
  156. unsigned long flags;
  157. int wbn;
  158. struct acm_wb *wb;
  159. int rc;
  160. spin_lock_irqsave(&acm->write_lock, flags);
  161. if (!acm->dev) {
  162. spin_unlock_irqrestore(&acm->write_lock, flags);
  163. return -ENODEV;
  164. }
  165. if (!acm->write_ready) {
  166. spin_unlock_irqrestore(&acm->write_lock, flags);
  167. return 0; /* A white lie */
  168. }
  169. wbn = acm->write_current;
  170. if (!acm_wb_is_used(acm, wbn)) {
  171. spin_unlock_irqrestore(&acm->write_lock, flags);
  172. return 0;
  173. }
  174. wb = &acm->wb[wbn];
  175. acm->write_ready = 0;
  176. spin_unlock_irqrestore(&acm->write_lock, flags);
  177. acm->writeurb->transfer_buffer = wb->buf;
  178. acm->writeurb->transfer_dma = wb->dmah;
  179. acm->writeurb->transfer_buffer_length = wb->len;
  180. acm->writeurb->dev = acm->dev;
  181. if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
  182. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  183. acm_write_done(acm);
  184. }
  185. return rc;
  186. }
  187. /*
  188. * attributes exported through sysfs
  189. */
  190. static ssize_t show_caps
  191. (struct device *dev, struct device_attribute *attr, char *buf)
  192. {
  193. struct usb_interface *intf = to_usb_interface(dev);
  194. struct acm *acm = usb_get_intfdata(intf);
  195. return sprintf(buf, "%d", acm->ctrl_caps);
  196. }
  197. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  198. static ssize_t show_country_codes
  199. (struct device *dev, struct device_attribute *attr, char *buf)
  200. {
  201. struct usb_interface *intf = to_usb_interface(dev);
  202. struct acm *acm = usb_get_intfdata(intf);
  203. memcpy(buf, acm->country_codes, acm->country_code_size);
  204. return acm->country_code_size;
  205. }
  206. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  207. static ssize_t show_country_rel_date
  208. (struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct usb_interface *intf = to_usb_interface(dev);
  211. struct acm *acm = usb_get_intfdata(intf);
  212. return sprintf(buf, "%d", acm->country_rel_date);
  213. }
  214. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  215. /*
  216. * Interrupt handlers for various ACM device responses
  217. */
  218. /* control interface reports status changes with "interrupt" transfers */
  219. static void acm_ctrl_irq(struct urb *urb)
  220. {
  221. struct acm *acm = urb->context;
  222. struct usb_cdc_notification *dr = urb->transfer_buffer;
  223. unsigned char *data;
  224. int newctrl;
  225. int retval;
  226. int status = urb->status;
  227. switch (status) {
  228. case 0:
  229. /* success */
  230. break;
  231. case -ECONNRESET:
  232. case -ENOENT:
  233. case -ESHUTDOWN:
  234. /* this urb is terminated, clean up */
  235. dbg("%s - urb shutting down with status: %d", __FUNCTION__, status);
  236. return;
  237. default:
  238. dbg("%s - nonzero urb status received: %d", __FUNCTION__, status);
  239. goto exit;
  240. }
  241. if (!ACM_READY(acm))
  242. goto exit;
  243. data = (unsigned char *)(dr + 1);
  244. switch (dr->bNotificationType) {
  245. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  246. dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
  247. break;
  248. case USB_CDC_NOTIFY_SERIAL_STATE:
  249. newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
  250. if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  251. dbg("calling hangup");
  252. tty_hangup(acm->tty);
  253. }
  254. acm->ctrlin = newctrl;
  255. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  256. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  257. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  258. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  259. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  260. break;
  261. default:
  262. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  263. dr->bNotificationType, dr->wIndex,
  264. dr->wLength, data[0], data[1]);
  265. break;
  266. }
  267. exit:
  268. retval = usb_submit_urb (urb, GFP_ATOMIC);
  269. if (retval)
  270. err ("%s - usb_submit_urb failed with result %d",
  271. __FUNCTION__, retval);
  272. }
  273. /* data interface returns incoming bytes, or we got unthrottled */
  274. static void acm_read_bulk(struct urb *urb)
  275. {
  276. struct acm_rb *buf;
  277. struct acm_ru *rcv = urb->context;
  278. struct acm *acm = rcv->instance;
  279. int status = urb->status;
  280. dbg("Entering acm_read_bulk with status %d", status);
  281. if (!ACM_READY(acm))
  282. return;
  283. if (status)
  284. dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
  285. buf = rcv->buffer;
  286. buf->size = urb->actual_length;
  287. if (likely(status == 0)) {
  288. spin_lock(&acm->read_lock);
  289. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  290. list_add_tail(&buf->list, &acm->filled_read_bufs);
  291. spin_unlock(&acm->read_lock);
  292. } else {
  293. /* we drop the buffer due to an error */
  294. spin_lock(&acm->read_lock);
  295. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  296. list_add(&buf->list, &acm->spare_read_bufs);
  297. spin_unlock(&acm->read_lock);
  298. /* nevertheless the tasklet must be kicked unconditionally
  299. so the queue cannot dry up */
  300. }
  301. tasklet_schedule(&acm->urb_task);
  302. }
  303. static void acm_rx_tasklet(unsigned long _acm)
  304. {
  305. struct acm *acm = (void *)_acm;
  306. struct acm_rb *buf;
  307. struct tty_struct *tty = acm->tty;
  308. struct acm_ru *rcv;
  309. unsigned long flags;
  310. unsigned char throttled;
  311. dbg("Entering acm_rx_tasklet");
  312. if (!ACM_READY(acm))
  313. return;
  314. spin_lock_irqsave(&acm->throttle_lock, flags);
  315. throttled = acm->throttle;
  316. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  317. if (throttled)
  318. return;
  319. next_buffer:
  320. spin_lock_irqsave(&acm->read_lock, flags);
  321. if (list_empty(&acm->filled_read_bufs)) {
  322. spin_unlock_irqrestore(&acm->read_lock, flags);
  323. goto urbs;
  324. }
  325. buf = list_entry(acm->filled_read_bufs.next,
  326. struct acm_rb, list);
  327. list_del(&buf->list);
  328. spin_unlock_irqrestore(&acm->read_lock, flags);
  329. dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
  330. tty_buffer_request_room(tty, buf->size);
  331. spin_lock_irqsave(&acm->throttle_lock, flags);
  332. throttled = acm->throttle;
  333. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  334. if (!throttled)
  335. tty_insert_flip_string(tty, buf->base, buf->size);
  336. tty_flip_buffer_push(tty);
  337. if (throttled) {
  338. dbg("Throttling noticed");
  339. spin_lock_irqsave(&acm->read_lock, flags);
  340. list_add(&buf->list, &acm->filled_read_bufs);
  341. spin_unlock_irqrestore(&acm->read_lock, flags);
  342. return;
  343. }
  344. spin_lock_irqsave(&acm->read_lock, flags);
  345. list_add(&buf->list, &acm->spare_read_bufs);
  346. spin_unlock_irqrestore(&acm->read_lock, flags);
  347. goto next_buffer;
  348. urbs:
  349. while (!list_empty(&acm->spare_read_bufs)) {
  350. spin_lock_irqsave(&acm->read_lock, flags);
  351. if (list_empty(&acm->spare_read_urbs)) {
  352. spin_unlock_irqrestore(&acm->read_lock, flags);
  353. return;
  354. }
  355. rcv = list_entry(acm->spare_read_urbs.next,
  356. struct acm_ru, list);
  357. list_del(&rcv->list);
  358. spin_unlock_irqrestore(&acm->read_lock, flags);
  359. buf = list_entry(acm->spare_read_bufs.next,
  360. struct acm_rb, list);
  361. list_del(&buf->list);
  362. rcv->buffer = buf;
  363. usb_fill_bulk_urb(rcv->urb, acm->dev,
  364. acm->rx_endpoint,
  365. buf->base,
  366. acm->readsize,
  367. acm_read_bulk, rcv);
  368. rcv->urb->transfer_dma = buf->dma;
  369. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  370. dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
  371. /* This shouldn't kill the driver as unsuccessful URBs are returned to the
  372. free-urbs-pool and resubmited ASAP */
  373. if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  374. list_add(&buf->list, &acm->spare_read_bufs);
  375. spin_lock_irqsave(&acm->read_lock, flags);
  376. list_add(&rcv->list, &acm->spare_read_urbs);
  377. spin_unlock_irqrestore(&acm->read_lock, flags);
  378. return;
  379. }
  380. }
  381. }
  382. /* data interface wrote those outgoing bytes */
  383. static void acm_write_bulk(struct urb *urb)
  384. {
  385. struct acm *acm = (struct acm *)urb->context;
  386. dbg("Entering acm_write_bulk with status %d", urb->status);
  387. acm_write_done(acm);
  388. acm_write_start(acm);
  389. if (ACM_READY(acm))
  390. schedule_work(&acm->work);
  391. }
  392. static void acm_softint(struct work_struct *work)
  393. {
  394. struct acm *acm = container_of(work, struct acm, work);
  395. dbg("Entering acm_softint.");
  396. if (!ACM_READY(acm))
  397. return;
  398. tty_wakeup(acm->tty);
  399. }
  400. /*
  401. * TTY handlers
  402. */
  403. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  404. {
  405. struct acm *acm;
  406. int rv = -EINVAL;
  407. int i;
  408. dbg("Entering acm_tty_open.");
  409. mutex_lock(&open_mutex);
  410. acm = acm_table[tty->index];
  411. if (!acm || !acm->dev)
  412. goto err_out;
  413. else
  414. rv = 0;
  415. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  416. tty->driver_data = acm;
  417. acm->tty = tty;
  418. /* force low_latency on so that our tty_push actually forces the data through,
  419. otherwise it is scheduled, and with high data rates data can get lost. */
  420. tty->low_latency = 1;
  421. if (usb_autopm_get_interface(acm->control) < 0)
  422. goto early_bail;
  423. mutex_lock(&acm->mutex);
  424. if (acm->used++) {
  425. usb_autopm_put_interface(acm->control);
  426. goto done;
  427. }
  428. acm->ctrlurb->dev = acm->dev;
  429. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  430. dbg("usb_submit_urb(ctrl irq) failed");
  431. goto bail_out;
  432. }
  433. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  434. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  435. goto full_bailout;
  436. INIT_LIST_HEAD(&acm->spare_read_urbs);
  437. INIT_LIST_HEAD(&acm->spare_read_bufs);
  438. INIT_LIST_HEAD(&acm->filled_read_bufs);
  439. for (i = 0; i < acm->rx_buflimit; i++) {
  440. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  441. }
  442. for (i = 0; i < acm->rx_buflimit; i++) {
  443. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  444. }
  445. acm->throttle = 0;
  446. tasklet_schedule(&acm->urb_task);
  447. done:
  448. err_out:
  449. mutex_unlock(&acm->mutex);
  450. mutex_unlock(&open_mutex);
  451. return rv;
  452. full_bailout:
  453. usb_kill_urb(acm->ctrlurb);
  454. bail_out:
  455. usb_autopm_put_interface(acm->control);
  456. acm->used--;
  457. mutex_unlock(&acm->mutex);
  458. early_bail:
  459. mutex_unlock(&open_mutex);
  460. return -EIO;
  461. }
  462. static void acm_tty_unregister(struct acm *acm)
  463. {
  464. int i,nr;
  465. nr = acm->rx_buflimit;
  466. tty_unregister_device(acm_tty_driver, acm->minor);
  467. usb_put_intf(acm->control);
  468. acm_table[acm->minor] = NULL;
  469. usb_free_urb(acm->ctrlurb);
  470. usb_free_urb(acm->writeurb);
  471. for (i = 0; i < nr; i++)
  472. usb_free_urb(acm->ru[i].urb);
  473. kfree(acm->country_codes);
  474. kfree(acm);
  475. }
  476. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  477. {
  478. struct acm *acm = tty->driver_data;
  479. int i,nr;
  480. if (!acm || !acm->used)
  481. return;
  482. nr = acm->rx_buflimit;
  483. mutex_lock(&open_mutex);
  484. if (!--acm->used) {
  485. if (acm->dev) {
  486. acm_set_control(acm, acm->ctrlout = 0);
  487. usb_kill_urb(acm->ctrlurb);
  488. usb_kill_urb(acm->writeurb);
  489. for (i = 0; i < nr; i++)
  490. usb_kill_urb(acm->ru[i].urb);
  491. usb_autopm_put_interface(acm->control);
  492. } else
  493. acm_tty_unregister(acm);
  494. }
  495. mutex_unlock(&open_mutex);
  496. }
  497. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  498. {
  499. struct acm *acm = tty->driver_data;
  500. int stat;
  501. unsigned long flags;
  502. int wbn;
  503. struct acm_wb *wb;
  504. dbg("Entering acm_tty_write to write %d bytes,", count);
  505. if (!ACM_READY(acm))
  506. return -EINVAL;
  507. if (!count)
  508. return 0;
  509. spin_lock_irqsave(&acm->write_lock, flags);
  510. if ((wbn = acm_wb_alloc(acm)) < 0) {
  511. spin_unlock_irqrestore(&acm->write_lock, flags);
  512. acm_write_start(acm);
  513. return 0;
  514. }
  515. wb = &acm->wb[wbn];
  516. count = (count > acm->writesize) ? acm->writesize : count;
  517. dbg("Get %d bytes...", count);
  518. memcpy(wb->buf, buf, count);
  519. wb->len = count;
  520. spin_unlock_irqrestore(&acm->write_lock, flags);
  521. if ((stat = acm_write_start(acm)) < 0)
  522. return stat;
  523. return count;
  524. }
  525. static int acm_tty_write_room(struct tty_struct *tty)
  526. {
  527. struct acm *acm = tty->driver_data;
  528. if (!ACM_READY(acm))
  529. return -EINVAL;
  530. /*
  531. * Do not let the line discipline to know that we have a reserve,
  532. * or it might get too enthusiastic.
  533. */
  534. return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
  535. }
  536. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  537. {
  538. struct acm *acm = tty->driver_data;
  539. if (!ACM_READY(acm))
  540. return -EINVAL;
  541. /*
  542. * This is inaccurate (overcounts), but it works.
  543. */
  544. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  545. }
  546. static void acm_tty_throttle(struct tty_struct *tty)
  547. {
  548. struct acm *acm = tty->driver_data;
  549. if (!ACM_READY(acm))
  550. return;
  551. spin_lock_bh(&acm->throttle_lock);
  552. acm->throttle = 1;
  553. spin_unlock_bh(&acm->throttle_lock);
  554. }
  555. static void acm_tty_unthrottle(struct tty_struct *tty)
  556. {
  557. struct acm *acm = tty->driver_data;
  558. if (!ACM_READY(acm))
  559. return;
  560. spin_lock_bh(&acm->throttle_lock);
  561. acm->throttle = 0;
  562. spin_unlock_bh(&acm->throttle_lock);
  563. tasklet_schedule(&acm->urb_task);
  564. }
  565. static void acm_tty_break_ctl(struct tty_struct *tty, int state)
  566. {
  567. struct acm *acm = tty->driver_data;
  568. if (!ACM_READY(acm))
  569. return;
  570. if (acm_send_break(acm, state ? 0xffff : 0))
  571. dbg("send break failed");
  572. }
  573. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  574. {
  575. struct acm *acm = tty->driver_data;
  576. if (!ACM_READY(acm))
  577. return -EINVAL;
  578. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  579. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  580. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  581. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  582. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  583. TIOCM_CTS;
  584. }
  585. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  586. unsigned int set, unsigned int clear)
  587. {
  588. struct acm *acm = tty->driver_data;
  589. unsigned int newctrl;
  590. if (!ACM_READY(acm))
  591. return -EINVAL;
  592. newctrl = acm->ctrlout;
  593. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  594. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  595. newctrl = (newctrl & ~clear) | set;
  596. if (acm->ctrlout == newctrl)
  597. return 0;
  598. return acm_set_control(acm, acm->ctrlout = newctrl);
  599. }
  600. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  601. {
  602. struct acm *acm = tty->driver_data;
  603. if (!ACM_READY(acm))
  604. return -EINVAL;
  605. return -ENOIOCTLCMD;
  606. }
  607. static const __u32 acm_tty_speed[] = {
  608. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  609. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  610. 57600, 115200, 230400, 460800, 500000, 576000,
  611. 921600, 1000000, 1152000, 1500000, 2000000,
  612. 2500000, 3000000, 3500000, 4000000
  613. };
  614. static const __u8 acm_tty_size[] = {
  615. 5, 6, 7, 8
  616. };
  617. static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
  618. {
  619. struct acm *acm = tty->driver_data;
  620. struct ktermios *termios = tty->termios;
  621. struct usb_cdc_line_coding newline;
  622. int newctrl = acm->ctrlout;
  623. if (!ACM_READY(acm))
  624. return;
  625. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  626. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  627. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  628. newline.bParityType = termios->c_cflag & PARENB ?
  629. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  630. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  631. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  632. if (!newline.dwDTERate) {
  633. newline.dwDTERate = acm->line.dwDTERate;
  634. newctrl &= ~ACM_CTRL_DTR;
  635. } else newctrl |= ACM_CTRL_DTR;
  636. if (newctrl != acm->ctrlout)
  637. acm_set_control(acm, acm->ctrlout = newctrl);
  638. if (memcmp(&acm->line, &newline, sizeof newline)) {
  639. memcpy(&acm->line, &newline, sizeof newline);
  640. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  641. newline.bCharFormat, newline.bParityType,
  642. newline.bDataBits);
  643. acm_set_line(acm, &acm->line);
  644. }
  645. }
  646. /*
  647. * USB probe and disconnect routines.
  648. */
  649. /* Little helper: write buffers free */
  650. static void acm_write_buffers_free(struct acm *acm)
  651. {
  652. int i;
  653. struct acm_wb *wb;
  654. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  655. usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
  656. }
  657. }
  658. /* Little helper: write buffers allocate */
  659. static int acm_write_buffers_alloc(struct acm *acm)
  660. {
  661. int i;
  662. struct acm_wb *wb;
  663. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  664. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  665. &wb->dmah);
  666. if (!wb->buf) {
  667. while (i != 0) {
  668. --i;
  669. --wb;
  670. usb_buffer_free(acm->dev, acm->writesize,
  671. wb->buf, wb->dmah);
  672. }
  673. return -ENOMEM;
  674. }
  675. }
  676. return 0;
  677. }
  678. static int acm_probe (struct usb_interface *intf,
  679. const struct usb_device_id *id)
  680. {
  681. struct usb_cdc_union_desc *union_header = NULL;
  682. struct usb_cdc_country_functional_desc *cfd = NULL;
  683. char *buffer = intf->altsetting->extra;
  684. int buflen = intf->altsetting->extralen;
  685. struct usb_interface *control_interface;
  686. struct usb_interface *data_interface;
  687. struct usb_endpoint_descriptor *epctrl;
  688. struct usb_endpoint_descriptor *epread;
  689. struct usb_endpoint_descriptor *epwrite;
  690. struct usb_device *usb_dev = interface_to_usbdev(intf);
  691. struct acm *acm;
  692. int minor;
  693. int ctrlsize,readsize;
  694. u8 *buf;
  695. u8 ac_management_function = 0;
  696. u8 call_management_function = 0;
  697. int call_interface_num = -1;
  698. int data_interface_num;
  699. unsigned long quirks;
  700. int num_rx_buf;
  701. int i;
  702. /* normal quirks */
  703. quirks = (unsigned long)id->driver_info;
  704. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  705. /* handle quirks deadly to normal probing*/
  706. if (quirks == NO_UNION_NORMAL) {
  707. data_interface = usb_ifnum_to_if(usb_dev, 1);
  708. control_interface = usb_ifnum_to_if(usb_dev, 0);
  709. goto skip_normal_probe;
  710. }
  711. /* normal probing*/
  712. if (!buffer) {
  713. err("Weird descriptor references\n");
  714. return -EINVAL;
  715. }
  716. if (!buflen) {
  717. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  718. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  719. buflen = intf->cur_altsetting->endpoint->extralen;
  720. buffer = intf->cur_altsetting->endpoint->extra;
  721. } else {
  722. err("Zero length descriptor references\n");
  723. return -EINVAL;
  724. }
  725. }
  726. while (buflen > 0) {
  727. if (buffer [1] != USB_DT_CS_INTERFACE) {
  728. err("skipping garbage\n");
  729. goto next_desc;
  730. }
  731. switch (buffer [2]) {
  732. case USB_CDC_UNION_TYPE: /* we've found it */
  733. if (union_header) {
  734. err("More than one union descriptor, skipping ...");
  735. goto next_desc;
  736. }
  737. union_header = (struct usb_cdc_union_desc *)
  738. buffer;
  739. break;
  740. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  741. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  742. break;
  743. case USB_CDC_HEADER_TYPE: /* maybe check version */
  744. break; /* for now we ignore it */
  745. case USB_CDC_ACM_TYPE:
  746. ac_management_function = buffer[3];
  747. break;
  748. case USB_CDC_CALL_MANAGEMENT_TYPE:
  749. call_management_function = buffer[3];
  750. call_interface_num = buffer[4];
  751. if ((call_management_function & 3) != 3)
  752. err("This device cannot do calls on its own. It is no modem.");
  753. break;
  754. default:
  755. err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
  756. break;
  757. }
  758. next_desc:
  759. buflen -= buffer[0];
  760. buffer += buffer[0];
  761. }
  762. if (!union_header) {
  763. if (call_interface_num > 0) {
  764. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  765. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  766. control_interface = intf;
  767. } else {
  768. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  769. return -ENODEV;
  770. }
  771. } else {
  772. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  773. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  774. if (!control_interface || !data_interface) {
  775. dev_dbg(&intf->dev,"no interfaces\n");
  776. return -ENODEV;
  777. }
  778. }
  779. if (data_interface_num != call_interface_num)
  780. dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
  781. skip_normal_probe:
  782. /*workaround for switched interfaces */
  783. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  784. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  785. struct usb_interface *t;
  786. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  787. t = control_interface;
  788. control_interface = data_interface;
  789. data_interface = t;
  790. } else {
  791. return -EINVAL;
  792. }
  793. }
  794. /* Accept probe requests only for the control interface */
  795. if (intf != control_interface)
  796. return -ENODEV;
  797. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  798. dev_dbg(&intf->dev,"The data interface isn't available\n");
  799. return -EBUSY;
  800. }
  801. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  802. return -EINVAL;
  803. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  804. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  805. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  806. /* workaround for switched endpoints */
  807. if (!usb_endpoint_dir_in(epread)) {
  808. /* descriptors are swapped */
  809. struct usb_endpoint_descriptor *t;
  810. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  811. t = epread;
  812. epread = epwrite;
  813. epwrite = t;
  814. }
  815. dbg("interfaces are valid");
  816. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  817. if (minor == ACM_TTY_MINORS) {
  818. err("no more free acm devices");
  819. return -ENODEV;
  820. }
  821. if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
  822. dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
  823. goto alloc_fail;
  824. }
  825. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  826. readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
  827. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
  828. acm->control = control_interface;
  829. acm->data = data_interface;
  830. acm->minor = minor;
  831. acm->dev = usb_dev;
  832. acm->ctrl_caps = ac_management_function;
  833. acm->ctrlsize = ctrlsize;
  834. acm->readsize = readsize;
  835. acm->rx_buflimit = num_rx_buf;
  836. acm->urb_task.func = acm_rx_tasklet;
  837. acm->urb_task.data = (unsigned long) acm;
  838. INIT_WORK(&acm->work, acm_softint);
  839. spin_lock_init(&acm->throttle_lock);
  840. spin_lock_init(&acm->write_lock);
  841. spin_lock_init(&acm->read_lock);
  842. mutex_init(&acm->mutex);
  843. acm->write_ready = 1;
  844. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  845. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  846. if (!buf) {
  847. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  848. goto alloc_fail2;
  849. }
  850. acm->ctrl_buffer = buf;
  851. if (acm_write_buffers_alloc(acm) < 0) {
  852. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  853. goto alloc_fail4;
  854. }
  855. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  856. if (!acm->ctrlurb) {
  857. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  858. goto alloc_fail5;
  859. }
  860. for (i = 0; i < num_rx_buf; i++) {
  861. struct acm_ru *rcv = &(acm->ru[i]);
  862. if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
  863. dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
  864. goto alloc_fail7;
  865. }
  866. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  867. rcv->instance = acm;
  868. }
  869. for (i = 0; i < num_rx_buf; i++) {
  870. struct acm_rb *buf = &(acm->rb[i]);
  871. if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
  872. dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
  873. goto alloc_fail7;
  874. }
  875. }
  876. acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  877. if (!acm->writeurb) {
  878. dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
  879. goto alloc_fail7;
  880. }
  881. usb_set_intfdata (intf, acm);
  882. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  883. if (i < 0)
  884. goto alloc_fail8;
  885. if (cfd) { /* export the country data */
  886. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  887. if (!acm->country_codes)
  888. goto skip_countries;
  889. acm->country_code_size = cfd->bLength - 4;
  890. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
  891. acm->country_rel_date = cfd->iCountryCodeRelDate;
  892. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  893. if (i < 0) {
  894. kfree(acm->country_codes);
  895. goto skip_countries;
  896. }
  897. i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
  898. if (i < 0) {
  899. kfree(acm->country_codes);
  900. goto skip_countries;
  901. }
  902. }
  903. skip_countries:
  904. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  905. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  906. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  907. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  908. usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  909. NULL, acm->writesize, acm_write_bulk, acm);
  910. acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  911. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  912. acm_set_control(acm, acm->ctrlout);
  913. acm->line.dwDTERate = cpu_to_le32(9600);
  914. acm->line.bDataBits = 8;
  915. acm_set_line(acm, &acm->line);
  916. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  917. usb_get_intf(control_interface);
  918. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  919. acm_table[minor] = acm;
  920. return 0;
  921. alloc_fail8:
  922. usb_free_urb(acm->writeurb);
  923. alloc_fail7:
  924. for (i = 0; i < num_rx_buf; i++)
  925. usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
  926. for (i = 0; i < num_rx_buf; i++)
  927. usb_free_urb(acm->ru[i].urb);
  928. usb_free_urb(acm->ctrlurb);
  929. alloc_fail5:
  930. acm_write_buffers_free(acm);
  931. alloc_fail4:
  932. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  933. alloc_fail2:
  934. kfree(acm);
  935. alloc_fail:
  936. return -ENOMEM;
  937. }
  938. static void stop_data_traffic(struct acm *acm)
  939. {
  940. int i;
  941. tasklet_disable(&acm->urb_task);
  942. usb_kill_urb(acm->ctrlurb);
  943. usb_kill_urb(acm->writeurb);
  944. for (i = 0; i < acm->rx_buflimit; i++)
  945. usb_kill_urb(acm->ru[i].urb);
  946. INIT_LIST_HEAD(&acm->filled_read_bufs);
  947. INIT_LIST_HEAD(&acm->spare_read_bufs);
  948. tasklet_enable(&acm->urb_task);
  949. cancel_work_sync(&acm->work);
  950. }
  951. static void acm_disconnect(struct usb_interface *intf)
  952. {
  953. struct acm *acm = usb_get_intfdata(intf);
  954. struct usb_device *usb_dev = interface_to_usbdev(intf);
  955. int i;
  956. if (!acm || !acm->dev) {
  957. dbg("disconnect on nonexisting interface");
  958. return;
  959. }
  960. mutex_lock(&open_mutex);
  961. if (!usb_get_intfdata(intf)) {
  962. mutex_unlock(&open_mutex);
  963. return;
  964. }
  965. if (acm->country_codes){
  966. device_remove_file(&acm->control->dev,
  967. &dev_attr_wCountryCodes);
  968. device_remove_file(&acm->control->dev,
  969. &dev_attr_iCountryCodeRelDate);
  970. }
  971. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  972. acm->dev = NULL;
  973. usb_set_intfdata(acm->control, NULL);
  974. usb_set_intfdata(acm->data, NULL);
  975. stop_data_traffic(acm);
  976. acm_write_buffers_free(acm);
  977. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  978. for (i = 0; i < acm->rx_buflimit; i++)
  979. usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
  980. usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
  981. if (!acm->used) {
  982. acm_tty_unregister(acm);
  983. mutex_unlock(&open_mutex);
  984. return;
  985. }
  986. mutex_unlock(&open_mutex);
  987. if (acm->tty)
  988. tty_hangup(acm->tty);
  989. }
  990. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  991. {
  992. struct acm *acm = usb_get_intfdata(intf);
  993. if (acm->susp_count++)
  994. return 0;
  995. /*
  996. we treat opened interfaces differently,
  997. we must guard against open
  998. */
  999. mutex_lock(&acm->mutex);
  1000. if (acm->used)
  1001. stop_data_traffic(acm);
  1002. mutex_unlock(&acm->mutex);
  1003. return 0;
  1004. }
  1005. static int acm_resume(struct usb_interface *intf)
  1006. {
  1007. struct acm *acm = usb_get_intfdata(intf);
  1008. int rv = 0;
  1009. if (--acm->susp_count)
  1010. return 0;
  1011. mutex_lock(&acm->mutex);
  1012. if (acm->used) {
  1013. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1014. if (rv < 0)
  1015. goto err_out;
  1016. tasklet_schedule(&acm->urb_task);
  1017. }
  1018. err_out:
  1019. mutex_unlock(&acm->mutex);
  1020. return rv;
  1021. }
  1022. /*
  1023. * USB driver structure.
  1024. */
  1025. static struct usb_device_id acm_ids[] = {
  1026. /* quirky and broken devices */
  1027. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1028. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1029. },
  1030. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1031. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1032. },
  1033. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1034. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1035. },
  1036. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1037. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1038. },
  1039. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1040. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1041. },
  1042. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1043. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1044. },
  1045. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1046. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1047. },
  1048. /* control interfaces with various AT-command sets */
  1049. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1050. USB_CDC_ACM_PROTO_AT_V25TER) },
  1051. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1052. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1053. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1054. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1055. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1056. USB_CDC_ACM_PROTO_AT_GSM) },
  1057. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1058. USB_CDC_ACM_PROTO_AT_3G ) },
  1059. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1060. USB_CDC_ACM_PROTO_AT_CDMA) },
  1061. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  1062. { }
  1063. };
  1064. MODULE_DEVICE_TABLE (usb, acm_ids);
  1065. static struct usb_driver acm_driver = {
  1066. .name = "cdc_acm",
  1067. .probe = acm_probe,
  1068. .disconnect = acm_disconnect,
  1069. .suspend = acm_suspend,
  1070. .resume = acm_resume,
  1071. .id_table = acm_ids,
  1072. .supports_autosuspend = 1,
  1073. };
  1074. /*
  1075. * TTY driver structures.
  1076. */
  1077. static const struct tty_operations acm_ops = {
  1078. .open = acm_tty_open,
  1079. .close = acm_tty_close,
  1080. .write = acm_tty_write,
  1081. .write_room = acm_tty_write_room,
  1082. .ioctl = acm_tty_ioctl,
  1083. .throttle = acm_tty_throttle,
  1084. .unthrottle = acm_tty_unthrottle,
  1085. .chars_in_buffer = acm_tty_chars_in_buffer,
  1086. .break_ctl = acm_tty_break_ctl,
  1087. .set_termios = acm_tty_set_termios,
  1088. .tiocmget = acm_tty_tiocmget,
  1089. .tiocmset = acm_tty_tiocmset,
  1090. };
  1091. /*
  1092. * Init / exit.
  1093. */
  1094. static int __init acm_init(void)
  1095. {
  1096. int retval;
  1097. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1098. if (!acm_tty_driver)
  1099. return -ENOMEM;
  1100. acm_tty_driver->owner = THIS_MODULE,
  1101. acm_tty_driver->driver_name = "acm",
  1102. acm_tty_driver->name = "ttyACM",
  1103. acm_tty_driver->major = ACM_TTY_MAJOR,
  1104. acm_tty_driver->minor_start = 0,
  1105. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1106. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1107. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1108. acm_tty_driver->init_termios = tty_std_termios;
  1109. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1110. tty_set_operations(acm_tty_driver, &acm_ops);
  1111. retval = tty_register_driver(acm_tty_driver);
  1112. if (retval) {
  1113. put_tty_driver(acm_tty_driver);
  1114. return retval;
  1115. }
  1116. retval = usb_register(&acm_driver);
  1117. if (retval) {
  1118. tty_unregister_driver(acm_tty_driver);
  1119. put_tty_driver(acm_tty_driver);
  1120. return retval;
  1121. }
  1122. info(DRIVER_VERSION ":" DRIVER_DESC);
  1123. return 0;
  1124. }
  1125. static void __exit acm_exit(void)
  1126. {
  1127. usb_deregister(&acm_driver);
  1128. tty_unregister_driver(acm_tty_driver);
  1129. put_tty_driver(acm_tty_driver);
  1130. }
  1131. module_init(acm_init);
  1132. module_exit(acm_exit);
  1133. MODULE_AUTHOR( DRIVER_AUTHOR );
  1134. MODULE_DESCRIPTION( DRIVER_DESC );
  1135. MODULE_LICENSE("GPL");