hso.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define DRIVER_VERSION "1.2"
  73. #define MOD_AUTHOR "Option Wireless"
  74. #define MOD_DESCRIPTION "USB High Speed Option driver"
  75. #define MOD_LICENSE "GPL"
  76. #define HSO_MAX_NET_DEVICES 10
  77. #define HSO__MAX_MTU 2048
  78. #define DEFAULT_MTU 1500
  79. #define DEFAULT_MRU 1500
  80. #define CTRL_URB_RX_SIZE 1024
  81. #define CTRL_URB_TX_SIZE 64
  82. #define BULK_URB_RX_SIZE 4096
  83. #define BULK_URB_TX_SIZE 8192
  84. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  86. #define MUX_BULK_RX_BUF_COUNT 4
  87. #define USB_TYPE_OPTION_VENDOR 0x20
  88. /* These definitions are used with the struct hso_net flags element */
  89. /* - use *_bit operations on it. (bit indices not values.) */
  90. #define HSO_NET_RUNNING 0
  91. #define HSO_NET_TX_TIMEOUT (HZ*10)
  92. #define HSO_SERIAL_MAGIC 0x48534f31
  93. /* Number of ttys to handle */
  94. #define HSO_SERIAL_TTY_MINORS 256
  95. #define MAX_RX_URBS 2
  96. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  97. {
  98. if (tty)
  99. return tty->driver_data;
  100. return NULL;
  101. }
  102. /*****************************************************************************/
  103. /* Debugging functions */
  104. /*****************************************************************************/
  105. #define D__(lvl_, fmt, arg...) \
  106. do { \
  107. printk(lvl_ "[%d:%s]: " fmt "\n", \
  108. __LINE__, __func__, ## arg); \
  109. } while (0)
  110. #define D_(lvl, args...) \
  111. do { \
  112. if (lvl & debug) \
  113. D__(KERN_INFO, args); \
  114. } while (0)
  115. #define D1(args...) D_(0x01, ##args)
  116. #define D2(args...) D_(0x02, ##args)
  117. #define D3(args...) D_(0x04, ##args)
  118. #define D4(args...) D_(0x08, ##args)
  119. #define D5(args...) D_(0x10, ##args)
  120. /*****************************************************************************/
  121. /* Enumerators */
  122. /*****************************************************************************/
  123. enum pkt_parse_state {
  124. WAIT_IP,
  125. WAIT_DATA,
  126. WAIT_SYNC
  127. };
  128. /*****************************************************************************/
  129. /* Structs */
  130. /*****************************************************************************/
  131. struct hso_shared_int {
  132. struct usb_endpoint_descriptor *intr_endp;
  133. void *shared_intr_buf;
  134. struct urb *shared_intr_urb;
  135. struct usb_device *usb;
  136. int use_count;
  137. int ref_count;
  138. struct mutex shared_int_lock;
  139. };
  140. struct hso_net {
  141. struct hso_device *parent;
  142. struct net_device *net;
  143. struct rfkill *rfkill;
  144. struct usb_endpoint_descriptor *in_endp;
  145. struct usb_endpoint_descriptor *out_endp;
  146. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  147. struct urb *mux_bulk_tx_urb;
  148. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  149. void *mux_bulk_tx_buf;
  150. struct sk_buff *skb_rx_buf;
  151. struct sk_buff *skb_tx_buf;
  152. enum pkt_parse_state rx_parse_state;
  153. spinlock_t net_lock;
  154. unsigned short rx_buf_size;
  155. unsigned short rx_buf_missing;
  156. struct iphdr rx_ip_hdr;
  157. unsigned long flags;
  158. };
  159. enum rx_ctrl_state{
  160. RX_IDLE,
  161. RX_SENT,
  162. RX_PENDING
  163. };
  164. #define BM_REQUEST_TYPE (0xa1)
  165. #define B_NOTIFICATION (0x20)
  166. #define W_VALUE (0x0)
  167. #define W_INDEX (0x2)
  168. #define W_LENGTH (0x2)
  169. #define B_OVERRUN (0x1<<6)
  170. #define B_PARITY (0x1<<5)
  171. #define B_FRAMING (0x1<<4)
  172. #define B_RING_SIGNAL (0x1<<3)
  173. #define B_BREAK (0x1<<2)
  174. #define B_TX_CARRIER (0x1<<1)
  175. #define B_RX_CARRIER (0x1<<0)
  176. struct hso_serial_state_notification {
  177. u8 bmRequestType;
  178. u8 bNotification;
  179. u16 wValue;
  180. u16 wIndex;
  181. u16 wLength;
  182. u16 UART_state_bitmap;
  183. } __attribute__((packed));
  184. struct hso_tiocmget {
  185. struct mutex mutex;
  186. wait_queue_head_t waitq;
  187. int intr_completed;
  188. struct usb_endpoint_descriptor *endp;
  189. struct urb *urb;
  190. struct hso_serial_state_notification serial_state_notification;
  191. u16 prev_UART_state_bitmap;
  192. struct uart_icount icount;
  193. };
  194. struct hso_serial {
  195. struct hso_device *parent;
  196. int magic;
  197. u8 minor;
  198. struct hso_shared_int *shared_int;
  199. /* rx/tx urb could be either a bulk urb or a control urb depending
  200. on which serial port it is used on. */
  201. struct urb *rx_urb[MAX_RX_URBS];
  202. u8 num_rx_urbs;
  203. u8 *rx_data[MAX_RX_URBS];
  204. u16 rx_data_length; /* should contain allocated length */
  205. struct urb *tx_urb;
  206. u8 *tx_data;
  207. u8 *tx_buffer;
  208. u16 tx_data_length; /* should contain allocated length */
  209. u16 tx_data_count;
  210. u16 tx_buffer_count;
  211. struct usb_ctrlrequest ctrl_req_tx;
  212. struct usb_ctrlrequest ctrl_req_rx;
  213. struct usb_endpoint_descriptor *in_endp;
  214. struct usb_endpoint_descriptor *out_endp;
  215. enum rx_ctrl_state rx_state;
  216. u8 rts_state;
  217. u8 dtr_state;
  218. unsigned tx_urb_used:1;
  219. /* from usb_serial_port */
  220. struct tty_struct *tty;
  221. int open_count;
  222. spinlock_t serial_lock;
  223. int (*write_data) (struct hso_serial *serial);
  224. struct hso_tiocmget *tiocmget;
  225. /* Hacks required to get flow control
  226. * working on the serial receive buffers
  227. * so as not to drop characters on the floor.
  228. */
  229. int curr_rx_urb_idx;
  230. u16 curr_rx_urb_offset;
  231. u8 rx_urb_filled[MAX_RX_URBS];
  232. struct tasklet_struct unthrottle_tasklet;
  233. struct work_struct retry_unthrottle_workqueue;
  234. };
  235. struct hso_device {
  236. union {
  237. struct hso_serial *dev_serial;
  238. struct hso_net *dev_net;
  239. } port_data;
  240. u32 port_spec;
  241. u8 is_active;
  242. u8 usb_gone;
  243. struct work_struct async_get_intf;
  244. struct work_struct async_put_intf;
  245. struct work_struct reset_device;
  246. struct usb_device *usb;
  247. struct usb_interface *interface;
  248. struct device *dev;
  249. struct kref ref;
  250. struct mutex mutex;
  251. };
  252. /* Type of interface */
  253. #define HSO_INTF_MASK 0xFF00
  254. #define HSO_INTF_MUX 0x0100
  255. #define HSO_INTF_BULK 0x0200
  256. /* Type of port */
  257. #define HSO_PORT_MASK 0xFF
  258. #define HSO_PORT_NO_PORT 0x0
  259. #define HSO_PORT_CONTROL 0x1
  260. #define HSO_PORT_APP 0x2
  261. #define HSO_PORT_GPS 0x3
  262. #define HSO_PORT_PCSC 0x4
  263. #define HSO_PORT_APP2 0x5
  264. #define HSO_PORT_GPS_CONTROL 0x6
  265. #define HSO_PORT_MSD 0x7
  266. #define HSO_PORT_VOICE 0x8
  267. #define HSO_PORT_DIAG2 0x9
  268. #define HSO_PORT_DIAG 0x10
  269. #define HSO_PORT_MODEM 0x11
  270. #define HSO_PORT_NETWORK 0x12
  271. /* Additional device info */
  272. #define HSO_INFO_MASK 0xFF000000
  273. #define HSO_INFO_CRC_BUG 0x01000000
  274. /*****************************************************************************/
  275. /* Prototypes */
  276. /*****************************************************************************/
  277. /* Serial driver functions */
  278. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  279. unsigned int set, unsigned int clear);
  280. static void ctrl_callback(struct urb *urb);
  281. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  282. static void hso_kick_transmit(struct hso_serial *serial);
  283. /* Helper functions */
  284. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  285. struct usb_device *usb, gfp_t gfp);
  286. static void handle_usb_error(int status, const char *function,
  287. struct hso_device *hso_dev);
  288. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  289. int type, int dir);
  290. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  291. static void hso_free_interface(struct usb_interface *intf);
  292. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  293. static int hso_stop_serial_device(struct hso_device *hso_dev);
  294. static int hso_start_net_device(struct hso_device *hso_dev);
  295. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  296. static int hso_stop_net_device(struct hso_device *hso_dev);
  297. static void hso_serial_ref_free(struct kref *ref);
  298. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  299. static int hso_mux_serial_read(struct hso_serial *serial);
  300. static void async_get_intf(struct work_struct *data);
  301. static void async_put_intf(struct work_struct *data);
  302. static int hso_put_activity(struct hso_device *hso_dev);
  303. static int hso_get_activity(struct hso_device *hso_dev);
  304. static void tiocmget_intr_callback(struct urb *urb);
  305. static void reset_device(struct work_struct *data);
  306. /*****************************************************************************/
  307. /* Helping functions */
  308. /*****************************************************************************/
  309. /* #define DEBUG */
  310. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  311. {
  312. return hso_dev->port_data.dev_net;
  313. }
  314. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  315. {
  316. return hso_dev->port_data.dev_serial;
  317. }
  318. /* Debugging functions */
  319. #ifdef DEBUG
  320. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  321. unsigned int len)
  322. {
  323. static char name[255];
  324. sprintf(name, "hso[%d:%s]", line_count, func_name);
  325. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  326. }
  327. #define DUMP(buf_, len_) \
  328. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  329. #define DUMP1(buf_, len_) \
  330. do { \
  331. if (0x01 & debug) \
  332. DUMP(buf_, len_); \
  333. } while (0)
  334. #else
  335. #define DUMP(buf_, len_)
  336. #define DUMP1(buf_, len_)
  337. #endif
  338. /* module parameters */
  339. static int debug;
  340. static int tty_major;
  341. static int disable_net;
  342. /* driver info */
  343. static const char driver_name[] = "hso";
  344. static const char tty_filename[] = "ttyHS";
  345. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  346. /* the usb driver itself (registered in hso_init) */
  347. static struct usb_driver hso_driver;
  348. /* serial structures */
  349. static struct tty_driver *tty_drv;
  350. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  351. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  352. static spinlock_t serial_table_lock;
  353. static const s32 default_port_spec[] = {
  354. HSO_INTF_MUX | HSO_PORT_NETWORK,
  355. HSO_INTF_BULK | HSO_PORT_DIAG,
  356. HSO_INTF_BULK | HSO_PORT_MODEM,
  357. 0
  358. };
  359. static const s32 icon321_port_spec[] = {
  360. HSO_INTF_MUX | HSO_PORT_NETWORK,
  361. HSO_INTF_BULK | HSO_PORT_DIAG2,
  362. HSO_INTF_BULK | HSO_PORT_MODEM,
  363. HSO_INTF_BULK | HSO_PORT_DIAG,
  364. 0
  365. };
  366. #define default_port_device(vendor, product) \
  367. USB_DEVICE(vendor, product), \
  368. .driver_info = (kernel_ulong_t)default_port_spec
  369. #define icon321_port_device(vendor, product) \
  370. USB_DEVICE(vendor, product), \
  371. .driver_info = (kernel_ulong_t)icon321_port_spec
  372. /* list of devices we support */
  373. static const struct usb_device_id hso_ids[] = {
  374. {default_port_device(0x0af0, 0x6711)},
  375. {default_port_device(0x0af0, 0x6731)},
  376. {default_port_device(0x0af0, 0x6751)},
  377. {default_port_device(0x0af0, 0x6771)},
  378. {default_port_device(0x0af0, 0x6791)},
  379. {default_port_device(0x0af0, 0x6811)},
  380. {default_port_device(0x0af0, 0x6911)},
  381. {default_port_device(0x0af0, 0x6951)},
  382. {default_port_device(0x0af0, 0x6971)},
  383. {default_port_device(0x0af0, 0x7011)},
  384. {default_port_device(0x0af0, 0x7031)},
  385. {default_port_device(0x0af0, 0x7051)},
  386. {default_port_device(0x0af0, 0x7071)},
  387. {default_port_device(0x0af0, 0x7111)},
  388. {default_port_device(0x0af0, 0x7211)},
  389. {default_port_device(0x0af0, 0x7251)},
  390. {default_port_device(0x0af0, 0x7271)},
  391. {default_port_device(0x0af0, 0x7311)},
  392. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  393. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  394. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  395. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  396. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  397. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  399. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  400. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  401. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  402. {USB_DEVICE(0x0af0, 0x7701)},
  403. {USB_DEVICE(0x0af0, 0x7706)},
  404. {USB_DEVICE(0x0af0, 0x7801)},
  405. {USB_DEVICE(0x0af0, 0x7901)},
  406. {USB_DEVICE(0x0af0, 0x7A01)},
  407. {USB_DEVICE(0x0af0, 0x7A05)},
  408. {USB_DEVICE(0x0af0, 0x8200)},
  409. {USB_DEVICE(0x0af0, 0x8201)},
  410. {USB_DEVICE(0x0af0, 0x8300)},
  411. {USB_DEVICE(0x0af0, 0x8302)},
  412. {USB_DEVICE(0x0af0, 0x8304)},
  413. {USB_DEVICE(0x0af0, 0x8400)},
  414. {USB_DEVICE(0x0af0, 0x8600)},
  415. {USB_DEVICE(0x0af0, 0x8800)},
  416. {USB_DEVICE(0x0af0, 0x8900)},
  417. {USB_DEVICE(0x0af0, 0xd035)},
  418. {USB_DEVICE(0x0af0, 0xd055)},
  419. {USB_DEVICE(0x0af0, 0xd155)},
  420. {USB_DEVICE(0x0af0, 0xd255)},
  421. {USB_DEVICE(0x0af0, 0xd057)},
  422. {USB_DEVICE(0x0af0, 0xd157)},
  423. {USB_DEVICE(0x0af0, 0xd257)},
  424. {USB_DEVICE(0x0af0, 0xd357)},
  425. {USB_DEVICE(0x0af0, 0xd058)},
  426. {USB_DEVICE(0x0af0, 0xc100)},
  427. {}
  428. };
  429. MODULE_DEVICE_TABLE(usb, hso_ids);
  430. /* Sysfs attribute */
  431. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. struct hso_device *hso_dev = dev_get_drvdata(dev);
  436. char *port_name;
  437. if (!hso_dev)
  438. return 0;
  439. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  440. case HSO_PORT_CONTROL:
  441. port_name = "Control";
  442. break;
  443. case HSO_PORT_APP:
  444. port_name = "Application";
  445. break;
  446. case HSO_PORT_APP2:
  447. port_name = "Application2";
  448. break;
  449. case HSO_PORT_GPS:
  450. port_name = "GPS";
  451. break;
  452. case HSO_PORT_GPS_CONTROL:
  453. port_name = "GPS Control";
  454. break;
  455. case HSO_PORT_PCSC:
  456. port_name = "PCSC";
  457. break;
  458. case HSO_PORT_DIAG:
  459. port_name = "Diagnostic";
  460. break;
  461. case HSO_PORT_DIAG2:
  462. port_name = "Diagnostic2";
  463. break;
  464. case HSO_PORT_MODEM:
  465. port_name = "Modem";
  466. break;
  467. case HSO_PORT_NETWORK:
  468. port_name = "Network";
  469. break;
  470. default:
  471. port_name = "Unknown";
  472. break;
  473. }
  474. return sprintf(buf, "%s\n", port_name);
  475. }
  476. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  477. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  478. {
  479. int idx;
  480. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  481. if (serial->rx_urb[idx] == urb)
  482. return idx;
  483. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  484. return -1;
  485. }
  486. /* converts mux value to a port spec value */
  487. static u32 hso_mux_to_port(int mux)
  488. {
  489. u32 result;
  490. switch (mux) {
  491. case 0x1:
  492. result = HSO_PORT_CONTROL;
  493. break;
  494. case 0x2:
  495. result = HSO_PORT_APP;
  496. break;
  497. case 0x4:
  498. result = HSO_PORT_PCSC;
  499. break;
  500. case 0x8:
  501. result = HSO_PORT_GPS;
  502. break;
  503. case 0x10:
  504. result = HSO_PORT_APP2;
  505. break;
  506. default:
  507. result = HSO_PORT_NO_PORT;
  508. }
  509. return result;
  510. }
  511. /* converts port spec value to a mux value */
  512. static u32 hso_port_to_mux(int port)
  513. {
  514. u32 result;
  515. switch (port & HSO_PORT_MASK) {
  516. case HSO_PORT_CONTROL:
  517. result = 0x0;
  518. break;
  519. case HSO_PORT_APP:
  520. result = 0x1;
  521. break;
  522. case HSO_PORT_PCSC:
  523. result = 0x2;
  524. break;
  525. case HSO_PORT_GPS:
  526. result = 0x3;
  527. break;
  528. case HSO_PORT_APP2:
  529. result = 0x4;
  530. break;
  531. default:
  532. result = 0x0;
  533. }
  534. return result;
  535. }
  536. static struct hso_serial *get_serial_by_shared_int_and_type(
  537. struct hso_shared_int *shared_int,
  538. int mux)
  539. {
  540. int i, port;
  541. port = hso_mux_to_port(mux);
  542. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  543. if (serial_table[i] &&
  544. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  545. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  546. return dev2ser(serial_table[i]);
  547. }
  548. }
  549. return NULL;
  550. }
  551. static struct hso_serial *get_serial_by_index(unsigned index)
  552. {
  553. struct hso_serial *serial = NULL;
  554. unsigned long flags;
  555. spin_lock_irqsave(&serial_table_lock, flags);
  556. if (serial_table[index])
  557. serial = dev2ser(serial_table[index]);
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. return serial;
  560. }
  561. static int get_free_serial_index(void)
  562. {
  563. int index;
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  567. if (serial_table[index] == NULL) {
  568. spin_unlock_irqrestore(&serial_table_lock, flags);
  569. return index;
  570. }
  571. }
  572. spin_unlock_irqrestore(&serial_table_lock, flags);
  573. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  574. return -1;
  575. }
  576. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&serial_table_lock, flags);
  580. if (serial)
  581. serial_table[index] = serial->parent;
  582. else
  583. serial_table[index] = NULL;
  584. spin_unlock_irqrestore(&serial_table_lock, flags);
  585. }
  586. static void handle_usb_error(int status, const char *function,
  587. struct hso_device *hso_dev)
  588. {
  589. char *explanation;
  590. switch (status) {
  591. case -ENODEV:
  592. explanation = "no device";
  593. break;
  594. case -ENOENT:
  595. explanation = "endpoint not enabled";
  596. break;
  597. case -EPIPE:
  598. explanation = "endpoint stalled";
  599. break;
  600. case -ENOSPC:
  601. explanation = "not enough bandwidth";
  602. break;
  603. case -ESHUTDOWN:
  604. explanation = "device disabled";
  605. break;
  606. case -EHOSTUNREACH:
  607. explanation = "device suspended";
  608. break;
  609. case -EINVAL:
  610. case -EAGAIN:
  611. case -EFBIG:
  612. case -EMSGSIZE:
  613. explanation = "internal error";
  614. break;
  615. case -EILSEQ:
  616. case -EPROTO:
  617. case -ETIME:
  618. case -ETIMEDOUT:
  619. explanation = "protocol error";
  620. if (hso_dev)
  621. schedule_work(&hso_dev->reset_device);
  622. break;
  623. default:
  624. explanation = "unknown status";
  625. break;
  626. }
  627. /* log a meaningful explanation of an USB status */
  628. D1("%s: received USB status - %s (%d)", function, explanation, status);
  629. }
  630. /* Network interface functions */
  631. /* called when net interface is brought up by ifconfig */
  632. static int hso_net_open(struct net_device *net)
  633. {
  634. struct hso_net *odev = netdev_priv(net);
  635. unsigned long flags = 0;
  636. if (!odev) {
  637. dev_err(&net->dev, "No net device !\n");
  638. return -ENODEV;
  639. }
  640. odev->skb_tx_buf = NULL;
  641. /* setup environment */
  642. spin_lock_irqsave(&odev->net_lock, flags);
  643. odev->rx_parse_state = WAIT_IP;
  644. odev->rx_buf_size = 0;
  645. odev->rx_buf_missing = sizeof(struct iphdr);
  646. spin_unlock_irqrestore(&odev->net_lock, flags);
  647. /* We are up and running. */
  648. set_bit(HSO_NET_RUNNING, &odev->flags);
  649. hso_start_net_device(odev->parent);
  650. /* Tell the kernel we are ready to start receiving from it */
  651. netif_start_queue(net);
  652. return 0;
  653. }
  654. /* called when interface is brought down by ifconfig */
  655. static int hso_net_close(struct net_device *net)
  656. {
  657. struct hso_net *odev = netdev_priv(net);
  658. /* we don't need the queue anymore */
  659. netif_stop_queue(net);
  660. /* no longer running */
  661. clear_bit(HSO_NET_RUNNING, &odev->flags);
  662. hso_stop_net_device(odev->parent);
  663. /* done */
  664. return 0;
  665. }
  666. /* USB tells is xmit done, we should start the netqueue again */
  667. static void write_bulk_callback(struct urb *urb)
  668. {
  669. struct hso_net *odev = urb->context;
  670. int status = urb->status;
  671. /* Sanity check */
  672. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  673. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  674. return;
  675. }
  676. /* Do we still have a valid kernel network device? */
  677. if (!netif_device_present(odev->net)) {
  678. dev_err(&urb->dev->dev, "%s: net device not present\n",
  679. __func__);
  680. return;
  681. }
  682. /* log status, but don't act on it, we don't need to resubmit anything
  683. * anyhow */
  684. if (status)
  685. handle_usb_error(status, __func__, odev->parent);
  686. hso_put_activity(odev->parent);
  687. /* Tell the network interface we are ready for another frame */
  688. netif_wake_queue(odev->net);
  689. }
  690. /* called by kernel when we need to transmit a packet */
  691. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  692. struct net_device *net)
  693. {
  694. struct hso_net *odev = netdev_priv(net);
  695. int result;
  696. /* Tell the kernel, "No more frames 'til we are done with this one." */
  697. netif_stop_queue(net);
  698. if (hso_get_activity(odev->parent) == -EAGAIN) {
  699. odev->skb_tx_buf = skb;
  700. return NETDEV_TX_OK;
  701. }
  702. /* log if asked */
  703. DUMP1(skb->data, skb->len);
  704. /* Copy it from kernel memory to OUR memory */
  705. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  706. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  707. /* Fill in the URB for shipping it out. */
  708. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  709. odev->parent->usb,
  710. usb_sndbulkpipe(odev->parent->usb,
  711. odev->out_endp->
  712. bEndpointAddress & 0x7F),
  713. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  714. odev);
  715. /* Deal with the Zero Length packet problem, I hope */
  716. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  717. /* Send the URB on its merry way. */
  718. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  719. if (result) {
  720. dev_warn(&odev->parent->interface->dev,
  721. "failed mux_bulk_tx_urb %d\n", result);
  722. net->stats.tx_errors++;
  723. netif_start_queue(net);
  724. } else {
  725. net->stats.tx_packets++;
  726. net->stats.tx_bytes += skb->len;
  727. }
  728. dev_kfree_skb(skb);
  729. /* we're done */
  730. return NETDEV_TX_OK;
  731. }
  732. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  733. {
  734. struct hso_net *odev = netdev_priv(net);
  735. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  736. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  737. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  738. }
  739. static const struct ethtool_ops ops = {
  740. .get_drvinfo = hso_get_drvinfo,
  741. .get_link = ethtool_op_get_link
  742. };
  743. /* called when a packet did not ack after watchdogtimeout */
  744. static void hso_net_tx_timeout(struct net_device *net)
  745. {
  746. struct hso_net *odev = netdev_priv(net);
  747. if (!odev)
  748. return;
  749. /* Tell syslog we are hosed. */
  750. dev_warn(&net->dev, "Tx timed out.\n");
  751. /* Tear the waiting frame off the list */
  752. if (odev->mux_bulk_tx_urb &&
  753. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  754. usb_unlink_urb(odev->mux_bulk_tx_urb);
  755. /* Update statistics */
  756. net->stats.tx_errors++;
  757. }
  758. /* make a real packet from the received USB buffer */
  759. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  760. unsigned int count, unsigned char is_eop)
  761. {
  762. unsigned short temp_bytes;
  763. unsigned short buffer_offset = 0;
  764. unsigned short frame_len;
  765. unsigned char *tmp_rx_buf;
  766. /* log if needed */
  767. D1("Rx %d bytes", count);
  768. DUMP(ip_pkt, min(128, (int)count));
  769. while (count) {
  770. switch (odev->rx_parse_state) {
  771. case WAIT_IP:
  772. /* waiting for IP header. */
  773. /* wanted bytes - size of ip header */
  774. temp_bytes =
  775. (count <
  776. odev->rx_buf_missing) ? count : odev->
  777. rx_buf_missing;
  778. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  779. odev->rx_buf_size, ip_pkt + buffer_offset,
  780. temp_bytes);
  781. odev->rx_buf_size += temp_bytes;
  782. buffer_offset += temp_bytes;
  783. odev->rx_buf_missing -= temp_bytes;
  784. count -= temp_bytes;
  785. if (!odev->rx_buf_missing) {
  786. /* header is complete allocate an sk_buffer and
  787. * continue to WAIT_DATA */
  788. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  789. if ((frame_len > DEFAULT_MRU) ||
  790. (frame_len < sizeof(struct iphdr))) {
  791. dev_err(&odev->net->dev,
  792. "Invalid frame (%d) length\n",
  793. frame_len);
  794. odev->rx_parse_state = WAIT_SYNC;
  795. continue;
  796. }
  797. /* Allocate an sk_buff */
  798. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  799. frame_len);
  800. if (!odev->skb_rx_buf) {
  801. /* We got no receive buffer. */
  802. D1("could not allocate memory");
  803. odev->rx_parse_state = WAIT_SYNC;
  804. return;
  805. }
  806. /* Copy what we got so far. make room for iphdr
  807. * after tail. */
  808. tmp_rx_buf =
  809. skb_put(odev->skb_rx_buf,
  810. sizeof(struct iphdr));
  811. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  812. sizeof(struct iphdr));
  813. /* ETH_HLEN */
  814. odev->rx_buf_size = sizeof(struct iphdr);
  815. /* Filip actually use .tot_len */
  816. odev->rx_buf_missing =
  817. frame_len - sizeof(struct iphdr);
  818. odev->rx_parse_state = WAIT_DATA;
  819. }
  820. break;
  821. case WAIT_DATA:
  822. temp_bytes = (count < odev->rx_buf_missing)
  823. ? count : odev->rx_buf_missing;
  824. /* Copy the rest of the bytes that are left in the
  825. * buffer into the waiting sk_buf. */
  826. /* Make room for temp_bytes after tail. */
  827. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  828. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  829. odev->rx_buf_missing -= temp_bytes;
  830. count -= temp_bytes;
  831. buffer_offset += temp_bytes;
  832. odev->rx_buf_size += temp_bytes;
  833. if (!odev->rx_buf_missing) {
  834. /* Packet is complete. Inject into stack. */
  835. /* We have IP packet here */
  836. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  837. /* don't check it */
  838. odev->skb_rx_buf->ip_summed =
  839. CHECKSUM_UNNECESSARY;
  840. skb_reset_mac_header(odev->skb_rx_buf);
  841. /* Ship it off to the kernel */
  842. netif_rx(odev->skb_rx_buf);
  843. /* No longer our buffer. */
  844. odev->skb_rx_buf = NULL;
  845. /* update out statistics */
  846. odev->net->stats.rx_packets++;
  847. odev->net->stats.rx_bytes += odev->rx_buf_size;
  848. odev->rx_buf_size = 0;
  849. odev->rx_buf_missing = sizeof(struct iphdr);
  850. odev->rx_parse_state = WAIT_IP;
  851. }
  852. break;
  853. case WAIT_SYNC:
  854. D1(" W_S");
  855. count = 0;
  856. break;
  857. default:
  858. D1(" ");
  859. count--;
  860. break;
  861. }
  862. }
  863. /* Recovery mechanism for WAIT_SYNC state. */
  864. if (is_eop) {
  865. if (odev->rx_parse_state == WAIT_SYNC) {
  866. odev->rx_parse_state = WAIT_IP;
  867. odev->rx_buf_size = 0;
  868. odev->rx_buf_missing = sizeof(struct iphdr);
  869. }
  870. }
  871. }
  872. /* Moving data from usb to kernel (in interrupt state) */
  873. static void read_bulk_callback(struct urb *urb)
  874. {
  875. struct hso_net *odev = urb->context;
  876. struct net_device *net;
  877. int result;
  878. int status = urb->status;
  879. /* is al ok? (Filip: Who's Al ?) */
  880. if (status) {
  881. handle_usb_error(status, __func__, odev->parent);
  882. return;
  883. }
  884. /* Sanity check */
  885. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  886. D1("BULK IN callback but driver is not active!");
  887. return;
  888. }
  889. usb_mark_last_busy(urb->dev);
  890. net = odev->net;
  891. if (!netif_device_present(net)) {
  892. /* Somebody killed our network interface... */
  893. return;
  894. }
  895. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  896. u32 rest;
  897. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  898. rest = urb->actual_length %
  899. le16_to_cpu(odev->in_endp->wMaxPacketSize);
  900. if (((rest == 5) || (rest == 6)) &&
  901. !memcmp(((u8 *) urb->transfer_buffer) +
  902. urb->actual_length - 4, crc_check, 4)) {
  903. urb->actual_length -= 4;
  904. }
  905. }
  906. /* do we even have a packet? */
  907. if (urb->actual_length) {
  908. /* Handle the IP stream, add header and push it onto network
  909. * stack if the packet is complete. */
  910. spin_lock(&odev->net_lock);
  911. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  912. (urb->transfer_buffer_length >
  913. urb->actual_length) ? 1 : 0);
  914. spin_unlock(&odev->net_lock);
  915. }
  916. /* We are done with this URB, resubmit it. Prep the USB to wait for
  917. * another frame. Reuse same as received. */
  918. usb_fill_bulk_urb(urb,
  919. odev->parent->usb,
  920. usb_rcvbulkpipe(odev->parent->usb,
  921. odev->in_endp->
  922. bEndpointAddress & 0x7F),
  923. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  924. read_bulk_callback, odev);
  925. /* Give this to the USB subsystem so it can tell us when more data
  926. * arrives. */
  927. result = usb_submit_urb(urb, GFP_ATOMIC);
  928. if (result)
  929. dev_warn(&odev->parent->interface->dev,
  930. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  931. result);
  932. }
  933. /* Serial driver functions */
  934. static void hso_init_termios(struct ktermios *termios)
  935. {
  936. /*
  937. * The default requirements for this device are:
  938. */
  939. termios->c_iflag &=
  940. ~(IGNBRK /* disable ignore break */
  941. | BRKINT /* disable break causes interrupt */
  942. | PARMRK /* disable mark parity errors */
  943. | ISTRIP /* disable clear high bit of input characters */
  944. | INLCR /* disable translate NL to CR */
  945. | IGNCR /* disable ignore CR */
  946. | ICRNL /* disable translate CR to NL */
  947. | IXON); /* disable enable XON/XOFF flow control */
  948. /* disable postprocess output characters */
  949. termios->c_oflag &= ~OPOST;
  950. termios->c_lflag &=
  951. ~(ECHO /* disable echo input characters */
  952. | ECHONL /* disable echo new line */
  953. | ICANON /* disable erase, kill, werase, and rprnt
  954. special characters */
  955. | ISIG /* disable interrupt, quit, and suspend special
  956. characters */
  957. | IEXTEN); /* disable non-POSIX special characters */
  958. termios->c_cflag &=
  959. ~(CSIZE /* no size */
  960. | PARENB /* disable parity bit */
  961. | CBAUD /* clear current baud rate */
  962. | CBAUDEX); /* clear current buad rate */
  963. termios->c_cflag |= CS8; /* character size 8 bits */
  964. /* baud rate 115200 */
  965. tty_termios_encode_baud_rate(termios, 115200, 115200);
  966. }
  967. static void _hso_serial_set_termios(struct tty_struct *tty,
  968. struct ktermios *old)
  969. {
  970. struct hso_serial *serial = get_serial_by_tty(tty);
  971. struct ktermios *termios;
  972. if (!serial) {
  973. printk(KERN_ERR "%s: no tty structures", __func__);
  974. return;
  975. }
  976. D4("port %d", serial->minor);
  977. /*
  978. * Fix up unsupported bits
  979. */
  980. termios = tty->termios;
  981. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  982. termios->c_cflag &=
  983. ~(CSIZE /* no size */
  984. | PARENB /* disable parity bit */
  985. | CBAUD /* clear current baud rate */
  986. | CBAUDEX); /* clear current buad rate */
  987. termios->c_cflag |= CS8; /* character size 8 bits */
  988. /* baud rate 115200 */
  989. tty_encode_baud_rate(tty, 115200, 115200);
  990. }
  991. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  992. {
  993. int result;
  994. /* We are done with this URB, resubmit it. Prep the USB to wait for
  995. * another frame */
  996. usb_fill_bulk_urb(urb, serial->parent->usb,
  997. usb_rcvbulkpipe(serial->parent->usb,
  998. serial->in_endp->
  999. bEndpointAddress & 0x7F),
  1000. urb->transfer_buffer, serial->rx_data_length,
  1001. hso_std_serial_read_bulk_callback, serial);
  1002. /* Give this to the USB subsystem so it can tell us when more data
  1003. * arrives. */
  1004. result = usb_submit_urb(urb, GFP_ATOMIC);
  1005. if (result) {
  1006. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  1007. __func__, result);
  1008. }
  1009. }
  1010. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1011. {
  1012. int count;
  1013. struct urb *curr_urb;
  1014. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1015. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1016. count = put_rxbuf_data(curr_urb, serial);
  1017. if (count == -1)
  1018. return;
  1019. if (count == 0) {
  1020. serial->curr_rx_urb_idx++;
  1021. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1022. serial->curr_rx_urb_idx = 0;
  1023. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1024. }
  1025. }
  1026. }
  1027. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1028. {
  1029. int count = 0;
  1030. struct urb *urb;
  1031. urb = serial->rx_urb[0];
  1032. if (serial->open_count > 0) {
  1033. count = put_rxbuf_data(urb, serial);
  1034. if (count == -1)
  1035. return;
  1036. }
  1037. /* Re issue a read as long as we receive data. */
  1038. if (count == 0 && ((urb->actual_length != 0) ||
  1039. (serial->rx_state == RX_PENDING))) {
  1040. serial->rx_state = RX_SENT;
  1041. hso_mux_serial_read(serial);
  1042. } else
  1043. serial->rx_state = RX_IDLE;
  1044. }
  1045. /* read callback for Diag and CS port */
  1046. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1047. {
  1048. struct hso_serial *serial = urb->context;
  1049. int status = urb->status;
  1050. /* sanity check */
  1051. if (!serial) {
  1052. D1("serial == NULL");
  1053. return;
  1054. } else if (status) {
  1055. handle_usb_error(status, __func__, serial->parent);
  1056. return;
  1057. }
  1058. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1059. D1("Actual length = %d\n", urb->actual_length);
  1060. DUMP1(urb->transfer_buffer, urb->actual_length);
  1061. /* Anyone listening? */
  1062. if (serial->open_count == 0)
  1063. return;
  1064. if (status == 0) {
  1065. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1066. u32 rest;
  1067. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1068. rest =
  1069. urb->actual_length %
  1070. le16_to_cpu(serial->in_endp->wMaxPacketSize);
  1071. if (((rest == 5) || (rest == 6)) &&
  1072. !memcmp(((u8 *) urb->transfer_buffer) +
  1073. urb->actual_length - 4, crc_check, 4)) {
  1074. urb->actual_length -= 4;
  1075. }
  1076. }
  1077. /* Valid data, handle RX data */
  1078. spin_lock(&serial->serial_lock);
  1079. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1080. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1081. spin_unlock(&serial->serial_lock);
  1082. } else if (status == -ENOENT || status == -ECONNRESET) {
  1083. /* Unlinked - check for throttled port. */
  1084. D2("Port %d, successfully unlinked urb", serial->minor);
  1085. spin_lock(&serial->serial_lock);
  1086. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1087. hso_resubmit_rx_bulk_urb(serial, urb);
  1088. spin_unlock(&serial->serial_lock);
  1089. } else {
  1090. D2("Port %d, status = %d for read urb", serial->minor, status);
  1091. return;
  1092. }
  1093. }
  1094. /*
  1095. * This needs to be a tasklet otherwise we will
  1096. * end up recursively calling this function.
  1097. */
  1098. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1099. {
  1100. unsigned long flags;
  1101. spin_lock_irqsave(&serial->serial_lock, flags);
  1102. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1103. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1104. else
  1105. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1106. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1107. }
  1108. static void hso_unthrottle(struct tty_struct *tty)
  1109. {
  1110. struct hso_serial *serial = get_serial_by_tty(tty);
  1111. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1112. }
  1113. static void hso_unthrottle_workfunc(struct work_struct *work)
  1114. {
  1115. struct hso_serial *serial =
  1116. container_of(work, struct hso_serial,
  1117. retry_unthrottle_workqueue);
  1118. hso_unthrottle_tasklet(serial);
  1119. }
  1120. /* open the requested serial port */
  1121. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1122. {
  1123. struct hso_serial *serial = get_serial_by_index(tty->index);
  1124. int result;
  1125. /* sanity check */
  1126. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1127. WARN_ON(1);
  1128. tty->driver_data = NULL;
  1129. D1("Failed to open port");
  1130. return -ENODEV;
  1131. }
  1132. mutex_lock(&serial->parent->mutex);
  1133. result = usb_autopm_get_interface(serial->parent->interface);
  1134. if (result < 0)
  1135. goto err_out;
  1136. D1("Opening %d", serial->minor);
  1137. kref_get(&serial->parent->ref);
  1138. /* setup */
  1139. spin_lock_irq(&serial->serial_lock);
  1140. tty->driver_data = serial;
  1141. tty_kref_put(serial->tty);
  1142. serial->tty = tty_kref_get(tty);
  1143. spin_unlock_irq(&serial->serial_lock);
  1144. /* check for port already opened, if not set the termios */
  1145. serial->open_count++;
  1146. if (serial->open_count == 1) {
  1147. serial->rx_state = RX_IDLE;
  1148. /* Force default termio settings */
  1149. _hso_serial_set_termios(tty, NULL);
  1150. tasklet_init(&serial->unthrottle_tasklet,
  1151. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1152. (unsigned long)serial);
  1153. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1154. hso_unthrottle_workfunc);
  1155. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1156. if (result) {
  1157. hso_stop_serial_device(serial->parent);
  1158. serial->open_count--;
  1159. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1160. }
  1161. } else {
  1162. D1("Port was already open");
  1163. }
  1164. usb_autopm_put_interface(serial->parent->interface);
  1165. /* done */
  1166. if (result)
  1167. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1168. err_out:
  1169. mutex_unlock(&serial->parent->mutex);
  1170. return result;
  1171. }
  1172. /* close the requested serial port */
  1173. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1174. {
  1175. struct hso_serial *serial = tty->driver_data;
  1176. u8 usb_gone;
  1177. D1("Closing serial port");
  1178. /* Open failed, no close cleanup required */
  1179. if (serial == NULL)
  1180. return;
  1181. mutex_lock(&serial->parent->mutex);
  1182. usb_gone = serial->parent->usb_gone;
  1183. if (!usb_gone)
  1184. usb_autopm_get_interface(serial->parent->interface);
  1185. /* reset the rts and dtr */
  1186. /* do the actual close */
  1187. serial->open_count--;
  1188. if (serial->open_count <= 0) {
  1189. serial->open_count = 0;
  1190. spin_lock_irq(&serial->serial_lock);
  1191. if (serial->tty == tty) {
  1192. serial->tty->driver_data = NULL;
  1193. serial->tty = NULL;
  1194. tty_kref_put(tty);
  1195. }
  1196. spin_unlock_irq(&serial->serial_lock);
  1197. if (!usb_gone)
  1198. hso_stop_serial_device(serial->parent);
  1199. tasklet_kill(&serial->unthrottle_tasklet);
  1200. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1201. }
  1202. if (!usb_gone)
  1203. usb_autopm_put_interface(serial->parent->interface);
  1204. mutex_unlock(&serial->parent->mutex);
  1205. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1206. }
  1207. /* close the requested serial port */
  1208. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1209. int count)
  1210. {
  1211. struct hso_serial *serial = get_serial_by_tty(tty);
  1212. int space, tx_bytes;
  1213. unsigned long flags;
  1214. /* sanity check */
  1215. if (serial == NULL) {
  1216. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1217. return -ENODEV;
  1218. }
  1219. spin_lock_irqsave(&serial->serial_lock, flags);
  1220. space = serial->tx_data_length - serial->tx_buffer_count;
  1221. tx_bytes = (count < space) ? count : space;
  1222. if (!tx_bytes)
  1223. goto out;
  1224. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1225. serial->tx_buffer_count += tx_bytes;
  1226. out:
  1227. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1228. hso_kick_transmit(serial);
  1229. /* done */
  1230. return tx_bytes;
  1231. }
  1232. /* how much room is there for writing */
  1233. static int hso_serial_write_room(struct tty_struct *tty)
  1234. {
  1235. struct hso_serial *serial = get_serial_by_tty(tty);
  1236. int room;
  1237. unsigned long flags;
  1238. spin_lock_irqsave(&serial->serial_lock, flags);
  1239. room = serial->tx_data_length - serial->tx_buffer_count;
  1240. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1241. /* return free room */
  1242. return room;
  1243. }
  1244. /* setup the term */
  1245. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1246. {
  1247. struct hso_serial *serial = get_serial_by_tty(tty);
  1248. unsigned long flags;
  1249. if (old)
  1250. D5("Termios called with: cflags new[%d] - old[%d]",
  1251. tty->termios->c_cflag, old->c_cflag);
  1252. /* the actual setup */
  1253. spin_lock_irqsave(&serial->serial_lock, flags);
  1254. if (serial->open_count)
  1255. _hso_serial_set_termios(tty, old);
  1256. else
  1257. tty->termios = old;
  1258. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1259. /* done */
  1260. }
  1261. /* how many characters in the buffer */
  1262. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1263. {
  1264. struct hso_serial *serial = get_serial_by_tty(tty);
  1265. int chars;
  1266. unsigned long flags;
  1267. /* sanity check */
  1268. if (serial == NULL)
  1269. return 0;
  1270. spin_lock_irqsave(&serial->serial_lock, flags);
  1271. chars = serial->tx_buffer_count;
  1272. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1273. return chars;
  1274. }
  1275. static int tiocmget_submit_urb(struct hso_serial *serial,
  1276. struct hso_tiocmget *tiocmget,
  1277. struct usb_device *usb)
  1278. {
  1279. int result;
  1280. if (serial->parent->usb_gone)
  1281. return -ENODEV;
  1282. usb_fill_int_urb(tiocmget->urb, usb,
  1283. usb_rcvintpipe(usb,
  1284. tiocmget->endp->
  1285. bEndpointAddress & 0x7F),
  1286. &tiocmget->serial_state_notification,
  1287. sizeof(struct hso_serial_state_notification),
  1288. tiocmget_intr_callback, serial,
  1289. tiocmget->endp->bInterval);
  1290. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1291. if (result) {
  1292. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1293. result);
  1294. }
  1295. return result;
  1296. }
  1297. static void tiocmget_intr_callback(struct urb *urb)
  1298. {
  1299. struct hso_serial *serial = urb->context;
  1300. struct hso_tiocmget *tiocmget;
  1301. int status = urb->status;
  1302. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1303. struct uart_icount *icount;
  1304. struct hso_serial_state_notification *serial_state_notification;
  1305. struct usb_device *usb;
  1306. /* Sanity checks */
  1307. if (!serial)
  1308. return;
  1309. if (status) {
  1310. handle_usb_error(status, __func__, serial->parent);
  1311. return;
  1312. }
  1313. tiocmget = serial->tiocmget;
  1314. if (!tiocmget)
  1315. return;
  1316. usb = serial->parent->usb;
  1317. serial_state_notification = &tiocmget->serial_state_notification;
  1318. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1319. serial_state_notification->bNotification != B_NOTIFICATION ||
  1320. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1321. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1322. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1323. dev_warn(&usb->dev,
  1324. "hso received invalid serial state notification\n");
  1325. DUMP(serial_state_notification,
  1326. sizeof(struct hso_serial_state_notification));
  1327. } else {
  1328. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1329. UART_state_bitmap);
  1330. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1331. icount = &tiocmget->icount;
  1332. spin_lock(&serial->serial_lock);
  1333. if ((UART_state_bitmap & B_OVERRUN) !=
  1334. (prev_UART_state_bitmap & B_OVERRUN))
  1335. icount->parity++;
  1336. if ((UART_state_bitmap & B_PARITY) !=
  1337. (prev_UART_state_bitmap & B_PARITY))
  1338. icount->parity++;
  1339. if ((UART_state_bitmap & B_FRAMING) !=
  1340. (prev_UART_state_bitmap & B_FRAMING))
  1341. icount->frame++;
  1342. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1343. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1344. icount->rng++;
  1345. if ((UART_state_bitmap & B_BREAK) !=
  1346. (prev_UART_state_bitmap & B_BREAK))
  1347. icount->brk++;
  1348. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1349. (prev_UART_state_bitmap & B_TX_CARRIER))
  1350. icount->dsr++;
  1351. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1352. (prev_UART_state_bitmap & B_RX_CARRIER))
  1353. icount->dcd++;
  1354. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1355. spin_unlock(&serial->serial_lock);
  1356. tiocmget->intr_completed = 1;
  1357. wake_up_interruptible(&tiocmget->waitq);
  1358. }
  1359. memset(serial_state_notification, 0,
  1360. sizeof(struct hso_serial_state_notification));
  1361. tiocmget_submit_urb(serial,
  1362. tiocmget,
  1363. serial->parent->usb);
  1364. }
  1365. /*
  1366. * next few functions largely stolen from drivers/serial/serial_core.c
  1367. */
  1368. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1369. * - mask passed in arg for lines of interest
  1370. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1371. * Caller should use TIOCGICOUNT to see which one it was
  1372. */
  1373. static int
  1374. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1375. {
  1376. DECLARE_WAITQUEUE(wait, current);
  1377. struct uart_icount cprev, cnow;
  1378. struct hso_tiocmget *tiocmget;
  1379. int ret;
  1380. tiocmget = serial->tiocmget;
  1381. if (!tiocmget)
  1382. return -ENOENT;
  1383. /*
  1384. * note the counters on entry
  1385. */
  1386. spin_lock_irq(&serial->serial_lock);
  1387. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1388. spin_unlock_irq(&serial->serial_lock);
  1389. add_wait_queue(&tiocmget->waitq, &wait);
  1390. for (;;) {
  1391. spin_lock_irq(&serial->serial_lock);
  1392. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1393. spin_unlock_irq(&serial->serial_lock);
  1394. set_current_state(TASK_INTERRUPTIBLE);
  1395. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1396. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1397. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1398. ret = 0;
  1399. break;
  1400. }
  1401. schedule();
  1402. /* see if a signal did it */
  1403. if (signal_pending(current)) {
  1404. ret = -ERESTARTSYS;
  1405. break;
  1406. }
  1407. cprev = cnow;
  1408. }
  1409. current->state = TASK_RUNNING;
  1410. remove_wait_queue(&tiocmget->waitq, &wait);
  1411. return ret;
  1412. }
  1413. /*
  1414. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1415. * Return: write counters to the user passed counter struct
  1416. * NB: both 1->0 and 0->1 transitions are counted except for
  1417. * RI where only 0->1 is counted.
  1418. */
  1419. static int hso_get_count(struct hso_serial *serial,
  1420. struct serial_icounter_struct __user *icnt)
  1421. {
  1422. struct serial_icounter_struct icount;
  1423. struct uart_icount cnow;
  1424. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1425. if (!tiocmget)
  1426. return -ENOENT;
  1427. spin_lock_irq(&serial->serial_lock);
  1428. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1429. spin_unlock_irq(&serial->serial_lock);
  1430. icount.cts = cnow.cts;
  1431. icount.dsr = cnow.dsr;
  1432. icount.rng = cnow.rng;
  1433. icount.dcd = cnow.dcd;
  1434. icount.rx = cnow.rx;
  1435. icount.tx = cnow.tx;
  1436. icount.frame = cnow.frame;
  1437. icount.overrun = cnow.overrun;
  1438. icount.parity = cnow.parity;
  1439. icount.brk = cnow.brk;
  1440. icount.buf_overrun = cnow.buf_overrun;
  1441. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1442. }
  1443. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1444. {
  1445. int retval;
  1446. struct hso_serial *serial = get_serial_by_tty(tty);
  1447. struct hso_tiocmget *tiocmget;
  1448. u16 UART_state_bitmap;
  1449. /* sanity check */
  1450. if (!serial) {
  1451. D1("no tty structures");
  1452. return -EINVAL;
  1453. }
  1454. spin_lock_irq(&serial->serial_lock);
  1455. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1456. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1457. tiocmget = serial->tiocmget;
  1458. if (tiocmget) {
  1459. UART_state_bitmap = le16_to_cpu(
  1460. tiocmget->prev_UART_state_bitmap);
  1461. if (UART_state_bitmap & B_RING_SIGNAL)
  1462. retval |= TIOCM_RNG;
  1463. if (UART_state_bitmap & B_RX_CARRIER)
  1464. retval |= TIOCM_CD;
  1465. if (UART_state_bitmap & B_TX_CARRIER)
  1466. retval |= TIOCM_DSR;
  1467. }
  1468. spin_unlock_irq(&serial->serial_lock);
  1469. return retval;
  1470. }
  1471. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1472. unsigned int set, unsigned int clear)
  1473. {
  1474. int val = 0;
  1475. unsigned long flags;
  1476. int if_num;
  1477. struct hso_serial *serial = get_serial_by_tty(tty);
  1478. /* sanity check */
  1479. if (!serial) {
  1480. D1("no tty structures");
  1481. return -EINVAL;
  1482. }
  1483. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1484. return -EINVAL;
  1485. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1486. spin_lock_irqsave(&serial->serial_lock, flags);
  1487. if (set & TIOCM_RTS)
  1488. serial->rts_state = 1;
  1489. if (set & TIOCM_DTR)
  1490. serial->dtr_state = 1;
  1491. if (clear & TIOCM_RTS)
  1492. serial->rts_state = 0;
  1493. if (clear & TIOCM_DTR)
  1494. serial->dtr_state = 0;
  1495. if (serial->dtr_state)
  1496. val |= 0x01;
  1497. if (serial->rts_state)
  1498. val |= 0x02;
  1499. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1500. return usb_control_msg(serial->parent->usb,
  1501. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1502. 0x21, val, if_num, NULL, 0,
  1503. USB_CTRL_SET_TIMEOUT);
  1504. }
  1505. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1506. unsigned int cmd, unsigned long arg)
  1507. {
  1508. struct hso_serial *serial = get_serial_by_tty(tty);
  1509. void __user *uarg = (void __user *)arg;
  1510. int ret = 0;
  1511. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1512. if (!serial)
  1513. return -ENODEV;
  1514. switch (cmd) {
  1515. case TIOCMIWAIT:
  1516. ret = hso_wait_modem_status(serial, arg);
  1517. break;
  1518. case TIOCGICOUNT:
  1519. ret = hso_get_count(serial, uarg);
  1520. break;
  1521. default:
  1522. ret = -ENOIOCTLCMD;
  1523. break;
  1524. }
  1525. return ret;
  1526. }
  1527. /* starts a transmit */
  1528. static void hso_kick_transmit(struct hso_serial *serial)
  1529. {
  1530. u8 *temp;
  1531. unsigned long flags;
  1532. int res;
  1533. spin_lock_irqsave(&serial->serial_lock, flags);
  1534. if (!serial->tx_buffer_count)
  1535. goto out;
  1536. if (serial->tx_urb_used)
  1537. goto out;
  1538. /* Wakeup USB interface if necessary */
  1539. if (hso_get_activity(serial->parent) == -EAGAIN)
  1540. goto out;
  1541. /* Switch pointers around to avoid memcpy */
  1542. temp = serial->tx_buffer;
  1543. serial->tx_buffer = serial->tx_data;
  1544. serial->tx_data = temp;
  1545. serial->tx_data_count = serial->tx_buffer_count;
  1546. serial->tx_buffer_count = 0;
  1547. /* If temp is set, it means we switched buffers */
  1548. if (temp && serial->write_data) {
  1549. res = serial->write_data(serial);
  1550. if (res >= 0)
  1551. serial->tx_urb_used = 1;
  1552. }
  1553. out:
  1554. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1555. }
  1556. /* make a request (for reading and writing data to muxed serial port) */
  1557. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1558. struct urb *ctrl_urb,
  1559. struct usb_ctrlrequest *ctrl_req,
  1560. u8 *ctrl_urb_data, u32 size)
  1561. {
  1562. int result;
  1563. int pipe;
  1564. /* Sanity check */
  1565. if (!serial || !ctrl_urb || !ctrl_req) {
  1566. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1567. return -EINVAL;
  1568. }
  1569. /* initialize */
  1570. ctrl_req->wValue = 0;
  1571. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1572. ctrl_req->wLength = cpu_to_le16(size);
  1573. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1574. /* Reading command */
  1575. ctrl_req->bRequestType = USB_DIR_IN |
  1576. USB_TYPE_OPTION_VENDOR |
  1577. USB_RECIP_INTERFACE;
  1578. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1579. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1580. } else {
  1581. /* Writing command */
  1582. ctrl_req->bRequestType = USB_DIR_OUT |
  1583. USB_TYPE_OPTION_VENDOR |
  1584. USB_RECIP_INTERFACE;
  1585. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1586. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1587. }
  1588. /* syslog */
  1589. D2("%s command (%02x) len: %d, port: %d",
  1590. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1591. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1592. /* Load ctrl urb */
  1593. ctrl_urb->transfer_flags = 0;
  1594. usb_fill_control_urb(ctrl_urb,
  1595. serial->parent->usb,
  1596. pipe,
  1597. (u8 *) ctrl_req,
  1598. ctrl_urb_data, size, ctrl_callback, serial);
  1599. /* Send it on merry way */
  1600. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1601. if (result) {
  1602. dev_err(&ctrl_urb->dev->dev,
  1603. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1604. result, type);
  1605. return result;
  1606. }
  1607. /* done */
  1608. return size;
  1609. }
  1610. /* called by intr_callback when read occurs */
  1611. static int hso_mux_serial_read(struct hso_serial *serial)
  1612. {
  1613. if (!serial)
  1614. return -EINVAL;
  1615. /* clean data */
  1616. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1617. /* make the request */
  1618. if (serial->num_rx_urbs != 1) {
  1619. dev_err(&serial->parent->interface->dev,
  1620. "ERROR: mux'd reads with multiple buffers "
  1621. "not possible\n");
  1622. return 0;
  1623. }
  1624. return mux_device_request(serial,
  1625. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1626. serial->parent->port_spec & HSO_PORT_MASK,
  1627. serial->rx_urb[0],
  1628. &serial->ctrl_req_rx,
  1629. serial->rx_data[0], serial->rx_data_length);
  1630. }
  1631. /* used for muxed serial port callback (muxed serial read) */
  1632. static void intr_callback(struct urb *urb)
  1633. {
  1634. struct hso_shared_int *shared_int = urb->context;
  1635. struct hso_serial *serial;
  1636. unsigned char *port_req;
  1637. int status = urb->status;
  1638. int i;
  1639. usb_mark_last_busy(urb->dev);
  1640. /* sanity check */
  1641. if (!shared_int)
  1642. return;
  1643. /* status check */
  1644. if (status) {
  1645. handle_usb_error(status, __func__, NULL);
  1646. return;
  1647. }
  1648. D4("\n--- Got intr callback 0x%02X ---", status);
  1649. /* what request? */
  1650. port_req = urb->transfer_buffer;
  1651. D4(" port_req = 0x%.2X\n", *port_req);
  1652. /* loop over all muxed ports to find the one sending this */
  1653. for (i = 0; i < 8; i++) {
  1654. /* max 8 channels on MUX */
  1655. if (*port_req & (1 << i)) {
  1656. serial = get_serial_by_shared_int_and_type(shared_int,
  1657. (1 << i));
  1658. if (serial != NULL) {
  1659. D1("Pending read interrupt on port %d\n", i);
  1660. spin_lock(&serial->serial_lock);
  1661. if (serial->rx_state == RX_IDLE &&
  1662. serial->open_count > 0) {
  1663. /* Setup and send a ctrl req read on
  1664. * port i */
  1665. if (!serial->rx_urb_filled[0]) {
  1666. serial->rx_state = RX_SENT;
  1667. hso_mux_serial_read(serial);
  1668. } else
  1669. serial->rx_state = RX_PENDING;
  1670. } else {
  1671. D1("Already a read pending on "
  1672. "port %d or port not open\n", i);
  1673. }
  1674. spin_unlock(&serial->serial_lock);
  1675. }
  1676. }
  1677. }
  1678. /* Resubmit interrupt urb */
  1679. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1680. }
  1681. /* called for writing to muxed serial port */
  1682. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1683. {
  1684. if (NULL == serial)
  1685. return -EINVAL;
  1686. return mux_device_request(serial,
  1687. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1688. serial->parent->port_spec & HSO_PORT_MASK,
  1689. serial->tx_urb,
  1690. &serial->ctrl_req_tx,
  1691. serial->tx_data, serial->tx_data_count);
  1692. }
  1693. /* write callback for Diag and CS port */
  1694. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1695. {
  1696. struct hso_serial *serial = urb->context;
  1697. int status = urb->status;
  1698. struct tty_struct *tty;
  1699. /* sanity check */
  1700. if (!serial) {
  1701. D1("serial == NULL");
  1702. return;
  1703. }
  1704. spin_lock(&serial->serial_lock);
  1705. serial->tx_urb_used = 0;
  1706. tty = tty_kref_get(serial->tty);
  1707. spin_unlock(&serial->serial_lock);
  1708. if (status) {
  1709. handle_usb_error(status, __func__, serial->parent);
  1710. tty_kref_put(tty);
  1711. return;
  1712. }
  1713. hso_put_activity(serial->parent);
  1714. if (tty) {
  1715. tty_wakeup(tty);
  1716. tty_kref_put(tty);
  1717. }
  1718. hso_kick_transmit(serial);
  1719. D1(" ");
  1720. }
  1721. /* called for writing diag or CS serial port */
  1722. static int hso_std_serial_write_data(struct hso_serial *serial)
  1723. {
  1724. int count = serial->tx_data_count;
  1725. int result;
  1726. usb_fill_bulk_urb(serial->tx_urb,
  1727. serial->parent->usb,
  1728. usb_sndbulkpipe(serial->parent->usb,
  1729. serial->out_endp->
  1730. bEndpointAddress & 0x7F),
  1731. serial->tx_data, serial->tx_data_count,
  1732. hso_std_serial_write_bulk_callback, serial);
  1733. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1734. if (result) {
  1735. dev_warn(&serial->parent->usb->dev,
  1736. "Failed to submit urb - res %d\n", result);
  1737. return result;
  1738. }
  1739. return count;
  1740. }
  1741. /* callback after read or write on muxed serial port */
  1742. static void ctrl_callback(struct urb *urb)
  1743. {
  1744. struct hso_serial *serial = urb->context;
  1745. struct usb_ctrlrequest *req;
  1746. int status = urb->status;
  1747. struct tty_struct *tty;
  1748. /* sanity check */
  1749. if (!serial)
  1750. return;
  1751. spin_lock(&serial->serial_lock);
  1752. serial->tx_urb_used = 0;
  1753. tty = tty_kref_get(serial->tty);
  1754. spin_unlock(&serial->serial_lock);
  1755. if (status) {
  1756. handle_usb_error(status, __func__, serial->parent);
  1757. tty_kref_put(tty);
  1758. return;
  1759. }
  1760. /* what request? */
  1761. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1762. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1763. D4("Actual length of urb = %d\n", urb->actual_length);
  1764. DUMP1(urb->transfer_buffer, urb->actual_length);
  1765. if (req->bRequestType ==
  1766. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1767. /* response to a read command */
  1768. serial->rx_urb_filled[0] = 1;
  1769. spin_lock(&serial->serial_lock);
  1770. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1771. spin_unlock(&serial->serial_lock);
  1772. } else {
  1773. hso_put_activity(serial->parent);
  1774. if (tty)
  1775. tty_wakeup(tty);
  1776. /* response to a write command */
  1777. hso_kick_transmit(serial);
  1778. }
  1779. tty_kref_put(tty);
  1780. }
  1781. /* handle RX data for serial port */
  1782. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1783. {
  1784. struct tty_struct *tty;
  1785. int write_length_remaining = 0;
  1786. int curr_write_len;
  1787. /* Sanity check */
  1788. if (urb == NULL || serial == NULL) {
  1789. D1("serial = NULL");
  1790. return -2;
  1791. }
  1792. /* All callers to put_rxbuf_data hold serial_lock */
  1793. tty = tty_kref_get(serial->tty);
  1794. /* Push data to tty */
  1795. if (tty) {
  1796. write_length_remaining = urb->actual_length -
  1797. serial->curr_rx_urb_offset;
  1798. D1("data to push to tty");
  1799. while (write_length_remaining) {
  1800. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1801. tty_kref_put(tty);
  1802. return -1;
  1803. }
  1804. curr_write_len = tty_insert_flip_string
  1805. (tty, urb->transfer_buffer +
  1806. serial->curr_rx_urb_offset,
  1807. write_length_remaining);
  1808. serial->curr_rx_urb_offset += curr_write_len;
  1809. write_length_remaining -= curr_write_len;
  1810. tty_flip_buffer_push(tty);
  1811. }
  1812. }
  1813. if (write_length_remaining == 0) {
  1814. serial->curr_rx_urb_offset = 0;
  1815. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1816. }
  1817. tty_kref_put(tty);
  1818. return write_length_remaining;
  1819. }
  1820. /* Base driver functions */
  1821. static void hso_log_port(struct hso_device *hso_dev)
  1822. {
  1823. char *port_type;
  1824. char port_dev[20];
  1825. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1826. case HSO_PORT_CONTROL:
  1827. port_type = "Control";
  1828. break;
  1829. case HSO_PORT_APP:
  1830. port_type = "Application";
  1831. break;
  1832. case HSO_PORT_GPS:
  1833. port_type = "GPS";
  1834. break;
  1835. case HSO_PORT_GPS_CONTROL:
  1836. port_type = "GPS control";
  1837. break;
  1838. case HSO_PORT_APP2:
  1839. port_type = "Application2";
  1840. break;
  1841. case HSO_PORT_PCSC:
  1842. port_type = "PCSC";
  1843. break;
  1844. case HSO_PORT_DIAG:
  1845. port_type = "Diagnostic";
  1846. break;
  1847. case HSO_PORT_DIAG2:
  1848. port_type = "Diagnostic2";
  1849. break;
  1850. case HSO_PORT_MODEM:
  1851. port_type = "Modem";
  1852. break;
  1853. case HSO_PORT_NETWORK:
  1854. port_type = "Network";
  1855. break;
  1856. default:
  1857. port_type = "Unknown";
  1858. break;
  1859. }
  1860. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1861. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1862. } else
  1863. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1864. dev2ser(hso_dev)->minor);
  1865. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1866. port_type, port_dev);
  1867. }
  1868. static int hso_start_net_device(struct hso_device *hso_dev)
  1869. {
  1870. int i, result = 0;
  1871. struct hso_net *hso_net = dev2net(hso_dev);
  1872. if (!hso_net)
  1873. return -ENODEV;
  1874. /* send URBs for all read buffers */
  1875. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1876. /* Prep a receive URB */
  1877. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1878. hso_dev->usb,
  1879. usb_rcvbulkpipe(hso_dev->usb,
  1880. hso_net->in_endp->
  1881. bEndpointAddress & 0x7F),
  1882. hso_net->mux_bulk_rx_buf_pool[i],
  1883. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1884. hso_net);
  1885. /* Put it out there so the device can send us stuff */
  1886. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1887. GFP_NOIO);
  1888. if (result)
  1889. dev_warn(&hso_dev->usb->dev,
  1890. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1891. i, result);
  1892. }
  1893. return result;
  1894. }
  1895. static int hso_stop_net_device(struct hso_device *hso_dev)
  1896. {
  1897. int i;
  1898. struct hso_net *hso_net = dev2net(hso_dev);
  1899. if (!hso_net)
  1900. return -ENODEV;
  1901. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1902. if (hso_net->mux_bulk_rx_urb_pool[i])
  1903. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1904. }
  1905. if (hso_net->mux_bulk_tx_urb)
  1906. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1907. return 0;
  1908. }
  1909. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1910. {
  1911. int i, result = 0;
  1912. struct hso_serial *serial = dev2ser(hso_dev);
  1913. if (!serial)
  1914. return -ENODEV;
  1915. /* If it is not the MUX port fill in and submit a bulk urb (already
  1916. * allocated in hso_serial_start) */
  1917. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1918. for (i = 0; i < serial->num_rx_urbs; i++) {
  1919. usb_fill_bulk_urb(serial->rx_urb[i],
  1920. serial->parent->usb,
  1921. usb_rcvbulkpipe(serial->parent->usb,
  1922. serial->in_endp->
  1923. bEndpointAddress &
  1924. 0x7F),
  1925. serial->rx_data[i],
  1926. serial->rx_data_length,
  1927. hso_std_serial_read_bulk_callback,
  1928. serial);
  1929. result = usb_submit_urb(serial->rx_urb[i], flags);
  1930. if (result) {
  1931. dev_warn(&serial->parent->usb->dev,
  1932. "Failed to submit urb - res %d\n",
  1933. result);
  1934. break;
  1935. }
  1936. }
  1937. } else {
  1938. mutex_lock(&serial->shared_int->shared_int_lock);
  1939. if (!serial->shared_int->use_count) {
  1940. result =
  1941. hso_mux_submit_intr_urb(serial->shared_int,
  1942. hso_dev->usb, flags);
  1943. }
  1944. serial->shared_int->use_count++;
  1945. mutex_unlock(&serial->shared_int->shared_int_lock);
  1946. }
  1947. if (serial->tiocmget)
  1948. tiocmget_submit_urb(serial,
  1949. serial->tiocmget,
  1950. serial->parent->usb);
  1951. return result;
  1952. }
  1953. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1954. {
  1955. int i;
  1956. struct hso_serial *serial = dev2ser(hso_dev);
  1957. struct hso_tiocmget *tiocmget;
  1958. if (!serial)
  1959. return -ENODEV;
  1960. for (i = 0; i < serial->num_rx_urbs; i++) {
  1961. if (serial->rx_urb[i]) {
  1962. usb_kill_urb(serial->rx_urb[i]);
  1963. serial->rx_urb_filled[i] = 0;
  1964. }
  1965. }
  1966. serial->curr_rx_urb_idx = 0;
  1967. serial->curr_rx_urb_offset = 0;
  1968. if (serial->tx_urb)
  1969. usb_kill_urb(serial->tx_urb);
  1970. if (serial->shared_int) {
  1971. mutex_lock(&serial->shared_int->shared_int_lock);
  1972. if (serial->shared_int->use_count &&
  1973. (--serial->shared_int->use_count == 0)) {
  1974. struct urb *urb;
  1975. urb = serial->shared_int->shared_intr_urb;
  1976. if (urb)
  1977. usb_kill_urb(urb);
  1978. }
  1979. mutex_unlock(&serial->shared_int->shared_int_lock);
  1980. }
  1981. tiocmget = serial->tiocmget;
  1982. if (tiocmget) {
  1983. wake_up_interruptible(&tiocmget->waitq);
  1984. usb_kill_urb(tiocmget->urb);
  1985. }
  1986. return 0;
  1987. }
  1988. static void hso_serial_common_free(struct hso_serial *serial)
  1989. {
  1990. int i;
  1991. if (serial->parent->dev)
  1992. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1993. tty_unregister_device(tty_drv, serial->minor);
  1994. for (i = 0; i < serial->num_rx_urbs; i++) {
  1995. /* unlink and free RX URB */
  1996. usb_free_urb(serial->rx_urb[i]);
  1997. /* free the RX buffer */
  1998. kfree(serial->rx_data[i]);
  1999. }
  2000. /* unlink and free TX URB */
  2001. usb_free_urb(serial->tx_urb);
  2002. kfree(serial->tx_data);
  2003. }
  2004. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  2005. int rx_size, int tx_size)
  2006. {
  2007. struct device *dev;
  2008. int minor;
  2009. int i;
  2010. minor = get_free_serial_index();
  2011. if (minor < 0)
  2012. goto exit;
  2013. /* register our minor number */
  2014. serial->parent->dev = tty_register_device(tty_drv, minor,
  2015. &serial->parent->interface->dev);
  2016. dev = serial->parent->dev;
  2017. dev_set_drvdata(dev, serial->parent);
  2018. i = device_create_file(dev, &dev_attr_hsotype);
  2019. /* fill in specific data for later use */
  2020. serial->minor = minor;
  2021. serial->magic = HSO_SERIAL_MAGIC;
  2022. spin_lock_init(&serial->serial_lock);
  2023. serial->num_rx_urbs = num_urbs;
  2024. /* RX, allocate urb and initialize */
  2025. /* prepare our RX buffer */
  2026. serial->rx_data_length = rx_size;
  2027. for (i = 0; i < serial->num_rx_urbs; i++) {
  2028. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2029. if (!serial->rx_urb[i]) {
  2030. dev_err(dev, "Could not allocate urb?\n");
  2031. goto exit;
  2032. }
  2033. serial->rx_urb[i]->transfer_buffer = NULL;
  2034. serial->rx_urb[i]->transfer_buffer_length = 0;
  2035. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2036. GFP_KERNEL);
  2037. if (!serial->rx_data[i]) {
  2038. dev_err(dev, "%s - Out of memory\n", __func__);
  2039. goto exit;
  2040. }
  2041. }
  2042. /* TX, allocate urb and initialize */
  2043. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2044. if (!serial->tx_urb) {
  2045. dev_err(dev, "Could not allocate urb?\n");
  2046. goto exit;
  2047. }
  2048. serial->tx_urb->transfer_buffer = NULL;
  2049. serial->tx_urb->transfer_buffer_length = 0;
  2050. /* prepare our TX buffer */
  2051. serial->tx_data_count = 0;
  2052. serial->tx_buffer_count = 0;
  2053. serial->tx_data_length = tx_size;
  2054. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2055. if (!serial->tx_data) {
  2056. dev_err(dev, "%s - Out of memory\n", __func__);
  2057. goto exit;
  2058. }
  2059. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2060. if (!serial->tx_buffer) {
  2061. dev_err(dev, "%s - Out of memory\n", __func__);
  2062. goto exit;
  2063. }
  2064. return 0;
  2065. exit:
  2066. hso_serial_common_free(serial);
  2067. return -1;
  2068. }
  2069. /* Creates a general hso device */
  2070. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2071. int port_spec)
  2072. {
  2073. struct hso_device *hso_dev;
  2074. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2075. if (!hso_dev)
  2076. return NULL;
  2077. hso_dev->port_spec = port_spec;
  2078. hso_dev->usb = interface_to_usbdev(intf);
  2079. hso_dev->interface = intf;
  2080. kref_init(&hso_dev->ref);
  2081. mutex_init(&hso_dev->mutex);
  2082. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2083. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2084. INIT_WORK(&hso_dev->reset_device, reset_device);
  2085. return hso_dev;
  2086. }
  2087. /* Removes a network device in the network device table */
  2088. static int remove_net_device(struct hso_device *hso_dev)
  2089. {
  2090. int i;
  2091. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2092. if (network_table[i] == hso_dev) {
  2093. network_table[i] = NULL;
  2094. break;
  2095. }
  2096. }
  2097. if (i == HSO_MAX_NET_DEVICES)
  2098. return -1;
  2099. return 0;
  2100. }
  2101. /* Frees our network device */
  2102. static void hso_free_net_device(struct hso_device *hso_dev)
  2103. {
  2104. int i;
  2105. struct hso_net *hso_net = dev2net(hso_dev);
  2106. if (!hso_net)
  2107. return;
  2108. remove_net_device(hso_net->parent);
  2109. if (hso_net->net) {
  2110. unregister_netdev(hso_net->net);
  2111. free_netdev(hso_net->net);
  2112. }
  2113. /* start freeing */
  2114. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2115. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2116. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2117. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2118. }
  2119. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2120. kfree(hso_net->mux_bulk_tx_buf);
  2121. hso_net->mux_bulk_tx_buf = NULL;
  2122. kfree(hso_dev);
  2123. }
  2124. static const struct net_device_ops hso_netdev_ops = {
  2125. .ndo_open = hso_net_open,
  2126. .ndo_stop = hso_net_close,
  2127. .ndo_start_xmit = hso_net_start_xmit,
  2128. .ndo_tx_timeout = hso_net_tx_timeout,
  2129. };
  2130. /* initialize the network interface */
  2131. static void hso_net_init(struct net_device *net)
  2132. {
  2133. struct hso_net *hso_net = netdev_priv(net);
  2134. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2135. /* fill in the other fields */
  2136. net->netdev_ops = &hso_netdev_ops;
  2137. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2138. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2139. net->type = ARPHRD_NONE;
  2140. net->mtu = DEFAULT_MTU - 14;
  2141. net->tx_queue_len = 10;
  2142. SET_ETHTOOL_OPS(net, &ops);
  2143. /* and initialize the semaphore */
  2144. spin_lock_init(&hso_net->net_lock);
  2145. }
  2146. /* Adds a network device in the network device table */
  2147. static int add_net_device(struct hso_device *hso_dev)
  2148. {
  2149. int i;
  2150. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2151. if (network_table[i] == NULL) {
  2152. network_table[i] = hso_dev;
  2153. break;
  2154. }
  2155. }
  2156. if (i == HSO_MAX_NET_DEVICES)
  2157. return -1;
  2158. return 0;
  2159. }
  2160. static int hso_rfkill_set_block(void *data, bool blocked)
  2161. {
  2162. struct hso_device *hso_dev = data;
  2163. int enabled = !blocked;
  2164. int rv;
  2165. mutex_lock(&hso_dev->mutex);
  2166. if (hso_dev->usb_gone)
  2167. rv = 0;
  2168. else
  2169. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2170. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2171. USB_CTRL_SET_TIMEOUT);
  2172. mutex_unlock(&hso_dev->mutex);
  2173. return rv;
  2174. }
  2175. static const struct rfkill_ops hso_rfkill_ops = {
  2176. .set_block = hso_rfkill_set_block,
  2177. };
  2178. /* Creates and sets up everything for rfkill */
  2179. static void hso_create_rfkill(struct hso_device *hso_dev,
  2180. struct usb_interface *interface)
  2181. {
  2182. struct hso_net *hso_net = dev2net(hso_dev);
  2183. struct device *dev = &hso_net->net->dev;
  2184. char *rfkn;
  2185. rfkn = kzalloc(20, GFP_KERNEL);
  2186. if (!rfkn)
  2187. dev_err(dev, "%s - Out of memory\n", __func__);
  2188. snprintf(rfkn, 20, "hso-%d",
  2189. interface->altsetting->desc.bInterfaceNumber);
  2190. hso_net->rfkill = rfkill_alloc(rfkn,
  2191. &interface_to_usbdev(interface)->dev,
  2192. RFKILL_TYPE_WWAN,
  2193. &hso_rfkill_ops, hso_dev);
  2194. if (!hso_net->rfkill) {
  2195. dev_err(dev, "%s - Out of memory\n", __func__);
  2196. kfree(rfkn);
  2197. return;
  2198. }
  2199. if (rfkill_register(hso_net->rfkill) < 0) {
  2200. rfkill_destroy(hso_net->rfkill);
  2201. kfree(rfkn);
  2202. hso_net->rfkill = NULL;
  2203. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2204. return;
  2205. }
  2206. }
  2207. static struct device_type hso_type = {
  2208. .name = "wwan",
  2209. };
  2210. /* Creates our network device */
  2211. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2212. int port_spec)
  2213. {
  2214. int result, i;
  2215. struct net_device *net;
  2216. struct hso_net *hso_net;
  2217. struct hso_device *hso_dev;
  2218. hso_dev = hso_create_device(interface, port_spec);
  2219. if (!hso_dev)
  2220. return NULL;
  2221. /* allocate our network device, then we can put in our private data */
  2222. /* call hso_net_init to do the basic initialization */
  2223. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2224. if (!net) {
  2225. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2226. goto exit;
  2227. }
  2228. hso_net = netdev_priv(net);
  2229. hso_dev->port_data.dev_net = hso_net;
  2230. hso_net->net = net;
  2231. hso_net->parent = hso_dev;
  2232. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2233. USB_DIR_IN);
  2234. if (!hso_net->in_endp) {
  2235. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2236. goto exit;
  2237. }
  2238. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2239. USB_DIR_OUT);
  2240. if (!hso_net->out_endp) {
  2241. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2242. goto exit;
  2243. }
  2244. SET_NETDEV_DEV(net, &interface->dev);
  2245. SET_NETDEV_DEVTYPE(net, &hso_type);
  2246. /* registering our net device */
  2247. result = register_netdev(net);
  2248. if (result) {
  2249. dev_err(&interface->dev, "Failed to register device\n");
  2250. goto exit;
  2251. }
  2252. /* start allocating */
  2253. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2254. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2255. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2256. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2257. goto exit;
  2258. }
  2259. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2260. GFP_KERNEL);
  2261. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2262. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2263. goto exit;
  2264. }
  2265. }
  2266. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2267. if (!hso_net->mux_bulk_tx_urb) {
  2268. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2269. goto exit;
  2270. }
  2271. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2272. if (!hso_net->mux_bulk_tx_buf) {
  2273. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2274. goto exit;
  2275. }
  2276. add_net_device(hso_dev);
  2277. hso_log_port(hso_dev);
  2278. hso_create_rfkill(hso_dev, interface);
  2279. return hso_dev;
  2280. exit:
  2281. hso_free_net_device(hso_dev);
  2282. return NULL;
  2283. }
  2284. static void hso_free_tiomget(struct hso_serial *serial)
  2285. {
  2286. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2287. if (tiocmget) {
  2288. if (tiocmget->urb) {
  2289. usb_free_urb(tiocmget->urb);
  2290. tiocmget->urb = NULL;
  2291. }
  2292. serial->tiocmget = NULL;
  2293. kfree(tiocmget);
  2294. }
  2295. }
  2296. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2297. static void hso_free_serial_device(struct hso_device *hso_dev)
  2298. {
  2299. struct hso_serial *serial = dev2ser(hso_dev);
  2300. if (!serial)
  2301. return;
  2302. set_serial_by_index(serial->minor, NULL);
  2303. hso_serial_common_free(serial);
  2304. if (serial->shared_int) {
  2305. mutex_lock(&serial->shared_int->shared_int_lock);
  2306. if (--serial->shared_int->ref_count == 0)
  2307. hso_free_shared_int(serial->shared_int);
  2308. else
  2309. mutex_unlock(&serial->shared_int->shared_int_lock);
  2310. }
  2311. hso_free_tiomget(serial);
  2312. kfree(serial);
  2313. kfree(hso_dev);
  2314. }
  2315. /* Creates a bulk AT channel */
  2316. static struct hso_device *hso_create_bulk_serial_device(
  2317. struct usb_interface *interface, int port)
  2318. {
  2319. struct hso_device *hso_dev;
  2320. struct hso_serial *serial;
  2321. int num_urbs;
  2322. struct hso_tiocmget *tiocmget;
  2323. hso_dev = hso_create_device(interface, port);
  2324. if (!hso_dev)
  2325. return NULL;
  2326. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2327. if (!serial)
  2328. goto exit;
  2329. serial->parent = hso_dev;
  2330. hso_dev->port_data.dev_serial = serial;
  2331. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2332. num_urbs = 2;
  2333. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2334. GFP_KERNEL);
  2335. /* it isn't going to break our heart if serial->tiocmget
  2336. * allocation fails don't bother checking this.
  2337. */
  2338. if (serial->tiocmget) {
  2339. tiocmget = serial->tiocmget;
  2340. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2341. if (tiocmget->urb) {
  2342. mutex_init(&tiocmget->mutex);
  2343. init_waitqueue_head(&tiocmget->waitq);
  2344. tiocmget->endp = hso_get_ep(
  2345. interface,
  2346. USB_ENDPOINT_XFER_INT,
  2347. USB_DIR_IN);
  2348. } else
  2349. hso_free_tiomget(serial);
  2350. }
  2351. }
  2352. else
  2353. num_urbs = 1;
  2354. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2355. BULK_URB_TX_SIZE))
  2356. goto exit;
  2357. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2358. USB_DIR_IN);
  2359. if (!serial->in_endp) {
  2360. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2361. goto exit2;
  2362. }
  2363. if (!
  2364. (serial->out_endp =
  2365. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2366. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2367. goto exit2;
  2368. }
  2369. serial->write_data = hso_std_serial_write_data;
  2370. /* and record this serial */
  2371. set_serial_by_index(serial->minor, serial);
  2372. /* setup the proc dirs and files if needed */
  2373. hso_log_port(hso_dev);
  2374. /* done, return it */
  2375. return hso_dev;
  2376. exit2:
  2377. hso_serial_common_free(serial);
  2378. exit:
  2379. hso_free_tiomget(serial);
  2380. kfree(serial);
  2381. kfree(hso_dev);
  2382. return NULL;
  2383. }
  2384. /* Creates a multiplexed AT channel */
  2385. static
  2386. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2387. int port,
  2388. struct hso_shared_int *mux)
  2389. {
  2390. struct hso_device *hso_dev;
  2391. struct hso_serial *serial;
  2392. int port_spec;
  2393. port_spec = HSO_INTF_MUX;
  2394. port_spec &= ~HSO_PORT_MASK;
  2395. port_spec |= hso_mux_to_port(port);
  2396. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2397. return NULL;
  2398. hso_dev = hso_create_device(interface, port_spec);
  2399. if (!hso_dev)
  2400. return NULL;
  2401. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2402. if (!serial)
  2403. goto exit;
  2404. hso_dev->port_data.dev_serial = serial;
  2405. serial->parent = hso_dev;
  2406. if (hso_serial_common_create
  2407. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2408. goto exit;
  2409. serial->tx_data_length--;
  2410. serial->write_data = hso_mux_serial_write_data;
  2411. serial->shared_int = mux;
  2412. mutex_lock(&serial->shared_int->shared_int_lock);
  2413. serial->shared_int->ref_count++;
  2414. mutex_unlock(&serial->shared_int->shared_int_lock);
  2415. /* and record this serial */
  2416. set_serial_by_index(serial->minor, serial);
  2417. /* setup the proc dirs and files if needed */
  2418. hso_log_port(hso_dev);
  2419. /* done, return it */
  2420. return hso_dev;
  2421. exit:
  2422. if (serial) {
  2423. tty_unregister_device(tty_drv, serial->minor);
  2424. kfree(serial);
  2425. }
  2426. if (hso_dev)
  2427. kfree(hso_dev);
  2428. return NULL;
  2429. }
  2430. static void hso_free_shared_int(struct hso_shared_int *mux)
  2431. {
  2432. usb_free_urb(mux->shared_intr_urb);
  2433. kfree(mux->shared_intr_buf);
  2434. mutex_unlock(&mux->shared_int_lock);
  2435. kfree(mux);
  2436. }
  2437. static
  2438. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2439. {
  2440. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2441. if (!mux)
  2442. return NULL;
  2443. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2444. USB_DIR_IN);
  2445. if (!mux->intr_endp) {
  2446. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2447. goto exit;
  2448. }
  2449. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2450. if (!mux->shared_intr_urb) {
  2451. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2452. goto exit;
  2453. }
  2454. mux->shared_intr_buf =
  2455. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2456. GFP_KERNEL);
  2457. if (!mux->shared_intr_buf) {
  2458. dev_err(&interface->dev, "Could not allocate intr buf?\n");
  2459. goto exit;
  2460. }
  2461. mutex_init(&mux->shared_int_lock);
  2462. return mux;
  2463. exit:
  2464. kfree(mux->shared_intr_buf);
  2465. usb_free_urb(mux->shared_intr_urb);
  2466. kfree(mux);
  2467. return NULL;
  2468. }
  2469. /* Gets the port spec for a certain interface */
  2470. static int hso_get_config_data(struct usb_interface *interface)
  2471. {
  2472. struct usb_device *usbdev = interface_to_usbdev(interface);
  2473. u8 config_data[17];
  2474. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2475. s32 result;
  2476. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2477. 0x86, 0xC0, 0, 0, config_data, 17,
  2478. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2479. return -EIO;
  2480. }
  2481. switch (config_data[if_num]) {
  2482. case 0x0:
  2483. result = 0;
  2484. break;
  2485. case 0x1:
  2486. result = HSO_PORT_DIAG;
  2487. break;
  2488. case 0x2:
  2489. result = HSO_PORT_GPS;
  2490. break;
  2491. case 0x3:
  2492. result = HSO_PORT_GPS_CONTROL;
  2493. break;
  2494. case 0x4:
  2495. result = HSO_PORT_APP;
  2496. break;
  2497. case 0x5:
  2498. result = HSO_PORT_APP2;
  2499. break;
  2500. case 0x6:
  2501. result = HSO_PORT_CONTROL;
  2502. break;
  2503. case 0x7:
  2504. result = HSO_PORT_NETWORK;
  2505. break;
  2506. case 0x8:
  2507. result = HSO_PORT_MODEM;
  2508. break;
  2509. case 0x9:
  2510. result = HSO_PORT_MSD;
  2511. break;
  2512. case 0xa:
  2513. result = HSO_PORT_PCSC;
  2514. break;
  2515. case 0xb:
  2516. result = HSO_PORT_VOICE;
  2517. break;
  2518. default:
  2519. result = 0;
  2520. }
  2521. if (result)
  2522. result |= HSO_INTF_BULK;
  2523. if (config_data[16] & 0x1)
  2524. result |= HSO_INFO_CRC_BUG;
  2525. return result;
  2526. }
  2527. /* called once for each interface upon device insertion */
  2528. static int hso_probe(struct usb_interface *interface,
  2529. const struct usb_device_id *id)
  2530. {
  2531. int mux, i, if_num, port_spec;
  2532. unsigned char port_mask;
  2533. struct hso_device *hso_dev = NULL;
  2534. struct hso_shared_int *shared_int;
  2535. struct hso_device *tmp_dev = NULL;
  2536. if_num = interface->altsetting->desc.bInterfaceNumber;
  2537. /* Get the interface/port specification from either driver_info or from
  2538. * the device itself */
  2539. if (id->driver_info)
  2540. port_spec = ((u32 *)(id->driver_info))[if_num];
  2541. else
  2542. port_spec = hso_get_config_data(interface);
  2543. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2544. dev_err(&interface->dev, "Not our interface\n");
  2545. return -ENODEV;
  2546. }
  2547. /* Check if we need to switch to alt interfaces prior to port
  2548. * configuration */
  2549. if (interface->num_altsetting > 1)
  2550. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2551. interface->needs_remote_wakeup = 1;
  2552. /* Allocate new hso device(s) */
  2553. switch (port_spec & HSO_INTF_MASK) {
  2554. case HSO_INTF_MUX:
  2555. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2556. /* Create the network device */
  2557. if (!disable_net) {
  2558. hso_dev = hso_create_net_device(interface,
  2559. port_spec);
  2560. if (!hso_dev)
  2561. goto exit;
  2562. tmp_dev = hso_dev;
  2563. }
  2564. }
  2565. if (hso_get_mux_ports(interface, &port_mask))
  2566. /* TODO: de-allocate everything */
  2567. goto exit;
  2568. shared_int = hso_create_shared_int(interface);
  2569. if (!shared_int)
  2570. goto exit;
  2571. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2572. if (port_mask & i) {
  2573. hso_dev = hso_create_mux_serial_device(
  2574. interface, i, shared_int);
  2575. if (!hso_dev)
  2576. goto exit;
  2577. }
  2578. }
  2579. if (tmp_dev)
  2580. hso_dev = tmp_dev;
  2581. break;
  2582. case HSO_INTF_BULK:
  2583. /* It's a regular bulk interface */
  2584. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) &&
  2585. !disable_net)
  2586. hso_dev = hso_create_net_device(interface, port_spec);
  2587. else
  2588. hso_dev =
  2589. hso_create_bulk_serial_device(interface, port_spec);
  2590. if (!hso_dev)
  2591. goto exit;
  2592. break;
  2593. default:
  2594. goto exit;
  2595. }
  2596. /* save our data pointer in this device */
  2597. usb_set_intfdata(interface, hso_dev);
  2598. /* done */
  2599. return 0;
  2600. exit:
  2601. hso_free_interface(interface);
  2602. return -ENODEV;
  2603. }
  2604. /* device removed, cleaning up */
  2605. static void hso_disconnect(struct usb_interface *interface)
  2606. {
  2607. hso_free_interface(interface);
  2608. /* remove reference of our private data */
  2609. usb_set_intfdata(interface, NULL);
  2610. }
  2611. static void async_get_intf(struct work_struct *data)
  2612. {
  2613. struct hso_device *hso_dev =
  2614. container_of(data, struct hso_device, async_get_intf);
  2615. usb_autopm_get_interface(hso_dev->interface);
  2616. }
  2617. static void async_put_intf(struct work_struct *data)
  2618. {
  2619. struct hso_device *hso_dev =
  2620. container_of(data, struct hso_device, async_put_intf);
  2621. usb_autopm_put_interface(hso_dev->interface);
  2622. }
  2623. static int hso_get_activity(struct hso_device *hso_dev)
  2624. {
  2625. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2626. if (!hso_dev->is_active) {
  2627. hso_dev->is_active = 1;
  2628. schedule_work(&hso_dev->async_get_intf);
  2629. }
  2630. }
  2631. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2632. return -EAGAIN;
  2633. usb_mark_last_busy(hso_dev->usb);
  2634. return 0;
  2635. }
  2636. static int hso_put_activity(struct hso_device *hso_dev)
  2637. {
  2638. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2639. if (hso_dev->is_active) {
  2640. hso_dev->is_active = 0;
  2641. schedule_work(&hso_dev->async_put_intf);
  2642. return -EAGAIN;
  2643. }
  2644. }
  2645. hso_dev->is_active = 0;
  2646. return 0;
  2647. }
  2648. /* called by kernel when we need to suspend device */
  2649. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2650. {
  2651. int i, result;
  2652. /* Stop all serial ports */
  2653. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2654. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2655. result = hso_stop_serial_device(serial_table[i]);
  2656. if (result)
  2657. goto out;
  2658. }
  2659. }
  2660. /* Stop all network ports */
  2661. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2662. if (network_table[i] &&
  2663. (network_table[i]->interface == iface)) {
  2664. result = hso_stop_net_device(network_table[i]);
  2665. if (result)
  2666. goto out;
  2667. }
  2668. }
  2669. out:
  2670. return 0;
  2671. }
  2672. /* called by kernel when we need to resume device */
  2673. static int hso_resume(struct usb_interface *iface)
  2674. {
  2675. int i, result = 0;
  2676. struct hso_net *hso_net;
  2677. /* Start all serial ports */
  2678. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2679. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2680. if (dev2ser(serial_table[i])->open_count) {
  2681. result =
  2682. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2683. hso_kick_transmit(dev2ser(serial_table[i]));
  2684. if (result)
  2685. goto out;
  2686. }
  2687. }
  2688. }
  2689. /* Start all network ports */
  2690. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2691. if (network_table[i] &&
  2692. (network_table[i]->interface == iface)) {
  2693. hso_net = dev2net(network_table[i]);
  2694. if (hso_net->flags & IFF_UP) {
  2695. /* First transmit any lingering data,
  2696. then restart the device. */
  2697. if (hso_net->skb_tx_buf) {
  2698. dev_dbg(&iface->dev,
  2699. "Transmitting"
  2700. " lingering data\n");
  2701. hso_net_start_xmit(hso_net->skb_tx_buf,
  2702. hso_net->net);
  2703. hso_net->skb_tx_buf = NULL;
  2704. }
  2705. result = hso_start_net_device(network_table[i]);
  2706. if (result)
  2707. goto out;
  2708. }
  2709. }
  2710. }
  2711. out:
  2712. return result;
  2713. }
  2714. static void reset_device(struct work_struct *data)
  2715. {
  2716. struct hso_device *hso_dev =
  2717. container_of(data, struct hso_device, reset_device);
  2718. struct usb_device *usb = hso_dev->usb;
  2719. int result;
  2720. if (hso_dev->usb_gone) {
  2721. D1("No reset during disconnect\n");
  2722. } else {
  2723. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2724. if (result < 0)
  2725. D1("unable to lock device for reset: %d\n", result);
  2726. else {
  2727. usb_reset_device(usb);
  2728. usb_unlock_device(usb);
  2729. }
  2730. }
  2731. }
  2732. static void hso_serial_ref_free(struct kref *ref)
  2733. {
  2734. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2735. hso_free_serial_device(hso_dev);
  2736. }
  2737. static void hso_free_interface(struct usb_interface *interface)
  2738. {
  2739. struct hso_serial *hso_dev;
  2740. struct tty_struct *tty;
  2741. int i;
  2742. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2743. if (serial_table[i] &&
  2744. (serial_table[i]->interface == interface)) {
  2745. hso_dev = dev2ser(serial_table[i]);
  2746. spin_lock_irq(&hso_dev->serial_lock);
  2747. tty = tty_kref_get(hso_dev->tty);
  2748. spin_unlock_irq(&hso_dev->serial_lock);
  2749. if (tty)
  2750. tty_hangup(tty);
  2751. mutex_lock(&hso_dev->parent->mutex);
  2752. tty_kref_put(tty);
  2753. hso_dev->parent->usb_gone = 1;
  2754. mutex_unlock(&hso_dev->parent->mutex);
  2755. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2756. }
  2757. }
  2758. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2759. if (network_table[i] &&
  2760. (network_table[i]->interface == interface)) {
  2761. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2762. /* hso_stop_net_device doesn't stop the net queue since
  2763. * traffic needs to start it again when suspended */
  2764. netif_stop_queue(dev2net(network_table[i])->net);
  2765. hso_stop_net_device(network_table[i]);
  2766. cancel_work_sync(&network_table[i]->async_put_intf);
  2767. cancel_work_sync(&network_table[i]->async_get_intf);
  2768. if (rfk) {
  2769. rfkill_unregister(rfk);
  2770. rfkill_destroy(rfk);
  2771. }
  2772. hso_free_net_device(network_table[i]);
  2773. }
  2774. }
  2775. }
  2776. /* Helper functions */
  2777. /* Get the endpoint ! */
  2778. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2779. int type, int dir)
  2780. {
  2781. int i;
  2782. struct usb_host_interface *iface = intf->cur_altsetting;
  2783. struct usb_endpoint_descriptor *endp;
  2784. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2785. endp = &iface->endpoint[i].desc;
  2786. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2787. (usb_endpoint_type(endp) == type))
  2788. return endp;
  2789. }
  2790. return NULL;
  2791. }
  2792. /* Get the byte that describes which ports are enabled */
  2793. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2794. {
  2795. int i;
  2796. struct usb_host_interface *iface = intf->cur_altsetting;
  2797. if (iface->extralen == 3) {
  2798. *ports = iface->extra[2];
  2799. return 0;
  2800. }
  2801. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2802. if (iface->endpoint[i].extralen == 3) {
  2803. *ports = iface->endpoint[i].extra[2];
  2804. return 0;
  2805. }
  2806. }
  2807. return -1;
  2808. }
  2809. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2810. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2811. struct usb_device *usb, gfp_t gfp)
  2812. {
  2813. int result;
  2814. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2815. usb_rcvintpipe(usb,
  2816. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2817. shared_int->shared_intr_buf,
  2818. 1,
  2819. intr_callback, shared_int,
  2820. shared_int->intr_endp->bInterval);
  2821. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2822. if (result)
  2823. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2824. result);
  2825. return result;
  2826. }
  2827. /* operations setup of the serial interface */
  2828. static const struct tty_operations hso_serial_ops = {
  2829. .open = hso_serial_open,
  2830. .close = hso_serial_close,
  2831. .write = hso_serial_write,
  2832. .write_room = hso_serial_write_room,
  2833. .ioctl = hso_serial_ioctl,
  2834. .set_termios = hso_serial_set_termios,
  2835. .chars_in_buffer = hso_serial_chars_in_buffer,
  2836. .tiocmget = hso_serial_tiocmget,
  2837. .tiocmset = hso_serial_tiocmset,
  2838. .unthrottle = hso_unthrottle
  2839. };
  2840. static struct usb_driver hso_driver = {
  2841. .name = driver_name,
  2842. .probe = hso_probe,
  2843. .disconnect = hso_disconnect,
  2844. .id_table = hso_ids,
  2845. .suspend = hso_suspend,
  2846. .resume = hso_resume,
  2847. .reset_resume = hso_resume,
  2848. .supports_autosuspend = 1,
  2849. };
  2850. static int __init hso_init(void)
  2851. {
  2852. int i;
  2853. int result;
  2854. /* put it in the log */
  2855. printk(KERN_INFO "hso: %s\n", version);
  2856. /* Initialise the serial table semaphore and table */
  2857. spin_lock_init(&serial_table_lock);
  2858. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2859. serial_table[i] = NULL;
  2860. /* allocate our driver using the proper amount of supported minors */
  2861. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2862. if (!tty_drv)
  2863. return -ENOMEM;
  2864. /* fill in all needed values */
  2865. tty_drv->magic = TTY_DRIVER_MAGIC;
  2866. tty_drv->owner = THIS_MODULE;
  2867. tty_drv->driver_name = driver_name;
  2868. tty_drv->name = tty_filename;
  2869. /* if major number is provided as parameter, use that one */
  2870. if (tty_major)
  2871. tty_drv->major = tty_major;
  2872. tty_drv->minor_start = 0;
  2873. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2874. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2875. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2876. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2877. tty_drv->init_termios = tty_std_termios;
  2878. hso_init_termios(&tty_drv->init_termios);
  2879. tty_set_operations(tty_drv, &hso_serial_ops);
  2880. /* register the tty driver */
  2881. result = tty_register_driver(tty_drv);
  2882. if (result) {
  2883. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2884. __func__, result);
  2885. return result;
  2886. }
  2887. /* register this module as an usb driver */
  2888. result = usb_register(&hso_driver);
  2889. if (result) {
  2890. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2891. result);
  2892. /* cleanup serial interface */
  2893. tty_unregister_driver(tty_drv);
  2894. return result;
  2895. }
  2896. /* done */
  2897. return 0;
  2898. }
  2899. static void __exit hso_exit(void)
  2900. {
  2901. printk(KERN_INFO "hso: unloaded\n");
  2902. tty_unregister_driver(tty_drv);
  2903. /* deregister the usb driver */
  2904. usb_deregister(&hso_driver);
  2905. }
  2906. /* Module definitions */
  2907. module_init(hso_init);
  2908. module_exit(hso_exit);
  2909. MODULE_AUTHOR(MOD_AUTHOR);
  2910. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2911. MODULE_LICENSE(MOD_LICENSE);
  2912. MODULE_INFO(Version, DRIVER_VERSION);
  2913. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2914. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2915. module_param(debug, int, S_IRUGO | S_IWUSR);
  2916. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2917. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2918. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2919. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2920. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2921. module_param(disable_net, int, S_IRUGO | S_IWUSR);