cdc-acm.c 39 KB

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