cdc-acm.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * cdc-acm.c
  4. *
  5. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  6. * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  8. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  9. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  10. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  11. * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
  12. *
  13. * USB Abstract Control Model driver for USB modems and ISDN adapters
  14. *
  15. * Sponsored by SuSE
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. #undef DEBUG
  32. #undef VERBOSE_DEBUG
  33. #include <linux/kernel.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/errno.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/log2.h>
  39. #include <linux/tty.h>
  40. #include <linux/serial.h>
  41. #include <linux/tty_driver.h>
  42. #include <linux/tty_flip.h>
  43. #include <linux/module.h>
  44. #include <linux/mutex.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/usb.h>
  47. #include <linux/usb/cdc.h>
  48. #include <asm/byteorder.h>
  49. #include <asm/unaligned.h>
  50. #include <linux/idr.h>
  51. #include <linux/list.h>
  52. #include "cdc-acm.h"
  53. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  54. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  55. static struct usb_driver acm_driver;
  56. static struct tty_driver *acm_tty_driver;
  57. static DEFINE_IDR(acm_minors);
  58. static DEFINE_MUTEX(acm_minors_lock);
  59. static void acm_tty_set_termios(struct tty_struct *tty,
  60. struct ktermios *termios_old);
  61. /*
  62. * acm_minors accessors
  63. */
  64. /*
  65. * Look up an ACM structure by minor. If found and not disconnected, increment
  66. * its refcount and return it with its mutex held.
  67. */
  68. static struct acm *acm_get_by_minor(unsigned int minor)
  69. {
  70. struct acm *acm;
  71. mutex_lock(&acm_minors_lock);
  72. acm = idr_find(&acm_minors, minor);
  73. if (acm) {
  74. mutex_lock(&acm->mutex);
  75. if (acm->disconnected) {
  76. mutex_unlock(&acm->mutex);
  77. acm = NULL;
  78. } else {
  79. tty_port_get(&acm->port);
  80. mutex_unlock(&acm->mutex);
  81. }
  82. }
  83. mutex_unlock(&acm_minors_lock);
  84. return acm;
  85. }
  86. /*
  87. * Try to find an available minor number and if found, associate it with 'acm'.
  88. */
  89. static int acm_alloc_minor(struct acm *acm)
  90. {
  91. int minor;
  92. mutex_lock(&acm_minors_lock);
  93. minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
  94. mutex_unlock(&acm_minors_lock);
  95. return minor;
  96. }
  97. /* Release the minor number associated with 'acm'. */
  98. static void acm_release_minor(struct acm *acm)
  99. {
  100. mutex_lock(&acm_minors_lock);
  101. idr_remove(&acm_minors, acm->minor);
  102. mutex_unlock(&acm_minors_lock);
  103. }
  104. /*
  105. * Functions for ACM control messages.
  106. */
  107. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  108. void *buf, int len)
  109. {
  110. int retval;
  111. retval = usb_autopm_get_interface(acm->control);
  112. if (retval)
  113. return retval;
  114. retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  115. request, USB_RT_ACM, value,
  116. acm->control->altsetting[0].desc.bInterfaceNumber,
  117. buf, len, 5000);
  118. dev_dbg(&acm->control->dev,
  119. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  120. __func__, request, value, len, retval);
  121. usb_autopm_put_interface(acm->control);
  122. return retval < 0 ? retval : 0;
  123. }
  124. /* devices aren't required to support these requests.
  125. * the cdc acm descriptor tells whether they do...
  126. */
  127. static inline int acm_set_control(struct acm *acm, int control)
  128. {
  129. if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
  130. return -EOPNOTSUPP;
  131. return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
  132. control, NULL, 0);
  133. }
  134. #define acm_set_line(acm, line) \
  135. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  136. #define acm_send_break(acm, ms) \
  137. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  138. static void acm_kill_urbs(struct acm *acm)
  139. {
  140. int i;
  141. usb_kill_urb(acm->ctrlurb);
  142. for (i = 0; i < ACM_NW; i++)
  143. usb_kill_urb(acm->wb[i].urb);
  144. for (i = 0; i < acm->rx_buflimit; i++)
  145. usb_kill_urb(acm->read_urbs[i]);
  146. }
  147. /*
  148. * Write buffer management.
  149. * All of these assume proper locks taken by the caller.
  150. */
  151. static int acm_wb_alloc(struct acm *acm)
  152. {
  153. int i, wbn;
  154. struct acm_wb *wb;
  155. wbn = 0;
  156. i = 0;
  157. for (;;) {
  158. wb = &acm->wb[wbn];
  159. if (!wb->use) {
  160. wb->use = 1;
  161. return wbn;
  162. }
  163. wbn = (wbn + 1) % ACM_NW;
  164. if (++i >= ACM_NW)
  165. return -1;
  166. }
  167. }
  168. static int acm_wb_is_avail(struct acm *acm)
  169. {
  170. int i, n;
  171. unsigned long flags;
  172. n = ACM_NW;
  173. spin_lock_irqsave(&acm->write_lock, flags);
  174. for (i = 0; i < ACM_NW; i++)
  175. n -= acm->wb[i].use;
  176. spin_unlock_irqrestore(&acm->write_lock, flags);
  177. return n;
  178. }
  179. /*
  180. * Finish write. Caller must hold acm->write_lock
  181. */
  182. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  183. {
  184. wb->use = 0;
  185. acm->transmitting--;
  186. usb_autopm_put_interface_async(acm->control);
  187. }
  188. /*
  189. * Poke write.
  190. *
  191. * the caller is responsible for locking
  192. */
  193. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  194. {
  195. int rc;
  196. acm->transmitting++;
  197. wb->urb->transfer_buffer = wb->buf;
  198. wb->urb->transfer_dma = wb->dmah;
  199. wb->urb->transfer_buffer_length = wb->len;
  200. wb->urb->dev = acm->dev;
  201. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  202. if (rc < 0) {
  203. dev_err(&acm->data->dev,
  204. "%s - usb_submit_urb(write bulk) failed: %d\n",
  205. __func__, rc);
  206. acm_write_done(acm, wb);
  207. }
  208. return rc;
  209. }
  210. /*
  211. * attributes exported through sysfs
  212. */
  213. static ssize_t show_caps
  214. (struct device *dev, struct device_attribute *attr, char *buf)
  215. {
  216. struct usb_interface *intf = to_usb_interface(dev);
  217. struct acm *acm = usb_get_intfdata(intf);
  218. return sprintf(buf, "%d", acm->ctrl_caps);
  219. }
  220. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  221. static ssize_t show_country_codes
  222. (struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. struct usb_interface *intf = to_usb_interface(dev);
  225. struct acm *acm = usb_get_intfdata(intf);
  226. memcpy(buf, acm->country_codes, acm->country_code_size);
  227. return acm->country_code_size;
  228. }
  229. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  230. static ssize_t show_country_rel_date
  231. (struct device *dev, struct device_attribute *attr, char *buf)
  232. {
  233. struct usb_interface *intf = to_usb_interface(dev);
  234. struct acm *acm = usb_get_intfdata(intf);
  235. return sprintf(buf, "%d", acm->country_rel_date);
  236. }
  237. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  238. /*
  239. * Interrupt handlers for various ACM device responses
  240. */
  241. static void acm_process_notification(struct acm *acm, unsigned char *buf)
  242. {
  243. int newctrl;
  244. int difference;
  245. struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf;
  246. unsigned char *data = buf + sizeof(struct usb_cdc_notification);
  247. switch (dr->bNotificationType) {
  248. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  249. dev_dbg(&acm->control->dev,
  250. "%s - network connection: %d\n", __func__, dr->wValue);
  251. break;
  252. case USB_CDC_NOTIFY_SERIAL_STATE:
  253. if (le16_to_cpu(dr->wLength) != 2) {
  254. dev_dbg(&acm->control->dev,
  255. "%s - malformed serial state\n", __func__);
  256. break;
  257. }
  258. newctrl = get_unaligned_le16(data);
  259. dev_dbg(&acm->control->dev,
  260. "%s - serial state: 0x%x\n", __func__, newctrl);
  261. if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  262. dev_dbg(&acm->control->dev,
  263. "%s - calling hangup\n", __func__);
  264. tty_port_tty_hangup(&acm->port, false);
  265. }
  266. difference = acm->ctrlin ^ newctrl;
  267. spin_lock(&acm->read_lock);
  268. acm->ctrlin = newctrl;
  269. acm->oldcount = acm->iocount;
  270. if (difference & ACM_CTRL_DSR)
  271. acm->iocount.dsr++;
  272. if (difference & ACM_CTRL_BRK)
  273. acm->iocount.brk++;
  274. if (difference & ACM_CTRL_RI)
  275. acm->iocount.rng++;
  276. if (difference & ACM_CTRL_DCD)
  277. acm->iocount.dcd++;
  278. if (difference & ACM_CTRL_FRAMING)
  279. acm->iocount.frame++;
  280. if (difference & ACM_CTRL_PARITY)
  281. acm->iocount.parity++;
  282. if (difference & ACM_CTRL_OVERRUN)
  283. acm->iocount.overrun++;
  284. spin_unlock(&acm->read_lock);
  285. if (difference)
  286. wake_up_all(&acm->wioctl);
  287. break;
  288. default:
  289. dev_dbg(&acm->control->dev,
  290. "%s - unknown notification %d received: index %d len %d\n",
  291. __func__,
  292. dr->bNotificationType, dr->wIndex, dr->wLength);
  293. }
  294. }
  295. /* control interface reports status changes with "interrupt" transfers */
  296. static void acm_ctrl_irq(struct urb *urb)
  297. {
  298. struct acm *acm = urb->context;
  299. struct usb_cdc_notification *dr = urb->transfer_buffer;
  300. unsigned int current_size = urb->actual_length;
  301. unsigned int expected_size, copy_size, alloc_size;
  302. int retval;
  303. int status = urb->status;
  304. switch (status) {
  305. case 0:
  306. /* success */
  307. break;
  308. case -ECONNRESET:
  309. case -ENOENT:
  310. case -ESHUTDOWN:
  311. /* this urb is terminated, clean up */
  312. acm->nb_index = 0;
  313. dev_dbg(&acm->control->dev,
  314. "%s - urb shutting down with status: %d\n",
  315. __func__, status);
  316. return;
  317. default:
  318. dev_dbg(&acm->control->dev,
  319. "%s - nonzero urb status received: %d\n",
  320. __func__, status);
  321. goto exit;
  322. }
  323. usb_mark_last_busy(acm->dev);
  324. if (acm->nb_index)
  325. dr = (struct usb_cdc_notification *)acm->notification_buffer;
  326. /* size = notification-header + (optional) data */
  327. expected_size = sizeof(struct usb_cdc_notification) +
  328. le16_to_cpu(dr->wLength);
  329. if (current_size < expected_size) {
  330. /* notification is transmitted fragmented, reassemble */
  331. if (acm->nb_size < expected_size) {
  332. if (acm->nb_size) {
  333. kfree(acm->notification_buffer);
  334. acm->nb_size = 0;
  335. }
  336. alloc_size = roundup_pow_of_two(expected_size);
  337. /*
  338. * kmalloc ensures a valid notification_buffer after a
  339. * use of kfree in case the previous allocation was too
  340. * small. Final freeing is done on disconnect.
  341. */
  342. acm->notification_buffer =
  343. kmalloc(alloc_size, GFP_ATOMIC);
  344. if (!acm->notification_buffer)
  345. goto exit;
  346. acm->nb_size = alloc_size;
  347. }
  348. copy_size = min(current_size,
  349. expected_size - acm->nb_index);
  350. memcpy(&acm->notification_buffer[acm->nb_index],
  351. urb->transfer_buffer, copy_size);
  352. acm->nb_index += copy_size;
  353. current_size = acm->nb_index;
  354. }
  355. if (current_size >= expected_size) {
  356. /* notification complete */
  357. acm_process_notification(acm, (unsigned char *)dr);
  358. acm->nb_index = 0;
  359. }
  360. exit:
  361. retval = usb_submit_urb(urb, GFP_ATOMIC);
  362. if (retval && retval != -EPERM)
  363. dev_err(&acm->control->dev,
  364. "%s - usb_submit_urb failed: %d\n", __func__, retval);
  365. }
  366. static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
  367. {
  368. int res;
  369. if (!test_and_clear_bit(index, &acm->read_urbs_free))
  370. return 0;
  371. res = usb_submit_urb(acm->read_urbs[index], mem_flags);
  372. if (res) {
  373. if (res != -EPERM) {
  374. dev_err(&acm->data->dev,
  375. "urb %d failed submission with %d\n",
  376. index, res);
  377. }
  378. set_bit(index, &acm->read_urbs_free);
  379. return res;
  380. } else {
  381. dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
  382. }
  383. return 0;
  384. }
  385. static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
  386. {
  387. int res;
  388. int i;
  389. for (i = 0; i < acm->rx_buflimit; ++i) {
  390. res = acm_submit_read_urb(acm, i, mem_flags);
  391. if (res)
  392. return res;
  393. }
  394. return 0;
  395. }
  396. static void acm_process_read_urb(struct acm *acm, struct urb *urb)
  397. {
  398. if (!urb->actual_length)
  399. return;
  400. tty_insert_flip_string(&acm->port, urb->transfer_buffer,
  401. urb->actual_length);
  402. tty_flip_buffer_push(&acm->port);
  403. }
  404. static void acm_read_bulk_callback(struct urb *urb)
  405. {
  406. struct acm_rb *rb = urb->context;
  407. struct acm *acm = rb->instance;
  408. unsigned long flags;
  409. int status = urb->status;
  410. dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
  411. rb->index, urb->actual_length, status);
  412. set_bit(rb->index, &acm->read_urbs_free);
  413. if (!acm->dev) {
  414. dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
  415. return;
  416. }
  417. switch (status) {
  418. case 0:
  419. usb_mark_last_busy(acm->dev);
  420. acm_process_read_urb(acm, urb);
  421. break;
  422. case -EPIPE:
  423. set_bit(EVENT_RX_STALL, &acm->flags);
  424. schedule_work(&acm->work);
  425. return;
  426. case -ENOENT:
  427. case -ECONNRESET:
  428. case -ESHUTDOWN:
  429. dev_dbg(&acm->data->dev,
  430. "%s - urb shutting down with status: %d\n",
  431. __func__, status);
  432. return;
  433. default:
  434. dev_dbg(&acm->data->dev,
  435. "%s - nonzero urb status received: %d\n",
  436. __func__, status);
  437. break;
  438. }
  439. /*
  440. * Unthrottle may run on another CPU which needs to see events
  441. * in the same order. Submission has an implict barrier
  442. */
  443. smp_mb__before_atomic();
  444. /* throttle device if requested by tty */
  445. spin_lock_irqsave(&acm->read_lock, flags);
  446. acm->throttled = acm->throttle_req;
  447. if (!acm->throttled) {
  448. spin_unlock_irqrestore(&acm->read_lock, flags);
  449. acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
  450. } else {
  451. spin_unlock_irqrestore(&acm->read_lock, flags);
  452. }
  453. }
  454. /* data interface wrote those outgoing bytes */
  455. static void acm_write_bulk(struct urb *urb)
  456. {
  457. struct acm_wb *wb = urb->context;
  458. struct acm *acm = wb->instance;
  459. unsigned long flags;
  460. int status = urb->status;
  461. if (status || (urb->actual_length != urb->transfer_buffer_length))
  462. dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
  463. urb->actual_length,
  464. urb->transfer_buffer_length,
  465. status);
  466. spin_lock_irqsave(&acm->write_lock, flags);
  467. acm_write_done(acm, wb);
  468. spin_unlock_irqrestore(&acm->write_lock, flags);
  469. set_bit(EVENT_TTY_WAKEUP, &acm->flags);
  470. schedule_work(&acm->work);
  471. }
  472. static void acm_softint(struct work_struct *work)
  473. {
  474. int i;
  475. struct acm *acm = container_of(work, struct acm, work);
  476. if (test_bit(EVENT_RX_STALL, &acm->flags)) {
  477. if (!(usb_autopm_get_interface(acm->data))) {
  478. for (i = 0; i < acm->rx_buflimit; i++)
  479. usb_kill_urb(acm->read_urbs[i]);
  480. usb_clear_halt(acm->dev, acm->in);
  481. acm_submit_read_urbs(acm, GFP_KERNEL);
  482. usb_autopm_put_interface(acm->data);
  483. }
  484. clear_bit(EVENT_RX_STALL, &acm->flags);
  485. }
  486. if (test_bit(EVENT_TTY_WAKEUP, &acm->flags)) {
  487. tty_port_tty_wakeup(&acm->port);
  488. clear_bit(EVENT_TTY_WAKEUP, &acm->flags);
  489. }
  490. }
  491. /*
  492. * TTY handlers
  493. */
  494. static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  495. {
  496. struct acm *acm;
  497. int retval;
  498. acm = acm_get_by_minor(tty->index);
  499. if (!acm)
  500. return -ENODEV;
  501. retval = tty_standard_install(driver, tty);
  502. if (retval)
  503. goto error_init_termios;
  504. tty->driver_data = acm;
  505. return 0;
  506. error_init_termios:
  507. tty_port_put(&acm->port);
  508. return retval;
  509. }
  510. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  511. {
  512. struct acm *acm = tty->driver_data;
  513. return tty_port_open(&acm->port, tty, filp);
  514. }
  515. static void acm_port_dtr_rts(struct tty_port *port, int raise)
  516. {
  517. struct acm *acm = container_of(port, struct acm, port);
  518. int val;
  519. int res;
  520. if (raise)
  521. val = ACM_CTRL_DTR | ACM_CTRL_RTS;
  522. else
  523. val = 0;
  524. /* FIXME: add missing ctrlout locking throughout driver */
  525. acm->ctrlout = val;
  526. res = acm_set_control(acm, val);
  527. if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
  528. dev_err(&acm->control->dev, "failed to set dtr/rts\n");
  529. }
  530. static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
  531. {
  532. struct acm *acm = container_of(port, struct acm, port);
  533. int retval = -ENODEV;
  534. int i;
  535. mutex_lock(&acm->mutex);
  536. if (acm->disconnected)
  537. goto disconnected;
  538. retval = usb_autopm_get_interface(acm->control);
  539. if (retval)
  540. goto error_get_interface;
  541. /*
  542. * FIXME: Why do we need this? Allocating 64K of physically contiguous
  543. * memory is really nasty...
  544. */
  545. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  546. acm->control->needs_remote_wakeup = 1;
  547. acm->ctrlurb->dev = acm->dev;
  548. retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
  549. if (retval) {
  550. dev_err(&acm->control->dev,
  551. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  552. goto error_submit_urb;
  553. }
  554. acm_tty_set_termios(tty, NULL);
  555. /*
  556. * Unthrottle device in case the TTY was closed while throttled.
  557. */
  558. spin_lock_irq(&acm->read_lock);
  559. acm->throttled = 0;
  560. acm->throttle_req = 0;
  561. spin_unlock_irq(&acm->read_lock);
  562. retval = acm_submit_read_urbs(acm, GFP_KERNEL);
  563. if (retval)
  564. goto error_submit_read_urbs;
  565. usb_autopm_put_interface(acm->control);
  566. mutex_unlock(&acm->mutex);
  567. return 0;
  568. error_submit_read_urbs:
  569. for (i = 0; i < acm->rx_buflimit; i++)
  570. usb_kill_urb(acm->read_urbs[i]);
  571. usb_kill_urb(acm->ctrlurb);
  572. error_submit_urb:
  573. usb_autopm_put_interface(acm->control);
  574. error_get_interface:
  575. disconnected:
  576. mutex_unlock(&acm->mutex);
  577. return usb_translate_errors(retval);
  578. }
  579. static void acm_port_destruct(struct tty_port *port)
  580. {
  581. struct acm *acm = container_of(port, struct acm, port);
  582. acm_release_minor(acm);
  583. usb_put_intf(acm->control);
  584. kfree(acm->country_codes);
  585. kfree(acm);
  586. }
  587. static void acm_port_shutdown(struct tty_port *port)
  588. {
  589. struct acm *acm = container_of(port, struct acm, port);
  590. struct urb *urb;
  591. struct acm_wb *wb;
  592. /*
  593. * Need to grab write_lock to prevent race with resume, but no need to
  594. * hold it due to the tty-port initialised flag.
  595. */
  596. spin_lock_irq(&acm->write_lock);
  597. spin_unlock_irq(&acm->write_lock);
  598. usb_autopm_get_interface_no_resume(acm->control);
  599. acm->control->needs_remote_wakeup = 0;
  600. usb_autopm_put_interface(acm->control);
  601. for (;;) {
  602. urb = usb_get_from_anchor(&acm->delayed);
  603. if (!urb)
  604. break;
  605. wb = urb->context;
  606. wb->use = 0;
  607. usb_autopm_put_interface_async(acm->control);
  608. }
  609. acm_kill_urbs(acm);
  610. }
  611. static void acm_tty_cleanup(struct tty_struct *tty)
  612. {
  613. struct acm *acm = tty->driver_data;
  614. tty_port_put(&acm->port);
  615. }
  616. static void acm_tty_hangup(struct tty_struct *tty)
  617. {
  618. struct acm *acm = tty->driver_data;
  619. tty_port_hangup(&acm->port);
  620. }
  621. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  622. {
  623. struct acm *acm = tty->driver_data;
  624. tty_port_close(&acm->port, tty, filp);
  625. }
  626. static int acm_tty_write(struct tty_struct *tty,
  627. const unsigned char *buf, int count)
  628. {
  629. struct acm *acm = tty->driver_data;
  630. int stat;
  631. unsigned long flags;
  632. int wbn;
  633. struct acm_wb *wb;
  634. if (!count)
  635. return 0;
  636. dev_vdbg(&acm->data->dev, "%d bytes from tty layer\n", count);
  637. spin_lock_irqsave(&acm->write_lock, flags);
  638. wbn = acm_wb_alloc(acm);
  639. if (wbn < 0) {
  640. spin_unlock_irqrestore(&acm->write_lock, flags);
  641. return 0;
  642. }
  643. wb = &acm->wb[wbn];
  644. if (!acm->dev) {
  645. wb->use = 0;
  646. spin_unlock_irqrestore(&acm->write_lock, flags);
  647. return -ENODEV;
  648. }
  649. count = (count > acm->writesize) ? acm->writesize : count;
  650. dev_vdbg(&acm->data->dev, "writing %d bytes\n", count);
  651. memcpy(wb->buf, buf, count);
  652. wb->len = count;
  653. stat = usb_autopm_get_interface_async(acm->control);
  654. if (stat) {
  655. wb->use = 0;
  656. spin_unlock_irqrestore(&acm->write_lock, flags);
  657. return stat;
  658. }
  659. if (acm->susp_count) {
  660. if (acm->putbuffer) {
  661. /* now to preserve order */
  662. usb_anchor_urb(acm->putbuffer->urb, &acm->delayed);
  663. acm->putbuffer = NULL;
  664. }
  665. usb_anchor_urb(wb->urb, &acm->delayed);
  666. spin_unlock_irqrestore(&acm->write_lock, flags);
  667. return count;
  668. } else {
  669. if (acm->putbuffer) {
  670. /* at this point there is no good way to handle errors */
  671. acm_start_wb(acm, acm->putbuffer);
  672. acm->putbuffer = NULL;
  673. }
  674. }
  675. stat = acm_start_wb(acm, wb);
  676. spin_unlock_irqrestore(&acm->write_lock, flags);
  677. if (stat < 0)
  678. return stat;
  679. return count;
  680. }
  681. static void acm_tty_flush_chars(struct tty_struct *tty)
  682. {
  683. struct acm *acm = tty->driver_data;
  684. struct acm_wb *cur = acm->putbuffer;
  685. int err;
  686. unsigned long flags;
  687. if (!cur) /* nothing to do */
  688. return;
  689. acm->putbuffer = NULL;
  690. err = usb_autopm_get_interface_async(acm->control);
  691. spin_lock_irqsave(&acm->write_lock, flags);
  692. if (err < 0) {
  693. cur->use = 0;
  694. acm->putbuffer = cur;
  695. goto out;
  696. }
  697. if (acm->susp_count)
  698. usb_anchor_urb(cur->urb, &acm->delayed);
  699. else
  700. acm_start_wb(acm, cur);
  701. out:
  702. spin_unlock_irqrestore(&acm->write_lock, flags);
  703. return;
  704. }
  705. static int acm_tty_put_char(struct tty_struct *tty, unsigned char ch)
  706. {
  707. struct acm *acm = tty->driver_data;
  708. struct acm_wb *cur;
  709. int wbn;
  710. unsigned long flags;
  711. overflow:
  712. cur = acm->putbuffer;
  713. if (!cur) {
  714. spin_lock_irqsave(&acm->write_lock, flags);
  715. wbn = acm_wb_alloc(acm);
  716. if (wbn >= 0) {
  717. cur = &acm->wb[wbn];
  718. acm->putbuffer = cur;
  719. }
  720. spin_unlock_irqrestore(&acm->write_lock, flags);
  721. if (!cur)
  722. return 0;
  723. }
  724. if (cur->len == acm->writesize) {
  725. acm_tty_flush_chars(tty);
  726. goto overflow;
  727. }
  728. cur->buf[cur->len++] = ch;
  729. return 1;
  730. }
  731. static int acm_tty_write_room(struct tty_struct *tty)
  732. {
  733. struct acm *acm = tty->driver_data;
  734. /*
  735. * Do not let the line discipline to know that we have a reserve,
  736. * or it might get too enthusiastic.
  737. */
  738. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  739. }
  740. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  741. {
  742. struct acm *acm = tty->driver_data;
  743. /*
  744. * if the device was unplugged then any remaining characters fell out
  745. * of the connector ;)
  746. */
  747. if (acm->disconnected)
  748. return 0;
  749. /*
  750. * This is inaccurate (overcounts), but it works.
  751. */
  752. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  753. }
  754. static void acm_tty_throttle(struct tty_struct *tty)
  755. {
  756. struct acm *acm = tty->driver_data;
  757. spin_lock_irq(&acm->read_lock);
  758. acm->throttle_req = 1;
  759. spin_unlock_irq(&acm->read_lock);
  760. }
  761. static void acm_tty_unthrottle(struct tty_struct *tty)
  762. {
  763. struct acm *acm = tty->driver_data;
  764. unsigned int was_throttled;
  765. spin_lock_irq(&acm->read_lock);
  766. was_throttled = acm->throttled;
  767. acm->throttled = 0;
  768. acm->throttle_req = 0;
  769. spin_unlock_irq(&acm->read_lock);
  770. if (was_throttled)
  771. acm_submit_read_urbs(acm, GFP_KERNEL);
  772. }
  773. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  774. {
  775. struct acm *acm = tty->driver_data;
  776. int retval;
  777. retval = acm_send_break(acm, state ? 0xffff : 0);
  778. if (retval < 0)
  779. dev_dbg(&acm->control->dev,
  780. "%s - send break failed\n", __func__);
  781. return retval;
  782. }
  783. static int acm_tty_tiocmget(struct tty_struct *tty)
  784. {
  785. struct acm *acm = tty->driver_data;
  786. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  787. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  788. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  789. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  790. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  791. TIOCM_CTS;
  792. }
  793. static int acm_tty_tiocmset(struct tty_struct *tty,
  794. unsigned int set, unsigned int clear)
  795. {
  796. struct acm *acm = tty->driver_data;
  797. unsigned int newctrl;
  798. newctrl = acm->ctrlout;
  799. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  800. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  801. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  802. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  803. newctrl = (newctrl & ~clear) | set;
  804. if (acm->ctrlout == newctrl)
  805. return 0;
  806. return acm_set_control(acm, acm->ctrlout = newctrl);
  807. }
  808. static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
  809. {
  810. struct serial_struct tmp;
  811. memset(&tmp, 0, sizeof(tmp));
  812. tmp.xmit_fifo_size = acm->writesize;
  813. tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
  814. tmp.close_delay = acm->port.close_delay / 10;
  815. tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  816. ASYNC_CLOSING_WAIT_NONE :
  817. acm->port.closing_wait / 10;
  818. if (copy_to_user(info, &tmp, sizeof(tmp)))
  819. return -EFAULT;
  820. else
  821. return 0;
  822. }
  823. static int set_serial_info(struct acm *acm,
  824. struct serial_struct __user *newinfo)
  825. {
  826. struct serial_struct new_serial;
  827. unsigned int closing_wait, close_delay;
  828. int retval = 0;
  829. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  830. return -EFAULT;
  831. close_delay = new_serial.close_delay * 10;
  832. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  833. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  834. mutex_lock(&acm->port.mutex);
  835. if (!capable(CAP_SYS_ADMIN)) {
  836. if ((close_delay != acm->port.close_delay) ||
  837. (closing_wait != acm->port.closing_wait))
  838. retval = -EPERM;
  839. else
  840. retval = -EOPNOTSUPP;
  841. } else {
  842. acm->port.close_delay = close_delay;
  843. acm->port.closing_wait = closing_wait;
  844. }
  845. mutex_unlock(&acm->port.mutex);
  846. return retval;
  847. }
  848. static int wait_serial_change(struct acm *acm, unsigned long arg)
  849. {
  850. int rv = 0;
  851. DECLARE_WAITQUEUE(wait, current);
  852. struct async_icount old, new;
  853. do {
  854. spin_lock_irq(&acm->read_lock);
  855. old = acm->oldcount;
  856. new = acm->iocount;
  857. acm->oldcount = new;
  858. spin_unlock_irq(&acm->read_lock);
  859. if ((arg & TIOCM_DSR) &&
  860. old.dsr != new.dsr)
  861. break;
  862. if ((arg & TIOCM_CD) &&
  863. old.dcd != new.dcd)
  864. break;
  865. if ((arg & TIOCM_RI) &&
  866. old.rng != new.rng)
  867. break;
  868. add_wait_queue(&acm->wioctl, &wait);
  869. set_current_state(TASK_INTERRUPTIBLE);
  870. schedule();
  871. remove_wait_queue(&acm->wioctl, &wait);
  872. if (acm->disconnected) {
  873. if (arg & TIOCM_CD)
  874. break;
  875. else
  876. rv = -ENODEV;
  877. } else {
  878. if (signal_pending(current))
  879. rv = -ERESTARTSYS;
  880. }
  881. } while (!rv);
  882. return rv;
  883. }
  884. static int acm_tty_get_icount(struct tty_struct *tty,
  885. struct serial_icounter_struct *icount)
  886. {
  887. struct acm *acm = tty->driver_data;
  888. icount->dsr = acm->iocount.dsr;
  889. icount->rng = acm->iocount.rng;
  890. icount->dcd = acm->iocount.dcd;
  891. icount->frame = acm->iocount.frame;
  892. icount->overrun = acm->iocount.overrun;
  893. icount->parity = acm->iocount.parity;
  894. icount->brk = acm->iocount.brk;
  895. return 0;
  896. }
  897. static int acm_tty_ioctl(struct tty_struct *tty,
  898. unsigned int cmd, unsigned long arg)
  899. {
  900. struct acm *acm = tty->driver_data;
  901. int rv = -ENOIOCTLCMD;
  902. switch (cmd) {
  903. case TIOCGSERIAL: /* gets serial port data */
  904. rv = get_serial_info(acm, (struct serial_struct __user *) arg);
  905. break;
  906. case TIOCSSERIAL:
  907. rv = set_serial_info(acm, (struct serial_struct __user *) arg);
  908. break;
  909. case TIOCMIWAIT:
  910. rv = usb_autopm_get_interface(acm->control);
  911. if (rv < 0) {
  912. rv = -EIO;
  913. break;
  914. }
  915. rv = wait_serial_change(acm, arg);
  916. usb_autopm_put_interface(acm->control);
  917. break;
  918. }
  919. return rv;
  920. }
  921. static void acm_tty_set_termios(struct tty_struct *tty,
  922. struct ktermios *termios_old)
  923. {
  924. struct acm *acm = tty->driver_data;
  925. struct ktermios *termios = &tty->termios;
  926. struct usb_cdc_line_coding newline;
  927. int newctrl = acm->ctrlout;
  928. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  929. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  930. newline.bParityType = termios->c_cflag & PARENB ?
  931. (termios->c_cflag & PARODD ? 1 : 2) +
  932. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  933. switch (termios->c_cflag & CSIZE) {
  934. case CS5:
  935. newline.bDataBits = 5;
  936. break;
  937. case CS6:
  938. newline.bDataBits = 6;
  939. break;
  940. case CS7:
  941. newline.bDataBits = 7;
  942. break;
  943. case CS8:
  944. default:
  945. newline.bDataBits = 8;
  946. break;
  947. }
  948. /* FIXME: Needs to clear unsupported bits in the termios */
  949. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  950. if (C_BAUD(tty) == B0) {
  951. newline.dwDTERate = acm->line.dwDTERate;
  952. newctrl &= ~ACM_CTRL_DTR;
  953. } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
  954. newctrl |= ACM_CTRL_DTR;
  955. }
  956. if (newctrl != acm->ctrlout)
  957. acm_set_control(acm, acm->ctrlout = newctrl);
  958. if (memcmp(&acm->line, &newline, sizeof newline)) {
  959. memcpy(&acm->line, &newline, sizeof newline);
  960. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  961. __func__,
  962. le32_to_cpu(newline.dwDTERate),
  963. newline.bCharFormat, newline.bParityType,
  964. newline.bDataBits);
  965. acm_set_line(acm, &acm->line);
  966. }
  967. }
  968. static const struct tty_port_operations acm_port_ops = {
  969. .dtr_rts = acm_port_dtr_rts,
  970. .shutdown = acm_port_shutdown,
  971. .activate = acm_port_activate,
  972. .destruct = acm_port_destruct,
  973. };
  974. /*
  975. * USB probe and disconnect routines.
  976. */
  977. /* Little helpers: write/read buffers free */
  978. static void acm_write_buffers_free(struct acm *acm)
  979. {
  980. int i;
  981. struct acm_wb *wb;
  982. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  983. usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
  984. }
  985. static void acm_read_buffers_free(struct acm *acm)
  986. {
  987. int i;
  988. for (i = 0; i < acm->rx_buflimit; i++)
  989. usb_free_coherent(acm->dev, acm->readsize,
  990. acm->read_buffers[i].base, acm->read_buffers[i].dma);
  991. }
  992. /* Little helper: write buffers allocate */
  993. static int acm_write_buffers_alloc(struct acm *acm)
  994. {
  995. int i;
  996. struct acm_wb *wb;
  997. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  998. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  999. &wb->dmah);
  1000. if (!wb->buf) {
  1001. while (i != 0) {
  1002. --i;
  1003. --wb;
  1004. usb_free_coherent(acm->dev, acm->writesize,
  1005. wb->buf, wb->dmah);
  1006. }
  1007. return -ENOMEM;
  1008. }
  1009. }
  1010. return 0;
  1011. }
  1012. static int acm_probe(struct usb_interface *intf,
  1013. const struct usb_device_id *id)
  1014. {
  1015. struct usb_cdc_union_desc *union_header = NULL;
  1016. struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
  1017. unsigned char *buffer = intf->altsetting->extra;
  1018. int buflen = intf->altsetting->extralen;
  1019. struct usb_interface *control_interface;
  1020. struct usb_interface *data_interface;
  1021. struct usb_endpoint_descriptor *epctrl = NULL;
  1022. struct usb_endpoint_descriptor *epread = NULL;
  1023. struct usb_endpoint_descriptor *epwrite = NULL;
  1024. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1025. struct usb_cdc_parsed_header h;
  1026. struct acm *acm;
  1027. int minor;
  1028. int ctrlsize, readsize;
  1029. u8 *buf;
  1030. int call_intf_num = -1;
  1031. int data_intf_num = -1;
  1032. unsigned long quirks;
  1033. int num_rx_buf;
  1034. int i;
  1035. int combined_interfaces = 0;
  1036. struct device *tty_dev;
  1037. int rv = -ENOMEM;
  1038. int res;
  1039. /* normal quirks */
  1040. quirks = (unsigned long)id->driver_info;
  1041. if (quirks == IGNORE_DEVICE)
  1042. return -ENODEV;
  1043. memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
  1044. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  1045. /* handle quirks deadly to normal probing*/
  1046. if (quirks == NO_UNION_NORMAL) {
  1047. data_interface = usb_ifnum_to_if(usb_dev, 1);
  1048. control_interface = usb_ifnum_to_if(usb_dev, 0);
  1049. /* we would crash */
  1050. if (!data_interface || !control_interface)
  1051. return -ENODEV;
  1052. goto skip_normal_probe;
  1053. }
  1054. /* normal probing*/
  1055. if (!buffer) {
  1056. dev_err(&intf->dev, "Weird descriptor references\n");
  1057. return -EINVAL;
  1058. }
  1059. if (!intf->cur_altsetting)
  1060. return -EINVAL;
  1061. if (!buflen) {
  1062. if (intf->cur_altsetting->endpoint &&
  1063. intf->cur_altsetting->endpoint->extralen &&
  1064. intf->cur_altsetting->endpoint->extra) {
  1065. dev_dbg(&intf->dev,
  1066. "Seeking extra descriptors on endpoint\n");
  1067. buflen = intf->cur_altsetting->endpoint->extralen;
  1068. buffer = intf->cur_altsetting->endpoint->extra;
  1069. } else {
  1070. dev_err(&intf->dev,
  1071. "Zero length descriptor references\n");
  1072. return -EINVAL;
  1073. }
  1074. }
  1075. cdc_parse_cdc_header(&h, intf, buffer, buflen);
  1076. union_header = h.usb_cdc_union_desc;
  1077. cmgmd = h.usb_cdc_call_mgmt_descriptor;
  1078. if (cmgmd)
  1079. call_intf_num = cmgmd->bDataInterface;
  1080. if (!union_header) {
  1081. if (call_intf_num > 0) {
  1082. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  1083. /* quirks for Droids MuIn LCD */
  1084. if (quirks & NO_DATA_INTERFACE) {
  1085. data_interface = usb_ifnum_to_if(usb_dev, 0);
  1086. } else {
  1087. data_intf_num = call_intf_num;
  1088. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1089. }
  1090. control_interface = intf;
  1091. } else {
  1092. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  1093. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  1094. return -ENODEV;
  1095. } else {
  1096. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  1097. combined_interfaces = 1;
  1098. control_interface = data_interface = intf;
  1099. goto look_for_collapsed_interface;
  1100. }
  1101. }
  1102. } else {
  1103. data_intf_num = union_header->bSlaveInterface0;
  1104. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  1105. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1106. }
  1107. if (!control_interface || !data_interface) {
  1108. dev_dbg(&intf->dev, "no interfaces\n");
  1109. return -ENODEV;
  1110. }
  1111. if (!data_interface->cur_altsetting || !control_interface->cur_altsetting)
  1112. return -ENODEV;
  1113. if (data_intf_num != call_intf_num)
  1114. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  1115. if (control_interface == data_interface) {
  1116. /* some broken devices designed for windows work this way */
  1117. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  1118. combined_interfaces = 1;
  1119. /* a popular other OS doesn't use it */
  1120. quirks |= NO_CAP_LINE;
  1121. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  1122. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  1123. return -EINVAL;
  1124. }
  1125. look_for_collapsed_interface:
  1126. res = usb_find_common_endpoints(data_interface->cur_altsetting,
  1127. &epread, &epwrite, &epctrl, NULL);
  1128. if (res)
  1129. return res;
  1130. goto made_compressed_probe;
  1131. }
  1132. skip_normal_probe:
  1133. /*workaround for switched interfaces */
  1134. if (data_interface->cur_altsetting->desc.bInterfaceClass
  1135. != CDC_DATA_INTERFACE_TYPE) {
  1136. if (control_interface->cur_altsetting->desc.bInterfaceClass
  1137. == CDC_DATA_INTERFACE_TYPE) {
  1138. dev_dbg(&intf->dev,
  1139. "Your device has switched interfaces.\n");
  1140. swap(control_interface, data_interface);
  1141. } else {
  1142. return -EINVAL;
  1143. }
  1144. }
  1145. /* Accept probe requests only for the control interface */
  1146. if (!combined_interfaces && intf != control_interface)
  1147. return -ENODEV;
  1148. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  1149. /* valid in this context */
  1150. dev_dbg(&intf->dev, "The data interface isn't available\n");
  1151. return -EBUSY;
  1152. }
  1153. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
  1154. control_interface->cur_altsetting->desc.bNumEndpoints == 0)
  1155. return -EINVAL;
  1156. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  1157. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  1158. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  1159. /* workaround for switched endpoints */
  1160. if (!usb_endpoint_dir_in(epread)) {
  1161. /* descriptors are swapped */
  1162. dev_dbg(&intf->dev,
  1163. "The data interface has switched endpoints\n");
  1164. swap(epread, epwrite);
  1165. }
  1166. made_compressed_probe:
  1167. dev_dbg(&intf->dev, "interfaces are valid\n");
  1168. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  1169. if (acm == NULL)
  1170. goto alloc_fail;
  1171. minor = acm_alloc_minor(acm);
  1172. if (minor < 0)
  1173. goto alloc_fail1;
  1174. ctrlsize = usb_endpoint_maxp(epctrl);
  1175. readsize = usb_endpoint_maxp(epread) *
  1176. (quirks == SINGLE_RX_URB ? 1 : 2);
  1177. acm->combined_interfaces = combined_interfaces;
  1178. acm->writesize = usb_endpoint_maxp(epwrite) * 20;
  1179. acm->control = control_interface;
  1180. acm->data = data_interface;
  1181. acm->minor = minor;
  1182. acm->dev = usb_dev;
  1183. if (h.usb_cdc_acm_descriptor)
  1184. acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
  1185. if (quirks & NO_CAP_LINE)
  1186. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1187. acm->ctrlsize = ctrlsize;
  1188. acm->readsize = readsize;
  1189. acm->rx_buflimit = num_rx_buf;
  1190. INIT_WORK(&acm->work, acm_softint);
  1191. init_waitqueue_head(&acm->wioctl);
  1192. spin_lock_init(&acm->write_lock);
  1193. spin_lock_init(&acm->read_lock);
  1194. mutex_init(&acm->mutex);
  1195. if (usb_endpoint_xfer_int(epread)) {
  1196. acm->bInterval = epread->bInterval;
  1197. acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
  1198. } else {
  1199. acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  1200. }
  1201. if (usb_endpoint_xfer_int(epwrite))
  1202. acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
  1203. else
  1204. acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
  1205. tty_port_init(&acm->port);
  1206. acm->port.ops = &acm_port_ops;
  1207. init_usb_anchor(&acm->delayed);
  1208. acm->quirks = quirks;
  1209. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1210. if (!buf)
  1211. goto alloc_fail2;
  1212. acm->ctrl_buffer = buf;
  1213. if (acm_write_buffers_alloc(acm) < 0)
  1214. goto alloc_fail4;
  1215. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1216. if (!acm->ctrlurb)
  1217. goto alloc_fail5;
  1218. for (i = 0; i < num_rx_buf; i++) {
  1219. struct acm_rb *rb = &(acm->read_buffers[i]);
  1220. struct urb *urb;
  1221. rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
  1222. &rb->dma);
  1223. if (!rb->base)
  1224. goto alloc_fail6;
  1225. rb->index = i;
  1226. rb->instance = acm;
  1227. urb = usb_alloc_urb(0, GFP_KERNEL);
  1228. if (!urb)
  1229. goto alloc_fail6;
  1230. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1231. urb->transfer_dma = rb->dma;
  1232. if (usb_endpoint_xfer_int(epread))
  1233. usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
  1234. acm->readsize,
  1235. acm_read_bulk_callback, rb,
  1236. acm->bInterval);
  1237. else
  1238. usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
  1239. acm->readsize,
  1240. acm_read_bulk_callback, rb);
  1241. acm->read_urbs[i] = urb;
  1242. __set_bit(i, &acm->read_urbs_free);
  1243. }
  1244. for (i = 0; i < ACM_NW; i++) {
  1245. struct acm_wb *snd = &(acm->wb[i]);
  1246. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1247. if (snd->urb == NULL)
  1248. goto alloc_fail7;
  1249. if (usb_endpoint_xfer_int(epwrite))
  1250. usb_fill_int_urb(snd->urb, usb_dev, acm->out,
  1251. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1252. else
  1253. usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
  1254. NULL, acm->writesize, acm_write_bulk, snd);
  1255. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1256. if (quirks & SEND_ZERO_PACKET)
  1257. snd->urb->transfer_flags |= URB_ZERO_PACKET;
  1258. snd->instance = acm;
  1259. }
  1260. usb_set_intfdata(intf, acm);
  1261. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1262. if (i < 0)
  1263. goto alloc_fail7;
  1264. if (h.usb_cdc_country_functional_desc) { /* export the country data */
  1265. struct usb_cdc_country_functional_desc * cfd =
  1266. h.usb_cdc_country_functional_desc;
  1267. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1268. if (!acm->country_codes)
  1269. goto skip_countries;
  1270. acm->country_code_size = cfd->bLength - 4;
  1271. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1272. cfd->bLength - 4);
  1273. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1274. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1275. if (i < 0) {
  1276. kfree(acm->country_codes);
  1277. acm->country_codes = NULL;
  1278. acm->country_code_size = 0;
  1279. goto skip_countries;
  1280. }
  1281. i = device_create_file(&intf->dev,
  1282. &dev_attr_iCountryCodeRelDate);
  1283. if (i < 0) {
  1284. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1285. kfree(acm->country_codes);
  1286. acm->country_codes = NULL;
  1287. acm->country_code_size = 0;
  1288. goto skip_countries;
  1289. }
  1290. }
  1291. skip_countries:
  1292. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1293. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1294. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1295. /* works around buggy devices */
  1296. epctrl->bInterval ? epctrl->bInterval : 16);
  1297. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1298. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1299. acm->notification_buffer = NULL;
  1300. acm->nb_index = 0;
  1301. acm->nb_size = 0;
  1302. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1303. acm->line.dwDTERate = cpu_to_le32(9600);
  1304. acm->line.bDataBits = 8;
  1305. acm_set_line(acm, &acm->line);
  1306. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1307. usb_set_intfdata(data_interface, acm);
  1308. usb_get_intf(control_interface);
  1309. tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
  1310. &control_interface->dev);
  1311. if (IS_ERR(tty_dev)) {
  1312. rv = PTR_ERR(tty_dev);
  1313. goto alloc_fail8;
  1314. }
  1315. if (quirks & CLEAR_HALT_CONDITIONS) {
  1316. usb_clear_halt(usb_dev, acm->in);
  1317. usb_clear_halt(usb_dev, acm->out);
  1318. }
  1319. return 0;
  1320. alloc_fail8:
  1321. if (acm->country_codes) {
  1322. device_remove_file(&acm->control->dev,
  1323. &dev_attr_wCountryCodes);
  1324. device_remove_file(&acm->control->dev,
  1325. &dev_attr_iCountryCodeRelDate);
  1326. kfree(acm->country_codes);
  1327. }
  1328. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1329. alloc_fail7:
  1330. usb_set_intfdata(intf, NULL);
  1331. for (i = 0; i < ACM_NW; i++)
  1332. usb_free_urb(acm->wb[i].urb);
  1333. alloc_fail6:
  1334. for (i = 0; i < num_rx_buf; i++)
  1335. usb_free_urb(acm->read_urbs[i]);
  1336. acm_read_buffers_free(acm);
  1337. usb_free_urb(acm->ctrlurb);
  1338. alloc_fail5:
  1339. acm_write_buffers_free(acm);
  1340. alloc_fail4:
  1341. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1342. alloc_fail2:
  1343. acm_release_minor(acm);
  1344. alloc_fail1:
  1345. kfree(acm);
  1346. alloc_fail:
  1347. return rv;
  1348. }
  1349. static void acm_disconnect(struct usb_interface *intf)
  1350. {
  1351. struct acm *acm = usb_get_intfdata(intf);
  1352. struct tty_struct *tty;
  1353. /* sibling interface is already cleaning up */
  1354. if (!acm)
  1355. return;
  1356. mutex_lock(&acm->mutex);
  1357. acm->disconnected = true;
  1358. if (acm->country_codes) {
  1359. device_remove_file(&acm->control->dev,
  1360. &dev_attr_wCountryCodes);
  1361. device_remove_file(&acm->control->dev,
  1362. &dev_attr_iCountryCodeRelDate);
  1363. }
  1364. wake_up_all(&acm->wioctl);
  1365. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1366. usb_set_intfdata(acm->control, NULL);
  1367. usb_set_intfdata(acm->data, NULL);
  1368. mutex_unlock(&acm->mutex);
  1369. tty = tty_port_tty_get(&acm->port);
  1370. if (tty) {
  1371. tty_vhangup(tty);
  1372. tty_kref_put(tty);
  1373. }
  1374. acm_kill_urbs(acm);
  1375. cancel_work_sync(&acm->work);
  1376. tty_unregister_device(acm_tty_driver, acm->minor);
  1377. acm_write_buffers_free(acm);
  1378. usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1379. acm_read_buffers_free(acm);
  1380. kfree(acm->notification_buffer);
  1381. if (!acm->combined_interfaces)
  1382. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1383. acm->data : acm->control);
  1384. tty_port_put(&acm->port);
  1385. }
  1386. #ifdef CONFIG_PM
  1387. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1388. {
  1389. struct acm *acm = usb_get_intfdata(intf);
  1390. int cnt;
  1391. spin_lock_irq(&acm->write_lock);
  1392. if (PMSG_IS_AUTO(message)) {
  1393. if (acm->transmitting) {
  1394. spin_unlock_irq(&acm->write_lock);
  1395. return -EBUSY;
  1396. }
  1397. }
  1398. cnt = acm->susp_count++;
  1399. spin_unlock_irq(&acm->write_lock);
  1400. if (cnt)
  1401. return 0;
  1402. acm_kill_urbs(acm);
  1403. cancel_work_sync(&acm->work);
  1404. return 0;
  1405. }
  1406. static int acm_resume(struct usb_interface *intf)
  1407. {
  1408. struct acm *acm = usb_get_intfdata(intf);
  1409. struct urb *urb;
  1410. int rv = 0;
  1411. spin_lock_irq(&acm->write_lock);
  1412. if (--acm->susp_count)
  1413. goto out;
  1414. if (tty_port_initialized(&acm->port)) {
  1415. rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
  1416. for (;;) {
  1417. urb = usb_get_from_anchor(&acm->delayed);
  1418. if (!urb)
  1419. break;
  1420. acm_start_wb(acm, urb->context);
  1421. }
  1422. /*
  1423. * delayed error checking because we must
  1424. * do the write path at all cost
  1425. */
  1426. if (rv < 0)
  1427. goto out;
  1428. rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
  1429. }
  1430. out:
  1431. spin_unlock_irq(&acm->write_lock);
  1432. return rv;
  1433. }
  1434. static int acm_reset_resume(struct usb_interface *intf)
  1435. {
  1436. struct acm *acm = usb_get_intfdata(intf);
  1437. if (tty_port_initialized(&acm->port))
  1438. tty_port_tty_hangup(&acm->port, false);
  1439. return acm_resume(intf);
  1440. }
  1441. #endif /* CONFIG_PM */
  1442. static int acm_pre_reset(struct usb_interface *intf)
  1443. {
  1444. struct acm *acm = usb_get_intfdata(intf);
  1445. clear_bit(EVENT_RX_STALL, &acm->flags);
  1446. return 0;
  1447. }
  1448. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1449. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1450. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1451. USB_CDC_ACM_PROTO_VENDOR)
  1452. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1453. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1454. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1455. USB_CDC_ACM_PROTO_VENDOR)
  1456. /*
  1457. * USB driver structure.
  1458. */
  1459. static const struct usb_device_id acm_ids[] = {
  1460. /* quirky and broken devices */
  1461. { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
  1462. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1463. { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
  1464. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1465. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1466. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1467. },
  1468. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1469. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1470. },
  1471. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1472. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1473. },
  1474. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1475. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1476. },
  1477. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1478. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1479. },
  1480. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1481. .driver_info = SINGLE_RX_URB,
  1482. },
  1483. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1484. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1485. },
  1486. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1487. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1488. },
  1489. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1490. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1491. },
  1492. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1493. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1494. },
  1495. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1496. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1497. },
  1498. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1499. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1500. },
  1501. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1502. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1503. },
  1504. { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
  1505. .driver_info = QUIRK_CONTROL_LINE_STATE, },
  1506. { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
  1507. { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
  1508. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1509. },
  1510. /* Motorola H24 HSPA module: */
  1511. { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
  1512. { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
  1513. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1514. },
  1515. { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
  1516. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1517. },
  1518. { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
  1519. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1520. },
  1521. { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
  1522. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1523. },
  1524. { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
  1525. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1526. },
  1527. { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
  1528. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1529. },
  1530. { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
  1531. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1532. },
  1533. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1534. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1535. data interface instead of
  1536. communications interface.
  1537. Maybe we should define a new
  1538. quirk for this. */
  1539. },
  1540. { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
  1541. .driver_info = NO_UNION_NORMAL,
  1542. },
  1543. { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
  1544. .driver_info = NO_UNION_NORMAL,
  1545. },
  1546. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1547. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1548. },
  1549. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1550. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1551. },
  1552. { USB_DEVICE(0xfff0, 0x0100), /* DATECS FP-2000 */
  1553. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1554. },
  1555. { USB_DEVICE(0x09d8, 0x0320), /* Elatec GmbH TWN3 */
  1556. .driver_info = NO_UNION_NORMAL, /* has misplaced union descriptor */
  1557. },
  1558. { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
  1559. .driver_info = CLEAR_HALT_CONDITIONS,
  1560. },
  1561. /* Nokia S60 phones expose two ACM channels. The first is
  1562. * a modem and is picked up by the standard AT-command
  1563. * information below. The second is 'vendor-specific' but
  1564. * is treated as a serial device at the S60 end, so we want
  1565. * to expose it on Linux too. */
  1566. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1567. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1568. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1569. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1570. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1571. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1572. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1573. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1574. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1575. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1576. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1577. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1578. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1579. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1580. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1581. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1582. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1583. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1584. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1585. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1586. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1587. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1588. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1589. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1590. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1591. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1592. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1593. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1594. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1595. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1596. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1597. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1598. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1599. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1600. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1601. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1602. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1603. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1604. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1605. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1606. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1607. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1608. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1609. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1610. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1611. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1612. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1613. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1614. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1615. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1616. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1617. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1618. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1619. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1620. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1621. { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
  1622. { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
  1623. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1624. /* Support for Owen devices */
  1625. { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
  1626. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1627. /* Support for Droids MuIn LCD */
  1628. { USB_DEVICE(0x04d8, 0x000b),
  1629. .driver_info = NO_DATA_INTERFACE,
  1630. },
  1631. #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
  1632. { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
  1633. .driver_info = IGNORE_DEVICE,
  1634. },
  1635. { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
  1636. .driver_info = IGNORE_DEVICE,
  1637. },
  1638. #endif
  1639. /*Samsung phone in firmware update mode */
  1640. { USB_DEVICE(0x04e8, 0x685d),
  1641. .driver_info = IGNORE_DEVICE,
  1642. },
  1643. /* Exclude Infineon Flash Loader utility */
  1644. { USB_DEVICE(0x058b, 0x0041),
  1645. .driver_info = IGNORE_DEVICE,
  1646. },
  1647. /* control interfaces without any protocol set */
  1648. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1649. USB_CDC_PROTO_NONE) },
  1650. /* control interfaces with various AT-command sets */
  1651. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1652. USB_CDC_ACM_PROTO_AT_V25TER) },
  1653. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1654. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1655. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1656. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1657. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1658. USB_CDC_ACM_PROTO_AT_GSM) },
  1659. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1660. USB_CDC_ACM_PROTO_AT_3G) },
  1661. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1662. USB_CDC_ACM_PROTO_AT_CDMA) },
  1663. { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
  1664. .driver_info = SEND_ZERO_PACKET,
  1665. },
  1666. { }
  1667. };
  1668. MODULE_DEVICE_TABLE(usb, acm_ids);
  1669. static struct usb_driver acm_driver = {
  1670. .name = "cdc_acm",
  1671. .probe = acm_probe,
  1672. .disconnect = acm_disconnect,
  1673. #ifdef CONFIG_PM
  1674. .suspend = acm_suspend,
  1675. .resume = acm_resume,
  1676. .reset_resume = acm_reset_resume,
  1677. #endif
  1678. .pre_reset = acm_pre_reset,
  1679. .id_table = acm_ids,
  1680. #ifdef CONFIG_PM
  1681. .supports_autosuspend = 1,
  1682. #endif
  1683. .disable_hub_initiated_lpm = 1,
  1684. };
  1685. /*
  1686. * TTY driver structures.
  1687. */
  1688. static const struct tty_operations acm_ops = {
  1689. .install = acm_tty_install,
  1690. .open = acm_tty_open,
  1691. .close = acm_tty_close,
  1692. .cleanup = acm_tty_cleanup,
  1693. .hangup = acm_tty_hangup,
  1694. .write = acm_tty_write,
  1695. .put_char = acm_tty_put_char,
  1696. .flush_chars = acm_tty_flush_chars,
  1697. .write_room = acm_tty_write_room,
  1698. .ioctl = acm_tty_ioctl,
  1699. .throttle = acm_tty_throttle,
  1700. .unthrottle = acm_tty_unthrottle,
  1701. .chars_in_buffer = acm_tty_chars_in_buffer,
  1702. .break_ctl = acm_tty_break_ctl,
  1703. .set_termios = acm_tty_set_termios,
  1704. .tiocmget = acm_tty_tiocmget,
  1705. .tiocmset = acm_tty_tiocmset,
  1706. .get_icount = acm_tty_get_icount,
  1707. };
  1708. /*
  1709. * Init / exit.
  1710. */
  1711. static int __init acm_init(void)
  1712. {
  1713. int retval;
  1714. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1715. if (!acm_tty_driver)
  1716. return -ENOMEM;
  1717. acm_tty_driver->driver_name = "acm",
  1718. acm_tty_driver->name = "ttyACM",
  1719. acm_tty_driver->major = ACM_TTY_MAJOR,
  1720. acm_tty_driver->minor_start = 0,
  1721. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1722. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1723. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1724. acm_tty_driver->init_termios = tty_std_termios;
  1725. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1726. HUPCL | CLOCAL;
  1727. tty_set_operations(acm_tty_driver, &acm_ops);
  1728. retval = tty_register_driver(acm_tty_driver);
  1729. if (retval) {
  1730. put_tty_driver(acm_tty_driver);
  1731. return retval;
  1732. }
  1733. retval = usb_register(&acm_driver);
  1734. if (retval) {
  1735. tty_unregister_driver(acm_tty_driver);
  1736. put_tty_driver(acm_tty_driver);
  1737. return retval;
  1738. }
  1739. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1740. return 0;
  1741. }
  1742. static void __exit acm_exit(void)
  1743. {
  1744. usb_deregister(&acm_driver);
  1745. tty_unregister_driver(acm_tty_driver);
  1746. put_tty_driver(acm_tty_driver);
  1747. idr_destroy(&acm_minors);
  1748. }
  1749. module_init(acm_init);
  1750. module_exit(acm_exit);
  1751. MODULE_AUTHOR(DRIVER_AUTHOR);
  1752. MODULE_DESCRIPTION(DRIVER_DESC);
  1753. MODULE_LICENSE("GPL");
  1754. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);