cdc-acm.c 38 KB

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